Finish input
This commit is contained in:
parent
fcb2997e69
commit
658eb5b60c
47
Input.c
Normal file
47
Input.c
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
|
||||||
|
#include "Input.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
|
||||||
|
void input_SetDefaultKeymap(System_Input *sys) {
|
||||||
|
sys->systemKeymap[input_Key_Up] = 'W';
|
||||||
|
sys->systemKeymap[input_Key_Left] = 'A';
|
||||||
|
sys->systemKeymap[input_Key_Down] = 'S';
|
||||||
|
sys->systemKeymap[input_Key_Right] = 'D';
|
||||||
|
sys->systemKeymap[input_Key_Jump] = VK_SPACE;
|
||||||
|
sys->systemKeymap[input_Key_Attack] = 'J';
|
||||||
|
sys->systemKeymap[input_Key_Spell] = 'K';
|
||||||
|
sys->systemKeymap[input_Key_Use] = 'L';
|
||||||
|
}
|
||||||
|
|
||||||
|
System_Input *input_NewSystem() {
|
||||||
|
System_Input *sys = malloc(sizeof(System_Input));
|
||||||
|
return sys;
|
||||||
|
}
|
||||||
|
|
||||||
|
void input_DeleteSystem(System_Input *sys) {
|
||||||
|
free(sys);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void input_Advance(System_Input *sys) {
|
||||||
|
for (int i = 0; i < input_Key_Count; i++) {
|
||||||
|
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate
|
||||||
|
// Checks the most significiant bit (of the SHORT returned)
|
||||||
|
if ((GetAsyncKeyState(sys->systemKeymap[i]) & 0x8000) != 0) {
|
||||||
|
// Pressed
|
||||||
|
if (sys->keys[i] == JustReleased || sys->keys[i] == Released)
|
||||||
|
sys->keys[i] = JustPressed;
|
||||||
|
else
|
||||||
|
sys->keys[i] = Pressed;
|
||||||
|
} else {
|
||||||
|
// Released
|
||||||
|
if (sys->keys[i] == JustPressed || sys->keys[i] == Pressed)
|
||||||
|
sys->keys[i] = JustReleased;
|
||||||
|
else
|
||||||
|
sys->keys[i] = Released;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
Input.h
32
Input.h
@ -22,6 +22,18 @@ typedef enum {
|
|||||||
input_Key_Count
|
input_Key_Count
|
||||||
} input_Key;
|
} input_Key;
|
||||||
|
|
||||||
|
// Names for input_Key
|
||||||
|
const char *input_KeyNames[input_Key_Count] = {
|
||||||
|
"Left",
|
||||||
|
"Right",
|
||||||
|
"Up",
|
||||||
|
"Down",
|
||||||
|
"Jump",
|
||||||
|
"Attack",
|
||||||
|
"Spell",
|
||||||
|
"Use"};
|
||||||
|
|
||||||
|
|
||||||
// States a key might in
|
// States a key might in
|
||||||
typedef enum {
|
typedef enum {
|
||||||
Pressed,
|
Pressed,
|
||||||
@ -30,11 +42,29 @@ typedef enum {
|
|||||||
JustReleased
|
JustReleased
|
||||||
} input_KeyState;
|
} input_KeyState;
|
||||||
|
|
||||||
|
static inline bool input_IsPressed(input_KeyState state) { return state == Pressed || state == JustPressed; }
|
||||||
|
static inline bool input_IsReleased(input_KeyState state) { return state == Released || state == JustReleased; }
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
input_KeyState keys[input_Key_Count]; // States of keys
|
input_KeyState keys[input_Key_Count]; // States of keys
|
||||||
|
unsigned int systemKeymap[input_Key_Count]; // Which system key (VK code) this function uses
|
||||||
|
// https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
|
||||||
} System_Input;
|
} System_Input;
|
||||||
|
|
||||||
|
|
||||||
|
// Creates a new input system.
|
||||||
|
// Uses a default keymap
|
||||||
|
System_Input *input_NewSystem();
|
||||||
|
void input_DeleteSystem(System_Input *sys);
|
||||||
|
|
||||||
|
// Sets the keymaps to default.
|
||||||
|
void input_SetDefaultKeymap(System_Input *sys);
|
||||||
|
|
||||||
|
// Update key states
|
||||||
|
void input_Advance(System_Input *sys);
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user