Skip to main content
GameDev.net gamedev.net
🔒 Locked

[SOLVED] SDL - Mouse Movement Slows Framerate

Started by kiaran Mar 24, 2009 at 11:47 PM 10 replies 8.8k views
Original Post
kiaran
kiaran
I have an SDL application displaying OpenGL graphics. It runs very smoothly until my mouse moves over the screen. Then I get drastic jumps in frame times causing a headache inducing jitter of the whole screen. It's subtle, but noticeable and quite unacceptable. Frame times when mouse is stationary: 0.017 0.017 0.017 ...and when mouse is moving: 0 0.032 0 0.032 0 The problem happens ONLY when the mouse is MOVING and ONLY when over the screen. Moving the mouse off the window does not cause the problem. Leaving the mouse stationary hovering over the SDL window does not cause the problem. I thought maybe my mouse movement callback function was slowing things down, so I disabled it. I disabled event handling for mouse movement altogether and the problem remains. I also tried turning off the cursor with SDL_ShowCursor(0) thinking that perhaps drawing the cursor over the OpenGL viewport was causing the problem... but that didn't help. Interestingly, the problem is not visible on my laptop. Just my desktop PC. Any help would be greatly appreciated. EDIT: I have found and tested this the solution proposed in this forum. Amazingly, the mouse jitter issue has completely disappeared now: http://www.gamedev.net/community/forums/topic.asp?whichpage=1&pagesize=25&topic_id=496012
Quote:
Original post by bobmitch SOLVED!!! It's a quirk of nvidia's slightly experimental drivers I think. After puzzling over my code and how opengl works, I narrowed it down to either an SDL bug or a driver bug. The more I fiddled with various delays in my main loop, the more it seemed like a threading issue... but my code is running in a single thread right now. Solution: Disable threaded optimization in the advanced settings in the Nvidia control panel, it looks like it's set to Auto by default.
[Edited by - kiaran on October 14, 2009 12:02:38 PM]
Kylotan
Kylotan
A similar issue here. And I think there was a third thread on this in recent days also. Not much help though, sorry!
TriggerB
TriggerB
I would say the problem is probably in how you're collecting events.

Whenever you move the mouse, it fires off a machine gun of events and floods the SDL event queue. Depending upon how you're checking for events, this can cause a problem.

For example, if you're collecting events like this:

SDL_Event event;SDL_PollEvent(event);if(event){     if(event->type == SDL_KEYUP){          //do stuff     }}


Your program will check that if statement AND all of it's children on every single event in the queue...and if you move your mouse, theres A LOT of events to go through. Also, since you're only checking one event at a time (assuming you call PollEvent once per loop) the events will just keep building up.

So, I'd suggest maybe polling for events explicitly like

if(event->type->key){} //or something like that


or you could do:

while(SDL_PollEvent(event)){//handle stuff}


Which would prevent events from backing up I believe.
kiaran
kiaran
Thanks for the help guys.

Trigger, I think your assessment is correct. If I disable event handling altogether, the glitching disappears. I should have some time tmrw to put together new event handling code that avoids the redundant mouse movement events flooding the event-stack.

I'll post my solution. Btw, my current event loop that is causing the problem looks like this:

while(SDL_PollEvent(event))
{
//Big switch case statement
}

kiaran
kiaran
Quick Update:

I've had some time to look into this further. Unfortunately this is turning out to be a nasty little bug.

I stripped my code down to basically this:

while(running){  while(SDL_PollEvent(event))  {    //DO NOTHING!!!!  }}


So even with an empty while loop, simply polling each event is enough to cause this problem. Moving the mouse causes jerky frame hitches. I thought, how many frackin events could there be to cause this kind of slowdown so I put a counter inside the event loop and it printed out 1 or 2 per frame. That seems like hardly enough to cause this kind of frame thrashing.

So I thought maybe SDL_PollEvent was the culprit. Removing SDL_PollEvent removes the bug but also disables event handling, clearly not a solution. So I thought I'd try this instead:

while(running){  //Instead of PollEvent  SDL_PumpEvents();  //Get mouse and keyboard state explicitly  SDL_GetMouseState(x, y);  Uint8 *keystate = SDL_GetKeyState(NULL);  if ( keystate[SDLK_RETURN] )    printf("Return Key Pressed.\n");}


But SDL_PumpEvents() causes the same mouse jitter issue as PollEvent does!!

This is getting really nasty. Unless anyone has any suggestions, I'm going to go with a platform specific mouse/keyboard query.

Thanks in advance!


kiaran
kiaran
BUMP.

I've reproduced this problem on multiple machines with very different configurations. Severe framerate glitching resulting in jittery animation when mouse moves across screen.

It has to be a bug with SDL. Since all I have to do to reproduce it is setup an empty event loop!
Zakwayda
Zakwayda
Is this 1.2.x, or 1.3?

If you're still stuck on this, you might ask about it on the SDL mailing list, and/or file a bug report. If this is in fact a bug, I imagine the developers would like to know about it (especially if this is v1.3).
kiaran
kiaran
jyk - I am using SDL 1.2. I didn't even know there was a 1.3 version out (I see it now in the SVN branch).

I will create a minimal application using SDL 1.3 and see if the problem persists. Thanks for the link to the mailing list too!

I would be very curious if anyone else has experienced this problem. If you're reading this and get this problem as well, please respond!
instinct46
instinct46
Right solved the problem ^^ instead of shoving this in the event handler I just placed it just above. Also remember to make the pointer(s) called mouseY, & mouseX if you wish to control the X axis as well.

The pointers should point to a varible which can easily be reconized as the mouses x n y mine are as follows.

mouseY = &mouseControls.y
mouseX = &mouseControls.x

SDL_GetMouseState(NULL, mouseY); //IMPORTANT PART// NULL WOULD BE mouseX

//The rest is just how I
if(mouseControls.y < (mainPlayers[PLAYERTWO].y + (mainPlayers[PLAYERTWO].height / 2) - 5) ){
mainPlayers[PLAYERTWO].ym = -5;
mainPlayers[PLAYERTWO].imageshow = 2;
}
if(mouseControls.y > (mainPlayers[PLAYERTWO].y + (mainPlayers[PLAYERTWO].height / 2) + 5) ){
mainPlayers[PLAYERTWO].ym = 5;
mainPlayers[PLAYERTWO].imageshow = 3;
}
if(mouseControls.y == (mainPlayers[PLAYERTWO].y + (mainPlayers[PLAYERTWO].height / 2)) ){
mainPlayers[PLAYERTWO].ym = 0;
mainPlayers[PLAYERTWO].imageshow = 0;
}

If their are any problems with this that anyone notices please pm me.
Kylotan
Kylotan
Quote:
Original post by instinct46
Right solved the problem ^^

Did you post this in the wrong thread?
kiaran
kiaran
instinct46 - Your first sentence got me very excited! But alas. it looks like you've posted in the wrong thread.
instinct46
instinct46
Sorry if it wasn't the same problem, but before I did the above code, and I had just added the mouse controls to my game the problem dramatically slowed down my frame rate.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.