fixed using unsupported rendering mode from config file with SDL1 target
[rocksndiamonds.git] / src / libgame / sdl.c
index 4d303910b57628c4ed7169b4839d0ffc341e30ba..d1406e8bcb5dff7fdfab840cd8d2346e7dd92949 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
 
@@ -64,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) */
@@ -93,10 +86,9 @@ 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)
 
@@ -110,17 +102,13 @@ static void UpdateScreen(SDL_Rect *rect)
     // 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)
+  if (video.screen_rendering_mode == SPECIAL_RENDERING_TARGET)
+    sdl_texture = sdl_texture_target;
+
   if (rect)
   {
     int bytes_x = screen->pitch / video.width;
@@ -138,29 +126,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
-  FinalizeScreen();
-#endif
+  if (video.screen_rendering_mode != SPECIAL_RENDERING_BITMAP)
+    FinalizeScreen();
 
-#if USE_TARGET_TEXTURE
-  SDL_SetRenderTarget(sdl_renderer, NULL);
-  SDL_RenderCopy(sdl_renderer, sdl_texture_target, NULL, NULL);
+  // 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
 
-  // 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);
@@ -169,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
@@ -383,6 +386,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;
@@ -454,7 +459,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);
@@ -466,13 +470,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))
   {
@@ -517,7 +514,6 @@ 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,
@@ -527,19 +523,9 @@ static boolean SDLCreateScreen(boolean fullscreen)
                                             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 USE_TARGET_TEXTURE
       if (sdl_texture_stream != NULL &&
          sdl_texture_target != NULL)
-#else
-      if (sdl_texture != NULL)
-#endif
       {
        // use SDL default values for RGB masks and no alpha channel
        new_surface = SDL_CreateRGBSurface(0, width, height, 32, 0,0,0, 0);
@@ -649,6 +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;
+
+      SDLSetScreenRenderingMode(setup.screen_rendering_mode);
     }
   }
 
@@ -722,7 +710,6 @@ 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 ||
@@ -757,27 +744,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;
 }
 
@@ -801,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)
@@ -867,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,
@@ -913,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,
@@ -1094,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);
       }
     }
   }
@@ -1153,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 */
@@ -1184,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);
     }
   }
 
@@ -1197,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();
     }