Backends/SDL2: Finished up improving error handling

Signed-off-by: Gabriel Ravier <gabravier@gmail.com>
This commit is contained in:
Gabriel Ravier 2020-04-12 00:24:30 +02:00
parent 12d45ac1ed
commit 5fc0f58525
3 changed files with 101 additions and 37 deletions

View file

@ -9,7 +9,7 @@
#include "../../WindowsWrapper.h"
#include "../Misc.h"
#define DEADZONE 10000;
#define DEADZONE 10000
static SDL_Joystick *joystick;
@ -19,7 +19,7 @@ BOOL ControllerBackend_Init(void)
{
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
{
Backend_PrintError("Couldn't initialise joystack SDL2 subsystem : %s", SDL_GetError());
Backend_PrintError("Couldn't initialise joystack SDL2 subsystem: %s", SDL_GetError());
return FALSE;
}
@ -47,11 +47,11 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status)
// Handle directional inputs
const Sint16 joystick_x = SDL_JoystickGetAxis(joystick, 0);
if (!joystick_x)
Backend_PrintError("Failed to get current state of X axis control on joystick : %s", SDL_GetError());
Backend_PrintError("Failed to get current state of X axis control on joystick: %s", SDL_GetError());
const Sint16 joystick_y = SDL_JoystickGetAxis(joystick, 1);
if (!joystick_y)
Backend_PrintError("Failed to get current state of Y axis control on joystick : %s", SDL_GetError());
Backend_PrintError("Failed to get current state of Y axis control on joystick: %s", SDL_GetError());
status->bLeft = joystick_x < axis_neutrals[0] - DEADZONE;
status->bRight = joystick_x > axis_neutrals[0] + DEADZONE;
@ -61,15 +61,15 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status)
// Handle button inputs
int total_buttons = SDL_JoystickNumButtons(joystick);
if (total_buttons < 0)
Backend_PrintError("Failed to get number of buttons on joystick : %s", SDL_GetError());
Backend_PrintError("Failed to get number of buttons on joystick: %s", SDL_GetError());
int total_axes = SDL_JoystickNumAxes(joystick);
if (total_axes < 0)
Backend_PrintError("Failed to get number of general axis controls on joystick : %s", SDL_GetError());
Backend_PrintError("Failed to get number of general axis controls on joystick: %s", SDL_GetError());
int total_hats = SDL_JoystickNumHats(joystick);
if (total_hats < 0)
Backend_PrintError("Failed to get number of POV hats on joystick : %s", SDL_GetError());
Backend_PrintError("Failed to get number of POV hats on joystick: %s", SDL_GetError());
unsigned int buttons_done = 0;
@ -141,7 +141,14 @@ BOOL ControllerBackend_ResetJoystickStatus(void)
void ControllerBackend_JoystickConnect(Sint32 joystick_id)
{
printf("Joystick #%d connected - %s\n", joystick_id, SDL_JoystickNameForIndex(joystick_id));
const char *joystick_name = SDL_JoystickNameForIndex(joystick_id);
if (joystick_name)
Backend_PrintInfo("Joystick #%d connected - %s", joystick_id, joystick_name);
else
{
Backend_PrintError("Couldn't get joystick name: %s", SDL_GetError());
Backend_PrintInfo("Joystick #%d connected - Name unknown", joystick_id);
}
if (joystick == NULL)
{
@ -150,17 +157,28 @@ void ControllerBackend_JoystickConnect(Sint32 joystick_id)
if (joystick != NULL)
{
int total_axes = SDL_JoystickNumAxes(joystick);
if (total_axes < 0)
Backend_PrintError("Couldn't get number of general axis control on connected joystick: %s", SDL_GetError());
int total_buttons = SDL_JoystickNumButtons(joystick);
if (total_buttons < 0)
Backend_PrintError("Couldn't get number of buttons on connected joystick: %s", SDL_GetError());
if (total_axes >= 2 && total_buttons >= 6)
{
printf("Joystick #%d selected\n", joystick_id);
Backend_PrintInfo("Joystick #%d selected", joystick_id);
// Set up neutral axes
axis_neutrals = (Sint16*)malloc(sizeof(Sint16) * total_axes);
for (int i = 0; i < total_axes; ++i)
axis_neutrals[i] = SDL_JoystickGetAxis(joystick, i);
if (axis_neutrals)
{
for (int i = 0; i < total_axes; ++i)
axis_neutrals[i] = SDL_JoystickGetAxis(joystick, i);
}
else
{
Backend_PrintError("Couldn't allocate memory for neutral axes");
}
}
else
{
@ -168,14 +186,22 @@ void ControllerBackend_JoystickConnect(Sint32 joystick_id)
joystick = NULL;
}
}
else
{
Backend_PrintError("Couldn't open joystick for use: %s", SDL_GetError());
}
}
}
void ControllerBackend_JoystickDisconnect(Sint32 joystick_id)
{
if (joystick_id == SDL_JoystickInstanceID(joystick))
SDL_JoystickID current_joystick_id = SDL_JoystickInstanceID(joystick);
if (current_joystick_id < 0)
Backend_PrintError("Couldn't get instance ID for current joystick: %s", SDL_GetError());
if (joystick_id == current_joystick_id)
{
printf("Joystick #%d disconnected\n", joystick_id);
Backend_PrintInfo("Joystick #%d disconnected", joystick_id);
SDL_JoystickClose(joystick);
joystick = NULL;

View file

@ -2,6 +2,7 @@
#include "Window.h"
#include <stddef.h>
#include <string>
#ifdef USE_OPENGLES2
#include <GLES2/gl2.h>
@ -22,15 +23,29 @@ static SDL_GLContext context;
BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_width, int *screen_height, BOOL fullscreen)
{
#ifdef USE_OPENGLES2
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES) < 0)
Backend_PrintError("Couldn't set OpenGL context type to ES: %s", SDL_GetError());
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0) < 0)
Backend_PrintError("Couldn't set OpenGL context flags to 0: %s", SDL_GetError());
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2) < 0)
Backend_PrintError("Couldn't set OpenGL major version to 2: %s", SDL_GetError());
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0) < 0)
Backend_PrintError("Couldn't set OpenGL minor version to 0: %s", SDL_GetError());
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) < 0)
Backend_PrintError("Couldn't set OpenGL context type to core: %s", SDL_GetError());
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG) < 0)
Backend_PrintError("Couldn't set OpenGL forward compatibility: %s", SDL_GetError());
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) < 0)
Backend_PrintError("Couldn't set OpenGL major version to 3: %s", SDL_GetError());
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2) < 0)
Backend_PrintError("Couldn't set OpenGL minor verison to 2: %s", SDL_GetError());
#endif
window = SDL_CreateWindow(window_title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, *screen_width, *screen_height, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | (fullscreen ? SDL_WINDOW_FULLSCREEN : 0));
@ -62,12 +77,13 @@ BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_wid
}
else
{
Backend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Could not load OpenGL functions");
Backend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Couldn't load OpenGL functions");
}
#endif
}
else
{
std::string error_message = std::string("Couldn't setup OpenGL context for rendering: ") + SDL_GetError();
Backend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "SDL_GL_MakeCurrent failed");
}
@ -75,6 +91,7 @@ BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_wid
}
else
{
std::string error_message = std::string("Couldn't create OpenGL context: %s", SDL_GetError());
Backend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Could not create OpenGL context");
}
@ -82,7 +99,8 @@ BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_wid
}
else
{
Backend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Could not create window");
std::string error_message = std::string("Could not create window: ") + SDL_GetError();
Backend_ShowMessageBox("Fatal error (OpenGL rendering backend)", error_message.c_str());
}
return FALSE;

