From 88137d1b6589c3a9ccc054cbe86a9cc9328191f7 Mon Sep 17 00:00:00 2001 From: Sasha Szpakowski Date: Sun, 20 Oct 2024 12:43:29 -0300 Subject: [PATCH] Add optional wait timeout parameter to love.event.pump. Deprecate love.event.wait. --- src/modules/event/Event.h | 2 +- src/modules/event/sdl/Event.cpp | 23 +++++++++++++++++------ src/modules/event/sdl/Event.h | 12 ++++-------- src/modules/event/wrap_Event.cpp | 5 ++++- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/modules/event/Event.h b/src/modules/event/Event.h index 3b3c33d44..e07a64e47 100644 --- a/src/modules/event/Event.h +++ b/src/modules/event/Event.h @@ -61,7 +61,7 @@ class Event : public Module bool poll(Message *&msg); virtual void clear(); - virtual void pump() = 0; + virtual void pump(float waitTimeout = 0.0f) = 0; virtual Message *wait() = 0; protected: diff --git a/src/modules/event/sdl/Event.cpp b/src/modules/event/sdl/Event.cpp index 869c10670..452dd7724 100644 --- a/src/modules/event/sdl/Event.cpp +++ b/src/modules/event/sdl/Event.cpp @@ -119,19 +119,30 @@ Event::~Event() SDL_QuitSubSystem(SDL_INIT_EVENTS); } -void Event::pump() +void Event::pump(float waitTimeout) { exceptionIfInRenderPass("love.event.pump"); - SDL_Event e; + int waitTimeoutMS = 0; + if (isinf(waitTimeout) || waitTimeout < 0.0f) + waitTimeoutMS = -1; // Wait forever. + else if (waitTimeout > 0.0f) + waitTimeoutMS = (int)std::min(LOVE_INT32_MAX, 1000LL * waitTimeout); - while (SDL_PollEvent(&e)) + // Wait for the first event, if requested. + SDL_Event e; + if (SDL_WaitEventTimeout(&e, waitTimeoutMS)) { - Message *msg = convert(e); + StrongRef msg(convert(e), Acquire::NORETAIN); if (msg) - { push(msg); - msg->release(); + + // Fetch any extra events that came in during WaitEvent. + while (SDL_PollEvent(&e)) + { + msg.set(convert(e), Acquire::NORETAIN); + if (msg) + push(msg); } } } diff --git a/src/modules/event/sdl/Event.h b/src/modules/event/sdl/Event.h index 78e166480..429059ef3 100644 --- a/src/modules/event/sdl/Event.h +++ b/src/modules/event/sdl/Event.h @@ -47,19 +47,15 @@ class Event : public love::event::Event * from devices and places it on the event queue. Normally not needed if you poll * for events. **/ - void pump(); + void pump(float waitTimeout = 0.0f) override; - /** - * Waits for the next event (indefinitely). Useful for creating games where - * the screen and game state only needs updating when the user interacts with - * the window. - **/ - Message *wait(); + // Deprecated. + Message *wait() override; /** * Clears the event queue. */ - void clear(); + void clear() override; private: diff --git a/src/modules/event/wrap_Event.cpp b/src/modules/event/wrap_Event.cpp index e51ff9557..175add59d 100644 --- a/src/modules/event/wrap_Event.cpp +++ b/src/modules/event/wrap_Event.cpp @@ -65,12 +65,15 @@ static int w_poll_i(lua_State *L) int w_pump(lua_State *L) { - luax_catchexcept(L, [&]() { instance()->pump(); }); + float waitTimeout = (float)luaL_optnumber(L, 1, 0.0f); + luax_catchexcept(L, [&]() { instance()->pump(waitTimeout); }); return 0; } int w_wait(lua_State *L) { + luax_markdeprecated(L, 1, "love.event.wait", API_FUNCTION, DEPRECATED_REPLACED, "waitTimeout parameter in love.event.pump"); + Message *m = nullptr; luax_catchexcept(L, [&]() { m = instance()->wait(); }); if (m != nullptr)