iOSTime.mm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "platformiOS/platformiOS.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. #import <mach/mach_time.h>
  53. static mach_timebase_info_data_t InitTimebaseInfo();
  54. static mach_timebase_info_data_t timebase_info = InitTimebaseInfo();
  55. static double absolute_to_seconds;
  56. static double seconds_to_absolute;
  57. static double absolute_to_millis;
  58. static double millis_to_absolute;
  59. mach_timebase_info_data_t InitTimebaseInfo()
  60. {
  61. mach_timebase_info_data_t timebase_info;
  62. mach_timebase_info(&timebase_info);
  63. absolute_to_seconds = timebase_info.numer / (1000000000.0 * timebase_info.denom);
  64. seconds_to_absolute = 1.0 / absolute_to_seconds;
  65. absolute_to_millis = timebase_info.numer / (1000000.0 * timebase_info.denom);
  66. millis_to_absolute = 1.0 / absolute_to_millis;
  67. return timebase_info;
  68. }
  69. /// Gets the time in milliseconds since some epoch. In this case, system start time.
  70. /// Storing milisec in a U32 overflows every 49.71 days
  71. U32 Platform::getRealMilliseconds()
  72. {
  73. // Duration is a S32 value.
  74. // if negative, it is in microseconds.
  75. // if positive, it is in milliseconds.
  76. Duration durTime = mach_absolute_time() * absolute_to_millis;
  77. U32 ret;
  78. if( durTime < 0 )
  79. ret = durTime / -1000;
  80. else
  81. ret = durTime;
  82. return ret;
  83. }
  84. U32 Platform::getVirtualMilliseconds()
  85. {
  86. return platState.currentTime;
  87. }
  88. void Platform::advanceTime(U32 delta)
  89. {
  90. platState.currentTime += delta;
  91. }
  92. /// Asks the operating system to put the process to sleep for at least ms milliseconds
  93. void Platform::sleep(U32 ms)
  94. {
  95. // note: this will overflow if you want to sleep for more than 49 days. just so ye know.
  96. //Luma: Re-enable sleeping... why was it commented out? No sense in that!
  97. usleep( ms * 1000 );
  98. }
  99. #pragma mark ---- TimeManager ----
  100. //--------------------------------------
  101. static void _iOSUpdateSleepTicks()
  102. {
  103. if (platState.backgrounded)
  104. platState.sleepTicks = Platform::getBackgroundSleepTime();
  105. else
  106. platState.sleepTicks = sgTimeManagerProcessInterval;
  107. }
  108. //--------------------------------------
  109. void TimeManager::process()
  110. {
  111. _iOSUpdateSleepTicks();
  112. U32 curTime = Platform::getRealMilliseconds(); // GTC returns Milliseconds, FYI.
  113. S32 elapsedTime = curTime - platState.lastTimeTick;
  114. if(elapsedTime <= platState.sleepTicks)
  115. {
  116. Platform::sleep(platState.sleepTicks - elapsedTime);
  117. }
  118. platState.lastTimeTick = Platform::getRealMilliseconds();
  119. TimeEvent event;
  120. event.elapsedTime = elapsedTime;
  121. Game->postEvent(event);
  122. }