From a617ea960de73bb164191bbc3e4b06be9b90542e Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Sun, 3 Aug 2014 05:01:18 +0200 Subject: [PATCH] Moved some aspect ratio calculations into video/util.cpp --- src/video/gl/gl_renderer.cpp | 52 +++++++++++----------------------- src/video/sdl/sdl_renderer.cpp | 63 ++++++++++++++++++++++-------------------- src/video/util.cpp | 21 ++++++++++++-- src/video/util.hpp | 2 ++ 4 files changed, 70 insertions(+), 68 deletions(-) diff --git a/src/video/gl/gl_renderer.cpp b/src/video/gl/gl_renderer.cpp index a1b97908c..089c7f2dc 100644 --- a/src/video/gl/gl_renderer.cpp +++ b/src/video/gl/gl_renderer.cpp @@ -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 (-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(desktop_size.width) / static_cast(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); diff --git a/src/video/sdl/sdl_renderer.cpp b/src/video/sdl/sdl_renderer.cpp index 41019e9d1..3ba6f80a5 100644 --- a/src/video/sdl/sdl_renderer.cpp +++ b/src/video/sdl/sdl_renderer.cpp @@ -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(desktop_size.width) / static_cast(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); diff --git a/src/video/util.cpp b/src/video/util.cpp index 5b10009c5..294ed2d4a 100644 --- a/src/video/util.cpp +++ b/src/video/util.cpp @@ -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(real_window_size.width) / window_size.width, static_cast(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(source.width) / + static_cast(source.height); + } + + float target_aspect = + static_cast(target.width) / + static_cast(target.height); + + return target_aspect / source_aspect; +} + /* EOF */ diff --git a/src/video/util.hpp b/src/video/util.hpp index 8073d9bfd..87b6784c6 100644 --- a/src/video/util.hpp +++ b/src/video/util.hpp @@ -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 */ -- 2.11.0