changed setting native surface to use internal conversion function (SDL1)
[rocksndiamonds.git] / src / libgame / sdl.c
index 65930a7c7b9068769664b65763276c4130cd7d98..4b44daa1c53d6ce82c6c59f62cc134800b671d61 100644 (file)
 
 /* SDL internal variables */
 #if defined(TARGET_SDL2)
-#define USE_TARGET_TEXTURE             TRUE
-#define USE_TARGET_TEXTURE_ONLY                FALSE
-
 static SDL_Window *sdl_window = NULL;
 static SDL_Renderer *sdl_renderer = NULL;
-#if USE_TARGET_TEXTURE
 static SDL_Texture *sdl_texture_stream = NULL;
 static SDL_Texture *sdl_texture_target = NULL;
-#else
-static SDL_Texture *sdl_texture = NULL;
-#endif
 static boolean fullscreen_enabled = FALSE;
 #endif
 
@@ -49,7 +42,22 @@ void SDLLimitScreenUpdates(boolean enable)
   limit_screen_updates = enable;
 }
 
-static void UpdateScreen(SDL_Rect *rect)
+static void FinalizeScreen(int draw_target)
+{
+  // copy global animations to render target buffer, if defined (below border)
+  if (gfx.draw_global_anim_function != NULL)
+    gfx.draw_global_anim_function(draw_target, 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_target);
+
+  // copy global animations to render target buffer, if defined (above border)
+  if (gfx.draw_global_anim_function != NULL)
+    gfx.draw_global_anim_function(draw_target, DRAW_GLOBAL_ANIM_STAGE_2);
+}
+
+static void UpdateScreenExt(SDL_Rect *rect, boolean with_frame_delay)
 {
   static unsigned int update_screen_delay = 0;
   unsigned int update_screen_delay_value = 50;         /* (milliseconds) */
@@ -78,44 +86,35 @@ static void UpdateScreen(SDL_Rect *rect)
   }
 #endif
 
-#if USE_FINAL_SCREEN_BITMAP
-  if (gfx.final_screen_bitmap != NULL) // may not be initialized yet
+  if (video.screen_rendering_mode == SPECIAL_RENDERING_BITMAP &&
+      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);
+    FinalizeScreen(DRAW_TO_SCREEN);
 
     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
+#if defined(TARGET_SDL2)
   SDL_Texture *sdl_texture = sdl_texture_stream;
-#endif
-#endif
 
-#if defined(TARGET_SDL2)
+  // deactivate use of target texture if render targets are not supported
+  if ((video.screen_rendering_mode == SPECIAL_RENDERING_TARGET ||
+       video.screen_rendering_mode == SPECIAL_RENDERING_DOUBLE) &&
+      sdl_texture_target == NULL)
+    video.screen_rendering_mode = SPECIAL_RENDERING_OFF;
+
+  if (video.screen_rendering_mode == SPECIAL_RENDERING_TARGET)
+    sdl_texture = sdl_texture_target;
+
   if (rect)
   {
     int bytes_x = screen->pitch / video.width;
@@ -133,39 +132,34 @@ static void UpdateScreen(SDL_Rect *rect)
   // clear render target buffer
   SDL_RenderClear(sdl_renderer);
 
-#if USE_TARGET_TEXTURE
-  SDL_SetRenderTarget(sdl_renderer, sdl_texture_target);
+  // set renderer to use target texture for rendering
+  if (video.screen_rendering_mode == SPECIAL_RENDERING_TARGET ||
+      video.screen_rendering_mode == SPECIAL_RENDERING_DOUBLE)
+    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
+  // copy backbuffer texture to render target buffer
+  if (video.screen_rendering_mode != SPECIAL_RENDERING_TARGET)
+    SDL_RenderCopy(sdl_renderer, sdl_texture_stream, NULL, NULL);
 
-#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);
+  if (video.screen_rendering_mode != SPECIAL_RENDERING_BITMAP)
+    FinalizeScreen(DRAW_TO_SCREEN);
 
-  // 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);
+  // when using target texture, copy it to screen buffer
+  if (video.screen_rendering_mode == SPECIAL_RENDERING_TARGET ||
+      video.screen_rendering_mode == SPECIAL_RENDERING_DOUBLE)
+  {
+    SDL_SetRenderTarget(sdl_renderer, NULL);
+    SDL_RenderCopy(sdl_renderer, sdl_texture_target, NULL, NULL);
+  }
 #endif
 
-#if USE_TARGET_TEXTURE
-  SDL_SetRenderTarget(sdl_renderer, NULL);
-  SDL_RenderCopy(sdl_renderer, sdl_texture_target, NULL, NULL);
-#endif
+  // global synchronization point of the game to align video frame delay
+  if (with_frame_delay)
+    WaitUntilDelayReached(&video.frame_delay, video.frame_delay_value);
 
-  // show render target buffer on screen
+#if defined(TARGET_SDL2)
+ // show render target buffer on screen
   SDL_RenderPresent(sdl_renderer);
-
 #else  // TARGET_SDL
   if (rect)
     SDL_UpdateRects(screen, 1, rect);
@@ -174,6 +168,16 @@ static void UpdateScreen(SDL_Rect *rect)
 #endif
 }
 
+static void UpdateScreen_WithFrameDelay(SDL_Rect *rect)
+{
+  UpdateScreenExt(rect, TRUE);
+}
+
+static void UpdateScreen_WithoutFrameDelay(SDL_Rect *rect)
+{
+  UpdateScreenExt(rect, FALSE);
+}
+
 static void SDLSetWindowIcon(char *basename)
 {
   /* (setting the window icon on Mac OS X would replace the high-quality
@@ -219,34 +223,7 @@ static boolean equalSDLPixelFormat(SDL_PixelFormat *format1,
          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;
+         format1->Bmask         == format2->Bmask);
 }
 
 SDL_Surface *SDLGetNativeSurface(SDL_Surface *surface)
@@ -275,21 +252,21 @@ SDL_Surface *SDLGetNativeSurface(SDL_Surface *surface)
   return new_surface;
 }
 
-#else
-
 boolean SDLSetNativeSurface(SDL_Surface **surface)
 {
   SDL_Surface *new_surface;
 
   if (surface == NULL ||
       *surface == NULL ||
-      !video.initialized)
+      backbuffer == NULL ||
+      backbuffer->surface == NULL)
     return FALSE;
 
-  new_surface = SDL_DisplayFormat(*surface);
+  // if pixel format already optimized for destination surface, do nothing
+  if (equalSDLPixelFormat((*surface)->format, backbuffer->surface->format))
+    return FALSE;
 
-  if (new_surface == NULL)
-    Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
+  new_surface = SDLGetNativeSurface(*surface);
 
   SDL_FreeSurface(*surface);
 
@@ -298,10 +275,15 @@ boolean SDLSetNativeSurface(SDL_Surface **surface)
   return TRUE;
 }
 
+#else
+
 SDL_Surface *SDLGetNativeSurface(SDL_Surface *surface)
 {
   SDL_Surface *new_surface;
 
+  if (surface == NULL)
+    return NULL;
+
   if (video.initialized)
     new_surface = SDL_DisplayFormat(surface);
   else
@@ -315,6 +297,24 @@ SDL_Surface *SDLGetNativeSurface(SDL_Surface *surface)
   return new_surface;
 }
 
+boolean SDLSetNativeSurface(SDL_Surface **surface)
+{
+  SDL_Surface *new_surface;
+
+  if (surface == NULL ||
+      *surface == NULL ||
+      !video.initialized)
+    return FALSE;
+
+  new_surface = SDLGetNativeSurface(*surface);
+
+  SDL_FreeSurface(*surface);
+
+  *surface = new_surface;
+
+  return TRUE;
+}
+
 #endif
 
 #if defined(TARGET_SDL2)
@@ -388,6 +388,8 @@ void SDLInitVideoBuffer(boolean fullscreen)
   video.window_scaling_percent = setup.window_scaling_percent;
   video.window_scaling_quality = setup.window_scaling_quality;
 
+  SDLSetScreenRenderingMode(setup.screen_rendering_mode);
+
 #if defined(TARGET_SDL2)
   // SDL 2.0: support for (desktop) fullscreen mode available
   video.fullscreen_available = TRUE;
@@ -425,7 +427,7 @@ void SDLInitVideoBuffer(boolean fullscreen)
      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);
+  ReCreateBitmap(&window, video.width, video.height);
 }
 
 static boolean SDLCreateScreen(boolean fullscreen)
@@ -440,6 +442,18 @@ static boolean SDLCreateScreen(boolean fullscreen)
   int surface_flags_fullscreen = SURFACE_FLAGS;        // (no fullscreen in SDL 1.2)
 #endif
 
+#if defined(TARGET_SDL2)
+#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
+#endif
+
   int width  = video.width;
   int height = video.height;
   int surface_flags = (fullscreen ? surface_flags_fullscreen :
@@ -459,7 +473,6 @@ static boolean SDLCreateScreen(boolean fullscreen)
   video.window_width  = window_scaling_factor * width;
   video.window_height = window_scaling_factor * height;
 
-#if USE_TARGET_TEXTURE
   if (sdl_texture_stream)
   {
     SDL_DestroyTexture(sdl_texture_stream);
@@ -471,13 +484,6 @@ static boolean SDLCreateScreen(boolean fullscreen)
     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))
   {
@@ -504,17 +510,8 @@ static boolean SDLCreateScreen(boolean fullscreen)
 
   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
+      sdl_renderer = SDL_CreateRenderer(sdl_window, -1, renderer_flags);
 
     if (sdl_renderer != NULL)
     {
@@ -522,29 +519,18 @@ static boolean SDLCreateScreen(boolean fullscreen)
       // 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 (SDL_RenderTargetSupported(sdl_renderer))
+       sdl_texture_target = SDL_CreateTexture(sdl_renderer,
+                                              SDL_PIXELFORMAT_ARGB8888,
+                                              SDL_TEXTUREACCESS_TARGET,
+                                              width, height);
 
-#if USE_TARGET_TEXTURE
-      if (sdl_texture_stream != NULL &&
-         sdl_texture_target != NULL)
-#else
-      if (sdl_texture != NULL)
-#endif
+      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);
@@ -654,6 +640,8 @@ boolean SDLSetVideoMode(boolean fullscreen)
       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);
     }
   }
 
@@ -727,11 +715,9 @@ void SDLSetWindowScaling(int window_scaling_percent)
 
 void SDLSetWindowScalingQuality(char *window_scaling_quality)
 {
-#if USE_TARGET_TEXTURE
   SDL_Texture *new_texture;
 
-  if (sdl_texture_stream == NULL ||
-      sdl_texture_target == NULL)
+  if (sdl_texture_stream == NULL)
     return;
 
   SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, window_scaling_quality);
@@ -748,10 +734,13 @@ void SDLSetWindowScalingQuality(char *window_scaling_quality)
     sdl_texture_stream = new_texture;
   }
 
-  new_texture = SDL_CreateTexture(sdl_renderer,
-                                 SDL_PIXELFORMAT_ARGB8888,
-                                 SDL_TEXTUREACCESS_TARGET,
-                                 video.width, video.height);
+  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;
 
   if (new_texture != NULL)
   {
@@ -762,27 +751,6 @@ void SDLSetWindowScalingQuality(char *window_scaling_quality)
 
   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;
 }
 
@@ -806,12 +774,27 @@ void SDLSetWindowFullscreen(boolean fullscreen)
     video.fullscreen_initial = FALSE;
   }
 }
+#endif
+
+void SDLSetScreenRenderingMode(char *screen_rendering_mode)
+{
+#if defined(TARGET_SDL2)
+  video.screen_rendering_mode =
+    (strEqual(screen_rendering_mode, STR_SPECIAL_RENDERING_BITMAP) ?
+     SPECIAL_RENDERING_BITMAP :
+     strEqual(screen_rendering_mode, STR_SPECIAL_RENDERING_TARGET) ?
+     SPECIAL_RENDERING_TARGET:
+     strEqual(screen_rendering_mode, STR_SPECIAL_RENDERING_DOUBLE) ?
+     SPECIAL_RENDERING_DOUBLE : SPECIAL_RENDERING_OFF);
+#else
+  video.screen_rendering_mode = SPECIAL_RENDERING_BITMAP;
+#endif
+}
 
 void SDLRedrawWindow()
 {
-  UpdateScreen(NULL);
+  UpdateScreen_WithoutFrameDelay(NULL);
 }
-#endif
 
 void SDLCreateBitmapContent(Bitmap *bitmap, int width, int height,
                            int depth)
@@ -872,7 +855,7 @@ void SDLCopyArea(Bitmap *src_bitmap, Bitmap *dst_bitmap,
                    &src_rect, real_dst_bitmap->surface, &dst_rect);
 
   if (dst_bitmap == window)
-    UpdateScreen(&dst_rect);
+    UpdateScreen_WithFrameDelay(&dst_rect);
 }
 
 void SDLBlitTexture(Bitmap *bitmap,
@@ -917,31 +900,35 @@ void SDLFillRectangle(Bitmap *dst_bitmap, int x, int y, int width, int 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
+    UpdateScreen_WithFrameDelay(&rect);
 }
 
-void SDLFadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
+void PrepareFadeBitmap(int draw_target)
+{
+  Bitmap *fade_bitmap =
+    (draw_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
+     draw_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
+
+  if (fade_bitmap == NULL)
+    return;
+
+  // copy backbuffer to fading buffer
+  BlitBitmap(backbuffer, fade_bitmap, 0, 0, gfx.win_xsize, gfx.win_ysize, 0, 0);
+
+  // add border and animations to fading buffer
+  FinalizeScreen(draw_target);
+}
+
+void SDLFadeRectangle(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_backup = gfx.fade_bitmap_backup->surface;
   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;
@@ -967,28 +954,24 @@ void SDLFadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
 
   dst_rect2 = dst_rect;
 
+  // before fading in, store backbuffer (without animation graphics)
+  if (fade_mode & (FADE_TYPE_FADE_IN | FADE_TYPE_TRANSFORM))
+    SDL_BlitSurface(surface_screen, &dst_rect, surface_backup, &src_rect);
+
   /* copy source and target surfaces to temporary surfaces for fading */
   if (fade_mode & FADE_TYPE_TRANSFORM)
   {
-    SDL_BlitSurface(surface_cross,  &src_rect, surface_source, &src_rect);
-    SDL_BlitSurface(surface_screen, &dst_rect, surface_target, &src_rect);
-
-    draw_global_border_function(DRAW_BORDER_TO_FADE_SOURCE);
-    draw_global_border_function(DRAW_BORDER_TO_FADE_TARGET);
+    // (source and target fading buffer already prepared)
   }
   else if (fade_mode & FADE_TYPE_FADE_IN)
   {
+    // (target fading buffer already prepared)
     SDL_BlitSurface(surface_black,  &src_rect, surface_source, &src_rect);
-    SDL_BlitSurface(surface_screen, &dst_rect, surface_target, &src_rect);
-
-    draw_global_border_function(DRAW_BORDER_TO_FADE_TARGET);
   }
   else         /* FADE_TYPE_FADE_OUT */
   {
-    SDL_BlitSurface(surface_screen, &dst_rect, surface_source, &src_rect);
+    // (source fading buffer already prepared)
     SDL_BlitSurface(surface_black,  &src_rect, surface_target, &src_rect);
-
-    draw_global_border_function(DRAW_BORDER_TO_FADE_SOURCE);
   }
 
   time_current = SDL_GetTicks();
@@ -1111,7 +1094,7 @@ void SDLFadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
        if (draw_border_function != NULL)
          draw_border_function();
 
-       UpdateScreen(&dst_rect2);
+       UpdateScreen_WithFrameDelay(&dst_rect2);
       }
     }
   }
