Skip to content

Commit

Permalink
make CVideo a singleton
Browse files Browse the repository at this point in the history
half of CVideo's member variables were already static members (in anon
namespaces) so it was never possibel to have more than one of this
class. Making this class a singelton allows us to move all these
variables into the CVideo class.
  • Loading branch information
gfgtdf committed Jan 11, 2016
1 parent 9d6f154 commit 9309151
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/actions/attack.cpp
Expand Up @@ -1413,7 +1413,7 @@ namespace

//to make mp games equal we only allow selecting advancements to the current side.
//otherwise we'd give an unfair advantage to the side that hosts ai sides if units advance during ai turns.
if(!non_interactive() && (force_dialog_ || (t.is_local_human() && !t.is_idle() && (is_current_side || !is_mp))))
if(!CVideo::get_singleton().non_interactive() && (force_dialog_ || (t.is_local_human() && !t.is_idle() && (is_current_side || !is_mp))))
{
res = dialogs::advance_unit_dialog(loc_);
}
Expand Down
2 changes: 1 addition & 1 deletion src/desktop/windows_tray_notification.cpp
Expand Up @@ -194,7 +194,7 @@ HWND windows_tray_notification::get_window_handle()
if (SDL_GetWMInfo(&wmInfo) != 1) {
#else
// SDL 1.2 keeps track of window handles internally whereas SDL 2.0 allows the caller control over which window to use
if (SDL_GetWindowWMInfo (static_cast<SDL_Window *> (*CVideo::get_window()), &wmInfo) != SDL_TRUE) {
if (SDL_GetWindowWMInfo (static_cast<SDL_Window *> (*CVideo::get_singleton().get_window()), &wmInfo) != SDL_TRUE) {
#endif
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/display.cpp
Expand Up @@ -240,7 +240,7 @@ display::display(const display_context * dc, CVideo& video, boost::weak_ptr<wb::

read(level.child_or_empty("display"));

if(non_interactive()
if(CVideo::get_singleton().non_interactive()
&& (get_video_surface() != NULL
&& video.faked())) {
screen_.lock_updates(true);
Expand Down
4 changes: 2 additions & 2 deletions src/editor/map/context_manager.cpp
Expand Up @@ -166,7 +166,7 @@ context_manager::~context_manager()
#if !SDL_VERSION_ATLEAST(2, 0, 0)
SDL_WM_SetCaption(game_config::get_default_title_string().c_str(), NULL);
#else
CVideo::set_window_title(game_config::get_default_title_string());
CVideo::get_singleton().set_window_title(game_config::get_default_title_string());
#endif
}

Expand Down Expand Up @@ -1030,7 +1030,7 @@ void context_manager::set_window_title()
#if !SDL_VERSION_ATLEAST(2, 0, 0)
SDL_WM_SetCaption(wm_title_string.c_str(), NULL);
#else
CVideo::set_window_title(wm_title_string);
CVideo::get_singleton().set_window_title(wm_title_string);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/events.cpp
Expand Up @@ -657,7 +657,7 @@ void peek_for_resize()
for (int i = 0; i < num; i++) {
if (events[i].type == SDL_WINDOWEVENT &&
events[i].window.event == SDL_WINDOWEVENT_RESIZED) {
update_framebuffer();
CVideo::get_singleton().update_framebuffer();

}
}
Expand Down
6 changes: 3 additions & 3 deletions src/game_launcher.cpp
Expand Up @@ -393,12 +393,12 @@ bool game_launcher::init_video()
video_.init_window();

// Set window title and icon
CVideo::set_window_title(game_config::get_default_title_string());
CVideo::get_singleton().set_window_title(game_config::get_default_title_string());

#if !(defined(__APPLE__))
surface icon(image::get_image("icons/icon-game.png", image::UNSCALED));
if(icon != NULL) {
CVideo::set_window_icon(icon);
CVideo::get_singleton().set_window_icon(icon);
}
#endif
return true;
Expand Down Expand Up @@ -1064,7 +1064,7 @@ bool game_launcher::change_language()

if (!(cmdline_opts_.nogui || cmdline_opts_.headless_unit_test)) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
CVideo::set_window_title(game_config::get_default_title_string());
CVideo::get_singleton().set_window_title(game_config::get_default_title_string());
#else
SDL_WM_SetCaption(game_config::get_default_title_string().c_str(), NULL);
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/game_preferences.cpp
Expand Up @@ -951,7 +951,7 @@ void set_autosavemax(int value)

std::string theme()
{
if(non_interactive()) {
if(CVideo::get_singleton().non_interactive()) {
static const std::string null_theme = "null";
return null_theme;
}
Expand Down
2 changes: 1 addition & 1 deletion src/loadscreen.cpp
Expand Up @@ -211,7 +211,7 @@ void loadscreen::draw_screen(const std::string &text)
#if SDL_VERSION_ATLEAST(2,0,0)
if (ev.type == SDL_WINDOWEVENT &&
ev.window.event == SDL_WINDOWEVENT_RESIZED) {
update_framebuffer();
screen_.update_framebuffer();
}
if (ev.type == SDL_WINDOWEVENT &&
(ev.window.event == SDL_WINDOWEVENT_RESIZED ||
Expand Down
10 changes: 5 additions & 5 deletions src/play_controller.cpp
Expand Up @@ -965,7 +965,7 @@ void play_controller::check_victory()
return;
}

if (non_interactive()) {
if (CVideo::get_singleton().non_interactive()) {
LOG_AIT << "winner: ";
BOOST_FOREACH(unsigned l, not_defeated) {
std::string ai = ai::manager::get_active_ai_identifier_for_side(l);
Expand All @@ -986,7 +986,7 @@ void play_controller::check_victory()

void play_controller::process_oos(const std::string& msg) const
{
if (non_interactive()) {
if (CVideo::get_singleton().non_interactive()) {
throw game::game_error(msg);
}
if (game_config::ignore_replay_errors) return;
Expand Down Expand Up @@ -1181,7 +1181,7 @@ void play_controller::play_turn()

LOG_NG << "turn: " << turn() << "\n";

if(non_interactive()) {
if(CVideo::get_singleton().non_interactive()) {
LOG_AIT << "Turn " << turn() << ":" << std::endl;
}

Expand All @@ -1206,7 +1206,7 @@ void play_controller::play_turn()
if(is_regular_game_end()) {
return;
}
if(non_interactive()) {
if(CVideo::get_singleton().non_interactive()) {
LOG_AIT << " Player " << current_side() << ": " <<
current_team().villages().size() << " Villages" <<
std::endl;
Expand Down Expand Up @@ -1236,7 +1236,7 @@ void play_controller::check_time_over()
return;
}

if(non_interactive()) {
if(CVideo::get_singleton().non_interactive()) {
LOG_AIT << "time over (draw)\n";
ai_testing::log_draw();
}
Expand Down
2 changes: 1 addition & 1 deletion src/preferences.cpp
Expand Up @@ -386,7 +386,7 @@ void _set_fullscreen(bool ison)

bool turbo()
{
if(non_interactive()) {
if(CVideo::get_singleton().non_interactive()) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/sdl/compat.hpp
Expand Up @@ -38,7 +38,7 @@
#define KMOD_META KMOD_GUI
#define SDL_FULLSCREEN SDL_WINDOW_FULLSCREEN_DESKTOP
#define SDL_EVENTMASK(EVENT) EVENT, EVENT
#define SDL_GetAppState CVideo::window_state
#define SDL_GetAppState CVideo::get_singleton().window_state

#endif

Expand Down
26 changes: 13 additions & 13 deletions src/video.cpp
Expand Up @@ -43,6 +43,8 @@ static lg::log_domain log_display("display");
#define LOG_DP LOG_STREAM(info, log_display)
#define ERR_DP LOG_STREAM(err, log_display)

CVideo* CVideo::singleton_ = NULL;

namespace {
#if !SDL_VERSION_ATLEAST(2, 0, 0)
bool is_fullscreen = false;
Expand Down Expand Up @@ -245,13 +247,9 @@ namespace {

surface frameBuffer = NULL;
bool fake_interactive = false;

#if SDL_VERSION_ATLEAST(2, 0, 0)
sdl::twindow* window = NULL;
#endif
}

bool non_interactive()
bool CVideo::non_interactive()
{
if (fake_interactive)
return false;
Expand Down Expand Up @@ -380,6 +378,7 @@ void CVideo::video_event_handler::handle_event(const SDL_Event &event)


CVideo::CVideo(FAKE_TYPES type) :
window(),
#ifdef SDL_GPU
shader_(),
#endif
Expand All @@ -391,6 +390,8 @@ CVideo::CVideo(FAKE_TYPES type) :
help_string_(0),
updatesLocked_(0)
{
assert(!singleton_);
singleton_ = this;
initSDL();
switch(type)
{
Expand Down Expand Up @@ -449,9 +450,8 @@ CVideo::~CVideo()
{
LOG_DP << "calling SDL_Quit()\n";
SDL_Quit();
#if SDL_VERSION_ATLEAST(2, 0, 0)
delete window;
#endif
assert(singleton_);
singleton_ = NULL;
LOG_DP << "called SDL_Quit()\n";
}

Expand Down Expand Up @@ -571,7 +571,7 @@ int CVideo::modePossible( int x, int y, int bits_per_pixel, int flags, bool curr

#if SDL_VERSION_ATLEAST(2, 0, 0)

void update_framebuffer()
void CVideo::update_framebuffer()
{
if (!window)
return;
Expand Down Expand Up @@ -608,7 +608,7 @@ bool CVideo::init_window()
}

// Initialize window
window = new sdl::twindow("", x, y, w, h, video_flags, SDL_RENDERER_SOFTWARE);
window.reset(new sdl::twindow("", x, y, w, h, video_flags, SDL_RENDERER_SOFTWARE));

std::cerr << "Setting mode to " << w << "x" << h << std::endl;

Expand Down Expand Up @@ -811,13 +811,13 @@ void CVideo::set_window_icon(surface& icon)

sdl::twindow *CVideo::get_window()
{
return window;
return window.get();
}

#endif

#if SDL_VERSION_ATLEAST(2, 0, 0)
static int sdl_display_index()
static int sdl_display_index(sdl::twindow* window)
{
if(window) {
return SDL_GetWindowDisplayIndex(*window);
Expand All @@ -831,7 +831,7 @@ std::vector<std::pair<int, int> > CVideo::get_available_resolutions()
{
std::vector<std::pair<int, int> > result;

const int modes = SDL_GetNumDisplayModes(sdl_display_index());
const int modes = SDL_GetNumDisplayModes(sdl_display_index(window.get()));
if(modes <= 0) {
std::cerr << "No modes supported\n";
return result;
Expand Down
24 changes: 15 additions & 9 deletions src/video.hpp
Expand Up @@ -19,6 +19,7 @@
#include "lua_jailbreak_exception.hpp"

#include <boost/utility.hpp>
#include <boost/scoped_ptr.hpp>

#if SDL_VERSION_ATLEAST(2,0,0)
#include "sdl/window.hpp"
Expand Down Expand Up @@ -49,16 +50,11 @@ GPU_Target *get_render_target();

surface display_format_alpha(surface surf);
surface& get_video_surface();
#if SDL_VERSION_ATLEAST(2, 0, 0)
//this needs to be invoked immediately after a resize event or the game will crash.
void update_framebuffer();
#endif


SDL_Rect screen_area();


bool non_interactive();

//which areas of the screen will be updated when the buffer is flipped?
void update_rect(size_t x, size_t y, size_t w, size_t h);
Expand All @@ -82,6 +78,9 @@ class CVideo : private boost::noncopyable {

CVideo(FAKE_TYPES type = NO_FAKE);
~CVideo();
static CVideo& get_singleton() { return *singleton_; }

bool non_interactive();

const static int DefaultBpp = 32;

Expand Down Expand Up @@ -212,26 +211,29 @@ class CVideo : private boost::noncopyable {
bool update_locked() const;

#if SDL_VERSION_ATLEAST(2, 0, 0)
//this needs to be invoked immediately after a resize event or the game will crash.
void update_framebuffer();

/**
* Wrapper for SDL_GetAppState.
*/
static Uint8 window_state();
Uint8 window_state();

/**
* Sets the title of the main window.
*
* @param title The new title for the window.
*/
static void set_window_title(const std::string& title);
void set_window_title(const std::string& title);

/**
* Sets the icon of the main window.
*
* @param icon The new icon for the window.
*/
static void set_window_icon(surface& icon);
void set_window_icon(surface& icon);

static sdl::twindow *get_window();
sdl::twindow *get_window();
#endif

/**
Expand All @@ -240,7 +242,11 @@ class CVideo : private boost::noncopyable {
std::vector<std::pair<int, int> > get_available_resolutions();

private:
static CVideo* singleton_;

#if SDL_VERSION_ATLEAST(2, 0, 0)
boost::scoped_ptr<sdl::twindow> window;
#endif
class video_event_handler : public events::sdl_handler {
public:
virtual void handle_event(const SDL_Event &event);
Expand Down

0 comments on commit 9309151

Please sign in to comment.