my vertical scrolling is slow

BlitzMax Forums/BlitzMax Beginners Area/my vertical scrolling is slow

First attempt to code with arrays :

Strict

Const Max_TILEMAP_HEIGHT = 1000

Type TScreen

	Field array_cells   : TCell[32 * MAX_TILEMAP_HEIGHT]
	Field array_tiles   : TTile[]
	Field array_cannons : TCannon[]
		
	Field NbTiles
	Field NbCannons
	
	Field ScrollY
	Field EllapsedTime
	
	Field t
		
	Function Init:TScreen ()
	
		Local s:TScreen = New TScreen
		
		' fill the array with empy values
		
		For Local y = 0 To MAX_TILEMAP_HEIGHT - 1
		
			For Local x=0 To 32 - 1
			
				Local i = y * 32 + x
				
				s.array_cells[i] = TCell.Create (x,y)
					
			Next
			
		Next
		
		s.NbTiles = 0
		s.NbCannons = 0
		
		s.EllapsedTime = MilliSecs()
		s.ScrollY = 0
	
		Return s
	
	End Function
	
	Method Add_tile (pPosX, pPosY)
	
		' check index if not out of bound
		
		Local index = pPosY * 32 + pPosX
		
		If index > 32 * MAX_TILEMAP_HEIGHT Then
			RuntimeError "Bad Index"
		End If
		
		array_cells[Index].Id_tile = NbTiles + 1
		
		NbTiles = NbTiles + 1		
		
		array_tiles = array_tiles [..NbTiles+1]
		
		array_tiles [NbTiles] = TTile.Create(pPosX, pPosY)
		
	End Method

	Method Add_cannon (pPosX, pPosY)
	
		' check index if not out of bound
		
		Local index = pPosY * 32 + pPosX
		
		If index > 32 * MAX_TILEMAP_HEIGHT Then
			RuntimeError "Bad Index"
		End If
		
		array_cells[index].Id_cannon = NbCannons + 1
		
		NbCannons = NbCannons + 1		
		
		array_cannons = array_cannons [..NbCannons+1]
		
		array_cannons [NbCannons] = TCannon.Create(pPosX, pPosY)		
	
	End Method
	
	Method Compute_logic()
	
		If EllapsedTime + 20 < MilliSecs() Then
			
			t = MilliSecs()
			
			For Local y=0 To MAX_TILEMAP_HEIGHT - 1
			
				For Local x=0 To 31
				
					Local Index = y * 32 + x
					 
		
					If array_cells[Index].Id_tile <> - 1 Then
					
						Local ix
						ix = array_cells[Index].Id_tile
						array_tiles [ix].PosY = array_tiles [ix].PosY - 1
						
					End If
		
					If array_cells[Index].Id_cannon <> - 1 Then
					
						Local ix
						ix = array_cells[Index].Id_cannon
						array_cannons [ix].PosY = array_cannons [ix].PosY - 1
					
					End If
		
				
				Next
				
			Next
			
			t = MilliSecs() - t
			
		
			EllapsedTime = MilliSecs()

		End If
	
	End Method
	
	Method Draw()
	
		For Local y=0 To MAX_TILEMAP_HEIGHT - 1
		
			For Local x=0 To 31
			
				Local Index = y * 32 + x
				 
	
				If array_cells[Index].Id_tile <> - 1 Then
				
					Local px,py, ix, r,g,b
					ix = array_cells[Index].Id_tile 
					px = array_tiles [ix].PosX
					py = array_tiles [ix].PosY
					r = array_tiles [ix].r
					g = array_tiles [ix].g
					b = array_tiles [ix].b
					
					SetColor r,g,b
					DrawRect px,py, 32,32
					
				End If
	
				If array_cells[Index].Id_cannon <> - 1 Then
				
					Local px,py, angle, ix
					ix = array_cells[Index].Id_cannon
					px = array_cannons [ix].PosX
					py = array_cannons [ix].PosY
					angle = array_cannons [ix].Angle
					
					SetColor 255,0,0
					DrawOval px,py, 32,32
					
					SetColor 0,0,255
					SetRotation angle
					SetLineWidth 3
					DrawLine px+16,py+16,px+16+20,py+16
					SetLineWidth 2
					SetRotation 0
				
				End If
	
			
			Next
			
		Next
		
		SetColor 255,255,255
		DrawText "Time=" + String(t),10,10
		
	End Method
	
	
End Type

Type TCell

	Field PosX
	Field PosY
	
	Field Id_tile
	Field Id_Cannon
	
	Function Create : TCell (PosX, PosY)
	
		Local c:TCell = New TCell
		
		c.PosX = PosX 
		c.PosY = PosY 
		
		c.Id_Tile = -1
		c.Id_Cannon = -1
		
		Return c
	
	End Function
	
End Type

Type TTile

	Field PosX
	Field PosY
	
	Field r
	Field g
	Field b
	
	Function Create:TTile (PosX, PosY)
	
		Local t:TTile = New TTile
		
		t.PosX = PosX * 32
		t.PosY = PosY * 32
		
		t.r = Rand (100,255)
		t.g = Rand (100,255)
		t.b = Rand (100,255)
		
		Return t
	
	End Function
	
End Type

Type TCannon

	'Field Id_cannon : Int

	Field PosX
	Field PosY
	
	Field angle
	
	Function Create:TCannon (PosX, PosY)
	
		Local c:TCannon = New TCannon
		
		c.PosX = PosX * 32
		c.PosY = PosY * 32
		
		c.angle = Rand (0,360)
		
		Return c
	
	End Function	
	
End Type


' main program

Graphics 1024, 768

Global Screen : TScreen = TScreen.init()

DrawText "please wait, computing random tiles and canon positions...",0,0
Flip

For Local i=1 To 10000

	Local x = Rand (0,31)
	Local y = Rand (0,MAX_TILEMAP_HEIGHT-1)
	Screen.Add_tile (x,y)

	x = Rand (0,31)
	y = Rand (0,MAX_TILEMAP_HEIGHT-1)
	Screen.Add_cannon (x,y)

Next


While Not KeyDown (KEY_ESCAPE)

	Screen.Compute_logic()
	Cls
	Screen.Draw()
	Flip
	
Wend