Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Game Programming > DirectX > Is there a way to use direct x to act like a timer


Reply
 
Thread Tools Display Modes
  #1  
Old 10-26-2001, 01:35 AM
andrewo
Guest
 
Posts: n/a
Question Is there a way to use direct x to act like a timer


In my games i have a sleep command as my timer for the game speed

but it is a little slow, and i heard that there is a directx command that also will do the same thing but runs smoother

Reply With Quote
  #2  
Old 10-26-2001, 01:53 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,884
Default Re: Is there a way to use direct x to act like a timer

Syncing to screen, also called vsync or wvbl (wait for vertical blanc)

First you need a reference to ddraw.tlb

The commands I use:
<pre>
' first some public declares
Public DD As DirectDraw ' our directdraw reference

' init DirectDraw
DirectDrawCreate ByVal 0&amp;, DD, Nothing
DD.SetCooperativeLevel Me.hWnd, DDSCL_EXCLUSIVE Or DDSCL_FULLSCREEN
DD.SetDisplayMode lXres, lYres, lColorDepth

' in the game loop use this
Do
'
' Game code
' Game code
'
DD.WaitForVerticalBlank DDWAITVB_BLOCKBEGIN, 0&amp;
Loop Until bGameFinished
</pre>
Reply With Quote
  #3  
Old 10-26-2001, 01:58 AM
andrewo
Guest
 
Posts: n/a
Default Re: Is there a way to use direct x to act like a timer

I dont understand how this works...what is the code actually doing to make it like a timer?
Reply With Quote
  #4  
Old 10-26-2001, 02:36 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,884
Default Re: Is there a way to use direct x to act like a timer

Suppose your screen is refreshed 70 times per second.

If you do all needed screen updates within 1/70th of a second and suspend all screen updates until the next screen refresh occurs, you will get smooth animation without needing a timer to refresh/repaint your screen.
Reply With Quote
  #5  
Old 10-26-2001, 07:01 AM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default Re: Is there a way to use direct x to act like a timer

Arnout, I think maybe you are missing the point here. V. Syncing doesnt have anything to do with timers, its just used to make things all fit together. I believe what andrewo wants is an alternative to the sleep API, to pause program executiong for a set period of time.

What you might be after is a way of making the game run at the same speed on any PC, without setting a FPS cap. However, I suspect you are just looking for an all-purpose timer. This code might help:

<pre>Public Sub DXTimer(lMilliseconds As Long)
Dim cTim As Long
cTim = DX.TickCount

Do Until DX.TickCount - cTim &gt;= lMilliseconds
DoEvents
'You could make it do other stuff here, like update the screen
'Or game phyics
Loop
End Sub</pre>

Of course, this will actually pause program execution for the specified time, just like sleep. Music will continue to run, as will sound, but you will have to engineer it specially if you want it to refresh the screen during this wait period. If that is what you are after, then I can help you there as well.

[img]images/icons/smile.gif[/img]
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #6  
Old 10-26-2001, 07:28 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,884
Default Re: Is there a way to use direct x to act like a timer

Must be me, but I've never used sleep or whatever timers in games/demo's.
What do you need a timer for??
Reply With Quote
  #7  
Old 10-26-2001, 07:31 AM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default Re: Is there a way to use direct x to act like a timer

Well, basically so that when the game is run on faster PCs it doesnt just zoom along, and the speed is uniform across a variety of platforms.

