Skip to content

Commit 1c3b57a

Browse files
author
Joe King
committed
Separate classes/functions into modules
1 parent 6726026 commit 1c3b57a

File tree

16 files changed

+793
-559
lines changed

16 files changed

+793
-559
lines changed

build.bas

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include once "dir.bi"
2+
3+
if dir("lib", fbDirectory) <> "lib" then
4+
mkdir("lib")
5+
end if
6+
7+
dim pathToFbc as string = "C:\Program Files (x86)\FreeBASIC\fbc.exe"
8+
dim args(20) as string
9+
10+
args(0) = "modules/timer.bas -lib -x lib/libtimer.a"
11+
args(1) = "modules/gfont.bas -lib -x lib/libgfont.a"
12+
args(2) = "modules/vector.bas -lib -x lib/libvector.a"
13+
args(3) = "modules/rgb.bas -lib -x lib/librgb.a"
14+
args(4) = "modules/mesh.bas -lib -x lib/libmesh.a"
15+
args(5) = "modules/flatmap.bas -lib -x lib/libflatmap.a"
16+
args(6) = "modules/bsp.bas -lib -x lib/libbsp.a"
17+
18+
print "Building modules..."
19+
dim i as integer
20+
dim percent as double
21+
dim s as string
22+
for i = 0 to 6
23+
if exec(pathToFbc, args(i)) = -1 then
24+
print "ERROR while running fbc with: "+args(i)
25+
else
26+
percent = (i/6)
27+
s = "["
28+
s += string(int(percent*25), "=")
29+
s += string(25-int(percent*25), " ")
30+
s += "] "
31+
s += str(int(percent*100))+"%"
32+
print s+chr(13);
33+
end if
34+
next i
35+
36+
print ""
37+
print "Compiling program..."
38+
if exec(pathToFbc, "-w all rcast.bas -p lib/ -exx") = -1 then
39+
print "ERROR while running fbc with: -w all rcast.bas -p lib/"
40+
end if
41+
print "Done!"

modules/bsp.bas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include once "inc/bsp.bi"
2+

