The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
image.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef IMAGE_HPP_INCLUDED
16 #define IMAGE_HPP_INCLUDED
17 
18 #include "map/location.hpp"
19 #include "sdl/utils.hpp"
20 #include "sdl/image.hpp"
21 #include "terrain/translation.hpp"
22 #include "game_config.hpp"
23 
24 #include <boost/unordered_map.hpp>
25 
26 
27 ///this module manages the cache of images. With an image name, you can get
28 ///the surface corresponding to that image.
29 //
30 namespace image {
31  template<typename T>
32  class cache_type;
33 
34  //a generic image locator. Abstracts the location of an image.
35  class locator
36  {
37  public:
38  enum type { NONE, FILE, SUB_FILE };
39  private:
40  // Called by each constructor after actual construction to
41  // initialize the index_ field
42  void init_index();
43  void parse_arguments();
44  struct value {
45  value();
46  value(const value &a);
47  value(const char *filename);
48  value(const std::string& filename);
49  value(const std::string& filename, const std::string& modifications);
50  value(const std::string& filename, const map_location& loc, int center_x, int center_y, const std::string& modifications);
51 
52  bool operator==(const value& a) const;
53  bool operator<(const value& a) const;
54 
59  int center_x_;
60  int center_y_;
61  };
62 
63  friend size_t hash_value(const value&);
64 
65  public:
66 
67  typedef boost::unordered_map<value, int> locator_finder_t;
68 
69  // Constructing locators is somewhat slow, accessing image
70  // through locators is fast. The idea is that calling functions
71  // should store locators, and not strings to construct locators
72  // (the second will work, of course, but will be slower)
73  locator();
74  locator(const locator &a, const std::string &mods ="");
75  locator(const char *filename);
76  locator(const std::string& filename);
77  locator(const std::string& filename, const std::string& modifications);
78  locator(const std::string &filename, const map_location &loc,
79  int center_x, int center_y, const std::string& modifications = "");
80 
81  locator& operator=(const locator &a);
82  bool operator==(const locator &a) const { return index_ == a.index_; }
83  bool operator!=(const locator &a) const { return index_ != a.index_; }
84  bool operator<(const locator &a) const { return index_ < a.index_; }
85 
86  const std::string &get_filename() const { return val_.filename_; }
87  const map_location& get_loc() const { return val_.loc_ ; }
88  int get_center_x() const { return val_.center_x_; }
89  int get_center_y() const { return val_.center_y_; }
91  type get_type() const { return val_.type_; }
92  // const int get_index() const { return index_; };
93 
94  // returns true if the locator does not correspond to any
95  // actual image
96  bool is_void() const { return val_.type_ == NONE; }
97 
98  /**
99  * Tests whether the file the locater points at exists.
100  *
101  * is_void doesn't seem to work before the image is loaded and also in
102  * debug mode a placeholder is returned. So it's not possible to test
103  * for the existence of a file. So this function does that. (Note it
104  * tests for existence not whether or not it's a valid image.)
105  *
106  * @return Whether or not the file exists.
107  */
108  bool file_exists() const;
109 
110  template <typename T>
111  bool in_cache(cache_type<T> &cache) const;
112  template <typename T>
114  template <typename T>
115  const T &locate_in_cache(cache_type<T> &cache) const;
116  template <typename T>
117  void add_to_cache(cache_type<T> &cache, const T &data) const;
118 
119  private:
120 
121  int index_;
123  };
124 
125  surface load_from_disk(const locator &loc);
126 
127 #ifdef SDL_GPU
128  sdl::timage load_texture(const locator &loc);
129 #endif
130 
131  size_t hash_value(const locator::value&);
132 
133 
135 #ifdef SDL_GPU
136  typedef cache_type<sdl::timage> texture_cache;
137 #endif
139 
140  typedef std::map<t_translation::t_terrain, surface> mini_terrain_cache_map;
141  extern mini_terrain_cache_map mini_terrain_cache;
142  extern mini_terrain_cache_map mini_fogged_terrain_cache;
143  extern mini_terrain_cache_map mini_highlighted_terrain_cache;
144 
145  ///light_string store colors info of central and adjacent hexes.
146  ///The structure is one or several 4 chars blocks (L,R,G,B)
147  ///where RGB is the color and L is the lightmap to use:
148  ///(-1:none, 0-5:transition in each direction, 6:full hex)
149  typedef std::basic_string<signed char> light_string;
150  ///return light_string of one light operation(see above)
151  light_string get_light_string(int op, int r, int g, int b);
152 
153  // pair each light possibility with its lighted surface
154  typedef std::map<light_string, surface> lit_variants;
155  // lighted variants for each locator
157 #ifdef SDL_GPU
158  typedef std::map<light_string, sdl::timage> lit_texture_variants;
159  typedef cache_type<lit_texture_variants> lit_texture_cache;
160 #endif
161 
162  void flush_cache();
163 
164  ///the image manager is responsible for setting up images, and destroying
165  ///all images when the program exits. It should probably
166  ///be created once for the life of the program
167  struct manager
168  {
169  manager();
170  ~manager();
171  };
172 
173  ///will make all scaled images have these rgb values added to all
174  ///their pixels. i.e. add a certain color hint to images. useful
175  ///for representing day/night. Invalidates all scaled images.
176  void set_color_adjustment(int r, int g, int b);
177 
179  {
180  public:
182  void reset();
183  private:
184  int r_, g_, b_;
185  };
186 
187  ///set the team colors used by the TC image modification
188  ///use a vector with one string for each team
189  ///using nullptr will reset to default TC
190  void set_team_colors(const std::vector<std::string>* colors = nullptr);
191 
192  const std::vector<std::string>& get_team_colors();
193 
194  ///sets the pixel format used by the images. Is called every time the
195  ///video mode changes. Invalidates all images.
196  void set_pixel_format(SDL_PixelFormat* format);
197 
198  ///sets the amount scaled images should be scaled. Invalidates all
199  ///scaled images.
200  void set_zoom(int zoom);
201 
202  /// UNSCALED : image will be drawn "as is" without changing size, even in case of redraw
203  /// SCALED_TO_ZOOM : image will be scaled taking zoom into account
204  /// HEXED : the hex mask is applied on the image
205  /// SCALED_TO_HEX : image will be scaled to fit into a hex, taking zoom into account
206  /// TOD_COLORED : same as SCALED_TO_HEX but ToD coloring is also applied
207  /// BRIGHTENED : same as TOD_COLORED but also brightened
209 
210  ///function to get the surface corresponding to an image.
211  surface get_image(const locator& i_locator, TYPE type=UNSCALED);
212 #ifdef SDL_GPU
213  sdl::timage get_texture(const locator &loc, TYPE type=UNSCALED);
214 #endif
215 
216  ///function to get the surface corresponding to an image.
217  ///after applying the lightmap encoded in ls
218  ///type should be HEXED or SCALED_TO_HEX
219 #ifdef SDL_GPU
220  sdl::timage get_lighted_texture(const image::locator& i_locator, const light_string& ls, TYPE type);
221 #endif
222  surface get_lighted_image(const image::locator& i_locator, const light_string& ls, TYPE type);
223 
224  ///function to get the standard hex mask
226 
227  ///function to check if an image fit into an hex
228  ///return false if the image has not the standard size.
229  bool is_in_hex(const locator& i_locator);
230 
231  ///function to check if an image is empty after hex cut
232  ///should be only used on terrain image (cache the hex cut version)
233  bool is_empty_hex(const locator& i_locator);
234 
235  ///function to reverse an image. The image MUST have originally been returned from
236  ///an image:: function. Returned images have the same semantics as for get_image()
238 
239  ///returns true if the given image actually exists, without loading it.
240  bool exists(const locator& i_locator);
241 
242  /// precache the existence of files in the subdir (ex: "terrain/")
243  void precache_file_existence(const std::string& subdir = "");
244  bool precached_file_exists(const std::string& file);
245 
246  /// initialize any private data, e.g. algorithm choices from preferences
248 
249  bool save_image(const locator& i_locator, const std::string& outfile);
250  bool save_image(const surface& surf, const std::string& outfile);
251 }
252 
253 #endif
254 
TYPE
UNSCALED : image will be drawn "as is" without changing size, even in case of redraw SCALED_TO_ZOOM :...
Definition: image.hpp:208
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:878
void precache_file_existence(const std::string &subdir)
precache the existence of files in the subdir (ex: "terrain/")
Definition: image.cpp:1229
std::map< t_translation::t_terrain, surface > mini_terrain_cache_map
Definition: image.hpp:140
void set_pixel_format(SDL_PixelFormat *format)
sets the pixel format used by the images.
Definition: image.cpp:666
map_location loc_
Definition: image.hpp:57
bool is_void() const
Definition: image.hpp:96
std::string filename_
Definition: image.hpp:56
value val_
Definition: image.hpp:122
void init_index()
Definition: image.cpp:220
bool operator<(const value &a) const
Definition: image.cpp:353
void set_team_colors(const std::vector< std::string > *colors)
set the team colors used by the TC image modification use a vector with one string for each team usin...
Definition: image.cpp:722
bool precached_file_exists(const std::string &file)
Definition: image.cpp:1240
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
int get_center_y() const
Definition: image.hpp:89
surface reverse_image(const surface &surf)
function to reverse an image.
Definition: image.cpp:1165
bool file_exists() const
Tests whether the file the locater points at exists.
Definition: image.cpp:628
mini_terrain_cache_map mini_fogged_terrain_cache
Definition: image.cpp:186
type get_type() const
Definition: image.hpp:91
friend size_t hash_value(const value &)
Definition: image.cpp:374
void add_to_cache(cache_type< T > &cache, const T &data) const
Definition: image.cpp:117
void parse_arguments()
Definition: image.cpp:232
GLboolean GLboolean g
Definition: glew.h:7319
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
cache_type< lit_variants > lit_cache
Definition: image.hpp:156
const map_location & get_loc() const
Definition: image.hpp:87
cache_type< surface > image_cache
Definition: image.hpp:134
bool save_image(const locator &i_locator, const std::string &filename)
Definition: image.cpp:1249
void flush_cache()
Definition: image.cpp:191
bool operator==(const locator &a) const
Definition: image.hpp:82
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
bool exists(const image::locator &i_locator)
returns true if the given image actually exists, without loading it.
Definition: image.cpp:1187
bool in_cache(cache_type< T > &cache) const
Definition: image.cpp:97
light_string get_light_string(int op, int r, int g, int b)
return light_string of one light operation(see above)
Definition: image.cpp:573
bool operator==(const value &a) const
Definition: image.cpp:338
std::string modifications_
Definition: image.hpp:58
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
std::map< light_string, surface > lit_variants
Definition: image.hpp:154
T & access_in_cache(cache_type< T > &cache) const
Definition: image.cpp:110
surf
Definition: filter.cpp:143
void set_color_adjustment(int r, int g, int b)
will make all scaled images have these rgb values added to all their pixels.
Definition: image.cpp:698
Encapsulates the map of the game.
Definition: location.hpp:38
const std::string & get_filename() const
Definition: image.hpp:86
const std::string & get_modifications() const
Definition: image.hpp:90
surface load_from_disk(const locator &loc)
Definition: image.cpp:633
cache_type< bool > bool_cache
Definition: image.hpp:138
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1222
Contains a wrapper class for the GPU_Image class.
locator & operator=(const locator &a)
Definition: image.cpp:296
static tcache cache
Definition: minimap.cpp:139
surface get_hexmask()
function to get the standard hex mask
Definition: image.cpp:1115
std::basic_string< signed char > light_string
light_string store colors info of central and adjacent hexes.
Definition: image.hpp:149
size_t hash_value(const locator::value &val)
Definition: image.cpp:374
mini_terrain_cache_map mini_highlighted_terrain_cache
Definition: image.cpp:187
void set_zoom(int amount)
sets the amount scaled images should be scaled.
Definition: image.cpp:736
the image manager is responsible for setting up images, and destroying all images when the program ex...
Definition: image.hpp:167
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
bool is_in_hex(const locator &i_locator)
function to check if an image fit into an hex return false if the image has not the standard size...
Definition: image.cpp:1121
mini_terrain_cache_map mini_terrain_cache
Definition: image.cpp:185
surface get_lighted_image(const image::locator &i_locator, const light_string &ls, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:1063
const T & locate_in_cache(cache_type< T > &cache) const
Definition: image.cpp:103
const std::vector< std::string > & modifications(bool mp)
bool is_empty_hex(const locator &i_locator)
function to check if an image is empty after hex cut should be only used on terrain image (cache the ...
Definition: image.cpp:1146
bool operator<(const locator &a) const
Definition: image.hpp:84
int get_center_x() const
Definition: image.hpp:88
this module manages the cache of images.
Definition: image.cpp:75
boost::unordered_map< value, int > locator_finder_t
Definition: image.hpp:67
const std::vector< std::string > & get_team_colors()
Definition: image.cpp:731
bool operator!=(const locator &a) const
Definition: image.hpp:83
bool update_from_preferences()
initialize any private data, e.g. algorithm choices from preferences
Definition: image.cpp:1274
GLsizei const GLcharARB ** string
Definition: glew.h:4503