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"
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 {
auto found = textures.find(name);
if (found == textures.end())
return nullptr;
return found->second;
return found->second.get();
}
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);
if (found == shaders.end())
return nullptr;
return found->second;
return found->second.get();
}
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);
if (found == fonts.end())
return nullptr;
return found->second;
return found->second.get();
}
void Assets::store(Font* font, std::string name){
fonts[name] = font;
fonts[name].reset(font);
}
Atlas* Assets::getAtlas(std::string name) const {
auto found = atlases.find(name);
if (found == atlases.end())
return nullptr;
return found->second;
return found->second.get();
}
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_
#include <string>
#include <memory>
#include <unordered_map>
class Texture;
@ -10,10 +11,10 @@ class Font;
class Atlas;
class Assets {
std::unordered_map<std::string, Texture*> textures;
std::unordered_map<std::string, Shader*> shaders;
std::unordered_map<std::string, Font*> fonts;
std::unordered_map<std::string, Atlas*> atlases;
std::unordered_map<std::string, std::shared_ptr<Texture>> textures;
std::unordered_map<std::string, std::shared_ptr<Shader>> shaders;
std::unordered_map<std::string, std::shared_ptr<Font>> fonts;
std::unordered_map<std::string, std::shared_ptr<Atlas>> atlases;
public:
~Assets();
Texture* getTexture(std::string name) const;
@ -27,6 +28,8 @@ public:
Atlas* getAtlas(std::string name) const;
void store(Atlas* atlas, std::string name);
void extend(const Assets& assets);
};
#endif /* ASSETS_ASSETS_H_ */

View File

@ -51,8 +51,23 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
}
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();
gui = new GUI();
@ -169,16 +184,16 @@ void Engine::loadContent() {
Shader::preprocessor->setPaths(resPaths.get());
assets.reset(new Assets());
unique_ptr<Assets> new_assets(new Assets());
std::cout << "-- loading assets" << std::endl;
AssetsLoader loader(assets.get(), resPaths.get());
AssetsLoader::createDefaults(loader);
AssetsLoader::addDefaults(loader);
AssetsLoader loader(new_assets.get(), resPaths.get());
AssetsLoader::createDefaults(loader);
AssetsLoader::addDefaults(loader);
while (loader.hasNext()) {
if (!loader.loadNext()) {
assets.reset();
Window::terminate();
throw initialize_error("could not to initialize assets");
new_assets.reset();
throw std::runtime_error("could not to load 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));
button->color(vec4(1.0f, 1.0f, 1.0f, 0.1f));
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& settings = engine->getSettings();
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);
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,
folder,
seed,