SpriteResource.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <cstring>
  24. #include <inttypes.h>
  25. #include "Allocator.h"
  26. #include "Filesystem.h"
  27. #include "Hash.h"
  28. #include "JSONParser.h"
  29. #include "SpriteResource.h"
  30. #include "StringUtils.h"
  31. namespace crown
  32. {
  33. namespace sprite_resource
  34. {
  35. //-----------------------------------------------------------------------------
  36. struct FrameData
  37. {
  38. float x0, y0;
  39. float x1, y1;
  40. float scale_x, scale_y;
  41. float offset_x, offset_y;
  42. };
  43. //-----------------------------------------------------------------------------
  44. void parse_frame(JSONElement frame, List<StringId32>& names, List<FrameData>& regions)
  45. {
  46. JSONElement name = frame.key("name");
  47. JSONElement region = frame.key("region");
  48. JSONElement offset = frame.key("offset");
  49. JSONElement scale = frame.key("scale");
  50. StringId32 name_hash = hash::murmur2_32(name.string_value(), name.size(), 0);
  51. FrameData fd;
  52. fd.x0 = region[0].float_value();
  53. fd.y0 = region[1].float_value();
  54. fd.x1 = region[2].float_value();
  55. fd.y1 = region[3].float_value();
  56. fd.offset_x = offset[0].float_value();
  57. fd.offset_y = offset[1].float_value();
  58. fd.scale_x = scale[0].float_value();
  59. fd.scale_y = scale[1].float_value();
  60. names.push_back(name_hash);
  61. regions.push_back(fd);
  62. }
  63. //-----------------------------------------------------------------------------
  64. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  65. {
  66. File* file = fs.open(resource_path, FOM_READ);
  67. char* buf = (char*)default_allocator().allocate(file->size());
  68. file->read(buf, file->size());
  69. JSONParser json(buf);
  70. JSONElement root = json.root();
  71. List<StringId32> m_names(default_allocator());
  72. List<FrameData> m_regions(default_allocator());
  73. List<float> m_vertices(default_allocator());
  74. // Read frames
  75. JSONElement frames = root.key("frames");
  76. uint32_t num_frames = frames.size();
  77. for (uint32_t i = 0; i < num_frames; i++)
  78. {
  79. parse_frame(frames[i], m_names, m_regions);
  80. }
  81. for (uint32_t i = 0; i < num_frames; i++)
  82. {
  83. const FrameData& fd = m_regions[i];
  84. // Compute uv coords
  85. float u0 = fd.x0;
  86. float v0 = fd.y0;
  87. float u1 = fd.x0 + fd.x1;
  88. float v1 = fd.y0 + fd.y1;
  89. // Compute positions
  90. float w = fd.x1;
  91. float h = fd.y1;
  92. float x0 = fd.scale_x * (-w * 0.5) + fd.offset_x;
  93. float y0 = fd.scale_y * (-h * 0.5) + fd.offset_y;
  94. float x1 = fd.scale_x * ( w * 0.5) + fd.offset_x;
  95. float y1 = fd.scale_y * ( h * 0.5) + fd.offset_y;
  96. m_vertices.push_back(x0); m_vertices.push_back(y0); // position
  97. m_vertices.push_back(u0); m_vertices.push_back(v0); // uv
  98. m_vertices.push_back(x1); m_vertices.push_back(y0); // position
  99. m_vertices.push_back(u1); m_vertices.push_back(v0); // uv
  100. m_vertices.push_back(x1); m_vertices.push_back(y1); // position
  101. m_vertices.push_back(u1); m_vertices.push_back(v1); // uv
  102. m_vertices.push_back(x0); m_vertices.push_back(y1); // position
  103. m_vertices.push_back(u0); m_vertices.push_back(v1); // uv
  104. }
  105. fs.close(file);
  106. default_allocator().deallocate(buf);
  107. SpriteHeader h;
  108. h.texture.id = hash::murmur2_64("textures/circle.texture", string::strlen("textures/circle.texture"), 0);
  109. h.num_frames = m_names.size();
  110. uint32_t offt = sizeof(SpriteHeader);
  111. h.frame_names_offset = offt; offt += sizeof(StringId32) * h.num_frames;
  112. h.frame_vertices_offset = offt; // offt += sizeof(float) * 16 * h.num_frames; <- not necessary, just for future reference
  113. out_file->write((char*) &h, sizeof(SpriteHeader));
  114. out_file->write((char*) m_names.begin(), sizeof(StringId32) * m_names.size());
  115. out_file->write((char*) m_vertices.begin(), sizeof(float) * 16 * m_vertices.size());
  116. }
  117. } // namespace sprite_resource
  118. } // namespace crown