diff --git a/src/gamecontroller.c b/src/gamecontroller.c index 7a2bb19..32cc39b 100644 --- a/src/gamecontroller.c +++ b/src/gamecontroller.c @@ -2,14 +2,16 @@ #include "util.h" static SDL_GameController *controller = NULL; +static SDL_Haptic *haptic = NULL; static Uint8 controllerMode = 0; void gamecontroller_set(SDL_GameController *ctrler) { + controller = ctrler; + const char *ctrlName = SDL_GameControllerName(controller); info("Game controller connected: %s", ctrlName); - controller = ctrler; // Try to determine if this is a PS3/4 controller if (ctrlName[0] == 'P' && @@ -18,6 +20,30 @@ gamecontroller_set(SDL_GameController *ctrler) controllerMode = 2; else controllerMode = 1; + + haptic = SDL_HapticOpenFromJoystick(SDL_GameControllerGetJoystick(controller)); + if (haptic) { + info("Haptics are supported by controller: %s", ctrlName); + if (SDL_HapticRumbleInit(haptic) >= 0) { + info("Haptics enabled for controller: %s", ctrlName); + } + else { + info("Failed to enable haptics for: %s", ctrlName); + } + } + else { + info("Haptics not supported by controller: %s", ctrlName); + } +} + +void +gamecontroller_rumble(float intensity, Uint32 duration) +{ + if (!haptic) + return; + + if (SDL_HapticRumblePlay(haptic, intensity, duration) != 0) + error("Failed to play rumble: %s", SDL_GetError()); } Uint8 @@ -31,4 +57,6 @@ gamecontroller_close() { if (controller) SDL_GameControllerClose(controller); + if (haptic) + SDL_HapticClose(haptic); } diff --git a/src/gamecontroller.h b/src/gamecontroller.h index 6622f9a..d846944 100644 --- a/src/gamecontroller.h +++ b/src/gamecontroller.h @@ -10,6 +10,9 @@ typedef struct GameController { void gamecontroller_set(SDL_GameController *controller); +void +gamecontroller_rumble(float intensity, Uint32 duration); + Uint8 gamecontroller_mode(void); diff --git a/src/main.c b/src/main.c index b196f48..0bc77e7 100644 --- a/src/main.c +++ b/src/main.c @@ -185,7 +185,7 @@ bool initSDL(void) { int imgFlags = IMG_INIT_PNG; - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER) < 0) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) < 0) { error("Could not initiate SDL2: %s", SDL_GetError()); return false; diff --git a/src/player.c b/src/player.c index 2198bdf..4c54f7b 100644 --- a/src/player.c +++ b/src/player.c @@ -35,6 +35,7 @@ #include "actiontextbuilder.h" #include "animation.h" #include "trap.h" +#include "gamecontroller.h" #ifdef STEAM_BUILD #include "steam/steamworks_api_wrapper.h" @@ -219,6 +220,7 @@ has_collided(Player *player, RoomMatrix *matrix, Vector2d direction) player->sprite->pos.x -= TILE_DIMENSION * (int)direction.x; player->sprite->pos.y -= TILE_DIMENSION * (int)direction.y; + gamecontroller_rumble(0.30, 100); if (space->monster) { on_monster_collision(player, space->monster, matrix, direction); } else { @@ -590,6 +592,8 @@ player_hit(Player *p, unsigned int dmg) actiontextbuilder_create_text(msg, C_RED, &p->sprite->pos); + + gamecontroller_rumble(0.50, 100); } else { actiontextbuilder_create_text("Dodged", C_YELLOW,