-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (132 loc) · 3.03 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
/*
OpenGL app
*/
import "sdl"
import "gl"
import "game/sched"
import "fmt"
//import "os"
import "flag"
// need to cast pointer from C. This really belongs in sdl package
import "unsafe"
type cast unsafe.Pointer
type Cmd interface {
Name() string
SleepTime() int
OnSetup()
OnUpdate()
OnDraw()
}
// Graphics init flags for SDL
const SDL_FLAGS uint32 =
sdl.OPENGLBLIT | sdl.OPENGL | sdl.DOUBLEBUF |
sdl.HWPALETTE | sdl.HWSURFACE | sdl.HWACCEL | sdl.RESIZABLE
// List of possible commands to present to stdout
var cmds []Cmd = make([]Cmd, 0, 200)
// Scheduler to run functions at specified times
var sch *sched.Scheduler
var MouseX int
var MouseY int
var MouseState uint8
var WinW int = 1080 // 640
var WinH int = 1024 // 480
var KeySym uint32 = sdl.K_UNKNOWN
const NearHeight = gl.GLdouble(0.05)
// Show the commands available to stdout and prompt the
// user to select one
func selectCommand(def_cmd int) *Cmd {
fmt.Printf("Select a job to run:\n")
for i := 0; i < len(cmds); i++ {
fmt.Printf("%v : %v\n", i, cmds[i].Name())
}
var idx int
fmt.Printf("> ")
if def_cmd == -1 {
fmt.Scan(&idx)
} else {
idx = def_cmd
}
fmt.Printf("Using %v %v\n",idx, cmds[idx].Name())
if idx >= 0 && idx < len(cmds) {
return &cmds[idx]
}
return nil
}
// global ticks variable.
// Ticks can only be read in sdl.GetTicks() by main thread
// therefore we set this variable
var Ticks uint32
func main() {
sch = sched.NewScheduler(0)
flag.Parse()
cmd := selectCommand(CmdNum)
SetupVideo()
// Let the selected command initialize
cmd.OnSetup()
// Extract sleep time between frames for this command
sleeptime := cmd.SleepTime()
var frames uint32 = 0
// var t0 uint32 = sdl.GetTicks()
// gt := sdl.GetTicks
if ShowFPS {
sch.AddInterval(1.0, 2, func(tc sched.TaskChan,sc *sched.Scheduler) {
val := <-tc
if val != sched.RUNNING {
sc.C<-sched.TaskResult{val,tc}
return
}
sc.C<-sched.TaskResult{sched.COMPLETED,tc}
// seconds := (t - t0) / 1000.0
// fps := float(frames) / float(1.0)
// os.Stdout.WriteString("Fdafsda")
//sch.LOG <- "FPS"
// fmt.Println("FPS")
return
// t0 = t
frames = 0
});
}
var running = true
for running {
e := &sdl.Event{}
for e.Poll() {
switch e.Type {
case sdl.QUIT:
running = false
break
case sdl.KEYDOWN:
kb := e.Keyboard()
KeySym = kb.Keysym.Sym
if KeySym == sdl.K_ESCAPE {
running = false
}
break
case sdl.MOUSEBUTTONUP, sdl.MOUSEBUTTONDOWN:
me := e.MouseButton()
MouseState = me.State
break
case sdl.MOUSEMOTION:
me := e.MouseMotion()
MouseX = int(me.X)
MouseY = int(me.Y)
MouseState = me.State
break
case sdl.VIDEORESIZE:
me := (*sdl.ResizeEvent)(cast(e))
sdl.SetVideoMode(int(me.W), int(me.H), 32, SDL_FLAGS)
ResizeWindow(me.W, me.H)
}
}
// sch.Update()
cmd.OnUpdate()
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT )
cmd.OnDraw()
sdl.GL_SwapBuffers()
if sleeptime != 0 {
sdl.Delay(uint32(sleeptime))
}
frames++
}
sdl.Quit()
}