diff options
Diffstat (limited to 'asset.cpp')
-rw-r--r-- | asset.cpp | 46 |
1 files changed, 34 insertions, 12 deletions
@@ -66,7 +66,12 @@ void register_asset_loader( manager.set_loader(magic, loader); } -void Asset_Arena::init(Arena* arena, const char* pack_name) { +void Asset_Arena::init( + Arena* arena, + const char* pack_name, + int max +) { + int i; p = pack_open(pack_name, arena); assert(p != 0); a = arena; @@ -76,27 +81,43 @@ void Asset_Arena::init(Arena* arena, const char* pack_name) { arena_alloc(a, asset_scratch_size), asset_scratch_size ); - assets = 0; + max_assets = max; + assets = (Bucket*)arena_alloc(arena, max * sizeof(Bucket)); + for (i = 0; i < max; i++) + assets[i].name = 0; } void Asset_Arena::destroy() { - Asset* a; - for (a = assets; a; a = a->next) { - a->loader->unload(a); + int i, c = max_assets; + for (i = 0; i < c; i++) { + Bucket& b = assets[i]; + if (b.name) + b.asset->loader->unload(b.asset); } pack_close(p); } -void Asset_Arena::claim(Asset* a) { - if (assets) { - a->next = assets; - assets = a; - } else assets = a; +int Asset_Arena::bucket(const char* name) { + uint32_t hash = hash_string(name); + int bucket = (int)(hash % max_assets); + int i; + for (i = 0; i < max_assets; i++) { + Bucket& b = assets[bucket]; + if (!b.name || string_equal(name, b.name)) + return bucket; + bucket = (bucket + 1) % max_assets; + } + return -1; } Asset* Asset_Arena::load(const char* name) { char magic[4]; - Pack_File* f = pack_open_file(p, name); + Pack_File* f; + int b = bucket(name); + assert(b >= 0); + if (assets[b].name) + return assets[b].asset; + f = pack_open_file(p, name); if (!f) return 0; pack_read(f, magic, 4); pack_seek(f, 0, seek_rel_start); @@ -105,7 +126,8 @@ Asset* Asset_Arena::load(const char* name) { Asset* asset = loader.load(a, s, f); if (asset) { asset->loader = &loader; - claim(asset); + assets[b].name = dup_string(a, name); + assets[b].asset = asset; } pack_close_file(f); return asset; |