Sprite.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "SpriteAnimator.h"
  29. namespace crown
  30. {
  31. //-----------------------------------------------------------------------------
  32. void Sprite::create(SpriteResource* sr, int32_t node, const Vector3& pos, const Quaternion& rot)
  33. {
  34. m_vb = sr->m_vb;
  35. m_ib = sr->m_ib;
  36. m_texture = sr->m_texture;
  37. m_vertex = sr->m_vertex;
  38. m_fragment = sr->m_fragment;
  39. m_program = sr->m_program;
  40. m_uniform = sr->m_uniform;
  41. set_local_position(pos);
  42. set_local_rotation(rot);
  43. m_node = node;
  44. m_animator = CE_NEW(default_allocator(), SpriteAnimator)(sr);
  45. }
  46. //-----------------------------------------------------------------------------
  47. void Sprite::destroy()
  48. {
  49. if (m_animator)
  50. {
  51. CE_DELETE(default_allocator(), m_animator);
  52. }
  53. }
  54. //-----------------------------------------------------------------------------
  55. Vector3 Sprite::local_position() const
  56. {
  57. Vector3 tmp = m_local_pose.translation();
  58. return tmp;
  59. }
  60. //-----------------------------------------------------------------------------
  61. Quaternion Sprite::local_rotation() const
  62. {
  63. Quaternion tmp = m_local_pose.to_quat();
  64. return tmp;
  65. }
  66. //-----------------------------------------------------------------------------
  67. Matrix4x4 Sprite::local_pose() const
  68. {
  69. return m_local_pose;
  70. }
  71. //-----------------------------------------------------------------------------
  72. Vector3 Sprite::world_position() const
  73. {
  74. Vector3 tmp = m_world_pose.translation();
  75. return tmp;
  76. }
  77. //-----------------------------------------------------------------------------
  78. Quaternion Sprite::world_rotation() const
  79. {
  80. Quaternion tmp = m_world_pose.to_quat();
  81. return tmp;
  82. }
  83. //-----------------------------------------------------------------------------
  84. Matrix4x4 Sprite::world_pose() const
  85. {
  86. return m_world_pose;
  87. }
  88. //-----------------------------------------------------------------------------
  89. void Sprite::set_local_position(const Vector3& pos)
  90. {
  91. m_local_pose.set_translation(pos);
  92. }
  93. //-----------------------------------------------------------------------------
  94. void Sprite::set_local_rotation(const Quaternion& rot)
  95. {
  96. Matrix4x4& local_pose = m_local_pose;
  97. Vector3 local_translation = local_pose.translation();
  98. local_pose = rot.to_mat4();
  99. local_pose.set_translation(local_translation);
  100. }
  101. //-----------------------------------------------------------------------------
  102. void Sprite::set_local_pose(const Matrix4x4& pose)
  103. {
  104. m_local_pose = pose;
  105. }
  106. } // namespace crown