A faster starfield

BlitzMax Forums/BlitzMax Programming/A faster starfield

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

If anyone is interested the final code for the starfield is,

Type TStarfield
	Global list:TList = CreateList()
	
	Field sx#,sy#
	Field layersX:Float[][3]
	Field layersY:Float[][3]
	Field layersTime:Int[][3]
	Field nstars
	Field rate
	
	Method New()
		list.addlast(Self)
	End Method
	
	Function Create:TStarfield(density#)
		Local s:TStarfield = New TStarfield
		s.nstars = width*height*density
		s.rate = 700
		
		For Local i = 0 To 2
			s.layersX[i] = New Float[s.nstars]
			s.layersY[i] = New Float[s.nstars]
			s.layersTime[i] = New Int[s.nstars]
			For Local t = 0 To s.nstars-1
				s.layersX[i][t]= Rnd(width)'TStar.Create(Rnd(sx),Rnd(sy),255,255,255,3)
				s.layersY[i][t]= Rnd(height)
			Next
		Next
		Return s
	End Function 
	
	Function DrawAll()
		For Local i:TStarfield = EachIn(list)
			i.draw()
		Next
	End Function
	
	Method Randomize()
		For Local t = 0 To 2
			For Local i = 0 To nstars-1
				layersX[t][i]=Rnd(width)
				layersY[t][i]=Rnd(height)
			Next
		Next
	End Method
	
	Method Move(x#,y#)
		For Local t = 0 To 2
			For Local i = 0 To nstars-1
				layersX[t][i]:+x*(t+1)/4
				layersY[t][i]:+y*(t+1)/4
				If layersX[t][i]>width
					layersX[t][i] = 0
					layersY[t][i] = Rnd(height)
				ElseIf layersX[t][i]<0
					layersX[t][i] = width
					layersY[t][i] = Rnd(height)
				EndIf
				
				If layersY[t][i]>height
					layersY[t][i] = 0
					layersX[t][i] = Rnd(width)
				ElseIf layersY[t][i]<0
					layersY[t][i] = height
					layersX[t][i] = Rnd(width)
				EndIf
			Next
		Next
	End Method

	
	Method Draw()
		Local time = MilliSecs()
		For Local t = 0 To 2
			For Local i = 0 To nstars-1
				If layersX[t][i]>0 And layersX[t][i]<width And layersY[t][i]>0 And layersY[t][i]<height
					If time - layersTime[t][i]>rate And Rand(600) = 1 Then layersTime[t][i] = MilliSecs()
					If time-layersTime[t][i]<rate 
						SetColor 128,128,128
						DrawRect layersX[t][i]-(t+1)/2,layersY[t][i]-(t+1)/2,t+1,t+1
						SetColor 255,255,255
					Else
						DrawRect layersX[t][i]-(t+1)/2,layersY[t][i]-(t+1)/2,t+1,t+1
					EndIf
				EndIf
			Next
		Next
	End Method
End Type


Thanks once again everyone for your help.