render_world.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "render_world.h"
  24. #include "device.h"
  25. #include "allocator.h"
  26. #include "camera.h"
  27. #include "resource.h"
  28. #include "log.h"
  29. #include "sprite_resource.h"
  30. #include "sprite.h"
  31. #include "material.h"
  32. #include "config.h"
  33. #include "gui.h"
  34. #include "mesh_resource.h"
  35. #include "scene_graph.h"
  36. #include <bgfx.h>
  37. namespace crown
  38. {
  39. RenderWorld::RenderWorld()
  40. : m_sprite_pool(default_allocator(), MAX_SPRITES, sizeof(Sprite), CE_ALIGNOF(Sprite))
  41. , m_gui_pool(default_allocator(), MAX_GUIS, sizeof(Gui), CE_ALIGNOF(Gui))
  42. {
  43. }
  44. RenderWorld::~RenderWorld()
  45. {
  46. }
  47. SpriteId RenderWorld::create_sprite(SpriteResource* sr, SceneGraph& sg, int32_t node)
  48. {
  49. Sprite* sprite = CE_NEW(m_sprite_pool, Sprite)(*this, sg, node, sr);
  50. return id_array::create(m_sprite, sprite);
  51. }
  52. void RenderWorld::destroy_sprite(SpriteId id)
  53. {
  54. CE_DELETE(m_sprite_pool, id_array::get(m_sprite, id));
  55. id_array::destroy(m_sprite, id);
  56. }
  57. Sprite* RenderWorld::get_sprite(SpriteId id)
  58. {
  59. return id_array::get(m_sprite, id);
  60. }
  61. GuiId RenderWorld::create_gui(uint16_t width, uint16_t height, const char* material)
  62. {
  63. Gui* gui = CE_NEW(m_gui_pool, Gui)(width, height, material);
  64. GuiId id = id_array::create(m_guis, gui);
  65. gui->set_id(id);
  66. return id;
  67. }
  68. void RenderWorld::destroy_gui(GuiId id)
  69. {
  70. CE_DELETE(m_gui_pool, id_array::get(m_guis, id));
  71. id_array::destroy(m_guis, id);
  72. }
  73. Gui* RenderWorld::get_gui(GuiId id)
  74. {
  75. return id_array::get(m_guis, id);
  76. }
  77. void RenderWorld::update(const Matrix4x4& view, const Matrix4x4& projection, uint16_t x, uint16_t y, uint16_t width, uint16_t height, float dt)
  78. {
  79. // Set view 0 clear state.
  80. bgfx::setViewClear(0
  81. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  82. , 0x353839FF
  83. , 1.0f
  84. , 0);
  85. // Set view and projection matrix for view 0.
  86. bgfx::setViewTransform(0, matrix4x4::to_float_ptr(view), matrix4x4::to_float_ptr(projection));
  87. bgfx::setViewRect(0, 0, 0, width, height);
  88. // This dummy draw call is here to make sure that view 0 is cleared
  89. // if no other draw calls are submitted to view 0.
  90. bgfx::submit(0);
  91. // Draw all sprites
  92. for (uint32_t s = 0; s < id_array::size(m_sprite); s++)
  93. {
  94. m_sprite[s]->render();
  95. }
  96. }
  97. } // namespace crown