@@ -1170,7 +1153,7 @@ void SDLFadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
        draw_border_function();
 
       /* only update the region of the screen that is affected from fading */
-      UpdateScreen(&dst_rect2);
+      UpdateScreen_WithFrameDelay(&dst_rect2);
     }
   }
   else         /* fading in, fading out or cross-fading */
@@ -1201,7 +1184,7 @@ void SDLFadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
        draw_border_function();
 
       /* only update the region of the screen that is affected from fading */
-      UpdateScreen(&dst_rect);
+      UpdateScreen_WithFrameDelay(&dst_rect);
     }
   }
 
@@ -1214,11 +1197,8 @@ void SDLFadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
 
     while (time_current < time_post_delay)
     {
-      // do not wait longer than 10 ms at a time to be able to ...
-      Delay(MIN(10, time_post_delay - time_current));
-
-      // ... continue drawing global animations during post delay
-      UpdateScreen(NULL);
+      // updating the screen contains waiting for frame delay (non-busy)
+      UpdateScreen_WithFrameDelay(NULL);
 
       time_current = SDL_GetTicks();
     }
@@ -1226,6 +1206,10 @@ void SDLFadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
 
   // restore function for drawing global masked border
   gfx.draw_global_border_function = draw_global_border_function;
+
+  // after fading in, restore backbuffer (without animation graphics)
+  if (fade_mode & (FADE_TYPE_FADE_IN | FADE_TYPE_TRANSFORM))
+    SDL_BlitSurface(surface_backup, &dst_rect, surface_screen, &src_rect);
 }
 
 void SDLDrawSimpleLine(Bitmap *dst_bitmap, int from_x, int from_y,