updated input

This commit is contained in:
Patrick Schwarzer 2025-04-30 16:53:53 +02:00
parent f44bcede74
commit b5c44e845a
5 changed files with 45 additions and 12 deletions

View file

@ -1,5 +1,5 @@
 
cmake_minimum_required(VERSION 4.0) cmake_minimum_required(VERSION 3.25.1)
if (POLICY CMP0141) if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW) cmake_policy(SET CMP0141 NEW)

View file

@ -1,7 +1,20 @@
#include <SDL.h>
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include "input.h" #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"); lua_getglobal(luaState, "engine");
if (lua_istable(luaState, -1)) { if (lua_istable(luaState, -1)) {
@ -18,13 +31,13 @@ void process_input(SDL_Keysym &keysym, bool down, lua_State &luaState) {
} }
else else
{ {
lua_getfield(luaState, -1, "KeyPressed"); lua_getfield(luaState, -1, "KeyReleased");
if (lua_isfunction(luaState, -1)) { if (lua_isfunction(luaState, -1)) {
lua_pushinteger(luaState, keysym.scancode); lua_pushinteger(luaState, keysym.scancode);
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
const char* error = lua_tostring(luaState, -1); 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;
} }
} }
} }

View file

@ -1,4 +1,16 @@
#pragma once #pragma once
void process_input(SDL_Keysym &keysym, bool down, lua_State &luaState);
#include <SDL.h>
#include <glad/glad.h>
#include <iostream>
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
void process_input(SDL_Keysym &keysym, bool down, lua_State *luaState);

View file

@ -69,18 +69,22 @@ int main(int argc, char* argv[]) {
SDL_Event event; SDL_Event event;
while (isRunning) { while (isRunning) {
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
SDL_KeyboardEvent* key;
key = &event.key;
SDL_Keysym* keysym;
keysym = &key->keysym;
switch (event.type) { switch (event.type) {
case SDL_QUIT: case SDL_QUIT:
isRunning = false; isRunning = false;
break; break;
case SDL_KEYUP: case SDL_KEYUP:
SDL_KeyboardEvent* key; process_input(*keysym, false, luaState);
key = &event.key; break;
SDL_Keysym* keysym;
keysym = &key->keysym;
process_input(*keysym, false, *luaState); case SDL_KEYDOWN:
process_input(*keysym, true, luaState);
break; break;
default: default:

View file

@ -1,4 +1,8 @@
function engine.KeyPressed(key) function engine.KeyReleased(key)
print("Pressed key: " .. key) print("Released key: " .. key)
end
function engine.KeyDown(key)
print("Key down: " .. key)
end end