+ 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, video.depth);
+}
+
+static SDL_Surface *SDLCreateScreen(DrawBuffer **backbuffer,
+ boolean fullscreen)
+{
+ SDL_Surface *new_surface = NULL;
+
+#if defined(TARGET_SDL2)
+ int surface_flags_window = SURFACE_FLAGS | SDL_WINDOW_RESIZABLE;
+ int surface_flags_fullscreen = SURFACE_FLAGS | SDL_WINDOW_FULLSCREEN_DESKTOP;
+#else
+ int surface_flags_window = SURFACE_FLAGS;
+ int surface_flags_fullscreen = SURFACE_FLAGS; // (no fullscreen in SDL 1.2)
+#endif
+
+ int width = video.width;
+ int 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 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 * width;
+ video.window_height = window_scaling_factor * height;
+
+ if ((*backbuffer)->surface)
+ {
+ SDL_FreeSurface((*backbuffer)->surface);
+ (*backbuffer)->surface = NULL;
+ }
+
+#if USE_TARGET_TEXTURE
+ 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;
+ }
+#else
+ if (sdl_texture)
+ {
+ SDL_DestroyTexture(sdl_texture);
+ sdl_texture = NULL;
+ }
+#endif
+
+ 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 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);
+
+#if USE_TARGET_TEXTURE
+ sdl_texture_stream = SDL_CreateTexture(sdl_renderer,
+ SDL_PIXELFORMAT_ARGB8888,
+ SDL_TEXTUREACCESS_STREAMING,
+ width, height);
+
+ sdl_texture_target = SDL_CreateTexture(sdl_renderer,
+ SDL_PIXELFORMAT_ARGB8888,
+ SDL_TEXTUREACCESS_TARGET,
+ width, height);
+#else
+ sdl_texture = SDL_CreateTexture(sdl_renderer,
+ SDL_PIXELFORMAT_ARGB8888,
+ SDL_TEXTUREACCESS_STREAMING,
+ width, height);
+#endif
+
+#if USE_TARGET_TEXTURE
+ if (sdl_texture_stream != NULL &&
+ sdl_texture_target != NULL)
+#else
+ if (sdl_texture != NULL)
+#endif
+ {
+ // 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
+ new_surface = SDL_SetVideoMode(width, height, video.depth, surface_flags);
+
+ if (new_surface == NULL)
+ Error(ERR_WARN, "SDL_SetVideoMode() failed: %s", SDL_GetError());
+#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;
+}
+
+boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
+{
+ boolean success = TRUE;
+ SDL_Surface *new_surface = NULL;
+
+ SetWindowTitle();
+
+ if (*backbuffer == NULL)
+ *backbuffer = CreateBitmapStruct();
+
+ /* (real bitmap might be larger in fullscreen mode with video offsets) */
+ (*backbuffer)->width = video.width;
+ (*backbuffer)->height = video.height;
+
+ if (fullscreen && !video.fullscreen_enabled && video.fullscreen_available)
+ {
+ /* switch display to fullscreen mode, if available */
+ new_surface = SDLCreateScreen(backbuffer, TRUE);
+
+ if (new_surface == NULL)
+ {
+ /* switching display to fullscreen mode failed -- do not try it again */
+ video.fullscreen_available = FALSE;
+
+ success = FALSE;
+ }
+ else
+ {
+ (*backbuffer)->surface = new_surface;
+
+ video.fullscreen_enabled = TRUE;
+
+ success = TRUE;
+ }
+ }
+
+ if ((!fullscreen && video.fullscreen_enabled) || new_surface == NULL)
+ {
+ /* switch display to window mode */
+ new_surface = SDLCreateScreen(backbuffer, FALSE);
+
+ if (new_surface == NULL)
+ {
+ /* switching display to window mode failed -- should not happen */
+
+ success = FALSE;
+ }
+ else
+ {
+ (*backbuffer)->surface = new_surface;
+
+ video.fullscreen_enabled = FALSE;
+ video.window_scaling_percent = setup.window_scaling_percent;
+ video.window_scaling_quality = setup.window_scaling_quality;
+
+ success = TRUE;
+ }
+ }
+
+#if defined(TARGET_SDL2)
+ SDLRedrawWindow(); // map window
+#endif
+
+#ifdef DEBUG
+#if defined(PLATFORM_WIN32)
+ // experimental drag and drop code
+
+ SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
+
+ {
+ SDL_SysWMinfo wminfo;
+ HWND hwnd;
+ boolean wminfo_success = FALSE;
+
+ SDL_VERSION(&wminfo.version);
+#if defined(TARGET_SDL2)
+ if (sdl_window)
+ wminfo_success = SDL_GetWindowWMInfo(sdl_window, &wminfo);
+#else
+ wminfo_success = (SDL_GetWMInfo(&wminfo) == 1);
+#endif
+
+ if (wminfo_success)
+ {
+#if defined(TARGET_SDL2)
+ hwnd = wminfo.info.win.window;
+#else
+ hwnd = wminfo.window;
+#endif
+
+ DragAcceptFiles(hwnd, TRUE);
+ }
+ }
+#endif
+#endif
+
+ return success;
+}
+
+void SDLSetWindowTitle()
+{
+#if defined(TARGET_SDL2)
+ SDL_SetWindowTitle(sdl_window, program.window_title);
+#else
+ SDL_WM_SetCaption(program.window_title, program.window_title);
+#endif
+}
+
+#if defined(TARGET_SDL2)
+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.width);
+ int new_window_height = (int)(window_scaling_factor * video.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)
+{
+#if USE_TARGET_TEXTURE
+ SDL_Texture *new_texture;
+
+ if (sdl_texture_stream == NULL ||
+ sdl_texture_target == 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;
+ }
+
+ new_texture = SDL_CreateTexture(sdl_renderer,
+ SDL_PIXELFORMAT_ARGB8888,
+ SDL_TEXTUREACCESS_TARGET,
+ video.width, video.height);
+
+ if (new_texture != NULL)
+ {
+ SDL_DestroyTexture(sdl_texture_target);
+
+ sdl_texture_target = new_texture;
+ }
+
+ SDLRedrawWindow();
+
+#else
+ if (sdl_texture == NULL)
+ return;
+
+ SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, window_scaling_quality);
+
+ SDL_Texture *new_texture = SDL_CreateTexture(sdl_renderer,
+ SDL_PIXELFORMAT_ARGB8888,
+ SDL_TEXTUREACCESS_STREAMING,
+ video.width, video.height);
+
+ if (new_texture != NULL)
+ {
+ SDL_DestroyTexture(sdl_texture);
+
+ sdl_texture = new_texture;
+
+ SDLRedrawWindow();
+ }
+#endif
+
+ video.window_scaling_quality = window_scaling_quality;
+}
+
+void SDLSetWindowFullscreen(boolean fullscreen)
+{
+ if (sdl_window == NULL)
+ return;
+
+ int flags = (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
+
+ if (SDL_SetWindowFullscreen(sdl_window, flags) == 0)
+ video.fullscreen_enabled = fullscreen_enabled = fullscreen;
+
+ // if screen size was changed in fullscreen mode, correct desktop window size
+ if (!fullscreen && video.fullscreen_initial)
+ {
+ SDLSetWindowScaling(setup.window_scaling_percent);
+ SDL_SetWindowPosition(sdl_window,
+ SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
+
+ video.fullscreen_initial = FALSE;
+ }
+}
+
+void SDLRedrawWindow()
+{
+ UpdateScreen(NULL);
+}
+#endif
+
+void SDLCreateBitmapContent(Bitmap *bitmap, int width, int height,
+ int depth)
+{
+ SDL_Surface *surface =
+ SDL_CreateRGBSurface(SURFACE_FLAGS, width, height, depth, 0,0,0, 0);
+
+ if (surface == NULL)
+ Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
+
+ SDLSetNativeSurface(&surface);
+
+ bitmap->surface = surface;
+}
+
+void SDLFreeBitmapPointers(Bitmap *bitmap)
+{
+ if (bitmap->surface)
+ SDL_FreeSurface(bitmap->surface);
+ if (bitmap->surface_masked)
+ SDL_FreeSurface(bitmap->surface_masked);
+
+ bitmap->surface = NULL;
+ bitmap->surface_masked = NULL;
+
+#if defined(TARGET_SDL2)
+ if (bitmap->texture)
+ SDL_DestroyTexture(bitmap->texture);
+ if (bitmap->texture_masked)
+ SDL_DestroyTexture(bitmap->texture_masked);
+
+ bitmap->texture = NULL;
+ bitmap->texture_masked = NULL;
+#endif
+}
+
+void SDLCopyArea(Bitmap *src_bitmap, Bitmap *dst_bitmap,
+ int src_x, int src_y, int width, int height,
+ int dst_x, int dst_y, int mask_mode)
+{
+ Bitmap *real_dst_bitmap = (dst_bitmap == window ? backbuffer : dst_bitmap);
+ SDL_Rect src_rect, dst_rect;
+
+ src_rect.x = src_x;
+ src_rect.y = src_y;
+ src_rect.w = width;
+ src_rect.h = height;
+
+ dst_rect.x = dst_x;
+ dst_rect.y = dst_y;
+ dst_rect.w = width;
+ dst_rect.h = height;
+
+ // if (src_bitmap != backbuffer || dst_bitmap != window)
+ if (!(src_bitmap == backbuffer && dst_bitmap == window))
+ SDL_BlitSurface((mask_mode == BLIT_MASKED ?
+ src_bitmap->surface_masked : src_bitmap->surface),
+ &src_rect, real_dst_bitmap->surface, &dst_rect);
+
+ if (dst_bitmap == window)
+ UpdateScreen(&dst_rect);
+}
+
+void SDLBlitTexture(Bitmap *bitmap,
+ int src_x, int src_y, int width, int height,
+ int dst_x, int dst_y, int mask_mode)
+{
+#if defined(TARGET_SDL2)
+ SDL_Texture *texture;
+ SDL_Rect src_rect;
+ SDL_Rect dst_rect;
+
+ texture =
+ (mask_mode == BLIT_MASKED ? bitmap->texture_masked : bitmap->texture);
+
+ if (texture == NULL)
+ return;
+
+ src_rect.x = src_x;
+ src_rect.y = src_y;
+ src_rect.w = width;
+ src_rect.h = height;
+
+ dst_rect.x = dst_x;
+ dst_rect.y = dst_y;
+ dst_rect.w = width;
+ dst_rect.h = height;
+
+ SDL_RenderCopy(sdl_renderer, texture, &src_rect, &dst_rect);
+#endif
+}
+
+void SDLFillRectangle(Bitmap *dst_bitmap, int x, int y, int width, int height,
+ Uint32 color)
+{
+ Bitmap *real_dst_bitmap = (dst_bitmap == window ? backbuffer : dst_bitmap);
+ SDL_Rect rect;
+
+ rect.x = x;
+ rect.y = y;
+ rect.w = width;
+ rect.h = height;
+
+ SDL_FillRect(real_dst_bitmap->surface, &rect, color);
+
+#if defined(TARGET_SDL2)
+ if (dst_bitmap == window)
+ {
+ // SDL_UpdateWindowSurface(sdl_window);
+ // SDL_UpdateWindowSurfaceRects(sdl_window, &rect, 1);
+ UpdateScreen(&rect);
+ }
+#else
+ if (dst_bitmap == window)
+ {
+ // SDL_UpdateRect(backbuffer->surface, x, y, width, height);
+ UpdateScreen(&rect);
+ }
+#endif
+}
+
+void SDLFadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
+ int fade_mode, int fade_delay, int post_delay,
+ void (*draw_border_function)(void))
+{
+ SDL_Surface *surface_source = gfx.fade_bitmap_source->surface;
+ SDL_Surface *surface_target = gfx.fade_bitmap_target->surface;
+ SDL_Surface *surface_black = gfx.fade_bitmap_black->surface;
+ SDL_Surface *surface_screen = backbuffer->surface;
+ SDL_Surface *surface_cross = (bitmap_cross ? bitmap_cross->surface : NULL);
+ SDL_Rect src_rect, dst_rect;
+ SDL_Rect dst_rect2;
+ int src_x = x, src_y = y;
+ int dst_x = x, dst_y = y;
+ unsigned int time_last, time_current;
+
+ // store function for drawing global masked border
+ void (*draw_global_border_function)(int) = gfx.draw_global_border_function;
+
+ // deactivate drawing of global border while fading, if needed
+ if (draw_border_function == NULL)
+ gfx.draw_global_border_function = NULL;
+
+ src_rect.x = src_x;
+ src_rect.y = src_y;
+ src_rect.w = width;
+ src_rect.h = height;
+
+ dst_rect.x = dst_x;
+ dst_rect.y = dst_y;
+ dst_rect.w = width; /* (ignored) */
+ dst_rect.h = height; /* (ignored) */