AndroidTime.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <unistd.h>
  25. //--------------------------------------
  26. void Platform::getLocalTime(LocalTime &lt)
  27. {
  28. struct tm systime;
  29. time_t long_time;
  30. /// Get time as long integer.
  31. time( &long_time );
  32. /// Convert to local time, thread safe.
  33. localtime_r( &long_time, &systime );
  34. /// Fill the return struct
  35. lt.sec = systime.tm_sec;
  36. lt.min = systime.tm_min;
  37. lt.hour = systime.tm_hour;
  38. lt.month = systime.tm_mon;
  39. lt.monthday = systime.tm_mday;
  40. lt.weekday = systime.tm_wday;
  41. lt.year = systime.tm_year;
  42. lt.yearday = systime.tm_yday;
  43. lt.isdst = systime.tm_isdst;
  44. }
  45. /// Gets the time in seconds since the Epoch
  46. U32 Platform::getTime()
  47. {
  48. time_t epoch_time;
  49. time( &epoch_time );
  50. return epoch_time;
  51. }
  52. static double absolute_to_seconds;
  53. static double seconds_to_absolute;
  54. static double absolute_to_millis;
  55. static double millis_to_absolute;
  56. /// Gets the time in milliseconds since some epoch. In this case, system start time.
  57. /// Storing milisec in a U32 overflows every 49.71 days
  58. U32 Platform::getRealMilliseconds()
  59. {
  60. struct timeval tv;
  61. gettimeofday(&tv, NULL);
  62. return ((tv.tv_sec) * 1000.0 + (tv.tv_usec) / 1000.0);
  63. }
  64. U32 Platform::getVirtualMilliseconds()
  65. {
  66. return platState.currentTime;
  67. }
  68. void Platform::advanceTime(U32 delta)
  69. {
  70. platState.currentTime += delta;
  71. }
  72. /// Asks the operating system to put the process to sleep for at least ms milliseconds
  73. void Platform::sleep(U32 ms)
  74. {
  75. // note: this will overflow if you want to sleep for more than 49 days. just so ye know.
  76. //Luma: Re-enable sleeping... why was it commented out? No sense in that!
  77. usleep( ms * 1000 );
  78. }
  79. #pragma mark ---- TimeManager ----
  80. //--------------------------------------
  81. static void _AndroidUpdateSleepTicks()
  82. {
  83. if (platState.backgrounded)
  84. platState.sleepTicks = Platform::getBackgroundSleepTime();
  85. else
  86. platState.sleepTicks = sgTimeManagerProcessInterval;
  87. }
  88. //--------------------------------------
  89. void TimeManager::process()
  90. {
  91. _AndroidUpdateSleepTicks();
  92. U32 curTime = Platform::getRealMilliseconds(); // GTC returns Milliseconds, FYI.
  93. S32 elapsedTime = curTime - platState.lastTimeTick;
  94. if(elapsedTime <= platState.sleepTicks)
  95. {
  96. Platform::sleep(platState.sleepTicks - elapsedTime);
  97. }
  98. platState.lastTimeTick = Platform::getRealMilliseconds();
  99. TimeEvent event;
  100. event.elapsedTime = elapsedTime;
  101. Game->postEvent(event);
  102. }