JacksEscape/input.c

93 lines
2.3 KiB
C
Raw Normal View History

2024-03-01 16:15:21 +08:00
2024-03-05 11:40:42 +08:00
#include "input.h"
#include "app.h"
2024-03-01 16:15:21 +08:00
#include <stdlib.h>
2024-03-04 15:05:21 +08:00
#include <stdio.h>
2024-03-01 16:15:21 +08:00
#include <windows.h>
2024-03-04 15:05:21 +08:00
const char *input_KeyNames[input_Key_Count] = {
"Left",
"Right",
"Up",
"Down",
"Jump",
"Attack",
"Spell",
"Use",
2024-03-05 15:20:32 +08:00
"Dash",
2024-04-23 14:48:01 +08:00
"Escape",
"Left Mouse Button",
"Right Mouse Button"};
2024-03-04 15:05:21 +08:00
2024-03-01 16:15:21 +08:00
void input_SetDefaultKeymap(System_Input *sys) {
2024-04-23 14:48:01 +08:00
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';
sys->systemKeymap[input_Key_Dash] = VK_OEM_1; // The ;: key on the US keyboard
sys->systemKeymap[input_Key_Escape] = VK_ESCAPE;
sys->systemKeymap[input_Key_LeftMouse] = VK_LBUTTON;
sys->systemKeymap[input_Key_RightMouse] = VK_RBUTTON;
2024-03-01 16:15:21 +08:00
}
2024-03-01 17:09:24 +08:00
System_Input *input_NewSystem(App *super) {
2024-03-01 16:15:21 +08:00
System_Input *sys = malloc(sizeof(System_Input));
memset(sys, 0, sizeof(System_Input));
sys->super = super;
input_SetDefaultKeymap(sys);
2024-03-01 16:15:21 +08:00
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
2024-03-26 14:07:49 +08:00
if (sys->keys[i] == JustReleased || sys->keys[i] == Released)
2024-03-01 16:15:21 +08:00
sys->keys[i] = JustPressed;
2024-03-26 14:07:49 +08:00
else
2024-03-01 16:15:21 +08:00
sys->keys[i] = Pressed;
} else {
// Released
if (sys->keys[i] == JustPressed || sys->keys[i] == Pressed)
sys->keys[i] = JustReleased;
else
sys->keys[i] = Released;
}
}
2024-03-04 15:05:21 +08:00
2024-04-22 05:46:05 +08:00
if (input_IsPressed(sys->keys[input_Key_Spell])) {
sys->super->timescale = 0.25;
} else {
sys->super->timescale = 1.0;
}
2024-03-04 15:05:21 +08:00
if (sys->keys[input_Key_Escape] == JustPressed) {
2024-04-22 05:28:50 +08:00
if (!sys->super->paused)
fprintf(stderr, "[input_Advance] Pausing\n");
else
fprintf(stderr, "[input_Advance] Unpausing\n");
sys->super->paused = !sys->super->paused;
2024-03-04 15:05:21 +08:00
}
2024-03-01 16:15:21 +08:00
}
2024-04-23 14:48:01 +08:00
Vec2 input_MousePosition(System_Input *sys) {
POINT point;
GetCursorPos(&point);
ScreenToClient((HWND)sys->super->window, &point);
return vec2(point.x, point.y);
}