Cleanup. GL bindings

This commit is contained in:
Matt Thorson 2020-08-03 22:45:50 -07:00
parent 1bf3c1a62f
commit 7329e98beb
6 changed files with 2246 additions and 26 deletions

View File

@ -5,7 +5,7 @@ namespace Strawberry.Sample
public class SampleGame : Game public class SampleGame : Game
{ {
public this(PlatformLayer platformLayer) public this(PlatformLayer platformLayer)
: base(platformLayer, "Strawberry Sample Game!", 320, 180, 3) : base(platformLayer, "Strawberry Sample Game!", 320, 180, 3, 1)
{ {
Controls.Init(); Controls.Init();
Scene = new Level(); Scene = new Level();

View File

@ -34,7 +34,7 @@ namespace Strawberry
private bool* keyboardState; private bool* keyboardState;
private int32 updateCounter; private int32 updateCounter;
public this(PlatformLayer platformLayer, String windowTitle, int32 width, int32 height, int32 windowScale, int gamepadLimit = 1) public this(PlatformLayer platformLayer, String windowTitle, int32 width, int32 height, int32 windowScale, int gamepadLimit)
: base() : base()
{ {
Game = this; Game = this;
@ -89,23 +89,25 @@ namespace Strawberry
public void Run() public void Run()
{ {
float msCounter = 0; float msCounter = 0;
uint32 prevTick = 0;
while (true) while (true)
{ {
if (PlatformLayer.Closed()) if (PlatformLayer.Closed())
return; return;
msCounter += PlatformLayer.Tick(); uint32 tick = PlatformLayer.Tick;
msCounter += (tick - prevTick);
prevTick = tick;
if (Time.FixedTimestep) if (Time.FixedTimestep)
{ {
Time.RawDelta = Time.TargetDeltaTime; Time.RawDelta = Time.TargetDeltaTime;
while (msCounter >= Time.TargetMilliseconds) while (msCounter >= Time.TargetMilliseconds)
{ {
msCounter -= Time.TargetMilliseconds;
PlatformLayer.UpdateInput(); PlatformLayer.UpdateInput();
Update(); Update();
Input.AfterUpdate(); Input.AfterUpdate();
msCounter -= Time.TargetMilliseconds;
} }
} }
else else

2205
src/PlatformLayer/GL.bf Normal file

File diff suppressed because it is too large Load Diff

View File

@ -4,18 +4,20 @@ namespace Strawberry
public abstract class PlatformLayer public abstract class PlatformLayer
{ {
public abstract void Init(); public abstract void Init();
public abstract uint32 Tick(); // Returns milliseconds since last tick public abstract bool Closed(); // If the game window has been closed
public abstract bool Closed(); // If the game window has been closed
//Rendering //Rendering
public abstract void RenderBegin(); public abstract void RenderBegin();
public abstract void RenderEnd(); public abstract void RenderEnd();
//Update
public abstract uint32 Tick { get; } // Milliseconds since game launched
//Input //Input
public abstract void UpdateInput(); public abstract void UpdateInput();
public abstract bool PollKey(Keys key);
public abstract bool CapsLock { get; } public abstract bool CapsLock { get; }
public abstract bool NumLock { get; } public abstract bool NumLock { get; }
public abstract bool PollKey(Keys key);
public abstract bool PollGamepadButton(int gamepadID, Buttons button); public abstract bool PollGamepadButton(int gamepadID, Buttons button);
public abstract float PollGamepadAxis(int gamepadID, Axes axis); public abstract float PollGamepadAxis(int gamepadID, Axes axis);
} }

View File

@ -11,6 +11,7 @@ namespace Strawberry
private SDL.Rect screenRect; private SDL.Rect screenRect;
private SDL.Renderer* renderer; private SDL.Renderer* renderer;
private SDL.SDL_GLContext glContext; private SDL.SDL_GLContext glContext;
private uint glProgram;
private SDL.SDL_GameController*[] gamepads; private SDL.SDL_GameController*[] gamepads;
private bool* keyboard; private bool* keyboard;
@ -39,8 +40,8 @@ namespace Strawberry
//Graphics //Graphics
{ {
window = SDL.CreateWindow(Game.Title, .Centered, .Centered, screenRect.w, screenRect.h, .Shown);
screenRect = SDL.Rect(0, 0, (int32)(Game.Width * Game.WindowScale), (int32)(Game.Height * Game.WindowScale)); screenRect = SDL.Rect(0, 0, (int32)(Game.Width * Game.WindowScale), (int32)(Game.Height * Game.WindowScale));
window = SDL.CreateWindow(Game.Title, .Centered, .Centered, screenRect.w, screenRect.h, .Shown | .OpenGL);
renderer = SDL.CreateRenderer(window, -1, .Accelerated); renderer = SDL.CreateRenderer(window, -1, .Accelerated);
screen = SDL.GetWindowSurface(window); screen = SDL.GetWindowSurface(window);
SDLImage.Init(.PNG | .JPG); SDLImage.Init(.PNG | .JPG);
@ -48,6 +49,9 @@ namespace Strawberry
glContext = SDL.GL_CreateContext(window); glContext = SDL.GL_CreateContext(window);
SDL.GL_SetSwapInterval(1); SDL.GL_SetSwapInterval(1);
GL.Init(=> SdlGetProcAddress);
glProgram = GL.glCreateProgram();
} }
//Audio //Audio
@ -64,21 +68,19 @@ namespace Strawberry
} }
} }
static void* SdlGetProcAddress(StringView string)
{
return SDL.SDL_GL_GetProcAddress(string.ToScopeCStr!());
}
public ~this() public ~this()
{ {
delete gamepads; delete gamepads;
}
public override void RenderBegin() GL.glDeleteProgram(glProgram);
{ SDL.GL_DeleteContext(glContext);
SDL.SetRenderDrawColor(renderer, Game.ClearColor.R, Game.ClearColor.G, Game.ClearColor.B, Game.ClearColor.A); SDL.DestroyWindow(window);
SDL.RenderClear(renderer); SDL.Quit();
SDL.RenderSetScale(renderer, Game.WindowScale, Game.WindowScale);
}
public override void RenderEnd()
{
SDL.RenderPresent(renderer);
} }
public override bool Closed() public override bool Closed()
@ -87,6 +89,20 @@ namespace Strawberry
return (SDL.PollEvent(out event) != 0 && event.type == .Quit); return (SDL.PollEvent(out event) != 0 && event.type == .Quit);
} }
public override uint32 Tick => SDL.GetTicks();
public override void RenderBegin()
{
GL.glClearColor(Game.ClearColor.Rf, Game.ClearColor.Gf, Game.ClearColor.Bf, Game.ClearColor.Af);
GL.glClear(GL.GL_COLOR_BUFFER_BIT);
GL.glCreateProgram();
}
public override void RenderEnd()
{
SDL.GL_SwapWindow(window);
}
public override void UpdateInput() public override void UpdateInput()
{ {
SDL.PumpEvents(); SDL.PumpEvents();
@ -124,10 +140,5 @@ namespace Strawberry
else else
return val / 32768f; return val / 32768f;
} }
public override uint32 Tick()
{
return SDL.GetTicks();
}
} }
} }

View File

@ -2,7 +2,7 @@ namespace Strawberry
{ {
static public class Time static public class Time
{ {
static public bool FixedTimestep = true; static public bool FixedTimestep = false;
static public float TargetDeltaTime = 1 / 60f; static public float TargetDeltaTime = 1 / 60f;
static public float Elapsed; static public float Elapsed;
static public float PreviousElapsed; static public float PreviousElapsed;