View file

@ -3,6 +3,7 @@
#include <stddef.h>
#include <stdlib.h>
#include <string>
#include "SDL.h"
@ -22,30 +23,43 @@ unsigned char* WindowBackend_Software_CreateWindow(const char *window_title, int
if (window != NULL)
{
if (fullscreen)
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
{
if (SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN) < 0)
Backend_PrintError("Couldn't set window to fullscree: %s", SDL_GetError());
}
window_sdlsurface = SDL_GetWindowSurface(window);
framebuffer_sdlsurface = SDL_CreateRGBSurfaceWithFormat(0, window_sdlsurface->w, window_sdlsurface->h, 0, SDL_PIXELFORMAT_RGB24);
if (framebuffer_sdlsurface != NULL)
if (window_sdlsurface != NULL)
{
*pitch = framebuffer_sdlsurface->pitch;
Backend_PostWindowCreation();
framebuffer_sdlsurface = SDL_CreateRGBSurfaceWithFormat(0, window_sdlsurface->w, window_sdlsurface->h, 0, SDL_PIXELFORMAT_RGB24);
return (unsigned char*)framebuffer_sdlsurface->pixels;
if (framebuffer_sdlsurface != NULL)
{
*pitch = framebuffer_sdlsurface->pitch;
Backend_PostWindowCreation();
return (unsigned char*)framebuffer_sdlsurface->pixels;
}
else
{
std::string error_message = std::string("Couldn't create framebuffer surface: ") + SDL_GetError();
Backend_ShowMessageBox("Fatal error (software rendering backend)", error_message.c_str());
SDL_DestroyWindow(window);
}
}
else
{
Backend_ShowMessageBox("Fatal error (software rendering backend)", "Could not create framebuffer surface");
std::string error_message = std::string("Couldn't get SDL surface associated with window: ") + SDL_GetError();
Backend_ShowMessageBox("Fatal error (software rendering backend)", error_message.c_str());
}
SDL_DestroyWindow(window);
}
else
{
Backend_ShowMessageBox("Fatal error (software rendering backend)", "Could not create window");
std::string error_message = std::string("Couldn't create window: ") + SDL_GetError();
Backend_ShowMessageBox("Fatal error (software rendering backend)", error_message.c_str());
}
return NULL;
@ -59,8 +73,11 @@ void WindowBackend_Software_DestroyWindow(void)
void WindowBackend_Software_Display(void)
{
SDL_BlitSurface(framebuffer_sdlsurface, NULL, window_sdlsurface, NULL);
SDL_UpdateWindowSurface(window);
if (SDL_BlitSurface(framebuffer_sdlsurface, NULL, window_sdlsurface, NULL) < 0)
Backend_PrintError("Couldn't blit framebuffer surface to window surface: %s", SDL_GetError());
if (SDL_UpdateWindowSurface(window) < 0)
Backend_PrintError("Couldn't copy window surface to the screen: %s", SDL_GetError());
}
void WindowBackend_Software_HandleWindowResize(unsigned int width, unsigned int height)
@ -71,4 +88,7 @@ void WindowBackend_Software_HandleWindowResize(unsigned int width, unsigned int
// https://wiki.libsdl.org/SDL_GetWindowSurface
// We need to fetch a new surface pointer
window_sdlsurface = SDL_GetWindowSurface(window);
if (!window_sdlsurface)
Backend_PrintError("Couldn't get SDL surface associated with window: %s", SDL_GetError());
}