AndroidTime.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platformAndroid/platformAndroid.h"
  23. #include "game/gameInterface.h"
  24. static double startupTime = 0;
  25. void android_StartupTime()
  26. {
  27. struct timeval tv;
  28. gettimeofday(&tv, NULL);
  29. startupTime = ((tv.tv_sec) * 1000.0 + (tv.tv_usec) / 1000.0);
  30. }
  31. #include <unistd.h>
  32. //--------------------------------------
  33. void Platform::getLocalTime(LocalTime &lt)
  34. {
  35. struct tm systime;
  36. time_t long_time;
  37. /// Get time as long integer.
  38. time( &long_time );
  39. /// Convert to local time, thread safe.
  40. localtime_r( &long_time, &systime );
  41. /// Fill the return struct
  42. lt.sec = systime.tm_sec;
  43. lt.min = systime.tm_min;
  44. lt.hour = systime.tm_hour;
  45. lt.month = systime.tm_mon;
  46. lt.monthday = systime.tm_mday;
  47. lt.weekday = systime.tm_wday;
  48. lt.year = systime.tm_year;
  49. lt.yearday = systime.tm_yday;
  50. lt.isdst = systime.tm_isdst;
  51. }
  52. /// Gets the time in seconds since the Epoch
  53. U32 Platform::getTime()
  54. {
  55. time_t epoch_time;
  56. time( &epoch_time );
  57. return epoch_time;
  58. }
  59. static double absolute_to_seconds;
  60. static double seconds_to_absolute;
  61. static double absolute_to_millis;
  62. static double millis_to_absolute;
  63. /// Gets the time in milliseconds since some epoch. In this case, system start time.
  64. /// Storing milisec in a U32 overflows every 49.71 days
  65. U32 Platform::getRealMilliseconds()
  66. {
  67. struct timeval tv;
  68. gettimeofday(&tv, NULL);
  69. double ret = ((tv.tv_sec) * 1000.0 + (tv.tv_usec) / 1000.0) - startupTime;
  70. return (U32)ret;
  71. }
  72. U32 Platform::getVirtualMilliseconds()
  73. {
  74. return platState.currentTime;
  75. }
  76. void Platform::advanceTime(U32 delta)
  77. {
  78. platState.currentTime += delta;
  79. }
  80. /// Asks the operating system to put the process to sleep for at least ms milliseconds
  81. void Platform::sleep(U32 ms)
  82. {
  83. // note: this will overflow if you want to sleep for more than 49 days. just so ye know.
  84. //Luma: Re-enable sleeping... why was it commented out? No sense in that!
  85. usleep( ms * 1000 );
  86. }
  87. #pragma mark ---- TimeManager ----
  88. //--------------------------------------
  89. static void _AndroidUpdateSleepTicks()
  90. {
  91. if (platState.backgrounded)
  92. platState.sleepTicks = Platform::getBackgroundSleepTime();
  93. else
  94. platState.sleepTicks = sgTimeManagerProcessInterval;
  95. }
  96. //--------------------------------------
  97. void TimeManager::process()
  98. {
  99. _AndroidUpdateSleepTicks();
  100. U32 curTime = Platform::getRealMilliseconds(); // GTC returns Milliseconds, FYI.
  101. S32 elapsedTime = curTime - platState.lastTimeTick;
  102. if(elapsedTime <= platState.sleepTicks)
  103. {
  104. Platform::sleep(platState.sleepTicks - elapsedTime);
  105. }
  106. platState.lastTimeTick = Platform::getRealMilliseconds();
  107. TimeEvent event;
  108. event.elapsedTime = elapsedTime;
  109. Game->postEvent(event);
  110. }