Timer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Copyright (c) 2006-2009 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include <common/config.h>
  21. #ifdef LOVE_WINDOWS
  22. # include <windows.h>
  23. # include <time.h>
  24. #else
  25. # include <sys/time.h>
  26. #endif
  27. #include "Timer.h"
  28. namespace love
  29. {
  30. namespace timer
  31. {
  32. namespace sdl
  33. {
  34. Timer::Timer()
  35. : time_init(0), currTime(0), prevFpsUpdate(0), fps(0), fpsUpdateFrequency(1),
  36. frames(0), dt(0)
  37. {
  38. // Init the SDL timer system.
  39. if(SDL_InitSubSystem(SDL_INIT_TIMER) < 0)
  40. throw Exception(SDL_GetError());
  41. }
  42. Timer::~Timer()
  43. {
  44. // Quit SDL timer.
  45. SDL_QuitSubSystem(SDL_INIT_TIMER);
  46. }
  47. const char * Timer::getName() const
  48. {
  49. return "love.timer.sdl";
  50. }
  51. void Timer::step()
  52. {
  53. // Frames rendered
  54. frames++;
  55. // "Current" time is previous time by now.
  56. prevTime = currTime;
  57. // Get ticks from SDL
  58. currTime = SDL_GetTicks();
  59. // Convert to number of seconds
  60. dt = (currTime - prevTime)/1000.0f;
  61. // Update FPS?
  62. if((currTime - prevFpsUpdate)/1000.0f > fpsUpdateFrequency)
  63. {
  64. fps = frames/fpsUpdateFrequency;
  65. prevFpsUpdate = currTime;
  66. frames = 0;
  67. }
  68. }
  69. void Timer::sleep(int ms)
  70. {
  71. if(ms > 0)
  72. SDL_Delay(ms);
  73. }
  74. float Timer::getDelta() const
  75. {
  76. return dt;
  77. }
  78. float Timer::getFPS() const
  79. {
  80. return fps;
  81. }
  82. float Timer::getTime() const
  83. {
  84. return (SDL_GetTicks() - time_init)/1000.0f;
  85. }
  86. float Timer::getMicroTime() const
  87. {
  88. #ifdef LOVE_WINDOWS
  89. __int64 ticks, freq;
  90. LARGE_INTEGER temp;
  91. QueryPerformanceCounter(&temp);
  92. ticks = temp.QuadPart;
  93. QueryPerformanceFrequency(&temp);
  94. freq = temp.QuadPart;
  95. __int64 secs = ticks/freq;
  96. __int64 usecs = static_cast<__int64>((ticks%freq)/(freq/1000000.0f));
  97. return secs%86400 + usecs/1000000.0f;
  98. #else
  99. timeval t;
  100. gettimeofday(&t, NULL);
  101. return t.tv_sec%86400 + t.tv_usec/1000000.0f;
  102. #endif
  103. }
  104. } // sdl
  105. } // timer
  106. } // love