fixed using unsupported rendering mode from config file with SDL1 target
[rocksndiamonds.git] / src / libgame / sdl.c
index d1bbe6efb28a1364ca4cde856b835c7553180f80..d1406e8bcb5dff7fdfab840cd8d2346e7dd92949 100644 (file)
@@ -57,7 +57,7 @@ static void FinalizeScreen()
     gfx.draw_global_anim_function(DRAW_GLOBAL_ANIM_STAGE_2);
 }
 
-static void UpdateScreen(SDL_Rect *rect)
+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) */
@@ -145,10 +145,15 @@ static void UpdateScreen(SDL_Rect *rect)
     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);
+  // global synchronization point of the game to align video frame delay
+  if (with_frame_delay)
+    WaitUntilDelayReached(&video.frame_delay, video.frame_delay_value);
 
+#if defined(TARGET_SDL2)
+ // show render target buffer on screen
+  SDL_RenderPresent(sdl_renderer);
 #else  // TARGET_SDL
   if (rect)
     SDL_UpdateRects(screen, 1, rect);
@@ -157,6 +162,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
@@ -370,13 +385,8 @@ void SDLInitVideoBuffer(boolean fullscreen)
 {
   video.window_scaling_percent = setup.window_scaling_percent;
   video.window_scaling_quality = setup.window_scaling_quality;
-  video.screen_rendering_mode =
-    (strEqual(setup.screen_rendering_mode, STR_SPECIAL_RENDERING_BITMAP) ?
-     SPECIAL_RENDERING_BITMAP :
-     strEqual(setup.screen_rendering_mode, STR_SPECIAL_RENDERING_TARGET) ?
-     SPECIAL_RENDERING_TARGET:
-     strEqual(setup.screen_rendering_mode, STR_SPECIAL_RENDERING_DOUBLE) ?
-     SPECIAL_RENDERING_DOUBLE : SPECIAL_RENDERING_OFF);
+
+  SDLSetScreenRenderingMode(setup.screen_rendering_mode);
 
 #if defined(TARGET_SDL2)
   // SDL 2.0: support for (desktop) fullscreen mode available
@@ -625,13 +635,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;
-      video.screen_rendering_mode =
-       (strEqual(setup.screen_rendering_mode, STR_SPECIAL_RENDERING_BITMAP) ?
-        SPECIAL_RENDERING_BITMAP :
-        strEqual(setup.screen_rendering_mode, STR_SPECIAL_RENDERING_TARGET) ?
-        SPECIAL_RENDERING_TARGET:
-        strEqual(setup.screen_rendering_mode, STR_SPECIAL_RENDERING_DOUBLE) ?
-        SPECIAL_RENDERING_DOUBLE : SPECIAL_RENDERING_OFF);
+
+      SDLSetScreenRenderingMode(setup.screen_rendering_mode);
     }
   }
 
@@ -762,12 +767,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)
@@ -828,7 +848,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,
@@ -874,7 +894,7 @@ void SDLFillRectangle(Bitmap *dst_bitmap, int x, int y, int width, int height,
   SDL_FillRect(real_dst_bitmap->surface, &rect, color);
 
   if (dst_bitmap == window)
-    UpdateScreen(&rect);
+    UpdateScreen_WithFrameDelay(&rect);
 }
 
 void SDLFadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
@@ -1055,7 +1075,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);
       }
     }
   }
@@ -1114,7 +1134,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 */
@@ -1145,7 +1165,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);
     }
   }
 
@@ -1158,11 +1178,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();
     }