2.5d vector map viewer

Miscellaneous Forums/Blitz Showcase/2.5d vector map viewer

I was sitting around last night on my computer and wrote a 2.5D vector map viewer... it has a 2d edit mode and a 2.5d view mode. This is just an early step towards a larger goal... Next I have to work on applying textures, creating floors (I'm thinking polygons), then height differances.
Well, here's what I wrote last night:
;An early attempt at a 2.5D vector map viewer.
;coded by Kevin Laherty (kalisme)
;written 17/3/08
;		-2d and 2.5d modes.
;		-currently unable to do textures.
;		-no collision detection yet.
;		-still buggy, but nicer than the one I 
;		 wrote yesterday! :)

Graphics 320,240,32
SetBuffer BackBuffer()
Dim zbuff(320)
Const fps = 30
Type vect
	Field x#,y#,id
End Type

Type wall
	Field id1,id2,norm#,fl_p
End Type

Function add_vector(x#,y#,id#)
	a.vect = New vect
	a\x# = x#
	a\y# = y#
	a\id = id
End Function

Function make_wall(id1,id2,norm#,fl_p)
	a.wall = New wall
	a\id1 = id1
	a\id2 = id2
	a\norm# = norm#
End Function

Function draw_map(cx#,cy#,ca#)
	For a.wall = Each wall
		For b.vect = Each vect
			If a\id1 = b\id Then x1# = b\x#: y1# = b\y#
			If a\id2 = b\id Then x2# = b\x#: y2# = b\y#
		Next
		Color 150,50,150
		Line x1,y1,x2,y2
	Next
	Color 0,200,0
	Oval cx#-5,cy#-5,10,10
	Line cx#,cy#,cx# + Cos(ca#)*10, cy# + Sin(ca#)*10
End Function

Function draw_vectors()
	For a.vect = Each vect
		Color 0,50,150
		Line a\x-2,a\y-2,a\x+2,a\y+2
		Line a\x-2,a\y+2,a\x+2,a\y-2
	Next
End Function

Function render_map_test(cx#,cy#,ca#,zoom#)
	ca#=ca#+90
	For a.wall = Each wall
		For b.vect = Each vect
			If a\id1 = b\id Then x1# = b\x#: y1# = b\y#
			If a\id2 = b\id Then x2# = b\x#: y2# = b\y#
		Next
		x1# = x1#-cx#
		x2# = x2#-cx#
		y1# = y1#-cy#
		y2# = y2#-cy#
		
		tx1# = (Cos(ca#) * x1#) + (Sin(ca#) * y1#)
		ty1# = (-Sin(ca#) * x1#) + (Cos(ca#) * y1#)
		
		tx2# = (Cos(ca#) * x2#) + (Sin(ca#) * y2#)
		ty2# = (-Sin(ca#) * x2#) + (Cos(ca#) * y2#)
		
		
		x1#=tx1#
		y1#=ty1#
		x2#=tx2#
		y2#=ty2#
		
		cull = 0
		If y1# > 0 And y2# > 0 Then cull = 1
		
		If cull = 0
			tx1# = x1#
			tx2# = x2#
			ty1# = y1#
			ty2# = y2#
			
			If ty1# > -1
				grad# = Float(x2#-x1#) / Float (y2#-y1#)
				tx1# = x1# + grad#*(1-y1)
				ty1# = -1
			EndIf
			dx1# = tx1# * zoom# * (1/Abs(ty1#)) + 160
			sc1#=Abs(600/(0-ty1#))
			If ty2# > -1
				grad# = Float(x2#-x1#) / Float (y2#-y1#)
				tx2# = x1# + grad#*(1-y1)
				ty2# = -1
			EndIf
			dx2# = tx2# * zoom# * (1/Abs(ty2#)) + 160
			sc2#=Abs(600/(0-ty2#))
			Line tx1#+160,ty1#+140,tx2#+160,ty2#+140
			
			Color 255,255,255
			Line dx1#,120-sc1#,dx1#,120+sc1#
			Line dx2#,120-sc2#,dx2#,120+sc2#
			Line dx1#,120-sc1#,dx2#,120-sc2#
			Line dx1#,120+sc1#,dx2#,120+sc2#
			
		EndIf
		
		
	Next
End Function
Function render_map(cx#,cy#,ca#,zoom#)
	ca#=ca#+90
	For clear = 0 To 320
		zbuff(clear)=0
	Next
	
	For a.wall = Each wall
		For b.vect = Each vect
			If a\id1 = b\id Then x1# = b\x#: y1# = b\y#
			If a\id2 = b\id Then x2# = b\x#: y2# = b\y#
		Next
		x1# = x1#-cx#
		x2# = x2#-cx#
		y1# = y1#-cy#
		y2# = y2#-cy#
		
		tx1# = (Cos(ca#) * x1#) + (Sin(ca#) * y1#)
		ty1# = (-Sin(ca#) * x1#) + (Cos(ca#) * y1#)
		
		tx2# = (Cos(ca#) * x2#) + (Sin(ca#) * y2#)
		ty2# = (-Sin(ca#) * x2#) + (Cos(ca#) * y2#)
				
		
		x1#=tx1#
		y1#=ty1#
		x2#=tx2#
		y2#=ty2#
		
		cull = 0
		If y1# > 0 And y2# > 0 Then cull = 1
		
		If cull = 0
			tx1# = x1#
			tx2# = x2#
			ty1# = y1#
			ty2# = y2#
			
			If ty1# > -1
				grad# = Float(x2#-x1#) / Float (y2#-y1#)
				tx1# = x1# + grad#*(1-y1)
				ty1# = -1
			EndIf
			dx1# = tx1# * zoom# * (1/Abs(ty1#)) + 160
			sc1#=Abs(600/(0-ty1#))
			If ty2# > -1
				grad# = Float(x2#-x1#) / Float (y2#-y1#)
				tx2# = x1# + grad#*(1-y1)
				ty2# = -1
			EndIf
			dx2# = tx2# * zoom# * (1/Abs(ty2#)) + 160
			sc2#=Abs(600/(0-ty2#))
			hx#=dx1#
			hy#=sc1#
			lx#=dx2#
			ly#=sc2#
			
			If hx#>lx#
				tempx#=hx#
				tempy#=hy#
				hx=lx#
				hy=ly#
				lx#=tempx#
				ly#=tempy#
			EndIf
			grad# = Float(ly#-hy#)/Float(lx#-hx#)
			For d = hx# To lx#
				oy# = (hy#+grad# * Float(d-hx# ))
				If d > 0 Then If d < 320 
					If zbuff(d) < oy# 
						zbuff(d)=oy#
						For e = 120-oy# To 120+oy#
							If e > 0 Then If e < 240
								tb= 360/(((120+oy#)-(120-oy#))/((120+oy#)-(120-oy#)-(e-(120-oy#))))
								coy#=oy#+(Sin(tb*5)*12)+20
								If coy# > 255 Then coy#=255
								If coy# < 20 Then coy# = 20
								WritePixelFast d, e, (coy# Shl 16) Or (coy# Shl 8) Or coy#, BackBuffer()
							EndIf
						Next
					EndIf
				EndIf
			Next
		EndIf
		
		
	Next
End Function
add_vector(40,40,1)
add_vector(210,50,2)
make_wall(1,2,1,0)


cam_X#=160
cam_y#=140
cam_ang#=30
nid=2

period = 1000/fps
time = MilliSecs()-period

While KeyHit(1) = 0
	Cls
	
	Repeat
		elapsed=MilliSecs()-time
	Until elapsed
	ticks=elapsed/period
	tween#=Float(elapsed Mod period)/Float(period)
	For k=1 To ticks
		time=time+period
		If KeyDown(200) Then cam_x# = cam_x# + Cos(cam_ang#)*2:cam_y# = cam_y# + Sin(cam_ang#)*2
		If KeyDown(208) Then cam_x# = cam_x# + Cos(cam_ang#)*-2:cam_y# = cam_y# + Sin(cam_ang#)*-2
		If KeyDown(203) Then cam_ang# = cam_ang#-5
		If KeyDown(205) Then cam_ang# = cam_ang#+5
		
	Next
	
	If KeyDown(57)
		Color 0,170,180
		Rect 1,1,319,120
		Color 50,80,50
		Rect 1,120,319,120
		
		LockBuffer BackBuffer()
		render_map(cam_x#,cam_y#,cam_ang#,90)
		UnlockBuffer BackBuffer()
		Color 255,100,120
		Text 10,10,"2.5D map viewer"
	Else
		draw_map(cam_x#,cam_y#,cam_ang#)
		draw_vectors()
		Color 255,255,255
		Text 10,10,"2D map editor"
		Text 10,190,"LMB = draw wall,
		Text 10,200,"RMB = start with New vector"
		Text 10,210,"1 = scribble walls, hold SPACE for"
		Text 10,220,"2.5D view"
	Plot MouseX(),MouseY()
	If KeyDown(2) Then nid=nid+1:add_vector(MouseX(),MouseY(),nid):make_wall(nid,nid-1,0,0)
	If MouseHit(1) Then nid=nid+1:add_vector(MouseX(),MouseY(),nid):make_wall(nid,nid-1,0,0)
	If MouseHit(2) Then nid=nid+1:add_vector(MouseX(),MouseY(),nid)
		
	EndIf
	
	Flip
	
Wend


controls:
LMB=draw wall (connects to last vector)
RMB=start new vector (not connecting to last vector)
"1"= wall scribble
hold "SPACE" to enter 2.5D mode.
(arrow keys control movement... think "doom")

There's no collision code yet... I think I'll probably use box - > line collision detection.
Oh, and I know it might seem that there'd be a massive slow down with the extra features I'm planning, but I'm just using the blitz2d code as a test and to figure out the maths... I'm hoping to port this over to SDL using DevC++ later on.

Any advice on adding textures or any hints on anything else would be nice.

interesting.

Please include and executable for those without blitz3D.

and screenshots!

Took the liberty to help. Also had to change the resolution for program because it wouldn't run on my computer.
Looks pretty good, keep up the good work.

EXE Download:
EXE No longer available.

2D Map


3D Map


Xzider: Thankyou for posting an executable and screenshots :)

Isn't this code compatable with Blitz plus? erg... I thought it would be... I actually had a free version of blitz2d (don't worry... got it from a magazine) before I purchased Blitz3d... I thought it had all the commands except the "graphics3d" commands....
...I wonder what the limitations of that ol' blitz2d were actually... "hmmm" & such. Maybe you could register it so you could actually get support or something... oh well.. never mind.

I've managed to figure out how to display textures on the walls... bit dodgey right now, but I did it at 3AM after two hours of sleep. I'll post it when it's stable (-ish).
Oh, and I'll post a screenshot or two and an executable... (well... probably a zip with the *.exe and a texture file).

(Oh, and I should add a vetex snapping option....)

hey kalisme, do you still have your deKAL system online? i'D like to have a copy.. but the link on your page isn't working...

No problem Kalisme, if you need me to update it at all let me know, or if you have somewhere to do it, let me know so I can delete off the forum:D Have fun coding!

This is really very cool nice job.

t3k|Mac: Unfortunatley deKAL fell through... :(
I sent you an Email explaning why... I should go write that on the worklog... I forgot.
Oh, and checked out your web site... Wow, classy :)
Great stuff indeedy.

Xzider: Thanks tons, I think I'm a bit busy with parties and stuff the next too days, but as soon as I get a chance I'm running back to the PC and locking myself away to code for a while.... YAY! =D

Neat, I wanted to make an 3d level editor like this inspired by the build engine. That was a super easy engine to make levels for.