home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / directx2 / sdk / samples / iklowns / cgtimer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-28  |  3.3 KB  |  142 lines

  1. /*===========================================================================*\
  2. |
  3. |  File:        cgtimer.cpp
  4. |
  5. |  Description: 
  6. |       
  7. |-----------------------------------------------------------------------------
  8. |
  9. |  Copyright (C) 1995-1996 Microsoft Corporation.  All Rights Reserved.
  10. |
  11. |  Written by Moss Bay Engineering, Inc. under contract to Microsoft Corporation
  12. |
  13. \*===========================================================================*/
  14.  
  15. /**************************************************************************
  16.  
  17.     (C) Copyright 1995-1996 Microsoft Corp.  All rights reserved.
  18.  
  19.     You have a royalty-free right to use, modify, reproduce and 
  20.     distribute the Sample Files (and/or any modified version) in 
  21.     any way you find useful, provided that you agree that 
  22.     Microsoft has no warranty obligations or liability for any 
  23.     Sample Application Files which are modified. 
  24.  
  25.     we do not recomend you base your game on IKlowns, start with one of
  26.     the other simpler sample apps in the GDK
  27.  
  28.  **************************************************************************/
  29.  
  30. #include <windows.h>
  31. #include "CGTimer.h"
  32.  
  33. static void TimerThread(void);
  34. extern CGameTimer * Timer;
  35. static CRITICAL_SECTION cs;
  36.  
  37. CGameTimer::CGameTimer(void)
  38. {
  39. #if 0
  40.     DWORD id;
  41.     // initialize our critical section dude
  42.     InitializeCriticalSection(&cs);
  43.  
  44.     EnterCriticalSection(&cs);
  45.     // create our Event item
  46.     Event = CreateEvent (
  47.         NULL,
  48.         FALSE,  // auto-reset
  49.         FALSE,  // initially not-signalled
  50.         NULL
  51.         );
  52.  
  53.     // create our timer thread
  54.     Time = 0;   // start at time-slice zero
  55.     SetRate(60);    // sixty Hertz
  56.     
  57.     thread = CreateThread(
  58.         NULL,
  59.             0,
  60.         (LPTHREAD_START_ROUTINE) TimerThread,
  61.         NULL,
  62.         0,
  63.         &id
  64.         );  
  65. //  SetThreadPriority( thread, THREAD_PRIORITY_HIGHEST );
  66.     LeaveCriticalSection(&cs);
  67. #endif
  68. }
  69.  
  70. CGameTimer::~CGameTimer(void)
  71. {
  72. #if 0
  73.     // kill the thread
  74.     TerminateThread(thread,0);
  75.     // remove the Event
  76.     CloseHandle(Event);
  77.     // and the CS
  78.     DeleteCriticalSection(&cs);
  79. #endif
  80. }
  81.  
  82. static void TimerThread(void)
  83. {
  84.     long mylasttime = timeGetTime();
  85.     int x;
  86.  
  87.     while (1)
  88.     {
  89.         // wait till it's time to wake up
  90.  
  91.         x = timeGetTime() - mylasttime;
  92.         while (x < Timer->rate)
  93.         {
  94.             Sleep(0);
  95.             x = timeGetTime() - mylasttime;
  96.         }
  97.         mylasttime = timeGetTime();
  98.  
  99.         // increment our notion of time
  100.         EnterCriticalSection(&cs);
  101.         if (!Timer->paused)
  102.         {
  103.             ++Timer->Time;
  104.             SetEvent(Timer->Event);                     
  105.         }   // let the other thread go now...
  106.  
  107.         LeaveCriticalSection(&cs);
  108.     }
  109. }
  110.  
  111. void    CGameTimer::Pause(void) 
  112. {
  113. //  EnterCriticalSection(&cs);
  114.     paused = TRUE;
  115. //  LeaveCriticalSection(&cs);
  116. };
  117.     
  118. void    CGameTimer::Resume(void) 
  119. {
  120. //  EnterCriticalSection(&cs);
  121.     paused = FALSE;
  122. //  LeaveCriticalSection(&cs);
  123. };
  124.  
  125. void CGameTimer::SetRate ( int hertz )
  126. {
  127.     if (hertz)
  128.         rate = 1000 / hertz;
  129. }
  130.  
  131. int CGameTimer::GetRate (void)
  132. {
  133.     if (rate)
  134.     {
  135.         return(1000 / rate);
  136.     }
  137.     else
  138.     {
  139.         return(0);      
  140.     }   
  141. }
  142.