modules/flatmap.bas

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#include once "inc/flatmap.bi"
2+
3+
constructor FlatMap(map_w as integer, map_h as integer)
4+
this._w = map_w
5+
this._h = map_h
6+
dim i as integer
7+
for i = 0 to MAP_WIDTH*MAP_HEIGHT-1: this._callbacks(i) = 0: next i
8+
'redim this._walls(map_w, map_h)
9+
'redim this._heights(map_w, map_h)
10+
'redim this._colors(map_w, map_h)
11+
end constructor
12+
function FlatMap.walls(x as integer, y as integer) as byte
13+
if x >= 0 and x < this._w and y >= 0 and y < this._h then
14+
return this._walls(x+((this._h-1-y) shl 10))
15+
else
16+
return 0
17+
end if
18+
end function
19+
function FlatMap.heights(x as integer, y as integer) as short
20+
if x >= 0 and x < this._w and y >= 0 and y < this._h then
21+
return this._heights(x+((this._h-1-y) shl 10))
22+
else
23+
return 0
24+
end if
25+
end function
26+
function FlatMap.colors(x as integer, y as integer) as integer
27+
if x >= 0 and x < this._w and y >= 0 and y < this._h then
28+
return this._colors(x+((this._h-1-y) shl 10))
29+
else
30+
return 0
31+
end if
32+
end function
33+
function FlatMap.callbacks(x as integer, y as integer) as sub(byref x_dx as uinteger, byref x_dy as uinteger, byref y_dx as uinteger, byref y_dy as uinteger)
34+
if x >= 0 and x < this._w and y >= 0 and y < this._h then
35+
return this._callbacks(x+((this._h-1-y) shl 10))
36+
else
37+
return 0
38+
end if
39+
end function
40+
function FlatMap.datas(x as integer, y as integer, z as integer=0) as integer
41+
if x >= 0 and x < this._w and y >= 0 and y < this._h then
42+
return this._data(x+((this._h-1-y) shl 10)+(z shl 20))
43+
else
44+
return 0
45+
end if
46+
end function
47+
property Flatmap.w() as integer
48+
return this._w
49+
end property
50+
property Flatmap.w(new_w as integer)
51+
this._w = new_w
52+
end property
53+
property Flatmap.h() as integer
54+
return this._h
55+
end property
56+
property Flatmap.h(new_h as integer)
57+
this._h = new_h
58+
end property
59+
function Flatmap.setWall(x as integer, y as integer, new_w as integer) as FlatMap ptr
60+
if x >= 0 and x < this._w and y >= 0 and y < this._h then
61+
this._walls(x+((this._h-1-y) shl 10)) = new_w
62+
end if
63+
return @this
64+
end function
65+
function Flatmap.setHeight(x as integer, y as integer, new_h as integer) as FlatMap ptr
66+
if x >= 0 and x < this._w and y >= 0 and y < this._h then
67+
this._heights(x+((this._h-1-y) shl 10)) = new_h
68+
end if
69+
return @this
70+
end function
71+
function Flatmap.setColor(x as integer, y as integer, new_c as integer) as FlatMap ptr
72+
if x >= 0 and x < this._w and y >= 0 and y < this._h then
73+
this._colors(x+((this._h-1-y) shl 10)) = new_c
74+
end if
75+
return @this
76+
end function
77+
function Flatmap.setCallback(x as integer, y as integer, s as sub(byref x_dx as uinteger, byref x_dy as uinteger, byref y_dx as uinteger, byref y_dy as uinteger)) as FlatMap ptr
78+
if x >= 0 and x < this._w and y >= 0 and y < this._h then
79+
this._callbacks((this._h-1-y) shl 10) = s
80+
end if
81+
return @this
82+
end function
83+
function Flatmap.setData(x as integer, y as integer, z as integer, value as integer) as FlatMap ptr
84+
if x >= 0 and x < this._w and y >= 0 and y < this._h then
85+
this._data(x+((this._h-1-y) shl 10)+(z shl 20)) = value
86+
end if
87+
return @this
88+
end function
89+
function FlatMap.getWallAvg(x as integer, y as integer, size as integer) as integer
90+
dim mx as integer, my as integer
91+
dim sum as double
92+
dim max as double
93+
for my = y to y+size-1
94+
for mx = x to x+size-1
95+
sum += this.walls(mx, my)
96+
next mx
97+
next my
98+
size *= size
99+
return sum / size
100+
end function
101+
function FlatMap.getHeightAvg(x as integer, y as integer, size as integer) as integer
102+
dim mx as integer, my as integer
103+
'dim sum as double
104+
dim max as double
105+
max = -99999
106+
for my = y to y+size-1
107+
for mx = x to x+size-1
108+
'sum += this.heights(mx, my)
109+
if this.heights(mx, my) > max then
110+
max = this.heights(mx, my)
111+
end if
112+
next mx
113+
next my
114+
'size *= size
115+
'return sum / size
116+
return max
117+
end function
118+
function FlatMap.getColorAvg(x as integer, y as integer, size as integer) as integer
119+
dim mx as integer, my as integer
120+
dim colr as integer
121+
dim max as double
122+
max = -99999
123+
for my = y to y+size-1
124+
for mx = x to x+size-1
125+
if this.heights(mx, my) > max then
126+
max = this.heights(mx, my)
127+
colr = this.colors(mx, my)
128+
end if
129+
next mx
130+
next my
131+
return colr
132+
'dim r as integer, g as integer, b as integer
133+
'for my = y to y+size-1
134+
' for mx = x to x+size-1
135+
' r += ((this.colors(mx, my) shr 16) and &hff)
136+
' g += ((this.colors(mx, my) shr 8) and &hff)
137+
' b += (this.colors(mx, my) and &hff)
138+
' next mx
139+
'next my
140+
'size *= size
141+
'return rgb(r / size, g / size, b / size)
142+
end function
143+
function FlatMap.getCallbackAvg(x as integer, y as integer, size as integer) as sub(byref x_dx as uinteger, byref x_dy as uinteger, byref y_dx as uinteger, byref y_dy as uinteger)
144+
dim mx as integer, my as integer
145+
'dim sum as double
146+
dim s as sub(byref x_dx as uinteger, byref x_dy as uinteger, byref y_dx as uinteger, byref y_dy as uinteger)
147+
dim max as double
148+
max = -99999
149+
s = 0
150+
for my = y to y+size-1
151+
for mx = x to x+size-1
152+
'sum += this.heights(mx, my)
153+
if this.heights(mx, my) > max then
154+
max = this.heights(mx, my)
155+
s = this.callbacks(mx, my)
156+
end if
157+
next mx
158+
next my
159+
'size *= size
160+
'return sum / size
161+
return s
162+
end function
163+
function FlatMap.getDataAvg(x as integer, y as integer, z as integer, size as integer) as integer
164+
dim mx as integer, my as integer
165+
'dim sum as double
166+
dim dat as integer
167+
dim max as double
168+
max = -99999
169+
for my = y to y+size-1
170+
for mx = x to x+size-1
171+
'sum += this.heights(mx, my)
172+
if this.heights(mx, my) > max then
173+
max = this.heights(mx, my)
174+
dat = this.datas(mx, my, z)
175+
end if
176+
next mx
177+
next my
178+
'size *= size
179+
'return sum / size
180+
return dat
181+
end function

