transferred input processing to its own file

This commit is contained in:
Patrick Schwarzer 2025-04-29 04:58:53 +02:00
parent 077b2759ad
commit f44bcede74
3 changed files with 25 additions and 56 deletions

View file

@ -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;
}
}