Skip to content

Commit

Permalink
Cleaned up various areas of code related to setting window resolution
Browse files Browse the repository at this point in the history
* Added a point ctor that takes an SDL_Point.
* Added a toggle_fullscreen function to CVideo.
* Change all management of resolution data to use point instead of std::pair<int, int>. This
  shortens the code considerably.
* Cleaned up preferences_dialog::set_resolution_list
* Made CVideo::set_resolution return a bool indicating whether resolution was changed.
* Swapped which of the two set_resolution overloads contains the main implementation.
* Moved the events::raise_resize_event() call when setting resolution to the CVideo function
  where it should have always been (instead of the prefs dialog callback). This is a pretty
  essential component and having it localized in one place meant any other area of the code
  that might have called set_resolution wouldn't have it.
  • Loading branch information
Vultraz authored and GregoryLundberg committed Nov 30, 2017
1 parent d006377 commit 41dccbf
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/game_launcher.cpp
Expand Up @@ -213,7 +213,7 @@ game_launcher::game_launcher(const commandline_options& cmdline_opts, const char
const int xres = std::get<0>(*cmdline_opts_.resolution);
const int yres = std::get<1>(*cmdline_opts_.resolution);
if(xres > 0 && yres > 0) {
preferences::_set_resolution(std::make_pair(xres, yres));
preferences::_set_resolution(point(xres, yres));
preferences::_set_maximized(false);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/dialogs/multiplayer/lobby.cpp
Expand Up @@ -179,7 +179,7 @@ void mp_lobby::post_build(window& win)
{
/** @todo Should become a global hotkey after 1.8, then remove it here. */
win.register_hotkey(hotkey::HOTKEY_FULLSCREEN,
std::bind(&CVideo::set_fullscreen, std::ref(win.video()), !preferences::fullscreen()));
std::bind(&CVideo::toggle_fullscreen, std::ref(win.video())));

/*** Local hotkeys. ***/
win.register_hotkey(hotkey::HOTKEY_PREFERENCES,
Expand Down
27 changes: 12 additions & 15 deletions src/gui/dialogs/preferences_dialog.cpp
Expand Up @@ -119,18 +119,20 @@ void preferences_dialog::set_resolution_list(menu_button& res_list, CVideo& vide
resolutions_ = video.get_available_resolutions(true);

std::vector<config> options;
for(const auto& res : resolutions_)
{
for(const point& res : resolutions_) {
config option;
option["label"] = formatter() << res.first << font::unicode_multiplication_sign << res.second;
option["label"] = formatter() << res.x << font::unicode_multiplication_sign << res.y;

const int div = boost::math::gcd(res.first, res.second);
const int ratio[2] {res.first/div, res.second/div};
if(ratio[0] <= 10 || ratio[1] <= 10) {
option["details"] = formatter() << "<span color='#777777'>(" << ratio[0] << ':' << ratio[1] << ")</span>";
const int div = boost::math::gcd(res.x, res.y);

const int x_ratio = res.x / div;
const int y_ratio = res.y / div;

if(x_ratio <= 10 || y_ratio <= 10) {
option["details"] = formatter() << "<span color='#777777'>(" << y_ratio << ':' << y_ratio << ")</span>";
}

options.push_back(option);
options.push_back(std::move(option));
}

const unsigned current_res = std::find(resolutions_.begin(), resolutions_.end(),
Expand Down Expand Up @@ -1037,15 +1039,10 @@ void preferences_dialog::fullscreen_toggle_callback(window& window)
void preferences_dialog::handle_res_select(window& window)
{
menu_button& res_list = find_widget<menu_button>(&window, "resolution_set", false);
const int choice = res_list.get_value();

if(resolutions_[static_cast<size_t>(choice)] == window.video().current_resolution()) {
return;
if(window.video().set_resolution(resolutions_[res_list.get_value()])) {
set_resolution_list(res_list, window.video());
}

window.video().set_resolution(resolutions_[static_cast<size_t>(choice)]);
events::raise_resize_event();
set_resolution_list(res_list, window.video());
}

void preferences_dialog::on_page_select(window& window)
Expand Down
4 changes: 3 additions & 1 deletion src/gui/dialogs/preferences_dialog.hpp
Expand Up @@ -30,6 +30,8 @@ namespace hotkey {
struct hotkey_command;
}

struct point;

namespace preferences {
enum PREFERENCE_VIEW {
VIEW_DEFAULT,
Expand Down Expand Up @@ -125,7 +127,7 @@ class preferences_dialog : public modal_dialog
(SPECIAL, "custom")
)

std::vector<std::pair<int,int> > resolutions_;
std::vector<point> resolutions_;
std::vector<config> adv_preferences_cfg_;

int last_selected_item_;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/dialogs/title_screen.cpp
Expand Up @@ -216,7 +216,7 @@ void title_screen::pre_show(window& win)
std::bind(&title_screen::hotkey_callback_select_tests, this, std::ref(win)));

win.register_hotkey(hotkey::HOTKEY_FULLSCREEN,
std::bind(&CVideo::set_fullscreen, std::ref(win.video()), !preferences::fullscreen()));
std::bind(&CVideo::toggle_fullscreen, std::ref(win.video())));

// A wrapper is needed here since the relevant display function is overloaded, and
// since the wrapper's signature doesn't exactly match what register_hotkey expects.
Expand Down
2 changes: 1 addition & 1 deletion src/hotkey/command_executor.cpp
Expand Up @@ -561,7 +561,7 @@ void execute_command(const hotkey_command& command, command_executor* executor,
executor->recalculate_minimap();
break;
case HOTKEY_FULLSCREEN:
executor->get_video().set_fullscreen(!preferences::fullscreen());
executor->get_video().toggle_fullscreen();
break;
case HOTKEY_SCREENSHOT:
make_screenshot(_("Screenshot"), executor->get_video(), &::screenshot);
Expand Down
24 changes: 13 additions & 11 deletions src/preferences/general.cpp
Expand Up @@ -19,18 +19,20 @@

#define GETTEXT_DOMAIN "wesnoth-lib"

#include "preferences/general.hpp"

#include "config.hpp"
#include "credentials.hpp"
#include "filesystem.hpp"
#include "game_config.hpp"
#include "hotkey/hotkey_item.hpp"
#include "lexical_cast.hpp"
#include "log.hpp"
#include "credentials.hpp"
#include "preferences/general.hpp"
#include "sound.hpp"
#include "video.hpp" // non_interactive()
#include "sdl/point.hpp"
#include "serialization/parser.hpp"
#include "sound.hpp"
#include "utils/general.hpp"
#include "video.hpp" // non_interactive()

#include <sys/stat.h> // for setting the permissions of the preferences file
#ifndef _WIN32
Expand Down Expand Up @@ -135,7 +137,7 @@ void prefs_event_handler::handle_window_event(const SDL_Event& event)

switch(event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
_set_resolution(std::make_pair(event.window.data1,event.window.data2));
_set_resolution(point(event.window.data1,event.window.data2));

break;

Expand Down Expand Up @@ -370,19 +372,19 @@ void set_scroll_to_action(bool ison)
prefs["scroll_to_action"] = ison;
}

std::pair<int,int> resolution()
point resolution()
{
const std::string& x = prefs["xresolution"], y = prefs["yresolution"];

if (!x.empty() && !y.empty()) {
try {
return std::make_pair(
return point(
std::max(std::stoi(x), min_window_width),
std::max(std::stoi(y), min_window_height));
} catch(std::invalid_argument) {}
}

return std::pair<int,int>(def_window_width, def_window_height);
return point(def_window_width, def_window_height);
}

bool maximized()
Expand All @@ -395,10 +397,10 @@ bool fullscreen()
return get("fullscreen", false);
}

void _set_resolution(const std::pair<int, int>& res)
void _set_resolution(const point& res)
{
preferences::set("xresolution", std::to_string(res.first));
preferences::set("yresolution", std::to_string(res.second));
preferences::set("xresolution", std::to_string(res.x));
preferences::set("yresolution", std::to_string(res.y));
}

void _set_maximized(bool ison)
Expand Down
6 changes: 4 additions & 2 deletions src/preferences/general.hpp
Expand Up @@ -24,6 +24,8 @@ class display;

#include <utility>

struct point;

namespace hotkey {
class hotkey_item;
}
Expand Down Expand Up @@ -81,8 +83,8 @@ namespace preferences {
bool scroll_to_action();
void set_scroll_to_action(bool ison);

std::pair<int,int> resolution();
void _set_resolution(const std::pair<int,int>& res);
point resolution();
void _set_resolution(const point& res);

bool maximized();
void _set_maximized(bool ison);
Expand Down
18 changes: 15 additions & 3 deletions src/sdl/point.hpp
Expand Up @@ -21,11 +21,21 @@
/** Holds a 2D point. */
struct point
{
point() : x(0), y(0)
point()
: x(0)
, y(0)
{
}

point(const int x_, const int y_) : x(x_), y(y_)
point(const int x_, const int y_)
: x(x_)
, y(y_)
{
}

point(const SDL_Point& p)
: x(p.x)
, y(p.y)
{
}

Expand All @@ -42,10 +52,12 @@ struct point
{
return x == point.x && y == point.y;
}

bool operator!=(const point& point) const
{
return x != point.x || y != point.y;
return !operator==(point);
}

bool operator<(const point& point) const
{
return x < point.x || (x == point.x && y < point.y);
Expand Down
65 changes: 37 additions & 28 deletions src/video.cpp
Expand Up @@ -19,6 +19,7 @@
#include "image.hpp"
#include "log.hpp"
#include "preferences/general.hpp"
#include "sdl/point.hpp"
#include "sdl/userevent.hpp"
#include "sdl/utils.hpp"
#include "sdl/window.hpp"
Expand Down Expand Up @@ -233,8 +234,8 @@ void CVideo::init_window()
const int y = preferences::fullscreen() ? SDL_WINDOWPOS_UNDEFINED : SDL_WINDOWPOS_CENTERED;

// Dimensions
const int w = preferences::resolution().first;
const int h = preferences::resolution().second;
const int w = preferences::resolution().x;
const int h = preferences::resolution().y;

// Video flags
int video_flags = 0;
Expand Down Expand Up @@ -267,7 +268,7 @@ void CVideo::init_window()
}
}

void CVideo::set_window_mode(int x, int y, const MODE_EVENT mode)
void CVideo::set_window_mode(const MODE_EVENT mode, const point& size)
{
assert(window);
if(fake_screen_) {
Expand All @@ -291,7 +292,7 @@ void CVideo::set_window_mode(int x, int y, const MODE_EVENT mode)

case TO_RES:
window->restore();
window->set_size(x, y);
window->set_size(size.x, size.y);
window->center();
break;
}
Expand Down Expand Up @@ -412,9 +413,9 @@ std::pair<float, float> CVideo::get_dpi_scale_factor() const
return result;
}

std::vector<std::pair<int, int>> CVideo::get_available_resolutions(const bool include_current)
std::vector<point> CVideo::get_available_resolutions(const bool include_current)
{
std::vector<std::pair<int, int>> result;
std::vector<point> result;

if(!window) {
return result;
Expand All @@ -428,8 +429,7 @@ std::vector<std::pair<int, int>> CVideo::get_available_resolutions(const bool in
return result;
}

const std::pair<int, int> min_res = std::make_pair(preferences::min_window_width, preferences::min_window_height);
const std::pair<int, int> current_res = current_resolution();
const point min_res(preferences::min_window_width, preferences::min_window_height);

#if 0
// DPI scale factor.
Expand All @@ -451,7 +451,7 @@ std::vector<std::pair<int, int>> CVideo::get_available_resolutions(const bool in
continue;
}

if(mode.w >= min_res.first && mode.h >= min_res.second) {
if(mode.w >= min_res.x && mode.h >= min_res.y) {
result.emplace_back(mode.w, mode.h);
}
}
Expand All @@ -462,7 +462,7 @@ std::vector<std::pair<int, int>> CVideo::get_available_resolutions(const bool in
}

if(include_current) {
result.push_back(current_res);
result.push_back(current_resolution());
}

std::sort(result.begin(), result.end());
Expand All @@ -476,10 +476,9 @@ surface& CVideo::getSurface()
return frameBuffer;
}

std::pair<int, int> CVideo::current_resolution()
point CVideo::current_resolution()
{
SDL_Point size = window->get_size();
return std::make_pair(size.x, size.y);
return point(window->get_size()); // Convert from plain SDL_Point
}

bool CVideo::is_fullscreen() const
Expand Down Expand Up @@ -535,7 +534,7 @@ void CVideo::clear_all_help_strings()
void CVideo::set_fullscreen(bool ison)
{
if(window && is_fullscreen() != ison) {
const std::pair<int, int>& res = preferences::resolution();
const point& res = preferences::resolution();

MODE_EVENT mode;

Expand All @@ -545,38 +544,48 @@ void CVideo::set_fullscreen(bool ison)
mode = preferences::maximized() ? TO_MAXIMIZED_WINDOW : TO_WINDOWED;
}

set_window_mode(res.first, res.second, mode);
set_window_mode(mode, res);

if(display::get_singleton()) {
display::get_singleton()->redraw_everything();
if(display* d = display::get_singleton()) {
d->redraw_everything();
}
}

// Change the config value.
preferences::_set_fullscreen(ison);
}

void CVideo::set_resolution(const std::pair<int, int>& resolution)
void CVideo::toggle_fullscreen()
{
set_resolution(resolution.first, resolution.second);
set_fullscreen(!preferences::fullscreen());
}

void CVideo::set_resolution(const unsigned width, const unsigned height)
bool CVideo::set_resolution(const unsigned width, const unsigned height)
{
if(static_cast<unsigned int>(current_resolution().first) == width
&& static_cast<unsigned int>(current_resolution().second) == height) {
return;
return set_resolution(point(width, height));
}

bool CVideo::set_resolution(const point& resolution)
{
if(resolution == current_resolution()) {
return false;
}

set_window_mode(width, height, TO_RES);
set_window_mode(TO_RES, resolution);

if(display::get_singleton()) {
display::get_singleton()->redraw_everything();
if(display* d = display::get_singleton()) {
d->redraw_everything();
}

// Change the config values.
preferences::_set_resolution(std::make_pair(width, height));
// Change the saved values in preferences.
preferences::_set_resolution(resolution);
preferences::_set_maximized(false);

// Push a window-resized event to the queue. This is necessary so various areas
// of the game (like GUI2) update properly with the new size.
events::raise_resize_event();

return true;
}

void CVideo::lock_flips(bool lock)
Expand Down

0 comments on commit 41dccbf

Please sign in to comment.