AndroidTime.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. //TODO: android
  29. /*
  30. struct tm systime;
  31. time_t long_time;
  32. /// Get time as long integer.
  33. time( &long_time );
  34. /// Convert to local time, thread safe.
  35. localtime_r( &long_time, &systime );
  36. /// Fill the return struct
  37. lt.sec = systime.tm_sec;
  38. lt.min = systime.tm_min;
  39. lt.hour = systime.tm_hour;
  40. lt.month = systime.tm_mon;
  41. lt.monthday = systime.tm_mday;
  42. lt.weekday = systime.tm_wday;
  43. lt.year = systime.tm_year;
  44. lt.yearday = systime.tm_yday;
  45. lt.isdst = systime.tm_isdst;
  46. */
  47. }
  48. /// Gets the time in seconds since the Epoch
  49. U32 Platform::getTime()
  50. {
  51. time_t epoch_time;
  52. //TODO: android
  53. //time( &epoch_time );
  54. return epoch_time;
  55. }
  56. //TODO: mach?
  57. //TODO: android
  58. /*static mach_timebase_info_data_t InitTimebaseInfo();
  59. /static mach_timebase_info_data_t timebase_info = InitTimebaseInfo();
  60. */
  61. static double absolute_to_seconds;
  62. static double seconds_to_absolute;
  63. static double absolute_to_millis;
  64. static double millis_to_absolute;
  65. //TODO: android
  66. /*
  67. mach_timebase_info_data_t InitTimebaseInfo()
  68. {
  69. mach_timebase_info_data_t timebase_info;
  70. mach_timebase_info(&timebase_info);
  71. absolute_to_seconds = timebase_info.numer / (1000000000.0 * timebase_info.denom);
  72. seconds_to_absolute = 1.0 / absolute_to_seconds;
  73. absolute_to_millis = timebase_info.numer / (1000000.0 * timebase_info.denom);
  74. millis_to_absolute = 1.0 / absolute_to_millis;
  75. return timebase_info;
  76. }*/
  77. /// Gets the time in milliseconds since some epoch. In this case, system start time.
  78. /// Storing milisec in a U32 overflows every 49.71 days
  79. U32 Platform::getRealMilliseconds()
  80. {
  81. // Duration is a S32 value.
  82. // if negative, it is in microseconds.
  83. // if positive, it is in milliseconds.
  84. //TODO: android
  85. /*
  86. Duration durTime = mach_absolute_time() * absolute_to_millis;
  87. U32 ret;
  88. if( durTime < 0 )
  89. ret = durTime / -1000;
  90. else
  91. ret = durTime;
  92. return ret;
  93. */
  94. return 0;
  95. }
  96. U32 Platform::getVirtualMilliseconds()
  97. {
  98. return platState.currentTime;
  99. }
  100. void Platform::advanceTime(U32 delta)
  101. {
  102. platState.currentTime += delta;
  103. }
  104. /// Asks the operating system to put the process to sleep for at least ms milliseconds
  105. void Platform::sleep(U32 ms)
  106. {
  107. // note: this will overflow if you want to sleep for more than 49 days. just so ye know.
  108. //Luma: Re-enable sleeping... why was it commented out? No sense in that!
  109. usleep( ms * 1000 );
  110. }
  111. #pragma mark ---- TimeManager ----
  112. //--------------------------------------
  113. static void _AndroidUpdateSleepTicks()
  114. {
  115. if (platState.backgrounded)
  116. platState.sleepTicks = Platform::getBackgroundSleepTime();
  117. else
  118. platState.sleepTicks = sgTimeManagerProcessInterval;
  119. }
  120. //--------------------------------------
  121. void TimeManager::process()
  122. {
  123. _AndroidUpdateSleepTicks();
  124. U32 curTime = Platform::getRealMilliseconds(); // GTC returns Milliseconds, FYI.
  125. S32 elapsedTime = curTime - platState.lastTimeTick;
  126. if(elapsedTime <= platState.sleepTicks)
  127. {
  128. Platform::sleep(platState.sleepTicks - elapsedTime);
  129. }
  130. platState.lastTimeTick = Platform::getRealMilliseconds();
  131. TimeEvent event;
  132. event.elapsedTime = elapsedTime;
  133. Game->postEvent(event);
  134. }