-
Notifications
You must be signed in to change notification settings - Fork 2
/
Scene.go
executable file
·121 lines (93 loc) · 2.98 KB
/
Scene.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Scene.go : contain game components within nodes, and stucture them inside of scenes for rendering etc
package Graphics
import (
"GT/Graphics/Components"
"GT/Graphics/Opengl"
"GT/Input"
"GT/Window"
"fmt"
"time"
)
// BaseScene : object to contain nodes for rendering. TODO: implement more scene types from this base type
type BaseScene struct {
RootNode *Components.Node
window *Window.Window
// TODO: can put fps into struct fps counter
targetFps int
fps int
timestart int32
update bool
}
// NewBasicScene : start a new simple 2D scene of orthographic orientation
func NewBasicScene() (BaseScene, error) {
s := BaseScene{RootNode: Components.NewNode("RootNode"), window: &Window.MainWindow}
s.fps = 0
s.timestart = int32(time.Now().Unix())
s.update = true
Opengl.SetViewPort(int32(s.window.Width), int32(s.window.Height))
Opengl.SetOrthographic(s.window.Width, s.window.Height)
return s, nil
}
// New3DScene : start a new simple 3D scene of orthographic orientation
func New3DScene() (BaseScene, error) {
s := BaseScene{RootNode: Components.NewNode("RootNode"), window: &Window.MainWindow}
s.fps = 0
s.timestart = int32(time.Now().Unix())
s.update = true
Opengl.SetViewPort(int32(s.window.Width), int32(s.window.Height))
Opengl.SetPerspective(s.window.Width, s.window.Height)
return s, nil
}
// Start : initiate a scene's continous animation. While the scene is running, draw it and update our nodes
func (s *BaseScene) Start() {
running := true
s.update = true
drawStart := int32(time.Now().UnixNano())
drawEnd := int32(time.Now().UnixNano())
delta := 0
onesec := int32(time.Second.Nanoseconds())
desiredFps := int32(s.targetFps)
// continue to render the scene unless signalled for termination
for running {
drawStart = int32(time.Now().UnixNano())
if GlobalInput.CheckForUpdates() {
running = !(GlobalInput.GetInputStatus("Esc") || GlobalInput.GetInputStatus("Quit"))
}
s.window.Clear()
s.RootNode.Update(float32(delta))
s.Draw()
s.window.Refresh()
drawEnd = int32(time.Now().UnixNano())
delta := drawEnd - drawStart
// fps limitation is optional, sleep for at least 1 ns to smooth rendering
if desiredFps > 0 {
// (1 second in ns / n frames - time elapsed in ns) = leftover sleep in ns needed to reach target fps
if delta < onesec/desiredFps {
time.Sleep(time.Duration(onesec/desiredFps - delta))
}
} else {
time.Sleep(time.Nanosecond)
}
}
}
// Draw : wrap the openGl draw method and report fps info
func (s *BaseScene) Draw() {
if s.update {
// TODO: remove hard coded resolution
// gl.BindTexture(gl.TEXTURE_2D, s.spriteSheet.textureId)
Opengl.BindBuffers()
s.update = false
}
Opengl.Draw()
// calc fps
if (int32(time.Now().Unix()) - s.timestart) >= 1 {
fmt.Printf("FPS: %d \n", s.fps)
s.fps = 0
s.timestart = int32(time.Now().Unix())
}
s.fps = s.fps + 1
}
// SetFPS forces the loop to delay such that the fps is the number inputted
func (s *BaseScene) SetFPS(fpsInputted int) {
s.targetFps = fpsInputted
}