The Battle for Wesnoth  1.15.2+dev
fake_unit_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "fake_unit_manager.hpp"
16 
17 #include "display.hpp"
18 #include "fake_unit_ptr.hpp"
19 #include "log.hpp"
20 #include "units/unit.hpp"
22 
23 static lg::log_domain log_engine("engine");
24 #define ERR_NG LOG_STREAM(err, log_engine)
25 
26 /** Temporarily register a unit to be drawn on the map (moving: can overlap others).
27  * The temp unit is added at the end of the temporary unit dequeue,
28  * and therefore gets drawn last, over other units and temp units.
29  * Adding the same unit twice isn't allowed.
30  */
32 {
33  if(std::find(fake_units_.begin(),fake_units_.end(), u) != fake_units_.end()) {
34  ERR_NG << "In fake_unit_manager::place_temporary_unit: attempt to add duplicate fake unit." << std::endl;
35  } else {
36  fake_units_.push_back(u);
37  my_display_.invalidate(u->get_location());
38  }
39 }
40 
41 /** Removes any instances of this unit from the temporary unit database. */
43 {
44  int removed = 0;
45  if (fake_units_.empty())
46  return removed;
48  std::remove(fake_units_.begin(), fake_units_.end(), u);
49  if (it != fake_units_.end()) {
50  removed = std::distance(it, fake_units_.end());
51  //std::remove doesn't remove anything without using erase afterwards.
52  fake_units_.erase(it, fake_units_.end());
53  my_display_.invalidate(u->get_location());
54  // Redraw with no location to get rid of haloes
55  u->anim_comp().clear_haloes();
56  }
57  if (removed > 1) {
58  ERR_NG << "Error: duplicate temp unit found in fake_unit_manager::remove_temporary_unit" << std::endl;
59  }
60  return removed;
61 }
void remove()
Removes a tip.
Definition: tooltip.cpp:189
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:2998
static lg::log_domain log_engine("engine")
int remove_temporary_unit(internal_ptr_type)
Deregister a unit from this manager.
std::deque< internal_ptr_type > fake_units_
collection of units destined to be drawn but not put into the unit map
map_display and display: classes which take care of displaying the map and game-data on the screen...
unit const * internal_ptr_type
display & my_display_
Reference to my display.
void place_temporary_unit(internal_ptr_type)
Register a unit with this manager.
bool find(E event, F functor)
Tests whether an event handler is available.
Standard logging facilities (interface).
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
#define ERR_NG