Fix cmake files and add input file

This commit is contained in:
Patrick Schwarzer 2025-04-28 03:28:25 +02:00
parent 2de172775d
commit 077b2759ad
8 changed files with 65 additions and 26 deletions

2
.vscode/launch.json vendored
View file

@ -30,7 +30,7 @@
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"environment": [], "environment": [],
"externalConsole": true, "externalConsole": false,
"MIMode": "gdb", "MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb", "miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [ "setupCommands": [

View file

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

View file

@ -9,6 +9,8 @@ set(CMAKE_CXX_STANDARD 20)
set(SOURCES set(SOURCES
main.cpp main.cpp
main.h main.h
input.cpp
input.h
${CMAKE_SOURCE_DIR}/include/glad.c ${CMAKE_SOURCE_DIR}/include/glad.c
) )
@ -26,15 +28,15 @@ if(WIN32)
${CMAKE_SOURCE_DIR}/lib/luajit.lib ${CMAKE_SOURCE_DIR}/lib/luajit.lib
) )
elseif(UNIX) elseif(UNIX)
include_directories(${SDL2_INCLUDE_DIRS} ${LUAJIT_INCLUDE_DIRS}) find_package(SDL2 REQUIRED)
link_directories(${SDL2_LIBRARY_DIRS} ${LUAJIT_LIBRARY_DIRS})
include_directories(${SDL2_INCLUDE_DIRS})
target_link_libraries(engine target_link_libraries(engine
${SDL2_LIBRARIES} SDL2::SDL2
${LUAJIT_LIBRARIES}
luajit luajit
dl # just in case LuaJIT needs dynamic linking dl
m # math lib, commonly needed m
) )
endif() endif()

35
engine/input.cpp Normal file
View file

@ -0,0 +1,35 @@
#include "input.h"
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;
}
}

4
engine/input.h Normal file
View file

@ -0,0 +1,4 @@
#pragma once
void process_input();

View file

@ -9,6 +9,8 @@ extern "C" {
#include <lauxlib.h> #include <lauxlib.h>
} }
#include "input.h"
bool isRunning = false; bool isRunning = false;
lua_State* luaState; lua_State* luaState;
@ -95,22 +97,22 @@ int main(int argc, char* argv[]) {
while (isRunning) { while (isRunning) {
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
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; SDL_KeyboardEvent* key;
key = &event.key; key = &event.key;
SDL_Keysym* keysym; SDL_Keysym* keysym;
keysym = &key->keysym; keysym = &key->keysym;
process_input(*keysym); process_input(*keysym);
break; break;
default: default:
break; break;
} }
} }

View file

@ -1,8 +1,4 @@
// Game-Engine.h : Include file for standard system include files, 
// or project specific include files.
#pragma once #pragma once
#include <iostream> #include <iostream>
// TODO: Reference additional headers your program requires here.

View file

@ -1,4 +1,4 @@
function engine.KeyPressed(key) function engine.KeyPressed(key)
print("Pressed key:" .. key) print("Pressed key: " .. key)
end end