[SDL] SDL Polling slugishness...
bob at pendleton.com
Mon Apr 12 15:23:21 PDT 2004
On Tue, 2004-04-06 at 01:03, leeor_net wrote:
> My program is extremely sluggish. Everything else works fine but the polling
> section is horribly slow! The lag just won't work for a real-time game.
> Can anybody help me with this?
> This is the current code I have:
> while (SDL_WaitEvent(&main_events) )
> switch (main_events.type)
> case SDL_QUIT:
> case SDL_KEYDOWN:
> // Check for the different keystates
> switch (main_events.key.keysym.sym)
> case SDLK_ESCAPE:
> goto terminate_program;
> case SDLK_UP:
> case SDLK_DOWN:
> case SDLK_LEFT:
> case SDLK_RIGHT:
> case SDL_MOUSEBUTTONDOWN:
> mouse_state = main_events.button.button;
> (I hope the formatting stays put).
It did :-)
Ok, there are several things that are worrisome in your code. First off,
you are using SDL_WaitEvent(). Which means that if there are no events
to process your program will wait until there is an event. That is
probably not what you want. Usually people use SDL_PollEvent() so that
they can process all pending events and then get on with the rest of the
code. The code you show will process all pending events and then wait
until more events come in. That can make it look like your program is
running slowly when in fact it is running as fast as it can.
Secondly, you do not show the bottom part of the loop, or the outer loop
of your program. So, I don't know what the context of this code is. It
may be that you are updating you game after processing each event. Since
moving the mouse can generate a huge number of events updating the
screen after each mouse event can make the program look very slow. But,
I can't tell what you are doing from the code you have posted.
Third, you haven't provided any reason for why you think it isn't
working correctly. What does it do that makes you think it is being
slow? For all I know you have a slow computer are are trying to do to
much in the rest of the program. I can't know what else you are doing so
I can't even be sure you are asking the right question :-)
Of course, there are ways of making this code fragment work correctly,
and there are times when I would use code like yours, but you haven't
provided enough information to let us know that it should work
correctly. And, it follows a pattern that is usually wrong.
> Is there something that I can do to speed things up?
> SDL mailing list
> SDL at libsdl.org
+ Bob Pendleton: writer and programmer. +
+ email: Bob at Pendleton.com +
+ web: www.GameProgrammer.com +
More information about the SDL