diff --git a/src/input.c b/src/input.c index 259a294..1f30a7f 100644 --- a/src/input.c +++ b/src/input.c @@ -99,6 +99,37 @@ get_event_key(SDL_Event *event) return key; } +static Uint64 +get_event_button(SDL_Event *event) +{ + Uint64 key; + switch (event->jbutton.button) { + case SDL_CONTROLLER_BUTTON_A: + key = KEY_NUM1 & KEY_ENTER; break; + case SDL_CONTROLLER_BUTTON_X: + key = KEY_NUM2; break; + case SDL_CONTROLLER_BUTTON_Y: + key = KEY_NUM3; break; + case SDL_CONTROLLER_BUTTON_B: + key = KEY_NUM4; break; + case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: + key = KEY_NUM5; break; + case SDL_CONTROLLER_BUTTON_START: + key = KEY_ESC; break; + case SDL_CONTROLLER_AXIS_TRIGGERRIGHT: + key = KEY_SPACE; break; + case SDL_CONTROLLER_BUTTON_INVALID: + default: + key = 0; break; + } + return key; +} + +static Uint64 +get_axis_motion(SDL_Event *event) +{ +} + static Uint32 get_event_modkey(SDL_Event *event) { @@ -173,6 +204,12 @@ input_handle_event(Input *input, SDL_Event *event) input->mouseX = event->motion.x; input->mouseY = event->motion.y; } + else if (event->type == SDL_CONTROLLERBUTTONDOWN) { + input->keyState |= get_event_button(event); + } + else if (event->type == SDL_CONTROLLERBUTTONUP) { + input->keyState &= ~get_event_button(event); + } } bool diff --git a/src/main.c b/src/main.c index cc206e4..c8fead2 100644 --- a/src/main.c +++ b/src/main.c @@ -154,6 +154,7 @@ static Screen *scoreScreen = NULL; static Sprite *new_skill_tooltip = NULL; static Sprite *howto_tooltip = NULL; static Sprite *new_artifact_tooltip = NULL; +static SDL_GameController *gController = NULL; static unsigned int cLevel = 1; static float deltaTime = 1.0; static double renderScale = 1.0; @@ -183,7 +184,7 @@ bool initSDL(void) { int imgFlags = IMG_INIT_PNG; - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) { error("Could not initiate SDL2: %s", SDL_GetError()); return false; @@ -209,6 +210,16 @@ bool initSDL(void) return false; } + if (SDL_NumJoysticks() > 0) { + gController = SDL_JoystickOpen(0); + if (!gController) { + error("Unable to open controller: %s", SDL_GetError()); + } + } + else { + error("No controller found"); + } + mixer_init(); char title_buffer[100]; @@ -288,6 +299,7 @@ initGame(void) gCamera = camera_create(gRenderer); gRoomMatrix = roommatrix_create(); gGui = gui_create(gCamera); + skillbar_set_controller_mode(gController != NULL); gSkillBar = skillbar_create(gCamera); item_builder_init(gRenderer); #ifdef DEBUG @@ -529,6 +541,7 @@ init(void) hiscore_init(); initMainMenu(); + tooltip_set_controller_mode(gController != NULL); howto_tooltip = tooltip_create(how_to_play_tooltip, gCamera); new_skill_tooltip = tooltip_create(skills_tooltip, gCamera); new_artifact_tooltip = tooltip_create(artifacts_tooltip, gCamera); @@ -1105,6 +1118,8 @@ void close(void) steam_shutdown(); #endif // STEAM_BUILD + if (gController) + SDL_JoystickClose(gController); SDL_DestroyRenderer(gRenderer); SDL_DestroyWindow(gWindow); gWindow = NULL;