summaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp58
1 files changed, 43 insertions, 15 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 6f649cd..eef9777 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -5,15 +5,18 @@
#include <iostream>
#include <lua.hpp>
-const char* DEFAULT_PROC = "include/default_proc.lua";
+const char *DEFAULT_PROC = "include/defaults.lua";
std::shared_ptr<Level> lvl;
-bool
-call_onkeypress(lua_State* L, char pressedKey);
+struct LState {
+ lua_State *onkeypress;
+ lua_State *onupdate;
+} typedef LState;
-int
-main(int argc, char** argv)
-{
+bool call_onkeypress(lua_State *L, char pressedKey);
+bool call_onupdate(lua_State *L);
+
+int main(int argc, char **argv) {
if (argc <= 1) {
return -1;
@@ -21,18 +24,18 @@ main(int argc, char** argv)
std::string lvl_pfx = argv[1];
- std::filesystem::path mapFile{ lvl_pfx + "/dng.map" };
- std::filesystem::path luaFile{ lvl_pfx + "/proc.lua" };
+ std::filesystem::path mapFile{lvl_pfx + "/dng.map"};
+ std::filesystem::path luaFile{lvl_pfx + "/proc.lua"};
lvl = std::make_shared<Level>();
lvl->loadLevelFromFile(mapFile.c_str());
- lua_State* L_lvl = luaL_newstate();
+ lua_State *L_lvl = luaL_newstate();
luaL_openlibs(L_lvl);
init_c_api(L_lvl);
- lua_State* L_default = luaL_newstate();
+ lua_State *L_default = luaL_newstate();
luaL_openlibs(L_default);
init_c_api(L_default);
@@ -43,8 +46,22 @@ main(int argc, char** argv)
return EXIT_FAILURE;
}
+ // Initialize to default
+ LState l_state = {.onkeypress = L_default, .onupdate = L_default};
+
if (std::filesystem::exists(luaFile) &&
- luaL_dofile(L_lvl, luaFile.c_str()) != LUA_OK) {
+ luaL_dofile(L_lvl, luaFile.c_str()) == LUA_OK) {
+
+ // overwrite defaults
+ lua_getglobal(L_lvl, "onKeyPress");
+ if (lua_isfunction(L_lvl, -1)) {
+ l_state.onkeypress = L_lvl;
+ }
+ lua_getglobal(L_lvl, "onUpdate");
+ if (lua_isfunction(L_lvl, -1)) {
+ l_state.onupdate = L_lvl;
+ }
+ } else if (std::filesystem::exists(luaFile)) {
std::cout << "[C] No Good" << std::endl;
luaL_error(L_lvl, "Error: %s\n", lua_tostring(L_lvl, -1));
return EXIT_FAILURE;
@@ -57,7 +74,10 @@ main(int argc, char** argv)
do {
lvl->print();
std::cin >> in;
- if (!call_onkeypress(L_default, in)) {
+ if (!call_onkeypress(l_state.onkeypress, in)) {
+ quit = true;
+ }
+ if (!call_onupdate(l_state.onupdate)) {
quit = true;
}
if (!quit && in == 'q') {
@@ -70,9 +90,7 @@ main(int argc, char** argv)
return EXIT_SUCCESS;
}
-bool
-call_onkeypress(lua_State* L, char pressedKey)
-{
+bool call_onkeypress(lua_State *L, char pressedKey) {
lua_getglobal(L, "onKeyPress");
if (!lua_isfunction(L, -1)) {
std::cout << "[C] Error onKeyPress not function | not found" << std::endl;
@@ -81,4 +99,14 @@ call_onkeypress(lua_State* L, char pressedKey)
lua_pushinteger(L, pressedKey);
lua_pcall(L, 1, 1, 0);
return true;
+}
+
+bool call_onupdate(lua_State *L) {
+ lua_getglobal(L, "onUpdate");
+ if (!lua_isfunction(L, -1)) {
+ std::cout << "[C] Error onUpdate not function | not found" << std::endl;
+ return false;
+ }
+ lua_pcall(L, 0, 1, 0);
+ return true;
} \ No newline at end of file