I'm getting severe slowdown when I create more than 600 individual stars for my games starfield. I have a very fast system and I remember doing the same in B3D and it not have a problem with it. So, I'm guessing that its my create_stars function.
Here are da codeeeez:
Below is the type for the stars :
Is this efficiant enough? I was thinking that instead of deleting a star when it exits the screen borders to actually reuse it and change its y position to the top of the screen again. Would that be better than creating a new star each time?
to help you guys more in helping me here is all the code.
Here are da codeeeez:
Below is the type for the stars :
Global num_stars = 0 Global max_stars = 50 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 = -1 s.speed = Rand(2,15) num_stars:+1 If num_stars > max_stars Exit EndIf 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 remove_star() For Local s:star = EachIn starlist If s.ypos > GR_HEIGHT ListRemove starlist,(s) num_stars:-1 EndIf Next End Method End Type
Is this efficiant enough? I was thinking that instead of deleting a star when it exits the screen borders to actually reuse it and change its y position to the top of the screen again. Would that be better than creating a new star each time?
to help you guys more in helping me here is all the code.
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 = 50 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 = -1 s.speed = Rand(2,15) num_stars:+1 If num_stars > max_stars Exit EndIf 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 remove_star() For Local s:star = EachIn starlist If s.ypos > GR_HEIGHT ListRemove starlist,(s) num_stars:-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() direction = 1 Repeat Cls For Local i:invader = EachIn invaderlist i.draw_invader() Next invader.animate_invaders() invader.move_invader() star.create_star() star.update_stars() star.draw_star() For Local s:star = EachIn starlist s.remove_star() Next DrawText "NUM STARS = : " +num_stars,0,0 FlushMem Flip Until KeyHit(KEY_ESCAPE)