Skip to content

Commit c8507f6

Browse files
author
Joe King
committed
Create FlatMap type for map values
1 parent e869428 commit c8507f6

File tree

1 file changed

+116
-43
lines changed

1 file changed

+116
-43
lines changed

rcast.bas

Lines changed: 116 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,95 @@ type Caster
2525
ray_offset as Vector
2626
end type
2727

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+
28107
declare sub drawLine(x0 as integer, y0 as integer, x1 as integer, y1 as integer, c as integer, a as integer = 0)
29108
declare function vectorFromAngle(a as double) as Vector
30109
declare function vectorToRight(u as Vector) as Vector
31110
declare function vectorDot(u as Vector, v as Vector) as double
32111
declare function VectorToUnit(u as Vector) as Vector
33112
declare sub main()
34-
declare sub loadMap(map() as integer, hmap() as integer, cmap() as integer)
113+
declare sub loadMap(map as FlatMap)
35114

36115
'// 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)
40117
'//=====================================================================
41118

42119
'// INIT SDL SYSTEM AND GRAPHICS ======================================
@@ -55,7 +132,7 @@ SDL_RenderSetLogicalSize( gfxRenderer, SCREEN_X, SCREEN_Y )
55132
SDL_SetRenderDrawBlendMode( gfxRenderer, SDL_BLENDMODE_BLEND )
56133
'//=====================================================================
57134

58-
loadMap map(), hmap(), cmap()
135+
loadMap map
59136
main
60137

61138
'// SHUTDOWN SDL ======================================================
@@ -157,20 +234,20 @@ sub main()
157234
vf = vectorToRight(vf)
158235
px -= vf.x * 0.1 * iif(keys[SDL_SCANCODE_LCTRL], 2, 1)
159236
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
167244
end if
168245
if keys[SDL_SCANCODE_D] then
169246
vf = vectorFromAngle(pa)
170247
vf = vectorToRight(vf)
171248
px += vf.x * 0.5 * iif(keys[SDL_SCANCODE_LCTRL], 2, 1)
172249
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
174251
'if nph-ph > 0.5 then
175252
' px -= vf.x * 0.1
176253
' py -= vf.y * 0.1
@@ -249,8 +326,8 @@ sub main()
249326
ey = 0
250327
dx += px
251328
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
254331
xHit = 1
255332
end if
256333
else
@@ -268,8 +345,8 @@ sub main()
268345
ey = iif(vray.y >= 0, 0, -1)
269346
dy += py
270347
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
273350
yHit = 1
274351
end if
275352
else
@@ -284,7 +361,7 @@ sub main()
284361
dy = iif(xDist > yDist, x_dy, y_dy)
285362
ex = iif(xDist > yDist, iif(vray.x >= 0, 0, -1), 0)
286363
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
288365
lx = lx and 1023
289366
ly = ly and 1023
290367

@@ -293,8 +370,8 @@ sub main()
293370
dc = 0
294371
dim h as double
295372
'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)
298375
dc = iif(xDist > yDist, 10, -10)
299376
top = midline+int(dist*(ph-h))+1
300377
if top <= bottom then
@@ -319,8 +396,8 @@ sub main()
319396
do until (xHit and (xDist > yDist)) or (yHit and (yDist > xDist))
320397
if xDist > yDist then '// if xDist is closer
321398
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
324401
xHit = 1
325402
end if
326403
else
@@ -329,8 +406,8 @@ sub main()
329406
xDist = abs(vray.x/(px-x_dx))
330407
else
331408
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
334411
yHit = 1
335412
end if
336413
else
@@ -341,15 +418,15 @@ sub main()
341418

342419

343420
'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
345422
dc = dc*dc
346423
'dc += vectorDot(vray, north)*16
347-
colr = cmap(lx, ly)
424+
colr = map.colors(lx, ly)
348425
dx = iif(xDist > yDist, x_dx, y_dx)
349426
dy = iif(xDist > yDist, x_dy, y_dy)
350427
ex = iif(xDist > yDist, iif(vray.x >= 0, 0, -1), 0)
351428
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
353430
lx = lx and 1023
354431
ly = ly and 1023
355432

@@ -367,8 +444,8 @@ sub main()
367444
drawLine f, top, f, bottom, colr, 0
368445
bottom = top-1
369446
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)
372449
top = midline+int(dist*(ph-h))+1
373450
if top <= bottom then '- maybe draw this one first? (make sure no lines overlap)
374451
dim ic as integer
@@ -471,27 +548,23 @@ function vectorDot(u as Vector, v as Vector) as double
471548
return u.x*v.x+u.y*v.y
472549
end function
473550

474-
sub loadMap(map() as integer, hmap() as integer, cmap() as integer)
551+
sub loadMap(map as FlatMap)
475552
dim x as integer, y as integer
476553
dim v as Vector
477554
'dim r as Vector
478555
dim r as integer
479556
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)
485563
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)
488566
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))
495568
end if
496569
next x
497570
next y

0 commit comments

Comments
 (0)