The Battle for Wesnoth  1.15.0+dev
picture.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://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 #pragma once
16 
17 #include "map/location.hpp"
18 #include "terrain/translation.hpp"
19 
20 #include <SDL2/SDL.h>
21 #include <unordered_map>
22 
23 class surface;
24 
25 ///this module manages the cache of images. With an image name, you can get
26 ///the surface corresponding to that image.
27 //
28 namespace image {
29  template<typename T>
30  class cache_type;
31 
32  //a generic image locator. Abstracts the location of an image.
33  class locator
34  {
35  public:
36  enum type { NONE, FILE, SUB_FILE };
37  private:
38  // Called by each constructor after actual construction to
39  // initialize the index_ field
40  void init_index();
41  void parse_arguments();
42  struct value {
43  value();
44  value(const char *filename);
45  value(const std::string& filename);
46  value(const std::string& filename, const std::string& modifications);
47  value(const std::string& filename, const map_location& loc, int center_x, int center_y, const std::string& modifications);
48 
49  bool operator==(const value& a) const;
50  bool operator<(const value& a) const;
51 
54  std::string filename_;
56  std::string modifications_;
57  int center_x_;
58  int center_y_;
59  };
60 
61  friend struct std::hash<value>;
62 
63  public:
64 
65  typedef std::unordered_map<value, int> locator_finder_t;
66 
67  // Constructing locators is somewhat slow, accessing image
68  // through locators is fast. The idea is that calling functions
69  // should store locators, and not strings to construct locators
70  // (the second will work, of course, but will be slower)
71  locator();
72  locator(const locator &a, const std::string &mods ="");
73  locator(const char *filename);
74  locator(const std::string& filename);
75  locator(const std::string& filename, const std::string& modifications);
76  locator(const std::string &filename, const map_location &loc,
77  int center_x, int center_y, const std::string& modifications = "");
78 
79  locator& operator=(const locator &a);
80  bool operator==(const locator &a) const { return index_ == a.index_; }
81  bool operator!=(const locator &a) const { return index_ != a.index_; }
82  bool operator<(const locator &a) const { return index_ < a.index_; }
83 
84  const std::string &get_filename() const { return val_.filename_; }
85  bool is_data_uri() const { return val_.is_data_uri_; }
86  const map_location& get_loc() const { return val_.loc_ ; }
87  int get_center_x() const { return val_.center_x_; }
88  int get_center_y() const { return val_.center_y_; }
89  const std::string& get_modifications() const {return val_.modifications_;}
90  type get_type() const { return val_.type_; }
91  // const int get_index() const { return index_; };
92 
93  // returns true if the locator does not correspond to any
94  // actual image
95  bool is_void() const { return val_.type_ == NONE; }
96 
97  /**
98  * Tests whether the file the locater points at exists.
99  *
100  * is_void doesn't seem to work before the image is loaded and also in
101  * debug mode a placeholder is returned. So it's not possible to test
102  * for the existence of a file. So this function does that. (Note it
103  * tests for existence not whether or not it's a valid image.)
104  *
105  * @return Whether or not the file exists.
106  */
107  bool file_exists() const;
108 
109  template <typename T>
110  bool in_cache(cache_type<T> &cache) const;
111  template <typename T>
112  T &access_in_cache(cache_type<T> &cache) const;
113  template <typename T>
114  const T &locate_in_cache(cache_type<T> &cache) const;
115  template <typename T>
116  void add_to_cache(cache_type<T> &cache, const T &data) const;
117 
118  private:
119 
120  int index_;
122  };
123 
124  surface load_from_disk(const locator &loc);
125 
126 
129 
130  typedef std::map<t_translation::terrain_code, surface> mini_terrain_cache_map;
131  extern mini_terrain_cache_map mini_terrain_cache;
132  extern mini_terrain_cache_map mini_fogged_terrain_cache;
133  extern mini_terrain_cache_map mini_highlighted_terrain_cache;
134 
135  ///light_string store colors info of central and adjacent hexes.
136  ///The structure is one or several 4 chars blocks (L,R,G,B)
137  ///where RGB is the color and L is the lightmap to use:
138  /// -1: none
139  /// 0: full hex
140  /// 1-6: concave corners
141  /// 7-12: convex half-corners 1
142  ///13-19: convex half-corners 2
143  typedef std::basic_string<signed char> light_string;
144  ///return light_string of one light operation(see above)
145  light_string get_light_string(int op, int r, int g, int b);
146 
147  // pair each light possibility with its lighted surface
148  typedef std::map<light_string, surface> lit_variants;
149  // lighted variants for each locator
151 
152  void flush_cache();
153 
154  ///the image manager is responsible for setting up images, and destroying
155  ///all images when the program exits. It should probably
156  ///be created once for the life of the program
157  struct manager
158  {
159  manager();
160  ~manager();
161  };
162 
163  ///will make all scaled images have these rgb values added to all
164  ///their pixels. i.e. add a certain color hint to images. useful
165  ///for representing day/night. Invalidates all scaled images.
166  void set_color_adjustment(int r, int g, int b);
167 
168  ///set the team colors used by the TC image modification
169  ///use a vector with one string for each team
170  ///using nullptr will reset to default TC
171  void set_team_colors(const std::vector<std::string>* colors = nullptr);
172 
173  const std::vector<std::string>& get_team_colors();
174 
175  ///sets the amount scaled images should be scaled. Invalidates all
176  ///scaled images.
177  void set_zoom(unsigned int zoom);
178 
179  /// UNSCALED : image will be drawn "as is" without changing size, even in case of redraw
180  /// SCALED_TO_ZOOM : image will be scaled taking zoom into account
181  /// HEXED : the hex mask is applied on the image
182  /// SCALED_TO_HEX : image will be scaled to fit into a hex, taking zoom into account
183  /// TOD_COLORED : same as SCALED_TO_HEX but ToD coloring is also applied
184  /// BRIGHTENED : same as TOD_COLORED but also brightened
186 
187  ///function to get the surface corresponding to an image.
188  surface get_image(const locator& i_locator, TYPE type=UNSCALED);
189 
190  ///function to get the surface corresponding to an image.
191  ///after applying the lightmap encoded in ls
192  ///type should be HEXED or SCALED_TO_HEX
193  surface get_lighted_image(const image::locator& i_locator, const light_string& ls, TYPE type);
194 
195  ///function to get the standard hex mask
197 
198  ///function to check if an image fit into an hex
199  ///return false if the image has not the standard size.
200  bool is_in_hex(const locator& i_locator);
201 
202  ///function to check if an image is empty after hex cut
203  ///should be only used on terrain image (cache the hex cut version)
204  bool is_empty_hex(const locator& i_locator);
205 
206  ///function to reverse an image. The image MUST have originally been returned from
207  ///an image:: function. Returned images have the same semantics as for get_image()
208  surface reverse_image(const surface &surf);
209 
210  ///returns true if the given image actually exists, without loading it.
211  bool exists(const locator& i_locator);
212 
213  /// precache the existence of files in the subdir (ex: "terrain/")
214  void precache_file_existence(const std::string& subdir = "");
215  bool precached_file_exists(const std::string& file);
216 
217  /// initialize any private data, e.g. algorithm choices from preferences
219 
221 
222  save_result save_image(const locator& i_locator, const std::string& outfile);
223  save_result save_image(const surface& surf, const std::string& outfile);
224 }
TYPE
UNSCALED : image will be drawn "as is" without changing size, even in case of redraw SCALED_TO_ZOOM :...
Definition: picture.hpp:185
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: picture.cpp:875
const std::string & get_modifications() const
Definition: picture.hpp:89
int get_center_y() const
Definition: picture.hpp:88
void precache_file_existence(const std::string &subdir)
precache the existence of files in the subdir (ex: "terrain/")
Definition: picture.cpp:1124
bool operator!=(const locator &a) const
Definition: picture.hpp:81
map_location loc_
Definition: picture.hpp:55
void add_to_cache(cache_type< T > &cache, const T &data) const
Definition: picture.cpp:145
std::string filename_
Definition: picture.hpp:54
void init_index()
Definition: picture.cpp:257
bool operator==(const locator &a) const
Definition: picture.hpp:80
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: picture.cpp:714
bool precached_file_exists(const std::string &file)
Definition: picture.cpp:1133
save_result
Definition: picture.hpp:220
surface reverse_image(const surface &surf)
function to reverse an image.
Definition: picture.cpp:1048
#define a
mini_terrain_cache_map mini_fogged_terrain_cache
Definition: picture.cpp:226
save_result save_image(const locator &i_locator, const std::string &filename)
Definition: picture.cpp:1143
T & access_in_cache(cache_type< T > &cache) const
Definition: picture.cpp:138
void parse_arguments()
Definition: picture.cpp:269
std::map< t_translation::terrain_code, surface > mini_terrain_cache_map
Definition: picture.hpp:130
type get_type() const
Definition: picture.hpp:90
cache_type< lit_variants > lit_cache
Definition: picture.hpp:150
cache_type< surface > image_cache
Definition: picture.hpp:127
void flush_cache()
Definition: picture.cpp:231
#define b
bool exists(const image::locator &i_locator)
returns true if the given image actually exists, without loading it.
Definition: picture.cpp:1070
const T & locate_in_cache(cache_type< T > &cache) const
Definition: picture.cpp:131
light_string get_light_string(int op, int r, int g, int b)
return light_string of one light operation(see above)
Definition: picture.cpp:602
std::string modifications_
Definition: picture.hpp:56
std::unordered_map< value, int > locator_finder_t
Definition: picture.hpp:65
void set_zoom(unsigned int amount)
sets the amount scaled images should be scaled.
Definition: picture.cpp:728
std::map< light_string, surface > lit_variants
Definition: picture.hpp:148
bool operator<(const locator &a) const
Definition: picture.hpp:82
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: picture.cpp:700
Encapsulates the map of the game.
Definition: location.hpp:42
surface load_from_disk(const locator &loc)
Definition: picture.cpp:675
cache_type< bool > bool_cache
Definition: picture.hpp:128
locator & operator=(const locator &a)
Definition: picture.cpp:350
double g
Definition: astarsearch.cpp:64
static tcache cache
Definition: minimap.cpp:134
surface get_hexmask()
function to get the standard hex mask
Definition: picture.cpp:1000
std::basic_string< signed char > light_string
light_string store colors info of central and adjacent hexes.
Definition: picture.hpp:143
mini_terrain_cache_map mini_highlighted_terrain_cache
Definition: picture.cpp:227
int get_center_x() const
Definition: picture.hpp:87
the image manager is responsible for setting up images, and destroying all images when the program ex...
Definition: picture.hpp:157
const std::string & get_filename() const
Definition: picture.hpp:84
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: picture.cpp:1006
bool is_data_uri() const
Definition: picture.hpp:85
bool operator<(const value &a) const
Definition: picture.cpp:431
mini_terrain_cache_map mini_terrain_cache
Definition: picture.cpp:225
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: picture.cpp:948
const std::vector< std::string > & modifications(bool mp)
Definition: game.cpp:742
const map_location & get_loc() const
Definition: picture.hpp:86
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: picture.cpp:1029
this module manages the cache of images.
bool operator==(const value &a) const
Definition: picture.cpp:417
const std::vector< std::string > & get_team_colors()
Definition: picture.cpp:723
bool update_from_preferences()
initialize any private data, e.g. algorithm choices from preferences
Definition: picture.cpp:1181
bool is_void() const
Definition: picture.hpp:95
bool in_cache(cache_type< T > &cache) const
Definition: picture.cpp:125
bool file_exists() const
Tests whether the file the locater points at exists.
Definition: picture.cpp:668