SHARE
TWEET

Skitchin

a guest Dec 23rd, 2009 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**************************************************************************
  2. ***************************************************************************
  3.         GameTimer.h
  4.  
  5.         The game timer class is responsible for managing our frame rate and
  6.         ensuring that the game will be ran at a constant playable speed on
  7.         various machines.
  8. ***************************************************************************
  9. ***************************************************************************/
  10. #pragma once
  11.  
  12. #include <time.h>
  13. #include <windows.h>
  14.  
  15. class GameTimer
  16. {
  17. private:
  18.         bool isEnabled;
  19.         clock_t starttime;
  20.         clock_t elapsed;
  21.  
  22. public:
  23.         GameTimer( );
  24.         ~GameTimer( );
  25.         bool getEnabled( );
  26.         int getMSElapsed( );
  27.         int getSecElapsed( );
  28.         void restart( );
  29.         void start( );
  30.         void stop( );
  31.         void waitMS( int milliseconds );
  32.         void waitSec( int seconds );
  33. };
  34.  
  35.  
  36. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  37.  
  38.  
  39. /**************************************************************************
  40. ***************************************************************************
  41.         GameTimer.cpp
  42.  
  43.         The game timer class is responsible for managing our frame rate and
  44.         ensuring that the game will be ran at a constant playable speed on
  45.         various machines.
  46. ***************************************************************************
  47. ***************************************************************************/
  48. #pragma once
  49.  
  50. #include "stdafx.h"
  51. #include "GameTimer.H"
  52.  
  53. GameTimer::GameTimer()
  54. {
  55.         stop( );
  56. };
  57.  
  58. bool GameTimer::getEnabled ( )
  59. {
  60.         return isEnabled;
  61. }
  62.  
  63. int GameTimer::getMSElapsed( )
  64. {
  65.         if( starttime > clock( ) || starttime < 0 )
  66.         {
  67.                 this->restart( ); //Prevent wrapping and negative values
  68.                 return 0;
  69.         }
  70.  
  71.         if( isEnabled == false )
  72.         {
  73.                 return 0;
  74.         }
  75.         else
  76.         {
  77.                 this->elapsed = ( clock( ) - starttime ) / ( CLOCKS_PER_SEC / 1000 );
  78.                 return elapsed;
  79.         }
  80. };
  81.  
  82. int GameTimer::getSecElapsed( )
  83. {
  84.         if( starttime > clock( ) || starttime < 0 )
  85.         {
  86.                 this->restart( ); //Prevent wrapping and negative values
  87.                 return 0;
  88.         }
  89.  
  90.         if( isEnabled == false )
  91.         {
  92.                 return 0;
  93.         }
  94.         else
  95.         {
  96.                 elapsed = ( clock( ) - starttime ) / CLOCKS_PER_SEC;
  97.                 return elapsed;
  98.         }
  99.        
  100. };
  101.  
  102. void GameTimer::restart( )
  103. {
  104.         starttime = clock( );
  105. };
  106.  
  107. void GameTimer::start( )
  108. {
  109.         if ( isEnabled == false )
  110.         {
  111.                 isEnabled = true;
  112.                 starttime = clock( );
  113.         }
  114. };
  115.  
  116. void GameTimer::stop( )
  117. {
  118.         isEnabled = false;
  119.         starttime = 0;
  120. };
  121.  
  122. void GameTimer::waitMS( int milliseconds )
  123. {
  124.         /*clock_t endwait;
  125.         endwait = clock( ) + ( milliseconds * ( CLOCKS_PER_SEC / 1000 ) );
  126.         while( clock( ) < endwait) {}*/
  127.         // Maintain above for reference
  128.         Sleep( milliseconds );
  129. };
  130.  
  131. void GameTimer::waitSec( int seconds )
  132. {
  133.         /*clock_t endwait;
  134.         endwait = clock( ) + seconds * CLOCKS_PER_SEC;
  135.         while( clock( ) < endwait) { }*/
  136.         // Maintain above for reference
  137.         Sleep( seconds * 1000 );
  138. };
  139.  
  140. GameTimer::~GameTimer( )
  141. {
  142.         //
  143. };
RAW Paste Data
Top