Constant framerate

Hello,

Is there a way to achieve constant framerate (same on all machines),
except using SDL_Delay ?
I remember previous posts on that topic where it was said that below
10 ms the precision was poor.
To have a high FPS I would need to call SDL_Delay(2) or so, and I don’t
think it’s very accurate.

Thanks,

Julien CLEMENT
@Julien_Clement1

Hi Julien,
As I’ve mentioned in other threads, it’s not usually a good idea to have a
consistent framerate - instead, you probably want to look into frame rate
independent movement. I think limiting the frame rate should be a
last-option, simply because if you set the frame rate too high for a
poor-performing machine, the software will run very poorly.
-OzOn Sat, May 14, 2011 at 3:49 PM, Julien CLEMENT wrote:

Hello,

Is there a way to achieve constant framerate (same on all machines),
except using SDL_Delay ?
I remember previous posts on that topic where it was said that below
10 ms the precision was poor.
To have a high FPS I would need to call SDL_Delay(2) or so, and I don’t
think it’s very accurate.

Thanks,

Julien CLEMENT
clementj2005 at yahoo.fr


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

Hi,On 2011-05-14 9:49 PM, Julien CLEMENT wrote:

Is there a way to achieve constant framerate (same on all machines),
except using SDL_Delay ?
I remember previous posts on that topic where it was said that below
10 ms the precision was poor.
To have a high FPS I would need to call SDL_Delay(2) or so, and I don’t
think it’s very accurate.
What you want to be doing is adding the time from last frame to this
frame to an accumulator value and step forward in your simulation code
until no time is left on the accumulator. Then make sure that you have
at least 1ms sleep at the end of the loop so that you give your timer
some precision.

And then render as fast as possible and activate vsync, that makes most
sense. The downside is that because your simulation runs at a fixed
speed it does not help if your computer is rendering faster than the
simulation is. The fix for this problem is keeping the last physics
state and the current one and lerping between them.

Google for “fix your timestep”. There is an article that explains the
concept in much greater detail.

Regards,
Armin

This may be an obviously bad idea, but what about using an SDL Timer?
I just set the timer call back to be 20ms, which is 50 fps (or 10ms
for 100fps). If you can be sure that the system can handle it (this is
where the real issue is, what to do with under-performers: drop
frames, frame-rate independent movement, etc.) then you are fine.
Maybe this doesn’t work for certain applications, but I’ve been doing
this since back in the glut/freeglut days before I started with SDL
and all my graphics classes taught us to do it this way.

Hope that helps,

JohnOn Sun, May 15, 2011 at 8:24 AM, Armin Ronacher <armin.ronacher at active-4.com> wrote:

Hi,

On 2011-05-14 9:49 PM, Julien CLEMENT wrote:

Is there a way to achieve constant framerate (same on all machines),
except using SDL_Delay ?
I remember previous posts on that topic where it was said that below
10 ms the precision was poor.
To have a high FPS I would need to call SDL_Delay(2) or so, and I don’t
think it’s very accurate.

What you want to be doing is adding the time from last frame to this frame
to an accumulator value and step forward in your simulation code until no
time is left on the accumulator. ?Then make sure that you have at least 1ms
sleep at the end of the loop so that you give your timer some precision.

And then render as fast as possible and activate vsync, that makes most
sense. ?The downside is that because your simulation runs at a fixed speed
it does not help if your computer is rendering faster than the simulation
is. ?The fix for this problem is keeping the last physics state and the
current one and lerping between them.

Google for “fix your timestep”. ?There is an article that explains the
concept in much greater detail.

Regards,
Armin


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

framerate independent movement is definitely the way to do it.

Generally, you want your objects to move the same distance in the same time on every machine; right?
Setting an FPS limit will only make this happen on systems that render faster than the limit you set. Many systems, especially if you’re using the SW renderer, will not do this.
Framerate independent movement works like this:

Code:

class MyAnimatonType
{
[…]
void step(Uint64 elapsedtime);
[…]
};

class LogicObject
{
[…]
void step(Uint64 elapsedtime);
[…]
};
[…]
std::vector<MyAnimationType*> my_animations;
std::vector<LogicObject*> my_logics;
[…]

