display.hpp

Go to the documentation of this file.
00001 /* $Id: display.hpp 53781 2012-04-05 11:04:26Z gabba $ */
00002 /*
00003    Copyright (C) 2003 - 2012 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /**
00017  * @file
00018  *
00019  * map_display and display: classes which take care of
00020  * displaying the map and game-data on the screen.
00021  *
00022  * The display is divided into two main sections:
00023  * - the game area, which displays the tiles of the game board, and units on them,
00024  * - and the side bar, which appears on the right hand side.
00025  * The side bar display is divided into three sections:
00026  * - the minimap, which is displayed at the top right
00027  * - the game status, which includes the day/night image,
00028  *   the turn number, information about the current side,
00029  *   and information about the hex currently moused over (highlighted)
00030  * - the unit status, which displays an image and stats
00031  *   for the current unit.
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     //TODO sort
00067 
00068     bool show_everything() const { return !viewpoint_; }
00069 
00070     const std::vector<team>& get_teams() {return *teams_;}
00071 
00072     /** The playing team is the team whose turn it is. */
00073     size_t playing_team() const { return activeTeam_; }
00074 
00075     bool team_valid() const { return currentTeam_ < teams_->size(); }
00076 
00077     /** The viewing team is the team currently viewing the game. */
00078     size_t viewing_team() const { return currentTeam_; }
00079     int viewing_side() const { return currentTeam_ + 1; }
00080 
00081     /**
00082      * Cancels all the exclusive draw requests.
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      * Allows a unit to request to be the only one drawn in its hex. Useful for situations where
00090      * multiple units (one real, multiple temporary) can end up stacked, such as with the whiteboard.
00091      * @param loc The location of the unit requesting exclusivity.
00092      * @param unit The unit requesting exlusivity.
00093      * @return false if there's already an exclusive draw request for this location.
00094      */
00095     bool add_exclusive_draw(const map_location& loc, unit& unit);
00096     /**
00097      * Cancels an exclusive draw request.
00098      * @return The id of the unit whose exclusive draw request was canceled, or else
00099      *         the empty string if there was no exclusive draw request for this location.
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      * Updates internals that cache map size. This should be called when the map
00118      * size has changed.
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      * Add r,g,b from tod_manager to the map
00139      *
00140      */
00141     void update_tod();
00142 
00143     /**
00144      * Add r,g,b to the colors for all images displayed on the map.
00145      *
00146      * Used for special effects like flashes.
00147      */
00148     void adjust_color_overlay(int r, int g, int b);
00149 
00150 
00151     /** Gets the underlying screen object. */
00152     CVideo& video() { return screen_; }
00153 
00154     /** return the screen surface or the surface used for map_screenshot. */
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      * the dimensions of the display. x and y are width/height.
00162      * mapx is the width of the portion of the display which shows the game area.
00163      * Between mapx and x is the sidebar region.
00164      */
00165     int w() const { return screen_.getx(); }    /**< width */
00166     int h() const { return screen_.gety(); }    /**< height */
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      * Returns the maximum area used for the map
00177      * regardless to resolution and view size
00178      */
00179     const SDL_Rect& max_map_area() const;
00180 
00181     /**
00182      * Returns the area used for the map
00183      */
00184     const SDL_Rect& map_area() const;
00185 
00186     /**
00187      * Returns the available area for a map, this may differ
00188      * from the above. This area will get the background area
00189      * applied to it.
00190      */
00191     const SDL_Rect& map_outside_area() const { return map_screenshot_ ?
00192         max_map_area() : theme_.main_map_location(screen_area()); }
00193 
00194     /** Check if the bbox of the hex at x,y has pixels outside the area rectangle. */
00195     bool outside_area(const SDL_Rect& area, const int x,const int y) const;
00196 
00197     /**
00198      * Function which returns the width of a hex in pixels,
00199      * up to where the next hex starts.
00200      * (i.e. not entirely from tip to tip -- use hex_size()
00201      * to get the distance from tip to tip)
00202      */
00203     int hex_width() const { return (zoom_*3)/4; }
00204 
00205     /**
00206      * Function which returns the size of a hex in pixels
00207      * (from top tip to bottom tip or left edge to right edge).
00208      */
00209     int hex_size() const { return zoom_; }
00210 
00211     /** Returns the current zoom factor. */
00212     double get_zoom_factor() const { return double(zoom_)/double(image::tile_size); }
00213 
00214     /**
00215      * given x,y co-ordinates of an onscreen pixel, will return the
00216      * location of the hex that this pixel corresponds to.
00217      * Returns an invalid location if the mouse isn't over any valid location.
00218      */
00219     const map_location hex_clicked_on(int x, int y) const;
00220 
00221     /**
00222      * given x,y co-ordinates of a pixel on the map, will return the
00223      * location of the hex that this pixel corresponds to.
00224      * Returns an invalid location if the mouse isn't over any valid location.
00225      */
00226     const map_location pixel_position_to_hex(int x, int y) const;
00227 
00228     /**
00229      * given x,y co-ordinates of the mouse, will return the location of the
00230      * hex in the minimap that the mouse is currently over, or an invalid
00231      * location if the mouse isn't over the minimap.
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     /** Function to invalidate the game status displayed on the sidebar. */
00242     void invalidate_game_status() { invalidateGameStatus_ = true; }
00243 
00244     /** Functions to get the on-screen positions of hexes. */
00245     int get_location_x(const map_location& loc) const;
00246     int get_location_y(const map_location& loc) const;
00247 
00248     /**
00249      * Rectangular area of hexes, allowing to decide how the top and bottom
00250      * edges handles the vertical shift for each parity of the x coordinate
00251      */
00252     struct rect_of_hexes{
00253         int left;
00254         int right;
00255         int top[2]; // for even and odd values of x, respectively
00256         int bottom[2];
00257 
00258         /**  very simple iterator to walk into the rect_of_hexes */
00259         struct iterator {
00260             iterator(const map_location &loc, const rect_of_hexes &rect)
00261                 : loc_(loc), rect_(rect){};
00262 
00263             /** increment y first, then when reaching bottom, increment x */
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     /** Return the rectangular area of hexes overlapped by r (r is in screen coordinates) */
00286     const rect_of_hexes hexes_under_rect(const SDL_Rect& r) const;
00287 
00288     /** Returns the rectangular area of visible hexes */
00289     const rect_of_hexes get_visible_hexes() const {return hexes_under_rect(map_area());};
00290 
00291     /** Returns true if location (x,y) is covered in shroud. */
00292     bool shrouded(const map_location& loc) const {
00293         return viewpoint_ && viewpoint_->shrouded(loc);
00294     }
00295     /** Returns true if location (x,y) is covered in fog. */
00296     bool fogged(const map_location& loc) const {
00297         return viewpoint_ && viewpoint_->fogged(loc);
00298     }
00299 
00300     /**
00301      * Determines whether a grid should be overlayed on the game board.
00302      * (to more clearly show where hexes are)
00303      */
00304     void set_grid(const bool grid) { grid_ = grid; }
00305 
00306     /** Getter for the x,y debug overlay on tiles */
00307     bool get_draw_coordinates() const { return draw_coordinates_; }
00308     /** Setter for the x,y debug overlay on tiles */
00309     void set_draw_coordinates(bool value) { draw_coordinates_ = value; }
00310 
00311     /** Getter for the terrain code debug overlay on tiles */
00312     bool get_draw_terrain_codes() const { return draw_terrain_codes_; }
00313     /** Setter for the terrain code debug overlay on tiles */
00314     void set_draw_terrain_codes(bool value) { draw_terrain_codes_ = value; }
00315 
00316     /** Save a (map-)screenshot and return the estimated file size */
00317     int screenshot(std::string filename, bool map_screenshot = false);
00318 
00319     /** Invalidates entire screen, including all tiles and sidebar. Calls redraw observers. */
00320     void redraw_everything();
00321 
00322     /** Adds a redraw observer, a function object to be called when redraw_everything is used */
00323     void add_redraw_observer(boost::function<void(display&)> f);
00324 
00325     /** Clear the redraw observers */
00326     void clear_redraw_observers();
00327 
00328     theme& get_theme() { return theme_; }
00329 
00330     /**
00331      * Retrieves a pointer to a theme UI button.
00332      *
00333      * @note The returned pointer may either be NULL, meaning the button
00334      *       isn't defined by the current theme, or point to a valid
00335      *       gui::button object. However, the objects retrieved will be
00336      *       destroyed and recreated by draw() method calls. Do *NOT* store
00337      *       these pointers for longer than strictly necessary to
00338      *       accomplish a specific task before the next screen refresh.
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     /** Function to invalidate all tiles. */
00350     void invalidate_all();
00351 
00352     /** Function to invalidate a specific tile for redrawing. */
00353     bool invalidate(const map_location& loc);
00354 
00355     bool invalidate(const std::set<map_location>& locs);
00356 
00357     /**
00358      * If this set is partially invalidated, invalidate all its hexes.
00359      * Returns if any new invalidation was needed
00360      */
00361     bool propagate_invalidation(const std::set<map_location>& locs);
00362 
00363     /** invalidate all hexes under the rectangle rect (in screen coordinates) */
00364     bool invalidate_locations_in_rect(const SDL_Rect& rect);
00365     bool invalidate_visible_locations_in_rect(const SDL_Rect& rect);
00366 
00367     /**
00368      * Function to invalidate animated terrains which may have changed.
00369      */
00370     virtual void invalidate_animations();
00371 
00372     /**
00373      * Per-location invalidation called by invalidate_animations()
00374      * defaults to no action, overridden by derived classes
00375      */
00376     virtual void invalidate_animations_location(const map_location& /*loc*/) {}
00377 
00378     const gamemap& get_map() const { return *map_; }
00379 
00380     /**
00381      * mouseover_hex_overlay_ require a prerendered surface
00382      * and is drawn underneath the mouse's location
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      * Debug function to toggle the "sunset" mode.
00392      * The map area become progressively darker,
00393      * except where hexes are refreshed.
00394      * delay is the number of frames between each darkening
00395      * (0 to toggle).
00396      */
00397     static void sunset(const size_t delay = 0);
00398 
00399     /** Toggle to continuously redraw the screen. */
00400     static void toggle_benchmark();
00401 
00402     /**
00403      * Toggle to debug foreground terrain.
00404      * Separate background and foreground layer
00405      * to better spot any error there.
00406      */
00407     static void toggle_debug_foreground();
00408 
00409     terrain_builder& get_builder() {return *builder_;};
00410 
00411     void flip();
00412 
00413     /** Copy the backbuffer to the framebuffer. */
00414     void update_display();
00415 
00416     /** Rebuild all dynamic terrain. */
00417     void rebuild_all();
00418 
00419     const theme::menu* menu_pressed();
00420 
00421     /**
00422      * Finds the menu which has a given item in it,
00423      * and enables or disables it.
00424      */
00425     void enable_menu(const std::string& item, bool enable);
00426 
00427     void set_diagnostic(const std::string& msg);
00428 
00429     /** Delay routines: use these not SDL_Delay (for --nogui). */
00430     void delay(unsigned int milliseconds) const;
00431 
00432     /**
00433      * Set/Get whether 'turbo' mode is on.
00434      * When turbo mode is on, everything moves much faster.
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     /** control unit idle animations and their frequency */
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      * Scrolls the display by xmov,ymov pixels.
00453      * Invalidation and redrawing will be scheduled.
00454      * @return true if the map actually moved.
00455      */
00456     bool scroll(int xmov, int ymov);
00457 
00458     /**
00459      * Zooms the display by the specified amount.
00460      * Negative values zoom out.
00461      * Note the amount should be a multiple of four,
00462      * otherwise the images might start to look odd
00463      * (hex_width() gets rounding errors).
00464      */
00465     void set_zoom(int amount);
00466 
00467     /** Sets the zoom amount to the default. */
00468     void set_default_zoom();
00469 
00470     enum SCROLL_TYPE { SCROLL, WARP, ONSCREEN, ONSCREEN_WARP };
00471 
00472     /**
00473      * Scroll such that location loc is on-screen.
00474      * WARP jumps to loc; SCROLL uses scroll speed;
00475      * ONSCREEN only scrolls if x,y is offscreen
00476      * force : scroll even if preferences tell us not to
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      * Scroll such that location loc1 is on-screen.
00482      * It will also try to make it such that loc2 is on-screen,
00483      * but this is not guaranteed. For ONSCREEN scrolls add_spacing
00484      * sets the desired minimum distance from the border in hexes.
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     /** Scroll to fit as many locations on-screen as possible, starting with the first. */
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     /** Expose the event, so observers can be notified about map scrolling. */
00497     events::generic_event &scroll_event() const { return scroll_event_; }
00498 
00499     events::generic_event& complete_redraw_event() { return complete_redraw_event_; }
00500 
00501     /** Check if a tile is fully visible on screen. */
00502     bool tile_fully_on_screen(const map_location& loc);
00503 
00504     /** Checks if location @a loc or one of the adjacent tiles is visible on screen. */
00505     bool tile_nearly_on_screen(const map_location &loc) const;
00506 
00507     /**
00508      * Draws invalidated items.
00509      * If update is true, will also copy the display to the frame buffer.
00510      * If force is true, will not skip frames, even if running behind.
00511      * Not virtual, since it gathers common actions. Calls various protected
00512      * virtuals (further below) to allow specialized behaviour in derived classes.
00513      */
00514     void draw(bool update=true, bool force=false);
00515 
00516     map_labels& labels();
00517     const map_labels& labels() const;
00518 
00519     /** Announce a message prominently. */
00520     void announce(const std::string& msg,
00521                const SDL_Color& color = font::GOOD_COLOR);
00522 
00523     /**
00524      * Schedule the minimap for recalculation.
00525      * Useful if any terrain in the map has changed.
00526      */
00527     void recalculate_minimap() {minimap_ = NULL; redrawMinimap_ = true; };
00528 
00529     /**
00530      * Schedule the minimap to be redrawn.
00531      * Useful if units have moved about on the map.
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      * Finds the start and end rows on the energy bar image.
00545      *
00546      * White pixels are substituted for the color of the energy.
00547      */
00548     const SDL_Rect& calculate_energy_bar(surface surf);
00549 
00550 
00551 protected:
00552     //TODO sort
00553     unit_map* units_;
00554 
00555     typedef std::map<map_location, std::string> exclusive_unit_draw_requests_t;
00556     /// map of hexes where only one unit should be drawn, the one identified by the associated id string
00557     exclusive_unit_draw_requests_t exclusive_unit_draw_requests_;
00558 
00559 
00560     /** Clear the screen contents */
00561     void clear_screen();
00562 
00563     /**
00564      * Called near the beginning of each draw() call.
00565      * Derived classes can use this to add extra actions before redrawing
00566      * invalidated hexes takes place. No action here by default.
00567      */
00568     virtual void pre_draw() {}
00569 
00570     /**
00571      * Called at the very end of each draw() call.
00572      * Derived classes can use this to add extra actions after redrawing
00573      * invalidated hexes takes place. No action here by default.
00574      */
00575     virtual void post_draw() {}
00576 
00577     /**
00578      * Get the clipping rectangle for drawing.
00579      * Virtual since the editor might use a slightly different approach.
00580      */
00581     virtual const SDL_Rect& get_clip_rect();
00582 
00583     /**
00584      * Only called when there's actual redrawing to do. Loops through
00585      * invalidated locations and redraws them. Derived classes can override
00586      * this, possibly to insert pre- or post-processing around a call to the
00587      * base class's function.
00588      */
00589     virtual void draw_invalidated();
00590 
00591     /**
00592      * Hook for actions to take right after draw() calls drawing_buffer_commit
00593      * No action here by default.
00594      */
00595     virtual void post_commit() {}
00596 
00597     /**
00598      * Redraws a single gamemap location.
00599      */
00600     virtual void draw_hex(const map_location& loc);
00601 
00602     /**
00603      * @returns the image type to be used for the passed hex
00604      */
00605     virtual image::TYPE get_image_type(const map_location& loc);
00606 
00607     /**
00608      * Called near the end of a draw operation, derived classes can use this
00609      * to render a specific sidebar. Very similar to post_commit.
00610      */
00611     virtual void draw_sidebar();
00612 
00613     /**
00614      * Draws the border tile overlay.
00615      * The routine determines by itself which border it is on
00616      * and draws an overlay accordingly. The definition of the
00617      * border is stored in the 'main_map_border' part of the theme.
00618      *
00619      * @param loc   the map location of the tile
00620      * @param xpos  the on-screen pixels x coordinate of the tile
00621      * @param ypos  the on-screen pixels y coordinate of the tile
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     /** Event raised when the map is being scrolled */
00671     mutable events::generic_event scroll_event_;
00672 
00673     /**
00674      * notify observers that the screen has been redrawn completely
00675      * atm this is used for replay_controller to add replay controls to the standard theme
00676      */
00677     events::generic_event complete_redraw_event_;
00678 
00679     /**
00680      * Holds the tick count for when the next drawing event is scheduled.
00681      * Drawing shouldn't occur before this time.
00682      */
00683     int nextDraw_;
00684 
00685     // Not set by the initializer:
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     // If we're transitioning from one time of day to the next,
00694     // then we will use these two masks on top of all hexes when we blit.
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     /** Local cache for preferences::animate_map, since it is constantly queried. */
00704     bool animate_map_;
00705 
00706     /** Local cache for preferences "local_tod_lighting" */
00707     bool local_tod_light_;
00708 
00709 public:
00710     /**
00711      * The layers to render something on. This value should never be stored
00712      * it's the internal drawing order and adding removing and reordering
00713      * the layers should be safe.
00714      * If needed in WML use the name and map that to the enum value.
00715      */
00716     enum tdrawing_layer{
00717         LAYER_TERRAIN_BG,          /**<
00718                                     * Layer for the terrain drawn behind the
00719                                     * unit.
00720                                     */
00721         LAYER_GRID_TOP,            /**< Top half part of grid image */
00722         LAYER_MOUSEOVER_OVERLAY,   /**< Mouseover overlay used by editor*/
00723         LAYER_FOOTSTEPS,           /**< Footsteps showing path from unit to mouse */
00724         LAYER_MOUSEOVER_TOP,       /**< Top half of image following the mouse */
00725         LAYER_UNIT_FIRST,          /**< Reserve layers to be selected for WML. */
00726         LAYER_UNIT_BG = LAYER_UNIT_FIRST+10,             /**< Used for the ellipse behind the unit. */
00727         LAYER_UNIT_DEFAULT=LAYER_UNIT_FIRST+40,/**<default layer for drawing units */
00728         LAYER_TERRAIN_FG = LAYER_UNIT_FIRST+50, /**<
00729                                     * Layer for the terrain drawn in front of
00730                                     * the unit.
00731                                     */
00732         LAYER_GRID_BOTTOM,         /**<
00733                                     * Used for the bottom half part of grid image.
00734                                     * Should be under moving units, to avoid masking south move.
00735                                     */
00736         LAYER_UNIT_MOVE_DEFAULT=LAYER_UNIT_FIRST+60/**<default layer for drawing moving units */,
00737         LAYER_UNIT_FG =  LAYER_UNIT_FIRST+80, /**<
00738                                     * Used for the ellipse in front of the
00739                                     * unit.
00740                                     */
00741         LAYER_UNIT_MISSILE_DEFAULT = LAYER_UNIT_FIRST+90, /**< default layer for missile frames*/
00742         LAYER_UNIT_LAST=LAYER_UNIT_FIRST+100,
00743         LAYER_REACHMAP,            /**< "black stripes" on unreachable hexes. */
00744         LAYER_MOUSEOVER_BOTTOM,    /**< Bottom half of image following the mouse */
00745         LAYER_FOG_SHROUD,          /**< Fog and shroud. */
00746         LAYER_ARROWS,              /**< Arrows from the arrows framework. Used for planned moves display. */
00747         LAYER_ACTIONS_NUMBERING,   /**< Move numbering for the whiteboard. */
00748         LAYER_SELECTED_HEX,        /**< Image on the selected unit */
00749         LAYER_ATTACK_INDICATOR,    /**< Layer which holds the attack indicator. */
00750         LAYER_UNIT_BAR,            /**<
00751                                     * Unit bars and overlays are drawn on this
00752                                     * layer (for testing here).
00753                                     */
00754         LAYER_MOVE_INFO,           /**< Movement info (defense%, ect...). */
00755         LAYER_LINGER_OVERLAY,      /**< The overlay used for the linger mode. */
00756         LAYER_BORDER,              /**< The border of the map. */
00757 
00758         LAYER_LAST_LAYER           /**<
00759                                     * Don't draw to this layer it's a dummy to
00760                                     * size the vector.
00761                                     */
00762         };
00763 
00764     /**
00765      * Draw an image at a certain location.
00766      * x,y: pixel location on screen to draw the image
00767      * image: the image to draw
00768      * reverse: if the image should be flipped across the x axis
00769      * greyscale: used for instance to give the petrified appearance to a unit image
00770      * alpha: the merging to use with the background
00771      * blendto: blend to this color using blend_ratio
00772      * submerged: the amount of the unit out of 1.0 that is submerged
00773      *            (presumably under water) and thus shouldn't be drawn
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      * Draw text on a hex. (0.5, 0.5) is the center.
00783      * The font size is adjusted to the zoom factor
00784      * and divided by 2 for tiny-gui.
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     //TODO sort
00793     size_t activeTeam_;
00794 
00795     /**
00796      * In order to render a hex properly it needs to be rendered per row. On
00797      * this row several layers need to be drawn at the same time. Mainly the
00798      * unit and the background terrain. This is needed since both can spill
00799      * in the next hex. The foreground terrain needs to be drawn before to
00800      * avoid decapitation a unit.
00801      *
00802      * In other words:
00803      * for every layer
00804      *   for every row (starting from the top)
00805      *     for every hex in the row
00806      *       ...
00807      *
00808      * this is modified to:
00809      * for every layer group
00810      *   for every row (starting from the top)
00811      *     for every layer in the group
00812      *       for every hex in the row
00813      *         ...
00814      *
00815      * * Surfaces are rendered per level in a map.
00816      * * Per level the items are rendered per location these locations are
00817      *   stored in the drawing order required for units.
00818      * * every location has a vector with surfaces, each with its own screen
00819      *   coordinate to render at.
00820      * * every vector element has a vector with surfaces to render.
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     /** Helper structure for rendering the terrains. */
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_;                      /**< x screen coordinate to render at. */
00863         int y_;                      /**< y screen coordinate to render at. */
00864         std::vector<surface> surf_;  /**< surface(s) to render. */
00865         SDL_Rect clip_;              /**<
00866                                       * The clipping area of the source if
00867                                       * ommitted the entire source is used.
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      * Add an item to the drawing buffer. You need to update screen on affected area
00879      *
00880      * @param layer              The layer to draw on.
00881      * @param loc                The hex the image belongs to, needed for the
00882      *                           drawing order.
00883      * @param blit               The structure to blit.
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     /** Draws the drawing_buffer_ and clears it. */
00897     void drawing_buffer_commit();
00898 
00899     /** Clears the drawing buffer. */
00900     void drawing_buffer_clear();
00901 
00902     /** redraw all panels associated with the map display */
00903     void draw_all_panels();
00904 
00905     /**
00906      * Initiate a redraw.
00907      *
00908      * Invalidate controls and panels when changed after they have been drawn
00909      * initially. Useful for dynamic theme modification.
00910      */
00911     void draw_init();
00912     void draw_wrap(bool update,bool force);
00913 
00914     /** Used to indicate to drawing functions that we are doing a map screenshot */
00915     bool map_screenshot_;
00916 
00917 public: //operations for the arrow framework
00918 
00919     void add_arrow(arrow&);
00920 
00921     void remove_arrow(arrow&);
00922 
00923     /** Called by arrow objects when they change. You should not need to call this directly. */
00924     void update_arrow(arrow & a);
00925 
00926 private:
00927     /** Handle for the label which displays frames per second. */
00928     int fps_handle_;
00929     /** Count work done for the debug info displayed under fps */
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     /** Debug flag - overlay x,y coords on tiles */
00941     bool draw_coordinates_;
00942     /** Debug flag - overlay terrain codes on tiles */
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     /** Maps the list of arrows for each location */
00948     arrows_map_t arrows_map_;
00949 
00950     tod_color color_adjust_;
00951 
00952 #if defined(__GLIBC__)
00953     /** Flag for bug #17573 - this is set in the constructor **/
00954     bool do_reverse_memcpy_workaround_;
00955 #endif
00956 
00957 protected:
00958     static display * singleton_;
00959 };
00960 
00961 #endif
00962 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:33 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs