umm... timer in DirectX?

Advanced game related topics
ChebbyShabby
Enthusiast
Enthusiast
Posts: 121
Joined: Mon Jun 26, 2006 10:47 am

umm... timer in DirectX?

Post by ChebbyShabby »

how do i do a 40 second countdown in a DirectX screen (within a continous loop)?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Code: Select all

t=ElapsedMilliseconds()
Repeat
  ExamineKeyboard()
  ClearScreen(#ScrnColor)
  DisplaySprite(#sprite1, s1x, s1y)
  DisplaySprite(#sprite2, s2x, s2y)
  DisplaySprite(#sprite3, s3x, s3y)
  FlipBuffers()
  Delay(1)
  If ElapsedMilliseconds()-t >=40000
    ; Timer expired
  EndIf
Until KeyboardPushed(#PB_Key_Escape)
BERESHEIT
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Hi again ChebbyShabby,

If you're using 4.00 then elapsedmilliseconds() returns a time. I can't remember if it was a 3.94 command, if not, GetTickCount_() is the windows API equivalent.

Perhaps something like this:

Code: Select all

tm.l = GetTickCount_() + 40000
While GetTickCount_() < tm
; whatever
Wend
You can replace GetTickCount_() with ElapsedMilliseconds() if your version supports it.

Also perhaps scout around for Droopy's lib, which has a ton of good practical stuff include some real timer routines (not a loop like above) or check out the API for these.


Edit:

Lol, netmaestro with the oiled holster and smoking gun again. :D


Edit again:

ChebbyShabby, http://www.PureArea.net has a very good code archive which is worth downloading and going through next time you have some decent internet access.
Dare2 cut down to size
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

:D

Well, you scalped me on the pointers you begger!
BERESHEIT
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

:D
Dare2 cut down to size
ChebbyShabby
Enthusiast
Enthusiast
Posts: 121
Joined: Mon Jun 26, 2006 10:47 am

Post by ChebbyShabby »

wow thanks both of u

however, i tried it on the demo version (since i'm not at home) and it freezes the directX screen. this is the code i used:

Code: Select all

tm.l = GetTickCount_() + 1000
While GetTickCount_() < tm
  pTime-1
Wend
where "pTime" is the variable i used to display the time on the screen

(i want pTime to decrease by 1 every second until it reaches 0)
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Try this code instead:

Code: Select all

starttm.l= GetTickCount_()
maxtm.l = starttm + 40000
runningtm.l = starttm + 1000
repeat 
  repeat:until GetTickCount_()> runningtm ;counts off 1 second
  pTime-1 ;update counter
  ;display of time goes here
  runningtm+1000 ;this adds a second onto previous value so we don't get  time creep
  currenttm=GetTickCount_()
until currenttm>=maxtm
You only want to decrement pTime once each second instead of every time your while loop is executed (66 or more times a second).


Demivec
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Another approach, using a timer callback. Maybe useful, maybe not.

Code: Select all

Global countDown.l

Procedure displayCountDown()
  StartDrawing(ImageOutput(0))
  Box(0,0,100,100,$000000)
  DrawText(40,40,Str(countdown),$FFFFFF,$000000)
  StopDrawing()
  SetGadgetState(0,ImageID(0))
EndProcedure

Procedure timerCallBack(hWnd.l,uMsg.l,idEvent.l,dwTime.l)
  countDown - 1
  If countDown < 0
    KillTimer_(WindowID(0),idEvent)
  Else
    displayCountDown()
  EndIf
EndProcedure

countDown = 4

CreateImage(0, 100,100)

OpenWindow(0, 0,0, 100,100, "Junk", #PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))
ImageGadget(0, 0,0, 100,100, ImageID(0))

SetTimer_(WindowID(0),1,1000,@timerCallBack())
displayCountDown()

Repeat
  ev.l = WindowEvent()
  If ev = #PB_Event_CloseWindow
    End
  EndIf
  Delay(1)
Until countDown < 0

HideGadget(0,#True)

Repeat
  ev.l = WaitWindowEvent()
Until ev = #PB_Event_CloseWindow

End
The above is quick(ly written) and makes lots of assumptions, consider it concept and not good necessarily good coding. :) It is also version 4 syntax so you might need to change some things (eg, swap window flags and title positioning).

Set countDown to however many seconds you want.
Dare2 cut down to size
Post Reply