mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
add background assets loader
This commit is contained in:
parent
245e040a1c
commit
0cf7d9b1ed
9 changed files with 104 additions and 46 deletions
|
|
@ -319,28 +319,27 @@ 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();
|
||||
}
|
||||
logger.info() << (path.string() + ".png");
|
||||
if (io::exists(path.string() + ".png")) {
|
||||
loader.add(AssetType::TEXTURE, path.string(), name, nullptr);
|
||||
return true; // TODO: fallbacks support
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Assets& AssetsLoader::getAssets() {
|
||||
return assets;
|
||||
}
|
||||
|
||||
Engine& AssetsLoader::getEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
|
@ -376,6 +375,15 @@ std::shared_ptr<Task> AssetsLoader::startTask(runnable onDone) {
|
|||
[this](const assetload::postfunc& func) { func(&assets); }
|
||||
);
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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"}
|
||||
)) {
|
||||
icon = "gui/no_icon";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,13 +56,12 @@ 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"}
|
||||
{worlds[i] / "icon",
|
||||
worlds[i] / "preview"}
|
||||
)) {
|
||||
icon = "gui/no_world_icon";
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue