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