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 "../../WindowsWrapper.h"
#include "../Misc.h" #include "../Misc.h"
#define DEADZONE 10000; #define DEADZONE 10000
static SDL_Joystick *joystick; static SDL_Joystick *joystick;
@ -141,7 +141,14 @@ BOOL ControllerBackend_ResetJoystickStatus(void)
void ControllerBackend_JoystickConnect(Sint32 joystick_id) 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) if (joystick == NULL)
{ {
@ -150,32 +157,51 @@ void ControllerBackend_JoystickConnect(Sint32 joystick_id)
if (joystick != NULL) if (joystick != NULL)
{ {
int total_axes = SDL_JoystickNumAxes(joystick); 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); 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) 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 // Set up neutral axes
axis_neutrals = (Sint16*)malloc(sizeof(Sint16) * total_axes); axis_neutrals = (Sint16*)malloc(sizeof(Sint16) * total_axes);
if (axis_neutrals)
{
for (int i = 0; i < total_axes; ++i) for (int i = 0; i < total_axes; ++i)
axis_neutrals[i] = SDL_JoystickGetAxis(joystick, i); axis_neutrals[i] = SDL_JoystickGetAxis(joystick, i);
} }
else else
{
Backend_PrintError("Couldn't allocate memory for neutral axes");
}
}
else
{ {
SDL_JoystickClose(joystick); SDL_JoystickClose(joystick);
joystick = NULL; joystick = NULL;
} }
} }
else
{
Backend_PrintError("Couldn't open joystick for use: %s", SDL_GetError());
}
} }
} }
void ControllerBackend_JoystickDisconnect(Sint32 joystick_id) 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); SDL_JoystickClose(joystick);
joystick = NULL; joystick = NULL;

View file

@ -2,6 +2,7 @@
#include "Window.h" #include "Window.h"
#include <stddef.h> #include <stddef.h>
#include <string>
#ifdef USE_OPENGLES2 #ifdef USE_OPENGLES2
#include <GLES2/gl2.h> #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) BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_width, int *screen_height, BOOL fullscreen)
{ {
#ifdef USE_OPENGLES2 #ifdef USE_OPENGLES2
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES) < 0)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); Backend_PrintError("Couldn't set OpenGL context type to ES: %s", SDL_GetError());
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_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 #else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) < 0)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); Backend_PrintError("Couldn't set OpenGL context type to core: %s", SDL_GetError());
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_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 #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)); 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 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 #endif
} }
else 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"); 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 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"); 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 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; return FALSE;

View file

@ -3,6 +3,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <string>
#include "SDL.h" #include "SDL.h"
@ -22,10 +23,16 @@ unsigned char* WindowBackend_Software_CreateWindow(const char *window_title, int
if (window != NULL) if (window != NULL)
{ {
if (fullscreen) 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); window_sdlsurface = SDL_GetWindowSurface(window);
if (window_sdlsurface != NULL)
{
framebuffer_sdlsurface = SDL_CreateRGBSurfaceWithFormat(0, window_sdlsurface->w, window_sdlsurface->h, 0, SDL_PIXELFORMAT_RGB24); framebuffer_sdlsurface = SDL_CreateRGBSurfaceWithFormat(0, window_sdlsurface->w, window_sdlsurface->h, 0, SDL_PIXELFORMAT_RGB24);
if (framebuffer_sdlsurface != NULL) if (framebuffer_sdlsurface != NULL)
@ -38,14 +45,21 @@ unsigned char* WindowBackend_Software_CreateWindow(const char *window_title, int
} }
else else
{ {
Backend_ShowMessageBox("Fatal error (software rendering backend)", "Could not create framebuffer surface"); 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); SDL_DestroyWindow(window);
} }
}
else else
{ {
Backend_ShowMessageBox("Fatal error (software rendering backend)", "Could not create window"); 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());
}
}
else
{
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; return NULL;
@ -59,8 +73,11 @@ void WindowBackend_Software_DestroyWindow(void)
void WindowBackend_Software_Display(void) void WindowBackend_Software_Display(void)
{ {
SDL_BlitSurface(framebuffer_sdlsurface, NULL, window_sdlsurface, NULL); if (SDL_BlitSurface(framebuffer_sdlsurface, NULL, window_sdlsurface, NULL) < 0)
SDL_UpdateWindowSurface(window); 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) 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 // https://wiki.libsdl.org/SDL_GetWindowSurface
// We need to fetch a new surface pointer // We need to fetch a new surface pointer
window_sdlsurface = SDL_GetWindowSurface(window); window_sdlsurface = SDL_GetWindowSurface(window);
if (!window_sdlsurface)
Backend_PrintError("Couldn't get SDL surface associated with window: %s", SDL_GetError());
} }