-
Notifications
You must be signed in to change notification settings - Fork 136
/
clock.go
94 lines (74 loc) · 2.08 KB
/
clock.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package engo
import (
"time"
)
// timer is a wrapper for time.Now().UnixNano() so we can test
// this wihout relying on time
type timer interface {
Now() int64
}
// realTime is the actual timer that uses time.Now().UnixNano()
type realTime struct{}
// Now implements the timer interface
func (realTime) Now() int64 {
return time.Now().UnixNano()
}
var theTimer timer = realTime{}
// The amound of nano seconds in a second.
const secondsInNano int64 = 1000000000
// A Clock is a measurement built in `engo` to measure the actual frames per seconds (framerate).
type Clock struct {
counter uint32
perSecond uint32
deltaStamp int64
elapsStamp int64
frameStamp int64
startStamp int64
paused bool
}
// NewClock creates a new timer which allows you to measure ticks per seconds. Be sure to call `Tick()` whenever you
// want a tick to occur - it does not automatically tick each frame.
func NewClock() *Clock {
currStamp := theTimer.Now()
clock := new(Clock)
clock.frameStamp = currStamp
clock.startStamp = currStamp
return clock
}
// Tick indicates a new tick/frame has occurred.
func (c *Clock) Tick() {
currStamp := theTimer.Now()
c.counter++
c.deltaStamp = currStamp - c.frameStamp
c.frameStamp = currStamp
c.elapsStamp += c.deltaStamp
if secondsInNano <= c.elapsStamp {
c.elapsStamp %= secondsInNano
c.perSecond = c.counter
c.counter = 0
}
}
// Delta is the amount of seconds between the last tick and the one before that
func (c *Clock) Delta() float32 {
if c.paused {
return 0
}
return float32(float64(c.deltaStamp) / float64(secondsInNano))
}
// Pause pauses the clock
func (c *Clock) Pause() {
c.paused = true
}
// Unpause unpauses the clock
func (c *Clock) Unpause() {
c.paused = false
}
// FPS is the amount of frames per second, computed every time a tick occurs at least a second after the previous update
func (c *Clock) FPS() float32 {
return float32(c.perSecond)
}
// Time is the number of seconds the clock has been running
func (c *Clock) Time() float32 {
currStamp := theTimer.Now()
return float32(float64(currStamp-c.startStamp) / float64(secondsInNano))
}