sprite_resource.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "filesystem.h"
  25. #include "string_utils.h"
  26. #include "json_parser.h"
  27. #include "sprite_resource.h"
  28. #include "string_utils.h"
  29. #include "array.h"
  30. #include "config.h"
  31. #include "reader_writer.h"
  32. #include "vector2.h"
  33. #include "vector4.h"
  34. #include <cfloat>
  35. #include <cstring>
  36. #include <inttypes.h>
  37. #include "log.h"
  38. namespace crown
  39. {
  40. namespace sprite_resource
  41. {
  42. struct SpriteFrame
  43. {
  44. StringId32 name;
  45. Vector4 region; // [x0, y0, x1, y1]
  46. Vector2 scale; // [Sx, Sy]
  47. Vector2 offset; // [Ox, Oy]
  48. };
  49. void parse_frame(JSONElement e, SpriteFrame& frame)
  50. {
  51. frame.name = e.key("name" ).to_string_id();
  52. frame.region = e.key("region").to_vector4();
  53. frame.offset = e.key("offset").to_vector2();
  54. frame.scale = e.key("scale" ).to_vector2();
  55. }
  56. void compile(const char* path, CompileOptions& opts)
  57. {
  58. static const uint32_t VERSION = 1;
  59. Buffer buf = opts.read(path);
  60. JSONParser json(array::begin(buf));
  61. JSONElement root = json.root();
  62. // Read width/height
  63. const float width = root.key("width" ).to_float();
  64. const float height = root.key("height").to_float();
  65. const uint32_t num_frames = root.key("frames").size();
  66. Array<float> vertices(default_allocator());
  67. Array<uint16_t> indices(default_allocator());
  68. uint32_t num_idx = 0;
  69. for (uint32_t i = 0; i < num_frames; i++)
  70. {
  71. JSONElement e(root.key("frames")[i]);
  72. SpriteFrame frame;
  73. parse_frame(e, frame);
  74. const SpriteFrame& fd = frame;
  75. // Compute uv coords
  76. const float u0 = fd.region.x / width;
  77. const float v0 = fd.region.y / height;
  78. const float u1 = (fd.region.x + fd.region.z) / width;
  79. const float v1 = (fd.region.y + fd.region.w) / height;
  80. // Compute positions
  81. const float w = fd.region.z / CE_PIXELS_PER_METER;
  82. const float h = fd.region.w / CE_PIXELS_PER_METER;
  83. const float x0 = fd.scale.x * (-w * 0.5f) + fd.offset.x;
  84. const float y0 = fd.scale.y * (-h * 0.5f) + fd.offset.y;
  85. const float x1 = fd.scale.x * ( w * 0.5f) + fd.offset.x;
  86. const float y1 = fd.scale.y * ( h * 0.5f) + fd.offset.y;
  87. array::push_back(vertices, x0); array::push_back(vertices, y0); // position
  88. array::push_back(vertices, u0); array::push_back(vertices, v0); // uv
  89. array::push_back(vertices, x1); array::push_back(vertices, y0); // position
  90. array::push_back(vertices, u1); array::push_back(vertices, v0); // uv
  91. array::push_back(vertices, x1); array::push_back(vertices, y1); // position
  92. array::push_back(vertices, u1); array::push_back(vertices, v1); // uv
  93. array::push_back(vertices, x0); array::push_back(vertices, y1); // position
  94. array::push_back(vertices, u0); array::push_back(vertices, v1); // uv
  95. 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));
  96. 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));
  97. num_idx += 4;
  98. }
  99. const uint32_t num_vertices = array::size(vertices) / 4; // 4 components per vertex
  100. const uint32_t num_indices = array::size(indices);
  101. // Write header
  102. opts.write(VERSION);
  103. opts.write(num_vertices);
  104. for (uint32_t i = 0; i < array::size(vertices); i++)
  105. {
  106. opts.write(vertices[i]);
  107. }
  108. opts.write(num_indices);
  109. for (uint32_t i = 0; i < array::size(indices); i++)
  110. {
  111. opts.write(indices[i]);
  112. }
  113. }
  114. void* load(File& file, Allocator& a)
  115. {
  116. BinaryReader br(file);
  117. uint32_t version;
  118. br.read(version);
  119. uint32_t num_verts;
  120. br.read(num_verts);
  121. const bgfx::Memory* vbmem = bgfx::alloc(num_verts * sizeof(float) * 4);
  122. br.read(vbmem->data, num_verts * sizeof(float) * 4);
  123. uint32_t num_inds;
  124. br.read(num_inds);
  125. const bgfx::Memory* ibmem = bgfx::alloc(num_inds * sizeof(uint16_t));
  126. br.read(ibmem->data, num_inds * sizeof(uint16_t));
  127. SpriteResource* so = (SpriteResource*) a.allocate(sizeof(SpriteResource));
  128. so->vbmem = vbmem;
  129. so->ibmem = ibmem;
  130. return so;
  131. }
  132. void online(StringId64 id, ResourceManager& rm)
  133. {
  134. SpriteResource* so = (SpriteResource*) rm.get(SPRITE_TYPE, id);
  135. bgfx::VertexDecl decl;
  136. decl.begin()
  137. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  138. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float, false)
  139. .end();
  140. so->vb = bgfx::createVertexBuffer(so->vbmem, decl);
  141. so->ib = bgfx::createIndexBuffer(so->ibmem);
  142. }
  143. void offline(StringId64 id, ResourceManager& rm)
  144. {
  145. SpriteResource* so = (SpriteResource*) rm.get(SPRITE_TYPE, id);
  146. bgfx::destroyVertexBuffer(so->vb);
  147. bgfx::destroyIndexBuffer(so->ib);
  148. }
  149. void unload(Allocator& a, void* resource)
  150. {
  151. a.deallocate(resource);
  152. }
  153. } // namespace sprite_resource
  154. namespace sprite_animation_resource
  155. {
  156. void parse_animations(JSONElement e, Array<SpriteAnimationName>& names, Array<SpriteAnimationData>& anim_data, Array<uint32_t>& frames)
  157. {
  158. const uint32_t num = e.key("animations").size();
  159. for (uint32_t i = 0; i < num; i++)
  160. {
  161. JSONElement anim(e.key("animations")[i]);
  162. SpriteAnimationName san;
  163. san.id = anim.key("name").to_string_id();
  164. const uint32_t num_frames = anim.key("frames").size();
  165. SpriteAnimationData sad;
  166. sad.num_frames = num_frames;
  167. sad.first_frame = array::size(frames);
  168. sad.time = anim.key("time").to_float();
  169. // Read frames
  170. for (uint32_t ff = 0; ff < num_frames; ff++)
  171. array::push_back(frames, (uint32_t) anim.key("frames")[ff].to_int());
  172. array::push_back(names, san);
  173. array::push_back(anim_data, sad);
  174. }
  175. }
  176. void compile(const char* path, CompileOptions& opts)
  177. {
  178. static const uint32_t VERSION = 1;
  179. Buffer buf = opts.read(path);
  180. JSONParser json(array::begin(buf));
  181. JSONElement root = json.root();
  182. Array<SpriteAnimationName> anim_names(default_allocator());
  183. Array<SpriteAnimationData> anim_data(default_allocator());
  184. Array<uint32_t> anim_frames(default_allocator());
  185. parse_animations(root, anim_names, anim_data, anim_frames);
  186. SpriteAnimationResource sar;
  187. sar.version = VERSION;
  188. sar.num_animations = array::size(anim_names);
  189. sar.num_frames = array::size(anim_frames);
  190. sar.frames_offset = uint32_t(sizeof(SpriteAnimationResource) +
  191. sizeof(SpriteAnimationName) * array::size(anim_names) +
  192. sizeof(SpriteAnimationData) * array::size(anim_data));
  193. opts.write(sar.version);
  194. opts.write(sar.num_animations);
  195. opts.write(sar.num_frames);
  196. opts.write(sar.frames_offset);
  197. for (uint32_t i = 0; i < array::size(anim_names); i++)
  198. {
  199. opts.write(anim_names[i].id);
  200. }
  201. for (uint32_t i = 0; i < array::size(anim_data); i++)
  202. {
  203. opts.write(anim_data[i].num_frames);
  204. opts.write(anim_data[i].first_frame);
  205. opts.write(anim_data[i].time);
  206. }
  207. for (uint32_t i = 0; i < array::size(anim_frames); i++)
  208. {
  209. opts.write(anim_frames[i]);
  210. }
  211. }
  212. void* load(File& file, Allocator& a)
  213. {
  214. const size_t file_size = file.size();
  215. void* res = a.allocate(file_size);
  216. file.read(res, file_size);
  217. return res;
  218. }
  219. void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  220. {
  221. }
  222. void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  223. {
  224. }
  225. void unload(Allocator& a, void* resource)
  226. {
  227. a.deallocate(resource);
  228. }
  229. const SpriteAnimationData* get_animation(const SpriteAnimationResource* sar, StringId32 name)
  230. {
  231. const uint32_t num = sar->num_animations;
  232. const SpriteAnimationName* begin = (SpriteAnimationName*) ((char*) sar + sizeof(*sar));
  233. const SpriteAnimationData* data = (SpriteAnimationData*) ((char*) sar + sizeof(*sar) + sizeof(SpriteAnimationName) * num);
  234. for (uint32_t i = 0; i < num; i++)
  235. {
  236. if (begin[i].id == name)
  237. return &data[i];
  238. }
  239. return NULL;
  240. }
  241. const uint32_t* get_animation_frames(const SpriteAnimationResource* sar)
  242. {
  243. return (uint32_t*) ((char*) sar + sar->frames_offset);
  244. }
  245. } // namespace sprite_animation_resource
  246. } // namespace crown