sprite.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "sprite.h"
  6. #include "vector3.h"
  7. #include "quaternion.h"
  8. #include "sprite_resource.h"
  9. #include "allocator.h"
  10. #include "scene_graph.h"
  11. #include "unit.h"
  12. #include "material.h"
  13. #include "render_world.h"
  14. #include "device.h"
  15. #include "material_manager.h"
  16. namespace crown
  17. {
  18. Sprite::Sprite(RenderWorld& render_world, SceneGraph& sg, UnitId id, const SpriteResource* sr)
  19. : m_render_world(render_world)
  20. , m_scene_graph(sg)
  21. , m_resource(sr)
  22. , m_frame(0)
  23. , _depth(0)
  24. , _unit_id(id)
  25. {
  26. }
  27. void Sprite::set_material(MaterialId id)
  28. {
  29. m_material = id;
  30. }
  31. void Sprite::set_frame(uint32_t i)
  32. {
  33. m_frame = i;
  34. }
  35. void Sprite::set_depth(int32_t z)
  36. {
  37. _depth = z;
  38. }
  39. void Sprite::render()
  40. {
  41. if (m_material.id != INVALID_ID)
  42. material_manager::get()->lookup_material(m_material)->bind();
  43. bgfx::setState(BGFX_STATE_RGB_WRITE
  44. | BGFX_STATE_ALPHA_WRITE
  45. | BGFX_STATE_DEPTH_TEST_LEQUAL
  46. | BGFX_STATE_DEPTH_WRITE
  47. | BGFX_STATE_CULL_CW
  48. | BGFX_STATE_MSAA
  49. | BGFX_STATE_BLEND_ALPHA);
  50. bgfx::setVertexBuffer(m_resource->vb);
  51. bgfx::setIndexBuffer(m_resource->ib, m_frame * 6, 6);
  52. TransformInstance ti = m_scene_graph.get(_unit_id);
  53. bgfx::setTransform(matrix4x4::to_float_ptr(m_scene_graph.world_pose(ti)));
  54. bgfx::submit(0, _depth);
  55. }
  56. } // namespace crown