diff --git a/.gitignore b/.gitignore index 1c643b3..849bb0f 100644 --- a/.gitignore +++ b/.gitignore @@ -365,4 +365,6 @@ FodyWeavers.xsd #TSR -main/ \ No newline at end of file +main/* +!main/scripts +!main/shaders \ No newline at end of file diff --git a/src/tsr/entity.cpp b/src/tsr/entity.cpp new file mode 100644 index 0000000..54ba2de --- /dev/null +++ b/src/tsr/entity.cpp @@ -0,0 +1,33 @@ +#include "entity.hpp" + +using namespace TSR; + +entt::entity EntitySystem::Spawn(const Transform& trans) { + auto& reg = Game::Registry(); + auto ent = reg.create(); + auto& comp = reg.emplace(ent); + comp.trans = trans; + return ent; +} + +void EntitySystem::Despawn(entt::entity ent) { + Game::Registry().destroy(ent); +} + +void EntitySystem::RegisterLuaFunctions() { + LuaAPI::RegisterFunction("ent_spawn", [](float x, float y, float z, float pitch, float yaw, float roll, float sx, float sy, float sz) { + Transform trans; + trans.pos.x = x; + trans.pos.y = y; + trans.pos.z = z; + trans.rot = glm::quat(glm::vec3(pitch, yaw, roll)); + trans.scl.x = sx; + trans.scl.y = sy; + trans.scl.z = sz; + return EntitySystem::Spawn(trans); + }); + + LuaAPI::RegisterFunction("ent_despawn", [](entt::entity ent) { + EntitySystem::Despawn(ent); + }); +} diff --git a/src/tsr/entity.hpp b/src/tsr/entity.hpp index c516998..ccfae1c 100644 --- a/src/tsr/entity.hpp +++ b/src/tsr/entity.hpp @@ -1,3 +1,23 @@ #pragma once #include -#include "game.hpp" \ No newline at end of file +#include "game.hpp" + +namespace TSR { + + struct TransformComponent { + Transform trans; + }; + + class EntitySystem { + + public: + + static entt::entity Spawn(const Transform& trans); + static void Despawn(entt::entity ent); + + static void RegisterLuaFunctions(); + + }; + + +} \ No newline at end of file diff --git a/src/tsr/entity_cct.cpp b/src/tsr/entity_cct.cpp index eb95e3a..33e0e95 100644 --- a/src/tsr/entity_cct.cpp +++ b/src/tsr/entity_cct.cpp @@ -37,8 +37,10 @@ static glm::vec3 CCTGetPos(CCTComponent& comp) { return glm::vec3(pos_px.x, pos_px.y, pos_px.z); } -static bool CCTTurnToAngle(CCTComponent& comp, float target, float step) { - if (target < comp.angle) +static bool CCTTurnToAngle(CCTComponent& comp, float target, float speed) { + auto step = speed / Game::TPS(); + + while (target < comp.angle) target += glm::two_pi(); auto diff_angle = target - comp.angle; @@ -121,24 +123,30 @@ void CCTSystem::SetPos(entt::entity ent, const glm::vec3& pos) { CCTSetPos(comp, pos); } -glm::vec3 TSR::CCTSystem::GetPos(entt::entity ent) { +glm::vec3 CCTSystem::GetPos(entt::entity ent) { auto& reg = Game::Registry(); auto& comp = reg.get(ent); return CCTGetPos(comp); } -bool CCTSystem::TurnTo(entt::entity ent, const glm::vec3& pos, float step) { +glm::vec3 CCTSystem::GetForward(entt::entity ent) { + auto& reg = Game::Registry(); + auto& comp = reg.get(ent); + return glm::vec3(std::sin(comp.angle), 0.0f, std::cos(comp.angle)); +} + +bool CCTSystem::TurnTo(entt::entity ent, const glm::vec3& pos, float speed) { auto& reg = Game::Registry(); auto& comp = reg.get(ent); - return CCTTurnTo(comp, pos, step); + return CCTTurnTo(comp, pos, speed); } -bool TSR::CCTSystem::TurnToAngle(entt::entity ent, float target, float step) { +bool CCTSystem::TurnToAngle(entt::entity ent, float target, float speed) { auto& reg = Game::Registry(); auto& comp = reg.get(ent); - return CCTTurnToAngle(comp, target, step); + return CCTTurnToAngle(comp, target, speed); } void CCTSystem::Update() { @@ -152,3 +160,42 @@ void CCTSystem::Update() { } } + +void TSR::CCTSystem::RegisterLuaFunctions() { + LuaAPI::RegisterFunction("cct_add", [](entt::entity ent, float radius, float height) { + CCTSystem::AddCCT(ent, radius, height); + }); + + LuaAPI::RegisterFunction("cct_remove", [](entt::entity ent) { + CCTSystem::RemoveCCT(ent); + }); + + LuaAPI::RegisterFunction("cct_move", [](entt::entity ent, float x, float y, float z) { + auto disp = glm::vec3(x, y, z); + return CCTSystem::Move(ent, disp, 1.0f / Game::TPS()); + }); + + LuaAPI::RegisterFunction("cct_setpos", [](entt::entity ent, float x, float y, float z) { + auto pos = glm::vec3(x, y, z); + CCTSystem::SetPos(ent, pos); + }); + + LuaAPI::RegisterFunction("cct_getpos", [](entt::entity ent) { + auto pos = CCTSystem::GetPos(ent); + return std::make_tuple(pos.x, pos.y, pos.z); + }); + + LuaAPI::RegisterFunction("cct_getforwardxz", [](entt::entity ent) { + auto forward = CCTSystem::GetForward(ent); + return std::make_tuple(forward.x, forward.z); + }); + + LuaAPI::RegisterFunction("cct_turnto", [](entt::entity ent, float x, float y, float z, float speed) { + auto pos = glm::vec3(x, y, z); + return CCTSystem::TurnTo(ent, pos, speed); + }); + + LuaAPI::RegisterFunction("cct_turntoangle", [](entt::entity ent, float angle, float speed) { + return CCTSystem::TurnToAngle(ent, angle, speed); + }); +} diff --git a/src/tsr/entity_cct.hpp b/src/tsr/entity_cct.hpp index 5fecb3f..6d99351 100644 --- a/src/tsr/entity_cct.hpp +++ b/src/tsr/entity_cct.hpp @@ -27,10 +27,15 @@ namespace TSR { static int Move(entt::entity ent, const glm::vec3& disp, float dt); static void SetPos(entt::entity ent, const glm::vec3& pos); static glm::vec3 GetPos(entt::entity ent); + + static glm::vec3 GetForward(entt::entity ent); + static bool TurnTo(entt::entity ent, const glm::vec3& pos, float step); static bool TurnToAngle(entt::entity ent, float target, float step); static void Update(); + static void RegisterLuaFunctions(); + }; } \ No newline at end of file diff --git a/src/tsr/entity_model.cpp b/src/tsr/entity_model.cpp index 3427493..a768ea0 100644 --- a/src/tsr/entity_model.cpp +++ b/src/tsr/entity_model.cpp @@ -36,7 +36,7 @@ void ModelSystem::AddModel(entt::entity ent, const std::string& name) { } } -void TSR::ModelSystem::RemoveModel(entt::entity ent) { +void ModelSystem::RemoveModel(entt::entity ent) { auto& reg = Game::Registry(); reg.erase(ent); //reg.erase(ent); @@ -241,4 +241,22 @@ void ModelSystem::Render(TSR::Renderer& renderer) { //World::Get().Reg() -} \ No newline at end of file +} + +void ModelSystem::RegisterLuaFunctions() { + LuaAPI::RegisterFunction("mdl_set", [](entt::entity ent, const std::string& name) { + ModelSystem::AddModel(ent, name); + }); + + LuaAPI::RegisterFunction("mdl_remove", [](entt::entity ent) { + ModelSystem::RemoveModel(ent); + }); + + LuaAPI::RegisterFunction("mdl_anim", [](entt::entity ent, const std::string& anim_name, float t, float fade_in_time) { + ModelSystem::Anim(ent, anim_name, t, fade_in_time); + }); + + LuaAPI::RegisterFunction("mdl_stopanim", [](entt::entity ent, const std::string& anim_name, float fade_out_time) { + ModelSystem::StopAnim(ent, anim_name, fade_out_time); + }); +} diff --git a/src/tsr/entity_model.hpp b/src/tsr/entity_model.hpp index b49e3ee..616226c 100644 --- a/src/tsr/entity_model.hpp +++ b/src/tsr/entity_model.hpp @@ -43,7 +43,7 @@ namespace TSR { static void Animate(); static void Render(Renderer& renderer); - + static void RegisterLuaFunctions(); }; diff --git a/src/tsr/game.cpp b/src/tsr/game.cpp index 6dd1212..c2e5245 100644 --- a/src/tsr/game.cpp +++ b/src/tsr/game.cpp @@ -8,6 +8,10 @@ #include "camera.hpp" #include "worldmap.hpp" #include "physics.hpp" +#include "input.hpp" +#include "entity.hpp" +#include "entity_model.hpp" +#include "entity_cct.hpp" using namespace TSR; @@ -31,6 +35,10 @@ static AssetPtr s_skybox_model; static uint32_t s_next_timer_id; +static bool s_camera_attached = false; +static entt::entity s_camera_attach_ent; +static glm::vec3 s_camera_attach_offset; + void Game::StartGame(const std::string& map_name) { s_sv_frame = 0; s_frame_t = 0.0f; @@ -44,36 +52,67 @@ void Game::StartGame(const std::string& map_name) { LuaAPI::Init(); - LuaAPI::RegisterFunction("g_settimer", [](float interval, bool repeat, sol::function callback) { + RegisterLuaFunctions(); + Input::RegisterLuaFunctions(); + EntitySystem::RegisterLuaFunctions(); + ModelSystem::RegisterLuaFunctions(); + CCTSystem::RegisterLuaFunctions(); + + LuaAPI::Load("scripts/autoexec.lua"); +} + +void Game::RegisterLuaFunctions() { + LuaAPI::RegisterFunction("g_settimer", [](float interval, bool repeat, LuaFunction 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; - }); + return s_sv_frame; + }); LuaAPI::RegisterFunction("g_gettps", []() { - return s_tps; - }); + return s_tps; + }); LuaAPI::RegisterFunction("g_require", [](const std::string& name) { - LuaAPI::Load(name); + LuaAPI::Load(name); + }); + + LuaAPI::RegisterFunction("g_getcameraforward", []() { + return std::make_tuple(cam.forward_backward_vector.x, cam.forward_backward_vector.y, cam.forward_backward_vector.z); + }); + + LuaAPI::RegisterFunction("g_getcameraforwardxz", []() { + return std::make_tuple(cam.forward_backward_vector.x, cam.forward_backward_vector.z); + }); + + LuaAPI::RegisterFunction("g_getcamerarightxz", []() { + return std::make_tuple(cam.right_vector.x, cam.right_vector.z); + }); + + LuaAPI::RegisterFunction("g_attachcamera", [](entt::entity ent, float x, float y, float z) { + s_camera_attached = true; + s_camera_attach_ent = ent; + s_camera_attach_offset = glm::vec3(x, y, z); }); - LuaAPI::Load("scripts/autoexec.lua"); + LuaAPI::RegisterFunction("g_detachcamera", []() { + s_camera_attached = false; + }); } void Game::EndGame() { s_timers.clear(); + Input::Shutdown(); LuaAPI::Shutdown(); s_registry.clear(); @@ -100,53 +139,6 @@ void Game::Run() { s_skybox_model = AssetMap::Get("models/skybox.mdl.json"); - //s_physics_scene->EnableDebug(false); - - 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); - - 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); - - CCTSystem::AddCCT(player, 0.25f, 1.2f); - - } - cam.third_person_distance = 5.0f; Window::SetInputMode(GLFW_CURSOR, GLFW_CURSOR_DISABLED); @@ -171,23 +163,12 @@ void Game::Run() { 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_Q: - ModelSystem::Anim(ent1, "close_left"); - ModelSystem::Anim(ent1, "close_right"); - break; } - - - } + if (action == GLFW_PRESS || action == GLFW_RELEASE) + Input::AddControlEvent(Input::ControlFromGlfwKey(key), action == GLFW_PRESS); + }); cam.movement_speed *= 0.1f; @@ -215,7 +196,7 @@ void Game::Run() { } - s_frame_t = (float)(real_time - time + frame_time_ms) / (float)frame_time_ms; + s_frame_t = glm::min((float)(real_time - time + frame_time_ms) / (float)frame_time_ms, 1.0f); ClFrame(renderer, 0.0f); } @@ -233,18 +214,7 @@ void Game::Run() { void Game::SvFrame() { float time = 1.0f / (float)s_tps; - //if (Window::KeyDown(GLFW_KEY_W)) - // cam.ProcessMovement(Camera::Movement::FORWARD, time); - //if (Window::KeyDown(GLFW_KEY_S)) - // cam.ProcessMovement(Camera::Movement::BACKWARD, time); - //if (Window::KeyDown(GLFW_KEY_A)) - // cam.ProcessMovement(Camera::Movement::LEFT, time); - //if (Window::KeyDown(GLFW_KEY_D)) - // cam.ProcessMovement(Camera::Movement::RIGHT, time); - //if (Window::KeyDown(GLFW_KEY_SPACE)) - // cam.ProcessMovement(Camera::Movement::UP, time); - //if (Window::KeyDown(GLFW_KEY_LEFT_SHIFT)) - // cam.ProcessMovement(Camera::Movement::DOWN, time); + Input::ProcessEvents(); // execute timers for (auto it = s_timers.begin(); it != s_timers.end();) { @@ -252,8 +222,7 @@ void Game::SvFrame() { 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(); + LuaAPI::Call(timer.callback); if (!timer.repeat) { it = s_timers.erase(it); @@ -264,59 +233,6 @@ void Game::SvFrame() { ++it; } - float move_forward = 0.0f; - float move_right = 0.0f; - - if (Window::KeyDown(GLFW_KEY_W)) - move_forward += 1.0f; - if (Window::KeyDown(GLFW_KEY_S)) - move_forward -= 1.0f; - if (Window::KeyDown(GLFW_KEY_A)) - move_right -= 1.0f; - if (Window::KeyDown(GLFW_KEY_D)) - move_right += 1.0f; - - static bool walking = false; - - if (move_forward != 0.0f || move_right != 0.0f) { - auto move_vector = cam.forward_backward_vector * move_forward + cam.right_vector * move_right; - - auto move_angle = std::atan2(move_vector.x, move_vector.z); - - CCTSystem::TurnToAngle(player, move_angle, 6.0f / TPS()); - - auto& cctcomp = Registry().get(player); - - glm::vec3 forward_vector(std::sin(cctcomp.angle), -1.0f, std::cos(cctcomp.angle)); - CCTSystem::Move(player, forward_vector * (3.0f / TPS()), time); - - if (!walking) { - walking = true; - ModelSystem::Anim(player, "walk2", 6.0f, 0.2f); - } - } - else { - if (walking) { - walking = false; - ModelSystem::StopAnim(player, "walk2", 0.1f); - } - } - - static bool aiming = false; - - if (Window::KeyDown(GLFW_KEY_E)) { - if (!aiming) { - aiming = true; - ModelSystem::Anim(player, "ruce", 0.0f, 0.2f); - } - } - else { - if (aiming) { - aiming = false; - ModelSystem::StopAnim(player, "ruce", 0.2f); - } - } - CCTSystem::Update(); ModelSystem::Animate(); @@ -340,7 +256,10 @@ void Game::ClFrame(Renderer& renderer, float frame_t) { if (s_skybox_model) s_skybox_model->Draw(renderer, nullptr); - cam.position = glm::vec3(Registry().get(player).ctx.model_matrix[3]) + glm::vec3(0.0f, 1.0f, 0.0f); + if (s_camera_attached) { + cam.position = glm::vec3(Registry().get(s_camera_attach_ent).ctx.model_matrix[3]) + s_camera_attach_offset; + } + renderer.SetViewMatrix(cam.GetViewMatrix()); renderer.Render(w, h); diff --git a/src/tsr/game.hpp b/src/tsr/game.hpp index f120554..cde4824 100644 --- a/src/tsr/game.hpp +++ b/src/tsr/game.hpp @@ -16,7 +16,7 @@ namespace TSR { float interval = 0.0f; uint32_t last_frame = 0; bool repeat = false; - sol::reference callback; + LuaReference callback; }; class Game { @@ -49,12 +49,9 @@ namespace TSR { static float FrameT() { return s_frame_t; } static int TPS() { return s_tps; } + static void RegisterLuaFunctions(); }; - - struct TransformComponent { - Transform trans; - }; } diff --git a/src/tsr/input.cpp b/src/tsr/input.cpp new file mode 100644 index 0000000..c183726 --- /dev/null +++ b/src/tsr/input.cpp @@ -0,0 +1,535 @@ +#include "input.hpp" + +using namespace TSR; + +const Control Input::s_glfw_key_map[] = { + KEY_NONE, // 0 + KEY_NONE, // 1 + KEY_NONE, // 2 + KEY_NONE, // 3 + KEY_NONE, // 4 + KEY_NONE, // 5 + KEY_NONE, // 6 + KEY_NONE, // 7 + KEY_NONE, // 8 + KEY_NONE, // 9 + KEY_NONE, // 10 + KEY_NONE, // 11 + KEY_NONE, // 12 + KEY_NONE, // 13 + KEY_NONE, // 14 + KEY_NONE, // 15 + KEY_NONE, // 16 + KEY_NONE, // 17 + KEY_NONE, // 18 + KEY_NONE, // 19 + KEY_NONE, // 20 + KEY_NONE, // 21 + KEY_NONE, // 22 + KEY_NONE, // 23 + KEY_NONE, // 24 + KEY_NONE, // 25 + KEY_NONE, // 26 + KEY_NONE, // 27 + KEY_NONE, // 28 + KEY_NONE, // 29 + KEY_NONE, // 30 + KEY_NONE, // 31 + KEY_SPACE, // 32 + KEY_NONE, // 33 + KEY_NONE, // 34 + KEY_NONE, // 35 + KEY_NONE, // 36 + KEY_NONE, // 37 + KEY_NONE, // 38 + KEY_APOSTROPHE, // 39 + KEY_NONE, // 40 + KEY_NONE, // 41 + KEY_NONE, // 42 + KEY_NONE, // 43 + KEY_COMMA, // 44 + KEY_MINUS, // 45 + KEY_PERIOD, // 46 + KEY_SLASH, // 47 + KEY_0, // 48 + KEY_1, // 49 + KEY_2, // 50 + KEY_3, // 51 + KEY_4, // 52 + KEY_5, // 53 + KEY_6, // 54 + KEY_7, // 55 + KEY_8, // 56 + KEY_9, // 57 + KEY_NONE, // 58 + KEY_SEMICOLON, // 59 + KEY_NONE, // 60 + KEY_EQUAL, // 61 + KEY_NONE, // 62 + KEY_NONE, // 63 + KEY_NONE, // 64 + KEY_A, // 65 + KEY_B, // 66 + KEY_C, // 67 + KEY_D, // 68 + KEY_E, // 69 + KEY_F, // 70 + KEY_G, // 71 + KEY_H, // 72 + KEY_I, // 73 + KEY_J, // 74 + KEY_K, // 75 + KEY_L, // 76 + KEY_M, // 77 + KEY_N, // 78 + KEY_O, // 79 + KEY_P, // 80 + KEY_Q, // 81 + KEY_R, // 82 + KEY_S, // 83 + KEY_T, // 84 + KEY_U, // 85 + KEY_V, // 86 + KEY_W, // 87 + KEY_X, // 88 + KEY_Y, // 89 + KEY_Z, // 90 + KEY_LEFT_BRACKET, // 91 + KEY_BACKSLASH, // 92 + KEY_RIGHT_BRACKET, // 93 + KEY_NONE, // 94 + KEY_NONE, // 95 + KEY_GRAVE_ACCENT, // 96 + KEY_NONE, // 97 + KEY_NONE, // 98 + KEY_NONE, // 99 + KEY_NONE, // 100 + KEY_NONE, // 101 + KEY_NONE, // 102 + KEY_NONE, // 103 + KEY_NONE, // 104 + KEY_NONE, // 105 + KEY_NONE, // 106 + KEY_NONE, // 107 + KEY_NONE, // 108 + KEY_NONE, // 109 + KEY_NONE, // 110 + KEY_NONE, // 111 + KEY_NONE, // 112 + KEY_NONE, // 113 + KEY_NONE, // 114 + KEY_NONE, // 115 + KEY_NONE, // 116 + KEY_NONE, // 117 + KEY_NONE, // 118 + KEY_NONE, // 119 + KEY_NONE, // 120 + KEY_NONE, // 121 + KEY_NONE, // 122 + KEY_NONE, // 123 + KEY_NONE, // 124 + KEY_NONE, // 125 + KEY_NONE, // 126 + KEY_NONE, // 127 + KEY_NONE, // 128 + KEY_NONE, // 129 + KEY_NONE, // 130 + KEY_NONE, // 131 + KEY_NONE, // 132 + KEY_NONE, // 133 + KEY_NONE, // 134 + KEY_NONE, // 135 + KEY_NONE, // 136 + KEY_NONE, // 137 + KEY_NONE, // 138 + KEY_NONE, // 139 + KEY_NONE, // 140 + KEY_NONE, // 141 + KEY_NONE, // 142 + KEY_NONE, // 143 + KEY_NONE, // 144 + KEY_NONE, // 145 + KEY_NONE, // 146 + KEY_NONE, // 147 + KEY_NONE, // 148 + KEY_NONE, // 149 + KEY_NONE, // 150 + KEY_NONE, // 151 + KEY_NONE, // 152 + KEY_NONE, // 153 + KEY_NONE, // 154 + KEY_NONE, // 155 + KEY_NONE, // 156 + KEY_NONE, // 157 + KEY_NONE, // 158 + KEY_NONE, // 159 + KEY_NONE, // 160 + KEY_WORLD_1, // 161 + KEY_WORLD_2, // 162 + KEY_NONE, // 163 + KEY_NONE, // 164 + KEY_NONE, // 165 + KEY_NONE, // 166 + KEY_NONE, // 167 + KEY_NONE, // 168 + KEY_NONE, // 169 + KEY_NONE, // 170 + KEY_NONE, // 171 + KEY_NONE, // 172 + KEY_NONE, // 173 + KEY_NONE, // 174 + KEY_NONE, // 175 + KEY_NONE, // 176 + KEY_NONE, // 177 + KEY_NONE, // 178 + KEY_NONE, // 179 + KEY_NONE, // 180 + KEY_NONE, // 181 + KEY_NONE, // 182 + KEY_NONE, // 183 + KEY_NONE, // 184 + KEY_NONE, // 185 + KEY_NONE, // 186 + KEY_NONE, // 187 + KEY_NONE, // 188 + KEY_NONE, // 189 + KEY_NONE, // 190 + KEY_NONE, // 191 + KEY_NONE, // 192 + KEY_NONE, // 193 + KEY_NONE, // 194 + KEY_NONE, // 195 + KEY_NONE, // 196 + KEY_NONE, // 197 + KEY_NONE, // 198 + KEY_NONE, // 199 + KEY_NONE, // 200 + KEY_NONE, // 201 + KEY_NONE, // 202 + KEY_NONE, // 203 + KEY_NONE, // 204 + KEY_NONE, // 205 + KEY_NONE, // 206 + KEY_NONE, // 207 + KEY_NONE, // 208 + KEY_NONE, // 209 + KEY_NONE, // 210 + KEY_NONE, // 211 + KEY_NONE, // 212 + KEY_NONE, // 213 + KEY_NONE, // 214 + KEY_NONE, // 215 + KEY_NONE, // 216 + KEY_NONE, // 217 + KEY_NONE, // 218 + KEY_NONE, // 219 + KEY_NONE, // 220 + KEY_NONE, // 221 + KEY_NONE, // 222 + KEY_NONE, // 223 + KEY_NONE, // 224 + KEY_NONE, // 225 + KEY_NONE, // 226 + KEY_NONE, // 227 + KEY_NONE, // 228 + KEY_NONE, // 229 + KEY_NONE, // 230 + KEY_NONE, // 231 + KEY_NONE, // 232 + KEY_NONE, // 233 + KEY_NONE, // 234 + KEY_NONE, // 235 + KEY_NONE, // 236 + KEY_NONE, // 237 + KEY_NONE, // 238 + KEY_NONE, // 239 + KEY_NONE, // 240 + KEY_NONE, // 241 + KEY_NONE, // 242 + KEY_NONE, // 243 + KEY_NONE, // 244 + KEY_NONE, // 245 + KEY_NONE, // 246 + KEY_NONE, // 247 + KEY_NONE, // 248 + KEY_NONE, // 249 + KEY_NONE, // 250 + KEY_NONE, // 251 + KEY_NONE, // 252 + KEY_NONE, // 253 + KEY_NONE, // 254 + KEY_NONE, // 255 + KEY_ESCAPE, // 256 + KEY_ENTER, // 257 + KEY_TAB, // 258 + KEY_BACKSPACE, // 259 + KEY_INSERT, // 260 + KEY_DELETE, // 261 + KEY_RIGHT, // 262 + KEY_LEFT, // 263 + KEY_DOWN, // 264 + KEY_UP, // 265 + KEY_PAGE_UP, // 266 + KEY_PAGE_DOWN, // 267 + KEY_HOME, // 268 + KEY_END, // 269 + KEY_NONE, // 270 + KEY_NONE, // 271 + KEY_NONE, // 272 + KEY_NONE, // 273 + KEY_NONE, // 274 + KEY_NONE, // 275 + KEY_NONE, // 276 + KEY_NONE, // 277 + KEY_NONE, // 278 + KEY_NONE, // 279 + KEY_CAPS_LOCK, // 280 + KEY_SCROLL_LOCK, // 281 + KEY_NUM_LOCK, // 282 + KEY_PRINT_SCREEN, // 283 + KEY_PAUSE, // 284 + KEY_NONE, // 285 + KEY_NONE, // 286 + KEY_NONE, // 287 + KEY_NONE, // 288 + KEY_NONE, // 289 + KEY_F1, // 290 + KEY_F2, // 291 + KEY_F3, // 292 + KEY_F4, // 293 + KEY_F5, // 294 + KEY_F6, // 295 + KEY_F7, // 296 + KEY_F8, // 297 + KEY_F9, // 298 + KEY_F10, // 299 + KEY_F11, // 300 + KEY_F12, // 301 + KEY_F13, // 302 + KEY_F14, // 303 + KEY_F15, // 304 + KEY_F16, // 305 + KEY_F17, // 306 + KEY_F18, // 307 + KEY_F19, // 308 + KEY_F20, // 309 + KEY_F21, // 310 + KEY_F22, // 311 + KEY_F23, // 312 + KEY_F24, // 313 + KEY_F25, // 314 + KEY_NONE, // 315 + KEY_NONE, // 316 + KEY_NONE, // 317 + KEY_NONE, // 318 + KEY_NONE, // 319 + KEY_KP_0, // 320 + KEY_KP_1, // 321 + KEY_KP_2, // 322 + KEY_KP_3, // 323 + KEY_KP_4, // 324 + KEY_KP_5, // 325 + KEY_KP_6, // 326 + KEY_KP_7, // 327 + KEY_KP_8, // 328 + KEY_KP_9, // 329 + KEY_KP_DECIMAL, // 330 + KEY_KP_DIVIDE, // 331 + KEY_KP_MULTIPLY, // 332 + KEY_KP_SUBTRACT, // 333 + KEY_KP_ADD, // 334 + KEY_KP_ENTER, // 335 + KEY_KP_EQUAL, // 336 + KEY_NONE, // 337 + KEY_NONE, // 338 + KEY_NONE, // 339 + KEY_LEFT_SHIFT, // 340 + KEY_LEFT_CONTROL, // 341 + KEY_LEFT_ALT, // 342 + KEY_LEFT_SUPER, // 343 + KEY_RIGHT_SHIFT, // 344 + KEY_RIGHT_CONTROL, // 345 + KEY_RIGHT_ALT, // 346 + KEY_RIGHT_SUPER, // 347 + KEY_MENU, // 348 +}; + +const Control Input::s_glfw_mouse_map[] = { + MOUSE_1, // 0 + MOUSE_2, // 1 + MOUSE_3, // 2 + MOUSE_4, // 3 + MOUSE_5, // 4 + MOUSE_6, // 5 +}; + +const char* const Input::s_control_names[] = { + "KEY_NONE", + "KEY_SPACE", + "KEY_APOSTROPHE", + "KEY_COMMA", + "KEY_MINUS", + "KEY_PERIOD", + "KEY_SLASH", + "KEY_0", + "KEY_1", + "KEY_2", + "KEY_3", + "KEY_4", + "KEY_5", + "KEY_6", + "KEY_7", + "KEY_8", + "KEY_9", + "KEY_SEMICOLON", + "KEY_EQUAL", + "KEY_A", + "KEY_B", + "KEY_C", + "KEY_D", + "KEY_E", + "KEY_F", + "KEY_G", + "KEY_H", + "KEY_I", + "KEY_J", + "KEY_K", + "KEY_L", + "KEY_M", + "KEY_N", + "KEY_O", + "KEY_P", + "KEY_Q", + "KEY_R", + "KEY_S", + "KEY_T", + "KEY_U", + "KEY_V", + "KEY_W", + "KEY_X", + "KEY_Y", + "KEY_Z", + "KEY_LEFT_BRACKET", + "KEY_BACKSLASH", + "KEY_RIGHT_BRACKET", + "KEY_GRAVE_ACCENT", + "KEY_WORLD_1", + "KEY_WORLD_2", + "KEY_ESCAPE", + "KEY_ENTER", + "KEY_TAB", + "KEY_BACKSPACE", + "KEY_INSERT", + "KEY_DELETE", + "KEY_RIGHT", + "KEY_LEFT", + "KEY_DOWN", + "KEY_UP", + "KEY_PAGE_UP", + "KEY_PAGE_DOWN", + "KEY_HOME", + "KEY_END", + "KEY_CAPS_LOCK", + "KEY_SCROLL_LOCK", + "KEY_NUM_LOCK", + "KEY_PRINT_SCREEN", + "KEY_PAUSE", + "KEY_F1", + "KEY_F2", + "KEY_F3", + "KEY_F4", + "KEY_F5", + "KEY_F6", + "KEY_F7", + "KEY_F8", + "KEY_F9", + "KEY_F10", + "KEY_F11", + "KEY_F12", + "KEY_F13", + "KEY_F14", + "KEY_F15", + "KEY_F16", + "KEY_F17", + "KEY_F18", + "KEY_F19", + "KEY_F20", + "KEY_F21", + "KEY_F22", + "KEY_F23", + "KEY_F24", + "KEY_F25", + "KEY_KP_0", + "KEY_KP_1", + "KEY_KP_2", + "KEY_KP_3", + "KEY_KP_4", + "KEY_KP_5", + "KEY_KP_6", + "KEY_KP_7", + "KEY_KP_8", + "KEY_KP_9", + "KEY_KP_DECIMAL", + "KEY_KP_DIVIDE", + "KEY_KP_MULTIPLY", + "KEY_KP_SUBTRACT", + "KEY_KP_ADD", + "KEY_KP_ENTER", + "KEY_KP_EQUAL", + "KEY_LEFT_SHIFT", + "KEY_LEFT_CONTROL", + "KEY_LEFT_ALT", + "KEY_LEFT_SUPER", + "KEY_RIGHT_SHIFT", + "KEY_RIGHT_CONTROL", + "KEY_RIGHT_ALT", + "KEY_RIGHT_SUPER", + "KEY_MENU", + "MOUSE_1", + "MOUSE_2", + "MOUSE_3", + "MOUSE_4", + "MOUSE_5", + "MOUSE_6", +}; + +std::vector Input::s_control_events; +LuaFunction Input::s_control_cb; + +Control Input::ControlFromGlfwKey(int key) { + return s_glfw_key_map[key]; +} + +Control Input::ControlFromGlfwMouseButton(int button) { + return s_glfw_mouse_map[button]; +} + +void Input::AddControlEvent(Control control, bool press) { + s_control_events.push_back({ control, press }); +} + +void Input::RegisterLuaFunctions() { + LuaAPI::RegisterFunction("i_setcontrolcallback", [](LuaFunction callback) { + s_control_cb = callback; + }); + +} + +void Input::ProcessEvents() { + if (s_control_cb) { + for (const auto& event : s_control_events) { + LuaAPI::Call(s_control_cb, s_control_names[event.control], event.press); + } + } + + s_control_events.clear(); +} + +void Input::Shutdown() { + s_control_cb = LuaFunction(); + s_control_events.clear(); +} + + + + diff --git a/src/tsr/input.hpp b/src/tsr/input.hpp new file mode 100644 index 0000000..765870b --- /dev/null +++ b/src/tsr/input.hpp @@ -0,0 +1,168 @@ +#pragma once + +#include +#include +#include "luaapi.hpp" + +namespace TSR { + + enum Control { + KEY_NONE, + KEY_SPACE, + KEY_APOSTROPHE, + KEY_COMMA, + KEY_MINUS, + KEY_PERIOD, + KEY_SLASH, + KEY_0, + KEY_1, + KEY_2, + KEY_3, + KEY_4, + KEY_5, + KEY_6, + KEY_7, + KEY_8, + KEY_9, + KEY_SEMICOLON, + KEY_EQUAL, + KEY_A, + KEY_B, + KEY_C, + KEY_D, + KEY_E, + KEY_F, + KEY_G, + KEY_H, + KEY_I, + KEY_J, + KEY_K, + KEY_L, + KEY_M, + KEY_N, + KEY_O, + KEY_P, + KEY_Q, + KEY_R, + KEY_S, + KEY_T, + KEY_U, + KEY_V, + KEY_W, + KEY_X, + KEY_Y, + KEY_Z, + KEY_LEFT_BRACKET, + KEY_BACKSLASH, + KEY_RIGHT_BRACKET, + KEY_GRAVE_ACCENT, + KEY_WORLD_1, + KEY_WORLD_2, + KEY_ESCAPE, + KEY_ENTER, + KEY_TAB, + KEY_BACKSPACE, + KEY_INSERT, + KEY_DELETE, + KEY_RIGHT, + KEY_LEFT, + KEY_DOWN, + KEY_UP, + KEY_PAGE_UP, + KEY_PAGE_DOWN, + KEY_HOME, + KEY_END, + KEY_CAPS_LOCK, + KEY_SCROLL_LOCK, + KEY_NUM_LOCK, + KEY_PRINT_SCREEN, + KEY_PAUSE, + KEY_F1, + KEY_F2, + KEY_F3, + KEY_F4, + KEY_F5, + KEY_F6, + KEY_F7, + KEY_F8, + KEY_F9, + KEY_F10, + KEY_F11, + KEY_F12, + KEY_F13, + KEY_F14, + KEY_F15, + KEY_F16, + KEY_F17, + KEY_F18, + KEY_F19, + KEY_F20, + KEY_F21, + KEY_F22, + KEY_F23, + KEY_F24, + KEY_F25, + KEY_KP_0, + KEY_KP_1, + KEY_KP_2, + KEY_KP_3, + KEY_KP_4, + KEY_KP_5, + KEY_KP_6, + KEY_KP_7, + KEY_KP_8, + KEY_KP_9, + KEY_KP_DECIMAL, + KEY_KP_DIVIDE, + KEY_KP_MULTIPLY, + KEY_KP_SUBTRACT, + KEY_KP_ADD, + KEY_KP_ENTER, + KEY_KP_EQUAL, + KEY_LEFT_SHIFT, + KEY_LEFT_CONTROL, + KEY_LEFT_ALT, + KEY_LEFT_SUPER, + KEY_RIGHT_SHIFT, + KEY_RIGHT_CONTROL, + KEY_RIGHT_ALT, + KEY_RIGHT_SUPER, + KEY_MENU, + MOUSE_1, + MOUSE_2, + MOUSE_3, + MOUSE_4, + MOUSE_5, + MOUSE_6, + CONTROL_COUNT + }; + + struct ControlEvent { + Control control; + bool press; + }; + + class Input { + + static const Control s_glfw_key_map[]; + static const Control s_glfw_mouse_map[]; + static const char* const s_control_names[]; + static std::vector s_control_events; + + static LuaFunction s_control_cb; + + public: + static Control ControlFromGlfwKey(int key); + static Control ControlFromGlfwMouseButton(int button); + + static void AddControlEvent(Control control, bool press); + + static void RegisterLuaFunctions(); + + static void ProcessEvents(); + + static void Shutdown(); + }; + + +} \ No newline at end of file diff --git a/src/tsr/luaapi.cpp b/src/tsr/luaapi.cpp index 7e95649..b3f13d8 100644 --- a/src/tsr/luaapi.cpp +++ b/src/tsr/luaapi.cpp @@ -20,6 +20,9 @@ void LuaAPI::Shutdown() { } void LuaAPI::Load(const std::string& name) { + if (s_loaded_scripts.contains(name)) + return; + s_loaded_scripts.insert(name); auto script = Filesystem::Read(name); diff --git a/src/tsr/luaapi.hpp b/src/tsr/luaapi.hpp index 11affe7..b41ffe6 100644 --- a/src/tsr/luaapi.hpp +++ b/src/tsr/luaapi.hpp @@ -1,10 +1,15 @@ #pragma once +#define SOL_CHECK_ARGUMENTS #include #include #include #include namespace TSR { + + using LuaReference = sol::reference; + using LuaFunction = sol::function; + class LuaAPI { static std::unique_ptr s_lua; @@ -27,6 +32,18 @@ namespace TSR { func(std::forward(args)...); } + template + static void Call(LuaReference& ref, Args&&... args) { + sol::protected_function func = ref; + auto res = func(std::forward(args)...); + + if (!res.valid()) { + sol::error err = res; + throw std::runtime_error(err.what()); + } + } + + static sol::state& GetState() { return *s_lua; } }; diff --git a/tsrecs.vcxproj b/tsrecs.vcxproj index af50143..1c86ea3 100644 --- a/tsrecs.vcxproj +++ b/tsrecs.vcxproj @@ -129,6 +129,8 @@ + + @@ -145,6 +147,7 @@ + diff --git a/tsrecs.vcxproj.filters b/tsrecs.vcxproj.filters index 34ae881..038e269 100644 --- a/tsrecs.vcxproj.filters +++ b/tsrecs.vcxproj.filters @@ -57,6 +57,12 @@ Source Files + + Source Files + + + Source Files + @@ -107,5 +113,8 @@ Header Files + + Header Files + \ No newline at end of file