From bb6115cfdfde19b6e6ca7495442ca59824eb7efe Mon Sep 17 00:00:00 2001 From: det-fys Date: Sun, 24 Dec 2023 18:02:06 +0100 Subject: [PATCH] luaaaaaaa --- src/tsr/game.cpp | 251 ++++++++++++++++++++++++++--------------- src/tsr/game.hpp | 10 ++ src/tsr/luaapi.cpp | 27 +++++ src/tsr/luaapi.hpp | 35 ++++++ src/tsr/physics.cpp | 19 ++-- src/tsr/renderer.cpp | 2 +- src/tsrecs.cpp | 26 +---- tsrecs.vcxproj | 2 + tsrecs.vcxproj.filters | 6 + 9 files changed, 253 insertions(+), 125 deletions(-) create mode 100644 src/tsr/luaapi.cpp create mode 100644 src/tsr/luaapi.hpp diff --git a/src/tsr/game.cpp b/src/tsr/game.cpp index f27747d..6dd1212 100644 --- a/src/tsr/game.cpp +++ b/src/tsr/game.cpp @@ -1,3 +1,6 @@ +#define NOMINMAX +#include + #include "game.hpp" #include "entity_model.hpp" #include "entity_cct.hpp" @@ -11,6 +14,7 @@ using namespace TSR; entt::registry Game::s_registry; std::unique_ptr Game::s_physics_scene; std::unique_ptr Game::s_map; +std::map Game::s_timers; uint64_t Game::s_sv_frame = 0; float Game::s_frame_t = 0.0f; @@ -25,6 +29,8 @@ static entt::entity player; static AssetPtr s_skybox_model; +static uint32_t s_next_timer_id; + void Game::StartGame(const std::string& map_name) { s_sv_frame = 0; s_frame_t = 0.0f; @@ -34,9 +40,42 @@ void Game::StartGame(const std::string& map_name) { s_physics_scene = std::make_unique(); s_map = std::make_unique(map_name, s_physics_scene.get()); + s_next_timer_id = 0U; + + LuaAPI::Init(); + + LuaAPI::RegisterFunction("g_settimer", [](float interval, bool repeat, sol::function callback) { + auto& timer = s_timers[++s_next_timer_id]; + timer.last_frame = s_sv_frame; + timer.interval = interval; + timer.repeat = repeat; + timer.callback = callback; + return s_next_timer_id; + }); + + LuaAPI::RegisterFunction("g_cleartimer", [](uint32_t id) { + s_timers.erase(id); + }); + + LuaAPI::RegisterFunction("g_getframe", []() { + return s_sv_frame; + }); + + LuaAPI::RegisterFunction("g_gettps", []() { + return s_tps; + }); + + LuaAPI::RegisterFunction("g_require", [](const std::string& name) { + LuaAPI::Load(name); + }); + + LuaAPI::Load("scripts/autoexec.lua"); } void Game::EndGame() { + s_timers.clear(); + LuaAPI::Shutdown(); + s_registry.clear(); s_map.reset(); s_physics_scene.reset(); @@ -44,139 +83,147 @@ void Game::EndGame() { void Game::Run() { - Filesystem::Init("main"); - WindowWrapper ww("TSR test", 640, 480); - ////Audio::Init(); + try { - Physics::Init(); + Filesystem::Init("main"); + WindowWrapper ww("TSR test", 640, 480); + ////Audio::Init(); - Renderer renderer; + Physics::Init(); - StartGame("maps/kalbatest1.mapproject.json"); + Renderer renderer; - while (!s_map->Loaded()) - s_map->LoadNext(); + StartGame("maps/kalbatest1.mapproject.json"); - s_skybox_model = AssetMap::Get("models/skybox.mdl.json"); + while (!s_map->Loaded()) + s_map->LoadNext(); - //s_physics_scene->EnableDebug(false); + s_skybox_model = AssetMap::Get("models/skybox.mdl.json"); - for (int i = 0; i < 10; ++i) { - auto ent = Game::Registry().create(); - ModelSystem::AddModel(ent, "models/test_skeletal.mdl.json"); - ModelSystem::Anim(ent, "init"); - ModelSystem::Anim(ent, "ruce"); - //ModelSystem::Anim(ent, "stand"); - //ModelSystem::Anim(ent, "walk2", i * 13.0f); + //s_physics_scene->EnableDebug(false); - glm::vec3 pos(2.0f * (i / 10), 0.0f, 2.0f * (i % 10)); + for (int i = 0; i < 10; ++i) { + auto ent = Game::Registry().create(); + ModelSystem::AddModel(ent, "models/test_skeletal.mdl.json"); + ModelSystem::Anim(ent, "init"); + ModelSystem::Anim(ent, "ruce"); + //ModelSystem::Anim(ent, "stand"); + //ModelSystem::Anim(ent, "walk2", i * 13.0f); - auto& trans = Registry().emplace_or_replace(ent); - trans.trans.pos = pos; - trans.trans.rot = glm::quat(glm::vec3(0.0f)); - trans.trans.scl = glm::vec3(1.0f); + glm::vec3 pos(2.0f * (i / 10), 0.0f, 2.0f * (i % 10)); - } + auto& trans = Registry().emplace_or_replace(ent); + trans.trans.pos = pos; + trans.trans.rot = glm::quat(glm::vec3(0.0f)); + trans.trans.scl = glm::vec3(1.0f); - { - ent1 = Game::Registry().create(); - ModelSystem::AddModel(ent1, "models/skrin.mdl.json"); - ModelSystem::Anim(ent1, "init"); - ModelSystem::Anim(ent1, "open_left"); - ModelSystem::Anim(ent1, "open_right"); - auto& trans = Registry().emplace_or_replace(ent1); - glm::vec3 pos(0.0f, 5.0f, 0.0f); - trans.trans.pos = pos; - trans.trans.rot = glm::quat(glm::vec3(0.0f)); - trans.trans.scl = glm::vec3(1.0f); - } + } - { - player = Game::Registry().create(); - ModelSystem::AddModel(player, "models/test_skeletal.mdl.json"); - ModelSystem::Anim(player, "init"); - ModelSystem::Anim(player, "stand"); - auto& trans = Registry().emplace_or_replace(player); - glm::vec3 pos(0.0f, 5.0f, 0.0f); - trans.trans.pos = pos; - trans.trans.rot = glm::quat(glm::vec3(0.0f)); - trans.trans.scl = glm::vec3(1.0f); + { + ent1 = Game::Registry().create(); + ModelSystem::AddModel(ent1, "models/skrin.mdl.json"); + ModelSystem::Anim(ent1, "init"); + ModelSystem::Anim(ent1, "open_left"); + ModelSystem::Anim(ent1, "open_right"); + auto& trans = Registry().emplace_or_replace(ent1); + glm::vec3 pos(0.0f, 5.0f, 0.0f); + trans.trans.pos = pos; + trans.trans.rot = glm::quat(glm::vec3(0.0f)); + trans.trans.scl = glm::vec3(1.0f); + } - CCTSystem::AddCCT(player, 0.25f, 1.2f); + { + player = Game::Registry().create(); + ModelSystem::AddModel(player, "models/test_skeletal.mdl.json"); + ModelSystem::Anim(player, "init"); + ModelSystem::Anim(player, "stand"); + auto& trans = Registry().emplace_or_replace(player); + glm::vec3 pos(0.0f, 5.0f, 0.0f); + trans.trans.pos = pos; + trans.trans.rot = glm::quat(glm::vec3(0.0f)); + trans.trans.scl = glm::vec3(1.0f); - } + CCTSystem::AddCCT(player, 0.25f, 1.2f); - cam.third_person_distance = 5.0f; + } - Window::SetInputMode(GLFW_CURSOR, GLFW_CURSOR_DISABLED); - Window::SetCursorPosCallback([](GLFWwindow* window, double xpos, double ypos) { + cam.third_person_distance = 5.0f; - static float last_mouse_x = 0.0f, last_mouse_y = 0.0f; + Window::SetInputMode(GLFW_CURSOR, GLFW_CURSOR_DISABLED); + Window::SetCursorPosCallback([](GLFWwindow* window, double xpos, double ypos) { + + static float last_mouse_x = 0.0f, last_mouse_y = 0.0f; - double xoffset = xpos - last_mouse_x; - double yoffset = last_mouse_y - ypos; // reversed since y-coordinates range from bottom to top + double xoffset = xpos - last_mouse_x; + double yoffset = last_mouse_y - ypos; // reversed since y-coordinates range from bottom to top - cam.ProcessMouse(xoffset, yoffset); + cam.ProcessMouse(xoffset, yoffset); - last_mouse_x = xpos; - last_mouse_y = ypos; - }); + last_mouse_x = xpos; + last_mouse_y = ypos; + }); - Window::SetKeyCallback([](GLFWwindow* window, int key, int scancode, int action, int mods) { - if (action == GLFW_PRESS) { - switch (key) { - case GLFW_KEY_F1: - s_debug_draw = !s_debug_draw; - s_physics_scene->EnableDebug(s_debug_draw); - break; + Window::SetKeyCallback([](GLFWwindow* window, int key, int scancode, int action, int mods) { + if (action == GLFW_PRESS) { + switch (key) { + case GLFW_KEY_F1: + s_debug_draw = !s_debug_draw; + s_physics_scene->EnableDebug(s_debug_draw); + break; - case GLFW_KEY_E: - ModelSystem::Anim(ent1, "open_left"); - ModelSystem::Anim(ent1, "open_right"); - break; + case GLFW_KEY_E: + ModelSystem::Anim(ent1, "open_left"); + ModelSystem::Anim(ent1, "open_right"); + break; - case GLFW_KEY_Q: - ModelSystem::Anim(ent1, "close_left"); - ModelSystem::Anim(ent1, "close_right"); - break; + case GLFW_KEY_Q: + ModelSystem::Anim(ent1, "close_left"); + ModelSystem::Anim(ent1, "close_right"); + break; + } + + + } + }); + cam.movement_speed *= 0.1f; - } + Window::Show(); - }); + auto start_t = std::chrono::steady_clock::now(); + uint32_t time = 0U; + uint32_t real_time = 0U; - cam.movement_speed *= 0.1f; + uint32_t frame_time_ms = 1000U / s_tps; - Window::Show(); + glfwSwapInterval(0); - auto start_t = std::chrono::steady_clock::now(); - uint32_t time = 0U; - uint32_t real_time = 0U; + while (!Window::ShouldClose()) { - uint32_t frame_time_ms = 1000U / s_tps; + auto diff = std::chrono::steady_clock::now() - start_t; + real_time = std::chrono::duration_cast(diff).count(); - glfwSwapInterval(0); + if (!Window::KeyDown(GLFW_KEY_C)) { + while (time < real_time) { + SvFrame(); + time += frame_time_ms; + } - while (!Window::ShouldClose()) { - - auto diff = std::chrono::steady_clock::now() - start_t; - real_time = std::chrono::duration_cast(diff).count(); - - if (!Window::KeyDown(GLFW_KEY_C)) { - while (time < real_time) { - SvFrame(); - time += frame_time_ms; } + s_frame_t = (float)(real_time - time + frame_time_ms) / (float)frame_time_ms; + + ClFrame(renderer, 0.0f); } - s_frame_t = (float)(real_time - time + frame_time_ms) / (float)frame_time_ms; - - ClFrame(renderer, 0.0f); + } + catch (std::exception ex) { + TsrPrintf("ERROR: %s\n", ex.what()); + MessageBoxA(NULL, ex.what(), "ERROR", MB_ICONERROR); } EndGame(); @@ -199,6 +246,24 @@ void Game::SvFrame() { //if (Window::KeyDown(GLFW_KEY_LEFT_SHIFT)) // cam.ProcessMovement(Camera::Movement::DOWN, time); + // execute timers + for (auto it = s_timers.begin(); it != s_timers.end();) { + auto& timer = it->second; + if (s_sv_frame != timer.last_frame && s_sv_frame - timer.last_frame >= (uint32_t)(timer.interval * (float)s_tps)) { + timer.last_frame = s_sv_frame; + + sol::protected_function func = timer.callback; + func(); + + if (!timer.repeat) { + it = s_timers.erase(it); + continue; + } + } + + ++it; + } + float move_forward = 0.0f; float move_right = 0.0f; diff --git a/src/tsr/game.hpp b/src/tsr/game.hpp index df59f45..f120554 100644 --- a/src/tsr/game.hpp +++ b/src/tsr/game.hpp @@ -8,15 +8,25 @@ #include "renderer.hpp" #include "worldmap.hpp" #include "physics.hpp" +#include "luaapi.hpp" namespace TSR { + struct Timer { + float interval = 0.0f; + uint32_t last_frame = 0; + bool repeat = false; + sol::reference callback; + }; + class Game { static entt::registry s_registry; static std::unique_ptr s_map; static std::unique_ptr s_physics_scene; + static std::map s_timers; + static uint64_t s_sv_frame; static float s_frame_t; static int s_tps; diff --git a/src/tsr/luaapi.cpp b/src/tsr/luaapi.cpp new file mode 100644 index 0000000..7e95649 --- /dev/null +++ b/src/tsr/luaapi.cpp @@ -0,0 +1,27 @@ +#include "luaapi.hpp" +#include "filesystem.hpp" + +using namespace TSR; + +std::unique_ptr LuaAPI::s_lua; +std::set LuaAPI::s_loaded_scripts; + +void LuaAPI::Init() { + s_lua = std::make_unique(); + s_lua->open_libraries(sol::lib::base, sol::lib::math, sol::lib::string, sol::lib::table, sol::lib::debug, sol::lib::coroutine); + //s_lua->script(R"( + // print("Hello from Lua!") + //)"); +} + +void LuaAPI::Shutdown() { + s_lua.reset(); + s_loaded_scripts.clear(); +} + +void LuaAPI::Load(const std::string& name) { + s_loaded_scripts.insert(name); + + auto script = Filesystem::Read(name); + s_lua->safe_script(script, name); +} \ No newline at end of file diff --git a/src/tsr/luaapi.hpp b/src/tsr/luaapi.hpp new file mode 100644 index 0000000..11affe7 --- /dev/null +++ b/src/tsr/luaapi.hpp @@ -0,0 +1,35 @@ +#pragma once +#include +#include +#include +#include + +namespace TSR { + class LuaAPI { + + static std::unique_ptr s_lua; + static std::set s_loaded_scripts; + + public: + static void Init(); + static void Shutdown(); + + static void Load(const std::string& name); + + template + static void RegisterFunction(const std::string& name, Args&&... args) { + s_lua->set_function(name, std::forward(args)...); + } + + template + static void Call(const std::string& name, Args&&... args) { + sol::protected_function func = s_lua->get(name); + func(std::forward(args)...); + } + + static sol::state& GetState() { return *s_lua; } + + }; + + +} \ No newline at end of file diff --git a/src/tsr/physics.cpp b/src/tsr/physics.cpp index 6509e07..ede8d15 100644 --- a/src/tsr/physics.cpp +++ b/src/tsr/physics.cpp @@ -25,13 +25,13 @@ void Physics::Init() { bool recordMemoryAllocations = true; - auto mPvd = PxCreatePvd(*s_foundation); - PxPvdTransport* transport = PxDefaultPvdSocketTransportCreate("localhost", 5425, 10); - mPvd->connect(*transport, PxPvdInstrumentationFlag::eALL); + //auto mPvd = PxCreatePvd(*s_foundation); + //PxPvdTransport* transport = PxDefaultPvdSocketTransportCreate("localhost", 5425, 10); + //mPvd->connect(*transport, PxPvdInstrumentationFlag::eALL); PxTolerancesScale scale; - s_physics = PxCreatePhysics(PX_PHYSICS_VERSION, *s_foundation, scale, recordMemoryAllocations, mPvd); + s_physics = PxCreatePhysics(PX_PHYSICS_VERSION, *s_foundation, scale, recordMemoryAllocations, nullptr); if (!s_physics) Throw("(PX) PxCreatePhysics failed!"); @@ -44,9 +44,14 @@ void Physics::Init() { } void Physics::Close() { - s_cooking->release(); - s_physics->release(); - s_foundation->release(); + if (s_cooking) + s_cooking->release(); + + if (s_physics) + s_physics->release(); + + if (s_foundation) + s_foundation->release(); } PhysicsScene::PhysicsScene() { diff --git a/src/tsr/renderer.cpp b/src/tsr/renderer.cpp index 17ebb3c..9f83f4b 100644 --- a/src/tsr/renderer.cpp +++ b/src/tsr/renderer.cpp @@ -579,7 +579,7 @@ void Renderer::Render(int width, int height) { // DEBUG glDisable(GL_DEPTH_TEST); if (!m_debug_lines.empty()) { - glStencilFunc(GL_ALWAYS, 0, 0xFF); + //glStencilFunc(GL_ALWAYS, 0, 0xFF); m_shader_debug.Use(); glUniformMatrix4fv(m_shader_debug.U(SU_VP), 1, GL_FALSE, glm::value_ptr(mat_vp)); diff --git a/src/tsrecs.cpp b/src/tsrecs.cpp index e73437a..b5bbe48 100644 --- a/src/tsrecs.cpp +++ b/src/tsrecs.cpp @@ -1,34 +1,12 @@ #include #include -#include "tsr/filesystem.hpp" -#include "tsr/window.hpp" -#include "tsr/renderer.hpp" -#include "tsr/model.hpp" -#include "tsr/camera.hpp" -#include "tsr/game.hpp" -#include "tsr/entity_model.hpp" -#include -using namespace TSR; -Camera cam; +#include "tsr/game.hpp" int main() { TsrPrintf("Hello (client)\n"); - try { - Game::Run(); - - //Client::Run(); - - - - } - catch (std::exception ex) { - - TsrPrintf("ERROR: %s\n", ex.what()); - MessageBoxA(NULL, ex.what(), "ERROR", MB_ICONERROR); - } - //Audio::Close(); + TSR::Game::Run(); return 0; } diff --git a/tsrecs.vcxproj b/tsrecs.vcxproj index d8529d3..af50143 100644 --- a/tsrecs.vcxproj +++ b/tsrecs.vcxproj @@ -129,6 +129,7 @@ + @@ -144,6 +145,7 @@ + diff --git a/tsrecs.vcxproj.filters b/tsrecs.vcxproj.filters index 65ca523..34ae881 100644 --- a/tsrecs.vcxproj.filters +++ b/tsrecs.vcxproj.filters @@ -54,6 +54,9 @@ Source Files + + Source Files + @@ -101,5 +104,8 @@ Header Files + + Header Files + \ No newline at end of file