diff --git a/engine/input.cpp b/engine/input.cpp index 6356338..14ff62f 100644 --- a/engine/input.cpp +++ b/engine/input.cpp @@ -1,35 +1,32 @@ #include "input.h" -void process_input(SDL_Keysym keysym) { - - - - - - - - // Call engine.keypressed(key) +void process_input(SDL_Keysym &keysym, bool down, lua_State &luaState) { lua_getglobal(luaState, "engine"); - if (lua_istable(luaState, -1)) { - lua_getfield(luaState, -1, "KeyPressed"); - 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; + if (lua_istable(luaState, -1)) { + if (down) { + lua_getfield(luaState, -1, "KeyDown"); + 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.KeyDown: " << error << std::endl; + } + } + } + else + { + lua_getfield(luaState, -1, "KeyPressed"); + 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; + } } } } - - switch (keysym.scancode) { - case SDL_SCANCODE_ESCAPE: - isRunning = false; - - std::cout << "Closing game\n"; - break; - default: - break; - } } diff --git a/engine/input.h b/engine/input.h index 312421b..b7a4d98 100644 --- a/engine/input.h +++ b/engine/input.h @@ -1,4 +1,4 @@ #pragma once -void process_input(); \ No newline at end of file +void process_input(SDL_Keysym &keysym, bool down, lua_State &luaState); diff --git a/engine/main.cpp b/engine/main.cpp index db3baa0..2884c01 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -20,33 +20,6 @@ void createLuaTables() { lua_setglobal(luaState, "engine"); } -// TEMPORARY -void process_input(SDL_Keysym keysym) { - // Call engine.keypressed(key) - lua_getglobal(luaState, "engine"); - if (lua_istable(luaState, -1)) { - lua_getfield(luaState, -1, "KeyPressed"); - 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; - } - } - } - - switch (keysym.scancode) { - case SDL_SCANCODE_ESCAPE: - isRunning = false; - - std::cout << "Closing game\n"; - break; - default: - break; - } -} - int main(int argc, char* argv[]) { std::cout << "Starting...\n"; @@ -107,8 +80,7 @@ int main(int argc, char* argv[]) { SDL_Keysym* keysym; keysym = &key->keysym; - process_input(*keysym); - + process_input(*keysym, false, *luaState); break; default: