Compare commits
No commits in common. "609f7337dabb21761ae13151cecd624b9b9dbc32" and "d5531ce370cffad14f0e3a19800072aced1a645b" have entirely different histories.
609f7337da
...
d5531ce370
@ -4,7 +4,7 @@
|
|||||||
namespace ougge {
|
namespace ougge {
|
||||||
|
|
||||||
Engine::Engine()
|
Engine::Engine()
|
||||||
: gameObjectPool(GAMEOBJECTPOOL_SIZE), textures(&resourceManager)
|
: gameObjectPool(GAMEOBJECTPOOL_SIZE)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ void Engine::init(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::openMainWindow(const std::string& window_title){
|
void Engine::openMainWindow(const std::string& window_title){
|
||||||
mainWindow.open(window_title, resourceManager);
|
mainWindow.open(window_title);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::startLoop(){
|
void Engine::startLoop(){
|
||||||
|
|||||||
@ -29,7 +29,6 @@ public:
|
|||||||
Mono::RuntimeJIT mono;
|
Mono::RuntimeJIT mono;
|
||||||
std::shared_ptr<Mono::Assembly> engineManagedAssembly;
|
std::shared_ptr<Mono::Assembly> engineManagedAssembly;
|
||||||
|
|
||||||
resources::ResourceManager resourceManager;
|
|
||||||
resources::CacheStorage<resources::Texture> textures;
|
resources::CacheStorage<resources::Texture> textures;
|
||||||
|
|
||||||
Engine();
|
Engine();
|
||||||
|
|||||||
@ -4,6 +4,8 @@
|
|||||||
#include "MainWindow.hpp"
|
#include "MainWindow.hpp"
|
||||||
#include "gui_exceptions.hpp"
|
#include "gui_exceptions.hpp"
|
||||||
#include "../common/ougge_format.hpp"
|
#include "../common/ougge_format.hpp"
|
||||||
|
#include "../resources/fonts.hpp"
|
||||||
|
#include "../resources/textures.hpp"
|
||||||
#include "../common/math.hpp"
|
#include "../common/math.hpp"
|
||||||
|
|
||||||
namespace ougge::gui {
|
namespace ougge::gui {
|
||||||
@ -19,7 +21,7 @@ f32 MainWindow::getDPI(){
|
|||||||
return dpi;
|
return dpi;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::open(const std::string& window_title, resources::ResourceManager& resourceManager){
|
void MainWindow::open(const std::string& window_title){
|
||||||
SDL_TRY(SDL_Init(SDL_INIT_EVERYTHING));
|
SDL_TRY(SDL_Init(SDL_INIT_EVERYTHING));
|
||||||
SDL_version v;
|
SDL_version v;
|
||||||
SDL_GetVersion(&v);
|
SDL_GetVersion(&v);
|
||||||
@ -70,12 +72,8 @@ void MainWindow::open(const std::string& window_title, resources::ResourceManage
|
|||||||
|
|
||||||
// Setup Dear ImGui style
|
// Setup Dear ImGui style
|
||||||
ImGui::StyleColorsDark();
|
ImGui::StyleColorsDark();
|
||||||
|
|
||||||
resources::ResourceFactory* font_res = resourceManager.tryGetResource(default_font_path);
|
|
||||||
if(font_res == nullptr)
|
|
||||||
throw UsefulException("can't find default font resource");
|
|
||||||
f32 dpi = getDPI();
|
f32 dpi = getDPI();
|
||||||
io.FontDefault = resources::ImFont_LoadFromResource(font_res, default_font_size, dpi);
|
io.FontDefault = resources::ImFont_LoadFromResource(default_font, default_font_size, dpi);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::close(){
|
void MainWindow::close(){
|
||||||
|
|||||||
@ -4,8 +4,6 @@
|
|||||||
#include <imgui.h>
|
#include <imgui.h>
|
||||||
#include "../common/std.hpp"
|
#include "../common/std.hpp"
|
||||||
#include "../common/time.hpp"
|
#include "../common/time.hpp"
|
||||||
#include "../resources/resources.hpp"
|
|
||||||
#include "../resources/fonts.hpp"
|
|
||||||
|
|
||||||
/// converts hex color to float vector
|
/// converts hex color to float vector
|
||||||
#define RGBAHexToF(R8,G8,B8,A8) ImVec4(((u8)35)/255.0f, ((u8)35)/255.0f, ((u8)50)/255.0f, ((u8)255)/255.0f)
|
#define RGBAHexToF(R8,G8,B8,A8) ImVec4(((u8)35)/255.0f, ((u8)35)/255.0f, ((u8)50)/255.0f, ((u8)255)/255.0f)
|
||||||
@ -14,7 +12,7 @@
|
|||||||
|
|
||||||
namespace ougge::gui {
|
namespace ougge::gui {
|
||||||
|
|
||||||
#define default_font_path "fonts/DroidSans.ttf"
|
#define default_font "DroidSans"
|
||||||
|
|
||||||
class MainWindow {
|
class MainWindow {
|
||||||
public:
|
public:
|
||||||
@ -30,7 +28,7 @@ private:
|
|||||||
bool show_metrics_window = false;
|
bool show_metrics_window = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void open(const std::string& window_title, resources::ResourceManager& resourceManager);
|
void open(const std::string& window_title);
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
/// process io events happened since previous frame
|
/// process io events happened since previous frame
|
||||||
|
|||||||
@ -1,40 +0,0 @@
|
|||||||
#include "MemoryStream.hpp"
|
|
||||||
|
|
||||||
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);
|
|
||||||
};
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
@ -1,8 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
|
|
||||||
class MemoryStreamRead : public std::istream {
|
|
||||||
public:
|
|
||||||
MemoryStreamRead(const void* p, const std::size_t n);
|
|
||||||
|
|
||||||
virtual ~MemoryStreamRead();
|
|
||||||
};
|
|
||||||
94
src/Resources/Resources.cpp
Normal file
94
src/Resources/Resources.cpp
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#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<std::string, Resource>* _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<std::istream> {
|
||||||
|
return std::make_unique<MemoryStreamRead>(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<std::string, Resource>();
|
||||||
|
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<<m / 1024.0f<<'G';
|
||||||
|
else if(m)
|
||||||
|
ss<<k / 1024.0f<<'M';
|
||||||
|
else if(k)
|
||||||
|
ss<<b / 1024.0f<<'K';
|
||||||
|
else ss<<b;
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
63
src/Resources/Resources.hpp
Normal file
63
src/Resources/Resources.hpp
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../common/std.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <functional>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
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<std::istream> () >;
|
||||||
|
|
||||||
|
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 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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -1,4 +1,7 @@
|
|||||||
#include "fonts.hpp"
|
#include <imgui.h>
|
||||||
|
#include "resources.hpp"
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
namespace ougge::resources {
|
namespace ougge::resources {
|
||||||
|
|
||||||
@ -13,16 +16,17 @@ ImFont* ImFont_LoadFromFile(const std::string& file_path, f32 font_size, f32 dpi
|
|||||||
return io.Fonts->AddFontFromFileTTF(file_path.c_str(), font_size, nullptr, glyph_ranges);
|
return io.Fonts->AddFontFromFileTTF(file_path.c_str(), font_size, nullptr, glyph_ranges);
|
||||||
}
|
}
|
||||||
|
|
||||||
ImFont* ImFont_LoadFromResource(ResourceFactory* res, f32 font_size, f32 dpi){
|
ImFont* ImFont_LoadFromResource(const std::string& font_name, f32 font_size, f32 dpi){
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
font_size *= dpi;
|
font_size *= dpi;
|
||||||
ImFontConfig font_cfg = ImFontConfig();
|
ImFontConfig font_cfg = ImFontConfig();
|
||||||
std::sprintf(font_cfg.Name, "%s %ipx", res->path.c_str(), (i32)font_size);
|
std::sprintf(font_cfg.Name, "%s %ipx", font_name.c_str(), (i32)font_size);
|
||||||
|
|
||||||
char* font_data = new char[res->size];
|
auto& res = getResource("fonts/" + font_name + ".ttf");
|
||||||
res->openStream()->read(font_data, res->size);
|
char* font_data = new char[res.size];
|
||||||
|
res.openStream()->read(font_data, res.size);
|
||||||
|
|
||||||
return io.Fonts->AddFontFromMemoryTTF((void*)(font_data), res->size,
|
return io.Fonts->AddFontFromMemoryTTF((void*)(font_data), res.size,
|
||||||
font_size, &font_cfg, glyph_ranges);
|
font_size, &font_cfg, glyph_ranges);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <imgui.h>
|
#include "imgui.h"
|
||||||
#include "../common/std.hpp"
|
#include "../common/std.hpp"
|
||||||
#include "resources.hpp"
|
|
||||||
|
|
||||||
namespace ougge::resources {
|
namespace ougge::resources {
|
||||||
|
|
||||||
ImFont* ImFont_LoadFromFile(const std::string& file_path, f32 font_size, f32 dpi);
|
ImFont* ImFont_LoadFromFile(const std::string& file_path, f32 font_size, f32 dpi);
|
||||||
|
|
||||||
ImFont* ImFont_LoadFromResource(ResourceFactory* res, f32 font_size, f32 dpi);
|
ImFont* ImFont_LoadFromResource(const std::string& font_name, f32 font_size, f32 dpi);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
96
src/Resources/textures.cpp
Normal file
96
src/Resources/textures.cpp
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
#include "textures.hpp"
|
||||||
|
#include <SDL_image.h>
|
||||||
|
#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<SDL_Texture>(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
44
src/Resources/textures.hpp
Normal file
44
src/Resources/textures.hpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <optional>
|
||||||
|
#include <SDL.h>
|
||||||
|
#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<SDL_Rect> texture_section;
|
||||||
|
std::optional<SDL_FRect> target_section;
|
||||||
|
std::optional<SDL_FPoint> rotation_center;
|
||||||
|
angle_t rotation_angle;
|
||||||
|
SDL_RendererFlip flip;
|
||||||
|
|
||||||
|
SDL_RenderCopyExF_Params();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Texture {
|
||||||
|
SDL_Renderer* renderer;
|
||||||
|
std::shared_ptr<SDL_Texture> 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);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
@ -63,22 +63,3 @@ std::string _ougge_format(const std::string& format_str, const size_t args_count
|
|||||||
va_end(vl);
|
va_end(vl);
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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<<m / 1024.0f<<'G';
|
|
||||||
else if(m)
|
|
||||||
ss<<k / 1024.0f<<'M';
|
|
||||||
else if(k)
|
|
||||||
ss<<b / 1024.0f<<'K';
|
|
||||||
else ss<<b;
|
|
||||||
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
|
|||||||
@ -4,5 +4,3 @@
|
|||||||
|
|
||||||
std::string _ougge_format(const std::string& format_str, const size_t args_count, ...);
|
std::string _ougge_format(const std::string& format_str, const size_t args_count, ...);
|
||||||
#define ougge_format(FORMAT_STR, ARGS...) _ougge_format(FORMAT_STR, count_args(ARGS) ,##ARGS)
|
#define ougge_format(FORMAT_STR, ARGS...) _ougge_format(FORMAT_STR, count_args(ARGS) ,##ARGS)
|
||||||
|
|
||||||
std::string formatSizeHumanReadable(std::size_t byte_n);
|
|
||||||
|
|||||||
@ -4,6 +4,8 @@
|
|||||||
#include "MainWindow.hpp"
|
#include "MainWindow.hpp"
|
||||||
#include "gui_exceptions.hpp"
|
#include "gui_exceptions.hpp"
|
||||||
#include "../common/ougge_format.hpp"
|
#include "../common/ougge_format.hpp"
|
||||||
|
#include "../resources/fonts.hpp"
|
||||||
|
#include "../resources/textures.hpp"
|
||||||
#include "../common/math.hpp"
|
#include "../common/math.hpp"
|
||||||
|
|
||||||
namespace ougge::gui {
|
namespace ougge::gui {
|
||||||
@ -19,7 +21,7 @@ f32 MainWindow::getDPI(){
|
|||||||
return dpi;
|
return dpi;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::open(const std::string& window_title, resources::ResourceManager& resourceManager){
|
void MainWindow::open(const std::string& window_title){
|
||||||
SDL_TRY(SDL_Init(SDL_INIT_EVERYTHING));
|
SDL_TRY(SDL_Init(SDL_INIT_EVERYTHING));
|
||||||
SDL_version v;
|
SDL_version v;
|
||||||
SDL_GetVersion(&v);
|
SDL_GetVersion(&v);
|
||||||
@ -70,12 +72,8 @@ void MainWindow::open(const std::string& window_title, resources::ResourceManage
|
|||||||
|
|
||||||
// Setup Dear ImGui style
|
// Setup Dear ImGui style
|
||||||
ImGui::StyleColorsDark();
|
ImGui::StyleColorsDark();
|
||||||
|
|
||||||
resources::ResourceFactory* font_res = resourceManager.tryGetResource(default_font_path);
|
|
||||||
if(font_res == nullptr)
|
|
||||||
throw UsefulException("can't find default font resource");
|
|
||||||
f32 dpi = getDPI();
|
f32 dpi = getDPI();
|
||||||
io.FontDefault = resources::ImFont_LoadFromResource(font_res, default_font_size, dpi);
|
io.FontDefault = resources::ImFont_LoadFromResource(default_font, default_font_size, dpi);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::close(){
|
void MainWindow::close(){
|
||||||
|
|||||||
@ -4,8 +4,6 @@
|
|||||||
#include <imgui.h>
|
#include <imgui.h>
|
||||||
#include "../common/std.hpp"
|
#include "../common/std.hpp"
|
||||||
#include "../common/time.hpp"
|
#include "../common/time.hpp"
|
||||||
#include "../resources/resources.hpp"
|
|
||||||
#include "../resources/fonts.hpp"
|
|
||||||
|
|
||||||
/// converts hex color to float vector
|
/// converts hex color to float vector
|
||||||
#define RGBAHexToF(R8,G8,B8,A8) ImVec4(((u8)35)/255.0f, ((u8)35)/255.0f, ((u8)50)/255.0f, ((u8)255)/255.0f)
|
#define RGBAHexToF(R8,G8,B8,A8) ImVec4(((u8)35)/255.0f, ((u8)35)/255.0f, ((u8)50)/255.0f, ((u8)255)/255.0f)
|
||||||
@ -14,7 +12,7 @@
|
|||||||
|
|
||||||
namespace ougge::gui {
|
namespace ougge::gui {
|
||||||
|
|
||||||
#define default_font_path "fonts/DroidSans.ttf"
|
#define default_font "DroidSans"
|
||||||
|
|
||||||
class MainWindow {
|
class MainWindow {
|
||||||
public:
|
public:
|
||||||
@ -30,7 +28,7 @@ private:
|
|||||||
bool show_metrics_window = false;
|
bool show_metrics_window = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void open(const std::string& window_title, resources::ResourceManager& resourceManager);
|
void open(const std::string& window_title);
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
/// process io events happened since previous frame
|
/// process io events happened since previous frame
|
||||||
|
|||||||
@ -9,19 +9,17 @@
|
|||||||
using namespace ougge;
|
using namespace ougge;
|
||||||
|
|
||||||
void drawTutel(Engine& engine){
|
void drawTutel(Engine& engine){
|
||||||
resources::Texture* tutel = engine.textures.tryGetOrCreate("tutel.png", engine.mainWindow.sdl_renderer);
|
resources::Texture& tutel = engine.textures.getOrCreate("tutel.png", engine.mainWindow.sdl_renderer);
|
||||||
if(tutel == nullptr){
|
|
||||||
throw new UsefulException("couldn't find resource 'tutel.png'");
|
|
||||||
}
|
|
||||||
resources::SDL_RenderCopyExF_Params params;
|
resources::SDL_RenderCopyExF_Params params;
|
||||||
params.target_section = SDL_FRectConstruct(100, 100, 400, 400);
|
params.target_section = SDL_FRectConstruct(100, 100, 400, 400);
|
||||||
static i32 si = 1;
|
static i32 si = 1;
|
||||||
params.rotation_angle = M_PI_4 * (si++ / engine.mainWindow.fps_max);
|
params.rotation_angle = M_PI_4 * (si++ / engine.mainWindow.fps_max);
|
||||||
tutel->render(params);
|
tutel.render(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char** argv){
|
int main(int argc, const char** argv){
|
||||||
try {
|
try {
|
||||||
|
resources::init();
|
||||||
std::cout<<"initialized resource loader"<<std::endl;
|
std::cout<<"initialized resource loader"<<std::endl;
|
||||||
|
|
||||||
Engine engine;
|
Engine engine;
|
||||||
|
|||||||
@ -1,40 +0,0 @@
|
|||||||
#include "MemoryStream.hpp"
|
|
||||||
|
|
||||||
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);
|
|
||||||
};
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
@ -1,8 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
|
|
||||||
class MemoryStreamRead : public std::istream {
|
|
||||||
public:
|
|
||||||
MemoryStreamRead(const void* p, const std::size_t n);
|
|
||||||
|
|
||||||
virtual ~MemoryStreamRead();
|
|
||||||
};
|
|
||||||
@ -1,4 +1,7 @@
|
|||||||
#include "fonts.hpp"
|
#include <imgui.h>
|
||||||
|
#include "resources.hpp"
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
namespace ougge::resources {
|
namespace ougge::resources {
|
||||||
|
|
||||||
@ -13,16 +16,17 @@ ImFont* ImFont_LoadFromFile(const std::string& file_path, f32 font_size, f32 dpi
|
|||||||
return io.Fonts->AddFontFromFileTTF(file_path.c_str(), font_size, nullptr, glyph_ranges);
|
return io.Fonts->AddFontFromFileTTF(file_path.c_str(), font_size, nullptr, glyph_ranges);
|
||||||
}
|
}
|
||||||
|
|
||||||
ImFont* ImFont_LoadFromResource(ResourceFactory* res, f32 font_size, f32 dpi){
|
ImFont* ImFont_LoadFromResource(const std::string& font_name, f32 font_size, f32 dpi){
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
font_size *= dpi;
|
font_size *= dpi;
|
||||||
ImFontConfig font_cfg = ImFontConfig();
|
ImFontConfig font_cfg = ImFontConfig();
|
||||||
std::sprintf(font_cfg.Name, "%s %ipx", res->path.c_str(), (i32)font_size);
|
std::sprintf(font_cfg.Name, "%s %ipx", font_name.c_str(), (i32)font_size);
|
||||||
|
|
||||||
char* font_data = new char[res->size];
|
auto& res = getResource("fonts/" + font_name + ".ttf");
|
||||||
res->openStream()->read(font_data, res->size);
|
char* font_data = new char[res.size];
|
||||||
|
res.openStream()->read(font_data, res.size);
|
||||||
|
|
||||||
return io.Fonts->AddFontFromMemoryTTF((void*)(font_data), res->size,
|
return io.Fonts->AddFontFromMemoryTTF((void*)(font_data), res.size,
|
||||||
font_size, &font_cfg, glyph_ranges);
|
font_size, &font_cfg, glyph_ranges);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <imgui.h>
|
#include "imgui.h"
|
||||||
#include "../common/std.hpp"
|
#include "../common/std.hpp"
|
||||||
#include "resources.hpp"
|
|
||||||
|
|
||||||
namespace ougge::resources {
|
namespace ougge::resources {
|
||||||
|
|
||||||
ImFont* ImFont_LoadFromFile(const std::string& file_path, f32 font_size, f32 dpi);
|
ImFont* ImFont_LoadFromFile(const std::string& file_path, f32 font_size, f32 dpi);
|
||||||
|
|
||||||
ImFont* ImFont_LoadFromResource(ResourceFactory* res, f32 font_size, f32 dpi);
|
ImFont* ImFont_LoadFromResource(const std::string& font_name, f32 font_size, f32 dpi);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,22 +4,45 @@
|
|||||||
#include "../common/ougge_format.hpp"
|
#include "../common/ougge_format.hpp"
|
||||||
#include "resources.hpp"
|
#include "resources.hpp"
|
||||||
#include "embedded_resources.h"
|
#include "embedded_resources.h"
|
||||||
#include "MemoryStream.hpp"
|
|
||||||
|
|
||||||
namespace ougge::resources {
|
namespace ougge::resources {
|
||||||
|
|
||||||
ResourceFactory::ResourceFactory(const std::string& path, const std::size_t size, StreamFactoryMethod open_read_steam_func)
|
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)
|
: 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::shared_ptr<ResourceMap> ResourceManager::_embeddedResourcesMap;
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
void ResourceManager::loadEmbeddedResources(){
|
MemoryStreamRead::MemoryStreamRead(const void* p, const std::size_t n)
|
||||||
if(_embeddedResourcesMap != nullptr)
|
: std::istream(new MemoryStreamBuf((void*)p, n))
|
||||||
return;
|
{}
|
||||||
|
|
||||||
_embeddedResourcesMap = std::make_shared<ResourceMap>();
|
MemoryStreamRead::~MemoryStreamRead(){
|
||||||
|
delete rdbuf();
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::unordered_map<std::string, Resource>* _resourceMap = nullptr;
|
||||||
|
|
||||||
|
void loadEmbeddedresources(){
|
||||||
for(int i = 0; i < EmbeddedResource_table_count; i++){
|
for(int i = 0; i < EmbeddedResource_table_count; i++){
|
||||||
const EmbeddedResource& e = EmbeddedResource_table[i];
|
const EmbeddedResource& e = EmbeddedResource_table[i];
|
||||||
std::cout <<"loading resource '" << e.path << "' "
|
std::cout <<"loading resource '" << e.path << "' "
|
||||||
@ -29,36 +52,43 @@ void ResourceManager::loadEmbeddedResources(){
|
|||||||
return std::make_unique<MemoryStreamRead>(e.data, e.size);
|
return std::make_unique<MemoryStreamRead>(e.data, e.size);
|
||||||
};
|
};
|
||||||
|
|
||||||
auto r = _embeddedResourcesMap->emplace(e.path,
|
auto r = _resourceMap->emplace(e.path,
|
||||||
ResourceFactory(e.path, e.size, embedded_resource_factory));
|
Resource(e.path, e.size, embedded_resource_factory));
|
||||||
if(!r.second)
|
if(!r.second)
|
||||||
throw UsefulException(ougge_format("can't load duplicate resource '%s'", e.path));
|
throw UsefulException(ougge_format("can't load duplicate resource '%s'", e.path));
|
||||||
}
|
}
|
||||||
|
|
||||||
_resourceMaps.push_back(_embeddedResourcesMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ResourceManager::ResourceManager(){
|
void init(){
|
||||||
loadEmbeddedResources();
|
if(_resourceMap != nullptr)
|
||||||
|
throw UsefulException("resource has been initialized already");
|
||||||
|
_resourceMap = new std::unordered_map<std::string, Resource>();
|
||||||
|
loadEmbeddedresources();
|
||||||
}
|
}
|
||||||
|
|
||||||
ResourceFactory* ResourceManager::tryGetResource(const std::string& path){
|
Resource& getResource(const std::string& path){
|
||||||
// reverse iteration of linked list
|
auto it = _resourceMap->find(path);
|
||||||
auto map_list_iterator = _resourceMaps.end();
|
if(it == _resourceMap->end())
|
||||||
while(map_list_iterator != _resourceMaps.begin()){
|
throw UsefulException(ougge_format("can't find resource '%s'", path.c_str()));
|
||||||
--map_list_iterator;
|
return it->second;
|
||||||
auto map = *map_list_iterator;
|
}
|
||||||
|
|
||||||
auto resource_iterator = map->find(path);
|
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;
|
||||||
|
|
||||||
// resource not found
|
if(g)
|
||||||
if(resource_iterator == map->end())
|
ss<<m / 1024.0f<<'G';
|
||||||
return nullptr;
|
else if(m)
|
||||||
|
ss<<k / 1024.0f<<'M';
|
||||||
|
else if(k)
|
||||||
|
ss<<b / 1024.0f<<'K';
|
||||||
|
else ss<<b;
|
||||||
|
|
||||||
return &resource_iterator->second;
|
return ss.str();
|
||||||
}
|
|
||||||
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,71 +4,60 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <memory>
|
|
||||||
#include <list>
|
|
||||||
|
|
||||||
namespace ougge::resources {
|
namespace ougge::resources {
|
||||||
|
|
||||||
class ResourceFactory {
|
// call this in main()
|
||||||
|
void init();
|
||||||
|
|
||||||
|
std::string formatSizeHumanReadable(std::size_t byte_n);
|
||||||
|
|
||||||
|
class Resource {
|
||||||
public:
|
public:
|
||||||
const std::string path;
|
const std::string path;
|
||||||
const std::size_t size;
|
const std::size_t size;
|
||||||
|
|
||||||
using StreamFactoryMethod = std::function< std::unique_ptr<std::istream> () >;
|
using StreamFactoryMethod = std::function< std::unique_ptr<std::istream> () >;
|
||||||
|
|
||||||
ResourceFactory(const std::string& path, const std::size_t size, StreamFactoryMethod open_read_steam_func);
|
Resource(const std::string& path, const std::size_t size, StreamFactoryMethod open_read_steam_func);
|
||||||
|
|
||||||
const StreamFactoryMethod openStream;
|
const StreamFactoryMethod openStream;
|
||||||
};
|
};
|
||||||
|
|
||||||
using ResourceMap = std::unordered_map<std::string, ResourceFactory>;
|
class MemoryStreamBuf : public std::streambuf {
|
||||||
|
|
||||||
class ResourceManager {
|
|
||||||
std::list<std::shared_ptr<ResourceMap>> _resourceMaps;
|
|
||||||
static std::shared_ptr<ResourceMap> _embeddedResourcesMap;
|
|
||||||
void loadEmbeddedResources();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ResourceManager();
|
MemoryStreamBuf(void* p, const std::size_t n);
|
||||||
|
|
||||||
void addResourceMap(std::shared_ptr<ResourceMap> m);
|
virtual std::istream::pos_type seekoff(
|
||||||
|
std::istream::off_type off,
|
||||||
std::shared_ptr<ResourceMap> loadResouceMap();
|
std::ios_base::seekdir dir,
|
||||||
void removeResourceMap();
|
std::ios_base::openmode which);
|
||||||
|
|
||||||
ResourceFactory* tryGetResource(const std::string& path);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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
|
/// @brief stores requested resources in memory
|
||||||
/// @tparam T must implement constructor `T(ResourceFactory*, ...)`
|
/// @tparam T must implement constructor `T(const Resource&, ...)`
|
||||||
template<class T>
|
template<class T>
|
||||||
class CacheStorage {
|
class CacheStorage {
|
||||||
std::unordered_map<std::string, T> _map;
|
std::unordered_map<std::string, T> _map;
|
||||||
ResourceManager* _resourceManager;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CacheStorage(ResourceManager*);
|
|
||||||
CacheStorage(CacheStorage&&) = delete;
|
|
||||||
CacheStorage(const CacheStorage&) = delete;
|
|
||||||
|
|
||||||
template<typename... TCtorArgs>
|
template<typename... TCtorArgs>
|
||||||
T* tryGetOrCreate(const std::string& name, TCtorArgs&&... ctor_args){
|
T& getOrCreate(const std::string& name, TCtorArgs&&... ctor_args){
|
||||||
auto it = _map.find(name);
|
auto it = _map.find(name);
|
||||||
if(it != _map.end())
|
if(it != _map.end())
|
||||||
return &it->second;
|
return it->second;
|
||||||
|
auto& res = getResource(name);
|
||||||
ResourceFactory* res = _resourceManager->tryGetResource(name);
|
|
||||||
if(res == nullptr)
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
auto e = _map.emplace(name, T(res, ctor_args...));
|
auto e = _map.emplace(name, T(res, ctor_args...));
|
||||||
return &e.first->second;
|
return e.first->second;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template <class T> inline CacheStorage<T>::CacheStorage(ResourceManager* resourceManager)
|
|
||||||
: _resourceManager(resourceManager)
|
|
||||||
{}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,8 +4,8 @@
|
|||||||
|
|
||||||
namespace ougge::resources {
|
namespace ougge::resources {
|
||||||
|
|
||||||
Texture::Texture(ResourceFactory* r, SDL_Renderer* renderer)
|
Texture::Texture(const Resource& r, SDL_Renderer* renderer)
|
||||||
: Texture(*r->openStream(), r->size, renderer)
|
: Texture(*r.openStream(), r.size, renderer)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Texture::Texture(std::istream& s, size_t size, SDL_Renderer* renderer)
|
Texture::Texture(std::istream& s, size_t size, SDL_Renderer* renderer)
|
||||||
|
|||||||
@ -33,7 +33,7 @@ struct Texture {
|
|||||||
i32 w;
|
i32 w;
|
||||||
i32 h;
|
i32 h;
|
||||||
|
|
||||||
Texture(ResourceFactory* r, SDL_Renderer* renderer);
|
Texture(const Resource& r, SDL_Renderer* renderer);
|
||||||
Texture(std::istream& s, size_t size, 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);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user