summaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorSteph Enders <smenders@gmail.com>2022-06-15 23:36:43 -0400
committerSteph Enders <smenders@gmail.com>2022-06-15 23:36:43 -0400
commiteaaf57114cfb74c29f1b0c4e9786bed6d225225c (patch)
tree8f4946942a9f93f2bba8b1c4c39045e8ee02811d /src/main.cpp
Initial setup commit
This setups up the project for messing around with C++ and Lua bindings. So far this project just prints the defined dng map and lets you move the character around. What this fails to do is actually provide any reason to use Lua at the moment. So I need to figure out some way of enabling logic on the processing side of things.
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..fb61369
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,83 @@
+#include "Api.h"
+#include "Level.h"
+#include <SFML/Graphics.hpp>
+#include <filesystem>
+#include <iostream>
+#include <lua.hpp>
+#include <memory>
+
+const char* DEFAULT_PROC = "include/default_proc.lua";
+std::shared_ptr<Level> lvl = std::make_shared<Level>();
+
+bool
+call_onkeypress(lua_State* L, char pressedKey);
+
+int
+main(int argc, char** argv)
+{
+
+ if (argc <= 1) {
+ return -1;
+ }
+
+ std::string lvl_pfx = argv[1];
+
+ std::filesystem::path mapFile{ lvl_pfx + "/dng.map" };
+ std::filesystem::path luaFile{ lvl_pfx + "/proc.lua" };
+
+ lvl->loadLevelFromFile(mapFile.c_str());
+
+ lua_State* L_lvl = luaL_newstate();
+ luaL_openlibs(L_lvl);
+ init_c_api(L_lvl);
+
+ lua_State* L_default = luaL_newstate();
+ luaL_openlibs(L_default);
+ init_c_api(L_default);
+
+ if (std::filesystem::exists(luaFile) &&
+ luaL_dofile(L_default, DEFAULT_PROC) != LUA_OK) {
+ std::cout << "Failed to load default proc" << std::endl;
+ luaL_error(L_default, "Error: %s", lua_tostring(L_default, -1));
+ return EXIT_FAILURE;
+ }
+
+ if (std::filesystem::exists(luaFile) &&
+ luaL_dofile(L_lvl, luaFile.c_str()) != LUA_OK) {
+ std::cout << "[C] No Good" << std::endl;
+ luaL_error(L_lvl, "Error: %s\n", lua_tostring(L_lvl, -1));
+ return EXIT_FAILURE;
+ }
+
+ bool quit = false;
+
+ char in;
+
+ do {
+ lvl->print();
+ std::cin >> in;
+ if (!call_onkeypress(L_default, in)) {
+ quit = true;
+ }
+ if (!quit && in == 'q') {
+ quit = true;
+ }
+ } while (!quit);
+
+ std::cout << "[C] Quit" << std::endl;
+
+ return EXIT_SUCCESS;
+}
+
+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;
+ return false;
+ }
+ lua_pushinteger(L, pressedKey);
+ lua_pcall(L, 1, 1, 0);
+ return true;
+} \ No newline at end of file