summaryrefslogtreecommitdiffstats
path: root/game/client/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 /game/client/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 'game/client/resource')
-rw-r--r--game/client/resource/CMakeLists.txt5
-rw-r--r--game/client/resource/sound_effect.cc115
-rw-r--r--game/client/resource/sound_effect.hh10
-rw-r--r--game/client/resource/texture_gui.cc110
-rw-r--r--game/client/resource/texture_gui.hh17
5 files changed, 257 insertions, 0 deletions
diff --git a/game/client/resource/CMakeLists.txt b/game/client/resource/CMakeLists.txt
new file mode 100644
index 0000000..baf2311
--- /dev/null
+++ b/game/client/resource/CMakeLists.txt
@@ -0,0 +1,5 @@
+target_sources(vclient PRIVATE
+ "${CMAKE_CURRENT_LIST_DIR}/sound_effect.cc"
+ "${CMAKE_CURRENT_LIST_DIR}/sound_effect.hh"
+ "${CMAKE_CURRENT_LIST_DIR}/texture_gui.cc"
+ "${CMAKE_CURRENT_LIST_DIR}/texture_gui.hh")
diff --git a/game/client/resource/sound_effect.cc b/game/client/resource/sound_effect.cc
new file mode 100644
index 0000000..e3a0a6a
--- /dev/null
+++ b/game/client/resource/sound_effect.cc
@@ -0,0 +1,115 @@
+#include "client/pch.hh"
+
+#include "client/resource/sound_effect.hh"
+
+#include "core/resource/resource.hh"
+
+#include "client/globals.hh"
+
+static emhash8::HashMap<std::string, resource_ptr<SoundEffect>> resource_map;
+
+static std::size_t drwav_read_physfs(void* file, void* output, std::size_t count)
+{
+ return static_cast<std::size_t>(PHYSFS_readBytes(reinterpret_cast<PHYSFS_File*>(file), output, count));
+}
+
+static drwav_bool32 drwav_seek_physfs(void* file, int offset, drwav_seek_origin origin)
+{
+ if(origin == drwav_seek_origin_current) {
+ return PHYSFS_seek(reinterpret_cast<PHYSFS_File*>(file), PHYSFS_tell(reinterpret_cast<PHYSFS_File*>(file)) + offset);
+ } else {
+ return PHYSFS_seek(reinterpret_cast<PHYSFS_File*>(file), offset);
+ }
+}
+
+template<>
+resource_ptr<SoundEffect> resource::load<SoundEffect>(const char* name, unsigned int flags)
+{
+ auto it = resource_map.find(name);
+
+ if(it != resource_map.cend()) {
+ // Return an existing resource
+ return it->second;
+ }
+
+ if(globals::sound_ctx == nullptr) {
+ // Sound is disabled
+ return nullptr;
+ }
+
+ auto file = PHYSFS_openRead(name);
+
+ if(file == nullptr) {
+ spdlog::warn("resource: {} [SoundEffect]: {}", name, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
+ return nullptr;
+ }
+
+ drwav wav_info;
+
+ if(!drwav_init(&wav_info, &drwav_read_physfs, &drwav_seek_physfs, file, nullptr)) {
+ spdlog::warn("resource: {} [SoundEffect]: drwav_init failed", name);
+ PHYSFS_close(file);
+ return nullptr;
+ }
+
+ if(wav_info.channels != 1) {
+ spdlog::warn("resource: {} [SoundEffect]: only mono sound files are allowed", name);
+ drwav_uninit(&wav_info);
+ PHYSFS_close(file);
+ return nullptr;
+ }
+
+ auto samples = new ALshort[wav_info.totalPCMFrameCount];
+ auto count = drwav_read_pcm_frames_s16(&wav_info, wav_info.totalPCMFrameCount, reinterpret_cast<drwav_int16*>(samples));
+ auto sample_rate = static_cast<ALsizei>(wav_info.sampleRate);
+ auto length = static_cast<ALsizei>(count * sizeof(ALshort));
+
+ drwav_uninit(&wav_info);
+ PHYSFS_close(file);
+
+ auto new_resource = std::make_shared<SoundEffect>();
+ new_resource->name = std::string(name);
+
+ alGenBuffers(1, &new_resource->buffer);
+ alBufferData(new_resource->buffer, AL_FORMAT_MONO16, samples, length, sample_rate);
+
+ delete[] samples;
+
+ return resource_map.insert_or_assign(name, new_resource).first->second;
+}
+
+template<>
+void resource::hard_cleanup<SoundEffect>(void)
+{
+ for(const auto& it : resource_map) {
+ if(it.second.use_count() > 1L) {
+ spdlog::warn("resource: zombie resource [SoundEffect] {} [use_count={}]", it.first, it.second.use_count());
+ } else {
+ spdlog::debug("resource: releasing [SoundEffect] {}", it.first);
+ }
+
+ alDeleteBuffers(1, &it.second->buffer);
+ }
+
+ resource_map.clear();
+}
+
+template<>
+void resource::soft_cleanup<SoundEffect>(void)
+{
+ auto iter = resource_map.cbegin();
+
+ while(iter != resource_map.cend()) {
+ if(iter->second.use_count() == 1L) {
+ spdlog::debug("resource: releasing [SoundEffect] {}", iter->first);
+
+ alDeleteBuffers(1, &iter->second->buffer);
+
+ iter = resource_map.erase(iter);
+
+ continue;
+ }
+
+ iter = std::next(iter);
+ }
+}
diff --git a/game/client/resource/sound_effect.hh b/game/client/resource/sound_effect.hh
new file mode 100644
index 0000000..8b1372b
--- /dev/null
+++ b/game/client/resource/sound_effect.hh
@@ -0,0 +1,10 @@
+#ifndef CLIENT_SOUND_EFFECT_HH
+#define CLIENT_SOUND_EFFECT_HH 1
+#pragma once
+
+struct SoundEffect final {
+ std::string name;
+ ALuint buffer;
+};
+
+#endif // CLIENT_SOUND_EFFECT_HH
diff --git a/game/client/resource/texture_gui.cc b/game/client/resource/texture_gui.cc
new file mode 100644
index 0000000..a931352
--- /dev/null
+++ b/game/client/resource/texture_gui.cc
@@ -0,0 +1,110 @@
+#include "client/pch.hh"
+
+#include "client/resource/texture_gui.hh"
+
+#include "core/resource/image.hh"
+#include "core/resource/resource.hh"
+
+static emhash8::HashMap<std::string, resource_ptr<TextureGUI>> resource_map;
+
+template<>
+resource_ptr<TextureGUI> resource::load<TextureGUI>(const char* name, unsigned int flags)
+{
+ auto it = resource_map.find(name);
+
+ if(it != resource_map.cend()) {
+ // Return an existing resource
+ return it->second;
+ }
+
+ unsigned int image_load_flags = 0U;
+
+ if(flags & TEXTURE_GUI_LOAD_VFLIP) {
+ image_load_flags |= IMAGE_LOAD_FLIP;
+ }
+
+ if(flags & TEXTURE_GUI_LOAD_GRAYSCALE) {
+ image_load_flags |= IMAGE_LOAD_GRAY;
+ }
+
+ if(auto image = resource::load<Image>(name, image_load_flags)) {
+ GLuint gl_texture;
+
+ glGenTextures(1, &gl_texture);
+ glBindTexture(GL_TEXTURE_2D, gl_texture);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image->size.x, image->size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
+
+ if(flags & TEXTURE_GUI_LOAD_CLAMP_S) {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ } else {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ }
+
+ if(flags & TEXTURE_GUI_LOAD_CLAMP_T) {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ } else {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ }
+
+ if(flags & TEXTURE_GUI_LOAD_LINEAR_MAG) {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ } else {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ }
+
+ if(flags & TEXTURE_GUI_LOAD_LINEAR_MIN) {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ } else {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ }
+
+ auto new_resource = std::make_shared<TextureGUI>();
+ new_resource->handle = static_cast<ImTextureID>(gl_texture);
+ new_resource->size.x = image->size.x;
+ new_resource->size.y = image->size.y;
+
+ return resource_map.insert_or_assign(name, new_resource).first->second;
+ }
+
+ return nullptr;
+}
+
+template<>
+void resource::hard_cleanup<TextureGUI>(void)
+{
+ for(const auto& it : resource_map) {
+ if(it.second.use_count() > 1L) {
+ spdlog::warn("resource: zombie resource [TextureGUI] {} [use_count={}]", it.first, it.second.use_count());
+ } else {
+ spdlog::debug("resource: releasing [TextureGUI] {}", it.first);
+ }
+
+ auto gl_texture = static_cast<GLuint>(it.second->handle);
+
+ glDeleteTextures(1, &gl_texture);
+ }
+
+ resource_map.clear();
+}
+
+template<>
+void resource::soft_cleanup<TextureGUI>(void)
+{
+ auto iter = resource_map.cbegin();
+
+ while(iter != resource_map.cend()) {
+ if(iter->second.use_count() == 1L) {
+ spdlog::debug("resource: releasing [TextureGUI] {}", iter->first);
+
+ auto gl_texture = static_cast<GLuint>(iter->second->handle);
+
+ glDeleteTextures(1, &gl_texture);
+
+ iter = resource_map.erase(iter);
+
+ continue;
+ }
+
+ iter = std::next(iter);
+ }
+}
diff --git a/game/client/resource/texture_gui.hh b/game/client/resource/texture_gui.hh
new file mode 100644
index 0000000..855596e
--- /dev/null
+++ b/game/client/resource/texture_gui.hh
@@ -0,0 +1,17 @@
+#ifndef CLIENT_TEXTURE2D_HH
+#define CLIENT_TEXTURE2D_HH 1
+#pragma once
+
+constexpr static unsigned int TEXTURE_GUI_LOAD_CLAMP_S = 0x0001;
+constexpr static unsigned int TEXTURE_GUI_LOAD_CLAMP_T = 0x0002;
+constexpr static unsigned int TEXTURE_GUI_LOAD_LINEAR_MAG = 0x0004;
+constexpr static unsigned int TEXTURE_GUI_LOAD_LINEAR_MIN = 0x0008;
+constexpr static unsigned int TEXTURE_GUI_LOAD_VFLIP = 0x0010;
+constexpr static unsigned int TEXTURE_GUI_LOAD_GRAYSCALE = 0x0020;
+
+struct TextureGUI {
+ ImTextureID handle;
+ glm::ivec2 size;
+};
+
+#endif // CLIENT_TEXTURE2D_HH