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 this(PlatformLayer platformLayer)
: base(platformLayer, "Strawberry Sample Game!", 320, 180, 3)
: base(platformLayer, "Strawberry Sample Game!", 320, 180, 3, 1)
{
Controls.Init();
Scene = new Level();

View File

@ -34,7 +34,7 @@ namespace Strawberry
private bool* keyboardState;
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()
{
Game = this;
@ -89,23 +89,25 @@ namespace Strawberry
public void Run()
{
float msCounter = 0;
uint32 prevTick = 0;
while (true)
{
if (PlatformLayer.Closed())
return;
msCounter += PlatformLayer.Tick();
uint32 tick = PlatformLayer.Tick;
msCounter += (tick - prevTick);
prevTick = tick;
if (Time.FixedTimestep)
{
Time.RawDelta = Time.TargetDeltaTime;
while (msCounter >= Time.TargetMilliseconds)
{
msCounter -= Time.TargetMilliseconds;
PlatformLayer.UpdateInput();
Update();
Input.AfterUpdate();
msCounter -= Time.TargetMilliseconds;
}
}
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 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
public abstract void RenderBegin();
public abstract void RenderEnd();
//Update
public abstract uint32 Tick { get; } // Milliseconds since game launched
//Input
public abstract void UpdateInput();
public abstract bool PollKey(Keys key);
public abstract bool CapsLock { get; }
public abstract bool NumLock { get; }
public abstract bool PollKey(Keys key);
public abstract bool PollGamepadButton(int gamepadID, Buttons button);
public abstract float PollGamepadAxis(int gamepadID, Axes axis);
}

View File

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

View File

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