Packs management update WIP

This commit is contained in:
MihailRis 2023-12-18 13:58:43 +03:00
parent 98021f533a
commit 826a743ab9
4 changed files with 69 additions and 36 deletions

View File

@ -6,32 +6,17 @@
#include "../graphics/Font.h" #include "../graphics/Font.h"
Assets::~Assets() { Assets::~Assets() {
for (auto& iter : shaders){
delete iter.second;
}
for (auto& iter : textures){
delete iter.second;
}
for (auto& iter : fonts){
delete iter.second;
}
for (auto& iter : atlases) {
delete iter.second;
}
} }
Texture* Assets::getTexture(std::string name) const { Texture* Assets::getTexture(std::string name) const {
auto found = textures.find(name); auto found = textures.find(name);
if (found == textures.end()) if (found == textures.end())
return nullptr; return nullptr;
return found->second; return found->second.get();
} }
void Assets::store(Texture* texture, std::string name){ void Assets::store(Texture* texture, std::string name){
textures[name] = texture; textures[name].reset(texture);
} }
@ -39,11 +24,11 @@ Shader* Assets::getShader(std::string name) const{
auto found = shaders.find(name); auto found = shaders.find(name);
if (found == shaders.end()) if (found == shaders.end())
return nullptr; return nullptr;
return found->second; return found->second.get();
} }
void Assets::store(Shader* shader, std::string name){ void Assets::store(Shader* shader, std::string name){
shaders[name] = shader; shaders[name].reset(shader);
} }
@ -51,20 +36,35 @@ Font* Assets::getFont(std::string name) const {
auto found = fonts.find(name); auto found = fonts.find(name);
if (found == fonts.end()) if (found == fonts.end())
return nullptr; return nullptr;
return found->second; return found->second.get();
} }
void Assets::store(Font* font, std::string name){ void Assets::store(Font* font, std::string name){
fonts[name] = font; fonts[name].reset(font);
} }
Atlas* Assets::getAtlas(std::string name) const { Atlas* Assets::getAtlas(std::string name) const {
auto found = atlases.find(name); auto found = atlases.find(name);
if (found == atlases.end()) if (found == atlases.end())
return nullptr; return nullptr;
return found->second; return found->second.get();
} }
void Assets::store(Atlas* atlas, std::string name){ void Assets::store(Atlas* atlas, std::string name){
atlases[name] = atlas; atlases[name].reset(atlas);
}
void Assets::extend(const Assets& assets) {
for (auto entry : assets.textures) {
textures[entry.first] = entry.second;
}
for (auto entry : assets.shaders) {
shaders[entry.first] = entry.second;
}
for (auto entry : assets.fonts) {
fonts[entry.first] = entry.second;
}
for (auto entry : assets.atlases) {
atlases[entry.first] = entry.second;
}
} }

View File

@ -2,6 +2,7 @@
#define ASSETS_ASSETS_H_ #define ASSETS_ASSETS_H_
#include <string> #include <string>
#include <memory>
#include <unordered_map> #include <unordered_map>
class Texture; class Texture;
@ -10,10 +11,10 @@ class Font;
class Atlas; class Atlas;
class Assets { class Assets {
std::unordered_map<std::string, Texture*> textures; std::unordered_map<std::string, std::shared_ptr<Texture>> textures;
std::unordered_map<std::string, Shader*> shaders; std::unordered_map<std::string, std::shared_ptr<Shader>> shaders;
std::unordered_map<std::string, Font*> fonts; std::unordered_map<std::string, std::shared_ptr<Font>> fonts;
std::unordered_map<std::string, Atlas*> atlases; std::unordered_map<std::string, std::shared_ptr<Atlas>> atlases;
public: public:
~Assets(); ~Assets();
Texture* getTexture(std::string name) const; Texture* getTexture(std::string name) const;
@ -27,6 +28,8 @@ public:
Atlas* getAtlas(std::string name) const; Atlas* getAtlas(std::string name) const;
void store(Atlas* atlas, std::string name); void store(Atlas* atlas, std::string name);
void extend(const Assets& assets);
}; };
#endif /* ASSETS_ASSETS_H_ */ #endif /* ASSETS_ASSETS_H_ */

