Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. --------------------------------------------------------------------------------
  2. ////////////////////////////////SERVER//////////////////////////////////////////
  3. --------------------------------------------------------------------------------
  4. ///ServerGameLogic.h\\\
  5. float       gameStartTime_;
  6. __int64     gameStartUtcTime_;
  7. __int64     GetUtcGameTime();
  8.  
  9. ///ServerGameLogic.cpp\\\
  10. void ServerGameLogic::Init(const GBGameInfo& ginfo, uint32_t creatorID) //při spuštění serveru
  11. {
  12.     //blablabla
  13.  
  14.     //zde server inicializuje čas (reálný?)
  15.     // init game time
  16.     gameStartTime_     = r3dGetTime(); // - nevim přesně co dělá r3dGetTime() - tuším, že měří čas od dané inicializace
  17.    
  18.     __int64 utcTime = GetUtcGameTime();
  19.     struct tm* tm = _gmtime64(&utcTime); // - struct tm - struktura pro lepší práci s časem - převod na hodiny, minuty, atd.
  20.     r3d_assert(tm);
  21.  
  22.     char buf[128];
  23.     asctime_s(buf, sizeof(buf), tm);
  24.     r3dOutToLog("Server time is %s", buf); // - vypíše tm ve formátu DEN MĚSÍC DEN (DATUM) HH:MM:SS ROK -> obsahuje převod času
  25.    
  26.     //blablabla inicializace serveru - nezajímavé věci
  27.  
  28.     return;
  29. }
  30. ///////////////////////////////
  31. část paketu PKT_C2S_JoinGameReq
  32. PKT_S2C_JoinGameAns_s n;
  33. //blablabla
  34. n.gameTime     = GetUtcGameTime(); // - posíláme klientovi informaci o čase
  35. //blablabla
  36.  
  37. ///////////////////////////////
  38. __int64 ServerGameLogic::GetUtcGameTime()
  39. {
  40.     // "world time start" offset, so gametime at 1st sep 2012 will be in 2018 range
  41.     struct tm toff = {0};
  42.     toff.tm_year   = 2011-1900;
  43.     toff.tm_mon    = 6;
  44.     toff.tm_mday   = 1;
  45.     toff.tm_isdst  = -1; // A value less than zero to have the C run-time library code compute whether standard time or daylight saving time is in effect.
  46.     __int64 secs0 = _mkgmtime64(&toff); // world start time
  47.     __int64 secs1 = _time64(&secs1);    // current UTC time
  48.  
  49.     // reassemble time, with speedup factor
  50.     return secs0 + (secs1 - secs0) * (__int64)GPP_Data.c_iGameTimeCompression;
  51. }
  52. --------------------------------------------------------------------------------
  53. ////////////////////////////////CLIENT//////////////////////////////////////////
  54. --------------------------------------------------------------------------------
  55. ///ClientGameLogic.h\\\
  56. __int64     gameStartUtcTime_;
  57. float       gameStartTime_;
  58. __int64     GetServerGameTime() const;
  59. void        UpdateTimeOfDay();
  60.  
  61. ///ClientGameLogic.cpp\\\
  62. část paketu PKT_C2S_JoinGameAns
  63. //blablabla
  64. gameStartUtcTime_ = n.gameTime;
  65. gameStartTime_    = r3dGetTime();
  66. UpdateTimeOfDay();
  67. //blablabla
  68.  
  69. __int64 ClientGameLogic::GetServerGameTime() const
  70. {
  71.     float secs = (r3dGetTime() - gameStartTime_) * (float)GPP->c_iGameTimeCompression;
  72.     __int64 gameUtcTime = gameStartUtcTime_ + __int64(secs);
  73.     return gameUtcTime;
  74. }
  75.  
  76. void ClientGameLogic::UpdateTimeOfDay()
  77. {
  78.     //blablabla
  79.     __int64 gameUtcTime = GetServerGameTime();
  80.     struct tm* tm = _gmtime64(&gameUtcTime);
  81.     r3d_assert(tm);
  82.  
  83.     r3dGameLevel::Environment.__CurTime  = (float)tm->tm_hour + (float)tm->tm_min / 59.0f;
  84.     //do not add seconds, much shadow flicker
  85.     //r3dGameLevel::Environment.__CurTime += (float)tm->tm_sec / (59.0f * 59.0f);
  86.    
  87.     // reset shadow cache if environment time is changed
  88.     if(fabs(r3dGameLevel::Environment.__CurTime - lastShadowCacheReset_) > 0.01f)
  89.     {
  90.         //r3dOutToLog("Server time: %f %d %d %d %d:%d\n", r3dGameLevel::Environment.__CurTime, tm->tm_mon, tm->tm_mday, 1900 + tm->tm_year, tm->tm_hour, tm->tm_min);
  91.         lastShadowCacheReset_ = r3dGameLevel::Environment.__CurTime;
  92.         ResetShadowCache();
  93.     }
  94. }
  95.  
  96. čas je zobrazován v ESC menu
  97. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  98. tj. uvedeno v HUDPause.cpp jako funkce SetTime(__int64 utcTime) - dosazujeme gameUtcTime tedy GetServerGameTime()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement