sprite_resource.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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(Filesystem& fs, const char* resource_path, File* out_file)
  57. {
  58. File* file = fs.open(resource_path, FOM_READ);
  59. JSONParser json(*file);
  60. fs.close(file);
  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. BinaryWriter bw(*out_file);
  67. Array<float> vertices(default_allocator());
  68. Array<uint16_t> indices(default_allocator());
  69. uint32_t num_idx = 0;
  70. for (uint32_t i = 0; i < num_frames; i++)
  71. {
  72. JSONElement e(root.key("frames")[i]);
  73. SpriteFrame frame;
  74. parse_frame(e, frame);
  75. const SpriteFrame& fd = frame;
  76. // Compute uv coords
  77. const float u0 = fd.region.x / width;
  78. const float v0 = fd.region.y / height;
  79. const float u1 = (fd.region.x + fd.region.z) / width;
  80. const float v1 = (fd.region.y + fd.region.w) / height;
  81. // Compute positions
  82. const float w = fd.region.z / CE_PIXELS_PER_METER;
  83. const float h = fd.region.w / CE_PIXELS_PER_METER;
  84. const float x0 = fd.scale.x * (-w * 0.5f) + fd.offset.x;
  85. const float y0 = fd.scale.y * (-h * 0.5f) + fd.offset.y;
  86. const float x1 = fd.scale.x * ( w * 0.5f) + fd.offset.x;
  87. const float y1 = fd.scale.y * ( h * 0.5f) + fd.offset.y;
  88. array::push_back(vertices, x0); array::push_back(vertices, y0); // position
  89. array::push_back(vertices, u0); array::push_back(vertices, v0); // uv
  90. array::push_back(vertices, x1); array::push_back(vertices, y0); // position
  91. array::push_back(vertices, u1); array::push_back(vertices, v0); // uv
  92. array::push_back(vertices, x1); array::push_back(vertices, y1); // position
  93. array::push_back(vertices, u1); array::push_back(vertices, v1); // uv
  94. array::push_back(vertices, x0); array::push_back(vertices, y1); // position
  95. array::push_back(vertices, u0); array::push_back(vertices, v1); // uv
  96. 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));
  97. 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));
  98. num_idx += 4;
  99. }
  100. const uint32_t num_vertices = array::size(vertices) / 4; // 4 components per vertex
  101. const uint32_t num_indices = array::size(indices);
  102. // Write header
  103. bw.write(SPRITE_VERSION);
  104. bw.write(num_vertices);
  105. if (array::size(vertices))
  106. bw.write(array::begin(vertices), sizeof(float) * array::size(vertices));
  107. bw.write(num_indices);
  108. if (array::size(indices))
  109. bw.write(array::begin(indices), sizeof(uint16_t) * array::size(indices));
  110. }
  111. void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  112. {
  113. File* file = bundle.open(id);
  114. BinaryReader br(*file);
  115. uint32_t version;
  116. br.read(version);
  117. uint32_t num_verts;
  118. br.read(num_verts);
  119. const bgfx::Memory* vbmem = bgfx::alloc(num_verts * sizeof(float) * 4);
  120. br.read(vbmem->data, num_verts * sizeof(float) * 4);
  121. uint32_t num_inds;
  122. br.read(num_inds);
  123. const bgfx::Memory* ibmem = bgfx::alloc(num_inds * sizeof(uint16_t));
  124. br.read(ibmem->data, num_inds * sizeof(uint16_t));
  125. bundle.close(file);
  126. SpriteResource* so = (SpriteResource*) default_allocator().allocate(sizeof(SpriteResource));
  127. so->vbmem = vbmem;
  128. so->ibmem = ibmem;
  129. return so;
  130. }
  131. void online(StringId64 id, ResourceManager& rm)
  132. {
  133. ResourceId res_id;
  134. res_id.type = SPRITE_TYPE;
  135. res_id.name = id;
  136. SpriteResource* so = (SpriteResource*) rm.get(res_id);
  137. bgfx::VertexDecl decl;
  138. decl.begin()
  139. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  140. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float, false)
  141. .end();
  142. so->vb = bgfx::createVertexBuffer(so->vbmem, decl);
  143. so->ib = bgfx::createIndexBuffer(so->ibmem);
  144. }
  145. void offline(StringId64 id, ResourceManager& rm)
  146. {
  147. ResourceId res_id;
  148. res_id.type = SPRITE_TYPE;
  149. res_id.name = id;
  150. SpriteResource* so = (SpriteResource*) rm.get(res_id);
  151. bgfx::destroyVertexBuffer(so->vb);
  152. bgfx::destroyIndexBuffer(so->ib);
  153. }
  154. void unload(Allocator& a, void* resource)
  155. {
  156. a.deallocate(resource);
  157. }
  158. } // namespace sprite_resource
  159. namespace sprite_animation_resource
  160. {
  161. void parse_animations(JSONElement e, Array<SpriteAnimationName>& names, Array<SpriteAnimationData>& anim_data, Array<uint32_t>& frames)
  162. {
  163. const uint32_t num = e.key("animations").size();
  164. for (uint32_t i = 0; i < num; i++)
  165. {
  166. JSONElement anim(e.key("animations")[i]);
  167. SpriteAnimationName san;
  168. san.id = anim.key("name").to_string_id();
  169. const uint32_t num_frames = anim.key("frames").size();
  170. SpriteAnimationData sad;
  171. sad.num_frames = num_frames;
  172. sad.first_frame = array::size(frames);
  173. sad.time = anim.key("time").to_float();
  174. // Read frames
  175. for (uint32_t ff = 0; ff < num_frames; ff++)
  176. array::push_back(frames, (uint32_t) anim.key("frames")[ff].to_int());
  177. array::push_back(names, san);
  178. array::push_back(anim_data, sad);
  179. }
  180. }
  181. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  182. {
  183. File* file = fs.open(resource_path, FOM_READ);
  184. JSONParser json(*file);
  185. fs.close(file);
  186. Array<SpriteAnimationName> anim_names(default_allocator());
  187. Array<SpriteAnimationData> anim_data(default_allocator());
  188. Array<uint32_t> anim_frames(default_allocator());
  189. parse_animations(json.root(), anim_names, anim_data, anim_frames);
  190. BinaryWriter bw(*out_file);
  191. bw.write(uint32_t(1)); // version
  192. bw.write(uint32_t(array::size(anim_names)));
  193. bw.write(uint32_t(array::size(anim_frames)));
  194. bw.write(uint32_t(
  195. sizeof(SpriteAnimationResource) +
  196. sizeof(SpriteAnimationName) * array::size(anim_names) +
  197. sizeof(SpriteAnimationData) * array::size(anim_data)));
  198. if (array::size(anim_names))
  199. bw.write(array::begin(anim_names), sizeof(SpriteAnimationName) * array::size(anim_names));
  200. if (array::size(anim_data))
  201. bw.write(array::begin(anim_data), sizeof(SpriteAnimationData) * array::size(anim_data));
  202. if (array::size(anim_frames))
  203. bw.write(array::begin(anim_frames), sizeof(uint32_t) * array::size(anim_frames));
  204. }
  205. void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  206. {
  207. File* file = bundle.open(id);
  208. const size_t file_size = file->size();
  209. void* res = allocator.allocate(file_size);
  210. file->read(res, file_size);
  211. bundle.close(file);
  212. return res;
  213. }
  214. void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  215. {
  216. }
  217. void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  218. {
  219. }
  220. void unload(Allocator& a, void* resource)
  221. {
  222. a.deallocate(resource);
  223. }
  224. const SpriteAnimationData* get_animation(const SpriteAnimationResource* sar, StringId32 name)
  225. {
  226. const uint32_t num = sar->num_animations;
  227. const SpriteAnimationName* begin = (SpriteAnimationName*) ((char*) sar + sizeof(*sar));
  228. const SpriteAnimationData* data = (SpriteAnimationData*) ((char*) sar + sizeof(*sar) + sizeof(SpriteAnimationName) * num);
  229. for (uint32_t i = 0; i < num; i++)
  230. {
  231. if (begin[i].id == name)
  232. return &data[i];
  233. }
  234. return NULL;
  235. }
  236. const uint32_t* get_animation_frames(const SpriteAnimationResource* sar)
  237. {
  238. return (uint32_t*) ((char*) sar + sar->frames_offset);
  239. }
  240. } // namespace sprite_animation_resource
  241. } // namespace crown