SpriteResource.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. DynamicString frame_name;
  51. name.string_value(frame_name);
  52. StringId32 name_hash = hash::murmur2_32(frame_name.c_str(), frame_name.length(), 0);
  53. FrameData fd;
  54. fd.x0 = region[0].float_value();
  55. fd.y0 = region[1].float_value();
  56. fd.x1 = region[2].float_value();
  57. fd.y1 = region[3].float_value();
  58. fd.offset_x = offset[0].float_value();
  59. fd.offset_y = offset[1].float_value();
  60. fd.scale_x = scale[0].float_value();
  61. fd.scale_y = scale[1].float_value();
  62. names.push_back(name_hash);
  63. regions.push_back(fd);
  64. }
  65. //-----------------------------------------------------------------------------
  66. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  67. {
  68. File* file = fs.open(resource_path, FOM_READ);
  69. char* buf = (char*)default_allocator().allocate(file->size());
  70. file->read(buf, file->size());
  71. JSONParser json(buf);
  72. JSONElement root = json.root();
  73. float width;
  74. float height;
  75. List<StringId32> m_names(default_allocator());
  76. List<FrameData> m_regions(default_allocator());
  77. List<float> m_vertices(default_allocator());
  78. List<uint16_t> m_indices(default_allocator());
  79. // Read width/height
  80. width = root.key("width").float_value();
  81. height = root.key("height").float_value();
  82. // Read frames
  83. JSONElement frames = root.key("frames");
  84. uint32_t num_frames = frames.size();
  85. for (uint32_t i = 0; i < num_frames; i++)
  86. {
  87. parse_frame(frames[i], m_names, m_regions);
  88. }
  89. uint32_t num_idx = 0;
  90. for (uint32_t i = 0; i < num_frames; i++)
  91. {
  92. const FrameData& fd = m_regions[i];
  93. // Compute uv coords
  94. const float u0 = fd.x0;
  95. const float v0 = fd.y0;
  96. const float u1 = fd.x0 + fd.x1;
  97. const float v1 = fd.y0 + fd.y1;
  98. const float aspect = (fd.x1 * width) / (fd.y1 * height);
  99. // Compute positions
  100. const float w = aspect;
  101. const float h = 1;
  102. const float x0 = fd.scale_x * (-w * 0.5) + fd.offset_x;
  103. const float y0 = fd.scale_y * (-h * 0.5) + fd.offset_y;
  104. const float x1 = fd.scale_x * ( w * 0.5) + fd.offset_x;
  105. const float y1 = fd.scale_y * ( h * 0.5) + fd.offset_y;
  106. m_vertices.push_back(x0); m_vertices.push_back(y0); // position
  107. m_vertices.push_back(u0); m_vertices.push_back(v0); // uv
  108. m_vertices.push_back(x1); m_vertices.push_back(y0); // position
  109. m_vertices.push_back(u1); m_vertices.push_back(v0); // uv
  110. m_vertices.push_back(x1); m_vertices.push_back(y1); // position
  111. m_vertices.push_back(u1); m_vertices.push_back(v1); // uv
  112. m_vertices.push_back(x0); m_vertices.push_back(y1); // position
  113. m_vertices.push_back(u0); m_vertices.push_back(v1); // uv
  114. m_indices.push_back(num_idx); m_indices.push_back(num_idx + 1); m_indices.push_back(num_idx + 2);
  115. m_indices.push_back(num_idx); m_indices.push_back(num_idx + 2); m_indices.push_back(num_idx + 3);
  116. num_idx += 4;
  117. }
  118. fs.close(file);
  119. default_allocator().deallocate(buf);
  120. SpriteHeader h;
  121. h.num_frames = m_names.size();
  122. h.num_vertices = m_vertices.size() / 4; // 4 components per vertex
  123. h.num_indices = m_indices.size();
  124. uint32_t offt = sizeof(SpriteHeader);
  125. /*h.frame_names_offset = offt*/; offt += sizeof(StringId32) * h.num_frames;
  126. h.vertices_offset = offt; offt += sizeof(float) * m_vertices.size();
  127. h.indices_offset = offt; offt += sizeof(uint16_t) * m_indices.size();
  128. out_file->write((char*) &h, sizeof(SpriteHeader));
  129. out_file->write((char*) m_names.begin(), sizeof(StringId32) * m_names.size());
  130. out_file->write((char*) m_vertices.begin(), sizeof(float) * m_vertices.size());
  131. out_file->write((char*) m_indices.begin(), sizeof(uint16_t) * m_indices.size());
  132. }
  133. } // namespace sprite_resource
  134. } // namespace crown