Merge pull request #773 from MihailRis/background-assets-loader

background assets loader
This commit is contained in:
MihailRis 2026-01-28 20:27:07 +03:00 • committed by GitHub
commit fb0ad19a65
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 311 additions and 107 deletions

View file

@ -5,6 +5,13 @@ A library for working with audio/visual assets.
## Functions
```lua
-- Requests background texture loading
assets.request_texture(
-- Image file
filename: string,
-- Texture name after loading
name: string,
)
-- Loads a texture
assets.load_texture(
-- Array of bytes of an image file

View file

@ -167,10 +167,11 @@ Properties:
Properties:
| Name | Type | Read | Write | Description |
| ------ | ------ | ---- | ----- | ---------------- |
| src | string | yes | yes | texture name |
| region | vec4 | yes | yes | image sub-region |
| Name | Type | Read | Write | Description |
| -------- | ------ | ---- | ----- | ---------------- |
| src | string | yes | yes | texture name |
| fallback | string | yes | yes | fallback texture |
| region | vec4 | yes | yes | image sub-region |
## Canvas

View file

@ -120,6 +120,7 @@ Inner text is a button text.
- `src` - name of an image stored in textures folder. Extension is not specified. Type: string.
Example: *gui/error*
- `fallback` - a fallback texture displayed when the main texture is missing / loading.
- `region` - image region x1, y1, x2, y2 from 0.0, 0.0 (upper left corner), 1.0, 1.0 (lower right corner)
## *canvas*

View file

@ -5,6 +5,14 @@
## Функции
```lua
-- Запрашивает фоновую загрузку текстуры
assets.request_texture(
-- Файл изображения
filename: string,
-- Имя текстуры после загрузки
name: string,
)
-- Загружает текстуру
assets.load_texture(
-- Массив байт файла изображения
@ -33,4 +41,4 @@ assets.to_canvas(
-- так и находящиеся в атласе ("атлас:имя_текстуры").
name: string
) -> Canvas
```
```

View file

@ -170,6 +170,7 @@ document["worlds-panel"]:clear()
| Название | Тип | Чтение | Запись | Описание |
| -------- | ------ | ------ | ------ | ---------------------- |
| src | string | да | да | отображаемая текстура |
| fallback | string | да | да | резервная текстура |
| region | vec4 | да | да | под-регион изображения |
## Холст (canvas)

View file

@ -121,6 +121,7 @@
## Изображение - *image*
- `src` - имя изображения в папке textures без указания расширения. Тип: строка. Например `gui/error`
- `fallback` - резервная текстура, отображаемая при отсутствии / фоновой загрузке основной
- `region` - под-регион изображения x1, y1, x2, y2 от 0.0, 0.0 (левый верхний угол), 1.0, 1.0 (правый нижний угол)
## Холст - *canvas*

View file

@ -11,5 +11,5 @@
<label ifnot='%{error}' pos='80,28' size='355,50' multiline='true' color='#FFFFFFB2'>
%{description}
</label>
<image pos='8,8' src='%{icon}' size='64'/>
<image pos='8,8' src='%{icon}' fallback='gui/no_icon' size='64'/>
</container>

View file

@ -4,7 +4,7 @@
hover-color='#162B3399'
onclick='app.open_world("%{name}")'
>
<image pos='1' src='%{icon}' size='96,64'></image>
<image pos='1' src='%{icon}' fallback='gui/no_world_icon' size='96,64'></image>
<label pos='104,8'>%{name}</label>
<button margin='4'
gravity='center-right'

View file

@ -319,28 +319,31 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) {
}
bool AssetsLoader::loadExternalTexture(
Assets* assets,
AssetsLoader& loader,
const std::string& name,
const std::vector<io::path>& alternatives
) {
if (assets->get<Texture>(name) != nullptr) {
if (loader.getAssets().get<Texture>(name) != nullptr) {
return true;
}
for (auto& path : alternatives) {
if (io::exists(path)) {
try {
auto image = imageio::read(path);
assets->store(Texture::from(image.get()), name);
return true;
} catch (const std::exception& err) {
logger.error() << "error while loading external "
<< path.string() << ": " << err.what();
}
loader.add(
AssetType::TEXTURE,
(path.parent() / path.stem()).string(),
name,
nullptr
);
return true;
}
}
return false;
}
Assets& AssetsLoader::getAssets() {
return assets;
}
Engine& AssetsLoader::getEngine() {
return engine;
}
@ -368,14 +371,24 @@ public:
}
};
std::shared_ptr<Task> AssetsLoader::startTask(runnable onDone) {
std::shared_ptr<Task> AssetsLoader::startTask(runnable onDone, int maxWorkers) {
auto pool =
std::make_shared<util::ThreadPool<aloader_entry, assetload::postfunc>>(
"assets-loader-pool",
[=]() { return std::make_shared<LoaderWorker>(this); },
[this](const assetload::postfunc& func) { func(&assets); }
[this](const assetload::postfunc& func) { func(&assets); },
maxWorkers
);
pool->setOnComplete(std::move(onDone));
pool->setJobsSource([this]() -> std::optional<aloader_entry> {
if (entries.empty()) {
return std::nullopt;
}
aloader_entry entry = std::move(entries.front());
entries.pop();
return entry;
});
while (!entries.empty()) {
aloader_entry entry = std::move(entries.front());
entries.pop();

View file

@ -113,7 +113,7 @@ public:
/// @throws assetload::error
void loadNext();
std::shared_ptr<Task> startTask(runnable onDone);
std::shared_ptr<Task> startTask(runnable onDone, int maxWorkers);
const ResPaths& getPaths() const;
aloader_func getLoader(AssetType tag);
@ -124,10 +124,11 @@ public:
static void addDefaults(AssetsLoader& loader, const Content* content);
static bool loadExternalTexture(
Assets* assets,
AssetsLoader& loader,
const std::string& name,
const std::vector<io::path>& alternatives
);
Assets& getAssets();
Engine& getEngine();
};

View file

@ -0,0 +1,92 @@
#include "AssetsManagement.hpp"
#include "assets/AssetsLoader.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"
static debug::Logger logger("assets-management");
AssetsManagement::AssetsManagement(Engine& engine)
: engine(engine), settings(engine.getSettings()) {
}
AssetsManagement::~AssetsManagement() {
finishBackgroundLoader();
}
const Assets* AssetsManagement::getStorage() const {
return assets.get();
}
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, settings.system.maxBgAssetLoaders.get()
);
return *backgroundLoader;
}
void AssetsManagement::loadAssets(Content* content) {
finishBackgroundLoader();
const auto& paths = engine.getPaths();
logger.info() << "loading assets";
Shader::preprocessor->setPaths(&paths.resPaths);
auto new_assets = std::make_unique<Assets>();
AssetsLoader loader(engine, *new_assets, paths.resPaths);
AssetsLoader::addDefaults(loader, content);
// no need
// correct log messages order is more useful
// todo: before setting to true, check if GLSLExtension thread safe
bool threading = false; // look at three upper lines
if (threading) {
auto task = loader.startTask(
[=]() {}, 0
);
task->waitForEnd();
} else {
while (loader.hasNext()) {
loader.loadNext();
}
}
assets = std::move(new_assets);
if (content) {
ModelsGenerator::prepare(*content, *assets);
}
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

@ -0,0 +1,33 @@
#pragma once
#include "assets/Assets.hpp"
#include <memory>
class AssetsLoader;
class Engine;
class Content;
class Task;
struct EngineSettings;
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;
const EngineSettings& settings;
std::unique_ptr<Assets> assets;
std::unique_ptr<AssetsLoader> backgroundLoader;
std::shared_ptr<Task> backgroundLoaderTask;
};

