JavaScript EditorFree JavaScript Editor     Ajax Editor 



Main Page
  Previous Section Next Section

More on Timing and Synchronization

In most of the programs thus far, I've been using a very cheesy timing system—Sleep(). That's about as low-tech as you can get <BG>. In real life, you need to lock your game to some frame rate, such as 30fps. A better way to achieve this lock is to start a timer at the top of the game loop (or take note of the current time) and then, at the end of the game loop, test if the amount of time for 30 fps—that is, 1/30 seconds—has elapsed. If so, continue to the next frame. If not, wait until the time has elapsed (maybe work on the next frame or do some housekeeping so as not to waste cycles).

In computer code, you would structure your Game_Main() something like this:

DWORD Get_Clock(void);
DWORD Start_Clock(void);
DWORD Wait_Clock(DWORD count);


int Game_Main(void *parms = NULL, int num_parms = 0)
{
// this is called each frame


// get the current time in milliseconds since windows
// was started
Get_Clock();

// do work...

// sync to frame rate, 30 fps in this case
Wait_Clock(30);

} // end Game_Mains

Simple as that! Oh yeah… what about these phantom functions? Ahhh yes, here they are—based on Win32 timing functions:

DWORD Get_Clock(void)
{
// this function returns the current tick count

// return time
return(GetTickCount());

} // end Get_Clock

///////////////////////////////////////////////////////////

DWORD Start_Clock(void)
{
// this function starts the clock, that is, saves the current
// count, use in conjunction with Wait_Clock()
return(start_clock_count = Get_Clock());

} // end Start_Clock

////////////////////////////////////////////////////////////

DWORD Wait_Clock(DWORD count)
{
// this function is used to wait for a specific number of clicks
// since the call to Start_Clock

while((Get_Clock() - start_clock_count) < count);
return(Get_Clock());

} // end Wait_Clock

Notice that they're based on the Win32 function GetTickCount(), which returns a DWORD equal to the number of milliseconds elapsed since Windows was started. Hence, the time is relative, but who cares? That's all you need. Also, notice the use of the global start_clock_count to store the starting time. It's set every time Get_Clock() is called and is defined like this in the library:

DWORD start_clock_count = 0;     // used for timing

C++

This is a perfect place for a C++ class; feel free.


DirectX also has a vertical blank detection function that you can use to determine the state of the electron gun as it renders the image on the CRT. The IDIRECTDRAW4 interface supports a function called WaitForVerticalBlank(), as shown here:

HRESULT WaitForVerticalBlank(DWORD dwFlags,
                             HANDLE hEvent);

You can use it to determine the various states of the vertical blank. dwFlags controls the operation of the function, and hEvent is a handle to a Win32 event (advanced stuff). Take a look at Table 8.3 for the valid flag settings.

Table 8.3. Flag Settings for WaitForVerticalBlank()
Flag Description
DDWAITVB_BLOCKBEGIN Returns when the vertical-blank interval begins.
DDWAITVB_BLOCKEND Returns when the vertical-blank interval ends and the display begins.

      Previous Section Next Section
    



    JavaScript EditorAjax Editor     JavaScript Editor