Uint64 lasttime = SDL_GetTicks();
while(game_is_running)
{
Uint64 now = SDL_GetTicks();
Uint64 elapsed = now - lasttime;

for(std::vector<MyAnimationType*>::iterator it = my_animations.begin(); it != my_animations.end(); ++it)
{
    (*it)->step(elapsed);
}
for(std::vector<LogicObject*>::iterator it = my_logics.begin(); it != my_logics.end(); ++it)
{
    (*it)->step(elapsed);
}
lasttime = now;

}

I’m not sure if SDL_GetTicks() is thread-safe (and if not, change all code relying on SDL_GetTicks to use a special version wrapped with a spinlock or mutex) on all platforms, but this should work whether you run your logic in the same thread as you draw or not (although you would probably need to move animations to the drawing loop).------------------------
EM3 Nathaniel Fries, U.S. Navy

http://natefries.net/

Hello,

Thank you for all your advices.
I’ve read the documentation mentioned (by Armin).

Let me know if I have understood the solution using the “accumulator”.
If the machine is fast enough, I will get more refresh of the display,
for instance each 2 steps of simulation, while in a slower machine
I will have a less smooth motion due to less refreshes, like one each
8 steps of simulation. Is that correct ?

The (very) pseudo code would be something like :

loop {
step = 0;
while (step <= physicsNSteps) {
updatePhysics();
++step;
}
refresh();
}

and the slower the machine, the bigger “physicsNSteps”.
The ideal case would be one refresh for each step of simulation
for which the graphics has changed.

Is that more or less correct ?

Thanks again, lads !

Julien

Le 15 mai 2011 ? 20:35, sdl-request at lists.libsdl.org a ?crit :> Send SDL mailing list submissions to

sdl at lists.libsdl.org

To subscribe or unsubscribe via the World Wide Web, visit
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
or, via email, send a message with subject or body ‘help’ to
sdl-request at lists.libsdl.org

You can reach the person managing the list at
sdl-owner at lists.libsdl.org

When replying, please edit your Subject line so it is more specific
than “Re: Contents of SDL digest…”

Today’s Topics:

  1. Constant framerate (Julien CLEMENT)
  2. Re: Constant framerate (Alex Barry)
  3. Re: Compiling sdl application with msvc (image28)
  4. Re: Compiling sdl application with msvc (image28)
  5. Re: Constant framerate (Armin Ronacher)
  6. Re: Constant framerate (John Magnotti)
  7. Re: Constant framerate (Nathaniel J Fries)

Message: 1
Date: Sat, 14 May 2011 21:49:25 +0200
From: Julien CLEMENT <@Julien_Clement1>
To: SDL
Subject: [SDL] Constant framerate
Message-ID:
Content-Type: text/plain; charset=us-ascii

Hello,

Is there a way to achieve constant framerate (same on all machines),
except using SDL_Delay ?
I remember previous posts on that topic where it was said that below
10 ms the precision was poor.
To have a high FPS I would need to call SDL_Delay(2) or so, and I don’t
think it’s very accurate.

Thanks,

Julien CLEMENT
@Julien_Clement1


Message: 2
Date: Sat, 14 May 2011 16:02:00 -0400
From: Alex Barry <alex.barry at gmail.com>
To: SDL Development List
Subject: Re: [SDL] Constant framerate
Message-ID:
Content-Type: text/plain; charset=“iso-8859-1”

Hi Julien,
As I’ve mentioned in other threads, it’s not usually a good idea to have a
consistent framerate - instead, you probably want to look into frame rate
independent movement. I think limiting the frame rate should be a
last-option, simply because if you set the frame rate too high for a
poor-performing machine, the software will run very poorly.
-Oz

On Sat, May 14, 2011 at 3:49 PM, Julien CLEMENT <@Julien_Clement1>wrote:

Hello,

Is there a way to achieve constant framerate (same on all machines),
except using SDL_Delay ?
I remember previous posts on that topic where it was said that below
10 ms the precision was poor.
To have a high FPS I would need to call SDL_Delay(2) or so, and I don’t
think it’s very accurate.

Thanks,

Julien CLEMENT
@Julien_Clement1


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

-------------- next part --------------
An HTML attachment was scrubbed…
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110514/563f9fb4/attachment-0001.htm


