UnitCompiler.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "Allocator.h"
  24. #include "File.h"
  25. #include "Filesystem.h"
  26. #include "Hash.h"
  27. #include "JSONParser.h"
  28. #include "UnitCompiler.h"
  29. #include "TempAllocator.h"
  30. #include "Log.h"
  31. namespace crown
  32. {
  33. //-----------------------------------------------------------------------------
  34. UnitCompiler::UnitCompiler()
  35. : m_renderable(default_allocator())
  36. , m_camera(default_allocator())
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. size_t UnitCompiler::compile_impl(Filesystem& fs, const char* resource_path)
  41. {
  42. File* file = fs.open(resource_path, FOM_READ);
  43. char file_buf[4096];
  44. file->read(file_buf, file->size());
  45. fs.close(file);
  46. JSONParser json(file_buf);
  47. JSONElement root = json.root();
  48. // Check for renderable
  49. if (root.has_key("renderable"))
  50. {
  51. JSONElement renderable_array = root.key("renderable");
  52. uint32_t renderable_array_size = renderable_array.size();
  53. for (uint32_t i = 0; i < renderable_array_size; i++)
  54. {
  55. DynamicString mesh_resource(default_allocator());
  56. mesh_resource += renderable_array[i].key("resource").string_value();
  57. mesh_resource += ".mesh";
  58. DynamicString mesh_name(default_allocator());
  59. mesh_name = renderable_array[i].key("name").string_value();
  60. UnitRenderable ur;
  61. ur.resource.id = hash::murmur2_64(mesh_resource.c_str(), string::strlen(mesh_resource.c_str()), 0);
  62. ur.name = hash::murmur2_32(mesh_name.c_str(), string::strlen(mesh_name.c_str()), 0);
  63. ur.visible = renderable_array[i].key("visible").bool_value();
  64. m_renderable.push_back(ur);
  65. }
  66. }
  67. // Check for cameras
  68. if (root.has_key("camera"))
  69. {
  70. JSONElement camera = root.key("camera");
  71. uint32_t num_cameras = camera.size();
  72. for (uint32_t i = 0; i < num_cameras; i++)
  73. {
  74. JSONElement camera_name = camera[i].key("name");
  75. UnitCamera uc;
  76. uc.name = hash::murmur2_32(camera_name.string_value(), camera_name.size(), 0);
  77. m_camera.push_back(uc);
  78. }
  79. }
  80. return sizeof(UnitHeader) +
  81. m_renderable.size() * sizeof(UnitRenderable) +
  82. m_camera.size() * sizeof(UnitCamera);
  83. }
  84. //-----------------------------------------------------------------------------
  85. void UnitCompiler::write_impl(File* out_file)
  86. {
  87. UnitHeader header;
  88. header.num_renderables = m_renderable.size();
  89. header.num_cameras = m_camera.size();
  90. header.renderables_offset = sizeof(UnitHeader);
  91. header.cameras_offset = sizeof(UnitHeader) + sizeof(UnitRenderable) * header.num_renderables;
  92. out_file->write((char*) &header, sizeof(UnitHeader));
  93. if (m_renderable.size() > 0)
  94. {
  95. out_file->write((char*) m_renderable.begin(), sizeof(UnitRenderable) * header.num_renderables);
  96. }
  97. if (m_camera.size() > 0)
  98. {
  99. out_file->write((char*) m_camera.begin(), sizeof(UnitCamera) * header.num_cameras);
  100. }
  101. // Cleanup
  102. m_renderable.clear();
  103. m_camera.clear();
  104. }
  105. } // namespace crown