🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

SDL event problem

Started by
6 comments, last by Feidias 19 years, 11 months ago
I'm using a separate loop to test while mouse button is pressed, but when I'm doing some time consuming routines at the same time it doesn't work anymore. Looks like extra mouse button down -events are generated and when I release the mouse button it stays within the loop for some seconds, even there is a check for mouse button up -event. The code for processing events is taken from the example source, but I'm probably just doing something wrong anyway..
Advertisement
I'm not understanding what you mean by "using a separate loop to test while mouse button is pressed".
Can you show the code you're using? It would be simpler.
- AdMiRaLP.S. sorry, I'm french, so i may not always speak english perfectly.
Quote: Original post by AdMiRaL
I'm not understanding what you mean by "using a separate loop to test while mouse button is pressed".


Well.. for example when you draw a circle, the mouse button is pressed down while drawing. And then, when mouse button is released it should exit the function which handles the drawing (WHILE mouse button is pressed). Just wondering why it stays in the function couple of seconds after you release the button.

This code (in separate function) is called from the main() event loop:

  while (done==0)  {    while (SDL_PollEvent(&event))    {      if (event.type==SDL_QUIT) done=1;      if (event.type==SDL_MOUSEMOTION) //draw...      //this should exit the routine when releasing the mouse      //button (and it does when there is no time consuming      //routines in SDL_MOUSEMOTION part)      if (event.type==SDL_MOUSEBUTTONUP) done=1;

When you recieve the SDL_MOUSEBUTTONUP event, you're going to have to make sure that the state variable in the mouse button event structure equals SDL_RELEASED.

if (Event.type == SDL_MOUSEBUTTONUP && Event.button.state == SDL_RELEASED) {
done = 1;
}

And, by the way, it is much cleaner, when you are checking for several events, to use a switch statement, like this:

switch (Event.type) {	case SDL_QUIT:		Loop = false;	break;	case SDL_MOUSEMOTION:		OnMouseMove(Event.motion.x, Event.motion.y);	break;}
I get it now.
The problem is you're doing too much time consuming things in your processEvents() function.

I suggest to do things like that:

void processEvents(){  SDL_Event event;  while( SDL_PollEvent( &event ) )  {    switch( event.type )    {      case SDL_MOUSEMOTION:        Mouse::mouseMoveEvent( event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel );        break;      case SDL_MOUSEBUTTONDOWN:        Mouse::mousePressEvent( event.button.button );        break;      case SDL_MOUSEBUTTONUP:        Mouse::mouseReleaseEvent( event.button.button );        break;      case SDL_QUIT:      default:        return; // this should immediatly quit    }  }}void update_application(){  // work with Mouse stored state, like  if ( Mouse::buttonDown() )  {    draw();  }}int main(...){//...  while( !SDL_QuitRequested() )  {    update_application();    processEvents();  }...}


The above code supposes you have a namespace, static class or singleton named Mouse, which is used to store mouse state, for use outside processEvents().
The thing to remember is you should do minimal things in the processEvents() function.
Time consuming code should appear outside the function (or just the while loop if you prefer), or you will experiment the same problem again.
Hope that'll help.
- AdMiRaLP.S. sorry, I'm french, so i may not always speak english perfectly.
I think I got it now. When the mouse up -event was detected 'done' was set to 1, but it didn't break out from the while loop because at the same time mouse motion events were generated. So I just added a break-statement to the mouse up -event to break out the event loop.
That's quite a hack for me, but it may be sufficient in your case:
you must understand that this loop can poll events you don't handle (like SDL_VIDEORESIZE or SDL_KEYDOWN), which you might want to be handled.
If you want a more versatile (my opinion) way to handle system events, you should look at my example above.

Regards.
- AdMiRaLP.S. sorry, I'm french, so i may not always speak english perfectly.
Quote: Original post by AdMiRaL
That's quite a hack for me, but it may be sufficient in your case:
you must understand that this loop can poll events you don't handle (like SDL_VIDEORESIZE or SDL_KEYDOWN), which you might want to be handled.


There is no need to handle anything else during the drawing operation (line, box and ellipse).

Quote: If you want a more versatile (my opinion) way to handle system events, you should look at my example above.


I don't see how that would work better, because you have to break out from the event loop when mouse button is released. That wont happen while it's processing mouse move events. The event while loop is simply processing all events it detects, so there is only one way to break out directly.

This topic is closed to new replies.

Advertisement