pixel scroller needs optimization...
Posted: Sun Nov 13, 2005 12:45 am
first of all, this is not strictly QB
the resolution is 800x600 but im still using 256 colors...
ive created a fairly simple pixel scroller with some pretty complex 2D camera routines (if keyboard-pounding indicates complexity...)
1- anyone see any ways to optimize the code below? im open to suggestions...
2- if you move the destination far away from CamXY you'll notice that the "come hither" routine is far from perfect. instead of moving in a straight line toward the destination, it first moves to whichever axis is closer then moves along that axis toward the destination. This could be a major problem later when i begin programming the path-finding. Could someone tell me how to fix that? I know it will entail drawing a line from xy to x2y2 and following it...
3- I want to draw an indicator on the screen that "points" to the destination. I know this will involve drawing an imaginary line from xy to x2y2 and clipping it at the screen edge... How?
BTW -
I'm doing 2 full page flips per frame because i like to be neat. I'd prefer to have the GUI on a seperate page because... well, im not sure why. It just seems more organized.
I get about 70-90 FPS, 30-50 with Windows Media Player running
Can my camera routines be combined so i have one equation that will move cameraXY from xy to x2y2?
What is frame-independent movement? does it apply? is it scary? 0_o
Here is the code--
*****************************************
Screen (800X600 resolution, 8bit color, 3 pages (0,1,2))
Sub MakeGameScreen
If VSync = 1 Then ScreenSync '(i.e. Wait &H8, &H8 or whatever...)
Flip 1,0 '(page flip)
Flip 2,0
End Sub
Sub DrawMap
ScreenSet 1,0 'sets work page to 1 and visible page to 0
ScreenTileX = Int(CamX / 15)
ScreenTileY = Int(CamY / 15)
Xpos = Int(CamX Mod 15)
Ypos = Int(CamY Mod 15)
For x = 1 To 55
For y = 1 To 41
Put ((x*15)- Xpos-15,(y*15) - Ypos-15), Tile(Map(x+ScreenTileX,y+ScreenTileY),0), PSet
Next y
Next x
End Sub
BeginProgram: '*******************
CamX = 10000
CamY = 10000
CamSpeed = 10
MaxCamSpeed = 50
ScreenDestX = CamX
ScreenDestY = CamY
VSync = 1
Seconds = 1
StartTime = Timer
Do
If Timer > StartTime + 1 Then Seconds = Seconds + 1: StartTime = Timer
If ScreenDestX > CamX Then
DistanceToGo = ScreenDestX - CamX +400
CamSpeedX = (DistanceToGo-400)/CamSpeed
If CamSpeedX < 2 Then CamSpeedX = 2
If CamSpeedX > MaxCamSpeed Then CamSpeedX = MaxCamSpeed
CamX = CamX + CamSpeedX
If CamX > ScreenDestX Then CamX = ScreenDestX
End If
If ScreenDestX < CamX Then
DistanceToGo = CamX - ScreenDestX +400
CamSpeedX = (DistanceToGo-400)/CamSpeed
If CamSpeedX < 2 Then CamSpeedX = 2
If CamSpeedX > MaxCamSpeed Then CamSpeedX = MaxCamSpeed
CamX = CamX - CamSpeedX
If CamX < ScreenDestX Then CamX = ScreenDestX
End If
If ScreenDestY < CamY Then
DistanceToGo = CamY - ScreenDestY +300
CamSpeedY = (DistanceToGo-300)/CamSpeed
If CamSpeedY < 2 Then CamSpeedY = 2
If CamSpeedY > MaxCamSpeed Then CamSpeedY = MaxCamSpeed
CamY = CamY - CamSpeedY
If CamY < ScreenDestY Then CamY = ScreenDestY
End If
If ScreenDestY > CamY Then
DistanceToGo = ScreenDestY - CamY +300
CamSpeedY = (DistanceToGo-300)/CamSpeed
If CamSpeedY < 2 Then CamSpeedY = 2
If CamSpeedY > MaxCamSpeed Then CamSpeedY = MaxCamSpeed
CamY = CamY + CamSpeedY
If CamY > ScreenDestY Then CamY = ScreenDestY
End If
If ScreenDestX < 1 Then ScreenDestX = 1
If ScreenDestX > 14189 Then ScreenDestX = 14189
If ScreenDestY < 1 Then ScreenDestY = 1
If ScreenDestY > 14399 Then ScreenDestY = 14399
If CamX < 1 Then CamX = 1
If CamX > 14189 Then CamX = 14189
If CamY < 1 Then CamY = 1
If CamY > 14399 Then CamY = 14399
Frames = Frames + 1
DrawMap
Circle (ScreenDestX-CamX+400, ScreenDestY-CamY+300),10,255
DrawIconPanel
DrawBox 4,12,340,80
Color 254,253
Locate 2, 2: Print Using "FPS: ###"; Frames/Seconds
Locate 3, 2: Print "Click left mouse button to drag screen."
Locate 4, 2: Print "Click left icon to quit."
Locate 5, 2: Print Using "Click middle icon to toggle VSync: #"; VSync
MakeGameScreen
MouseCheck
fjf$ = Inkey$
If fjf$ = "q" Then End
Loop
EndProgram: '************************
the resolution is 800x600 but im still using 256 colors...
ive created a fairly simple pixel scroller with some pretty complex 2D camera routines (if keyboard-pounding indicates complexity...)
1- anyone see any ways to optimize the code below? im open to suggestions...
2- if you move the destination far away from CamXY you'll notice that the "come hither" routine is far from perfect. instead of moving in a straight line toward the destination, it first moves to whichever axis is closer then moves along that axis toward the destination. This could be a major problem later when i begin programming the path-finding. Could someone tell me how to fix that? I know it will entail drawing a line from xy to x2y2 and following it...
3- I want to draw an indicator on the screen that "points" to the destination. I know this will involve drawing an imaginary line from xy to x2y2 and clipping it at the screen edge... How?
BTW -
I'm doing 2 full page flips per frame because i like to be neat. I'd prefer to have the GUI on a seperate page because... well, im not sure why. It just seems more organized.
I get about 70-90 FPS, 30-50 with Windows Media Player running
Can my camera routines be combined so i have one equation that will move cameraXY from xy to x2y2?
What is frame-independent movement? does it apply? is it scary? 0_o
Here is the code--
*****************************************
Screen (800X600 resolution, 8bit color, 3 pages (0,1,2))
Sub MakeGameScreen
If VSync = 1 Then ScreenSync '(i.e. Wait &H8, &H8 or whatever...)
Flip 1,0 '(page flip)
Flip 2,0
End Sub
Sub DrawMap
ScreenSet 1,0 'sets work page to 1 and visible page to 0
ScreenTileX = Int(CamX / 15)
ScreenTileY = Int(CamY / 15)
Xpos = Int(CamX Mod 15)
Ypos = Int(CamY Mod 15)
For x = 1 To 55
For y = 1 To 41
Put ((x*15)- Xpos-15,(y*15) - Ypos-15), Tile(Map(x+ScreenTileX,y+ScreenTileY),0), PSet
Next y
Next x
End Sub
BeginProgram: '*******************
CamX = 10000
CamY = 10000
CamSpeed = 10
MaxCamSpeed = 50
ScreenDestX = CamX
ScreenDestY = CamY
VSync = 1
Seconds = 1
StartTime = Timer
Do
If Timer > StartTime + 1 Then Seconds = Seconds + 1: StartTime = Timer
If ScreenDestX > CamX Then
DistanceToGo = ScreenDestX - CamX +400
CamSpeedX = (DistanceToGo-400)/CamSpeed
If CamSpeedX < 2 Then CamSpeedX = 2
If CamSpeedX > MaxCamSpeed Then CamSpeedX = MaxCamSpeed
CamX = CamX + CamSpeedX
If CamX > ScreenDestX Then CamX = ScreenDestX
End If
If ScreenDestX < CamX Then
DistanceToGo = CamX - ScreenDestX +400
CamSpeedX = (DistanceToGo-400)/CamSpeed
If CamSpeedX < 2 Then CamSpeedX = 2
If CamSpeedX > MaxCamSpeed Then CamSpeedX = MaxCamSpeed
CamX = CamX - CamSpeedX
If CamX < ScreenDestX Then CamX = ScreenDestX
End If
If ScreenDestY < CamY Then
DistanceToGo = CamY - ScreenDestY +300
CamSpeedY = (DistanceToGo-300)/CamSpeed
If CamSpeedY < 2 Then CamSpeedY = 2
If CamSpeedY > MaxCamSpeed Then CamSpeedY = MaxCamSpeed
CamY = CamY - CamSpeedY
If CamY < ScreenDestY Then CamY = ScreenDestY
End If
If ScreenDestY > CamY Then
DistanceToGo = ScreenDestY - CamY +300
CamSpeedY = (DistanceToGo-300)/CamSpeed
If CamSpeedY < 2 Then CamSpeedY = 2
If CamSpeedY > MaxCamSpeed Then CamSpeedY = MaxCamSpeed
CamY = CamY + CamSpeedY
If CamY > ScreenDestY Then CamY = ScreenDestY
End If
If ScreenDestX < 1 Then ScreenDestX = 1
If ScreenDestX > 14189 Then ScreenDestX = 14189
If ScreenDestY < 1 Then ScreenDestY = 1
If ScreenDestY > 14399 Then ScreenDestY = 14399
If CamX < 1 Then CamX = 1
If CamX > 14189 Then CamX = 14189
If CamY < 1 Then CamY = 1
If CamY > 14399 Then CamY = 14399
Frames = Frames + 1
DrawMap
Circle (ScreenDestX-CamX+400, ScreenDestY-CamY+300),10,255
DrawIconPanel
DrawBox 4,12,340,80
Color 254,253
Locate 2, 2: Print Using "FPS: ###"; Frames/Seconds
Locate 3, 2: Print "Click left mouse button to drag screen."
Locate 4, 2: Print "Click left icon to quit."
Locate 5, 2: Print Using "Click middle icon to toggle VSync: #"; VSync
MakeGameScreen
MouseCheck
fjf$ = Inkey$
If fjf$ = "q" Then End
Loop
EndProgram: '************************