-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
268 lines (220 loc) · 6.45 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/go-gl/mathgl/mgl32"
"github.com/tinogoehlert/goom/drivers"
"github.com/tinogoehlert/goom/drivers/opengl"
drvShared "github.com/tinogoehlert/goom/drivers/pkg"
"github.com/tinogoehlert/goom/goom"
"github.com/tinogoehlert/goom/graphics"
"github.com/tinogoehlert/goom/level"
"github.com/tinogoehlert/goom/run"
"github.com/tinogoehlert/goom/utils"
)
var (
logger = utils.GoomConsole
midiOptions = strings.Join([]string{
string(drivers.SdlMusic),
string(drivers.Noop),
}, "|")
// flags
iwadfile = flag.String("iwad", "DOOM1", "IWAD file to load (without extension)")
pwadfile = flag.String("pwad", "", "PWAD file to load (without extension)")
levelName = flag.String("level", "E1M1", "Level to start e.g. E1M1")
fpsMax = flag.Int("fpsmax", 0, "Limit FPS")
winDrv = flag.String("windowdrv", "sdl", "Window and Input driver name")
freeLook = flag.Bool("freelook", false, "Allow to look up and down")
windowHeight = 600
windowWidth = 800
gameDefs = "resources/defs.yaml"
verticalMouse = false
)
func main() {
flag.Parse()
mainDrivers := drivers.Drivers{
Window: drivers.WindowDrivers[drivers.WindowDriver(strings.ToLower(*winDrv))],
Audio: drivers.AudioDrivers[drivers.SdlAudio],
Music: drivers.MusicDrivers[drivers.SdlMusic],
Input: drivers.InputDrivers[drivers.InputDriver(strings.ToLower(*winDrv))],
GetTime: drivers.TimerFuncs[drivers.SdlTimer],
}
logger.Green("GOOM - DOOM clone written in Go")
logger.Green("Press Q to exit GOOM.")
logger.Green("MUSIC: %T", mainDrivers.Music)
e := newEngine(&mainDrivers)
inputFunc := func() {
input(e)
}
e.Window().RunGame(inputFunc, e.World().Update, e.render)
}
type engine struct {
*run.Runner
stats *renderStats
}
func newEngine(drivers *drivers.Drivers) *engine {
var err error
e := &engine{
&run.Runner{Drivers: drivers},
&renderStats{lastUpdate: time.Now()},
}
// init all subsystems
e.InitWAD(*iwadfile, *pwadfile, gameDefs)
e.InitAudio()
err = e.InitRenderer(windowWidth, windowHeight)
if err != nil {
logger.Red("failed to init renderer %s", err.Error())
}
// load mission
mission := e.GameData().Level(strings.ToUpper(*levelName))
e.Renderer().LoadLevel(mission, e.GameData())
e.World().LoadLevel(mission)
player := e.World().Me()
ssect, err := mission.FindPositionInBsp(level.GLNodesName, player.Position()[0], player.Position()[1])
if err != nil {
logger.Print("could not find GLnode for pos %v", player.Position())
} else {
sector := mission.SectorFromSSect(ssect)
player.SetSector(sector)
}
return e
}
func (e *engine) render(interpolTime float64) {
started := e.GetTime()
e.Renderer().RenderNewFrame()
e.Renderer().SetViewPort(e.Window().GetSize())
mission := e.World().GetLevel()
mission.WalkBsp(func(i int, n *level.Node, b level.BBox) {
e.Renderer().DrawSubSector(i)
})
player := e.World().Me()
e.Renderer().DrawThings(e.World().Things())
e.Renderer().DrawHUD(player, interpolTime)
ssect, err := mission.FindPositionInBsp(level.GLNodesName, player.Position()[0], player.Position()[1])
if err != nil {
logger.Print("could not find GLnode for pos %v", player.Position())
} else {
var sector = mission.SectorFromSSect(ssect)
player.SetSector(sector)
player.Lift(sector.FloorHeight())
}
e.Renderer().Camera().SetCamera(player.Position(), player.Direction(), player.Height())
e.Renderer().SetPlayerPosition(mgl32.Vec3{
-player.Position()[0],
player.Height(),
player.Position()[1],
})
e.stats.showStats(e.GameData(), e.Renderer())
e.stats.countedFrames++
ft := e.GetTime() - started
e.stats.accumulatedTime += time.Duration(ft * float64(time.Second))
if *fpsMax > 0 {
time.Sleep(time.Second / time.Duration(*fpsMax))
}
}
// DrawText draws a string on the screen
func drawText(fonts graphics.FontBook, fontName graphics.FontName, text string, xpos, ypos, scaleFactor float32, gr *opengl.GLRenderer) {
font := fonts[fontName]
spacing := float32(font.GetSpacing()) * scaleFactor
// currently, we only know uppercase glyphs
text = strings.ToUpper(text)
for _, r := range text {
if r == ' ' {
xpos -= spacing
continue
}
glyph := font.GetGlyph(r)
if glyph == nil {
xpos -= spacing
continue
}
gr.DrawHUdElement(glyph.GetName(), xpos, ypos, scaleFactor)
xpos -= spacing + float32(glyph.Width())*scaleFactor
}
}
type renderStats struct {
countedFrames int
accumulatedTime time.Duration
fps int
meanFrameTime float32
lastUpdate time.Time
}
func (rs *renderStats) showStats(gd *goom.GameData, gr *opengl.GLRenderer) {
t1 := time.Now()
if t1.Sub(rs.lastUpdate) >= time.Second {
rs.fps = rs.countedFrames
rs.meanFrameTime = (float32(rs.accumulatedTime) / float32(rs.countedFrames)) / float32(time.Millisecond)
rs.countedFrames = 0
rs.accumulatedTime = time.Duration(0)
rs.lastUpdate = t1
}
fpsText := fmt.Sprintf("FPS: %d", rs.fps)
// TODO: position relatively to the window size
drawText(gd.Fonts, graphics.FnCompositeRed, fpsText, 800, 600, 0.6, gr)
ftimeText := fmt.Sprintf("frame time: %.6f ms", rs.meanFrameTime)
// TODO: position relatively to the window size
drawText(gd.Fonts, graphics.FnCompositeRed, ftimeText, 800, 580, 0.6, gr)
}
func input(e *engine) {
in := e.Input
player := e.World().Me()
if in.IsPressed(drvShared.KeyW) {
player.Forward(1)
}
if in.IsPressed(drvShared.KeyS) {
player.Forward(-1)
}
if in.IsPressed(drvShared.KeyA) {
player.Strafe(-1)
}
if in.IsPressed(drvShared.KeyD) {
player.Strafe(1)
}
if in.IsPressed(drvShared.KeyLeft) {
player.Turn(-1.5)
}
if in.IsPressed(drvShared.KeyRight) {
player.Turn(1.5)
}
if *freeLook {
if in.IsPressed(drvShared.KeyUp) {
player.Pitch(1.5)
}
if in.IsPressed(drvShared.KeyDown) {
player.Pitch(-1.5)
}
}
if in.IsPressed(drvShared.KeyLShift) || in.IsMousePressed(drvShared.MouseLeft) {
player.FireWeapon()
}
if in.IsPressed(drvShared.KeyQ) {
in.SetMouseCameraEnabled(false)
os.Exit(0)
}
if in.IsPressed(drvShared.KeyM) {
in.SetMouseCameraEnabled(true)
}
if in.IsPressed(drvShared.KeyK) {
in.SetMouseCameraEnabled(false)
}
if in.IsPressed(drvShared.KeyF7) {
verticalMouse = true
}
if *freeLook {
if in.IsPressed(drvShared.KeyF8) {
verticalMouse = false
player.ResetPitch()
}
}
if xDelta, yDelta := in.GetCursorDelta(); xDelta != 0 || yDelta != 0 {
player.Turn(float32(xDelta / 10))
if *freeLook {
if verticalMouse {
player.Pitch(float32(-yDelta / 10))
}
}
}
}