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