add background assets loader

This commit is contained in:
MihailRis 2026-01-23 22:40:57 +03:00
parent 245e040a1c
commit 0cf7d9b1ed
9 changed files with 104 additions and 46 deletions

View file

@ -1,13 +1,12 @@
#include "AssetsManagement.hpp"
#include "assets/Assets.hpp"
#include "assets/AssetsLoader.hpp"
#include "engine/Engine.hpp"
#include "debug/Logger.hpp"
#include "content/Content.hpp"
#include "graphics/core/Shader.hpp"
#include "EnginePaths.hpp"
#include "coders/GLSLExtension.hpp"
#include "content/Content.hpp"
#include "debug/Logger.hpp"
#include "engine/Engine.hpp"
#include "EnginePaths.hpp"
#include "graphics/core/Shader.hpp"
#include "graphics/render/ModelsGenerator.hpp"
#include "graphics/ui/GUI.hpp"
@ -15,6 +14,10 @@ static debug::Logger logger("assets-management");
AssetsManagement::AssetsManagement(Engine& engine) : engine(engine) {}
AssetsManagement::~AssetsManagement() {
finishBackgroundLoader();
}
const Assets* AssetsManagement::getStorage() const {
return assets.get();
}
@ -23,7 +26,23 @@ Assets* AssetsManagement::getStorage() {
return assets.get();
}
AssetsLoader& AssetsManagement::acquireBackgroundLoader() {
if (backgroundLoader) {
return *backgroundLoader;
}
if (assets == nullptr) {
throw std::runtime_error("no assets storage available");
}
backgroundLoader = std::make_unique<AssetsLoader>(
engine, *assets, engine.getResPaths()
);
backgroundLoaderTask = backgroundLoader->startTask(nullptr);
return *backgroundLoader;
}
void AssetsManagement::loadAssets(Content* content) {
finishBackgroundLoader();
const auto& paths = engine.getPaths();
logger.info() << "loading assets";
Shader::preprocessor->setPaths(&paths.resPaths);
@ -34,7 +53,7 @@ void AssetsManagement::loadAssets(Content* content) {
// no need
// correct log messages order is more useful
// todo: before setting to true, check if GLSLExtension thread safe
// todo: before setting to true, check if GLSLExtension thread safe
bool threading = false; // look at three upper lines
if (threading) {
auto task = loader.startTask([=](){});
@ -51,3 +70,17 @@ void AssetsManagement::loadAssets(Content* content) {
assets->setup();
engine.getGUI().onAssetsLoad(assets.get());
}
void AssetsManagement::update() {
if (backgroundLoaderTask) {
backgroundLoaderTask->update();
}
}
void AssetsManagement::finishBackgroundLoader() {
if (backgroundLoaderTask == nullptr) {
return;
}
backgroundLoaderTask.reset();
backgroundLoader.reset();
}

View file

@ -1,22 +1,31 @@
#pragma once
#include "assets/Assets.hpp"
#include <memory>
class Assets;
class AssetsLoader;
class Engine;
class Content;
class Task;
class AssetsManagement {
class AssetsManagement final {
public:
AssetsManagement(Engine& engine);
~AssetsManagement();
void loadAssets(Content* content);
void update();
Assets* getStorage();
const Assets* getStorage() const;
AssetsLoader& acquireBackgroundLoader();
private:
void finishBackgroundLoader();
Engine& engine;
std::unique_ptr<Assets> assets;
std::unique_ptr<AssetsLoader> backgroundLoader;
std::shared_ptr<Task> backgroundLoaderTask;
};

View file

@ -4,48 +4,45 @@
#define GLEW_STATIC
#endif
#include "debug/Logger.hpp"
#include "assets/AssetsLoader.hpp"
#include "AssetsManagement.hpp"
#include "audio/audio.hpp"
#include "coders/commons.hpp"
#include "coders/GLSLExtension.hpp"
#include "coders/toml.hpp"
#include "coders/commons.hpp"
#include "devtools/Editor.hpp"
#include "devtools/Project.hpp"
#include "devtools/DebuggingServer.hpp"
#include "content/ContentControl.hpp"
#include "core_defs.hpp"
#include "io/io.hpp"
#include "io/settings_io.hpp"
#include "debug/Logger.hpp"
#include "devtools/DebuggingServer.hpp"
#include "devtools/Editor.hpp"
#include "devtools/Project.hpp"
#include "EnginePaths.hpp"
#include "frontend/locale.hpp"
#include "frontend/menu.hpp"
#include "frontend/screens/Screen.hpp"
#include "graphics/render/ModelsGenerator.hpp"
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/Shader.hpp"
#include "graphics/ui/GUI.hpp"
#include "graphics/ui/elements/Menu.hpp"
#include "logic/EngineController.hpp"
#include "graphics/ui/GUI.hpp"
#include "io/io.hpp"
#include "io/settings_io.hpp"
#include "logic/CommandsInterpreter.hpp"
#include "logic/scripting/scripting.hpp"
#include "logic/EngineController.hpp"
#include "logic/scripting/scripting_hud.hpp"
#include "logic/scripting/scripting.hpp"
#include "Mainloop.hpp"
#include "network/Network.hpp"
#include "ServerMainloop.hpp"
#include "util/platform.hpp"
#include "util/stringutil.hpp"
#include "window/input.hpp"
#include "window/Window.hpp"
#include "world/Level.hpp"
#include "AssetsManagement.hpp"
#include "Mainloop.hpp"
#include "ServerMainloop.hpp"
#include "WindowControl.hpp"
#include "EnginePaths.hpp"
#include "world/Level.hpp"
#include <iostream>
#include <assert.h>
#include <glm/glm.hpp>
#include <unordered_set>
#include <functional>
#include <utility>
static debug::Logger logger("engine");
@ -266,6 +263,7 @@ void Engine::applicationTick() {
void Engine::updateFrontend() {
double delta = time.getDelta();
assets->update();
updateHotkeys();
audio::update(delta);
gui->act(delta, window->getSize());
@ -421,6 +419,10 @@ Assets* Engine::getAssets() {
return assets->getStorage();
}
AssetsLoader& Engine::acquireBackgroundLoader() {
return assets->acquireBackgroundLoader();
}
EnginePaths& Engine::getPaths() {
return *paths;
}

View file

@ -10,6 +10,7 @@
#include <string>
class Assets;
class AssetsLoader;
class AssetsManagement;
class ContentControl;
class EngineController;
@ -110,6 +111,8 @@ public:
/// @brief Get active assets storage instance
Assets* getAssets();
AssetsLoader& acquireBackgroundLoader();
/// @brief Get writeable engine settings structure instance
EngineSettings& getSettings();

View file

@ -274,6 +274,9 @@ ResPaths::ResPaths(std::vector<PathsRoot> roots)
}
io::path ResPaths::find(const std::string& filename) const {
if (filename.find(':') != std::string::npos) {
return filename;
}
for (int i = roots.size() - 1; i >= 0; i--) {
auto& root = roots[i];
auto file = root.path / filename;