Added love.event.quit() and love.quit() callback
This commit is contained in:
@@ -14,21 +14,20 @@ function love.boot()
|
||||
end)
|
||||
|
||||
-- Init event handlers table
|
||||
local t = {
|
||||
"mousepressed",
|
||||
"mousereleased",
|
||||
"mousemoved",
|
||||
"keypressed",
|
||||
"keyreleased",
|
||||
"textinput",
|
||||
}
|
||||
love.handlers = {}
|
||||
for i, name in ipairs(t) do
|
||||
love.handlers[name] = function(...)
|
||||
local fn = love[name]
|
||||
if fn then fn(...) end
|
||||
local function makewrapper(name)
|
||||
return function(...)
|
||||
if love[name] then return love[name](...) end
|
||||
end
|
||||
end
|
||||
love.handlers = {
|
||||
["quit"] = function() end,
|
||||
["mousepressed"] = makewrapper("mousepressed"),
|
||||
["mousereleased"] = makewrapper("mousereleased"),
|
||||
["mousemoved"] = makewrapper("mousemoved"),
|
||||
["keypressed"] = makewrapper("keypressed"),
|
||||
["keyreleased"] = makewrapper("keyreleased"),
|
||||
["textinput"] = makewrapper("textinput"),
|
||||
}
|
||||
|
||||
-- Try to mount .exe file, then the first argument
|
||||
for i, v in ipairs { love.argv[1], love.argv[2] } do
|
||||
@@ -72,6 +71,11 @@ function love.run()
|
||||
if not name then
|
||||
break
|
||||
end
|
||||
if name == "quit" then
|
||||
if not love.quit or not love.quit() then
|
||||
os.exit(a)
|
||||
end
|
||||
end
|
||||
love.handlers[name](a, b, c, d)
|
||||
end
|
||||
-- Update
|
||||
|
||||
@@ -22,6 +22,7 @@ static struct {
|
||||
|
||||
const char* event_typestr(int type) {
|
||||
switch (type) {
|
||||
case EVENT_QUIT : return "quit";
|
||||
case EVENT_KEYBOARD_PRESSED : return "keypressed";
|
||||
case EVENT_KEYBOARD_RELEASED : return "keyreleased";
|
||||
case EVENT_KEYBOARD_TEXTINPUT : return "textinput";
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
enum {
|
||||
EVENT_NULL,
|
||||
EVENT_QUIT,
|
||||
EVENT_KEYBOARD_PRESSED,
|
||||
EVENT_KEYBOARD_RELEASED,
|
||||
EVENT_KEYBOARD_TEXTINPUT,
|
||||
@@ -21,6 +22,11 @@ enum {
|
||||
typedef union {
|
||||
int type;
|
||||
|
||||
struct {
|
||||
int type;
|
||||
int status;
|
||||
} quit;
|
||||
|
||||
struct {
|
||||
int type;
|
||||
int x, y;
|
||||
|
||||
@@ -21,6 +21,10 @@ int l_event_poll(lua_State *L) {
|
||||
lua_pushstring(L, event_typestr(e.type));
|
||||
|
||||
switch(e.type) {
|
||||
case EVENT_QUIT:
|
||||
lua_pushnumber(L, e.quit.status);
|
||||
return 2;
|
||||
|
||||
case EVENT_KEYBOARD_PRESSED:
|
||||
case EVENT_KEYBOARD_RELEASED:
|
||||
lua_pushstring(L, e.keyboard.key);
|
||||
@@ -46,16 +50,30 @@ int l_event_poll(lua_State *L) {
|
||||
lua_pushnumber(L, e.mouse.button);
|
||||
return 4;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int l_event_quit(lua_State *L) {
|
||||
int status = luaL_optnumber(L, 1, 0);
|
||||
event_t e;
|
||||
e.type = EVENT_QUIT;
|
||||
e.quit.status = status;
|
||||
event_push(&e);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int luaopen_event(lua_State *L) {
|
||||
luaL_Reg reg[] = {
|
||||
{ "pump", l_event_pump },
|
||||
{ "poll", l_event_poll },
|
||||
{ "quit", l_event_quit },
|
||||
{ 0, 0 },
|
||||
};
|
||||
luaL_newlib(L, reg);
|
||||
|
||||
Reference in New Issue
Block a user