image.hpp

Go to the documentation of this file.
00001 /* $Id: image.hpp 26213 2008-04-28 16:54:29Z mog $ */
00002 /*
00003    Copyright (C) 2003 - 2008 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 version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 #ifndef IMAGE_HPP_INCLUDED
00016 #define IMAGE_HPP_INCLUDED
00017 
00018 #include "map.hpp"
00019 #include "sdl_utils.hpp"
00020 
00021 #include "SDL.h"
00022 #include <string>
00023 #include <vector>
00024 
00025 ///this module manages the cache of images. With an image name, you can get
00026 ///the surface corresponding to that image.
00027 //
00028 ///images come in a number of varieties:
00029 /// - unscaled: no modifications have been done on the image.
00030 /// - scaled_to_hex: images are scaled to the size of a tile
00031 /// - scaled_to_zoom: images are scaled to the zoom factor, factor 1 is same as unscaled no other modifications are done
00032 /// - unmasked: images are scaled, but have no time of day masking applied to them
00033 /// - brightened: images are scaled and brighter than normal.
00034 namespace image {
00035 #ifdef USE_TINY_GUI
00036     // images in tiny-gui will be scaled at install time
00037     const int tile_size = 36;
00038 #else
00039     const int tile_size = 72;
00040 #endif
00041 
00042     template<typename T>
00043     struct cache_item {
00044         cache_item() : loaded(false), item() {}
00045         cache_item(T item) : loaded(true), item(item) {}
00046 
00047         bool loaded;
00048         T item;
00049     };
00050 
00051     //a generic image locator. Abstracts the location of an image.
00052     class locator
00053     {
00054     private:
00055         // Called by each constructor after actual construction to
00056         // initialize the index_ field
00057         void init_index();
00058         void parse_arguments();
00059     public:
00060         enum type { NONE, FILE, SUB_FILE };
00061 
00062         struct value {
00063             value();
00064             value(const value &a);
00065             value(const char *filename);
00066             value(const char *filename, const std::string& modifications);
00067             value(const std::string& filename);
00068              value(const std::string& filename, const std::string& modifications);
00069             value(const std::string& filename, const gamemap::location& loc, const std::string& modifications);
00070             value(const std::string& filename, const gamemap::location& loc, int center_x, int center_y, const std::string& modifications);
00071 
00072             bool operator==(const value& a) const;
00073             bool operator<(const value& a) const;
00074 
00075             type type_;
00076             std::string filename_;
00077             gamemap::location loc_;
00078             std::string modifications_;
00079             int center_x_;
00080             int center_y_;
00081         };
00082 
00083         // Constructing locators is somewhat slow, accessing image
00084         // through locators is fast. The idea is that calling functions
00085         // should store locators, and not strings to construct locators
00086         // (the second will work, of course, but will be slower)
00087             locator();
00088         locator(const locator &a, const std::string &mods ="");
00089         locator(const char *filename);
00090         locator(const char *filename, const std::string& modifications);
00091         locator(const std::string& filename);
00092         locator(const std::string& filename, const std::string& modifications);
00093         locator(const std::string& filename, const gamemap::location& loc, const std::string& modifications="");
00094         locator(const std::string& filename, const gamemap::location& loc, int center_x, int center_y, const std::string& modifications="");
00095 
00096         locator& operator=(const locator &a);
00097         bool operator==(const locator &a) const { return index_ == a.index_; }
00098         bool operator!=(const locator &a) const { return index_ != a.index_; }
00099         bool operator<(const locator &a) const { return index_ < a.index_; }
00100 
00101         const std::string &get_filename() const { return val_.filename_; }
00102         const gamemap::location& get_loc() const { return val_.loc_ ; }
00103         const std::string& get_modifications() const {return val_.modifications_;}
00104         type get_type() const { return val_.type_; };
00105         // const int get_index() const { return index_; };
00106 
00107         // returns true if the locator does not correspond to any
00108         // actual image
00109         bool is_void() const { return val_.type_ == NONE; }
00110         // loads the image it is pointing to from the disk
00111         surface load_from_disk() const;
00112 
00113 #if 0
00114         // returns true if the locator already was stored in the given
00115         // cache
00116         template<typename T>
00117         bool in_cache(const std::vector<cache_item<T> >& cache) const;
00118         // returns the image it is corresponding to in the given cache
00119         template<typename T>
00120         T locate_in_cache(const std::vector<cache_item<T> >& cache) const;
00121         // adds the given image to the given cache, indexed with the
00122         // current locator
00123         template<typename T>
00124         void add_to_cache(std::vector<cache_item<T> >& cache, const T &image) const;
00125 #endif
00126         bool in_cache(const std::vector<cache_item<surface> >& cache) const
00127             { return index_ == -1 ? false : cache[index_].loaded; }
00128         surface locate_in_cache(const std::vector<cache_item<surface> >& cache) const
00129             { return index_ == -1 ? surface() : cache[index_].item; }
00130         void add_to_cache(std::vector<cache_item<surface> >& cache, const surface &image) const
00131             { if(index_ != -1 ) cache[index_] = cache_item<surface>(image); }
00132         bool in_cache(const std::vector<cache_item<locator> >& cache) const
00133             { return index_ == -1 ? false : cache[index_].loaded; }
00134         locator locate_in_cache(const std::vector<cache_item<locator> >& cache) const
00135             { return index_ == -1 ? locator() : cache[index_].item; }
00136         void add_to_cache(std::vector<cache_item<locator> >& cache, const locator &image) const
00137             { if(index_ != -1) cache[index_] = cache_item<locator>(image); }
00138     protected:
00139         static int last_index_;
00140     private:
00141 
00142         surface load_image_file() const;
00143         surface load_image_sub_file() const;
00144 
00145         int index_;
00146         value val_;
00147     };
00148 
00149 
00150     typedef std::vector<cache_item<surface> > image_cache;
00151     typedef std::vector<cache_item<locator> > locator_cache;
00152     typedef std::map<t_translation::t_terrain, surface> mini_terrain_cache_map;
00153     extern mini_terrain_cache_map mini_terrain_cache;
00154     extern mini_terrain_cache_map mini_fogged_terrain_cache;
00155 
00156     void flush_cache();
00157 
00158     ///the image manager is responsible for setting up images, and destroying
00159     ///all images when the program exits. It should probably
00160     ///be created once for the life of the program
00161     struct manager
00162     {
00163         manager();
00164         ~manager();
00165     };
00166 
00167     ///function to set the program's icon to the window manager.
00168     ///must be called after SDL_Init() is called, but before setting the
00169     ///video mode
00170     void set_wm_icon();
00171 
00172     ///will make all scaled images have these rgb values added to all
00173     ///their pixels. i.e. add a certain colour hint to images. useful
00174     ///for representing day/night. Invalidates all scaled images.
00175     void set_colour_adjustment(int r, int g, int b);
00176 
00177     ///set the team colors used by the TC image modification
00178     ///use a vector with one string for each team
00179     ///using NULL will reset to default TC
00180     void set_team_colors(const std::vector<std::string>* colors = NULL);
00181 
00182     ///function which sets a certain image as a 'mask' for all scaled images.
00183     ///the 'mask' is blitted onto all scaled images.
00184     void set_image_mask(const std::string& image_name);
00185 
00186     extern SDL_PixelFormat* pixel_format;
00187 
00188     ///sets the pixel format used by the images. Is called every time the
00189     ///video mode changes. Invalidates all images.
00190     void set_pixel_format(SDL_PixelFormat* format);
00191 
00192     ///sets the amount scaled images should be scaled. Invalidates all
00193     ///scaled images.
00194     void set_zoom(int zoom);
00195 
00196     // unscaled : image will be drawn "as is" without changing size, even in case of redraw
00197     // hexed : the hex mask is applied on the image
00198     // unmasked : image will be scaled to fit into a hex, taking zoom into account
00199     // scaled_to_hex : same but ToD coloring is also applied
00200     enum TYPE { UNSCALED, HEXED, UNMASKED, SCALED_TO_HEX, SCALED_TO_ZOOM, BRIGHTENED, SEMI_BRIGHTENED };
00201 
00202 
00203     ///function to get the surface corresponding to an image.
00204     ///note that this surface must be freed by the user by calling
00205     ///SDL_FreeSurface()
00206     surface get_image(const locator& i_locator, TYPE type=UNSCALED, bool add_to_cache = true);
00207 
00208     ///function to reverse an image. The image MUST have originally been returned from
00209     ///an image:: function. Returned images have the same semantics as for get_image()
00210     ///and must be freed using SDL_FreeSurface()
00211     surface reverse_image(const surface &surf);
00212 
00213     //returns true if the given image actually exists, without loading it.
00214     bool exists(const locator& i_locator);
00215 }
00216 
00217 #endif
00218 

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs