@@ -25,18 +25,95 @@ type Caster
25
25
ray_offset as Vector
26
26
end type
27
27
28
+ type FlatMap
29
+ private :
30
+ _w as integer
31
+ _h as integer
32
+ redim _walls( 0 , 0 ) as byte
33
+ redim _heights( 0 , 0 ) as short
34
+ redim _colors( 0 , 0 ) as integer
35
+ public :
36
+ declare constructor(w as integer , h as integer )
37
+ declare function walls(x as integer , y as integer ) as byte
38
+ declare function heights(x as integer , y as integer ) as short
39
+ declare function colors(x as integer , y as integer ) as integer
40
+ declare property w() as integer
41
+ declare property w(new_w as integer )
42
+ declare property h() as integer
43
+ declare property h(new_h as integer )
44
+ declare function setWall(x as integer , y as integer , new_w as integer ) as FlatMap ptr
45
+ declare function setHeight(x as integer , y as integer , new_h as integer ) as FlatMap ptr
46
+ declare function setColor(x as integer , y as integer , new_c as integer ) as FlatMap ptr
47
+ end type
48
+ constructor FlatMap(map_w as integer , map_h as integer )
49
+ this._w = map_w
50
+ this._h = map_h
51
+ redim this._walls(map_w, map_h)
52
+ redim this._heights(map_w, map_h)
53
+ redim this._colors(map_w, map_h)
54
+ end constructor
55
+ function FlatMap.walls(x as integer , y as integer ) as byte
56
+ if x >= 0 and x < this._w and y >= 0 and y < this._h then
57
+ return this._walls(x, this._h- 1 -y)
58
+ else
59
+ return 0
60
+ end if
61
+ end function
62
+ function FlatMap.heights(x as integer , y as integer ) as short
63
+ if x >= 0 and x < this._w and y >= 0 and y < this._h then
64
+ return this._heights(x, this._h- 1 -y)
65
+ else
66
+ return 0
67
+ end if
68
+ end function
69
+ function FlatMap.colors(x as integer , y as integer ) as integer
70
+ if x >= 0 and x < this._w and y >= 0 and y < this._h then
71
+ return this._colors(x, this._h- 1 -y)
72
+ else
73
+ return 0
74
+ end if
75
+ end function
76
+ property Flatmap.w() as integer
77
+ return this._w
78
+ end property
79
+ property Flatmap.w(new_w as integer )
80
+ this._w = new_w
81
+ end property
82
+ property Flatmap.h() as integer
83
+ return this._h
84
+ end property
85
+ property Flatmap.h(new_h as integer )
86
+ this._h = new_h
87
+ end property
88
+ function Flatmap.setWall(x as integer , y as integer , new_w as integer ) as FlatMap ptr
89
+ if x >= 0 and x < this._w and y >= 0 and y < this._h then
90
+ this._walls(x, this._h- 1 -y) = new_w
91
+ end if
92
+ return @this
93
+ end function
94
+ function Flatmap.setHeight(x as integer , y as integer , new_h as integer ) as FlatMap ptr
95
+ if x >= 0 and x < this._w and y >= 0 and y < this._h then
96
+ this._heights(x, this._h- 1 -y) = new_h
97
+ end if
98
+ return @this
99
+ end function
100
+ function Flatmap.setColor(x as integer , y as integer , new_c as integer ) as FlatMap ptr
101
+ if x >= 0 and x < this._w and y >= 0 and y < this._h then
102
+ this._colors(x, this._h- 1 -y) = new_c
103
+ end if
104
+ return @this
105
+ end function
106
+
28
107
declare sub drawLine(x0 as integer , y0 as integer , x1 as integer , y1 as integer , c as integer , a as integer = 0 )
29
108
declare function vectorFromAngle(a as double ) as Vector
30
109
declare function vectorToRight(u as Vector) as Vector
31
110
declare function vectorDot(u as Vector, v as Vector) as double
32
111
declare function VectorToUnit(u as Vector) as Vector
33
112
declare sub main()
34
- declare sub loadMap(map() as integer , hmap() as integer , cmap() as integer )
113
+ declare sub loadMap(map as FlatMap )
35
114
36
115
'// SHARED ============================================================
37
- dim shared map(MAP_WIDTH, MAP_HEIGHT) as integer
38
- dim shared hmap(MAP_WIDTH, MAP_HEIGHT) as integer
39
- dim shared cmap(MAP_WIDTH, MAP_HEIGHT) as integer
116
+ dim shared map as FlatMap = FlatMap(MAP_WIDTH, MAP_HEIGHT)
40
117
'//=====================================================================
41
118
42
119
'// INIT SDL SYSTEM AND GRAPHICS ======================================
@@ -55,7 +132,7 @@ SDL_RenderSetLogicalSize( gfxRenderer, SCREEN_X, SCREEN_Y )
55
132
SDL_SetRenderDrawBlendMode( gfxRenderer, SDL_BLENDMODE_BLEND )
56
133
'//=====================================================================
57
134
58
- loadMap map(), hmap(), cmap()
135
+ loadMap map
59
136
main
60
137
61
138
'// SHUTDOWN SDL ======================================================
@@ -157,20 +234,20 @@ sub main()
157
234
vf = vectorToRight(vf)
158
235
px -= vf.x * 0.1 * iif(keys[SDL_SCANCODE_LCTRL], 2 , 1 )
159
236
py -= vf.y * 0.1 * iif(keys[SDL_SCANCODE_LCTRL], 2 , 1 )
160
- nph = (hmap (int(px), MAP_HEIGHT- 1 - int(py))*0 . 01 )+ 1
161
- if nph-ph > 0.5 then
162
- px += vf.x * 0.1
163
- py += vf.y * 0.1
164
- elseif (dv = 0 ) and (nph-ph) < . 1 then
165
- ph = nph
166
- end if
237
+ ' nph = (map.heights (int(px), int(py))*0.01)+1
238
+ ' if nph-ph > 0.5 then
239
+ ' px += vf.x * 0.1
240
+ ' py += vf.y * 0.1
241
+ ' elseif (dv = 0) and (nph-ph) < .1 then
242
+ ' ph = nph
243
+ ' end if
167
244
end if
168
245
if keys[SDL_SCANCODE_D] then
169
246
vf = vectorFromAngle(pa)
170
247
vf = vectorToRight(vf)
171
248
px += vf.x * 0.5 * iif(keys[SDL_SCANCODE_LCTRL], 2 , 1 )
172
249
py += vf.y * 0.5 * iif(keys[SDL_SCANCODE_LCTRL], 2 , 1 )
173
- nph = (hmap (int(px), MAP_HEIGHT- 1 - int(py))*0 . 01 )+ 2
250
+ ' nph = (map.heights (int(px), int(py))*0.01)+2
174
251
'if nph-ph > 0.5 then
175
252
' px -= vf.x * 0.1
176
253
' py -= vf.y * 0.1
@@ -249,8 +326,8 @@ sub main()
249
326
ey = 0
250
327
dx += px
251
328
dy += py
252
- if int(dy)+ey >= 0 and int(dy)+ey < MAP_HEIGHT then
253
- if map(int(dx)+ex, MAP_HEIGHT- 1 -( int(dy)+ey) ) then
329
+ if int(dy)+ey >= 0 and int(dy)+ey < map.h then
330
+ if map.walls (int(dx)+ex, int(dy)+ey) then
254
331
xHit = 1
255
332
end if
256
333
else
@@ -268,8 +345,8 @@ sub main()
268
345
ey = iif(vray.y >= 0 , 0 , - 1 )
269
346
dy += py
270
347
dx += px
271
- if int(dx)+ex >= 0 and int(dx)+ex < MAP_WIDTH then
272
- if map(int(dx)+ex, MAP_HEIGHT- 1 -( int(dy)+ey) ) then
348
+ if int(dx)+ex >= 0 and int(dx)+ex < map.w then
349
+ if map.walls (int(dx)+ex, int(dy)+ey) then
273
350
yHit = 1
274
351
end if
275
352
else
@@ -284,7 +361,7 @@ sub main()
284
361
dy = iif(xDist > yDist, x_dy, y_dy)
285
362
ex = iif(xDist > yDist, iif(vray.x >= 0 , 0 , - 1 ), 0 )
286
363
ey = iif(yDist > xDist, iif(vray.y >= 0 , 0 , - 1 ), 0 )
287
- lx = int(dx)+ex: ly = MAP_HEIGHT- 1 -( int(dy)+ey)
364
+ lx = int(dx)+ex: ly = int(dy)+ey
288
365
lx = lx and 1023
289
366
ly = ly and 1023
290
367
@@ -293,8 +370,8 @@ sub main()
293
370
dc = 0
294
371
dim h as double
295
372
'h = hmap(int(px), MAP_HEIGHT-1-int(py))*0.01
296
- h = hmap (lx, ly)* 0.01
297
- colr = cmap (lx, ly)
373
+ h = map.heights (lx, ly)* 0.01
374
+ colr = map.colors (lx, ly)
298
375
dc = iif(xDist > yDist, 10 , - 10 )
299
376
top = midline+int(dist*(ph-h))+ 1
300
377
if top <= bottom then
@@ -319,8 +396,8 @@ sub main()
319
396
do until (xHit and (xDist > yDist)) or (yHit and (yDist > xDist))
320
397
if xDist > yDist then '// if xDist is closer
321
398
x_dx += x_ax: x_dy += x_ay
322
- if int(x_dy)+x_ey >= 0 and int(x_dy)+x_ey < MAP_HEIGHT then
323
- if map(int(x_dx)+x_ex, MAP_HEIGHT- 1 -( int(x_dy)+x_ey) ) then
399
+ if int(x_dy)+x_ey >= 0 and int(x_dy)+x_ey < map.h then
400
+ if map.walls (int(x_dx)+x_ex, int(x_dy)+x_ey) then
324
401
xHit = 1
325
402
end if
326
403
else
@@ -329,8 +406,8 @@ sub main()
329
406
xDist = abs(vray.x/(px-x_dx))
330
407
else
331
408
y_dy += y_ay: y_dx += y_ax
332
- if int(y_dx)+y_ex >= 0 and int(y_dx)+y_ex < MAP_WIDTH then
333
- if map(int(y_dx)+y_ex, MAP_HEIGHT- 1 -( int(y_dy)+y_ey) ) then
409
+ if int(y_dx)+y_ex >= 0 and int(y_dx)+y_ex < map.w then
410
+ if map.walls (int(y_dx)+y_ex, int(y_dy)+y_ey) then
334
411
yHit = 1
335
412
end if
336
413
else
@@ -341,15 +418,15 @@ sub main()
341
418
342
419
343
420
'dc = iif(xDist > yDist, xDist, yDist)*-84
344
- dc = (abs(lx-int(px))+abs((MAP_HEIGHT- 1 -ly) -int(py)))*0 . 01
421
+ dc = (abs(lx-int(px))+abs(ly -int(py)))* 0.01
345
422
dc = dc*dc
346
423
'dc += vectorDot(vray, north)*16
347
- colr = cmap (lx, ly)
424
+ colr = map.colors (lx, ly)
348
425
dx = iif(xDist > yDist, x_dx, y_dx)
349
426
dy = iif(xDist > yDist, x_dy, y_dy)
350
427
ex = iif(xDist > yDist, iif(vray.x >= 0 , 0 , - 1 ), 0 )
351
428
ey = iif(yDist > xDist, iif(vray.y >= 0 , 0 , - 1 ), 0 )
352
- lx = int(dx)+ex: ly = MAP_HEIGHT- 1 -( int(dy)+ey)
429
+ lx = int(dx)+ex: ly = int(dy)+ey
353
430
lx = lx and 1023
354
431
ly = ly and 1023
355
432
@@ -367,8 +444,8 @@ sub main()
367
444
drawLine f, top, f, bottom, colr, 0
368
445
bottom = top- 1
369
446
end if
370
- h = hmap (lx, ly)* 0.01
371
- colr = cmap (lx, ly)
447
+ h = map.heights (lx, ly)* 0.01
448
+ colr = map.colors (lx, ly)
372
449
top = midline+int(dist*(ph-h))+ 1
373
450
if top <= bottom then '- maybe draw this one first? (make sure no lines overlap)
374
451
dim ic as integer
@@ -471,27 +548,23 @@ function vectorDot(u as Vector, v as Vector) as double
471
548
return u.x*v.x+u.y*v.y
472
549
end function
473
550
474
- sub loadMap(map() as integer , hmap() as integer , cmap() as integer )
551
+ sub loadMap(map as FlatMap )
475
552
dim x as integer , y as integer
476
553
dim v as Vector
477
554
'dim r as Vector
478
555
dim r as integer
479
556
v = vectorFromAngle(rnd( 1 )* 360 )
480
- for y = 0 to MAP_HEIGHT- 1
481
- for x = 0 to MAP_WIDTH- 1
482
- if (x = 0 ) or (y = 0 ) or (x = (MAP_WIDTH- 1 )) or (y = (MAP_HEIGHT- 1 )) then
483
- map(x, y) = 1
484
- cmap(x, y) = &hffe4d8
557
+ for y = 0 to map.h- 1
558
+ for x = 0 to map.w- 1
559
+ if (x = 0 ) or (y = 0 ) or (x = (map.w- 1 )) or (y = (map.h- 1 )) then
560
+ map.setWall(x, y, 1 )
561
+ map.setHeight(x, y, 0 )
562
+ map.setColor(x, y, &hffe4d8 )
485
563
else
486
- hmap (x, y) = (abs(sin(x* 3 *TO_RAD)*cos(y* 3 *TO_RAD))+sin(x* 3 *TO_RAD))*- 3000
487
- 'hmap (x, y) += (abs(sin(x*TO_RAD)*cos(y*TO_RAD))+sin(x*3*TO_RAD))*-3000
564
+ map.setWall (x, y, 0 )
565
+ map.setHeight (x, y, (abs(sin(x* 3 * TO_RAD)*cos(y*3 * TO_RAD))+sin(x*3 *TO_RAD))*- 3000 )
488
566
r = int( 16 *rnd( 1 ))- 32
489
- cmap(x, y) = rgb( &h8d +r, &hb2 +r, &h7c +r)
490
- 'hmap(x, y) += abs(v.x*v.y)*-10
491
- 'r = vectorToRight(v)
492
- 'v.x += r.x*rnd(1): v.y += r.y*rnd(1)
493
- 'v = vectorToUnit(v)
494
- 'cmap(x, y) = rgb(&h8d, &hb2, &h7c)
567
+ map.setColor(x, y, rgb( &h8d +r, &hb2 +r, &h7c +r))
495
568
end if
496
569
next x
497
570
next y
0 commit comments