2
0

level_resource.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "level_resource.h"
  24. #include "array.h"
  25. #include "memory.h"
  26. #include "json_parser.h"
  27. #include "filesystem.h"
  28. namespace crown
  29. {
  30. template <typename STREAM, typename T> inline STREAM& operator&(STREAM& stream, T& t)
  31. {
  32. return t.serialize(stream);
  33. }
  34. template <> inline BinaryWriter& operator&(BinaryWriter& bw, bool& v)
  35. {
  36. bw.write(v);
  37. return bw;
  38. }
  39. template <> inline BinaryReader& operator&(BinaryReader& br, bool& v)
  40. {
  41. br.read(v);
  42. return br;
  43. }
  44. template <> inline BinaryWriter& operator&(BinaryWriter& bw, float& v)
  45. {
  46. bw.write(v);
  47. return bw;
  48. }
  49. template <> inline BinaryReader& operator&(BinaryReader& br, float& v)
  50. {
  51. br.read(v);
  52. return br;
  53. }
  54. template <> inline BinaryWriter& operator&(BinaryWriter& bw, char& v)
  55. {
  56. bw.write(v);
  57. return bw;
  58. }
  59. template <> inline BinaryReader& operator&(BinaryReader& br, char& v)
  60. {
  61. br.read(v);
  62. return br;
  63. }
  64. template <> inline BinaryWriter& operator&(BinaryWriter& bw, int8_t& v)
  65. {
  66. bw.write(v);
  67. return bw;
  68. }
  69. template <> inline BinaryReader& operator&(BinaryReader& br, int8_t& v)
  70. {
  71. br.read(v);
  72. return br;
  73. }
  74. template <> inline BinaryWriter& operator&(BinaryWriter& bw, uint8_t& v)
  75. {
  76. bw.write(v);
  77. return bw;
  78. }
  79. template <> inline BinaryReader& operator&(BinaryReader& br, uint8_t& v)
  80. {
  81. br.read(v);
  82. return br;
  83. }
  84. template <> inline BinaryWriter& operator&(BinaryWriter& bw, uint32_t& v)
  85. {
  86. bw.write(v);
  87. return bw;
  88. }
  89. template <> inline BinaryReader& operator&(BinaryReader& br, uint32_t& v)
  90. {
  91. br.read(v);
  92. return br;
  93. }
  94. template <> inline BinaryWriter& operator&(BinaryWriter& br, uint64_t& v)
  95. {
  96. br.write(v);
  97. return br;
  98. }
  99. template <> inline BinaryReader& operator&(BinaryReader& br, uint64_t& v)
  100. {
  101. br.read(v);
  102. return br;
  103. }
  104. template <> inline BinaryReader& operator&(BinaryReader& br, ResourceId& id)
  105. {
  106. return br & id.type & id.name;
  107. }
  108. template <> inline BinaryWriter& operator&(BinaryWriter& bw, Vector3& v)
  109. {
  110. return bw & v.x & v.y & v.z;
  111. }
  112. template <> inline BinaryReader& operator&(BinaryReader& br, Vector3& v)
  113. {
  114. return br & v.x & v.y & v.z;
  115. }
  116. template <> inline BinaryWriter& operator&(BinaryWriter& bw, Quaternion& q)
  117. {
  118. return bw & q.x & q.y & q.z & q.w;
  119. }
  120. template <> inline BinaryReader& operator&(BinaryReader& br, Quaternion& q)
  121. {
  122. return br & q.x & q.y & q.z & q.w;
  123. }
  124. template <> inline BinaryWriter& operator&(BinaryWriter& bw, LevelResource& data)
  125. {
  126. return bw
  127. & data.version
  128. & data.num_units
  129. & data.units_offset
  130. & data.num_sounds
  131. & data.sounds_offset;
  132. }
  133. template <> inline BinaryWriter& operator&(BinaryWriter& bw, LevelUnit& data)
  134. {
  135. return bw
  136. & data.name
  137. & data.position
  138. & data.rotation
  139. & data._pad;
  140. }
  141. template <> inline BinaryWriter& operator&(BinaryWriter& bw, LevelSound& data)
  142. {
  143. return bw
  144. & data.name
  145. & data.position
  146. & data.volume
  147. & data.range
  148. & data.loop
  149. & data._pad[0]
  150. & data._pad[1]
  151. & data._pad[2];
  152. }
  153. template <typename T> inline BinaryWriter& operator&(BinaryWriter& bw, Array<T> arr)
  154. {
  155. for (uint32_t i = 0; i < array::size(arr); i++)
  156. bw & arr[i];
  157. return bw;
  158. }
  159. namespace level_resource
  160. {
  161. void compile(const char* path, CompileOptions& opts)
  162. {
  163. static const uint32_t VERSION = 1;
  164. Buffer buf = opts.read(path);
  165. JSONParser json(array::begin(buf));
  166. JSONElement root = json.root();
  167. Array<LevelUnit> units(default_allocator());
  168. Array<LevelSound> sounds(default_allocator());
  169. {
  170. JSONElement sounds_arr = root.key("sounds");
  171. const uint32_t size = sounds_arr.size();
  172. for (uint32_t i = 0; i < size; i++)
  173. {
  174. JSONElement e = sounds_arr[i];
  175. LevelSound ls;
  176. ls.name = e.key("name").to_resource_id("sound").name;
  177. ls.position = e.key("position").to_vector3();
  178. ls.volume = e.key("volume").to_float();
  179. ls.range = e.key("range").to_float();
  180. ls.loop = e.key("loop").to_bool();
  181. array::push_back(sounds, ls);
  182. }
  183. }
  184. {
  185. JSONElement units_arr = root.key("units");
  186. const uint32_t size = units_arr.size();
  187. for (uint32_t i = 0; i < size; i++)
  188. {
  189. JSONElement e = units_arr[i];
  190. LevelUnit lu;
  191. lu.name = e.key("name").to_resource_id("unit").name;
  192. lu.position = e.key("position").to_vector3();
  193. lu.rotation = e.key("rotation").to_quaternion();
  194. array::push_back(units, lu);
  195. }
  196. }
  197. LevelResource lr;
  198. lr.version = VERSION;
  199. lr.num_units = array::size(units);
  200. lr.num_sounds = array::size(sounds);
  201. uint32_t offt = sizeof(LevelResource);
  202. lr.units_offset = offt; offt += sizeof(LevelUnit) * lr.num_units;
  203. lr.sounds_offset = offt;
  204. opts._bw & lr
  205. & units
  206. & sounds;
  207. }
  208. void* load(File& file, Allocator& a)
  209. {
  210. const size_t file_size = file.size();
  211. void* res = a.allocate(file_size);
  212. file.read(res, file_size);
  213. return res;
  214. }
  215. void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  216. {
  217. }
  218. void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  219. {
  220. }
  221. void unload(Allocator& allocator, void* resource)
  222. {
  223. allocator.deallocate(resource);
  224. }
  225. uint32_t num_units(const LevelResource* lr)
  226. {
  227. return lr->num_units;
  228. }
  229. const LevelUnit* get_unit(const LevelResource* lr, uint32_t i)
  230. {
  231. CE_ASSERT(i < num_units(lr), "Index out of bounds");
  232. const LevelUnit* begin = (LevelUnit*)((char*)lr + lr->units_offset);
  233. return &begin[i];
  234. }
  235. uint32_t num_sounds(const LevelResource* lr)
  236. {
  237. return lr->num_sounds;
  238. }
  239. const LevelSound* get_sound(const LevelResource* lr, uint32_t i)
  240. {
  241. CE_ASSERT(i < num_sounds(lr), "Index out of bounds");
  242. const LevelSound* begin = (LevelSound*)((char*)lr + lr->sounds_offset);
  243. return &begin[i];
  244. }
  245. } // namespace level_resource
  246. } // namespace crown