From 0a709b5788b9f584156d93893d904cda725cbeab Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Wed, 6 May 2020 21:13:34 -0700 Subject: [PATCH] Replaced SDLApp code in Game, no longer extends it --- src/Components/Timer.bf | 6 +- src/Game.bf | 121 +++++++++++++++++++++++++++++++------ src/Input/Input.bf | 24 ++++++++ src/Input/VirtualAxis.bf | 6 +- src/Input/VirtualButton.bf | 2 +- src/Physics/JumpThru.bf | 6 +- src/Physics/Solid.bf | 8 +-- src/Static/Draw.bf | 4 +- 8 files changed, 142 insertions(+), 35 deletions(-) create mode 100644 src/Input/Input.bf diff --git a/src/Components/Timer.bf b/src/Components/Timer.bf index 879de3d..b3bb3bf 100644 --- a/src/Components/Timer.bf +++ b/src/Components/Timer.bf @@ -8,7 +8,7 @@ namespace Strawberry private float value; public Action OnComplete ~ delete _; - public bool DestroyOnComplete; + public bool RemoveOnComplete; public this() : base(false, false) @@ -21,7 +21,7 @@ namespace Strawberry { Value = value; OnComplete = onComplete; - DestroyOnComplete = destroyOnComplete; + RemoveOnComplete = destroyOnComplete; } public float Value @@ -49,7 +49,7 @@ namespace Strawberry Active = false; OnComplete?.Invoke(); - if (DestroyOnComplete) + if (RemoveOnComplete) RemoveSelf(); } } diff --git a/src/Game.bf b/src/Game.bf index e734b33..5f9e4b9 100644 --- a/src/Game.bf +++ b/src/Game.bf @@ -1,6 +1,10 @@ using SDL2; using System; using System.Collections; +using System.Reflection; +using System.IO; +using System.Diagnostics; +using System.Threading; namespace Strawberry { @@ -9,14 +13,25 @@ namespace Strawberry static public Game Game; } - public class Game : SDLApp + public class Game { - public List VirtualInputs; - + public readonly List VirtualInputs; + public readonly String Title; + public readonly int Width; + public readonly int Height; + private Scene scene; private Scene switchToScene; private bool updating; + + public SDL.Renderer* Renderer { get; private set; } + public bool* KeyboardState { get; private set; } + public bool* PreviousKeyboardState { get; private set; } + private SDL.Rect screenRect; + private SDL.Window* window; + private SDL.Surface* screen; + private int32 updateCounter; public this(String windowTitle, int32 width, int32 height) : base() @@ -24,9 +39,9 @@ namespace Strawberry Game = this; VirtualInputs = new List(); - mTitle.Set(windowTitle); - mWidth = width; - mHeight = height; + Title = windowTitle; + Width = width; + Height = height; screenRect = SDL.Rect(0, 0, width, height); } @@ -44,14 +59,82 @@ namespace Strawberry Game = null; } - public new virtual void Init() + public void Run() { - base.Init(); + Stopwatch sw = scope .(); + sw.Start(); + int curPhysTickCount = 0; + + while (true) + { + SDL.Event event; + if (SDL.PollEvent(out event) != 0 && event.type == .Quit) + return; + + // Fixed 60 Hz update + double msPerTick = 1000 / 60.0; + int newPhysTickCount = (int)(sw.ElapsedMilliseconds / msPerTick); + + int addTicks = newPhysTickCount - curPhysTickCount; + if (curPhysTickCount == 0) + { + // Initial render + Render(); + } + else + { + PreviousKeyboardState = KeyboardState; + KeyboardState = SDL.GetKeyboardState(null); + + addTicks = Math.Min(addTicks, 20); // Limit catchup + if (addTicks > 0) + { + for (int i < addTicks) + { + updateCounter++; + Update(); + } + Render(); + } + else + Thread.Sleep(1); + } + + curPhysTickCount = newPhysTickCount; + } } - public override void Update() + public virtual void Init() { - base.Update(); + String exePath = scope .(); + Environment.GetExecutableFilePath(exePath); + String exeDir = scope .(); + Path.GetDirectoryPath(exePath, exeDir); + Directory.SetCurrentDirectory(exeDir); + + SDL.Init(.Video | .Events | .Audio); + SDL.EventState(.JoyAxisMotion, .Disable); + SDL.EventState(.JoyBallMotion, .Disable); + SDL.EventState(.JoyHatMotion, .Disable); + SDL.EventState(.JoyButtonDown, .Disable); + SDL.EventState(.JoyButtonUp, .Disable); + SDL.EventState(.JoyDeviceAdded, .Disable); + SDL.EventState(.JoyDeviceRemoved, .Disable); + + window = SDL.CreateWindow(Title, .Centered, .Centered, screenRect.w, screenRect.h, .Shown); + Renderer = SDL.CreateRenderer(window, -1, .Accelerated); + screen = SDL.GetWindowSurface(window); + SDLImage.Init(.PNG | .JPG); + SDLMixer.OpenAudio(44100, SDLMixer.MIX_DEFAULT_FORMAT, 2, 4096); + + SDLTTF.Init(); + } + + public virtual void Update() + { + //Input + for (var i in VirtualInputs) + i.Update(); //Switch scenes if (switchToScene != scene) @@ -62,9 +145,6 @@ namespace Strawberry scene.Started(); } - for (var i in VirtualInputs) - i.Update(); - if (scene != null) scene.Update(); @@ -72,15 +152,18 @@ namespace Strawberry Time.Elapsed += Time.Delta; } - public override void Draw() + public void Render() { - base.Draw(); - - SDL2.SDL.SetRenderDrawColor(mRenderer, 0, 0, 0, 255); - SDL2.SDL.RenderFillRect(mRenderer, &screenRect); - + SDL.SetRenderDrawColor(Renderer, 0, 0, 0, 255); + SDL.RenderClear(Renderer); if (Scene != null) Scene.Draw(); + SDL.RenderPresent(Renderer); + } + + public virtual void Draw() + { + } public Scene Scene diff --git a/src/Input/Input.bf b/src/Input/Input.bf new file mode 100644 index 0000000..ec0718d --- /dev/null +++ b/src/Input/Input.bf @@ -0,0 +1,24 @@ +using SDL2; + +namespace Strawberry +{ + static public class Input + { + static public bool KeyCheck(SDL.Scancode key) + { + if (Game.KeyboardState == null) + return false; + return Game.KeyboardState[(int)key]; + } + + static public bool KeyPressed(SDL.Scancode key) + { + return KeyCheck(key) && (Game.PreviousKeyboardState == null || !Game.PreviousKeyboardState[(int)key]); + } + + static public bool KeyReleased(SDL.Scancode key) + { + return (Game.PreviousKeyboardState != null && Game.PreviousKeyboardState[(int)key]) && !KeyCheck(key); + } + } +} diff --git a/src/Input/VirtualAxis.bf b/src/Input/VirtualAxis.bf index f305ce5..8a82bca 100644 --- a/src/Input/VirtualAxis.bf +++ b/src/Input/VirtualAxis.bf @@ -141,9 +141,9 @@ namespace Strawberry public override void Update() { - if (Game.IsKeyDown(PositiveKeycode)) + if (Input.KeyCheck(PositiveKeycode)) { - if (Game.IsKeyDown(NegativeKeycode)) + if (Input.KeyCheck(NegativeKeycode)) { switch (OverlapBehavior) { @@ -170,7 +170,7 @@ namespace Strawberry value = 1; } } - else if (Game.IsKeyDown(NegativeKeycode)) + else if (Input.KeyCheck(NegativeKeycode)) { turned = false; value = -1; diff --git a/src/Input/VirtualButton.bf b/src/Input/VirtualButton.bf index fb2f92f..d658bb8 100644 --- a/src/Input/VirtualButton.bf +++ b/src/Input/VirtualButton.bf @@ -129,7 +129,7 @@ namespace Strawberry { get { - return Game.IsKeyDown(Keycode); + return Input.KeyCheck(Keycode); } } } diff --git a/src/Physics/JumpThru.bf b/src/Physics/JumpThru.bf index 18f1bf4..0db1b1f 100644 --- a/src/Physics/JumpThru.bf +++ b/src/Physics/JumpThru.bf @@ -26,7 +26,7 @@ namespace Strawberry for (var a in riders) { a.MoveExactX(amount); - a.Pushed += Point.UnitX * amount; + a.MovedByGeometry += Point.UnitX * amount; } } else @@ -47,7 +47,7 @@ namespace Strawberry { let move = (Top + amount) - a.Bottom; a.MoveExactY(move); - a.Pushed += Point.UnitY * move; + a.MovedByGeometry += Point.UnitY * move; } } Y += amount; @@ -59,7 +59,7 @@ namespace Strawberry for (var a in riders) { a.MoveExactY(amount); - a.Pushed += Point.UnitY * amount; + a.MovedByGeometry += Point.UnitY * amount; } Collidable = true; diff --git a/src/Physics/Solid.bf b/src/Physics/Solid.bf index b1064b0..1ea1a4b 100644 --- a/src/Physics/Solid.bf +++ b/src/Physics/Solid.bf @@ -45,13 +45,13 @@ namespace Strawberry else move = Left - a.Right; a.MoveExactX(move, scope => a.Squish, this); - a.Pushed += Point.UnitX * move; + a.MovedByGeometry += Point.UnitX * move; } else if (riders.Contains(a)) { //Carry a.MoveExactX(amount); - a.Pushed += Point.UnitX * amount; + a.MovedByGeometry += Point.UnitX * amount; } } @@ -81,13 +81,13 @@ namespace Strawberry else move = Top - a.Bottom; a.MoveExactY(move, scope => a.Squish, this); - a.Pushed += Point.UnitY * move; + a.MovedByGeometry += Point.UnitY * move; } else if (riders.Contains(a)) { //Carry a.MoveExactY(amount); - a.Pushed += Point.UnitY * amount; + a.MovedByGeometry += Point.UnitY * amount; } } diff --git a/src/Static/Draw.bf b/src/Static/Draw.bf index c069159..c7fef75 100644 --- a/src/Static/Draw.bf +++ b/src/Static/Draw.bf @@ -4,8 +4,8 @@ namespace Strawberry { static public void Rect(int x, int y, int w, int h, SDL2.SDL.Color color) { - SDL2.SDL.SetRenderDrawColor(Game.mRenderer, color.r, color.g, color.b, color.a); - SDL2.SDL.RenderFillRect(Game.mRenderer, &SDL2.SDL.Rect((int32)x, (int32)y, (int32)w, (int32)h)); + SDL2.SDL.SetRenderDrawColor(Game.Renderer, color.r, color.g, color.b, color.a); + SDL2.SDL.RenderFillRect(Game.Renderer, &SDL2.SDL.Rect((int32)x, (int32)y, (int32)w, (int32)h)); } static public void Rect(Rect rect, SDL2.SDL.Color color)