-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
210 lines (175 loc) · 5.66 KB
/
main.go
File metadata and controls
210 lines (175 loc) · 5.66 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// run in the source code directory with: go run *.go <filename>.mki3d
package main
import (
// "errors"
"fmt"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
"strconv"
// "github.com/mki1967/go-mki3d/mki3d"
// "github.com/go-gl/mathgl/mgl32"
// "github.com/mki1967/go-mki3d/glmki3d"
"log"
"math/rand"
"os"
// "path/filepath"
"runtime"
// "strings"
"flag"
"time"
)
func init() {
// GLFW event handling must run on the main OS thread
runtime.LockOSThread()
rand.Seed(time.Now().Unix()) // init random generator
}
var Window *glfw.Window // main window
func message(msg string) error {
fmt.Println(msg)
err := error(nil)
/* May cause problems if the process runs in the background ...
err = Window.Iconify()
if err != nil {
return err
}
fmt.Print("(PRESS ENTER TO RESUME:)")
fmt.Scanln()
err = Window.Restore()
if err != nil {
panic(err)
}
// err = Window.Maximize()
Window.Show()
fmt.Println("RESUMED.")
*/
return err
}
var doInMainThread func() = nil
var PathToAssets string = "assets"
func pathToAssets() string {
pathToAssets := "assets"
/*
gopath, isGopath := os.LookupEnv("GOPATH")
// pathToAssets := os.Getenv("GOPATH") + "/src/github.com/mki1967/mki3dgame/assets"
if isGopath {
pathToAssets = gopath + "/src/github.com/mki1967/mki3dgame/assets"
fmt.Println("If you have built from source code, then you should have some assests in: " + pathToAssets)
}
// check if it is installed in '/usr/...' directory -- requires Go version >= 1.8
execPath, err := os.Executable()
fmt.Printf("execPath = %v\n", execPath) // test
if err == nil && strings.Contains(execPath, "/usr/") {
execDir := filepath.Dir(execPath)
// relative '../share/games/mki3game/assets/' to
pathToAssets = filepath.Dir(execDir) + "/share/games/mki3game/assets/"
fmt.Println("If you have installed from distribution, then you should have some assests in: " + pathToAssets)
}
*/
envPath, isEnvPath := os.LookupEnv("MKI3DGAME_ASSETS")
if isEnvPath {
// fmt.Println("environment variable MKI3DGAME_ASSETS is set to " + envPath) // test
pathToAssets = envPath
}
// get path to assets from command line argument
// if len(os.Args) < 2 {
if len(flag.Args()) < 1 {
// panic(errors.New(" *** PROVIDE PATH TO ASSETS DIRECTORY AS A COMMAND LINE ARGUMENT !!! *** "))
// fmt.Println(" *** YOU CAN PROVIDE PATH TO YOUR ASSETS DIRECTORY AS A COMMAND LINE ARGUMENT !!! *** ")
fmt.Println(" Trying to use default path to assets: '" + pathToAssets + "'")
} else {
// fmt.Println("Path to assets from command line argument: " + os.Args[1])
// pathToAssets = os.Args[1]
pathToAssets = flag.Args()[0]
}
return pathToAssets
}
func main() {
windowWidthPtr := flag.Int("width", 800, "initial width of the window")
windowHeightPtr := flag.Int("height", 600, "initial height of the window")
flag.Parse()
// fmt.Println(flag.Args())
pathToAssets := pathToAssets()
PathToAssets = pathToAssets
// test for zenity
ZenityTest()
// fragments from https://github.com/go-gl/examples/blob/master/gl41core-cube/cube.go
if err := glfw.Init(); err != nil {
log.Fatalln("failed to initialize glfw:", err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.Resizable, glfw.True)
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
// glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
glfw.WindowHint(glfw.Samples, 4) // try multisampling for better quality ...
window, err := glfw.CreateWindow(*windowWidthPtr, *windowHeightPtr, "Mki3dgame", nil, nil)
if err != nil {
panic(err)
}
Window = window // copy to global variable
window.MakeContextCurrent()
// Initialize Glow
if err := gl.Init(); err != nil {
panic(err)
}
// version := gl.GoStr(gl.GetString(gl.VERSION))
// fmt.Println("OpenGL version", version)
// Configure global settings
gl.Enable(gl.MULTISAMPLE) // probably not needed ...
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LEQUAL)
gl.ClearColor(0.0, 0.0, 0.3, 1.0)
// init game structure from assets and the game window
game, err := MakeEmptyGame(pathToAssets, window)
if err != nil {
panic(err)
}
// load the first stage
err = game.Init()
if err != nil {
panic(err)
}
// message(helpText) // initial help message
firstLine := "MKI3D GAME with " + strconv.Itoa(len(game.AssetsPtr.Stages)) + " stages.\n"
fmt.Println(firstLine + helpText)
ZenityInfo(firstLine+"\nPRESS THE MOUSE ON SCREEN SECTORS OR USE THE KEYS.\n\nPRESS 'H' FOR HELP. ", "6")
// doInMainThread = ZenityHelp
game.Paused = true // start in the paused state
game.ProbeTime()
glfw.SwapInterval(1) // try to avoid tearing ???
// game.CheckGamepad() // test
// main loop
for !window.ShouldClose() && !game.Quit {
// Maintenance
window.SwapBuffers()
if doInMainThread != nil {
doInMainThread() // execute required function
doInMainThread = nil // done
}
// if( game.Paused ) { // old version
if game.Paused { // new version
game.CancelAction()
// glfw.WaitEvents()
time.Sleep(time.Second / 2)
game.ProbeTime() // refresh last time
} else {
game.ProbeTime()
game.Update()
// game.Paused = game.PauseRequest.TestAndCancel() // check for pause
// game.Paused.Set(game.PauseRequest.TestAndCancel()) // check for pause -- new version
}
glfw.PollEvents()
game.CheckGamepad() // test
game.Redraw()
}
fmt.Println("YOUR TOTAL SCORE IS: ", game.TotalScore)
ts := strconv.FormatFloat(game.TotalScore, 'f', 2, 64)
ZenityInfo("YOUR TOTAL SCORE IS: "+ts, "4")
// cleanup
game.StageDSPtr.DeleteData()
game.FrameDSPtr.DeleteData()
game.SectorsDSPtr.DeleteData()
game.TokenDSPtr.DeleteData()
game.MonsterDSPtr.DeleteData()
}