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 "Platform.h"
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
@ -9,11 +10,9 @@
|
||||||
|
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#include "../Rendering.h"
|
|
||||||
|
|
||||||
#include "../../WindowsWrapper.h"
|
#include "../../WindowsWrapper.h"
|
||||||
|
|
||||||
#include "Platform.h"
|
#include "../Rendering.h"
|
||||||
#include "../../Main.h"
|
#include "../../Main.h"
|
||||||
#include "../../Organya.h"
|
#include "../../Organya.h"
|
||||||
#include "../../Profile.h"
|
#include "../../Profile.h"
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "../Controller.h"
|
#include "../Controller.h"
|
||||||
|
#include "Controller.h"
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -11,23 +12,12 @@ static SDL_Joystick *joystick;
|
||||||
static int joystick_neutral_x;
|
static int joystick_neutral_x;
|
||||||
static int joystick_neutral_y;
|
static int joystick_neutral_y;
|
||||||
|
|
||||||
|
void ControllerBackend_JoystickCallback(int joystick_id, BOOL connected);
|
||||||
|
|
||||||
BOOL ControllerBackend_Init(void)
|
BOOL ControllerBackend_Init(void)
|
||||||
{
|
{
|
||||||
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
|
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;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,35 +43,20 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status)
|
||||||
numButtons = 32;
|
numButtons = 32;
|
||||||
|
|
||||||
// Read whatever buttons actually exist
|
// Read whatever buttons actually exist
|
||||||
int i = 0;
|
for (int i = 0; i < numButtons; ++i)
|
||||||
for (; i < numButtons; ++i)
|
status->bButton[i] = SDL_JoystickGetButton(joystick, i);
|
||||||
{
|
|
||||||
if (SDL_JoystickGetButton(joystick, i) != 0)
|
|
||||||
status->bButton[i] = TRUE;
|
|
||||||
else
|
|
||||||
status->bButton[i] = FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Blank the buttons that do not
|
// Blank the buttons that do not
|
||||||
for (; i < 32; ++i)
|
for (int i = numButtons; i < 32; ++i)
|
||||||
status->bButton[i] = FALSE;
|
status->bButton[i] = FALSE;
|
||||||
|
|
||||||
status->bDown = FALSE;
|
|
||||||
status->bRight = FALSE;
|
|
||||||
status->bUp = FALSE;
|
|
||||||
status->bLeft = FALSE;
|
|
||||||
|
|
||||||
const Sint16 joystick_x = SDL_JoystickGetAxis(joystick, 0);
|
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);
|
const Sint16 joystick_y = SDL_JoystickGetAxis(joystick, 1);
|
||||||
if (joystick_y < joystick_neutral_y - 10000)
|
|
||||||
status->bUp = TRUE;
|
status->bLeft = joystick_x < joystick_neutral_x - 10000;
|
||||||
else if (joystick_y > joystick_neutral_y + 10000)
|
status->bRight = joystick_x > joystick_neutral_x + 10000;
|
||||||
status->bDown = TRUE;
|
status->bUp = joystick_y < joystick_neutral_y - 10000;
|
||||||
|
status->bDown = joystick_y > joystick_neutral_y + 10000;
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
@ -91,8 +66,35 @@ BOOL ControllerBackend_ResetJoystickStatus(void)
|
||||||
if (joystick == NULL)
|
if (joystick == NULL)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
joystick_neutral_x = SDL_JoystickGetAxis(joystick, 0);
|
// The code that would normally run here has been moved to JoystickCallback, to better-support hotplugging
|
||||||
joystick_neutral_y = SDL_JoystickGetAxis(joystick, 1);
|
|
||||||
|
|
||||||
return TRUE;
|
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 "Platform.h"
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -6,11 +7,10 @@
|
||||||
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
|
|
||||||
#include "../Rendering.h"
|
|
||||||
|
|
||||||
#include "../../WindowsWrapper.h"
|
#include "../../WindowsWrapper.h"
|
||||||
|
|
||||||
#include "Platform.h"
|
#include "Controller.h"
|
||||||
|
#include "../Rendering.h"
|
||||||
#include "../../Main.h"
|
#include "../../Main.h"
|
||||||
#include "../../Organya.h"
|
#include "../../Organya.h"
|
||||||
#include "../../Profile.h"
|
#include "../../Profile.h"
|
||||||
|
@ -196,6 +196,14 @@ BOOL PlatformBackend_SystemTask(void)
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case SDL_JOYDEVICEADDED:
|
||||||
|
ControllerBackend_JoystickConnect(event.jdevice.which);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDL_JOYDEVICEREMOVED:
|
||||||
|
ControllerBackend_JoystickDisconnect(event.jdevice.which);
|
||||||
|
break;
|
||||||
|
|
||||||
case SDL_DROPFILE:
|
case SDL_DROPFILE:
|
||||||
LoadProfile(event.drop.file);
|
LoadProfile(event.drop.file);
|
||||||
SDL_free(event.drop.file);
|
SDL_free(event.drop.file);
|
||||||
|
|
Loading…
Add table
Reference in a new issue