SDL Polling slugishness

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:
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).

Is there something that I can do to speed things up?

Thanks…

Leeor…

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:
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).

Is there something that I can do to speed things up?

If it lags only when you move the mouse then you could set up an event
filter in your SDL init function to prevent mouse motion events from
entering the queue, with a line looking something like:

SDL_SetEventFilter(SDLEventFilter);

Your filter function should look something like:

int SDLEventFilter(const SDL_Event* filterEvent)
{
if (filterEvent->type == SDL_MOUSEMOTION)
return 0;

return 1;

}

Obviously this isn’t an option if you need mouse motion events.

JamesOn Tue, 06 Apr 2004 06:03:00 +0000, leeor_net wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 might be your problem:

| while (SDL_WaitEvent(&main_events) )

Maybe you mean SDL_PollEvent instead of SDL_WaitEvent? SDL_WaitEvent
waits indefinitely for the next available event, while SDL_PollEvent
will return immediately if there are no events in the queue.

Max Watson <@Max_Watson>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFAesSRmMf0Xg7SvtkRAk/2AKCpcR3nK91L+h19ek9yPy7Nkqm56gCfTMGy
N1zEG6z/9XzUsHD7HSmdZhA=
=psEx
-----END PGP SIGNATURE-----

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:
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 :slight_smile:

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 :slight_smile:

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.

	Bob PendletonOn Tue, 2004-04-06 at 01:03, leeor_net wrote:

Is there something that I can do to speed things up?

Thanks…

Leeor…


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±--------------------------------------+

well from what I see in that code, you are not polling but you are
"waiting" for events in that loop, which might explain the slow down. I’d
rather use SDL_PollEvent instead…

hope that helps…> 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:
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).

Is there something that I can do to speed things up?

Thanks…

Leeor…