-
-
Notifications
You must be signed in to change notification settings - Fork 993
/
display.cpp
3739 lines (3211 loc) · 105 KB
/
display.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2003 - 2014 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Routines to set up the display, scroll and zoom the map.
*/
#include "arrow.hpp"
#include "cursor.hpp"
#include "display.hpp"
#include "fake_unit_manager.hpp"
#include "game_preferences.hpp"
#include "gettext.hpp"
#include "halo.hpp"
#include "language.hpp"
#include "log.hpp"
#include "marked-up_text.hpp"
#include "map.hpp"
#include "map_label.hpp"
#include "minimap.hpp"
#include "overlay.hpp"
#include "play_controller.hpp" //note: this can probably be refactored out
#include "reports.hpp"
#include "resources.hpp"
#include "synced_context.hpp"
#include "team.hpp"
#include "terrain_builder.hpp"
#include "text.hpp"
#include "time_of_day.hpp"
#include "tooltips.hpp"
#include "tod_manager.hpp"
#include "unit.hpp"
#include "unit_animation_component.hpp"
#include "unit_drawer.hpp"
#include "whiteboard/manager.hpp"
#include "SDL_image.h"
#include <boost/foreach.hpp>
#ifdef __SUNPRO_CC
// GCC doesn't have hypot in cmath so include it for Sun Studio
#include <math.h>
#endif
#include <cmath>
// Includes for bug #17573
#if defined(__GLIBC__)
#include <gnu/libc-version.h>
#include <cstdio>
#endif
static lg::log_domain log_display("display");
#define ERR_DP LOG_STREAM(err, log_display)
#define LOG_DP LOG_STREAM(info, log_display)
#define DBG_DP LOG_STREAM(debug, log_display)
namespace {
const int DefaultZoom = game_config::tile_size;
const int SmallZoom = DefaultZoom / 2;
const int MinZoom = 4;
const int MaxZoom = 288;
size_t sunset_delay = 0;
bool benchmark = false;
bool debug_foreground = false;
}
int display::last_zoom_ = SmallZoom;
void display::parse_team_overlays()
{
const team& curr_team = dc_->teams()[playing_team()];
const team& prev_team = dc_->teams()[playing_team()-1 < dc_->teams().size() ? playing_team()-1 : dc_->teams().size()-1];
BOOST_FOREACH(const game_display::overlay_map::value_type i, *overlays_) {
const overlay& ov = i.second;
if (!ov.team_name.empty() &&
((ov.team_name.find(curr_team.team_name()) + 1) != 0) !=
((ov.team_name.find(prev_team.team_name()) + 1) != 0))
{
invalidate(i.first);
}
}
}
void display::add_overlay(const map_location& loc, const std::string& img, const std::string& halo,const std::string& team_name, bool visible_under_fog)
{
if (halo_man_) {
const halo::handle halo_handle = halo_man_->add(get_location_x(loc) + hex_size() / 2,
get_location_y(loc) + hex_size() / 2, halo, loc);
const overlay item(img, halo, halo_handle, team_name, visible_under_fog);
overlays_->insert(overlay_map::value_type(loc,item));
}
}
void display::remove_overlay(const map_location& loc)
{
/* This code no longer needed because of RAII in halo::handles
if (halo_man_) {
typedef overlay_map::const_iterator Itor;
std::pair<Itor,Itor> itors = overlays_->equal_range(loc);
while(itors.first != itors.second) {
halo_man_->remove(itors.first->second.halo_handle);
++itors.first;
}
}
*/
overlays_->erase(loc);
}
void display::remove_single_overlay(const map_location& loc, const std::string& toDelete)
{
//Iterate through the values with key of loc
typedef overlay_map::iterator Itor;
overlay_map::iterator iteratorCopy;
std::pair<Itor,Itor> itors = overlays_->equal_range(loc);
while(itors.first != itors.second) {
//If image or halo of overlay struct matches toDelete, remove the overlay
if(itors.first->second.image == toDelete || itors.first->second.halo == toDelete) {
iteratorCopy = itors.first;
++itors.first;
//Not needed because of RAII --> halo_man_->remove(iteratorCopy->second.halo_handle);
overlays_->erase(iteratorCopy);
}
else {
++itors.first;
}
}
}
display::display(const display_context * dc, CVideo& video, boost::weak_ptr<wb::manager> wb, reports & reports_object, const config& theme_cfg, const config& level) :
dc_(dc),
halo_man_(new halo::manager(*this)),
wb_(wb),
exclusive_unit_draw_requests_(),
screen_(video),
currentTeam_(0),
dont_show_all_(false),
energy_bar_rects_(),
xpos_(0),
ypos_(0),
view_locked_(false),
theme_(theme_cfg, screen_area()),
zoom_(DefaultZoom),
fake_unit_man_(new fake_unit_manager(*this)),
builder_(new terrain_builder(level, &dc_->map(), theme_.border().tile_image)),
minimap_(NULL),
minimap_location_(sdl::empty_rect),
redrawMinimap_(false),
redraw_background_(true),
invalidateAll_(true),
grid_(false),
diagnostic_label_(0),
panelsDrawn_(false),
turbo_speed_(2),
turbo_(false),
invalidateGameStatus_(true),
map_labels_(new map_labels(*this, 0)),
reports_object_(reports_object),
scroll_event_("scrolled"),
complete_redraw_event_("completely_redrawn"),
nextDraw_(0),
reportRects_(),
#ifdef SDL_GPU
reportImages_(),
#else
reportSurfaces_(),
#endif
reports_(),
menu_buttons_(),
action_buttons_(),
sliders_(),
invalidated_(),
previous_invalidated_(),
mouseover_hex_overlay_(NULL),
tod_hex_mask1(NULL),
tod_hex_mask2(NULL),
fog_images_(),
shroud_images_(),
selectedHex_(),
mouseoverHex_(),
keys_(),
animate_map_(true),
local_tod_light_(false),
flags_(),
activeTeam_(0),
drawing_buffer_(),
map_screenshot_(false),
reach_map_(),
reach_map_old_(),
reach_map_changed_(true),
overlays_(NULL),
fps_handle_(0),
invalidated_hexes_(0),
drawn_hexes_(0),
idle_anim_(preferences::idle_anim()),
idle_anim_rate_(1.0),
map_screenshot_surf_(NULL),
redraw_observers_(),
draw_coordinates_(false),
draw_terrain_codes_(false),
arrows_map_(),
color_adjust_()
#ifdef SDL_GPU
, update_panel_image_(true),
panel_image_()
#endif
#if defined(__GLIBC__)
, do_reverse_memcpy_workaround_(false)
#endif
{
singleton_ = this;
resources::fake_units = fake_unit_man_.get();
blindfold_ctr_ = 0;
read(level.child_or_empty("display"));
if(non_interactive()
&& (get_video_surface() != NULL
&& video.faked())) {
screen_.lock_updates(true);
}
fill_images_list(game_config::fog_prefix, fog_images_);
fill_images_list(game_config::shroud_prefix, shroud_images_);
set_idle_anim_rate(preferences::idle_anim_rate());
image::set_zoom(zoom_);
init_flags();
#if defined(__GLIBC__) && !SDL_VERSION_ATLEAST(2,0,0)
// Runtime checks for bug #17573
// Get glibc runtime version information
int glibc, glibc_minor;
sscanf(gnu_get_libc_version(), "%d.%d", &glibc, &glibc_minor);
// Get SDL runtime version information
const SDL_version* v = SDL_Linked_Version();
do_reverse_memcpy_workaround_ = (glibc > 2 || (glibc == 2 && glibc_minor >= 13)) &&
(v->major < 1 || (v->major == 1 && v->minor < 2) ||
(v->major == 1 && v->minor == 2 && v->patch < 15) );
#endif
}
display::~display()
{
singleton_ = NULL;
resources::fake_units = NULL;
}
void display::init_flags() {
flags_.clear();
if (!dc_) return;
flags_.resize(dc_->teams().size());
std::vector<std::string> side_colors;
side_colors.reserve(dc_->teams().size());
for(size_t i = 0; i != dc_->teams().size(); ++i) {
std::string side_color = team::get_side_color_index(i+1);
side_colors.push_back(side_color);
init_flags_for_side_internal(i, side_color);
}
image::set_team_colors(&side_colors);
}
void display::reinit_flags_for_side(size_t side)
{
if (!dc_ || side >= dc_->teams().size()) {
ERR_DP << "Cannot rebuild flags for inexistent or unconfigured side " << side << '\n';
return;
}
init_flags_for_side_internal(side, team::get_side_color_index(side + 1));
}
void display::init_flags_for_side_internal(size_t n, const std::string& side_color)
{
assert(dc_ != NULL);
assert(n < dc_->teams().size());
assert(n < flags_.size());
std::string flag = dc_->teams()[n].flag();
std::string old_rgb = game_config::flag_rgb;
std::string new_rgb = side_color;
if(flag.empty()) {
flag = game_config::images::flag;
}
LOG_DP << "Adding flag for team " << n << " from animation " << flag << "\n";
// Must recolor flag image
animated<image::locator> temp_anim;
std::vector<std::string> items = utils::square_parenthetical_split(flag);
std::vector<std::string>::const_iterator itor = items.begin();
for(; itor != items.end(); ++itor) {
const std::vector<std::string>& items = utils::split(*itor, ':');
std::string str;
int time;
if(items.size() > 1) {
str = items.front();
time = atoi(items.back().c_str());
} else {
str = *itor;
time = 100;
}
std::stringstream temp;
temp << str << "~RC(" << old_rgb << ">"<< new_rgb << ")";
image::locator flag_image(temp.str());
temp_anim.add_frame(time, flag_image);
}
animated<image::locator>& f = flags_[n];
f = temp_anim;
f.start_animation(rand() % f.get_end_time(), true);
}
#ifdef SDL_GPU
sdl::timage display::get_flag(const map_location& loc)
{
if(!get_map().is_village(loc)) {
return surface(NULL);
}
for(size_t i = 0; i != dc_->teams().size(); ++i) {
if(dc_->teams()[i].owns_village(loc) &&
(!fogged(loc) || !dc_->teams()[currentTeam_].is_enemy(i+1)))
{
flags_[i].update_last_draw_time();
const image::locator &image_flag = animate_map_ ?
flags_[i].get_current_frame() : flags_[i].get_first_frame();
return image::get_texture(image_flag, image::TOD_COLORED);
}
}
return sdl::timage();
}
#else
surface display::get_flag(const map_location& loc)
{
if(!get_map().is_village(loc)) {
return surface(NULL);
}
for(size_t i = 0; i != dc_->teams().size(); ++i) {
if(dc_->teams()[i].owns_village(loc) &&
(!fogged(loc) || !dc_->teams()[currentTeam_].is_enemy(i+1)))
{
flags_[i].update_last_draw_time();
const image::locator &image_flag = animate_map_ ?
flags_[i].get_current_frame() : flags_[i].get_first_frame();
return image::get_image(image_flag, image::TOD_COLORED);
}
}
return surface(NULL);
}
#endif
void display::set_team(size_t teamindex, bool show_everything)
{
assert(teamindex < dc_->teams().size());
currentTeam_ = teamindex;
if (!show_everything)
{
labels().set_team(&dc_->teams()[teamindex]);
dont_show_all_ = true;
}
else
{
labels().set_team(NULL);
dont_show_all_ = false;
}
labels().recalculate_labels();
if(boost::shared_ptr<wb::manager> w = wb_.lock())
w->on_viewer_change(teamindex);
}
void display::set_playing_team(size_t teamindex)
{
assert(teamindex < dc_->teams().size());
activeTeam_ = teamindex;
invalidate_game_status();
}
bool display::add_exclusive_draw(const map_location& loc, unit& unit)
{
if (loc.valid() && exclusive_unit_draw_requests_.find(loc) == exclusive_unit_draw_requests_.end())
{
exclusive_unit_draw_requests_[loc] = unit.id();
return true;
}
else
{
return false;
}
}
std::string display::remove_exclusive_draw(const map_location& loc)
{
std::string id = "";
if(loc.valid())
{
id = exclusive_unit_draw_requests_[loc];
//id will be set to the default "" string by the [] operator if the map doesn't have anything for that loc.
exclusive_unit_draw_requests_.erase(loc);
}
return id;
}
const time_of_day & display::get_time_of_day(const map_location& /*loc*/) const
{
static time_of_day tod;
return tod;
}
/**
* Display objects don't hold a tod maanger, instead game_display objects do. If the base version of this method is called,
* try to get it from resources and use an assert to check for failure.
*/
const tod_manager & display::get_tod_man() const
{
assert(resources::tod_manager);
return *resources::tod_manager;
}
void display::update_tod() {
const time_of_day& tod = get_time_of_day();
tod_color col = color_adjust_ + tod.color;
image::set_color_adjustment(col.r, col.g, col.b);
}
void display::adjust_color_overlay(int r, int g, int b) {
color_adjust_ = tod_color(r, g, b);
update_tod();
}
void display::fill_images_list(const std::string& prefix, std::vector<std::string>& images)
{
// search prefix.png, prefix1.png, prefix2.png ...
for(int i=0; ; ++i){
std::ostringstream s;
s << prefix;
if(i != 0)
s << i;
s << ".png";
if(image::exists(s.str()))
images.push_back(s.str());
else if(i>0)
break;
}
if (images.empty())
images.push_back("");
}
const std::string& display::get_variant(const std::vector<std::string>& variants, const map_location &loc) const
{
//TODO use better noise function
return variants[abs(loc.x + loc.y) % variants.size()];
}
void display::rebuild_all()
{
builder_->rebuild_all();
}
void display::reload_map()
{
redraw_background_ = true;
builder_->reload_map();
}
void display::change_display_context(const display_context * dc)
{
dc_ = dc;
builder_->change_map(&dc_->map()); //TODO: Should display_context own and initalize the builder object?
}
void display::reset_halo_manager()
{
halo_man_.reset(new halo::manager(*this));
}
void display::reset_halo_manager(halo::manager & halo_man)
{
halo_man_.reset(&halo_man);
}
void display::blindfold(bool value)
{
if(value == true)
++blindfold_ctr_;
else
--blindfold_ctr_;
}
bool display::is_blindfolded() const
{
return blindfold_ctr_ > 0;
}
const SDL_Rect& display::max_map_area() const
{
static SDL_Rect max_area = {0, 0, 0, 0};
// hex_size() is always a multiple of 4
// and hex_width() a multiple of 3,
// so there shouldn't be off-by-one-errors
// due to rounding.
// To display a hex fully on screen,
// a little bit extra space is needed.
// Also added the border two times.
max_area.w = static_cast<int>((get_map().w() + 2 * theme_.border().size + 1.0/3.0) * hex_width());
max_area.h = static_cast<int>((get_map().h() + 2 * theme_.border().size + 0.5) * hex_size());
return max_area;
}
const SDL_Rect& display::map_area() const
{
static SDL_Rect max_area;
max_area = max_map_area();
// if it's for map_screenshot, maximize and don't recenter
if (map_screenshot_) {
return max_area;
}
static SDL_Rect res;
res = map_outside_area();
if(max_area.w < res.w) {
// map is smaller, center
res.x += (res.w - max_area.w)/2;
res.w = max_area.w;
}
if(max_area.h < res.h) {
// map is smaller, center
res.y += (res.h - max_area.h)/2;
res.h = max_area.h;
}
return res;
}
bool display::outside_area(const SDL_Rect& area, const int x, const int y) const
{
const int x_thresh = hex_size();
const int y_thresh = hex_size();
return (x < area.x || x > area.x + area.w - x_thresh ||
y < area.y || y > area.y + area.h - y_thresh);
}
// This function uses the screen as reference
const map_location display::hex_clicked_on(int xclick, int yclick) const
{
const SDL_Rect& rect = map_area();
if(sdl::point_in_rect(xclick,yclick,rect) == false) {
return map_location();
}
xclick -= rect.x;
yclick -= rect.y;
return pixel_position_to_hex(xpos_ + xclick, ypos_ + yclick);
}
// This function uses the rect of map_area as reference
const map_location display::pixel_position_to_hex(int x, int y) const
{
// adjust for the border
x -= static_cast<int>(theme_.border().size * hex_width());
y -= static_cast<int>(theme_.border().size * hex_size());
// The editor can modify the border and this will result in a negative y
// value. Instead of adding extra cases we just shift the hex. Since the
// editor doesn't use the direction this is no problem.
const int offset = y < 0 ? 1 : 0;
if(offset) {
x += hex_width();
y += hex_size();
}
const int s = hex_size();
const int tesselation_x_size = hex_width() * 2;
const int tesselation_y_size = s;
const int x_base = x / tesselation_x_size * 2;
const int x_mod = x % tesselation_x_size;
const int y_base = y / tesselation_y_size;
const int y_mod = y % tesselation_y_size;
int x_modifier = 0;
int y_modifier = 0;
if (y_mod < tesselation_y_size / 2) {
if ((x_mod * 2 + y_mod) < (s / 2)) {
x_modifier = -1;
y_modifier = -1;
} else if ((x_mod * 2 - y_mod) < (s * 3 / 2)) {
x_modifier = 0;
y_modifier = 0;
} else {
x_modifier = 1;
y_modifier = -1;
}
} else {
if ((x_mod * 2 - (y_mod - s / 2)) < 0) {
x_modifier = -1;
y_modifier = 0;
} else if ((x_mod * 2 + (y_mod - s / 2)) < s * 2) {
x_modifier = 0;
y_modifier = 0;
} else {
x_modifier = 1;
y_modifier = 0;
}
}
return map_location(x_base + x_modifier - offset, y_base + y_modifier - offset);
}
display::rect_of_hexes::iterator& display::rect_of_hexes::iterator::operator++()
{
if (loc_.y < rect_.bottom[loc_.x & 1])
++loc_.y;
else {
++loc_.x;
loc_.y = rect_.top[loc_.x & 1];
}
return *this;
}
// begin is top left, and end is after bottom right
display::rect_of_hexes::iterator display::rect_of_hexes::begin() const
{
return iterator(map_location(left, top[left & 1]), *this);
}
display::rect_of_hexes::iterator display::rect_of_hexes::end() const
{
return iterator(map_location(right+1, top[(right+1) & 1]), *this);
}
const display::rect_of_hexes display::hexes_under_rect(const SDL_Rect& r) const
{
rect_of_hexes res;
if (r.w<=0 || r.h<=0) {
// empty rect, return dummy values giving begin=end
res.left = 0;
res.right = -1; // end is right+1
res.top[0] = 0;
res.top[1] = 0;
res.bottom[0] = 0;
res.bottom[1] = 0;
return res;
}
SDL_Rect map_rect = map_area();
// translate rect coordinates from screen-based to map_area-based
int x = xpos_ - map_rect.x + r.x;
int y = ypos_ - map_rect.y + r.y;
// we use the "double" type to avoid important rounding error (size of an hex!)
// we will also need to use std::floor to avoid bad rounding at border (negative values)
double tile_width = hex_width();
double tile_size = hex_size();
double border = theme_.border().size;
// we minus "0.(3)", for horizontal imbrication.
// reason is: two adjacent hexes each overlap 1/4 of their width, so for
// grid calculation 3/4 of tile width is used, which by default gives
// 18/54=0.(3). Note that, while tile_width is zoom dependand, 0.(3) is not.
res.left = static_cast<int>(std::floor(-border + x / tile_width - 0.3333333));
// we remove 1 pixel of the rectangle dimensions
// (the rounded division take one pixel more than needed)
res.right = static_cast<int>(std::floor(-border + (x + r.w-1) / tile_width));
// for odd x, we must shift up one half-hex. Since x will vary along the edge,
// we store here the y values for even and odd x, respectively
res.top[0] = static_cast<int>(std::floor(-border + y / tile_size));
res.top[1] = static_cast<int>(std::floor(-border + y / tile_size - 0.5));
res.bottom[0] = static_cast<int>(std::floor(-border + (y + r.h-1) / tile_size));
res.bottom[1] = static_cast<int>(std::floor(-border + (y + r.h-1) / tile_size - 0.5));
// TODO: in some rare cases (1/16), a corner of the big rect is on a tile
// (the 72x72 rectangle containing the hex) but not on the hex itself
// Can maybe be optimized by using pixel_position_to_hex
return res;
}
int display::get_location_x(const map_location& loc) const
{
return static_cast<int>(map_area().x + (loc.x + theme_.border().size) * hex_width() - xpos_);
}
int display::get_location_y(const map_location& loc) const
{
return static_cast<int>(map_area().y + (loc.y + theme_.border().size) * zoom_ - ypos_ + (is_odd(loc.x) ? zoom_/2 : 0));
}
map_location display::minimap_location_on(int x, int y)
{
//TODO: don't return location for this,
// instead directly scroll to the clicked pixel position
if (!sdl::point_in_rect(x, y, minimap_area())) {
return map_location();
}
// we transform the coordinates from minimap to the full map image
// probably more adjustments to do (border, minimap shift...)
// but the mouse and human capacity to evaluate the rectangle center
// is not pixel precise.
int px = (x - minimap_location_.x) * get_map().w()*hex_width() / minimap_location_.w;
int py = (y - minimap_location_.y) * get_map().h()*hex_size() / minimap_location_.h;
map_location loc = pixel_position_to_hex(px, py);
if (loc.x < 0)
loc.x = 0;
else if (loc.x >= get_map().w())
loc.x = get_map().w() - 1;
if (loc.y < 0)
loc.y = 0;
else if (loc.y >= get_map().h())
loc.y = get_map().h() - 1;
return loc;
}
int display::screenshot(std::string filename, bool map_screenshot)
{
int size = 0;
if (!map_screenshot) {
surface screenshot_surf = screen_.getSurface();
image::save_image(screenshot_surf, filename);
size = screenshot_surf->w * screenshot_surf->h;
} else {
if (get_map().empty()) {
// Map Screenshot are big, abort and warn the user if he does strange things
std::cerr << "No map, can't do a Map Screenshot. If it was not wanted, check your hotkey.\n";
return -1;
}
SDL_Rect area = max_map_area();
map_screenshot_surf_ = create_compatible_surface(screen_.getSurface(), area.w, area.h);
if (map_screenshot_surf_ == NULL) {
// Memory problem ?
std::cerr << "Can't create the screenshot surface. Maybe too big, try dezooming.\n";
return -1;
}
size = map_screenshot_surf_->w * map_screenshot_surf_->h;
// back up the current map view position and move to top-left
int old_xpos = xpos_;
int old_ypos = ypos_;
xpos_ = 0;
ypos_ = 0;
// we reroute render output to the screenshot surface and invalidate all
map_screenshot_= true ;
invalidateAll_ = true;
DBG_DP << "draw() with map_screenshot\n";
draw(true,true);
// finally save the image on disk
image::save_image(map_screenshot_surf_,filename);
//NOTE: need to be sure that we free this huge surface (is it enough?)
map_screenshot_surf_ = NULL;
// restore normal rendering
map_screenshot_= false;
xpos_ = old_xpos;
ypos_ = old_ypos;
// some drawing functions are confused by the temporary change
// of the map_area and thus affect the UI outside of the map
redraw_everything();
}
// convert pixel size to BMP size
size = (2048 + size*3);
return size;
}
gui::button* display::find_action_button(const std::string& id)
{
for (size_t i = 0; i < action_buttons_.size(); ++i) {
if(action_buttons_[i].id() == id) {
return &action_buttons_[i];
}
}
return NULL;
}
gui::button* display::find_menu_button(const std::string& id)
{
for (size_t i = 0; i < menu_buttons_.size(); ++i) {
if(menu_buttons_[i].id() == id) {
return &menu_buttons_[i];
}
}
return NULL;
}
gui::slider* display::find_slider(const std::string& id)
{
for (size_t i = 0; i < sliders_.size(); ++i) {
if(sliders_[i].id() == id) {
return &sliders_[i];
}
}
return NULL;
}
void display::create_buttons()
{
std::vector<gui::button> menu_work;
std::vector<gui::button> action_work;
std::vector<gui::slider> slider_work;
DBG_DP << "creating sliders...\n";
const std::vector<theme::slider>& sliders = theme_.sliders();
for(std::vector<theme::slider>::const_iterator i = sliders.begin(); i != sliders.end(); ++i) {
gui::slider s(screen_, i->image(), i->black_line());
DBG_DP << "drawing button " << i->get_id() << "\n";
s.set_id(i->get_id());
const SDL_Rect& loc = i->location(screen_area());
s.set_location(loc);
//TODO support for non zoom sliders
s.set_max(MaxZoom);
s.set_min(MinZoom);
s.set_value(zoom_);
if (!i->tooltip().empty()){
s.set_tooltip_string(i->tooltip());
}
if(sdl::rects_overlap(s.location(),map_outside_area())) {
s.set_volatile(true);
}
gui::slider* s_prev = find_slider(s.id());
if(s_prev) {
s.set_max(s_prev->max_value());
s.set_min(s_prev->min_value());
s.set_value(s_prev->value());
s.enable(s_prev->enabled());
}
slider_work.push_back(s);
}
DBG_DP << "creating menu buttons...\n";
const std::vector<theme::menu>& buttons = theme_.menus();
for(std::vector<theme::menu>::const_iterator i = buttons.begin(); i != buttons.end(); ++i) {
if (!i->is_button()) continue;
gui::button b(screen_, i->title(), gui::button::TYPE_PRESS, i->image(),
gui::button::DEFAULT_SPACE, true, i->overlay());
DBG_DP << "drawing button " << i->get_id() << "\n";
b.set_id(i->get_id());
const SDL_Rect& loc = i->location(screen_area());
b.set_location(loc.x,loc.y);
if (!i->tooltip().empty()){
b.set_tooltip_string(i->tooltip());
}
if(sdl::rects_overlap(b.location(),map_outside_area())) {
b.set_volatile(true);
}
gui::button* b_prev = find_menu_button(b.id());
if(b_prev) b.enable(b_prev->enabled());
menu_work.push_back(b);
}
DBG_DP << "creating action buttons...\n";
const std::vector<theme::action>& actions = theme_.actions();
for(std::vector<theme::action>::const_iterator i = actions.begin(); i != actions.end(); ++i) {
gui::button b(screen_, i->title(), string_to_button_type(i->type()), i->image(),
gui::button::DEFAULT_SPACE, true, i->overlay());
DBG_DP << "drawing button " << i->get_id() << "\n";
b.set_id(i->get_id());
const SDL_Rect& loc = i->location(screen_area());
b.set_location(loc.x,loc.y);
if (!i->tooltip(0).empty()){
b.set_tooltip_string(i->tooltip(0));
}
if(sdl::rects_overlap(b.location(),map_outside_area())) {
b.set_volatile(true);
}
gui::button* b_prev = find_action_button(b.id());
if(b_prev) b.enable(b_prev->enabled());
action_work.push_back(b);
}
menu_buttons_.swap(menu_work);
action_buttons_.swap(action_work);
sliders_.swap(slider_work);
DBG_DP << "buttons created\n";
}
#ifdef SDL_GPU
void display::render_buttons()
{
BOOST_FOREACH(gui::button &btn, menu_buttons_) {
btn.set_dirty(true);
}
BOOST_FOREACH(gui::button &btn, action_buttons_) {
btn.set_dirty(true);
}
BOOST_FOREACH(gui::slider &sld, sliders_) {
sld.set_dirty(true);
}
}
#endif
gui::button::TYPE display::string_to_button_type(std::string type)
{
gui::button::TYPE res = gui::button::TYPE_PRESS;
if (type == "checkbox") { res = gui::button::TYPE_CHECK; }
else if (type == "image") { res = gui::button::TYPE_IMAGE; }
else if (type == "radiobox") { res = gui::button::TYPE_RADIO; }
else if (type == "turbo") { res = gui::button::TYPE_TURBO; }
return res;
}
static const std::string& get_direction(size_t n)
{
static std::string const dirs[6] = { "-n", "-ne", "-se", "-s", "-sw", "-nw" };
return dirs[n >= sizeof(dirs)/sizeof(*dirs) ? 0 : n];
}
#ifdef SDL_GPU
std::vector<sdl::timage> display::get_fog_shroud_images(const map_location& loc, image::TYPE image_type)
#else
std::vector<surface> display::get_fog_shroud_images(const map_location& loc, image::TYPE image_type)
#endif
{
std::vector<std::string> names;
map_location adjacent[6];
get_adjacent_tiles(loc,adjacent);
enum visibility {FOG=0, SHROUD=1, CLEAR=2};
visibility tiles[6];
const std::string* image_prefix[] =
{ &game_config::fog_prefix, &game_config::shroud_prefix};
for(int i = 0; i != 6; ++i) {
if(shrouded(adjacent[i])) {
tiles[i] = SHROUD;
} else if(!fogged(loc) && fogged(adjacent[i])) {
tiles[i] = FOG;
} else {
tiles[i] = CLEAR;
}
}