world previews demo
This commit is contained in:
@@ -19,18 +19,6 @@ namespace scripting {
|
||||
extern lua::LuaState* state;
|
||||
}
|
||||
|
||||
static int l_get_worlds_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_pushstring(L, worlds[i].filename().u8string().c_str());
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_new_world(lua_State* L) {
|
||||
auto name = lua_tostring(L, 1);
|
||||
auto seed = lua_tostring(L, 2);
|
||||
@@ -183,7 +171,6 @@ static int l_get_generators(lua_State* L) {
|
||||
}
|
||||
|
||||
const luaL_Reg corelib [] = {
|
||||
{"get_worlds_list", lua_wrap_errors<l_get_worlds_list>},
|
||||
{"new_world", lua_wrap_errors<l_new_world>},
|
||||
{"open_world", lua_wrap_errors<l_open_world>},
|
||||
{"close_world", lua_wrap_errors<l_close_world>},
|
||||
|
||||
@@ -75,12 +75,15 @@ static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content*
|
||||
lua_pushstring(L, pack.version.c_str());
|
||||
lua_setfield(L, -2, "version");
|
||||
|
||||
// hmm
|
||||
// FIXME: hmm
|
||||
auto assets = scripting::engine->getAssets();
|
||||
std::string icon = pack.id+".icon";
|
||||
if (assets->getTexture(icon) == nullptr) {
|
||||
auto iconfile = pack.folder/fs::path("icon.png");
|
||||
if (fs::is_regular_file(iconfile)) {
|
||||
if (!fs::exists(iconfile)) {
|
||||
iconfile = pack.folder/fs::path("preview.png");
|
||||
}
|
||||
if (fs::exists(iconfile)) {
|
||||
auto image = imageio::read(iconfile.string());
|
||||
assets->store(Texture::from(image.get()), icon);
|
||||
} else {
|
||||
|
||||
@@ -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