Message: 3
Date: Sat, 14 May 2011 18:49:01 -0700
From: “image28”
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Compiling sdl application with msvc
Message-ID: <1305424141.m2f.28912 at forums.libsdl.org>
Content-Type: text/plain; charset=“iso-8859-1”

Using vc++ 2008, which doesn’t seem to have a subsystem option.
it’s outputing…

ignoring unknown option ‘/SUBSYSTEM:console’

-------------- next part --------------
An HTML attachment was scrubbed…
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110514/44504ce3/attachment-0001.htm


Message: 4
Date: Sat, 14 May 2011 19:02:56 -0700
From: “image28”
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Compiling sdl application with msvc
Message-ID: <1305424976.m2f.28913 at forums.libsdl.org>
Content-Type: text/plain; charset=“iso-8859-1”

was a linker option… Game is compiled an running with google-breakpad crash reporting :slight_smile:

Thanks heaps.
Kevin

-------------- next part --------------
An HTML attachment was scrubbed…
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110514/a3df92e6/attachment-0001.htm


Message: 5
Date: Sun, 15 May 2011 15:24:54 +0200
From: Armin Ronacher <armin.ronacher at active-4.com>
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Constant framerate
Message-ID: <4DCFD426.5010301 at active-4.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi,

On 2011-05-14 9:49 PM, Julien CLEMENT wrote:

Is there a way to achieve constant framerate (same on all machines),
except using SDL_Delay ?
I remember previous posts on that topic where it was said that below
10 ms the precision was poor.
To have a high FPS I would need to call SDL_Delay(2) or so, and I don’t
think it’s very accurate.
What you want to be doing is adding the time from last frame to this
frame to an accumulator value and step forward in your simulation code
until no time is left on the accumulator. Then make sure that you have
at least 1ms sleep at the end of the loop so that you give your timer
some precision.

And then render as fast as possible and activate vsync, that makes most
sense. The downside is that because your simulation runs at a fixed
speed it does not help if your computer is rendering faster than the
simulation is. The fix for this problem is keeping the last physics
state and the current one and lerping between them.

Google for “fix your timestep”. There is an article that explains the
concept in much greater detail.

Regards,
Armin


Message: 6
Date: Sun, 15 May 2011 12:31:45 -0500
From: John Magnotti <john.magnotti at gmail.com>
To: SDL Development List
Subject: Re: [SDL] Constant framerate
Message-ID: <BANLkTimmXnRfF+umoX8s0BNh1tOCmAG5Ew at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

This may be an obviously bad idea, but what about using an SDL Timer?
I just set the timer call back to be 20ms, which is 50 fps (or 10ms
for 100fps). If you can be sure that the system can handle it (this is
where the real issue is, what to do with under-performers: drop
frames, frame-rate independent movement, etc.) then you are fine.
Maybe this doesn’t work for certain applications, but I’ve been doing
this since back in the glut/freeglut days before I started with SDL
and all my graphics classes taught us to do it this way.

Hope that helps,

John

On Sun, May 15, 2011 at 8:24 AM, Armin Ronacher <armin.ronacher at active-4.com> wrote:

Hi,

On 2011-05-14 9:49 PM, Julien CLEMENT wrote:

Is there a way to achieve constant framerate (same on all machines),
except using SDL_Delay ?
I remember previous posts on that topic where it was said that below
10 ms the precision was poor.
To have a high FPS I would need to call SDL_Delay(2) or so, and I don’t
think it’s very accurate.

What you want to be doing is adding the time from last frame to this frame
to an accumulator value and step forward in your simulation code until no
time is left on the accumulator. ?Then make sure that you have at least 1ms
sleep at the end of the loop so that you give your timer some precision.

And then render as fast as possible and activate vsync, that makes most
sense. ?The downside is that because your simulation runs at a fixed speed
it does not help if your computer is rendering faster than the simulation
is. ?The fix for this problem is keeping the last physics state and the
current one and lerping between them.

Google for “fix your timestep”. ?There is an article that explains the
concept in much greater detail.

Regards,
Armin


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


Message: 7
Date: Sun, 15 May 2011 11:33:31 -0700
From: “Nathaniel J Fries”
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Constant framerate
Message-ID: <1305484411.m2f.28916 at forums.libsdl.org>
Content-Type: text/plain; charset=“iso-8859-1”

