Advertisement

SDL event problem

Started by August 14, 2004 06:35 AM
6 comments, last by Feidias 20 years, 3 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..
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.
Advertisement
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.
Advertisement
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