memory-related refactor
This commit is contained in:
+12
-12
@@ -18,8 +18,8 @@ Texture* Assets::getTexture(std::string name) const {
|
|||||||
return found->second.get();
|
return found->second.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Assets::store(Texture* texture, std::string name){
|
void Assets::store(std::unique_ptr<Texture> texture, std::string name){
|
||||||
textures.emplace(name, texture);
|
textures.emplace(name, std::move(texture));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -30,8 +30,8 @@ Shader* Assets::getShader(std::string name) const{
|
|||||||
return found->second.get();
|
return found->second.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Assets::store(Shader* shader, std::string name){
|
void Assets::store(std::unique_ptr<Shader> shader, std::string name){
|
||||||
shaders.emplace(name, shader);
|
shaders.emplace(name, std::move(shader));
|
||||||
}
|
}
|
||||||
|
|
||||||
Font* Assets::getFont(std::string name) const {
|
Font* Assets::getFont(std::string name) const {
|
||||||
@@ -41,8 +41,8 @@ Font* Assets::getFont(std::string name) const {
|
|||||||
return found->second.get();
|
return found->second.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Assets::store(Font* font, std::string name){
|
void Assets::store(std::unique_ptr<Font> font, std::string name){
|
||||||
fonts.emplace(name, font);
|
fonts.emplace(name, std::move(font));
|
||||||
}
|
}
|
||||||
|
|
||||||
Atlas* Assets::getAtlas(std::string name) const {
|
Atlas* Assets::getAtlas(std::string name) const {
|
||||||
@@ -52,8 +52,8 @@ Atlas* Assets::getAtlas(std::string name) const {
|
|||||||
return found->second.get();
|
return found->second.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Assets::store(Atlas* atlas, std::string name){
|
void Assets::store(std::unique_ptr<Atlas> atlas, std::string name){
|
||||||
atlases.emplace(name, atlas);
|
atlases.emplace(name, std::move(atlas));
|
||||||
}
|
}
|
||||||
|
|
||||||
audio::Sound* Assets::getSound(std::string name) const {
|
audio::Sound* Assets::getSound(std::string name) const {
|
||||||
@@ -63,8 +63,8 @@ audio::Sound* Assets::getSound(std::string name) const {
|
|||||||
return found->second.get();
|
return found->second.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Assets::store(audio::Sound* sound, std::string name) {
|
void Assets::store(std::unique_ptr<audio::Sound> sound, std::string name) {
|
||||||
sounds.emplace(name, sound);
|
sounds.emplace(name, std::move(sound));
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<TextureAnimation>& Assets::getAnimations() {
|
const std::vector<TextureAnimation>& Assets::getAnimations() {
|
||||||
@@ -82,6 +82,6 @@ UiDocument* Assets::getLayout(std::string name) const {
|
|||||||
return found->second.get();
|
return found->second.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Assets::store(UiDocument* layout, std::string name) {
|
void Assets::store(std::unique_ptr<UiDocument> layout, std::string name) {
|
||||||
layouts[name] = std::shared_ptr<UiDocument>(layout);
|
layouts[name] = std::shared_ptr<UiDocument>(std::move(layout));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,25 +39,25 @@ public:
|
|||||||
~Assets();
|
~Assets();
|
||||||
|
|
||||||
Texture* getTexture(std::string name) const;
|
Texture* getTexture(std::string name) const;
|
||||||
void store(Texture* texture, std::string name);
|
void store(std::unique_ptr<Texture> texture, std::string name);
|
||||||
|
|
||||||
Shader* getShader(std::string name) const;
|
Shader* getShader(std::string name) const;
|
||||||
void store(Shader* shader, std::string name);
|
void store(std::unique_ptr<Shader> shader, std::string name);
|
||||||
|
|
||||||
Font* getFont(std::string name) const;
|
Font* getFont(std::string name) const;
|
||||||
void store(Font* font, std::string name);
|
void store(std::unique_ptr<Font> font, std::string name);
|
||||||
|
|
||||||
Atlas* getAtlas(std::string name) const;
|
Atlas* getAtlas(std::string name) const;
|
||||||
void store(Atlas* atlas, std::string name);
|
void store(std::unique_ptr<Atlas> atlas, std::string name);
|
||||||
|
|
||||||
audio::Sound* getSound(std::string name) const;
|
audio::Sound* getSound(std::string name) const;
|
||||||
void store(audio::Sound* sound, std::string name);
|
void store(std::unique_ptr<audio::Sound> sound, std::string name);
|
||||||
|
|
||||||
const std::vector<TextureAnimation>& getAnimations();
|
const std::vector<TextureAnimation>& getAnimations();
|
||||||
void store(const TextureAnimation& animation);
|
void store(const TextureAnimation& animation);
|
||||||
|
|
||||||
UiDocument* getLayout(std::string name) const;
|
UiDocument* getLayout(std::string name) const;
|
||||||
void store(UiDocument* layout, std::string name);
|
void store(std::unique_ptr<UiDocument> layout, std::string name);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ASSETS_ASSETS_HPP_
|
#endif // ASSETS_ASSETS_HPP_
|
||||||
|
|||||||
@@ -218,7 +218,7 @@ bool AssetsLoader::loadExternalTexture(
|
|||||||
if (fs::exists(path)) {
|
if (fs::exists(path)) {
|
||||||
try {
|
try {
|
||||||
auto image = imageio::read(path.string());
|
auto image = imageio::read(path.string());
|
||||||
assets->store(Texture::from(image.get()).release(), name);
|
assets->store(Texture::from(image.get()), name);
|
||||||
return true;
|
return true;
|
||||||
} catch (const std::exception& err) {
|
} catch (const std::exception& err) {
|
||||||
logger.error() << "error while loading external "
|
logger.error() << "error while loading external "
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ assetload::postfunc assetload::texture(
|
|||||||
imageio::read(paths->find(filename+".png").u8string()).release()
|
imageio::read(paths->find(filename+".png").u8string()).release()
|
||||||
);
|
);
|
||||||
return [name, image](auto assets) {
|
return [name, image](auto assets) {
|
||||||
assets->store(Texture::from(image.get()).release(), name);
|
assets->store(Texture::from(image.get()), name);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,7 +103,7 @@ assetload::postfunc assetload::atlas(
|
|||||||
Atlas* atlas = builder.build(2, false).release();
|
Atlas* atlas = builder.build(2, false).release();
|
||||||
return [=](auto assets) {
|
return [=](auto assets) {
|
||||||
atlas->prepare();
|
atlas->prepare();
|
||||||
assets->store(atlas, name);
|
assets->store(std::unique_ptr<Atlas>(atlas), name);
|
||||||
for (const auto& file : names) {
|
for (const auto& file : names) {
|
||||||
animation(assets, paths, name, directory, file, atlas);
|
animation(assets, paths, name, directory, file, atlas);
|
||||||
}
|
}
|
||||||
@@ -129,7 +129,7 @@ assetload::postfunc assetload::font(
|
|||||||
for (auto& page : *pages) {
|
for (auto& page : *pages) {
|
||||||
textures.emplace_back(Texture::from(page.get()));
|
textures.emplace_back(Texture::from(page.get()));
|
||||||
}
|
}
|
||||||
assets->store(new Font(std::move(textures), res, 4), name);
|
assets->store(std::make_unique<Font>(std::move(textures), res, 4), name);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,8 +143,7 @@ assetload::postfunc assetload::layout(
|
|||||||
return [=](auto assets) {
|
return [=](auto assets) {
|
||||||
try {
|
try {
|
||||||
auto cfg = std::dynamic_pointer_cast<LayoutCfg>(config);
|
auto cfg = std::dynamic_pointer_cast<LayoutCfg>(config);
|
||||||
auto document = UiDocument::read(cfg->env, name, file);
|
assets->store(UiDocument::read(cfg->env, name, file), name);
|
||||||
assets->store(document.release(), name);
|
|
||||||
} catch (const parsing_error& err) {
|
} catch (const parsing_error& err) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"failed to parse layout XML '"+file+"':\n"+err.errorLog()
|
"failed to parse layout XML '"+file+"':\n"+err.errorLog()
|
||||||
@@ -190,7 +189,7 @@ assetload::postfunc assetload::sound(
|
|||||||
}
|
}
|
||||||
auto sound = baseSound.release();
|
auto sound = baseSound.release();
|
||||||
return [=](auto assets) {
|
return [=](auto assets) {
|
||||||
assets->store(sound, name);
|
assets->store(std::unique_ptr<audio::Sound>(sound), name);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,7 +304,7 @@ static bool animation(
|
|||||||
auto animation = create_animation(
|
auto animation = create_animation(
|
||||||
srcAtlas.get(), dstAtlas, name, builder.getNames(), frameList
|
srcAtlas.get(), dstAtlas, name, builder.getNames(), frameList
|
||||||
);
|
);
|
||||||
assets->store(srcAtlas.release(), atlasName + "/" + name + "_animation");
|
assets->store(std::move(srcAtlas), atlasName + "/" + name + "_animation");
|
||||||
assets->store(animation);
|
assets->store(animation);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ LevelFrontend::LevelFrontend(LevelController* controller, Assets* assets)
|
|||||||
contentCache(std::make_unique<ContentGfxCache>(level->content, assets))
|
contentCache(std::make_unique<ContentGfxCache>(level->content, assets))
|
||||||
{
|
{
|
||||||
assets->store(
|
assets->store(
|
||||||
BlocksPreview::build(contentCache.get(), assets, level->content).release(),
|
BlocksPreview::build(contentCache.get(), assets, level->content),
|
||||||
"block-previews"
|
"block-previews"
|
||||||
);
|
);
|
||||||
controller->getPlayerController()->listenBlockInteraction(
|
controller->getPlayerController()->listenBlockInteraction(
|
||||||
|
|||||||
+10
-5
@@ -62,9 +62,11 @@ gui::page_loader_func menus::create_page_loader(Engine* engine) {
|
|||||||
auto file = engine->getResPaths()->find("layouts/pages/"+name+".xml");
|
auto file = engine->getResPaths()->find("layouts/pages/"+name+".xml");
|
||||||
auto fullname = "core:pages/"+name;
|
auto fullname = "core:pages/"+name;
|
||||||
|
|
||||||
auto document = UiDocument::read(scripting::get_root_environment(), fullname, file).release();
|
auto document_ptr = UiDocument::read(
|
||||||
engine->getAssets()->store(document, fullname);
|
scripting::get_root_environment(), fullname, file
|
||||||
|
);
|
||||||
|
auto document = document_ptr.get();
|
||||||
|
engine->getAssets()->store(std::move(document_ptr), fullname);
|
||||||
scripting::on_ui_open(document, std::move(args));
|
scripting::on_ui_open(document, std::move(args));
|
||||||
return document->getRoot();
|
return document->getRoot();
|
||||||
};
|
};
|
||||||
@@ -75,8 +77,11 @@ UiDocument* menus::show(Engine* engine, const std::string& name, std::vector<dyn
|
|||||||
auto file = engine->getResPaths()->find("layouts/"+name+".xml");
|
auto file = engine->getResPaths()->find("layouts/"+name+".xml");
|
||||||
auto fullname = "core:layouts/"+name;
|
auto fullname = "core:layouts/"+name;
|
||||||
|
|
||||||
auto document = UiDocument::read(scripting::get_root_environment(), fullname, file).release();
|
auto document_ptr = UiDocument::read(
|
||||||
engine->getAssets()->store(document, fullname);
|
scripting::get_root_environment(), fullname, file
|
||||||
|
);
|
||||||
|
auto document = document_ptr.get();
|
||||||
|
engine->getAssets()->store(std::move(document_ptr), fullname);
|
||||||
scripting::on_ui_open(document, std::move(args));
|
scripting::on_ui_open(document, std::move(args));
|
||||||
menu->addPage(name, document->getRoot());
|
menu->addPage(name, document->getRoot());
|
||||||
menu->setPage(name);
|
menu->setPage(name);
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ glshader compile_shader(GLenum type, const GLchar* source, const std::string& fi
|
|||||||
return glshader(new GLuint(shader), shader_deleter);
|
return glshader(new GLuint(shader), shader_deleter);
|
||||||
}
|
}
|
||||||
|
|
||||||
Shader* Shader::create(
|
std::unique_ptr<Shader> Shader::create(
|
||||||
const std::string& vertexFile,
|
const std::string& vertexFile,
|
||||||
const std::string& fragmentFile,
|
const std::string& fragmentFile,
|
||||||
const std::string& vertexCode,
|
const std::string& vertexCode,
|
||||||
@@ -125,5 +125,5 @@ Shader* Shader::create(
|
|||||||
"shader program linking failed:\n"+std::string(infoLog)
|
"shader program linking failed:\n"+std::string(infoLog)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return new Shader(id);
|
return std::make_unique<Shader>(id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "../../typedefs.hpp"
|
#include "../../typedefs.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
@@ -36,7 +37,7 @@ public:
|
|||||||
/// @param vertexSource vertex shader source code
|
/// @param vertexSource vertex shader source code
|
||||||
/// @param fragmentSource fragment shader source code
|
/// @param fragmentSource fragment shader source code
|
||||||
/// @return linked shader program containing vertex and fragment shaders
|
/// @return linked shader program containing vertex and fragment shaders
|
||||||
static Shader* create(
|
static std::unique_ptr<Shader> create(
|
||||||
const std::string& vertexFile,
|
const std::string& vertexFile,
|
||||||
const std::string& fragmentFile,
|
const std::string& fragmentFile,
|
||||||
const std::string& vertexSource,
|
const std::string& vertexSource,
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ std::shared_ptr<Menu> GUI::getMenu() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GUI::onAssetsLoad(Assets* assets) {
|
void GUI::onAssetsLoad(Assets* assets) {
|
||||||
assets->store(new UiDocument(
|
assets->store(std::make_unique<UiDocument>(
|
||||||
"core:root",
|
"core:root",
|
||||||
uidocscript {},
|
uidocscript {},
|
||||||
std::dynamic_pointer_cast<gui::UINode>(container),
|
std::dynamic_pointer_cast<gui::UINode>(container),
|
||||||
|
|||||||
Reference in New Issue
Block a user