My_timer_id = SDL_AddTimer(delay, my_callbackfunc, my_callback_param);

my_timer_id = SDL_AddTimer(delay, my_callbackfunc, my_callback_param);

Hi,

Can someone help me with above?

I wish to call “GameEngineLoop” function 30 times per second.

Thanks!

P.S. - I got the little window and taskbar icon to work now (was wrong
dimensions)

Jesse “JeZ+Lee” Palser
SLNTHERO at aol.com
Silent Hero Productions®
Video Game Design Studio
http://www.SilentHeroProductions.com

Your game engine loop has nothing to do with the timer. It is the
main loop of the game. It handles all the functions thus only needs
to be called once to start it. Once started, it handles everything
else (or delegates out). It can loop 3 times per second or 300 times
per second.

Richard HancockOn Jul 7, 2008, at 4:12 PM, Jesse P. wrote:

my_timer_id = SDL_AddTimer(delay, my_callbackfunc, my_callback_param);

Hi,

Can someone help me with above?

I wish to call “GameEngineLoop” function 30 times per second.

Thanks!

P.S. - I got the little window and taskbar icon to work now (was
wrong dimensions)

Jesse “JeZ+Lee” Palser
SLNTHERO at aol.com
Silent Hero Productions®
Video Game Design Studio
http://www.SilentHeroProductions.com


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

It is best not to call a function like what I imagine "GameEngineLoop"
would do (modify mutable state) from a Timer callback. One way to do
it is with custom userevents( error checking not present: read the
documentation for the functions):

enum UserEventType
{
GAME_ENGINE_LOOP,
// other custom events
};

Uint32 callback(Uint32 interval, void *unused)
{
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = GAME_ENGINE_LOOP;
SDL_PushEvent(&event);
return interval;
}

void gameEngineLoop()
{
SDL_Surface *screen = SDL_GetVideoSurface();
SDL_FillRect(screen,0,rand());
SDL_Flip(screen);
}

int main(int, char **)
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
SDL_Surface *screen = SDL_SetVideoMode(800,600,32,SDL_SWSURFACE);

SDL_TimerID timer = SDL_AddTimer(static_cast<unsigned>(1000.0f /

30.0f), &callback, 0);

bool running = true;
while(running)
{
    SDL_Event event;
    while(SDL_PollEvent(&event))
    {
        // test for keys, mouse etc
        if(event.type == SDL_USEREVENT)
        {
            if(event.user.code == GAME_ENGINE_LOOP)
            {
                gameEngineLoop();
            }
            // else ( handle other events )
        }
        else if(event.type == SDL_QUIT)
        {
            running = false;
        }
        // other event types
    }
}
SDL_Quit();
return 0;

}

Using SDL_WaitEvent prevents “background” processing, which may or may
not be required (you may want animations to play more often - to sync
with the screen frame rate rather than the more artificial game logic
rate). You could choose additional timers to handle this.

Another option would be to try handle this in the main loop itself,
with no timers.On Mon, Jul 7, 2008 at 10:12 PM, Jesse P. wrote:

Can someone help me with above?

I wish to call “GameEngineLoop” function 30 times per second.

Hi,

Making a game and need a constant 30 Frames Per Second.
What is the smartest option?

JeZ+Lee
slnthero at aol.com

Brian wrote:> It is best not to call a function like what I imagine “GameEngineLoop”

would do (modify mutable state) from a Timer callback. One way to do
it is with custom userevents( error checking not present: read the
documentation for the functions):

enum UserEventType
{
GAME_ENGINE_LOOP,
// other custom events
};

Uint32 callback(Uint32 interval, void *unused)
{
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = GAME_ENGINE_LOOP;
SDL_PushEvent(&event);
return interval;
}

void gameEngineLoop()
{
SDL_Surface *screen = SDL_GetVideoSurface();
SDL_FillRect(screen,0,rand());
SDL_Flip(screen);
}

int main(int, char **)
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
SDL_Surface *screen = SDL_SetVideoMode(800,600,32,SDL_SWSURFACE);

SDL_TimerID timer = SDL_AddTimer(static_cast<unsigned>(1000.0f /

30.0f), &callback, 0);

