-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
346 lines (285 loc) · 7.79 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"io"
"io/ioutil"
"log"
"math"
"os"
"path"
"strings"
"github.com/kyriacos/colorbuffer"
"github.com/veandco/go-sdl2/sdl"
)
// All the game globals
var (
Window *sdl.Window // The main window that we render to
Renderer *sdl.Renderer // The SDL renderer
CB *colorbuffer.ColorBuffer // The instance of ColorBuffer we use to update every tick
CBTexture *sdl.Texture
Textures map[string]*image.NRGBA // Stores all the texture images
G *Game // The game instance
showFPS = flag.Bool("showFPS", false, "Show current FPS and on exit display the average FPS.")
)
func castAllRays() {
// initial ray angle
angle := G.Player.rotationAngle - (FOV / 2)
for column := 0; column < NumRays; column++ {
ray := G.Rays[column]
ray.Cast(angle)
angle += FOV / NumRays
}
}
func run() (err error) {
if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil {
fmt.Fprintf(os.Stderr, "Error initializing SDL: %s\n", err)
return
}
Window, err = sdl.CreateWindow(
"RayCaster",
sdl.WINDOWPOS_CENTERED,
sdl.WINDOWPOS_CENTERED,
WindowWidth,
WindowHeight,
sdl.WINDOW_OPENGL)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating SDL window: %s\n", err)
return
}
Renderer, err = sdl.CreateRenderer(Window, -1, sdl.RENDERER_SOFTWARE) // -1 is the default driver (the graphics driver)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating SDL renderer: %s\n", err)
return
}
if err = Renderer.SetDrawBlendMode(sdl.BLENDMODE_BLEND); err != nil {
fmt.Fprintf(os.Stderr, "Failed to set blend mode: %s", err)
return
}
return nil
}
func destroy() {
// defer order?
defer sdl.Quit()
defer Window.Destroy()
defer Renderer.Destroy()
defer CBTexture.Destroy()
}
func loadTextures() {
imageDir := "./images/"
files, err := ioutil.ReadDir(imageDir)
if err != nil {
log.Fatal(err)
}
Textures = make(map[string]*image.NRGBA, len(files))
for _, file := range files {
filename := file.Name()
f, err := os.Open(imageDir + filename)
if err != nil {
log.Fatalf("Could not open file: %s", err)
}
defer f.Close()
imgNRGBA, err := decodeImage(f)
if err != nil {
log.Fatalf("Could not decode image from file: %s. Error: %s", err, imageDir+filename)
}
Textures[strings.TrimSuffix(filename, path.Ext(filename))] = imgNRGBA
}
}
func decodeImage(r io.Reader) (*image.NRGBA, error) {
img, err := png.Decode(r)
if err != nil {
return nil, err
}
var imgNRGBA *image.NRGBA
var ok bool
if imgNRGBA, ok = img.(*image.NRGBA); !ok {
switch img.ColorModel() {
case color.RGBAModel:
imgNRGBA = image.NewNRGBA(img.Bounds())
draw.Draw(imgNRGBA, img.Bounds(), img, image.Point{}, draw.Src)
case color.GrayModel, color.Gray16Model, color.AlphaModel, color.Alpha16Model:
fallthrough
default:
log.Fatal("Unsupported image format.")
}
}
return imgNRGBA, nil
}
func setup() {
// Load textures from images directory
loadTextures()
// initialize map
level := LoadLevel("./levels/level1.json")
G.GameMap = NewGameMap(level)
// initialize rays
G.Rays = NewRays()
// initialize the player
G.Player = &Player{
x: WindowWidth / 2,
y: WindowHeight / 2,
width: 1,
height: 1,
turnDirection: 0,
walkDirection: 0,
rotationAngle: 2 * PI / 2,
walkSpeed: 100,
turnSpeed: 70 * (PI / 180),
}
// initialize the color buffer
CB = colorbuffer.NewColorBuffer(WindowWidth, WindowHeight)
// create color buffer texture
var err error
CBTexture, err = Renderer.CreateTexture(
sdl.PIXELFORMAT_ABGR8888, // endianess https://forums.libsdl.org/viewtopic.php?p=39284
sdl.TEXTUREACCESS_STREAMING,
WindowWidth,
WindowHeight,
)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating the texture: %s", err)
panic(err)
}
}
func update(elapsedMS float64) {
G.Player.Update(elapsedMS * 1000.0)
castAllRays()
}
func renderColorBuffer() {
// update the sdl texture
CBTexture.Update(nil, CB.Pixels, CB.Stride)
// copy the texture to the renderer
Renderer.Copy(CBTexture, nil, nil) // nil and nil since we want to use the entire texture (src and dest used if you want to get a subset of the texture)
}
func project3d() {
for i := 0; i < NumRays; i++ {
ray := G.Rays[i]
// calculate perpendicular distance to remove the fisheye effect
perpendicularDistance := ray.distance * math.Cos(ray.angle-G.Player.rotationAngle)
distanceToProjPlane := (WindowWidth / 2) / math.Tan(FOV/2)
projectedWallHeight := (TileSize / perpendicularDistance) * distanceToProjPlane
wallStripHeight := int(projectedWallHeight)
// where the wall starts - starts right after our ceiling
wallTopPixel := (WindowHeight / 2) - (wallStripHeight / 2) // middle of the screen and half the wall height
if wallTopPixel < 0 {
wallTopPixel = 0
}
// ends where the floor starts rendering
wallBottomPixel := (WindowHeight / 2) + (wallStripHeight / 2)
if wallBottomPixel > WindowHeight {
wallBottomPixel = WindowHeight
}
// set color for the ceiling
for y := 0; y < wallTopPixel; y++ {
CB.Set(i, y, 0x333333FF)
}
// same for all the columns of X
var textureOffsetX int
if ray.wasHitVertical { // use Y to get the offset instead
textureOffsetX = int(ray.wallHitY) % TextureHeight
} else {
textureOffsetX = int(ray.wallHitX) % TextureWidth
}
// render the wall from top to bottom - cols
for y := wallTopPixel; y < wallBottomPixel; y++ {
distanceFromTop := y + (wallStripHeight / 2) - (WindowHeight / 2)
textureOffsetY := float64(distanceFromTop) * float64(TextureHeight) / float64(wallStripHeight)
texel := Textures["redbrick"].NRGBAAt(int(textureOffsetX), int(textureOffsetY))
var c uint32 = uint32(texel.R)<<24 | uint32(texel.G)<<16 | uint32(texel.B)<<8 | uint32(texel.A)
CB.Set(i, y, c)
}
// set color for the floor
for y := wallBottomPixel; y < WindowHeight; y++ {
CB.Set(i, y, 0x777777FF)
}
}
}
func render() {
Renderer.SetDrawColor(0, 0, 0, 255)
Renderer.Clear() // clear back buffer
project3d()
renderColorBuffer()
// render all game objects for current frame
G.GameMap.Render()
G.Player.Render()
for _, ray := range G.Rays {
ray.Render(Renderer, G.Player.x, G.Player.y)
}
// swap current buffer with back buffer
Renderer.Present()
}
func processInput() {
if event := sdl.PollEvent(); event != nil {
switch t := event.(type) {
case *sdl.QuitEvent: // sdl.QUIT
// println("Quit")
G.Running = false
case *sdl.KeyboardEvent:
key := t.Keysym.Sym
if t.Type == sdl.KEYDOWN {
switch key {
case sdl.K_ESCAPE:
G.Running = false
case sdl.K_UP:
G.Player.walkDirection = 1
case sdl.K_DOWN:
G.Player.walkDirection = -1
case sdl.K_RIGHT:
G.Player.turnDirection = 1
case sdl.K_LEFT:
G.Player.turnDirection = -1
}
}
if t.Type == sdl.KEYUP {
switch key {
case sdl.K_UP, sdl.K_DOWN:
G.Player.walkDirection = 0
case sdl.K_RIGHT, sdl.K_LEFT:
G.Player.turnDirection = 0
}
}
}
}
}
func main() {
flag.Parse()
G = &Game{
Running: false,
TicksLastFrame: 0,
}
if err := run(); err != nil {
destroy()
os.Exit(1)
}
var (
counter = 0
elapsedMS, sumFPS float64
)
setup()
G.Running = true
for G.Running {
start := sdl.GetPerformanceCounter()
processInput()
update(elapsedMS)
render()
end := sdl.GetPerformanceCounter()
elapsedMS = float64(end-start) / float64(sdl.GetPerformanceFrequency()*1000.0)
sdl.Delay(uint32(math.Floor(FrameTimeLength - elapsedMS))) // pause until we reach the target frames
if *showFPS {
elapsed := float64(end-start) / float64(sdl.GetPerformanceFrequency())
counter++
currentFPS := 1.0 / elapsed
sumFPS += currentFPS
fmt.Printf("FPS: %f\n", 1.0/elapsed)
}
}
destroy()
if *showFPS {
fmt.Printf("Average FPS: %f\n", sumFPS/float64(counter))
}
os.Exit(0)
}