home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Issue 2 / Freelog_HS_3_Setp_Oct_Nov_2000_CD2.mdx / Arcade / Orbit / src / timer.c < prev    next >
C/C++ Source or Header  |  1999-08-19  |  2KB  |  95 lines

  1. /*
  2.  
  3. ORBIT, a freeware space combat simulator
  4. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20. */
  21.  
  22. #include "orbit.h"
  23.  
  24. double Time ()
  25. {
  26.     static double oldtime=0.0;
  27.     double newtime, elapsed;
  28.  
  29.     newtime = glutGet (GLUT_ELAPSED_TIME);
  30.     elapsed = newtime - oldtime;
  31.     oldtime = newtime;
  32.  
  33.     return (elapsed/1000.0);
  34. }
  35.  
  36. InitTimer()
  37. /*
  38.  *  Check for and initialize the performance timer
  39.  */
  40. {
  41. #ifdef WIN32
  42.     /* This gives a compiler warning; not completely sure why */
  43.     if (!QueryPerformanceFrequency (&ticks_per_sec)) exit (0);
  44. #endif
  45.     last_ticks = 0;
  46. }
  47.  
  48. DeltaTime()
  49. /*
  50.  *  Figure out how much time has elapsed since the last time we were called
  51.  */
  52. {
  53. #ifdef WIN32
  54.     _int64 ticks, elapsed;
  55. #else
  56.     int ticks, elapsed;
  57. #endif
  58.  
  59.     if (paused)
  60.     {
  61.         deltaT = 0.0;
  62.         return 0;
  63.     }
  64.  
  65. #ifdef WIN32
  66.     /* This gives a compiler warning; not completely sure why */
  67.     QueryPerformanceCounter (&ticks);
  68. #else
  69.     ticks = glutGet (GLUT_ELAPSED_TIME);
  70. #endif
  71.  
  72.     if (last_ticks == 0)
  73.     {
  74.         deltaT = 0.0;
  75.     }
  76.     else
  77.     {
  78.         elapsed = ticks - last_ticks;
  79. #ifdef WIN32
  80.         deltaT = ((double) elapsed) / ((double) ticks_per_sec);
  81. #else
  82.         deltaT = ((double) elapsed) / 1000.0;
  83. #endif
  84.     }
  85.  
  86.     /* If more than maximum, slow things down */
  87.     if (deltaT > MAXDELTAT) deltaT = MAXDELTAT;
  88.  
  89.     absT += deltaT;
  90.     last_ticks = ticks;
  91.  
  92.     return 0;
  93. }
  94.  
  95.