game_display.hpp

Go to the documentation of this file.
00001 /* $Id: game_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 /** @file */
00017 
00018 #ifndef GAME_DISPLAY_H_INCLUDED
00019 #define GAME_DISPLAY_H_INCLUDED
00020 
00021 class config;
00022 class tod_manager;
00023 class team;
00024 class unit;
00025 class unit_map;
00026 
00027 #include "animated.hpp"
00028 #include "chat_events.hpp"
00029 #include "display.hpp"
00030 #include "pathfind/pathfind.hpp"
00031 
00032 #include <deque>
00033 
00034 // This needs to be separate from display.h because of the static
00035 // singleton member, which will otherwise trigger link failure
00036 // when building the editor.
00037 
00038 class game_display : public display
00039 {
00040 public:
00041     game_display(unit_map& units, CVideo& video,
00042             const gamemap& map, const tod_manager& tod_manager,
00043             const std::vector<team>& t, const config& theme_cfg,
00044             const config& level);
00045 
00046     static game_display* create_dummy_display(CVideo& video);
00047 
00048     ~game_display();
00049     static game_display* get_singleton()
00050     {
00051         return static_cast<game_display*>(singleton_);
00052     }
00053 
00054     /**
00055      * Update lighting settings.
00056      *
00057      * Should be called on every new turn.
00058      */
00059     void new_turn();
00060 
00061     /**
00062      * Scrolls to the leader of a certain side.
00063      *
00064      * This will normally be the playing team.
00065      */
00066     void scroll_to_leader(unit_map& units, int side, SCROLL_TYPE scroll_type = ONSCREEN,bool force = true);
00067 
00068     /**
00069      * Function to display a location as selected.
00070      *
00071      * If a unit is in the location, and there is no unit in the currently
00072      * highlighted hex, the unit will be displayed in the sidebar.
00073      */
00074     virtual void select_hex(map_location hex);
00075 
00076     /**
00077      * Function to highlight a location.
00078      *
00079      * If a unit is in the location, it will be displayed in the sidebar.
00080      * Selection is used when a unit has been clicked on, while highlighting is
00081      * used when a location has been moused over.
00082      */
00083     virtual void highlight_hex(map_location hex);
00084 
00085     /**
00086      * Change the unit to be displayed in the sidebar.
00087      *
00088      * This is used when selecting or highlighting is not wanted.
00089      */
00090     void display_unit_hex(map_location hex);
00091 
00092     /**
00093      * Sets the paths that are currently displayed as available
00094      * for the unit to move along.
00095      * All other paths will be grayed out.
00096      */
00097     void highlight_reach(const pathfind::paths &paths_list);
00098 
00099     /**
00100      * Add more paths to highlight.  Print numbers where they overlap.
00101      * Used only by Show Enemy Moves.
00102      */
00103   void highlight_another_reach(const pathfind::paths &paths_list);
00104 
00105     /** Reset highlighting of paths. */
00106     void unhighlight_reach();
00107 
00108     /**
00109      * Sets the route along which footsteps are drawn to show movement of a
00110      * unit. If NULL, no route is displayed. @a route does not have to remain
00111      * valid after being set.
00112      */
00113   void set_route(const pathfind::marked_route *route);
00114 
00115     /** Function to float a label above a tile */
00116     void float_label(const map_location& loc, const std::string& text,
00117                      int red, int green, int blue);
00118 
00119     /**
00120      * Function to return 2 half-hex footsteps images for the given location.
00121      * Only loc is on the current route set by set_route.
00122      */
00123     std::vector<surface> footsteps_images(const map_location& loc);
00124 
00125     /** Draws the movement info (turns available) for a given location. */
00126     void draw_movement_info(const map_location& loc);
00127 
00128     void draw_report(const std::string &report_name);
00129 
00130     /** Function to invalidate that unit status displayed on the sidebar. */
00131     void invalidate_unit() { invalidateGameStatus_ = true; }
00132 
00133     /** Same as invalidate_unit() if moving the displayed unit. */
00134     void invalidate_unit_after_move(const map_location& src, const map_location& dst);
00135 
00136     const time_of_day& get_time_of_day(const map_location& loc) const;
00137 
00138     bool has_time_area() const;
00139 
00140 protected:
00141     /**
00142      * game_display pre_draw does specific things related e.g. to unit rendering
00143      * and calls the whiteboard pre-draw method.
00144      */
00145     void pre_draw();
00146     /**
00147      * Calls the whiteboard's post-draw method.
00148      */
00149     void post_draw();
00150 
00151     void draw_invalidated();
00152 
00153     void post_commit();
00154 
00155     void draw_hex(const map_location& loc);
00156 
00157     /**
00158      * Animated hex invalidation specific to gameplay
00159      */
00160     void invalidate_animations();
00161 
00162     /**
00163      * Extra game per-location invalidation (village ownership)
00164      */
00165     void invalidate_animations_location(const map_location& loc);
00166 
00167 
00168 public:
00169     /** A temporary unit that can be placed on the map.
00170         Temporary units can overlap units.
00171         Adding the same unit twice isn't allowed.
00172         The fake_unit owns its underlying unit and when
00173         it goes out of scope it removes itself from the fake_units list.
00174         The intent is to provide exception safety when the code
00175         creating the temp unit is unexpectedly forced out of scope.
00176      */
00177     class fake_unit : public unit {
00178     public:
00179         explicit fake_unit(unit const & u);
00180         fake_unit(fake_unit const & u);
00181         fake_unit & operator=(fake_unit const & u);
00182         ///If already in the queue, the copied unit will replace the one in the queue
00183         fake_unit & operator=(unit const & u);
00184 
00185         ///Removes from fake_units list if necessary
00186         ~fake_unit();
00187         /**Place on the fake_units deque at the end of the deque
00188          * drawn last over all other units.
00189          * Duplicate additions are not allowed.
00190          */
00191         void place_on_game_display(game_display * d);
00192         /** Removes any instances of this temporary unit from the temporary unit vector.
00193          *  Returns the number of temp units deleted (0 or 1, any other number indicates an error).
00194          */
00195         int remove_from_game_display();
00196     private :
00197         game_display * my_display_;
00198     };
00199 
00200     //Anticipate making place_temporary_unit and remove_temporary_unit private to force exception safety
00201     friend class game_display::fake_unit;
00202 
00203 private:
00204     /** Temporarily place a unit on map (moving: can overlap others).
00205      *  The temp unit is added at the end of the temporary unit deque,
00206      *  and therefore gets drawn last, over other units and temp units.
00207      *  Adding the same unit twice isn't allowed.
00208      */
00209     void place_temporary_unit(unit *u);
00210 
00211     /** Removes any instances of this temporary unit from the temporary unit vector.
00212      *  Returns the number of temp units deleted (0 or 1, any other number indicates an error).
00213      */
00214     int remove_temporary_unit(unit *u);
00215 public:
00216 
00217 
00218 
00219     /** Set the attack direction indicator. */
00220     void set_attack_indicator(const map_location& src, const map_location& dst);
00221     void clear_attack_indicator();
00222 
00223     /** Function to get attack direction suffix. */
00224     std::string attack_indicator_direction() const {
00225         return map_location::write_direction(
00226             attack_indicator_src_.get_relative_dir(attack_indicator_dst_));
00227     }
00228 
00229     /**
00230      * Functions to add and remove overlays from locations.
00231      *
00232      * An overlay is an image that is displayed on top of the tile.
00233      * One tile may have multiple overlays.
00234      */
00235     void add_overlay(const map_location& loc, const std::string& image,
00236         const std::string& halo="", const std::string& team_name="",
00237         bool visible_under_fog = true);
00238 
00239     /** remove_overlay will remove all overlays on a tile. */
00240     void remove_overlay(const map_location& loc);
00241 
00242     /** remove_single_overlay will remove a single overlay from a tile */
00243     void remove_single_overlay(const map_location& loc, const std::string& toDelete);
00244 
00245     /**
00246      * Check the overlay_map for proper team-specific overlays to be
00247      * displayed/hidden
00248      */
00249     void parse_team_overlays();
00250 
00251     // Functions used in the editor:
00252 
00253     //void draw_terrain_palette(int x, int y, terrain_type::TERRAIN selected);
00254     t_translation::t_terrain get_terrain_on(int palx, int paly, int x, int y);
00255 
00256     void send_notification(const std::string& owner, const std::string& message);
00257 
00258     /**
00259      * Sets the team controlled by the player using the computer.
00260      *
00261      * Data from this team will be displayed in the game status.
00262      * set_playing_team sets the team whose turn it currently is
00263      */
00264     void set_team(size_t team, bool observe=false);
00265     void set_playing_team(size_t team);
00266 
00267 
00268     const map_location &displayed_unit_hex() const { return displayedUnitHex_; }
00269 
00270 
00271     /**
00272      * annotate hex with number, useful for debugging or UI prototype
00273      */
00274     static int& debug_highlight(const map_location& loc);
00275     static void clear_debug_highlights() { debugHighlights_.clear(); }
00276 
00277 
00278     /** The playing team is the team whose turn it is. */
00279     int playing_side() const { return activeTeam_ + 1; }
00280 
00281 
00282     std::string current_team_name() const;
00283 
00284     void add_observer(const std::string& name) { observers_.insert(name); }
00285     void remove_observer(const std::string& name) { observers_.erase(name); }
00286     const std::set<std::string>& observers() const { return observers_; }
00287 
00288     void add_chat_message(const time_t& time, const std::string& speaker,
00289         int side, const std::string& msg, events::chat_handler::MESSAGE_TYPE type, bool bell);
00290     void clear_chat_messages() { prune_chat_messages(true); }
00291 
00292     void begin_game();
00293 
00294     virtual bool in_game() const { return in_game_; }
00295 
00296 
00297     /**
00298      * Sets the linger mode for the display.
00299      * There have been some discussions on what to do with fog and shroud
00300      * the extra variables make it easier to modify the behaviour. There
00301      * might even be a split between victory and defeat.
00302      *
00303      * @todo if the current implementation is wanted we can change
00304      * the stuff back to a boolean
00305      */
00306     enum tgame_mode {
00307         RUNNING,         /**< no linger overlay, show fog and shroud. */
00308         LINGER_SP,       /**< linger overlay, show fog and shroud. */
00309         LINGER_MP };     /**< linger overlay, show fog and shroud. */
00310 
00311     void set_game_mode(const tgame_mode game_mode);
00312 
00313 private:
00314     game_display(const game_display&);
00315     void operator=(const game_display&);
00316 
00317     void draw_sidebar();
00318 
00319     // This surface must be freed by the caller
00320     surface get_flag(const map_location& loc);
00321 
00322     /// collection of units destined to be drawn but not put into the unit map
00323     std::deque<unit*> fake_units_;
00324 
00325     // Locations of the attack direction indicator's parts
00326     map_location attack_indicator_src_;
00327     map_location attack_indicator_dst_;
00328 
00329 
00330 
00331     pathfind::marked_route route_;
00332 
00333     const tod_manager& tod_manager_;
00334 
00335     const config& level_;
00336 
00337     void invalidate_route();
00338 
00339     map_location displayedUnitHex_;
00340 
00341     struct overlay {
00342         overlay(const std::string& img, const std::string& halo_img,
00343                 int handle, const std::string& overlay_team_name, const bool fogged) : image(img), halo(halo_img),
00344                 team_name(overlay_team_name), halo_handle(handle) , visible_in_fog(fogged){}
00345         std::string image;
00346         std::string halo;
00347         std::string team_name;
00348         int halo_handle;
00349         bool visible_in_fog;
00350     };
00351 
00352     typedef std::multimap<map_location,overlay> overlay_map;
00353 
00354     overlay_map overlays_;
00355 
00356 
00357 
00358     double sidebarScaling_;
00359 
00360     bool first_turn_, in_game_;
00361 
00362     std::set<std::string> observers_;
00363 
00364     struct chat_message
00365     {
00366         chat_message(int speaker, int h) : speaker_handle(speaker), handle(h), created_at(SDL_GetTicks())
00367         {}
00368 
00369         int speaker_handle;
00370         int handle;
00371         Uint32 created_at;
00372     };
00373 
00374     void prune_chat_messages(bool remove_all=false);
00375 
00376     std::vector<chat_message> chat_messages_;
00377 
00378     // Tiles lit for showing where unit(s) can reach
00379     typedef std::map<map_location,unsigned int> reach_map;
00380     reach_map reach_map_;
00381     reach_map reach_map_old_;
00382     bool reach_map_changed_;
00383     void process_reachmap_changes();
00384 
00385     tgame_mode game_mode_;
00386 
00387     // For debug mode
00388     static std::map<map_location, int> debugHighlights_;
00389 
00390     /** Animated flags for each team */
00391     std::vector<animated<image::locator> > flags_;
00392 
00393 
00394 };
00395 
00396 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:52 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs