Moved some aspect ratio calculations into video/util.cpp
authorIngo Ruhnke <grumbel@gmail.com>
Sun, 3 Aug 2014 03:01:18 +0000 (05:01 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Sun, 3 Aug 2014 03:01:36 +0000 (05:01 +0200)
src/video/gl/gl_renderer.cpp
src/video/sdl/sdl_renderer.cpp
src/video/util.cpp
src/video/util.hpp

index a1b9790..089c7f2 100644 (file)
@@ -37,8 +37,8 @@
 
 GLRenderer::GLRenderer() :
   window(),
-  desktop_size(-1, -1),
-  screen_size(-1, -1),
+  desktop_size(0, 0),
+  screen_size(0, 0),
   fullscreen_active(false),
   last_texture(static_cast<GLuint> (-1))
 {
@@ -472,52 +472,32 @@ GLRenderer::resize(int w, int h)
 void
 GLRenderer::apply_config()
 {
-  if (false)
-  {
-    log_info << "Applying Config:"
-             << "\n  Desktop: " << desktop_size.width << "x" << desktop_size.height
-             << "\n  Window:  " << g_config->window_size
-             << "\n  FullRes: " << g_config->fullscreen_size
-             << "\n  Aspect:  " << g_config->aspect_size
-             << "\n  Magnif:  " << g_config->magnification
-             << std::endl;
-  }
-
-  float target_aspect = static_cast<float>(desktop_size.width) / static_cast<float>(desktop_size.height);
-  if (g_config->aspect_size != Size(0, 0))
-  {
-    target_aspect = float(g_config->aspect_size.width) / float(g_config->aspect_size.height);
-  }
-
-  float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
-  if (desktop_size.width != -1 && desktop_size.height != -1)
-  {
-    desktop_aspect = float(desktop_size.width) / float(desktop_size.height);
-  }
+  apply_video_mode(screen_size, g_config->use_fullscreen);
 
-  Size screen_size;
+  Size target_size = g_config->use_fullscreen ?
+    g_config->fullscreen_size :
+    g_config->window_size;
 
-  // Get the screen width
-  if (g_config->use_fullscreen)
+  float pixel_aspect_ratio = 1.0f;
+  if (g_config->aspect_size != Size(0, 0))
   {
-    screen_size = g_config->fullscreen_size;
-    desktop_aspect = float(screen_size.width) / float(screen_size.height);
+    pixel_aspect_ratio = calculate_pixel_aspect_ratio(desktop_size,
+                                                      g_config->aspect_size);
   }
-  else
+  else if (g_config->use_fullscreen)
   {
-    screen_size = g_config->window_size;
+    pixel_aspect_ratio = calculate_pixel_aspect_ratio(desktop_size,
+                                                      target_size);
   }
 
-  apply_video_mode(screen_size, g_config->use_fullscreen);
-
   Size max_size(1280, 800);
   Size min_size(640, 480);
 
   Vector scale;
   Size logical_size;
-  calculate_viewport(min_size, max_size,
-                     screen_size,
-                     target_aspect / desktop_aspect, g_config->magnification,
+  calculate_viewport(min_size, max_size, screen_size,
+                     pixel_aspect_ratio,
+                     g_config->magnification,
                      scale,
                      logical_size,
                      viewport);
index 41019e9..3ba6f80 100644 (file)
@@ -36,13 +36,19 @@ SDLRenderer::SDLRenderer() :
   window(),
   renderer(),
   viewport(),
-  desktop_size()
+  desktop_size(0, 0)
 {
   Renderer::instance_ = this;
 
   SDL_DisplayMode mode;
-  SDL_GetDesktopDisplayMode(0, &mode);
-  desktop_size = Size(mode.w, mode.h);
+  if (SDL_GetDesktopDisplayMode(0, &mode) != 0)
+  {
+    log_warning << "Couldn't get desktop display mode: " << SDL_GetError() << std::endl;
+  }
+  else
+  {
+    desktop_size = Size(mode.w, mode.h);
+  }
 
   log_info << "creating SDLRenderer" << std::endl;
   int width  = g_config->window_size.width;
@@ -245,7 +251,17 @@ SDLRenderer::apply_video_mode()
     }
     else
     {
-      SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
+      if (SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN) != 0)
+      {
+        log_warning << "failed to switch to fullscreen mode: "
+                    << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
+                    << SDL_GetError() << std::endl;
+      }
+      else
+      {
+        log_info << "switched to fullscreen mode: "
+                 << mode.w << "x" << mode.h << "@" << mode.refresh_rate << std::endl;
+      }
     }
   }
 
@@ -254,44 +270,31 @@ SDLRenderer::apply_video_mode()
 void
 SDLRenderer::apply_viewport()
 {
-  // calculate the aspect ratio
-  float target_aspect = static_cast<float>(desktop_size.width) / static_cast<float>(desktop_size.height);
-  if (g_config->aspect_size != Size(0, 0))
-  {
-    target_aspect = float(g_config->aspect_size.width) / float(g_config->aspect_size.height);
-  }
-
-  float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
-  if (desktop_size.width != -1 && desktop_size.height != -1)
-  {
-    desktop_aspect = float(desktop_size.width) / float(desktop_size.height);
-  }
-
-  Size screen_size;
+  Size target_size = g_config->use_fullscreen ?
+    g_config->fullscreen_size :
+    g_config->window_size;
 
-  // Get the screen width
-  if (g_config->use_fullscreen)
+  float pixel_aspect_ratio = 1.0f;
+  if (g_config->aspect_size != Size(0, 0))
   {
-    screen_size = g_config->fullscreen_size;
-    desktop_aspect = float(screen_size.width) / float(screen_size.height);
+    pixel_aspect_ratio = calculate_pixel_aspect_ratio(desktop_size,
+                                                      g_config->aspect_size);
   }
-  else
+  else if (g_config->use_fullscreen)
   {
-    screen_size = g_config->window_size;
+    pixel_aspect_ratio = calculate_pixel_aspect_ratio(desktop_size,
+                                                      target_size);
   }
 
   // calculate the viewport
   Size max_size(1280, 800);
   Size min_size(640, 480);
 
-  // FIXME: don't do this, save window size
-  Size window_size;
-  SDL_GetWindowSize(window, &window_size.width, &window_size.height);
-
   Vector scale;
   Size logical_size;
-  calculate_viewport(min_size, max_size, window_size,
-                     target_aspect / desktop_aspect,
+  calculate_viewport(min_size, max_size,
+                     target_size,
+                     pixel_aspect_ratio,
                      g_config->magnification,
                      scale, logical_size, viewport);
 
index 5b10009..294ed2d 100644 (file)
@@ -26,7 +26,7 @@ namespace {
 inline Size
 apply_pixel_aspect_ratio_pre(const Size& window_size, float pixel_aspect_ratio)
 {
-  if (pixel_aspect_ratio < 1.0f)
+  if (true)
   {
     return Size(window_size.width * pixel_aspect_ratio,
                 window_size.height);
@@ -41,7 +41,7 @@ apply_pixel_aspect_ratio_pre(const Size& window_size, float pixel_aspect_ratio)
 inline void
 apply_pixel_aspect_ratio_post(const Size& real_window_size, const Size& window_size, float scale,
                                    SDL_Rect& out_viewport, Vector& out_scale)
-{  
+{
   Vector transform(static_cast<float>(real_window_size.width) / window_size.width,
                    static_cast<float>(real_window_size.height) / window_size.height);
   out_viewport.x *= transform.x;
@@ -128,4 +128,21 @@ void calculate_viewport(const Size& min_size, const Size& max_size,
                                 out_viewport, out_scale);
 }
 
+float calculate_pixel_aspect_ratio(const Size& source, const Size& target)
+{
+  float source_aspect = 16.0f / 9.0f; // random guess
+  if (source != Size(0, 0))
+  {
+    source_aspect =
+      static_cast<float>(source.width) /
+      static_cast<float>(source.height);
+  }
+
+  float target_aspect =
+    static_cast<float>(target.width) /
+    static_cast<float>(target.height);
+
+  return target_aspect / source_aspect;
+}
+
 /* EOF */
index 8073d9b..87b6784 100644 (file)
@@ -29,6 +29,8 @@ void calculate_viewport(const Size& min_size, const Size& max_size,
                         Size& out_logical_size,
                         SDL_Rect& out_viewport);
 
+float calculate_pixel_aspect_ratio(const Size& source, const Size& target);
+
 #endif
 
 /* EOF */