Sprite.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "Sprite.h"
  24. #include "Vector3.h"
  25. #include "Quaternion.h"
  26. #include "SpriteResource.h"
  27. #include "Allocator.h"
  28. #include "SceneGraph.h"
  29. #include "Unit.h"
  30. #include "Renderer.h"
  31. #include "Material.h"
  32. #include "RenderWorld.h"
  33. namespace crown
  34. {
  35. //-----------------------------------------------------------------------------
  36. Sprite::Sprite(RenderWorld& render_world, SceneGraph& sg, int32_t node, const SpriteResource* sr)
  37. : m_render_world(render_world)
  38. , m_scene_graph(sg)
  39. , m_node(node)
  40. , m_resource(sr)
  41. , m_start_frame(0)
  42. , m_cur_frame(0)
  43. , m_tot_time(1.0)
  44. , m_anim_time(0.0)
  45. , m_loop(false)
  46. , m_is_playing(false)
  47. {
  48. m_vb = sr->vertex_buffer();
  49. m_ib = sr->index_buffer();
  50. }
  51. //-----------------------------------------------------------------------------
  52. Sprite::~Sprite()
  53. {
  54. }
  55. //-----------------------------------------------------------------------------
  56. Vector3 Sprite::local_position() const
  57. {
  58. return m_scene_graph.local_position(m_node);
  59. }
  60. //-----------------------------------------------------------------------------
  61. Quaternion Sprite::local_rotation() const
  62. {
  63. return m_scene_graph.local_rotation(m_node);
  64. }
  65. //-----------------------------------------------------------------------------
  66. Matrix4x4 Sprite::local_pose() const
  67. {
  68. return m_scene_graph.local_pose(m_node);
  69. }
  70. //-----------------------------------------------------------------------------
  71. Vector3 Sprite::world_position() const
  72. {
  73. return m_scene_graph.world_position(m_node);
  74. }
  75. //-----------------------------------------------------------------------------
  76. Quaternion Sprite::world_rotation() const
  77. {
  78. return m_scene_graph.world_rotation(m_node);
  79. }
  80. //-----------------------------------------------------------------------------
  81. Matrix4x4 Sprite::world_pose() const
  82. {
  83. return m_scene_graph.world_pose(m_node);
  84. }
  85. //-----------------------------------------------------------------------------
  86. void Sprite::set_local_position(Unit* unit, const Vector3& pos)
  87. {
  88. unit->set_local_position(m_node, pos);
  89. }
  90. //-----------------------------------------------------------------------------
  91. void Sprite::set_local_rotation(Unit* unit, const Quaternion& rot)
  92. {
  93. unit->set_local_rotation(m_node, rot);
  94. }
  95. //-----------------------------------------------------------------------------
  96. void Sprite::set_local_pose(Unit* unit, const Matrix4x4& pose)
  97. {
  98. unit->set_local_pose(m_node, pose);
  99. }
  100. //-----------------------------------------------------------------------------
  101. void Sprite::play_animation(uint32_t start, uint32_t end, float time, bool loop)
  102. {
  103. m_start_frame = start;
  104. m_end_frame = end;
  105. m_tot_time = time;
  106. m_anim_time = 0.0;
  107. m_loop = loop;
  108. }
  109. //-----------------------------------------------------------------------------
  110. void Sprite::stop_animation()
  111. {
  112. m_cur_frame = 0;
  113. }
  114. //-----------------------------------------------------------------------------
  115. void Sprite::set_frame(uint32_t i)
  116. {
  117. CE_ASSERT(i < m_resource->num_frames(), "Frame out of bounds");
  118. m_cur_frame = i;
  119. }
  120. //-----------------------------------------------------------------------------
  121. void Sprite::set_material(MaterialId mat)
  122. {
  123. m_material = mat;
  124. }
  125. //-----------------------------------------------------------------------------
  126. void Sprite::render(Renderer& r, UniformId uniform, float dt)
  127. {
  128. // Compute current frame
  129. uint32_t cur_frame = ((m_end_frame - m_start_frame) / m_tot_time) * m_anim_time;
  130. m_cur_frame = cur_frame > m_end_frame ? m_end_frame : cur_frame;
  131. m_anim_time += dt;
  132. Material* material = m_render_world.lookup_material(m_material);
  133. material->bind(r, uniform);
  134. r.set_state(STATE_DEPTH_WRITE
  135. | STATE_COLOR_WRITE
  136. | STATE_ALPHA_WRITE
  137. | STATE_CULL_CW
  138. | STATE_BLEND_EQUATION_ADD
  139. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  140. r.set_vertex_buffer(m_vb);
  141. const uint32_t start_index = m_cur_frame * 6;
  142. r.set_index_buffer(m_ib, start_index, 6);
  143. r.set_pose(world_pose());
  144. r.commit(0);
  145. if (m_cur_frame == m_end_frame && m_loop)
  146. {
  147. m_anim_time = 0.0;
  148. m_cur_frame = m_start_frame;
  149. }
  150. }
  151. } // namespace crown