PDA

Click to See Complete Forum and Search --> : Smooth Timers and Clearing up DirectDraw


dangerousdave
May 10th, 2001, 12:12 PM
OK, So I got two questions.
1) I need a smooth method of timing. I am animating an image and the VB timer is not very accurate, I was wondering if anyone else had a suggestion - perhaps using GetTickCount somehow? I dunno.
2) I have never been good at clearing up DC's and whatnots, preferring just to use the good 'ole "End" command and leave it at that, but with DirectDraw I'm seeing a 2ish% drop in system resources every time I run my program, presumably becuase I am not deleting my surfaces afterwards. Could someone help by telling me what needs deleting when I end my program and how to delete it (i'm not exactly well versed in DX).
Help much appreciated.

PsychoMark
May 10th, 2001, 12:43 PM
1)

Dim lTimer As Long

lTimer = GetTickCount()

Do While bRunning
If GetTickCount() - lTimer >= 10 Then
' Actions here

lTimer = GetTickCount()
End If

DoEvents
Loop

This will run the code at "'Actions here" at leat every 10 milliseconds (may vary if your loop is not fast enough, but most of the time this'll do fine)


2) In this case, DX is the DirectX object, DD the DirectDraw object and a sample surface is called DDSurface...

DD.RestoreDisplayMode
Set DDSurface = Nothing
Set DD = Nothing
Set DX = Nothing

Basically, you perform all actions which are needed to restore the system first, then set all the components to nothing in the reversed order at which you created them (in the sample, DX was created first, DD next and the DDSurface last, so they're destroyed in reverse order)...


Hope this helps...


Btw, NEVER use 'End', it's a very BAD habit... unload forms using 'Unload Form1', so they have a chance to unload properly. You might not notice any difference, but once you get more advanced in VB, the 'End' command might crash the whole program, so it's better to not get used to it now, than to re-learn it later...

Fox
May 10th, 2001, 12:45 PM
1) GetTickCount example (http://fox.acky.net/coding/api/gettickcount.zip)

2) [Just saw your post PsychoMark]
I agree, well I use a 'ShutDown' function instead, that looks like this:

Sub ShutDown()
'Release modules
mBitmap.ShutDown
mSound.ShutDown
mInput.ShutDown
mGame.ShutDown

'Release windows
Unload frmMain
Unload frmLog

'Exit
End
End Sub

PsychoMark
May 10th, 2001, 12:57 PM
I noticed that in your game, but the Shutdown function itself is not a DX or VB function, so you'll still have to build it yourself. (if anyone doesn't know what game I'm talking about, check this thread (http://www.vbforums.com/showthread.php?s=&threadid=73493)...

A final tip: as you might have seen in Fox's code, it uses multiple modules. This is good! I've only once made a game with DirectX which fitted into one module, but it becomes a mess. Try to seperate your graphics and sound code, or parts of the game (like the menu, the game itself, etc.)... if you write your code to be flexible enough, you can easily modify or extend it later...

dangerousdave
May 10th, 2001, 01:01 PM
Thanks a lot!

One question, is it good practise to have a loop going constantly with no time period interval? I've never used a constant loop before, only timers, because I always believed that loop round and round and round was bad practise.

PsychoMark
May 10th, 2001, 01:04 PM
I always use a loop with my DirectX games, because it's fullscreen and has main focus anyways. And it's much faster, a timer can't keep up with the 60-80 FPS I normally have...