texture.h
1 /*************************************************************************/
2 /* texture.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef TEXTURE_H
30 #define TEXTURE_H
31 
32 #include "resource.h"
33 #include "servers/visual_server.h"
34 #include "math_2d.h"
35 
42 class Texture : public Resource {
43 
44  OBJ_TYPE( Texture, Resource );
45  OBJ_SAVE_TYPE( Texture ); //children are all saved as Texture, so they can be exchanged
46 protected:
47 
48  static void _bind_methods();
49 public:
50 
51  enum Flags {
52  FLAG_MIPMAPS=VisualServer::TEXTURE_FLAG_MIPMAPS,
53  FLAG_REPEAT=VisualServer::TEXTURE_FLAG_REPEAT,
54  FLAG_FILTER=VisualServer::TEXTURE_FLAG_FILTER,
55  FLAG_ANISOTROPIC_FILTER=VisualServer::TEXTURE_FLAG_ANISOTROPIC_FILTER,
56  FLAG_CONVERT_TO_LINEAR=VisualServer::TEXTURE_FLAG_CONVERT_TO_LINEAR,
57  FLAG_VIDEO_SURFACE=VisualServer::TEXTURE_FLAG_VIDEO_SURFACE,
58  FLAGS_DEFAULT=FLAG_MIPMAPS|FLAG_REPEAT|FLAG_FILTER,
59  FLAG_MIRRORED_REPEAT=VisualServer::TEXTURE_FLAG_MIRRORED_REPEAT
60  };
61 
62 
63  virtual int get_width() const=0;
64  virtual int get_height() const=0;
65  virtual Size2 get_size() const;
66  virtual RID get_rid() const=0;
67 
68  virtual bool has_alpha() const=0;
69 
70  virtual void set_flags(uint32_t p_flags)=0;
71  virtual uint32_t get_flags() const=0;
72 
73  virtual void draw(RID p_canvas_item, const Point2& p_pos, const Color& p_modulate=Color(1,1,1), bool p_transpose=false) const;
74  virtual void draw_rect(RID p_canvas_item,const Rect2& p_rect, bool p_tile=false,const Color& p_modulate=Color(1,1,1), bool p_transpose=false) const;
75  virtual void draw_rect_region(RID p_canvas_item,const Rect2& p_rect, const Rect2& p_src_rect,const Color& p_modulate=Color(1,1,1), bool p_transpose=false) const;
76  virtual bool get_rect_region(const Rect2& p_rect, const Rect2& p_src_rect,Rect2& r_rect,Rect2& r_src_rect) const;
77 
78 
79 
80  Texture();
81 };
82 
83 VARIANT_ENUM_CAST( Texture::Flags );
84 
85 
86 class ImageTexture : public Texture {
87 
88  OBJ_TYPE( ImageTexture, Texture );
89  RES_BASE_EXTENSION("tex");
90 public:
91  enum Storage {
92  STORAGE_RAW,
93  STORAGE_COMPRESS_LOSSY,
94  STORAGE_COMPRESS_LOSSLESS
95  };
96 private:
97  RID texture;
98  Image::Format format;
99  uint32_t flags;
100  int w,h;
101  Storage storage;
102  Size2 size_override;
103  float lossy_storage_quality;
104 
105 protected:
106  virtual bool can_reload_from_file();
107  virtual void reload_from_file();
108 
109  bool _set(const StringName& p_name, const Variant& p_value);
110  bool _get(const StringName& p_name,Variant &r_ret) const;
111  void _get_property_list( List<PropertyInfo> *p_list) const;
112 
113  void _reload_hook(const RID& p_hook);
114  virtual void _resource_path_changed();
115  static void _bind_methods();
116 
117  void _set_data(Dictionary p_data);
118 
119 public:
120 
121 
122  void create(int p_width, int p_height,Image::Format p_format,uint32_t p_flags=FLAGS_DEFAULT);
123  void create_from_image(const Image& p_image, uint32_t p_flags=FLAGS_DEFAULT);
124 
125 
126  void set_flags(uint32_t p_flags);
127  uint32_t get_flags() const;
128  Image::Format get_format() const;
129  void load(const String& p_path);
130  void set_data(const Image& p_image);
131  Image get_data() const;
132 
133  int get_width() const;
134  int get_height() const;
135 
136  virtual RID get_rid() const;
137 
138  bool has_alpha() const;
139  virtual void draw(RID p_canvas_item, const Point2& p_pos, const Color& p_modulate=Color(1,1,1), bool p_transpose=false) const;
140  virtual void draw_rect(RID p_canvas_item,const Rect2& p_rect, bool p_tile=false,const Color& p_modulate=Color(1,1,1), bool p_transpose=false) const;
141  virtual void draw_rect_region(RID p_canvas_item,const Rect2& p_rect, const Rect2& p_src_rect,const Color& p_modulate=Color(1,1,1), bool p_transpose=false) const;
142  void set_storage(Storage p_storage);
143  Storage get_storage() const;
144 
145  void set_lossy_storage_quality(float p_lossy_storage_quality);
146  float get_lossy_storage_quality() const;
147 
148  void fix_alpha_edges();
149  void premultiply_alpha();
150  void normal_to_xy();
151  void shrink_x2_and_keep_size();
152 
153 
154  void set_size_override(const Size2& p_size);
155 
156  virtual void set_path(const String& p_path,bool p_take_over=false);
157 
158  ImageTexture();
159  ~ImageTexture();
160 
161 };
162 
163 
164 VARIANT_ENUM_CAST( ImageTexture::Storage );
165 
166 class AtlasTexture : public Texture {
167 
168  OBJ_TYPE( AtlasTexture, Texture );
169  RES_BASE_EXTENSION("atex");
170 protected:
171 
172 
173  Ref<Texture> atlas;
174  Rect2 region;
175  Rect2 margin;
176 
177  static void _bind_methods();
178 public:
179 
180  virtual int get_width() const;
181  virtual int get_height() const;
182  virtual RID get_rid() const;
183 
184  virtual bool has_alpha() const;
185 
186  virtual void set_flags(uint32_t p_flags);
187  virtual uint32_t get_flags() const;
188 
189  void set_atlas(const Ref<Texture>& p_atlas);
190  Ref<Texture> get_atlas() const;
191 
192  void set_region(const Rect2& p_region);
193  Rect2 get_region() const ;
194 
195  void set_margin(const Rect2& p_margin);
196  Rect2 get_margin() const ;
197 
198  virtual void draw(RID p_canvas_item, const Point2& p_pos, const Color& p_modulate=Color(1,1,1), bool p_transpose=false) const;
199  virtual void draw_rect(RID p_canvas_item,const Rect2& p_rect, bool p_tile=false,const Color& p_modulate=Color(1,1,1), bool p_transpose=false) const;
200  virtual void draw_rect_region(RID p_canvas_item,const Rect2& p_rect, const Rect2& p_src_rect,const Color& p_modulate=Color(1,1,1), bool p_transpose=false) const;
201  virtual bool get_rect_region(const Rect2& p_rect, const Rect2& p_src_rect,Rect2& r_rect,Rect2& r_src_rect) const;
202 
203 
204  AtlasTexture();
205 };
206 
207 class LargeTexture : public Texture {
208 
209  OBJ_TYPE( LargeTexture, Texture );
210  RES_BASE_EXTENSION("ltex");
211 protected:
212 
213  struct Piece {
214 
215  Point2 offset;
216  Ref<Texture> texture;
217  };
218 
219  Vector<Piece> pieces;
220  Size2i size;
221 
222 
223  Array _get_data() const;
224  void _set_data(const Array& p_array);
225  static void _bind_methods();
226 public:
227 
228  virtual int get_width() const;
229  virtual int get_height() const;
230  virtual RID get_rid() const;
231 
232  virtual bool has_alpha() const;
233 
234  virtual void set_flags(uint32_t p_flags);
235  virtual uint32_t get_flags() const;
236 
237  int add_piece(const Point2& p_offset,const Ref<Texture>& p_texture);
238  void set_piece_offset(int p_idx, const Point2& p_offset);
239  void set_piece_texture(int p_idx, const Ref<Texture>& p_texture);
240 
241  void set_size(const Size2& p_size);
242  void clear();
243 
244  int get_piece_count() const;
245  Vector2 get_piece_offset(int p_idx) const;
246  Ref<Texture> get_piece_texture(int p_idx) const;
247 
248  virtual void draw(RID p_canvas_item, const Point2& p_pos, const Color& p_modulate=Color(1,1,1), bool p_transpose=false) const;
249  virtual void draw_rect(RID p_canvas_item,const Rect2& p_rect, bool p_tile=false,const Color& p_modulate=Color(1,1,1), bool p_transpose=false) const;
250  virtual void draw_rect_region(RID p_canvas_item,const Rect2& p_rect, const Rect2& p_src_rect,const Color& p_modulate=Color(1,1,1), bool p_transpose=false) const;
251 
252 
253  LargeTexture();
254 };
255 
256 
257 
258 class CubeMap : public Resource {
259 
260  OBJ_TYPE( CubeMap, Resource );
261  RES_BASE_EXTENSION("cbm");
262 public:
263  enum Storage {
264  STORAGE_RAW,
265  STORAGE_COMPRESS_LOSSY,
266  STORAGE_COMPRESS_LOSSLESS
267  };
268 
269  enum Side {
270 
271  SIDE_LEFT,
272  SIDE_RIGHT,
273  SIDE_BOTTOM,
274  SIDE_TOP,
275  SIDE_FRONT,
276  SIDE_BACK
277  };
278 
279  enum Flags {
280  FLAG_MIPMAPS=VisualServer::TEXTURE_FLAG_MIPMAPS,
281  FLAG_REPEAT=VisualServer::TEXTURE_FLAG_REPEAT,
282  FLAG_FILTER=VisualServer::TEXTURE_FLAG_FILTER,
283  FLAGS_DEFAULT=FLAG_MIPMAPS|FLAG_REPEAT|FLAG_FILTER,
284  };
285 
286 private:
287 
288  bool valid[6];
289  RID cubemap;
290  Image::Format format;
291  uint32_t flags;
292  int w,h;
293  Storage storage;
294  Size2 size_override;
295  float lossy_storage_quality;
296 
297  _FORCE_INLINE_ bool _is_valid() const { for(int i=0;i<6;i++) { if (valid[i]) return true; } return false; }
298 
299 protected:
300 
301  bool _set(const StringName& p_name, const Variant& p_value);
302  bool _get(const StringName& p_name,Variant &r_ret) const;
303  void _get_property_list( List<PropertyInfo> *p_list) const;
304 
305  static void _bind_methods();
306 public:
307 
308  void set_flags(uint32_t p_flags);
309  uint32_t get_flags() const;
310  void set_side(Side p_side,const Image& p_image);
311  Image get_side(Side p_side) const;
312 
313  Image::Format get_format() const;
314  int get_width() const;
315  int get_height() const;
316 
317  virtual RID get_rid() const;
318 
319  void set_storage(Storage p_storage);
320  Storage get_storage() const;
321 
322  void set_lossy_storage_quality(float p_lossy_storage_quality);
323  float get_lossy_storage_quality() const;
324 
325  virtual void set_path(const String& p_path,bool p_take_over=false);
326 
327  CubeMap();
328  ~CubeMap();
329 
330 };
331 
332 VARIANT_ENUM_CAST( CubeMap::Flags );
333 VARIANT_ENUM_CAST( CubeMap::Side );
334 VARIANT_ENUM_CAST( CubeMap::Storage );
335 
336 
337 /*
338  enum CubeMapSide {
339 
340  CUBEMAP_LEFT,
341  CUBEMAP_RIGHT,
342  CUBEMAP_BOTTOM,
343  CUBEMAP_TOP,
344  CUBEMAP_FRONT,
345  CUBEMAP_BACK,
346  };
347 
348 */
349 //VARIANT_ENUM_CAST( Texture::CubeMapSide );
350 
351 
352 #endif
Definition: array.h:38
Definition: variant.h:74
Definition: math_2d.h:369
Format
Definition: image.h:57
Definition: color.h:37
Definition: texture.h:258
Definition: math_2d.h:204
Definition: image.h:47
Definition: string_db.h:48
Definition: texture.h:86
Definition: texture.h:207
Definition: resource.h:89
Definition: rid.h:47
Definition: dictionary.h:42
Definition: math_2d.h:65
Definition: texture.h:42
Definition: texture.h:166
Definition: ustring.h:64
Definition: vector.h:43
Definition: texture.h:213