image.hpp

Go to the documentation of this file.
00001 /* $Id: image.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
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 #ifndef IMAGE_HPP_INCLUDED
00017 #define IMAGE_HPP_INCLUDED
00018 
00019 #include "map_location.hpp"
00020 #include "sdl_utils.hpp"
00021 #include "terrain_translation.hpp"
00022 
00023 ///this module manages the cache of images. With an image name, you can get
00024 ///the surface corresponding to that image.
00025 //
00026 namespace image {
00027     const int tile_size = 72;
00028 
00029     template<typename T>
00030     class cache_type;
00031 
00032     //a generic image locator. Abstracts the location of an image.
00033     class locator
00034     {
00035     public:
00036         enum type { NONE, FILE, SUB_FILE };
00037     private:
00038         // Called by each constructor after actual construction to
00039         // initialize the index_ field
00040         void init_index();
00041         void parse_arguments();
00042         struct value {
00043             value();
00044             value(const value &a);
00045             value(const char *filename);
00046             value(const std::string& filename);
00047             value(const std::string& filename, const std::string& modifications);
00048             value(const std::string& filename, const map_location& loc, int center_x, int center_y, const std::string& modifications);
00049 
00050             bool operator==(const value& a) const;
00051             bool operator<(const value& a) const;
00052 
00053             type type_;
00054             std::string filename_;
00055             map_location loc_;
00056             std::string modifications_;
00057             int center_x_;
00058             int center_y_;
00059         };
00060 
00061         friend size_t hash_value(const value&);
00062 
00063     public:
00064 
00065         /**
00066          * @todo replace this with std::unordered_map<value, int> or boost::unordered_map<value, int>
00067          *       boost::unordered_map can almost just be dropped in as boost::hash<T>(T val) will return hash_value(val), but it
00068          *       requires boost 1.35 (preferably 1.36 or later)
00069          *
00070          **/
00071         typedef std::map<size_t, std::map<value, int> > locator_finder_t;
00072 
00073         // Constructing locators is somewhat slow, accessing image
00074         // through locators is fast. The idea is that calling functions
00075         // should store locators, and not strings to construct locators
00076         // (the second will work, of course, but will be slower)
00077             locator();
00078         locator(const locator &a, const std::string &mods ="");
00079         locator(const char *filename);
00080         locator(const std::string& filename);
00081         locator(const std::string& filename, const std::string& modifications);
00082         locator(const std::string &filename, const map_location &loc,
00083         int center_x, int center_y, const std::string& modifications = "");
00084 
00085         locator& operator=(const locator &a);
00086         bool operator==(const locator &a) const { return index_ == a.index_; }
00087         bool operator!=(const locator &a) const { return index_ != a.index_; }
00088         bool operator<(const locator &a) const { return index_ < a.index_; }
00089 
00090         const std::string &get_filename() const { return val_.filename_; }
00091         const map_location& get_loc() const { return val_.loc_ ; }
00092         int get_center_x() const { return val_.center_x_; }
00093         int get_center_y() const { return val_.center_y_; }
00094         const std::string& get_modifications() const {return val_.modifications_;}
00095         type get_type() const { return val_.type_; };
00096         // const int get_index() const { return index_; };
00097 
00098         // returns true if the locator does not correspond to any
00099         // actual image
00100         bool is_void() const { return val_.type_ == NONE; }
00101 
00102         /**
00103          * Tests whether the file the locater points at exists.
00104          *
00105          * is_void doesn't seem to work before the image is loaded and also in
00106          * debug mode a placeholder is returned. So it's not possible to test
00107          * for the existence of a file. So this function does that. (Note it
00108          * tests for existence not whether or not it's a valid image.)
00109          *
00110          * @return                Whether or not the file exists.
00111          */
00112         bool file_exists() const;
00113 
00114         // loads the image it is pointing to from the disk
00115         surface load_from_disk() const;
00116 
00117         template <typename T>
00118         bool in_cache(cache_type<T> &cache) const;
00119         template <typename T>
00120         T &access_in_cache(cache_type<T> &cache) const;
00121         template <typename T>
00122         const T &locate_in_cache(cache_type<T> &cache) const;
00123         template <typename T>
00124         void add_to_cache(cache_type<T> &cache, const T &data) const;
00125 
00126     private:
00127 
00128         surface load_image_file() const;
00129         surface load_image_sub_file() const;
00130 
00131         int index_;
00132         value val_;
00133     };
00134 
00135     size_t hash_value(const locator::value&);
00136 
00137 
00138     typedef cache_type<surface> image_cache;
00139     typedef cache_type<bool> bool_cache;
00140 
00141     typedef std::map<t_translation::t_terrain, surface> mini_terrain_cache_map;
00142     extern mini_terrain_cache_map mini_terrain_cache;
00143     extern mini_terrain_cache_map mini_fogged_terrain_cache;
00144 
00145     ///light_string store colors info of central and adjacents hexes.
00146     ///The structure is one or several 4 chars blocks (L,R,G,B)
00147     ///where RGB is the color and L is the lightmap to use:
00148     ///(-1:none, 0-5:transition in each direction, 6:full hex)
00149     typedef std::basic_string<signed char> light_string;
00150     ///return light_string of one light operation(see above)
00151     light_string get_light_string(int op, int r, int g, int b);
00152 
00153     // pair each light possibilty with its lighted surface
00154     typedef std::map<light_string, surface> lit_variants;
00155     // lighted variants for each locator
00156     typedef cache_type<lit_variants> lit_cache;
00157 
00158     void flush_cache();
00159 
00160     ///the image manager is responsible for setting up images, and destroying
00161     ///all images when the program exits. It should probably
00162     ///be created once for the life of the program
00163     struct manager
00164     {
00165         manager();
00166         ~manager();
00167     };
00168 
00169     ///will make all scaled images have these rgb values added to all
00170     ///their pixels. i.e. add a certain color hint to images. useful
00171     ///for representing day/night. Invalidates all scaled images.
00172     void set_color_adjustment(int r, int g, int b);
00173 
00174     class color_adjustment_resetter
00175     {
00176         public:
00177             color_adjustment_resetter();
00178             void reset();
00179         private:
00180             int r_, g_, b_;
00181     };
00182 
00183     ///set the team colors used by the TC image modification
00184     ///use a vector with one string for each team
00185     ///using NULL will reset to default TC
00186     void set_team_colors(const std::vector<std::string>* colors = NULL);
00187 
00188     const std::vector<std::string>& get_team_colors();
00189 
00190     ///sets the pixel format used by the images. Is called every time the
00191     ///video mode changes. Invalidates all images.
00192     void set_pixel_format(SDL_PixelFormat* format);
00193 
00194     ///sets the amount scaled images should be scaled. Invalidates all
00195     ///scaled images.
00196     void set_zoom(int zoom);
00197 
00198     /// UNSCALED : image will be drawn "as is" without changing size, even in case of redraw
00199     /// SCALED_TO_ZOOM : image will be scaled taking zoom into account
00200     /// HEXED : the hex mask is applied on the image
00201     /// SCALED_TO_HEX : image will be scaled to fit into a hex, taking zoom into account
00202     /// TOD_COLORED : same as SCALED_TO_HEX but ToD coloring is also applied
00203     /// BRIGHTENED  : same as TOD_COLORED but also brightened
00204     enum TYPE { UNSCALED, SCALED_TO_ZOOM, HEXED, SCALED_TO_HEX, TOD_COLORED, BRIGHTENED};
00205 
00206     ///function to get the surface corresponding to an image.
00207     ///note that this surface must be freed by the user by calling
00208     ///SDL_FreeSurface()
00209     surface get_image(const locator& i_locator, TYPE type=UNSCALED);
00210 
00211     ///function to get the surface corresponding to an image.
00212     ///after applying the lightmap encoded in ls
00213     ///type should be HEXED or SCALED_TO_HEX
00214     surface get_lighted_image(const image::locator& i_locator, const light_string& ls, TYPE type);
00215 
00216     ///function to get the standard hex mask
00217     surface get_hexmask();
00218 
00219     ///function to check if an image fit into an hex
00220     ///return false if the image has not the standard size.
00221     bool is_in_hex(const locator& i_locator);
00222 
00223     ///function to check if an image is empty after hex cut
00224     ///should be only used on terrain image (cache the hex cut version)
00225     bool is_empty_hex(const locator& i_locator);
00226 
00227     ///function to reverse an image. The image MUST have originally been returned from
00228     ///an image:: function. Returned images have the same semantics as for get_image()
00229     ///and must be freed using SDL_FreeSurface()
00230     surface reverse_image(const surface &surf);
00231 
00232     ///returns true if the given image actually exists, without loading it.
00233     bool exists(const locator& i_locator);
00234 
00235     /// precache the existence of files in the subdir (ex: "terrain/")
00236     void precache_file_existence(const std::string& subdir = "");
00237     bool precached_file_exists(const std::string& file);
00238 }
00239 
00240 #endif
00241 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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