mirror of
https://github.com/love2d/love.git
synced 2026-08-16 16:20:42 +02:00
Updated the Lua wrapper code for modules to account for cases where the module's instance is removed from memory completely and recreated during the program's lifetime.
This commit is contained in:
@@ -82,6 +82,7 @@ Message *Message::fromLua(lua_State *L, int n)
|
||||
|
||||
Event::Event()
|
||||
{
|
||||
moduleType = M_EVENT;
|
||||
mutex = thread::newMutex();
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ static void windowToPixelCoords(int *x, int *y)
|
||||
{
|
||||
double scale = 1.0;
|
||||
|
||||
window::Window *window = (window::Window *) Module::findInstance("love.window.");
|
||||
window::Window *window = Module::getInstance<window::Window>(Module::M_WINDOW);
|
||||
if (window != nullptr)
|
||||
scale = window->getPixelScale();
|
||||
|
||||
@@ -129,7 +129,7 @@ Message *Event::convert(const SDL_Event &e) const
|
||||
case SDL_KEYDOWN:
|
||||
if (e.key.repeat)
|
||||
{
|
||||
kb = (love::keyboard::Keyboard *) Module::findInstance("love.keyboard.");
|
||||
kb = Module::getInstance<love::keyboard::Keyboard>(Module::M_KEYBOARD);
|
||||
if (kb && !kb->hasKeyRepeat())
|
||||
break;
|
||||
}
|
||||
@@ -245,7 +245,7 @@ Message *Event::convert(const SDL_Event &e) const
|
||||
|
||||
Message *Event::convertJoystickEvent(const SDL_Event &e) const
|
||||
{
|
||||
joystick::JoystickModule *joymodule = (joystick::JoystickModule *) Module::findInstance("love.joystick.");
|
||||
joystick::JoystickModule *joymodule = Module::getInstance<joystick::JoystickModule>(Module::M_JOYSTICK);
|
||||
if (!joymodule)
|
||||
return 0;
|
||||
|
||||
@@ -416,7 +416,7 @@ Message *Event::convertWindowEvent(const SDL_Event &e) const
|
||||
arg1->release();
|
||||
break;
|
||||
case SDL_WINDOWEVENT_RESIZED:
|
||||
win = (window::Window *) Module::findInstance("love.window.");
|
||||
win = Module::getInstance<window::Window>(Module::M_WINDOW);
|
||||
if (win)
|
||||
{
|
||||
int px_w = e.window.data1;
|
||||
@@ -431,7 +431,7 @@ Message *Event::convertWindowEvent(const SDL_Event &e) const
|
||||
|
||||
win->onWindowResize(e.window.data1, e.window.data2);
|
||||
|
||||
graphics::Graphics *gfx = (graphics::Graphics *) Module::findInstance("love.graphics.");
|
||||
graphics::Graphics *gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
|
||||
if (gfx)
|
||||
gfx->setViewportSize(px_w, px_h);
|
||||
|
||||
|
||||
@@ -33,13 +33,13 @@ namespace event
|
||||
namespace sdl
|
||||
{
|
||||
|
||||
static Event *instance = 0;
|
||||
#define instance() (Module::getInstance<Event>(Module::M_EVENT))
|
||||
|
||||
static int poll_i(lua_State *L)
|
||||
{
|
||||
Message *m;
|
||||
|
||||
while (instance->poll(m))
|
||||
while (instance()->poll(m))
|
||||
{
|
||||
int args = m->toLua(L);
|
||||
m->release();
|
||||
@@ -52,7 +52,7 @@ static int poll_i(lua_State *L)
|
||||
|
||||
int w_pump(lua_State *)
|
||||
{
|
||||
instance->pump();
|
||||
instance()->pump();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ int w_wait(lua_State *L)
|
||||
{
|
||||
Message *m;
|
||||
|
||||
if ((m = instance->wait()))
|
||||
if ((m = instance()->wait()))
|
||||
{
|
||||
int args = m->toLua(L);
|
||||
m->release();
|
||||
@@ -86,7 +86,7 @@ int w_push(lua_State *L)
|
||||
if (!success)
|
||||
return 1;
|
||||
|
||||
instance->push(m);
|
||||
instance()->push(m);
|
||||
m->release();
|
||||
|
||||
return 1;
|
||||
@@ -94,14 +94,14 @@ int w_push(lua_State *L)
|
||||
|
||||
int w_clear(lua_State *)
|
||||
{
|
||||
instance->clear();
|
||||
instance()->clear();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_quit(lua_State *L)
|
||||
{
|
||||
Message *m = new Message("quit");
|
||||
instance->push(m);
|
||||
instance()->push(m);
|
||||
m->release();
|
||||
luax_pushboolean(L, true);
|
||||
return 1;
|
||||
@@ -121,7 +121,8 @@ static const luaL_Reg functions[] =
|
||||
|
||||
extern "C" int luaopen_love_event(lua_State *L)
|
||||
{
|
||||
if (instance == 0)
|
||||
Event *instance = instance();
|
||||
if (instance == nullptr)
|
||||
{
|
||||
luax_catchexcept(L, [&](){ instance = new Event(); });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user