data.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Global data list
  2. let data_cached_scene_raws: map_t<string, scene_t> = map_create();
  3. let data_cached_meshes: map_t<string, mesh_data_t> = map_create();
  4. let data_cached_cameras: map_t<string, camera_data_t> = map_create();
  5. let data_cached_materials: map_t<string, material_data_t> = map_create();
  6. let data_cached_worlds: map_t<string, world_data_t> = map_create();
  7. let data_cached_shaders: map_t<string, shader_data_t> = map_create();
  8. let data_cached_blobs: map_t<string, buffer_t> = map_create();
  9. let data_cached_images: map_t<string, gpu_texture_t> = map_create();
  10. let data_cached_videos: map_t<string, video_t> = map_create();
  11. let data_cached_fonts: map_t<string, draw_font_t> = map_create();
  12. /// if arm_audio
  13. let data_cached_sounds: map_t<string, sound_t> = map_create();
  14. /// end
  15. let data_assets_loaded: i32 = 0;
  16. function data_get_mesh(file: string, name: string): mesh_data_t {
  17. let handle: string = file + name;
  18. let cached: mesh_data_t = map_get(data_cached_meshes, handle);
  19. if (cached != null) {
  20. return cached;
  21. }
  22. let b: mesh_data_t = mesh_data_parse(file, name);
  23. map_set(data_cached_meshes, handle, b);
  24. b._.handle = handle;
  25. return b;
  26. }
  27. function data_get_camera(file: string, name: string): camera_data_t {
  28. let handle: string = file + name;
  29. let cached: camera_data_t = map_get(data_cached_cameras, handle);
  30. if (cached != null) {
  31. return cached;
  32. }
  33. let b: camera_data_t = camera_data_parse(file, name);
  34. map_set(data_cached_cameras, handle, b);
  35. return b;
  36. }
  37. function data_get_material(file: string, name: string): material_data_t {
  38. let handle: string = file + name;
  39. let cached: material_data_t = map_get(data_cached_materials, handle);
  40. if (cached != null) {
  41. return cached;
  42. }
  43. let b: material_data_t = material_data_parse(file, name);
  44. map_set(data_cached_materials, handle, b);
  45. return b;
  46. }
  47. function data_get_world(file: string, name: string): world_data_t {
  48. if (name == null) { // No world defined in scene
  49. return null;
  50. }
  51. let handle: string = file + name;
  52. let cached: world_data_t = map_get(data_cached_worlds, handle);
  53. if (cached != null) {
  54. return cached;
  55. }
  56. let b: world_data_t = world_data_parse(file, name);
  57. map_set(data_cached_worlds, handle, b);
  58. return b;
  59. }
  60. function data_get_shader(file: string, name: string): shader_data_t {
  61. // Only one context override per shader data for now
  62. let handle: string = name;
  63. let cached: shader_data_t = map_get(data_cached_shaders, handle); // Shader must have unique name
  64. if (cached != null) {
  65. return cached;
  66. }
  67. let b: shader_data_t = shader_data_parse(file, name);
  68. map_set(data_cached_shaders, handle, b);
  69. return b;
  70. }
  71. function data_get_scene_raw(file: string): scene_t {
  72. let cached: scene_t = map_get(data_cached_scene_raws, file);
  73. if (cached != null) {
  74. return cached;
  75. }
  76. // If no extension specified, set to .arm
  77. let ext: string = ends_with(file, ".arm") ? "" : ".arm";
  78. let b: buffer_t = data_get_blob(file + ext);
  79. let parsed: scene_t = armpack_decode(b);
  80. map_set(data_cached_scene_raws, file, parsed);
  81. return parsed;
  82. }
  83. // Raw assets
  84. function data_get_blob(file: string): buffer_t {
  85. let cached: buffer_t = map_get(data_cached_blobs, file);
  86. if (cached != null) {
  87. return cached;
  88. }
  89. let b: buffer_t = iron_load_blob(data_resolve_path(file));
  90. map_set(data_cached_blobs, file, b);
  91. data_assets_loaded++;
  92. return b;
  93. }
  94. function data_get_image(file: string): gpu_texture_t {
  95. let cached: gpu_texture_t = map_get(data_cached_images, file);
  96. if (cached != null) {
  97. return cached;
  98. }
  99. let image_: any = iron_load_texture(data_resolve_path(file));
  100. if (image_ == null) {
  101. return null;
  102. }
  103. let b: gpu_texture_t = image_;
  104. map_set(data_cached_images, file, b);
  105. data_assets_loaded++;
  106. return b;
  107. }
  108. function data_get_video(file: string): video_t {
  109. file = substring(file, 0, file.length - 4) + ".webm";
  110. let cached: video_t = map_get(data_cached_videos, file);
  111. if (cached != null) {
  112. return cached;
  113. }
  114. // let b: video_t = iron_load_video(data_resolve_path(file));
  115. // map_set(data_cached_videos, file, b);
  116. // data_assets_loaded++;
  117. // return b;
  118. return null;
  119. }
  120. function data_get_font(file: string): draw_font_t {
  121. let cached: draw_font_t = map_get(data_cached_fonts, file);
  122. if (cached != null) {
  123. return cached;
  124. }
  125. let blob: buffer_t = iron_load_blob(data_resolve_path(file));
  126. let b: draw_font_t = {buf : blob, index : 0};
  127. map_set(data_cached_fonts, file, b);
  128. data_assets_loaded++;
  129. return b;
  130. }
  131. /// if arm_audio
  132. function data_get_sound(file: string): sound_t {
  133. let cached: sound_t = map_get(data_cached_sounds, file);
  134. if (cached != null) {
  135. return cached;
  136. }
  137. let b: sound_t = sound_create(iron_load_sound(data_resolve_path(file)));
  138. map_set(data_cached_sounds, file, b);
  139. data_assets_loaded++;
  140. return b;
  141. }
  142. /// end
  143. function data_delete_mesh(handle: string) {
  144. let mesh: mesh_data_t = map_get(data_cached_meshes, handle);
  145. if (mesh == null) {
  146. return;
  147. }
  148. mesh_data_delete(mesh);
  149. map_delete(data_cached_meshes, handle);
  150. }
  151. function data_delete_blob(handle: string) {
  152. let blob: buffer_t = map_get(data_cached_blobs, handle);
  153. if (blob == null) {
  154. return;
  155. }
  156. map_delete(data_cached_blobs, handle);
  157. }
  158. function data_delete_image(handle: string) {
  159. let image: gpu_texture_t = map_get(data_cached_images, handle);
  160. if (image == null) {
  161. return;
  162. }
  163. gpu_delete_texture(image);
  164. map_delete(data_cached_images, handle);
  165. }
  166. function data_delete_video(handle: string) {
  167. let video: video_t = map_get(data_cached_videos, handle);
  168. if (video == null) {
  169. return;
  170. }
  171. video_unload(video);
  172. map_delete(data_cached_videos, handle);
  173. }
  174. function data_delete_font(handle: string) {
  175. let font: draw_font_t = map_get(data_cached_fonts, handle);
  176. if (font == null) {
  177. return;
  178. }
  179. draw_font_destroy(font);
  180. map_delete(data_cached_fonts, handle);
  181. }
  182. /// if arm_audio
  183. function data_delete_sound(handle: string) {
  184. let sound: sound_t = map_get(data_cached_sounds, handle);
  185. if (sound == null) {
  186. return;
  187. }
  188. sound_unload(sound);
  189. map_delete(data_cached_sounds, handle);
  190. }
  191. /// end
  192. function data_delete_all() {
  193. let cached_meshes_keys: string[] = map_keys(data_cached_meshes);
  194. for (let i: i32 = 0; i < cached_meshes_keys.length; ++i) {
  195. let c: mesh_data_t = map_get(data_cached_meshes, cached_meshes_keys[i]);
  196. mesh_data_delete(c);
  197. }
  198. data_cached_meshes = map_create();
  199. let cached_shaders_keys: string[] = map_keys(data_cached_shaders);
  200. for (let i: i32 = 0; i < cached_shaders_keys.length; ++i) {
  201. let c: shader_data_t = map_get(data_cached_shaders, cached_shaders_keys[i]);
  202. shader_data_delete(c);
  203. }
  204. data_cached_shaders = map_create();
  205. data_cached_scene_raws = map_create();
  206. data_cached_cameras = map_create();
  207. data_cached_materials = map_create();
  208. data_cached_worlds = map_create();
  209. render_path_unload();
  210. data_cached_blobs = map_create();
  211. let cached_images_keys: string[] = map_keys(data_cached_images);
  212. for (let i: i32 = 0; i < cached_images_keys.length; ++i) {
  213. let c: gpu_texture_t = map_get(data_cached_images, cached_images_keys[i]);
  214. gpu_delete_texture(c);
  215. }
  216. data_cached_images = map_create();
  217. /// if arm_audio
  218. let cached_sounds_keys: string[] = map_keys(data_cached_sounds);
  219. for (let i: i32 = 0; i < cached_sounds_keys.length; ++i) {
  220. let c: sound_t = map_get(data_cached_sounds, cached_sounds_keys[i]);
  221. sound_unload(c);
  222. }
  223. data_cached_sounds = map_create();
  224. /// end
  225. let cached_videos_keys: string[] = map_keys(data_cached_videos);
  226. for (let i: i32 = 0; i < cached_videos_keys.length; ++i) {
  227. let c: video_t = map_get(data_cached_videos, cached_videos_keys[i]);
  228. video_unload(c);
  229. }
  230. data_cached_videos = map_create();
  231. let cached_fonts_keys: string[] = map_keys(data_cached_fonts);
  232. for (let i: i32 = 0; i < cached_fonts_keys.length; ++i) {
  233. let c: draw_font_t = map_get(data_cached_fonts, cached_fonts_keys[i]);
  234. draw_font_destroy(c);
  235. }
  236. data_cached_fonts = map_create();
  237. }
  238. function data_sep(): string {
  239. /// if arm_windows
  240. return "\\";
  241. /// else
  242. return "/";
  243. /// end
  244. }
  245. function data_path(): string {
  246. /// if arm_android
  247. return "data" + data_sep();
  248. /// else
  249. return "." + data_sep() + "data" + data_sep();
  250. /// end
  251. }
  252. function data_is_abs(file: string): bool {
  253. return char_at(file, 0) == "/" || char_at(file, 1) == ":" || (char_at(file, 0) == "\\" && char_at(file, 1) == "\\");
  254. }
  255. function data_is_up(file: string): bool {
  256. return char_at(file, 0) == "." && char_at(file, 1) == ".";
  257. }
  258. function data_base_name(path: string): string {
  259. let slash: i32 = string_last_index_of(path, data_sep());
  260. return slash >= 0 ? substring(path, slash + 1, path.length) : path;
  261. }
  262. function data_resolve_path(file: string): string {
  263. if (data_is_abs(file) || data_is_up(file)) {
  264. return file;
  265. }
  266. return data_path() + file;
  267. }