00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef DISPLAY_H_INCLUDED
00035 #define DISPLAY_H_INCLUDED
00036
00037 class config;
00038 class terrain_builder;
00039 struct time_of_day;
00040 class map_labels;
00041 class arrow;
00042
00043 #include "font.hpp"
00044 #include "key.hpp"
00045 #include "team.hpp"
00046 #include "time_of_day.hpp"
00047 #include "theme.hpp"
00048 #include "video.hpp"
00049 #include "widgets/button.hpp"
00050
00051 #include <list>
00052
00053 #include <boost/function.hpp>
00054 #include <boost/scoped_ptr.hpp>
00055
00056 class gamemap;
00057
00058 class display
00059 {
00060 public:
00061 display(unit_map* units, CVideo& video, const gamemap* map, const std::vector<team>* t,
00062 const config& theme_cfg, const config& level);
00063 virtual ~display();
00064 static display* get_singleton() { return singleton_ ;}
00065
00066
00067
00068 bool show_everything() const { return !viewpoint_; }
00069
00070 const std::vector<team>& get_teams() {return *teams_;}
00071
00072
00073 size_t playing_team() const { return activeTeam_; }
00074
00075 bool team_valid() const { return currentTeam_ < teams_->size(); }
00076
00077
00078 size_t viewing_team() const { return currentTeam_; }
00079 int viewing_side() const { return currentTeam_ + 1; }
00080
00081
00082
00083
00084 void clear_exclusive_draws() { exclusive_unit_draw_requests_.clear(); }
00085 unit_map& get_units() {return *units_;}
00086 const unit_map& get_const_units() const {return *units_;}
00087
00088
00089
00090
00091
00092
00093
00094
00095 bool add_exclusive_draw(const map_location& loc, unit& unit);
00096
00097
00098
00099
00100
00101 std::string remove_exclusive_draw(const map_location& loc);
00102
00103 void draw_bar(const std::string& image, int xpos, int ypos,
00104 const map_location& loc, size_t height, double filled,
00105 const SDL_Color& col, fixed_t alpha);
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 void reload_map();
00121
00122 void change_map(const gamemap* m);
00123 void change_teams(const std::vector<team>* teams);
00124 void change_units(unit_map* units);
00125
00126 static Uint32 rgb(Uint8 red, Uint8 green, Uint8 blue)
00127 { return 0xFF000000 | (red << 16) | (green << 8) | blue; }
00128 static Uint8 red(Uint32 color)
00129 { return (color & 0x00FF0000) >> 16;}
00130 static Uint8 green(Uint32 color)
00131 { return (color & 0x0000FF00) >> 8;}
00132 static Uint8 blue(Uint32 color)
00133 { return (color & 0x000000FF) ;}
00134 static Uint32 max_rgb(Uint32 first,Uint32 second)
00135 { return rgb(std::max(red(first),red(second)),std::max(green(first),green(second)),std::max(blue(first),blue(second))) ; }
00136
00137
00138
00139
00140
00141 void update_tod();
00142
00143
00144
00145
00146
00147
00148 void adjust_color_overlay(int r, int g, int b);
00149
00150
00151
00152 CVideo& video() { return screen_; }
00153
00154
00155 surface get_screen_surface() { return map_screenshot_ ? map_screenshot_surf_ : screen_.getSurface();}
00156
00157 virtual bool in_game() const { return false; }
00158 virtual bool in_editor() const { return false; }
00159
00160
00161
00162
00163
00164
00165 int w() const { return screen_.getx(); }
00166 int h() const { return screen_.gety(); }
00167 const SDL_Rect& minimap_area() const
00168 { return theme_.mini_map_location(screen_area()); }
00169 const SDL_Rect& unit_image_area() const
00170 { return theme_.unit_image_location(screen_area()); }
00171
00172 SDL_Rect screen_area() const
00173 { return create_rect(0, 0, w(), h()); }
00174
00175
00176
00177
00178
00179 const SDL_Rect& max_map_area() const;
00180
00181
00182
00183
00184 const SDL_Rect& map_area() const;
00185
00186
00187
00188
00189
00190
00191 const SDL_Rect& map_outside_area() const { return map_screenshot_ ?
00192 max_map_area() : theme_.main_map_location(screen_area()); }
00193
00194
00195 bool outside_area(const SDL_Rect& area, const int x,const int y) const;
00196
00197
00198
00199
00200
00201
00202
00203 int hex_width() const { return (zoom_*3)/4; }
00204
00205
00206
00207
00208
00209 int hex_size() const { return zoom_; }
00210
00211
00212 double get_zoom_factor() const { return double(zoom_)/double(image::tile_size); }
00213
00214
00215
00216
00217
00218
00219 const map_location hex_clicked_on(int x, int y) const;
00220
00221
00222
00223
00224
00225
00226 const map_location pixel_position_to_hex(int x, int y) const;
00227
00228
00229
00230
00231
00232
00233 map_location minimap_location_on(int x, int y);
00234
00235 const map_location& selected_hex() const { return selectedHex_; }
00236 const map_location& mouseover_hex() const { return mouseoverHex_; }
00237
00238 virtual void select_hex(map_location hex);
00239 virtual void highlight_hex(map_location hex);
00240
00241
00242 void invalidate_game_status() { invalidateGameStatus_ = true; }
00243
00244
00245 int get_location_x(const map_location& loc) const;
00246 int get_location_y(const map_location& loc) const;
00247
00248
00249
00250
00251
00252 struct rect_of_hexes{
00253 int left;
00254 int right;
00255 int top[2];
00256 int bottom[2];
00257
00258
00259 struct iterator {
00260 iterator(const map_location &loc, const rect_of_hexes &rect)
00261 : loc_(loc), rect_(rect){};
00262
00263
00264 iterator& operator++();
00265 bool operator==(const iterator &that) const { return that.loc_ == loc_; }
00266 bool operator!=(const iterator &that) const { return that.loc_ != loc_; }
00267 const map_location& operator*() const {return loc_;};
00268
00269 typedef std::forward_iterator_tag iterator_category;
00270 typedef map_location value_type;
00271 typedef int difference_type;
00272 typedef const map_location *pointer;
00273 typedef const map_location &reference;
00274
00275 private:
00276 map_location loc_;
00277 const rect_of_hexes &rect_;
00278 };
00279 typedef iterator const_iterator;
00280
00281 iterator begin() const;
00282 iterator end() const;
00283 };
00284
00285
00286 const rect_of_hexes hexes_under_rect(const SDL_Rect& r) const;
00287
00288
00289 const rect_of_hexes get_visible_hexes() const {return hexes_under_rect(map_area());};
00290
00291
00292 bool shrouded(const map_location& loc) const {
00293 return viewpoint_ && viewpoint_->shrouded(loc);
00294 }
00295
00296 bool fogged(const map_location& loc) const {
00297 return viewpoint_ && viewpoint_->fogged(loc);
00298 }
00299
00300
00301
00302
00303
00304 void set_grid(const bool grid) { grid_ = grid; }
00305
00306
00307 bool get_draw_coordinates() const { return draw_coordinates_; }
00308
00309 void set_draw_coordinates(bool value) { draw_coordinates_ = value; }
00310
00311
00312 bool get_draw_terrain_codes() const { return draw_terrain_codes_; }
00313
00314 void set_draw_terrain_codes(bool value) { draw_terrain_codes_ = value; }
00315
00316
00317 int screenshot(std::string filename, bool map_screenshot = false);
00318
00319
00320 void redraw_everything();
00321
00322
00323 void add_redraw_observer(boost::function<void(display&)> f);
00324
00325
00326 void clear_redraw_observers();
00327
00328 theme& get_theme() { return theme_; }
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 gui::button* find_button(const std::string& id);
00341 gui::button::TYPE string_to_button_type(std::string type);
00342 void create_buttons();
00343 void invalidate_theme() { panelsDrawn_ = false; }
00344
00345 void refresh_report(std::string const &report_name, const config &);
00346
00347 void draw_minimap_units();
00348
00349
00350 void invalidate_all();
00351
00352
00353 bool invalidate(const map_location& loc);
00354
00355 bool invalidate(const std::set<map_location>& locs);
00356
00357
00358
00359
00360
00361 bool propagate_invalidation(const std::set<map_location>& locs);
00362
00363
00364 bool invalidate_locations_in_rect(const SDL_Rect& rect);
00365 bool invalidate_visible_locations_in_rect(const SDL_Rect& rect);
00366
00367
00368
00369
00370 virtual void invalidate_animations();
00371
00372
00373
00374
00375
00376 virtual void invalidate_animations_location(const map_location& ) {}
00377
00378 const gamemap& get_map() const { return *map_; }
00379
00380
00381
00382
00383
00384 void set_mouseover_hex_overlay(const surface& image)
00385 { mouseover_hex_overlay_ = image; }
00386
00387 void clear_mouseover_hex_overlay()
00388 { mouseover_hex_overlay_ = NULL; }
00389
00390
00391
00392
00393
00394
00395
00396
00397 static void sunset(const size_t delay = 0);
00398
00399
00400 static void toggle_benchmark();
00401
00402
00403
00404
00405
00406
00407 static void toggle_debug_foreground();
00408
00409 terrain_builder& get_builder() {return *builder_;};
00410
00411 void flip();
00412
00413
00414 void update_display();
00415
00416
00417 void rebuild_all();
00418
00419 const theme::menu* menu_pressed();
00420
00421
00422
00423
00424
00425 void enable_menu(const std::string& item, bool enable);
00426
00427 void set_diagnostic(const std::string& msg);
00428
00429
00430 void delay(unsigned int milliseconds) const;
00431
00432
00433
00434
00435
00436 void set_turbo(const bool turbo) { turbo_ = turbo; }
00437
00438 double turbo_speed() const;
00439
00440 void set_turbo_speed(const double speed) { turbo_speed_ = speed; }
00441
00442
00443 void set_idle_anim(bool ison) { idle_anim_ = ison; }
00444 bool idle_anim() const { return idle_anim_; }
00445 void set_idle_anim_rate(int rate);
00446 double idle_anim_rate() const { return idle_anim_rate_; }
00447
00448 void bounds_check_position();
00449 void bounds_check_position(int& xpos, int& ypos);
00450
00451
00452
00453
00454
00455
00456 bool scroll(int xmov, int ymov);
00457
00458
00459
00460
00461
00462
00463
00464
00465 void set_zoom(int amount);
00466
00467
00468 void set_default_zoom();
00469
00470 enum SCROLL_TYPE { SCROLL, WARP, ONSCREEN, ONSCREEN_WARP };
00471
00472
00473
00474
00475
00476
00477
00478 void scroll_to_tile(const map_location& loc, SCROLL_TYPE scroll_type=ONSCREEN, bool check_fogged=true,bool force = true);
00479
00480
00481
00482
00483
00484
00485
00486 void scroll_to_tiles(map_location loc1, map_location loc2,
00487 SCROLL_TYPE scroll_type=ONSCREEN, bool check_fogged=true,
00488 double add_spacing=0.0, bool force = true);
00489
00490
00491 void scroll_to_tiles(const std::vector<map_location>& locs,
00492 SCROLL_TYPE scroll_type=ONSCREEN, bool check_fogged=true,
00493 bool only_if_possible=false,
00494 double add_spacing=0.0, bool force = true);
00495
00496
00497 events::generic_event &scroll_event() const { return scroll_event_; }
00498
00499 events::generic_event& complete_redraw_event() { return complete_redraw_event_; }
00500
00501
00502 bool tile_fully_on_screen(const map_location& loc);
00503
00504
00505 bool tile_nearly_on_screen(const map_location &loc) const;
00506
00507
00508
00509
00510
00511
00512
00513
00514 void draw(bool update=true, bool force=false);
00515
00516 map_labels& labels();
00517 const map_labels& labels() const;
00518
00519
00520 void announce(const std::string& msg,
00521 const SDL_Color& color = font::GOOD_COLOR);
00522
00523
00524
00525
00526
00527 void recalculate_minimap() {minimap_ = NULL; redrawMinimap_ = true; };
00528
00529
00530
00531
00532
00533 void redraw_minimap() { redrawMinimap_ = true; }
00534
00535 virtual const time_of_day& get_time_of_day(const map_location& loc = map_location::null_location) const;
00536
00537 virtual bool has_time_area() const {return false;};
00538
00539 void write(config& cfg) const;
00540 private:
00541 void read(const config& cfg);
00542
00543
00544
00545
00546
00547
00548 const SDL_Rect& calculate_energy_bar(surface surf);
00549
00550
00551 protected:
00552
00553 unit_map* units_;
00554
00555 typedef std::map<map_location, std::string> exclusive_unit_draw_requests_t;
00556
00557 exclusive_unit_draw_requests_t exclusive_unit_draw_requests_;
00558
00559
00560
00561 void clear_screen();
00562
00563
00564
00565
00566
00567
00568 virtual void pre_draw() {}
00569
00570
00571
00572
00573
00574
00575 virtual void post_draw() {}
00576
00577
00578
00579
00580
00581 virtual const SDL_Rect& get_clip_rect();
00582
00583
00584
00585
00586
00587
00588
00589 virtual void draw_invalidated();
00590
00591
00592
00593
00594
00595 virtual void post_commit() {}
00596
00597
00598
00599
00600 virtual void draw_hex(const map_location& loc);
00601
00602
00603
00604
00605 virtual image::TYPE get_image_type(const map_location& loc);
00606
00607
00608
00609
00610
00611 virtual void draw_sidebar();
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623 virtual void draw_border(const map_location& loc,
00624 const int xpos, const int ypos);
00625
00626 void draw_minimap();
00627
00628 enum TERRAIN_TYPE { BACKGROUND, FOREGROUND};
00629
00630 std::vector<surface> get_terrain_images(const map_location &loc,
00631 const std::string& timeid,
00632 image::TYPE type,
00633 TERRAIN_TYPE terrain_type);
00634
00635 std::vector<surface> get_fog_shroud_images(const map_location& loc, image::TYPE image_type);
00636
00637 void draw_image_for_report(surface& img, SDL_Rect& rect);
00638
00639 void scroll_to_xy(int screenxpos, int screenypos, SCROLL_TYPE scroll_type,bool force = true);
00640
00641 void fill_images_list(const std::string& prefix, std::vector<std::string>& images);
00642
00643 const std::string& get_variant(const std::vector<std::string>& variants, const map_location &loc) const;
00644
00645 CVideo& screen_;
00646 const gamemap* map_;
00647 size_t currentTeam_;
00648 const std::vector<team>* teams_;
00649 const team *viewpoint_;
00650 std::map<surface,SDL_Rect> energy_bar_rects_;
00651 int xpos_, ypos_;
00652 theme theme_;
00653 int zoom_;
00654 static int last_zoom_;
00655 boost::scoped_ptr<terrain_builder> builder_;
00656 surface minimap_;
00657 SDL_Rect minimap_location_;
00658 bool redrawMinimap_;
00659 bool redraw_background_;
00660 bool invalidateAll_;
00661 bool grid_;
00662 int diagnostic_label_;
00663 bool panelsDrawn_;
00664 double turbo_speed_;
00665 bool turbo_;
00666 bool invalidateGameStatus_;
00667 boost::scoped_ptr<map_labels> map_labels_;
00668
00669
00670
00671 mutable events::generic_event scroll_event_;
00672
00673
00674
00675
00676
00677 events::generic_event complete_redraw_event_;
00678
00679
00680
00681
00682
00683 int nextDraw_;
00684
00685
00686 std::map<std::string, SDL_Rect> reportRects_;
00687 std::map<std::string, surface> reportSurfaces_;
00688 std::map<std::string, config> reports_;
00689 std::vector<gui::button> buttons_;
00690 std::set<map_location> invalidated_;
00691 std::set<map_location> previous_invalidated_;
00692 surface mouseover_hex_overlay_;
00693
00694
00695 surface tod_hex_mask1, tod_hex_mask2;
00696 std::vector<std::string> fog_images_;
00697 std::vector<std::string> shroud_images_;
00698
00699 map_location selectedHex_;
00700 map_location mouseoverHex_;
00701 CKey keys_;
00702
00703
00704 bool animate_map_;
00705
00706
00707 bool local_tod_light_;
00708
00709 public:
00710
00711
00712
00713
00714
00715
00716 enum tdrawing_layer{
00717 LAYER_TERRAIN_BG,
00718
00719
00720
00721 LAYER_GRID_TOP,
00722 LAYER_MOUSEOVER_OVERLAY,
00723 LAYER_FOOTSTEPS,
00724 LAYER_MOUSEOVER_TOP,
00725 LAYER_UNIT_FIRST,
00726 LAYER_UNIT_BG = LAYER_UNIT_FIRST+10,
00727 LAYER_UNIT_DEFAULT=LAYER_UNIT_FIRST+40,
00728 LAYER_TERRAIN_FG = LAYER_UNIT_FIRST+50,
00729
00730
00731
00732 LAYER_GRID_BOTTOM,
00733
00734
00735
00736 LAYER_UNIT_MOVE_DEFAULT=LAYER_UNIT_FIRST+60,
00737 LAYER_UNIT_FG = LAYER_UNIT_FIRST+80,
00738
00739
00740
00741 LAYER_UNIT_MISSILE_DEFAULT = LAYER_UNIT_FIRST+90,
00742 LAYER_UNIT_LAST=LAYER_UNIT_FIRST+100,
00743 LAYER_REACHMAP,
00744 LAYER_MOUSEOVER_BOTTOM,
00745 LAYER_FOG_SHROUD,
00746 LAYER_ARROWS,
00747 LAYER_ACTIONS_NUMBERING,
00748 LAYER_SELECTED_HEX,
00749 LAYER_ATTACK_INDICATOR,
00750 LAYER_UNIT_BAR,
00751
00752
00753
00754 LAYER_MOVE_INFO,
00755 LAYER_LINGER_OVERLAY,
00756 LAYER_BORDER,
00757
00758 LAYER_LAST_LAYER
00759
00760
00761
00762 };
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775 void render_image(int x, int y, const display::tdrawing_layer drawing_layer,
00776 const map_location& loc, surface image,
00777 bool hreverse=false, bool greyscale=false,
00778 fixed_t alpha=ftofxp(1.0), Uint32 blendto=0,
00779 double blend_ratio=0, double submerged=0.0,bool vreverse =false);
00780
00781
00782
00783
00784
00785
00786 void draw_text_in_hex(const map_location& loc,
00787 const tdrawing_layer layer, const std::string& text, size_t font_size,
00788 SDL_Color color, double x_in_hex=0.5, double y_in_hex=0.5);
00789
00790 protected:
00791
00792
00793 size_t activeTeam_;
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822 class drawing_buffer_key
00823 {
00824 private:
00825 unsigned int key_;
00826
00827 static const tdrawing_layer layer_groups[];
00828 static const unsigned int max_layer_group;
00829
00830 public:
00831 drawing_buffer_key(const map_location &loc, tdrawing_layer layer);
00832
00833 bool operator<(const drawing_buffer_key &rhs) const { return key_ < rhs.key_; }
00834 };
00835
00836
00837 class tblit
00838 {
00839 public:
00840 tblit(const tdrawing_layer layer, const map_location& loc,
00841 const int x, const int y, const surface& surf,
00842 const SDL_Rect& clip)
00843 : x_(x), y_(y), surf_(1, surf), clip_(clip),
00844 key_(loc, layer)
00845 {}
00846
00847 tblit(const tdrawing_layer layer, const map_location& loc,
00848 const int x, const int y, const std::vector<surface>& surf,
00849 const SDL_Rect& clip)
00850 : x_(x), y_(y), surf_(surf), clip_(clip),
00851 key_(loc, layer)
00852 {}
00853
00854 int x() const { return x_; }
00855 int y() const { return y_; }
00856 const std::vector<surface> &surf() const { return surf_; }
00857 const SDL_Rect &clip() const { return clip_; }
00858
00859 bool operator<(const tblit &rhs) const { return key_ < rhs.key_; }
00860
00861 private:
00862 int x_;
00863 int y_;
00864 std::vector<surface> surf_;
00865 SDL_Rect clip_;
00866
00867
00868
00869 drawing_buffer_key key_;
00870 };
00871
00872 typedef std::list<tblit> tdrawing_buffer;
00873 tdrawing_buffer drawing_buffer_;
00874
00875 public:
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885 void drawing_buffer_add(const tdrawing_layer layer,
00886 const map_location& loc, int x, int y, const surface& surf,
00887 const SDL_Rect &clip = SDL_Rect());
00888
00889 void drawing_buffer_add(const tdrawing_layer layer,
00890 const map_location& loc, int x, int y,
00891 const std::vector<surface> &surf,
00892 const SDL_Rect &clip = SDL_Rect());
00893
00894 protected:
00895
00896
00897 void drawing_buffer_commit();
00898
00899
00900 void drawing_buffer_clear();
00901
00902
00903 void draw_all_panels();
00904
00905
00906
00907
00908
00909
00910
00911 void draw_init();
00912 void draw_wrap(bool update,bool force);
00913
00914
00915 bool map_screenshot_;
00916
00917 public:
00918
00919 void add_arrow(arrow&);
00920
00921 void remove_arrow(arrow&);
00922
00923
00924 void update_arrow(arrow & a);
00925
00926 private:
00927
00928 int fps_handle_;
00929
00930 int invalidated_hexes_;
00931 int drawn_hexes_;
00932
00933 bool idle_anim_;
00934 double idle_anim_rate_;
00935
00936 surface map_screenshot_surf_;
00937
00938 std::vector<boost::function<void(display&)> > redraw_observers_;
00939
00940
00941 bool draw_coordinates_;
00942
00943 bool draw_terrain_codes_;
00944
00945 typedef std::list<arrow*> arrows_list_t;
00946 typedef std::map<map_location, arrows_list_t > arrows_map_t;
00947
00948 arrows_map_t arrows_map_;
00949
00950 tod_color color_adjust_;
00951
00952 #if defined(__GLIBC__)
00953
00954 bool do_reverse_memcpy_workaround_;
00955 #endif
00956
00957 protected:
00958 static display * singleton_;
00959 };
00960
00961 #endif
00962