summaryrefslogtreecommitdiffstats
path: root/core/resource
diff options
context:
space:
mode:
authoruntodesu <kirill@untode.su>2025-07-01 03:08:39 +0500
committeruntodesu <kirill@untode.su>2025-07-01 03:08:39 +0500
commit458e0005690ea9d579588a0a12368fc2c2c9a93a (patch)
tree588a9ca6cb3c76d9193b5bd4601d64f0e50e8c8c /core/resource
parentc7b0c8e0286a1b2bb7ec55e579137dfc3b22eeb9 (diff)
downloadvoxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.tar.bz2
voxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.zip
I hyper-focued on refactoring again
- I put a cool-sounding "we are number one" remix on repeat and straight up grinded the entire repository to a better state until 03:09 AM. I guess I have something wrong in my brain that makes me do this shit
Diffstat (limited to 'core/resource')
-rw-r--r--core/resource/CMakeLists.txt6
-rw-r--r--core/resource/binfile.cc70
-rw-r--r--core/resource/binfile.hh10
-rw-r--r--core/resource/image.cc112
-rw-r--r--core/resource/image.hh13
-rw-r--r--core/resource/resource.hh18
6 files changed, 229 insertions, 0 deletions
diff --git a/core/resource/CMakeLists.txt b/core/resource/CMakeLists.txt
new file mode 100644
index 0000000..ef2ffae
--- /dev/null
+++ b/core/resource/CMakeLists.txt
@@ -0,0 +1,6 @@
+target_sources(core PRIVATE
+ "${CMAKE_CURRENT_LIST_DIR}/binfile.cc"
+ "${CMAKE_CURRENT_LIST_DIR}/binfile.hh"
+ "${CMAKE_CURRENT_LIST_DIR}/image.cc"
+ "${CMAKE_CURRENT_LIST_DIR}/image.hh"
+ "${CMAKE_CURRENT_LIST_DIR}/resource.hh")
diff --git a/core/resource/binfile.cc b/core/resource/binfile.cc
new file mode 100644
index 0000000..e17db50
--- /dev/null
+++ b/core/resource/binfile.cc
@@ -0,0 +1,70 @@
+#include "core/pch.hh"
+
+#include "core/resource/binfile.hh"
+
+#include "core/resource/resource.hh"
+
+static emhash8::HashMap<std::string, resource_ptr<BinFile>> resource_map;
+
+template<>
+resource_ptr<BinFile> resource::load<BinFile>(const char* name, unsigned int flags)
+{
+ auto it = resource_map.find(name);
+
+ if(it != resource_map.cend()) {
+ // Return an existing resource
+ return it->second;
+ }
+
+ auto file = PHYSFS_openRead(name);
+
+ if(file == nullptr) {
+ spdlog::warn("resource: {}: {}", name, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
+ return nullptr;
+ }
+
+ auto new_resource = std::make_shared<BinFile>();
+ new_resource->size = PHYSFS_fileLength(file);
+ new_resource->buffer = new std::byte[new_resource->size];
+
+ PHYSFS_readBytes(file, new_resource->buffer, new_resource->size);
+ PHYSFS_close(file);
+
+ return resource_map.insert_or_assign(name, new_resource).first->second;
+}
+
+template<>
+void resource::hard_cleanup<BinFile>(void)
+{
+ for(const auto& it : resource_map) {
+ if(it.second.use_count() > 1L) {
+ spdlog::warn("resource: zombie resource [BinFile] {} [use_count={}]", it.first, it.second.use_count());
+ } else {
+ spdlog::debug("resource: releasing [BinFile] {}", it.first);
+ }
+
+ delete[] it.second->buffer;
+ }
+
+ resource_map.clear();
+}
+
+template<>
+void resource::soft_cleanup<BinFile>(void)
+{
+ auto iter = resource_map.cbegin();
+
+ while(iter != resource_map.cend()) {
+ if(iter->second.use_count() == 1L) {
+ spdlog::debug("resource: releasing [BinFile] {}", iter->first);
+
+ delete[] iter->second->buffer;
+
+ iter = resource_map.erase(iter);
+
+ continue;
+ }
+
+ iter = std::next(iter);
+ }
+}
diff --git a/core/resource/binfile.hh b/core/resource/binfile.hh
new file mode 100644
index 0000000..6bb5e3d
--- /dev/null
+++ b/core/resource/binfile.hh
@@ -0,0 +1,10 @@
+#ifndef CORE_RESOURCE_BINFILE_HH
+#define CORE_RESOURCE_BINFILE_HH 1
+#pragma once
+
+struct BinFile final {
+ std::byte* buffer;
+ std::size_t size;
+};
+
+#endif // CORE_RESOURCE_BINFILE_HH
diff --git a/core/resource/image.cc b/core/resource/image.cc
new file mode 100644
index 0000000..1601703
--- /dev/null
+++ b/core/resource/image.cc
@@ -0,0 +1,112 @@
+#include "core/pch.hh"
+
+#include "core/resource/image.hh"
+
+#include "core/resource/resource.hh"
+
+static emhash8::HashMap<std::string, resource_ptr<Image>> resource_map;
+
+static int stbi_physfs_read(void* context, char* data, int size)
+{
+ return PHYSFS_readBytes(reinterpret_cast<PHYSFS_File*>(context), data, size);
+}
+
+static void stbi_physfs_skip(void* context, int count)
+{
+ auto file = reinterpret_cast<PHYSFS_File*>(context);
+ PHYSFS_seek(file, PHYSFS_tell(file) + count);
+}
+
+static int stbi_physfs_eof(void* context)
+{
+ return PHYSFS_eof(reinterpret_cast<PHYSFS_File*>(context));
+}
+
+template<>
+resource_ptr<Image> resource::load<Image>(const char* name, unsigned int flags)
+{
+ auto it = resource_map.find(name);
+
+ if(it != resource_map.cend()) {
+ // Return an existing resource
+ return it->second;
+ }
+
+ auto file = PHYSFS_openRead(name);
+
+ if(file == nullptr) {
+ spdlog::warn("resource: {}: {}", name, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
+ 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>();
+
+ 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);
+ } else {
+ new_resource->pixels = stbi_load_from_callbacks(
+ &callbacks, file, &new_resource->size.x, &new_resource->size.y, nullptr, STBI_rgb_alpha);
+ }
+
+ PHYSFS_close(file);
+
+ if(new_resource->pixels == nullptr) {
+ spdlog::warn("resource: {}: {}", name, stbi_failure_reason());
+ return nullptr;
+ }
+
+ if(new_resource->size.x <= 0 || new_resource->size.y <= 0) {
+ spdlog::warn("resource {}: invalid dimensions", name);
+ stbi_image_free(new_resource->pixels);
+ return nullptr;
+ }
+
+ return resource_map.insert_or_assign(name, new_resource).first->second;
+}
+
+template<>
+void resource::hard_cleanup<Image>(void)
+{
+ 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);
+ }
+
+ resource_map.clear();
+}
+
+template<>
+void resource::soft_cleanup<Image>(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);
+ }
+}
diff --git a/core/resource/image.hh b/core/resource/image.hh
new file mode 100644
index 0000000..13e912e
--- /dev/null
+++ b/core/resource/image.hh
@@ -0,0 +1,13 @@
+#ifndef CORE_RESOURCE_IMAGE_HH
+#define CORE_RESOURCE_IMAGE_HH 1
+#pragma once
+
+constexpr static unsigned int IMAGE_LOAD_GRAY = 0x0001U;
+constexpr static unsigned int IMAGE_LOAD_FLIP = 0x0002U;
+
+struct Image final {
+ stbi_uc* pixels;
+ glm::ivec2 size;
+};
+
+#endif // CORE_RESOURCE_IMAGE_HH
diff --git a/core/resource/resource.hh b/core/resource/resource.hh
new file mode 100644
index 0000000..4946ffa
--- /dev/null
+++ b/core/resource/resource.hh
@@ -0,0 +1,18 @@
+#ifndef CORE_RESOURCE_RESOURCE_HH
+#define CORE_RESOURCE_RESOURCE_HH 1
+#pragma once
+
+template<typename T>
+using resource_ptr = std::shared_ptr<const T>;
+
+namespace resource
+{
+template<typename T>
+resource_ptr<T> load(const char* name, unsigned int flags = 0U);
+template<typename T>
+void hard_cleanup(void);
+template<typename T>
+void soft_cleanup(void);
+} // namespace resource
+
+#endif // CORE_RESOURCE_RESOURCE_HH