The Battle for Wesnoth  1.15.0-dev
display.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Routines to set up the display, scroll and zoom the map.
18  */
19 
20 #include "arrow.hpp"
21 #include "cursor.hpp"
22 #include "display.hpp"
23 #include "fake_unit_manager.hpp"
24 #include "font/sdl_ttf.hpp"
25 #include "font/text.hpp"
26 #include "preferences/game.hpp"
27 #include "gettext.hpp"
28 #include "halo.hpp"
30 #include "language.hpp"
31 #include "log.hpp"
32 #include "font/marked-up_text.hpp"
33 #include "map/map.hpp"
34 #include "map/label.hpp"
35 #include "minimap.hpp"
36 #include "overlay.hpp"
37 #include "play_controller.hpp" //note: this can probably be refactored out
38 #include "reports.hpp"
39 #include "resources.hpp"
40 #include "color.hpp"
41 #include "synced_context.hpp"
42 #include "team.hpp"
43 #include "terrain/builder.hpp"
44 #include "time_of_day.hpp"
45 #include "tooltips.hpp"
46 #include "tod_manager.hpp"
47 #include "units/unit.hpp"
49 #include "units/drawer.hpp"
50 #include "whiteboard/manager.hpp"
51 #include "show_dialog.hpp"
53 
54 #include <SDL_image.h>
55 
56 #include <array>
57 #include <cmath>
58 #include <iomanip>
59 #include <utility>
60 
61 #ifdef _WIN32
62 #include <windows.h>
63 #endif
64 
65 // Includes for bug #17573
66 
67 static lg::log_domain log_display("display");
68 #define ERR_DP LOG_STREAM(err, log_display)
69 #define LOG_DP LOG_STREAM(info, log_display)
70 #define DBG_DP LOG_STREAM(debug, log_display)
71 
72 // These are macros instead of proper constants so that they auto-update if the game config is reloaded.
73 #define zoom_levels (game_config::zoom_levels)
74 #define final_zoom_index (static_cast<int>(zoom_levels.size()) - 1)
75 #define DefaultZoom (game_config::tile_size)
76 #define SmallZoom (DefaultZoom / 2)
77 #define MinZoom (zoom_levels.front())
78 #define MaxZoom (zoom_levels.back())
79 
80 namespace {
81  bool benchmark = false;
82 
83  bool debug_foreground = false;
84 
85  int prevLabel = 0;
86 }
87 
88 unsigned int display::zoom_ = DefaultZoom;
89 unsigned int display::last_zoom_ = SmallZoom;
90 
92 {
93  const team& curr_team = dc_->teams()[playing_team()];
94  const team& prev_team = playing_team() == 0
95  ? dc_->teams().back()
97  for (const game_display::overlay_map::value_type i : get_overlays()) {
98  const overlay& ov = i.second;
99  if (!ov.team_name.empty() &&
100  ((ov.team_name.find(curr_team.team_name()) + 1) != 0) !=
101  ((ov.team_name.find(prev_team.team_name()) + 1) != 0))
102  {
103  invalidate(i.first);
104  }
105  }
106 }
107 
108 
109 void display::add_overlay(const map_location& loc, const std::string& img, const std::string& halo, const std::string& team_name, const std::string& item_id, bool visible_under_fog)
110 {
111  if (halo_man_) {
112  halo::handle halo_handle;
113  if(halo != "") {
114  halo_handle = halo_man_->add(get_location_x(loc) + hex_size() / 2,
115  get_location_y(loc) + hex_size() / 2, halo, loc);
116  }
117 
118  get_overlays().emplace(loc, overlay(img, halo, halo_handle, team_name, item_id, visible_under_fog));
119  }
120 }
121 
123 {
124  /* This code no longer needed because of RAII in halo::handles
125  if (halo_man_) {
126  typedef overlay_map::const_iterator Itor;
127  std::pair<Itor,Itor> itors = get_overlays().equal_range(loc);
128  while(itors.first != itors.second) {
129  halo_man_->remove(itors.first->second.halo_handle);
130  ++itors.first;
131  }
132  }
133  */
134 
135  get_overlays().erase(loc);
136 }
137 
138 void display::remove_single_overlay(const map_location& loc, const std::string& toDelete)
139 {
140  //Iterate through the values with key of loc
141  typedef overlay_map::iterator Itor;
142  overlay_map::iterator iteratorCopy;
143  std::pair<Itor,Itor> itors = get_overlays().equal_range(loc);
144  while(itors.first != itors.second) {
145  //If image or halo of overlay struct matches toDelete, remove the overlay
146  if(itors.first->second.image == toDelete || itors.first->second.halo == toDelete || itors.first->second.id == toDelete) {
147  iteratorCopy = itors.first;
148  ++itors.first;
149  //Not needed because of RAII --> halo_man_->remove(iteratorCopy->second.halo_handle);
150  get_overlays().erase(iteratorCopy);
151  }
152  else {
153  ++itors.first;
154  }
155  }
156 }
157 
158 display::display(const display_context * dc, std::weak_ptr<wb::manager> wb, reports & reports_object, const config& theme_cfg, const config& level, bool auto_join) :
159  video2::draw_layering(auto_join),
160  dc_(dc),
161  halo_man_(new halo::manager(*this)),
162  wb_(wb),
165  currentTeam_(0),
166  dont_show_all_(false),
167  xpos_(0),
168  ypos_(0),
169  view_locked_(false),
170  theme_(theme_cfg, screen_.screen_area()),
171  zoom_index_(0),
172  fake_unit_man_(new fake_unit_manager(*this)),
173  builder_(new terrain_builder(level, (dc_ ? &dc_->map() : nullptr), theme_.border().tile_image, theme_.border().show_border)),
174  minimap_(nullptr),
176  redrawMinimap_(false),
177  redraw_background_(true),
178  invalidateAll_(true),
179  grid_(false),
181  panelsDrawn_(false),
182  turbo_speed_(2),
183  turbo_(false),
184  invalidateGameStatus_(true),
185  map_labels_(new map_labels(nullptr)),
186  reports_object_(&reports_object),
187  scroll_event_("scrolled"),
188  complete_redraw_event_("completely_redrawn"),
189  frametimes_(50),
190  fps_counter_(),
191  fps_start_(),
192  fps_actual_(),
193  reportRects_(),
194  reportSurfaces_(),
195  reports_(),
196  menu_buttons_(),
197  action_buttons_(),
198  invalidated_(),
199  mouseover_hex_overlay_(nullptr),
200  tod_hex_mask1(nullptr),
201  tod_hex_mask2(nullptr),
202  fog_images_(),
203  shroud_images_(),
204  selectedHex_(),
205  mouseoverHex_(),
206  keys_(),
207  animate_map_(true),
208  animate_water_(true),
209  flags_(),
210  activeTeam_(0),
211  drawing_buffer_(),
212  map_screenshot_(false),
213  reach_map_(),
214  reach_map_old_(),
215  reach_map_changed_(true),
216  fps_handle_(0),
218  drawn_hexes_(0),
220  idle_anim_rate_(1.0),
221  map_screenshot_surf_(nullptr),
223  draw_coordinates_(false),
224  draw_terrain_codes_(false),
225  draw_num_of_bitmaps_(false),
226  arrows_map_(),
227  color_adjust_(),
228  dirty_()
229 {
230  //The following assertion fails when starting a campaign
231  assert(singleton_ == nullptr);
232  singleton_ = this;
233 
235 
236  blindfold_ctr_ = 0;
237 
238  read(level.child_or_empty("display"));
239 
241  && (screen_.getSurface() != nullptr
242  && screen_.faked())) {
243  screen_.lock_updates(true);
244  }
245 
248 
250 
251  zoom_index_ = std::distance(zoom_levels.begin(), std::find(zoom_levels.begin(), zoom_levels.end(), zoom_));
252 
254 
255  init_flags();
256 
257  if(!menu_buttons_.empty() || !action_buttons_.empty()) {
258  create_buttons();
259  }
260 
261 #ifdef _WIN32
262  // Increase timer resolution to prevent delays getting much longer than they should.
263  timeBeginPeriod(1u);
264 #endif
265 }
266 
268 {
269 #ifdef _WIN32
270  timeEndPeriod(1u);
271 #endif
272 
273  singleton_ = nullptr;
274  resources::fake_units = nullptr;
275 }
276 
277 void display::set_theme(config theme_cfg) {
278  theme_ = theme(theme_cfg, screen_.screen_area());
279  builder_->set_draw_border(theme_.border().show_border);
280  menu_buttons_.clear();
281  action_buttons_.clear();
282  create_buttons();
284 }
285 
287 
288  flags_.clear();
289  if (!dc_) return;
290  flags_.resize(dc_->teams().size());
291 
292  std::vector<std::string> side_colors;
293  side_colors.reserve(dc_->teams().size());
294 
295  for(const team& t : dc_->teams()) {
296  std::string side_color = t.color();
297  side_colors.push_back(side_color);
298  init_flags_for_side_internal(t.side() - 1, side_color);
299  }
300  image::set_team_colors(&side_colors);
301 }
302 
303 void display::reinit_flags_for_side(std::size_t side)
304 {
305  if (!dc_ || side >= dc_->teams().size()) {
306  ERR_DP << "Cannot rebuild flags for inexistent or unconfigured side " << side << '\n';
307  return;
308  }
309 
310  init_flags_for_side_internal(side, dc_->teams()[side].color());
311 }
312 
313 void display::init_flags_for_side_internal(std::size_t n, const std::string& side_color)
314 {
315  assert(dc_ != nullptr);
316  assert(n < dc_->teams().size());
317  assert(n < flags_.size());
318 
319  std::string flag = dc_->teams()[n].flag();
320  std::string old_rgb = game_config::flag_rgb;
321  std::string new_rgb = side_color;
322 
323  if(flag.empty()) {
325  }
326 
327  LOG_DP << "Adding flag for team " << n << " from animation " << flag << "\n";
328 
329  // Must recolor flag image
330  animated<image::locator> temp_anim;
331 
332  std::vector<std::string> items = utils::square_parenthetical_split(flag);
333 
334  for(const std::string& item : items) {
335  const std::vector<std::string>& sub_items = utils::split(item, ':');
336  std::string str = item;
337  int time = 100;
338 
339  if(sub_items.size() > 1) {
340  str = sub_items.front();
341  try {
342  time = std::max<int>(1, std::stoi(sub_items.back()));
343  } catch(const std::invalid_argument&) {
344  ERR_DP << "Invalid time value found when constructing flag for side " << n << ": " << sub_items.back() << "\n";
345  }
346  }
347 
348  std::stringstream temp;
349  temp << str << "~RC(" << old_rgb << ">"<< new_rgb << ")";
350  image::locator flag_image(temp.str());
351  temp_anim.add_frame(time, flag_image);
352  }
353 
355  f = temp_anim;
356  auto time = f.get_end_time();
357  if (time > 0) {
358  f.start_animation(randomness::rng::default_instance().get_random_int(0, time-1), true);
359  }
360  else {
361  // this can happen if both flag and game_config::images::flag are empty.
362  ERR_DP << "missing flag for team" << n << "\n";
363  }
364 }
365 
367 {
368  if(!get_map().is_village(loc)) {
369  return surface(nullptr);
370  }
371 
372  for (const team& t : dc_->teams()) {
373  if (t.owns_village(loc) && (!fogged(loc) || !dc_->get_team(viewing_side()).is_enemy(t.side())))
374  {
375  auto& flag = flags_[t.side() - 1];
376  flag.update_last_draw_time();
377  const image::locator &image_flag = animate_map_ ?
378  flag.get_current_frame() : flag.get_first_frame();
379  return image::get_image(image_flag, image::TOD_COLORED);
380  }
381  }
382 
383  return surface(nullptr);
384 }
385 
386 void display::set_team(std::size_t teamindex, bool show_everything)
387 {
388  assert(teamindex < dc_->teams().size());
389  currentTeam_ = teamindex;
390  if (!show_everything)
391  {
392  labels().set_team(&dc_->teams()[teamindex]);
393  dont_show_all_ = true;
394  }
395  else
396  {
397  labels().set_team(nullptr);
398  dont_show_all_ = false;
399  }
401  if(std::shared_ptr<wb::manager> w = wb_.lock())
402  w->on_viewer_change(teamindex);
403 }
404 
405 void display::set_playing_team(std::size_t teamindex)
406 {
407  assert(teamindex < dc_->teams().size());
408  activeTeam_ = teamindex;
410 }
411 
413 {
414  if (loc.valid() && exclusive_unit_draw_requests_.find(loc) == exclusive_unit_draw_requests_.end())
415  {
416  exclusive_unit_draw_requests_[loc] = unit.id();
417  return true;
418  }
419  else
420  {
421  return false;
422  }
423 }
424 
426 {
427  std::string id = "";
428  if(loc.valid())
429  {
431  //id will be set to the default "" string by the [] operator if the map doesn't have anything for that loc.
433  }
434  return id;
435 }
436 
437 const time_of_day & display::get_time_of_day(const map_location& /*loc*/) const
438 {
439  static time_of_day tod;
440  return tod;
441 }
442 
443 void display::update_tod(const time_of_day* tod_override)
444 {
445  const time_of_day* tod = tod_override;
446  if(tod == nullptr) {
447  tod = &get_time_of_day();
448  }
449 
450  const tod_color col = color_adjust_ + tod->color;
451  image::set_color_adjustment(col.r, col.g, col.b);
452 }
453 
454 void display::adjust_color_overlay(int r, int g, int b)
455 {
456  color_adjust_ = tod_color(r, g, b);
457  update_tod();
458 }
459 
460 void display::fill_images_list(const std::string& prefix, std::vector<std::string>& images)
461 {
462  if(prefix == ""){
463  return;
464  }
465 
466  // search prefix.png, prefix1.png, prefix2.png ...
467  for(int i=0; ; ++i){
468  std::ostringstream s;
469  s << prefix;
470  if(i != 0)
471  s << i;
472  s << ".png";
473  if(image::exists(s.str()))
474  images.push_back(s.str());
475  else if(i>0)
476  break;
477  }
478  if (images.empty())
479  images.emplace_back();
480 }
481 
482 const std::string& display::get_variant(const std::vector<std::string>& variants, const map_location &loc)
483 {
484  //TODO use better noise function
485  return variants[std::abs(loc.x + loc.y) % variants.size()];
486 }
487 
489 {
490  builder_->rebuild_all();
491 }
492 
494 {
495  redraw_background_ = true;
496  builder_->reload_map();
497 }
498 
500 {
501  dc_ = dc;
502  builder_->change_map(&dc_->map()); //TODO: Should display_context own and initialize the builder object?
503 }
504 
506 {
507  halo_man_.reset(new halo::manager(*this));
508 }
509 
511 {
512  halo_man_.reset(&halo_man);
513 }
514 
515 void display::blindfold(bool value)
516 {
517  if(value == true)
518  ++blindfold_ctr_;
519  else
520  --blindfold_ctr_;
521 }
522 
524 {
525  return blindfold_ctr_ > 0;
526 }
527 
528 
529 const SDL_Rect& display::max_map_area() const
530 {
531  static SDL_Rect max_area {0, 0, 0, 0};
532 
533  // hex_size() is always a multiple of 4
534  // and hex_width() a multiple of 3,
535  // so there shouldn't be off-by-one-errors
536  // due to rounding.
537  // To display a hex fully on screen,
538  // a little bit extra space is needed.
539  // Also added the border two times.
540  max_area.w = static_cast<int>((get_map().w() + 2 * theme_.border().size + 1.0/3.0) * hex_width());
541  max_area.h = static_cast<int>((get_map().h() + 2 * theme_.border().size + 0.5) * hex_size());
542 
543  return max_area;
544 }
545 
546 const SDL_Rect& display::map_area() const
547 {
548  static SDL_Rect max_area;
549  max_area = max_map_area();
550 
551  // if it's for map_screenshot, maximize and don't recenter
552  if (map_screenshot_) {
553  return max_area;
554  }
555 
556  static SDL_Rect res;
557  res = map_outside_area();
558 
559  if(max_area.w < res.w) {
560  // map is smaller, center
561  res.x += (res.w - max_area.w)/2;
562  res.w = max_area.w;
563  }
564 
565  if(max_area.h < res.h) {
566  // map is smaller, center
567  res.y += (res.h - max_area.h)/2;
568  res.h = max_area.h;
569  }
570 
571  return res;
572 }
573 
574 bool display::outside_area(const SDL_Rect& area, const int x, const int y)
575 {
576  const int x_thresh = hex_size();
577  const int y_thresh = hex_size();
578  return (x < area.x || x > area.x + area.w - x_thresh ||
579  y < area.y || y > area.y + area.h - y_thresh);
580 }
581 
582 // This function uses the screen as reference
583 const map_location display::hex_clicked_on(int xclick, int yclick) const
584 {
585  const SDL_Rect& rect = map_area();
586  if(sdl::point_in_rect(xclick,yclick,rect) == false) {
587  return map_location();
588  }
589 
590  xclick -= rect.x;
591  yclick -= rect.y;
592 
593  return pixel_position_to_hex(xpos_ + xclick, ypos_ + yclick);
594 }
595 
596 
597 // This function uses the rect of map_area as reference
599 {
600  // adjust for the border
601  x -= static_cast<int>(theme_.border().size * hex_width());
602  y -= static_cast<int>(theme_.border().size * hex_size());
603  // The editor can modify the border and this will result in a negative y
604  // value. Instead of adding extra cases we just shift the hex. Since the
605  // editor doesn't use the direction this is no problem.
606  const int offset = y < 0 ? 1 : 0;
607  if(offset) {
608  x += hex_width();
609  y += hex_size();
610  }
611  const int s = hex_size();
612  const int tesselation_x_size = hex_width() * 2;
613  const int tesselation_y_size = s;
614  const int x_base = x / tesselation_x_size * 2;
615  const int x_mod = x % tesselation_x_size;
616  const int y_base = y / tesselation_y_size;
617  const int y_mod = y % tesselation_y_size;
618 
619  int x_modifier = 0;
620  int y_modifier = 0;
621 
622  if (y_mod < tesselation_y_size / 2) {
623  if ((x_mod * 2 + y_mod) < (s / 2)) {
624  x_modifier = -1;
625  y_modifier = -1;
626  } else if ((x_mod * 2 - y_mod) < (s * 3 / 2)) {
627  x_modifier = 0;
628  y_modifier = 0;
629  } else {
630  x_modifier = 1;
631  y_modifier = -1;
632  }
633 
634  } else {
635  if ((x_mod * 2 - (y_mod - s / 2)) < 0) {
636  x_modifier = -1;
637  y_modifier = 0;
638  } else if ((x_mod * 2 + (y_mod - s / 2)) < s * 2) {
639  x_modifier = 0;
640  y_modifier = 0;
641  } else {
642  x_modifier = 1;
643  y_modifier = 0;
644  }
645  }
646 
647  return map_location(x_base + x_modifier - offset, y_base + y_modifier - offset);
648 }
649 
651 {
652  if (loc_.y < rect_.bottom[loc_.x & 1])
653  ++loc_.y;
654  else {
655  ++loc_.x;
656  loc_.y = rect_.top[loc_.x & 1];
657  }
658 
659  return *this;
660 }
661 
662 // begin is top left, and end is after bottom right
664 {
665  return iterator(map_location(left, top[left & 1]), *this);
666 }
668 {
669  return iterator(map_location(right+1, top[(right+1) & 1]), *this);
670 }
671 
672 const display::rect_of_hexes display::hexes_under_rect(const SDL_Rect& r) const
673 {
674  rect_of_hexes res;
675 
676  if (r.w<=0 || r.h<=0) {
677  // empty rect, return dummy values giving begin=end
678  res.left = 0;
679  res.right = -1; // end is right+1
680  res.top[0] = 0;
681  res.top[1] = 0;
682  res.bottom[0] = 0;
683  res.bottom[1] = 0;
684  return res;
685  }
686 
687  SDL_Rect map_rect = map_area();
688  // translate rect coordinates from screen-based to map_area-based
689  int x = xpos_ - map_rect.x + r.x;
690  int y = ypos_ - map_rect.y + r.y;
691  // we use the "double" type to avoid important rounding error (size of an hex!)
692  // we will also need to use std::floor to avoid bad rounding at border (negative values)
693  double tile_width = hex_width();
694  double tile_size = hex_size();
695  double border = theme_.border().size;
696  // we minus "0.(3)", for horizontal imbrication.
697  // reason is: two adjacent hexes each overlap 1/4 of their width, so for
698  // grid calculation 3/4 of tile width is used, which by default gives
699  // 18/54=0.(3). Note that, while tile_width is zoom dependent, 0.(3) is not.
700  res.left = static_cast<int>(std::floor(-border + x / tile_width - 0.3333333));
701  // we remove 1 pixel of the rectangle dimensions
702  // (the rounded division take one pixel more than needed)
703  res.right = static_cast<int>(std::floor(-border + (x + r.w-1) / tile_width));
704 
705  // for odd x, we must shift up one half-hex. Since x will vary along the edge,
706  // we store here the y values for even and odd x, respectively
707  res.top[0] = static_cast<int>(std::floor(-border + y / tile_size));
708  res.top[1] = static_cast<int>(std::floor(-border + y / tile_size - 0.5));
709  res.bottom[0] = static_cast<int>(std::floor(-border + (y + r.h-1) / tile_size));
710  res.bottom[1] = static_cast<int>(std::floor(-border + (y + r.h-1) / tile_size - 0.5));
711 
712  // TODO: in some rare cases (1/16), a corner of the big rect is on a tile
713  // (the 72x72 rectangle containing the hex) but not on the hex itself
714  // Can maybe be optimized by using pixel_position_to_hex
715 
716  return res;
717 }
718 
720 {
721  return currentTeam_ < dc_->teams().size();
722 }
723 
724 bool display::shrouded(const map_location& loc) const
725 {
726  return is_blindfolded() || (dont_show_all_ && dc_->teams()[currentTeam_].shrouded(loc));
727 }
728 
729 bool display::fogged(const map_location& loc) const
730 {
731  return is_blindfolded() || (dont_show_all_ && dc_->teams()[currentTeam_].fogged(loc));
732 }
733 
735 {
736  return static_cast<int>(map_area().x + (loc.x + theme_.border().size) * hex_width() - xpos_);
737 }
738 
740 {
741  return static_cast<int>(map_area().y + (loc.y + theme_.border().size) * zoom_ - ypos_ + (is_odd(loc.x) ? zoom_/2 : 0));
742 }
743 
745 {
746  //TODO: don't return location for this,
747  // instead directly scroll to the clicked pixel position
748 
749  if (!sdl::point_in_rect(x, y, minimap_area())) {
750  return map_location();
751  }
752 
753  // we transform the coordinates from minimap to the full map image
754  // probably more adjustments to do (border, minimap shift...)
755  // but the mouse and human capacity to evaluate the rectangle center
756  // is not pixel precise.
757  int px = (x - minimap_location_.x) * get_map().w()*hex_width() / std::max (minimap_location_.w, 1);
758  int py = (y - minimap_location_.y) * get_map().h()*hex_size() / std::max(minimap_location_.h, 1);
759 
760  map_location loc = pixel_position_to_hex(px, py);
761  if (loc.x < 0)
762  loc.x = 0;
763  else if (loc.x >= get_map().w())
764  loc.x = get_map().w() - 1;
765 
766  if (loc.y < 0)
767  loc.y = 0;
768  else if (loc.y >= get_map().h())
769  loc.y = get_map().h() - 1;
770 
771  return loc;
772 }
773 
774 surface display::screenshot(bool map_screenshot)
775 {
776  if (!map_screenshot) {
777  // Use make_neutral_surface() to copy surface content
779  } else {
780  if (get_map().empty()) {
781  ERR_DP << "No map loaded, cannot create a map screenshot.\n";
782  return nullptr;
783  }
784 
785  SDL_Rect area = max_map_area();
787 
788  if (map_screenshot_surf_ == nullptr) {
789  // Memory problem ?
790  ERR_DP << "Could not create screenshot surface, try zooming out.\n";
791  return nullptr;
792  }
793 
794  // back up the current map view position and move to top-left
795  int old_xpos = xpos_;
796  int old_ypos = ypos_;
797  xpos_ = 0;
798  ypos_ = 0;
799 
800  // we reroute render output to the screenshot surface and invalidate all
801  map_screenshot_= true;
802  invalidateAll_ = true;
803  DBG_DP << "draw() with map_screenshot\n";
804  draw(true,true);
805 
806  // restore normal rendering
807  map_screenshot_= false;
808  xpos_ = old_xpos;
809  ypos_ = old_ypos;
810 
811  // Clear map_screenshot_surf_ and return a new surface that contains the same data
812  surface surf(std::move(map_screenshot_surf_));
813  return surf;
814  }
815 }
816 
817 std::shared_ptr<gui::button> display::find_action_button(const std::string& id)
818 {
819  for (std::size_t i = 0; i < action_buttons_.size(); ++i) {
820  if(action_buttons_[i]->id() == id) {
821  return action_buttons_[i];
822  }
823  }
824  return nullptr;
825 }
826 
827 std::shared_ptr<gui::button> display::find_menu_button(const std::string& id)
828 {
829  for (std::size_t i = 0; i < menu_buttons_.size(); ++i) {
830  if(menu_buttons_[i]->id() == id) {
831  return menu_buttons_[i];
832  }
833  }
834  return nullptr;
835 }
836 
838 {
839  DBG_DP << "positioning menu buttons...\n";
840  for(const auto& menu : theme_.menus()) {
841  std::shared_ptr<gui::button> b = find_menu_button(menu.get_id());
842  if(b) {
843  const SDL_Rect& loc = menu.location(screen_.screen_area());
844  b->set_location(loc);
845  b->set_measurements(0,0);
846  b->set_label(menu.title());
847  b->set_image(menu.image());
848  }
849  }
850 
851  DBG_DP << "positioning action buttons...\n";
852  for(const auto& action : theme_.actions()) {
853  std::shared_ptr<gui::button> b = find_action_button(action.get_id());
854  if(b) {
855  const SDL_Rect& loc = action.location(screen_.screen_area());
856  b->set_location(loc);
857  b->set_measurements(0,0);
858  b->set_label(action.title());
859  b->set_image(action.image());
860  }
861  }
862 }
863 
865 {
866  std::vector<std::shared_ptr<gui::button>> menu_work;
867  std::vector<std::shared_ptr<gui::button>> action_work;
868 
869  DBG_DP << "creating menu buttons...\n";
870  for(const auto& menu : theme_.menus()) {
871  if (!menu.is_button()) continue;
872 
873  std::shared_ptr<gui::button> b(new gui::button(screen_, menu.title(), gui::button::TYPE_PRESS, menu.image(),
874  gui::button::DEFAULT_SPACE, false, menu.overlay()));
875  DBG_DP << "drawing button " << menu.get_id() << "\n";
876  b->join_same(this);
877  b->set_id(menu.get_id());
878  if (!menu.tooltip().empty()){
879  b->set_tooltip_string(menu.tooltip());
880  }
881 
882  std::shared_ptr<gui::button> b_prev = find_menu_button(b->id());
883  if(b_prev) {
884  b->enable(b_prev->enabled());
885  }
886 
887  menu_work.push_back(b);
888  }
889 
890  DBG_DP << "creating action buttons...\n";
891  for(const auto& action : theme_.actions()) {
892  std::shared_ptr<gui::button> b(new gui::button(screen_, action.title(), string_to_button_type(action.type()), action.image(),
893  gui::button::DEFAULT_SPACE, false, action.overlay()));
894 
895  DBG_DP << "drawing button " << action.get_id() << "\n";
896  b->set_id(action.get_id());
897  b->join_same(this);
898  if (!action.tooltip(0).empty()){
899  b->set_tooltip_string(action.tooltip(0));
900  }
901 
902  std::shared_ptr<gui::button> b_prev = find_action_button(b->id());
903  if(b_prev) {
904  b->enable(b_prev->enabled());
905  if (b_prev->get_type() == gui::button::TYPE_CHECK) {
906  b->set_check(b_prev->checked());
907  }
908  }
909 
910  action_work.push_back(b);
911  }
912 
913 
914  menu_buttons_.clear();
915  menu_buttons_.assign(menu_work.begin(), menu_work.end());
916  action_buttons_.clear();
917  action_buttons_.assign(action_work.begin(), action_work.end());
918 
919  layout_buttons();
920  DBG_DP << "buttons created\n";
921 }
922 
924 {
925  for (std::shared_ptr<gui::button> btn : menu_buttons_) {
926  btn->set_dirty(true);
927  btn->draw();
928  }
929 
930  for (std::shared_ptr<gui::button> btn : action_buttons_) {
931  btn->set_dirty(true);
932  btn->draw();
933  }
934 }
935 
936 
938 {
940  if (type == "checkbox") { res = gui::button::TYPE_CHECK; }
941  else if (type == "image") { res = gui::button::TYPE_IMAGE; }
942  else if (type == "radiobox") { res = gui::button::TYPE_RADIO; }
943  else if (type == "turbo") { res = gui::button::TYPE_TURBO; }
944  return res;
945 }
946 
947 static const std::string& get_direction(std::size_t n)
948 {
949  static const std::array<std::string, 6> dirs {{ "-n", "-ne", "-se", "-s", "-sw", "-nw" }};
950  return dirs[n >= dirs.size() ? 0 : n];
951 }
952 
953 std::vector<surface> display::get_fog_shroud_images(const map_location& loc, image::TYPE image_type)
954 {
955  std::vector<std::string> names;
956 
957  adjacent_loc_array_t adjacent;
958  get_adjacent_tiles(loc, adjacent.data());
959 
960  enum visibility {FOG=0, SHROUD=1, CLEAR=2};
961  visibility tiles[6];
962 
963  const std::string* image_prefix[] =
965 
966  for(int i = 0; i < 6; ++i) {
967  if(shrouded(adjacent[i])) {
968  tiles[i] = SHROUD;
969  } else if(!fogged(loc) && fogged(adjacent[i])) {
970  tiles[i] = FOG;
971  } else {
972  tiles[i] = CLEAR;
973  }
974  }
975 
976  for(int v = FOG; v != CLEAR; ++v) {
977  // Find somewhere that doesn't have overlap to use as a starting point
978  int start;
979  for(start = 0; start != 6; ++start) {
980  if(tiles[start] != v) {
981  break;
982  }
983  }
984 
985  if(start == 6) {
986  // Completely surrounded by fog or shroud. This might have
987  // a special graphic.
988  const std::string name = *image_prefix[v] + "-all.png";
989  if ( image::exists(name) ) {
990  names.push_back(name);
991  // Proceed to the next visibility (fog -> shroud -> clear).
992  continue;
993  }
994  // No special graphic found. We'll just combine some other images
995  // and hope it works out.
996  start = 0;
997  }
998 
999  // Find all the directions overlap occurs from
1000  for (int i = (start+1)%6, cap1 = 0; i != start && cap1 != 6; ++cap1) {
1001  if(tiles[i] == v) {
1002  std::ostringstream stream;
1003  std::string name;
1004  stream << *image_prefix[v];
1005 
1006  for (int cap2 = 0; v == tiles[i] && cap2 != 6; i = (i+1)%6, ++cap2) {
1007  stream << get_direction(i);
1008 
1009  if(!image::exists(stream.str() + ".png")) {
1010  // If we don't have any surface at all,
1011  // then move onto the next overlapped area
1012  if(name.empty()) {
1013  i = (i+1)%6;
1014  }
1015  break;
1016  } else {
1017  name = stream.str();
1018  }
1019  }
1020 
1021  if(!name.empty()) {
1022  names.push_back(name + ".png");
1023  }
1024  } else {
1025  i = (i+1)%6;
1026  }
1027  }
1028  }
1029 
1030  // now get the surfaces
1031  std::vector<surface> res;
1032 
1033  for (std::string& name : names) {
1034  surface surf(image::get_image(name, image_type));
1035  if (surf)
1036  res.push_back(std::move(surf));
1037  }
1038 
1039  return res;
1040 }
1041 
1043  const std::string& timeid,
1045 {
1046  terrain_image_vector_.clear();
1047 
1048  terrain_builder::TERRAIN_TYPE builder_terrain_type =
1049  (terrain_type == FOREGROUND ?
1051 
1052  const terrain_builder::imagelist* const terrains = builder_->get_terrain_at(loc,
1053  timeid, builder_terrain_type);
1054 
1056 
1057  const time_of_day& tod = get_time_of_day(loc);
1058 
1059  //get all the light transitions
1060  adjacent_loc_array_t adjs;
1061  std::array<const time_of_day*, 6> atods;
1062  get_adjacent_tiles(loc, adjs.data());
1063  for(size_t d = 0; d < adjs.size(); ++d){
1064  atods[d] = &get_time_of_day(adjs[d]);
1065  }
1066 
1067  for(int d=0; d<6; ++d){
1068  /* concave
1069  _____
1070  / \
1071  / atod1 \_____
1072  \ !tod / \
1073  \_____/ atod2 \
1074  / \__\ !tod /
1075  / \_____/
1076  \ tod /
1077  \_____/*/
1078 
1079  const time_of_day& atod1 = *atods[d];
1080  const time_of_day& atod2 = *atods[(d + 1) % 6];
1081 
1082  if(atod1.color == tod.color || atod2.color == tod.color || atod1.color != atod2.color)
1083  continue;
1084 
1085  if(lt.empty()) {
1086  //color the full hex before adding transitions
1087  tod_color col = tod.color + color_adjust_;
1088  lt = image::get_light_string(0, col.r, col.g, col.b);
1089  }
1090 
1091  // add the directional transitions
1092  tod_color acol = atod1.color + color_adjust_;
1093  lt += image::get_light_string(d + 1, acol.r, acol.g, acol.b);
1094  }
1095  for(int d=0; d<6; ++d){
1096  /* convex 1
1097  _____
1098  / \
1099  / atod1 \_____
1100  \ !tod / \
1101  \_____/ atod2 \
1102  / \__\ tod /
1103  / \_____/
1104  \ tod /
1105  \_____/*/
1106 
1107  const time_of_day& atod1 = *atods[d];
1108  const time_of_day& atod2 = *atods[(d + 1) % 6];
1109 
1110  if(atod1.color == tod.color || atod1.color == atod2.color)
1111  continue;
1112 
1113  if(lt.empty()) {
1114  //color the full hex before adding transitions
1115  tod_color col = tod.color + color_adjust_;
1116  lt = image::get_light_string(0, col.r, col.g, col.b);
1117  }
1118 
1119  // add the directional transitions
1120  tod_color acol = atod1.color + color_adjust_;
1121  lt += image::get_light_string(d + 7, acol.r, acol.g, acol.b);
1122  }
1123  for(int d=0; d<6; ++d){
1124  /* convex 2
1125  _____
1126  / \
1127  / atod1 \_____
1128  \ tod / \
1129  \_____/ atod2 \
1130  / \__\ !tod /
1131  / \_____/
1132  \ tod /
1133  \_____/*/
1134 
1135  const time_of_day& atod1 = *atods[d];
1136  const time_of_day& atod2 = *atods[(d + 1) % 6];
1137 
1138  if(atod2.color == tod.color || atod1.color == atod2.color)
1139  continue;
1140 
1141  if(lt.empty()) {
1142  //color the full hex before adding transitions
1143  tod_color col = tod.color + color_adjust_;
1144  lt = image::get_light_string(0, col.r, col.g, col.b);
1145  }
1146 
1147  // add the directional transitions
1148  tod_color acol = atod2.color + color_adjust_;
1149  lt += image::get_light_string(d + 13, acol.r, acol.g, acol.b);
1150  }
1151 
1152  if(lt.empty()){
1153  tod_color col = tod.color + color_adjust_;
1154  if(!col.is_zero()){
1155  // no real lightmap needed but still color the hex
1156  lt = image::get_light_string(-1, col.r, col.g, col.b);
1157  }
1158  }
1159 
1160  if(terrains != nullptr) {
1161  // Cache the offmap name.
1162  // Since it is themeable it can change,
1163  // so don't make it static.
1164  const std::string off_map_name = "terrain/" + theme_.border().tile_image;
1165  for(const auto& terrain : *terrains) {
1166  const image::locator &image = animate_map_ ?
1167  terrain.get_current_frame() : terrain.get_first_frame();
1168 
1169  // We prevent ToD coloring and brightening of off-map tiles,
1170  // We need to test for the tile to be rendered and
1171  // not the location, since the transitions are rendered
1172  // over the offmap-terrain and these need a ToD coloring.
1173  surface surf;
1174  const bool off_map = (image.get_filename() == off_map_name || image.get_modifications().find("NO_TOD_SHIFT()") != std::string::npos);
1175 
1176  if(off_map) {
1177  surf = image::get_image(image, image::SCALED_TO_HEX);
1178  } else if(lt.empty()) {
1179  surf = image::get_image(image, image::SCALED_TO_HEX);
1180  } else {
1181  surf = image::get_lighted_image(image, lt, image::SCALED_TO_HEX);
1182  }
1183 
1184  if (!surf.null()) {
1185  terrain_image_vector_.push_back(std::move(surf));
1186  }
1187  }
1188  }
1189 }
1190 
1191 void display::drawing_buffer_add(const drawing_layer layer,
1192  const map_location& loc, int x, int y, const surface& surf,
1193  const SDL_Rect &clip)
1194 {
1195  drawing_buffer_.emplace_back(layer, loc, x, y, surf, clip);
1196 }
1197 
1198 void display::drawing_buffer_add(const drawing_layer layer,
1199  const map_location& loc, int x, int y,
1200  const std::vector<surface> &surf,
1201  const SDL_Rect &clip)
1202 {
1203  drawing_buffer_.emplace_back(layer, loc, x, y, surf, clip);
1204 }
1205 
1206 // FIXME: temporary method. Group splitting should be made
1207 // public into the definition of drawing_layer
1208 //
1209 // The drawing is done per layer_group, the range per group is [low, high).
1210 const std::array<display::drawing_layer, 4> display::drawing_buffer_key::layer_groups {{
1211  LAYER_TERRAIN_BG,
1212  LAYER_UNIT_FIRST,
1213  LAYER_UNIT_MOVE_DEFAULT,
1214  // Make sure the movement doesn't show above fog and reachmap.
1215  LAYER_REACHMAP
1216 }};
1217 
1218 enum {
1219  // you may adjust the following when needed:
1220 
1221  // maximum border. 3 should be safe even if a larger border is in use somewhere
1222  MAX_BORDER = 3,
1223 
1224  // store x, y, and layer in one 32 bit integer
1225  // 4 most significant bits == layer group => 16
1226  BITS_FOR_LAYER_GROUP = 4,
1227 
1228  // 10 second most significant bits == y => 1024
1229  BITS_FOR_Y = 10,
1230 
1231  // 1 third most significant bit == x parity => 2
1232  BITS_FOR_X_PARITY = 1,
1233 
1234  // 8 fourth most significant bits == layer => 256
1235  BITS_FOR_LAYER = 8,
1236 
1237  // 9 least significant bits == x / 2 => 512 (really 1024 for x)
1238  BITS_FOR_X_OVER_2 = 9
1239 };
1240 
1241 inline display::drawing_buffer_key::drawing_buffer_key(const map_location &loc, drawing_layer layer)
1242  : key_(0)
1243 {
1244  // Start with the index of last group entry...
1245  unsigned int group_i = layer_groups.size() - 1;
1246 
1247  // ...and works backwards until the group containing the specified layer is found.
1248  while(layer < layer_groups[group_i]) {
1249  --group_i;
1250  }
1251 
1252  enum {
1253  SHIFT_LAYER = BITS_FOR_X_OVER_2,
1254  SHIFT_X_PARITY = BITS_FOR_LAYER + SHIFT_LAYER,
1255  SHIFT_Y = BITS_FOR_X_PARITY + SHIFT_X_PARITY,
1256  SHIFT_LAYER_GROUP = BITS_FOR_Y + SHIFT_Y
1257  };
1258  static_assert(SHIFT_LAYER_GROUP + BITS_FOR_LAYER_GROUP == sizeof(key_) * 8, "Bit field too small");
1259 
1260  // the parity of x must be more significant than the layer but less significant than y.
1261  // Thus basically every row is split in two: First the row containing all the odd x
1262  // then the row containing all the even x. Since thus the least significant bit of x is
1263  // not required for x ordering anymore it can be shifted out to the right.
1264  const unsigned int x_parity = static_cast<unsigned int>(loc.x) & 1;
1265  key_ = (group_i << SHIFT_LAYER_GROUP) | (static_cast<unsigned int>(loc.y + MAX_BORDER) << SHIFT_Y);
1266  key_ |= (x_parity << SHIFT_X_PARITY);
1267  key_ |= (static_cast<unsigned int>(layer) << SHIFT_LAYER) | static_cast<unsigned int>(loc.x + MAX_BORDER) / 2;
1268 }
1269 
1270 void display::drawing_buffer_commit()
1271 {
1272  // std::list::sort() is a stable sort
1273  drawing_buffer_.sort();
1274 
1275  SDL_Rect clip_rect = map_area();
1276  surface& screen = get_screen_surface();
1277  clip_rect_setter set_clip_rect(screen, &clip_rect);
1278 
1279  /*
1280  * Info regarding the rendering algorithm.
1281  *
1282  * In order to render a hex properly it needs to be rendered per row. On
1283  * this row several layers need to be drawn at the same time. Mainly the
1284  * unit and the background terrain. This is needed since both can spill
1285  * in the next hex. The foreground terrain needs to be drawn before to
1286  * avoid decapitation a unit.
1287  *
1288  * This ended in the following priority order:
1289  * layergroup > location > layer > 'blit_helper' > surface
1290  */
1291 
1292  for (const blit_helper &blit : drawing_buffer_) {
1293  for (const surface& surf : blit.surf()) {
1294  // Note that dstrect can be changed by sdl_blit
1295  // and so a new instance should be initialized
1296  // to pass to each call to sdl_blit.
1297  SDL_Rect dstrect {blit.x(), blit.y(), 0, 0};
1298  SDL_Rect srcrect = blit.clip();
1299  SDL_Rect *srcrectArg = (srcrect.x | srcrect.y | srcrect.w | srcrect.h)
1300  ? &srcrect : nullptr;
1301  sdl_blit(surf, srcrectArg, screen, &dstrect);
1302  //NOTE: the screen part should already be marked as 'to update'
1303  }
1304  }
1306 }
1307 
1309 {
1310  drawing_buffer_.clear();
1311 }
1312 
1314 {
1315  benchmark = !benchmark;
1316 }
1317 
1319 {
1320  debug_foreground = !debug_foreground;
1321 }
1322 
1324 {
1325  if(video().faked()) {
1326  return;
1327  }
1328 
1329  surface& frameBuffer = video().getSurface();
1330 
1331  font::draw_floating_labels(frameBuffer);
1333 
1334  video().flip();
1335 
1337  font::undraw_floating_labels(frameBuffer);
1338 }
1339 
1340 // frametime is in milliseconds
1341 static unsigned calculate_fps(unsigned frametime)
1342 {
1343  return frametime != 0u ? 1000u / frametime : 999u;
1344 }
1345 
1347 {
1348  if (screen_.update_locked()) {
1349  return;
1350  }
1351 
1352  if(preferences::show_fps() || benchmark) {
1353  static int frames = 0;
1354  ++frames;
1355  const int sample_freq = 10;
1356  if(frames == sample_freq) {
1357  const auto minmax_it = std::minmax_element(frametimes_.begin(), frametimes_.end());
1358  const unsigned render_avg = std::accumulate(frametimes_.begin(), frametimes_.end(), 0) / frametimes_.size();
1359  const int avg_fps = calculate_fps(render_avg);
1360  const int max_fps = calculate_fps(*minmax_it.first);
1361  const int min_fps = calculate_fps(*minmax_it.second);
1362  frames = 0;
1363 
1364  if(fps_handle_ != 0) {
1366  fps_handle_ = 0;
1367  }
1368  std::ostringstream stream;
1369  stream << "<tt> min/avg/max/act</tt>\n";
1370  stream << "<tt>FPS: " << std::setfill(' ') << std::setw(3) << min_fps << '/'<< std::setw(3) << avg_fps << '/' << std::setw(3) << max_fps << '/' << std::setw(3) << fps_actual_ << "</tt>\n";
1371  stream << "<tt>Time: " << std::setfill(' ') << std::setw(3) << *minmax_it.first << '/' << std::setw(3) << render_avg << '/' << std::setw(3) << *minmax_it.second << " ms</tt>\n";
1372  if (game_config::debug) {
1373  stream << "\nhex: " << drawn_hexes_*1.0/sample_freq;
1375  stream << " (" << (invalidated_hexes_-drawn_hexes_)*1.0/sample_freq << ")";
1376  }
1377  drawn_hexes_ = 0;
1378  invalidated_hexes_ = 0;
1379 
1380  font::floating_label flabel(stream.str());
1381  flabel.set_font_size(12);
1382  flabel.set_color(benchmark ? font::BAD_COLOR : font::NORMAL_COLOR);
1383  flabel.set_position(10, 100);
1384  flabel.set_alignment(font::LEFT_ALIGN);
1385 
1387  }
1388  } else if(fps_handle_ != 0) {
1390  fps_handle_ = 0;
1391  drawn_hexes_ = 0;
1392  invalidated_hexes_ = 0;
1393  }
1394 
1395  flip();
1396 }
1397 static void draw_panel(CVideo &video, const theme::panel& panel, std::vector<std::shared_ptr<gui::button>>& /*buttons*/)
1398 {
1399  //log_scope("draw panel");
1400  DBG_DP << "drawing panel " << panel.get_id() << "\n";
1401 
1402  surface surf(image::get_image(panel.image()));
1403 
1404  const SDL_Rect screen = video.screen_area();
1405  SDL_Rect& loc = panel.location(screen);
1406 
1407  DBG_DP << "panel location: x=" << loc.x << ", y=" << loc.y
1408  << ", w=" << loc.w << ", h=" << loc.h << "\n";
1409 
1410  if(!surf.null()) {
1411  if(surf->w != loc.w || surf->h != loc.h) {
1412  surf.assign(tile_surface(surf,loc.w,loc.h));
1413  }
1414  video.blit_surface(loc.x, loc.y, surf);
1415  }
1416 }
1417 
1418 static void draw_label(CVideo& video, surface target, const theme::label& label)
1419 {
1420  //log_scope("draw label");
1421 
1422  const color_t& RGB = label.font_rgb();
1423 
1424  std::string c_start="<";
1425  std::string c_sep=",";
1426  std::string c_end=">";
1427  std::stringstream color;
1428  color<< c_start << RGB.r << c_sep << RGB.g << c_sep << RGB.b << c_end;
1429  std::string text = label.text();
1430 
1431  if(label.font_rgb_set()) {
1432  color<<text;
1433  text = color.str();
1434  }
1435  const std::string& icon = label.icon();
1436  SDL_Rect& loc = label.location(video.screen_area());
1437 
1438  if(icon.empty() == false) {
1439  surface surf(image::get_image(icon));
1440  if(!surf.null()) {
1441  if(surf->w > loc.w || surf->h > loc.h) {
1442  surf.assign(scale_surface(surf,loc.w,loc.h));
1443  }
1444 
1445  sdl_blit(surf,nullptr,target,&loc);
1446  }
1447 
1448  if(text.empty() == false) {
1449  tooltips::add_tooltip(loc,text);
1450  }
1451  } else if(text.empty() == false) {
1452  font::draw_text(&video,loc,label.font_size(),font::NORMAL_COLOR,text,loc.x,loc.y);
1453  }
1454 
1455 }
1456 
1458 {
1459  surface& screen(screen_.getSurface());
1460 
1461  /*
1462  * The minimap is also a panel, force it to update its contents.
1463  * This is required when the size of the minimap has been modified.
1464  */
1466 
1467  for(const auto& panel : theme_.panels()) {
1468  draw_panel(video(), panel, menu_buttons_);
1469  }
1470 
1471  for(const auto& label : theme_.labels()) {
1472  draw_label(video(), screen, label);
1473  }
1474 
1475  render_buttons();
1476 }
1477 
1478 static void draw_background(surface screen, const SDL_Rect& area, const std::string& image)
1479 {
1480  // No background image, just fill in black.
1481  if(image.empty()) {
1482  sdl::fill_rectangle(area, color_t(0, 0, 0));
1483  return;
1484  }
1485 
1486  const surface background(image::get_image(image));
1487  if(background.null()) {
1488  return;
1489  }
1490 
1491  const unsigned int width = background->w;
1492  const unsigned int height = background->h;
1493 
1494  const unsigned int w_count = static_cast<int>(std::ceil(static_cast<double>(area.w) / static_cast<double>(width)));
1495  const unsigned int h_count = static_cast<int>(std::ceil(static_cast<double>(area.h) / static_cast<double>(height)));
1496 
1497  for(unsigned int w = 0, w_off = area.x; w < w_count; ++w, w_off += width) {
1498  for(unsigned int h = 0, h_off = area.y; h < h_count; ++h, h_off += height) {
1499  SDL_Rect clip = sdl::create_rect(w_off, h_off, 0, 0);
1500  sdl_blit(background, nullptr, screen, &clip);
1501  }
1502  }
1503 }
1504 
1506  const drawing_layer layer,
1507  const std::string& text,
1508  std::size_t font_size,
1509  color_t color,
1510  double x_in_hex,
1511  double y_in_hex)
1512 {
1513  if (text.empty()) return;
1514 
1515  const std::size_t font_sz = static_cast<std::size_t>(font_size * get_zoom_factor());
1516 
1517  surface text_surf = font::get_rendered_text(text, font_sz, color);
1518  surface back_surf = font::get_rendered_text(text, font_sz, font::BLACK_COLOR);
1519  const int x = get_location_x(loc) - text_surf->w/2
1520  + static_cast<int>(x_in_hex* hex_size());
1521  const int y = get_location_y(loc) - text_surf->h/2
1522  + static_cast<int>(y_in_hex* hex_size());
1523  for (int dy=-1; dy <= 1; ++dy) {
1524  for (int dx=-1; dx <= 1; ++dx) {
1525  if (dx!=0 || dy!=0) {
1526  drawing_buffer_add(layer, loc, x + dx, y + dy, back_surf);
1527  }
1528  }
1529  }
1530  drawing_buffer_add(layer, loc, x, y, text_surf);
1531 }
1532 
1533 //TODO: convert this to use sdl::ttexture
1535  const map_location& loc, surface image,
1536  bool hreverse, bool greyscale, fixed_t alpha,
1537  color_t blendto, double blend_ratio, double submerged, bool vreverse)
1538 {
1539  if (image==nullptr)
1540  return;
1541 
1542  SDL_Rect image_rect {x, y, image->w, image->h};
1543  SDL_Rect clip_rect = map_area();
1544  if (!sdl::rects_overlap(image_rect, clip_rect))
1545  return;
1546 
1547  surface surf(image);
1548 
1549  if(hreverse) {
1550  surf = image::reverse_image(surf);
1551  }
1552  if(vreverse) {
1553  surf = flop_surface(surf);
1554  }
1555 
1556  if(greyscale) {
1557  surf = greyscale_image(surf);
1558  }
1559 
1560  if(blend_ratio != 0) {
1561  surf = blend_surface(surf, blend_ratio, blendto);
1562  }
1563  if(alpha > ftofxp(1.0)) {
1564  surf = brighten_image(surf, alpha);
1565  //} else if(alpha != 1.0 && blendto != 0) {
1566  // surf.assign(blend_surface(surf,1.0-alpha,blendto));
1567  } else if(alpha != ftofxp(1.0)) {
1568  surface temp = make_neutral_surface(surf);
1569  adjust_surface_alpha(temp, alpha);
1570  surf = temp;
1571  }
1572 
1573  if(surf == nullptr) {
1574  ERR_DP << "surface lost..." << std::endl;
1575  return;
1576  }
1577 
1578  if(submerged > 0.0) {
1579  // divide the surface into 2 parts
1580  const int submerge_height = std::max<int>(0, surf->h*(1.0-submerged));
1581  const int depth = surf->h - submerge_height;
1582  SDL_Rect srcrect {0, 0, surf->w, submerge_height};
1583  drawing_buffer_add(drawing_layer, loc, x, y, surf, srcrect);
1584 
1585  if(submerge_height != surf->h) {
1586  //the lower part will be transparent
1587  float alpha_base = 0.3f; // 30% alpha at surface of water
1588  float alpha_delta = 0.015f; // lose 1.5% per pixel depth
1589  alpha_delta *= zoom_ / DefaultZoom; // adjust with zoom
1590  surf = submerge_alpha(surf, depth, alpha_base, alpha_delta);
1591 
1592  srcrect.y = submerge_height;
1593  srcrect.h = surf->h-submerge_height;
1594  y += submerge_height;
1595 
1596  drawing_buffer_add(drawing_layer, loc, x, y, surf, srcrect);
1597  }
1598  } else {
1599  // simple blit
1600  drawing_buffer_add(drawing_layer, loc, x, y, surf);
1601  }
1602 }
1604 {
1606  selectedHex_ = hex;
1609 }
1610 
1612 {
1614  mouseoverHex_ = hex;
1616 }
1617 
1618 void display::set_diagnostic(const std::string& msg)
1619 {
1620  if(diagnostic_label_ != 0) {
1622  diagnostic_label_ = 0;
1623  }
1624 
1625  if(!msg.empty()) {
1626  font::floating_label flabel(msg);
1628  flabel.set_color(font::YELLOW_COLOR);
1629  flabel.set_position(300, 50);
1630  flabel.set_clip_rect(map_outside_area());
1631 
1633  }
1634 }
1635 
1637 {
1638  if (get_map().empty()) {
1639  return;
1640  }
1641 
1642  if(benchmark) {
1643  invalidateAll_ = true;
1644  }
1645 
1646  if(!panelsDrawn_) {
1647  draw_all_panels();
1648  panelsDrawn_ = true;
1649  }
1650 
1651  if(redraw_background_) {
1652  // Full redraw of the background
1653  const SDL_Rect clip_rect = map_outside_area();
1654  const surface& screen = get_screen_surface();
1655  clip_rect_setter set_clip_rect(screen, &clip_rect);
1656  SDL_FillRect(screen, &clip_rect, 0x00000000);
1657  draw_background(screen, clip_rect, theme_.border().background_image);
1658  redraw_background_ = false;
1659 
1660  // Force a full map redraw
1661  invalidateAll_ = true;
1662  }
1663 
1664  if(invalidateAll_) {
1665  DBG_DP << "draw() with invalidateAll\n";
1666 
1667  // toggle invalidateAll_ first to allow regular invalidations
1668  invalidateAll_ = false;
1670 
1671  redrawMinimap_ = true;
1672  }
1673 }
1674 
1675 void display::draw_wrap(bool update, bool force)
1676 {
1677  static int time_between_draws = preferences::draw_delay();
1678  if(time_between_draws == 0) {
1679  time_between_draws = 1000 / screen_.current_refresh_rate();
1680  }
1681 
1682  if(redrawMinimap_) {
1683  redrawMinimap_ = false;
1684  draw_minimap();
1685  }
1686 
1687  if(update) {
1688  update_display();
1689 
1690  frametimes_.push_back(SDL_GetTicks() - last_frame_finished_);
1691  fps_counter_++;
1692  using std::chrono::duration_cast;
1693  using std::chrono::seconds;
1694  using std::chrono::steady_clock;
1695  const seconds current_second = duration_cast<seconds>(steady_clock::now().time_since_epoch());
1696  if(current_second != fps_start_) {
1697  fps_start_ = current_second;
1699  fps_counter_ = 0;
1700  }
1701  int longest_frame = *std::max_element(frametimes_.begin(), frametimes_.end());
1702  int wait_time = time_between_draws - longest_frame;
1703 
1704  if(!force && !benchmark && wait_time > 0) {
1705  // If it's not time yet to draw, delay until it is
1706  SDL_Delay(wait_time);
1707  }
1708 
1709  last_frame_finished_ = SDL_GetTicks();
1710  }
1711 }
1712 
1714 {
1715  for(auto i = action_buttons_.begin(); i != action_buttons_.end(); ++i) {
1716  if((*i)->pressed()) {
1717  const std::size_t index = std::distance(action_buttons_.begin(), i);
1718  if(index >= theme_.actions().size()) {
1719  assert(false);
1720  return nullptr;
1721  }
1722  return &theme_.actions()[index];
1723  }
1724  }
1725 
1726  return nullptr;
1727 }
1728 
1730 {
1731  for(auto i = menu_buttons_.begin(); i != menu_buttons_.end(); ++i) {
1732  if((*i)->pressed()) {
1733  const std::size_t index = std::distance(menu_buttons_.begin(), i);
1734  if(index >= theme_.menus().size()) {
1735  assert(false);
1736  return nullptr;
1737  }
1738  return theme_.get_menu_item((*i)->id());
1739  }
1740  }
1741 
1742  return nullptr;
1743 }
1744 
1745 void display::enable_menu(const std::string& item, bool enable)
1746 {
1747  for(auto menu = theme_.menus().begin(); menu != theme_.menus().end(); ++menu) {
1748 
1749  const auto hasitem = std::find_if(menu->items().begin(), menu->items().end(),
1750  [&item](const config& c) { return c["id"].str() == item; }
1751  );
1752 
1753  if(hasitem != menu->items().end()) {
1754  const std::size_t index = std::distance(theme_.menus().begin(), menu);
1755  if(index >= menu_buttons_.size()) {
1756  continue;
1757  }
1758  menu_buttons_[index]->enable(enable);
1759  }
1760  }
1761 }
1762 
1763 void display::announce(const std::string& message, const color_t& color, const announce_options& options)
1764 {
1765  if(options.discard_previous) {
1766  font::remove_floating_label(prevLabel);
1767  }
1768  font::floating_label flabel(message);
1770  flabel.set_color(color);
1772  flabel.set_lifetime(options.lifetime);
1773  flabel.set_clip_rect(map_outside_area());
1774 
1775  prevLabel = font::add_floating_label(flabel);
1776 }
1777 
1779 {
1780  const SDL_Rect& area = minimap_area();
1781 
1782  if(area.w == 0 || area.h == 0) {
1783  return;
1784  }
1785 
1786  if(minimap_ == nullptr || minimap_->w > area.w || minimap_->h > area.h) {
1787  minimap_ = image::getMinimap(area.w, area.h, get_map(),
1788  dc_->teams().empty() ? nullptr : &dc_->teams()[currentTeam_],
1789  (selectedHex_.valid() && !is_blindfolded()) ? &reach_map_ : nullptr);
1790  if(minimap_ == nullptr) {
1791  return;
1792  }
1793  }
1794 
1795  const surface& screen(screen_.getSurface());
1796  clip_rect_setter clip_setter(screen, &area);
1797 
1798  color_t back_color {31,31,23,SDL_ALPHA_OPAQUE};
1799  draw_centered_on_background(minimap_, area, back_color, screen);
1800 
1801  //update the minimap location for mouse and units functions
1802  minimap_location_.x = area.x + (area.w - minimap_->w) / 2;
1803  minimap_location_.y = area.y + (area.h - minimap_->h) / 2;
1804  minimap_location_.w = minimap_->w;
1805  minimap_location_.h = minimap_->h;
1806 
1808 
1809  // calculate the visible portion of the map:
1810  // scaling between minimap and full map images
1811  double xscaling = 1.0*minimap_->w / (get_map().w()*hex_width());
1812  double yscaling = 1.0*minimap_->h / (get_map().h()*hex_size());
1813 
1814  // we need to shift with the border size
1815  // and the 0.25 from the minimap balanced drawing
1816  // and the possible difference between real map and outside off-map
1817  SDL_Rect map_rect = map_area();
1818  SDL_Rect map_out_rect = map_outside_area();
1819  double border = theme_.border().size;
1820  double shift_x = - border*hex_width() - (map_out_rect.w - map_rect.w) / 2;
1821  double shift_y = - (border+0.25)*hex_size() - (map_out_rect.h - map_rect.h) / 2;
1822 
1823  int view_x = static_cast<int>((xpos_ + shift_x) * xscaling);
1824  int view_y = static_cast<int>((ypos_ + shift_y) * yscaling);
1825  int view_w = static_cast<int>(map_out_rect.w * xscaling);
1826  int view_h = static_cast<int>(map_out_rect.h * yscaling);
1827 
1828  SDL_Rect outline_rect {
1829  minimap_location_.x + view_x - 1,
1830  minimap_location_.y + view_y - 1,
1831  view_w + 2,
1832  view_h + 2
1833  };
1834 
1835  sdl::draw_rectangle(outline_rect, {255, 255, 255, 255});
1836 }
1837 
1839 {
1840  if (!preferences::minimap_draw_units() || is_blindfolded()) return;
1841 
1842  double xscaling = 1.0 * minimap_location_.w / get_map().w();
1843  double yscaling = 1.0 * minimap_location_.h / get_map().h();
1844 
1845  for(const auto& u : dc_->units()) {
1846  if (fogged(u.get_location()) ||
1847  (dc_->teams()[currentTeam_].is_enemy(u.side()) &&
1848  u.invisible(u.get_location())) ||
1849  u.get_hidden()) {
1850  continue;
1851  }
1852 
1853  int side = u.side();
1854  color_t col = team::get_minimap_color(side);
1855 
1857 
1858  if (dc_->teams()[currentTeam_].is_enemy(side)) {
1860  } else {
1861 
1862  if (currentTeam_ +1 == static_cast<unsigned>(side)) {
1863 
1864  if (u.movement_left() == u.total_movement())
1866  else if (u.movement_left() == 0)
1868  else
1870 
1871  } else
1873  }
1874  }
1875 
1876  double u_x = u.get_location().x * xscaling;
1877  double u_y = (u.get_location().y + (is_odd(u.get_location().x) ? 1 : -1)/4.0) * yscaling;
1878  // use 4/3 to compensate the horizontal hexes imbrication
1879  double u_w = 4.0 / 3.0 * xscaling;
1880  double u_h = yscaling;
1881 
1882  SDL_Rect r {
1883  minimap_location_.x + int(std::round(u_x))
1884  , minimap_location_.y + int(std::round(u_y))
1885  , int(std::round(u_w))
1886  , int(std::round(u_h))
1887  };
1888 
1889  sdl::fill_rectangle(r, col);
1890  }
1891 }
1892 
1893 bool display::scroll(int xmove, int ymove, bool force)
1894 {
1895  if(view_locked_ && !force) {
1896  return false;
1897  }
1898 
1899  // No move offset, do nothing.
1900  if(xmove == 0 && ymove == 0) {
1901  return false;
1902  }
1903 
1904  int new_x = xpos_ + xmove;
1905  int new_y = ypos_ + ymove;
1906 
1907  bounds_check_position(new_x, new_y);
1908 
1909  // Camera position doesn't change, exit.
1910  if(xpos_ == new_x && ypos_ == new_y) {
1911  return false;
1912  }
1913 
1914  const int diff_x = xpos_ - new_x;
1915  const int diff_y = ypos_ - new_y;
1916 
1917  xpos_ = new_x;
1918  ypos_ = new_y;
1919 
1920  /* Adjust floating label positions. This only affects labels whose position is anchored
1921  * to the map instead of the screen. In order to do that, we want to adjust their drawing
1922  * coordinates in the opposite direction of the screen scroll.
1923  *
1924  * The check a few lines up prevents any scrolling from happening if the camera position
1925  * doesn't change. Without that, the label still scroll even when the map edge is reached.
1926  * If that's removed, the following formula should work instead:
1927  *
1928  * const int label_[x,y]_adjust = [x,y]pos_ - new_[x,y];
1929  */
1930  font::scroll_floating_labels(diff_x, diff_y);
1931 
1933 
1934  //
1935  // NOTE: the next three blocks can be removed once we switch to accelerated rendering.
1936  //
1937 
1938  if(!screen_.update_locked()) {
1939  surface& screen(screen_.getSurface());
1940 
1941  SDL_Rect dstrect = map_area();
1942  dstrect.x += diff_x;
1943  dstrect.y += diff_y;
1944  dstrect = sdl::intersect_rects(dstrect, map_area());
1945 
1946  SDL_Rect srcrect = dstrect;
1947  srcrect.x -= diff_x;
1948  srcrect.y -= diff_y;
1949 
1950  // This is a workaround for a SDL2 bug when blitting on overlapping surfaces. The bug
1951  // only strikes during scrolling, but will then duplicate textures across the entire map.
1952  surface screen_copy = make_neutral_surface(screen);
1953 
1954  SDL_SetSurfaceBlendMode(screen_copy, SDL_BLENDMODE_NONE);
1955  SDL_BlitSurface(screen_copy, &srcrect, screen, &dstrect);
1956  }
1957 
1958  if(diff_y != 0) {
1959  SDL_Rect r = map_area();
1960 
1961  if(diff_y < 0) {
1962  r.y = r.y + r.h + diff_y;
1963  }
1964 
1965  r.h = std::abs(diff_y);
1967  }
1968 
1969  if(diff_x != 0) {
1970  SDL_Rect r = map_area();
1971 
1972  if(diff_x < 0) {
1973  r.x = r.x + r.w + diff_x;
1974  }
1975 
1976  r.w = std::abs(diff_x);
1978  }
1979 
1981 
1982  redrawMinimap_ = true;
1983 
1984  return true;
1985 }
1986 
1988 {
1989  return zoom_ == MaxZoom;
1990 }
1991 
1993 {
1994  return zoom_ == MinZoom;
1995 }
1996 
1997 bool display::set_zoom(bool increase)
1998 {
1999  // Ensure we don't try to access nonexistent vector indices.
2000  zoom_index_ = utils::clamp(increase ? zoom_index_ + 1 : zoom_index_ - 1, 0, final_zoom_index);
2001 
2002  // No validation check is needed in the next step since we've already set the index here and
2003  // know the new zoom value is indeed valid.
2004  return set_zoom(zoom_levels[zoom_index_], false);
2005 }
2006 
2007 bool display::set_zoom(unsigned int amount, const bool validate_value_and_set_index)
2008 {
2009  unsigned int new_zoom = utils::clamp(amount, MinZoom, MaxZoom);
2010 
2011  LOG_DP << "new_zoom = " << new_zoom << std::endl;
2012 
2013  if(new_zoom == zoom_) {
2014  return false;
2015  }
2016 
2017  // Confirm this is indeed a valid zoom level.
2018  if(validate_value_and_set_index) {
2019  auto iter = std::lower_bound(zoom_levels.begin(), zoom_levels.end(), new_zoom);
2020 
2021  if(iter == zoom_levels.end()) {
2022  // This should never happen, since the value was already clamped earlier
2023  return false;
2024  } else if(iter != zoom_levels.begin()) {
2025  float diff = *iter - *(iter - 1);
2026  float lower = (new_zoom - *(iter - 1)) / diff;
2027  float upper = (*iter - new_zoom) / diff;
2028  if(lower < upper) {
2029  // It's actually closer to the previous element.
2030  iter--;
2031  }
2032  }
2033 
2034  new_zoom = *iter;
2035  zoom_index_ = std::distance(zoom_levels.begin(), iter);
2036  }
2037 
2038  const SDL_Rect& area = map_area();
2039 
2040  // Turn the zoom factor to a double in order to avoid rounding errors.
2041  double zoom_factor = static_cast<double>(new_zoom) / static_cast<double>(zoom_);
2042 
2043  xpos_ = std::round(((xpos_ + area.w / 2) * zoom_factor) - (area.w / 2));
2044  ypos_ = std::round(((ypos_ + area.h / 2) * zoom_factor) - (area.h / 2));
2045 
2046  zoom_ = new_zoom;
2048  if(zoom_ != DefaultZoom) {
2049  last_zoom_ = zoom_;
2050  }
2051 
2053 
2055  redraw_background_ = true;
2056  invalidate_all();
2057 
2058  // Forces a redraw after zooming.
2059  // This prevents some graphic glitches from occurring.
2060  draw();
2061 
2062  return true;
2063 }
2064 
2066 {
2067  if (zoom_ != DefaultZoom) {
2068  last_zoom_ = zoom_;
2070  } else {
2071  // When we are already at the default zoom,
2072  // switch to the last zoom used
2074  }
2075 }
2076 
2078 {
2079  int x = get_location_x(loc);
2080  int y = get_location_y(loc);
2081  return !outside_area(map_area(), x, y);
2082 }
2083 
2085 {
2086  int x = get_location_x(loc);
2087  int y = get_location_y(loc);
2088  const SDL_Rect &area = map_area();
2089  int hw = hex_width(), hs = hex_size();
2090  return x + hs >= area.x - hw && x < area.x + area.w + hw &&
2091  y + hs >= area.y - hs && y < area.y + area.h + hs;
2092 }
2093 
2094 void display::scroll_to_xy(int screenxpos, int screenypos, SCROLL_TYPE scroll_type, bool force)
2095 {
2096  if(!force && (view_locked_ || !preferences::scroll_to_action())) return;
2097  if(screen_.update_locked()) {
2098  return;
2099  }
2100  const SDL_Rect area = map_area();
2101  const int xmove_expected = screenxpos - (area.x + area.w/2);
2102  const int ymove_expected = screenypos - (area.y + area.h/2);
2103 
2104  int xpos = xpos_ + xmove_expected;
2105  int ypos = ypos_ + ymove_expected;
2106  bounds_check_position(xpos, ypos);
2107  int xmove = xpos - xpos_;
2108  int ymove = ypos - ypos_;
2109 
2110  if(scroll_type == WARP || scroll_type == ONSCREEN_WARP || turbo_speed() > 2.0 || preferences::scroll_speed() > 99) {
2111  scroll(xmove,ymove,true);
2112  draw();
2113  return;
2114  }
2115 
2116  // Doing an animated scroll, with acceleration etc.
2117 
2118  int x_old = 0;
2119  int y_old = 0;
2120 
2121  const double dist_total = std::hypot(xmove, ymove);
2122  double dist_moved = 0.0;
2123 
2124  int t_prev = SDL_GetTicks();
2125 
2126  double velocity = 0.0;
2127  while (dist_moved < dist_total) {
2128  events::pump();
2129 
2130  int t = SDL_GetTicks();
2131  double dt = (t - t_prev) / 1000.0;
2132  if (dt > 0.200) {
2133  // Do not skip too many frames on slow PCs
2134  dt = 0.200;
2135  }
2136  t_prev = t;
2137 
2138  const double accel_time = 0.3 / turbo_speed(); // seconds until full speed is reached
2139  const double decel_time = 0.4 / turbo_speed(); // seconds from full speed to stop
2140 
2141  double velocity_max = preferences::scroll_speed() * 60.0;
2142  velocity_max *= turbo_speed();
2143  double accel = velocity_max / accel_time;
2144  double decel = velocity_max / decel_time;
2145 
2146  // If we started to decelerate now, where would we stop?
2147  double stop_time = velocity / decel;
2148  double dist_stop = dist_moved + velocity*stop_time - 0.5*decel*stop_time*stop_time;
2149  if (dist_stop > dist_total || velocity > velocity_max) {
2150  velocity -= decel * dt;
2151  if (velocity < 1.0) velocity = 1.0;
2152  } else {
2153  velocity += accel * dt;
2154  if (velocity > velocity_max) velocity = velocity_max;
2155  }
2156 
2157  dist_moved += velocity * dt;
2158  if (dist_moved > dist_total) dist_moved = dist_total;
2159 
2160  int x_new = std::round(xmove * dist_moved / dist_total);
2161  int y_new = std::round(ymove * dist_moved / dist_total);
2162 
2163  int dx = x_new - x_old;
2164  int dy = y_new - y_old;
2165 
2166  scroll(dx,dy,true);
2167  x_old += dx;
2168  y_old += dy;
2169  draw();
2170  }
2171 }
2172 
2173 void display::scroll_to_tile(const map_location& loc, SCROLL_TYPE scroll_type, bool check_fogged, bool force)
2174 {
2175  if(get_map().on_board(loc) == false) {
2176  ERR_DP << "Tile at " << loc << " isn't on the map, can't scroll to the tile." << std::endl;
2177  return;
2178  }
2179 
2180  std::vector<map_location> locs;
2181  locs.push_back(loc);
2182  scroll_to_tiles(locs, scroll_type, check_fogged,false,0.0,force);
2183 }
2184 
2186  SCROLL_TYPE scroll_type, bool check_fogged,
2187  double add_spacing, bool force)
2188 {
2189  std::vector<map_location> locs;
2190  locs.push_back(loc1);
2191  locs.push_back(loc2);
2192  scroll_to_tiles(locs, scroll_type, check_fogged, false, add_spacing,force);
2193 }
2194 
2195 void display::scroll_to_tiles(const std::vector<map_location>::const_iterator & begin,
2196  const std::vector<map_location>::const_iterator & end,
2197  SCROLL_TYPE scroll_type, bool check_fogged,
2198  bool only_if_possible, double add_spacing, bool force)
2199 {
2200  // basically we calculate the min/max coordinates we want to have on-screen
2201  int minx = 0;
2202  int maxx = 0;
2203  int miny = 0;
2204  int maxy = 0;
2205  bool valid = false;
2206 
2207  for(std::vector<map_location>::const_iterator itor = begin; itor != end ; ++itor) {
2208  if(get_map().on_board(*itor) == false) continue;
2209  if(check_fogged && fogged(*itor)) continue;
2210 
2211  int x = get_location_x(*itor);
2212  int y = get_location_y(*itor);
2213 
2214  if (!valid) {
2215  minx = x;
2216  maxx = x;
2217  miny = y;
2218  maxy = y;
2219  valid = true;
2220  } else {
2221  int minx_new = std::min<int>(minx,x);
2222  int miny_new = std::min<int>(miny,y);
2223  int maxx_new = std::max<int>(maxx,x);
2224  int maxy_new = std::max<int>(maxy,y);
2225  SDL_Rect r = map_area();
2226  r.x = minx_new;
2227  r.y = miny_new;
2228  if(outside_area(r, maxx_new, maxy_new)) {
2229  // we cannot fit all locations to the screen
2230  if (only_if_possible) return;
2231  break;
2232  }
2233  minx = minx_new;
2234  miny = miny_new;
2235  maxx = maxx_new;
2236  maxy = maxy_new;
2237  }
2238  }
2239  //if everything is fogged or the location list is empty
2240  if(!valid) return;
2241 
2242  if (scroll_type == ONSCREEN || scroll_type == ONSCREEN_WARP) {
2243  SDL_Rect r = map_area();
2244  int spacing = std::round(add_spacing * hex_size());
2245  r.x += spacing;
2246  r.y += spacing;
2247  r.w -= 2*spacing;
2248  r.h -= 2*spacing;
2249  if (!outside_area(r, minx,miny) && !outside_area(r, maxx,maxy)) {
2250  return;
2251  }
2252  }
2253 
2254  // let's do "normal" rectangle math from now on
2255  SDL_Rect locs_bbox;
2256  locs_bbox.x = minx;
2257  locs_bbox.y = miny;
2258  locs_bbox.w = maxx - minx + hex_size();
2259  locs_bbox.h = maxy - miny + hex_size();
2260 
2261  // target the center
2262  int target_x = locs_bbox.x + locs_bbox.w/2;
2263  int target_y = locs_bbox.y + locs_bbox.h/2;
2264 
2265  if (scroll_type == ONSCREEN || scroll_type == ONSCREEN_WARP) {
2266  // when doing an ONSCREEN scroll we do not center the target unless needed
2267  SDL_Rect r = map_area();
2268  int map_center_x = r.x + r.w/2;
2269  int map_center_y = r.y + r.h/2;
2270 
2271  int h = r.h;
2272  int w = r.w;
2273 
2274  // we do not want to be only inside the screen rect, but center a bit more
2275  double inside_frac = 0.5; // 0.0 = always center the target, 1.0 = scroll the minimum distance
2276  w = static_cast<int>(w * inside_frac);
2277  h = static_cast<int>(h * inside_frac);
2278 
2279  // shrink the rectangle by the size of the locations rectangle we found
2280  // such that the new task to fit a point into a rectangle instead of rectangle into rectangle
2281  w -= locs_bbox.w;
2282  h -= locs_bbox.h;
2283 
2284  if (w < 1) w = 1;
2285  if (h < 1) h = 1;
2286 
2287  r.x = target_x - w/2;
2288  r.y = target_y - h/2;
2289  r.w = w;
2290  r.h = h;
2291 
2292  // now any point within r is a possible target to scroll to
2293  // we take the one with the minimum distance to map_center
2294  // which will always be at the border of r
2295 
2296  if (map_center_x < r.x) {
2297  target_x = r.x;
2298  target_y = map_center_y;
2299  if (target_y < r.y) target_y = r.y;
2300  if (target_y > r.y+r.h-1) target_y = r.y+r.h-1;
2301  } else if (map_center_x > r.x+r.w-1) {
2302  target_x = r.x+r.w-1;
2303  target_y = map_center_y;
2304  if (target_y < r.y) target_y = r.y;
2305  if (target_y >= r.y+r.h) target_y = r.y+r.h-1;
2306  } else if (map_center_y < r.y) {
2307  target_y = r.y;
2308  target_x = map_center_x;
2309  if (target_x < r.x) target_x = r.x;
2310  if (target_x > r.x+r.w-1) target_x = r.x+r.w-1;
2311  } else if (map_center_y > r.y+r.h-1) {
2312  target_y = r.y+r.h-1;
2313  target_x = map_center_x;
2314  if (target_x < r.x) target_x = r.x;
2315  if (target_x > r.x+r.w-1) target_x = r.x+r.w-1;
2316  } else {
2317  ERR_DP << "Bug in the scrolling code? Looks like we would not need to scroll after all..." << std::endl;
2318  // keep the target at the center
2319  }
2320  }
2321 
2322  scroll_to_xy(target_x, target_y,scroll_type,force);
2323 }
2324 
2325 
2327 {
2328  const unsigned int orig_zoom = zoom_;
2329 
2330  if(zoom_ < MinZoom) {
2331  zoom_ = MinZoom;
2332  }
2333 
2334  if(zoom_ > MaxZoom) {
2335  zoom_ = MaxZoom;
2336  }
2337 
2339 
2340  if(zoom_ != orig_zoom) {
2342  }
2343 }
2344 
2345 void display::bounds_check_position(int& xpos, int& ypos) const
2346 {
2347  const int tile_width = hex_width();
2348 
2349  // Adjust for the border 2 times
2350  const int xend = static_cast<int>(tile_width * (get_map().w() + 2 * theme_.border().size) + tile_width/3);
2351  const int yend = static_cast<int>(zoom_ * (get_map().h() + 2 * theme_.border().size) + zoom_/2);
2352 
2353  if(xpos > xend - map_area().w) {
2354  xpos = xend - map_area().w;
2355  }
2356 
2357  if(ypos > yend - map_area().h) {
2358  ypos = yend - map_area().h;
2359  }
2360 
2361  if(xpos < 0) {
2362  xpos = 0;
2363  }
2364 
2365  if(ypos < 0) {
2366  ypos = 0;
2367  }
2368 }
2369 
2370 double display::turbo_speed() const
2371 {
2372  bool res = turbo_;
2373  if(keys_[SDLK_LSHIFT] || keys_[SDLK_RSHIFT]) {
2374  res = !res;
2375  }
2376 
2377  res |= screen_.faked();
2378  if (res)
2379  return turbo_speed_;
2380  else
2381  return 1.0;
2382 }
2383 
2385 {
2386  idle_anim_rate_ = std::pow(2.0, -rate/10.0);
2387 }
2388 
2390 {
2391  if(screen_.update_locked())
2392  return;
2393 
2394  invalidateGameStatus_ = true;
2395 
2396  reportRects_.clear();
2397  reportSurfaces_.clear();
2398  reports_.clear();
2399 
2401 
2403 
2405 
2406  if(!menu_buttons_.empty() || !action_buttons_.empty()) {
2407  create_buttons();
2408  }
2409 
2410  if(resources::controller) {
2412  if(command_executor != nullptr) {
2413  // This function adds button overlays,
2414  // it needs to be run after recreating the buttons.
2415  command_executor->set_button_state();
2416  }
2417  }
2418 
2419  panelsDrawn_ = false;
2420  if (!gui::in_dialog()) {
2422  }
2423 
2424  redraw_background_ = true;
2425 
2426  for(std::function<void(display&)> f : redraw_observers_) {
2427  f(*this);
2428  }
2429 
2430  int ticks1 = SDL_GetTicks();
2431  invalidate_all();
2432  int ticks2 = SDL_GetTicks();
2433  draw(true,true);
2434  int ticks3 = SDL_GetTicks();
2435  LOG_DP << "invalidate and draw: " << (ticks3 - ticks2) << " and " << (ticks2 - ticks1) << "\n";
2436 
2438 }
2439 
2440 void display::add_redraw_observer(std::function<void(display&)> f)
2441 {
2442  redraw_observers_.push_back(f);
2443 }
2444 
2446 {
2447  redraw_observers_.clear();
2448 }
2449 
2451  draw(true, false);
2452 }
2453 
2454 void display::draw(bool update) {
2455  draw(update, false);
2456 }
2457 
2458 
2459 void display::draw(bool update,bool force) {
2460 // log_scope("display::draw");
2461 
2462  if (screen_.update_locked()) {
2463  return;
2464  }
2465 
2466  if (dirty_) {
2467  flip_locker flip_lock(screen_);
2468  dirty_ = false;
2470  return;
2471  }
2472 
2473  // Trigger cache rebuild when preference gets changed
2476  builder_->rebuild_cache_all();
2477  }
2478 
2480 
2481  draw_init();
2482  pre_draw();
2483  // invalidate all that needs to be invalidated
2485 
2486  if(!get_map().empty()) {
2487  //int simulate_delay = 0;
2488 
2489  /*
2490  * draw_invalidated() also invalidates the halos, so also needs to be
2491  * ran if invalidated_.empty() == true.
2492  */
2493  if(!invalidated_.empty() || preferences::show_haloes()) {
2494  draw_invalidated();
2495  invalidated_.clear();
2496  }
2498  post_commit();
2499  draw_sidebar();
2500 
2501  // Simulate slow PC:
2502  //SDL_Delay(2*simulate_delay + rand() % 20);
2503  }
2504  draw_wrap(update, force);
2505  post_draw();
2506 }
2507 
2509 {
2510  return *map_labels_;
2511 }
2512 
2514 {
2515  return *map_labels_;
2516 }
2517 
2518 const SDL_Rect& display::get_clip_rect()
2519 {
2520  return map_area();
2521 }
2522 
2524 // log_scope("display::draw_invalidated");
2525  SDL_Rect clip_rect = get_clip_rect();
2526  surface& screen = get_screen_surface();
2527  clip_rect_setter set_clip_rect(screen, &clip_rect);
2528  for (const map_location& loc : invalidated_) {
2529  int xpos = get_location_x(loc);
2530  int ypos = get_location_y(loc);
2531 
2532  //const bool on_map = get_map().on_board(loc);
2533  SDL_Rect hex_rect = sdl::create_rect(xpos, ypos, zoom_, zoom_);
2534  if(!sdl::rects_overlap(hex_rect,clip_rect)) {
2535  continue;
2536  }
2537  draw_hex(loc);
2538  drawn_hexes_+=1;
2539  }
2540  invalidated_hexes_ += invalidated_.size();
2541 
2542  if (dc_->teams().empty())
2543  {
2544  // The unit drawer can't function without teams
2545  return;
2546  }
2547 
2548  unit_drawer drawer = unit_drawer(*this);
2549 
2550  for (const map_location& loc : invalidated_) {
2551  unit_map::const_iterator u_it = dc_->units().find(loc);
2553  if (u_it != dc_->units().end()
2554  && (request == exclusive_unit_draw_requests_.end() || request->second == u_it->id()))
2555  drawer.redraw_unit(*u_it);
2556  }
2557 
2558 }
2559 
2561  int xpos = get_location_x(loc);
2562  int ypos = get_location_y(loc);
2563  image::TYPE image_type = get_image_type(loc);
2564  const bool on_map = get_map().on_board(loc);
2565  const time_of_day& tod = get_time_of_day(loc);
2566 
2567  int num_images_fg = 0;
2568  int num_images_bg = 0;
2569 
2570  if(!shrouded(loc)) {
2571  // unshrouded terrain (the normal case)
2572  get_terrain_images(loc, tod.id, BACKGROUND); // updates terrain_image_vector_
2574  num_images_bg = terrain_image_vector_.size();
2575 
2576  get_terrain_images(loc, tod.id, FOREGROUND); // updates terrain_image_vector_
2578  num_images_fg = terrain_image_vector_.size();
2579 
2580  // Draw the grid, if that's been enabled
2581  if(grid_) {
2583  drawing_buffer_add(LAYER_GRID_TOP, loc, xpos, ypos,
2586  drawing_buffer_add(LAYER_GRID_BOTTOM, loc, xpos, ypos,
2587  image::get_image(grid_bottom, image::TOD_COLORED));
2588  }
2589  }
2590 
2591  if(!shrouded(loc)) {
2592  typedef overlay_map::const_iterator Itor;
2593  std::pair<Itor,Itor> overlays = get_overlays().equal_range(loc);
2594  const bool have_overlays = overlays.first != overlays.second;
2595 
2596  if(have_overlays) {
2597  tod_color tod_col = tod.color + color_adjust_;
2598  image::light_string lt = image::get_light_string(-1, tod_col.r, tod_col.g, tod_col.b);
2599 
2600  for( ; overlays.first != overlays.second; ++overlays.first) {
2601  const std::string& current_team_name = get_teams()[viewing_team()].team_name();
2602  const std::vector<std::string>& current_team_names = utils::split(current_team_name);
2603  const std::vector<std::string>& team_names = utils::split(overlays.first->second.team_name);
2604  if ((overlays.first->second.team_name.empty() ||
2605  std::find_first_of(team_names.begin(), team_names.end(),
2606  current_team_names.begin(), current_team_names.end()) != team_names.end())
2607  && !(fogged(loc) && !overlays.first->second.visible_in_fog))
2608  {
2609 
2610  const std::string image = overlays.first->second.image;
2611  const surface surf = image.find("~NO_TOD_SHIFT()") == std::string::npos ?
2613  drawing_buffer_add(LAYER_TERRAIN_BG, loc, xpos, ypos, surf);
2614  }
2615  }
2616  }
2617  }
2618 
2619  if(!shrouded(loc)) {
2620  // village-control flags.
2621  drawing_buffer_add(LAYER_TERRAIN_BG, loc, xpos, ypos, get_flag(loc));
2622  }
2623 
2624  // Draw the time-of-day mask on top of the terrain in the hex.
2625  // tod may differ from tod if hex is illuminated.
2626  const std::string& tod_hex_mask = tod.image_mask;
2627  if(tod_hex_mask1 != nullptr || tod_hex_mask2 != nullptr) {
2630  } else if(!tod_hex_mask.empty()) {
2631  drawing_buffer_add(LAYER_TERRAIN_FG, loc, xpos, ypos,
2632  image::get_image(tod_hex_mask,image::SCALED_TO_HEX));
2633  }
2634 
2635  // Paint mouseover overlays
2636  if(loc == mouseoverHex_ && (on_map || (in_editor() && get_map().on_board_with_border(loc)))
2637  && mouseover_hex_overlay_ != nullptr) {
2639  }
2640 
2641  // Paint arrows
2642  arrows_map_t::const_iterator arrows_in_hex = arrows_map_.find(loc);
2643  if(arrows_in_hex != arrows_map_.end()) {
2644  for (arrow* const a : arrows_in_hex->second) {
2645  a->draw_hex(loc);
2646  }
2647  }
2648 
2649  // Apply shroud, fog and linger overlay
2650 
2651  if(shrouded(loc)) {
2652  // We apply void also on off-map tiles
2653  // to shroud the half-hexes too
2654  const std::string& shroud_image = get_variant(shroud_images_, loc);
2655  drawing_buffer_add(LAYER_FOG_SHROUD, loc, xpos, ypos,
2656  image::get_image(shroud_image, image_type));
2657  } else if(fogged(loc)) {
2658  const std::string& fog_image = get_variant(fog_images_, loc);
2659  drawing_buffer_add(LAYER_FOG_SHROUD, loc, xpos, ypos,
2660  image::get_image(fog_image, image_type));
2661  }
2662 
2663  if(!shrouded(loc)) {
2664  drawing_buffer_add(LAYER_FOG_SHROUD, loc, xpos, ypos, get_fog_shroud_images(loc, image_type));
2665  }
2666 
2667  if (on_map) {
2668  if (draw_coordinates_) {
2669  int off_x = xpos + hex_size()/2;
2670  int off_y = ypos + hex_size()/2;
2671  surface text = font::get_rendered_text(lexical_cast<std::string>(loc), font::SIZE_SMALL, font::NORMAL_COLOR);
2672  surface bg = create_neutral_surface(text->w, text->h);
2673  SDL_Rect bg_rect {0, 0, text->w, text->h};
2674  sdl::fill_surface_rect(bg, &bg_rect, 0xaa000000);
2675  off_x -= text->w / 2;
2676  off_y -= text->h / 2;
2677  if (draw_terrain_codes_) {
2678  off_y -= text->h / 2;
2679  }
2680  if (draw_num_of_bitmaps_) {
2681  off_y -= text->h / 2;
2682  }
2683  drawing_buffer_add(LAYER_FOG_SHROUD, loc, off_x, off_y, bg);
2684  drawing_buffer_add(LAYER_FOG_SHROUD, loc, off_x, off_y, text);
2685  }
2686  if (draw_terrain_codes_ && (game_config::debug || !shrouded(loc))) {
2687  int off_x = xpos + hex_size()/2;
2688  int off_y = ypos + hex_size()/2;
2689  surface text = font::get_rendered_text(lexical_cast<std::string>(get_map().get_terrain(loc)), font::SIZE_SMALL, font::NORMAL_COLOR);
2690  surface bg = create_neutral_surface(text->w, text->h);
2691  SDL_Rect bg_rect {0, 0, text->w, text->h};
2692  sdl::fill_surface_rect(bg, &bg_rect, 0xaa000000);
2693  off_x -= text->w / 2;
2694  off_y -= text->h / 2;
2696  off_y += text->h / 2;
2697  } else if (draw_num_of_bitmaps_ && !draw_coordinates_) {
2698  off_y -= text->h / 2;
2699  }
2700  drawing_buffer_add(LAYER_FOG_SHROUD, loc, off_x, off_y, bg);
2701  drawing_buffer_add(LAYER_FOG_SHROUD, loc, off_x, off_y, text);
2702  }
2703  if (draw_num_of_bitmaps_) {
2704  int off_x = xpos + hex_size()/2;
2705  int off_y = ypos + hex_size()/2;
2706  surface text = font::get_rendered_text(lexical_cast<std::string>(num_images_bg + num_images_fg), font::SIZE_SMALL, font::NORMAL_COLOR);
2707  surface bg = create_neutral_surface(text->w, text->h);
2708  SDL_Rect bg_rect {0, 0, text->w, text->h};
2709  sdl::fill_surface_rect(bg, &bg_rect, 0xaa000000);
2710  off_x -= text->w / 2;
2711  off_y -= text->h / 2;
2712  if (draw_coordinates_) {
2713  off_y += text->h / 2;
2714  }
2715  if (draw_terrain_codes_) {
2716  off_y += text->h / 2;
2717  }
2718  drawing_buffer_add(LAYER_FOG_SHROUD, loc, off_x, off_y, bg);
2719  drawing_buffer_add(LAYER_FOG_SHROUD, loc, off_x, off_y, text);
2720  }
2721  }
2722 
2723  if(debug_foreground) {
2724  drawing_buffer_add(LAYER_UNIT_DEFAULT, loc, xpos, ypos,
2725  image::get_image("terrain/foreground.png", image_type));
2726  }
2727 
2728 }
2729 
2731  return image::TOD_COLORED;
2732 }
2733 
2734 /*void display::draw_sidebar() {
2735 
2736 }*/
2737 
2738 void display::draw_image_for_report(surface& img, SDL_Rect& rect)
2739 {
2740  SDL_Rect visible_area = get_non_transparent_portion(img);
2741  SDL_Rect target = rect;
2742  if(visible_area.x != 0 || visible_area.y != 0 || visible_area.w != img->w || visible_area.h != img->h) {
2743  if(visible_area.w == 0 || visible_area.h == 0) {
2744  return;
2745  }
2746 
2747  if(visible_area.w > rect.w || visible_area.h > rect.h) {
2748  img.assign(get_surface_portion(img,visible_area));
2749  img.assign(scale_surface(img,rect.w,rect.h));
2750  visible_area.x = 0;
2751  visible_area.y = 0;
2752  visible_area.w = img->w;
2753  visible_area.h = img->h;
2754  } else {
2755  target.x = rect.x + (rect.w - visible_area.w)/2;
2756  target.y = rect.y + (rect.h - visible_area.h)/2;
2757  target.w = visible_area.w;
2758  target.h = visible_area.h;
2759  }
2760 
2761  sdl_blit(img,&visible_area,screen_.getSurface(),&target);
2762  } else {
2763  if(img->w != rect.w || img->h != rect.h) {
2764  img.assign(scale_surface(img,rect.w,rect.h));
2765  }
2766 
2767  sdl_blit(img,nullptr,screen_.getSurface(),&target);
2768  }
2769 }
2770 
2771 /**
2772  * Redraws the specified report (if anything has changed).
2773  * If a config is not supplied, it will be generated via
2774  * reports::generate_report().
2775  */
2776 void display::refresh_report(const std::string& report_name, const config * new_cfg)
2777 {
2778  const theme::status_item *item = theme_.get_status_item(report_name);
2779  if (!item) {
2780  reportSurfaces_[report_name].assign(nullptr);
2781  return;
2782  }
2783 
2784  // Now we will need the config. Generate one if needed.
2785 
2786  boost::optional <events::mouse_handler &> mhb = boost::none;
2787 
2788  if (resources::controller) {
2790  }
2791 
2792  reports::context temp_context = reports::context(*dc_, *this, *resources::tod_manager, wb_.lock(), mhb);
2793 
2794  const config generated_cfg = new_cfg ? config() : reports_object_->generate_report(report_name, temp_context);
2795  if ( new_cfg == nullptr )
2796  new_cfg = &generated_cfg;
2797 
2798  SDL_Rect &rect = reportRects_[report_name];
2799  const SDL_Rect &new_rect = item->location(screen_.screen_area());
2800  surface &surf = reportSurfaces_[report_name];
2801  config &report = reports_[report_name];
2802 
2803  // Report and its location is unchanged since last time. Do nothing.
2804  if (surf && rect == new_rect && report == *new_cfg) {
2805  return;
2806  }
2807 
2808  // Update the config in reports_.
2809  report = *new_cfg;
2810 
2811  if (surf) {
2812  sdl_blit(surf, nullptr, screen_.getSurface(), &rect);
2813  }
2814 
2815  // If the rectangle has just changed, assign the surface to it
2816  if (!surf || new_rect != rect)
2817  {
2818  surf.assign(nullptr);
2819  rect = new_rect;
2820 
2821  // If the rectangle is present, and we are blitting text,
2822  // then we need to backup the surface.
2823  // (Images generally won't need backing up,
2824  // unless they are transparent, but that is done later).
2825  if (rect.w > 0 && rect.h > 0) {
2827  if (reportSurfaces_[report_name] == nullptr) {
2828  ERR_DP << "Could not backup background for report!" << std::endl;
2829  }
2830  }
2831  }
2832 
2834 
2835  if (report.empty()) return;
2836 
2837  int x = rect.x, y = rect.y;
2838 
2839  // Add prefix, postfix elements.
2840  // Make sure that they get the same tooltip
2841  // as the guys around them.
2842  std::string str = item->prefix();
2843  if (!str.empty()) {
2844  config &e = report.add_child_at("element", config(), 0);
2845  e["text"] = str;
2846  e["tooltip"] = report.child("element")["tooltip"];
2847  }
2848  str = item->postfix();
2849  if (!str.empty()) {
2850  config &e = report.add_child("element");
2851  e["text"] = str;
2852  e["tooltip"] = report.child("element", -1)["tooltip"];
2853  }
2854 
2855  // Loop through and display each report element.
2856  int tallest = 0;
2857  int image_count = 0;
2858  bool used_ellipsis = false;
2859  std::ostringstream ellipsis_tooltip;
2860  SDL_Rect ellipsis_area = rect;
2861 
2862  for (config::const_child_itors elements = report.child_range("element");
2863  elements.begin() != elements.end(); elements.pop_front())
2864  {
2865  SDL_Rect area {x, y, rect.w + rect.x - x, rect.h + rect.y - y};
2866  if (area.h <= 0) break;
2867 
2868  std::string t = elements.front()["text"];
2869  if (!t.empty())
2870  {
2871  if (used_ellipsis) goto skip_element;
2872 
2873  // Draw a text element.
2874  font::pango_text text;
2875  if (item->font_rgb_set()) {
2876  text.set_foreground_color(item->font_rgb());
2877  }
2878  bool eol = false;
2879  if (t[t.size() - 1] == '\n') {
2880  eol = true;
2881  t = t.substr(0, t.size() - 1);
2882  }
2883  text.set_font_size(item->font_size());
2884  text.set_text(t, true);
2885  text.set_maximum_width(area.w);
2886  text.set_maximum_height(area.h, false);
2887  surface s = text.render();
2888 
2889  // check if next element is text with almost no space to show it
2890  const int minimal_text = 12; // width in pixels
2891  config::const_child_iterator ee = elements.begin();
2892  if (!eol && rect.w - (x - rect.x + s->w) < minimal_text &&
2893  ++ee != elements.end() && !(*ee)["text"].empty())
2894  {
2895  // make this element longer to trigger rendering of ellipsis
2896  // (to indicate that next elements have not enough space)
2897  //NOTE this space should be longer than minimal_text pixels
2898  t = t + " ";
2899  text.set_text(t, true);
2900  s = text.render();
2901  // use the area of this element for next tooltips
2902  used_ellipsis = true;
2903  ellipsis_area.x = x;
2904  ellipsis_area.y = y;
2905  ellipsis_area.w = s->w;
2906  ellipsis_area.h = s->h;
2907  }
2908 
2909  screen_.blit_surface(x, y, s);
2910  area.w = s->w;
2911  area.h = s->h;
2912  if (area.h > tallest) {
2913  tallest = area.h;
2914  }
2915  if (eol) {
2916  x = rect.x;
2917  y += tallest;
2918  tallest = 0;
2919  } else {
2920  x += area.w;
2921  }
2922  }
2923  else if (!(t = elements.front()["image"].str()).empty())
2924  {
2925  if (used_ellipsis) goto skip_element;
2926 
2927  // Draw an image element.
2928  surface img(image::get_image(t));
2929 
2930  if (!img) {
2931  ERR_DP << "could not find image for report: '" << t << "'" << std::endl;
2932  continue;
2933  }
2934 
2935  if (area.w < img->w && image_count) {
2936  // We have more than one image, and this one doesn't fit.
2938  used_ellipsis = true;
2939  }
2940 
2941  if (img->w < area.w) area.w = img->w;
2942  if (img->h < area.h) area.h = img->h;
2943  draw_image_for_report(img, area);
2944 
2945  ++image_count;
2946  if (area.h > tallest) {
2947  tallest = area.h;
2948  }
2949 
2950  if (!used_ellipsis) {
2951  x += area.w;
2952  } else {
2953  ellipsis_area = area;
2954  }
2955  }
2956  else
2957  {
2958  // No text nor image, skip this element
2959  continue;
2960  }
2961 
2962  skip_element:
2963  t = elements.front()["tooltip"].t_str().base_str();
2964  if (!t.empty()) {
2965  if (!used_ellipsis) {
2966  tooltips::add_tooltip(area, t, elements.front()["help"].t_str().base_str());
2967  } else {
2968  // Collect all tooltips for the ellipsis.
2969  // TODO: need a better separator
2970  // TODO: assign an action
2971  ellipsis_tooltip << t;
2972  config::const_child_iterator ee = elements.begin();
2973  if (++ee != elements.end())
2974  ellipsis_tooltip << "\n _________\n\n";
2975  }
2976  }
2977  }
2978 
2979  if (used_ellipsis) {
2980  tooltips::add_tooltip(ellipsis_area, ellipsis_tooltip.str());
2981  }
2982 }
2983 
2985 {
2986  DBG_DP << "invalidate_all()\n";
2987  invalidateAll_ = true;
2988 #ifdef _OPENMP
2989 #pragma omp critical(invalidated_)
2990 #endif //_OPENMP
2991  invalidated_.clear();
2992 }
2993 
2995 {
2996  if(invalidateAll_)
2997  return false;
2998 
2999  bool tmp;
3000 #ifdef _OPENMP
3001 #pragma omp critical(invalidated_)
3002 #endif //_OPENMP
3003  tmp = invalidated_.insert(loc).second;
3004  return tmp;
3005 }
3006 
3007 bool display::invalidate(const std::set<map_location>& locs)
3008 {
3009  if(invalidateAll_)
3010  return false;
3011  bool ret = false;
3012  for (const map_location& loc : locs) {
3013 #ifdef _OPENMP
3014 #pragma omp critical(invalidated_)
3015 #endif //_OPENMP
3016  ret = invalidated_.insert(loc).second || ret;
3017  }
3018  return ret;
3019 }
3020 
3021 bool display::propagate_invalidation(const std::set<map_location>& locs)
3022 {
3023  if(invalidateAll_)
3024  return false;
3025 
3026  if(locs.size()<=1)
3027  return false; // propagation never needed
3028 
3029  bool result = false;
3030 #ifdef _OPENMP
3031 #pragma omp critical(invalidated_)
3032 #endif //_OPENMP
3033  {
3034  // search the first hex invalidated (if any)
3035  std::set<map_location>::const_iterator i = locs.begin();
3036  for(; i != locs.end() && invalidated_.count(*i) == 0 ; ++i) {}
3037 
3038  if (i != locs.end()) {
3039 
3040  // propagate invalidation
3041  // 'i' is already in, but I suspect that splitting the range is bad
3042  // especially because locs are often adjacents
3043  size_t previous_size = invalidated_.size();
3044  invalidated_.insert(locs.begin(), locs.end());
3045  result = previous_size < invalidated_.size();
3046  }
3047  }
3048  return result;
3049 }
3050 
3052 {
3054 }
3055 
3056 bool display::invalidate_locations_in_rect(const SDL_Rect& rect)
3057 {
3058  if(invalidateAll_)
3059  return false;
3060 
3061  bool result = false;
3062  for (const map_location &loc : hexes_under_rect(rect)) {
3063  result |= invalidate(loc);
3064  }
3065  return result;
3066 }
3067 
3069  if (get_map().is_village(loc)) {
3070  const int owner = dc_->village_owner(loc);
3071  if (owner >= 0 && flags_[owner].need_update()
3072  && (!fogged(loc) || !dc_->teams()[currentTeam_].is_enemy(owner+1))) {
3073  invalidate(loc);
3074  }
3075  }
3076 }
3077 
3079 {
3082  if (animate_map_) {
3083  for (const map_location &loc : get_visible_hexes())
3084  {
3085  if (shrouded(loc)) continue;
3086  if (builder_->update_animation(loc)) {
3087  invalidate(loc);
3088  } else {
3090  }
3091  }
3092  }
3093 
3094  for(const unit& u : dc_->units()) {
3095  u.anim_comp().refresh();
3096  }
3097  for (const unit* u : *fake_unit_man_) {
3098  u->anim_comp().refresh();
3099  }
3100 
3101 
3102  bool new_inval;
3103  do {
3104  new_inval = false;
3105  for (const unit & u : dc_->units()) {
3106  new_inval |= u.anim_comp().invalidate(*this);
3107  }
3108  for (const unit* u : *fake_unit_man_) {
3109  new_inval |= u->anim_comp().invalidate(*this);
3110  }
3111  } while (new_inval);
3112 }
3113 
3115 {
3116  for(const unit & u : dc_->units()) {
3117  u.anim_comp().set_standing();
3118  }
3119 }
3120 
3122 {
3123  const arrow_path_t & arrow_path = arrow.get_path();
3124  for (const map_location& loc : arrow_path)
3125  {
3126  arrows_map_[loc].push_back(&arrow);
3127  }
3128 }
3129 
3131 {
3132  const arrow_path_t & arrow_path = arrow.get_path();
3133  for (const map_location& loc : arrow_path)
3134  {
3135  arrows_map_[loc].remove(&arrow);
3136  }
3137 }
3138 
3140 {
3141  const arrow_path_t & previous_path = arrow.get_previous_path();
3142  for (const map_location& loc : previous_path)
3143  {
3144  arrows_map_[loc].remove(&arrow);
3145  }
3146  const arrow_path_t & arrow_path = arrow.get_path();
3147  for (const map_location& loc : arrow_path)
3148  {
3149  arrows_map_[loc].push_back(&arrow);
3150  }
3151 }
3152 
3154 {
3155  const SDL_Rect& rect = map_area();
3156  return pixel_position_to_hex(xpos_ + rect.x + rect.w / 2 , ypos_ + rect.y + rect.h / 2 );
3157 }
3158 
3159 void display::write(config& cfg) const
3160 {
3161  cfg["view_locked"] = view_locked_;
3162  cfg["color_adjust_red"] = color_adjust_.r;
3163  cfg["color_adjust_green"] = color_adjust_.g;
3164  cfg["color_adjust_blue"] = color_adjust_.b;
3165  get_middle_location().write(cfg.add_child("location"));
3166 }
3167 
3168 void display::read(const config& cfg)
3169 {
3170  view_locked_ = cfg["view_locked"].to_bool(false);
3171  color_adjust_.r = cfg["color_adjust_red"].to_int(0);
3172  color_adjust_.g = cfg["color_adjust_green"].to_int(0);
3173  color_adjust_.b = cfg["color_adjust_blue"].to_int(0);
3174 }
3175 
3177 {
3178  if (!reach_map_changed_) return;
3179  if (reach_map_.empty() != reach_map_old_.empty()) {
3180  // Invalidate everything except the non-darkened tiles
3181  reach_map &full = reach_map_.empty() ? reach_map_old_ : reach_map_;
3182 
3183  for (const auto& hex : get_visible_hexes()) {
3184  reach_map::iterator reach = full.find(hex);
3185  if (reach == full.end()) {
3186  // Location needs to be darkened or brightened
3187  invalidate(hex);
3188  } else if (reach->second != 1) {
3189  // Number needs to be displayed or cleared
3190  invalidate(hex);
3191  }
3192  }
3193  } else if (!reach_map_.empty()) {
3194  // Invalidate only changes
3195  reach_map::iterator reach, reach_old;
3196  for (reach = reach_map_.begin(); reach != reach_map_.end(); ++reach) {
3197  reach_old = reach_map_old_.find(reach->first);
3198  if (reach_old == reach_map_old_.end()) {
3199  invalidate(reach->first);
3200  } else {
3201  if (reach_old->second != reach->second) {
3202  invalidate(reach->first);
3203  }
3204  reach_map_old_.erase(reach_old);
3205  }
3206  }
3207  for (reach_old = reach_map_old_.begin(); reach_old != reach_map_old_.end(); ++reach_old) {
3208  invalidate(reach_old->first);
3209  }
3210  }
3212  reach_map_changed_ = false;
3213 }
3214 
3215 void display::handle_window_event(const SDL_Event& event) {
3216  if (event.type == SDL_WINDOWEVENT) {
3217  switch (event.window.event) {
3218  case SDL_WINDOWEVENT_RESIZED:
3219  case SDL_WINDOWEVENT_RESTORED:
3220  case SDL_WINDOWEVENT_EXPOSED:
3221  dirty_ = true;
3222 
3223  break;
3224  }
3225  }
3226 
3227 
3228 }
3229 
3230 void display::handle_event(const SDL_Event& event) {
3232  return;
3233  }
3234  if (event.type == DRAW_ALL_EVENT) {
3235  draw();
3236  }
3237 }
3238 
3239 display *display::singleton_ = nullptr;
constexpr const T & clamp(const T &value, const T &min, const T &max)
Definition: general.hpp:31
std::shared_ptr< gui::button > find_action_button(const std::string &id)
Retrieves a pointer to a theme UI button.
Definition: display.cpp:817
std::size_t font_size() const
Definition: theme.hpp:108
play_controller * controller
Definition: resources.cpp:21
TYPE
UNSCALED : image will be drawn "as is" without changing size, even in case of redraw SCALED_TO_ZOOM :...
Definition: picture.hpp:190
void drawing_buffer_commit()
Draws the drawing_buffer_ and clears it.
Definition: display.cpp:1270
int village_owner(const map_location &loc) const
Given the location of a village, will return the 0-based index of the team that currently owns it...
void draw_minimap_units()
Definition: display.cpp:1838
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: picture.cpp:1022
int zoom_index_
Definition: display.hpp:739
void raise_volatile_undraw_event()
Definition: events.cpp:704
std::string unmoved_color()
Definition: general.cpp:345
void recalculate_shroud()
Definition: label.cpp:277
virtual void pre_draw()
Called near the beginning of each draw() call.
Definition: display.hpp:669
bool discard_previous
An announcement according these options should replace the previous announce (typical of fast announc...
Definition: display.hpp:597
SDL_Rect & location(const SDL_Rect &screen) const
Definition: theme.cpp:312
surface create_neutral_surface(int w, int h)
Definition: utils.cpp:84
draw_layering(const bool auto_join=true)
Definition: video.cpp:49
const std::string & icon() const
Definition: theme.hpp:104
bool is_odd(T num)
Definition: math.hpp:34
virtual void select_hex(map_location hex)
Definition: display.cpp:1603
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:423
::tod_manager * tod_manager
Definition: resources.cpp:29
Arrows destined to be drawn on the map.
int h() const
Effective map height.
Definition: map.hpp:116
reports * reports_object_
Definition: display.hpp:755
unit_iterator end()
Definition: map.hpp:415
bool update_locked() const
Whether the screen has been &#39;locked&#39; or not.
Definition: video.cpp:357
bool minimap_draw_units()
Definition: general.cpp:925
void write(config &cfg) const
Definition: display.cpp:3159
virtual void handle_window_event(const SDL_Event &event)
Definition: display.cpp:3215
hotkey::command_executor * get_hotkey_command_executor() override
Optionally get a command executor to handle context menu events.
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:87
bool show_fps()
Definition: general.cpp:965
const team & get_team(int side) const
void invalidate_game_status()
Function to invalidate the game status displayed on the sidebar.
Definition: display.hpp:290
Small struct to store and manipulate ToD color adjusts.
Definition: time_of_day.hpp:28
void adjust_color_overlay(int r, int g, int b)
Add r,g,b to the colors for all images displayed on the map.
Definition: display.cpp:454
const arrow_path_t & get_path() const
Definition: arrow.cpp:120
#define DefaultZoom
Definition: display.cpp:75
std::string image_mask
The image that is to be laid over all images while this time of day lasts.
Definition: time_of_day.hpp:97
void get_adjacent_tiles(const map_location &a, map_location *res)
Function which, given a location, will place all adjacent locations in res.
Definition: location.cpp:517
void invalidate_animations()
Function to invalidate animated terrains and units which may have changed.
Definition: display.cpp:3078
#define ERR_DP
Definition: display.cpp:68
virtual void draw_sidebar()
Called near the end of a draw operation, derived classes can use this to render a specific sidebar...
Definition: display.hpp:712
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:2994
color_t font_rgb() const
Definition: theme.hpp:109
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:254
void draw_wrap(bool update, bool force)
Definition: display.cpp:1675
void flip()
Definition: display.cpp:1323
This class represents a single unit of a specific type.
Definition: unit.hpp:99
bool animate_water_
Local version of preferences::animate_water, used to detect when it&#39;s changed.
Definition: display.hpp:793
virtual void notify_observers()
tod_color color
The color modifications that should be made to the game board to reflect the time of day...
static void toggle_benchmark()
Toggle to continuously redraw the screen.
Definition: display.cpp:1313
CKey keys_
Definition: display.hpp:787
bool set_text(const std::string &text, const bool markedup)
Sets the text to render.
Definition: text.cpp:284
void set_clip_rect(const SDL_Rect &r)
bool set_zoom(bool increase)
Zooms the display in (true) or out (false).
Definition: display.cpp:1997
void change_display_context(const display_context *dc)
Definition: display.cpp:499
const map_location hex_clicked_on(int x, int y) const
given x,y co-ordinates of an onscreen pixel, will return the location of the hex that this pixel corr...
Definition: display.cpp:583
boost::circular_buffer< unsigned > frametimes_
Definition: display.hpp:766
bool rects_overlap(const SDL_Rect &rect1, const SDL_Rect &rect2)
Tests whether two rectangles overlap.
Definition: rect.cpp:33
int ypos_
Definition: display.hpp:735
void set_team_colors(const std::vector< std::string > *colors)
set the team colors used by the TC image modification use a vector with one string for each team usin...
Definition: picture.cpp:861
bool reach_map_changed_
Definition: display.hpp:1026
static double get_zoom_factor()
Returns the current zoom factor.
Definition: display.hpp:257
drawing_buffer drawing_buffer_
Definition: display.hpp:968
void set_playing_team(std::size_t team)
set_playing_team sets the team whose turn it currently is
Definition: display.cpp:405
surface reverse_image(const surface &surf)
function to reverse an image.
Definition: picture.cpp:1195
void draw_init()
Initiate a redraw.
Definition: display.cpp:1636
void set_default_zoom()
Sets the zoom amount to the default.
Definition: display.cpp:2065
void set_idle_anim_rate(int rate)
Definition: display.cpp:2384
const map_location pixel_position_to_hex(int x, int y) const
given x,y co-ordinates of a pixel on the map, will return the location of the hex that this pixel cor...
Definition: display.cpp:598
void scroll_floating_labels(double xmove, double ymove)
moves all floating labels that have &#39;scroll_mode&#39; set to ANCHOR_LABEL_MAP
#define a
int draw_delay()
Definition: general.cpp:975
Definition: video.hpp:31
bool minimap_movement_coding()
Definition: general.cpp:905
std::map< std::string, SDL_Rect > reportRects_
Definition: display.hpp:773
void draw_all_panels()
redraw all panels associated with the map display
Definition: display.cpp:1457
void render_image(int x, int y, const display::drawing_layer drawing_layer, const map_location &loc, surface image, bool hreverse=false, bool greyscale=false, fixed_t alpha=ftofxp(1.0), color_t blendto={0, 0, 0}, double blend_ratio=0, double submerged=0.0, bool vreverse=false)
Draw an image at a certain location.
Definition: display.cpp:1534
bool in_dialog()
Definition: show_dialog.cpp:56
virtual void draw_invalidated()
Only called when there&#39;s actual redrawing to do.
Definition: display.cpp:2523
map_location mouseoverHex_
Definition: display.hpp:786
void flip()
Renders the screen.
Definition: video.cpp:337
void init_flags()
Init the flag list and the team colors used by ~TC.
Definition: display.cpp:286
Manages a list of fake units for the display object.
Mouseover overlay used by editor.
Definition: display.hpp:820
virtual void draw_hex(const map_location &hex)
Definition: arrow.cpp:136
child_itors child_range(config_key_type key)
Definition: config.cpp:366
static void draw_background(surface screen, const SDL_Rect &area, const std::string &image)
Definition: display.cpp:1478
std::string id
Definition: time_of_day.hpp:91
void lock_updates(bool value)
Stop the screen being redrawn.
Definition: video.cpp:348
bool draw_coordinates_
Debug flag - overlay x,y coords on tiles.
Definition: display.hpp:1048
const SDL_Rect & map_area() const
Returns the area used for the map.
Definition: display.cpp:546
int scroll_speed()
Definition: general.cpp:861
void remove_floating_label(int handle)
removes the floating label given by &#39;handle&#39; from the screen
void redraw_everything()
Invalidates entire screen, including all tiles and sidebar.
Definition: display.cpp:2389
static lg::log_domain log_display("display")
int fps_handle_
Handle for the label which displays frames per second.
Definition: display.hpp:1035
bool show_everything() const
Definition: display.hpp:89
The class terrain_builder is constructed from a config object, and a gamemap object.
Definition: builder.hpp:45
map_location minimap_location_on(int x, int y)
given x,y co-ordinates of the mouse, will return the location of the hex in the minimap that the mous...
Definition: display.cpp:744
void fill_surface_rect(surface &dst, SDL_Rect *dst_rect, const uint32_t color)
Fill a rectangle on a given surface.
Definition: rect.hpp:114
void reset_halo_manager()
Definition: display.cpp:505
void add_frame(int duration, const T &value, bool force_change=false)
Adds a frame to an animation.
int viewing_side() const
Definition: display.hpp:102
reach_map reach_map_
Definition: display.hpp:1024
bool non_interactive() const
Definition: video.cpp:135
SDL_Rect intersect_rects(const SDL_Rect &rect1, const SDL_Rect &rect2)
Calculates the intersection of two rectangles.
Definition: rect.cpp:39
#define h
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
iterator & operator++()
increment y first, then when reaching bottom, increment x
Definition: display.cpp:650
std::vector< std::function< void(display &)> > redraw_observers_
Definition: display.hpp:1045
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:361
#define MaxZoom
Definition: display.cpp:78
map_location selectedHex_
Definition: display.hpp:785
void set_font_size(int font_size)
const std::vector< std::string > items
CVideo & screen_
Definition: display.hpp:732
surface scale_surface(const surface &surf, int w, int h)
Scale a surface using alpha-weighted modified bilinear filtering Note: causes artifacts with alpha gr...
Definition: utils.cpp:274
static display * singleton_
Definition: display.hpp:1064
void blit_surface(int x, int y, surface surf, SDL_Rect *srcrect=nullptr, SDL_Rect *clip_rect=nullptr)
Draws a surface directly onto the screen framebuffer.
Definition: video.cpp:163
#define d
virtual void draw()
Draws invalidated items.
Definition: display.cpp:2450
surface blend_surface(const surface &surf, const double amount, const color_t color)
Blends a surface with a color.
Definition: utils.cpp:1804
bool animate_water()
Definition: general.cpp:900
const int SIZE_PLUS
Definition: constants.cpp:26
Top half part of grid image.
Definition: display.hpp:819
surface get_surface_portion(const surface &src, SDL_Rect &area)
Get a portion of the screen.
Definition: utils.cpp:2273
void enable_menu(const std::string &item, bool enable)
Finds the menu which has a given item in it, and enables or disables it.
Definition: display.cpp:1745
Rectangular area of hexes, allowing to decide how the top and bottom edges handles the vertical shift...
Definition: display.hpp:300
an object to leave the synced context during draw or unsynced wml items when we don’t know whether w...
surface getMinimap(int w, int h, const gamemap &map, const team *vw, const std::map< map_location, unsigned int > *reach_map, bool ignore_terrain_disabled)
function to create the minimap for a given map the surface returned must be freed by the user ...
Definition: minimap.cpp:39
const std::vector< label > & labels() const
Definition: theme.hpp:245
-file sdl_utils.hpp
static unsigned int zoom_
Definition: display.hpp:738
void clear_redraw_observers()
Clear the redraw observers.
Definition: display.cpp:2445
const SDL_Rect & minimap_area() const
mapx is the width of the portion of the display which shows the game area.
Definition: display.hpp:213
drawing_layer
The layers to render something on.
Definition: display.hpp:814
virtual const gamemap & map() const =0
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
std::vector< surface > get_fog_shroud_images(const map_location &loc, image::TYPE image_type)
Definition: display.cpp:953
void reset_standing_animations()
Definition: display.cpp:3114
void remove_arrow(arrow &)
Definition: display.cpp:3130
surface & getSurface()
Returns a reference to the framebuffer.
Definition: video.cpp:472
bool panelsDrawn_
Definition: display.hpp:750
#define DBG_DP
Definition: display.cpp:70
void draw_centered_on_background(surface surf, const SDL_Rect &rect, const color_t &color, surface target)
Definition: utils.cpp:2388
surface tod_hex_mask2
Definition: display.hpp:781
SDL_Rect get_non_transparent_portion(const surface &surf)
Definition: utils.cpp:2319
void init_flags_for_side_internal(std::size_t side, const std::string &side_color)
Definition: display.cpp:313
void update_display()
Copy the backbuffer to the framebuffer.
Definition: display.cpp:1346
std::vector< map_location > arrow_path_t
Definition: arrow.hpp:24
bool null() const
Definition: surface.hpp:79
Unit and team statistics.
very simple iterator to walk into the rect_of_hexes
Definition: display.hpp:307
iterator begin() const
Definition: display.cpp:663
const rect_of_hexes get_visible_hexes() const
Returns the rectangular area of visible hexes.
Definition: display.hpp:337
surface submerge_alpha(const surface &surf, int depth, float alpha_base, float alpha_delta)
Progressively reduce alpha of bottom part of the surface.
Definition: utils.cpp:1336
std::vector< std::shared_ptr< gui::button > > action_buttons_
Definition: display.hpp:776
map_location loc_
surface map_screenshot_surf_
Definition: display.hpp:1043
void clear_tooltips()
Definition: tooltips.cpp:120
virtual bool in_editor() const
Definition: display.hpp:201
bool team_valid() const
Definition: display.cpp:719
#define b
const theme::action * action_pressed()
Definition: display.cpp:1713
bool show_haloes()
Definition: game.cpp:860
const int SIZE_XLARGE
Definition: constants.cpp:29
const std::string & text() const
Definition: theme.hpp:102
std::vector< std::string > shroud_images_
Definition: display.hpp:783
void draw_image_for_report(surface &img, SDL_Rect &rect)
Definition: display.cpp:2738
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
bool exists(const image::locator &i_locator)
returns true if the given image actually exists, without loading it.
Definition: picture.cpp:1217
void blindfold(bool flag)
Definition: display.cpp:515
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
events::generic_event complete_redraw_event_
notify observers that the screen has been redrawn completely atm this is used for replay_controller t...
Definition: display.hpp:764
void parse_team_overlays()
Check the overlay_map for proper team-specific overlays to be displayed/hidden.
Definition: display.cpp:91
const config & options()
Definition: game.cpp:568
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
surface flop_surface(const surface &surf)
Definition: utils.cpp:2070
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
const std::string & id() const
Gets this unit&#39;s id.
Definition: unit.hpp:305
uint32_t last_frame_finished_
Definition: display.hpp:770
void adjust_surface_alpha(surface &surf, fixed_t amount)
Definition: utils.cpp:1174
void get_terrain_images(const map_location &loc, const std::string &timeid, TERRAIN_TYPE terrain_type)
Definition: display.cpp:1042
const arrow_path_t & get_previous_path() const
Definition: arrow.cpp:125
double turbo_speed() const
Definition: display.cpp:2370
static bool zoom_at_max()
Definition: display.cpp:1987
void draw_minimap()
Definition: display.cpp:1778
light_string get_light_string(int op, int r, int g, int b)
return light_string of one light operation(see above)
Definition: picture.cpp:714
const std::string & prefix() const
Definition: theme.hpp:126
void new_animation_frame()
Definition: animated.cpp:30
static unsigned calculate_fps(unsigned frametime)
Definition: display.cpp:1341
static bool outside_area(const SDL_Rect &area, const int x, const int y)
Check if the bbox of the hex at x,y has pixels outside the area rectangle.
Definition: display.cpp:574
bool tile_fully_on_screen(const map_location &loc) const
Check if a tile is fully visible on screen.
Definition: display.cpp:2077
const theme::menu * menu_pressed()
Definition: display.cpp:1729
Arrows destined to be drawn on the map.
Definition: arrow.hpp:29
surface screenshot(bool map_screenshot=false)
Capture a (map-)screenshot into a surface.
Definition: display.cpp:774
bool empty() const
Tell if the map is of 0 size.
Definition: map.hpp:174
std::string theme()
Definition: game.cpp:818
void set_lifetime(int lifetime)
bool font_rgb_set() const
Definition: theme.hpp:110
bool invalidate_visible_locations_in_rect(const SDL_Rect &rect)
Definition: display.cpp:3051
void recalculate_minimap()
Schedule the minimap for recalculation.
Definition: display.hpp:615
void scroll_to_xy(int screenxpos, int screenypos, SCROLL_TYPE scroll_type, bool force=true)
Definition: display.cpp:2094
config & add_child_at(config_key_type key, const config &val, unsigned index)
Definition: config.cpp:511
bool valid() const
Definition: location.hpp:93
bool map_screenshot_
Used to indicate to drawing functions that we are doing a map screenshot.
Definition: display.hpp:1009
void draw_rectangle(const SDL_Rect &rect, const color_t &color)
Draw a rectangle outline.
Definition: rect.cpp:57
arrows_map_t arrows_map_
Maps the list of arrows for each location.
Definition: display.hpp:1057
virtual image::TYPE get_image_type(const map_location &loc)
Definition: display.cpp:2730
bool idle_anim() const
Definition: display.hpp:491
const std::unique_ptr< map_labels > map_labels_
Definition: display.hpp:754
int xpos_
Definition: display.hpp:735
config generate_report(const std::string &name, context &ct, bool only_static=false)
Definition: reports.cpp:1580
pango_text & set_font_size(const unsigned font_size)
Definition: text.cpp:333
void update_tod(const time_of_day *tod_override=nullptr)
Applies r,g,b coloring to the map.
Definition: display.cpp:443
void render_buttons()
Definition: display.cpp:923
bool dirty_
Definition: display.hpp:1061
std::string background_image
Definition: theme.hpp:86
bool idle_anim_
Definition: display.hpp:1040
SDL_Rect minimap_location_
Definition: display.hpp:744
double size
Definition: theme.hpp:84
void set_zoom(unsigned int amount)
sets the amount scaled images should be scaled.
Definition: picture.cpp:875
TERRAIN_TYPE
Used as a parameter for the get_terrain_at function.
Definition: builder.hpp:49
display(const display_context *dc, std::weak_ptr< wb::manager > wb, reports &reports_object, const config &theme_cfg, const config &level, bool auto_join=true)
Definition: display.cpp:158
std::string grid_bottom
tod_color color_adjust_
Definition: display.hpp:1059
uint8_t r
Red value.
Definition: color.hpp:177
fake_unit_manager * fake_units
Definition: resources.cpp:30
bool is_enemy(int n) const
Definition: team.hpp:243
void draw_text_in_hex(const map_location &loc, const drawing_layer layer, const std::string &text, std::size_t font_size, color_t color, double x_in_hex=0.5, double y_in_hex=0.5)
Draw text on a hex.
Definition: display.cpp:1505
void bounds_check_position()
Definition: display.cpp:2326
virtual void post_draw()
Called at the very end of each draw() call.
Definition: display.hpp:676
Modify, read and display user preferences.
bool font_rgb_set() const
Definition: theme.hpp:134
bool invalidateGameStatus_
Definition: display.hpp:753
#define MinZoom
Definition: display.cpp:77
void set_position(double xpos, double ypos)
void update_arrow(arrow &a)
Called by arrow objects when they change.
Definition: display.cpp:3139
map_display and display: classes which take care of displaying the map and game-data on the screen...
unsigned int fps_actual_
Definition: display.hpp:769
virtual const unit_map & units() const =0
bool animate_map()
Definition: general.cpp:895
std::string shroud_prefix
Definition: game_config.cpp:71
void create_buttons()
Definition: display.cpp:864
const std::string & get_id() const
Definition: theme.hpp:49
std::array< map_location, 6 > adjacent_loc_array_t
Definition: location.hpp:170
const color_t YELLOW_COLOR
static const std::string & get_direction(std::size_t n)
Definition: display.cpp:947
const rect_of_hexes hexes_under_rect(const SDL_Rect &r) const
Return the rectangular area of hexes overlapped by r (r is in screen coordinates) ...
Definition: display.cpp:672
void invalidate_all()
Function to invalidate all tiles.
Definition: display.cpp:2984
void layout_buttons()
Definition: display.cpp:837
int invalidated_hexes_
Count work done for the debug info displayed under fps.
Definition: display.hpp:1037
bool draw_terrain_codes_
Debug flag - overlay terrain codes on tiles.
Definition: display.hpp:1050
int32_t fixed_t
Definition: math.hpp:292
void pump()
Definition: events.cpp:425
const color_t NORMAL_COLOR
bool fogged(const map_location &loc) const
Returns true if location (x,y) is covered in fog.
Definition: display.cpp:729
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:22
static int hex_width()
Function which returns the width of a hex in pixels, up to where the next hex starts.
Definition: display.hpp:248
lu_byte right
Definition: lparser.cpp:1027
std::string flag_rgb
static unsigned int last_zoom_
Definition: display.hpp:740
Fog and shroud.
Definition: display.hpp:843
void set_color_adjustment(int r, int g, int b)
will make all scaled images have these rgb values added to all their pixels.
Definition: picture.cpp:847
std::unique_ptr< halo::manager > halo_man_
Definition: display.hpp:656
#define ftofxp(x)
IN: float or int - OUT: fixed_t.
Definition: math.hpp:297
int idle_anim_rate()
Definition: general.cpp:467
Encapsulates the map of the game.
Definition: location.hpp:42
void set_color(const color_t &color)
void set_team(const team *)
Definition: label.cpp:138
bool shrouded(const map_location &loc) const
Returns true if location (x,y) is covered in shroud.
Definition: display.cpp:724
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:238
#define DRAW_ALL_EVENT
Definition: events.hpp:29
void reinit_flags_for_side(std::size_t side)
Rebuild the flag list (not team colors) for a single side.
Definition: display.cpp:303
unit_iterator find(std::size_t id)
Definition: map.cpp:311
void process_reachmap_changes()
Definition: display.cpp:3176
bool faked() const
Definition: video.hpp:60
const border_t & border() const
Definition: theme.hpp:275
Definition: video.cpp:45
void recalculate_labels()
Definition: label.cpp:244
int w() const
Effective map width.
Definition: map.hpp:113
virtual void handle_event(const SDL_Event &)
Definition: display.cpp:3230
surface & get_screen_surface()
return the screen surface or the surface used for map_screenshot.
Definition: display.hpp:198
surface tod_hex_mask1
Definition: display.hpp:781
const color_t BLACK_COLOR
virtual overlay_map & get_overlays()=0
std::string allied_color()
Definition: general.cpp:305
std::size_t i
Definition: function.cpp:933
virtual const std::vector< team > & teams() const =0
int current_refresh_rate() const
Definition: video.hpp:147
bool is_blindfolded() const
Definition: display.cpp:523
bool show_border
Definition: theme.hpp:89
void scroll_to_tile(const map_location &loc, SCROLL_TYPE scroll_type=ONSCREEN, bool check_fogged=true, bool force=true)
Scroll such that location loc is on-screen.
Definition: display.cpp:2173
void announce(const std::string &msg, const color_t &color=font::GOOD_COLOR, const announce_options &options=announce_options())
Announce a message prominently.
Definition: display.cpp:1763
virtual void highlight_hex(map_location hex)
Definition: display.cpp:1611
Holds options for calls to function &#39;announce&#39; (announce).
Definition: display.hpp:587
std::size_t activeTeam_
Definition: display.hpp:885
const std::string & postfix() const
Definition: theme.hpp:127
std::vector< std::shared_ptr< gui::button > > menu_buttons_
Definition: display.hpp:776
std::string moved_color()
Definition: general.cpp:335
void set_diagnostic(const std::string &msg)
Definition: display.cpp:1618
void scroll_to_tiles(map_location loc1, map_location loc2, SCROLL_TYPE scroll_type=ONSCREEN, bool check_fogged=true, double add_spacing=0.0, bool force=true)
Scroll such that location loc1 is on-screen.
Definition: display.cpp:2185
void invalidate_animations_location(const map_location &loc)
Per-location invalidation called by invalidate_animations() Extra game per-location invalidation (vil...
Definition: display.cpp:3068
default layer for drawing units
Definition: display.hpp:825
map_location get_middle_location() const
Definition: display.cpp:3153
static map_location::DIRECTION s
std::vector< std::string > names
Definition: build_info.cpp:54
unsigned int fps_counter_
Definition: display.hpp:767
virtual ~display()
Definition: display.cpp:267
double g
Definition: astarsearch.cpp:64
color_t font_rgb() const
Definition: theme.hpp:133
std::size_t playing_team() const
The playing team is the team whose turn it is.
Definition: display.hpp:96
bool draw_num_of_bitmaps_
Debug flag - overlay number of bitmaps on tiles.
Definition: display.hpp:1052
bool redraw_background_
Definition: display.hpp:746
void read(const config &cfg)
Definition: display.cpp:3168
bool dont_show_all_
Definition: display.hpp:734
int get_location_y(const map_location &loc) const
Definition: display.cpp:739
std::basic_string< signed char > light_string
light_string store colors info of central and adjacent hexes.
Definition: picture.hpp:144
const SDL_Rect & map_outside_area() const
Returns the available area for a map, this may differ from the above.
Definition: display.hpp:236
Helper structure for rendering the terrains.
Definition: display.hpp:928
void undraw_floating_labels(surface screen)
void raise_volatile_draw_event()
Definition: events.cpp:686
surface get_rendered_text(const std::string &str, int size, const color_t &color, int style)
Definition: sdl_ttf.cpp:333
int add_floating_label(const floating_label &flabel)
add a label floating on the screen above everything else.
const std::string & image() const
Definition: theme.hpp:151
bool tile_nearly_on_screen(const map_location &loc) const
Checks if location loc or one of the adjacent tiles is visible on screen.
Definition: display.cpp:2084
bool invalidate_locations_in_rect(const SDL_Rect &rect)
invalidate all hexes under the rectangle rect (in screen coordinates)
Definition: display.cpp:3056
Layer for the terrain drawn in front of the unit.
Definition: display.hpp:826
bool add_exclusive_draw(const map_location &loc, unit &unit)
Allows a unit to request to be the only one drawn in its hex.
Definition: display.cpp:412
virtual void draw_hex(const map_location &loc)
Redraws a single gamemap location.
Definition: display.cpp:2560
Represents terrains which are to be drawn in front of them.
Definition: builder.hpp:54
static bool zoom_at_min()
Definition: display.cpp:1992
int w
const menu * get_menu_item(const std::string &key) const
Definition: theme.cpp:904
Used for the bottom half part of grid image.
Definition: display.hpp:830
theme theme_
Definition: display.hpp:737
virtual const SDL_Rect & get_clip_rect()
Get the clipping rectangle for drawing.
Definition: display.cpp:2518
const bool & debug
bool view_locked_
Definition: display.hpp:736
surface mouseover_hex_overlay_
Definition: display.hpp:778
const std::vector< action > & actions() const
Definition: theme.hpp:248
Definitions for the terrain builder.
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
surface greyscale_image(const surface &surf)
Definition: utils.cpp:728
Represents terrains which are to be drawn behind unit sprites.
Definition: builder.hpp:50
Layer for the terrain drawn behind the unit.
Definition: display.hpp:815
config & add_child(config_key_type key)
Definition: config.cpp:479
std::vector< std::string > fog_images_
Definition: display.hpp:782
Text class.
Definition: text.hpp:74
std::chrono::seconds fps_start_
Definition: display.hpp:768
std::vector< animated< image::locator > > imagelist
A shorthand typedef for a list of animated image locators, the base data type returned by the get_ter...
Definition: builder.hpp:72
void set_team(std::size_t team, bool observe=false)
Sets the team controlled by the player using the computer.
Definition: display.cpp:386
surface get_lighted_image(const image::locator &i_locator, const light_string &ls, TYPE type)
function to get the surface corresponding to an image.
Definition: picture.cpp:1095
const status_item * get_status_item(const std::string &item) const
Definition: theme.cpp:863
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
Creates an SDL_Rect with the given dimensions.
Definition: rect.hpp:39
std::map< map_location, unsigned int > reach_map
Definition: display.hpp:1023
bool redrawMinimap_
Definition: display.hpp:745
void add_overlay(const map_location &loc, const std::string &image, const std::string &halo="", const std::string &team_name="", const std::string &item_id="", bool visible_under_fog=true)
Functions to add and remove overlays from locations.
Definition: display.cpp:109
bool set_resolution(const SDL_Rect &screen)
Definition: theme.cpp:586
std::string partial_color()
Definition: general.cpp:355
Definition: display.hpp:44
void drawing_buffer_add(const drawing_layer layer, const map_location &loc, int x, int y, const surface &surf, const SDL_Rect &clip=SDL_Rect())
Add an item to the drawing buffer.
Definition: display.cpp:1191
pango_text & set_maximum_width(int width)
Definition: text.cpp:366
void assign(SDL_Surface *surf)
Definition: surface.hpp:46
const std::vector< team > & get_teams() const
Definition: display.hpp:93
static color_t get_minimap_color(int side)
Definition: team.cpp:945
reach_map reach_map_old_
Definition: display.hpp:1025
int get_location_x(const map_location &loc) const
Functions to get the on-screen positions of hexes.
Definition: display.cpp:734
const gamemap & get_map() const
Definition: display.hpp:91
events::mouse_handler & get_mouse_handler_base() override
Get a reference to a mouse handler member a derived class uses.
virtual void post_commit()
Hook for actions to take right after draw() calls drawing_buffer_commit No action here by default...
Definition: display.hpp:696
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:661
bool invalidateAll_
Definition: display.hpp:747
void redraw_unit(const unit &u) const
draw a unit.
Definition: drawer.cpp:54
events::generic_event scroll_event_
Event raised when the map is being scrolled.
Definition: display.hpp:758
iterator end() const
Definition: display.cpp:667
#define f
int lifetime
Lifetime measured in frames.
Definition: display.hpp:590
double t
Definition: astarsearch.cpp:64
void reload_map()
Updates internals that cache map size.
Definition: display.cpp:493
color_t rep() const
High-contrast shade, intended for the minimap markers.
Definition: color_range.hpp:96
constexpr const SDL_Rect empty_rect
Definition: rect.hpp:31
std::map< std::string, surface > reportSurfaces_
Definition: display.hpp:774
std::set< map_location > invalidated_
Definition: display.hpp:777
bool find(E event, F functor)
Tests whether an event handler is available.
unsigned int tile_size
Definition: game_config.cpp:68
lu_byte left
Definition: lparser.cpp:1026
void drawing_buffer_clear()
Clears the drawing buffer.
Definition: display.cpp:1308
std::map< std::string, config > reports_
Definition: display.hpp:775
const display_context * dc_
Definition: display.hpp:655
surface brighten_image(const surface &surf, fixed_t amount)
Definition: utils.cpp:1132
std::size_t viewing_team() const
The viewing team is the team currently viewing the game.
Definition: display.hpp:101
surface tile_surface(const surface &surf, int w, int h, bool centered)
Tile a surface.
Definition: utils.cpp:639
SDL_Rect draw_text(surface &dst, const SDL_Rect &area, int size, const color_t &color, const std::string &txt, int x, int y, bool use_tooltips, int style)
Function to draw text on a surface.
surface make_neutral_surface(const surface &surf)
Definition: utils.cpp:72
const color_range & color_info(const std::string &name)
std::vector< surface > terrain_image_vector_
Definition: display.hpp:805
this module manages the cache of images.
double idle_anim_rate_
Definition: display.hpp:1041
void fill_rectangle(const SDL_Rect &rect, const color_t &color)
Draws a filled rectangle.
Definition: rect.cpp:65
int drawn_hexes_
Definition: display.hpp:1038
Standard logging facilities (interface).
double turbo_speed_
Definition: display.hpp:751
static gui::button::TYPE string_to_button_type(std::string type)
Definition: display.cpp:937
int diagnostic_label_
Definition: display.hpp:749
void add_redraw_observer(std::function< void(display &)> f)
Adds a redraw observer, a function object to be called when redraw_everything is used.
Definition: display.cpp:2440
CVideo & video()
Gets the underlying screen object.
Definition: display.hpp:195
surface create_compatible_surface(const surface &surf, int width, int height)
Definition: utils.cpp:2099
std::string ellipsis
std::size_t font_size() const
Definition: theme.hpp:132
EXIT_STATUS start(const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
Definition: editor_main.cpp:28
static void draw_label(CVideo &video, surface target, const theme::label &label)
Definition: display.cpp:1418
bool scroll_to_action()
Definition: general.cpp:365
map_labels & labels()
Definition: display.cpp:2508
const std::vector< menu > & menus() const
Definition: theme.hpp:246
#define e
static void fill_images_list(const std::string &prefix, std::vector< std::string > &images)
Definition: display.cpp:460
const std::unique_ptr< fake_unit_manager > fake_unit_man_
Definition: display.hpp:741
#define SmallZoom
Definition: display.cpp:76
#define final_zoom_index
Definition: display.cpp:74
std::vector< animated< image::locator > > flags_
Animated flags for each team.
Definition: display.hpp:801
const config & child_or_empty(config_key_type key) const
Returns the first child with the given key, or an empty config if there is none.
Definition: config.cpp:456
surface & render()
Returns the rendered text.
Definition: text.cpp:91
pango_text & set_maximum_height(int height, bool multiline)
Definition: text.cpp:393
#define zoom_levels
Definition: display.cpp:73
std::string team_name
Definition: overlay.hpp:37
void sdl_blit(const surface &src, SDL_Rect *src_rect, surface &dst, SDL_Rect *dst_rect)
Definition: utils.hpp:33
static void draw_panel(CVideo &video, const theme::panel &panel, std::vector< std::shared_ptr< gui::button >> &)
Definition: display.cpp:1397
uint8_t g
Green value.
Definition: color.hpp:180
uint8_t b
Blue value.
Definition: color.hpp:183
virtual const time_of_day & get_time_of_day(const map_location &loc=map_location::null_location()) const
Definition: display.cpp:437
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
mock_char c
void remove_single_overlay(const map_location &loc, const std::string &toDelete)
remove_single_overlay will remove a single overlay from a tile
Definition: display.cpp:138
SDL_Rect screen_area(bool as_pixels=true) const
Returns the current window renderer area, either in pixels or screen coordinates. ...
Definition: video.cpp:299
surface get_flag(const map_location &loc)
Definition: display.cpp:366
pango_text & set_foreground_color(const color_t &color)
Definition: text.cpp:356
const int font_size
Definition: button.cpp:40
static map_location::DIRECTION n
std::shared_ptr< halo_record > handle
Definition: halo.hpp:31
int add_tooltip(const SDL_Rect &rect, const std::string &message, const std::string &action, bool use_markup, const surface &foreground)
Definition: tooltips.cpp:175
bool scroll(int xmov, int ymov, bool force=false)
Scrolls the display by xmov,ymov pixels.
Definition: display.cpp:1893
bool turbo_
Definition: display.hpp:752
void add_arrow(arrow &)
Definition: display.cpp:3121
void remove_overlay(const map_location &loc)
remove_overlay will remove all overlays on a tile.
Definition: display.cpp:122
bool animate_map_
Local cache for preferences::animate_map, since it is constantly queried.
Definition: display.hpp:790
const SDL_Rect & max_map_area() const
Returns the maximum area used for the map regardless to resolution and view size. ...
Definition: display.cpp:529
static rng & default_instance()
Definition: random.cpp:73
#define LOG_DP
Definition: display.cpp:69
const std::vector< panel > & panels() const
Definition: theme.hpp:244
static void toggle_debug_foreground()
Toggle to debug foreground terrain.
Definition: display.cpp:1318
std::string grid_top
const std::unique_ptr< terrain_builder > builder_
Definition: display.hpp:742
static const std::string & get_variant(const std::vector< std::string > &variants, const map_location &loc)
Definition: display.cpp:482
void invalidate_theme()
Definition: display.hpp:401
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
std::string fog_prefix
Definition: game_config.cpp:71
bool empty() const
Definition: config.cpp:886
void refresh_report(const std::string &report_name, const config *new_cfg=nullptr)
Redraws the specified report (if anything has changed).
Definition: display.cpp:2776
std::string enemy_color()
Definition: general.cpp:325
std::weak_ptr< wb::manager > wb_
Definition: display.hpp:657
std::shared_ptr< gui::button > find_menu_button(const std::string &id)
Definition: display.cpp:827
Definition: display.hpp:48
void set_theme(config theme_cfg)
Definition: display.cpp:277
int blindfold_ctr_
Definition: display.hpp:651
const std::string & team_name() const
Definition: team.hpp:296
void draw_floating_labels(surface screen)
TERRAIN_TYPE
Definition: display.hpp:716
bool grid_
Definition: display.hpp:748
std::vector< std::string > square_parenthetical_split(const std::string &val, const char separator, const std::string &left, const std::string &right, const int flags)
Similar to parenthetical_split, but also expands embedded square brackets.
int get_end_time() const
std::string remove_exclusive_draw(const map_location &loc)
Cancels an exclusive draw request.
Definition: display.cpp:425
const int SIZE_SMALL
Definition: constants.cpp:23
const color_t BAD_COLOR
std::size_t currentTeam_
Definition: display.hpp:733
void rebuild_all()
Rebuild all dynamic terrain.
Definition: display.cpp:488
bool propagate_invalidation(const std::set< map_location > &locs)
If this set is partially invalidated, invalidate all its hexes.
Definition: display.cpp:3021
void write(config &cfg) const
Definition: location.cpp:210
surface minimap_
Definition: display.hpp:743
void start_animation(int start_time, bool cycles=false)
Starts an animation cycle.