Split SDL2 code from Input.cpp
...So, *all* of it.
This commit is contained in:
parent
142bca6578
commit
37c5236b9b
4 changed files with 133 additions and 96 deletions
|
@ -163,8 +163,10 @@ add_executable(CSE2 WIN32
|
||||||
"src/ValueView.cpp"
|
"src/ValueView.cpp"
|
||||||
"src/ValueView.h"
|
"src/ValueView.h"
|
||||||
"src/WindowsWrapper.h"
|
"src/WindowsWrapper.h"
|
||||||
|
"src/Backends/Controller/SDL2.cpp"
|
||||||
"src/Backends/Platform/SDL2.cpp"
|
"src/Backends/Platform/SDL2.cpp"
|
||||||
"src/Backends/Audio.h"
|
"src/Backends/Audio.h"
|
||||||
|
"src/Backends/Controller.h"
|
||||||
"src/Backends/Platform.h"
|
"src/Backends/Platform.h"
|
||||||
"src/Backends/Rendering.h"
|
"src/Backends/Rendering.h"
|
||||||
)
|
)
|
||||||
|
|
10
src/Backends/Controller.h
Normal file
10
src/Backends/Controller.h
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../WindowsWrapper.h"
|
||||||
|
|
||||||
|
#include "../Input.h"
|
||||||
|
|
||||||
|
void ControllerBackend_Deinit(void);
|
||||||
|
BOOL ControllerBackend_Init(void);
|
||||||
|
BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status);
|
||||||
|
BOOL ControllerBackend_ResetJoystickStatus(void);
|
116
src/Backends/Controller/SDL2.cpp
Normal file
116
src/Backends/Controller/SDL2.cpp
Normal file
|
@ -0,0 +1,116 @@
|
||||||
|
#include "../Controller.h"
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "SDL.h"
|
||||||
|
|
||||||
|
#include "../../WindowsWrapper.h"
|
||||||
|
|
||||||
|
// The original names for these variables are unknown
|
||||||
|
static SDL_Joystick *joystick = NULL;
|
||||||
|
static int joystick_neutral_x = 0;
|
||||||
|
static int joystick_neutral_y = 0;
|
||||||
|
|
||||||
|
void ControllerBackend_Deinit(void)
|
||||||
|
{
|
||||||
|
// Close opened joystick (if exists)
|
||||||
|
if (joystick != NULL)
|
||||||
|
{
|
||||||
|
SDL_JoystickClose(joystick);
|
||||||
|
joystick = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
||||||
|
}
|
||||||
|
|
||||||
|
// It looks like Pixel declared his functions early, so he could forward-reference
|
||||||
|
BOOL FindAndOpenDirectInputDevice(void);
|
||||||
|
|
||||||
|
BOOL ControllerBackend_Init(void)
|
||||||
|
{
|
||||||
|
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
|
||||||
|
|
||||||
|
if (!FindAndOpenDirectInputDevice())
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The original name for this function and its variables are unknown.
|
||||||
|
// This function finds and hooks the first available DirectInput device (or SDL Joystick, in this case).
|
||||||
|
BOOL FindAndOpenDirectInputDevice(void)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < SDL_NumJoysticks(); ++i)
|
||||||
|
printf("Joystick #%d name: %s\n", i, SDL_JoystickNameForIndex(i));
|
||||||
|
|
||||||
|
// Open first available joystick
|
||||||
|
for (i = 0; i < SDL_NumJoysticks(); ++i)
|
||||||
|
{
|
||||||
|
joystick = SDL_JoystickOpen(i);
|
||||||
|
|
||||||
|
// Break as soon as a joystick is properly opened
|
||||||
|
if (joystick != NULL)
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status)
|
||||||
|
{
|
||||||
|
if (joystick == NULL)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
// The original Input.cpp assumed there were 32 buttons (because of DirectInput's 'DIJOYSTATE' struct)
|
||||||
|
int numButtons = SDL_JoystickNumButtons(joystick);
|
||||||
|
if (numButtons > 32)
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Blank the buttons that do not
|
||||||
|
for (; 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;
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL ControllerBackend_ResetJoystickStatus(void)
|
||||||
|
{
|
||||||
|
if (joystick == NULL)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
joystick_neutral_x = SDL_JoystickGetAxis(joystick, 0);
|
||||||
|
joystick_neutral_y = SDL_JoystickGetAxis(joystick, 1);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
101
src/Input.cpp
101
src/Input.cpp
|
@ -1,116 +1,25 @@
|
||||||
#include "Input.h"
|
#include "Input.h"
|
||||||
|
|
||||||
#include <stddef.h>
|
#include "Backends/Controller.h"
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "SDL.h"
|
|
||||||
|
|
||||||
#include "WindowsWrapper.h"
|
#include "WindowsWrapper.h"
|
||||||
|
|
||||||
// The original names for these variables are unknown
|
|
||||||
static SDL_Joystick *joystick = NULL;
|
|
||||||
static int joystick_neutral_x = 0;
|
|
||||||
static int joystick_neutral_y = 0;
|
|
||||||
|
|
||||||
void ReleaseDirectInput(void)
|
void ReleaseDirectInput(void)
|
||||||
{
|
{
|
||||||
// Close opened joystick (if exists)
|
ControllerBackend_Deinit();
|
||||||
if (joystick != NULL)
|
|
||||||
{
|
|
||||||
SDL_JoystickClose(joystick);
|
|
||||||
joystick = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// It looks like Pixel declared his functions early, so he could forward-reference
|
|
||||||
BOOL FindAndOpenDirectInputDevice(void);
|
|
||||||
|
|
||||||
BOOL InitDirectInput(void)
|
BOOL InitDirectInput(void)
|
||||||
{
|
{
|
||||||
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
|
return ControllerBackend_Init();
|
||||||
|
|
||||||
if (!FindAndOpenDirectInputDevice())
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
// The original name for this function and its variables are unknown.
|
|
||||||
// This function finds and hooks the first available DirectInput device (or SDL Joystick, in this case).
|
|
||||||
BOOL FindAndOpenDirectInputDevice(void)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < SDL_NumJoysticks(); ++i)
|
|
||||||
printf("Joystick #%d name: %s\n", i, SDL_JoystickNameForIndex(i));
|
|
||||||
|
|
||||||
// Open first available joystick
|
|
||||||
for (i = 0; i < SDL_NumJoysticks(); ++i)
|
|
||||||
{
|
|
||||||
joystick = SDL_JoystickOpen(i);
|
|
||||||
|
|
||||||
// Break as soon as a joystick is properly opened
|
|
||||||
if (joystick != NULL)
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
return FALSE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL GetJoystickStatus(JOYSTICK_STATUS *status)
|
BOOL GetJoystickStatus(JOYSTICK_STATUS *status)
|
||||||
{
|
{
|
||||||
if (joystick == NULL)
|
return ControllerBackend_GetJoystickStatus(status);
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
// The original Input.cpp assumed there were 32 buttons (because of DirectInput's 'DIJOYSTATE' struct)
|
|
||||||
int numButtons = SDL_JoystickNumButtons(joystick);
|
|
||||||
if (numButtons > 32)
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Blank the buttons that do not
|
|
||||||
for (; 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;
|
|
||||||
|
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL ResetJoystickStatus(void)
|
BOOL ResetJoystickStatus(void)
|
||||||
{
|
{
|
||||||
if (joystick == NULL)
|
return ControllerBackend_ResetJoystickStatus();
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
joystick_neutral_x = SDL_JoystickGetAxis(joystick, 0);
|
|
||||||
joystick_neutral_y = SDL_JoystickGetAxis(joystick, 1);
|
|
||||||
|
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue