diff --git a/src/dusk/game.c b/src/dusk/game.c index d02d7df..7ae5628 100644 --- a/src/dusk/game.c +++ b/src/dusk/game.c @@ -26,9 +26,9 @@ void gameInit(void) { } void gameUpdate(void) { - overworldUpdate(); - uiTextboxUpdate(); - eventUpdate(); + // overworldUpdate(); + // uiTextboxUpdate(); + // eventUpdate(); consoleUpdate(); inputUpdate(); diff --git a/src/dusk/input.c b/src/dusk/input.c index 2c1720a..f4654e8 100644 --- a/src/dusk/input.c +++ b/src/dusk/input.c @@ -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) { diff --git a/src/dusksdl2/display/render.c b/src/dusksdl2/display/render.c index 6885b2d..5f5f4ab 100644 --- a/src/dusksdl2/display/render.c +++ b/src/dusksdl2/display/render.c @@ -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()); diff --git a/src/dusksdl2/input.c b/src/dusksdl2/input.c index 8a27389..229ca87 100644 --- a/src/dusksdl2/input.c +++ b/src/dusksdl2/input.c @@ -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; } \ No newline at end of file diff --git a/src/dusksdl2/main.c b/src/dusksdl2/main.c index 4ec4139..f46368b 100644 --- a/src/dusksdl2/main.c +++ b/src/dusksdl2/main.c @@ -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();