From d700aae02e347f189e78008d74de9eae857ddc82 Mon Sep 17 00:00:00 2001 From: Timerix Date: Mon, 19 May 2025 02:27:39 +0500 Subject: [PATCH] duplicate files fix --- src/Resources/Resources.cpp | 94 ------------------------------------ src/Resources/Resources.hpp | 63 ------------------------ src/Resources/textures.cpp | 96 ------------------------------------- src/Resources/textures.hpp | 44 ----------------- 4 files changed, 297 deletions(-) delete mode 100644 src/Resources/Resources.cpp delete mode 100644 src/Resources/Resources.hpp delete mode 100644 src/Resources/textures.cpp delete mode 100644 src/Resources/textures.hpp diff --git a/src/Resources/Resources.cpp b/src/Resources/Resources.cpp deleted file mode 100644 index a996571..0000000 --- a/src/Resources/Resources.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#include -#include -#include "../common/UsefulException.hpp" -#include "../common/ougge_format.hpp" -#include "resources.hpp" -#include "embedded_resources.h" - - -namespace ougge::resources { - -Resource::Resource(const std::string& path, const std::size_t size, StreamFactoryMethod open_read_steam_func) - : path(path), size(size), openStream(open_read_steam_func) -{} - -MemoryStreamBuf::MemoryStreamBuf(void* _p, const std::size_t n){ - char* p=(char*)_p; - setg(p, p, p + n); - setp(p, p + n); -} - -std::istream::pos_type MemoryStreamBuf::seekoff( - std::istream::off_type off, - std::ios_base::seekdir dir, - std::ios_base::openmode which) -{ - if (dir == std::ios_base::cur) - gbump(off); - else if (dir == std::ios_base::end) - setg(eback(), egptr() + off, egptr()); - else if (dir == std::ios_base::beg) - setg(eback(), eback() + off, egptr()); - return gptr() - eback(); -} - -MemoryStreamRead::MemoryStreamRead(const void* p, const std::size_t n) - : std::istream(new MemoryStreamBuf((void*)p, n)) -{} - -MemoryStreamRead::~MemoryStreamRead(){ - delete rdbuf(); -} - -static std::unordered_map* _resourceMap = nullptr; - -void loadEmbeddedresources(){ - for(int i = 0; i < EmbeddedResource_table_count; i++){ - const EmbeddedResource& e = EmbeddedResource_table[i]; - std::cout <<"loading resource '" << e.path << "' " - << formatSizeHumanReadable(e.size) << std::endl; - - auto embedded_resource_factory = [e]() -> std::unique_ptr { - return std::make_unique(e.data, e.size); - }; - - auto r = _resourceMap->emplace(e.path, - Resource(e.path, e.size, embedded_resource_factory)); - if(!r.second) - throw UsefulException(ougge_format("can't load duplicate resource '%s'", e.path)); - } -} - -void init(){ - if(_resourceMap != nullptr) - throw UsefulException("resource has been initialized already"); - _resourceMap = new std::unordered_map(); - loadEmbeddedresources(); -} - -Resource& getResource(const std::string& path){ - auto it = _resourceMap->find(path); - if(it == _resourceMap->end()) - throw UsefulException(ougge_format("can't find resource '%s'", path.c_str())); - return it->second; -} - -std::string formatSizeHumanReadable(std::size_t b){ - std::stringstream ss; - ss.precision(3); - std::size_t k = b / 1024; - std::size_t m = k / 1024; - std::size_t g = m / 1024; - - if(g) - ss< -#include -#include - -namespace ougge::resources { - -// call this in main() -void init(); - -std::string formatSizeHumanReadable(std::size_t byte_n); - -class Resource { -public: - const std::string path; - const std::size_t size; - - using StreamFactoryMethod = std::function< std::unique_ptr () >; - - Resource(const std::string& path, const std::size_t size, StreamFactoryMethod open_read_steam_func); - - const StreamFactoryMethod openStream; -}; - -class MemoryStreamBuf : public std::streambuf { -public: - MemoryStreamBuf(void* p, const std::size_t n); - - virtual std::istream::pos_type seekoff( - std::istream::off_type off, - std::ios_base::seekdir dir, - std::ios_base::openmode which); -}; - -class MemoryStreamRead : public std::istream { -public: - MemoryStreamRead(const void* p, const std::size_t n); - - virtual ~MemoryStreamRead(); -}; - -Resource& getResource(const std::string& path); - -/// @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/textures.cpp b/src/Resources/textures.cpp deleted file mode 100644 index 2476a2b..0000000 --- a/src/Resources/textures.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include "textures.hpp" -#include -#include "../gui/gui_exceptions.hpp" - -namespace ougge::resources { - -Texture::Texture(const Resource& r, SDL_Renderer* renderer) - : Texture(*r.openStream(), r.size, renderer) -{} - -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 gui::SDLException(); - texture = std::shared_ptr(IMG_LoadTexture_RW(renderer, sdl_stream, 1), SDL_DestroyTexture); - if(!texture) - throw gui::IMGException(); - SDL_TRY(SDL_QueryTexture(texture.get(), nullptr, nullptr, &w, &h)); -} - -SDL_RenderCopyExF_Params::SDL_RenderCopyExF_Params() - : rotation_angle(0), flip(SDL_FLIP_NONE) -{} - -void Texture::render(const SDL_FRect& target_section){ - SDL_TRY( - 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.get(), &texture_section, &target_section) - ); -} - -void Texture::render(const SDL_RenderCopyExF_Params& p){ - SDL_TRY( - SDL_RenderCopyExF(renderer, texture.get(), - optional_value_ptr_or_null(p.texture_section), - optional_value_ptr_or_null(p.target_section), - -1.0f * angleToDegree(normalizeAngle(p.rotation_angle)), - optional_value_ptr_or_null(p.rotation_center), - p.flip - ) - ) -} - -static Sint64 istream_size(SDL_RWops* context){ - return (Sint64)(size_t)context->hidden.unknown.data2; -} - -static Sint64 istream_seek(SDL_RWops* context, Sint64 offset, i32 whence){ - std::istream* stream = (std::istream*)context->hidden.unknown.data1; - switch(whence){ - case SEEK_SET: stream->seekg(offset, std::ios::beg); break; - case SEEK_CUR: stream->seekg(offset, std::ios::cur); break; - case SEEK_END: stream->seekg(offset, std::ios::end); break; - default: break; - } - return stream->fail() ? -1 : (Sint64)stream->tellg(); -} - -static size_t istream_read(SDL_RWops* context, void *ptr, size_t size, size_t maxnum){ - if(size == 0) - return -1; - std::istream* stream = (std::istream*)context->hidden.unknown.data1; - stream->read((char*)ptr, size * maxnum); - - return stream->bad() ? -1 : stream->gcount() / size; -} - -static i32 istream_close(SDL_RWops* context){ - if (context) - SDL_FreeRW(context); - return 0; -} - -SDL_RWops* SDL_RWFromIStream(std::istream& stream, size_t size){ - SDL_RWops* rwops = SDL_AllocRW(); - if(rwops) { - rwops->size = istream_size; - rwops->seek = istream_seek; - rwops->read = istream_read; - rwops->write = nullptr; - rwops->close = istream_close; - rwops->hidden.unknown.data1 = &stream; - rwops->hidden.unknown.data2 = (void*)size; - rwops->type = SDL_RWOPS_UNKNOWN; - } - return rwops; -} - -} diff --git a/src/Resources/textures.hpp b/src/Resources/textures.hpp deleted file mode 100644 index 2c05b7c..0000000 --- a/src/Resources/textures.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include -#include -#include -#include "resources.hpp" -#include "../common/math.hpp" - -namespace ougge::resources { - -#define SDL_RectConstruct(X, Y, W, H) (SDL_Rect){X, Y, W, H} -#define SDL_FRectConstruct(X, Y, W, H) (SDL_FRect){X, Y, W, H} -#define SDL_PointConstruct(X, Y) (SDL_Point){X, Y, W, H} -#define SDL_FPointConstruct(X, Y) (SDL_FPoint){X, Y, W, H} - -#define optional_value_ptr_or_null(OPT) (OPT ? &(*OPT) : nullptr) - -SDL_RWops* SDL_RWFromIStream(std::istream& stream, size_t size); - -struct SDL_RenderCopyExF_Params { - std::optional texture_section; - std::optional target_section; - std::optional rotation_center; - angle_t rotation_angle; - SDL_RendererFlip flip; - - SDL_RenderCopyExF_Params(); -}; - -struct Texture { - SDL_Renderer* renderer; - std::shared_ptr texture; - i32 w; - i32 h; - - 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); - void render(const SDL_RenderCopyExF_Params& params); -}; - -}