animated_texture.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**************************************************************************/
  2. /* animated_texture.cpp */
  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. #include "animated_texture.h"
  31. #include "core/os/os.h"
  32. #include "servers/rendering/rendering_server.h"
  33. void AnimatedTexture::_update_proxy() {
  34. RWLockRead r(rw_lock);
  35. float delta;
  36. if (prev_ticks == 0) {
  37. delta = 0;
  38. prev_ticks = OS::get_singleton()->get_ticks_usec();
  39. } else {
  40. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  41. delta = float(double(ticks - prev_ticks) / 1000000.0);
  42. prev_ticks = ticks;
  43. }
  44. time += delta;
  45. float speed = speed_scale == 0 ? 0 : std::abs(1.0 / speed_scale);
  46. int iter_max = frame_count;
  47. while (iter_max && !pause) {
  48. float frame_limit = frames[current_frame].duration * speed;
  49. if (time > frame_limit) {
  50. if (speed_scale > 0.0) {
  51. current_frame++;
  52. } else {
  53. current_frame--;
  54. }
  55. if (current_frame >= frame_count) {
  56. if (one_shot) {
  57. current_frame = frame_count - 1;
  58. } else {
  59. current_frame = 0;
  60. }
  61. } else if (current_frame < 0) {
  62. if (one_shot) {
  63. current_frame = 0;
  64. } else {
  65. current_frame = frame_count - 1;
  66. }
  67. }
  68. time -= frame_limit;
  69. } else {
  70. break;
  71. }
  72. iter_max--;
  73. }
  74. if (frames[current_frame].texture.is_valid()) {
  75. RenderingServer::get_singleton()->texture_proxy_update(proxy, frames[current_frame].texture->get_rid());
  76. }
  77. }
  78. void AnimatedTexture::set_frames(int p_frames) {
  79. ERR_FAIL_COND(p_frames < 1 || p_frames > MAX_FRAMES);
  80. RWLockWrite r(rw_lock);
  81. frame_count = p_frames;
  82. }
  83. int AnimatedTexture::get_frames() const {
  84. return frame_count;
  85. }
  86. void AnimatedTexture::set_current_frame(int p_frame) {
  87. ERR_FAIL_COND(p_frame < 0 || p_frame >= frame_count);
  88. RWLockWrite r(rw_lock);
  89. current_frame = p_frame;
  90. time = 0;
  91. }
  92. int AnimatedTexture::get_current_frame() const {
  93. return current_frame;
  94. }
  95. void AnimatedTexture::set_pause(bool p_pause) {
  96. RWLockWrite r(rw_lock);
  97. pause = p_pause;
  98. }
  99. bool AnimatedTexture::get_pause() const {
  100. return pause;
  101. }
  102. void AnimatedTexture::set_one_shot(bool p_one_shot) {
  103. RWLockWrite r(rw_lock);
  104. one_shot = p_one_shot;
  105. }
  106. bool AnimatedTexture::get_one_shot() const {
  107. return one_shot;
  108. }
  109. void AnimatedTexture::set_frame_texture(int p_frame, const Ref<Texture2D> &p_texture) {
  110. ERR_FAIL_COND(p_texture == this);
  111. ERR_FAIL_INDEX(p_frame, MAX_FRAMES);
  112. RWLockWrite w(rw_lock);
  113. frames[p_frame].texture = p_texture;
  114. }
  115. Ref<Texture2D> AnimatedTexture::get_frame_texture(int p_frame) const {
  116. ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, Ref<Texture2D>());
  117. RWLockRead r(rw_lock);
  118. return frames[p_frame].texture;
  119. }
  120. void AnimatedTexture::set_frame_duration(int p_frame, float p_duration) {
  121. ERR_FAIL_INDEX(p_frame, MAX_FRAMES);
  122. RWLockWrite r(rw_lock);
  123. frames[p_frame].duration = p_duration;
  124. }
  125. float AnimatedTexture::get_frame_duration(int p_frame) const {
  126. ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, 0);
  127. RWLockRead r(rw_lock);
  128. return frames[p_frame].duration;
  129. }
  130. void AnimatedTexture::set_speed_scale(float p_scale) {
  131. ERR_FAIL_COND(p_scale < -1000 || p_scale >= 1000);
  132. RWLockWrite r(rw_lock);
  133. speed_scale = p_scale;
  134. }
  135. float AnimatedTexture::get_speed_scale() const {
  136. return speed_scale;
  137. }
  138. int AnimatedTexture::get_width() const {
  139. RWLockRead r(rw_lock);
  140. if (frames[current_frame].texture.is_null()) {
  141. return 1;
  142. }
  143. return frames[current_frame].texture->get_width();
  144. }
  145. int AnimatedTexture::get_height() const {
  146. RWLockRead r(rw_lock);
  147. if (frames[current_frame].texture.is_null()) {
  148. return 1;
  149. }
  150. return frames[current_frame].texture->get_height();
  151. }
  152. RID AnimatedTexture::get_rid() const {
  153. return proxy;
  154. }
  155. bool AnimatedTexture::has_alpha() const {
  156. RWLockRead r(rw_lock);
  157. if (frames[current_frame].texture.is_null()) {
  158. return false;
  159. }
  160. return frames[current_frame].texture->has_alpha();
  161. }
  162. Ref<Image> AnimatedTexture::get_image() const {
  163. RWLockRead r(rw_lock);
  164. if (frames[current_frame].texture.is_null()) {
  165. return Ref<Image>();
  166. }
  167. return frames[current_frame].texture->get_image();
  168. }
  169. bool AnimatedTexture::is_pixel_opaque(int p_x, int p_y) const {
  170. RWLockRead r(rw_lock);
  171. if (frames[current_frame].texture.is_valid()) {
  172. return frames[current_frame].texture->is_pixel_opaque(p_x, p_y);
  173. }
  174. return true;
  175. }
  176. void AnimatedTexture::_validate_property(PropertyInfo &p_property) const {
  177. String prop = p_property.name;
  178. if (prop.begins_with("frame_")) {
  179. int frame = prop.get_slicec('/', 0).get_slicec('_', 1).to_int();
  180. if (frame >= frame_count) {
  181. p_property.usage = PROPERTY_USAGE_NONE;
  182. }
  183. }
  184. }
  185. void AnimatedTexture::_bind_methods() {
  186. ClassDB::bind_method(D_METHOD("set_frames", "frames"), &AnimatedTexture::set_frames);
  187. ClassDB::bind_method(D_METHOD("get_frames"), &AnimatedTexture::get_frames);
  188. ClassDB::bind_method(D_METHOD("set_current_frame", "frame"), &AnimatedTexture::set_current_frame);
  189. ClassDB::bind_method(D_METHOD("get_current_frame"), &AnimatedTexture::get_current_frame);
  190. ClassDB::bind_method(D_METHOD("set_pause", "pause"), &AnimatedTexture::set_pause);
  191. ClassDB::bind_method(D_METHOD("get_pause"), &AnimatedTexture::get_pause);
  192. ClassDB::bind_method(D_METHOD("set_one_shot", "one_shot"), &AnimatedTexture::set_one_shot);
  193. ClassDB::bind_method(D_METHOD("get_one_shot"), &AnimatedTexture::get_one_shot);
  194. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &AnimatedTexture::set_speed_scale);
  195. ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedTexture::get_speed_scale);
  196. ClassDB::bind_method(D_METHOD("set_frame_texture", "frame", "texture"), &AnimatedTexture::set_frame_texture);
  197. ClassDB::bind_method(D_METHOD("get_frame_texture", "frame"), &AnimatedTexture::get_frame_texture);
  198. ClassDB::bind_method(D_METHOD("set_frame_duration", "frame", "duration"), &AnimatedTexture::set_frame_duration);
  199. ClassDB::bind_method(D_METHOD("get_frame_duration", "frame"), &AnimatedTexture::get_frame_duration);
  200. ADD_PROPERTY(PropertyInfo(Variant::INT, "frames", PROPERTY_HINT_RANGE, "1," + itos(MAX_FRAMES), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_frames", "get_frames");
  201. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_frame", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_current_frame", "get_current_frame");
  202. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pause"), "set_pause", "get_pause");
  203. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  204. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "-60,60,0.1,or_less,or_greater"), "set_speed_scale", "get_speed_scale");
  205. for (int i = 0; i < MAX_FRAMES; i++) {
  206. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "frame_" + itos(i) + "/texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_frame_texture", "get_frame_texture", i);
  207. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "frame_" + itos(i) + "/duration", PROPERTY_HINT_RANGE, "0.0,16.0,0.01,or_greater,suffix:s", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_frame_duration", "get_frame_duration", i);
  208. }
  209. BIND_CONSTANT(MAX_FRAMES);
  210. }
  211. void AnimatedTexture::_finish_non_thread_safe_setup() {
  212. RenderingServer::get_singleton()->connect("frame_pre_draw", callable_mp(this, &AnimatedTexture::_update_proxy));
  213. }
  214. AnimatedTexture::AnimatedTexture() {
  215. //proxy = RS::get_singleton()->texture_create();
  216. proxy_ph = RS::get_singleton()->texture_2d_placeholder_create();
  217. proxy = RS::get_singleton()->texture_proxy_create(proxy_ph);
  218. RenderingServer::get_singleton()->texture_set_force_redraw_if_visible(proxy, true);
  219. MessageQueue::get_main_singleton()->push_callable(callable_mp(this, &AnimatedTexture::_finish_non_thread_safe_setup));
  220. }
  221. AnimatedTexture::~AnimatedTexture() {
  222. ERR_FAIL_NULL(RenderingServer::get_singleton());
  223. RS::get_singleton()->free_rid(proxy);
  224. RS::get_singleton()->free_rid(proxy_ph);
  225. }