animated_texture.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**************************************************************************/
  2. /* animated_texture.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 ANIMATED_TEXTURE_H
  31. #define ANIMATED_TEXTURE_H
  32. #include "scene/resources/texture.h"
  33. class AnimatedTexture : public Texture2D {
  34. GDCLASS(AnimatedTexture, Texture2D);
  35. // Use readers writers lock for this, since its far more times read than written to.
  36. RWLock rw_lock;
  37. public:
  38. enum {
  39. MAX_FRAMES = 256
  40. };
  41. private:
  42. RID proxy_ph;
  43. RID proxy;
  44. struct Frame {
  45. Ref<Texture2D> texture;
  46. float duration = 1.0;
  47. };
  48. Frame frames[MAX_FRAMES];
  49. int frame_count = 1.0;
  50. int current_frame = 0;
  51. bool pause = false;
  52. bool one_shot = false;
  53. float speed_scale = 1.0;
  54. float time = 0.0;
  55. uint64_t prev_ticks = 0;
  56. void _update_proxy();
  57. void _finish_non_thread_safe_setup();
  58. protected:
  59. static void _bind_methods();
  60. void _validate_property(PropertyInfo &p_property) const;
  61. public:
  62. void set_frames(int p_frames);
  63. int get_frames() const;
  64. void set_current_frame(int p_frame);
  65. int get_current_frame() const;
  66. void set_pause(bool p_pause);
  67. bool get_pause() const;
  68. void set_one_shot(bool p_one_shot);
  69. bool get_one_shot() const;
  70. void set_frame_texture(int p_frame, const Ref<Texture2D> &p_texture);
  71. Ref<Texture2D> get_frame_texture(int p_frame) const;
  72. void set_frame_duration(int p_frame, float p_duration);
  73. float get_frame_duration(int p_frame) const;
  74. void set_speed_scale(float p_scale);
  75. float get_speed_scale() const;
  76. virtual int get_width() const override;
  77. virtual int get_height() const override;
  78. virtual RID get_rid() const override;
  79. virtual bool has_alpha() const override;
  80. virtual Ref<Image> get_image() const override;
  81. bool is_pixel_opaque(int p_x, int p_y) const override;
  82. AnimatedTexture();
  83. ~AnimatedTexture();
  84. };
  85. #endif // ANIMATED_TEXTURE_H