mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-12 06:21:50 +00:00
Merge pull request #773 from MihailRis/background-assets-loader
background assets loader
This commit is contained in:
commit
fb0ad19a65
25 changed files with 311 additions and 107 deletions
|
|
@ -5,6 +5,13 @@ A library for working with audio/visual assets.
|
||||||
## Functions
|
## Functions
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
|
-- Requests background texture loading
|
||||||
|
assets.request_texture(
|
||||||
|
-- Image file
|
||||||
|
filename: string,
|
||||||
|
-- Texture name after loading
|
||||||
|
name: string,
|
||||||
|
)
|
||||||
-- Loads a texture
|
-- Loads a texture
|
||||||
assets.load_texture(
|
assets.load_texture(
|
||||||
-- Array of bytes of an image file
|
-- Array of bytes of an image file
|
||||||
|
|
|
||||||
|
|
@ -167,10 +167,11 @@ Properties:
|
||||||
|
|
||||||
Properties:
|
Properties:
|
||||||
|
|
||||||
| Name | Type | Read | Write | Description |
|
| Name | Type | Read | Write | Description |
|
||||||
| ------ | ------ | ---- | ----- | ---------------- |
|
| -------- | ------ | ---- | ----- | ---------------- |
|
||||||
| src | string | yes | yes | texture name |
|
| src | string | yes | yes | texture name |
|
||||||
| region | vec4 | yes | yes | image sub-region |
|
| fallback | string | yes | yes | fallback texture |
|
||||||
|
| region | vec4 | yes | yes | image sub-region |
|
||||||
|
|
||||||
## Canvas
|
## Canvas
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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.
|
- `src` - name of an image stored in textures folder. Extension is not specified. Type: string.
|
||||||
Example: *gui/error*
|
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)
|
- `region` - image region x1, y1, x2, y2 from 0.0, 0.0 (upper left corner), 1.0, 1.0 (lower right corner)
|
||||||
|
|
||||||
## *canvas*
|
## *canvas*
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,14 @@
|
||||||
## Функции
|
## Функции
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
|
-- Запрашивает фоновую загрузку текстуры
|
||||||
|
assets.request_texture(
|
||||||
|
-- Файл изображения
|
||||||
|
filename: string,
|
||||||
|
-- Имя текстуры после загрузки
|
||||||
|
name: string,
|
||||||
|
)
|
||||||
|
|
||||||
-- Загружает текстуру
|
-- Загружает текстуру
|
||||||
assets.load_texture(
|
assets.load_texture(
|
||||||
-- Массив байт файла изображения
|
-- Массив байт файла изображения
|
||||||
|
|
|
||||||
|
|
@ -170,6 +170,7 @@ document["worlds-panel"]:clear()
|
||||||
| Название | Тип | Чтение | Запись | Описание |
|
| Название | Тип | Чтение | Запись | Описание |
|
||||||
| -------- | ------ | ------ | ------ | ---------------------- |
|
| -------- | ------ | ------ | ------ | ---------------------- |
|
||||||
| src | string | да | да | отображаемая текстура |
|
| src | string | да | да | отображаемая текстура |
|
||||||
|
| fallback | string | да | да | резервная текстура |
|
||||||
| region | vec4 | да | да | под-регион изображения |
|
| region | vec4 | да | да | под-регион изображения |
|
||||||
|
|
||||||
## Холст (canvas)
|
## Холст (canvas)
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,7 @@
|
||||||
## Изображение - *image*
|
## Изображение - *image*
|
||||||
|
|
||||||
- `src` - имя изображения в папке textures без указания расширения. Тип: строка. Например `gui/error`
|
- `src` - имя изображения в папке textures без указания расширения. Тип: строка. Например `gui/error`
|
||||||
|
- `fallback` - резервная текстура, отображаемая при отсутствии / фоновой загрузке основной
|
||||||
- `region` - под-регион изображения x1, y1, x2, y2 от 0.0, 0.0 (левый верхний угол), 1.0, 1.0 (правый нижний угол)
|
- `region` - под-регион изображения x1, y1, x2, y2 от 0.0, 0.0 (левый верхний угол), 1.0, 1.0 (правый нижний угол)
|
||||||
|
|
||||||
## Холст - *canvas*
|
## Холст - *canvas*
|
||||||
|
|
|
||||||
|
|
@ -11,5 +11,5 @@
|
||||||
<label ifnot='%{error}' pos='80,28' size='355,50' multiline='true' color='#FFFFFFB2'>
|
<label ifnot='%{error}' pos='80,28' size='355,50' multiline='true' color='#FFFFFFB2'>
|
||||||
%{description}
|
%{description}
|
||||||
</label>
|
</label>
|
||||||
<image pos='8,8' src='%{icon}' size='64'/>
|
<image pos='8,8' src='%{icon}' fallback='gui/no_icon' size='64'/>
|
||||||
</container>
|
</container>
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
hover-color='#162B3399'
|
hover-color='#162B3399'
|
||||||
onclick='app.open_world("%{name}")'
|
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>
|
<label pos='104,8'>%{name}</label>
|
||||||
<button margin='4'
|
<button margin='4'
|
||||||
gravity='center-right'
|
gravity='center-right'
|
||||||
|
|
|
||||||
|
|
@ -319,28 +319,31 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AssetsLoader::loadExternalTexture(
|
bool AssetsLoader::loadExternalTexture(
|
||||||
Assets* assets,
|
AssetsLoader& loader,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const std::vector<io::path>& alternatives
|
const std::vector<io::path>& alternatives
|
||||||
) {
|
) {
|
||||||
if (assets->get<Texture>(name) != nullptr) {
|
if (loader.getAssets().get<Texture>(name) != nullptr) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
for (auto& path : alternatives) {
|
for (auto& path : alternatives) {
|
||||||
if (io::exists(path)) {
|
if (io::exists(path)) {
|
||||||
try {
|
loader.add(
|
||||||
auto image = imageio::read(path);
|
AssetType::TEXTURE,
|
||||||
assets->store(Texture::from(image.get()), name);
|
(path.parent() / path.stem()).string(),
|
||||||
return true;
|
name,
|
||||||
} catch (const std::exception& err) {
|
nullptr
|
||||||
logger.error() << "error while loading external "
|
);
|
||||||
<< path.string() << ": " << err.what();
|
return true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Assets& AssetsLoader::getAssets() {
|
||||||
|
return assets;
|
||||||
|
}
|
||||||
|
|
||||||
Engine& AssetsLoader::getEngine() {
|
Engine& AssetsLoader::getEngine() {
|
||||||
return engine;
|
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 =
|
auto pool =
|
||||||
std::make_shared<util::ThreadPool<aloader_entry, assetload::postfunc>>(
|
std::make_shared<util::ThreadPool<aloader_entry, assetload::postfunc>>(
|
||||||
"assets-loader-pool",
|
"assets-loader-pool",
|
||||||
[=]() { return std::make_shared<LoaderWorker>(this); },
|
[=]() { 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->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()) {
|
while (!entries.empty()) {
|
||||||
aloader_entry entry = std::move(entries.front());
|
aloader_entry entry = std::move(entries.front());
|
||||||
entries.pop();
|
entries.pop();
|
||||||
|
|
|
||||||
|
|
@ -113,7 +113,7 @@ public:
|
||||||
/// @throws assetload::error
|
/// @throws assetload::error
|
||||||
void loadNext();
|
void loadNext();
|
||||||
|
|
||||||
std::shared_ptr<Task> startTask(runnable onDone);
|
std::shared_ptr<Task> startTask(runnable onDone, int maxWorkers);
|
||||||
|
|
||||||
const ResPaths& getPaths() const;
|
const ResPaths& getPaths() const;
|
||||||
aloader_func getLoader(AssetType tag);
|
aloader_func getLoader(AssetType tag);
|
||||||
|
|
@ -124,10 +124,11 @@ public:
|
||||||
static void addDefaults(AssetsLoader& loader, const Content* content);
|
static void addDefaults(AssetsLoader& loader, const Content* content);
|
||||||
|
|
||||||
static bool loadExternalTexture(
|
static bool loadExternalTexture(
|
||||||
Assets* assets,
|
AssetsLoader& loader,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const std::vector<io::path>& alternatives
|
const std::vector<io::path>& alternatives
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Assets& getAssets();
|
||||||
Engine& getEngine();
|
Engine& getEngine();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
92
src/engine/AssetsManagement.cpp
Normal file
92
src/engine/AssetsManagement.cpp
Normal 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();
|
||||||
|
}
|
||||||
33
src/engine/AssetsManagement.hpp
Normal file
33
src/engine/AssetsManagement.hpp
Normal 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;
|
||||||
|
};
|
||||||
|
|
@ -4,47 +4,45 @@
|
||||||
#define GLEW_STATIC
|
#define GLEW_STATIC
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "debug/Logger.hpp"
|
#include "AssetsManagement.hpp"
|
||||||
#include "assets/AssetsLoader.hpp"
|
|
||||||
#include "audio/audio.hpp"
|
#include "audio/audio.hpp"
|
||||||
|
#include "coders/commons.hpp"
|
||||||
#include "coders/GLSLExtension.hpp"
|
#include "coders/GLSLExtension.hpp"
|
||||||
#include "coders/toml.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 "content/ContentControl.hpp"
|
||||||
#include "core_defs.hpp"
|
#include "core_defs.hpp"
|
||||||
#include "io/io.hpp"
|
#include "debug/Logger.hpp"
|
||||||
#include "io/settings_io.hpp"
|
#include "devtools/DebuggingServer.hpp"
|
||||||
|
#include "devtools/Editor.hpp"
|
||||||
|
#include "devtools/Project.hpp"
|
||||||
|
#include "EnginePaths.hpp"
|
||||||
#include "frontend/locale.hpp"
|
#include "frontend/locale.hpp"
|
||||||
#include "frontend/menu.hpp"
|
#include "frontend/menu.hpp"
|
||||||
#include "frontend/screens/Screen.hpp"
|
#include "frontend/screens/Screen.hpp"
|
||||||
#include "graphics/render/ModelsGenerator.hpp"
|
|
||||||
#include "graphics/core/DrawContext.hpp"
|
#include "graphics/core/DrawContext.hpp"
|
||||||
#include "graphics/core/Shader.hpp"
|
#include "graphics/core/Shader.hpp"
|
||||||
#include "graphics/ui/GUI.hpp"
|
|
||||||
#include "graphics/ui/elements/Menu.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/CommandsInterpreter.hpp"
|
||||||
#include "logic/scripting/scripting.hpp"
|
#include "logic/EngineController.hpp"
|
||||||
#include "logic/scripting/scripting_hud.hpp"
|
#include "logic/scripting/scripting_hud.hpp"
|
||||||
|
#include "logic/scripting/scripting.hpp"
|
||||||
|
#include "Mainloop.hpp"
|
||||||
#include "network/Network.hpp"
|
#include "network/Network.hpp"
|
||||||
|
#include "ServerMainloop.hpp"
|
||||||
#include "util/platform.hpp"
|
#include "util/platform.hpp"
|
||||||
|
#include "util/stringutil.hpp"
|
||||||
#include "window/input.hpp"
|
#include "window/input.hpp"
|
||||||
#include "window/Window.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "world/Level.hpp"
|
|
||||||
#include "Mainloop.hpp"
|
|
||||||
#include "ServerMainloop.hpp"
|
|
||||||
#include "WindowControl.hpp"
|
#include "WindowControl.hpp"
|
||||||
#include "EnginePaths.hpp"
|
#include "world/Level.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
static debug::Logger logger("engine");
|
static debug::Logger logger("engine");
|
||||||
|
|
||||||
|
|
@ -84,6 +82,7 @@ void Engine::onContentLoad() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::initializeClient() {
|
void Engine::initializeClient() {
|
||||||
|
assets = std::make_unique<AssetsManagement>(*this);
|
||||||
windowControl = std::make_unique<WindowControl>(*this);
|
windowControl = std::make_unique<WindowControl>(*this);
|
||||||
auto [window, input] = windowControl->initialize();
|
auto [window, input] = windowControl->initialize();
|
||||||
|
|
||||||
|
|
@ -264,6 +263,7 @@ void Engine::applicationTick() {
|
||||||
|
|
||||||
void Engine::updateFrontend() {
|
void Engine::updateFrontend() {
|
||||||
double delta = time.getDelta();
|
double delta = time.getDelta();
|
||||||
|
assets->update();
|
||||||
updateHotkeys();
|
updateHotkeys();
|
||||||
audio::update(delta);
|
audio::update(delta);
|
||||||
gui->act(delta, window->getSize());
|
gui->act(delta, window->getSize());
|
||||||
|
|
@ -305,7 +305,7 @@ void Engine::renderFrame() {
|
||||||
screen->draw(time.getDelta());
|
screen->draw(time.getDelta());
|
||||||
|
|
||||||
DrawContext ctx(nullptr, *window, nullptr);
|
DrawContext ctx(nullptr, *window, nullptr);
|
||||||
gui->draw(ctx, *assets);
|
gui->draw(ctx, *assets->getStorage());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::saveSettings() {
|
void Engine::saveSettings() {
|
||||||
|
|
@ -363,33 +363,7 @@ void Engine::setLevelConsumer(OnWorldOpen levelConsumer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::loadAssets() {
|
void Engine::loadAssets() {
|
||||||
logger.info() << "loading assets";
|
assets->loadAssets(content->get());
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::loadProject() {
|
void Engine::loadProject() {
|
||||||
|
|
@ -442,7 +416,11 @@ EngineSettings& Engine::getSettings() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Assets* Engine::getAssets() {
|
Assets* Engine::getAssets() {
|
||||||
return assets.get();
|
return assets->getStorage();
|
||||||
|
}
|
||||||
|
|
||||||
|
AssetsLoader& Engine::acquireBackgroundLoader() {
|
||||||
|
return assets->acquireBackgroundLoader();
|
||||||
}
|
}
|
||||||
|
|
||||||
EnginePaths& Engine::getPaths() {
|
EnginePaths& Engine::getPaths() {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class Assets;
|
class Assets;
|
||||||
|
class AssetsLoader;
|
||||||
|
class AssetsManagement;
|
||||||
class ContentControl;
|
class ContentControl;
|
||||||
class EngineController;
|
class EngineController;
|
||||||
class EnginePaths;
|
class EnginePaths;
|
||||||
|
|
@ -52,7 +54,7 @@ class Engine : public util::ObjectsKeeper {
|
||||||
std::unique_ptr<EnginePaths> paths;
|
std::unique_ptr<EnginePaths> paths;
|
||||||
std::unique_ptr<Project> project;
|
std::unique_ptr<Project> project;
|
||||||
std::unique_ptr<SettingsHandler> settingsHandler;
|
std::unique_ptr<SettingsHandler> settingsHandler;
|
||||||
std::unique_ptr<Assets> assets;
|
std::unique_ptr<AssetsManagement> assets;
|
||||||
std::shared_ptr<Screen> screen;
|
std::shared_ptr<Screen> screen;
|
||||||
std::unique_ptr<ContentControl> content;
|
std::unique_ptr<ContentControl> content;
|
||||||
std::unique_ptr<EngineController> controller;
|
std::unique_ptr<EngineController> controller;
|
||||||
|
|
@ -109,6 +111,8 @@ public:
|
||||||
/// @brief Get active assets storage instance
|
/// @brief Get active assets storage instance
|
||||||
Assets* getAssets();
|
Assets* getAssets();
|
||||||
|
|
||||||
|
AssetsLoader& acquireBackgroundLoader();
|
||||||
|
|
||||||
/// @brief Get writeable engine settings structure instance
|
/// @brief Get writeable engine settings structure instance
|
||||||
EngineSettings& getSettings();
|
EngineSettings& getSettings();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -274,6 +274,9 @@ ResPaths::ResPaths(std::vector<PathsRoot> roots)
|
||||||
}
|
}
|
||||||
|
|
||||||
io::path ResPaths::find(const std::string& filename) const {
|
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--) {
|
for (int i = roots.size() - 1; i >= 0; i--) {
|
||||||
auto& root = roots[i];
|
auto& root = roots[i];
|
||||||
auto file = root.path / filename;
|
auto file = root.path / filename;
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "assets/Assets.hpp"
|
#include "assets/Assets.hpp"
|
||||||
|
#include "assets/assets_util.hpp"
|
||||||
#include "graphics/core/Atlas.hpp"
|
#include "graphics/core/Atlas.hpp"
|
||||||
#include "graphics/core/Batch2D.hpp"
|
#include "graphics/core/Batch2D.hpp"
|
||||||
#include "graphics/core/DrawContext.hpp"
|
#include "graphics/core/DrawContext.hpp"
|
||||||
|
|
@ -16,37 +17,25 @@ Image::Image(GUI& gui, std::string texture, glm::vec2 size)
|
||||||
setInteractive(false);
|
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) {
|
void Image::draw(const DrawContext& pctx, const Assets& assets) {
|
||||||
glm::vec2 pos = calcPos();
|
glm::vec2 pos = calcPos();
|
||||||
auto batch = pctx.getBatch2D();
|
auto batch = pctx.getBatch2D();
|
||||||
|
|
||||||
Texture* texture = nullptr;
|
auto textureRegion = refreshTexture(assets);
|
||||||
auto separator = this->texture.find(':');
|
|
||||||
if (separator == std::string::npos) {
|
batch->setRegion(textureRegion.region);
|
||||||
texture = assets.get<Texture>(this->texture);
|
batch->texture(textureRegion.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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
batch->rect(
|
batch->rect(
|
||||||
pos.x,
|
pos.x,
|
||||||
pos.y,
|
pos.y,
|
||||||
|
|
@ -73,10 +62,18 @@ const std::string& Image::getTexture() const {
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::string& Image::getFallback() const {
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
void Image::setTexture(const std::string& name) {
|
void Image::setTexture(const std::string& name) {
|
||||||
texture = name;
|
texture = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Image::setFallback(const std::string& name) {
|
||||||
|
fallback = name;
|
||||||
|
}
|
||||||
|
|
||||||
void Image::setRegion(const UVRegion& region) {
|
void Image::setRegion(const UVRegion& region) {
|
||||||
this->region = region;
|
this->region = region;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,21 +3,30 @@
|
||||||
#include "UINode.hpp"
|
#include "UINode.hpp"
|
||||||
#include "maths/UVRegion.hpp"
|
#include "maths/UVRegion.hpp"
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
struct TextureRegion;
|
||||||
|
}
|
||||||
|
|
||||||
namespace gui {
|
namespace gui {
|
||||||
class Image : public UINode {
|
class Image final : public UINode {
|
||||||
protected:
|
protected:
|
||||||
std::string texture;
|
std::string texture;
|
||||||
|
std::string fallback;
|
||||||
UVRegion region {};
|
UVRegion region {};
|
||||||
bool autoresize = false;
|
bool autoresize = false;
|
||||||
|
|
||||||
|
util::TextureRegion refreshTexture(const Assets& assets);
|
||||||
public:
|
public:
|
||||||
Image(GUI& gui, std::string texture, glm::vec2 size=glm::vec2(32,32));
|
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);
|
void setAutoResize(bool flag);
|
||||||
virtual bool isAutoResize() const;
|
bool isAutoResize() const;
|
||||||
virtual const std::string& getTexture() const;
|
const std::string& getTexture() const;
|
||||||
virtual void setTexture(const std::string& name);
|
const std::string& getFallback() const;
|
||||||
|
void setTexture(const std::string& name);
|
||||||
|
void setFallback(const std::string& name);
|
||||||
void setRegion(const UVRegion& region);
|
void setRegion(const UVRegion& region);
|
||||||
const UVRegion& getRegion() const;
|
const UVRegion& getRegion() const;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -632,8 +632,10 @@ static std::shared_ptr<UINode> read_image(
|
||||||
const UiXmlReader& reader, const xml::xmlelement& element
|
const UiXmlReader& reader, const xml::xmlelement& element
|
||||||
) {
|
) {
|
||||||
std::string src = element.attr("src", "").getText();
|
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);
|
read_uinode(reader, element, *image);
|
||||||
|
image->setFallback(std::move(fallback));
|
||||||
|
|
||||||
if (element.has("region")) {
|
if (element.has("region")) {
|
||||||
auto vec = element.attr("region").asVec4();
|
auto vec = element.attr("region").asVec4();
|
||||||
|
|
|
||||||
|
|
@ -94,6 +94,9 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) {
|
||||||
builder.add("do-write-lights", &settings.debug.doWriteLights);
|
builder.add("do-write-lights", &settings.debug.doWriteLights);
|
||||||
builder.add("do-trace-shaders", &settings.debug.doTraceShaders);
|
builder.add("do-trace-shaders", &settings.debug.doTraceShaders);
|
||||||
builder.add("enable-experimental", &settings.debug.enableExperimental);
|
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 {
|
dv::value SettingsHandler::getValue(const std::string& name) const {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include "api_lua.hpp"
|
#include "api_lua.hpp"
|
||||||
|
|
||||||
#include "assets/Assets.hpp"
|
#include "assets/Assets.hpp"
|
||||||
|
#include "assets/AssetsLoader.hpp"
|
||||||
#include "coders/png.hpp"
|
#include "coders/png.hpp"
|
||||||
#include "coders/vcm.hpp"
|
#include "coders/vcm.hpp"
|
||||||
#include "debug/Logger.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) {
|
static int l_load_texture(lua::State* L) {
|
||||||
if (lua::isstring(L, 3) && lua::require_lstring(L, 3) != "png") {
|
if (lua::isstring(L, 3) && lua::require_lstring(L, 3) != "png") {
|
||||||
throw std::runtime_error("unsupportd image format");
|
throw std::runtime_error("unsupportd image format");
|
||||||
|
|
@ -108,6 +117,7 @@ static int l_to_canvas(lua::State* L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const luaL_Reg assetslib[] = {
|
const luaL_Reg assetslib[] = {
|
||||||
|
{"request_texture", lua::wrap<l_request_texture>},
|
||||||
{"load_texture", lua::wrap<l_load_texture>},
|
{"load_texture", lua::wrap<l_load_texture>},
|
||||||
{"parse_model", lua::wrap<l_parse_model>},
|
{"parse_model", lua::wrap<l_parse_model>},
|
||||||
{"to_canvas", lua::wrap<l_to_canvas>},
|
{"to_canvas", lua::wrap<l_to_canvas>},
|
||||||
|
|
|
||||||
|
|
@ -397,6 +397,13 @@ static int p_get_src(UINode* node, lua::State* L) {
|
||||||
return 0;
|
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) {
|
static int p_get_region(UINode* node, lua::State* L) {
|
||||||
if (auto image = dynamic_cast<Image*>(node)) {
|
if (auto image = dynamic_cast<Image*>(node)) {
|
||||||
const auto& region = image->getRegion();
|
const auto& region = image->getRegion();
|
||||||
|
|
@ -663,6 +670,7 @@ static int l_gui_getattr(lua::State* L) {
|
||||||
{"syntax", p_get_syntax},
|
{"syntax", p_get_syntax},
|
||||||
{"markup", p_get_markup},
|
{"markup", p_get_markup},
|
||||||
{"src", p_get_src},
|
{"src", p_get_src},
|
||||||
|
{"fallback", p_get_fallback},
|
||||||
{"value", p_get_value},
|
{"value", p_get_value},
|
||||||
{"min", p_get_min},
|
{"min", p_get_min},
|
||||||
{"max", p_get_max},
|
{"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));
|
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) {
|
static void p_set_region(UINode* node, lua::State* L, int idx) {
|
||||||
if (auto image = dynamic_cast<Image*>(node)) {
|
if (auto image = dynamic_cast<Image*>(node)) {
|
||||||
auto vec = lua::tovec4(L, idx);
|
auto vec = lua::tovec4(L, idx);
|
||||||
|
|
@ -962,6 +975,7 @@ static int l_gui_setattr(lua::State* L) {
|
||||||
{"syntax", p_set_syntax},
|
{"syntax", p_set_syntax},
|
||||||
{"markup", p_set_markup},
|
{"markup", p_set_markup},
|
||||||
{"src", p_set_src},
|
{"src", p_set_src},
|
||||||
|
{"fallback", p_set_fallback},
|
||||||
{"caret", p_set_caret},
|
{"caret", p_set_caret},
|
||||||
{"value", p_set_value},
|
{"value", p_set_value},
|
||||||
{"min", p_set_min},
|
{"min", p_set_min},
|
||||||
|
|
|
||||||
|
|
@ -87,10 +87,10 @@ static int l_pack_get_info(
|
||||||
lua::setfield(L, "path");
|
lua::setfield(L, "path");
|
||||||
|
|
||||||
if (!engine->isHeadless()) {
|
if (!engine->isHeadless()) {
|
||||||
auto assets = engine->getAssets();
|
auto& assetsLoader = engine->acquireBackgroundLoader();
|
||||||
std::string icon = pack.id + ".icon";
|
std::string icon = pack.id + ".icon";
|
||||||
if (!AssetsLoader::loadExternalTexture(
|
if (!AssetsLoader::loadExternalTexture(
|
||||||
assets, icon, {pack.folder / "icon.png"}
|
assetsLoader, icon, {pack.folder / "icon.png"}
|
||||||
)) {
|
)) {
|
||||||
icon = "gui/no_icon";
|
icon = "gui/no_icon";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -56,10 +56,9 @@ static int l_get_list(lua::State* L) {
|
||||||
lua::pushstring(L, name);
|
lua::pushstring(L, name);
|
||||||
lua::setfield(L, "name");
|
lua::setfield(L, "name");
|
||||||
|
|
||||||
auto assets = engine->getAssets();
|
|
||||||
std::string icon = "world#" + name + ".icon";
|
std::string icon = "world#" + name + ".icon";
|
||||||
if (!engine->isHeadless() && !AssetsLoader::loadExternalTexture(
|
if (!engine->isHeadless() && !AssetsLoader::loadExternalTexture(
|
||||||
assets,
|
engine->acquireBackgroundLoader(),
|
||||||
icon,
|
icon,
|
||||||
{worlds[i] / "icon.png",
|
{worlds[i] / "icon.png",
|
||||||
worlds[i] / "preview.png"}
|
worlds[i] / "preview.png"}
|
||||||
|
|
|
||||||
|
|
@ -115,6 +115,10 @@ struct UiSettings {
|
||||||
struct NetworkSettings {
|
struct NetworkSettings {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct SystemSettings {
|
||||||
|
IntegerSetting maxBgAssetLoaders {3, -4, 16};
|
||||||
|
};
|
||||||
|
|
||||||
struct EngineSettings {
|
struct EngineSettings {
|
||||||
AudioSettings audio;
|
AudioSettings audio;
|
||||||
DisplaySettings display;
|
DisplaySettings display;
|
||||||
|
|
@ -125,4 +129,5 @@ struct EngineSettings {
|
||||||
UiSettings ui;
|
UiSettings ui;
|
||||||
NetworkSettings network;
|
NetworkSettings network;
|
||||||
PathfindingSettings pathfinding;
|
PathfindingSettings pathfinding;
|
||||||
|
SystemSettings system;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <optional>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
@ -48,6 +49,7 @@ namespace util {
|
||||||
std::atomic<int> busyWorkers = 0;
|
std::atomic<int> busyWorkers = 0;
|
||||||
std::atomic<uint> jobsDone = 0;
|
std::atomic<uint> jobsDone = 0;
|
||||||
std::atomic<bool> working = true;
|
std::atomic<bool> working = true;
|
||||||
|
supplier<std::optional<T>> jobsSource = nullptr;
|
||||||
bool failed = false;
|
bool failed = false;
|
||||||
bool standaloneResults = true;
|
bool standaloneResults = true;
|
||||||
bool stopOnFail = true;
|
bool stopOnFail = true;
|
||||||
|
|
@ -151,6 +153,10 @@ namespace util {
|
||||||
return working;
|
return working;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setJobsSource(supplier<std::optional<T>>&& source) {
|
||||||
|
jobsSource = std::move(source);
|
||||||
|
}
|
||||||
|
|
||||||
void terminate() override {
|
void terminate() override {
|
||||||
if (!working) {
|
if (!working) {
|
||||||
return;
|
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) {
|
if (failed) {
|
||||||
throw std::runtime_error("some job failed");
|
throw std::runtime_error("some job failed");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue