Avoid WindowsWrapper.h in controller backend

This commit is contained in:
Clownacy 2020-04-13 14:56:48 +01:00
parent 026fea52ff
commit 8549fa561e
3 changed files with 23 additions and 27 deletions

View file

@ -1,7 +1,5 @@
#pragma once #pragma once
#include "../WindowsWrapper.h" bool ControllerBackend_Init(void);
BOOL ControllerBackend_Init(void);
void ControllerBackend_Deinit(void); void ControllerBackend_Deinit(void);
BOOL ControllerBackend_GetJoystickStatus(BOOL **buttons, unsigned int *button_count, short **axes, unsigned int *axis_count); bool ControllerBackend_GetJoystickStatus(bool **buttons, unsigned int *button_count, short **axes, unsigned int *axis_count);

View file

@ -8,11 +8,10 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "../Misc.h" #include "../Misc.h"
#include "../../WindowsWrapper.h"
#define DEADZONE (10000.0f / 32767.0f) #define DEADZONE (10000.0f / 32767.0f)
static BOOL joystick_connected; static bool joystick_connected;
static int connected_joystick_id; static int connected_joystick_id;
static float *axis_neutrals; static float *axis_neutrals;
@ -47,7 +46,7 @@ static void JoystickCallback(int joystick_id, int event)
axis_neutrals[i] = axes[i]; axis_neutrals[i] = axes[i];
printf("Joystick #%d selected\n", joystick_id); printf("Joystick #%d selected\n", joystick_id);
joystick_connected = TRUE; joystick_connected = true;
connected_joystick_id = joystick_id; connected_joystick_id = joystick_id;
} }
else else
@ -64,7 +63,7 @@ static void JoystickCallback(int joystick_id, int event)
if (joystick_connected && joystick_id == connected_joystick_id) if (joystick_connected && joystick_id == connected_joystick_id)
{ {
Backend_PrintInfo("Joystick #%d disconnected", connected_joystick_id); Backend_PrintInfo("Joystick #%d disconnected", connected_joystick_id);
joystick_connected = FALSE; joystick_connected = false;
free(axis_neutrals); free(axis_neutrals);
} }
@ -73,7 +72,7 @@ static void JoystickCallback(int joystick_id, int event)
} }
} }
BOOL ControllerBackend_Init(void) bool ControllerBackend_Init(void)
{ {
// Connect joysticks that are already plugged-in // Connect joysticks that are already plugged-in
for (int i = GLFW_JOYSTICK_1; i < GLFW_JOYSTICK_LAST; ++i) for (int i = GLFW_JOYSTICK_1; i < GLFW_JOYSTICK_LAST; ++i)
@ -83,24 +82,24 @@ BOOL ControllerBackend_Init(void)
// Set-up the callback for future (dis)connections // Set-up the callback for future (dis)connections
glfwSetJoystickCallback(JoystickCallback); glfwSetJoystickCallback(JoystickCallback);
return TRUE; return true;
} }
void ControllerBackend_Deinit(void) void ControllerBackend_Deinit(void)
{ {
glfwSetJoystickCallback(NULL); glfwSetJoystickCallback(NULL);
joystick_connected = FALSE; joystick_connected = false;
connected_joystick_id = 0; connected_joystick_id = 0;
free(axis_neutrals); free(axis_neutrals);
axis_neutrals = NULL; axis_neutrals = NULL;
} }
BOOL ControllerBackend_GetJoystickStatus(BOOL **buttons, unsigned int *button_count, short **axes, unsigned int *axis_count) bool ControllerBackend_GetJoystickStatus(bool **buttons, unsigned int *button_count, short **axes, unsigned int *axis_count)
{ {
if (!joystick_connected) if (!joystick_connected)
return FALSE; return false;
int total_glfw_buttons; int total_glfw_buttons;
const unsigned char *glfw_buttons = glfwGetJoystickButtons(connected_joystick_id, &total_glfw_buttons); const unsigned char *glfw_buttons = glfwGetJoystickButtons(connected_joystick_id, &total_glfw_buttons);
@ -116,14 +115,14 @@ BOOL ControllerBackend_GetJoystickStatus(BOOL **buttons, unsigned int *button_co
*button_count = total_glfw_buttons + total_glfw_axes * 2 + total_glfw_hats * 4; *button_count = total_glfw_buttons + total_glfw_axes * 2 + total_glfw_hats * 4;
*axis_count = total_glfw_axes; *axis_count = total_glfw_axes;
static BOOL *button_buffer = NULL; static bool *button_buffer = NULL;
static short *axis_buffer = NULL; static short *axis_buffer = NULL;
BOOL *new_button_buffer = (BOOL*)realloc(button_buffer, *button_count * sizeof(BOOL)); bool *new_button_buffer = (bool*)realloc(button_buffer, *button_count * sizeof(bool));
short *new_axis_buffer = (short*)realloc(axis_buffer, *axis_count * sizeof(short)); short *new_axis_buffer = (short*)realloc(axis_buffer, *axis_count * sizeof(short));
if (new_button_buffer == NULL || new_axis_buffer == NULL) if (new_button_buffer == NULL || new_axis_buffer == NULL)
return FALSE; return false;
button_buffer = new_button_buffer; button_buffer = new_button_buffer;
axis_buffer = new_axis_buffer; axis_buffer = new_axis_buffer;
@ -167,5 +166,5 @@ BOOL ControllerBackend_GetJoystickStatus(BOOL **buttons, unsigned int *button_co
*axes = axis_buffer; *axes = axis_buffer;
return TRUE; return true;
} }

View file

@ -7,7 +7,6 @@
#include "SDL.h" #include "SDL.h"
#include "../Misc.h" #include "../Misc.h"
#include "../../WindowsWrapper.h"
#define DEADZONE 10000 #define DEADZONE 10000
@ -15,15 +14,15 @@ static SDL_Joystick *joystick;
static Sint16 *axis_neutrals; static Sint16 *axis_neutrals;
BOOL ControllerBackend_Init(void) bool ControllerBackend_Init(void)
{ {
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
{ {
Backend_PrintError("Couldn't initialise joystick SDL subsystem: %s", SDL_GetError()); Backend_PrintError("Couldn't initialise joystick SDL subsystem: %s", SDL_GetError());
return FALSE; return false;
} }
return TRUE; return true;
} }
void ControllerBackend_Deinit(void) void ControllerBackend_Deinit(void)
@ -37,10 +36,10 @@ void ControllerBackend_Deinit(void)
SDL_QuitSubSystem(SDL_INIT_JOYSTICK); SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
} }
BOOL ControllerBackend_GetJoystickStatus(BOOL **buttons, unsigned int *button_count, short **axes, unsigned int *axis_count) bool ControllerBackend_GetJoystickStatus(bool **buttons, unsigned int *button_count, short **axes, unsigned int *axis_count)
{ {
if (joystick == NULL) if (joystick == NULL)
return FALSE; return false;
int total_sdl_buttons = SDL_JoystickNumButtons(joystick); int total_sdl_buttons = SDL_JoystickNumButtons(joystick);
if (total_sdl_buttons < 0) if (total_sdl_buttons < 0)
@ -57,14 +56,14 @@ BOOL ControllerBackend_GetJoystickStatus(BOOL **buttons, unsigned int *button_co
*button_count = total_sdl_buttons + total_sdl_axes * 2 + total_sdl_hats * 4; *button_count = total_sdl_buttons + total_sdl_axes * 2 + total_sdl_hats * 4;
*axis_count = total_sdl_axes; *axis_count = total_sdl_axes;
static BOOL *button_buffer = NULL; static bool *button_buffer = NULL;
static short *axis_buffer = NULL; static short *axis_buffer = NULL;
BOOL *new_button_buffer = (BOOL*)realloc(button_buffer, *button_count * sizeof(BOOL)); bool *new_button_buffer = (bool*)realloc(button_buffer, *button_count * sizeof(bool));
short *new_axis_buffer = (short*)realloc(axis_buffer, *axis_count * sizeof(short)); short *new_axis_buffer = (short*)realloc(axis_buffer, *axis_count * sizeof(short));
if (new_button_buffer == NULL || new_axis_buffer == NULL) if (new_button_buffer == NULL || new_axis_buffer == NULL)
return FALSE; return false;
button_buffer = new_button_buffer; button_buffer = new_button_buffer;
axis_buffer = new_axis_buffer; axis_buffer = new_axis_buffer;
@ -110,7 +109,7 @@ BOOL ControllerBackend_GetJoystickStatus(BOOL **buttons, unsigned int *button_co
*axes = axis_buffer; *axes = axis_buffer;
return TRUE; return true;
} }
void ControllerBackend_JoystickConnect(Sint32 joystick_id) void ControllerBackend_JoystickConnect(Sint32 joystick_id)