Implement KeyPressed
This commit is contained in:
parent
b5c44e845a
commit
e9f0bc17a6
2 changed files with 28 additions and 6 deletions
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
|
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
|
@ -11,35 +12,56 @@ extern "C" {
|
||||||
|
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
// Implement KeyPressed that basically just gets called once on process_input down = true
|
static std::unordered_set<int> heldKeys;
|
||||||
// resets when the key is released again
|
|
||||||
|
|
||||||
void process_input(SDL_Keysym &keysym, bool down, lua_State *luaState) {
|
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)) {
|
||||||
|
int scancode = keysym.scancode;
|
||||||
|
|
||||||
if (down) {
|
if (down) {
|
||||||
lua_getfield(luaState, -1, "KeyDown");
|
lua_getfield(luaState, -1, "KeyDown");
|
||||||
if (lua_isfunction(luaState, -1)) {
|
if (lua_isfunction(luaState, -1)) {
|
||||||
lua_pushinteger(luaState, keysym.scancode);
|
lua_pushinteger(luaState, 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.KeyDown: " << error << std::endl;
|
std::cerr << "Lua error in engine.KeyDown: " << error << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (heldKeys.find(scancode) == heldKeys.end()){
|
||||||
|
heldKeys.insert(scancode);
|
||||||
|
|
||||||
|
lua_pop(luaState, 1);
|
||||||
|
|
||||||
|
lua_getfield(luaState, -1, "KeyPressed");
|
||||||
|
if (lua_isfunction(luaState, -1)) {
|
||||||
|
lua_pushinteger(luaState, 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
lua_getfield(luaState, -1, "KeyReleased");
|
lua_getfield(luaState, -1, "KeyReleased");
|
||||||
if (lua_isfunction(luaState, -1)) {
|
if (lua_isfunction(luaState, -1)) {
|
||||||
lua_pushinteger(luaState, keysym.scancode);
|
lua_pushinteger(luaState, 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.KeyReleased: " << error << std::endl;
|
std::cerr << "Lua error in engine.KeyReleased: " << error << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
heldKeys.erase(scancode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lua_pop(luaState, 1);
|
||||||
}
|
}
|
||||||
|
|
4
test.lua
4
test.lua
|
@ -3,6 +3,6 @@ function engine.KeyReleased(key)
|
||||||
print("Released key: " .. key)
|
print("Released key: " .. key)
|
||||||
end
|
end
|
||||||
|
|
||||||
function engine.KeyDown(key)
|
function engine.KeyPressed(key)
|
||||||
print("Key down: " .. key)
|
print("Pressed key: " .. key)
|
||||||
end
|
end
|
Loading…
Add table
Add a link
Reference in a new issue