texture.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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/curve.h"
  39. #include "scene/resources/gradient.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. bool image_stored;
  96. protected:
  97. virtual void reload_from_file();
  98. bool _set(const StringName &p_name, const Variant &p_value);
  99. bool _get(const StringName &p_name, Variant &r_ret) const;
  100. void _get_property_list(List<PropertyInfo> *p_list) const;
  101. void _reload_hook(const RID &p_hook);
  102. virtual void _resource_path_changed();
  103. static void _bind_methods();
  104. void _set_data(Dictionary p_data);
  105. public:
  106. void create(int p_width, int p_height, Image::Format p_format, uint32_t p_flags = FLAGS_DEFAULT);
  107. void create_from_image(const Ref<Image> &p_image, uint32_t p_flags = FLAGS_DEFAULT);
  108. void set_flags(uint32_t p_flags);
  109. uint32_t get_flags() const;
  110. Image::Format get_format() const;
  111. #ifndef DISABLE_DEPRECATED
  112. Error load(const String &p_path);
  113. #endif
  114. void set_data(const Ref<Image> &p_image);
  115. Ref<Image> get_data() const;
  116. int get_width() const;
  117. int get_height() const;
  118. virtual RID get_rid() const;
  119. bool has_alpha() const;
  120. 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;
  121. 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;
  122. 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;
  123. void set_storage(Storage p_storage);
  124. Storage get_storage() const;
  125. bool is_pixel_opaque(int p_x, int p_y) const;
  126. void set_lossy_storage_quality(float p_lossy_storage_quality);
  127. float get_lossy_storage_quality() const;
  128. void set_size_override(const Size2 &p_size);
  129. virtual void set_path(const String &p_path, bool p_take_over = false);
  130. ImageTexture();
  131. ~ImageTexture();
  132. };
  133. class StreamTexture : public Texture {
  134. GDCLASS(StreamTexture, Texture);
  135. public:
  136. enum DataFormat {
  137. DATA_FORMAT_IMAGE,
  138. DATA_FORMAT_LOSSLESS,
  139. DATA_FORMAT_LOSSY
  140. };
  141. enum FormatBits {
  142. FORMAT_MASK_IMAGE_FORMAT = (1 << 20) - 1,
  143. FORMAT_BIT_LOSSLESS = 1 << 20,
  144. FORMAT_BIT_LOSSY = 1 << 21,
  145. FORMAT_BIT_STREAM = 1 << 22,
  146. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  147. FORMAT_BIT_DETECT_3D = 1 << 24,
  148. FORMAT_BIT_DETECT_SRGB = 1 << 25,
  149. FORMAT_BIT_DETECT_NORMAL = 1 << 26,
  150. };
  151. private:
  152. Error _load_data(const String &p_path, int &tw, int &th, int &tw_custom, int &th_custom, int &flags, Ref<Image> &image, int p_size_limit = 0);
  153. String path_to_file;
  154. RID texture;
  155. Image::Format format;
  156. uint32_t flags;
  157. int w, h;
  158. mutable Ref<BitMap> alpha_cache;
  159. virtual void reload_from_file();
  160. static void _requested_3d(void *p_ud);
  161. static void _requested_srgb(void *p_ud);
  162. static void _requested_normal(void *p_ud);
  163. protected:
  164. static void _bind_methods();
  165. void _validate_property(PropertyInfo &property) const;
  166. public:
  167. typedef void (*TextureFormatRequestCallback)(const Ref<StreamTexture> &);
  168. static TextureFormatRequestCallback request_3d_callback;
  169. static TextureFormatRequestCallback request_srgb_callback;
  170. static TextureFormatRequestCallback request_normal_callback;
  171. uint32_t get_flags() const;
  172. Image::Format get_format() const;
  173. Error load(const String &p_path);
  174. String get_load_path() const;
  175. int get_width() const;
  176. int get_height() const;
  177. virtual RID get_rid() const;
  178. virtual void set_path(const String &p_path, bool p_take_over);
  179. 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;
  180. 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;
  181. 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;
  182. virtual bool has_alpha() const;
  183. virtual void set_flags(uint32_t p_flags);
  184. bool is_pixel_opaque(int p_x, int p_y) const;
  185. virtual Ref<Image> get_data() const;
  186. StreamTexture();
  187. ~StreamTexture();
  188. };
  189. class ResourceFormatLoaderStreamTexture : public ResourceFormatLoader {
  190. GDCLASS(ResourceFormatLoaderStreamTexture, ResourceFormatLoader)
  191. public:
  192. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  193. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  194. virtual bool handles_type(const String &p_type) const;
  195. virtual String get_resource_type(const String &p_path) const;
  196. };
  197. VARIANT_ENUM_CAST(ImageTexture::Storage);
  198. class AtlasTexture : public Texture {
  199. GDCLASS(AtlasTexture, Texture);
  200. RES_BASE_EXTENSION("atlastex");
  201. protected:
  202. Ref<Texture> atlas;
  203. Rect2 region;
  204. Rect2 margin;
  205. bool filter_clip;
  206. static void _bind_methods();
  207. public:
  208. virtual int get_width() const;
  209. virtual int get_height() const;
  210. virtual RID get_rid() const;
  211. virtual bool has_alpha() const;
  212. virtual void set_flags(uint32_t p_flags);
  213. virtual uint32_t get_flags() const;
  214. void set_atlas(const Ref<Texture> &p_atlas);
  215. Ref<Texture> get_atlas() const;
  216. void set_region(const Rect2 &p_region);
  217. Rect2 get_region() const;
  218. void set_margin(const Rect2 &p_margin);
  219. Rect2 get_margin() const;
  220. void set_filter_clip(const bool p_enable);
  221. bool has_filter_clip() const;
  222. 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;
  223. 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;
  224. 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;
  225. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const;
  226. bool is_pixel_opaque(int p_x, int p_y) const;
  227. AtlasTexture();
  228. };
  229. class LargeTexture : public Texture {
  230. GDCLASS(LargeTexture, Texture);
  231. RES_BASE_EXTENSION("largetex");
  232. protected:
  233. struct Piece {
  234. Point2 offset;
  235. Ref<Texture> texture;
  236. };
  237. Vector<Piece> pieces;
  238. Size2i size;
  239. Array _get_data() const;
  240. void _set_data(const Array &p_array);
  241. static void _bind_methods();
  242. public:
  243. virtual int get_width() const;
  244. virtual int get_height() const;
  245. virtual RID get_rid() const;
  246. virtual bool has_alpha() const;
  247. virtual void set_flags(uint32_t p_flags);
  248. virtual uint32_t get_flags() const;
  249. int add_piece(const Point2 &p_offset, const Ref<Texture> &p_texture);
  250. void set_piece_offset(int p_idx, const Point2 &p_offset);
  251. void set_piece_texture(int p_idx, const Ref<Texture> &p_texture);
  252. void set_size(const Size2 &p_size);
  253. void clear();
  254. int get_piece_count() const;
  255. Vector2 get_piece_offset(int p_idx) const;
  256. Ref<Texture> get_piece_texture(int p_idx) const;
  257. Ref<Image> to_image() const;
  258. 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;
  259. 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;
  260. 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;
  261. bool is_pixel_opaque(int p_x, int p_y) const;
  262. LargeTexture();
  263. };
  264. class CubeMap : public Resource {
  265. GDCLASS(CubeMap, Resource);
  266. RES_BASE_EXTENSION("cubemap");
  267. public:
  268. enum Storage {
  269. STORAGE_RAW,
  270. STORAGE_COMPRESS_LOSSY,
  271. STORAGE_COMPRESS_LOSSLESS
  272. };
  273. enum Side {
  274. SIDE_LEFT,
  275. SIDE_RIGHT,
  276. SIDE_BOTTOM,
  277. SIDE_TOP,
  278. SIDE_FRONT,
  279. SIDE_BACK
  280. };
  281. enum Flags {
  282. FLAG_MIPMAPS = VisualServer::TEXTURE_FLAG_MIPMAPS,
  283. FLAG_REPEAT = VisualServer::TEXTURE_FLAG_REPEAT,
  284. FLAG_FILTER = VisualServer::TEXTURE_FLAG_FILTER,
  285. FLAGS_DEFAULT = FLAG_MIPMAPS | FLAG_REPEAT | FLAG_FILTER,
  286. };
  287. private:
  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. _FORCE_INLINE_ bool _is_valid() const {
  297. for (int i = 0; i < 6; i++) {
  298. if (valid[i]) return true;
  299. }
  300. return false;
  301. }
  302. protected:
  303. bool _set(const StringName &p_name, const Variant &p_value);
  304. bool _get(const StringName &p_name, Variant &r_ret) const;
  305. void _get_property_list(List<PropertyInfo> *p_list) const;
  306. static void _bind_methods();
  307. public:
  308. void set_flags(uint32_t p_flags);
  309. uint32_t get_flags() const;
  310. void set_side(Side p_side, const Ref<Image> &p_image);
  311. Ref<Image> get_side(Side p_side) const;
  312. Image::Format get_format() const;
  313. int get_width() const;
  314. int get_height() const;
  315. virtual RID get_rid() const;
  316. void set_storage(Storage p_storage);
  317. Storage get_storage() const;
  318. void set_lossy_storage_quality(float p_lossy_storage_quality);
  319. float get_lossy_storage_quality() const;
  320. virtual void set_path(const String &p_path, bool p_take_over = false);
  321. CubeMap();
  322. ~CubeMap();
  323. };
  324. VARIANT_ENUM_CAST(CubeMap::Flags)
  325. VARIANT_ENUM_CAST(CubeMap::Side)
  326. VARIANT_ENUM_CAST(CubeMap::Storage)
  327. class TextureLayered : public Resource {
  328. GDCLASS(TextureLayered, Resource)
  329. public:
  330. enum Flags {
  331. FLAG_MIPMAPS = VisualServer::TEXTURE_FLAG_MIPMAPS,
  332. FLAG_REPEAT = VisualServer::TEXTURE_FLAG_REPEAT,
  333. FLAG_FILTER = VisualServer::TEXTURE_FLAG_FILTER,
  334. FLAG_CONVERT_TO_LINEAR = VisualServer::TEXTURE_FLAG_CONVERT_TO_LINEAR,
  335. FLAGS_DEFAULT = FLAG_FILTER,
  336. };
  337. private:
  338. bool is_3d;
  339. RID texture;
  340. Image::Format format;
  341. uint32_t flags;
  342. int width;
  343. int height;
  344. int depth;
  345. void _set_data(const Dictionary &p_data);
  346. Dictionary _get_data() const;
  347. protected:
  348. static void _bind_methods();
  349. public:
  350. void set_flags(uint32_t p_flags);
  351. uint32_t get_flags() const;
  352. Image::Format get_format() const;
  353. uint32_t get_width() const;
  354. uint32_t get_height() const;
  355. uint32_t get_depth() const;
  356. void create(uint32_t p_width, uint32_t p_height, uint32_t p_depth, Image::Format p_format, uint32_t p_flags = FLAGS_DEFAULT);
  357. void set_layer_data(const Ref<Image> &p_image, int p_layer);
  358. Ref<Image> get_layer_data(int p_layer) const;
  359. void set_data_partial(const Ref<Image> &p_image, int p_x_ofs, int p_y_ofs, int p_z, int p_mipmap = 0);
  360. virtual RID get_rid() const;
  361. virtual void set_path(const String &p_path, bool p_take_over = false);
  362. TextureLayered(bool p_3d = false);
  363. ~TextureLayered();
  364. };
  365. VARIANT_ENUM_CAST(TextureLayered::Flags)
  366. class Texture3D : public TextureLayered {
  367. GDCLASS(Texture3D, TextureLayered)
  368. public:
  369. Texture3D() :
  370. TextureLayered(true) {}
  371. };
  372. class TextureArray : public TextureLayered {
  373. GDCLASS(TextureArray, TextureLayered)
  374. public:
  375. TextureArray() :
  376. TextureLayered(false) {}
  377. };
  378. class ResourceFormatLoaderTextureLayered : public ResourceFormatLoader {
  379. GDCLASS(ResourceFormatLoaderTextureLayered, ResourceFormatLoader)
  380. public:
  381. enum Compression {
  382. COMPRESSION_LOSSLESS,
  383. COMPRESSION_VRAM,
  384. COMPRESSION_UNCOMPRESSED
  385. };
  386. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  387. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  388. virtual bool handles_type(const String &p_type) const;
  389. virtual String get_resource_type(const String &p_path) const;
  390. };
  391. class CurveTexture : public Texture {
  392. GDCLASS(CurveTexture, Texture)
  393. RES_BASE_EXTENSION("curvetex")
  394. private:
  395. RID _texture;
  396. Ref<Curve> _curve;
  397. int _width;
  398. void _update();
  399. protected:
  400. static void _bind_methods();
  401. public:
  402. void set_width(int p_width);
  403. int get_width() const;
  404. void ensure_default_setup(float p_min = 0, float p_max = 1);
  405. void set_curve(Ref<Curve> p_curve);
  406. Ref<Curve> get_curve() const;
  407. virtual RID get_rid() const;
  408. virtual int get_height() const { return 1; }
  409. virtual bool has_alpha() const { return false; }
  410. virtual void set_flags(uint32_t p_flags) {}
  411. virtual uint32_t get_flags() const { return FLAG_FILTER; }
  412. CurveTexture();
  413. ~CurveTexture();
  414. };
  415. /*
  416. enum CubeMapSide {
  417. CUBEMAP_LEFT,
  418. CUBEMAP_RIGHT,
  419. CUBEMAP_BOTTOM,
  420. CUBEMAP_TOP,
  421. CUBEMAP_FRONT,
  422. CUBEMAP_BACK,
  423. };
  424. */
  425. //VARIANT_ENUM_CAST( Texture::CubeMapSide );
  426. class GradientTexture : public Texture {
  427. GDCLASS(GradientTexture, Texture)
  428. public:
  429. struct Point {
  430. float offset;
  431. Color color;
  432. bool operator<(const Point &p_ponit) const {
  433. return offset < p_ponit.offset;
  434. }
  435. };
  436. private:
  437. Ref<Gradient> gradient;
  438. bool update_pending;
  439. RID texture;
  440. int width;
  441. void _queue_update();
  442. void _update();
  443. protected:
  444. static void _bind_methods();
  445. public:
  446. void set_gradient(Ref<Gradient> p_gradient);
  447. Ref<Gradient> get_gradient() const;
  448. void set_width(int p_width);
  449. int get_width() const;
  450. virtual RID get_rid() const { return texture; }
  451. virtual int get_height() const { return 1; }
  452. virtual bool has_alpha() const { return true; }
  453. virtual void set_flags(uint32_t p_flags) {}
  454. virtual uint32_t get_flags() const { return FLAG_FILTER; }
  455. virtual Ref<Image> get_data() const;
  456. GradientTexture();
  457. virtual ~GradientTexture();
  458. };
  459. class ProxyTexture : public Texture {
  460. GDCLASS(ProxyTexture, Texture)
  461. private:
  462. RID proxy;
  463. Ref<Texture> base;
  464. protected:
  465. static void _bind_methods();
  466. public:
  467. void set_base(const Ref<Texture> &p_texture);
  468. Ref<Texture> get_base() const;
  469. virtual int get_width() const;
  470. virtual int get_height() const;
  471. virtual RID get_rid() const;
  472. virtual bool has_alpha() const;
  473. virtual void set_flags(uint32_t p_flags);
  474. virtual uint32_t get_flags() const;
  475. ProxyTexture();
  476. ~ProxyTexture();
  477. };
  478. class AnimatedTexture : public Texture {
  479. GDCLASS(AnimatedTexture, Texture)
  480. //use readers writers lock for this, since its far more times read than written to
  481. RWLock *rw_lock;
  482. private:
  483. enum {
  484. MAX_FRAMES = 256
  485. };
  486. RID proxy;
  487. struct Frame {
  488. Ref<Texture> texture;
  489. float delay_sec;
  490. Frame() {
  491. delay_sec = 0;
  492. }
  493. };
  494. Frame frames[MAX_FRAMES];
  495. int frame_count;
  496. int current_frame;
  497. float fps;
  498. float time;
  499. uint64_t prev_ticks;
  500. void _update_proxy();
  501. protected:
  502. static void _bind_methods();
  503. void _validate_property(PropertyInfo &property) const;
  504. public:
  505. void set_frames(int p_frames);
  506. int get_frames() const;
  507. void set_frame_texture(int p_frame, const Ref<Texture> &p_texture);
  508. Ref<Texture> get_frame_texture(int p_frame) const;
  509. void set_frame_delay(int p_frame, float p_delay_sec);
  510. float get_frame_delay(int p_frame) const;
  511. void set_fps(float p_fps);
  512. float get_fps() const;
  513. virtual int get_width() const;
  514. virtual int get_height() const;
  515. virtual RID get_rid() const;
  516. virtual bool has_alpha() const;
  517. virtual void set_flags(uint32_t p_flags);
  518. virtual uint32_t get_flags() const;
  519. virtual Ref<Image> get_data() const;
  520. bool is_pixel_opaque(int p_x, int p_y) const;
  521. AnimatedTexture();
  522. ~AnimatedTexture();
  523. };
  524. #endif