+ LastFrameCounter = FrameCounter;
+
+ /*
+ if (FrameCounter % 2)
+ return;
+ */
+ }
+#endif
+
+#if USE_FINAL_SCREEN_BITMAP
+ if (gfx.final_screen_bitmap != NULL) // may not be initialized yet
+ {
+ // !!! TEST !!!
+ // draw global animations using bitmaps instead of using textures
+ // to prevent texture scaling artefacts (this is potentially slower)
+
+ BlitBitmap(backbuffer, gfx.final_screen_bitmap, 0, 0,
+ gfx.win_xsize, gfx.win_ysize, 0, 0);
+
+ // copy global animations to render target buffer, if defined (below border)
+ if (gfx.draw_global_anim_function != NULL)
+ gfx.draw_global_anim_function(DRAW_GLOBAL_ANIM_STAGE_1);
+
+ // copy global masked border to render target buffer, if defined
+ if (gfx.draw_global_border_function != NULL)
+ gfx.draw_global_border_function(DRAW_BORDER_TO_SCREEN);
+
+ // copy global animations to render target buffer, if defined (above border)
+ if (gfx.draw_global_anim_function != NULL)
+ gfx.draw_global_anim_function(DRAW_GLOBAL_ANIM_STAGE_2);
+
+ screen = gfx.final_screen_bitmap->surface;
+
+ // force full window redraw
+ rect = NULL;
+ }
+#endif
+
+#if USE_TARGET_TEXTURE
+#if USE_TARGET_TEXTURE_ONLY
+ SDL_Texture *sdl_texture = sdl_texture_target;
+#else
+ SDL_Texture *sdl_texture = sdl_texture_stream;
+#endif
+#endif
+
+#if defined(TARGET_SDL2)
+ if (rect)
+ {
+ int bytes_x = screen->pitch / video.width;
+ int bytes_y = screen->pitch;
+
+ 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);
+ }
+
+ // clear render target buffer
+ SDL_RenderClear(sdl_renderer);
+
+#if USE_TARGET_TEXTURE
+ SDL_SetRenderTarget(sdl_renderer, sdl_texture_target);
+
+ // copy backbuffer to render target buffer
+ if (sdl_texture != sdl_texture_target)
+ SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, NULL);
+#else
+ // copy backbuffer to render target buffer
+ SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, NULL);
+#endif
+
+#if !USE_FINAL_SCREEN_BITMAP
+ // copy global animations to render target buffer, if defined (below border)
+ if (gfx.draw_global_anim_function != NULL)
+ gfx.draw_global_anim_function(DRAW_GLOBAL_ANIM_STAGE_1);
+
+ // copy global masked border to render target buffer, if defined
+ if (gfx.draw_global_border_function != NULL)
+ gfx.draw_global_border_function(DRAW_BORDER_TO_SCREEN);
+
+ // copy global animations to render target buffer, if defined (above border)
+ if (gfx.draw_global_anim_function != NULL)
+ gfx.draw_global_anim_function(DRAW_GLOBAL_ANIM_STAGE_2);
+#endif
+
+#if USE_TARGET_TEXTURE
+ SDL_SetRenderTarget(sdl_renderer, NULL);
+ SDL_RenderCopy(sdl_renderer, sdl_texture_target, NULL, NULL);
+#endif
+
+ // show render target buffer on screen
+ SDL_RenderPresent(sdl_renderer);
+
+#else // TARGET_SDL
+ if (rect)
+ SDL_UpdateRects(screen, 1, rect);
+ else
+ SDL_UpdateRect(screen, 0, 0, 0, 0);
+#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_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;