Display: cleaned up overlay map getter interface

Instead of a pointer to a member of a derived class, used a virtual function implemented
in both derived classes (game_display and editor_display) that return an reference to the
appropriate object.

Also removed the overlay.hpp include from display.hpp. We can forward-declare the overlay
struct.

(cherry-picked from commit e583c470be)
This commit is contained in:
Charles Dang 2018-04-20 10:48:39 +11:00
parent 04ff36f6e8
commit 2af98f7dd8
7 changed files with 28 additions and 21 deletions

View file

@ -94,7 +94,7 @@ void display::parse_team_overlays()
const team& prev_team = playing_team() == 0
? dc_->teams().back()
: dc_->get_team(playing_team());
for (const game_display::overlay_map::value_type i : *overlays_) {
for (const game_display::overlay_map::value_type i : get_overlays()) {
const overlay& ov = i.second;
if (!ov.team_name.empty() &&
((ov.team_name.find(curr_team.team_name()) + 1) != 0) !=
@ -115,7 +115,7 @@ void display::add_overlay(const map_location& loc, const std::string& img, const
get_location_y(loc) + hex_size() / 2, halo, loc);
}
overlays_->emplace(loc, overlay(img, halo, halo_handle, team_name, item_id, visible_under_fog));
get_overlays().emplace(loc, overlay(img, halo, halo_handle, team_name, item_id, visible_under_fog));
}
}
@ -124,7 +124,7 @@ void display::remove_overlay(const map_location& loc)
/* This code no longer needed because of RAII in halo::handles
if (halo_man_) {
typedef overlay_map::const_iterator Itor;
std::pair<Itor,Itor> itors = overlays_->equal_range(loc);
std::pair<Itor,Itor> itors = get_overlays().equal_range(loc);
while(itors.first != itors.second) {
halo_man_->remove(itors.first->second.halo_handle);
++itors.first;
@ -132,7 +132,7 @@ void display::remove_overlay(const map_location& loc)
}
*/
overlays_->erase(loc);
get_overlays().erase(loc);
}
void display::remove_single_overlay(const map_location& loc, const std::string& toDelete)
@ -140,14 +140,14 @@ void display::remove_single_overlay(const map_location& loc, const std::string&
//Iterate through the values with key of loc
typedef overlay_map::iterator Itor;
overlay_map::iterator iteratorCopy;
std::pair<Itor,Itor> itors = overlays_->equal_range(loc);
std::pair<Itor,Itor> itors = get_overlays().equal_range(loc);
while(itors.first != itors.second) {
//If image or halo of overlay struct matches toDelete, remove the overlay
if(itors.first->second.image == toDelete || itors.first->second.halo == toDelete || itors.first->second.id == toDelete) {
iteratorCopy = itors.first;
++itors.first;
//Not needed because of RAII --> halo_man_->remove(iteratorCopy->second.halo_handle);
overlays_->erase(iteratorCopy);
get_overlays().erase(iteratorCopy);
}
else {
++itors.first;
@ -213,7 +213,6 @@ display::display(const display_context * dc, std::weak_ptr<wb::manager> wb, repo
reach_map_(),
reach_map_old_(),
reach_map_changed_(true),
overlays_(nullptr),
fps_handle_(0),
invalidated_hexes_(0),
drawn_hexes_(0),
@ -2591,7 +2590,7 @@ void display::draw_hex(const map_location& loc) {
if(!shrouded(loc)) {
typedef overlay_map::const_iterator Itor;
std::pair<Itor,Itor> overlays = overlays_->equal_range(loc);
std::pair<Itor,Itor> overlays = get_overlays().equal_range(loc);
const bool have_overlays = overlays.first != overlays.second;
if(have_overlays) {

View file

@ -39,6 +39,7 @@ class map_labels;
class arrow;
class reports;
class team;
struct overlay;
namespace halo {
class manager;
@ -61,8 +62,6 @@ namespace wb {
#include "video.hpp"
#include "widgets/button.hpp"
#include "overlay.hpp"
#include <boost/circular_buffer.hpp>
#include "utils/functional.hpp"
@ -1026,11 +1025,9 @@ protected:
typedef std::multimap<map_location, overlay> overlay_map;
virtual overlay_map& get_overlays() = 0;
private:
overlay_map* overlays_;
/** Handle for the label which displays frames per second. */
int fps_handle_;
/** Count work done for the debug info displayed under fps */
@ -1060,9 +1057,6 @@ private:
bool dirty_;
public:
void replace_overlay_map(overlay_map* overlays) { overlays_ = overlays; }
protected:
static display * singleton_;
};

View file

@ -16,6 +16,7 @@
#include "editor/controller/editor_controller.hpp"
#include "editor/editor_display.hpp"
#include "lexical_cast.hpp"
#include "overlay.hpp"
#include "reports.hpp"
#include "team.hpp"
#include "terrain/builder.hpp"
@ -127,4 +128,9 @@ const time_of_day& editor_display::get_time_of_day(const map_location& /*loc*/)
return controller_.get_current_map_context().get_time_manager()->get_time_of_day();
}
display::overlay_map& editor_display::get_overlays()
{
return controller_.get_current_map_context().get_overlays();
}
} //end namespace editor

View file

@ -50,6 +50,9 @@ protected:
void draw_hex(const map_location& loc) override;
/** Inherited from display. */
virtual overlay_map& get_overlays() override;
const SDL_Rect& get_clip_rect() override;
void draw_sidebar() override;

View file

@ -99,9 +99,6 @@ void context_manager::refresh_on_context_change()
// TODO register the tod_manager with the gui?
resources::tod_manager = get_map_context().get_time_manager();
gui().replace_overlay_map(&get_map_context().get_overlays());
resources::classification = &get_map_context().get_classification();
gui().init_flags();

View file

@ -43,6 +43,7 @@
#include "units/unit.hpp"
#include "units/drawer.hpp"
#include "whiteboard/manager.hpp"
#include "overlay.hpp"
static lg::log_domain log_display("display");
#define ERR_DP LOG_STREAM(err, log_display)
@ -79,7 +80,6 @@ game_display::game_display(game_board& board, std::weak_ptr<wb::manager> wb,
mode_(RUNNING),
needs_rebuild_(false)
{
replace_overlay_map(&overlay_map_);
video().clear_screen();
}
@ -671,3 +671,8 @@ bool game_display::maybe_rebuild() {
}
return false;
}
display::overlay_map& game_display::get_overlays()
{
return overlay_map_;
}

View file

@ -142,6 +142,9 @@ protected:
virtual void draw_hex(const map_location& loc) override;
/** Inherited from display. */
virtual overlay_map& get_overlays() override;
public:
/** Set the attack direction indicator. */
void set_attack_indicator(const map_location& src, const map_location& dst);