osxTime.mm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #import "platform/platform.h"
  23. #import "platformOSX/platformOSX.h"
  24. #import "platform/event.h"
  25. #import "game/gameInterface.h"
  26. #pragma mark ---- TimeManager Class Methods ----
  27. //------------------------------------------------------------------------------
  28. static void _OSXUpdateSleepTicks()
  29. {
  30. osxPlatState * platState = [osxPlatState sharedPlatState];
  31. if (platState.backgrounded)
  32. platState.sleepTicks = (U32)Platform::getBackgroundSleepTime();
  33. else
  34. platState.sleepTicks = (U32)sgTimeManagerProcessInterval;
  35. }
  36. //------------------------------------------------------------------------------
  37. // Responsible for calculating ticks and posting the TimeEvent
  38. void TimeManager::process()
  39. {
  40. osxPlatState * platState = [osxPlatState sharedPlatState];
  41. _OSXUpdateSleepTicks();
  42. U32 curTime = Platform::getRealMilliseconds(); // GTC returns Milliseconds, FYI.
  43. U32 elapsedTime = curTime - platState.lastTimeTick;
  44. if(elapsedTime <= platState.sleepTicks)
  45. {
  46. Platform::sleep(platState.sleepTicks - elapsedTime);
  47. }
  48. platState.lastTimeTick = Platform::getRealMilliseconds();
  49. TimeEvent event;
  50. event.elapsedTime = elapsedTime;
  51. Game->postEvent(event);
  52. }
  53. #pragma mark ---- Platform Namespace Time Functions ----
  54. //------------------------------------------------------------------------------
  55. // Gets the local time on an OS X device
  56. void Platform::getLocalTime(LocalTime &lt)
  57. {
  58. struct tm systime;
  59. time_t long_time;
  60. /// Get time as long integer.
  61. time(&long_time);
  62. /// Convert to local time, thread safe.
  63. localtime_r(&long_time, &systime);
  64. /// Fill the return struct
  65. lt.sec = systime.tm_sec;
  66. lt.min = systime.tm_min;
  67. lt.hour = systime.tm_hour;
  68. lt.month = systime.tm_mon;
  69. lt.monthday = systime.tm_mday;
  70. lt.weekday = systime.tm_wday;
  71. lt.year = systime.tm_year;
  72. lt.yearday = systime.tm_yday;
  73. lt.isdst = systime.tm_isdst;
  74. }
  75. //------------------------------------------------------------------------------
  76. // Gets the time in seconds since the Epoch
  77. U32 Platform::getTime()
  78. {
  79. time_t epoch_time;
  80. time(&epoch_time);
  81. return (U32)epoch_time;
  82. }
  83. //------------------------------------------------------------------------------
  84. // Gets the time in milliseconds since some epoch. In this case, the current system
  85. // absolute time. Storing milisec in a U32 overflows every 49.71 days.
  86. U32 Platform::getRealMilliseconds()
  87. {
  88. return (U32)([NSDate timeIntervalSinceReferenceDate] * 1000);
  89. }
  90. //------------------------------------------------------------------------------
  91. // Gets the running time for this app in milliseconds
  92. U32 Platform::getVirtualMilliseconds()
  93. {
  94. return [[osxPlatState sharedPlatState] currentSimTime];
  95. }
  96. //------------------------------------------------------------------------------
  97. // Moves the simulation time forward
  98. void Platform::advanceTime(U32 delta)
  99. {
  100. osxPlatState * platState = [osxPlatState sharedPlatState];
  101. platState.currentSimTime += delta;
  102. }
  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
  108. usleep(ms * 1000);
  109. }