From 1bc750490a624027ca4ad3c2fffeaa9a02087f34 Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Wed, 6 May 2020 21:54:41 -0700 Subject: [PATCH] Gamepad input started --- src/Game.bf | 95 +++++++++++++++++++++++++------------- src/Input/Input.bf | 24 ---------- src/Input/VirtualAxis.bf | 8 ++-- src/Input/VirtualButton.bf | 58 +++++++++++++++++++++-- src/Input/VirtualInput.bf | 3 ++ 5 files changed, 122 insertions(+), 66 deletions(-) delete mode 100644 src/Input/Input.bf diff --git a/src/Game.bf b/src/Game.bf index 5f9e4b9..2978107 100644 --- a/src/Game.bf +++ b/src/Game.bf @@ -25,12 +25,12 @@ namespace Strawberry 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 bool* keyboardState; + private SDL.SDL_GameController* gamepad; private int32 updateCounter; public this(String windowTitle, int32 width, int32 height) @@ -44,6 +44,31 @@ namespace Strawberry Height = height; screenRect = SDL.Rect(0, 0, width, height); + + String exePath = scope .(); + Environment.GetExecutableFilePath(exePath); + String exeDir = scope .(); + Path.GetDirectoryPath(exePath, exeDir); + Directory.SetCurrentDirectory(exeDir); + + SDL.Init(.Video | .Events | .Audio | .GameController); + 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(); + + gamepad = SDL.GameControllerOpen(0); } public ~this() @@ -83,8 +108,8 @@ namespace Strawberry } else { - PreviousKeyboardState = KeyboardState; - KeyboardState = SDL.GetKeyboardState(null); + keyboardState = SDL.GetKeyboardState(null); + SDL.GameControllerUpdate(); addTicks = Math.Min(addTicks, 20); // Limit catchup if (addTicks > 0) @@ -104,32 +129,6 @@ namespace Strawberry } } - public virtual void Init() - { - 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 @@ -156,14 +155,14 @@ namespace Strawberry { SDL.SetRenderDrawColor(Renderer, 0, 0, 0, 255); SDL.RenderClear(Renderer); - if (Scene != null) - Scene.Draw(); + Draw(); SDL.RenderPresent(Renderer); } public virtual void Draw() { - + if (Scene != null) + Scene.Draw(); } public Scene Scene @@ -180,5 +179,35 @@ namespace Strawberry switchToScene = value; } } + + // Input + + public bool KeyCheck(SDL.Scancode key) + { + if (keyboardState == null) + return false; + return keyboardState[(int)key]; + } + + public bool GamepadButtonCheck(SDL.SDL_GameControllerButton button) + { + if (gamepad == null) + return false; + return SDL.GameControllerGetButton(gamepad, button) == 1; + } + + public float GamepadAxisCheck(SDL.SDL_GameControllerAxis axis) + { + if (gamepad == null) + return 0; + + let val = SDL.GameControllerGetAxis(gamepad, 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 deleted file mode 100644 index ec0718d..0000000 --- a/src/Input/Input.bf +++ /dev/null @@ -1,24 +0,0 @@ -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 8a82bca..9058769 100644 --- a/src/Input/VirtualAxis.bf +++ b/src/Input/VirtualAxis.bf @@ -5,8 +5,6 @@ namespace Strawberry { public class VirtualAxis : VirtualInput { - public enum OverlapBehaviors { TakeNewer, TakeOlder, CancelOut } - public float Value { get; private set; } public int Valuei { get; private set; } public bool Pressed { get; private set; } @@ -141,9 +139,9 @@ namespace Strawberry public override void Update() { - if (Input.KeyCheck(PositiveKeycode)) + if (Game.KeyCheck(PositiveKeycode)) { - if (Input.KeyCheck(NegativeKeycode)) + if (Game.KeyCheck(NegativeKeycode)) { switch (OverlapBehavior) { @@ -170,7 +168,7 @@ namespace Strawberry value = 1; } } - else if (Input.KeyCheck(NegativeKeycode)) + else if (Game.KeyCheck(NegativeKeycode)) { turned = false; value = -1; diff --git a/src/Input/VirtualButton.bf b/src/Input/VirtualButton.bf index d658bb8..5e67580 100644 --- a/src/Input/VirtualButton.bf +++ b/src/Input/VirtualButton.bf @@ -1,6 +1,7 @@ using System.Collections; using System; using System.Diagnostics; +using SDL2; namespace Strawberry { @@ -84,12 +85,18 @@ namespace Strawberry // Setup Calls - public VirtualButton Key(SDL2.SDL.Scancode keycode) + public VirtualButton Key(SDL.Scancode keycode) { nodes.Add(new KeyboardKey(keycode)); return this; } + public VirtualButton Button(SDL.SDL_GameControllerButton button) + { + nodes.Add(new GamepadButton(button)); + return this; + } + public VirtualButton PressBuffer(float time) { pressBuffer = time; @@ -118,9 +125,9 @@ namespace Strawberry private class KeyboardKey : Node { - public SDL2.SDL.Scancode Keycode; + public SDL.Scancode Keycode; - public this(SDL2.SDL.Scancode keycode) + public this(SDL.Scancode keycode) { Keycode = keycode; } @@ -129,7 +136,50 @@ namespace Strawberry { get { - return Input.KeyCheck(Keycode); + return Game.KeyCheck(Keycode); + } + } + } + + private class GamepadButton : Node + { + public SDL.SDL_GameControllerButton Button; + + public this(SDL.SDL_GameControllerButton button) + { + Button = button; + } + + override public bool Check + { + get + { + return Game.GamepadButtonCheck(Button); + } + } + } + + private class GamepadAxis : Node + { + public SDL.SDL_GameControllerAxis Axis; + public float Threshold; + public ThresholdConditions Condition; + + public this(SDL.SDL_GameControllerAxis axis, float threshold, ThresholdConditions condition = .GreaterThan) + { + Axis = axis; + Threshold = threshold; + Condition = condition; + } + + override public bool Check + { + get + { + if (Condition == .GreaterThan) + return Game.GamepadAxisCheck(Axis) >= Threshold; + else + return Game.GamepadAxisCheck(Axis) <= Threshold; } } } diff --git a/src/Input/VirtualInput.bf b/src/Input/VirtualInput.bf index c00f89f..2ff5323 100644 --- a/src/Input/VirtualInput.bf +++ b/src/Input/VirtualInput.bf @@ -2,6 +2,9 @@ namespace Strawberry { public abstract class VirtualInput { + public enum OverlapBehaviors { TakeNewer, TakeOlder, CancelOut } + public enum ThresholdConditions { GreaterThan, LessThan } + public this() { Game.VirtualInputs.Add(this);