+ ReCreateBitmap(window, video.width, video.height, video.depth);
+}
+
+static SDL_Surface *SDLCreateScreen(DrawBuffer **backbuffer,
+ boolean fullscreen)
+{
+ SDL_Surface *new_surface = NULL;
+
+#if defined(TARGET_SDL2)
+ static boolean fullscreen_enabled = FALSE;
+ int surface_flags_window = SURFACE_FLAGS | SDL_WINDOW_RESIZABLE;
+#if USE_DESKTOP_FULLSCREEN
+ int surface_flags_fullscreen = SURFACE_FLAGS | SDL_WINDOW_FULLSCREEN_DESKTOP;
+#else
+ int surface_flags_fullscreen = SURFACE_FLAGS | SDL_WINDOW_FULLSCREEN;
+#endif
+
+#else
+ int surface_flags_window = SURFACE_FLAGS;
+ int surface_flags_fullscreen = SURFACE_FLAGS | SDL_FULLSCREEN;
+#endif
+
+ int width = (fullscreen ? fullscreen_width : video.width);
+ int height = (fullscreen ? fullscreen_height : video.height);
+ int surface_flags = (fullscreen ? surface_flags_fullscreen :
+ surface_flags_window);
+
+ // default window size is unscaled
+ video.window_width = video.width;
+ video.window_height = video.height;
+
+#if defined(TARGET_SDL2)
+
+ // store if initial screen mode on game start is fullscreen mode
+ if (sdl_window == NULL)
+ video.fullscreen_initial = fullscreen;
+
+#if USE_RENDERER
+ float window_scaling_factor = (float)setup.window_scaling_percent / 100;
+#if !USE_DESKTOP_FULLSCREEN
+ float screen_scaling_factor = (fullscreen ? 1 : window_scaling_factor);
+#endif
+
+ video.window_width = window_scaling_factor * width;
+ video.window_height = window_scaling_factor * height;
+
+ if ((*backbuffer)->surface)
+ {
+ SDL_FreeSurface((*backbuffer)->surface);
+ (*backbuffer)->surface = NULL;
+ }
+
+ if (sdl_texture)
+ {
+ SDL_DestroyTexture(sdl_texture);
+ sdl_texture = NULL;
+ }
+
+ if (!(fullscreen && fullscreen_enabled))
+ {
+ if (sdl_renderer)
+ {
+ SDL_DestroyRenderer(sdl_renderer);
+ sdl_renderer = NULL;
+ }
+
+ if (sdl_window)
+ {
+ SDL_DestroyWindow(sdl_window);
+ sdl_window = NULL;
+ }
+ }
+
+ if (sdl_window == NULL)
+ sdl_window = SDL_CreateWindow(program.window_title,
+ SDL_WINDOWPOS_CENTERED,
+ SDL_WINDOWPOS_CENTERED,
+#if USE_DESKTOP_FULLSCREEN
+ video.window_width,
+ video.window_height,
+#else
+ (int)(screen_scaling_factor * width),
+ (int)(screen_scaling_factor * height),
+#endif
+ surface_flags);
+
+ if (sdl_window != NULL)
+ {
+#if 0
+ /* if SDL_CreateRenderer() is called from within a VirtualBox Windows VM
+ *without* enabling 2D/3D acceleration and/or guest additions installed,
+ it will crash if flags are *not* set to SDL_RENDERER_SOFTWARE (because
+ it will try to use accelerated graphics and apparently fails miserably) */
+ if (sdl_renderer == NULL)
+ sdl_renderer = SDL_CreateRenderer(sdl_window, -1, SDL_RENDERER_SOFTWARE);
+#else
+ if (sdl_renderer == NULL)
+ sdl_renderer = SDL_CreateRenderer(sdl_window, -1, 0);
+#endif
+
+ if (sdl_renderer != NULL)
+ {
+ SDL_RenderSetLogicalSize(sdl_renderer, width, height);
+ // SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
+ SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, setup.window_scaling_quality);
+
+ sdl_texture = SDL_CreateTexture(sdl_renderer,
+ SDL_PIXELFORMAT_ARGB8888,
+ SDL_TEXTUREACCESS_STREAMING,
+ width, height);
+
+ if (sdl_texture != NULL)
+ {
+ // use SDL default values for RGB masks and no alpha channel
+ new_surface = SDL_CreateRGBSurface(0, width, height, 32, 0,0,0, 0);
+
+ if (new_surface == NULL)
+ Error(ERR_WARN, "SDL_CreateRGBSurface() failed: %s",
+ SDL_GetError());
+ }
+ else
+ {
+ Error(ERR_WARN, "SDL_CreateTexture() failed: %s", SDL_GetError());
+ }
+ }
+ else
+ {
+ Error(ERR_WARN, "SDL_CreateRenderer() failed: %s", SDL_GetError());
+ }
+ }
+ else
+ {
+ Error(ERR_WARN, "SDL_CreateWindow() failed: %s", SDL_GetError());
+ }
+
+#else
+
+ if (sdl_window)
+ SDL_DestroyWindow(sdl_window);
+
+ sdl_window = SDL_CreateWindow(program.window_title,
+ SDL_WINDOWPOS_CENTERED,
+ SDL_WINDOWPOS_CENTERED,
+ width, height,
+ surface_flags);
+
+ if (sdl_window != NULL)
+ new_surface = SDL_GetWindowSurface(sdl_window);
+#endif
+
+#else
+ new_surface = SDL_SetVideoMode(width, height, video.depth, surface_flags);
+#endif
+
+#if defined(TARGET_SDL2)
+ // store fullscreen state ("video.fullscreen_enabled" may not reflect this!)
+ if (new_surface != NULL)
+ fullscreen_enabled = fullscreen;
+#endif
+
+ return new_surface;