CacheStorage
This commit is contained in:
parent
87296180ee
commit
a99f58e475
2
dependencies/imgui
vendored
2
dependencies/imgui
vendored
@ -1 +1 @@
|
||||
Subproject commit 271910e3495e686a252b4b85369dec0605ba0d20
|
||||
Subproject commit 7b6314f47d2aaa3758cfeeca66af34f5c9309ca4
|
||||
2
dependencies/resource_embedder
vendored
2
dependencies/resource_embedder
vendored
@ -1 +1 @@
|
||||
Subproject commit b921791be512bffc252ab555e47d48e0fb8261b5
|
||||
Subproject commit e062aba71a93f4504180d1a4037b1849c95ba51d
|
||||
@ -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<Resources::Texture> 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)
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
#include <unordered_map>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include "../exceptions.hpp"
|
||||
|
||||
@ -4,6 +4,8 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace ougge::Resources {
|
||||
|
||||
@ -43,4 +45,21 @@ public:
|
||||
|
||||
Resource& getResource(const std::string& path);
|
||||
|
||||
}
|
||||
/// @brief stores requested resources in memory
|
||||
/// @tparam T must implement constructor `T(const Resource&, ...)`
|
||||
template<class T>
|
||||
class CacheStorage {
|
||||
std::unordered_map<std::string, T> _map;
|
||||
|
||||
public:
|
||||
template<typename... TCtorArgs>
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -23,6 +23,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct {
|
||||
const char* path;
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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<SDL_Texture>(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,
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
#include <memory>
|
||||
#include <SDL.h>
|
||||
#include "Resources.hpp"
|
||||
|
||||
@ -29,15 +30,12 @@ struct SDL_RenderCopyExF_Params {
|
||||
|
||||
struct Texture {
|
||||
SDL_Renderer* renderer;
|
||||
SDL_Texture* texture;
|
||||
std::shared_ptr<SDL_Texture> 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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user