/
clock.go
61 lines (52 loc) · 1.09 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
// Copyright 2014 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
elapsedStep float64
step float64
numStep uint32
delta float64
fps float64
frames uint64
start time.Time
frame time.Time
}
func NewClock(step float64) *Clock {
clock := new(Clock)
clock.start = time.Now()
clock.step = step
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
c.numStep = 0.0
}
if (c.elapsed / c.step) >= float64(c.numStep) {
c.numStep++
responder.Step(c.step, c.numStep)
}
}
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())
}