Scrolling breaks at 0,0 origin!

BlitzMax Forums/BlitzMax Programming/Scrolling breaks at 0,0 origin!

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.

SuperStrict

Graphics 800, 600

Global SyncWait:Int = True
Global DEBUG:Int = False
Global UPDATE_FREQUENCY:Int = 50
Global update_time:Float = 1000 / UPDATE_FREQUENCY
 
Global UPS:TTicker = New TTicker
Global FPS:TTicker = New TTicker

Global MapW:Int = 19, MapH:Int = 18, MapL:Int = 9

Global px% '********************************************
Global py% '********************************************

'create player
Global player:TPlayer = New TPlayer
player.x = 0
player.y = 0
player.z = 1
player.maxspeed = 5

Local execution_time:Float = 0
Local t:Int = MilliSecs() 

While Not KeyHit(KEY_ESCAPE) 
	execution_time:+(MilliSecs() - t) 
 	t = MilliSecs() 
	
    While execution_time >= update_time
		UPS.Update()
		Update()
		execution_time:- update_time
	Wend

	FPS.Update() 
	Render(execution_time / update_time) 
	
Wend
End

Function Update() 
	'debug tools
	If KeyHit(KEY_MINUS) And KeyDown(KEY_LSHIFT) And KeyDown(KEY_LCONTROL) 
		player.maxspeed:-0.5
	End If
	If KeyHit(KEY_EQUALS) And KeyDown(KEY_LSHIFT) And KeyDown(KEY_LCONTROL) 
		player.maxspeed:+0.5
	End If
	
	'angle
	If KeyDown(KEY_UP) And KeyDown(KEY_LEFT) 
		player.angle = 135
	Else If KeyDown(KEY_UP) And KeyDown(KEY_RIGHT) 
		player.angle = 45
	Else If KeyDown(KEY_DOWN) And KeyDown(KEY_LEFT) 
		player.angle = 225
	Else If KeyDown(KEY_DOWN) And KeyDown(KEY_RIGHT) 
		player.angle = 315
	Else If KeyDown(KEY_UP) 
		player.angle = 90
	Else If KeyDown(KEY_DOWN) 
		player.angle = 270
	Else If KeyDown(KEY_LEFT) 
		player.angle = 180
	Else If KeyDown(KEY_RIGHT) 
		player.angle = 0
	End If
	
	'speed
	If KeyDown(KEY_UP) And KeyDown(KEY_LEFT)   
		player.speed = player.speed = 0.5
	Else If KeyDown(KEY_UP) And KeyDown(KEY_RIGHT)   
		player.speed = player.speed = 0.5
	Else If KeyDown(KEY_DOWN) And KeyDown(KEY_LEFT) 
		player.speed = player.speed = 0.5
	Else If KeyDown(KEY_DOWN) And KeyDown(KEY_RIGHT)  
		player.speed = player.speed + 0.5
	Else If KeyDown(KEY_UP)   
		player.speed = player.speed + 0.5
	Else If KeyDown(KEY_DOWN)  
		player.speed = player.speed + 0.5
	Else If KeyDown(KEY_LEFT) 
		player.speed = player.speed + 0.5
	Else If KeyDown(KEY_RIGHT) 
		player.speed = player.speed + 0.5
	Else
		player.speed = player.speed / 3
	End If
		
	'limit speed
	If player.speed < 0.5 player.speed = 0
	If player.speed > player.maxspeed player.speed = player.maxspeed
	
	player.oldx = player.x
	player.oldy = player.y

	player.x = player.x + Cos(player.angle) * (player.speed * 32) / (1000.0 / Float(update_time)) 
	player.y = player.y - Sin(player.angle) * (player.speed * 32) / (1000.0 / Float(update_time)) 
End Function
Function Render(tween:Float) 
	'Local tX:Int = Int(player.x * tween + player.oldx * (1.0 - tween)) 
	'Local tY:Int = Int(player.y * tween + player.oldy * (1.0 - tween)) 

	Cls()
	Local ox% = 0 - player.x + ((MapW * 32) / 2)
	Local oy% = 0 - player.y + ((MapH * 32) / 2)
	If px >= 0 And (ox <0) player.x:+32 ox:-1       '**********************
	If px <-32 And (ox > px) player.x:-32; ox:+1    '**********************
	If py >= 0 And (oy <0) player.y:+32; oy:-1      '**********************
	If py <-32 And (oy > py) player.y:-32; oy:+1    '**********************

	SetOrigin(ox,oy ) 

	'Draw zone
	For Local layer:Int = 1 To 9
		RenderLayer(player.x, player.y, player.z, layer) 
	Next
	
	SetOrigin(0,0)
	
	DrawGrid() 
	SetColor 255,0,0
	'FPS
	DrawText "DEBUG MODE", 700, 10
	DrawText "UPS: " + UPS.count, 700, 25
	DrawText "FPS: " + FPS.count, 700, 40
	DrawText "Speed: " + player.speed + " of " + player.maxspeed, 700, 60
	DrawText "x: " + player.x, 700, 75
	DrawText "y: " + player.y, 700, 90
	DrawText "ox: "+ox,700,105
	DrawText "oy: "+oy,700,120
	SetColor 255,255,255
	
	'Delay(10)
	Flip SyncWait
	
	px =ox%
	py =oy%
	
End Function

'============================================================================================

'RenderLayer
Function RenderLayer(x:Int, y:Int, mZ:Int, layer:Int, modes:Int = 0) 
	Local vY:Int
	Local vX:Int
	Local tile:Int
	
	'calculate relative top/left edge
	Local oX:Int = x - ((MapW * 32) / 2) 
	Local oY:Int = y - ((MapH * 32) / 2) 
	
	'DrawRect(0, 0, 32, 32) 
	
	For vY = 0 To MapH - 1
		For vX = 0 To MapW - 1
			Local tX:Int = vX + (oX / 32) 
			Local tY:Int = vY + (oY / 32) 
			Local mX:Int = 0
			Local mY:Int = 0

			'translate position to world zone
			While tX < 0
				tX:+MapW
				mX:-1
			Wend
			While tY < 0
				tY:+MapH
				mY:-1
			Wend
			While tX > MapW - 1
				tX:-MapW
				mX:+1
			Wend
			While tY > MapH - 1
				tY:-MapH
				mY:+1
			Wend

			'Set zone
			Local zone:String = "x" + mX + "y" + mY + "z" + mZ
			
			'Draw tile	
			Local x:Int = oX + (vX * 32)
			Local y:Int = oY + (vY * 32) 
			DrawText(tx, x+5, y+5) 
			DrawText(ty, x+5, y+15) 
		Next
	Next
End Function

'============================================================================================
Function DrawGrid() 
	Local x:Int
	Local y:Int
	For x = 0 To MapW
		DrawLine(x * 32, 0, x * 32, MapH * 32) 
	Next
	For y = 0 To MapH
		DrawLine(0, y * 32, MapW * 32, y * 32) 
	Next
End Function

Type TPlayer
	Field name:String
	Field x:Int
	Field y:Int
	Field z:Int
	Field oldx:Float
	Field oldy:Float
	Field vx:Float
	Field vy:Float
	Field layer:Int = 5
	Field speed:Float
	Field maxspeed:Float = 1
	Field angle:Int
End Type

Type TTicker
	Field count:Int
	Field timeout:Float = 1
	Field timer:Float
	Field counter:Int
		 
	Method update() 
		If Self.timer < MilliSecs() 
			Self.timer = MilliSecs() + (Self.timeout * 1000) 
			Self.count = Self.counter
			Self.counter = 0
		Else
			Self.counter:+1
		End If
	End Method
End Type