View File

@ -51,8 +51,23 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
} }
auto resdir = paths->getResources(); auto resdir = paths->getResources();
contentPacks.push_back(ContentPack::read(resdir/path("content/base")));
loadContent(); std::cout << "-- loading assets" << std::endl;
std::vector<path> roots {resdir};
resPaths.reset(new ResPaths(resdir, roots));
assets.reset(new Assets());
AssetsLoader loader(assets.get(), resPaths.get());
AssetsLoader::createDefaults(loader);
AssetsLoader::addDefaults(loader);
Shader::preprocessor->setPaths(resPaths.get());
while (loader.hasNext()) {
if (!loader.loadNext()) {
assets.reset();
Window::terminate();
throw initialize_error("could not to initialize assets");
}
}
Audio::initialize(); Audio::initialize();
gui = new GUI(); gui = new GUI();
@ -169,16 +184,16 @@ void Engine::loadContent() {
Shader::preprocessor->setPaths(resPaths.get()); Shader::preprocessor->setPaths(resPaths.get());
assets.reset(new Assets()); unique_ptr<Assets> new_assets(new Assets());
std::cout << "-- loading assets" << std::endl; std::cout << "-- loading assets" << std::endl;
AssetsLoader loader(assets.get(), resPaths.get()); AssetsLoader loader(new_assets.get(), resPaths.get());
AssetsLoader::createDefaults(loader); AssetsLoader::createDefaults(loader);
AssetsLoader::addDefaults(loader); AssetsLoader::addDefaults(loader);
while (loader.hasNext()) { while (loader.hasNext()) {
if (!loader.loadNext()) { if (!loader.loadNext()) {
assets.reset(); new_assets.reset();
Window::terminate(); throw std::runtime_error("could not to load assets");
throw initialize_error("could not to initialize assets");
} }
} }
assets->extend(*new_assets.get());
} }

View File

@ -158,6 +158,13 @@ Panel* create_main_menu_panel(Engine* engine, PagesControl* menu) {
vec4(10.0f, 8.0f, 10.0f, 8.0f)); vec4(10.0f, 8.0f, 10.0f, 8.0f));
button->color(vec4(1.0f, 1.0f, 1.0f, 0.1f)); button->color(vec4(1.0f, 1.0f, 1.0f, 0.1f));
button->listenAction([=](GUI* gui) { button->listenAction([=](GUI* gui) {
// TODO: complete and move somewhere
auto resdir = engine->getPaths()->getResources();
auto& packs = engine->getContentPacks();
packs.clear();
packs.push_back(ContentPack::read(resdir/path("content/base")));
engine->loadContent();
auto* content = engine->getContent(); auto* content = engine->getContent();
auto& settings = engine->getSettings(); auto& settings = engine->getSettings();
auto folder = paths->getWorldsFolder()/u8path(name); auto folder = paths->getWorldsFolder()/u8path(name);
@ -269,6 +276,14 @@ Panel* create_new_world_panel(Engine* engine, PagesControl* menu) {
auto folder = paths->getWorldsFolder()/u8path(nameutf8); auto folder = paths->getWorldsFolder()/u8path(nameutf8);
std::filesystem::create_directories(folder); std::filesystem::create_directories(folder);
// TODO: complete and move somewhere
auto resdir = engine->getPaths()->getResources();
auto packs = engine->getContentPacks();
packs.clear();
packs.push_back(ContentPack::read(resdir/path("content/base")));
engine->loadContent();
Level* level = World::create(nameutf8, Level* level = World::create(nameutf8,
folder, folder,
seed, seed,