Sprite.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "Material.h"
  31. #include "RenderWorld.h"
  32. namespace crown
  33. {
  34. //-----------------------------------------------------------------------------
  35. Sprite::Sprite(RenderWorld& render_world, SceneGraph& sg, int32_t node, const SpriteResource* sr)
  36. : m_render_world(render_world)
  37. , m_scene_graph(sg)
  38. , m_node(node)
  39. , m_resource(sr)
  40. , m_frame(0)
  41. , m_animation(NULL)
  42. , m_time(0)
  43. , m_loop(false)
  44. {
  45. Blob vmem = sr->get_vertices();
  46. Blob imem = sr->get_indices();
  47. bgfx::VertexDecl decl;
  48. decl.begin()
  49. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  50. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float, true)
  51. .end();
  52. m_vb = bgfx::createVertexBuffer(bgfx::makeRef((void*) vmem.m_data, vmem.m_size), decl);
  53. m_ib = bgfx::createIndexBuffer(bgfx::makeRef((void*) imem.m_data, imem.m_size));
  54. }
  55. //-----------------------------------------------------------------------------
  56. Sprite::~Sprite()
  57. {
  58. }
  59. //-----------------------------------------------------------------------------
  60. Vector3 Sprite::local_position() const
  61. {
  62. return m_scene_graph.local_position(m_node);
  63. }
  64. //-----------------------------------------------------------------------------
  65. Quaternion Sprite::local_rotation() const
  66. {
  67. return m_scene_graph.local_rotation(m_node);
  68. }
  69. //-----------------------------------------------------------------------------
  70. Matrix4x4 Sprite::local_pose() const
  71. {
  72. return m_scene_graph.local_pose(m_node);
  73. }
  74. //-----------------------------------------------------------------------------
  75. Vector3 Sprite::world_position() const
  76. {
  77. return m_scene_graph.world_position(m_node);
  78. }
  79. //-----------------------------------------------------------------------------
  80. Quaternion Sprite::world_rotation() const
  81. {
  82. return m_scene_graph.world_rotation(m_node);
  83. }
  84. //-----------------------------------------------------------------------------
  85. Matrix4x4 Sprite::world_pose() const
  86. {
  87. return m_scene_graph.world_pose(m_node);
  88. }
  89. //-----------------------------------------------------------------------------
  90. void Sprite::set_local_position(Unit* unit, const Vector3& pos)
  91. {
  92. unit->set_local_position(m_node, pos);
  93. }
  94. //-----------------------------------------------------------------------------
  95. void Sprite::set_local_rotation(Unit* unit, const Quaternion& rot)
  96. {
  97. unit->set_local_rotation(m_node, rot);
  98. }
  99. //-----------------------------------------------------------------------------
  100. void Sprite::set_local_pose(Unit* unit, const Matrix4x4& pose)
  101. {
  102. unit->set_local_pose(m_node, pose);
  103. }
  104. //-----------------------------------------------------------------------------
  105. void Sprite::set_material(MaterialId mat)
  106. {
  107. m_material = mat;
  108. }
  109. //-----------------------------------------------------------------------------
  110. void Sprite::set_frame(uint32_t i)
  111. {
  112. m_frame = i;
  113. }
  114. //-----------------------------------------------------------------------------
  115. void Sprite::play_animation(const char* name, bool loop)
  116. {
  117. if (m_animation)
  118. return;
  119. m_animation = m_resource->get_animation(name);
  120. m_time = 0;
  121. m_loop = loop;
  122. m_frame = m_resource->get_animation_frame(m_animation, m_animation->start_frame);
  123. }
  124. //-----------------------------------------------------------------------------
  125. void Sprite::stop_animation()
  126. {
  127. m_animation = NULL;
  128. m_time = 0;
  129. m_frame = 0;
  130. }
  131. //-----------------------------------------------------------------------------
  132. void Sprite::update(float dt)
  133. {
  134. if (!m_animation)
  135. return;
  136. m_time += dt;
  137. if (m_time >= m_animation->time)
  138. {
  139. if (m_loop)
  140. {
  141. m_time = 0;
  142. }
  143. else
  144. {
  145. stop_animation();
  146. return;
  147. }
  148. }
  149. uint32_t frame = m_animation->num_frames * (m_time / m_animation->time);
  150. m_frame = m_resource->get_animation_frame(m_animation, frame);
  151. }
  152. //-----------------------------------------------------------------------------
  153. void Sprite::render()
  154. {
  155. Material* material = m_render_world.get_material(m_material);
  156. material->bind();
  157. ///
  158. /// @param _state State flags. Default state for primitive type is
  159. /// triangles. See: BGFX_STATE_DEFAULT.
  160. ///
  161. /// BGFX_STATE_ALPHA_WRITE - Enable alpha write.
  162. /// BGFX_STATE_DEPTH_WRITE - Enable depth write.
  163. /// BGFX_STATE_DEPTH_TEST_* - Depth test function.
  164. /// BGFX_STATE_BLEND_* - See NOTE 1: BGFX_STATE_BLEND_FUNC.
  165. /// BGFX_STATE_BLEND_EQUATION_* - See NOTE 2.
  166. /// BGFX_STATE_CULL_* - Backface culling mode.
  167. /// BGFX_STATE_RGB_WRITE - Enable RGB write.
  168. /// BGFX_STATE_MSAA - Enable MSAA.
  169. /// BGFX_STATE_PT_[LINES/POINTS] - Primitive type.
  170. ///
  171. /// @param _rgba Sets blend factor used by BGFX_STATE_BLEND_FACTOR and
  172. /// BGFX_STATE_BLEND_INV_FACTOR blend modes.
  173. ///
  174. /// NOTE:
  175. /// 1. Use BGFX_STATE_ALPHA_REF, BGFX_STATE_POINT_SIZE and
  176. /// BGFX_STATE_BLEND_FUNC macros to setup more complex states.
  177. /// 2. BGFX_STATE_BLEND_EQUATION_ADD is set when no other blend
  178. /// equation is specified.
  179. ///
  180. bgfx::setState(BGFX_STATE_DEFAULT
  181. | BGFX_STATE_BLEND_ALPHA);
  182. bgfx::setVertexBuffer(m_vb);
  183. bgfx::setIndexBuffer(m_ib, m_frame * 6, 6);
  184. bgfx::setTransform(matrix4x4::to_float_ptr(world_pose()));
  185. bgfx::submit(0);
  186. }
  187. } // namespace crown