View file

@ -4,47 +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 "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");
@ -84,6 +82,7 @@ void Engine::onContentLoad() {
}
void Engine::initializeClient() {
assets = std::make_unique<AssetsManagement>(*this);
windowControl = std::make_unique<WindowControl>(*this);
auto [window, input] = windowControl->initialize();
@ -264,6 +263,7 @@ void Engine::applicationTick() {
void Engine::updateFrontend() {
double delta = time.getDelta();
assets->update();
updateHotkeys();
audio::update(delta);
gui->act(delta, window->getSize());
@ -305,7 +305,7 @@ void Engine::renderFrame() {
screen->draw(time.getDelta());
DrawContext ctx(nullptr, *window, nullptr);
gui->draw(ctx, *assets);
gui->draw(ctx, *assets->getStorage());
}
void Engine::saveSettings() {
@ -363,33 +363,7 @@ void Engine::setLevelConsumer(OnWorldOpen levelConsumer) {
}
void Engine::loadAssets() {
logger.info() << "loading assets";
Shader::preprocessor->setPaths(&paths->resPaths);
auto content = this->content->get();
auto new_assets = std::make_unique<Assets>();
AssetsLoader loader(*this, *new_assets, paths->resPaths);
AssetsLoader::addDefaults(loader, content);
// no need
// correct log messages order is more useful
// todo: before setting to true, check if GLSLExtension thread safe
bool threading = false; // look at three upper lines
if (threading) {
auto task = loader.startTask([=](){});
task->waitForEnd();
} else {
while (loader.hasNext()) {
loader.loadNext();
}
}
assets = std::move(new_assets);
if (content) {
ModelsGenerator::prepare(*content, *assets);
}
assets->setup();
gui->onAssetsLoad(assets.get());
assets->loadAssets(content->get());
}
void Engine::loadProject() {
@ -442,7 +416,11 @@ EngineSettings& Engine::getSettings() {
}
Assets* Engine::getAssets() {
return assets.get();
return assets->getStorage();
}
AssetsLoader& Engine::acquireBackgroundLoader() {
return assets->acquireBackgroundLoader();
}
EnginePaths& Engine::getPaths() {

View file

@ -10,6 +10,8 @@
#include <string>
class Assets;
class AssetsLoader;
class AssetsManagement;
class ContentControl;
class EngineController;
class EnginePaths;
@ -52,7 +54,7 @@ class Engine : public util::ObjectsKeeper {
std::unique_ptr<EnginePaths> paths;
std::unique_ptr<Project> project;
std::unique_ptr<SettingsHandler> settingsHandler;
std::unique_ptr<Assets> assets;
std::unique_ptr<AssetsManagement> assets;
std::shared_ptr<Screen> screen;
std::unique_ptr<ContentControl> content;
std::unique_ptr<EngineController> controller;
@ -109,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;

View file

@ -3,6 +3,7 @@
#include <utility>
#include "assets/Assets.hpp"
#include "assets/assets_util.hpp"
#include "graphics/core/Atlas.hpp"
#include "graphics/core/Batch2D.hpp"
#include "graphics/core/DrawContext.hpp"
@ -16,37 +17,25 @@ Image::Image(GUI& gui, std::string texture, glm::vec2 size)
setInteractive(false);
}
util::TextureRegion Image::refreshTexture(const Assets& assets) {
auto region = util::get_texture_region(assets, texture, fallback);
if (region.texture && autoresize) {
setSize(glm::vec2(
region.texture->getWidth() * region.region.getWidth(),
region.texture->getHeight() * region.region.getHeight()
));
}
return region;
}
void Image::draw(const DrawContext& pctx, const Assets& assets) {
glm::vec2 pos = calcPos();
auto batch = pctx.getBatch2D();
Texture* texture = nullptr;
auto separator = this->texture.find(':');
if (separator == std::string::npos) {
texture = assets.get<Texture>(this->texture);
batch->texture(texture);
if (texture && autoresize) {
setSize(glm::vec2(texture->getWidth(), texture->getHeight()));
}
} else {
auto atlasName = this->texture.substr(0, separator);
if (auto atlas = assets.get<Atlas>(atlasName)) {
if (auto region =
atlas->getIf(this->texture.substr(separator + 1))) {
texture = atlas->getTexture();
batch->texture(atlas->getTexture());
batch->setRegion(*region);
if (autoresize) {
setSize(glm::vec2(
texture->getWidth() * region->getWidth(),
texture->getHeight() * region->getHeight()
));
}
} else {
batch->texture(nullptr);
}
}
}
auto textureRegion = refreshTexture(assets);
batch->setRegion(textureRegion.region);
batch->texture(textureRegion.texture);
batch->rect(
pos.x,
pos.y,
@ -73,10 +62,18 @@ const std::string& Image::getTexture() const {
return texture;
}
const std::string& Image::getFallback() const {
return fallback;
}
void Image::setTexture(const std::string& name) {
texture = name;
}
void Image::setFallback(const std::string& name) {
fallback = name;
}
void Image::setRegion(const UVRegion& region) {
this->region = region;
}

View file

@ -3,21 +3,30 @@
#include "UINode.hpp"
#include "maths/UVRegion.hpp"
namespace util {
struct TextureRegion;
}
namespace gui {
class Image : public UINode {
class Image final : public UINode {
protected:
std::string texture;
std::string fallback;
UVRegion region {};
bool autoresize = false;
util::TextureRegion refreshTexture(const Assets& assets);
public:
Image(GUI& gui, std::string texture, glm::vec2 size=glm::vec2(32,32));
virtual void draw(const DrawContext& pctx, const Assets& assets) override;
void draw(const DrawContext& pctx, const Assets& assets) override;
virtual void setAutoResize(bool flag);
virtual bool isAutoResize() const;
virtual const std::string& getTexture() const;
virtual void setTexture(const std::string& name);
void setAutoResize(bool flag);
bool isAutoResize() const;
const std::string& getTexture() const;
const std::string& getFallback() const;
void setTexture(const std::string& name);
void setFallback(const std::string& name);
void setRegion(const UVRegion& region);
const UVRegion& getRegion() const;
};

View file

@ -632,8 +632,10 @@ static std::shared_ptr<UINode> read_image(
const UiXmlReader& reader, const xml::xmlelement& element
) {
std::string src = element.attr("src", "").getText();
auto image = std::make_shared<Image>(reader.getGUI(), src);
std::string fallback = element.attr("fallback", "").getText();
auto image = std::make_shared<Image>(reader.getGUI(), std::move(src));
read_uinode(reader, element, *image);
image->setFallback(std::move(fallback));
if (element.has("region")) {
auto vec = element.attr("region").asVec4();

View file

@ -94,6 +94,9 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) {
builder.add("do-write-lights", &settings.debug.doWriteLights);
builder.add("do-trace-shaders", &settings.debug.doTraceShaders);
builder.add("enable-experimental", &settings.debug.enableExperimental);
builder.addSection("system");
builder.add("max-bg-asset-loaders", &settings.system.maxBgAssetLoaders);
}
dv::value SettingsHandler::getValue(const std::string& name) const {

View file

@ -1,6 +1,7 @@
#include "api_lua.hpp"
#include "assets/Assets.hpp"
#include "assets/AssetsLoader.hpp"
#include "coders/png.hpp"
#include "coders/vcm.hpp"
#include "debug/Logger.hpp"
@ -24,6 +25,14 @@ static void load_texture(
}
}
static int l_request_texture(lua::State* L) {
std::string filename = lua::require_string(L, 1);
std::string alias = lua::require_string(L, 2);
auto& loader = engine->acquireBackgroundLoader();
loader.add(AssetType::TEXTURE, filename, alias);
return 0;
}
static int l_load_texture(lua::State* L) {
if (lua::isstring(L, 3) && lua::require_lstring(L, 3) != "png") {
throw std::runtime_error("unsupportd image format");
@ -108,6 +117,7 @@ static int l_to_canvas(lua::State* L) {
}
const luaL_Reg assetslib[] = {
{"request_texture", lua::wrap<l_request_texture>},
{"load_texture", lua::wrap<l_load_texture>},
{"parse_model", lua::wrap<l_parse_model>},
{"to_canvas", lua::wrap<l_to_canvas>},

View file

@ -397,6 +397,13 @@ static int p_get_src(UINode* node, lua::State* L) {
return 0;
}
static int p_get_fallback(UINode* node, lua::State* L) {
if (auto image = dynamic_cast<Image*>(node)) {
return lua::pushstring(L, image->getFallback());
}
return 0;
}
static int p_get_region(UINode* node, lua::State* L) {
if (auto image = dynamic_cast<Image*>(node)) {
const auto& region = image->getRegion();
@ -663,6 +670,7 @@ static int l_gui_getattr(lua::State* L) {
{"syntax", p_get_syntax},
{"markup", p_get_markup},
{"src", p_get_src},
{"fallback", p_get_fallback},
{"value", p_get_value},
{"min", p_get_min},
{"max", p_get_max},
@ -790,6 +798,11 @@ static void p_set_src(UINode* node, lua::State* L, int idx) {
modelviewer->setModel(lua::require_string(L, idx));
}
}
static void p_set_fallback(UINode* node, lua::State* L, int idx) {
if (auto image = dynamic_cast<Image*>(node)) {
image->setFallback(lua::require_string(L, idx));
}
}
static void p_set_region(UINode* node, lua::State* L, int idx) {
if (auto image = dynamic_cast<Image*>(node)) {
auto vec = lua::tovec4(L, idx);
@ -962,6 +975,7 @@ static int l_gui_setattr(lua::State* L) {
{"syntax", p_set_syntax},
{"markup", p_set_markup},
{"src", p_set_src},
{"fallback", p_set_fallback},
{"caret", p_set_caret},
{"value", p_set_value},
{"min", p_set_min},

View file

@ -87,10 +87,10 @@ static int l_pack_get_info(
lua::setfield(L, "path");
if (!engine->isHeadless()) {
auto assets = engine->getAssets();
auto& assetsLoader = engine->acquireBackgroundLoader();
std::string icon = pack.id + ".icon";
if (!AssetsLoader::loadExternalTexture(
assets, icon, {pack.folder / "icon.png"}
assetsLoader, icon, {pack.folder / "icon.png"}
)) {
icon = "gui/no_icon";
}

View file

@ -56,10 +56,9 @@ static int l_get_list(lua::State* L) {
lua::pushstring(L, name);
lua::setfield(L, "name");
auto assets = engine->getAssets();
std::string icon = "world#" + name + ".icon";
if (!engine->isHeadless() && !AssetsLoader::loadExternalTexture(
assets,
engine->acquireBackgroundLoader(),
icon,
{worlds[i] / "icon.png",
worlds[i] / "preview.png"}

View file

@ -115,6 +115,10 @@ struct UiSettings {
struct NetworkSettings {
};
struct SystemSettings {
IntegerSetting maxBgAssetLoaders {3, -4, 16};
};
struct EngineSettings {
AudioSettings audio;
DisplaySettings display;
@ -125,4 +129,5 @@ struct EngineSettings {
UiSettings ui;
NetworkSettings network;
PathfindingSettings pathfinding;
SystemSettings system;
};

View file

@ -2,6 +2,7 @@
#include <atomic>
#include <chrono>
#include <optional>
#include <condition_variable>
#include <functional>
#include <iostream>
@ -48,6 +49,7 @@ namespace util {
std::atomic<int> busyWorkers = 0;
std::atomic<uint> jobsDone = 0;
std::atomic<bool> working = true;
supplier<std::optional<T>> jobsSource = nullptr;
bool failed = false;
bool standaloneResults = true;
bool stopOnFail = true;
@ -151,6 +153,10 @@ namespace util {
return working;
}
void setJobsSource(supplier<std::optional<T>>&& source) {
jobsSource = std::move(source);
}
void terminate() override {
if (!working) {
return;
@ -221,6 +227,22 @@ namespace util {
}
}
}
if (jobsSource) {
bool jobsAdded = false;
std::lock_guard<std::mutex> jobsLock(jobsMutex);
while (true) {
auto job = jobsSource();
if (job.has_value()) {
jobs.push(std::move(job.value()));
jobsAdded = true;
} else {
break;
}
}
if (jobsAdded) {
jobsMutexCondition.notify_one();
}
}
if (failed) {
throw std::runtime_error("some job failed");
}