modules/gfont.bas

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include once "modules/inc/gfont.bi"
2+
3+
constructor GFont(renderer as SDL_Renderer ptr ptr)
4+
this._gfx_renderer = renderer
5+
end constructor
6+
7+
sub GFont.release()
8+
SDL_DestroyTexture( this._gfx_sprites )
9+
end sub
10+
11+
function GFont.load(filename as string, img_w as integer, img_h as integer, sp_w as integer, sp_h as integer, scale_x as double=1.0, scale_y as double=0) as GFont ptr
12+
13+
if scale_y = 0 then
14+
scale_y = scale_x
15+
end if
16+
17+
dim gfxSource as SDL_Surface ptr = SDL_LoadBMP(filename)
18+
19+
SDL_SetColorKey( gfxSource, SDL_TRUE, SDL_MapRGB(gfxSource->format, 255, 0, 255) )
20+
this._gfx_sprites = SDL_CreateTextureFromSurface( *this._gfx_renderer, gfxSource )
21+
22+
dim row_w as integer, row_h as integer
23+
row_w = int(img_w / sp_w)
24+
row_h = int(img_h / sp_h)
25+
26+
dim i as integer
27+
for i = 0 to row_w*row_h-1
28+
this._sprites(i).x = (i mod row_w)*sp_w
29+
this._sprites(i).y = int(i/row_w)*sp_h
30+
this._sprites(i).w = sp_w
31+
this._sprites(i).h = sp_h
32+
next i
33+
34+
this._sprites_w = sp_w
35+
this._sprites_h = sp_h
36+
37+
return @this
38+
39+
end function
40+
41+
function GFont.writeText(text as string, x as integer, y as integer) as GFont ptr
42+
43+
dim n as integer
44+
dim v as integer
45+
46+
dim dstRect as SDL_Rect
47+
dstRect.x = x: dstRect.y = y
48+
dstRect.w = this._sprites_w: dstRect.h = this._sprites_h
49+
50+
for n = 1 to len(text)
51+
v = asc(mid$(text, n, 1))-32+this._sprite_offset
52+
SDL_RenderCopy( *this._gfx_renderer, this._gfx_sprites, @this._sprites(v), @dstRect)
53+
dstRect.x += this._sprites_w
54+
next n
55+
56+
return @this
57+
58+
end function
59+
60+
function GFont.centerText(text as string, y as integer) as GFont ptr
61+
62+
if this._screen_w = 0 then
63+
SDL_RenderGetLogicalSize(*this._gfx_renderer, @this._screen_w, null)
64+
end if
65+
66+
return this.writeText(text, int((this._screen_w-len(text)*this._sprites_w)/2), y)
67+
68+
end function
69+
70+
function GFont.setOffset(offset as integer) as GFont ptr
71+
72+
this._sprite_offset = offset
73+
74+
return @this
75+
76+
end function

