Fix initial mouse position and unsafe SDL_Event union access

This commit is contained in:
Tamás Bálint Misius 2020-08-23 00:25:43 +02:00
parent fd032eff36
commit efc693bf43
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
3 changed files with 13 additions and 5 deletions

View File

@ -406,11 +406,11 @@ void EventProcess(SDL_Event event)
// if mouse hasn't moved yet, sdl will send 0,0. We don't want that
if (hasMouseMoved)
{
mousex = event.motion.x;
mousey = event.motion.y;
mousex = event.button.x;
mousey = event.button.y;
}
mouseButton = event.button.button;
engine->onMouseClick(event.motion.x, event.motion.y, mouseButton);
engine->onMouseClick(mousex, mousey, mouseButton);
mouseDown = true;
#if !defined(NDEBUG) && !defined(DEBUG)
@ -421,8 +421,8 @@ void EventProcess(SDL_Event event)
// if mouse hasn't moved yet, sdl will send 0,0. We don't want that
if (hasMouseMoved)
{
mousex = event.motion.x;
mousey = event.motion.y;
mousex = event.button.x;
mousey = event.button.y;
}
mouseButton = event.button.button;
engine->onMouseUnclick(mousex, mousey, mouseButton);
@ -441,6 +441,7 @@ void EventProcess(SDL_Event event)
{
//initial mouse coords, sdl won't tell us this if mouse hasn't moved
CalculateMousePosition(&mousex, &mousey);
engine->initialMouse(mousex, mousey);
engine->onMouseMove(mousex, mousey);
calculatedInitialMouse = true;
}

View File

@ -276,6 +276,12 @@ void Engine::onMouseUnclick(int x, int y, unsigned button)
state_->DoMouseUp(x, y, button);
}
void Engine::initialMouse(int x, int y)
{
mousexp_ = x;
mouseyp_ = y;
}
void Engine::onMouseMove(int x, int y)
{
mousex_ = x;

View File

@ -25,6 +25,7 @@ namespace ui
void ShowWindow(Window * window);
int CloseWindow();
void initialMouse(int x, int y);
void onMouseMove(int x, int y);
void onMouseClick(int x, int y, unsigned button);
void onMouseUnclick(int x, int y, unsigned button);