From 704a8dea0993f9903c524f035818a51b5c6793dd Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Wed, 9 Jun 2021 23:37:03 +0200 Subject: [PATCH] Group mouse move events from C API function Groups together consecutive mouse move events like done in core.step() lua function but on the C side. It does not introduce any meaningful speedup but it theory is more efficient and simplifies the Lua code. The simplification of the Lua code alone is enough to justify this change? --- data/core/init.lua | 14 +++----------- src/api/system.c | 8 ++++++++ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/data/core/init.lua b/data/core/init.lua index cc4b46d8..95eb7973 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -948,26 +948,18 @@ end function core.step() -- handle events local did_keymap = false - local mouse_moved = false - local mouse = { x = 0, y = 0, dx = 0, dy = 0 } - for type, a,b,c,d in system.poll_event do - if type == "mousemoved" then - mouse_moved = true - mouse.x, mouse.y = a, b - mouse.dx, mouse.dy = mouse.dx + c, mouse.dy + d - elseif type == "textinput" and did_keymap then + if type == "textinput" and did_keymap then did_keymap = false + elseif type == "mousemoved" then + core.try(core.on_event, type, a, b, c, d) else local _, res = core.try(core.on_event, type, a, b, c, d) did_keymap = res or did_keymap end core.redraw = true end - if mouse_moved then - core.try(core.on_event, "mousemoved", mouse.x, mouse.y, mouse.dx, mouse.dy) - end local width, height = renderer.get_size() diff --git a/src/api/system.c b/src/api/system.c index bbe0801b..d00a9e47 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -201,6 +201,14 @@ top: return 4; case SDL_MOUSEMOTION: + SDL_PumpEvents(); + SDL_Event event_plus; + while (SDL_PeepEvents(&event_plus, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) > 0) { + e.motion.x = event_plus.motion.x; + e.motion.y = event_plus.motion.y; + e.motion.xrel += event_plus.motion.xrel; + e.motion.yrel += event_plus.motion.yrel; + } lua_pushstring(L, "mousemoved"); lua_pushnumber(L, e.motion.x); lua_pushnumber(L, e.motion.y);