You can find more info on this here (Lucky's VB)
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #8  
Old 10-26-2001, 07:35 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,884
Default Re: Is there a way to use direct x to act like a timer

But if you use a WVBL, your execution will be halted until the screen starts to redraw. So whatever speed you have, it will never be to fast...
Reply With Quote
  #9  
Old 10-26-2001, 07:38 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,884
Default Re: Is there a way to use direct x to act like a timer

Just read Lucky's page... It's merely a trick to keep slower computers in speed...

Indeed when you don't have the speed to build the screen every WVBL the game can become unplayable ...
Reply With Quote
  #10  
Old 10-30-2001, 11:13 PM
andrewo
Guest
 
Posts: n/a
Default Re: Is there a way to use direct x to act like a timer

Hey so arnoutv are u sayin your code waits for the next frame to go by or something?

i've seen this timer that works by going frame by frame and adding an amount of milliseconds for each frame

does anyone know how to do this?
Reply With Quote
  #11  
Old 10-30-2001, 11:18 PM
andrewo
Guest
 
Posts: n/a
Default Re: Is there a way to use direct x to act like a timer

hey i cant find the ddraw.tlb file on my computer , is there another similar file that i can use?
Reply With Quote
  #12  
Old 10-31-2001, 01:13 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,884
Default Re: Is there a way to use direct x to act like a timer

You're right, I wait for the screen to be repainted to do all actions.
Reply With Quote
  #13  
Old 10-31-2001, 08:47 AM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default Re: Is there a way to use direct x to act like a timer

You dont need a ddraw TLB, you just need to Reference DirectX 7 for Visual Basic Type Library in your project. The actual file it all comes from is called DX7VB.DLL and comes with DirectX 7, so if you have DX 7 installed on your computer, then it is very likely you will have this file also.
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #14  
Old 11-01-2001, 02:20 AM
andrewo
Guest
 
Posts: n/a
Default Where do I enter the code?

Ok for the code below I dont understand where you set the amount of milliseconds
and also where you actually enter this code...
I tried adding this in the code like this
section of vb but it says argument not optional..........................
Public Sub DXTimer(lMilliseconds As Long)
Dim cTim As Long
cTim = DX.TickCount

Do Until DX.TickCount - cTim &gt;= lMilliseconds
DoEvents
Print "Hi"
Loop
End Sub
Private Sub Command1_Click()
DXTimer
End Sub


-----------------------------------------------------------------------------------------
Also with this code, I also dont know where to enter it
and where do you add the amount of milliseconds between frames?
I

Public DD As DirectDraw ' our directdraw reference

DirectDrawCreate ByVal 0&amp;, DD, Nothing
DD.SetCooperativeLevel Me.hWnd, DDSCL_EXCLUSIVE Or DDSCL_FULLSCREEN
DD.SetDisplayMode lXres, lYres, lColorDepth

Do

DD.WaitForVerticalBlank DDWAITVB_BLOCKBEGIN, 0&amp;
Loop Until bGameFinished
Reply With Quote
  #15  
Old 11-01-2001, 03:29 PM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default Re: Where do I enter the code?

Your problem is that you called the timer sub without passing the correct parameters. To call the sub you would use Call DXTimer(enter number here) to get it to work.

As for the second problem, check out my DirectDraw/Direct3D/DirectMusic sample code (not tutorial) in the Code Library
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #16  
Old 11-02-2001, 05:32 AM
andrewo
Guest
 
Posts: n/a
Default Re: Where do I enter the code?

still doesnt work.....................


Public Sub DXTimer(lMilliseconds As Long)
Dim cTim As Long
cTim = DX.TickCount

Do Until DX.TickCount - cTim &gt;= lMilliseconds
DoEvents
'You could make it do other stuff here, like update the screen
'Or game phyics
Print "G"
Loop
End Sub

Private Sub Command1_Click()
Call DXTimer(500)
Print "hi"
End Sub

-------------------------------------
it now says "Run-time error '424' "
"Object required"

on the line
cTim = DX.TickCount
Reply With Quote
  #17  
Old 11-02-2001, 08:07 AM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default Re: Where do I enter the code?

Thats because you havent declared DX as a DirectX object. It sounds to me like you are very new to DirectX. I would suggest checking out my tutorial(s) in Tutors Corner, the DirectMusic tutorial is the starter one that deals with setting up DirectX and its objects.
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #18  
Old 11-04-2001, 12:55 AM
andrewo
Guest
 
Posts: n/a
Default Re: Where do I enter the code?

Sorry I still got absolutely no idea even after looking at the tutorials..

Can you just right me a full example of using the timer within a program squirm
Reply With Quote
  #19  
Old 11-04-2001, 02:14 AM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default Re: Where do I enter the code?

Just place this with your variable declarations:

Dim DX As New DirectX7
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #20  
Old 11-04-2001, 03:43 AM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default Re: Where do I enter the code?

andrewo, it seems to me that you are using DirectX for the sole purpose of acting like a timer. When you first asked the question I assumed that you were already using DirectX for the graphics/sound/music, and so adding this piece of code would be simple.

However, it is clear to me now that you are not already using DirectX. In this case I would suggest using the timer API calls instead. They have a better resolution that the timer control, down to 1ms which I am sure is adequate for your purposes (DirectX cannot do any better). Referencing and declaring DirectX for the sole purpose of a timer in my opinion is a waste of system resources.

Not that I want to put you off DirectX, I just think you might be doing something which is not necessary.

[img]images/icons/smile.gif[/img]
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->