Gamepad input started

This commit is contained in:
Matt Thorson 2020-05-06 21:54:41 -07:00
parent 0a709b5788
commit 1bc750490a
5 changed files with 122 additions and 66 deletions

View File

@ -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;
}
}
}

View File

@ -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);
}
}
}

View File

@ -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;

View File

@ -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;
}
}
}

View File

@ -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);