added key() / keyp() LUA api

This commit is contained in:
BADIM-PC\Vadim
2017-12-29 17:59:07 +03:00
parent c3a57954aa
commit ca0a7c7167
6 changed files with 172 additions and 124 deletions

View File

@@ -360,61 +360,57 @@ static s32 lua_btnp(lua_State* lua)
tic_machine* machine = getLuaMachine(lua);
tic_mem* memory = (tic_mem*)machine;
if(machine->memory.input.gamepad)
s32 top = lua_gettop(lua);
if (top == 0)
{
s32 top = lua_gettop(lua);
if (top == 0)
{
lua_pushinteger(lua, memory->api.btnp(memory, -1, -1, -1));
}
else if(top == 1)
{
s32 index = getLuaNumber(lua, 1) & 0xf;
lua_pushboolean(lua, memory->api.btnp(memory, index, -1, -1));
}
else if (top == 3)
{
s32 index = getLuaNumber(lua, 1) & 0xf;
u32 hold = getLuaNumber(lua, 2);
u32 period = getLuaNumber(lua, 3);
lua_pushboolean(lua, memory->api.btnp(memory, index, hold, period));
}
else luaL_error(lua, "invalid params, btnp [ id [ hold period ] ]\n");
return 1;
lua_pushinteger(lua, memory->api.btnp(memory, -1, -1, -1));
}
else luaL_error(lua, "gamepad input not declared in metadata\n");
else if(top == 1)
{
s32 index = getLuaNumber(lua, 1) & 0x1f;
return 0;
lua_pushboolean(lua, memory->api.btnp(memory, index, -1, -1));
}
else if (top == 3)
{
s32 index = getLuaNumber(lua, 1) & 0x1f;
u32 hold = getLuaNumber(lua, 2);
u32 period = getLuaNumber(lua, 3);
lua_pushboolean(lua, memory->api.btnp(memory, index, hold, period));
}
else
{
luaL_error(lua, "invalid params, btnp [ id [ hold period ] ]\n");
return 0;
}
return 1;
}
static s32 lua_btn(lua_State* lua)
{
tic_machine* machine = getLuaMachine(lua);
if(machine->memory.input.gamepad)
s32 top = lua_gettop(lua);
if (top == 0)
{
s32 top = lua_gettop(lua);
if (top == 0)
{
lua_pushinteger(lua, machine->memory.ram.input.gamepads.data);
}
else if (top == 1)
{
s32 index = getLuaNumber(lua, 1) & 0xf;
lua_pushboolean(lua, machine->memory.ram.input.gamepads.data & (1 << index));
}
else luaL_error(lua, "invalid params, btn [ id ]\n");
return 1;
lua_pushinteger(lua, machine->memory.ram.input.gamepads.data);
}
else luaL_error(lua, "gamepad input not declared in metadata\n");
else if (top == 1)
{
u32 index = getLuaNumber(lua, 1) & 0x1f;
lua_pushboolean(lua, machine->memory.ram.input.gamepads.data & (1 << index));
}
else
{
luaL_error(lua, "invalid params, btn [ id ]\n");
return 0;
}
return 0;
return 1;
}
static s32 lua_spr(lua_State* lua)
@@ -787,31 +783,28 @@ static s32 lua_reset(lua_State* lua)
static s32 lua_key(lua_State* lua)
{
tic_machine* machine = getLuaMachine(lua);
tic_mem* tic = &machine->memory;
s32 top = lua_gettop(lua);
tic80_input* input = &machine->memory.ram.input;
tic80_input* input = &tic->ram.input;
if (top == 1)
enum{Count = COUNT_OF(input->keyboard.keys)};
if (top == 0)
{
u8 index = getLuaNumber(lua, 1)+1;
if(index > tic_key_unknown)
{
for(s32 i = 0; i < COUNT_OF(input->keyboard.keys); i++)
{
if(input->keyboard.keys[i] == index)
{
lua_pushboolean(lua, true);
return 1;
}
}
}
lua_pushboolean(lua, false);
lua_pushboolean(lua, tic->api.key(tic, tic_key_unknown));
return 1;
}
else luaL_error(lua, "invalid params, key ( code )\n");
else if (top == 1)
{
tic_key key = getLuaNumber(lua, 1) + TIC_KEY_START_INDEX;
lua_pushboolean(lua, tic->api.key(tic, key));
return 1;
}
else luaL_error(lua, "invalid params, key [code]\n");
return 0;
}
@@ -819,65 +812,35 @@ static s32 lua_key(lua_State* lua)
static s32 lua_keyp(lua_State* lua)
{
tic_machine* machine = getLuaMachine(lua);
tic_mem* tic = &machine->memory;
s32 top = lua_gettop(lua);
tic80_input* input = &machine->memory.ram.input;
if (top == 0)
{
for(s32 i = 0; i < COUNT_OF(input->keyboard.keys); i++)
{
s32 code = input->keyboard.keys[i];
bool found = false;
for(s32 p = 0; p < COUNT_OF(machine->state.keyboard.previous.keys); p++)
{
if(machine->state.keyboard.previous.keys[p] == code)
{
found = true;
break;
}
}
if(!found)
{
lua_pushinteger(lua, code);
return 1;
}
}
lua_pushboolean(lua, tic->api.keyp(tic, tic_key_unknown, -1, -1));
}
else if(top == 1)
{
{
s32 code = getLuaNumber(lua, 1);
tic_key key = getLuaNumber(lua, 1) + TIC_KEY_START_INDEX;;
if(code > 0)
{
for(s32 i = 0; i < COUNT_OF(input->keyboard.keys); i++)
{
if(input->keyboard.keys[i] == code)
{
for(s32 p = 0; p < COUNT_OF(machine->state.keyboard.previous.keys); p++)
{
if(machine->state.keyboard.previous.keys[p] == code)
{
lua_pushboolean(lua, true);
return 1;
}
}
}
}
}
lua_pushboolean(lua, false);
return 1;
}
lua_pushboolean(lua, tic->api.keyp(tic, key, -1, -1));
}
else luaL_error(lua, "invalid params, keyp [ code ]\n");
else if(top == 3)
{
tic_key key = getLuaNumber(lua, 1) + TIC_KEY_START_INDEX;
u32 hold = getLuaNumber(lua, 2);
u32 period = getLuaNumber(lua, 3);
return 0;
lua_pushboolean(lua, tic->api.keyp(tic, key, hold, period));
}
else
{
luaL_error(lua, "invalid params, keyp [ code [ hold period ] ]\n");
return 0;
}
return 1;
}
static s32 lua_memcpy(lua_State* lua)
@@ -1129,21 +1092,15 @@ static s32 lua_mouse(lua_State *lua)
{
tic_machine* machine = getLuaMachine(lua);
if(machine->memory.input.mouse)
{
const tic80_mouse* mouse = &machine->memory.ram.input.mouse;
const tic80_mouse* mouse = &machine->memory.ram.input.mouse;
lua_pushinteger(lua, mouse->x);
lua_pushinteger(lua, mouse->y);
lua_pushboolean(lua, mouse->left);
lua_pushboolean(lua, mouse->middle);
lua_pushboolean(lua, mouse->right);
lua_pushinteger(lua, mouse->x);
lua_pushinteger(lua, mouse->y);
lua_pushboolean(lua, mouse->left);
lua_pushboolean(lua, mouse->middle);
lua_pushboolean(lua, mouse->right);
return 5;
}
else luaL_error(lua, "mouse input not declared in metadata\n");
return 0;
return 5;
}
static s32 lua_dofile(lua_State *lua)