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()
{
boost::unique_lock<boost::mutex> lock(eventsM);
while(!SDLEventsQueue.empty())
boost::unique_lock<boost::mutex> lock(eventsMutex);
for (auto const & currentEvent : eventsQueue)
{
GH.events().allowEventHandling(true);
SDL_Event currentEvent = SDLEventsQueue.front();
if (currentEvent.type == SDL_MOUSEMOTION)
{
cursorPosition = Point(currentEvent.motion.x, currentEvent.motion.y);
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);
}
eventsQueue.clear();
}
bool InputHandler::ignoreEventsUntilInput()
{
bool inputFound = false;
boost::unique_lock<boost::mutex> lock(eventsM);
while(!SDLEventsQueue.empty())
boost::unique_lock<boost::mutex> lock(eventsMutex);
for (auto const & event : eventsQueue)
{
SDL_Event ev = SDLEventsQueue.front();
SDLEventsQueue.pop();
switch(ev.type)
switch(event.type)
{
case SDL_MOUSEBUTTONDOWN:
case SDL_FINGERDOWN:
@@ -116,6 +105,7 @@ bool InputHandler::ignoreEventsUntilInput()
inputFound = true;
}
}
eventsQueue.clear();
return inputFound;
}
@@ -179,8 +169,18 @@ void InputHandler::preprocessEvent(const SDL_Event & ev)
}
{
boost::unique_lock<boost::mutex> lock(eventsM);
SDLEventsQueue.push(ev);
boost::unique_lock<boost::mutex> lock(eventsMutex);
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
{
std::queue<SDL_Event> SDLEventsQueue;
boost::mutex eventsM;
std::vector<SDL_Event> eventsQueue;
boost::mutex eventsMutex;
Point cursorPosition;
float pointerSpeedMultiplier;