1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-11-25 09:44:02 +00:00

ignore mouse motion events generated by SDL_WarpMouse().

Also fix bug that mouse.change got stuck on a tiny value and constantly
move the camera in editor mode.
This is a continuation of 764d106d50 and seems to fix the
previous issues with SDL2 >= 2.0.17

Also remove some unused vars.
This commit is contained in:
fgenesis 2022-08-24 14:16:25 +02:00
parent cf6464daa7
commit a494a3f411
4 changed files with 62 additions and 69 deletions

View file

@ -500,7 +500,6 @@ void DSQ::debugMenu()
if (core->getShiftState()) if (core->getShiftState())
{ {
core->frameOutputMode = false;
dsq->game->togglePause(true); dsq->game->togglePause(true);
std::string s = dsq->getUserInputString(stringbank.get(2012), ""); std::string s = dsq->getUserInputString(stringbank.get(2012), "");
stringToUpper(s); stringToUpper(s);
@ -4155,12 +4154,6 @@ void DSQ::modifyDt(float &dt)
gameSpeed.update(dt); gameSpeed.update(dt);
dt *= gameSpeed.x; dt *= gameSpeed.x;
if (frameOutputMode)
{
dt = 1.0f/60.0f;
doScreenshot = true;
}
} }
void DSQ::removeElement(Element *element) void DSQ::removeElement(Element *element)

View file

@ -135,8 +135,6 @@ GameOver::GameOver() : StateObject()
void GameOver::applyState() void GameOver::applyState()
{ {
const bool frameOutputGameOver = false;
core->sound->fadeMusic(SFT_OUT, 1); core->sound->fadeMusic(SFT_OUT, 1);
@ -147,12 +145,6 @@ void GameOver::applyState()
core->sound->playSfx("Death"); core->sound->playSfx("Death");
if (frameOutputGameOver)
{
dsq->fpsText->alpha = 0;
core->frameOutputMode = true;
}
Quad *q = new Quad; Quad *q = new Quad;
{ {
q->color = 0; q->color = 0;
@ -248,13 +240,6 @@ void GameOver::applyState()
} }
else else
dsq->title(); dsq->title();
if (frameOutputGameOver)
core->frameOutputMode = false;
} }
void GameOver::removeState() void GameOver::removeState()

View file

