mirror of
https://github.com/MaddyThorson/StrawberryBF.git
synced 2024-11-28 16:58:56 +08:00
Separated Input into its own static class
This commit is contained in:
parent
6bc89ac9d1
commit
f9455366f0
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
91
src/Input/Input.bf
Normal file
91
src/Input/Input.bf
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user