Skip to content

Commit

Permalink
Merge pull request #568 from Vultraz/prefs_to_display
Browse files Browse the repository at this point in the history
Moved video related functions from preferences to CVideo
  • Loading branch information
Vultraz committed Dec 31, 2015
2 parents a17bd92 + 4ab959f commit 4af59ef
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 180 deletions.
5 changes: 4 additions & 1 deletion src/game_initialization/multiplayer.cpp
Expand Up @@ -649,7 +649,10 @@ 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);

if (disp.get_singleton()) {
disp.get_singleton()->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 ;
Expand Down
15 changes: 8 additions & 7 deletions src/game_launcher.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -409,15 +408,15 @@ 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 = 32;
bpp = CVideo::DefaultBpp;
}

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";
}
Expand All @@ -433,7 +432,9 @@ bool game_launcher::init_video()

std::cerr << "Setting mode to " << resolution.first << "x" << resolution.second << "x" << bpp << "\n";
const int res = video_.setMode(resolution.first,resolution.second,bpp,video_flags);
#if !SDL_VERSION_ATLEAST(2, 0, 0)
video_.setBpp(bpp);
#endif
if(res == 0) {
std::cerr << "Required video mode, " << resolution.first << "x"
<< resolution.second << "x" << bpp << " is not supported\n";
Expand Down
4 changes: 2 additions & 2 deletions src/game_preferences_display.cpp
Expand Up @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions src/gui/dialogs/lobby_main.cpp
Expand Up @@ -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>
Expand Down Expand Up @@ -431,15 +430,15 @@ 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";
}
}

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();
Expand Down
3 changes: 1 addition & 2 deletions src/gui/dialogs/title_screen.cpp
Expand Up @@ -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>
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/window.cpp
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/hotkey/command_executor.cpp
Expand Up @@ -28,7 +28,6 @@
#include "gettext.hpp"
#include "log.hpp"
#include "preferences.hpp"
#include "preferences_display.hpp"
#include "game_end_exceptions.hpp"

#include <cassert>
Expand Down Expand Up @@ -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()) {
Expand Down
134 changes: 4 additions & 130 deletions src/preferences_display.cpp
Expand Up @@ -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));
}
Expand All @@ -58,138 +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 = 24;
#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));
}

bpp = video.modePossible(resolution.first, resolution.second,
DefaultBPP, video_flags, true);

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;
bpp = video.modePossible(resolution.first, resolution.second,
DefaultBPP, video_flags);
}

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;
int bpp = video.bppForMode(res.first, res.second, flags);

if(bpp > 0) {
video.setMode(res.first,res.second,bpp,flags);
if(disp) {
disp->redraw_everything();
}
} else {
int tmp_flags = flags;
std::pair<int,int> tmp_res;
if(detect_video_settings(video, tmp_res, bpp, tmp_flags)) {
set_resolution(video, tmp_res.first, tmp_res.second);
// TODO: see if below line is actually needed, possibly for displays that only support 16 bbp
} else if(video.modePossible(1024,768,16,flags)) {
set_resolution(video, 1024, 768);
} else {
gui2::show_transient_message(video,"",_("The video mode could not be changed. Your window manager must be set to 16 bits per pixel to run the game in windowed mode. Your display must support 1024x768x16 to run the game full screen."));
}
// We reinit color cursors, because SDL on Mac seems to forget the SDL_Cursor
set_color_cursors(preferences::get("color_cursors", false));
}
}
}

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;
}

const int flags = fullscreen() ? SDL_FULLSCREEN : 0;
int bpp = video.bppForMode(width, height, flags);

if(bpp != 0) {
//video.setMode(width, height, bpp, flags);

#if !SDL_VERSION_ATLEAST(2, 0, 0)
if(disp) {
disp->redraw_everything();
}
#endif

} else {
// grzywacz: is this even true?
gui2::show_transient_message(video,"",_("The video mode could not be changed. Your window manager must be set to 16 bits per pixel to run the game in windowed mode. Your display must support 1024x768x16 to run the game full screen."));
return false;
}

_set_resolution(std::make_pair(width, height));

return true;
}

void set_turbo(bool ison)
{
Expand Down Expand Up @@ -307,7 +178,10 @@ bool show_video_mode_dialog(display& disp)
return false;
}

set_resolution(resolutions[static_cast<size_t>(choice)]);
if (disp.get_singleton()) {
disp.get_singleton()->video().set_resolution(resolutions[static_cast<size_t>(choice)]);
}

return true;
}

Expand Down
29 changes: 0 additions & 29 deletions src/preferences_display.hpp
Expand Up @@ -15,7 +15,6 @@
#ifndef PREFERENCES_DISPLAY_HPP_INCLUDED
#define PREFERENCES_DISPLAY_HPP_INCLUDED

class CVideo;
class config;
class display;

Expand All @@ -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);
Expand Down

0 comments on commit 4af59ef

Please sign in to comment.