From f9455366f06b5f099d859ecbbb0f10b5edbeca55 Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Sun, 17 May 2020 18:16:32 -0700 Subject: [PATCH] Separated Input into its own static class --- src/Core/Game.bf | 44 ++---------------- src/Input/Input.bf | 91 ++++++++++++++++++++++++++++++++++++++ src/Input/VirtualAxis.bf | 14 +++--- src/Input/VirtualButton.bf | 8 ++-- 4 files changed, 106 insertions(+), 51 deletions(-) create mode 100644 src/Input/Input.bf diff --git a/src/Core/Game.bf b/src/Core/Game.bf index 5f60097..182bcac 100644 --- a/src/Core/Game.bf +++ b/src/Core/Game.bf @@ -32,7 +32,6 @@ namespace Strawberry private SDL.Window* window; private SDL.Surface* screen; private bool* keyboardState; - private SDL.SDL_GameController*[] gamepads; private int32 updateCounter; public this(String windowTitle, int32 width, int32 height, int32 windowScale, int gamepadLimit = 1) @@ -71,12 +70,8 @@ namespace Strawberry screen = SDL.GetWindowSurface(window); SDLImage.Init(.PNG | .JPG); SDLMixer.OpenAudio(44100, SDLMixer.MIX_DEFAULT_FORMAT, 2, 4096); - SDLTTF.Init(); - - this.gamepads = new SDL.SDL_GameController*[gamepadLimit]; - for (let i < this.gamepads.Count) - this.gamepads[i] = SDL.GameControllerOpen((int32)i); + Input.Init(gamepadLimit); } public ~this() @@ -96,7 +91,7 @@ namespace Strawberry delete VirtualInputs; } - delete gamepads; + Input.Dispose(); Game = null; } @@ -126,16 +121,15 @@ namespace Strawberry } else { - keyboardState = SDL.GetKeyboardState(null); - SDL.GameControllerUpdate(); - addTicks = Math.Min(addTicks, 20); // Limit catchup if (addTicks > 0) { for (int i < addTicks) { + Input.BeforeUpdate(); updateCounter++; Update(); + Input.AfterUpdate(); } Render(); } @@ -203,35 +197,5 @@ namespace Strawberry switchToScene = value; } } - - // Input - - public bool KeyCheck(SDL.Scancode key) - { - if (keyboardState == null) - return false; - return keyboardState[(int)key]; - } - - public bool GamepadButtonCheck(int gamepadID, SDL.SDL_GameControllerButton button) - { - if (gamepads == null) - return false; - return SDL.GameControllerGetButton(gamepads[gamepadID], button) == 1; - } - - public float GamepadAxisCheck(int gamepadID, SDL.SDL_GameControllerAxis axis) - { - if (gamepads == null) - return 0; - - let val = SDL.GameControllerGetAxis(gamepads[gamepadID], axis); - if (val == 0) - return 0; - else if (val > 0) - return val / 32767f; - else - return val / 32768f; - } } } diff --git a/src/Input/Input.bf b/src/Input/Input.bf new file mode 100644 index 0000000..fcf2e04 --- /dev/null +++ b/src/Input/Input.bf @@ -0,0 +1,91 @@ +using SDL2; +using System; +using System.Diagnostics; + +namespace Strawberry +{ + static public class Input + { + static private bool* keyboard; + static private bool[] previousKeyboard; + static private SDL.SDL_GameController*[] gamepads; + + static public void Init(int gamepadLimit) + { + keyboard = SDL.GetKeyboardState(null); + previousKeyboard = new bool[(int)SDL.Scancode.NUMSCANCODES]; + + gamepads = new SDL.SDL_GameController*[gamepadLimit]; + for (let i < gamepads.Count) + gamepads[i] = SDL.GameControllerOpen((int32)i); + } + + static public void Dispose() + { + delete previousKeyboard; + delete gamepads; + } + + static public void BeforeUpdate() + { + SDL.PumpEvents(); + SDL.GameControllerUpdate(); + } + + static public void AfterUpdate() + { + for (let i < previousKeyboard.Count) + previousKeyboard[i] = keyboard[i]; + } + + static public bool KeyCheck(SDL.Scancode key) + { + if (keyboard == null) + Debug.FatalError("Polling keyboard before Input.Init"); + + return keyboard[(int)key]; + } + + static public bool KeyPressed(SDL.Scancode key) + { + if (keyboard == null) + Debug.FatalError("Polling keyboard before Input.Init"); + + return keyboard[(int)key] && !previousKeyboard[(int)key]; + } + + static public bool KeyReleased(SDL.Scancode key) + { + if (keyboard == null) + Debug.FatalError("Polling keyboard before Input.Init"); + + return !keyboard[(int)key] && previousKeyboard[(int)key]; + } + + static public bool GamepadButtonCheck(int gamepadID, SDL.SDL_GameControllerButton button) + { + if (gamepads == null) + Debug.FatalError("Polling gamepad before Input.Init"); + if (gamepadID >= gamepads.Count) + Debug.FatalError("Gamepad index out of range (increase Game.gamepadLimit!"); + + return SDL.GameControllerGetButton(gamepads[gamepadID], button) == 1; + } + + static public float GamepadAxisCheck(int gamepadID, SDL.SDL_GameControllerAxis axis) + { + if (gamepads == null) + Debug.FatalError("Polling gamepad before Input.Init"); + if (gamepadID >= gamepads.Count) + Debug.FatalError("Gamepad index out of range (increase Game.gamepadLimit!"); + + let val = SDL.GameControllerGetAxis(gamepads[gamepadID], axis); + if (val == 0) + return 0; + else if (val > 0) + return val / 32767f; + else + return val / 32768f; + } + } +} diff --git a/src/Input/VirtualAxis.bf b/src/Input/VirtualAxis.bf index 591fe0d..6a22d09 100644 --- a/src/Input/VirtualAxis.bf +++ b/src/Input/VirtualAxis.bf @@ -152,9 +152,9 @@ namespace Strawberry public override void Update() { - if (Game.KeyCheck(PositiveKeycode)) + if (Input.KeyCheck(PositiveKeycode)) { - if (Game.KeyCheck(NegativeKeycode)) + if (Input.KeyCheck(NegativeKeycode)) { switch (OverlapBehavior) { @@ -181,7 +181,7 @@ namespace Strawberry value = 1; } } - else if (Game.KeyCheck(NegativeKeycode)) + else if (Input.KeyCheck(NegativeKeycode)) { turned = false; value = -1; @@ -222,9 +222,9 @@ namespace Strawberry public override void Update() { - if (Game.GamepadButtonCheck(GamepadID, PositiveButton)) + if (Input.GamepadButtonCheck(GamepadID, PositiveButton)) { - if (Game.GamepadButtonCheck(GamepadID, NegativeButton)) + if (Input.GamepadButtonCheck(GamepadID, NegativeButton)) { switch (OverlapBehavior) { @@ -251,7 +251,7 @@ namespace Strawberry value = 1; } } - else if (Game.GamepadButtonCheck(GamepadID, NegativeButton)) + else if (Input.GamepadButtonCheck(GamepadID, NegativeButton)) { turned = false; value = -1; @@ -289,7 +289,7 @@ namespace Strawberry { get { - let val = Game.GamepadAxisCheck(GamepadID, Axis); + let val = Input.GamepadAxisCheck(GamepadID, Axis); if (Math.Abs(val) < Deadzone) return 0; else diff --git a/src/Input/VirtualButton.bf b/src/Input/VirtualButton.bf index a1bbe40..4c43302 100644 --- a/src/Input/VirtualButton.bf +++ b/src/Input/VirtualButton.bf @@ -142,7 +142,7 @@ namespace Strawberry { get { - return Game.KeyCheck(Keycode); + return Input.KeyCheck(Keycode); } } } @@ -162,7 +162,7 @@ namespace Strawberry { get { - return Game.GamepadButtonCheck(GamepadID, Button); + return Input.GamepadButtonCheck(GamepadID, Button); } } } @@ -187,9 +187,9 @@ namespace Strawberry get { if (Condition == .GreaterThan) - return Game.GamepadAxisCheck(GamepadID, Axis) >= Threshold; + return Input.GamepadAxisCheck(GamepadID, Axis) >= Threshold; else - return Game.GamepadAxisCheck(GamepadID, Axis) <= Threshold; + return Input.GamepadAxisCheck(GamepadID, Axis) <= Threshold; } } }