luaaaaaaa
This commit is contained in:
parent
c07c1233ad
commit
bb6115cfdf
@ -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,6 +83,8 @@ void Game::EndGame() {
|
||||
|
||||
void Game::Run() {
|
||||
|
||||
try {
|
||||
|
||||
Filesystem::Init("main");
|
||||
WindowWrapper ww("TSR test", 640, 480);
|
||||
////Audio::Init();
|
||||
@ -179,6 +220,12 @@ void Game::Run() {
|
||||
ClFrame(renderer, 0.0f);
|
||||
}
|
||||
|
||||
}
|
||||
catch (std::exception ex) {
|
||||
TsrPrintf("ERROR: %s\n", ex.what());
|
||||
MessageBoxA(NULL, ex.what(), "ERROR", MB_ICONERROR);
|
||||
}
|
||||
|
||||
EndGame();
|
||||
Physics::Close();
|
||||
}
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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
27
src/tsr/luaapi.cpp
Normal 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
35
src/tsr/luaapi.hpp
Normal 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; }
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
@ -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,8 +44,13 @@ void Physics::Init() {
|
||||
}
|
||||
|
||||
void Physics::Close() {
|
||||
if (s_cooking)
|
||||
s_cooking->release();
|
||||
|
||||
if (s_physics)
|
||||
s_physics->release();
|
||||
|
||||
if (s_foundation)
|
||||
s_foundation->release();
|
||||
}
|
||||
|
||||
|
||||
@ -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));
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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" />
|
||||
|
||||
@ -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>
|
||||
Loading…
x
Reference in New Issue
Block a user