pixel scroller needs optimization...

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Halifax
Coder
Posts: 21
Joined: Thu Apr 07, 2005 4:20 pm
Location: utah

pixel scroller needs optimization...

Post by Halifax »

first of all, this is not strictly QB :roll:
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: '************************
And you will come to find that we are all one mind, capable of all that's imagined and all conceivable - Maynard
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

What are you compiling with? I keep getting a truck-load of errors, and what's with this:

Code: Select all

Screen (800X600 resolution, 8bit color, 3 pages (0,1,2)) 
Shouldn't it be:

Code: Select all

SCREENRES 800, 600, 8, 3
?? But I still get DIM errors where arrays aren't defined.... =P
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
User avatar
Halifax
Coder
Posts: 21
Joined: Thu Apr 07, 2005 4:20 pm
Location: utah

explanation

Post by Halifax »

okay, to start, i am using freebasic IDE jellyfish to compile and program it.
about your errors with arrays, i have them all defined in a .bi include file. so id be suprised if you didnt get errors. the entire program is a little large to post and it would not really help much anyway, would it? i could send an execute if it really matters but the whole thing is over a megabyte (mostly because of the map file)

about the screenres function...
im not sure what you mean. the FB function for setting screen resolution is Screen WhichScreen, BitDepth, NumPages, etc...
im just using the default screen 19 which is 800X600. im not making my own resoltution. but you are right, if i were to make my own resolution, id have to use Screenres i think.
And you will come to find that we are all one mind, capable of all that's imagined and all conceivable - Maynard
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

Well it's very helpfull to post a working code,. so someone can see what they have to work with as far as suggestions... =)
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations »

And, for everyone's sake, use a code tag when posting code.

Code: Select all

IF CodeTagUsed <> TRUE THEN PRINT "HAHAHA! You should've used code tags, you idiot!"
IF CodeTagUsed = TRUE THEN PRINT "Now you're learning!"
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Stop whining. ^^

At both ratt and PQBC..



Halifax, I glanced over the code and there really isnt too much to be done in terms of major speed bosts.. you can always use lookup tables to increase the math calculation speed etc.. but any spede gain would be minimal..

FB is just too slow, sadly, for highres realtime games like this..

You could try using SDL or Allegro, or even openGL
I have left this dump.
Nemesis
Coder
Posts: 36
Joined: Mon Aug 16, 2004 12:54 pm
Location: Colorado Springs, Colorado

Post by Nemesis »

Code: Select all

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) 
xxpos=Xpos-15
yypos=Ypos-15
For x = 1 To 55 
  xx=x*15
  sctx=x+ScreenTileX
 For y=1 To 41 
  Put (xx- xxpos,(y*15) - yypos, Tile(Map(sctx,y+ScreenTileY),0), PSet 
 Next y
Next x 
End Sub 
I added a few presums to eliminate unnecessary nested calculations.
It should show a little speed increase. Then if you wanted you could still speed it up by adding a few LUT calculations like...

Code: Select all

dim LUT15(1 to 41)
 for x=lbound(LUT15) to Ubound(LUT15)
  LUT15(x)=x*15
 next
[code/)
This would eliminate calculating y*15 about 2000+ times  :)

Cya,

Nemesis
Post Reply