texture.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*************************************************************************/
  2. /* texture.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef TEXTURE_H
  31. #define TEXTURE_H
  32. #include "core/io/resource_loader.h"
  33. #include "core/math/rect2.h"
  34. #include "core/os/mutex.h"
  35. #include "core/os/rw_lock.h"
  36. #include "core/os/thread_safe.h"
  37. #include "core/resource.h"
  38. #include "scene/resources/color_ramp.h"
  39. #include "scene/resources/curve.h"
  40. #include "servers/visual_server.h"
  41. /**
  42. @author Juan Linietsky <reduzio@gmail.com>
  43. */
  44. class Texture : public Resource {
  45. GDCLASS(Texture, Resource);
  46. OBJ_SAVE_TYPE(Texture); //children are all saved as Texture, so they can be exchanged
  47. protected:
  48. static void _bind_methods();
  49. public:
  50. enum Flags {
  51. FLAG_MIPMAPS = VisualServer::TEXTURE_FLAG_MIPMAPS,
  52. FLAG_REPEAT = VisualServer::TEXTURE_FLAG_REPEAT,
  53. FLAG_FILTER = VisualServer::TEXTURE_FLAG_FILTER,
  54. FLAG_ANISOTROPIC_FILTER = VisualServer::TEXTURE_FLAG_ANISOTROPIC_FILTER,
  55. FLAG_CONVERT_TO_LINEAR = VisualServer::TEXTURE_FLAG_CONVERT_TO_LINEAR,
  56. FLAG_VIDEO_SURFACE = VisualServer::TEXTURE_FLAG_USED_FOR_STREAMING,
  57. FLAGS_DEFAULT = FLAG_MIPMAPS | FLAG_REPEAT | FLAG_FILTER,
  58. FLAG_MIRRORED_REPEAT = VisualServer::TEXTURE_FLAG_MIRRORED_REPEAT
  59. };
  60. virtual int get_width() const = 0;
  61. virtual int get_height() const = 0;
  62. virtual Size2 get_size() const;
  63. virtual RID get_rid() const = 0;
  64. virtual bool is_pixel_opaque(int p_x, int p_y) const;
  65. virtual bool has_alpha() const = 0;
  66. virtual void set_flags(uint32_t p_flags) = 0;
  67. virtual uint32_t get_flags() const = 0;
  68. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  69. 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 Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  70. 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 Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  71. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const;
  72. virtual Ref<Image> get_data() const { return Ref<Image>(); }
  73. Texture();
  74. };
  75. VARIANT_ENUM_CAST(Texture::Flags);
  76. class BitMap;
  77. class ImageTexture : public Texture {
  78. GDCLASS(ImageTexture, Texture);
  79. RES_BASE_EXTENSION("tex");
  80. public:
  81. enum Storage {
  82. STORAGE_RAW,
  83. STORAGE_COMPRESS_LOSSY,
  84. STORAGE_COMPRESS_LOSSLESS
  85. };
  86. private:
  87. RID texture;
  88. Image::Format format;
  89. uint32_t flags;
  90. int w, h;
  91. Storage storage;
  92. Size2 size_override;
  93. float lossy_storage_quality;
  94. mutable Ref<BitMap> alpha_cache;
  95. protected:
  96. virtual void reload_from_file();
  97. bool _set(const StringName &p_name, const Variant &p_value);
  98. bool _get(const StringName &p_name, Variant &r_ret) const;
  99. void _get_property_list(List<PropertyInfo> *p_list) const;
  100. void _reload_hook(const RID &p_hook);
  101. virtual void _resource_path_changed();
  102. static void _bind_methods();
  103. void _set_data(Dictionary p_data);
  104. public:
  105. void create(int p_width, int p_height, Image::Format p_format, uint32_t p_flags = FLAGS_DEFAULT);
  106. void create_from_image(const Ref<Image> &p_image, uint32_t p_flags = FLAGS_DEFAULT);
  107. void set_flags(uint32_t p_flags);
  108. uint32_t get_flags() const;
  109. Image::Format get_format() const;
  110. #ifndef DISABLE_DEPRECATED
  111. Error load(const String &p_path);
  112. #endif
  113. void set_data(const Ref<Image> &p_image);
  114. Ref<Image> get_data() const;
  115. int get_width() const;
  116. int get_height() const;
  117. virtual RID get_rid() const;
  118. bool has_alpha() const;
  119. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  120. 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 Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  121. 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 Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  122. void set_storage(Storage p_storage);
  123. Storage get_storage() const;
  124. bool is_pixel_opaque(int p_x, int p_y) const;
  125. void set_lossy_storage_quality(float p_lossy_storage_quality);
  126. float get_lossy_storage_quality() const;
  127. void set_size_override(const Size2 &p_size);
  128. virtual void set_path(const String &p_path, bool p_take_over = false);
  129. ImageTexture();
  130. ~ImageTexture();
  131. };
  132. class StreamTexture : public Texture {
  133. GDCLASS(StreamTexture, Texture);
  134. public:
  135. enum DataFormat {
  136. DATA_FORMAT_IMAGE,
  137. DATA_FORMAT_LOSSLESS,
  138. DATA_FORMAT_LOSSY
  139. };
  140. enum FormatBits {
  141. FORMAT_MASK_IMAGE_FORMAT = (1 << 20) - 1,
  142. FORMAT_BIT_LOSSLESS = 1 << 20,
  143. FORMAT_BIT_LOSSY = 1 << 21,
  144. FORMAT_BIT_STREAM = 1 << 22,
  145. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  146. FORMAT_BIT_DETECT_3D = 1 << 24,
  147. FORMAT_BIT_DETECT_SRGB = 1 << 25,
  148. FORMAT_BIT_DETECT_NORMAL = 1 << 26,
  149. };
  150. private:
  151. Error _load_data(const String &p_path, int &tw, int &th, int &flags, Ref<Image> &image, int p_size_limit = 0);
  152. String path_to_file;
  153. RID texture;
  154. Image::Format format;
  155. uint32_t flags;
  156. int w, h;
  157. mutable Ref<BitMap> alpha_cache;
  158. virtual void reload_from_file();
  159. static void _requested_3d(void *p_ud);
  160. static void _requested_srgb(void *p_ud);
  161. static void _requested_normal(void *p_ud);
  162. protected:
  163. static void _bind_methods();
  164. public:
  165. typedef void (*TextureFormatRequestCallback)(const Ref<StreamTexture> &);
  166. static TextureFormatRequestCallback request_3d_callback;
  167. static TextureFormatRequestCallback request_srgb_callback;
  168. static TextureFormatRequestCallback request_normal_callback;
  169. uint32_t get_flags() const;
  170. Image::Format get_format() const;
  171. Error load(const String &p_path);
  172. String get_load_path() const;
  173. int get_width() const;
  174. int get_height() const;
  175. virtual RID get_rid() const;
  176. virtual void set_path(const String &p_path, bool p_take_over);
  177. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  178. 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 Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  179. 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 Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  180. virtual bool has_alpha() const;
  181. virtual void set_flags(uint32_t p_flags);
  182. bool is_pixel_opaque(int p_x, int p_y) const;
  183. virtual Ref<Image> get_data() const;
  184. StreamTexture();
  185. ~StreamTexture();
  186. };
  187. class ResourceFormatLoaderStreamTexture : public ResourceFormatLoader {
  188. GDCLASS(ResourceFormatLoaderStreamTexture, ResourceFormatLoader)
  189. public:
  190. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  191. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  192. virtual bool handles_type(const String &p_type) const;
  193. virtual String get_resource_type(const String &p_path) const;
  194. };
  195. VARIANT_ENUM_CAST(ImageTexture::Storage);
  196. class AtlasTexture : public Texture {
  197. GDCLASS(AtlasTexture, Texture);
  198. RES_BASE_EXTENSION("atlastex");
  199. protected:
  200. Ref<Texture> atlas;
  201. Rect2 region;
  202. Rect2 margin;
  203. bool filter_clip;
  204. static void _bind_methods();
  205. public:
  206. virtual int get_width() const;
  207. virtual int get_height() const;
  208. virtual RID get_rid() const;
  209. virtual bool has_alpha() const;
  210. virtual void set_flags(uint32_t p_flags);
  211. virtual uint32_t get_flags() const;
  212. void set_atlas(const Ref<Texture> &p_atlas);
  213. Ref<Texture> get_atlas() const;
  214. void set_region(const Rect2 &p_region);
  215. Rect2 get_region() const;
  216. void set_margin(const Rect2 &p_margin);
  217. Rect2 get_margin() const;
  218. void set_filter_clip(const bool p_enable);
  219. bool has_filter_clip() const;
  220. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  221. 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 Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  222. 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 Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  223. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const;
  224. bool is_pixel_opaque(int p_x, int p_y) const;
  225. AtlasTexture();
  226. };
  227. class LargeTexture : public Texture {
  228. GDCLASS(LargeTexture, Texture);
  229. RES_BASE_EXTENSION("largetex");
  230. protected:
  231. struct Piece {
  232. Point2 offset;
  233. Ref<Texture> texture;
  234. };
  235. Vector<Piece> pieces;
  236. Size2i size;
  237. Array _get_data() const;
  238. void _set_data(const Array &p_array);
  239. static void _bind_methods();
  240. public:
  241. virtual int get_width() const;
  242. virtual int get_height() const;
  243. virtual RID get_rid() const;
  244. virtual bool has_alpha() const;
  245. virtual void set_flags(uint32_t p_flags);
  246. virtual uint32_t get_flags() const;
  247. int add_piece(const Point2 &p_offset, const Ref<Texture> &p_texture);
  248. void set_piece_offset(int p_idx, const Point2 &p_offset);
  249. void set_piece_texture(int p_idx, const Ref<Texture> &p_texture);
  250. void set_size(const Size2 &p_size);
  251. void clear();
  252. int get_piece_count() const;
  253. Vector2 get_piece_offset(int p_idx) const;
  254. Ref<Texture> get_piece_texture(int p_idx) const;
  255. Ref<Image> to_image() const;
  256. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  257. 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 Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  258. 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 Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  259. bool is_pixel_opaque(int p_x, int p_y) const;
  260. LargeTexture();
  261. };
  262. class CubeMap : public Resource {
  263. GDCLASS(CubeMap, Resource);
  264. RES_BASE_EXTENSION("cubemap");
  265. public:
  266. enum Storage {
  267. STORAGE_RAW,
  268. STORAGE_COMPRESS_LOSSY,
  269. STORAGE_COMPRESS_LOSSLESS
  270. };
  271. enum Side {
  272. SIDE_LEFT,
  273. SIDE_RIGHT,
  274. SIDE_BOTTOM,
  275. SIDE_TOP,
  276. SIDE_FRONT,
  277. SIDE_BACK
  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. private:
  286. bool valid[6];
  287. RID cubemap;
  288. Image::Format format;
  289. uint32_t flags;
  290. int w, h;
  291. Storage storage;
  292. Size2 size_override;
  293. float lossy_storage_quality;
  294. _FORCE_INLINE_ bool _is_valid() const {
  295. for (int i = 0; i < 6; i++) {
  296. if (valid[i]) return true;
  297. }
  298. return false;
  299. }
  300. protected:
  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. static void _bind_methods();
  305. public:
  306. void set_flags(uint32_t p_flags);
  307. uint32_t get_flags() const;
  308. void set_side(Side p_side, const Ref<Image> &p_image);
  309. Ref<Image> get_side(Side p_side) const;
  310. Image::Format get_format() const;
  311. int get_width() const;
  312. int get_height() const;
  313. virtual RID get_rid() const;
  314. void set_storage(Storage p_storage);
  315. Storage get_storage() const;
  316. void set_lossy_storage_quality(float p_lossy_storage_quality);
  317. float get_lossy_storage_quality() const;
  318. virtual void set_path(const String &p_path, bool p_take_over = false);
  319. CubeMap();
  320. ~CubeMap();
  321. };
  322. VARIANT_ENUM_CAST(CubeMap::Flags)
  323. VARIANT_ENUM_CAST(CubeMap::Side)
  324. VARIANT_ENUM_CAST(CubeMap::Storage)
  325. class TextureLayered : public Resource {
  326. GDCLASS(TextureLayered, Resource)
  327. public:
  328. enum Flags {
  329. FLAG_MIPMAPS = VisualServer::TEXTURE_FLAG_MIPMAPS,
  330. FLAG_REPEAT = VisualServer::TEXTURE_FLAG_REPEAT,
  331. FLAG_FILTER = VisualServer::TEXTURE_FLAG_FILTER,
  332. FLAG_CONVERT_TO_LINEAR = VisualServer::TEXTURE_FLAG_CONVERT_TO_LINEAR,
  333. FLAGS_DEFAULT = FLAG_FILTER,
  334. };
  335. private:
  336. bool is_3d;
  337. RID texture;
  338. Image::Format format;
  339. uint32_t flags;
  340. int width;
  341. int height;
  342. int depth;
  343. void _set_data(const Dictionary &p_data);
  344. Dictionary _get_data() const;
  345. protected:
  346. static void _bind_methods();
  347. public:
  348. void set_flags(uint32_t p_flags);
  349. uint32_t get_flags() const;
  350. Image::Format get_format() const;
  351. uint32_t get_width() const;
  352. uint32_t get_height() const;
  353. uint32_t get_depth() const;
  354. void create(uint32_t p_width, uint32_t p_height, uint32_t p_depth, Image::Format p_format, uint32_t p_flags = FLAGS_DEFAULT);
  355. void set_layer_data(const Ref<Image> &p_image, int p_layer);
  356. Ref<Image> get_layer_data(int p_layer) const;
  357. void set_data_partial(const Ref<Image> &p_image, int p_x_ofs, int p_y_ofs, int p_z, int p_mipmap = 0);
  358. virtual RID get_rid() const;
  359. virtual void set_path(const String &p_path, bool p_take_over = false);
  360. TextureLayered(bool p_3d = false);
  361. ~TextureLayered();
  362. };
  363. VARIANT_ENUM_CAST(TextureLayered::Flags)
  364. class Texture3D : public TextureLayered {
  365. GDCLASS(Texture3D, TextureLayered)
  366. public:
  367. Texture3D() :
  368. TextureLayered(true) {}
  369. };
  370. class TextureArray : public TextureLayered {
  371. GDCLASS(TextureArray, TextureLayered)
  372. public:
  373. TextureArray() :
  374. TextureLayered(false) {}
  375. };
  376. class ResourceFormatLoaderTextureLayered : public ResourceFormatLoader {
  377. GDCLASS(ResourceFormatLoaderTextureLayered, ResourceFormatLoader)
  378. public:
  379. enum Compression {
  380. COMPRESSION_LOSSLESS,
  381. COMPRESSION_VRAM,
  382. COMPRESSION_UNCOMPRESSED
  383. };
  384. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  385. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  386. virtual bool handles_type(const String &p_type) const;
  387. virtual String get_resource_type(const String &p_path) const;
  388. };
  389. class CurveTexture : public Texture {
  390. GDCLASS(CurveTexture, Texture)
  391. RES_BASE_EXTENSION("curvetex")
  392. private:
  393. RID _texture;
  394. Ref<Curve> _curve;
  395. int _width;
  396. void _update();
  397. protected:
  398. static void _bind_methods();
  399. public:
  400. void set_width(int p_width);
  401. int get_width() const;
  402. void ensure_default_setup(float p_min = 0, float p_max = 1);
  403. void set_curve(Ref<Curve> p_curve);
  404. Ref<Curve> get_curve() const;
  405. virtual RID get_rid() const;
  406. virtual int get_height() const { return 1; }
  407. virtual bool has_alpha() const { return false; }
  408. virtual void set_flags(uint32_t p_flags) {}
  409. virtual uint32_t get_flags() const { return FLAG_FILTER; }
  410. CurveTexture();
  411. ~CurveTexture();
  412. };
  413. /*
  414. enum CubeMapSide {
  415. CUBEMAP_LEFT,
  416. CUBEMAP_RIGHT,
  417. CUBEMAP_BOTTOM,
  418. CUBEMAP_TOP,
  419. CUBEMAP_FRONT,
  420. CUBEMAP_BACK,
  421. };
  422. */
  423. //VARIANT_ENUM_CAST( Texture::CubeMapSide );
  424. class GradientTexture : public Texture {
  425. GDCLASS(GradientTexture, Texture)
  426. public:
  427. struct Point {
  428. float offset;
  429. Color color;
  430. bool operator<(const Point &p_ponit) const {
  431. return offset < p_ponit.offset;
  432. }
  433. };
  434. private:
  435. Ref<Gradient> gradient;
  436. bool update_pending;
  437. RID texture;
  438. int width;
  439. void _queue_update();
  440. void _update();
  441. protected:
  442. static void _bind_methods();
  443. public:
  444. void set_gradient(Ref<Gradient> p_gradient);
  445. Ref<Gradient> get_gradient() const;
  446. void set_width(int p_width);
  447. int get_width() const;
  448. virtual RID get_rid() const { return texture; }
  449. virtual int get_height() const { return 1; }
  450. virtual bool has_alpha() const { return true; }
  451. virtual void set_flags(uint32_t p_flags) {}
  452. virtual uint32_t get_flags() const { return FLAG_FILTER; }
  453. virtual Ref<Image> get_data() const;
  454. GradientTexture();
  455. virtual ~GradientTexture();
  456. };
  457. class ProxyTexture : public Texture {
  458. GDCLASS(ProxyTexture, Texture)
  459. private:
  460. RID proxy;
  461. Ref<Texture> base;
  462. protected:
  463. static void _bind_methods();
  464. public:
  465. void set_base(const Ref<Texture> &p_texture);
  466. Ref<Texture> get_base() const;
  467. virtual int get_width() const;
  468. virtual int get_height() const;
  469. virtual RID get_rid() const;
  470. virtual bool has_alpha() const;
  471. virtual void set_flags(uint32_t p_flags);
  472. virtual uint32_t get_flags() const;
  473. ProxyTexture();
  474. ~ProxyTexture();
  475. };
  476. class AnimatedTexture : public Texture {
  477. GDCLASS(AnimatedTexture, Texture)
  478. //use readers writers lock for this, since its far more times read than written to
  479. RWLock *rw_lock;
  480. private:
  481. enum {
  482. MAX_FRAMES = 256
  483. };
  484. RID proxy;
  485. struct Frame {
  486. Ref<Texture> texture;
  487. float delay_sec;
  488. Frame() {
  489. delay_sec = 0;
  490. }
  491. };
  492. Frame frames[MAX_FRAMES];
  493. int frame_count;
  494. int current_frame;
  495. float fps;
  496. float time;
  497. uint64_t prev_ticks;
  498. void _update_proxy();
  499. protected:
  500. static void _bind_methods();
  501. void _validate_property(PropertyInfo &property) const;
  502. public:
  503. void set_frames(int p_frames);
  504. int get_frames() const;
  505. void set_frame_texture(int p_frame, const Ref<Texture> &p_texture);
  506. Ref<Texture> get_frame_texture(int p_frame) const;
  507. void set_frame_delay(int p_frame, float p_delay_sec);
  508. float get_frame_delay(int p_frame) const;
  509. void set_fps(float p_fps);
  510. float get_fps() const;
  511. virtual int get_width() const;
  512. virtual int get_height() const;
  513. virtual RID get_rid() const;
  514. virtual bool has_alpha() const;
  515. virtual void set_flags(uint32_t p_flags);
  516. virtual uint32_t get_flags() const;
  517. virtual Ref<Image> get_data() const;
  518. bool is_pixel_opaque(int p_x, int p_y) const;
  519. AnimatedTexture();
  520. ~AnimatedTexture();
  521. };
  522. #endif