on2_timer.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "on2_timer.h"
  2. #if defined(WIN32)
  3. #include <windows.h>
  4. #include <mmsystem.h>
  5. #else
  6. #include <sys/time.h>
  7. #endif
  8. #if defined( WIN32 )
  9. const unsigned long MAX_BEFORE_ROLLOVER = (1000 * 60 * 60 * 24);
  10. #else
  11. const unsigned long MAX_BEFORE_ROLLOVER = 0xFFFFFFFF;
  12. #endif
  13. // full day in milliseconds
  14. const unsigned long kDuckFullDayMilli = 86400000;
  15. void
  16. on2Timer_Init ( on2Timer* pTimer )
  17. {
  18. pTimer->elapsedMilli = 0;
  19. pTimer->baseMilli = 0;
  20. }
  21. /* The DeInit function was in danger of trying to free statically allocated timers goofy */
  22. unsigned long
  23. on2Timer_Start ( on2Timer* pTimer )
  24. {
  25. pTimer->baseMilli = on2Timer_GetCurrentTimeMilli();
  26. pTimer->elapsedMilli = 0;
  27. return pTimer->baseMilli;
  28. }
  29. unsigned long
  30. on2Timer_Stop ( on2Timer* pTimer )
  31. {
  32. unsigned long currentMilli = on2Timer_GetCurrentTimeMilli();
  33. if(currentMilli >= pTimer->baseMilli)
  34. {
  35. pTimer->elapsedMilli = currentMilli - pTimer->baseMilli;
  36. return pTimer->elapsedMilli;
  37. }
  38. // rollover condition, get milli before rollover, add to current milli
  39. // I think if there is a rollover during timing on win32 this will cause a crash
  40. // when the addition of currentMilli and rollMilli results in overflowing the size of
  41. // and unsigned long int
  42. else
  43. {
  44. unsigned long rollMilli = MAX_BEFORE_ROLLOVER - pTimer->baseMilli;
  45. pTimer->elapsedMilli = currentMilli + rollMilli;
  46. return pTimer->elapsedMilli;
  47. }
  48. }
  49. unsigned long
  50. on2Timer_GetCurrentElapsedMilli ( on2Timer* pTimer )
  51. {
  52. unsigned long currentMilli = on2Timer_GetCurrentTimeMilli();
  53. if(currentMilli >= pTimer->baseMilli)
  54. {
  55. return ( currentMilli - pTimer->baseMilli );
  56. }
  57. // rollover condition, get milli before rollover, add to current milli
  58. else
  59. {
  60. unsigned long rollMilli = MAX_BEFORE_ROLLOVER - pTimer->baseMilli;
  61. return ( currentMilli + rollMilli );
  62. }
  63. }
  64. unsigned long
  65. on2Timer_GetCurrentTimeSeconds ()
  66. {
  67. unsigned long currentMilli = on2Timer_GetCurrentTimeMilli();
  68. return currentMilli / 1000;
  69. }
  70. unsigned long
  71. on2Timer_GetCurrentTimeMilli ()
  72. {
  73. #if !defined(WIN32)
  74. unsigned long currentMilli;
  75. struct timeval tv;
  76. struct timezone tz;
  77. gettimeofday ( &tv, &tz );
  78. currentMilli = (tv.tv_sec * 1000) + (tv.tv_usec/1000);
  79. return currentMilli;
  80. #else
  81. return timeGetTime();
  82. #endif
  83. }
  84. int
  85. on2Timer_Sleep( int msec )
  86. {
  87. #ifdef _WIN32
  88. Sleep( msec );
  89. #endif
  90. return 0;
  91. }