luaaaaaaa

This commit is contained in:
det-fys 2023-12-24 18:02:06 +01:00
parent c07c1233ad
commit bb6115cfdf
9 changed files with 253 additions and 125 deletions

View File

@ -1,3 +1,6 @@
#define NOMINMAX
#include <windows.h>
#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<PhysicsScene> Game::s_physics_scene;
std::unique_ptr<WorldMap> Game::s_map;
std::map<uint32_t, Timer> 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<Model> 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<PhysicsScene>();
s_map = std::make_unique<WorldMap>(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<Model>("models/skybox.mdl.json");
while (!s_map->Loaded())
s_map->LoadNext();
//s_physics_scene->EnableDebug(false);
s_skybox_model = AssetMap::Get<Model>("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<TransformComponent>(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<TransformComponent>(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<TransformComponent>(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<TransformComponent>(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<TransformComponent>(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<TransformComponent>(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<std::chrono::milliseconds>(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<std::chrono::milliseconds>(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;

View File

@ -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<WorldMap> s_map;
static std::unique_ptr<PhysicsScene> s_physics_scene;
static std::map<uint32_t, Timer> s_timers;
static uint64_t s_sv_frame;
static float s_frame_t;
static int s_tps;

27
src/tsr/luaapi.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "luaapi.hpp"
#include "filesystem.hpp"
using namespace TSR;
std::unique_ptr<sol::state> LuaAPI::s_lua;
std::set<std::string> LuaAPI::s_loaded_scripts;
void LuaAPI::Init() {
s_lua = std::make_unique<sol::state>();
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);
}

35
src/tsr/luaapi.hpp Normal file
View File

@ -0,0 +1,35 @@
#pragma once
#include <sol/sol.hpp>
#include <string>
#include <memory>
#include <set>
namespace TSR {
class LuaAPI {
static std::unique_ptr<sol::state> s_lua;
static std::set<std::string> s_loaded_scripts;
public:
static void Init();
static void Shutdown();
static void Load(const std::string& name);
template <typename... Args>
static void RegisterFunction(const std::string& name, Args&&... args) {
s_lua->set_function(name, std::forward<Args>(args)...);
}
template <typename... Args>
static void Call(const std::string& name, Args&&... args) {
sol::protected_function func = s_lua->get<sol::protected_function>(name);
func(std::forward<Args>(args)...);
}
static sol::state& GetState() { return *s_lua; }
};
}

View File

@ -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() {

View File

@ -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));

View File

@ -1,34 +1,12 @@
#include <iostream>
#include <fstream>
#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 <windows.h>
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;
}

View File

@ -129,6 +129,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\tsr\luaapi.cpp" />
<ClCompile Include="src\tsr\entity_cct.cpp" />
<ClCompile Include="src\tsr\physics.cpp" />
<ClCompile Include="src\tsr\camera.cpp" />
@ -144,6 +145,7 @@
<ClCompile Include="src\tsr\worldmap.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\tsr\luaapi.hpp" />
<ClInclude Include="src\tsr\entity_cct.hpp" />
<ClInclude Include="src\tsr\physics.hpp" />
<ClInclude Include="src\tsr\camera.hpp" />

View File

@ -54,6 +54,9 @@
<ClCompile Include="src\tsr\entity_cct.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\tsr\luaapi.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\tsr\window.hpp">
@ -101,5 +104,8 @@
<ClInclude Include="src\tsr\entity_cct.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\tsr\luaapi.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>