| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- // Global data list
- let data_cached_scene_raws: map_t<string, scene_t> = map_create();
- let data_cached_meshes: map_t<string, mesh_data_t> = map_create();
- let data_cached_cameras: map_t<string, camera_data_t> = map_create();
- let data_cached_materials: map_t<string, material_data_t> = map_create();
- let data_cached_worlds: map_t<string, world_data_t> = map_create();
- let data_cached_shaders: map_t<string, shader_data_t> = map_create();
- let data_cached_blobs: map_t<string, buffer_t> = map_create();
- let data_cached_images: map_t<string, gpu_texture_t> = map_create();
- let data_cached_videos: map_t<string, video_t> = map_create();
- let data_cached_fonts: map_t<string, draw_font_t> = map_create();
- /// if arm_audio
- let data_cached_sounds: map_t<string, sound_t> = map_create();
- /// end
- let data_assets_loaded: i32 = 0;
- function data_get_mesh(file: string, name: string): mesh_data_t {
- let handle: string = file + name;
- let cached: mesh_data_t = map_get(data_cached_meshes, handle);
- if (cached != null) {
- return cached;
- }
- let b: mesh_data_t = mesh_data_parse(file, name);
- map_set(data_cached_meshes, handle, b);
- b._.handle = handle;
- return b;
- }
- function data_get_camera(file: string, name: string): camera_data_t {
- let handle: string = file + name;
- let cached: camera_data_t = map_get(data_cached_cameras, handle);
- if (cached != null) {
- return cached;
- }
- let b: camera_data_t = camera_data_parse(file, name);
- map_set(data_cached_cameras, handle, b);
- return b;
- }
- function data_get_material(file: string, name: string): material_data_t {
- let handle: string = file + name;
- let cached: material_data_t = map_get(data_cached_materials, handle);
- if (cached != null) {
- return cached;
- }
- let b: material_data_t = material_data_parse(file, name);
- map_set(data_cached_materials, handle, b);
- return b;
- }
- function data_get_world(file: string, name: string): world_data_t {
- if (name == null) { // No world defined in scene
- return null;
- }
- let handle: string = file + name;
- let cached: world_data_t = map_get(data_cached_worlds, handle);
- if (cached != null) {
- return cached;
- }
- let b: world_data_t = world_data_parse(file, name);
- map_set(data_cached_worlds, handle, b);
- return b;
- }
- function data_get_shader(file: string, name: string): shader_data_t {
- // Only one context override per shader data for now
- let handle: string = name;
- let cached: shader_data_t = map_get(data_cached_shaders, handle); // Shader must have unique name
- if (cached != null) {
- return cached;
- }
- let b: shader_data_t = shader_data_parse(file, name);
- map_set(data_cached_shaders, handle, b);
- return b;
- }
- function data_get_scene_raw(file: string): scene_t {
- let cached: scene_t = map_get(data_cached_scene_raws, file);
- if (cached != null) {
- return cached;
- }
- // If no extension specified, set to .arm
- let ext: string = ends_with(file, ".arm") ? "" : ".arm";
- let b: buffer_t = data_get_blob(file + ext);
- let parsed: scene_t = armpack_decode(b);
- map_set(data_cached_scene_raws, file, parsed);
- return parsed;
- }
- // Raw assets
- function data_get_blob(file: string): buffer_t {
- let cached: buffer_t = map_get(data_cached_blobs, file);
- if (cached != null) {
- return cached;
- }
- let b: buffer_t = iron_load_blob(data_resolve_path(file));
- map_set(data_cached_blobs, file, b);
- data_assets_loaded++;
- return b;
- }
- function data_get_image(file: string): gpu_texture_t {
- let cached: gpu_texture_t = map_get(data_cached_images, file);
- if (cached != null) {
- return cached;
- }
- let image_: any = iron_load_texture(data_resolve_path(file));
- if (image_ == null) {
- return null;
- }
- let b: gpu_texture_t = image_;
- map_set(data_cached_images, file, b);
- data_assets_loaded++;
- return b;
- }
- function data_get_video(file: string): video_t {
- file = substring(file, 0, file.length - 4) + ".webm";
- let cached: video_t = map_get(data_cached_videos, file);
- if (cached != null) {
- return cached;
- }
- // let b: video_t = iron_load_video(data_resolve_path(file));
- // map_set(data_cached_videos, file, b);
- // data_assets_loaded++;
- // return b;
- return null;
- }
- function data_get_font(file: string): draw_font_t {
- let cached: draw_font_t = map_get(data_cached_fonts, file);
- if (cached != null) {
- return cached;
- }
- let blob: buffer_t = iron_load_blob(data_resolve_path(file));
- let b: draw_font_t = {buf : blob, index : 0};
- map_set(data_cached_fonts, file, b);
- data_assets_loaded++;
- return b;
- }
- /// if arm_audio
- function data_get_sound(file: string): sound_t {
- let cached: sound_t = map_get(data_cached_sounds, file);
- if (cached != null) {
- return cached;
- }
- let b: sound_t = sound_create(iron_load_sound(data_resolve_path(file)));
- map_set(data_cached_sounds, file, b);
- data_assets_loaded++;
- return b;
- }
- /// end
- function data_delete_mesh(handle: string) {
- let mesh: mesh_data_t = map_get(data_cached_meshes, handle);
- if (mesh == null) {
- return;
- }
- mesh_data_delete(mesh);
- map_delete(data_cached_meshes, handle);
- }
- function data_delete_blob(handle: string) {
- let blob: buffer_t = map_get(data_cached_blobs, handle);
- if (blob == null) {
- return;
- }
- map_delete(data_cached_blobs, handle);
- }
- function data_delete_image(handle: string) {
- let image: gpu_texture_t = map_get(data_cached_images, handle);
- if (image == null) {
- return;
- }
- gpu_delete_texture(image);
- map_delete(data_cached_images, handle);
- }
- function data_delete_video(handle: string) {
- let video: video_t = map_get(data_cached_videos, handle);
- if (video == null) {
- return;
- }
- video_unload(video);
- map_delete(data_cached_videos, handle);
- }
- function data_delete_font(handle: string) {
- let font: draw_font_t = map_get(data_cached_fonts, handle);
- if (font == null) {
- return;
- }
- draw_font_destroy(font);
- map_delete(data_cached_fonts, handle);
- }
- /// if arm_audio
- function data_delete_sound(handle: string) {
- let sound: sound_t = map_get(data_cached_sounds, handle);
- if (sound == null) {
- return;
- }
- sound_unload(sound);
- map_delete(data_cached_sounds, handle);
- }
- /// end
- function data_delete_all() {
- let cached_meshes_keys: string[] = map_keys(data_cached_meshes);
- for (let i: i32 = 0; i < cached_meshes_keys.length; ++i) {
- let c: mesh_data_t = map_get(data_cached_meshes, cached_meshes_keys[i]);
- mesh_data_delete(c);
- }
- data_cached_meshes = map_create();
- let cached_shaders_keys: string[] = map_keys(data_cached_shaders);
- for (let i: i32 = 0; i < cached_shaders_keys.length; ++i) {
- let c: shader_data_t = map_get(data_cached_shaders, cached_shaders_keys[i]);
- shader_data_delete(c);
- }
- data_cached_shaders = map_create();
- data_cached_scene_raws = map_create();
- data_cached_cameras = map_create();
- data_cached_materials = map_create();
- data_cached_worlds = map_create();
- render_path_unload();
- data_cached_blobs = map_create();
- let cached_images_keys: string[] = map_keys(data_cached_images);
- for (let i: i32 = 0; i < cached_images_keys.length; ++i) {
- let c: gpu_texture_t = map_get(data_cached_images, cached_images_keys[i]);
- gpu_delete_texture(c);
- }
- data_cached_images = map_create();
- /// if arm_audio
- let cached_sounds_keys: string[] = map_keys(data_cached_sounds);
- for (let i: i32 = 0; i < cached_sounds_keys.length; ++i) {
- let c: sound_t = map_get(data_cached_sounds, cached_sounds_keys[i]);
- sound_unload(c);
- }
- data_cached_sounds = map_create();
- /// end
- let cached_videos_keys: string[] = map_keys(data_cached_videos);
- for (let i: i32 = 0; i < cached_videos_keys.length; ++i) {
- let c: video_t = map_get(data_cached_videos, cached_videos_keys[i]);
- video_unload(c);
- }
- data_cached_videos = map_create();
- let cached_fonts_keys: string[] = map_keys(data_cached_fonts);
- for (let i: i32 = 0; i < cached_fonts_keys.length; ++i) {
- let c: draw_font_t = map_get(data_cached_fonts, cached_fonts_keys[i]);
- draw_font_destroy(c);
- }
- data_cached_fonts = map_create();
- }
- function data_sep(): string {
- /// if arm_windows
- return "\\";
- /// else
- return "/";
- /// end
- }
- function data_path(): string {
- /// if arm_android
- return "data" + data_sep();
- /// else
- return "." + data_sep() + "data" + data_sep();
- /// end
- }
- function data_is_abs(file: string): bool {
- return char_at(file, 0) == "/" || char_at(file, 1) == ":" || (char_at(file, 0) == "\\" && char_at(file, 1) == "\\");
- }
- function data_is_up(file: string): bool {
- return char_at(file, 0) == "." && char_at(file, 1) == ".";
- }
- function data_base_name(path: string): string {
- let slash: i32 = string_last_index_of(path, data_sep());
- return slash >= 0 ? substring(path, slash + 1, path.length) : path;
- }
- function data_resolve_path(file: string): string {
- if (data_is_abs(file) || data_is_up(file)) {
- return file;
- }
- return data_path() + file;
- }
|