render_world.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "render_world.h"
  6. #include "device.h"
  7. #include "allocator.h"
  8. #include "camera.h"
  9. #include "log.h"
  10. #include "sprite_resource.h"
  11. #include "sprite.h"
  12. #include "material.h"
  13. #include "config.h"
  14. #include "gui.h"
  15. #include "mesh_resource.h"
  16. #include "scene_graph.h"
  17. #include <bgfx.h>
  18. namespace crown
  19. {
  20. RenderWorld::RenderWorld()
  21. : m_sprite_pool(default_allocator(), MAX_SPRITES, sizeof(Sprite), CE_ALIGNOF(Sprite))
  22. , m_gui_pool(default_allocator(), MAX_GUIS, sizeof(Gui), CE_ALIGNOF(Gui))
  23. {
  24. }
  25. RenderWorld::~RenderWorld()
  26. {
  27. }
  28. SpriteId RenderWorld::create_sprite(SpriteResource* sr, SceneGraph& sg, int32_t node)
  29. {
  30. Sprite* sprite = CE_NEW(m_sprite_pool, Sprite)(*this, sg, node, sr);
  31. return id_array::create(m_sprite, sprite);
  32. }
  33. void RenderWorld::destroy_sprite(SpriteId id)
  34. {
  35. CE_DELETE(m_sprite_pool, id_array::get(m_sprite, id));
  36. id_array::destroy(m_sprite, id);
  37. }
  38. Sprite* RenderWorld::get_sprite(SpriteId id)
  39. {
  40. return id_array::get(m_sprite, id);
  41. }
  42. GuiId RenderWorld::create_gui(uint16_t width, uint16_t height, const char* material)
  43. {
  44. Gui* gui = CE_NEW(m_gui_pool, Gui)(width, height, material);
  45. GuiId id = id_array::create(m_guis, gui);
  46. gui->set_id(id);
  47. return id;
  48. }
  49. void RenderWorld::destroy_gui(GuiId id)
  50. {
  51. CE_DELETE(m_gui_pool, id_array::get(m_guis, id));
  52. id_array::destroy(m_guis, id);
  53. }
  54. Gui* RenderWorld::get_gui(GuiId id)
  55. {
  56. return id_array::get(m_guis, id);
  57. }
  58. void RenderWorld::update(const Matrix4x4& view, const Matrix4x4& projection, uint16_t x, uint16_t y, uint16_t width, uint16_t height, float dt)
  59. {
  60. // Set view 0 clear state.
  61. bgfx::setViewClear(0
  62. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  63. , 0x353839FF
  64. , 1.0f
  65. , 0);
  66. // Set view and projection matrix for view 0.
  67. bgfx::setViewTransform(0, matrix4x4::to_float_ptr(view), matrix4x4::to_float_ptr(projection));
  68. bgfx::setViewRect(0, 0, 0, width, height);
  69. // This dummy draw call is here to make sure that view 0 is cleared
  70. // if no other draw calls are submitted to view 0.
  71. bgfx::submit(0);
  72. // Draw all sprites
  73. for (uint32_t s = 0; s < id_array::size(m_sprite); s++)
  74. {
  75. m_sprite[s]->render();
  76. }
  77. }
  78. } // namespace crown