framerate independent movement is definitely the way to do it.

Generally, you want your objects to move the same distance in the same time on every machine; right?
Setting an FPS limit will only make this happen on systems that render faster than the limit you set. Many systems, especially if you’re using the SW renderer, will not do this.
Framerate independent movement works like this:

Code:

class MyAnimatonType
{
[…]
void step(Uint64 elapsedtime);
[…]
};

class LogicObject
{
[…]
void step(Uint64 elapsedtime);
[…]
};
[…]
std::vector<MyAnimationType*> my_animations;
std::vector<LogicObject*> my_logics;
[…]

Uint64 lasttime = SDL_GetTicks();
while(game_is_running)
{
Uint64 now = SDL_GetTicks();
Uint64 elapsed = now - lasttime;

for(std::vector<MyAnimationType*>::iterator it = my_animations.begin(); it != my_animations.end(); ++it)
{
(it)->step(elapsed);
}
for(std::vector<LogicObject
>::iterator it = my_logics.begin(); it != my_logics.end(); ++it)
{
(*it)->step(elapsed);
}
lasttime = now;
}

I’m not sure if SDL_GetTicks() is thread-safe (and if not, change all code relying on SDL_GetTicks to use a special version wrapped with a spinlock or mutex) on all platforms, but this should work whether you run your logic in the same thread as you draw or not (although you would probably need to move animations to the drawing loop).


EM3 Nathaniel Fries, U.S. Navy

http://natefries.net/

-------------- next part --------------
An HTML attachment was scrubbed…
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110515/eb6c1beb/attachment.htm



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

End of SDL Digest, Vol 53, Issue 37


Julien CLEMENT
@Julien_Clement1

it would be more like this:

// Setup
timestep = 1/60
accumulator = 0.0
lastUpdate = SDL_GetTicks()

// Running code
secFromLast = SDL_GetTicks() - lastUpdate
accumulator += secFromLast
while accumulator >= 0 {
updatePhysics( timestep )
accumulator -= timestep
}
lastUpdate = SDL_GetTicks()

-AlexOn Sun, May 15, 2011 at 4:15 PM, Julien CLEMENT wrote:

Hello,

Thank you for all your advices.
I’ve read the documentation mentioned (by Armin).

Let me know if I have understood the solution using the “accumulator”.
If the machine is fast enough, I will get more refresh of the display,
for instance each 2 steps of simulation, while in a slower machine
I will have a less smooth motion due to less refreshes, like one each
8 steps of simulation. Is that correct ?

The (very) pseudo code would be something like :

loop {
step = 0;
while (step <= physicsNSteps) {
updatePhysics();
++step;
}
refresh();
}

and the slower the machine, the bigger “physicsNSteps”.
The ideal case would be one refresh for each step of simulation
for which the graphics has changed.

Is that more or less correct ?

Thanks again, lads !

Julien

Le 15 mai 2011 ? 20:35, sdl-request at lists.libsdl.org a ?crit :

Send SDL mailing list submissions to
sdl at lists.libsdl.org

To subscribe or unsubscribe via the World Wide Web, visit
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
or, via email, send a message with subject or body ‘help’ to
sdl-request at lists.libsdl.org

You can reach the person managing the list at
sdl-owner at lists.libsdl.org

When replying, please edit your Subject line so it is more specific
than “Re: Contents of SDL digest…”

Today’s Topics:

  1. Constant framerate (Julien CLEMENT)
  2. Re: Constant framerate (Alex Barry)
  3. Re: Compiling sdl application with msvc (image28)
  4. Re: Compiling sdl application with msvc (image28)
  5. Re: Constant framerate (Armin Ronacher)
  6. Re: Constant framerate (John Magnotti)
  7. Re: Constant framerate (Nathaniel J Fries)

Message: 1
Date: Sat, 14 May 2011 21:49:25 +0200
From: Julien CLEMENT
To: SDL
Subject: [SDL] Constant framerate
Message-ID:
Content-Type: text/plain; charset=us-ascii

Hello,

Is there a way to achieve constant framerate (same on all machines),
except using SDL_Delay ?
I remember previous posts on that topic where it was said that below
10 ms the precision was poor.
To have a high FPS I would need to call SDL_Delay(2) or so, and I don’t
think it’s very accurate.

Thanks,

