EmscriptenTime.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. // Portions Copyright (c) 2014 James S Urquhart
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "platformEmscripten/platformEmscripten.h"
  24. #include "game/gameInterface.h"
  25. #include <SDL/SDL.h>
  26. #include <unistd.h>
  27. //--------------------------------------
  28. void Platform::getLocalTime(LocalTime &lt)
  29. {
  30. // TODO
  31. dMemset(&lt, '\0', sizeof(LocalTime));
  32. }
  33. /// Gets the time in seconds since the Epoch
  34. U32 Platform::getTime()
  35. {
  36. // TODO
  37. return SDL_GetTicks() / 1000;
  38. }
  39. /// Gets the time in milliseconds since some epoch. In this case, system start time.
  40. /// Storing milisec in a U32 overflows every 49.71 days
  41. U32 Platform::getRealMilliseconds()
  42. {
  43. return SDL_GetTicks();
  44. }
  45. U32 Platform::getVirtualMilliseconds()
  46. {
  47. return gPlatState.currentTime;
  48. }
  49. void Platform::advanceTime(U32 delta)
  50. {
  51. gPlatState.currentTime += delta;
  52. }
  53. void Platform::sleep(U32 ms)
  54. {
  55. usleep( ms * 1000 );
  56. }
  57. #pragma mark ---- TimeManager ----
  58. //--------------------------------------
  59. void TimeManager::process()
  60. {
  61. if (gPlatState.backgrounded)
  62. gPlatState.sleepTicks = Platform::getBackgroundSleepTime();
  63. else
  64. gPlatState.sleepTicks = sgTimeManagerProcessInterval;
  65. U32 curTime = Platform::getRealMilliseconds(); // GTC returns Milliseconds, FYI.
  66. S32 elapsedTime = curTime - gPlatState.lastTimeTick;
  67. if(elapsedTime <= gPlatState.sleepTicks)
  68. {
  69. Platform::sleep(gPlatState.sleepTicks - elapsedTime);
  70. }
  71. gPlatState.lastTimeTick = Platform::getRealMilliseconds();
  72. TimeEvent event;
  73. event.elapsedTime = elapsedTime;
  74. Game->postEvent(event);
  75. }