Tuesday April 16, 2024
 home | contact | Support | DirectX.. Its doing all the hard work for us...

PrtX. Timing is important...
by bkenwright@xbdev.net

Well now, we need a way of keeping track of time!  Knowing how many seconds or milliseconds has passed... and this is what where going to show you how to do. (download full source code).

1 second, 5 seconds, 0.1 second etc,... you need some way of keeping track of time... especially when it comes to things like animation.  The simplest and probably easiest way to keep track of time is to use the GetTickCount() function which returns how many milli-seconds has elapsed since the computer has started.

 DWORD GetTickCount(VOID);

To use the GetTickCount() in our code, we simply keep track of its previous value, and then we compare it to the current GetTickCount() the difference between them tells us how many milliseconds has passed since we called it last.

The program isn't much to look at but its very simple and you should easily understand how I'm using the GetTickCount() function and how you could adapt it easily for times of 0.1, 0.01 seconds.. etc.

 Well if you watch the program long enough you'll see the triangle move up a little bit each 2 seconds!  You could change the delay time to 0.1 and watch the triangle just shoot of the screen...lol.

Here's the juicy piece of code to show you timing...

 /***************************************************************************/ /*                                                                         */ /* File: game.cpp                                                          */ /*                                                                         */ /***************************************************************************/   #include #include   // Okay now, this function will only return true if it is called and at least // two seconds has passed, else it will return false.   bool DelayTime() {       static DWORD prev_time = 0;         // Convert our GetTickCount value to seconds instead of milliseconds.       DWORD cur_time = 0.001 * GetTickCount();         if(prev_time < cur_time)       {             // If where in here 2 seconds has passed. Change the 2 to 0.5 etc for a             // a less time etc.             prev_time = cur_time + 2;             return true;       }         // If where here 2 seconds hasn't passed!       return false; }     void draw_triangle(float x, float y, float z) {       struct my_vertex       {             FLOAT x, y, z;  // D3DFVF_XYZ             DWORD colour;   // D3DFVF_DIFFUSE       };       my_vertex v[] =       {   // Three points make up the face of the triangle.             {-0.5f + x, -0.5f + y, 0.0f +z, 0xffff0000},  //[1]             { 0.0f + x,  0.5f + y, 0.0f +z, 0xff00222f},  //[2]             { 0.5f + x, -0.5f + y, 0.0f +z, 0xff0aff00}   //[3]       };       UINT my_vertex_description = (D3DFVF_XYZ | D3DFVF_DIFFUSE);         IDirect3DVertexBuffer8 * DX_vb;       g_pD3DDevice->CreateVertexBuffer( 3*sizeof(my_vertex), 0, my_vertex_description, D3DPOOL_MANAGED, &DX_vb );         // Copy our array which is in computer memory over to the directX memory.. using that pointer we       // just created etc.       unsigned char *temp_pointer_vb;       DX_vb->Lock(0,0, &temp_pointer_vb, 0);       memcpy(temp_pointer_vb, v, sizeof(v) );       DX_vb->Unlock();         g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);       g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);       g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_DIFFUSE);         // Draw our triangle.       g_pD3DDevice->SetStreamSource(0, DX_vb, sizeof(my_vertex));       g_pD3DDevice->SetVertexShader(my_vertex_description);       g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);         DX_vb->Release(); }       // Well if we need to do any drawing or rendering triangles we can do it in here! void Render() {       if(!g_pD3DDevice)return;         // Clear the back buffer to a blue color       g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );         float static move = 0;       // If 2 seconds has passed DelayTime will return true! and so our triangle will move a little bit       // up.       if( DelayTime() )             move = move + 0.1;         draw_triangle(0.0f, move, 0.0f);         // After rendering the scene we display it.       g_pD3DDevice->Present( NULL, NULL, NULL, NULL ); }   void mainloop() {       Render(); }

Well its not the greatest piece of code!  But its simple and you should be able to follow what is happening easily... my mission is accomplished :)

Once you start to become familiar with your code etc, I think it would be better if you created a CTimer class with a static member variable that keeps track of all the CTimer instances there are, ... and you simply call its member variable UpdateAll() once for all the CTimer instances.