1 | //============================================================================ |
---|---|
2 | // |
3 | // SSSS tt lll lll |
4 | // SS SS tt ll ll |
5 | // SS tttttt eeee ll ll aaaa |
6 | // SSSS tt ee ee ll ll aa |
7 | // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" |
8 | // SS SS tt ee ll ll aa aa |
9 | // SSSS ttt eeeee llll llll aaaaa |
10 | // |
11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony |
12 | // and the Stella Team |
13 | // |
14 | // See the file "License.txt" for information on usage and redistribution of |
15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. |
16 | //============================================================================ |
17 | |
18 | #include "FpsMeter.hxx" |
19 | |
20 | using namespace std::chrono; |
21 | |
22 | FpsMeter::FpsMeter(uInt32 queueSize) |
23 | : myQueue(queueSize) |
24 | { |
25 | reset(); |
26 | } |
27 | |
28 | void FpsMeter::reset(uInt32 garbageFrameLimit) |
29 | { |
30 | myQueue.clear(); |
31 | myQueueOffset = 0; |
32 | myFrameCount = 0; |
33 | myFps = 0; |
34 | myGarbageFrameCounter = 0; |
35 | myGarbageFrameLimit = garbageFrameLimit; |
36 | } |
37 | |
38 | void FpsMeter::render(uInt32 frameCount) |
39 | { |
40 | if (myGarbageFrameCounter < myGarbageFrameLimit) { |
41 | myGarbageFrameCounter += frameCount; |
42 | return; |
43 | } |
44 | |
45 | size_t queueSize = myQueue.capacity(); |
46 | entry first, last; |
47 | |
48 | last.frames = frameCount; |
49 | last.timestamp = high_resolution_clock::now(); |
50 | |
51 | if (myQueue.size() < queueSize) { |
52 | myQueue.push_back(last); |
53 | myFrameCount += frameCount; |
54 | |
55 | first = myQueue.at(myQueueOffset); |
56 | } else { |
57 | myFrameCount = myFrameCount - myQueue.at(myQueueOffset).frames + frameCount; |
58 | myQueue.at(myQueueOffset) = last; |
59 | |
60 | myQueueOffset = (myQueueOffset + 1) % queueSize; |
61 | first = myQueue.at(myQueueOffset); |
62 | } |
63 | |
64 | float myTimeInterval = |
65 | duration_cast<duration<float>>(last.timestamp - first.timestamp).count(); |
66 | |
67 | if (myTimeInterval > 0) myFps = (myFrameCount - first.frames) / myTimeInterval; |
68 | } |
69 | |
70 | float FpsMeter::fps() const |
71 | { |
72 | return myFps; |
73 | } |
74 |