Hi everyone, I made a starfield for a game I'm currently working on. It works well enough (I may try to improve the visuals later) but it gets slow when there are more than several hundred stars onscreen at once. I was wondering if anyone could look through the code and see if they had any ideas of how to improve the speed. Thanks a lot!
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(sx#,sy#,density#) Local s:TStarfield = New TStarfield s.nstars = sx*sy*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(sx)'TStar.Create(Rnd(sx),Rnd(sy),255,255,255,3) s.layersY[i][t]= Rnd(sy) Next Next Return s End Function Function DrawAll(x#,y#) For Local i:TStarfield = EachIn(list) i.draw(x#,y#) Next End Function Function UpdateAll(x#,y#) For Local i:TStarfield = EachIn(list) i.update(x#,y#) Next End Function Method Draw(x#,y#) Local time = MilliSecs() For Local t = 0 To 2 Local tx = x*(t+1)/2 Local ty = y*(t+1)/2 For Local i = 0 To nstars-1 If layersX[t][i]+tx>0 And layersX[t][i]+tx<GraphicsWidth() And layersY[t][i]+ty>0 And layersY[t][i]+ty<GraphicsWidth() If time-layersTime[t][i]<rate Then SetColor 128,128,128 DrawOval layersX[t][i]+tx-(t+1)/2,layersY[t][i]+ty-(t+1)/2,t+1,t+1 SetColor 255,255,255 EndIf Next Next End Method Method Update(x#,y#) Local time = MilliSecs() For Local t = 0 To 2 Local tx = x*(3-t)/2 Local ty = y*(3-t)/2 For Local i = 0 To nstars-1 If layersX[t][i]+tx>0 And layersX[t][i]+tx<GraphicsWidth() And layersY[t][i]+ty>0 And layersY[t][i]+ty<GraphicsWidth() If time - layersTime[t][i]>rate And Rand(600) = 1 Then layersTime[t][i] = MilliSecs() EndIf Next Next End Method End Type