The Battle for Wesnoth  1.17.14+dev
display.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2022
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  *
19  * map_display and display: classes which take care of
20  * displaying the map and game-data on the screen.
21  *
22  * The display is divided into two main sections:
23  * - the game area, which displays the tiles of the game board, and units on them,
24  * - and the side bar, which appears on the right hand side.
25  * The side bar display is divided into three sections:
26  * - the minimap, which is displayed at the top right
27  * - the game status, which includes the day/night image,
28  * the turn number, information about the current side,
29  * and information about the hex currently moused over (highlighted)
30  * - the unit status, which displays an image and stats
31  * for the current unit.
32  */
33 
34 #pragma once
35 
36 class config;
37 class fake_unit_manager;
38 class terrain_builder;
39 class map_labels;
40 class arrow;
41 class reports;
42 class team;
43 struct overlay;
44 
45 namespace halo {
46  class manager;
47 }
48 
49 namespace wb {
50  class manager;
51 }
52 
53 #include "animated.hpp"
54 #include "display_context.hpp"
55 #include "filesystem.hpp"
56 #include "font/standard_colors.hpp"
57 #include "game_config.hpp"
59 #include "halo.hpp"
60 #include "picture.hpp" //only needed for enums (!)
61 #include "key.hpp"
62 #include "time_of_day.hpp"
63 #include "sdl/rect.hpp"
64 #include "sdl/surface.hpp"
65 #include "sdl/texture.hpp"
66 #include "theme.hpp"
67 #include "widgets/button.hpp"
68 
69 #include <boost/circular_buffer.hpp>
70 
71 #include <bitset>
72 #include <functional>
73 #include <chrono>
74 #include <cstdint>
75 #include <deque>
76 #include <list>
77 #include <map>
78 #include <memory>
79 #include <vector>
80 
81 class gamemap;
82 
84 {
85 public:
86  display(const display_context* dc,
87  std::weak_ptr<wb::manager> wb,
88  reports& reports_object,
89  const std::string& theme_id,
90  const config& level);
91 
92  virtual ~display();
93  /**
94  * Returns the display object if a display object exists. Otherwise it returns nullptr.
95  * the display object represents the game gui which handles themewml and drawing the map.
96  * A display object only exists during a game or while the mapeditor is running.
97  */
98  static display* get_singleton() { return singleton_ ;}
99 
100  bool show_everything() const { return !dont_show_all_ && !is_blindfolded(); }
101 
102  const gamemap& get_map() const { return dc_->map(); }
103 
104  const std::vector<team>& get_teams() const {return dc_->teams();}
105 
106  /** The playing team is the team whose turn it is. */
107  std::size_t playing_team() const { return activeTeam_; }
108 
109  bool team_valid() const;
110 
111  /** The viewing team is the team currently viewing the game. */
112  std::size_t viewing_team() const { return currentTeam_; }
113  int viewing_side() const { return currentTeam_ + 1; }
114 
115  /**
116  * Sets the team controlled by the player using the computer.
117  * Data from this team will be displayed in the game status.
118  */
119  void set_team(std::size_t team, bool observe=false);
120 
121  /**
122  * set_playing_team sets the team whose turn it currently is
123  */
124  void set_playing_team(std::size_t team);
125 
126 
127  /**
128  * Cancels all the exclusive draw requests.
129  */
130  void clear_exclusive_draws() { exclusive_unit_draw_requests_.clear(); }
131  const unit_map& get_units() const {return dc_->units();}
132 
133  /**
134  * Allows a unit to request to be the only one drawn in its hex. Useful for situations where
135  * multiple units (one real, multiple temporary) can end up stacked, such as with the whiteboard.
136  * @param loc The location of the unit requesting exclusivity.
137  * @param unit The unit requesting exclusivity.
138  * @return false if there's already an exclusive draw request for this location.
139  */
140  bool add_exclusive_draw(const map_location& loc, unit& unit);
141  /**
142  * Cancels an exclusive draw request.
143  * @return The id of the unit whose exclusive draw request was canceled, or else
144  * the empty string if there was no exclusive draw request for this location.
145  */
146  std::string remove_exclusive_draw(const map_location& loc);
147 
148  /**
149  * Functions to add and remove overlays from locations.
150  *
151  * An overlay is an image that is displayed on top of the tile.
152  * One tile may have multiple overlays.
153  */
154  void add_overlay(const map_location& loc, const std::string& image,
155  const std::string& halo="", const std::string& team_name="",const std::string& item_id="",
156  bool visible_under_fog = true, float submerge = 0.0f, float z_order = 0);
157 
158  /** remove_overlay will remove all overlays on a tile. */
159  void remove_overlay(const map_location& loc);
160 
161  /** remove_single_overlay will remove a single overlay from a tile */
162  void remove_single_overlay(const map_location& loc, const std::string& toDelete);
163 
164  /**
165  * Updates internals that cache map size. This should be called when the map
166  * size has changed.
167  */
168  void reload_map();
169 
170  void change_display_context(const display_context* dc);
171 
173  {
174  return *dc_;
175  }
176 
177  halo::manager& get_halo_manager() { return halo_man_; }
178 
179  /**
180  * Applies r,g,b coloring to the map.
181  *
182  * The color is usually taken from @ref get_time_of_day unless @a tod_override is given, in which
183  * case that color is used.
184  *
185  * @param tod_override The ToD to apply to the map instead of that of the current ToD's.
186  */
187  void update_tod(const time_of_day* tod_override = nullptr);
188 
189  /**
190  * Add r,g,b to the colors for all images displayed on the map.
191  *
192  * Used for special effects like flashes.
193  */
194  void adjust_color_overlay(int r, int g, int b);
195  tod_color get_color_overlay() const { return color_adjust_; }
196 
197  virtual bool in_game() const { return false; }
198  virtual bool in_editor() const { return false; }
199 
200  /** Virtual functions shadowed in game_display. These are needed to generate reports easily, without dynamic casting. Hope to factor out eventually. */
201  virtual const map_location & displayed_unit_hex() const { return map_location::null_location(); }
202  virtual int playing_side() const { return -100; } //In this case give an obviously wrong answer to fail fast, since this could actually cause a big bug. */
203  virtual const std::set<std::string>& observers() const { static const std::set<std::string> fake_obs = std::set<std::string> (); return fake_obs; }
204 
205  /**
206  * mapx is the width of the portion of the display which shows the game area.
207  * Between mapx and x is the sidebar region.
208  */
209 
210  const rect& minimap_area() const;
211  const rect& palette_area() const;
212  const rect& unit_image_area() const;
213 
214  /**
215  * Returns the maximum area used for the map
216  * regardless to resolution and view size
217  */
218  rect max_map_area() const;
219 
220  /**
221  * Returns the area used for the map
222  */
223  rect map_area() const;
224 
225  /**
226  * Returns the available area for a map, this may differ
227  * from the above. This area will get the background area
228  * applied to it.
229  */
230  rect map_outside_area() const;
231 
232  /** Check if the bbox of the hex at x,y has pixels outside the area rectangle. */
233  static bool outside_area(const SDL_Rect& area, const int x,const int y);
234 
235  /**
236  * Function which returns the width of a hex in pixels,
237  * up to where the next hex starts.
238  * (i.e. not entirely from tip to tip -- use hex_size()
239  * to get the distance from tip to tip)
240  */
241  static int hex_width() { return (zoom_*3)/4; }
242 
243  /**
244  * Function which returns the size of a hex in pixels
245  * (from top tip to bottom tip or left edge to right edge).
246  */
247  static int hex_size(){ return zoom_; }
248 
249  /** Returns the current zoom factor. */
250  static double get_zoom_factor()
251  {
252  return static_cast<double>(zoom_) / static_cast<double>(game_config::tile_size);
253  }
254 
255  /** Scale the width and height of a rect by the current zoom factor */
256  static rect scaled_to_zoom(const SDL_Rect& r)
257  {
258  const double zf = get_zoom_factor();
259  return {r.x, r.y, int(r.w * zf), int(r.h * zf)};
260  }
261 
262  static point scaled_to_zoom(const point& p)
263  {
264  const double zf = get_zoom_factor();
265  return {int(p.x * zf), int(p.y * zf)};
266  }
267 
268  /**
269  * given x,y co-ordinates of an onscreen pixel, will return the
270  * location of the hex that this pixel corresponds to.
271  * Returns an invalid location if the mouse isn't over any valid location.
272  */
273  const map_location hex_clicked_on(int x, int y) const;
274 
275  /**
276  * given x,y co-ordinates of a pixel on the map, will return the
277  * location of the hex that this pixel corresponds to.
278  * Returns an invalid location if the mouse isn't over any valid location.
279  */
280  const map_location pixel_position_to_hex(int x, int y) const;
281 
282  /**
283  * given x,y co-ordinates of the mouse, will return the location of the
284  * hex in the minimap that the mouse is currently over, or an invalid
285  * location if the mouse isn't over the minimap.
286  */
287  map_location minimap_location_on(int x, int y);
288 
289  const map_location& selected_hex() const { return selectedHex_; }
290  const map_location& mouseover_hex() const { return mouseoverHex_; }
291 
292  virtual void select_hex(map_location hex);
293  virtual void highlight_hex(map_location hex);
294 
295  /** Function to invalidate the game status displayed on the sidebar. */
296  void invalidate_game_status() { invalidateGameStatus_ = true; }
297 
298  /** Functions to get the on-screen positions of hexes. */
299  int get_location_x(const map_location& loc) const;
300  int get_location_y(const map_location& loc) const;
301  point get_location(const map_location& loc) const;
302 
303  /**
304  * Rectangular area of hexes, allowing to decide how the top and bottom
305  * edges handles the vertical shift for each parity of the x coordinate
306  */
308  int left;
309  int right;
310  int top[2]; // for even and odd values of x, respectively
311  int bottom[2];
312 
313  /** very simple iterator to walk into the rect_of_hexes */
314  struct iterator {
316  : loc_(loc), rect_(rect){}
317 
318  /** increment y first, then when reaching bottom, increment x */
319  iterator& operator++();
320  bool operator==(const iterator &that) const { return that.loc_ == loc_; }
321  bool operator!=(const iterator &that) const { return that.loc_ != loc_; }
322  const map_location& operator*() const {return loc_;}
323 
324  typedef std::forward_iterator_tag iterator_category;
326  typedef int difference_type;
327  typedef const map_location *pointer;
328  typedef const map_location &reference;
329 
330  private:
333  };
335 
336  iterator begin() const;
337  iterator end() const;
338  };
339 
340  /** Return the rectangular area of hexes overlapped by r (r is in screen coordinates) */
341  const rect_of_hexes hexes_under_rect(const SDL_Rect& r) const;
342 
343  /** Returns the rectangular area of visible hexes */
344  const rect_of_hexes get_visible_hexes() const {return hexes_under_rect(map_area());}
345 
346  /** Returns true if location (x,y) is covered in shroud. */
347  bool shrouded(const map_location& loc) const;
348 
349  /** Returns true if location (x,y) is covered in fog. */
350  bool fogged(const map_location& loc) const;
351 
352  /** Capture a (map-)screenshot into a surface. */
353  surface screenshot(bool map_screenshot = false);
354 
355  /** Marks everything for rendering including all tiles and sidebar.
356  * Also calls redraw observers. */
357  void queue_rerender();
358 
359  /** Queues repainting to the screen, but doesn't rerender. */
360  void queue_repaint();
361 
362  /** Adds a redraw observer, a function object to be called when a
363  * full rerender is queued. */
364  void add_redraw_observer(std::function<void(display&)> f);
365 
366  /** Clear the redraw observers */
367  void clear_redraw_observers();
368 
369  theme& get_theme() { return theme_; }
370  void set_theme(const std::string& new_theme);
371 
372  /**
373  * Retrieves a pointer to a theme UI button.
374  *
375  * @note The returned pointer may either be nullptr, meaning the button
376  * isn't defined by the current theme, or point to a valid
377  * gui::button object. However, the objects retrieved will be
378  * destroyed and recreated by draw() method calls. Do *NOT* store
379  * these pointers for longer than strictly necessary to
380  * accomplish a specific task before the next screen refresh.
381  */
382  std::shared_ptr<gui::button> find_action_button(const std::string& id);
383  std::shared_ptr<gui::button> find_menu_button(const std::string& id);
384 
385  void create_buttons();
386 
387  void layout_buttons();
388 
389  void draw_buttons();
390 
391  /** Update the given report. Actual drawing is done in draw_report(). */
392  void refresh_report(const std::string& report_name, const config * new_cfg=nullptr);
393 
394  /**
395  * Draw the specified report.
396  *
397  * If test_run is true, it will simulate the draw without actually
398  * drawing anything. This will add any overflowing information to the
399  * report tooltip, and also registers the tooltip.
400  */
401  void draw_report(const std::string& report_name, bool test_run = false);
402 
403  /** Draw all reports in the given region.
404  * Returns true if something was drawn, false otherwise. */
405  bool draw_reports(const rect& region);
406 
407  void draw_minimap_units();
408 
409  /** Function to invalidate all tiles. */
410  void invalidate_all();
411 
412  /** Function to invalidate a specific tile for redrawing. */
413  bool invalidate(const map_location& loc);
414 
415  bool invalidate(const std::set<map_location>& locs);
416 
417  /**
418  * If this set is partially invalidated, invalidate all its hexes.
419  * Returns if any new invalidation was needed
420  */
421  bool propagate_invalidation(const std::set<map_location>& locs);
422 
423  /** invalidate all hexes under the rectangle rect (in screen coordinates) */
424  bool invalidate_locations_in_rect(const SDL_Rect& rect);
425  bool invalidate_visible_locations_in_rect(const SDL_Rect& rect);
426 
427  /**
428  * Function to invalidate animated terrains and units which may have changed.
429  */
430  void invalidate_animations();
431 
432  /**
433  * Per-location invalidation called by invalidate_animations()
434  * Extra game per-location invalidation (village ownership)
435  */
436  void invalidate_animations_location(const map_location& loc);
437 
438  void reset_standing_animations();
439 
440  terrain_builder& get_builder() {return *builder_;}
441 
442  void update_fps_label();
443  void clear_fps_label();
444  void update_fps_count();
445 
446  /** Rebuild all dynamic terrain. */
447  void rebuild_all();
448 
449  const theme::action* action_pressed();
450  const theme::menu* menu_pressed();
451 
452  void set_diagnostic(const std::string& msg);
453 
454  double turbo_speed() const;
455 
456  void bounds_check_position();
457  void bounds_check_position(int& xpos, int& ypos) const;
458 
459  /**
460  * Scrolls the display by xmov,ymov pixels.
461  * Invalidation and redrawing will be scheduled.
462  * @return true if the map actually moved.
463  */
464  bool scroll(int xmov, int ymov, bool force = false);
465 
466  /** Zooms the display in (true) or out (false). */
467  bool set_zoom(bool increase);
468 
469  /** Sets the display zoom to the specified amount. */
470  bool set_zoom(unsigned int amount, const bool validate_value_and_set_index = true);
471 
472  static bool zoom_at_max();
473  static bool zoom_at_min();
474 
475  /** Sets the zoom amount to the default. */
476  void toggle_default_zoom();
477 
478  bool view_locked() const { return view_locked_; }
479 
480  /** Sets whether the map view is locked (e.g. so the user can't scroll away) */
481  void set_view_locked(bool value) { view_locked_ = value; }
482 
483  enum SCROLL_TYPE { SCROLL, WARP, ONSCREEN, ONSCREEN_WARP };
484 
485  /**
486  * Scroll such that location loc is on-screen.
487  * WARP jumps to loc; SCROLL uses scroll speed;
488  * ONSCREEN only scrolls if x,y is offscreen
489  * force : scroll even if preferences tell us not to,
490  * or the view is locked.
491  */
492  void scroll_to_tile(const map_location& loc, SCROLL_TYPE scroll_type=ONSCREEN, bool check_fogged=true,bool force = true);
493 
494  /**
495  * Scroll such that location loc1 is on-screen.
496  * It will also try to make it such that loc2 is on-screen,
497  * but this is not guaranteed. For ONSCREEN scrolls add_spacing
498  * sets the desired minimum distance from the border in hexes.
499  */
500  void scroll_to_tiles(map_location loc1, map_location loc2,
501  SCROLL_TYPE scroll_type=ONSCREEN, bool check_fogged=true,
502  double add_spacing=0.0, bool force=true);
503 
504  /** Scroll to fit as many locations on-screen as possible, starting with the first. */
505  void scroll_to_tiles(const std::vector<map_location>::const_iterator & begin,
506  const std::vector<map_location>::const_iterator & end,
507  SCROLL_TYPE scroll_type=ONSCREEN, bool check_fogged=true,
508  bool only_if_possible=false, double add_spacing=0.0,
509  bool force=true);
510  /** Scroll to fit as many locations on-screen as possible, starting with the first. */
511  void scroll_to_tiles(const std::vector<map_location>& locs,
512  SCROLL_TYPE scroll_type=ONSCREEN, bool check_fogged=true,
513  bool only_if_possible=false,
514  double add_spacing=0.0, bool force=true)
515  {
516  scroll_to_tiles(locs.begin(), locs.end(), scroll_type, check_fogged,
517  only_if_possible, add_spacing, force);
518  }
519 
520  /** Expose the event, so observers can be notified about map scrolling. */
521  events::generic_event &scroll_event() const { return scroll_event_; }
522 
523  /** Check if a tile is fully visible on screen. */
524  bool tile_fully_on_screen(const map_location& loc) const;
525 
526  /** Checks if location @a loc or one of the adjacent tiles is visible on screen. */
527  bool tile_nearly_on_screen(const map_location &loc) const;
528 
529  /** Prevent the game display from drawing.
530  * Used while story screen is showing to prevent flicker. */
531  void set_prevent_draw(bool pd) { prevent_draw_ = pd; }
532  bool get_prevent_draw() { return prevent_draw_; }
533 
534 private:
535  bool prevent_draw_ = false;
536 
537 public:
538  /** ToD mask smooth fade */
539  void fade_tod_mask(const std::string& old, const std::string& new_);
540 
541  /** Screen fade */
542  void fade_to(const color_t& color, int duration);
543  void set_fade(const color_t& color);
544 
545 private:
546  color_t fade_color_ = {0,0,0,0};
547 
548 public:
549  /*-------------------------------------------------------*/
550  /* top_level_drawable interface (called by draw_manager) */
551  /*-------------------------------------------------------*/
552 
553  /** Update animations and internal state */
554  virtual void update() override;
555 
556  /** Finalize screen layout. */
557  virtual void layout() override;
558 
559  /** Update offscreen render buffers. */
560  virtual void render() override;
561 
562  /** Paint the indicated region to the screen. */
563  virtual bool expose(const rect& region) override;
564 
565  /** Return the current draw location of the display, on the screen. */
566  virtual rect screen_location() override;
567 
568 private:
569  /** Render textures, for intermediate rendering. */
570  texture front_ = {};
571  texture back_ = {};
572 
573  /** Ensure render textures are valid and correct. */
574  void update_render_textures();
575 
576  /** Draw/redraw the off-map background area.
577  * This updates both render textures. */
578  void render_map_outside_area();
579 
580  /** Perform rendering of invalidated items. */
581  void draw();
582 
583 public:
584  map_labels& labels();
585  const map_labels& labels() const;
586 
587  /** Holds options for calls to function 'announce' (@ref announce). */
589  {
590  /** Lifetime measured in milliseconds. */
591  int lifetime;
592 
593  /**
594  * An announcement according these options should replace the
595  * previous announce (typical of fast announcing) or not
596  * (typical of movement feedback).
597  */
599 
601  : lifetime(1600)
602  , discard_previous(false)
603  {
604  }
605  };
606 
607  /** Announce a message prominently. */
608  void announce(const std::string& msg,
609  const color_t& color = font::GOOD_COLOR,
611 
612  /**
613  * Schedule the minimap for recalculation.
614  * Useful if any terrain in the map has changed.
615  */
616  void recalculate_minimap();
617 
618  /**
619  * Schedule the minimap to be redrawn.
620  * Useful if units have moved about on the map.
621  */
622  void redraw_minimap();
623 
624 private:
625  /** Actually draw the minimap. */
626  void draw_minimap();
627 
628 public:
629 
630  virtual const time_of_day& get_time_of_day(const map_location& loc = map_location::null_location()) const;
631 
632  virtual bool has_time_area() const {return false;}
633 
634  void blindfold(bool flag);
635  bool is_blindfolded() const;
636 
637  void write(config& cfg) const;
638 
639 private:
640  void read(const config& cfg);
641 
642 public:
643  /** Init the flag list and the team colors used by ~TC */
644  void init_flags();
645 
646  /** Rebuild the flag list (not team colors) for a single side. */
647  void reinit_flags_for_team(const team&);
648  void reset_reports(reports& reports_object)
649  {
650  reports_object_ = &reports_object;
651  }
652 
653 private:
654  void init_flags_for_side_internal(std::size_t side, const std::string& side_color);
655 
657 
658 protected:
659  //TODO sort
662  std::weak_ptr<wb::manager> wb_;
663 
664  typedef std::map<map_location, std::string> exclusive_unit_draw_requests_t;
665  /** map of hexes where only one unit should be drawn, the one identified by the associated id string */
666  exclusive_unit_draw_requests_t exclusive_unit_draw_requests_;
667 
668  map_location get_middle_location() const;
669 
670  /**
671  * Get the clipping rectangle for drawing.
672  * Virtual since the editor might use a slightly different approach.
673  */
674  virtual rect get_clip_rect() const;
675 
676  /**
677  * Only called when there's actual redrawing to do. Loops through
678  * invalidated locations and redraws them. Derived classes can override
679  * this, possibly to insert pre- or post-processing around a call to the
680  * base class's function.
681  */
682  virtual void draw_invalidated();
683 
684  /**
685  * Redraws a single gamemap location.
686  */
687  virtual void draw_hex(const map_location& loc);
688 
689  enum TERRAIN_TYPE { BACKGROUND, FOREGROUND};
690 
691  void get_terrain_images(const map_location &loc,
692  const std::string& timeid,
694 
695  std::vector<texture> get_fog_shroud_images(const map_location& loc, image::TYPE image_type);
696 
697  void scroll_to_xy(int screenxpos, int screenypos, SCROLL_TYPE scroll_type,bool force = true);
698 
699  static void fill_images_list(const std::string& prefix, std::vector<std::string>& images);
700 
701  static const std::string& get_variant(const std::vector<std::string>& variants, const map_location &loc);
702 
703  std::size_t currentTeam_;
704  bool dont_show_all_; //const team *viewpoint_;
705  /**
706  * Position of the top-left corner of the viewport, in pixels.
707  *
708  * Dependent on zoom_.. For example, ypos_==72 only means we're one
709  * hex below the top of the map when zoom_ == 72 (the default value).
710  */
711  int xpos_, ypos_;
714  /**
715  * The current zoom, in pixels (on screen) per 72 pixels (in the
716  * graphic assets), i.e., 72 means 100%.
717  */
718  static unsigned int zoom_;
720  /** The previous value of zoom_. */
721  static unsigned int last_zoom_;
722  const std::unique_ptr<fake_unit_manager> fake_unit_man_;
723  const std::unique_ptr<terrain_builder> builder_;
730  const std::unique_ptr<map_labels> map_labels_;
732 
733  /** Event raised when the map is being scrolled */
735 
736  boost::circular_buffer<unsigned> frametimes_; // in milliseconds
737  int current_frame_sample_ = 0;
738  unsigned int fps_counter_;
739  std::chrono::seconds fps_start_;
740  unsigned int fps_actual_;
741  uint32_t last_frame_finished_ = 0u;
742 
743  // Not set by the initializer:
744  std::map<std::string, rect> reportLocations_;
745  std::map<std::string, texture> reportSurfaces_;
746  std::map<std::string, config> reports_;
747  std::vector<std::shared_ptr<gui::button>> menu_buttons_, action_buttons_;
748  std::set<map_location> invalidated_;
749  // If we're transitioning from one time of day to the next,
750  // then we will use these two masks on top of all hexes when we blit.
751  texture tod_hex_mask1 = {};
752  texture tod_hex_mask2 = {};
753  uint8_t tod_hex_alpha1 = 0;
754  uint8_t tod_hex_alpha2 = 0;
755  std::vector<std::string> fog_images_;
756  std::vector<std::string> shroud_images_;
757 
761 
762  /** Local cache for preferences::animate_map, since it is constantly queried. */
764 
765  /** Local version of preferences::animate_water, used to detect when it's changed. */
767 
768 private:
769 
770  texture get_flag(const map_location& loc);
771 
772  /** Animated flags for each team */
773  std::vector<animated<image::locator>> flags_;
774 
775  // This vector is a class member to avoid repeated memory allocations in get_terrain_images(),
776  // which turned out to be a significant bottleneck while profiling.
777  std::vector<texture> terrain_image_vector_;
778 
779 public:
780  /**
781  * The layers to render something on. This value should never be stored
782  * it's the internal drawing order and adding removing and reordering
783  * the layers should be safe.
784  * If needed in WML use the name and map that to the enum value.
785  */
787  LAYER_TERRAIN_BG, /**<
788  * Layer for the terrain drawn behind the
789  * unit.
790  */
791  LAYER_GRID_TOP, /**< Top half part of grid image */
792  LAYER_MOUSEOVER_OVERLAY, /**< Mouseover overlay used by editor*/
793  LAYER_FOOTSTEPS, /**< Footsteps showing path from unit to mouse */
794  LAYER_MOUSEOVER_TOP, /**< Top half of image following the mouse */
795  LAYER_UNIT_FIRST, /**< Reserve layers to be selected for WML. */
796  LAYER_UNIT_BG = LAYER_UNIT_FIRST+10, /**< Used for the ellipse behind the unit. */
797  LAYER_UNIT_DEFAULT=LAYER_UNIT_FIRST+40,/**<default layer for drawing units */
798  LAYER_TERRAIN_FG = LAYER_UNIT_FIRST+50, /**<
799  * Layer for the terrain drawn in front of
800  * the unit.
801  */
802  LAYER_GRID_BOTTOM, /**<
803  * Used for the bottom half part of grid image.
804  * Should be under moving units, to avoid masking south move.
805  */
806  LAYER_UNIT_MOVE_DEFAULT=LAYER_UNIT_FIRST+60/**<default layer for drawing moving units */,
807  LAYER_UNIT_FG = LAYER_UNIT_FIRST+80, /**<
808  * Used for the ellipse in front of the
809  * unit.
810  */
811  LAYER_UNIT_MISSILE_DEFAULT = LAYER_UNIT_FIRST+90, /**< default layer for missile frames*/
812  LAYER_UNIT_LAST=LAYER_UNIT_FIRST+100,
813  LAYER_REACHMAP, /**< "black stripes" on unreachable hexes. */
814  LAYER_MOUSEOVER_BOTTOM, /**< Bottom half of image following the mouse */
815  LAYER_FOG_SHROUD, /**< Fog and shroud. */
816  LAYER_ARROWS, /**< Arrows from the arrows framework. Used for planned moves display. */
817  LAYER_ACTIONS_NUMBERING, /**< Move numbering for the whiteboard. */
818  LAYER_SELECTED_HEX, /**< Image on the selected unit */
819  LAYER_ATTACK_INDICATOR, /**< Layer which holds the attack indicator. */
820  LAYER_UNIT_BAR, /**<
821  * Unit bars and overlays are drawn on this
822  * layer (for testing here).
823  */
824  LAYER_MOVE_INFO, /**< Movement info (defense%, etc...). */
825  LAYER_LINGER_OVERLAY, /**< The overlay used for the linger mode. */
826  LAYER_BORDER, /**< The border of the map. */
827  };
828 
829  static void add_submerge_ipf_mod(std::string& image_path, int image_height, double submersion_amount, int shift = 0);
830 
831  /**
832  * Draw text on a hex. (0.5, 0.5) is the center.
833  * The font size is adjusted to the zoom factor.
834  */
835  void draw_text_in_hex(const map_location& loc,
836  const drawing_layer layer, const std::string& text, std::size_t font_size,
837  color_t color, double x_in_hex=0.5, double y_in_hex=0.5);
838 
839 protected:
840 
841  //TODO sort
842  std::size_t activeTeam_;
843 
844  /**
845  * Helper for rendering the map by ordering draw operations.
846  *
847  * In order to render a hex properly, they need to be rendered per row.
848  * In this row several layers need to be drawn at the same time, mainly
849  * the unit and the background terrain. This is needed since both can spill
850  * into the next hex. The foreground terrain needs to be drawn before to
851  * avoid decapitating a unit.
852  *
853  * In other words:
854  * for every layer
855  * for every row (starting from the top)
856  * for every hex in the row
857  * ...
858  *
859  * this is modified to:
860  * for every layer group
861  * for every row (starting from the top)
862  * for every layer in the group
863  * for every hex in the row
864  * ...
865  */
866  struct draw_helper
867  {
868  /** Controls the ordering of draw calls by layer and location. */
869  const uint32_t key;
870 
871  /** Handles the actual drawing at this location. */
872  std::function<void(const rect&)> do_draw;
873 
874  /** The screen coordinates for the specified hex. This is passed to @ref do_draw */
875  rect dest;
876 
877  bool operator<(const draw_helper& rhs) const
878  {
879  return key < rhs.key;
880  }
881  };
882 
883  std::list<draw_helper> drawing_buffer_;
884 
885 public:
886  /**
887  * Add an item to the drawing buffer.
888  *
889  * @param layer The layer to draw on.
890  * @param loc The hex the image belongs to, needed for the drawing order.
891  * @param draw_func The draw operation to be run.
892  */
893  void drawing_buffer_add(const drawing_layer layer, const map_location& loc, decltype(draw_helper::do_draw) draw_func);
894 
895 protected:
896 
897  /** Draws the drawing_buffer_ and clears it. */
898  void drawing_buffer_commit();
899 
900  /** Redraws all panels intersecting the given region.
901  * Returns true if something was drawn, false otherwise. */
902  bool draw_all_panels(const rect& region);
903 
904 private:
905  void draw_panel(const theme::panel& panel);
906  void draw_label(const theme::label& label);
907 
908 protected:
909 
910  /**
911  * Initiate a redraw.
912  *
913  * Invalidate controls and panels when changed after they have been drawn
914  * initially. Useful for dynamic theme modification.
915  */
916  void draw_init();
917  void draw_wrap(bool update,bool force);
918 
919  /** Used to indicate to drawing functions that we are doing a map screenshot */
921 
922 public: //operations for the arrow framework
923 
924  void add_arrow(arrow&);
925 
926  void remove_arrow(arrow&);
927 
928  /** Called by arrow objects when they change. You should not need to call this directly. */
929  void update_arrow(arrow & a);
930 
931 protected:
932 
933  // Tiles lit for showing where unit(s) can reach
934  typedef std::map<map_location,unsigned int> reach_map;
935  reach_map reach_map_;
936  reach_map reach_map_old_;
938  void process_reachmap_changes();
939 
940  typedef std::map<map_location, std::vector<overlay>> overlay_map;
941 
942  virtual overlay_map& get_overlays() = 0;
943 
944 private:
945  /** Handle for the label which displays frames per second. */
947  /** Count work done for the debug info displayed under fps */
950 
951  std::vector<std::function<void(display&)>> redraw_observers_;
952 
953 public:
954  enum DEBUG_FLAG {
955  /** Overlays x,y coords on tiles */
957 
958  /** Overlays terrain codes on tiles */
960 
961  /** Overlays number of bitmaps on tiles */
963 
964  /** Separates background and foreground terrain layers. */
966 
967  /** Toggle to continuously redraw the whole map. */
969 
970  /** Dummy entry to size the bitmask. Keep this last! */
971  __NUM_DEBUG_FLAGS
972  };
973 
974  bool debug_flag_set(DEBUG_FLAG flag) const
975  {
976  return debug_flags_.test(flag);
977  }
978 
979  void set_debug_flag(DEBUG_FLAG flag, bool value)
980  {
981  debug_flags_.set(flag, value);
982  }
983 
985  {
986  debug_flags_.flip(flag);
987  }
988 
989 private:
990  /** Currently set debug flags. */
991  std::bitset<__NUM_DEBUG_FLAGS> debug_flags_;
992 
993  typedef std::list<arrow*> arrows_list_t;
994  typedef std::map<map_location, arrows_list_t > arrows_map_t;
995  /** Maps the list of arrows for each location */
996  arrows_map_t arrows_map_;
997 
999 
1000  std::vector<std::tuple<int, int, int>> fps_history_;
1001 
1002 protected:
1004 };
1005 
1006 struct blindfold
1007 {
1008  blindfold(display& d, bool lock=true) : display_(d), blind(lock) {
1009  if(blind) {
1010  display_.blindfold(true);
1011  }
1012  }
1013 
1015  unblind();
1016  }
1017 
1018  void unblind() {
1019  if(blind) {
1020  display_.blindfold(false);
1021  display_.queue_rerender();
1022  blind = false;
1023  }
1024  }
1025 
1026 private:
1028  bool blind;
1029 };
The overlay used for the linger mode.
Definition: display.hpp:825
const map_location & mouseover_hex() const
Definition: display.hpp:290
TYPE
Used to specify the rendering format of images.
Definition: picture.hpp:228
int zoom_index_
Definition: display.hpp:719
Reserve layers to be selected for WML.
Definition: display.hpp:795
bool discard_previous
An announcement according these options should replace the previous announce (typical of fast announc...
Definition: display.hpp:598
halo::manager & get_halo_manager()
Definition: display.hpp:177
Move numbering for the whiteboard.
Definition: display.hpp:817
reports * reports_object_
Definition: display.hpp:731
bool view_locked() const
Definition: display.hpp:478
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:98
virtual const map_location & displayed_unit_hex() const
Virtual functions shadowed in game_display.
Definition: display.hpp:201
void invalidate_game_status()
Function to invalidate the game status displayed on the sidebar.
Definition: display.hpp:296
Small struct to store and manipulate ToD color adjusts.
Definition: time_of_day.hpp:27
theme & get_theme()
Definition: display.hpp:369
static int hex_size()
Function which returns the size of a hex in pixels (from top tip to bottom tip or left edge to right ...
Definition: display.hpp:247
This class represents a single unit of a specific type.
Definition: unit.hpp:133
bool animate_water_
Local version of preferences::animate_water, used to detect when it&#39;s changed.
Definition: display.hpp:766
CKey keys_
Definition: display.hpp:760
virtual bool in_game() const
Definition: display.hpp:197
static rect scaled_to_zoom(const SDL_Rect &r)
Scale the width and height of a rect by the current zoom factor.
Definition: display.hpp:256
boost::circular_buffer< unsigned > frametimes_
Definition: display.hpp:736
const map_location & operator*() const
Definition: display.hpp:322
int ypos_
Definition: display.hpp:711
const map_location & selected_hex() const
Definition: display.hpp:289
bool reach_map_changed_
Definition: display.hpp:937
rect dest
The screen coordinates for the specified hex.
Definition: display.hpp:875
static double get_zoom_factor()
Returns the current zoom factor.
Definition: display.hpp:250
events::generic_event & scroll_event() const
Expose the event, so observers can be notified about map scrolling.
Definition: display.hpp:521
const color_t GOOD_COLOR
void clear_exclusive_draws()
Cancels all the exclusive draw requests.
Definition: display.hpp:130
Arrows from the arrows framework.
Definition: display.hpp:816
#define a
void scroll_to_tiles(const std::vector< map_location > &locs, SCROLL_TYPE scroll_type=ONSCREEN, bool check_fogged=true, bool only_if_possible=false, double add_spacing=0.0, bool force=true)
Scroll to fit as many locations on-screen as possible, starting with the first.
Definition: display.hpp:511
std::function< void(const rect &)> do_draw
Handles the actual drawing at this location.
Definition: display.hpp:872
void toggle_debug_flag(DEBUG_FLAG flag)
Definition: display.hpp:984
map_location mouseoverHex_
Definition: display.hpp:759
Manages a list of fake units for the display object.
bool operator!=(const iterator &that) const
Definition: display.hpp:321
Mouseover overlay used by editor.
Definition: display.hpp:792
int fps_handle_
Handle for the label which displays frames per second.
Definition: display.hpp:946
bool show_everything() const
Definition: display.hpp:100
The class terrain_builder is constructed from a config object, and a gamemap object.
Definition: builder.hpp:48
bool blind
Definition: display.hpp:1028
std::map< map_location, std::string > exclusive_unit_draw_requests_t
Definition: display.hpp:664
const unit_map & get_units() const
Definition: display.hpp:131
int viewing_side() const
Definition: display.hpp:113
reach_map reach_map_
Definition: display.hpp:935
const rect_of_hexes & rect_
Definition: display.hpp:332
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
std::vector< std::function< void(display &)> > redraw_observers_
Definition: display.hpp:951
Bottom half of image following the mouse.
Definition: display.hpp:814
double turbo_speed()
Definition: general.cpp:479
map_location selectedHex_
Definition: display.hpp:758
std::list< draw_helper > drawing_buffer_
Definition: display.hpp:883
static display * singleton_
Definition: display.hpp:1003
#define d
Top half part of grid image.
Definition: display.hpp:791
terrain_builder & get_builder()
Definition: display.hpp:440
Rectangular area of hexes, allowing to decide how the top and bottom edges handles the vertical shift...
Definition: display.hpp:307
static unsigned int zoom_
The current zoom, in pixels (on screen) per 72 pixels (in the graphic assets), i.e., 72 means 100%.
Definition: display.hpp:718
Toggle to continuously redraw the whole map.
Definition: display.hpp:968
texture minimap_
Definition: display.hpp:724
drawing_layer
The layers to render something on.
Definition: display.hpp:786
std::vector< texture > terrain_image_vector_
Definition: display.hpp:777
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:32
static void layout()
Unit and team statistics.
void invalidate_all()
Mark the entire screen as requiring redraw.
very simple iterator to walk into the rect_of_hexes
Definition: display.hpp:314
const rect_of_hexes get_visible_hexes() const
Returns the rectangular area of visible hexes.
Definition: display.hpp:344
map_location loc_
std::map< std::string, rect > reportLocations_
Definition: display.hpp:744
virtual bool in_editor() const
Definition: display.hpp:198
Top half of image following the mouse.
Definition: display.hpp:794
#define b
std::vector< std::string > shroud_images_
Definition: display.hpp:756
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:56
Overlays x,y coords on tiles.
Definition: display.hpp:956
const config & options()
Definition: game.cpp:555
halo::manager halo_man_
Definition: display.hpp:661
void write(std::ostream &out, const configr_of &cfg, unsigned int level)
Definition: parser.cpp:764
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:75
Image on the selected unit.
Definition: display.hpp:818
std::map< std::string, texture > reportSurfaces_
Definition: display.hpp:745
void set_theme(const std::string &theme)
Definition: game.cpp:816
std::string label
What to show in the filter&#39;s drop-down list.
Definition: manager.cpp:217
Arrows destined to be drawn on the map.
Definition: arrow.hpp:30
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:627
std::map< map_location, std::vector< overlay > > overlay_map
Definition: display.hpp:940
Animate units.
const map_location & reference
Definition: display.hpp:328
bool map_screenshot_
Used to indicate to drawing functions that we are doing a map screenshot.
Definition: display.hpp:920
arrows_map_t arrows_map_
Maps the list of arrows for each location.
Definition: display.hpp:996
This class is the frontend of the whiteboard framework for the rest of the Wesnoth code...
Definition: manager.hpp:43
const std::unique_ptr< map_labels > map_labels_
Definition: display.hpp:730
Encapsulates the map of the game.
Definition: map.hpp:171
void unblind()
Definition: display.hpp:1018
SDL_Rect minimap_location_
Definition: display.hpp:725
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:58
tod_color color_adjust_
Definition: display.hpp:998
virtual int playing_side() const
Definition: display.hpp:202
void set_prevent_draw(bool pd)
Prevent the game display from drawing.
Definition: display.hpp:531
bool invalidateGameStatus_
Definition: display.hpp:729
unsigned int fps_actual_
Definition: display.hpp:740
A top-level drawable item (TLD), such as a window.
Separates background and foreground terrain layers.
Definition: display.hpp:965
int invalidated_hexes_
Count work done for the debug info displayed under fps.
Definition: display.hpp:948
static int hex_width()
Function which returns the width of a hex in pixels, up to where the next hex starts.
Definition: display.hpp:241
static unsigned int last_zoom_
The previous value of zoom_.
Definition: display.hpp:721
Fog and shroud.
Definition: display.hpp:815
Encapsulates the map of the game.
Definition: location.hpp:38
static bool expose()
Holds options for calls to function &#39;announce&#39; (announce).
Definition: display.hpp:588
std::size_t activeTeam_
Definition: display.hpp:842
std::vector< std::shared_ptr< gui::button > > menu_buttons_
Definition: display.hpp:747
mock_party p
static std::string get_location(const std::string &loc)
static void update()
blindfold(display &d, bool lock=true)
Definition: display.hpp:1008
virtual bool has_time_area() const
Definition: display.hpp:632
unsigned int fps_counter_
Definition: display.hpp:738
Definition: theme.hpp:42
double g
Definition: astarsearch.cpp:65
std::size_t playing_team() const
The playing team is the team whose turn it is.
Definition: display.hpp:107
bool redraw_background_
Definition: display.hpp:726
bool dont_show_all_
Definition: display.hpp:704
Definitions related to theme-support.
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:46
const display_context & get_disp_context() const
Definition: display.hpp:172
Holds a 2D point.
Definition: point.hpp:24
void set_debug_flag(DEBUG_FLAG flag, bool value)
Definition: display.hpp:979
static point scaled_to_zoom(const point &p)
Definition: display.hpp:262
const map_location * pointer
Definition: display.hpp:327
Declarations for File-IO.
theme theme_
Definition: display.hpp:713
Movement info (defense%, etc...).
Definition: display.hpp:824
bool view_locked_
Definition: display.hpp:712
static void render()
std::map< map_location, arrows_list_t > arrows_map_t
Definition: display.hpp:994
void set_view_locked(bool value)
Sets whether the map view is locked (e.g.
Definition: display.hpp:481
std::list< arrow * > arrows_list_t
Definition: display.hpp:993
std::vector< std::string > fog_images_
Definition: display.hpp:755
std::chrono::seconds fps_start_
Definition: display.hpp:739
display & display_
Definition: display.hpp:1027
std::map< map_location, unsigned int > reach_map
Definition: display.hpp:934
Helper for rendering the map by ordering draw operations.
Definition: display.hpp:866
virtual const std::set< std::string > & observers() const
Definition: display.hpp:203
Definition: display.hpp:45
std::bitset< __NUM_DEBUG_FLAGS > debug_flags_
Currently set debug flags.
Definition: display.hpp:991
const std::vector< team > & get_teams() const
Definition: display.hpp:104
reach_map reach_map_old_
Definition: display.hpp:936
const gamemap & get_map() const
Definition: display.hpp:102
exclusive_unit_draw_requests_t exclusive_unit_draw_requests_
map of hexes where only one unit should be drawn, the one identified by the associated id string ...
Definition: display.hpp:666
Overlays number of bitmaps on tiles.
Definition: display.hpp:962
Contains the SDL_Rect helper code.
iterator(const map_location &loc, const rect_of_hexes &rect)
Definition: display.hpp:315
bool invalidateAll_
Definition: display.hpp:727
events::generic_event scroll_event_
Event raised when the map is being scrolled.
Definition: display.hpp:734
#define f
int lifetime
Lifetime measured in milliseconds.
Definition: display.hpp:591
std::set< map_location > invalidated_
Definition: display.hpp:748
unsigned int tile_size
Definition: game_config.cpp:69
std::map< std::string, config > reports_
Definition: display.hpp:746
const display_context * dc_
Definition: display.hpp:660
std::forward_iterator_tag iterator_category
Definition: display.hpp:324
std::size_t viewing_team() const
The viewing team is the team currently viewing the game.
Definition: display.hpp:112
Functions to load and save images from/to disk.
int drawn_hexes_
Definition: display.hpp:949
int diagnostic_label_
Definition: display.hpp:728
static const map_location & null_location()
Definition: location.hpp:81
Overlays terrain codes on tiles.
Definition: display.hpp:959
tod_color get_color_overlay() const
Definition: display.hpp:195
Container associating units to locations.
Definition: map.hpp:98
bool debug_flag_set(DEBUG_FLAG flag) const
Definition: display.hpp:974
const std::unique_ptr< fake_unit_manager > fake_unit_man_
Definition: display.hpp:722
std::vector< animated< image::locator > > flags_
Animated flags for each team.
Definition: display.hpp:773
Definition: draw.hpp:42
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
std::vector< std::tuple< int, int, int > > fps_history_
Definition: display.hpp:1000
bool operator<(const draw_helper &rhs) const
Definition: display.hpp:877
Class that keeps track of all the keys on the keyboard.
Definition: key.hpp:28
The border of the map.
Definition: display.hpp:826
bool get_prevent_draw()
Definition: display.hpp:532
bool animate_map_
Local cache for preferences::animate_map, since it is constantly queried.
Definition: display.hpp:763
bool operator==(const iterator &that) const
Definition: display.hpp:320
const std::unique_ptr< terrain_builder > builder_
Definition: display.hpp:723
void reset_reports(reports &reports_object)
Definition: display.hpp:648
const uint32_t key
Controls the ordering of draw calls by layer and location.
Definition: display.hpp:869
std::weak_ptr< wb::manager > wb_
Definition: display.hpp:662
Definition: display.hpp:49
Layer which holds the attack indicator.
Definition: display.hpp:819
int blindfold_ctr_
Definition: display.hpp:656
TERRAIN_TYPE
Definition: display.hpp:689
Footsteps showing path from unit to mouse.
Definition: display.hpp:793
"black stripes" on unreachable hexes.
Definition: display.hpp:813
std::size_t currentTeam_
Definition: display.hpp:703