add 'test' library

This commit is contained in:
MihailRis 2024-12-07 22:16:50 +03:00
parent bbb9987140
commit 59402b6607
10 changed files with 56 additions and 12 deletions

View File

@ -1 +1,3 @@
print("Hello from the example test!") print("Hello from the example test!")
test.sleep(1)
print("2")

View File

@ -9,6 +9,11 @@ function sleep(timesec)
end end
end end
if test then
test.sleep = sleep
test.name = __VC_TEST_NAME
end
------------------------------------------------ ------------------------------------------------
------------------- Events --------------------- ------------------- Events ---------------------
------------------------------------------------ ------------------------------------------------

View File

@ -190,9 +190,15 @@ void Engine::runTest() {
logger.info() << "nothing to do"; logger.info() << "nothing to do";
return; return;
} }
int tps = 20;
logger.info() << "starting test " << params.testFile; logger.info() << "starting test " << params.testFile;
auto process = scripting::start_coroutine(params.testFile); auto process = scripting::start_coroutine(params.testFile);
while (process->isActive()) { while (process->isActive()) {
frame++;
delta = 1.0f / static_cast<float>(tps);
lastTime += delta;
process->update(); process->update();
} }
logger.info() << "test finished"; logger.info() << "test finished";
@ -466,6 +472,10 @@ double Engine::getDelta() const {
return delta; return delta;
} }
double Engine::getUptime() const {
return lastTime;
}
void Engine::setScreen(std::shared_ptr<Screen> screen) { void Engine::setScreen(std::shared_ptr<Screen> screen) {
// reset audio channels (stop all sources) // reset audio channels (stop all sources)
audio::reset_channel(audio::get_channel_index("regular")); audio::reset_channel(audio::get_channel_index("regular"));
@ -534,3 +544,7 @@ SettingsHandler& Engine::getSettingsHandler() {
network::Network& Engine::getNetwork() { network::Network& Engine::getNetwork() {
return *network; return *network;
} }
const CoreParameters& Engine::getCoreParameters() const {
return params;
}

View File

@ -125,6 +125,8 @@ public:
/// @brief Get current frame delta-time /// @brief Get current frame delta-time
double getDelta() const; double getDelta() const;
double getUptime() const;
/// @brief Get active assets storage instance /// @brief Get active assets storage instance
Assets* getAssets(); Assets* getAssets();
@ -166,4 +168,6 @@ public:
SettingsHandler& getSettingsHandler(); SettingsHandler& getSettingsHandler();
network::Network& getNetwork(); network::Network& getNetwork();
const CoreParameters& getCoreParameters() const;
}; };

View File

@ -37,6 +37,7 @@ extern const luaL_Reg packlib[];
extern const luaL_Reg particleslib[]; // gfx.particles extern const luaL_Reg particleslib[]; // gfx.particles
extern const luaL_Reg playerlib[]; extern const luaL_Reg playerlib[];
extern const luaL_Reg quatlib[]; extern const luaL_Reg quatlib[];
extern const luaL_Reg testlib[];
extern const luaL_Reg text3dlib[]; // gfx.text3d extern const luaL_Reg text3dlib[]; // gfx.text3d
extern const luaL_Reg timelib[]; extern const luaL_Reg timelib[];
extern const luaL_Reg tomllib[]; extern const luaL_Reg tomllib[];

View File

@ -0,0 +1,5 @@
#include "api_lua.hpp"
const luaL_Reg testlib[] = {
{NULL, NULL}
};

View File

@ -2,15 +2,18 @@
#include "window/Window.hpp" #include "window/Window.hpp"
#include "api_lua.hpp" #include "api_lua.hpp"
static int l_time_uptime(lua::State* L) { using namespace scripting;
return lua::pushnumber(L, Window::time());
static int l_uptime(lua::State* L) {
return lua::pushnumber(L, engine->getUptime());
} }
static int l_time_delta(lua::State* L) { static int l_delta(lua::State* L) {
return lua::pushnumber(L, scripting::engine->getDelta()); return lua::pushnumber(L, engine->getDelta());
} }
const luaL_Reg timelib[] = { const luaL_Reg timelib[] = {
{"uptime", lua::wrap<l_time_uptime>}, {"uptime", lua::wrap<l_uptime>},
{"delta", lua::wrap<l_time_delta>}, {"delta", lua::wrap<l_delta>},
{NULL, NULL}}; {NULL, NULL}
};

View File

@ -9,6 +9,7 @@
#include "util/stringutil.hpp" #include "util/stringutil.hpp"
#include "libs/api_lua.hpp" #include "libs/api_lua.hpp"
#include "lua_custom_types.hpp" #include "lua_custom_types.hpp"
#include "engine.hpp"
static debug::Logger logger("lua-state"); static debug::Logger logger("lua-state");
static lua::State* main_thread = nullptr; static lua::State* main_thread = nullptr;
@ -57,7 +58,10 @@ static void create_libs(State* L, StateType stateType) {
openlib(L, "vec3", vec3lib); openlib(L, "vec3", vec3lib);
openlib(L, "vec4", vec4lib); openlib(L, "vec4", vec4lib);
if (stateType == StateType::BASE) { if (stateType == StateType::TEST) {
openlib(L, "test", testlib);
}
if (stateType == StateType::BASE || stateType == StateType::TEST) {
openlib(L, "gui", guilib); openlib(L, "gui", guilib);
openlib(L, "input", inputlib); openlib(L, "input", inputlib);
openlib(L, "inventory", inventorylib); openlib(L, "inventory", inventorylib);
@ -110,11 +114,15 @@ void lua::init_state(State* L, StateType stateType) {
newusertype<LuaVoxelFragment>(L); newusertype<LuaVoxelFragment>(L);
} }
void lua::initialize(const EnginePaths& paths) { void lua::initialize(const EnginePaths& paths, const CoreParameters& params) {
logger.info() << LUA_VERSION; logger.info() << LUA_VERSION;
logger.info() << LUAJIT_VERSION; logger.info() << LUAJIT_VERSION;
main_thread = create_state(paths, StateType::BASE); main_thread = create_state(
paths, params.headless ? StateType::TEST : StateType::BASE
);
lua::pushstring(main_thread, params.testFile.stem().u8string());
lua::setglobal(main_thread, "__VC_TEST_NAME");
} }
void lua::finalize() { void lua::finalize() {

View File

@ -8,14 +8,16 @@
#include "lua_util.hpp" #include "lua_util.hpp"
class EnginePaths; class EnginePaths;
struct CoreParameters;
namespace lua { namespace lua {
enum class StateType { enum class StateType {
BASE, BASE,
TEST,
GENERATOR, GENERATOR,
}; };
void initialize(const EnginePaths& paths); void initialize(const EnginePaths& paths, const CoreParameters& params);
void finalize(); void finalize();
bool emit_event( bool emit_event(

View File

@ -66,7 +66,7 @@ int scripting::load_script(
void scripting::initialize(Engine* engine) { void scripting::initialize(Engine* engine) {
scripting::engine = engine; scripting::engine = engine;
lua::initialize(*engine->getPaths()); lua::initialize(*engine->getPaths(), engine->getCoreParameters());
load_script(fs::path("stdlib.lua"), true); load_script(fs::path("stdlib.lua"), true);
load_script(fs::path("classes.lua"), true); load_script(fs::path("classes.lua"), true);