Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Optimzing Speed
#1
Hi there...I need some help with my budding engine for a game in the future. As of now, it's just a circle but it's my first attempt. I tried to complie it but it moves really slowly (I have a 2.0 GHz rig).

Any ideas on how to speed it up?

Code:
SCREEN 13
x = 160
y = 100
CIRCLE (x, y), 2

'START OF LOOP

DO
strtloop:
     IF INKEY$ = "w" THEN
          y = y - 1
          CLS
          CIRCLE (x, y), 2
          GOTO strtloop
     END IF
     IF INKEY$ = "a" THEN
          x = x - 1
          CLS
          CIRCLE (x, y), 2
          GOTO strtloop
     END IF
     IF INKEY$ = "s" THEN
          y = y + 1
          CLS
          CIRCLE (x, y), 2
          GOTO strtloop
     END IF
     IF INKEY$ = "d" THEN
          x = x + 1
          CLS
          CIRCLE (x, y), 2
          GOTO strtloop
     END IF
LOOP WHILE INKEY$ <> "q"
Reply
#2
Warning: anyone who repeats me gets EATEN!!!!

Speed up by:

1) Video swaps, with UGL (a library for qb..) instead of just CLS.
2) Use a CASE instead of 4 IFs. The SELECT CASE will think once, the IFs will think 4 times.
3) Put the "q" in the CASE too, as DO WHILE bla... is inefficient and will cause you a lot of trouble later, since you will put stuff in between the "LOOP UNTIL.." end and IF INKEY = "d"...
4) This is preference only, but put identical stuff at the end..
5) Remove START OF LOOP comment, it won't help you..
6) Put the draw routine first...
7) Use a better keyhandler.... INP(96) will start you off.... same as inkey except it gives a number from 0 to 255... if greater than 127 then that means it was the last key pressed. UGL has a perfect multi-keyhandler, though. IF you code it in right!

Here is the right way to do select case, in qb, without implementing (1):

Code:
SCREEN 13: x = 160: y = 100

DO
'start draw routine
CLS : CIRCLE (x, y), 2

'start key input routine
SELECT CASE LCASE$(INKEY$)
case "w": y = y - 1
case "a": x = x - 1
case "s": y = y + 1
case "d": x = x + 1
case "q": EXIT DO
END SELECT
LOOP

SYSTEM

NOTE: this WILL flicker, because you need a constant frame rate if you plan on having ANY effects, or things that move without you having to move also, in your game. It will NOT flicker if you implement video page swapping... You have to learn UGL or you'll be stuck with screen 7 video page swap commands forever..........
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#3
Don't use CLS, just redraw the circle with the background color before drawing the new one.
Antoni
Reply
#4
Take a look at this:

Code:
DECLARE SUB PsetB (x%, y%, c%)
DECLARE SUB CircleB (x%, y%, r%, c%)
DECLARE SUB ClsB ()

'$DYNAMIC

DIM SHARED Buffer%(32001)

SCREEN 13

CONST Pi# = 3.141592654#

'Init Background Buffer
Buffer%(0)=320*8
Buffer%(1)=200

x% = 160
y% = 100

CircleB x%, y%, 16, 15
PUT (0, 0), Buffer%

DO

moved% = 0

i$ = INKEY$

SELECT CASE INP(&H60)
  CASE 75:
   x% = x% - 1
   moved% = 1
  CASE 77:
   x% = x% + 1
   moved% = 1
  CASE 72:
   y% = y% - 1
   moved% = 1
  CASE 80:
   y% = y% + 1
   moved% = 1
  CASE 1:
   EXIT DO
END SELECT

WAIT &H3DA, 8
WAIT &H3DA, 8, 8

IF moved% = 1 THEN
  ClsB
  CircleB x%, y%, 16, 15
  PUT (0, 0), Buffer%, PSET
END IF

LOOP

REM $STATIC
SUB CircleB (x%, y%, r%, c%)
FOR n! = 0 TO 2 * Pi# STEP (1 / 180) * Pi#
  PsetB x% + SIN(n!) * r% * (5 / 6), y% + COS(n!) * r% * (5 / 6), c%
NEXT n!
END SUB

SUB ClsB
DEF SEG = VARSEG(Buffer%(0))
FOR n& = 5 TO 63999
  POKE n&, 0
NEXT n&
DEF SEG
END SUB

SUB PsetB (x%, y%, c%)
DEF SEG = VARSEG(Buffer%(0))
POKE 4 + x% + 320& * y%, c%
DEF SEG
END SUB

If you have questions about the PsetB routine, read this: http://faq.qbasicnews.com/?blast=DoubleBuffering
B 4 EVER
Reply
#5
Thanks a lot for your help...

I got it written down and it works (minor flicker).

Besides how do I display the FPS as a constantly changing number? Or this is impossible in such an old language?
Reply
#6
It's not old, it's obsolete... er haha fooled you there.. qb isn't obsolete or old, it just has a lot of stupid points and is supported very badly by XP.

To answer your question, put this before the main loop:

t1# = TIMER

and this right after the DO of the loop:
loop1% = loop1% + 1
t2# = TIMER
if t2# - t1# = .3 then LOCATE 1,1: PRINT loop1% t1# = t2#: loop1% = 0


Accuracy depends on the timer function.

Now Moneo is going to start another crusade about how this won't work if you are testing this at approximately 23:59:59pm..
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#7
Quote:Now Moneo is going to start another crusade about how this won't work if you are testing this at approximately 23:59:59pm..
Wonderful, Aga, you remembered! I'm flattered.
Just a passing comment: There is no such time as 23:59:59pm, It's 23:59:59 only. The "pm" is totally superflous when you work with military time, as does TIMER.
*****
Reply
#8
Quote:
Agamemnus Wrote:Now Moneo is going to start another crusade about how this won't work if you are testing this at approximately 23:59:59pm..
Wonderful, Aga, you remembered! I'm flattered.
Just a passing comment: There is no such time as 23:59:59pm, It's 23:59:59 only. The "pm" is totally superflous when you work with military time, as does TIMER$.
*****

LOL :rotfl:
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#9
Quote:
Moneo Wrote:
Agamemnus Wrote:Now Moneo is going to start another crusade about how this won't work if you are testing this at approximately 23:59:59pm..
Wonderful, Aga, you remembered! I'm flattered.
Just a passing comment: There is no such time as 23:59:59pm, It's 23:59:59 only. The "pm" is totally superflous when you work with military time, as does TIMER$.
*****

LOL :rotfl:

LOL^2

:rotfl:
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#10
Maybe he's on a planet with 48 hours of time a day instead of 24. It could happen.
am an asshole. Get used to it.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)