Add hotplugging support to SDL2 backend
This commit is contained in:
parent
7ee2e68c2e
commit
65325e2b34
3 changed files with 53 additions and 44 deletions
|
@ -1,4 +1,5 @@
|
|||
#include "../Platform.h"
|
||||
#include "Platform.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <stddef.h>
|
||||
|
@ -9,11 +10,9 @@
|
|||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "../Rendering.h"
|
||||
|
||||
#include "../../WindowsWrapper.h"
|
||||
|
||||
#include "Platform.h"
|
||||
#include "../Rendering.h"
|
||||
#include "../../Main.h"
|
||||
#include "../../Organya.h"
|
||||
#include "../../Profile.h"
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "../Controller.h"
|
||||
#include "Controller.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
@ -11,23 +12,12 @@ static SDL_Joystick *joystick;
|
|||
static int joystick_neutral_x;
|
||||
static int joystick_neutral_y;
|
||||
|
||||
void ControllerBackend_JoystickCallback(int joystick_id, BOOL connected);
|
||||
|
||||
BOOL ControllerBackend_Init(void)
|
||||
{
|
||||
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
|
||||
|
||||
for (int i = 0; i < SDL_NumJoysticks(); ++i)
|
||||
{
|
||||
printf("Joystick #%d connected - %s\n", i, SDL_JoystickNameForIndex(i));
|
||||
|
||||
if (joystick == NULL)
|
||||
{
|
||||
joystick = SDL_JoystickOpen(i);
|
||||
|
||||
if (joystick != NULL)
|
||||
printf("Joystick #%d selected\n", i);
|
||||
}
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
@ -53,35 +43,20 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status)
|
|||
numButtons = 32;
|
||||
|
||||
// Read whatever buttons actually exist
|
||||
int i = 0;
|
||||
for (; i < numButtons; ++i)
|
||||
{
|
||||
if (SDL_JoystickGetButton(joystick, i) != 0)
|
||||
status->bButton[i] = TRUE;
|
||||
else
|
||||
status->bButton[i] = FALSE;
|
||||
}
|
||||
for (int i = 0; i < numButtons; ++i)
|
||||
status->bButton[i] = SDL_JoystickGetButton(joystick, i);
|
||||
|
||||
// Blank the buttons that do not
|
||||
for (; i < 32; ++i)
|
||||
for (int i = numButtons; i < 32; ++i)
|
||||
status->bButton[i] = FALSE;
|
||||
|
||||
status->bDown = FALSE;
|
||||
status->bRight = FALSE;
|
||||
status->bUp = FALSE;
|
||||
status->bLeft = FALSE;
|
||||
|
||||
const Sint16 joystick_x = SDL_JoystickGetAxis(joystick, 0);
|
||||
if (joystick_x < joystick_neutral_x - 10000)
|
||||
status->bLeft = TRUE;
|
||||
else if (joystick_x > joystick_neutral_x + 10000)
|
||||
status->bRight = TRUE;
|
||||
|
||||
const Sint16 joystick_y = SDL_JoystickGetAxis(joystick, 1);
|
||||
if (joystick_y < joystick_neutral_y - 10000)
|
||||
status->bUp = TRUE;
|
||||
else if (joystick_y > joystick_neutral_y + 10000)
|
||||
status->bDown = TRUE;
|
||||
|
||||
status->bLeft = joystick_x < joystick_neutral_x - 10000;
|
||||
status->bRight = joystick_x > joystick_neutral_x + 10000;
|
||||
status->bUp = joystick_y < joystick_neutral_y - 10000;
|
||||
status->bDown = joystick_y > joystick_neutral_y + 10000;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
@ -91,8 +66,35 @@ BOOL ControllerBackend_ResetJoystickStatus(void)
|
|||
if (joystick == NULL)
|
||||
return FALSE;
|
||||
|
||||
joystick_neutral_x = SDL_JoystickGetAxis(joystick, 0);
|
||||
joystick_neutral_y = SDL_JoystickGetAxis(joystick, 1);
|
||||
// The code that would normally run here has been moved to JoystickCallback, to better-support hotplugging
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void ControllerBackend_JoystickConnect(Sint32 joystick_id)
|
||||
{
|
||||
printf("Joystick #%d connected - %s\n", joystick_id, SDL_JoystickNameForIndex(joystick_id));
|
||||
|
||||
if (joystick == NULL)
|
||||
{
|
||||
joystick = SDL_JoystickOpen(joystick_id);
|
||||
|
||||
if (joystick != NULL)
|
||||
{
|
||||
printf("Joystick #%d selected\n", joystick_id);
|
||||
|
||||
// Reset default stick positions (this is performed in ResetJoystickStatus in vanilla Cave Story
|
||||
joystick_neutral_x = SDL_JoystickGetAxis(joystick, 0);
|
||||
joystick_neutral_y = SDL_JoystickGetAxis(joystick, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ControllerBackend_JoystickDisconnect(Sint32 joystick_id)
|
||||
{
|
||||
if (joystick_id == SDL_JoystickInstanceID(joystick))
|
||||
{
|
||||
printf("Joystick #%d disconnected\n", joystick_id);
|
||||
joystick = NULL;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "../Platform.h"
|
||||
#include "Platform.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
@ -6,11 +7,10 @@
|
|||
|
||||
#include "SDL.h"
|
||||
|
||||
#include "../Rendering.h"
|
||||
|
||||
#include "../../WindowsWrapper.h"
|
||||
|
||||
#include "Platform.h"
|
||||
#include "Controller.h"
|
||||
#include "../Rendering.h"
|
||||
#include "../../Main.h"
|
||||
#include "../../Organya.h"
|
||||
#include "../../Profile.h"
|
||||
|
@ -196,6 +196,14 @@ BOOL PlatformBackend_SystemTask(void)
|
|||
|
||||
break;
|
||||
|
||||
case SDL_JOYDEVICEADDED:
|
||||
ControllerBackend_JoystickConnect(event.jdevice.which);
|
||||
break;
|
||||
|
||||
case SDL_JOYDEVICEREMOVED:
|
||||
ControllerBackend_JoystickDisconnect(event.jdevice.which);
|
||||
break;
|
||||
|
||||
case SDL_DROPFILE:
|
||||
LoadProfile(event.drop.file);
|
||||
SDL_free(event.drop.file);
|
||||
|
|
Loading…
Add table
Reference in a new issue