Main Menu

Asteroids

Started by monkeybot, December 16, 2009, 03:23:17 PM

Previous topic - Next topic

monkeybot

weird............after compiling it is now running wayyyyyy too fast on my machine.!!!???!?!?!

gonna rip my Proprietary timer code out and run on setfps().

it is strange this has just started to happen,seemingly all of a sudden.

PB 1-64M
Editor119b3.exe

kevin

#31

; PROJECT : ast frag2
; AUTHOR  : Microsoft
; CREATED : 16/01/2010
; EDITED  : 21/12/2010
; ---------------------------------------------------------------------
ScreenVsync on
gametimer2=timer()

gameloop()
end

Constant gamespeed=20 ;fps


function gameloop()
   local x
    Repeat
      Cls 0
;
      If timer()>=gametimer2
;        gametimer2=(Timer()+(1000/gamespeed));gamespeed)
      gametimer2=(Timer()+20);gamespeed)
        circle x,100,5,1
        x=x+1
        sync
    EndIf
    ; sync
  Until   false
endFunction



  The logic of the loop is broken, rather than redraw completely at 20 MS intervals it's drawing the circle + syncing, but clearing the graphics buffer every iteration.  



ScreenVsync on

Constant gamespeed=50 ;fps

gameloop()

end



function gameloop()
  local x
gametimer2=timer()

  Repeat
;
CurrentTime=Timer()
     If CurrentTime>=gametimer2
    gametimer2=(CurrentTime+(1000/GameSpeed))

; redraw frame only when at least 20 milliseconds have pasted
RenderGame()    

  EndIf
  ; sync
  Until spacekey()
endFunction



Psub RenderGame()

     Cls
        circle x,100,5,1
        x=mod(x+1,800)
        sync

EndPsub




  This approach is basically what SetFPS does anyway, with one exception, setting fps means that when syncing on computers that can run the loop faster than required (Less than 20, when that's the target), than it gives the free time given back to windows.   This is not he case in this loop however, as this loop is a hogging all the cpu since it's constantly waiting for the next frame to occur.
 

 This can be dropped in a bit like this,



ScreenVsync on
Constant gamespeed=50 ;fps

gameloop()

end



function gameloop()
  local x
gametimer2=timer()

  Repeat

;
CurrentTime=Timer()

TimeToNextFrame=gametimer2-CurrentTime

     If TimeToNextFrame<1

      gametimer2=(CurrentTime+(1000/GameSpeed))

; redraw frame only when at least 20 milliseconds have pasted
RenderGame()    

else

; CHeck if there's a more then 5 milliseonds to go
if TimeToNextFrame>=5
wait 4
endif

  EndIf
  ; sync
  Until spacekey()
endFunction



Psub RenderGame()

     Cls
        circle x,100,5,1
        x=mod(x+1,800)
        sync

EndPsub





monkeybot

oh yeah thanks yet again kevin.

p.s. i am love with PB again after empty sorted the IDE. woohoo.

Cheers.

monkeybot

#33
I have been meaning to post the source for asteroids for a while,it is fairly long in the tooth and has a few bugs,but i wont spend any more time on it.

ATLUS

monkeybot, Great Work!!!  ;)

monkeybot


Sigtrygg

Hello monkeybot!

I have tried to start your Asteroids, but an error massage appeared:

Error String: Variable 'inputlibrary_version' is Not defined in Scope declaration.

I use PlayBasic Vers. 1.64N

Can you tell me, what I have to do?

Greetings by

Sigtrygg

monkeybot

i assume the source?

um...did you decompress the archive properly?
do you have the input lib in PB installed correctly?

kevin


  I had a look at this the other day (thanks for posting btw), and had much the same issue, it seemed that one of the sources wasn't included in the project file so I had to add it again.  After that it was fine. 

monkeybot

oops
i better sort that out.