mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
Finally abstracted constants properly. Changed how the event module works; wasn't really abstract before. Removed libs we're not going to use: native, signal.
This commit is contained in:
@@ -34,6 +34,100 @@ namespace sdl
|
||||
{
|
||||
static Event * instance = 0;
|
||||
|
||||
static bool to_message(lua_State * L, Event::Message & msg)
|
||||
{
|
||||
const char * str = luaL_checkstring(L, 1);
|
||||
|
||||
if(!Event::getConstant(str, msg.type))
|
||||
return false;
|
||||
|
||||
switch(msg.type)
|
||||
{
|
||||
case Event::TYPE_KEY_PRESSED:
|
||||
if(!Event::getConstant(luaL_checkstring(L, 2), msg.keyboard.k))
|
||||
return false;
|
||||
msg.keyboard.u = (unsigned short)luaL_optint(L, 3, 0);
|
||||
return true;
|
||||
case Event::TYPE_KEY_RELEASED:
|
||||
if(!Event::getConstant(luaL_checkstring(L, 2), msg.keyboard.k))
|
||||
return false;
|
||||
return true;
|
||||
case Event::TYPE_MOUSE_PRESSED:
|
||||
case Event::TYPE_MOUSE_RELEASED:
|
||||
if(!Event::getConstant(luaL_checkstring(L, 2), msg.mouse.b))
|
||||
return false;
|
||||
msg.mouse.x = luaL_checkint(L, 3);
|
||||
msg.mouse.y = luaL_checkint(L, 4);
|
||||
return true;
|
||||
case Event::TYPE_JOYSTICK_PRESSED:
|
||||
case Event::TYPE_JOYSTICK_RELEASED:
|
||||
msg.joystick.index = luaL_checkint(L, 2);
|
||||
msg.joystick.button = luaL_checkint(L, 3);
|
||||
return true;
|
||||
case Event::TYPE_QUIT:
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static int push_message(lua_State * L, const Event::Message & msg)
|
||||
{
|
||||
const char * str = 0;
|
||||
|
||||
if(!Event::getConstant(msg.type, str))
|
||||
return 0;
|
||||
|
||||
lua_pushstring(L, str);
|
||||
|
||||
switch(msg.type)
|
||||
{
|
||||
case Event::TYPE_KEY_PRESSED:
|
||||
if(!Event::getConstant(msg.keyboard.k, str))
|
||||
return 0;
|
||||
lua_pushstring(L, str);
|
||||
lua_pushinteger(L, msg.keyboard.u);
|
||||
return 3;
|
||||
case Event::TYPE_KEY_RELEASED:
|
||||
if(!Event::getConstant(msg.keyboard.k, str))
|
||||
return 0;
|
||||
lua_pushstring(L, str);
|
||||
return 2;
|
||||
case Event::TYPE_MOUSE_PRESSED:
|
||||
case Event::TYPE_MOUSE_RELEASED:
|
||||
if(!Event::getConstant(msg.mouse.b, str))
|
||||
return 0;
|
||||
lua_pushinteger(L, msg.mouse.x);
|
||||
lua_pushinteger(L, msg.mouse.y);
|
||||
lua_pushstring(L, str);
|
||||
return 4;
|
||||
case Event::TYPE_JOYSTICK_PRESSED:
|
||||
case Event::TYPE_JOYSTICK_RELEASED:
|
||||
lua_pushinteger(L, msg.joystick.index);
|
||||
lua_pushinteger(L, msg.joystick.button);
|
||||
return 3;
|
||||
case Event::TYPE_QUIT:
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int poll_i(lua_State * L)
|
||||
{
|
||||
static Event::Message m;
|
||||
|
||||
while(instance->poll(m))
|
||||
{
|
||||
int args = push_message(L, m);
|
||||
if(args > 0)
|
||||
return args;
|
||||
}
|
||||
|
||||
// No pending events.
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_pump(lua_State * L)
|
||||
{
|
||||
instance->pump();
|
||||
@@ -42,23 +136,35 @@ namespace sdl
|
||||
|
||||
int w_poll(lua_State * L)
|
||||
{
|
||||
return instance->poll(L);
|
||||
lua_pushcclosure(L, &poll_i, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_wait(lua_State * L)
|
||||
{
|
||||
return instance->wait(L);
|
||||
}
|
||||
static Event::Message m;
|
||||
|
||||
if(instance->wait(m))
|
||||
{
|
||||
return push_message(L, m);
|
||||
}
|
||||
|
||||
int w_quit(lua_State * L)
|
||||
{
|
||||
instance->quit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_push(lua_State * L)
|
||||
{
|
||||
return instance->push(L);
|
||||
static Event::Message m;
|
||||
|
||||
if(!to_message(L, m))
|
||||
{
|
||||
luax_pushboolean(L, false);
|
||||
return 1;
|
||||
}
|
||||
|
||||
luax_pushboolean(L, instance->push(m));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// List of functions to wrap.
|
||||
@@ -66,23 +172,10 @@ namespace sdl
|
||||
{ "pump", w_pump },
|
||||
{ "poll", w_poll },
|
||||
{ "wait", w_wait },
|
||||
{ "quit", w_quit },
|
||||
{ "push", w_push },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
// List of constants.
|
||||
static const Constant constants[] = {
|
||||
{ "event_keypressed", Event::EVENT_KEYDOWN },
|
||||
{ "event_keyreleased", Event::EVENT_KEYUP },
|
||||
{ "event_mousepressed", Event::EVENT_MOUSEBUTTONDOWN },
|
||||
{ "event_mousereleased", Event::EVENT_MOUSEBUTTONUP },
|
||||
{ "event_joystickpressed", Event::EVENT_JOYBUTTONDOWN },
|
||||
{ "event_joystickreleased", Event::EVENT_JOYBUTTONUP },
|
||||
{ "event_quit", Event::EVENT_QUIT },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
int luaopen_love_event(lua_State * L)
|
||||
{
|
||||
if(instance == 0)
|
||||
@@ -103,7 +196,6 @@ namespace sdl
|
||||
w.flags = MODULE_T;
|
||||
w.functions = functions;
|
||||
w.types = 0;
|
||||
w.constants = constants;
|
||||
|
||||
return luax_register_module(L, w);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user