sprite.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "sprite_resource.h"
  27. #include "allocator.h"
  28. #include "scene_graph.h"
  29. #include "unit.h"
  30. #include "renderer.h"
  31. #include "material.h"
  32. #include "render_world.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_frame(0)
  42. , m_animation(NULL)
  43. , m_time(0)
  44. , m_loop(false)
  45. {
  46. m_vb = sr->vertex_buffer();
  47. m_ib = sr->index_buffer();
  48. }
  49. //-----------------------------------------------------------------------------
  50. Sprite::~Sprite()
  51. {
  52. }
  53. //-----------------------------------------------------------------------------
  54. Vector3 Sprite::local_position() const
  55. {
  56. return m_scene_graph.local_position(m_node);
  57. }
  58. //-----------------------------------------------------------------------------
  59. Quaternion Sprite::local_rotation() const
  60. {
  61. return m_scene_graph.local_rotation(m_node);
  62. }
  63. //-----------------------------------------------------------------------------
  64. Matrix4x4 Sprite::local_pose() const
  65. {
  66. return m_scene_graph.local_pose(m_node);
  67. }
  68. //-----------------------------------------------------------------------------
  69. Vector3 Sprite::world_position() const
  70. {
  71. return m_scene_graph.world_position(m_node);
  72. }
  73. //-----------------------------------------------------------------------------
  74. Quaternion Sprite::world_rotation() const
  75. {
  76. return m_scene_graph.world_rotation(m_node);
  77. }
  78. //-----------------------------------------------------------------------------
  79. Matrix4x4 Sprite::world_pose() const
  80. {
  81. return m_scene_graph.world_pose(m_node);
  82. }
  83. //-----------------------------------------------------------------------------
  84. void Sprite::set_local_position(Unit* unit, const Vector3& pos)
  85. {
  86. unit->set_local_position(m_node, pos);
  87. }
  88. //-----------------------------------------------------------------------------
  89. void Sprite::set_local_rotation(Unit* unit, const Quaternion& rot)
  90. {
  91. unit->set_local_rotation(m_node, rot);
  92. }
  93. //-----------------------------------------------------------------------------
  94. void Sprite::set_local_pose(Unit* unit, const Matrix4x4& pose)
  95. {
  96. unit->set_local_pose(m_node, pose);
  97. }
  98. //-----------------------------------------------------------------------------
  99. void Sprite::set_material(MaterialId mat)
  100. {
  101. m_material = mat;
  102. }
  103. //-----------------------------------------------------------------------------
  104. void Sprite::set_frame(uint32_t i)
  105. {
  106. m_frame = i;
  107. }
  108. //-----------------------------------------------------------------------------
  109. void Sprite::play_animation(const char* name, bool loop)
  110. {
  111. if (m_animation)
  112. return;
  113. m_animation = m_resource->get_animation(name);
  114. m_time = 0;
  115. m_loop = loop;
  116. m_frame = m_resource->get_animation_frame(m_animation, m_animation->start_frame);
  117. }
  118. //-----------------------------------------------------------------------------
  119. void Sprite::stop_animation()
  120. {
  121. m_animation = NULL;
  122. m_time = 0;
  123. m_frame = 0;
  124. }
  125. //-----------------------------------------------------------------------------
  126. void Sprite::update(float dt)
  127. {
  128. if (!m_animation)
  129. return;
  130. m_time += dt;
  131. if (m_time >= m_animation->time)
  132. {
  133. if (m_loop)
  134. {
  135. m_time = 0;
  136. }
  137. else
  138. {
  139. stop_animation();
  140. return;
  141. }
  142. }
  143. uint32_t frame = (uint32_t) m_animation->num_frames * (m_time / m_animation->time);
  144. m_frame = m_resource->get_animation_frame(m_animation, frame);
  145. }
  146. //-----------------------------------------------------------------------------
  147. void Sprite::render(Renderer& r, UniformId uniform, float dt)
  148. {
  149. Material* material = m_render_world.get_material(m_material);
  150. material->bind(r, uniform);
  151. r.set_state(STATE_DEPTH_WRITE
  152. | STATE_COLOR_WRITE
  153. | STATE_ALPHA_WRITE
  154. | STATE_CULL_CW
  155. | STATE_BLEND_EQUATION_ADD
  156. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  157. r.set_vertex_buffer(m_vb);
  158. r.set_index_buffer(m_ib, m_frame * 6, 6);
  159. r.set_pose(world_pose());
  160. r.commit(0);
  161. }
  162. } // namespace crown