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