Module Timer
[hide private]
[frames] | no frames]

Source Code for Module Timer

 1  ##################################################################### 
 2  # -*- coding: iso-8859-1 -*-                                        # 
 3  #                                                                   # 
 4  # Frets on Fire                                                     # 
 5  # Copyright (C) 2006 Sami Kyöstilä                                  # 
 6  #                                                                   # 
 7  # This program is free software; you can redistribute it and/or     # 
 8  # modify it under the terms of the GNU General Public License       # 
 9  # as published by the Free Software Foundation; either version 2    # 
10  # of the License, or (at your option) any later version.            # 
11  #                                                                   # 
12  # This program is distributed in the hope that it will be useful,   # 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of    # 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     # 
15  # GNU General Public License for more details.                      # 
16  #                                                                   # 
17  # You should have received a copy of the GNU General Public License # 
18  # along with this program; if not, write to the Free Software       # 
19  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,        # 
20  # MA  02110-1301, USA.                                              # 
21  ##################################################################### 
22   
23  import pygame 
24  import time 
25   
26 -class Timer(object):
27 - def __init__(self, fps = 60, tickrate = 1.0):
28 self.fps = fps 29 self.timestep = 1000.0 / fps 30 self.tickrate = tickrate 31 self.ticks = self.getTime() 32 self.frame = 0 33 self.fpsEstimate = 0 34 self.fpsEstimateStartTick = self.ticks 35 self.fpsEstimateStartFrame = self.frame 36 self.highPriority = False
37
38 - def getTime(self):
39 return int(pygame.time.get_ticks() * self.tickrate)
40 41 time = property(getTime) 42
43 - def advanceFrame(self):
44 while True: 45 ticks = self.getTime() 46 diff = ticks - self.ticks 47 if diff >= self.timestep: 48 break 49 if not self.highPriority: 50 pygame.time.wait(0) 51 52 self.ticks = ticks 53 self.frame += 1 54 55 if ticks > self.fpsEstimateStartTick + 250: 56 n = self.frame - self.fpsEstimateStartFrame 57 self.fpsEstimate = 1000.0 * n / (ticks - self.fpsEstimateStartTick) 58 self.fpsEstimateStartTick = ticks 59 self.fpsEstimateStartFrame = self.frame 60 61 return [min(diff, self.timestep * 16)]
62