Skip to content

Commit

Permalink
Merge branch 'add_display_context'
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Jun 10, 2014
2 parents 35b8730 + 43ade46 commit ae5436d
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 99 deletions.
74 changes: 31 additions & 43 deletions src/display.cpp
Expand Up @@ -78,8 +78,8 @@ int display::last_zoom_ = SmallZoom;

void display::parse_team_overlays()
{
const team& curr_team = (*teams_)[playing_team()];
const team& prev_team = (*teams_)[playing_team()-1 < teams_->size() ? playing_team()-1 : teams_->size()-1];
const team& curr_team = dc_->teams()[playing_team()];
const team& prev_team = dc_->teams()[playing_team()-1 < dc_->teams().size() ? playing_team()-1 : dc_->teams().size()-1];
BOOST_FOREACH(const game_display::overlay_map::value_type i, *overlays_) {
const overlay& ov = i.second;
if (!ov.team_name.empty() &&
Expand Down Expand Up @@ -136,21 +136,19 @@ void display::remove_single_overlay(const map_location& loc, const std::string&



display::display(unit_map* units, CVideo& video, const gamemap* map, const std::vector<team>* t,const config& theme_cfg, const config& level) :
units_(units),
display::display(const display_context * dc, CVideo& video, const config& theme_cfg, const config& level) :
dc_(dc),
exclusive_unit_draw_requests_(),
screen_(video),
map_(map),
currentTeam_(0),
teams_(t),
viewpoint_(NULL),
energy_bar_rects_(),
xpos_(0),
ypos_(0),
view_locked_(false),
theme_(theme_cfg, screen_area()),
zoom_(DefaultZoom),
builder_(new terrain_builder(level, map, theme_.border().tile_image)),
builder_(new terrain_builder(level, &dc_->map(), theme_.border().tile_image)),
minimap_(NULL),
minimap_location_(sdl::empty_rect),
redrawMinimap_(false),
Expand Down Expand Up @@ -252,13 +250,13 @@ display::~display()
void display::init_flags() {

flags_.clear();
if (!teams_) return;
flags_.resize(teams_->size());
if (!dc_) return;
flags_.resize(dc_->teams().size());

std::vector<std::string> side_colors;
side_colors.reserve(teams_->size());
side_colors.reserve(dc_->teams().size());

for(size_t i = 0; i != teams_->size(); ++i) {
for(size_t i = 0; i != dc_->teams().size(); ++i) {
std::string side_color = team::get_side_color_index(i+1);
side_colors.push_back(side_color);
init_flags_for_side_internal(i, side_color);
Expand All @@ -268,7 +266,7 @@ void display::init_flags() {

void display::reinit_flags_for_side(size_t side)
{
if (!teams_ || side >= teams_->size()) {
if (!dc_ || side >= dc_->teams().size()) {
ERR_DP << "Cannot rebuild flags for inexistent or unconfigured side " << side << '\n';
return;
}
Expand All @@ -278,11 +276,11 @@ void display::reinit_flags_for_side(size_t side)

void display::init_flags_for_side_internal(size_t n, const std::string& side_color)
{
assert(teams_ != NULL);
assert(n < teams_->size());
assert(dc_ != NULL);
assert(n < dc_->teams().size());
assert(n < flags_.size());

std::string flag = (*teams_)[n].flag();
std::string flag = dc_->teams()[n].flag();
std::string old_rgb = game_config::flag_rgb;
std::string new_rgb = side_color;

Expand Down Expand Up @@ -336,9 +334,9 @@ surface display::get_flag(const map_location& loc)
return surface(NULL);
}

for(size_t i = 0; i != teams_->size(); ++i) {
if((*teams_)[i].owns_village(loc) &&
(!fogged(loc) || !(*teams_)[currentTeam_].is_enemy(i+1)))
for(size_t i = 0; i != dc_->teams().size(); ++i) {
if(dc_->teams()[i].owns_village(loc) &&
(!fogged(loc) || !dc_->teams()[currentTeam_].is_enemy(i+1)))
{
flags_[i].update_last_draw_time();
const image::locator &image_flag = animate_map_ ?
Expand All @@ -352,12 +350,12 @@ surface display::get_flag(const map_location& loc)

void display::set_team(size_t teamindex, bool show_everything)
{
assert(teamindex < teams_->size());
assert(teamindex < dc_->teams().size());
currentTeam_ = teamindex;
if (!show_everything)
{
labels().set_team(&(*teams_)[teamindex]);
viewpoint_ = &(*teams_)[teamindex];
labels().set_team(&dc_->teams()[teamindex]);
viewpoint_ = &dc_->teams()[teamindex];
}
else
{
Expand All @@ -371,7 +369,7 @@ void display::set_team(size_t teamindex, bool show_everything)

void display::set_playing_team(size_t teamindex)
{
assert(teamindex < teams_->size());
assert(teamindex < dc_->teams().size());
activeTeam_ = teamindex;
invalidate_game_status();
}
Expand Down Expand Up @@ -563,20 +561,10 @@ void display::reload_map()
builder_->reload_map();
}

void display::change_map(const gamemap* m)
void display::change_display_context(const display_context * dc)
{
map_ = m;
builder_->change_map(m);
}

void display::change_units(const unit_map* umap)
{
units_ = umap;
}

void display::change_teams(const std::vector<team>* teams)
{
teams_ = teams;
dc_ = dc;
builder_->change_map(&dc_->map()); //TODO: Should display_context own and initalize the builder object?
}

void display::blindfold(bool value)
Expand Down Expand Up @@ -1911,9 +1899,9 @@ void display::draw_minimap_units()
double xscaling = 1.0 * minimap_location_.w / get_map().w();
double yscaling = 1.0 * minimap_location_.h / get_map().h();

for(unit_map::const_iterator u = units_->begin(); u != units_->end(); ++u) {
for(unit_map::const_iterator u = dc_->units().begin(); u != dc_->units().end(); ++u) {
if (fogged(u->get_location()) ||
((*teams_)[currentTeam_].is_enemy(u->side()) &&
(dc_->teams()[currentTeam_].is_enemy(u->side()) &&
u->invisible(u->get_location())) ||
u->get_hidden()) {
continue;
Expand All @@ -1924,7 +1912,7 @@ void display::draw_minimap_units()

if (preferences::minimap_movement_coding()) {

if ((*teams_)[currentTeam_].is_enemy(side)) {
if (dc_->teams()[currentTeam_].is_enemy(side)) {
col = int_to_color(game_config::color_info(preferences::enemy_color()).rep());
} else {

Expand Down Expand Up @@ -2554,9 +2542,9 @@ void display::draw_invalidated() {
invalidated_hexes_ += invalidated_.size();

BOOST_FOREACH(const map_location& loc, invalidated_) {
unit_map::const_iterator u_it = units_->find(loc);
unit_map::const_iterator u_it = dc_->units().find(loc);
exclusive_unit_draw_requests_t::iterator request = exclusive_unit_draw_requests_.find(loc);
if (u_it != units_->end()
if (u_it != dc_->units().end()
&& (request == exclusive_unit_draw_requests_.end() || request->second == u_it->id()))
u_it->redraw_unit();
}
Expand Down Expand Up @@ -2596,7 +2584,7 @@ void display::draw_hex(const map_location& loc) {
std::pair<Itor,Itor> overlays = overlays_->equal_range(loc);
for( ; overlays.first != overlays.second; ++overlays.first) {
if ((overlays.first->second.team_name == "" ||
overlays.first->second.team_name.find((*teams_)[playing_team()].team_name()) != std::string::npos)
overlays.first->second.team_name.find(dc_->teams()[playing_team()].team_name()) != std::string::npos)
&& !(fogged(loc) && !overlays.first->second.visible_in_fog))
{
drawing_buffer_add(LAYER_TERRAIN_BG, loc, xpos, ypos,
Expand Down Expand Up @@ -3027,7 +3015,7 @@ void display::invalidate_animations_location(const map_location& loc) {
if (get_map().is_village(loc)) {
const int owner = village_owner(loc);
if (owner >= 0 && flags_[owner].need_update()
&& (!fogged(loc) || !(*teams_)[currentTeam_].is_enemy(owner+1))) {
&& (!fogged(loc) || !dc_->teams()[currentTeam_].is_enemy(owner+1))) {
invalidate(loc);
}
}
Expand All @@ -3036,7 +3024,7 @@ void display::invalidate_animations_location(const map_location& loc) {

std::vector<const unit*> display::get_unit_list_for_invalidation() {
std::vector<const unit*> unit_list;
BOOST_FOREACH(const unit &u, *units_) {
BOOST_FOREACH(const unit &u, dc_->units()) {
unit_list.push_back(&u);
}
return unit_list;
Expand Down
21 changes: 9 additions & 12 deletions src/display.hpp
Expand Up @@ -39,6 +39,7 @@ struct time_of_day;
class map_labels;
class arrow;

#include "display_context.hpp"
#include "font.hpp"
#include "key.hpp"
#include "team.hpp"
Expand All @@ -62,19 +63,21 @@ class gamemap;
class display
{
public:
display(unit_map* units, CVideo& video, const gamemap* map, const std::vector<team>* t,
display(const display_context * dc, CVideo& video,
const config& theme_cfg, const config& level);
virtual ~display();
static display* get_singleton() { return singleton_ ;}

bool show_everything() const { return !viewpoint_ && !is_blindfolded(); }

const std::vector<team>& get_teams() const {return *teams_;}
const gamemap& get_map() const { return dc_->map(); }

const std::vector<team>& get_teams() const {return dc_->teams();}

/** The playing team is the team whose turn it is. */
size_t playing_team() const { return activeTeam_; }

bool team_valid() const { return currentTeam_ < teams_->size(); }
bool team_valid() const { return currentTeam_ < dc_->teams().size(); }

/** The viewing team is the team currently viewing the game. */
size_t viewing_team() const { return currentTeam_; }
Expand All @@ -96,7 +99,7 @@ class display
* Cancels all the exclusive draw requests.
*/
void clear_exclusive_draws() { exclusive_unit_draw_requests_.clear(); }
const unit_map& get_units() const {return *units_;}
const unit_map& get_units() const {return dc_->units();}

/**
* Allows a unit to request to be the only one drawn in its hex. Useful for situations where
Expand Down Expand Up @@ -152,9 +155,7 @@ class display
*/
void reload_map();

void change_map(const gamemap* m);
void change_teams(const std::vector<team>* teams);
void change_units(const unit_map* units);
void change_display_context(const display_context * dc);

static Uint32 rgb(Uint8 red, Uint8 green, Uint8 blue)
{ return 0xFF000000 | (red << 16) | (green << 8) | blue; }
Expand Down Expand Up @@ -418,8 +419,6 @@ class display
*/
void invalidate_animations_location(const map_location& loc);

const gamemap& get_map() const { return *map_; }

/**
* mouseover_hex_overlay_ require a prerendered surface
* and is drawn underneath the mouse's location
Expand Down Expand Up @@ -627,7 +626,7 @@ class display

protected:
//TODO sort
const unit_map* units_;
const display_context * dc_;

typedef std::map<map_location, std::string> exclusive_unit_draw_requests_t;
/// map of hexes where only one unit should be drawn, the one identified by the associated id string
Expand Down Expand Up @@ -720,9 +719,7 @@ class display
const std::string& get_variant(const std::vector<std::string>& variants, const map_location &loc) const;

CVideo& screen_;
const gamemap* map_;
size_t currentTeam_;
const std::vector<team>* teams_;
const team *viewpoint_;
std::map<surface,SDL_Rect> energy_bar_rects_;
int xpos_, ypos_;
Expand Down
40 changes: 40 additions & 0 deletions src/display_context.hpp
@@ -0,0 +1,40 @@
/*
Copyright (C) 2014 by Chris Beck <render787@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

/**
*
* This class is an abstract base class designed to simplify the use
* of the display object.
*
**/

#ifndef DISPLAY_CONTEXT_HPP_INCLUDED
#define DISPLAY_CONTEXT_HPP_INCLUDED

#include<vector>

class team;
class gamemap;
class unit_map;

class display_context {
public:
virtual const std::vector<team> & teams() const = 0;
virtual const gamemap & map() const = 0;
virtual const unit_map & units() const = 0;

virtual ~display_context() {}
};

#endif
7 changes: 2 additions & 5 deletions src/editor/editor_controller.cpp
Expand Up @@ -56,12 +56,11 @@ static std::vector<std::string> saved_windows_;

namespace editor {


editor_controller::editor_controller(const config &game_config, CVideo& video)
: controller_base(SDL_GetTicks(), game_config, video)
, mouse_handler_base()
, active_menu_(editor::MAP)
, gui_(new editor_display(NULL, video, NULL, NULL, get_theme(game_config, "editor"), config()))
, gui_(new editor_display(editor::get_dummy_display_context(), video, get_theme(game_config, "editor"), config()))
, tods_()
, context_manager_(new context_manager(*gui_.get(), game_config_))
, toolkit_(NULL)
Expand Down Expand Up @@ -90,9 +89,7 @@ editor_controller::editor_controller(const config &game_config, CVideo& video)

void editor_controller::init_gui()
{
gui_->change_map(&context_manager_->get_map());
gui_->change_units(&context_manager_->get_map_context().get_units());
gui_->change_teams(&context_manager_->get_map_context().get_teams());
gui_->change_display_context(&context_manager_->get_map_context());
gui_->set_grid(preferences::grid());
prefs_disp_manager_.reset(new preferences::display_manager(&gui()));
gui_->add_redraw_observer(boost::bind(&editor_controller::display_redraw_callback, this, _1));
Expand Down
34 changes: 30 additions & 4 deletions src/editor/editor_display.cpp
Expand Up @@ -19,9 +19,35 @@

namespace editor {

editor_display::editor_display(unit_map* units, CVideo& video, const editor_map* map,
const std::vector<team>* t, const config& theme_cfg, const config& level)
: display(units, video, map, t, theme_cfg, level)
// Define dummy display context;

class dummy_editor_display_context : public display_context
{
config dummy_cfg1;

editor_map em;
unit_map u;
std::vector<team> t;

public:
dummy_editor_display_context() : dummy_cfg1(), em(dummy_cfg1), u(), t() {}
virtual ~dummy_editor_display_context(){}

virtual const gamemap & map() const { return em; }
virtual const unit_map & units() const { return u; }
virtual const std::vector<team> & teams() const { return t; }
};

const display_context * get_dummy_display_context() {
static const dummy_editor_display_context dedc = dummy_editor_display_context();
return &dedc;
}

// End dummy display context

editor_display::editor_display(const display_context * dc, CVideo& video,
const config& theme_cfg, const config& level)
: display(dc, video, theme_cfg, level)
, brush_locations_()
, palette_report_()
{
Expand Down Expand Up @@ -110,7 +136,7 @@ void editor_display::draw_sidebar()
refresh_report("position", &element);
}

if (teams_->empty()) {
if (dc_->teams().empty()) {
text = int(get_map().villages().size());
refresh_report("villages", &element);
} else {
Expand Down

0 comments on commit ae5436d

Please sign in to comment.