Skip to content

Commit

Permalink
- Added haptics on SDL. (#486)
Browse files Browse the repository at this point in the history
* - Added haptics on SDL.
- Using GL 3.1 context instead of 2.0.

* Undo experimental context version bump.
  • Loading branch information
dborel authored Mar 2, 2024
1 parent 653da99 commit 5fb1adb
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/platform/sdl2/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ struct sdl_input *sdl_inputs;
int sdl_numjoysticks, sdl_numcontrollers;
SDL_Joystick *sdl_joysticks[MAX_JOYS];
SDL_GameController *sdl_controllers[MAX_JOYS];
SDL_Haptic *sdl_haptics[MAX_JOYS];
SDL_Window *sdl_window;
SDL_DisplayMode sdl_displaymode;

Expand All @@ -99,7 +100,13 @@ bool osJoyReady(int index) {
}

void osJoyVibrate(int index, float L, float R) {
// TODO
if (index >= sdl_numcontrollers)
return;
if (SDL_IsGameController(index)) {
SDL_GameControllerRumble(sdl_controllers[index], L*0xFFFF, R*0xFFFF, 500);
} else {
SDL_HapticRumblePlay(sdl_haptics[index], L+R, 500);
}
}

bool isKeyPressed (SDL_Scancode scancode) {
Expand Down Expand Up @@ -257,6 +264,8 @@ void joyAdd(int index) {
}
else {
sdl_joysticks[index] = SDL_JoystickOpen(index);
sdl_haptics[index] = SDL_HapticOpenFromJoystick(sdl_joysticks[index]);
SDL_HapticRumbleInit(sdl_haptics[index]);
}
// Update number of joysticks
sdl_numjoysticks = SDL_NumJoysticks();
Expand All @@ -282,6 +291,8 @@ void joyRemove(Sint32 instanceID) {
i = joyGetIndex(instanceID);
if (i >= 0) {
SDL_JoystickClose(sdl_joysticks[i]);
SDL_HapticClose(sdl_haptics[i]);
sdl_haptics[i] = NULL;
sdl_numjoysticks--;
}
}
Expand Down Expand Up @@ -519,7 +530,7 @@ int main(int argc, char **argv) {

int w, h;
SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_EVENTS|SDL_INIT_GAMECONTROLLER);
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_EVENTS|SDL_INIT_GAMECONTROLLER|SDL_INIT_HAPTIC);

SDL_GetCurrentDisplayMode(0, &sdl_displaymode);

Expand Down

0 comments on commit 5fb1adb

Please sign in to comment.