world previews demo
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
#include "lua_commons.h"
|
||||
#include "api_lua.h"
|
||||
#include "../scripting.h"
|
||||
#include "../../../assets/Assets.h"
|
||||
#include "../../../coders/imageio.h"
|
||||
#include "../../../graphics/core/Texture.h"
|
||||
#include "../../../files/engine_paths.h"
|
||||
#include "../../../world/Level.h"
|
||||
#include "../../../world/World.h"
|
||||
#include "../../../engine.h"
|
||||
@@ -10,6 +14,39 @@
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
static int l_world_get_list(lua_State* L) {
|
||||
auto paths = scripting::engine->getPaths();
|
||||
auto worlds = paths->scanForWorlds();
|
||||
|
||||
lua_createtable(L, worlds.size(), 0);
|
||||
for (size_t i = 0; i < worlds.size(); i++) {
|
||||
lua_createtable(L, 0, 1);
|
||||
|
||||
auto name = worlds[i].filename().u8string();
|
||||
lua_pushstring(L, name.c_str());
|
||||
lua_setfield(L, -2, "name");
|
||||
|
||||
// FIXME: hmm
|
||||
auto assets = scripting::engine->getAssets();
|
||||
std::string icon = "world:"+name+".icon";
|
||||
|
||||
if (assets->getTexture(icon) == nullptr) {
|
||||
auto iconfile = worlds[i]/fs::path("preview.png");
|
||||
if (fs::is_regular_file(iconfile)) {
|
||||
auto image = imageio::read(iconfile.string());
|
||||
assets->store(Texture::from(image.get()), icon);
|
||||
} else {
|
||||
icon = "gui/no_world_icon";
|
||||
}
|
||||
}
|
||||
lua_pushstring(L, icon.c_str());
|
||||
lua_setfield(L, -2, "icon");
|
||||
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_world_get_total_time(lua_State* L) {
|
||||
lua_pushnumber(L, scripting::level->getWorld()->totalTime);
|
||||
return 1;
|
||||
@@ -39,6 +76,7 @@ static int l_world_exists(lua_State* L) {
|
||||
}
|
||||
|
||||
const luaL_Reg worldlib [] = {
|
||||
{"get_list", lua_wrap_errors<l_world_get_list>},
|
||||
{"get_total_time", lua_wrap_errors<l_world_get_total_time>},
|
||||
{"get_day_time", lua_wrap_errors<l_world_get_day_time>},
|
||||
{"set_day_time", lua_wrap_errors<l_world_set_day_time>},
|
||||
|
||||
Reference in New Issue
Block a user