bool running = true;
while(running)
{
    SDL_Event event;
    while(SDL_PollEvent(&event))
    {
        // test for keys, mouse etc
        if(event.type == SDL_USEREVENT)
        {
            if(event.user.code == GAME_ENGINE_LOOP)
            {
                gameEngineLoop();
            }
            // else ( handle other events )
        }
        else if(event.type == SDL_QUIT)
        {
            running = false;
        }
        // other event types
    }
}
SDL_Quit();
return 0;

}

Using SDL_WaitEvent prevents “background” processing, which may or may
not be required (you may want animations to play more often - to sync
with the screen frame rate rather than the more artificial game logic
rate). You could choose additional timers to handle this.

Another option would be to try handle this in the main loop itself,
with no timers.

On Mon, Jul 7, 2008 at 10:12 PM, Jesse P. wrote:

Can someone help me with above?

I wish to call “GameEngineLoop” function 30 times per second.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

You may also consider looking at the “Fixed rate pigs”, by David
Olofson (http://olofson.net/mixed.html).On Mon, Jul 7, 2008 at 11:03 PM, Jesse P. wrote:

Hi,

Making a game and need a constant 30 Frames Per Second.
What is the smartest option?

Do something like this:
void GameEngineLoop(whatever parameters)
{
while !finished
{
int Time = SDL_GetTicks();
HandleInputEvents;
ProcessStuff;
DoBlitting;
int timeRemaining = <insert calculation here, involving the difference between Time + 1/30 of a second and the current value of SDL_GetTicks()>
SDL_Delay(timeRemaining);
}
Cleanup;
}

This is, obviously, very rough pseudocode, but you get the idea. Know how long each frame is supposed to take, and at the end of each loop, calculate how much longer you’ve got left and sleep for that long.>----- Original Message ----

From: Jesse P.
Subject: Re: [SDL] my_timer_id = SDL_AddTimer(delay, my_callbackfunc, my_callback_param);

Hi,

Making a game and need a constant 30 Frames Per Second.
What is the smartest option?

Hi,

So the following is a good method to make game 30 Frames Per Second
(roughly) ?-----------------------------------------------------------------------------------------------------------------------
//Uint32 SystemTicks;
//Uint32 NextFrameTicks;
while (game->EXITgame == false)
{
SystemTicks = SDL_GetTicks();
NextFrameTicks = SystemTicks + 33;

    control->GetInputFromUser();
    if (game->GameScreen == PublishedByclnetScreen)  

DisplayPublishedByclnetScreen();
else if (game->GameScreen == SDLInformationScreen)
DisplaySDLInformationScreen();
else if (game->GameScreen == SilentHeroProductionsScreen)
DisplaySilentHeroProductionsScreen();
else if (game->GameScreen == TitleScreen) DisplayTitleScreen();

    SystemTicks = SDL_GetTicks();
    if (NextFrameTicks >= SystemTicks)  

SDL_Delay(NextFrameTicks-SystemTicks);
}

JeZ+Lee
SLNTHERO at aol.com

Mason Wheeler wrote:

Do something like this:
void GameEngineLoop(whatever parameters)
{
while !finished
{
int Time = SDL_GetTicks();
HandleInputEvents;
ProcessStuff;
DoBlitting;
int timeRemaining = <insert calculation here, involving the
difference between Time + 1/30 of a second and the current value of
SDL_GetTicks()>
SDL_Delay(timeRemaining);
}
Cleanup;
}

This is, obviously, very rough pseudocode, but you get the idea. Know
how long each frame is supposed to take, and at the end of each loop,
calculate how much longer you’ve got left and sleep for that long.

----- Original Message ----
From: Jesse P.
Subject: Re: [SDL] my_timer_id = SDL_AddTimer(delay, my_callbackfunc,
my_callback_param);

Hi,

Making a game and need a constant 30 Frames Per Second.
What is the smartest option?



SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Yes, but keep in mind the problems with SDL_Delay’s resolution, which can become significant at this level of precision. There was a thread discussing ways to deal with it on here last week.>----- Original Message ----

From: Jesse P.
Subject: Re: [SDL] my_timer_id = SDL_AddTimer(delay, my_callbackfunc, my_callback_param);

Hi,

So the following is a good method to make game 30 Frames Per Second
(roughly) ?