Time based loop limiting

M

Michael Tomer

I'm trying to write a loop that will only run a certain number of times
per second. For instance, in Ruby-Processing, you would use
"frame_rate(30)" to make the main loop execute no more than 30 evenly
spaced frames per second. How might I go about doing something like
that?

I could try calling sleep with a float that represents the difference
between the next time I want the loop to run and the current time. That
seems clunky, though.

I also thought about using the Observer pattern to have a timer alert
the simulation each time it should update. That also seems wrong.

I feel like I must be overlooking something simple. Any ideas?
 
P

Paul Smith

I think your first method is the one I've seen the most in game loops.
You calculate how long you should sleep based on what time it is now
and what time you want to wake up, being careful that your last frame
could have taken over 1/30th of a second.

You also use the elapsed time in your physics, so the physics engine
is not tied to your frame rate.
 
M

Michael Tomer

Thanks, Paul. I feel better knowing that my first guess was right, even
though it initially struck me as wrong. I guess that'll teach me to
second guess myself.
 
B

Bertram Scharpf

Hi,

Am Mittwoch, 09. Sep 2009, 01:38:33 +0900 schrieb Michael Tomer:
I'm trying to write a loop that will only run a certain number of times
per second.

The proper way to do this is to call the setitimer(2) function
that will send SIGALRM periodically to the process. It has been
discussed a long time ago:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/130989

Still I cannot find a setitimer mapping in the Ruby interpreter.
So I wrote my own:

http://bertram-scharpf.homelinux.com:8808/doc_root/itimer-1.0/rdoc/index.html

Bertram
 
B

brabuhr

Am Mittwoch, 09. Sep 2009, 01:38:33 +0900 schrieb Michael Tomer:

The proper way to do this is to call the setitimer(2) function
that will send SIGALRM periodically to the process. It has been
discussed a long time ago:

=A0http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/130989

Still I cannot find a setitimer mapping in the Ruby interpreter.
So I wrote my own:

=A0http://bertram-scharpf.homelinux.com:8808/doc_root/itimer-1.0/rdoc/ind=
ex.html

Took a shot with FFI:

require 'ffi'

module LibC
extend FFI::Library

class Timeval < FFI::Struct
layout :tv_sec, :long,
:tv_usec, :long
end

class Itimerval < FFI::Struct
layout :it_interval, Timeval,
:it_value, Timeval
end

attach_function :getitimer, [:int, :pointer], :int
attach_function :setitimer, [:int, :pointer, :pointer], :int
end


Signal.trap("SIGALRM") do
puts "SIGALRM: #{Time.now}"
end

tv =3D LibC::Timeval.new
tv[:tv_sec] =3D 2
tv[:tv_usec] =3D 0

puts Time.now
LibC::setitimer(0, tv, nil)
sleep 15
puts Time.now


#=3D>
# Wed Sep 09 02:57:18 +0000 2009
# SIGALRM: Wed Sep 09 02:57:20 +0000 2009
# SIGALRM: Wed Sep 09 02:57:22 +0000 2009
# SIGALRM: Wed Sep 09 02:57:24 +0000 2009
# SIGALRM: Wed Sep 09 02:57:26 +0000 2009
# SIGALRM: Wed Sep 09 02:57:28 +0000 2009
# SIGALRM: Wed Sep 09 02:57:30 +0000 2009
# SIGALRM: Wed Sep 09 02:57:32 +0000 2009
# SIGALRM: Wed Sep 09 02:57:33 +0000 2009
# Wed Sep 09 02:57:33 +0000 2009
 
M

Mike Barsalou

Hi,

Am Mittwoch, 09. Sep 2009, 01:38:33 +0900 schrieb Michael Tomer:


The proper way to do this is to call the setitimer(2) function
that will send SIGALRM periodically to the process. It has been
discussed a long time ago:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/130989

Still I cannot find a setitimer mapping in the Ruby interpreter.
So I wrote my own:

http://bertram-scharpf.homelinux.com:8808/doc_root/itimer-1.0/rdoc/in...

Bertram


How would one make use of this?
 
B

Bertram Scharpf

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top