diff options
| author | Arslaan Pathan <[email protected]> | 2025-06-11 07:40:15 +1200 |
|---|---|---|
| committer | Arslaan Pathan <[email protected]> | 2025-06-11 07:40:15 +1200 |
| commit | 8d6464fe655a68d278a9806392ec1488757adf2e (patch) | |
| tree | e585d55421983d1e15a9459ad64adca7621172fa /src/main.cpp | |
| download | showdownofthesticks-8d6464fe655a68d278a9806392ec1488757adf2e.tar.xz showdownofthesticks-8d6464fe655a68d278a9806392ec1488757adf2e.zip | |
Initial commit
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..9777a6f --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,137 @@ +#include <SDL2/SDL.h> +#include <iostream> +#include "lua/lua.hpp" + +#define WIDTH 1280 +#define HEIGHT 720 +#define KEYSYM event.key.keysym.sym + +SDL_Rect rect = {WIDTH/2, HEIGHT/2, 50, 50}; + +constexpr char* LUA_FILE = R"( +movementSpeed = 3; + +function Setup() +end + +function Update() + if keys[getKeycodeByName("RIGHT")] or keys[getKeycodeByName("D")] then + movePlayer(movementSpeed, 0) + end + if keys[getKeycodeByName("LEFT")] or keys[getKeycodeByName("A")] then + movePlayer(movementSpeed * -1, 0) + end + if keys[getKeycodeByName("DOWN")] or keys[getKeycodeByName("S")] then + movePlayer(0, movementSpeed) + end + if keys[getKeycodeByName("UP")] or keys[getKeycodeByName("W")] then + movePlayer(0, movementSpeed * -1) + end +end +)"; + +int l_move_rect(lua_State* L) { + int dx = luaL_checkinteger(L, 1); + int dy = luaL_checkinteger(L, 2); + rect.x += dx; + rect.y += dy; + return 0; +} + +int l_keycode_from_string(lua_State* L) { + const char* keystr = luaL_checkstring(L, 1); + + int scancode = -2; + + if (strcmp(keystr, "RIGHT") == 0) scancode = SDL_SCANCODE_RIGHT; + else if (strcmp(keystr, "LEFT") == 0) scancode = SDL_SCANCODE_LEFT; + else if (strcmp(keystr, "UP") == 0) scancode = SDL_SCANCODE_UP; + else if (strcmp(keystr, "DOWN") == 0) scancode = SDL_SCANCODE_DOWN; + else if (strcmp(keystr, "D") == 0) scancode = SDL_SCANCODE_D; + else if (strcmp(keystr, "A") == 0) scancode = SDL_SCANCODE_A; + else if (strcmp(keystr, "S") == 0) scancode = SDL_SCANCODE_S; + else if (strcmp(keystr, "W") == 0) scancode = SDL_SCANCODE_W; + + scancode += 1; + + lua_pushinteger(L, scancode); + return 1; +} + +void push_keys_to_lua(lua_State* L, bool keys[SDL_NUM_SCANCODES]) { + lua_newtable(L); // create a new table on the stack + + for (int i = 0; i < SDL_NUM_SCANCODES; i++) { + lua_pushinteger(L, i + 1); + lua_pushboolean(L, keys[i]); + lua_settable(L, -3); + } +} + +void call_lua_function(lua_State* L, const char* func_name) { + lua_getglobal(L, func_name); + if (lua_isfunction(L, -1)) { + if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + std::cerr << "Lua Error in " << func_name << ": " << lua_tostring(L, -1) << std::endl; + lua_pop(L, 1); + } + } else { + lua_pop(L, 1); + } +} + + +int main() { + lua_State* L = luaL_newstate(); + luaL_openlibs(L); + lua_register(L, "movePlayer", l_move_rect); + lua_register(L, "getKeycodeByName", l_keycode_from_string); + luaL_dostring(L, LUA_FILE); + + SDL_Window* window = nullptr; + SDL_Renderer* renderer = nullptr; + SDL_Event event; + + bool keys[SDL_NUM_SCANCODES] = {false}; + bool running = true; + + push_keys_to_lua(L, keys); + lua_setglobal(L, "keys"); + + SDL_Init(SDL_INIT_EVERYTHING); + SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer); + + call_lua_function(L, "Setup"); + + while (running) { + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + running = false; + } + + if (event.type == SDL_KEYDOWN) { + keys[event.key.keysym.scancode] = true; + } + if (event.type == SDL_KEYUP) { + keys[event.key.keysym.scancode] = false; + } + } + + push_keys_to_lua(L, keys); + lua_setglobal(L, "keys"); + + call_lua_function(L, "Update"); + + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + SDL_RenderFillRect(renderer, &rect); + + SDL_RenderPresent(renderer); + SDL_Delay(10); + } + + lua_close(L); + return 0; +} |