modules/inc/bsp.bi

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
#inclib "bsp"
3+
4+
#include once "modules/inc/vector.bi"
5+
6+
type BspNode
7+
private:
8+
_normal as Vector
9+
_behind as BspNode ptr
10+
_front as BspNode ptr
11+
_data as any ptr
12+
public:
13+
declare constructor()
14+
declare function getNormal() as Vector ptr
15+
declare function getBehind() as BspNode ptr
16+
declare function getFront() as BspNode ptr
17+
declare function getData() as any ptr
18+
declare function setData(p as any ptr) as BspNode ptr
19+
end type
20+
21+
constructor BspNode
22+
this._behind = 0
23+
this._front = 0
24+
this._data = 0
25+
end constructor
26+
function BspNode.getNormal() as Vector ptr
27+
return @this._normal
28+
end function
29+
function BspNode.getBehind() as BspNode ptr
30+
return this._behind
31+
end function
32+
function BspNode.getFront() as BspNode ptr
33+
return this._front
34+
end function
35+
function BspNode.getData() as any ptr
36+
return this._data
37+
end function
38+
function BspNode.setData(p as any ptr) as BspNode ptr
39+
this._data = p
40+
return @this
41+
end function
42+
43+
type BspTree
44+
private:
45+
_node_start as BspNode
46+
_nodes(4096) as BspNode
47+
public:
48+
declare function addBehind(normal as Vector) as BspTree ptr
49+
declare function addFront(normal as Vector) as BspTree ptr
50+
end type

modules/inc/flatmap.bi

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
#inclib "flatmap"
3+
4+
#define MAP_WIDTH 1024
5+
#define MAP_HEIGHT 1024
6+
7+
type FlatMap
8+
private:
9+
_w as integer
10+
_h as integer
11+
dim _walls(MAP_WIDTH*MAP_HEIGHT) as byte
12+
dim _heights(MAP_WIDTH*MAP_HEIGHT) as short
13+
dim _colors(MAP_WIDTH*MAP_HEIGHT) as integer
14+
dim _callbacks(MAP_WIDTH*MAP_HEIGHT) as sub(byref x_dx as uinteger, byref x_dy as uinteger, byref y_dx as uinteger, byref y_dy as uinteger)
15+
dim _data(MAP_WIDTH*MAP_HEIGHT*2) as integer
16+
public:
17+
declare constructor(w as integer, h as integer)
18+
declare function walls(x as integer, y as integer) as byte
19+
declare function heights(x as integer, y as integer) as short
20+
declare function colors(x as integer, y as integer) as integer
21+
declare function callbacks(x as integer, y as integer) as sub(byref x_dx as uinteger, byref x_dy as uinteger, byref y_dx as uinteger, byref y_dy as uinteger)
22+
declare function datas(x as integer, y as integer, z as integer=0) as integer
23+
declare property w() as integer
24+
declare property w(new_w as integer)
25+
declare property h() as integer
26+
declare property h(new_h as integer)
27+
declare function setWall(x as integer, y as integer, new_w as integer) as FlatMap ptr
28+
declare function setHeight(x as integer, y as integer, new_h as integer) as FlatMap ptr
29+
declare function setColor(x as integer, y as integer, new_c as integer) as FlatMap ptr
30+
declare function setCallback(x as integer, y as integer, s as sub(byref x_dx as uinteger, byref x_dy as uinteger, byref y_dx as uinteger, byref y_dy as uinteger)) as FlatMap ptr
31+
declare function setData(x as integer, y as integer, z as integer, value as integer) as FlatMap ptr
32+
declare function getWallAvg(x as integer, y as integer, size as integer) as integer
33+
declare function getHeightAvg(x as integer, y as integer, size as integer) as integer
34+
declare function getColorAvg(x as integer, y as integer, size as integer) as integer
35+
declare function getCallbackAvg(x as integer, y as integer, size as integer) as sub(byref x_dx as uinteger, byref x_dy as uinteger, byref y_dx as uinteger, byref y_dy as uinteger)
36+
declare function getDataAvg(x as integer, y as integer, z as integer, size as integer) as integer
37+
end type

0 commit comments

Comments
 (0)