@ -281,6 +281,7 @@ Core::Core(const std::string &filesystem, const std::string& extraDataDir, int n
window = NULL; window = NULL;
sound = NULL; sound = NULL;
_extraDataDir = extraDataDir; _extraDataDir = extraDataDir;
sdlUserMouseEventID = SDL_RegisterEvents(1);
if (userDataSubFolder.empty()) if (userDataSubFolder.empty())
userDataSubFolder = appName; userDataSubFolder = appName;
@ -364,8 +365,6 @@ Core::Core(const std::string &filesystem, const std::string& extraDataDir, int n
lib_graphics = lib_sound = lib_input = false; lib_graphics = lib_sound = lib_input = false;
mouseConstraint = false; mouseConstraint = false;
mouseCircle = 0; mouseCircle = 0;
frameOutputMode = false;
updateMouse = true;
particlesPaused = false; particlesPaused = false;
joystickAsMouse = false; joystickAsMouse = false;
flipMouseButtons = 0; flipMouseButtons = 0;
@ -677,8 +676,6 @@ void Core::onUpdate(float dt)
{ {
if (minimized) return; if (minimized) return;
core->mouse.lastPosition = core->mouse.position;
pollEvents(dt); pollEvents(dt);
@ -964,14 +961,20 @@ void Core::resetTimer()
void Core::setMousePosition(const Vector &p) void Core::setMousePosition(const Vector &p)
{ {
core->mouse.position = p;
float px = p.x + virtualOffX; float px = p.x + virtualOffX;
float py = p.y; float py = p.y;
SDL_Event ev = { sdlUserMouseEventID };
ev.motion.state = 0;
SDL_PushEvent(&ev);
window->warpMouse( window->warpMouse(
px * (float(width)/float(virtualWidth)), px * (float(width)/float(virtualWidth)),
py * (float(height)/float(virtualHeight)) py * (float(height)/float(virtualHeight))
); );
ev.motion.state = 1;
SDL_PushEvent(&ev);
} }
// used to update all render objects either uniformly or as part of a time sliced update process // used to update all render objects either uniformly or as part of a time sliced update process
@ -1274,6 +1277,12 @@ bool Core::doMouseConstraint()
void Core::onEvent(const SDL_Event& event) void Core::onEvent(const SDL_Event& event)
{ {
const bool focus = window->hasFocus(); const bool focus = window->hasFocus();
if(event.type == sdlUserMouseEventID)
{
mouse._enableMotionEvents = event.motion.state;
return;
}
switch (event.type) switch (event.type)
{ {
case SDL_KEYDOWN: case SDL_KEYDOWN:
@ -1312,10 +1321,15 @@ void Core::onEvent(const SDL_Event& event)
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
{ {
if (focus && updateMouse) if (focus)
{ {
mouse.position.x = ((event.motion.x) * (float(virtualWidth)/float(getWindowWidth()))) - getVirtualOffX(); const float mx = float(virtualWidth)/float(getWindowWidth());
mouse.position.y = event.motion.y * (float(virtualHeight)/float(getWindowHeight())); const float my = float(virtualHeight)/float(getWindowHeight());
mouse.position.x = ((event.motion.x) * mx) - getVirtualOffX();
mouse.position.y = event.motion.y * my;
if(mouse._enableMotionEvents)
mouse._wasMoved = true;
} }
} }
break; break;
@ -1324,7 +1338,7 @@ void Core::onEvent(const SDL_Event& event)
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:
{ {
if (focus && updateMouse) if (focus)
{ {
if (event.wheel.y > 0) if (event.wheel.y > 0)
mouse.scrollWheelChange = 1; mouse.scrollWheelChange = 1;
@ -1345,7 +1359,7 @@ void Core::onEvent(const SDL_Event& event)
#else #else
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
{ {
if (focus && updateMouse) if (focus)
{ {
switch(event.button.button) switch(event.button.button)
{ {
@ -1362,7 +1376,7 @@ void Core::onEvent(const SDL_Event& event)
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
{ {
if (focus && updateMouse) if (focus)
{ {
switch(event.button.button) switch(event.button.button)
{ {
@ -1382,49 +1396,47 @@ void Core::onEvent(const SDL_Event& event)
void Core::pollEvents(float dt) void Core::pollEvents(float dt)
{ {
bool warpMouse=false; int x, y;
unsigned mousestate = SDL_GetMouseState(&x,&y);
if (mouse.buttonsEnabled)
if (updateMouse)
{ {
int x, y; mouse.buttons.left = mousestate & SDL_BUTTON(1)?DOWN:UP;
unsigned mousestate = SDL_GetMouseState(&x,&y); mouse.buttons.right = mousestate & SDL_BUTTON(3)?DOWN:UP;
mouse.buttons.middle = mousestate & SDL_BUTTON(2)?DOWN:UP;
if (mouse.buttonsEnabled) for(unsigned i = 0; i < mouseExtraButtons; ++i)
mouse.buttons.extra[i] = mousestate & SDL_BUTTON(3+i)?DOWN:UP;
mouse.pure_buttons = mouse.buttons;
mouse.rawButtonMask = mousestate;
if (flipMouseButtons)
{ {
mouse.buttons.left = mousestate & SDL_BUTTON(1)?DOWN:UP; std::swap(mouse.buttons.left, mouse.buttons.right);
mouse.buttons.right = mousestate & SDL_BUTTON(3)?DOWN:UP;
mouse.buttons.middle = mousestate & SDL_BUTTON(2)?DOWN:UP;
for(unsigned i = 0; i < mouseExtraButtons; ++i)
mouse.buttons.extra[i] = mousestate & SDL_BUTTON(3+i)?DOWN:UP;
mouse.pure_buttons = mouse.buttons;
mouse.rawButtonMask = mousestate;
if (flipMouseButtons)
{
std::swap(mouse.buttons.left, mouse.buttons.right);
}
} }
else }
{ else
mouse.buttons.left = mouse.buttons.right = mouse.buttons.middle = UP; {
} mouse.buttons.left = mouse.buttons.right = mouse.buttons.middle = UP;
mouse.scrollWheelChange = 0;
mouse.change = Vector(0,0);
mouse.lastPosition = mouse.position;
} }
mouse.scrollWheelChange = 0;
mouse.lastPosition = mouse.position;
mouse.change = Vector(0, 0);
mouse._wasMoved = false;
// This polls SDL events and causes Core::onEvent() to be called,
// which also updates mouse position etc
window->handleInput(); window->handleInput();
if(updateMouse) if(mouse._wasMoved)
{ {
if (doMouseConstraint()) if(doMouseConstraint())
{
setMousePosition(mouse.position); setMousePosition(mouse.position);
window->handleInput();
}
mouse.change = mouse.position - mouse.lastPosition; mouse.change = mouse.position - mouse.lastPosition;
} }

View file

@ -89,6 +89,8 @@ struct Mouse
{ {
scrollWheelChange = 0; scrollWheelChange = 0;
buttonsEnabled = true; buttonsEnabled = true;
_wasMoved = false;
_enableMotionEvents = true;
} }
Vector position, lastPosition; Vector position, lastPosition;
MouseButtons buttons; MouseButtons buttons;
@ -96,8 +98,11 @@ struct Mouse
unsigned rawButtonMask; unsigned rawButtonMask;
Vector change; Vector change;
bool buttonsEnabled; bool buttonsEnabled;
int scrollWheelChange; int scrollWheelChange;
// transient; not for use outside of event handling
bool _wasMoved;
bool _enableMotionEvents;
}; };
enum FollowCameraLock enum FollowCameraLock
@ -383,9 +388,6 @@ public:
bool joystickAsMouse; bool joystickAsMouse;
virtual void prepScreen(bool t){} virtual void prepScreen(bool t){}
bool updateMouse;
bool frameOutputMode;
ParticleEffect* createParticleEffect(const std::string &name, const Vector &position, int layer, float rotz=0); ParticleEffect* createParticleEffect(const std::string &name, const Vector &position, int layer, float rotz=0);
std::string secondaryTexturePath; std::string secondaryTexturePath;
@ -523,6 +525,7 @@ public:
Joystick *getJoystickForSourceID(int sourceID); Joystick *getJoystickForSourceID(int sourceID);
private: private:
std::vector<Joystick*> joysticks; std::vector<Joystick*> joysticks;
int sdlUserMouseEventID;
}; };
extern Core *core; extern Core *core;