SpriteResource.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "ReaderWriter.h"
  34. #include <cfloat>
  35. namespace crown
  36. {
  37. namespace sprite_resource
  38. {
  39. struct SpriteFrame
  40. {
  41. StringId32 name;
  42. Vector4 region; // [x0, y0, x1, y1]
  43. Vector2 scale; // [Sx, Sy]
  44. Vector2 offset; // [Ox, Oy]
  45. };
  46. //-----------------------------------------------------------------------------
  47. void parse_frame(JSONElement e, SpriteFrame& frame)
  48. {
  49. frame.name = e.key("name" ).to_string_id();
  50. frame.region = e.key("region").to_vector4();
  51. frame.offset = e.key("offset").to_vector2();
  52. frame.scale = e.key("scale" ).to_vector2();
  53. }
  54. //-----------------------------------------------------------------------------
  55. void parse_animation(JSONElement e, SpriteAnimation& anim)
  56. {
  57. anim.name = e.key("name").to_string_id();
  58. anim.time = e.key("time").to_float();
  59. anim.num_frames = 0;
  60. anim.start_frame = 0;
  61. }
  62. //-----------------------------------------------------------------------------
  63. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  64. {
  65. File* file = fs.open(resource_path, FOM_READ);
  66. JSONParser json(*file);
  67. fs.close(file);
  68. JSONElement root = json.root();
  69. // Read width/height
  70. const float width = root.key("width" ).to_float();
  71. const float height = root.key("height").to_float();
  72. const uint32_t num_frames = root.key("frames").size();
  73. const uint32_t num_animations = root.key("animations").size();
  74. BinaryWriter bw(*out_file);
  75. Array<float> vertices(default_allocator());
  76. Array<uint16_t> indices(default_allocator());
  77. uint32_t num_idx = 0;
  78. for (uint32_t i = 0; i < num_frames; i++)
  79. {
  80. JSONElement e(root.key("frames")[i]);
  81. SpriteFrame frame;
  82. parse_frame(e, frame);
  83. const SpriteFrame& fd = frame;
  84. // Compute uv coords
  85. const float u0 = fd.region.x / width;
  86. const float v0 = fd.region.y / height;
  87. const float u1 = (fd.region.x + fd.region.z) / width;
  88. const float v1 = (fd.region.y + fd.region.w) / height;
  89. // Compute positions
  90. const float w = fd.region.z / CE_PIXELS_PER_METER;
  91. const float h = fd.region.w / CE_PIXELS_PER_METER;
  92. const float x0 = fd.scale.x * (-w * 0.5) + fd.offset.x;
  93. const float y0 = fd.scale.y * (-h * 0.5) + fd.offset.y;
  94. const float x1 = fd.scale.x * ( w * 0.5) + fd.offset.x;
  95. const float y1 = fd.scale.y * ( h * 0.5) + fd.offset.y;
  96. array::push_back(vertices, x0); array::push_back(vertices, y0); // position
  97. array::push_back(vertices, u0); array::push_back(vertices, v0); // uv
  98. array::push_back(vertices, x1); array::push_back(vertices, y0); // position
  99. array::push_back(vertices, u1); array::push_back(vertices, v0); // uv
  100. array::push_back(vertices, x1); array::push_back(vertices, y1); // position
  101. array::push_back(vertices, u1); array::push_back(vertices, v1); // uv
  102. array::push_back(vertices, x0); array::push_back(vertices, y1); // position
  103. array::push_back(vertices, u0); array::push_back(vertices, v1); // uv
  104. array::push_back(indices, uint16_t(num_idx)); array::push_back(indices, uint16_t(num_idx + 1)); array::push_back(indices, uint16_t(num_idx + 2));
  105. array::push_back(indices, uint16_t(num_idx)); array::push_back(indices, uint16_t(num_idx + 2)); array::push_back(indices, uint16_t(num_idx + 3));
  106. num_idx += 4;
  107. }
  108. const uint32_t num_vertices = array::size(vertices) / 4; // 4 components per vertex
  109. const uint32_t num_indices = array::size(indices);
  110. // Write animations
  111. Array<SpriteAnimation> animations(default_allocator());
  112. Array<uint32_t> frames(default_allocator());
  113. for (uint32_t i = 0; i < num_animations; i++)
  114. {
  115. JSONElement e(root.key("animations")[i]);
  116. SpriteAnimation anim;
  117. parse_animation(e, anim);
  118. // Read frames
  119. const uint32_t num_frames = e.key("frames").size();
  120. anim.num_frames = num_frames;
  121. anim.start_frame = array::size(frames); // Relative offset
  122. for (uint32_t ff = 0; ff < num_frames; ff++)
  123. array::push_back(frames, (uint32_t) e.key("frames")[ff].to_int());
  124. array::push_back(animations, anim);
  125. }
  126. // Write header
  127. bw.write(uint32_t(0)); // vb
  128. bw.write(uint32_t(0)); // ib
  129. bw.write(num_animations); uint32_t offt = sizeof(SpriteHeader);
  130. bw.write(offt);
  131. bw.write(num_vertices); offt += sizeof(SpriteAnimation) * array::size(animations) + sizeof(uint32_t) * array::size(frames);
  132. bw.write(offt);
  133. bw.write(num_indices); offt += sizeof(float) * array::size(vertices);
  134. bw.write(offt);
  135. if (array::size(animations))
  136. bw.write(array::begin(animations), sizeof(SpriteAnimation) * array::size(animations));
  137. if (array::size(frames))
  138. bw.write(array::begin(frames), sizeof(uint32_t) * array::size(frames));
  139. if (array::size(vertices))
  140. bw.write(array::begin(vertices), sizeof(float) * array::size(vertices));
  141. if (array::size(indices))
  142. bw.write(array::begin(indices), sizeof(uint16_t) * array::size(indices));
  143. }
  144. } // namespace sprite_resource
  145. } // namespace crown