+static boolean limit_screen_updates = FALSE;
+
+
+/* functions from SGE library */
+void sge_Line(SDL_Surface *, Sint16, Sint16, Sint16, Sint16, Uint32);
+
+void SDLLimitScreenUpdates(boolean enable)
+{
+ limit_screen_updates = enable;
+}
+
+static void UpdateScreen(SDL_Rect *rect)
+{
+ static unsigned int update_screen_delay = 0;
+ unsigned int update_screen_delay_value = 20; /* (milliseconds) */
+
+ if (limit_screen_updates &&
+ !DelayReached(&update_screen_delay, update_screen_delay_value))
+ return;
+
+ LimitScreenUpdates(FALSE);
+
+#if defined(TARGET_SDL2)
+#if USE_RENDERER
+ SDL_Surface *screen = backbuffer->surface;
+
+ if (rect)
+ {
+ int bytes_x = screen->pitch / video.width;
+ int bytes_y = screen->pitch;
+
+ if (video.fullscreen_enabled)
+ bytes_x = screen->pitch / fullscreen_width;
+
+ SDL_UpdateTexture(sdl_texture, rect,
+ screen->pixels + rect->x * bytes_x + rect->y * bytes_y,
+ screen->pitch);
+ }
+ else
+ {
+ SDL_UpdateTexture(sdl_texture, NULL, screen->pixels, screen->pitch);
+ }
+ SDL_RenderClear(sdl_renderer);
+ SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, NULL);
+ SDL_RenderPresent(sdl_renderer);
+#else
+ if (rect)
+ SDL_UpdateWindowSurfaceRects(sdl_window, rect, 1);
+ else
+ SDL_UpdateWindowSurface(sdl_window);
+#endif
+
+#else // TARGET_SDL
+ if (rect)
+ SDL_UpdateRects(backbuffer->surface, 1, rect);
+ else
+ SDL_UpdateRect(backbuffer->surface, 0, 0, 0, 0);
+#endif
+}
+
+static void setFullscreenParameters(char *fullscreen_mode_string)
+{
+#if defined(TARGET_SDL2)
+ fullscreen_width = video.width;
+ fullscreen_height = video.height;
+ fullscreen_xoffset = 0;
+ fullscreen_yoffset = 0;
+
+#else
+
+ struct ScreenModeInfo *fullscreen_mode;
+ int i;
+
+ fullscreen_mode = get_screen_mode_from_string(fullscreen_mode_string);
+
+ if (fullscreen_mode == NULL)
+ return;
+
+ for (i = 0; video.fullscreen_modes[i].width != -1; i++)
+ {
+ if (fullscreen_mode->width == video.fullscreen_modes[i].width &&
+ fullscreen_mode->height == video.fullscreen_modes[i].height)
+ {
+ fullscreen_width = fullscreen_mode->width;
+ fullscreen_height = fullscreen_mode->height;
+
+ fullscreen_xoffset = (fullscreen_width - video.width) / 2;
+ fullscreen_yoffset = (fullscreen_height - video.height) / 2;
+
+ break;
+ }
+ }
+#endif
+}
+
+static void SDLSetWindowIcon(char *basename)
+{
+ /* (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() failed: %s", SDL_GetError());
+
+ return;
+ }
+
+ /* set transparent color */
+ SDL_SetColorKey(surface, SET_TRANSPARENT_PIXEL,
+ SDL_MapRGB(surface->format, 0x00, 0x00, 0x00));
+
+#if defined(TARGET_SDL2)
+ SDL_SetWindowIcon(sdl_window, surface);
+#else
+ SDL_WM_SetIcon(surface, NULL);
+#endif
+#endif
+}
+
+#if defined(TARGET_SDL2)
+
+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 &&
+ format1->Amask == format2->Amask);
+}
+
+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 = SDL_ConvertSurface(*surface, backbuffer->surface->format, 0);
+
+ if (new_surface == NULL)
+ Error(ERR_EXIT, "SDL_ConvertSurface() failed: %s", SDL_GetError());
+
+ SDL_FreeSurface(*surface);
+
+ *surface = new_surface;
+
+ return TRUE;
+}
+
+SDL_Surface *SDLGetNativeSurface(SDL_Surface *surface)
+{
+ SDL_Surface *new_surface;
+
+ if (surface == NULL)
+ return NULL;
+
+ if (backbuffer && backbuffer->surface)
+ new_surface = SDL_ConvertSurface(surface, backbuffer->surface->format, 0);
+ else
+ new_surface = SDL_ConvertSurface(surface, surface->format, 0);
+
+ if (new_surface == NULL)
+ Error(ERR_EXIT, "SDL_ConvertSurface() failed: %s", SDL_GetError());
+
+ return new_surface;
+}
+
+#else
+
+boolean SDLSetNativeSurface(SDL_Surface **surface)
+{
+ SDL_Surface *new_surface;
+
+ if (surface == NULL ||
+ *surface == NULL ||
+ !video.initialized)
+ return FALSE;
+
+ new_surface = SDL_DisplayFormat(*surface);
+
+ if (new_surface == NULL)
+ Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
+
+ SDL_FreeSurface(*surface);
+
+ *surface = new_surface;
+
+ return TRUE;
+}
+
+SDL_Surface *SDLGetNativeSurface(SDL_Surface *surface)
+{
+ SDL_Surface *new_surface;
+
+ if (video.initialized)
+ new_surface = SDL_DisplayFormat(surface);
+ else
+ new_surface = SDL_ConvertSurface(surface, surface->format, SURFACE_FLAGS);
+
+ if (new_surface == NULL)
+ Error(ERR_EXIT, "%s() failed: %s",
+ (video.initialized ? "SDL_DisplayFormat" : "SDL_ConvertSurface"),
+ SDL_GetError());
+
+ return new_surface;
+}
+
+#endif