From de6d21be90d0b18c2e49cc394fd1fc00b66ae919 Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Mon, 27 Sep 2021 11:29:49 +0800 Subject: [PATCH] Accumulate SDL mouse motion events to avoid excess function calls and allocations --- src/application.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/application.cpp b/src/application.cpp index 2499e98..14bcfdb 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -511,6 +511,13 @@ void application::render(double alpha) void application::translate_sdl_events() { + // Mouse motion event accumulators + bool mouse_motion = false; + int mouse_x; + int mouse_y; + int mouse_dx = 0; + int mouse_dy = 0; + SDL_Event sdl_event; while (SDL_PollEvent(&sdl_event)) { @@ -537,8 +544,13 @@ void application::translate_sdl_events() case SDL_MOUSEMOTION: { - /// @WARNING: more than one mouse motion event is often generated per frame and may be a source of lag. - mouse->move(sdl_event.motion.x, sdl_event.motion.y, sdl_event.motion.xrel, sdl_event.motion.yrel); + // More than one mouse motion event is often generated per frame, and may be a source of lag. + // Mouse events are accumulated here to prevent excess function calls and allocations + mouse_motion = true; + mouse_x = sdl_event.motion.x; + mouse_y = sdl_event.motion.y; + mouse_dx += sdl_event.motion.xrel; + mouse_dy += sdl_event.motion.yrel; break; } @@ -673,6 +685,12 @@ void application::translate_sdl_events() } } } + + // Process accumulated mouse motion events + if (mouse_motion) + { + mouse->move(mouse_x, mouse_y, mouse_dx, mouse_dy); + } } void application::window_resized()