From 41d5c5b5c8fc3cc0766c6e01e4e020b1c4a15440 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Tue, 7 Apr 2020 17:46:02 +0100 Subject: [PATCH] Make it so `Backend_Init` can fail --- src/Backends/GLFW3/Misc.cpp | 7 +++++-- src/Backends/Misc.h | 2 +- src/Backends/SDL2/Misc.cpp | 26 +++++++++++++++++--------- src/Main.cpp | 3 ++- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/Backends/GLFW3/Misc.cpp b/src/Backends/GLFW3/Misc.cpp index 73240b38..c025c33d 100644 --- a/src/Backends/GLFW3/Misc.cpp +++ b/src/Backends/GLFW3/Misc.cpp @@ -150,9 +150,12 @@ static void DragAndDropCallback(GLFWwindow *window, int count, const char **path LoadProfile(paths[0]); } -void Backend_Init(void) +BOOL Backend_Init(void) { - glfwInit(); + if (glfwInit() == GL_TRUE) + return TRUE; + else + return FALSE; } void Backend_Deinit(void) diff --git a/src/Backends/Misc.h b/src/Backends/Misc.h index 7cdf3d2f..adf99783 100644 --- a/src/Backends/Misc.h +++ b/src/Backends/Misc.h @@ -85,7 +85,7 @@ enum extern BOOL bActive; -void Backend_Init(void); +BOOL Backend_Init(void); void Backend_Deinit(void); void Backend_PostWindowCreation(void); BOOL Backend_GetBasePath(char *string_buffer); diff --git a/src/Backends/SDL2/Misc.cpp b/src/Backends/SDL2/Misc.cpp index aa8e0511..1cdfc955 100644 --- a/src/Backends/SDL2/Misc.cpp +++ b/src/Backends/SDL2/Misc.cpp @@ -30,21 +30,29 @@ static unsigned char *cursor_surface_pixels; static SDL_Surface *cursor_surface; static SDL_Cursor *cursor; -void Backend_Init(void) +BOOL Backend_Init(void) { - SDL_Init(SDL_INIT_EVENTS); + if (SDL_Init(SDL_INIT_EVENTS) == 0) + { + if (SDL_InitSubSystem(SDL_INIT_VIDEO) == 0) + { + puts("Available SDL2 video drivers:"); - SDL_InitSubSystem(SDL_INIT_VIDEO); + for (int i = 0; i < SDL_GetNumVideoDrivers(); ++i) + puts(SDL_GetVideoDriver(i)); - puts("Available SDL2 video drivers:"); + const char *driver = SDL_GetCurrentVideoDriver(); - for (int i = 0; i < SDL_GetNumVideoDrivers(); ++i) - puts(SDL_GetVideoDriver(i)); + if (driver != NULL) + printf("Selected SDL2 video driver: %s\n", driver); - const char *driver = SDL_GetCurrentVideoDriver(); + return TRUE; + } - if (driver != NULL) - printf("Selected SDL2 video driver: %s\n", driver); + SDL_Quit(); + } + + return FALSE; } void Backend_Deinit(void) diff --git a/src/Main.cpp b/src/Main.cpp index 4979237e..b4f70a11 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -88,7 +88,8 @@ int main(int argc, char *argv[]) int i; - Backend_Init(); + if (!Backend_Init()) + return EXIT_FAILURE; // Get executable's path if (!Backend_GetBasePath(gModulePath))