Skip to content

Commit

Permalink
Add optional wait timeout parameter to love.event.pump.
Browse files Browse the repository at this point in the history
Deprecate love.event.wait.
  • Loading branch information
slime73 committed Oct 20, 2024
1 parent 0c7fc9c commit 88137d1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/modules/event/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 17 additions & 6 deletions src/modules/event/sdl/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64>(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<Message> 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);
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/modules/event/sdl/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
5 changes: 4 additions & 1 deletion src/modules/event/wrap_Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 88137d1

Please sign in to comment.