diff options
| author | untodesu <kirill@untode.su> | 2025-09-11 14:13:39 +0500 |
|---|---|---|
| committer | untodesu <kirill@untode.su> | 2025-09-11 14:13:39 +0500 |
| commit | 8784cbfebcb8a0220fb947a6070032e20b80fc2f (patch) | |
| tree | 2e03a2c013ed7b19a5dafaba1ddfb05c1878449a /core/resource/image.cc | |
| parent | f0cc06c7388acb32b86301965c5b2547e4e3b919 (diff) | |
| download | voxelius-8784cbfebcb8a0220fb947a6070032e20b80fc2f.tar.bz2 voxelius-8784cbfebcb8a0220fb947a6070032e20b80fc2f.zip | |
Another qfengine graft: resource management
DECOPYPASTA DECOPYPASTA DECOPYPASTA DECOPYPASTA
Diffstat (limited to 'core/resource/image.cc')
| -rw-r--r-- | core/resource/image.cc | 96 |
1 files changed, 31 insertions, 65 deletions
diff --git a/core/resource/image.cc b/core/resource/image.cc index 8b570e4..d5e256d 100644 --- a/core/resource/image.cc +++ b/core/resource/image.cc @@ -4,7 +4,7 @@ #include "core/resource/resource.hh" -static emhash8::HashMap<std::string, resource_ptr<Image>> resource_map; +#include "core/utils/physfs.hh" static int stbi_physfs_read(void* context, char* data, int size) { @@ -22,94 +22,60 @@ static int stbi_physfs_eof(void* context) return PHYSFS_eof(reinterpret_cast<PHYSFS_File*>(context)); } -template<> -resource_ptr<Image> resource::load<Image>(std::string_view name, unsigned int flags) +static const void* image_load_func(const char* name, std::uint32_t flags) { - auto it = resource_map.find(std::string(name)); + assert(name); - if(it != resource_map.cend()) { - // Return an existing resource - return it->second; - } + stbi_io_callbacks callbacks; + callbacks.read = &stbi_physfs_read; + callbacks.skip = &stbi_physfs_skip; + callbacks.eof = &stbi_physfs_eof; + + stbi_set_flip_vertically_on_load(bool(flags & IMAGE_LOAD_FLIP)); - auto file = PHYSFS_openRead(std::string(name).c_str()); + auto file = PHYSFS_openRead(name); if(file == nullptr) { - spdlog::warn("resource: {}: {}", name, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); + spdlog::error("image: {}: {}", name, utils::physfs_error()); return nullptr; } - if(flags & IMAGE_LOAD_FLIP) { - stbi_set_flip_vertically_on_load(true); - } - else { - stbi_set_flip_vertically_on_load(false); - } - - stbi_io_callbacks callbacks; - callbacks.read = &stbi_physfs_read; - callbacks.skip = &stbi_physfs_skip; - callbacks.eof = &stbi_physfs_eof; - - auto new_resource = std::make_shared<Image>(); + int desired_channels; if(flags & IMAGE_LOAD_GRAY) { - new_resource->pixels = stbi_load_from_callbacks(&callbacks, file, &new_resource->size.x, &new_resource->size.y, nullptr, STBI_grey); + desired_channels = STBI_grey; } else { - new_resource->pixels = stbi_load_from_callbacks( - &callbacks, file, &new_resource->size.x, &new_resource->size.y, nullptr, STBI_rgb_alpha); + desired_channels = STBI_rgb_alpha; } - PHYSFS_close(file); + int width, height, channels; + auto pixels = stbi_load_from_callbacks(&callbacks, file, &width, &height, &channels, desired_channels); - if(new_resource->pixels == nullptr) { - spdlog::warn("resource: {}: {}", name, stbi_failure_reason()); - return nullptr; - } + PHYSFS_close(file); - if(new_resource->size.x <= 0 || new_resource->size.y <= 0) { - spdlog::warn("resource {}: invalid dimensions", name); - stbi_image_free(new_resource->pixels); + if(pixels == nullptr) { + spdlog::error("image: {}: {}", name, stbi_failure_reason()); return nullptr; } - return resource_map.insert_or_assign(std::string(name), new_resource).first->second; + auto image = new Image; + image->pixels = pixels; + image->size = glm::ivec2(width, height); + return image; } -template<> -void resource::hard_cleanup<Image>(void) +static void image_free_func(const void* resource) { - for(const auto& it : resource_map) { - if(it.second.use_count() > 1L) { - spdlog::warn("resource: zombie resource [Image] {} [use_count={}]", it.first, it.second.use_count()); - } - else { - spdlog::debug("resource: releasing [Image] {}", it.first); - } - - stbi_image_free(it.second->pixels); - } + assert(resource); + + auto image = reinterpret_cast<const Image*>(resource); + stbi_image_free(image->pixels); - resource_map.clear(); + delete image; } -template<> -void resource::soft_cleanup<Image>(void) +void Image::register_resource(void) { - auto iter = resource_map.cbegin(); - - while(iter != resource_map.cend()) { - if(iter->second.use_count() == 1L) { - spdlog::debug("resource: releasing [Image] {}", iter->first); - - stbi_image_free(iter->second->pixels); - - iter = resource_map.erase(iter); - - continue; - } - - iter = std::next(iter); - } + resource::register_loader<Image>(&image_load_func, &image_free_func); } |
