Sign in to follow this  

[SDL] Wait event

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
@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 :)

Share this post


Link to post
Share on other sites
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 program
int 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.

Share this post


Link to post
Share on other sites

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this