Julien CLEMENT
clementj2005 at yahoo.fr


Message: 2
Date: Sat, 14 May 2011 16:02:00 -0400
From: Alex Barry <@Alex_Barry>
To: SDL Development List
Subject: Re: [SDL] Constant framerate
Message-ID:
Content-Type: text/plain; charset=“iso-8859-1”

Hi Julien,
As I’ve mentioned in other threads, it’s not usually a good idea to have
a
consistent framerate - instead, you probably want to look into frame rate
independent movement. I think limiting the frame rate should be a
last-option, simply because if you set the frame rate too high for a
poor-performing machine, the software will run very poorly.
-Oz

On Sat, May 14, 2011 at 3:49 PM, Julien CLEMENT <clementj2005 at yahoo.fr wrote:

Hello,

Is there a way to achieve constant framerate (same on all machines),
except using SDL_Delay ?
I remember previous posts on that topic where it was said that below
10 ms the precision was poor.
To have a high FPS I would need to call SDL_Delay(2) or so, and I don’t
think it’s very accurate.

Thanks,

Julien CLEMENT
clementj2005 at yahoo.fr


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

-------------- next part --------------
An HTML attachment was scrubbed…
URL: <
http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110514/563f9fb4/attachment-0001.htm


Message: 3
Date: Sat, 14 May 2011 18:49:01 -0700
From: “image28”
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Compiling sdl application with msvc
Message-ID: <1305424141.m2f.28912 at forums.libsdl.org>
Content-Type: text/plain; charset=“iso-8859-1”

Using vc++ 2008, which doesn’t seem to have a subsystem option.
it’s outputing…

ignoring unknown option ‘/SUBSYSTEM:console’

-------------- next part --------------
An HTML attachment was scrubbed…
URL: <
http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110514/44504ce3/attachment-0001.htm


Message: 4
Date: Sat, 14 May 2011 19:02:56 -0700
From: “image28”
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Compiling sdl application with msvc
Message-ID: <1305424976.m2f.28913 at forums.libsdl.org>
Content-Type: text/plain; charset=“iso-8859-1”

was a linker option… Game is compiled an running with google-breakpad
crash reporting :slight_smile:

Thanks heaps.
Kevin

-------------- next part --------------
An HTML attachment was scrubbed…
URL: <
http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110514/a3df92e6/attachment-0001.htm


Message: 5
Date: Sun, 15 May 2011 15:24:54 +0200
From: Armin Ronacher <armin.ronacher at active-4.com>
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Constant framerate
Message-ID: <4DCFD426.5010301 at active-4.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi,

On 2011-05-14 9:49 PM, Julien CLEMENT wrote:

Is there a way to achieve constant framerate (same on all machines),
except using SDL_Delay ?
I remember previous posts on that topic where it was said that below
10 ms the precision was poor.
To have a high FPS I would need to call SDL_Delay(2) or so, and I don’t
think it’s very accurate.
What you want to be doing is adding the time from last frame to this
frame to an accumulator value and step forward in your simulation code
until no time is left on the accumulator. Then make sure that you have
at least 1ms sleep at the end of the loop so that you give your timer
some precision.

And then render as fast as possible and activate vsync, that makes most
sense. The downside is that because your simulation runs at a fixed
speed it does not help if your computer is rendering faster than the
simulation is. The fix for this problem is keeping the last physics
state and the current one and lerping between them.

Google for “fix your timestep”. There is an article that explains the
concept in much greater detail.

Regards,
Armin


Message: 6
Date: Sun, 15 May 2011 12:31:45 -0500
From: John Magnotti <john.magnotti at gmail.com>
To: SDL Development List
Subject: Re: [SDL] Constant framerate
Message-ID: <BANLkTimmXnRfF+umoX8s0BNh1tOCmAG5Ew at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

This may be an obviously bad idea, but what about using an SDL Timer?
I just set the timer call back to be 20ms, which is 50 fps (or 10ms
for 100fps). If you can be sure that the system can handle it (this is
where the real issue is, what to do with under-performers: drop
frames, frame-rate independent movement, etc.) then you are fine.
Maybe this doesn’t work for certain applications, but I’ve been doing
this since back in the glut/freeglut days before I started with SDL
and all my graphics classes taught us to do it this way.

Hope that helps,

