memory-related refactor

This commit is contained in:
MihailRis 2024-05-25 09:26:21 +03:00
parent d437b67580
commit 43903bb378
9 changed files with 41 additions and 36 deletions

View File

@ -18,8 +18,8 @@ Texture* Assets::getTexture(std::string name) const {
return found->second.get();
}
void Assets::store(Texture* texture, std::string name){
textures.emplace(name, texture);
void Assets::store(std::unique_ptr<Texture> texture, std::string name){
textures.emplace(name, std::move(texture));
}
@ -30,8 +30,8 @@ Shader* Assets::getShader(std::string name) const{
return found->second.get();
}
void Assets::store(Shader* shader, std::string name){
shaders.emplace(name, shader);
void Assets::store(std::unique_ptr<Shader> shader, std::string name){
shaders.emplace(name, std::move(shader));
}
Font* Assets::getFont(std::string name) const {
@ -41,8 +41,8 @@ Font* Assets::getFont(std::string name) const {
return found->second.get();
}
void Assets::store(Font* font, std::string name){
fonts.emplace(name, font);
void Assets::store(std::unique_ptr<Font> font, std::string name){
fonts.emplace(name, std::move(font));
}
Atlas* Assets::getAtlas(std::string name) const {
@ -52,8 +52,8 @@ Atlas* Assets::getAtlas(std::string name) const {
return found->second.get();
}
void Assets::store(Atlas* atlas, std::string name){
atlases.emplace(name, atlas);
void Assets::store(std::unique_ptr<Atlas> atlas, std::string name){
atlases.emplace(name, std::move(atlas));
}
audio::Sound* Assets::getSound(std::string name) const {
@ -63,8 +63,8 @@ audio::Sound* Assets::getSound(std::string name) const {
return found->second.get();
}
void Assets::store(audio::Sound* sound, std::string name) {
sounds.emplace(name, sound);
void Assets::store(std::unique_ptr<audio::Sound> sound, std::string name) {
sounds.emplace(name, std::move(sound));
}
const std::vector<TextureAnimation>& Assets::getAnimations() {
@ -82,6 +82,6 @@ UiDocument* Assets::getLayout(std::string name) const {
return found->second.get();
}
void Assets::store(UiDocument* layout, std::string name) {
layouts[name] = std::shared_ptr<UiDocument>(layout);
void Assets::store(std::unique_ptr<UiDocument> layout, std::string name) {
layouts[name] = std::shared_ptr<UiDocument>(std::move(layout));
}

View File

@ -39,25 +39,25 @@ public:
~Assets();
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;
void store(Shader* shader, std::string name);
void store(std::unique_ptr<Shader> shader, std::string name);
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;
void store(Atlas* atlas, std::string name);
void store(std::unique_ptr<Atlas> atlas, std::string name);
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();
void store(const TextureAnimation& animation);
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_

View File

@ -218,7 +218,7 @@ bool AssetsLoader::loadExternalTexture(
if (fs::exists(path)) {
try {
auto image = imageio::read(path.string());
assets->store(Texture::from(image.get()).release(), name);
assets->store(Texture::from(image.get()), name);
return true;
} catch (const std::exception& err) {
logger.error() << "error while loading external "

View File

@ -44,7 +44,7 @@ assetload::postfunc assetload::texture(
imageio::read(paths->find(filename+".png").u8string()).release()
);
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();
return [=](auto assets) {
atlas->prepare();
assets->store(atlas, name);
assets->store(std::unique_ptr<Atlas>(atlas), name);
for (const auto& file : names) {
animation(assets, paths, name, directory, file, atlas);
}
@ -129,7 +129,7 @@ assetload::postfunc assetload::font(
for (auto& page : *pages) {
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) {
try {
auto cfg = std::dynamic_pointer_cast<LayoutCfg>(config);
auto document = UiDocument::read(cfg->env, name, file);
assets->store(document.release(), name);
assets->store(UiDocument::read(cfg->env, name, file), name);
} catch (const parsing_error& err) {
throw std::runtime_error(
"failed to parse layout XML '"+file+"':\n"+err.errorLog()
@ -190,7 +189,7 @@ assetload::postfunc assetload::sound(
}
auto sound = baseSound.release();
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(
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);
return true;
}

View File

@ -19,7 +19,7 @@ LevelFrontend::LevelFrontend(LevelController* controller, Assets* assets)
contentCache(std::make_unique<ContentGfxCache>(level->content, assets))
{
assets->store(
BlocksPreview::build(contentCache.get(), assets, level->content).release(),
BlocksPreview::build(contentCache.get(), assets, level->content),
"block-previews"
);
controller->getPlayerController()->listenBlockInteraction(

View File

@ -62,9 +62,11 @@ gui::page_loader_func menus::create_page_loader(Engine* engine) {
auto file = engine->getResPaths()->find("layouts/pages/"+name+".xml");
auto fullname = "core:pages/"+name;
auto document = UiDocument::read(scripting::get_root_environment(), fullname, file).release();
engine->getAssets()->store(document, fullname);
auto document_ptr = UiDocument::read(
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));
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 fullname = "core:layouts/"+name;
auto document = UiDocument::read(scripting::get_root_environment(), fullname, file).release();
engine->getAssets()->store(document, fullname);
auto document_ptr = UiDocument::read(
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));
menu->addPage(name, document->getRoot());
menu->setPage(name);

View File

@ -98,7 +98,7 @@ glshader compile_shader(GLenum type, const GLchar* source, const std::string& fi
return glshader(new GLuint(shader), shader_deleter);
}
Shader* Shader::create(
std::unique_ptr<Shader> Shader::create(
const std::string& vertexFile,
const std::string& fragmentFile,
const std::string& vertexCode,
@ -125,5 +125,5 @@ Shader* Shader::create(
"shader program linking failed:\n"+std::string(infoLog)
);
}
return new Shader(id);
return std::make_unique<Shader>(id);
}

View File

@ -4,6 +4,7 @@
#include "../../typedefs.hpp"
#include <string>
#include <memory>
#include <unordered_map>
#include <glm/glm.hpp>
@ -36,7 +37,7 @@ public:
/// @param vertexSource vertex shader source code
/// @param fragmentSource fragment shader source code
/// @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& fragmentFile,
const std::string& vertexSource,

View File

@ -51,7 +51,7 @@ std::shared_ptr<Menu> GUI::getMenu() {
}
void GUI::onAssetsLoad(Assets* assets) {
assets->store(new UiDocument(
assets->store(std::make_unique<UiDocument>(
"core:root",
uidocscript {},
std::dynamic_pointer_cast<gui::UINode>(container),