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