File: FpsClock.py

package info (click to toggle)
solarwolf 1.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,348 kB
  • ctags: 371
  • sloc: python: 2,640; makefile: 56; sh: 31
file content (55 lines) | stat: -rw-r--r-- 1,766 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""FpsClock - a clock to manage a game's FPS stuff"""

import pygame.time


class FpsClock:
    "class for managing FPS related stuff"
    def __init__(self, desired_fps=30, do_report=0):
        "create FpsClock instance, give desired running fps and enable report"
        self.do_report = do_report
        self.frame_count = 0
        self.frame_timer = pygame.time.get_ticks()
        self.frame_delay = 0
        self.last_tick = pygame.time.get_ticks()
        self.set_fps(desired_fps)
        self.current_fps = 0.0


    def set_fps(self, desired_fps):
        "set the desired frames per second"
        if desired_fps:
            self.fps_ticks = int((0.975/desired_fps) * 1000)
            #slight fudge, not quite 1000millis
        else:
            self.fps_ticks = 0
        self.desired_fps = desired_fps


    def tick(self):
        "call this once per frame"
        #delay until milliseconds per frame has passed
        if self.fps_ticks:
            now = pygame.time.get_ticks()
            wait = self.fps_ticks - (now - self.last_tick)
            pygame.time.delay(wait)
            self.frame_delay += wait
        self.last_tick = pygame.time.get_ticks()

        #update current_fps
        self.frame_count += 1
        time = self.last_tick - self.frame_timer
        if time > 1000:
            time -= self.frame_delay
            self.current_fps = self.frame_count / ((time / 1000.0) or 1)
            self.frame_count = 0
            self.frame_delay = 0
            self.frame_timer = self.last_tick
            if self.do_report: self.report()


    def report(self):
        "override this for fancier fps reporting"
        subst = 1.0/self.current_fps, self.current_fps
        print 'AVG TIME: %.3f   FPS: %.2f' % subst