AssetsLoader::loadExternalTexture
This commit is contained in:
@@ -11,10 +11,12 @@
|
|||||||
#include "../constants.h"
|
#include "../constants.h"
|
||||||
#include "../data/dynamic.h"
|
#include "../data/dynamic.h"
|
||||||
#include "../debug/Logger.h"
|
#include "../debug/Logger.h"
|
||||||
|
#include "../coders/imageio.h"
|
||||||
#include "../files/files.h"
|
#include "../files/files.h"
|
||||||
#include "../files/engine_paths.h"
|
#include "../files/engine_paths.h"
|
||||||
#include "../content/Content.h"
|
#include "../content/Content.h"
|
||||||
#include "../content/ContentPack.h"
|
#include "../content/ContentPack.h"
|
||||||
|
#include "../graphics/core/Texture.hpp"
|
||||||
#include "../logic/scripting/scripting.h"
|
#include "../logic/scripting/scripting.h"
|
||||||
|
|
||||||
static debug::Logger logger("assets-loader");
|
static debug::Logger logger("assets-loader");
|
||||||
@@ -204,6 +206,26 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) {
|
|||||||
loader.add(AssetType::atlas, TEXTURES_FOLDER+"/items", "items");
|
loader.add(AssetType::atlas, TEXTURES_FOLDER+"/items", "items");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool AssetsLoader::loadExternalTexture(
|
||||||
|
Assets* assets,
|
||||||
|
const std::string& name,
|
||||||
|
std::vector<std::filesystem::path> alternatives
|
||||||
|
) {
|
||||||
|
for (auto& path : alternatives) {
|
||||||
|
if (fs::exists(path)) {
|
||||||
|
try {
|
||||||
|
auto image = imageio::read(path.string());
|
||||||
|
assets->store(Texture::from(image.get()), name);
|
||||||
|
return true;
|
||||||
|
} catch (const std::exception& err) {
|
||||||
|
logger.error() << "error while loading external "
|
||||||
|
<< path.u8string() << ": " << err.what();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
const ResPaths* AssetsLoader::getPaths() const {
|
const ResPaths* AssetsLoader::getPaths() const {
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,15 +93,21 @@ public:
|
|||||||
bool hasNext() const;
|
bool hasNext() const;
|
||||||
bool loadNext();
|
bool loadNext();
|
||||||
|
|
||||||
|
std::shared_ptr<Task> startTask(runnable onDone);
|
||||||
|
|
||||||
|
const ResPaths* getPaths() const;
|
||||||
|
aloader_func getLoader(AssetType tag);
|
||||||
|
|
||||||
/// @brief Enqueue core and content assets
|
/// @brief Enqueue core and content assets
|
||||||
/// @param loader target loader
|
/// @param loader target loader
|
||||||
/// @param content engine content
|
/// @param content engine content
|
||||||
static void addDefaults(AssetsLoader& loader, const Content* content);
|
static void addDefaults(AssetsLoader& loader, const Content* content);
|
||||||
|
|
||||||
std::shared_ptr<Task> startTask(runnable onDone);
|
static bool loadExternalTexture(
|
||||||
|
Assets* assets,
|
||||||
const ResPaths* getPaths() const;
|
const std::string& name,
|
||||||
aloader_func getLoader(AssetType tag);
|
std::vector<std::filesystem::path> alternatives
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ASSETS_ASSETS_LOADER_H
|
#endif // ASSETS_ASSETS_LOADER_H
|
||||||
|
|||||||
@@ -2,12 +2,11 @@
|
|||||||
#include "lua_commons.h"
|
#include "lua_commons.h"
|
||||||
#include "../scripting.h"
|
#include "../scripting.h"
|
||||||
#include "../../../engine.h"
|
#include "../../../engine.h"
|
||||||
#include "../../../coders/imageio.h"
|
#include "../../../assets/AssetsLoader.h"
|
||||||
#include "../../../files/engine_paths.h"
|
#include "../../../files/engine_paths.h"
|
||||||
#include "../../../files/WorldFiles.h"
|
#include "../../../files/WorldFiles.h"
|
||||||
#include "../../../world/Level.h"
|
#include "../../../world/Level.h"
|
||||||
#include "../../../world/World.h"
|
#include "../../../world/World.h"
|
||||||
#include "../../../graphics/core/Texture.hpp"
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
@@ -75,21 +74,14 @@ static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content*
|
|||||||
lua_pushstring(L, pack.version.c_str());
|
lua_pushstring(L, pack.version.c_str());
|
||||||
lua_setfield(L, -2, "version");
|
lua_setfield(L, -2, "version");
|
||||||
|
|
||||||
// FIXME: hmm
|
|
||||||
auto assets = scripting::engine->getAssets();
|
auto assets = scripting::engine->getAssets();
|
||||||
std::string icon = pack.id+".icon";
|
std::string icon = pack.id+".icon";
|
||||||
if (assets->getTexture(icon) == nullptr) {
|
if (!AssetsLoader::loadExternalTexture(assets, icon, {
|
||||||
auto iconfile = pack.folder/fs::path("icon.png");
|
pack.folder/fs::path("icon.png")
|
||||||
if (!fs::exists(iconfile)) {
|
})) {
|
||||||
iconfile = pack.folder/fs::path("preview.png");
|
icon = "gui/no_icon";
|
||||||
}
|
|
||||||
if (fs::exists(iconfile)) {
|
|
||||||
auto image = imageio::read(iconfile.string());
|
|
||||||
assets->store(Texture::from(image.get()), icon);
|
|
||||||
} else {
|
|
||||||
icon = "gui/no_icon";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_pushstring(L, icon.c_str());
|
lua_pushstring(L, icon.c_str());
|
||||||
lua_setfield(L, -2, "icon");
|
lua_setfield(L, -2, "icon");
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,7 @@
|
|||||||
#include "api_lua.h"
|
#include "api_lua.h"
|
||||||
#include "../scripting.h"
|
#include "../scripting.h"
|
||||||
#include "../../../assets/Assets.h"
|
#include "../../../assets/Assets.h"
|
||||||
#include "../../../coders/imageio.h"
|
#include "../../../assets/AssetsLoader.h"
|
||||||
#include "../../../graphics/core/Texture.hpp"
|
|
||||||
#include "../../../files/engine_paths.h"
|
#include "../../../files/engine_paths.h"
|
||||||
#include "../../../world/Level.h"
|
#include "../../../world/Level.h"
|
||||||
#include "../../../world/World.h"
|
#include "../../../world/World.h"
|
||||||
@@ -26,18 +25,13 @@ static int l_world_get_list(lua_State* L) {
|
|||||||
lua_pushstring(L, name.c_str());
|
lua_pushstring(L, name.c_str());
|
||||||
lua_setfield(L, -2, "name");
|
lua_setfield(L, -2, "name");
|
||||||
|
|
||||||
// FIXME: hmm
|
|
||||||
auto assets = scripting::engine->getAssets();
|
auto assets = scripting::engine->getAssets();
|
||||||
std::string icon = "world:"+name+".icon";
|
std::string icon = "world:"+name+".icon";
|
||||||
|
if (!AssetsLoader::loadExternalTexture(assets, icon, {
|
||||||
if (assets->getTexture(icon) == nullptr) {
|
worlds[i]/fs::path("icon.png"),
|
||||||
auto iconfile = worlds[i]/fs::path("preview.png");
|
worlds[i]/fs::path("preview.png")
|
||||||
if (fs::is_regular_file(iconfile)) {
|
})) {
|
||||||
auto image = imageio::read(iconfile.string());
|
icon = "gui/no_world_icon";
|
||||||
assets->store(Texture::from(image.get()), icon);
|
|
||||||
} else {
|
|
||||||
icon = "gui/no_world_icon";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
lua_pushstring(L, icon.c_str());
|
lua_pushstring(L, icon.c_str());
|
||||||
lua_setfield(L, -2, "icon");
|
lua_setfield(L, -2, "icon");
|
||||||
|
|||||||
Reference in New Issue
Block a user