-
Notifications
You must be signed in to change notification settings - Fork 27
/
clock.go
52 lines (43 loc) · 884 Bytes
/
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
// Copyright 2014-2016 Joseph Hager. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package engi
import (
"math"
"time"
)
type Clock struct {
elapsed float64
delta float64
fps float64
frames uint64
start time.Time
frame time.Time
}
func NewClock() *Clock {
clock := new(Clock)
clock.start = time.Now()
clock.Tick()
return clock
}
func (c *Clock) Tick() {
now := time.Now()
c.frames += 1
c.delta = now.Sub(c.frame).Seconds()
c.elapsed += c.delta
c.frame = now
if c.elapsed >= 1 {
c.fps = float64(c.frames)
c.elapsed = math.Mod(c.elapsed, 1)
c.frames = 0
}
}
func (c *Clock) Delta() float32 {
return float32(c.delta)
}
func (c *Clock) Fps() float32 {
return float32(c.fps)
}
func (c *Clock) Time() float32 {
return float32(time.Now().Sub(c.start).Seconds())
}