retrait code de test.
[minwii.git] / src / pgu / timer.py
1 """A timer for games with set-rate FPS.
2 """
3
4 import pygame
5
6 class Timer:
7 """A timer for games with set-rate FPS.
8
9 <pre>Timer(fps)</pre>
10 """
11
12 def __init__(self,fps):
13 if fps == 0:
14 self.tick = self._blank
15 return
16 self.wait = 1000/fps
17 self.nt = pygame.time.get_ticks()
18 pygame.time.wait(0)
19
20 def _blank(self):
21 pass
22
23 def tick(self):
24 """Wait correct amount of time each frame. Call this once per frame.
25
26 <pre>Timer.tick()</pre>
27 """
28 self.ct = pygame.time.get_ticks()
29 if self.ct < self.nt:
30 pygame.time.wait(self.nt-self.ct)
31 self.nt+=self.wait
32 else:
33 self.nt = pygame.time.get_ticks()+self.wait
34
35
36 class Speedometer:
37 """A timer replacement that returns out FPS once a second.
38 <pre>Speedometer()</pre>
39
40 <strong>Attributes</strong>
41 <dl>
42 <dt>fps <dd>always set to the current FPS
43 </dl>
44 """
45 def __init__(self):
46 self.frames = 0
47 self.st = pygame.time.get_ticks()
48 self.fps = 0
49
50 def tick(self):
51 """ Call this once per frame.
52
53 <pre>Speedometer.tick()</pre>
54 """
55 r = None
56 self.frames += 1
57 self.ct = pygame.time.get_ticks()
58 if (self.ct - self.st) >= 1000:
59 r = self.fps = self.frames
60 #print "%s: %d fps"%(self.__class__.__name__,self.fps)
61 self.frames = 0
62 self.st += 1000
63 pygame.time.wait(0) #NOTE: not sure why, but you gotta call this now and again
64 return r
65
66
67
68 # vim: set filetype=python sts=4 sw=4 noet si :