laser beam code

BlitzMax Forums/BlitzMax Beginners Area/laser beam code

hub(Posted 2010) [#1]
Hi !

i'm searching a fast method to draw a long beam. i've the following code. Perhaps have you some idea to improve/optimize it.
Thanks !

Strict

Graphics 800,600

HideMouse()

Global List_beams:TList = CreateList()

Global hImage:TImage = CreateImage (3,1)

Local pixmap:TPixmap=LockImage(hImage)
For Local i=0 To 2
	WritePixel(pixmap,i,0,$ffffffff)
Next
UnlockImage(hImage)

Type Tbeam

	Field ox#
	Field oy#
	Field dx#
	Field dy#
	Field Width
	Field Speed#
	
	Function Init:Tbeam (x,y, Width, Speed#)
	
		Local l:Tbeam = New Tbeam
		
		l.ox# = x
		l.oy# = y
		l.dx# = x
		l.dy# = y
		l.Width = Width	
		l.Speed = Speed
		
		ListAddLast List_beams, l
	
	End Function	
	
	Function Compute_all()
		For Local l:Tbeam = EachIn List_beams
			If Abs (l.oy-l.dy) <> l.Width Then
				l.dy = l.dy - 1 * l.Speed#
			Else
				If l.dy > 0 Then
					l.dy = l.dy - 1 * l.Speed#
				End If
				If l.oy > 0 Then 
					l.oy = l.oy - 1 * l.Speed#
				End If
			End If
			If l.oy# < 0 Then
				List_beams.remove l
			End If
		Next
	End Function
	
	Function Draw_all()
		For Local l:Tbeam = EachIn List_beams
			'SetColor 0,255,0
			'DrawLine l.ox-10, l.oy,l.dx#-10, l.dy#
			SetColor 255,0,0
			SetScale 1.0,l.dy#-l.oy#
			DrawImage himage, l.ox, l.oy
			SetScale 1,1
			SetColor 255,255,255

		Next
	End Function
	
End Type

While Not KeyDown(KEY_eSCAPE)
	Cls
		SetColor 255,255,255
		Plot MouseX(), MouseY()
		
		If MouseHit(1) Then Tbeam.Init (MouseX(), MouseY(), 200,10)
		
		Tbeam.Compute_all()
		Tbeam.Draw_all()
		
	Flip
Wend

ShowMouse()


Last edited 2010


Perturbatio(Posted 2010) [#2]
To demonstrate the speed up from my code, here's your one modified to have a simple FPS calc and no refresh limit (with some bug fixes):


And my code:


I get a difference of about 2000 FPS between yours and mine (and it looks a little nicer)

I added the ability to adjust the width of the graphic, added alpha to the graphic edges and created blurred duplicates to try to add a glow.

Also added laser repeat rate and speed

Last edited 2010

Last edited 2010


hub(Posted 2010) [#3]
Thanks Perturbatio !


Perturbatio(Posted 2010) [#4]
yar!