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