+ // (setting the window icon on Mac OS X would replace the high-quality
+ // dock icon with the currently smaller (and uglier) icon from file)
+
+#if !defined(PLATFORM_MACOSX)
+ char *filename = getCustomImageFilename(basename);
+ SDL_Surface *surface;
+
+ if (filename == NULL)
+ {
+ Error(ERR_WARN, "SDLSetWindowIcon(): cannot find file '%s'", basename);
+
+ return;
+ }
+
+ if ((surface = IMG_Load(filename)) == NULL)
+ {
+ Error(ERR_WARN, "IMG_Load('%s') failed: %s", basename, SDL_GetError());
+
+ return;
+ }
+
+ // set transparent color
+ SDL_SetColorKey(surface, SET_TRANSPARENT_PIXEL,
+ SDL_MapRGB(surface->format, 0x00, 0x00, 0x00));
+
+ SDL_SetWindowIcon(sdl_window, surface);
+#endif
+}
+
+static boolean equalSDLPixelFormat(SDL_PixelFormat *format1,
+ SDL_PixelFormat *format2)
+{
+ return (format1->format == format2->format &&
+ format1->BitsPerPixel == format2->BitsPerPixel &&
+ format1->BytesPerPixel == format2->BytesPerPixel &&
+ format1->Rmask == format2->Rmask &&
+ format1->Gmask == format2->Gmask &&
+ format1->Bmask == format2->Bmask);
+}
+
+static Pixel SDLGetColorKey(SDL_Surface *surface)
+{
+ Pixel color_key;
+
+ if (SDL_GetColorKey(surface, &color_key) != 0)
+ return -1;
+
+ return color_key;
+}
+
+static boolean SDLHasColorKey(SDL_Surface *surface)
+{
+ return (SDLGetColorKey(surface) != -1);
+}
+
+static boolean SDLHasAlpha(SDL_Surface *surface)
+{
+ SDL_BlendMode blend_mode;
+
+ if (SDL_GetSurfaceBlendMode(surface, &blend_mode) != 0)
+ return FALSE;
+
+ return (blend_mode == SDL_BLENDMODE_BLEND);
+}
+
+static void SDLSetAlpha(SDL_Surface *surface, boolean set, int alpha)
+{
+ SDL_BlendMode blend_mode = (set ? SDL_BLENDMODE_BLEND : SDL_BLENDMODE_NONE);
+
+ SDL_SetSurfaceBlendMode(surface, blend_mode);
+ SDL_SetSurfaceAlphaMod(surface, alpha);
+}
+
+SDL_Surface *SDLGetNativeSurface(SDL_Surface *surface)
+{
+ SDL_PixelFormat format;
+ SDL_Surface *new_surface;
+
+ if (surface == NULL)
+ return NULL;
+
+ if (backbuffer && backbuffer->surface)
+ {
+ format = *backbuffer->surface->format;
+ format.Amask = surface->format->Amask; // keep alpha channel
+ }
+ else
+ {
+ format = *surface->format;
+ }
+
+ new_surface = SDL_ConvertSurface(surface, &format, 0);
+
+ if (new_surface == NULL)
+ Error(ERR_EXIT, "SDL_ConvertSurface() failed: %s", SDL_GetError());
+
+ // workaround for a bug in SDL 2.0.12 (which does not convert the color key)
+ if (SDLHasColorKey(surface) && !SDLHasColorKey(new_surface))
+ SDL_SetColorKey(new_surface, SET_TRANSPARENT_PIXEL,
+ SDLGetColorKey(surface));
+
+ return new_surface;
+}
+
+boolean SDLSetNativeSurface(SDL_Surface **surface)
+{
+ SDL_Surface *new_surface;
+
+ if (surface == NULL ||
+ *surface == NULL ||
+ backbuffer == NULL ||
+ backbuffer->surface == NULL)
+ return FALSE;
+
+ // if pixel format already optimized for destination surface, do nothing
+ if (equalSDLPixelFormat((*surface)->format, backbuffer->surface->format))
+ return FALSE;
+
+ new_surface = SDLGetNativeSurface(*surface);
+
+ SDL_FreeSurface(*surface);
+
+ *surface = new_surface;
+
+ return TRUE;
+}
+
+static SDL_Texture *SDLCreateTextureFromSurface(SDL_Surface *surface)
+{
+ if (program.headless)
+ return NULL;
+
+ SDL_Texture *texture = SDL_CreateTextureFromSurface(sdl_renderer, surface);
+
+ if (texture == NULL)
+ Error(ERR_EXIT, "SDL_CreateTextureFromSurface() failed: %s",
+ SDL_GetError());
+
+ return texture;
+}
+
+void SDLCreateBitmapTextures(Bitmap *bitmap)
+{
+ if (bitmap == NULL)
+ return;
+
+ if (bitmap->texture)
+ SDL_DestroyTexture(bitmap->texture);
+ if (bitmap->texture_masked)
+ SDL_DestroyTexture(bitmap->texture_masked);
+
+ bitmap->texture = SDLCreateTextureFromSurface(bitmap->surface);
+ bitmap->texture_masked = SDLCreateTextureFromSurface(bitmap->surface_masked);
+}
+
+void SDLFreeBitmapTextures(Bitmap *bitmap)
+{
+ if (bitmap == NULL)
+ return;
+
+ if (bitmap->texture)
+ SDL_DestroyTexture(bitmap->texture);
+ if (bitmap->texture_masked)
+ SDL_DestroyTexture(bitmap->texture_masked);
+
+ bitmap->texture = NULL;
+ bitmap->texture_masked = NULL;
+}
+
+void SDLInitVideoDisplay(void)
+{
+ // initialize SDL video
+ if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
+ Error(ERR_EXIT, "SDL_InitSubSystem() failed: %s", SDL_GetError());
+
+ // set default SDL depth
+ video.default_depth = 32; // (how to determine video depth in SDL2?)
+ //
+ // Code used with SDL 1.2:
+ // video.default_depth = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
+}
+
+static void SDLInitVideoBuffer_VideoBuffer(boolean fullscreen)
+{
+ if (program.headless)
+ return;
+
+ video.window_scaling_percent = setup.window_scaling_percent;
+ video.window_scaling_quality = setup.window_scaling_quality;
+
+ SDLSetScreenRenderingMode(setup.screen_rendering_mode);
+
+ // SDL 2.0: support for (desktop) fullscreen mode available
+ video.fullscreen_available = TRUE;
+
+ // open SDL video output device (window or fullscreen mode)
+ if (!SDLSetVideoMode(fullscreen))
+ Error(ERR_EXIT, "setting video mode failed");
+
+ // !!! SDL2 can only set the window icon if the window already exists !!!
+ // set window icon
+ SDLSetWindowIcon(program.icon_filename);
+
+ // set window and icon title
+ SDLSetWindowTitle();
+}
+
+static void SDLInitVideoBuffer_DrawBuffer(void)
+{
+ /* SDL cannot directly draw to the visible video framebuffer like X11,
+ but always uses a backbuffer, which is then blitted to the visible
+ video framebuffer with 'SDL_UpdateRect' (or replaced with the current
+ visible video framebuffer with 'SDL_Flip', if the hardware supports
+ this). Therefore do not use an additional backbuffer for drawing, but
+ use a symbolic buffer (distinguishable from the SDL backbuffer) called
+ 'window', which indicates that the SDL backbuffer should be updated to
+ the visible video framebuffer when attempting to blit to it.
+
+ For convenience, it seems to be a good idea to create this symbolic
+ buffer 'window' at the same size as the SDL backbuffer. Although it
+ should never be drawn to directly, it would do no harm nevertheless. */
+
+ // create additional (symbolic) buffer for double-buffering
+ ReCreateBitmap(&window, video.width, video.height);
+
+ // create dummy drawing buffer for headless mode, if needed
+ if (program.headless)
+ ReCreateBitmap(&backbuffer, video.width, video.height);
+}
+
+void SDLInitVideoBuffer(boolean fullscreen)
+{
+ SDLInitVideoBuffer_VideoBuffer(fullscreen);
+ SDLInitVideoBuffer_DrawBuffer();
+}
+
+static boolean SDLCreateScreen(boolean fullscreen)
+{
+ SDL_Surface *new_surface = NULL;
+
+ int surface_flags_window = SURFACE_FLAGS | SDL_WINDOW_RESIZABLE;
+ int surface_flags_fullscreen = SURFACE_FLAGS | SDL_WINDOW_FULLSCREEN_DESKTOP;
+
+#if 1
+ int renderer_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE;
+#else
+ /* 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) */
+ int renderer_flags = SDL_RENDERER_SOFTWARE;
+#endif
+
+ SDLSetScreenSizeAndOffsets(video.width, video.height);
+
+ int width = video.width;
+ int height = video.height;
+ int screen_width = video.screen_width;
+ int screen_height = video.screen_height;
+ int surface_flags = (fullscreen ? surface_flags_fullscreen :
+ surface_flags_window);
+
+ // default window size is unscaled
+ video.window_width = screen_width;
+ video.window_height = screen_height;
+
+ // store if initial screen mode is fullscreen mode when changing screen size
+ video.fullscreen_initial = fullscreen;
+
+ float window_scaling_factor = (float)setup.window_scaling_percent / 100;
+
+ video.window_width = window_scaling_factor * screen_width;
+ video.window_height = window_scaling_factor * screen_height;
+
+ if (sdl_texture_stream)
+ {
+ SDL_DestroyTexture(sdl_texture_stream);
+ sdl_texture_stream = NULL;
+ }
+
+ if (sdl_texture_target)
+ {
+ SDL_DestroyTexture(sdl_texture_target);
+ sdl_texture_target = 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,
+ video.window_width,
+ video.window_height,
+ surface_flags);
+
+ if (sdl_window != NULL)
+ {
+ if (sdl_renderer == NULL)
+ sdl_renderer = SDL_CreateRenderer(sdl_window, -1, renderer_flags);
+
+ if (sdl_renderer != NULL)
+ {
+ SDL_RenderSetLogicalSize(sdl_renderer, screen_width, screen_height);
+ // SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
+ SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, setup.window_scaling_quality);
+
+ SDLSetScreenVsyncMode(setup.vsync_mode);
+
+ sdl_texture_stream = SDL_CreateTexture(sdl_renderer,
+ SDL_PIXELFORMAT_ARGB8888,
+ SDL_TEXTUREACCESS_STREAMING,
+ width, height);
+
+ if (SDL_RenderTargetSupported(sdl_renderer))
+ sdl_texture_target = SDL_CreateTexture(sdl_renderer,
+ SDL_PIXELFORMAT_ARGB8888,
+ SDL_TEXTUREACCESS_TARGET,
+ width, height);
+
+ if (sdl_texture_stream != 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());
+ }
+
+ // store fullscreen state ("video.fullscreen_enabled" may not reflect this!)
+ if (new_surface != NULL)
+ fullscreen_enabled = fullscreen;
+
+ if (backbuffer == NULL)
+ backbuffer = CreateBitmapStruct();
+
+ backbuffer->width = video.width;
+ backbuffer->height = video.height;
+
+ if (backbuffer->surface)
+ SDL_FreeSurface(backbuffer->surface);
+
+ backbuffer->surface = new_surface;
+
+ return (new_surface != NULL);
+}
+
+boolean SDLSetVideoMode(boolean fullscreen)
+{
+ boolean success = FALSE;
+
+ SetWindowTitle();
+
+ if (fullscreen && !video.fullscreen_enabled && video.fullscreen_available)
+ {
+ // switch display to fullscreen mode, if available
+ success = SDLCreateScreen(TRUE);
+
+ if (!success)
+ {
+ // switching display to fullscreen mode failed -- do not try it again
+ video.fullscreen_available = FALSE;
+ }
+ else
+ {
+ video.fullscreen_enabled = TRUE;
+ }
+ }
+
+ if ((!fullscreen && video.fullscreen_enabled) || !success)
+ {
+ // switch display to window mode
+ success = SDLCreateScreen(FALSE);
+
+ if (!success)
+ {
+ // switching display to window mode failed -- should not happen
+ }
+ else
+ {
+ video.fullscreen_enabled = FALSE;
+ video.window_scaling_percent = setup.window_scaling_percent;
+ video.window_scaling_quality = setup.window_scaling_quality;
+
+ SDLSetScreenRenderingMode(setup.screen_rendering_mode);
+ }
+ }
+
+ SDLRedrawWindow(); // map window
+
+ return success;
+}
+
+void SDLSetWindowTitle(void)
+{
+ if (sdl_window == NULL)
+ return;
+
+ SDL_SetWindowTitle(sdl_window, program.window_title);
+}
+
+void SDLSetWindowScaling(int window_scaling_percent)
+{
+ if (sdl_window == NULL)
+ return;
+
+ float window_scaling_factor = (float)window_scaling_percent / 100;
+ int new_window_width = (int)(window_scaling_factor * video.screen_width);
+ int new_window_height = (int)(window_scaling_factor * video.screen_height);
+
+ SDL_SetWindowSize(sdl_window, new_window_width, new_window_height);
+
+ video.window_scaling_percent = window_scaling_percent;
+ video.window_width = new_window_width;
+ video.window_height = new_window_height;
+
+ SetWindowTitle();
+}
+
+void SDLSetWindowScalingQuality(char *window_scaling_quality)
+{
+ SDL_Texture *new_texture;
+
+ if (sdl_texture_stream == NULL)
+ return;
+
+ SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, window_scaling_quality);
+
+ new_texture = SDL_CreateTexture(sdl_renderer,
+ SDL_PIXELFORMAT_ARGB8888,
+ SDL_TEXTUREACCESS_STREAMING,
+ video.width, video.height);
+
+ if (new_texture != NULL)
+ {
+ SDL_DestroyTexture(sdl_texture_stream);
+
+ sdl_texture_stream = new_texture;
+ }
+
+ if (SDL_RenderTargetSupported(sdl_renderer))
+ new_texture = SDL_CreateTexture(sdl_renderer,
+ SDL_PIXELFORMAT_ARGB8888,
+ SDL_TEXTUREACCESS_TARGET,
+ video.width, video.height);
+ else
+ new_texture = NULL;