Skip to content

Commit

Permalink
Cleaned up the mess of CVideo references passed around the game initi…
Browse files Browse the repository at this point in the history
…alization process

Essentially, we had CVideo arguments being passed down this chain:
- game_launcher
- free-standing MP initialization functions
- campaign_controller
- playsingle_controller/playmp_controller
- play_controller
- game_display
- display

And likewise down through
- game_launcher
- editor_controller
- editor_display
- display

With only a minimal number of actual calls along the way. :| There were maybe... two remaining?

This removes the CVideo arguments and class members from both chains (except of course, game_launcher.
That's where the "real" CVideo object lives).

The display class now initializes its CVideo reference from the singleton, which is also used in the
very few other places it's needed. I also replaced a check for a null video ptr in show_tooltip()
with a faked() check (see src/tooltips.cpp). That seems to make more sense, since CVideo is never
null now.
  • Loading branch information
Vultraz authored and GregoryLundberg committed Nov 30, 2017
1 parent cdfe689 commit e3f4a59
Show file tree
Hide file tree
Showing 23 changed files with 72 additions and 90 deletions.
10 changes: 5 additions & 5 deletions src/display.cpp
Expand Up @@ -153,13 +153,13 @@ void display::remove_single_overlay(const map_location& loc, const std::string&
}
}

