Skip to content

Commit

Permalink
Feat: Init and run libuv event loop for each game-loop iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
mcb2003 committed Jun 16, 2024
1 parent 86621ca commit de2cee3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 15 additions & 1 deletion rt/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ Runtime::Runtime() : L() {
UV_VERSION_SUFFIX, uv_version_string()));
}

// Initialize the event loop
int res = uv_loop_init(&m_loop);
if (res < 0) {
throw std::runtime_error(
fmt::format("Could not initialize event loop: {}", uv_strerror(res)));
}

luaL_openlibs(L);
}

Runtime::~Runtime() {}
Runtime::~Runtime() { uv_loop_close(&m_loop); }

void Runtime::loadFile(const char *, const char *, const char *) {
// Fixme: Implement this! It is overridden by EngineImpl, as it uses
Expand Down Expand Up @@ -86,6 +93,13 @@ void Runtime::setup() {
}

bool Runtime::runOnce() {
// Run the libuv event loop
int res = uv_run(&m_loop, UV_RUN_NOWAIT);
if (res < 0) {
throw std::runtime_error(
fmt::format("Error running event loop: {}", uv_strerror(res)));
}

lua_settop(L, 0); // Clear stack

// Run pending tasks
Expand Down
3 changes: 3 additions & 0 deletions rt/runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <string_view>

#include <uv.h>

#include "lua/state.hpp"

namespace lege {
Expand All @@ -22,6 +24,7 @@ class Runtime {
bool runOnce();

protected:
uv_loop_t m_loop;
lua::State L;
};

Expand Down

0 comments on commit de2cee3

Please sign in to comment.