From 7723506e197803ac00a8f2c066c705a36f46cada Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Fri, 28 Oct 2022 21:20:40 +0300 Subject: [PATCH 29/29] sdl2: Do error checking in set_video_mode() See osdn #46000 Signed-off-by: Marko Lindqvist --- client/gui-sdl2/graphics.c | 38 +++++++++++++++++++++++++++----------- client/gui-sdl2/graphics.h | 2 +- client/gui-sdl2/gui_main.c | 8 +++++--- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/client/gui-sdl2/graphics.c b/client/gui-sdl2/graphics.c index ff0b70a6c4..168049c183 100644 --- a/client/gui-sdl2/graphics.c +++ b/client/gui-sdl2/graphics.c @@ -551,11 +551,11 @@ void quit_sdl(void) } /**********************************************************************//** - Switch to passed video mode. + Switch to given video mode. **************************************************************************/ -int set_video_mode(int width, int height, int flags_in) +bool set_video_mode(unsigned width, unsigned height, unsigned flags_in) { - unsigned int flags; + unsigned flags; main_data.screen = SDL_CreateWindow(_("SDL2 Client for Freeciv"), SDL_WINDOWPOS_UNDEFINED, @@ -563,6 +563,11 @@ int set_video_mode(int width, int height, int flags_in) width, height, 0); + if (main_data.screen == NULL) { + log_fatal(_("Failed to create main window: %s"), SDL_GetError()); + return FALSE; + } + if (flags_in & SDL_WINDOW_FULLSCREEN) { SDL_DisplayMode mode; @@ -577,22 +582,23 @@ int set_video_mode(int width, int height, int flags_in) main_surface = SDL_CreateRGBSurface(0, width, height, 32, 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF); - } else { - main_surface = SDL_CreateRGBSurface(0, width, height, 32, - 0x00FF0000, 0x0000FF00, - 0x000000FF, 0xFF000000); - } - - if (is_bigendian()) { main_data.map = SDL_CreateRGBSurface(0, width, height, 32, 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF); } else { + main_surface = SDL_CreateRGBSurface(0, width, height, 32, + 0x00FF0000, 0x0000FF00, + 0x000000FF, 0xFF000000); main_data.map = SDL_CreateRGBSurface(0, width, height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); } + if (main_surface == NULL || main_data.map == NULL) { + log_fatal(_("Failed to create RGB surface: %s"), SDL_GetError()); + return FALSE; + } + if (gui_options.gui_sdl2_swrenderer) { flags = SDL_RENDERER_SOFTWARE; } else { @@ -601,11 +607,21 @@ int set_video_mode(int width, int height, int flags_in) main_data.renderer = SDL_CreateRenderer(main_data.screen, -1, flags); + if (main_data.renderer == NULL) { + log_fatal(_("Failed to create renderer: %s"), SDL_GetError()); + return FALSE; + } + main_data.maintext = SDL_CreateTexture(main_data.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height); + if (main_data.maintext == NULL) { + log_fatal(_("Failed to create texture: %s"), SDL_GetError()); + return FALSE; + } + if (main_data.gui) { FREESURFACE(main_data.gui->surface); main_data.gui->surface = create_surf(width, height, SDL_SWSURFACE); @@ -615,7 +631,7 @@ int set_video_mode(int width, int height, int flags_in) clear_surface(main_data.gui->surface, NULL); - return 0; + return TRUE; } /**********************************************************************//** diff --git a/client/gui-sdl2/graphics.h b/client/gui-sdl2/graphics.h index 579687eebe..74dc9d1cf2 100644 --- a/client/gui-sdl2/graphics.h +++ b/client/gui-sdl2/graphics.h @@ -278,7 +278,7 @@ void create_line(SDL_Surface *dest, Sint16 x0, Sint16 y0, Sint16 x1, Sint16 y1, /* SDL */ void init_sdl(int f); void quit_sdl(void); -int set_video_mode(int width, int height, int flags); +bool set_video_mode(unsigned width, unsigned height, unsigned flags); void update_main_screen(void); diff --git a/client/gui-sdl2/gui_main.c b/client/gui-sdl2/gui_main.c index 3ab584e7b3..bc718e6103 100644 --- a/client/gui-sdl2/gui_main.c +++ b/client/gui-sdl2/gui_main.c @@ -990,9 +990,11 @@ int ui_main(int argc, char *argv[]) flags &= ~SDL_WINDOW_FULLSCREEN; } log_normal(_("Using Video Output: %s"), SDL_GetCurrentVideoDriver()); - set_video_mode(gui_options.gui_sdl2_screen.width, - gui_options.gui_sdl2_screen.height, - flags); + if (!set_video_mode(gui_options.gui_sdl2_screen.width, + gui_options.gui_sdl2_screen.height, + flags)) { + return EXIT_FAILURE; + } user_event_type = SDL_RegisterEvents(1); -- 2.35.1