Major slowdown creating more than 600 particles.

BlitzMax Forums/BlitzMax Beginners Area/Major slowdown creating more than 600 particles.

Hi Guys. Thanks for the replies. I've changed the code so that it recycles each particle and resets its position to -1 when it goes beyond the boundaries of the screen. I've noticed a slight increase in speed but still lagging with just 401 particles. I have taken out the create_star() function and moved it outside the loop so it can only ever create 401 particles. I still get lag with this.

Here is the current code for the stars and the code for the whole game:

Global invaderlist:TList = CreateList()
Global starlist:TList = CreateList()

Type star

	Field xpos:Int
	Field ypos:Int
	Field speed:Int
	

	Function create_star()
	
		For Local x:Int = 0 To max_stars
		Local s:star = New star
		ListAddLast starlist,(s)
		s.xpos = rnd(0,GR_WIDTH)
		s.ypos = Rand(-1,-GR_HEIGHT)
		s.speed = Rand(2,15)
		num_stars:+1
		Next
		
	End Function
	
	Function update_stars()
	
		For Local s:star = EachIn starlist
		
			s.ypos:+s.speed
			
		Next
		
	End Function
	
	Function draw_star()
	
		For Local s:star = EachIn starlist
		
			DrawImage stars,s.xpos,s.ypos,0
			
		Next
		
	End Function
	
	Method recycle_star()
	
		For Local s:star = EachIn starlist
		If s.ypos > GR_HEIGHT
		s.ypos = -1
		EndIf
		Next
		
	End Method
	
End Type


Strict

Framework BRL.glmax2d
Import BRL.max2d
Import BRL.pngloader
Import BRL.oggloader
Import BRL.Random
Import BRL.Ramstream

Incbin "gfx/invaders.png"
Incbin "gfx/B-MAX.png"
Incbin "gfx/rasterbar.png"
Incbin "gfx/title.png"
Incbin "gfx/star.png"

Const GR_WIDTH = 1024, GR_HEIGHT = 768, DEPTH = 16, HERTZ = 60

Graphics GR_WIDTH,GR_HEIGHT,DEPTH,HERTZ

SetMaskColor 255,0,255
Global invadr:TImage = LoadAnimImage("incbin::gfx/invaders.png",60,40,0,16,MASKEDIMAGE|FILTEREDIMAGE)
Global stars:TImage = LoadImage("incbin::gfx/star.png",FILTEREDIMAGE)

Global iframetimer = MilliSecs()

Global direction:Int

Global num_stars = 0
Global max_stars = 400

Global invaderlist:TList = CreateList()
Global starlist:TList = CreateList()

Type star

	Field xpos:Int
	Field ypos:Int
	Field speed:Int
	

	Function create_star()
	
		For Local x:Int = 0 To max_stars
		Local s:star = New star
		ListAddLast starlist,(s)
		s.xpos = rnd(0,GR_WIDTH)
		s.ypos = Rand(-1,-GR_HEIGHT)
		s.speed = Rand(2,15)
		num_stars:+1
		Next
		
	End Function
	
	Function update_stars()
	
		For Local s:star = EachIn starlist
		
			s.ypos:+s.speed
			
		Next
		
	End Function
	
	Function draw_star()
	
		For Local s:star = EachIn starlist
		
			DrawImage stars,s.xpos,s.ypos,0
			
		Next
		
	End Function
	
	Method recycle_star()
	
		For Local s:star = EachIn starlist
		If s.ypos > GR_HEIGHT
		s.ypos = -1
		EndIf
		Next
		
	End Method
	
End Type

		
Type invader
	
	Global iframe:Int = 0
	Global speed:Float = 8
	Global Iid:Int = 0
	Global ixpos:Float = 100
	Global iypos:Float = 40
	
	Field xpos:Int
	Field ypos:Int
	Field id:Int
	
	Function create_invaders()
	
		For Local x:Int = 0 To 59
		
			Local i:invader = New invader
			i.xpos = ixpos
			i.ypos = iypos
			i.id   = Iid
			ListAddLast invaderlist,(i)
			
			ixpos:+70
			
			If ixpos > GraphicsWidth() - 100
			
				ixpos = 100
				iypos:+50
				
			End If
			
		Next
		
	End Function
	
	Function animate_invaders()
	
		If MilliSecs() > iframetimer + 100
		iframetimer = MilliSecs()
		iframe:+1
		If iframe > 3 Then iframe = 0
		End If
		
	End Function
	
	Method draw_invader()
	
		For Local i:invader = EachIn invaderlist
		
			DrawImage invadr,i.xpos,i.ypos,iframe
			
		Next
		
	End Method
	
	Function move_invader()
		
		If direction = 1
		
			For Local i:invader = EachIn invaderlist
			i.xpos:+speed
			If i.xpos > GraphicsWidth() - 60
				direction = 0
			EndIf
			Next
			
		ElseIf direction = 0

			For Local i:invader = EachIn invaderlist
			i.xpos:-speed
			If i.xpos < 0
				direction = 1
			EndIf
			Next	
		
		EndIf
			
			
	End Function
	
End Type

invader.create_invaders()
star.create_star()
direction = 1

Repeat

	Cls
	
		For Local i:invader = EachIn invaderlist
			i.draw_invader()
			
		Next
		
		invader.animate_invaders()
		invader.move_invader()	
		
		star.update_stars()
		star.draw_star()
		For Local s:star = EachIn starlist
			s.recycle_star()
		Next
		
		DrawText "NUM STARS = : " +num_stars,0,0
		
	FlushMem
	Flip
	
Until KeyHit(KEY_ESCAPE)					


Now dreamora mentioned that it may be drawimage which is causing the problem so I'll try a simple pixel plot instead. I'll post the code in just a sec.

:)

[edit]Plot didnt make a single bit of difference. :/