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))
{
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);
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;
}
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;
+ }
}
}
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);
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);
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;
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 */
Size& out_logical_size,
SDL_Rect& out_viewport);
+float calculate_pixel_aspect_ratio(const Size& source, const Size& target);
+
#endif
/* EOF */