John

On Sun, May 15, 2011 at 8:24 AM, Armin Ronacher <armin.ronacher at active-4.com> wrote:

Hi,

On 2011-05-14 9:49 PM, Julien CLEMENT wrote:

Is there a way to achieve constant framerate (same on all machines),
except using SDL_Delay ?
I remember previous posts on that topic where it was said that below
10 ms the precision was poor.
To have a high FPS I would need to call SDL_Delay(2) or so, and I don’t
think it’s very accurate.

What you want to be doing is adding the time from last frame to this
frame

to an accumulator value and step forward in your simulation code until
no

time is left on the accumulator. ?Then make sure that you have at least
1ms

sleep at the end of the loop so that you give your timer some precision.

And then render as fast as possible and activate vsync, that makes most
sense. ?The downside is that because your simulation runs at a fixed
speed

it does not help if your computer is rendering faster than the
simulation

is. ?The fix for this problem is keeping the last physics state and the
current one and lerping between them.

Google for “fix your timestep”. ?There is an article that explains the
concept in much greater detail.

Regards,
Armin


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


Message: 7
Date: Sun, 15 May 2011 11:33:31 -0700
From: “Nathaniel J Fries”
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Constant framerate
Message-ID: <1305484411.m2f.28916 at forums.libsdl.org>
Content-Type: text/plain; charset=“iso-8859-1”

framerate independent movement is definitely the way to do it.

Generally, you want your objects to move the same distance in the same
time on every machine; right?
Setting an FPS limit will only make this happen on systems that render
faster than the limit you set. Many systems, especially if you’re using the
SW renderer, will not do this.
Framerate independent movement works like this:

Code:

class MyAnimatonType
{
[…]
void step(Uint64 elapsedtime);
[…]
};

class LogicObject
{
[…]
void step(Uint64 elapsedtime);
[…]
};
[…]
std::vector<MyAnimationType*> my_animations;
std::vector<LogicObject*> my_logics;
[…]

Uint64 lasttime = SDL_GetTicks();
while(game_is_running)
{
Uint64 now = SDL_GetTicks();
Uint64 elapsed = now - lasttime;

for(std::vector<MyAnimationType*>::iterator it =
my_animations.begin(); it != my_animations.end(); ++it)
{
(it)->step(elapsed);
}
for(std::vector<LogicObject
>::iterator it = my_logics.begin(); it !=
my_logics.end(); ++it)
{
(*it)->step(elapsed);
}
lasttime = now;
}

I’m not sure if SDL_GetTicks() is thread-safe (and if not, change all
code relying on SDL_GetTicks to use a special version wrapped with a
spinlock or mutex) on all platforms, but this should work whether you run
your logic in the same thread as you draw or not (although you would
probably need to move animations to the drawing loop).


EM3 Nathaniel Fries, U.S. Navy

http://natefries.net/

-------------- next part --------------
An HTML attachment was scrubbed…
URL: <
http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110515/eb6c1beb/attachment.htm



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

End of SDL Digest, Vol 53, Issue 37


Julien CLEMENT
clementj2005 at yahoo.fr


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

since updatePhysics, etc takes greater than 0 time, you might want
something like this instead:

// Setup
timestep = 1/60
accumulator = 0.0
lastUpdate = SDL_GetTicks()

// Running code
thisUpdate = SDL_GetTicks()
secFromLast = thisUpdate - lastUpdate
accumulator += secFromLast
while accumulator >= 0 {
updatePhysics( timestep )
accumulator -= timestep
}
lastUpdate = thisUpdateOn 15 May 2011 16:33, Alex Barry <alex.barry at gmail.com> wrote:

it would be more like this:

// Setup
timestep = 1/60
accumulator = 0.0
lastUpdate = SDL_GetTicks()

// Running code
secFromLast = SDL_GetTicks() - lastUpdate
accumulator += secFromLast
while accumulator >= 0 {
? updatePhysics( timestep )
? accumulator -= timestep
}
lastUpdate = SDL_GetTicks()

Also, timestep should be 1000/60 and accumulator should be an integer.On 15 May 2011 16:39, Kenneth Bull <@Kenneth_Bull> wrote:

// Setup
timestep = 1/60
accumulator = 0.0
lastUpdate = SDL_GetTicks()