[SDL] Mouse events problem.

Started by
3 comments, last by Perost 19 years ago
I'm having a weird problem with mouse events in SDL. I've been using SDL for a long time now, and it's been working perfectly. But now I'm rewriting some parts of a game engine I'm working on, and commented away lots of code. Suddenly the mouse started to move very choppy. After having googled, debugged, profiled, googled and some more googling I'm clueless. It seems like SDL keeps all mouse events, and then once in a while it sends me all events at once, meaning I get about 40-60 mouse events in one frame. I've tried everything I can think of, and the only thing left to do is rewrite the entire game. I'm using Linux and OpenGL, and here's my event loop:

while(mRunLoop)
{
	while(SDL_PollEvent(&event))
	{
		switch(event.type)
		{ 
			case SDL_ACTIVEEVENT:
				if(event.active.gain == 0)
				{
				        active = false;
				}
				else
				{
					active = true;
				}
				break;	
			case SDL_VIDEORESIZE:
					//Resize window, OGL stuff.
					break;
				case SDL_KEYDOWN:
					if(event.key.keysym.sym == SDLK_ESCAPE)
					{
						return true;
					}
					break;
				case SDL_KEYUP:
					
					break;
				case SDL_MOUSEMOTION:
					cout << i2 << endl;
					i2++;
					event_engine->MouseEvent(event);
					break;
				case SDL_MOUSEBUTTONDOWN:
					event_engine->MouseEvent(event);
					break;
				case SDL_MOUSEBUTTONUP:
					event_engine->MouseEvent(event);
					break;
				case SDL_QUIT:
		
					return true;
					break;	
                                case default:
                                        break;
			}
		}
	
		if(active)
		{
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
 			glLoadIdentity(); 
			
			if(mGameLoop != 0)
			{
				mGameLoop->Call();
			}
			
			SDL_GL_SwapBuffers();
		}
 	}	
}
This event_engine->MouseEvent(event); sends events to my event_engine, which sends them to other places. Anyway, I've tried commenting them to, and it made no difference. i2 is a static int to keep track on mouse moves. Once in a while it prints out 50-60 numbers, and then nothing happens for a couple of seconds. If anyone can help me I'd be really happy, since I have absolutely no clue about this. Damn, I've got to learn to keep it short [smile]
Advertisement
Your code looks fine to me. I have worked with SDL a bit, so I know that in some cases, on Linux, problems might arise with various issues such as this. I have not used it in Linux though. However, here are a few things you can try:

First setup a Event filter to filter out the mouse events so they are sent into a different function, to which there you can process them. An example of doing that can be found here.

Second you can try to add a call to SDL_PumpEvents in a few different places in your code to see if that will help any.

Third you can try using a different structure for polling for messages. Poll event is the best, but just to see if you can get it to work otherways, try SDL_WaitEvent

That's about the best advice I can offer. What you should do as well is make a new SDL program that uses the same base SDL framework, but does not use your engine and see if you get the same results. This can tell you if it's SDL or your engine [smile] Good luck!

- Drew
Thanks for the reply Drew! I've used the same code in other apps, and it works. Must be something with my engine, but the problem started when I began removing stuff from it. Since I haven't removed anything that has something to do with the events I think it's weird. But I'll try your suggestions tomorrow (on my way to bed now [smile]). If it won't work I'll just have to rewrite everything from scratch. On second thought I might do that anyway [smile].
Quote:Original post by Perost
I've used the same code in other apps, and it works. Must be something with my engine, but the problem started when I began removing stuff from it.


[lol] Oh yes...that. I've done that many times before and broke my stuff. The best thing to do then is if you still have a back up copy, work with that and reapply your changes to it step by step to see what happened. If you have Word 2003 on a Windows machine, there is a Document Compare that I use in cases like this.

Starting over isn't too bad of an idea either [wink] I always do some rewriting in hopes of getting new better ideas.
Well, I still haven't solved this, so I'm giving up now. The code was a mess, so I'm starting over again. But thanks for the support.

This topic is closed to new replies.

Advertisement