1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-13 19:54:17 +02:00

Changed event queue to vector

This commit is contained in:
Ivan Savenko
2023-05-19 18:15:56 +03:00
parent 0e70f2998f
commit bb36336aed
2 changed files with 21 additions and 21 deletions

View File

@@ -75,40 +75,29 @@ void InputHandler::handleCurrentEvent(const SDL_Event & current)
void InputHandler::processEvents() void InputHandler::processEvents()
{ {
boost::unique_lock<boost::mutex> lock(eventsM); boost::unique_lock<boost::mutex> lock(eventsMutex);
while(!SDLEventsQueue.empty()) for (auto const & currentEvent : eventsQueue)
{ {
GH.events().allowEventHandling(true); GH.events().allowEventHandling(true);
SDL_Event currentEvent = SDLEventsQueue.front();
if (currentEvent.type == SDL_MOUSEMOTION) if (currentEvent.type == SDL_MOUSEMOTION)
{ {
cursorPosition = Point(currentEvent.motion.x, currentEvent.motion.y); cursorPosition = Point(currentEvent.motion.x, currentEvent.motion.y);
mouseButtonsMask = currentEvent.motion.state; mouseButtonsMask = currentEvent.motion.state;
} }
SDLEventsQueue.pop();
// In a sequence of mouse motion events, skip all but the last one.
// This prevents freezes when every motion event takes longer to handle than interval at which
// the events arrive (like dragging on the minimap in world view, with redraw at every event)
// so that the events would start piling up faster than they can be processed.
if ((currentEvent.type == SDL_MOUSEMOTION) && !SDLEventsQueue.empty() && (SDLEventsQueue.front().type == SDL_MOUSEMOTION))
continue;
handleCurrentEvent(currentEvent); handleCurrentEvent(currentEvent);
} }
eventsQueue.clear();
} }
bool InputHandler::ignoreEventsUntilInput() bool InputHandler::ignoreEventsUntilInput()
{ {
bool inputFound = false; bool inputFound = false;
boost::unique_lock<boost::mutex> lock(eventsM); boost::unique_lock<boost::mutex> lock(eventsMutex);
while(!SDLEventsQueue.empty()) for (auto const & event : eventsQueue)
{ {
SDL_Event ev = SDLEventsQueue.front(); switch(event.type)
SDLEventsQueue.pop();
switch(ev.type)
{ {
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
case SDL_FINGERDOWN: case SDL_FINGERDOWN:
@@ -116,6 +105,7 @@ bool InputHandler::ignoreEventsUntilInput()
inputFound = true; inputFound = true;
} }
} }
eventsQueue.clear();
return inputFound; return inputFound;
} }
@@ -179,8 +169,18 @@ void InputHandler::preprocessEvent(const SDL_Event & ev)
} }
{ {
boost::unique_lock<boost::mutex> lock(eventsM); boost::unique_lock<boost::mutex> lock(eventsMutex);
SDLEventsQueue.push(ev);
if(ev.type == SDL_MOUSEMOTION && !eventsQueue.empty() && eventsQueue.back().type == SDL_MOUSEMOTION)
{
// In a sequence of mouse motion events, skip all but the last one.
// This prevents freezes when every motion event takes longer to handle than interval at which
// the events arrive (like dragging on the minimap in world view, with redraw at every event)
// so that the events would start piling up faster than they can be processed.
eventsQueue.back() = ev;
return;
}
eventsQueue.push_back(ev);
} }
} }

View File

@@ -24,8 +24,8 @@ class UserEventHandler;
class InputHandler class InputHandler
{ {
std::queue<SDL_Event> SDLEventsQueue; std::vector<SDL_Event> eventsQueue;
boost::mutex eventsM; boost::mutex eventsMutex;
Point cursorPosition; Point cursorPosition;
float pointerSpeedMultiplier; float pointerSpeedMultiplier;