prepared global animation functions to update different screen buffers
[rocksndiamonds.git] / src / libgame / sdl.c
index d1bbe6efb28a1364ca4cde856b835c7553180f80..a88201fde27fb1ea3ea29dcbe34a8e633cd8cc42 100644 (file)
@@ -42,22 +42,22 @@ void SDLLimitScreenUpdates(boolean enable)
   limit_screen_updates = enable;
 }
 
-static void FinalizeScreen()
+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_GLOBAL_ANIM_STAGE_1);
+    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_BORDER_TO_SCREEN);
+    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_GLOBAL_ANIM_STAGE_2);
+    gfx.draw_global_anim_function(draw_target, 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) */
@@ -95,7 +95,7 @@ static void UpdateScreen(SDL_Rect *rect)
     BlitBitmap(backbuffer, gfx.final_screen_bitmap, 0, 0,
               gfx.win_xsize, gfx.win_ysize, 0, 0);
 
-    FinalizeScreen();
+    FinalizeScreen(DRAW_TO_SCREEN);
 
     screen = gfx.final_screen_bitmap->surface;
 
@@ -106,6 +106,12 @@ static void UpdateScreen(SDL_Rect *rect)
 #if defined(TARGET_SDL2)
   SDL_Texture *sdl_texture = sdl_texture_stream;
 
+  // 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;
 
@@ -136,7 +142,7 @@ static void UpdateScreen(SDL_Rect *rect)
     SDL_RenderCopy(sdl_renderer, sdl_texture_stream, NULL, NULL);
 
   if (video.screen_rendering_mode != SPECIAL_RENDERING_BITMAP)
-    FinalizeScreen();
+    FinalizeScreen(DRAW_TO_SCREEN);
 
   // when using target texture, copy it to screen buffer
   if (video.screen_rendering_mode == SPECIAL_RENDERING_TARGET ||
@@ -145,10 +151,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 +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
@@ -370,13 +391,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
@@ -430,6 +446,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 :
@@ -486,17 +514,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)
     {
@@ -509,13 +528,13 @@ static boolean SDLCreateScreen(boolean fullscreen)
                                             SDL_TEXTUREACCESS_STREAMING,
                                             width, height);
 
-      sdl_texture_target = SDL_CreateTexture(sdl_renderer,
-                                            SDL_PIXELFORMAT_ARGB8888,
-                                            SDL_TEXTUREACCESS_TARGET,
-                                            width, height);
+      if (SDL_RenderTargetSupported(sdl_renderer))
+       sdl_texture_target = SDL_CreateTexture(sdl_renderer,
+                                              SDL_PIXELFORMAT_ARGB8888,
+                                              SDL_TEXTUREACCESS_TARGET,
+                                              width, height);
 
-      if (sdl_texture_stream != NULL &&
-         sdl_texture_target != NULL)
+      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);
@@ -625,13 +644,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);
     }
   }
 
@@ -707,8 +721,7 @@ void SDLSetWindowScalingQuality(char *window_scaling_quality)
 {
   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);
@@ -725,10 +738,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,12 +778,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 +859,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 +905,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,
@@ -917,22 +948,22 @@ void SDLFadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
     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);
+    draw_global_border_function(DRAW_TO_FADE_SOURCE);
+    draw_global_border_function(DRAW_TO_FADE_TARGET);
   }
   else if (fade_mode & FADE_TYPE_FADE_IN)
   {
     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);
+    draw_global_border_function(DRAW_TO_FADE_TARGET);
   }
   else         /* FADE_TYPE_FADE_OUT */
   {
     SDL_BlitSurface(surface_screen, &dst_rect, surface_source, &src_rect);
     SDL_BlitSurface(surface_black,  &src_rect, surface_target, &src_rect);
 
-    draw_global_border_function(DRAW_BORDER_TO_FADE_SOURCE);
+    draw_global_border_function(DRAW_TO_FADE_SOURCE);
   }
 
   time_current = SDL_GetTicks();
@@ -1055,7 +1086,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 +1145,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 +1176,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 +1189,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();
     }