From aab5583552cf6e5289884b68b0d346c4419e0726 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Fri, 28 Oct 2022 21:27:59 +0300 Subject: [PATCH 23/23] sdl2: Do error checking in set_video_mode() See osdn #46000 Signed-off-by: Marko Lindqvist --- client/gui-sdl2/graphics.c | 58 ++++++++++++++++++++++++-------------- client/gui-sdl2/graphics.h | 2 +- client/gui-sdl2/gui_main.c | 8 ++++-- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/client/gui-sdl2/graphics.c b/client/gui-sdl2/graphics.c index 7e2b891b59..da2ea05181 100644 --- a/client/gui-sdl2/graphics.c +++ b/client/gui-sdl2/graphics.c @@ -549,48 +549,54 @@ void quit_sdl(void) } /************************************************************************** - Switch to passed video mode. + Switch to given video mode. **************************************************************************/ -int set_video_mode(int iWidth, int iHeight, int iFlags) +bool set_video_mode(int width, int height, int flags_in) { - unsigned int flags; + unsigned flags; Main.screen = SDL_CreateWindow(_("SDL2 Client for Freeciv"), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - iWidth, iHeight, + width, height, 0); - if (iFlags & SDL_WINDOW_FULLSCREEN) { + if (Main.screen == NULL) { + log_fatal(_("Failed to create main window: %s"), SDL_GetError()); + return FALSE; + } + + if (flags_in & SDL_WINDOW_FULLSCREEN) { SDL_DisplayMode mode; /* Use SDL_WINDOW_FULLSCREEN_DESKTOP instead of real SDL_WINDOW_FULLSCREEN */ SDL_SetWindowFullscreen(Main.screen, SDL_WINDOW_FULLSCREEN_DESKTOP); SDL_GetWindowDisplayMode(Main.screen, &mode); - iWidth = mode.w; - iHeight = mode.h; + width = mode.w; + height = mode.h; } if (is_bigendian()) { - main_surface = SDL_CreateRGBSurface(0, iWidth, iHeight, 32, + main_surface = SDL_CreateRGBSurface(0, width, height, 32, 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF); - } else { - main_surface = SDL_CreateRGBSurface(0, iWidth, iHeight, 32, - 0x00FF0000, 0x0000FF00, - 0x000000FF, 0xFF000000); - } - - if (is_bigendian()) { - Main.map = SDL_CreateRGBSurface(0, iWidth, iHeight, 32, + Main.map = SDL_CreateRGBSurface(0, width, height, 32, 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF); } else { - Main.map = SDL_CreateRGBSurface(0, iWidth, iHeight, 32, + main_surface = SDL_CreateRGBSurface(0, width, height, 32, + 0x00FF0000, 0x0000FF00, + 0x000000FF, 0xFF000000); + Main.map = SDL_CreateRGBSurface(0, width, height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); } + if (main_surface == NULL || Main.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 { @@ -599,21 +605,31 @@ int set_video_mode(int iWidth, int iHeight, int iFlags) Main.renderer = SDL_CreateRenderer(Main.screen, -1, flags); + if (Main.renderer == NULL) { + log_fatal(_("Failed to create renderer: %s"), SDL_GetError()); + return FALSE; + } + Main.maintext = SDL_CreateTexture(Main.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, - iWidth, iHeight); + width, height); + + if (Main.maintext == NULL) { + log_fatal(_("Failed to create texture: %s"), SDL_GetError()); + return FALSE; + } if (Main.gui) { FREESURFACE(Main.gui->surface); - Main.gui->surface = create_surf(iWidth, iHeight, SDL_SWSURFACE); + Main.gui->surface = create_surf(width, height, SDL_SWSURFACE); } else { - Main.gui = add_gui_layer(iWidth, iHeight); + Main.gui = add_gui_layer(width, height); } clear_surface(Main.gui->surface, NULL); - return 0; + return TRUE; } /************************************************************************** diff --git a/client/gui-sdl2/graphics.h b/client/gui-sdl2/graphics.h index 5e05c7eb27..f2f4c29605 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 iWidth, int iHeight, int iFlags); +bool set_video_mode(int width, int height, int flags_in); void update_main_screen(void); diff --git a/client/gui-sdl2/gui_main.c b/client/gui-sdl2/gui_main.c index a647194973..ef21e5040c 100644 --- a/client/gui-sdl2/gui_main.c +++ b/client/gui-sdl2/gui_main.c @@ -983,9 +983,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