FpsCounter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %
Metric Value
dl 0
loc 34
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_fps() 0 11 2
A tick() 0 12 1
A __init__() 0 7 1
1
import platform
2
import time
3
import sys
4
5
pyVer = sys.version_info
6
7
# Choose best time function for what we are running on
8
if pyVer.major == 3 and pyVer.minor >= 3:
9
    time_func = time.monotonic
10
elif platform.system() == 'Windows':
11
    time_func = time.clock
12
else:
13
    time_func = time.time
14
15
class Timer(object):
16
    ''' General purpose timing class. '''
17
    def __init__(self):
18
        self.currentTime = self.previousTime = self.time()
19
20
        self.tickDelta = 0
21
22
    def time(self):
23
        ''' Get current time in miliseconds '''
24
        return int(time_func() * 1000)
25
26
    def tick(self):
27
        ''' Get time delta since last call of tick '''
28
        self.previousTime = self.currentTime
29
30
        self.currentTime = self.time()
31
32
        self.tickDelta = self.currentTime - self.previousTime
33
34
        return self.tickDelta
35
36
class FpsCounter(Timer):
37
    ''' Helper class for Frames per Second calculation '''
38
    def __init__(self):
39
40
        super(FpsCounter, self).__init__()
41
42
        self.frames = 0
43
        self.fpsTime = 0
44
        self.fps = 0
45
46
    def tick(self):
47
        '''
48
        Calculate Time delta, and keep running total of frames since last
49
        fps calculation.
50
        Must be called once per rendered frame.
51
        '''
52
        super(FpsCounter, self).tick()
53
54
        self.fpsTime += self.tickDelta
55
        self.frames += 1
56
57
        return self.tickDelta
58
59
    def get_fps(self):
60
        ''' Calculate and return the estimated Frames Per Second '''
61
        if self.fpsTime == 0:
62
            self.fpsTime += 1
63
64
        self.fps = self.frames / (self.fpsTime / 1000.0)
65
66
        self.fpsTime = 0
67
        self.frames = 0
68
69
        return self.fps
70