Adds controller haptics

This commit is contained in:
Linus Probert 2018-10-12 23:39:54 +02:00
parent 423ff733af
commit 850aae34ee
4 changed files with 37 additions and 2 deletions

View File

@ -2,14 +2,16 @@
#include "util.h" #include "util.h"
static SDL_GameController *controller = NULL; static SDL_GameController *controller = NULL;
static SDL_Haptic *haptic = NULL;
static Uint8 controllerMode = 0; static Uint8 controllerMode = 0;
void void
gamecontroller_set(SDL_GameController *ctrler) gamecontroller_set(SDL_GameController *ctrler)
{ {
controller = ctrler;
const char *ctrlName = SDL_GameControllerName(controller); const char *ctrlName = SDL_GameControllerName(controller);
info("Game controller connected: %s", ctrlName); info("Game controller connected: %s", ctrlName);
controller = ctrler;
// Try to determine if this is a PS3/4 controller // Try to determine if this is a PS3/4 controller
if (ctrlName[0] == 'P' && if (ctrlName[0] == 'P' &&
@ -18,6 +20,30 @@ gamecontroller_set(SDL_GameController *ctrler)
controllerMode = 2; controllerMode = 2;
else else
controllerMode = 1; 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 Uint8
@ -31,4 +57,6 @@ gamecontroller_close()
{ {
if (controller) if (controller)
SDL_GameControllerClose(controller); SDL_GameControllerClose(controller);
if (haptic)
SDL_HapticClose(haptic);
} }

View File

@ -10,6 +10,9 @@ typedef struct GameController {
void void
gamecontroller_set(SDL_GameController *controller); gamecontroller_set(SDL_GameController *controller);
void
gamecontroller_rumble(float intensity, Uint32 duration);
Uint8 Uint8
gamecontroller_mode(void); gamecontroller_mode(void);

View File

@ -185,7 +185,7 @@ bool initSDL(void)
{ {
int imgFlags = IMG_INIT_PNG; 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()); error("Could not initiate SDL2: %s", SDL_GetError());
return false; return false;

View File

@ -35,6 +35,7 @@
#include "actiontextbuilder.h" #include "actiontextbuilder.h"
#include "animation.h" #include "animation.h"
#include "trap.h" #include "trap.h"
#include "gamecontroller.h"
#ifdef STEAM_BUILD #ifdef STEAM_BUILD
#include "steam/steamworks_api_wrapper.h" #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.x -= TILE_DIMENSION * (int)direction.x;
player->sprite->pos.y -= TILE_DIMENSION * (int)direction.y; player->sprite->pos.y -= TILE_DIMENSION * (int)direction.y;
gamecontroller_rumble(0.30, 100);
if (space->monster) { if (space->monster) {
on_monster_collision(player, space->monster, matrix, direction); on_monster_collision(player, space->monster, matrix, direction);
} else { } else {
@ -590,6 +592,8 @@ player_hit(Player *p, unsigned int dmg)
actiontextbuilder_create_text(msg, actiontextbuilder_create_text(msg,
C_RED, C_RED,
&p->sprite->pos); &p->sprite->pos);
gamecontroller_rumble(0.50, 100);
} else { } else {
actiontextbuilder_create_text("Dodged", actiontextbuilder_create_text("Dodged",
C_YELLOW, C_YELLOW,