Console print input test

This commit is contained in:
2025-08-11 23:30:17 -05:00
parent a70cd7b4d2
commit e946b74bd0
5 changed files with 71 additions and 9 deletions

View File

@@ -26,9 +26,9 @@ void gameInit(void) {
}
void gameUpdate(void) {
overworldUpdate();
uiTextboxUpdate();
eventUpdate();
// overworldUpdate();
// uiTextboxUpdate();
// eventUpdate();
consoleUpdate();
inputUpdate();

View File

@@ -18,11 +18,6 @@ void inputInit(void) {
void inputUpdate(void) {
INPUT.previous = INPUT.current;
INPUT.current = inputStateGet();
#if DUSK_KEYBOARD_SUPPORT == 1
#endif
}
bool_t inputIsDown(const uint8_t bind) {

View File

@@ -31,7 +31,8 @@ errorret_t renderInit(void) {
SDL_WINDOWPOS_UNDEFINED,
RENDER_WINDOW_WIDTH_DEFAULT,
RENDER_WINDOW_HEIGHT_DEFAULT,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI |
SDL_INIT_GAMECONTROLLER
);
if(!RENDER_WINDOW) {
errorThrow("SDL_CreateWindow failed: %s", SDL_GetError());

View File

@@ -8,8 +8,65 @@
#include "dusksdl2.h"
#include "input.h"
typedef struct {
const SDL_GameControllerButton button;
const uint8_t bind;
} inputsdlbuttonmap_t;
typedef struct {
SDL_Scancode code;
uint8_t bind;
} inputsdlkbmap_t;
inputsdlbuttonmap_t INPUT_SDL_BUTTON_MAP[] = {
{ SDL_CONTROLLER_BUTTON_DPAD_UP, INPUT_BIND_UP },
{ SDL_CONTROLLER_BUTTON_DPAD_DOWN, INPUT_BIND_DOWN },
{ SDL_CONTROLLER_BUTTON_DPAD_LEFT, INPUT_BIND_LEFT },
{ SDL_CONTROLLER_BUTTON_DPAD_RIGHT, INPUT_BIND_RIGHT },
{ SDL_CONTROLLER_BUTTON_A, INPUT_BIND_ACTION },
{ SDL_CONTROLLER_BUTTON_B, INPUT_BIND_CANCEL },
{ 0, 0 }
};
inputsdlkbmap_t INPUT_SDL_KEYBOARD_MAP[] = {
{ SDL_SCANCODE_W, INPUT_BIND_UP },
{ SDL_SCANCODE_S, INPUT_BIND_DOWN },
{ SDL_SCANCODE_A, INPUT_BIND_LEFT },
{ SDL_SCANCODE_D, INPUT_BIND_RIGHT },
{ SDL_SCANCODE_SPACE, INPUT_BIND_ACTION },
{ SDL_SCANCODE_ESCAPE, INPUT_BIND_CANCEL },
{ 0, 0 }
};
uint8_t inputStateGet() {
uint8_t state = 0;
// Get gamepad state.
for(int32_t i = 0; i < SDL_NumJoysticks(); i++) {
if(!SDL_IsGameController(i)) continue;
SDL_GameController *controller = SDL_GameControllerOpen(i);
if(!controller) continue;
inputsdlbuttonmap_t *map = INPUT_SDL_BUTTON_MAP;
do {
if(SDL_GameControllerGetButton(controller, map->button)) {
state |= map->bind;
}
map++;
} while(map->button != 0);
}
// Get keyboard state.
const uint8_t *keyboardState = SDL_GetKeyboardState(NULL);
inputsdlkbmap_t *kbmap = INPUT_SDL_KEYBOARD_MAP;
do {
if(keyboardState[kbmap->code]) {
state |= kbmap->bind;
}
kbmap++;
} while(kbmap->code != 0);
return state;
}

View File

@@ -7,6 +7,8 @@
#include "display/render.h"
#include "game.h"
#include "console/console.h"
#include "input.h"
#define mainError(ret) \
if((ret).code != ERROR_OK) { \
@@ -23,6 +25,13 @@ int main(int argc, char *argv[]) {
while(RENDER_RUNNING) {
gameUpdate();
mainError(renderDraw());
if(inputPressed(INPUT_BIND_UP)) consolePrint("Up pressed");
if(inputPressed(INPUT_BIND_DOWN)) consolePrint("Down pressed");
if(inputPressed(INPUT_BIND_LEFT)) consolePrint("Left pressed");
if(inputPressed(INPUT_BIND_RIGHT)) consolePrint("Right pressed");
if(inputPressed(INPUT_BIND_ACTION)) consolePrint("Action pressed");
if(inputPressed(INPUT_BIND_CANCEL)) consolePrint("Cancel pressed");
}
gameDispose();