kmonsoor / pyglet

Automatically exported from code.google.com/p/pyglet
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Interval scheduling with fps limit. #691

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Consider the following test:

import unittest
from pyglet import clock

class ClockIntervalSchedulingTestCase(unittest.TestCase):
    """
    Test case for clock scheduling of time-interval callbacks with limited frame rate.
    """
    def setUp(self):
        clock.set_default(clock.Clock())

        # calls counter
        self.counter = 0

        # expected delta time between calls for each callback
        self.rate = 30
        self.expected_dt = 1./self.rate

        clock.schedule_interval(self.callback, self.expected_dt)

    def callback(self, dt):
        self.counter += 1

    def test_schedule_interval_with_fps_limit(self):
        clock.set_fps_limit(60)
        for x in range(60):
            clock.tick()

        self.assertEqual(self.counter, self.rate)

if __name__ == '__main__':
    unittest.main()

Because the clock is running at 60 frames per second, for 60 ticks, the cicle 
runs for 1 second (this is tested in pyglet).

Because the call rate is 30 per second, it is expected that there are 30 calls 
in 1 second (+/-1%). However, when I run this test, the number of calls is 
20-21 (i.e. the test fails with 21 != 30).

I notice that if we use

        clock.schedule(self.callback)

there are 60 calls, as expected (one for each tick).

Original issue reported on code.google.com by jorgecar...@gmail.com on 11 Dec 2013 at 3:59

GoogleCodeExporter commented 9 years ago
I tried using this code http://stackoverflow.com/a/13151104/931303 for 
scheduling, and it passes the test (29 != 30, which is ok given that 
set_fps_limit has always some error).

Original comment by jorgecar...@gmail.com on 12 Dec 2013 at 10:32