diff --git a/CMakeLists.txt b/CMakeLists.txt index 35eb7ad..14659f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@  -cmake_minimum_required(VERSION 4.0) +cmake_minimum_required(VERSION 3.25.1) if (POLICY CMP0141) cmake_policy(SET CMP0141 NEW) diff --git a/engine/input.cpp b/engine/input.cpp index 14ff62f..502f512 100644 --- a/engine/input.cpp +++ b/engine/input.cpp @@ -1,7 +1,20 @@ + +#include + +extern "C" { + #include + #include + #include +} + + #include "input.h" -void process_input(SDL_Keysym &keysym, bool down, lua_State &luaState) { +// Implement KeyPressed that basically just gets called once on process_input down = true +// resets when the key is released again + +void process_input(SDL_Keysym &keysym, bool down, lua_State *luaState) { lua_getglobal(luaState, "engine"); if (lua_istable(luaState, -1)) { @@ -18,13 +31,13 @@ void process_input(SDL_Keysym &keysym, bool down, lua_State &luaState) { } else { - lua_getfield(luaState, -1, "KeyPressed"); + lua_getfield(luaState, -1, "KeyReleased"); if (lua_isfunction(luaState, -1)) { lua_pushinteger(luaState, keysym.scancode); if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { const char* error = lua_tostring(luaState, -1); - std::cerr << "Lua error in engine.KeyPressed: " << error << std::endl; + std::cerr << "Lua error in engine.KeyReleased: " << error << std::endl; } } } diff --git a/engine/input.h b/engine/input.h index b7a4d98..afb39a8 100644 --- a/engine/input.h +++ b/engine/input.h @@ -1,4 +1,16 @@ #pragma once -void process_input(SDL_Keysym &keysym, bool down, lua_State &luaState); + +#include +#include +#include + +extern "C" { + #include + #include + #include +} + + +void process_input(SDL_Keysym &keysym, bool down, lua_State *luaState); diff --git a/engine/main.cpp b/engine/main.cpp index 2884c01..00157d0 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -69,18 +69,22 @@ int main(int argc, char* argv[]) { SDL_Event event; while (isRunning) { while (SDL_PollEvent(&event)) { + SDL_KeyboardEvent* key; + key = &event.key; + SDL_Keysym* keysym; + keysym = &key->keysym; + switch (event.type) { case SDL_QUIT: isRunning = false; break; case SDL_KEYUP: - SDL_KeyboardEvent* key; - key = &event.key; - SDL_Keysym* keysym; - keysym = &key->keysym; + process_input(*keysym, false, luaState); + break; - process_input(*keysym, false, *luaState); + case SDL_KEYDOWN: + process_input(*keysym, true, luaState); break; default: diff --git a/test.lua b/test.lua index 235365a..c4255c3 100644 --- a/test.lua +++ b/test.lua @@ -1,4 +1,8 @@ -function engine.KeyPressed(key) - print("Pressed key: " .. key) +function engine.KeyReleased(key) + print("Released key: " .. key) end + +function engine.KeyDown(key) + print("Key down: " .. key) +end \ No newline at end of file