[SDL] Wait event

Started by
3 comments, last by Simian Man 16 years, 2 months ago
Hi, I use SDL_WaitEvent() so it will only draw the scene when neccessary. However, it does not react to input very well...you have to keep keys pressed for some time before its noticed. Is there any way I can only draw it when neccessary that DOES work properly?
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
Quote:Original post by Decrius
Hi,

I use SDL_WaitEvent() so it will only draw the scene when neccessary.

However, it does not react to input very well...you have to keep keys pressed for some time before its noticed.

Is there any way I can only draw it when neccessary that DOES work properly?


I have used SDL_WaitEvent and it always worked like a charm for me. Could you post your code?
Almost all scenes need to be drawn even when there's no new input. For example, enemies might move even while the player is stationary. For that, you'll need to draw *all the time*. So just draw all the time, even if it's not necessary right now.

while(running)
{
handle input;
update scene;
draw scene;
}
@Ezbez

Yeah, but I'm not making a game. I'm making a class that will handle text in and output via a console -> a GUI class to draw a console.

However, I do get incoming messages from external clients...

@Simian Man

I've used the example that comes with SDL itself...I'm pretty sure it should work. ..


Anyways, I think I found a solution:

SDL_Event event;    bool first_event;    bool done = false;    while (!done)    {        first_event = true;        while (!SDL_PollEvent(&event))        {            SDL_Delay(10);        }        while (first_event || SDL_PollEvent(&event))        {            switch (event.type)            {                // handle user input            }            first_event = false;        }                // draw scene    }


This works :), doesn't redraws the whole scene when its not needed :)

Thanks anyways :)
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Quote:Original post by Decrius
Yeah, but I'm not making a game. I'm making a class that will handle text in and output via a console -> a GUI class to draw a console.

However, I do get incoming messages from external clients...


If this is just a widget, I question whether you should be calling SDL_WaitEvent or SDL_PollEvent at all. If I'm using a widget, I don't expect or want it to be handling the event queue. I would implement your console like so (psuedocode):
class Console{   // ...   void handleEvent(SDL_Event& e)   {      // inspect the event and see if it pertains to us   }   // ...};// user programint main( ){   Console c;   // ...   while(SDL_PollEvent(&event))   {      c.handleEvent(event);      // handle app specific events   }   // ...}


This way the user program can handle input however it wants, and you don't have multiple event loops to keep track of.

This topic is closed to new replies.

Advertisement