display::display(const display_context * dc, CVideo& video, std::weak_ptr<wb::manager> wb, reports & reports_object, const config& theme_cfg, const config& level, bool auto_join) :
display::display(const display_context * dc, std::weak_ptr<wb::manager> wb, reports & reports_object, const config& theme_cfg, const config& level, bool auto_join) :
video2::draw_layering(auto_join),
dc_(dc),
halo_man_(new halo::manager(*this)),
wb_(wb),
exclusive_unit_draw_requests_(),
screen_(video),
screen_(CVideo::get_singleton()),
currentTeam_(0),
dont_show_all_(false),
xpos_(0),
Expand Down Expand Up @@ -236,9 +236,9 @@ display::display(const display_context * dc, CVideo& video, std::weak_ptr<wb::ma

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

if(video.non_interactive()
&& (video.getSurface() != nullptr
&& video.faked())) {
if(screen_.non_interactive()
&& (screen_.getSurface() != nullptr
&& screen_.faked())) {
screen_.lock_updates(true);
}

Expand Down
2 changes: 1 addition & 1 deletion src/display.hpp
Expand Up @@ -78,7 +78,7 @@ class gamemap;
class display : public filter_context, public video2::draw_layering
{
public:
display(const display_context * dc, CVideo& video, std::weak_ptr<wb::manager> wb,
display(const display_context * dc, std::weak_ptr<wb::manager> wb,
reports & reports_object,
const config& theme_cfg, const config& level, bool auto_join=true);
virtual ~display();
Expand Down
6 changes: 3 additions & 3 deletions src/editor/controller/editor_controller.cpp
Expand Up @@ -59,17 +59,17 @@ static std::vector<std::string> saved_windows_;

namespace editor {

editor_controller::editor_controller(const config &game_config, CVideo& video)
editor_controller::editor_controller(const config &game_config)
: controller_base(game_config)
, mouse_handler_base()
, quit_confirmation(std::bind(&editor_controller::quit_confirm, this))
, active_menu_(editor::MAP)
, reports_(new reports())
, gui_(new editor_display(*this, video, *reports_, controller_base::get_theme(game_config, "editor")))
, gui_(new editor_display(*this, *reports_, controller_base::get_theme(game_config, "editor")))
, tods_()
, context_manager_(new context_manager(*gui_.get(), game_config_))
, toolkit_(nullptr)
, tooltip_manager_(video)
, tooltip_manager_()
, floating_label_manager_(nullptr)
, help_manager_(nullptr)
, do_quit_(false)
Expand Down
2 changes: 1 addition & 1 deletion src/editor/controller/editor_controller.hpp
Expand Up @@ -79,7 +79,7 @@ class editor_controller : public controller_base,
* to the map can be retrieved between the main loop's end and the controller's
* destruction.
*/
editor_controller(const config &game_config, CVideo& video);
explicit editor_controller(const config &game_config);

~editor_controller();

Expand Down
6 changes: 3 additions & 3 deletions src/editor/editor_display.cpp
Expand Up @@ -54,12 +54,12 @@ const display_context * get_dummy_display_context() {

// End dummy display context

editor_display::editor_display(editor_controller& controller, CVideo& video, reports& reports_object, const config& theme_cfg)
: display(get_dummy_display_context(), video, std::shared_ptr<wb::manager>(), reports_object, theme_cfg, config())
editor_display::editor_display(editor_controller& controller, reports& reports_object, const config& theme_cfg)
: display(get_dummy_display_context(), std::shared_ptr<wb::manager>(), reports_object, theme_cfg, config())
, brush_locations_()
, controller_(controller)
{
video.clear_screen();
video().clear_screen();
}

void editor_display::add_brush_loc(const map_location& hex)
Expand Down
2 changes: 1 addition & 1 deletion src/editor/editor_display.hpp
Expand Up @@ -24,7 +24,7 @@ const display_context * get_dummy_display_context();
class editor_display : public display
{
public:
editor_display(editor_controller& controller, CVideo& video, reports& reports_object, const config& theme_cfg);
editor_display(editor_controller& controller, reports& reports_object, const config& theme_cfg);

bool in_editor() const override { return true; }

Expand Down
4 changes: 2 additions & 2 deletions src/editor/editor_main.cpp
Expand Up @@ -25,15 +25,15 @@ lg::log_domain log_editor("editor");

namespace editor {

EXIT_STATUS start(const config& game_conf, CVideo& video, const std::string& filename /* = "" */,
EXIT_STATUS start(const config& game_conf, const std::string& filename /* = "" */,
bool take_screenshot /* = false */, const std::string& screenshot_filename /* = "map_screenshot.bmp" */)
{
EXIT_STATUS e = EXIT_ERROR;
try {
hotkey::scope_changer h_;
hotkey::deactivate_all_scopes();
hotkey::set_scope_active(hotkey::SCOPE_EDITOR);
editor_controller editor(game_conf, video);
editor_controller editor(game_conf);
if (!filename.empty() && filesystem::file_exists (filename)) {
if (filesystem::is_directory(filename)) {
editor.context_manager_->set_default_dir(filename);
Expand Down
2 changes: 1 addition & 1 deletion src/editor/editor_main.hpp
Expand Up @@ -34,6 +34,6 @@ enum EXIT_STATUS {
* go back to the titlescreen or quit to desktop altogether)
*/

EXIT_STATUS start(const config& game_config, CVideo& video, const std::string& filename = "", bool take_screenshot = false, const std::string& screenshot_filename = "map_screenshot.bmp");
EXIT_STATUS start(const config& game_config, const std::string& filename = "", bool take_screenshot = false, const std::string& screenshot_filename = "map_screenshot.bmp");

} //end namespace editor
10 changes: 5 additions & 5 deletions src/game_display.cpp
Expand Up @@ -62,13 +62,13 @@ std::map<map_location,fixed_t> game_display::debugHighlights_;
*/
std::vector<surface> footsteps_images(const map_location& loc, const pathfind::marked_route & route_, const display_context * dc_);

game_display::game_display(game_board& board, CVideo& video, std::weak_ptr<wb::manager> wb,
game_display::game_display(game_board& board, std::weak_ptr<wb::manager> wb,
reports & reports_object,
const tod_manager& tod,
const config& theme_cfg,
const config& level,
bool) :
display(&board, video, wb, reports_object, theme_cfg, level, false),
display(&board, wb, reports_object, theme_cfg, level, false),
overlay_map_(),
attack_indicator_src_(),
attack_indicator_dst_(),
Expand All @@ -83,17 +83,17 @@ game_display::game_display(game_board& board, CVideo& video, std::weak_ptr<wb::m
needs_rebuild_(false)
{
replace_overlay_map(&overlay_map_);
video.clear_screen();
video().clear_screen();
}

game_display* game_display::create_dummy_display(CVideo& video)
game_display* game_display::create_dummy_display()
{
static config dummy_cfg;
static config dummy_cfg2;
static game_board dummy_board(std::make_shared<terrain_type_data>(dummy_cfg), dummy_cfg2);
static tod_manager dummy_tod(dummy_cfg);
static reports rep_;
return new game_display(dummy_board, video, std::shared_ptr<wb::manager>(), rep_, dummy_tod,
return new game_display(dummy_board, std::shared_ptr<wb::manager>(), rep_, dummy_tod,
dummy_cfg, dummy_cfg, true);
}

Expand Down
4 changes: 2 additions & 2 deletions src/game_display.hpp
Expand Up @@ -36,15 +36,15 @@ class game_board;
class game_display : public display
{
public:
game_display(game_board& board, CVideo& video,
game_display(game_board& board,
std::weak_ptr<wb::manager> wb,
reports & reports_object,
const tod_manager& tod_manager,
const config& theme_cfg,
const config& level,
bool dummy=false);

static game_display* create_dummy_display(CVideo& video);
static game_display* create_dummy_display();

~game_display();
static game_display* get_singleton()
Expand Down
29 changes: 13 additions & 16 deletions src/game_initialization/multiplayer.cpp
Expand Up @@ -50,7 +50,7 @@ static lg::log_domain log_mp("mp/main");
namespace
{
/** Opens a new server connection and prompts the client for login credentials, if necessary. */
std::pair<wesnothd_connection_ptr, config> open_connection(CVideo& video, std::string host)
std::pair<wesnothd_connection_ptr, config> open_connection(std::string host)
{
DBG_MP << "opening connection" << std::endl;

Expand Down Expand Up @@ -323,7 +323,7 @@ std::pair<wesnothd_connection_ptr, config> open_connection(CVideo& video, std::s
gui2::dialogs::mp_login dlg(host, error_message, !((*error)["password_request"].empty()));

// Need to show the dialog from the main thread or it won't appear.
events::call_in_main_thread([&dlg, &video]() { dlg.show(); });
events::call_in_main_thread([&dlg]() { dlg.show(); });

switch(dlg.get_retval()) {
//Log in with password
Expand Down Expand Up @@ -360,16 +360,13 @@ std::pair<wesnothd_connection_ptr, config> open_connection(CVideo& video, std::s
/** Helper struct to manage the MP workflow arguments. */
struct mp_workflow_helper
{
mp_workflow_helper(CVideo& video, const config& gc, saved_game& state, wesnothd_connection* connection, mp::lobby_info* li)
: video(video)
, game_config(gc)
mp_workflow_helper(const config& gc, saved_game& state, wesnothd_connection* connection, mp::lobby_info* li)
: game_config(gc)
, state(state)
, connection(connection)
, lobby_info(li)
{}

CVideo& video;

const config& game_config;

saved_game& state;
Expand Down Expand Up @@ -423,7 +420,7 @@ void enter_wait_mode(mp_workflow_helper_ptr helper, int game_id, bool observe)
}

if(dlg_ok) {
campaign_controller controller(helper->video, helper->state, helper->game_config, game_config_manager::get()->terrain_types());
campaign_controller controller(helper->state, helper->game_config, game_config_manager::get()->terrain_types());
controller.set_mp_info(campaign_info.get());
controller.play_game();
}
Expand Down Expand Up @@ -454,7 +451,7 @@ void enter_staging_mode(mp_workflow_helper_ptr helper)
} // end connect_engine_ptr, dlg scope

if(dlg_ok) {
campaign_controller controller(helper->video, helper->state, helper->game_config, game_config_manager::get()->terrain_types());
campaign_controller controller(helper->state, helper->game_config, game_config_manager::get()->terrain_types());
controller.set_mp_info(campaign_info.get());
controller.play_game();
}
Expand Down Expand Up @@ -569,7 +566,7 @@ bool enter_lobby_mode(mp_workflow_helper_ptr helper, const std::vector<std::stri
/** Pubic entry points for the MP workflow */
namespace mp
{
void start_client(CVideo& video, const config& game_config, saved_game& state, const std::string& host)
void start_client(const config& game_config, saved_game& state, const std::string& host)
{
const config* game_config_ptr = &game_config;

Expand All @@ -586,7 +583,7 @@ void start_client(CVideo& video, const config& game_config, saved_game& state, c
config lobby_config;

gui2::dialogs::loading_screen::display([&]() {
std::tie(connection, lobby_config) = open_connection(video, host);
std::tie(connection, lobby_config) = open_connection(host);
});

if(!connection) {
Expand All @@ -597,7 +594,7 @@ void start_client(CVideo& video, const config& game_config, saved_game& state, c
bool re_enter = false;

do {
workflow_helper.reset(new mp_workflow_helper(video, *game_config_ptr, state, connection.get(), nullptr));
workflow_helper.reset(new mp_workflow_helper(*game_config_ptr, state, connection.get(), nullptr));

// A return of false means a config reload was requested, so do that and then loop.
re_enter = !enter_lobby_mode(workflow_helper, installed_addons, lobby_config);
Expand Down Expand Up @@ -641,20 +638,20 @@ bool goto_mp_wait(saved_game& state, const config& game_config, wesnothd_connect
return dlg.show();
}

void start_local_game(CVideo& video, const config& game_config, saved_game& state)
void start_local_game(const config& game_config, saved_game& state)
{
DBG_MP << "starting local game" << std::endl;

preferences::set_message_private(false);

// TODO: should lobby_info take a nullptr in this case, or should we pass the installed_addons data here too?
lobby_info li(game_config, {});
mp_workflow_helper_ptr workflow_helper = std::make_shared<mp_workflow_helper>(video, game_config, state, nullptr, &li);
mp_workflow_helper_ptr workflow_helper = std::make_shared<mp_workflow_helper>(game_config, state, nullptr, &li);

enter_create_mode(workflow_helper);
}

void start_local_game_commandline(CVideo& video, const config& game_config, saved_game& state, const commandline_options& cmdline_opts)
void start_local_game_commandline(const config& game_config, saved_game& state, const commandline_options& cmdline_opts)
{
DBG_MP << "starting local MP game from commandline" << std::endl;

Expand Down Expand Up @@ -750,7 +747,7 @@ void start_local_game_commandline(CVideo& video, const config& game_config, save
unsigned int repeat = (cmdline_opts.multiplayer_repeat) ? *cmdline_opts.multiplayer_repeat : 1;
for(unsigned int i = 0; i < repeat; i++){
saved_game state_copy(state);
campaign_controller controller(video, state_copy, game_config, game_config_manager::get()->terrain_types());
campaign_controller controller(state_copy, game_config, game_config_manager::get()->terrain_types());
controller.play_game();
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/game_initialization/multiplayer.hpp
Expand Up @@ -33,27 +33,25 @@ const size_t max_login_size = 20;

/** Starts a multiplayer game in single-user mode.
*
* @param video The global display
* @param game_config The global, top-level WML configuration for the game
*/
void start_local_game(CVideo& video, const config& game_config,
void start_local_game(const config& game_config,
saved_game& state);

/** Starts a multiplayer game in single-user mode.
*
* Same parameters as start_local_game plus:
* cmdline_opts The commandline options
*/
void start_local_game_commandline(CVideo& video, const config& game_config,
void start_local_game_commandline(const config& game_config,
saved_game& state, const commandline_options& cmdline_opts);

/** Starts a multiplayer game in client mode.
*
* @param video The global display
* @param game_config The global, top-level WML configuration for the game
* @param host The host to connect to.
*/
void start_client(CVideo& video, const config& game_config,
void start_client(const config& game_config,
saved_game& state, const std::string& host);

/**
Expand Down
6 changes: 3 additions & 3 deletions src/game_initialization/playcampaign.cpp
Expand Up @@ -186,7 +186,7 @@ void campaign_controller::show_carryover_message(playsingle_controller& playcont

LEVEL_RESULT campaign_controller::playsingle_scenario(end_level_data &end_level)
{
playsingle_controller playcontroller(is_replay_ ? state_.get_replay_starting_pos() : state_.get_starting_pos(), state_, game_config_, tdata_, video_, false);
playsingle_controller playcontroller(is_replay_ ? state_.get_replay_starting_pos() : state_.get_starting_pos(), state_, game_config_, tdata_, false);
LOG_NG << "created objects... " << (SDL_GetTicks() - playcontroller.get_ticks()) << "\n";
if(is_replay_) {
playcontroller.enable_replay(is_unit_test_);
Expand All @@ -208,7 +208,7 @@ LEVEL_RESULT campaign_controller::playsingle_scenario(end_level_data &end_level)
end_level = playcontroller.get_end_level_data_const();

show_carryover_message(playcontroller, end_level, res);
if(!video_.faked())
if(!CVideo::get_singleton().faked())
{
playcontroller.maybe_linger();
}
Expand All @@ -221,7 +221,7 @@ LEVEL_RESULT campaign_controller::playmp_scenario(end_level_data &end_level)
{

playmp_controller playcontroller(state_.get_starting_pos(), state_,
game_config_, tdata_, video_, mp_info_);
game_config_, tdata_, mp_info_);
LEVEL_RESULT res = playcontroller.play_scenario(state_.get_starting_pos());

//Check if the player started as mp client and changed to host
Expand Down
6 changes: 2 additions & 4 deletions src/game_initialization/playcampaign.hpp
Expand Up @@ -57,17 +57,15 @@ struct mp_campaign_info

class campaign_controller
{
CVideo& video_;
saved_game& state_;
const config& game_config_;
const ter_data_cache & tdata_;
const bool is_unit_test_;
bool is_replay_;
mp_campaign_info* mp_info_;
public:
campaign_controller(CVideo& video, saved_game& state, const config& game_config, const ter_data_cache & tdata, bool is_unit_test = false)
: video_(video)
, state_(state)
campaign_controller(saved_game& state, const config& game_config, const ter_data_cache & tdata, bool is_unit_test = false)
: state_(state)
, game_config_(game_config)
, tdata_(tdata)
, is_unit_test_(is_unit_test)
Expand Down

0 comments on commit e3f4a59

Please sign in to comment.