Move relevant functions from preferences to display class

This affects window setter functions such as set_fullscreen() and set_resolution()
This commit is contained in:
Charles Dang 2015-12-24 01:30:59 +11:00
parent 136ca10a9a
commit ccc2433979
11 changed files with 161 additions and 176 deletions

View file

@ -649,7 +649,7 @@ static void do_preferences_dialog(game_display& disp, const config& game_config)
* @todo This might no longer be needed when gui2 is done.
*/
const SDL_Rect rect = screen_area();
preferences::set_resolution(disp.video(), rect.w, rect.h);
disp.video().set_resolution(rect.w, rect.h);
gui2::settings::gamemap_width += rect.w - gui2::settings::screen_width ;
gui2::settings::gamemap_height += rect.h - gui2::settings::screen_height ;

View file

@ -47,7 +47,6 @@
#include "network.hpp"
#include "game_initialization/playcampaign.hpp" // for play_game, etc
#include "preferences.hpp" // for disable_preferences_save, etc
#include "preferences_display.hpp" // for detect_video_settings, etc
#include "savegame.hpp" // for clean_saves, etc
#include "scripting/application_lua_kernel.hpp"
#include "sdl/utils.hpp" // for surface
@ -188,7 +187,7 @@ game_launcher::game_launcher(const commandline_options& cmdline_opts, const char
if (cmdline_opts_.fps)
preferences::set_show_fps(true);
if (cmdline_opts_.fullscreen)
preferences::set_fullscreen(true);
video_.set_fullscreen(true);
if (cmdline_opts_.load)
game::load_game_exception::game = *cmdline_opts_.load;
if (cmdline_opts_.max_fps) {
@ -247,7 +246,7 @@ game_launcher::game_launcher(const commandline_options& cmdline_opts, const char
const int yres = cmdline_opts_.resolution->get<1>();
if(xres > 0 && yres > 0) {
const std::pair<int,int> resolution(xres,yres);
preferences::set_resolution(resolution);
video_.set_resolution(resolution);
}
}
if (cmdline_opts_.screenshot) {
@ -291,7 +290,7 @@ game_launcher::game_launcher(const commandline_options& cmdline_opts, const char
}
if (cmdline_opts_.windowed)
preferences::set_fullscreen(false);
video_.set_fullscreen(false);
if (cmdline_opts_.with_replay)
game::load_game_exception::show_replay = true;
@ -409,7 +408,7 @@ bool game_launcher::init_video()
int bpp = 0;
int video_flags = 0;
bool found_matching = preferences::detect_video_settings(video_, resolution, bpp, video_flags);
bool found_matching = video_.detect_video_settings(resolution, bpp, video_flags);
if (cmdline_opts_.screenshot) {
bpp = CVideo::DefaultBpp;
@ -417,7 +416,7 @@ bool game_launcher::init_video()
if(!found_matching && (video_flags & SDL_FULLSCREEN)) {
video_flags ^= SDL_FULLSCREEN;
found_matching = preferences::detect_video_settings(video_, resolution, bpp, video_flags);
found_matching = video_.detect_video_settings(resolution, bpp, video_flags);
if (found_matching) {
std::cerr << "Failed to set " << resolution.first << 'x' << resolution.second << 'x' << bpp << " in fullscreen mode. Using windowed instead.\n";
}

View file

@ -1720,10 +1720,10 @@ void show_preferences_dialog(display& disp, const config& game_cfg)
show_video_mode_dialog(disp);
break;
case preferences_dialog::video_mode_change_exception::MAKE_FULLSCREEN:
set_fullscreen(true);
disp.video().set_fullscreen(true);
break;
case preferences_dialog::video_mode_change_exception::MAKE_WINDOWED:
set_fullscreen(false);
disp.video().set_fullscreen(false);
break;
}

View file

@ -46,7 +46,6 @@
#include "log.hpp"
#include "network.hpp"
#include "playmp_controller.hpp"
#include "preferences_display.hpp"
#include "mp_ui_alerts.hpp"
#include <boost/bind.hpp>
@ -431,7 +430,7 @@ static void signal_handler_sdl_video_resize(const event::tevent event,
return;
}
if(!preferences::set_resolution(video, new_size.x, new_size.y)) {
if(!video.set_resolution(new_size.x, new_size.y)) {
LOG_GUI_E << __func__ << ": resize aborted, resize failed.\n";
}
@ -439,7 +438,7 @@ static void signal_handler_sdl_video_resize(const event::tevent event,
static bool fullscreen(CVideo& video)
{
preferences::set_fullscreen(video, !preferences::fullscreen());
video.set_fullscreen(!preferences::fullscreen());
// Setting to fullscreen doesn't seem to generate a resize event.
const SDL_Rect& rect = screen_area();

View file

@ -38,7 +38,6 @@
#include "gui/widgets/progress_bar.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "preferences_display.hpp"
#include "utils/foreach.tpp"
#include <boost/bind.hpp>
@ -170,7 +169,7 @@ static void animate_logo(size_t& timer_id,
static bool fullscreen(CVideo& video)
{
preferences::set_fullscreen(video, !preferences::fullscreen());
video.set_fullscreen(!preferences::fullscreen());
// Setting to fullscreen doesn't seem to generate a resize event.
const SDL_Rect& rect = screen_area();

View file

@ -1463,7 +1463,7 @@ void twindow::signal_handler_sdl_video_resize(const event::tevent event,
}
#endif
if(!preferences::set_resolution(video_, new_size.x, new_size.y)) {
if(!video_.set_resolution(new_size.x, new_size.y)) {
LOG_GUI_E << LOG_HEADER << " resize aborted, resize failed.\n";
return;

View file

@ -28,7 +28,6 @@
#include "gettext.hpp"
#include "log.hpp"
#include "preferences.hpp"
#include "preferences_display.hpp"
#include "game_end_exceptions.hpp"
#include <cassert>
@ -579,7 +578,7 @@ void execute_command(display& disp, const hotkey_command& command, command_execu
disp.set_default_zoom();
break;
case HOTKEY_FULLSCREEN:
preferences::set_fullscreen(!preferences::fullscreen());
disp.video().set_fullscreen(!preferences::fullscreen());
break;
case HOTKEY_MAP_SCREENSHOT:
if (!disp.in_game() && !disp.in_editor()) {

View file

@ -48,7 +48,6 @@ display_manager::display_manager(display* d)
set_grid(grid());
set_turbo(turbo());
set_turbo_speed(turbo_speed());
set_fullscreen(fullscreen());
set_scroll_to_action(scroll_to_action());
set_color_cursors(preferences::get("color_cursors", false));
}
@ -58,136 +57,10 @@ display_manager::~display_manager()
disp = NULL;
}
bool detect_video_settings(CVideo& video, std::pair<int,int>& resolution, int& bpp, int& video_flags)
{
video_flags = fullscreen() ? SDL_FULLSCREEN : 0;
resolution = preferences::resolution();
int DefaultBPP = video.DefaultBpp;
#if !SDL_VERSION_ATLEAST(2, 0, 0)
/* This needs to be fixed properly. */
const SDL_VideoInfo* const video_info = SDL_GetVideoInfo();
if(video_info != NULL && video_info->vfmt != NULL) {
DefaultBPP = video_info->vfmt->BitsPerPixel;
}
#endif
std::cerr << "Checking video mode: " << resolution.first << 'x'
<< resolution.second << 'x' << DefaultBPP << "...\n";
typedef std::pair<int, int> res_t;
std::vector<res_t> res_list = video.get_available_resolutions();
if (res_list.empty()) {
res_list.push_back(res_t(800, 480));
res_list.push_back(res_t(800, 600));
res_list.push_back(res_t(1024, 600));
res_list.push_back(res_t(1024, 768));
res_list.push_back(res_t(1920, 1080));
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
bpp = DefaultBPP;
#else
bpp = video.modePossible(resolution.first, resolution.second,
DefaultBPP, video_flags, true);
#endif
BOOST_REVERSE_FOREACH(const res_t &res, res_list)
{
if (bpp != 0) break;
std::cerr << "Video mode " << resolution.first << 'x'
<< resolution.second << 'x' << DefaultBPP
<< " is not supported; attempting " << res.first
<< 'x' << res.second << 'x' << DefaultBPP << "...\n";
resolution = res;
#if SDL_VERSION_ATLEAST(2, 0, 0)
bpp = DefaultBPP;
#else
bpp = video.modePossible(resolution.first, resolution.second,
DefaultBPP, video_flags);
#endif
}
return bpp != 0;
}
void set_fullscreen(CVideo& video, const bool ison)
{
_set_fullscreen(ison);
const std::pair<int,int>& res = resolution();
if(video.isFullScreen() != ison) {
const int flags = ison ? SDL_FULLSCREEN : 0;
#if SDL_VERSION_ATLEAST(2, 0, 0)
int bpp = video.DefaultBpp;
#else
int bpp = video.bppForMode(res.first, res.second, flags);
#endif
video.setMode(res.first,res.second,bpp,flags);
if(disp) {
disp->redraw_everything();
}
}
}
void set_fullscreen(bool ison)
{
if(disp != NULL) {
set_fullscreen(disp->video(), ison);
} else {
// Only change the config value.
_set_fullscreen(ison);
}
}
void set_scroll_to_action(bool ison)
{
_set_scroll_to_action(ison);
}
void set_resolution(const std::pair<int,int>& resolution)
{
if(disp) {
set_resolution(disp->video(), resolution.first, resolution.second);
} else {
// Only change the config value. This part is needed when wesnoth is
// started with the -r parameter.
_set_resolution(resolution);
}
}
bool set_resolution(CVideo& video
, const unsigned width, const unsigned height)
{
SDL_Rect rect;
SDL_GetClipRect(video.getSurface(), &rect);
if(static_cast<unsigned int> (rect.w) == width && static_cast<unsigned int>(rect.h) == height) {
return true;
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
int bpp = video.DefaultBpp;
#else
const int flags = fullscreen() ? SDL_FULLSCREEN : 0;
int bpp = video.bppForMode(width, height, flags);
#endif
if(bpp != 0) {
//video.setMode(width, height, bpp, flags);
#if !SDL_VERSION_ATLEAST(2, 0, 0)
if(disp) {
disp->redraw_everything();
}
#endif
}
_set_resolution(std::make_pair(width, height));
return true;
}
void set_turbo(bool ison)
{
@ -305,7 +178,7 @@ bool show_video_mode_dialog(display& disp)
return false;
}
set_resolution(resolutions[static_cast<size_t>(choice)]);
disp.video().set_resolution(resolutions[static_cast<size_t>(choice)]);
return true;
}

View file

@ -15,7 +15,6 @@
#ifndef PREFERENCES_DISPLAY_HPP_INCLUDED
#define PREFERENCES_DISPLAY_HPP_INCLUDED
class CVideo;
class config;
class display;
@ -33,36 +32,8 @@ namespace preferences {
~display_manager();
};
/**
* Detect a good resolution.
*
* @param video The video 'holding' the framebuffer.
* @param resolution Any good resolution is returned through this reference.
* @param bpp A reference through which the best bpp is returned.
* @param video_flags A reference through which the video flags for setting the video mode are returned.
*
* @returns Whether valid video settings were found.
*/
bool detect_video_settings(CVideo& video, std::pair<int,int>& resolution, int& bpp, int& video_flags);
void set_fullscreen(CVideo& video, const bool ison);
void set_fullscreen(bool ison);
void set_scroll_to_action(bool ison);
void set_resolution(const std::pair<int,int>& res);
/**
* Set the resolution.
*
* @param video The video 'holding' the framebuffer.
* @param width The new width.
* @param height The new height.
*
* @returns The status true if width and height are the
* size of the framebuffer, false otherwise.
*/
bool set_resolution(CVideo& video
, const unsigned width, const unsigned height);
void set_turbo(bool ison);
void set_ellipses(bool ison);
void set_grid(bool ison);

View file

@ -24,7 +24,6 @@
#include "image.hpp"
#include "log.hpp"
#include "preferences.hpp"
#include "preferences_display.hpp"
#include "sdl/utils.hpp"
#include "sdl/rect.hpp"
#include "sdl/window.hpp"
@ -56,7 +55,9 @@ void resize_monitor::process(events::pump_info &info) {
if(info.resize_dimensions.first >= preferences::min_allowed_width()
&& info.resize_dimensions.second >= preferences::min_allowed_height()
&& disallow_resize == 0) {
preferences::set_resolution(info.resize_dimensions);
if (display::get_singleton()) {
display::get_singleton()->video().set_resolution(info.resize_dimensions);
}
}
#else
if(info.resize_dimensions.first > 0 &&
@ -905,3 +906,121 @@ void CVideo::clear_all_help_strings()
clear_help_string(help_string_);
}
bool CVideo::detect_video_settings(std::pair<int,int>& resolution, int& bpp, int& video_flags)
{
video_flags = preferences::fullscreen() ? SDL_FULLSCREEN : 0;
resolution = preferences::resolution();
int DefaultBPP = DefaultBpp;
#if !SDL_VERSION_ATLEAST(2, 0, 0)
/* This needs to be fixed properly. */
const SDL_VideoInfo* const video_info = SDL_GetVideoInfo();
if(video_info != NULL && video_info->vfmt != NULL) {
DefaultBPP = video_info->vfmt->BitsPerPixel;
}
#endif
std::cerr << "Checking video mode: " << resolution.first << 'x'
<< resolution.second << 'x' << DefaultBPP << "...\n";
typedef std::pair<int, int> res_t;
std::vector<res_t> res_list = get_available_resolutions();
if (res_list.empty()) {
res_list.push_back(res_t(800, 480));
res_list.push_back(res_t(800, 600));
res_list.push_back(res_t(1024, 600));
res_list.push_back(res_t(1024, 768));
res_list.push_back(res_t(1920, 1080));
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
bpp = DefaultBPP;
#else
bpp = modePossible(resolution.first, resolution.second,
DefaultBPP, video_flags, true);
#endif
BOOST_REVERSE_FOREACH(const res_t &res, res_list)
{
if (bpp != 0) break;
std::cerr << "Video mode " << resolution.first << 'x'
<< resolution.second << 'x' << DefaultBPP
<< " is not supported; attempting " << res.first
<< 'x' << res.second << 'x' << DefaultBPP << "...\n";
resolution = res;
#if SDL_VERSION_ATLEAST(2, 0, 0)
bpp = DefaultBPP;
#else
bpp = modePossible(resolution.first, resolution.second,
DefaultBPP, video_flags);
#endif
}
return bpp != 0;
}
void CVideo::set_fullscreen(bool ison)
{
if(display::get_singleton() != NULL) {
const std::pair<int,int>& res = preferences::resolution();
if(isFullScreen() != ison) {
const int flags = ison ? SDL_FULLSCREEN : 0;
#if SDL_VERSION_ATLEAST(2, 0, 0)
int bpp = DefaultBpp;
#else
int bpp = bppForMode(res.first, res.second, flags);
#endif
setMode(res.first,res.second,bpp,flags);
if(display::get_singleton()) {
display::get_singleton()->redraw_everything();
}
}
}
// Change the config value.
preferences::_set_fullscreen(ison);
}
void CVideo::set_resolution(const std::pair<int,int>& resolution)
{
if(display::get_singleton()) {
set_resolution(resolution.first, resolution.second);
} else {
// Only change the config value. This part is needed when wesnoth is
// started with the -r parameter.
preferences::_set_resolution(resolution);
}
}
bool CVideo::set_resolution(const unsigned width, const unsigned height)
{
SDL_Rect rect;
SDL_GetClipRect(getSurface(), &rect);
if(static_cast<unsigned int> (rect.w) == width && static_cast<unsigned int>(rect.h) == height) {
return true;
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
int bpp = DefaultBpp;
#else
const int flags = preferences::fullscreen() ? SDL_FULLSCREEN : 0;
int bpp = bppForMode(width, height, flags);
#endif
if(bpp != 0) {
// setMode(width, height, bpp, flags);
#if !SDL_VERSION_ATLEAST(2, 0, 0)
if(display::get_singleton()) {
display::get_singleton()->redraw_everything();
}
#endif
}
preferences::_set_resolution(std::make_pair(width, height));
return true;
}

View file

@ -82,6 +82,32 @@ class CVideo : private boost::noncopyable {
int modePossible( int x, int y, int bits_per_pixel, int flags, bool current_screen_optimal=false);
int setMode( int x, int y, int bits_per_pixel, int flags );
/**
* Detect a good resolution.
*
* @param video The video 'holding' the framebuffer.
* @param resolution Any good resolution is returned through this reference.
* @param bpp A reference through which the best bpp is returned.
* @param video_flags A reference through which the video flags for setting the video mode are returned.
*
* @returns Whether valid video settings were found.
*/
bool detect_video_settings(std::pair<int,int>& resolution, int& bpp, int& video_flags);
void set_fullscreen(bool ison);
/**
* Set the resolution.
*
* @param width The new width.
* @param height The new height.
*
* @returns The status true if width and height are the
* size of the framebuffer, false otherwise.
*/
void set_resolution(const std::pair<int,int>& res);
bool set_resolution(const unsigned width, const unsigned height);
//did the mode change, since the last call to the modeChanged() method?
bool modeChanged();