Page 1 of 1

umm... timer in DirectX?

Posted: Wed Jun 28, 2006 11:47 am
by ChebbyShabby
how do i do a 40 second countdown in a DirectX screen (within a continous loop)?

Posted: Wed Jun 28, 2006 11:53 am
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)

Posted: Wed Jun 28, 2006 11:55 am
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.

Posted: Wed Jun 28, 2006 12:01 pm
by netmaestro
:D

Well, you scalped me on the pointers you begger!

Posted: Wed Jun 28, 2006 1:19 pm
by Dare
:D

Posted: Wed Jun 28, 2006 1:21 pm
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)

Posted: Wed Jun 28, 2006 9:14 pm
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

Posted: Thu Jun 29, 2006 3:01 am
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.