From a99f58e475f33a330ff5abf172830ca948ff9bf8 Mon Sep 17 00:00:00 2001 From: Timerix Date: Wed, 7 Aug 2024 19:40:55 +0300 Subject: [PATCH] CacheStorage --- dependencies/imgui | 2 +- dependencies/resource_embedder | 2 +- src/GUI/MainWindow.cpp | 10 +++++----- src/Resources/Resources.cpp | 1 - src/Resources/Resources.hpp | 21 ++++++++++++++++++++- src/Resources/embedded_resources.h | 1 + src/Resources/fonts.cpp | 2 +- src/Resources/textures.cpp | 29 ++++++++++------------------- src/Resources/textures.hpp | 10 ++++------ 9 files changed, 43 insertions(+), 35 deletions(-) diff --git a/dependencies/imgui b/dependencies/imgui index 271910e..7b6314f 160000 --- a/dependencies/imgui +++ b/dependencies/imgui @@ -1 +1 @@ -Subproject commit 271910e3495e686a252b4b85369dec0605ba0d20 +Subproject commit 7b6314f47d2aaa3758cfeeca66af34f5c9309ca4 diff --git a/dependencies/resource_embedder b/dependencies/resource_embedder index b921791..e062aba 160000 --- a/dependencies/resource_embedder +++ b/dependencies/resource_embedder @@ -1 +1 @@ -Subproject commit b921791be512bffc252ab555e47d48e0fb8261b5 +Subproject commit e062aba71a93f4504180d1a4037b1849c95ba51d diff --git a/src/GUI/MainWindow.cpp b/src/GUI/MainWindow.cpp index 533e44a..ef7c39a 100644 --- a/src/GUI/MainWindow.cpp +++ b/src/GUI/MainWindow.cpp @@ -159,10 +159,11 @@ void MainWindow::draw_frame(){ SDL_SetRenderDrawColor(sdl_renderer, (Uint8)(clear_color.x * 255), (Uint8)(clear_color.y * 255), (Uint8)(clear_color.z * 255), (Uint8)(clear_color.w * 255)); SDL_RenderClear(sdl_renderer); - // Resources::getCached - Resources::Texture sprite(sdl_renderer); - sprite.loadFrom(Resources::getResource("tutel.png")); - sprite.render(SDL_FRectConstruct(100, 100, 400, 400)); + // Example sprite + //Resources::Texture tutel(Resources::getResource("tutel.png"), sdl_renderer); + static Resources::CacheStorage textures; + Resources::Texture& tutel = textures.getOrCreate("tutel.png", sdl_renderer); + tutel.render(SDL_FRectConstruct(100, 100, 400, 400)); ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), sdl_renderer); // Swap buffers @@ -182,7 +183,6 @@ void MainWindow::startAndWait(){ // main loop while(loop_running){ // waits for events - main_loop_wait_for_input = false; poll_events(frame_updates_requested, main_loop_wait_for_input); if(frame_updates_requested==0) diff --git a/src/Resources/Resources.cpp b/src/Resources/Resources.cpp index 1767e48..d49dcd3 100644 --- a/src/Resources/Resources.cpp +++ b/src/Resources/Resources.cpp @@ -1,4 +1,3 @@ -#include #include #include #include "../exceptions.hpp" diff --git a/src/Resources/Resources.hpp b/src/Resources/Resources.hpp index 65c73bf..9a9d6e6 100644 --- a/src/Resources/Resources.hpp +++ b/src/Resources/Resources.hpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include namespace ougge::Resources { @@ -43,4 +45,21 @@ public: Resource& getResource(const std::string& path); -} \ No newline at end of file +/// @brief stores requested resources in memory +/// @tparam T must implement constructor `T(const Resource&, ...)` +template +class CacheStorage { + std::unordered_map _map; + +public: + template + T& getOrCreate(const std::string& name, TCtorArgs&&... ctor_args){ + auto it = _map.find(name); + if(it != _map.end()) + return it->second; + auto& res = getResource(name); + auto e = _map.emplace(name, T(res, ctor_args...)); + return e.first->second; + } +}; +} diff --git a/src/Resources/embedded_resources.h b/src/Resources/embedded_resources.h index 817e24a..96333b1 100644 --- a/src/Resources/embedded_resources.h +++ b/src/Resources/embedded_resources.h @@ -23,6 +23,7 @@ extern "C" { #endif #include +#include typedef struct { const char* path; diff --git a/src/Resources/fonts.cpp b/src/Resources/fonts.cpp index c866a9b..96e6d63 100644 --- a/src/Resources/fonts.cpp +++ b/src/Resources/fonts.cpp @@ -22,7 +22,7 @@ ImFont* ImFont_LoadFromResource(const std::string& font_name, f32 font_size, f32 ImFontConfig font_cfg = ImFontConfig(); std::sprintf(font_cfg.Name, "%s %ipx", font_name.c_str(), (i32)font_size); - Resource& res = getResource("fonts/" + font_name + ".ttf"); + auto& res = getResource("fonts/" + font_name + ".ttf"); char* font_data = new char[res.size]; res.openStream()->read(font_data, res.size); diff --git a/src/Resources/textures.cpp b/src/Resources/textures.cpp index a469cc6..f400471 100644 --- a/src/Resources/textures.cpp +++ b/src/Resources/textures.cpp @@ -4,29 +4,20 @@ namespace ougge::Resources { -Texture::Texture(SDL_Renderer* renderer) - : renderer(renderer), texture(nullptr), w(0), h(0) +Texture::Texture(const Resource& r, SDL_Renderer* renderer) + : Texture(*r.openStream(), r.size, renderer) {} -Texture::~Texture(){ - SDL_DestroyTexture(texture); -} - -void Texture::loadFrom(const Resource& r){ - auto s = r.openStream(); - loadFrom(*s, r.size); -} - -void Texture::loadFrom(std::istream& s, size_t size){ - if(texture) - throw UsefulException("texture has been loaded already"); +Texture::Texture(std::istream& s, size_t size, SDL_Renderer* renderer) + : renderer(renderer), texture(nullptr), w(0), h(0) +{ SDL_RWops* sdl_stream = SDL_RWFromIStream(s, size); if(!sdl_stream) throw SDLException(); - texture = IMG_LoadTexture_RW(renderer, sdl_stream, 1); + texture = std::shared_ptr(IMG_LoadTexture_RW(renderer, sdl_stream, 1), SDL_DestroyTexture); if(!texture) throw IMGException(); - SDL_TRY(SDL_QueryTexture(texture, nullptr, nullptr, &w, &h)); + SDL_TRY(SDL_QueryTexture(texture.get(), nullptr, nullptr, &w, &h)); } SDL_RenderCopyExF_Params::SDL_RenderCopyExF_Params() @@ -35,19 +26,19 @@ SDL_RenderCopyExF_Params::SDL_RenderCopyExF_Params() void Texture::render(const SDL_FRect& target_section){ SDL_TRY( - SDL_RenderCopyF(renderer, texture, nullptr, &target_section) + SDL_RenderCopyF(renderer, texture.get(), nullptr, &target_section) ); } void Texture::render(const SDL_FRect& target_section, const SDL_Rect& texture_section){ SDL_TRY( - SDL_RenderCopyF(renderer, texture, &texture_section, &target_section) + SDL_RenderCopyF(renderer, texture.get(), &texture_section, &target_section) ); } void Texture::render(const SDL_RenderCopyExF_Params& p){ SDL_TRY( - SDL_RenderCopyExF(renderer, texture, + SDL_RenderCopyExF(renderer, texture.get(), optional_value_ptr_or_null(p.texture_section), optional_value_ptr_or_null(p.target_section), p.rotation_angle, diff --git a/src/Resources/textures.hpp b/src/Resources/textures.hpp index 37b0dd5..4c8a5fb 100644 --- a/src/Resources/textures.hpp +++ b/src/Resources/textures.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "Resources.hpp" @@ -29,15 +30,12 @@ struct SDL_RenderCopyExF_Params { struct Texture { SDL_Renderer* renderer; - SDL_Texture* texture; + std::shared_ptr texture; int w; int h; - Texture(SDL_Renderer* renderer); - ~Texture(); - - void loadFrom(const Resource& r); - void loadFrom(std::istream& s, size_t size); + Texture(const Resource& r, SDL_Renderer* renderer); + Texture(std::istream& s, size_t size, SDL_Renderer* renderer); void render(const SDL_FRect& target_section); void render(const SDL_FRect& target_section, const SDL_Rect& texture_section);