replace global function GetTime() with class function C4TimeMilliseconds::Now()

stable-5.4
Tobias Zwick 2013-12-07 21:27:01 +07:00
parent 3c4ba7f494
commit fe2a400c02
27 changed files with 111 additions and 104 deletions

View File

@ -108,6 +108,4 @@ inline void operator delete(void *p, const char *, long)
#include "C4Game.h" #include "C4Game.h"
#include "C4TimeMilliseconds.h"
#endif // INC_C4Include #endif // INC_C4Include

View File

@ -362,7 +362,7 @@ bool C4PlayerControlAssignment::IsComboMatched(const C4PlayerControlRecentKeyLis
// check if combo is currently fulfilled (assuming TriggerKey is already matched) // check if combo is currently fulfilled (assuming TriggerKey is already matched)
if (fComboIsSequence) if (fComboIsSequence)
{ {
C4TimeMilliseconds tKeyLast = GetTime(); C4TimeMilliseconds tKeyLast = C4TimeMilliseconds::Now();
// combo is a sequence: The last keys of RecentKeys must match the sequence // combo is a sequence: The last keys of RecentKeys must match the sequence
// the last ComboKey is the TriggerKey, which is omitted because it has already been matched and is not to be found in RecentKeys yet // the last ComboKey is the TriggerKey, which is omitted because it has already been matched and is not to be found in RecentKeys yet
KeyComboVec::const_reverse_iterator i = KeyCombo.rbegin()+1; KeyComboVec::const_reverse_iterator i = KeyCombo.rbegin()+1;
@ -1038,7 +1038,7 @@ bool C4PlayerControl::ProcessKeyDown(const C4KeyCodeEx &pressed_key, const C4Key
{ {
// add key to local "down" list if it's not already in there // add key to local "down" list if it's not already in there
// except for some mouse events for which a down state does not make sense // except for some mouse events for which a down state does not make sense
C4PlayerControlRecentKey RKey(pressed_key,matched_key,GetTime()); C4PlayerControlRecentKey RKey(pressed_key,matched_key,C4TimeMilliseconds::Now());
if (!Key_IsMouse(pressed_key.Key) || Inside<uint8_t>(Key_GetMouseEvent(pressed_key.Key), KEY_MOUSE_Button1, KEY_MOUSE_ButtonMax)) if (!Key_IsMouse(pressed_key.Key) || Inside<uint8_t>(Key_GetMouseEvent(pressed_key.Key), KEY_MOUSE_Button1, KEY_MOUSE_ButtonMax))
{ {
if (std::find(DownKeys.begin(), DownKeys.end(), pressed_key) == DownKeys.end()) DownKeys.push_back(RKey); if (std::find(DownKeys.begin(), DownKeys.end(), pressed_key) == DownKeys.end()) DownKeys.push_back(RKey);
@ -1288,7 +1288,7 @@ void C4PlayerControl::Execute()
} }
} }
// cleanup old recent keys // cleanup old recent keys
C4TimeMilliseconds tNow = GetTime(); C4TimeMilliseconds tNow = C4TimeMilliseconds::Now();
C4PlayerControlRecentKeyList::iterator irk; C4PlayerControlRecentKeyList::iterator irk;
for (irk = RecentKeys.begin(); irk != RecentKeys.end(); ++irk) for (irk = RecentKeys.begin(); irk != RecentKeys.end(); ++irk)
{ {

View File

@ -868,7 +868,7 @@ bool C4ApplicationGameTimer::Execute(int iTimeout, pollfd *)
{ {
// Check timer and reset // Check timer and reset
if (!CheckAndReset()) return true; if (!CheckAndReset()) return true;
C4TimeMilliseconds tNow = GetTime(); C4TimeMilliseconds tNow = C4TimeMilliseconds::Now();
// Execute // Execute
if (tNow >= tLastGameTick + iGameTickDelay || Game.GameGo) if (tNow >= tLastGameTick + iGameTickDelay || Game.GameGo)
{ {

View File

@ -1479,7 +1479,7 @@ static long FnGetTime(C4PropList * _this)
{ {
// check network, record, etc // check network, record, etc
if (::Control.SyncMode()) return 0; if (::Control.SyncMode()) return 0;
return GetTime().AsInt(); return C4TimeMilliseconds::Now().AsInt();
} }
static C4Value FnSetPlrExtraData(C4PropList * _this, int iPlayer, C4String * DataName, const C4Value & Data) static C4Value FnSetPlrExtraData(C4PropList * _this, int iPlayer, C4String * DataName, const C4Value & Data)

View File

@ -539,7 +539,7 @@ namespace C4GUI
public: public:
WoodenLabel(const char *szLblText, const C4Rect &rcBounds, DWORD dwFClr=0xffffffff, CStdFont *pFont=NULL, int32_t iAlign=ACenter, bool fMarkup=true) // ctor WoodenLabel(const char *szLblText, const C4Rect &rcBounds, DWORD dwFClr=0xffffffff, CStdFont *pFont=NULL, int32_t iAlign=ACenter, bool fMarkup=true) // ctor
: Label(szLblText, rcBounds, iAlign, dwFClr, pFont, true, true, fMarkup), iAutoScrollDelay(0), tLastChangeTime(GetTime()), iScrollPos(0), iScrollDir(0), iRightIndent(0) : Label(szLblText, rcBounds, iAlign, dwFClr, pFont, true, true, fMarkup), iAutoScrollDelay(0), tLastChangeTime(C4TimeMilliseconds::Now()), iScrollPos(0), iScrollDir(0), iRightIndent(0)
{ SetAutosize(false); this->rcBounds=rcBounds; }// ctor - re-sets bounds after SetText { SetAutosize(false); this->rcBounds=rcBounds; }// ctor - re-sets bounds after SetText
static int32_t GetDefaultHeight(CStdFont *pUseFont=NULL); static int32_t GetDefaultHeight(CStdFont *pUseFont=NULL);
@ -2451,7 +2451,7 @@ namespace C4GUI
int32_t LDownX, LDownY; // position where left button was pressed last int32_t LDownX, LDownY; // position where left button was pressed last
DWORD dwKeys; // shift, ctrl, etc. DWORD dwKeys; // shift, ctrl, etc.
bool fActive; bool fActive;
C4TimeMilliseconds tLastMovementTime; // GetTime() when the mouse pos changed last C4TimeMilliseconds tLastMovementTime; // C4TimeMilliseconds::Now() when the mouse pos changed last
// whether last input was done by mouse // whether last input was done by mouse
// set to true whenever mouse pos changes or buttons are pressed // set to true whenever mouse pos changes or buttons are pressed
@ -2482,8 +2482,8 @@ namespace C4GUI
void SetOwnedMouse(bool fToVal) { fActive=fToVal; } void SetOwnedMouse(bool fToVal) { fActive=fToVal; }
void ResetToolTipTime() { tLastMovementTime = GetTime(); } void ResetToolTipTime() { tLastMovementTime = C4TimeMilliseconds::Now(); }
bool IsMouseStill() { return GetTime()-tLastMovementTime >= C4GUI_ToolTipShowTime; } bool IsMouseStill() { return C4TimeMilliseconds::Now()-tLastMovementTime >= C4GUI_ToolTipShowTime; }
void ResetActiveInput() { fActiveInput = false; } void ResetActiveInput() { fActiveInput = false; }
bool IsActiveInput() { return fActiveInput; } bool IsActiveInput() { return fActiveInput; }

View File

@ -150,7 +150,7 @@ namespace C4GUI
// reset selection // reset selection
iSelectionStart = iSelectionEnd = 0; iSelectionStart = iSelectionEnd = 0;
// cursor might have moved: ensure it is shown // cursor might have moved: ensure it is shown
tLastInputTime=GetTime(); tLastInputTime = C4TimeMilliseconds::Now();
} }
void Edit::DeleteSelection() void Edit::DeleteSelection()
@ -162,7 +162,7 @@ namespace C4GUI
// adjust cursor pos // adjust cursor pos
if (iCursorPos > iSelBegin) iCursorPos = Max(iSelBegin, iCursorPos - iSelEnd + iSelBegin); if (iCursorPos > iSelBegin) iCursorPos = Max(iSelBegin, iCursorPos - iSelEnd + iSelBegin);
// cursor might have moved: ensure it is shown // cursor might have moved: ensure it is shown
tLastInputTime=GetTime(); tLastInputTime = C4TimeMilliseconds::Now();
// nothing selected // nothing selected
iSelectionStart = iSelectionEnd = iSelBegin; iSelectionStart = iSelectionEnd = iSelBegin;
} }
@ -189,7 +189,7 @@ namespace C4GUI
// advance cursor // advance cursor
iCursorPos += iTextLen; iCursorPos += iTextLen;
// cursor moved: ensure it is shown // cursor moved: ensure it is shown
tLastInputTime=GetTime(); tLastInputTime = C4TimeMilliseconds::Now();
ScrollCursorInView(); ScrollCursorInView();
} }
// done; return whether everything was inserted // done; return whether everything was inserted
@ -452,7 +452,7 @@ namespace C4GUI
iCursorPos += iMoveLength; iCursorPos += iMoveLength;
} }
// show cursor // show cursor
tLastInputTime=GetTime(); tLastInputTime = C4TimeMilliseconds::Now();
ScrollCursorInView(); ScrollCursorInView();
// operation recognized // operation recognized
return true; return true;
@ -556,7 +556,7 @@ namespace C4GUI
// select all // select all
iSelectionStart=0; iSelectionEnd=iCursorPos=SLen(Text); iSelectionStart=0; iSelectionEnd=iCursorPos=SLen(Text);
// begin with a flashing cursor // begin with a flashing cursor
tLastInputTime=GetTime(); tLastInputTime = C4TimeMilliseconds::Now();
} }
void Edit::OnLooseFocus() void Edit::OnLooseFocus()
@ -629,7 +629,8 @@ namespace C4GUI
// draw edit text // draw edit text
pDraw->TextOut(pDrawText, *pFont, 1.0f, cgo.Surface, rcClientRect.x + cgo.TargetX - iXScroll, iY0 + cgo.TargetY - 1, dwFontClr, ALeft, false); pDraw->TextOut(pDrawText, *pFont, 1.0f, cgo.Surface, rcClientRect.x + cgo.TargetX - iXScroll, iY0 + cgo.TargetY - 1, dwFontClr, ALeft, false);
// draw cursor // draw cursor
if (HasDrawFocus() && !(((tLastInputTime-GetTime())/500)%2)) bool fBlink = ((tLastInputTime - C4TimeMilliseconds::Now())/500)%2 == 0;
if (HasDrawFocus() && fBlink)
{ {
char cAtCursor = pDrawText[iCursorPos]; pDrawText[iCursorPos]=0; int32_t w,h,wc; char cAtCursor = pDrawText[iCursorPos]; pDrawText[iCursorPos]=0; int32_t w,h,wc;
pFont->GetTextExtent(pDrawText, w, h, false); pFont->GetTextExtent(pDrawText, w, h, false);

View File

@ -153,7 +153,7 @@ namespace C4GUI
if (iAlign == ALeft) iXOff += 5; if (iAlign == ALeft) iXOff += 5;
if (iAutoScrollDelay) if (iAutoScrollDelay)
{ {
C4TimeMilliseconds tNow = GetTime(); C4TimeMilliseconds tNow = C4TimeMilliseconds::Now();
if (tNow - tLastChangeTime >= iAutoScrollDelay) if (tNow - tLastChangeTime >= iAutoScrollDelay)
{ {
if (!iScrollDir) iScrollDir=1; if (!iScrollDir) iScrollDir=1;

View File

@ -364,7 +364,7 @@ namespace C4GUI
void Tabular::DoCaptionScroll(int32_t iDir) void Tabular::DoCaptionScroll(int32_t iDir)
{ {
// store time of scrolling change // store time of scrolling change
tLastScrollTime = GetTime(); tLastScrollTime = C4TimeMilliseconds::Now();
// change scrolling within max range // change scrolling within max range
int32_t iAvailableTabSpace = rcBounds.Wdt; int32_t iAvailableTabSpace = rcBounds.Wdt;
int32_t iScrollPinSize = GetTopSize(); int32_t iScrollPinSize = GetTopSize();
@ -377,7 +377,8 @@ namespace C4GUI
if (!fDrawSelf) return; if (!fDrawSelf) return;
bool fGfx = HasGfx(); bool fGfx = HasGfx();
// execute scrolling // execute scrolling
if ((fScrollingLeftDown || fScrollingRightDown) && GetTime()-tLastScrollTime >= C4GUI_TabCaptionScrollTime) bool fCaptionScrollDelayOver = C4TimeMilliseconds::Now() - tLastScrollTime >= C4GUI_TabCaptionScrollTime;
if ((fScrollingLeftDown || fScrollingRightDown) && fCaptionScrollDelayOver)
DoCaptionScroll(fScrollingRightDown - fScrollingLeftDown); DoCaptionScroll(fScrollingRightDown - fScrollingLeftDown);
// border // border
if (!fGfx) Draw3DFrame(cgo, false, 1, 0xaf, eTabPos!=tbTop, GetTopSize(), eTabPos!=tbLeft, GetLeftSize()); if (!fGfx) Draw3DFrame(cgo, false, 1, 0xaf, eTabPos!=tbTop, GetTopSize(), eTabPos!=tbLeft, GetLeftSize());

View File

@ -929,7 +929,7 @@ void C4LandscapeRenderGL::BuildMatMap(GLfloat *pFMap, GLubyte *pIMap)
while(p = strchr(p, '-')) { p++; iPhases++; } while(p = strchr(p, '-')) { p++; iPhases++; }
// Hard-coded hack. Fix me! // Hard-coded hack. Fix me!
const int iPhaseLength = 300; const int iPhaseLength = 300;
float phase = (iPhases == 1 ? 0 : float(GetTime().AsInt() % (iPhases * iPhaseLength)) / iPhaseLength); float phase = (iPhases == 1 ? 0 : float(C4TimeMilliseconds::Now().AsInt() % (iPhases * iPhaseLength)) / iPhaseLength);
// Find our transition // Find our transition
const char *pFrom = pEntry->GetTextureName(); const char *pFrom = pEntry->GetTextureName();

View File

@ -59,7 +59,7 @@ public:
inline void Start() inline void Start()
{ {
if (!iStartCalled) if (!iStartCalled)
tStartTime = GetTime(); tStartTime = C4TimeMilliseconds::Now();
iCount ++; iCount ++;
iCountPart ++; iCountPart ++;
iStartCalled ++; iStartCalled ++;
@ -71,7 +71,7 @@ public:
iStartCalled --; iStartCalled --;
if (!iStartCalled && iCount >= 100) if (!iStartCalled && iCount >= 100)
{ {
uint32_t tTime = GetTime() - tStartTime; uint32_t tTime = C4TimeMilliseconds::Now() - tStartTime;
tTimeSum += tTime; tTimeSum += tTime;
tTimeSumPart += tTime; tTimeSumPart += tTime;

View File

@ -43,12 +43,13 @@ char DummyData[1024 * 1024];
class MyCBClass : public C4NetIOMan class MyCBClass : public C4NetIOMan
{ {
unsigned int iTime, iPcks; unsigned int tTime, iPcks;
public: public:
virtual bool OnConn(const C4NetIO::addr_t &addr, const C4NetIO::addr_t &addr2, C4NetIO *pNetIO) virtual bool OnConn(const C4NetIO::addr_t &addr, const C4NetIO::addr_t &addr2, C4NetIO *pNetIO)
{ {
cout << "got connection from " << inet_ntoa(addr.sin_addr) << endl; cout << "got connection from " << inet_ntoa(addr.sin_addr) << endl;
iTime = GetTime(); iPcks = 0; tTime = C4TimeMilliseconds::Now();
iPcks = 0;
#ifdef ASYNC_CONNECT #ifdef ASYNC_CONNECT
if (!fHost) if (!fHost)
@ -62,10 +63,12 @@ public:
} }
virtual void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO) virtual void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO)
{ {
if (GetTime() > iTime + 1000) C4TimeMilliseconds tNow = C4TimeMilliseconds::Now();
if (tNow > tTime + 1000)
{ {
cout << iPcks << " packets in " << GetTime() - iTime << " ms (" << iPcks * 1000 / (GetTime() - iTime) << " per second, " << (iPcks ? (GetTime() - iTime) * 1000 / iPcks : -1u) << "us per packet)" << endl; cout << iPcks << " packets in " << tNow - tTime << " ms (" << iPcks * 1000 / (tNow - tTime) << " per second, " << (iPcks ? (tNow - tTime) * 1000 / iPcks : -1u) << "us per packet)" << endl;
iTime = GetTime(); iPcks = 0; tTime = C4TimeMilliseconds::Now();
iPcks = 0;
} }
if (!rPacket.getStatus()) if (!rPacket.getStatus())
{ {

View File

@ -63,7 +63,7 @@ bool C4GameControlNetwork::Init(int32_t inClientID, bool fnHost, int32_t iStartT
// ok // ok
fEnabled = true; fRunning = false; fEnabled = true; fRunning = false;
iTargetFPS = 38; iTargetFPS = 38;
tNextControlRequest = GetTime() + C4ControlRequestInterval; tNextControlRequest = C4TimeMilliseconds::Now() + C4ControlRequestInterval;
if(tWaitStart) if(tWaitStart)
{ {
delete tWaitStart; delete tWaitStart;
@ -95,7 +95,7 @@ void C4GameControlNetwork::Execute() // by main thread
// Save time the control tick was reached // Save time the control tick was reached
if (!tWaitStart) if (!tWaitStart)
tWaitStart = new C4TimeMilliseconds(GetTime()); tWaitStart = new C4TimeMilliseconds(C4TimeMilliseconds::Now());
// Execute any queued sync control // Execute any queued sync control
ExecQueuedSyncCtrl(); ExecQueuedSyncCtrl();
@ -745,7 +745,7 @@ void C4GameControlNetwork::CheckCompleteCtrl(bool fSetEvent) // by both
// target ctrl tick to reach? // target ctrl tick to reach?
if (iControlReady < iTargetTick && if (iControlReady < iTargetTick &&
(!fActivated || iControlSent > iControlReady) && (!fActivated || iControlSent > iControlReady) &&
GetTime() >= tNextControlRequest) C4TimeMilliseconds::Now() >= tNextControlRequest)
{ {
Application.InteractiveThread.ThreadLogS("Network: Recovering: Requesting control for tick %d...", iControlReady + 1); Application.InteractiveThread.ThreadLogS("Network: Recovering: Requesting control for tick %d...", iControlReady + 1);
// make request // make request
@ -756,7 +756,7 @@ void C4GameControlNetwork::CheckCompleteCtrl(bool fSetEvent) // by both
else else
::Network.Clients.BroadcastMsgToConnClients(Pkt); ::Network.Clients.BroadcastMsgToConnClients(Pkt);
// set time for next request // set time for next request
tNextControlRequest = GetTime() + C4ControlRequestInterval; tNextControlRequest = C4TimeMilliseconds::Now() + C4ControlRequestInterval;
} }
} }
@ -774,7 +774,7 @@ C4GameControlPacket *C4GameControlNetwork::PackCompleteCtrl(int32_t iTick)
{ {
// async mode: wait n extra frames for slow clients // async mode: wait n extra frames for slow clients
const int iMaxWait = (Config.Network.AsyncMaxWait * 1000) / iTargetFPS; const int iMaxWait = (Config.Network.AsyncMaxWait * 1000) / iTargetFPS;
if (eMode != CNM_Async || !tWaitStart || GetTime() <= *tWaitStart + iMaxWait) if (eMode != CNM_Async || !tWaitStart || C4TimeMilliseconds::Now() <= *tWaitStart + iMaxWait)
return NULL; return NULL;
} }
@ -805,7 +805,7 @@ C4GameControlPacket *C4GameControlNetwork::PackCompleteCtrl(int32_t iTick)
::Network.Clients.BroadcastMsgToConnClients(MkC4NetIOPacket(PID_Control, *pComplete)); ::Network.Clients.BroadcastMsgToConnClients(MkC4NetIOPacket(PID_Control, *pComplete));
// advance control request time // advance control request time
tNextControlRequest = Max(tNextControlRequest, GetTime() + C4ControlRequestInterval); tNextControlRequest = Max(tNextControlRequest, C4TimeMilliseconds::Now() + C4ControlRequestInterval);
// return // return
return pComplete; return pComplete;
@ -861,7 +861,7 @@ void C4GameControlNetwork::ExecQueuedSyncCtrl() // by main thread
C4GameControlPacket::C4GameControlPacket() C4GameControlPacket::C4GameControlPacket()
: iClientID(C4ClientIDUnknown), : iClientID(C4ClientIDUnknown),
iCtrlTick(-1), iCtrlTick(-1),
tTime(GetTime()), tTime(C4TimeMilliseconds::Now()),
pNext(NULL) pNext(NULL)
{ {
@ -870,7 +870,7 @@ C4GameControlPacket::C4GameControlPacket()
C4GameControlPacket::C4GameControlPacket(const C4GameControlPacket &Pkt2) C4GameControlPacket::C4GameControlPacket(const C4GameControlPacket &Pkt2)
: C4PacketBase(Pkt2), iClientID(Pkt2.getClientID()), : C4PacketBase(Pkt2), iClientID(Pkt2.getClientID()),
iCtrlTick(Pkt2.getCtrlTick()), iCtrlTick(Pkt2.getCtrlTick()),
tTime(GetTime()), tTime(C4TimeMilliseconds::Now()),
pNext(NULL) pNext(NULL)
{ {
Ctrl.Copy(Pkt2.getControl()); Ctrl.Copy(Pkt2.getControl());

View File

@ -88,7 +88,7 @@ bool C4InteractiveThread::PushEvent(C4InteractiveEventType eEvent, void *pData)
pEvent->Type = eEvent; pEvent->Type = eEvent;
pEvent->Data = pData; pEvent->Data = pData;
#ifdef _DEBUG #ifdef _DEBUG
pEvent->Time = GetTime(); pEvent->Time = C4TimeMilliseconds::Now();
#endif #endif
pEvent->Next = NULL; pEvent->Next = NULL;
// add item (at end) // add item (at end)
@ -118,7 +118,7 @@ bool C4InteractiveThread::PopEvent(C4InteractiveEventType *pEventType, void **pp
*ppData = pEvent->Data; *ppData = pEvent->Data;
#ifdef _DEBUG #ifdef _DEBUG
if (Game.IsRunning) if (Game.IsRunning)
AvgNetEvDelay += ((GetTime() - pEvent->Time) - AvgNetEvDelay) / 100; AvgNetEvDelay += ((C4TimeMilliseconds::Now() - pEvent->Time) - AvgNetEvDelay) / 100;
#endif #endif
// remove // remove
delete pFirstEvent; delete pFirstEvent;

View File

@ -1832,7 +1832,7 @@ bool C4NetIOUDP::Init(uint16_t inPort)
fMultiCast = false; fMultiCast = false;
if(!tNextCheck) tNextCheck = new C4TimeMilliseconds(); if(!tNextCheck) tNextCheck = new C4TimeMilliseconds();
*tNextCheck = GetTime() + iCheckInterval; *tNextCheck = C4TimeMilliseconds::Now() + iCheckInterval;
// ok, that's all for now. // ok, that's all for now.
// call InitBroadcast for more initialization fun // call InitBroadcast for more initialization fun
@ -2004,7 +2004,7 @@ bool C4NetIOUDP::Execute(int iMaxTime, pollfd *) // (mt-safe)
ResetError(); ResetError();
// adjust maximum block time // adjust maximum block time
C4TimeMilliseconds tNow = GetTime(); C4TimeMilliseconds tNow = C4TimeMilliseconds::Now();
uint32_t iMaxBlock = GetNextTick(tNow) - tNow; uint32_t iMaxBlock = GetNextTick(tNow) - tNow;
if (iMaxTime == TO_INF || iMaxTime > (int) iMaxBlock) iMaxTime = iMaxBlock; if (iMaxTime == TO_INF || iMaxTime > (int) iMaxBlock) iMaxTime = iMaxBlock;
@ -2014,7 +2014,7 @@ bool C4NetIOUDP::Execute(int iMaxTime, pollfd *) // (mt-safe)
// connection check needed? // connection check needed?
if (tNextCheck) if (tNextCheck)
if (*tNextCheck <= GetTime()) if (*tNextCheck <= C4TimeMilliseconds::Now())
DoCheck(); DoCheck();
// client timeout? // client timeout?
for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next) for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
@ -2540,7 +2540,7 @@ bool C4NetIOUDP::Peer::Check(bool fForceCheck)
if (eStatus != CS_Works) return true; if (eStatus != CS_Works) return true;
// prevent re-check (check floods) // prevent re-check (check floods)
// instead, ask for other packets that are missing until recheck is allowed // instead, ask for other packets that are missing until recheck is allowed
bool fNoReCheck = !!tNextReCheck && *tNextReCheck > GetTime(); bool fNoReCheck = !!tNextReCheck && *tNextReCheck > C4TimeMilliseconds::Now();
if (!fNoReCheck) iLastPacketAsked = iLastMCPacketAsked = 0; if (!fNoReCheck) iLastPacketAsked = iLastMCPacketAsked = 0;
unsigned int iStartAt = fNoReCheck ? Max(iLastPacketAsked + 1, iIPacketCounter) : iIPacketCounter; unsigned int iStartAt = fNoReCheck ? Max(iLastPacketAsked + 1, iIPacketCounter) : iIPacketCounter;
unsigned int iStartAtMC = fNoReCheck ? Max(iLastMCPacketAsked + 1, iIMCPacketCounter) : iIMCPacketCounter; unsigned int iStartAtMC = fNoReCheck ? Max(iLastMCPacketAsked + 1, iIMCPacketCounter) : iIMCPacketCounter;
@ -2560,7 +2560,7 @@ bool C4NetIOUDP::Peer::Check(bool fForceCheck)
if (!fNoReCheck) if (!fNoReCheck)
{ {
if(!tNextReCheck) tNextReCheck = new C4TimeMilliseconds(); if(!tNextReCheck) tNextReCheck = new C4TimeMilliseconds();
*tNextReCheck = iEAskCnt ? GetTime() + iReCheckInterval : 0; *tNextReCheck = iEAskCnt ? C4TimeMilliseconds::Now() + iReCheckInterval : 0;
} }
// something to ask for? (or check forced?) // something to ask for? (or check forced?)
if (iEAskCnt || fForceCheck) if (iEAskCnt || fForceCheck)
@ -2795,7 +2795,7 @@ void C4NetIOUDP::Peer::CheckTimeout()
// timeout set? // timeout set?
if (!tTimeout) return; if (!tTimeout) return;
// check // check
if (GetTime() > *tTimeout) if (C4TimeMilliseconds::Now() > *tTimeout)
OnTimeout(); OnTimeout();
} }
@ -2946,7 +2946,7 @@ void C4NetIOUDP::Peer::SetTimeout(int iLength, int iRetryCnt) // (mt-safe)
if (iLength != TO_INF) if (iLength != TO_INF)
{ {
if(!tTimeout) tTimeout = new C4TimeMilliseconds(); if(!tTimeout) tTimeout = new C4TimeMilliseconds();
*tTimeout = GetTime() + iLength; *tTimeout = C4TimeMilliseconds::Now() + iLength;
} }
else else
{ {
@ -3171,7 +3171,7 @@ void C4NetIOUDP::DoCheck() // (mt-safe)
// set time for next check // set time for next check
if(!tNextCheck) tNextCheck = new C4TimeMilliseconds(); if(!tNextCheck) tNextCheck = new C4TimeMilliseconds();
*tNextCheck = GetTime() + iCheckInterval; *tNextCheck = C4TimeMilliseconds::Now() + iCheckInterval;
} }
// debug // debug
@ -3219,7 +3219,7 @@ void C4NetIOUDP::CloseDebugLog()
void C4NetIOUDP::DebugLogPkt(bool fOut, const C4NetIOPacket &Pkt) void C4NetIOUDP::DebugLogPkt(bool fOut, const C4NetIOPacket &Pkt)
{ {
StdStrBuf O; StdStrBuf O;
uint32_t tTime = GetTime().AsInt(); uint32_t tTime = C4TimeMilliseconds::Now().AsInt();
O.Format("%s %u:%02u:%02u:%03u %s:%d:", fOut ? "out" : "in ", O.Format("%s %u:%02u:%02u:%03u %s:%d:", fOut ? "out" : "in ",
(tTime / 1000 / 60 / 60), (tTime / 1000 / 60) % 60, (tTime / 1000) % 60, tTime % 1000, (tTime / 1000 / 60 / 60), (tTime / 1000 / 60) % 60, (tTime / 1000) % 60, tTime % 1000,
inet_ntoa(Pkt.getAddr().sin_addr), htons(Pkt.getAddr().sin_port)); inet_ntoa(Pkt.getAddr().sin_addr), htons(Pkt.getAddr().sin_port));

View File

@ -1508,7 +1508,7 @@ C4Network2Res::Ref C4Network2::RetrieveRes(const C4Network2ResCore &Core, int32_
C4GUI::ProgressDialog *pDlg = NULL; C4GUI::ProgressDialog *pDlg = NULL;
bool fLog = false; bool fLog = false;
int32_t iProcess = -1; int32_t iProcess = -1;
C4TimeMilliseconds tTimeout = GetTime() + iTimeoutLen; C4TimeMilliseconds tTimeout = C4TimeMilliseconds::Now() + iTimeoutLen;
// wait for resource // wait for resource
while (isEnabled()) while (isEnabled())
{ {
@ -1542,12 +1542,12 @@ C4Network2Res::Ref C4Network2::RetrieveRes(const C4Network2ResCore &Core, int32_
if (pRes && pRes->getPresentPercent() != iProcess) if (pRes && pRes->getPresentPercent() != iProcess)
{ {
iProcess = pRes->getPresentPercent(); iProcess = pRes->getPresentPercent();
tTimeout = GetTime() + iTimeoutLen; tTimeout = C4TimeMilliseconds::Now() + iTimeoutLen;
} }
else else
{ {
// if not: check timeout // if not: check timeout
if (GetTime() > tTimeout) if (C4TimeMilliseconds::Now() > tTimeout)
{ {
LogFatal(FormatString(LoadResStr("IDS_NET_ERR_RESTIMEOUT"), szResName).getData()); LogFatal(FormatString(LoadResStr("IDS_NET_ERR_RESTIMEOUT"), szResName).getData());
if (pDlg) delete pDlg; if (pDlg) delete pDlg;
@ -1584,7 +1584,7 @@ C4Network2Res::Ref C4Network2::RetrieveRes(const C4Network2ResCore &Core, int32_
} }
else else
{ {
if (!Application.ScheduleProcs(tTimeout - GetTime())) if (!Application.ScheduleProcs(tTimeout - C4TimeMilliseconds::Now()))
{ return NULL; } { return NULL; }
} }
@ -1787,7 +1787,7 @@ void C4Network2::RequestActivate()
} }
// ensure interval // ensure interval
if (tLastActivateRequest) if (tLastActivateRequest)
if(GetTime() < *tLastActivateRequest + C4NetActivationReqInterval) if(C4TimeMilliseconds::Now() < *tLastActivateRequest + C4NetActivationReqInterval)
return; return;
// status not reached yet? May be chasing, let's delay this. // status not reached yet? May be chasing, let's delay this.
if (!fStatusReached) if (!fStatusReached)
@ -1799,9 +1799,9 @@ void C4Network2::RequestActivate()
Clients.SendMsgToHost(MkC4NetIOPacket(PID_ClientActReq, C4PacketActivateReq(Game.FrameCounter))); Clients.SendMsgToHost(MkC4NetIOPacket(PID_ClientActReq, C4PacketActivateReq(Game.FrameCounter)));
// store time // store time
if(!tLastActivateRequest) if(!tLastActivateRequest)
tLastActivateRequest = new C4TimeMilliseconds(GetTime()); tLastActivateRequest = new C4TimeMilliseconds(C4TimeMilliseconds::Now());
else else
*tLastActivateRequest = GetTime(); *tLastActivateRequest = C4TimeMilliseconds::Now();
} }
void C4Network2::DeactivateInactiveClients() void C4Network2::DeactivateInactiveClients()

View File

@ -77,7 +77,7 @@ bool C4Network2IO::Init(int16_t iPortTCP, int16_t iPortUDP, int16_t iPortDiscove
if (pNetIO_TCP || pNetIO_UDP) Clear(); if (pNetIO_TCP || pNetIO_UDP) Clear();
// init members // init members
tLastPing = tLastStatistic = GetTime(); tLastPing = tLastStatistic = C4TimeMilliseconds::Now();
iTCPIRate = iTCPORate = iTCPBCRate = 0; iTCPIRate = iTCPORate = iTCPBCRate = 0;
iUDPIRate = iUDPORate = iUDPBCRate = 0; iUDPIRate = iUDPORate = iUDPBCRate = 0;
@ -201,7 +201,7 @@ bool C4Network2IO::Init(int16_t iPortTCP, int16_t iPortUDP, int16_t iPortDiscove
} }
// own timer // own timer
tLastExecute = GetTime(); tLastExecute = C4TimeMilliseconds::Now();
Thread.AddProc(this); Thread.AddProc(this);
// ok // ok
@ -506,7 +506,7 @@ bool C4Network2IO::OnConn(const C4NetIO::addr_t &PeerAddr, const C4NetIO::addr_t
return false; return false;
} }
#if(C4NET2IO_DUMP_LEVEL > 1) #if(C4NET2IO_DUMP_LEVEL > 1)
uint32_t tTime = GetTime().AsInt(); uint32_t tTime = C4TimeMilliseconds::Now().AsInt();
ThreadLogS("OnConn: %u:%02u:%02u:%03u: %s", ThreadLogS("OnConn: %u:%02u:%02u:%03u: %s",
(tTime / 1000 / 60 / 60), (tTime / 1000 / 60) % 60, (tTime / 1000) % 60, tTime % 1000, (tTime / 1000 / 60 / 60), (tTime / 1000 / 60) % 60, (tTime / 1000) % 60, tTime % 1000,
getNetIOName(pNetIO)); getNetIOName(pNetIO));
@ -561,7 +561,7 @@ void C4Network2IO::OnDisconn(const C4NetIO::addr_t &addr, C4NetIO *pNetIO, const
return; return;
} }
#if(C4NET2IO_DUMP_LEVEL > 1) #if(C4NET2IO_DUMP_LEVEL > 1)
uint32_t tTime = GetTime().AsInt(); uint32_t tTime = C4TimeMilliseconds::Now().AsInt();
ThreadLogS("OnDisconn: %u:%02u:%02u:%03u: %s", ThreadLogS("OnDisconn: %u:%02u:%02u:%03u: %s",
(tTime / 1000 / 60 / 60), (tTime / 1000 / 60) % 60, (tTime / 1000) % 60, tTime % 1000, (tTime / 1000 / 60 / 60), (tTime / 1000 / 60) % 60, (tTime / 1000) % 60, tTime % 1000,
getNetIOName(pNetIO)); getNetIOName(pNetIO));
@ -591,7 +591,7 @@ void C4Network2IO::OnDisconn(const C4NetIO::addr_t &addr, C4NetIO *pNetIO, const
void C4Network2IO::OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO) void C4Network2IO::OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO)
{ {
#if(C4NET2IO_DUMP_LEVEL > 1) #if(C4NET2IO_DUMP_LEVEL > 1)
uint32_t tTime = GetTime().AsInt(); uint32_t tTime = C4TimeMilliseconds::Now().AsInt();
ThreadLogS("OnPacket: %u:%02u:%02u:%03u: status %02x %s", ThreadLogS("OnPacket: %u:%02u:%02u:%03u: status %02x %s",
(tTime / 1000 / 60 / 60), (tTime / 1000 / 60) % 60, (tTime / 1000) % 60, tTime % 1000, (tTime / 1000 / 60 / 60), (tTime / 1000 / 60) % 60, (tTime / 1000) % 60, tTime % 1000,
rPacket.getStatus(), getNetIOName(pNetIO)); rPacket.getStatus(), getNetIOName(pNetIO));
@ -601,8 +601,9 @@ void C4Network2IO::OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO)
C4Network2IOConnection *pConn = GetConnection(rPacket.getAddr(), pNetIO); C4Network2IOConnection *pConn = GetConnection(rPacket.getAddr(), pNetIO);
if (!pConn) { Application.InteractiveThread.ThreadLog("Network: could not find connection for packet from %s:%d!", inet_ntoa(rPacket.getAddr().sin_addr), htons(rPacket.getAddr().sin_port)); return; } if (!pConn) { Application.InteractiveThread.ThreadLog("Network: could not find connection for packet from %s:%d!", inet_ntoa(rPacket.getAddr().sin_addr), htons(rPacket.getAddr().sin_port)); return; }
#if(C4NET2IO_DUMP_LEVEL > 2) #if(C4NET2IO_DUMP_LEVEL > 2)
if (GetTime() - tTime > 100) uint32_t iFindConnectionBlocked = C4TimeMilliseconds::Now() - tTime;
ThreadLogS("OnPacket: ... blocked %d ms for finding the connection!", GetTime() - tTime); if (iFindConnectionBlocked > 100)
ThreadLogS("OnPacket: ... blocked %d ms for finding the connection!", iFindConnectionBlocked);
#endif #endif
// notify // notify
pConn->OnPacketReceived(rPacket.getStatus()); pConn->OnPacketReceived(rPacket.getStatus());
@ -610,8 +611,9 @@ void C4Network2IO::OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO)
HandlePacket(rPacket, pConn, true); HandlePacket(rPacket, pConn, true);
// log time // log time
#if(C4NET2IO_DUMP_LEVEL > 1) #if(C4NET2IO_DUMP_LEVEL > 1)
if (GetTime() - tTime > 100) uint32_t iHandlingBlocked = C4TimeMilliseconds::Now() - tTime;
ThreadLogS("OnPacket: ... blocked %d ms for handling!", GetTime() - tTime); if (iHandlingBlocked > 100)
ThreadLogS("OnPacket: ... blocked %d ms for handling!", iHandlingBlocked);
#endif #endif
} }
@ -623,7 +625,7 @@ void C4Network2IO::OnError(const char *strError, C4NetIO *pNetIO)
bool C4Network2IO::Execute(int iTimeout, pollfd *) bool C4Network2IO::Execute(int iTimeout, pollfd *)
{ {
tLastExecute = GetTime(); tLastExecute = C4TimeMilliseconds::Now();
// check for timeout // check for timeout
CheckTimeout(); CheckTimeout();
@ -845,7 +847,7 @@ bool C4Network2IO::HandlePacket(const C4NetIOPacket &rPacket, C4Network2IOConnec
#if(C4NET2IO_DUMP_LEVEL > 0) #if(C4NET2IO_DUMP_LEVEL > 0)
if (fThread && Pkt.getPktType() != PID_Ping && Pkt.getPktType() != PID_Pong && Pkt.getPktType() != PID_NetResData) if (fThread && Pkt.getPktType() != PID_Ping && Pkt.getPktType() != PID_Pong && Pkt.getPktType() != PID_NetResData)
{ {
uint32_t tTime = GetTime().AsInt(); uint32_t tTime = C4TimeMilliseconds::Now().AsInt();
// StdStrBuf PacketDump = DecompileToBuf<StdCompilerINIWrite>(mkNamingAdaptrPacket); // StdStrBuf PacketDump = DecompileToBuf<StdCompilerINIWrite>(mkNamingAdaptrPacket);
StdStrBuf PacketHeader = FormatString("HandlePacket: %u:%02u:%02u:%03u by %s:%d (%lu bytes, counter %d)", StdStrBuf PacketHeader = FormatString("HandlePacket: %u:%02u:%02u:%03u by %s:%d (%lu bytes, counter %d)",
tTime / 1000 / 60 / 60, (tTime / 1000 / 60) % 60, (tTime / 1000) % 60, tTime % 1000, tTime / 1000 / 60 / 60, (tTime / 1000 / 60) % 60, (tTime / 1000) % 60, tTime % 1000,
@ -870,14 +872,14 @@ bool C4Network2IO::HandlePacket(const C4NetIOPacket &rPacket, C4Network2IOConnec
{ {
fHandled = true; fHandled = true;
#if(C4NET2IO_DUMP_LEVEL > 2) #if(C4NET2IO_DUMP_LEVEL > 2)
C4TimeMilliseconds tStart = GetTime(); C4TimeMilliseconds tStart = C4TimeMilliseconds::Now();
#endif #endif
// call handler(s) // call handler(s)
CallHandlers(pHData->HandlerID, &Pkt, pConn, fThread); CallHandlers(pHData->HandlerID, &Pkt, pConn, fThread);
#if(C4NET2IO_DUMP_LEVEL > 2) #if(C4NET2IO_DUMP_LEVEL > 2)
uint32_t iBlockedTime = GetTime() - tStart; uint32_t iBlockedTime = C4TimeMilliseconds::Now() - tStart;
if (fThread && iBlockedTime > 100) if (fThread && iBlockedTime > 100)
{ {
ThreadLogS("HandlePacket: ... blocked for %u ms!", iBlockedTime); ThreadLogS("HandlePacket: ... blocked for %u ms!", iBlockedTime);
@ -1334,7 +1336,7 @@ int C4Network2IOConnection::getLag() const
// Last ping not answered yet? // Last ping not answered yet?
if(tLastPing && (!tLastPong || *tLastPing > *tLastPong)) if(tLastPing && (!tLastPong || *tLastPing > *tLastPong))
{ {
int iPingLag = GetTime() - *tLastPing; int iPingLag = C4TimeMilliseconds::Now() - *tLastPing;
// Use it for lag measurement once it's larger then the last ping time // Use it for lag measurement once it's larger then the last ping time
// (the ping time won't be better than this anyway once the pong's here) // (the ping time won't be better than this anyway once the pong's here)
return Max(iPingLag, iPingTime); return Max(iPingLag, iPingTime);
@ -1377,7 +1379,7 @@ void C4Network2IOConnection::OnPing()
// Save time // Save time
if(!tLastPing) tLastPing = new C4TimeMilliseconds(); if(!tLastPing) tLastPing = new C4TimeMilliseconds();
*tLastPing = GetTime(); *tLastPing = C4TimeMilliseconds::Now();
} }
void C4Network2IOConnection::SetPingTime(int inPingTime) void C4Network2IOConnection::SetPingTime(int inPingTime)
@ -1386,7 +1388,7 @@ void C4Network2IOConnection::SetPingTime(int inPingTime)
iPingTime = inPingTime; iPingTime = inPingTime;
// pong received - save timestamp // pong received - save timestamp
if(!tLastPong) tLastPong = new C4TimeMilliseconds(); if(!tLastPong) tLastPong = new C4TimeMilliseconds();
*tLastPong = GetTime(); *tLastPong = C4TimeMilliseconds::Now();
} }
void C4Network2IOConnection::SetStatus(C4Network2IOConnStatus nStatus) void C4Network2IOConnection::SetStatus(C4Network2IOConnStatus nStatus)

View File

@ -513,14 +513,14 @@ void C4PacketJoinData::CompileFunc(StdCompiler *pComp)
// *** C4PacketPing // *** C4PacketPing
C4PacketPing::C4PacketPing(uint32_t iPacketCounter, uint32_t iRemotePacketCounter) C4PacketPing::C4PacketPing(uint32_t iPacketCounter, uint32_t iRemotePacketCounter)
: tTime(GetTime()), : tTime(C4TimeMilliseconds::Now()),
iPacketCounter(iPacketCounter) iPacketCounter(iPacketCounter)
{ {
} }
uint32_t C4PacketPing::getTravelTime() const uint32_t C4PacketPing::getTravelTime() const
{ {
return GetTime() - tTime; return C4TimeMilliseconds::Now() - tTime;
} }
void C4PacketPing::CompileFunc(StdCompiler *pComp) void C4PacketPing::CompileFunc(StdCompiler *pComp)

View File

@ -99,7 +99,7 @@ public:
// StdSchedulerProc override // StdSchedulerProc override
virtual void GetFDs(std::vector<struct pollfd> & rfds) virtual void GetFDs(std::vector<struct pollfd> & rfds)
{ {
if (!query_time) query(GetTime()); if (!query_time) query(C4TimeMilliseconds::Now());
rfds.insert(rfds.end(), fds.begin(), fds.end()); rfds.insert(rfds.end(), fds.begin(), fds.end());
} }
virtual C4TimeMilliseconds GetNextTick(C4TimeMilliseconds Now) virtual C4TimeMilliseconds GetNextTick(C4TimeMilliseconds Now)

View File

@ -37,10 +37,13 @@
static void sdlToC4MCBtn(const SDL_MouseButtonEvent &e, int32_t& button, DWORD& flags) static void sdlToC4MCBtn(const SDL_MouseButtonEvent &e, int32_t& button, DWORD& flags)
{ {
static int lastLeftClick = 0, lastRightClick = 0; C4TimeMilliseconds lastLeftClick = C4TimeMilliseconds::Now();
C4TimeMilliseconds lastRightClick = C4TimeMilliseconds::Now();
static int lastX = 0, lastY = 0; static int lastX = 0, lastY = 0;
static const int clickDist = 2; static const int clickDist = 2;
static const int clickDelay = 400;
button = C4MC_Button_None; button = C4MC_Button_None;
flags = 0; flags = 0;
@ -48,14 +51,14 @@ static void sdlToC4MCBtn(const SDL_MouseButtonEvent &e, int32_t& button, DWORD&
{ {
case SDL_BUTTON_LEFT: case SDL_BUTTON_LEFT:
if (e.state == SDL_PRESSED) if (e.state == SDL_PRESSED)
if (GetTime() - lastLeftClick < 400 && abs(lastX-e.x) <= clickDist && abs(lastY-e.y) <= clickDist) if (C4TimeMilliseconds::Now() - lastLeftClick < clickDelay && abs(lastX-e.x) <= clickDist && abs(lastY-e.y) <= clickDist)
{ {
lastLeftClick = 0; lastLeftClick = 0;
button = C4MC_Button_LeftDouble; button = C4MC_Button_LeftDouble;
} }
else else
{ {
lastLeftClick = GetTime(); lastLeftClick = C4TimeMilliseconds::Now();
button = C4MC_Button_LeftDown; button = C4MC_Button_LeftDown;
} }
else else
@ -63,14 +66,14 @@ static void sdlToC4MCBtn(const SDL_MouseButtonEvent &e, int32_t& button, DWORD&
break; break;
case SDL_BUTTON_RIGHT: case SDL_BUTTON_RIGHT:
if (e.state == SDL_PRESSED) if (e.state == SDL_PRESSED)
if (GetTime() - lastRightClick < 400) if (C4TimeMilliseconds::Now() - lastRightClick < clickDelay)
{ {
lastRightClick = 0; lastRightClick = 0;
button = C4MC_Button_RightDouble; button = C4MC_Button_RightDouble;
} }
else else
{ {
lastRightClick = GetTime(); lastRightClick = C4TimeMilliseconds::Now();
button = C4MC_Button_RightDown; button = C4MC_Button_RightDown;
} }
else else

View File

@ -232,7 +232,7 @@ bool C4SoundInstance::Create(C4SoundEffect *pnEffect, bool fLoop, int32_t inVolu
// Set effect // Set effect
pEffect = pnEffect; pEffect = pnEffect;
// Set // Set
tStarted = GetTime(); tStarted = C4TimeMilliseconds::Now();
iVolume = inVolume; iPan = 0; iChannel = -1; iVolume = inVolume; iPan = 0; iChannel = -1;
iNearInstanceMax = inNearInstanceMax; iNearInstanceMax = inNearInstanceMax;
this->iFalloffDistance = iFalloffDistance; this->iFalloffDistance = iFalloffDistance;
@ -248,7 +248,7 @@ bool C4SoundInstance::CheckStart()
// already started? // already started?
if (isStarted()) return true; if (isStarted()) return true;
// don't bother if half the time is up and the sound is not looping // don't bother if half the time is up and the sound is not looping
if (GetTime() > tStarted + pEffect->Length / 2 && !fLooping) if (C4TimeMilliseconds::Now() > tStarted + pEffect->Length / 2 && !fLooping)
return false; return false;
// do near-instances check // do near-instances check
int32_t iNearInstances = pObj ? pEffect->GetStartedInstanceCount(pObj->GetX(), pObj->GetY(), C4NearSoundRadius) int32_t iNearInstances = pObj ? pEffect->GetStartedInstanceCount(pObj->GetX(), pObj->GetY(), C4NearSoundRadius)
@ -268,10 +268,10 @@ bool C4SoundInstance::Start()
if (!FSOUND_SetLoopMode(iChannel, fLooping ? FSOUND_LOOP_NORMAL : FSOUND_LOOP_OFF)) if (!FSOUND_SetLoopMode(iChannel, fLooping ? FSOUND_LOOP_NORMAL : FSOUND_LOOP_OFF))
{ Stop(); return false; } { Stop(); return false; }
// set position // set position
if (GetTime() > tStarted + 20) if (C4TimeMilliseconds::Now() > tStarted + 20)
{ {
assert(pEffect->Length > 0); assert(pEffect->Length > 0);
int32_t iTime = (GetTime() - tStarted) % pEffect->Length; int32_t iTime = (C4TimeMilliseconds::Now() - tStarted) % pEffect->Length;
FSOUND_SetCurrentPosition(iChannel, iTime / 10 * pEffect->SampleRate / 100); FSOUND_SetCurrentPosition(iChannel, iTime / 10 * pEffect->SampleRate / 100);
} }
#elif defined HAVE_LIBSDL_MIXER #elif defined HAVE_LIBSDL_MIXER
@ -329,7 +329,7 @@ bool C4SoundInstance::Playing()
#ifdef HAVE_FMOD #ifdef HAVE_FMOD
if (fLooping) return true; if (fLooping) return true;
return isStarted() ? FSOUND_GetCurrentSample(iChannel) == pEffect->pSample return isStarted() ? FSOUND_GetCurrentSample(iChannel) == pEffect->pSample
: GetTime() < tStarted + pEffect->Length; : C4TimeMilliseconds::Now() < tStarted + pEffect->Length;
#endif #endif
#ifdef HAVE_LIBSDL_MIXER #ifdef HAVE_LIBSDL_MIXER
return Application.MusicSystem.MODInitialized && (iChannel != -1) && Mix_Playing(iChannel); return Application.MusicSystem.MODInitialized && (iChannel != -1) && Mix_Playing(iChannel);

View File

@ -1,7 +1,8 @@
/* /*
* OpenClonk, http://www.openclonk.org * OpenClonk, http://www.openclonk.org
* *
* Copyright (c) 2005, 2011 Günther Brammer * Copyright (c) 2013 Tobias Zwick
* Copyright (c) 2005, 2011 Günther Brammer
* Copyright (c) 2012 Martin Plicht * Copyright (c) 2012 Martin Plicht
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de
* *
@ -17,7 +18,6 @@
* See clonk_trademark_license.txt for full license. * See clonk_trademark_license.txt for full license.
*/ */
#include "C4Include.h"
#include "C4TimeMilliseconds.h" #include "C4TimeMilliseconds.h"
#ifdef _WIN32 #ifdef _WIN32
@ -25,7 +25,7 @@
#include <C4windowswrapper.h> #include <C4windowswrapper.h>
#include <mmsystem.h> #include <mmsystem.h>
C4TimeMilliseconds GetTime() C4TimeMilliseconds C4TimeMilliseconds::Now()
{ {
return C4TimeMilliseconds(timeGetTime()); return C4TimeMilliseconds(timeGetTime());
} }
@ -38,7 +38,7 @@ C4TimeMilliseconds GetTime()
#include <time.h> #include <time.h>
#endif #endif
C4TimeMilliseconds GetTime() C4TimeMilliseconds C4TimeMilliseconds::Now()
{ {
#ifdef __APPLE__ #ifdef __APPLE__
static time_t sec_offset; static time_t sec_offset;
@ -54,4 +54,4 @@ C4TimeMilliseconds GetTime()
#endif #endif
} }
#endif #endif

View File

@ -40,6 +40,8 @@ private:
uint32_t time; uint32_t time;
public: public:
static C4TimeMilliseconds Now();
C4TimeMilliseconds() : time(0) { } C4TimeMilliseconds() : time(0) { }
C4TimeMilliseconds(uint32_t millis) : time(millis) { } C4TimeMilliseconds(uint32_t millis) : time(millis) { }
C4TimeMilliseconds(const C4TimeMilliseconds& rhs) : time(rhs.time) { } C4TimeMilliseconds(const C4TimeMilliseconds& rhs) : time(rhs.time) { }

View File

@ -229,10 +229,6 @@ bool IsGermanSystem();
// open a weblink in an external browser // open a weblink in an external browser
bool OpenURL(const char* szURL); bool OpenURL(const char* szURL);
// Get a monotonically increasing timestamp in milliseconds
class C4TimeMilliseconds;
C4TimeMilliseconds GetTime();
#ifdef _WIN32 #ifdef _WIN32
#include <io.h> #include <io.h>
#define F_OK 0 #define F_OK 0

View File

@ -65,14 +65,14 @@ bool StdSchedulerProc::ExecuteUntil(int iTimeout)
if (!Execute()) if (!Execute())
return false; return false;
// Calculate endpoint // Calculate endpoint
C4TimeMilliseconds tStopTime = GetTime() + iTimeout; C4TimeMilliseconds tStopTime = C4TimeMilliseconds::Now() + iTimeout;
for (;;) for (;;)
{ {
// Call execute with given timeout // Call execute with given timeout
if (!Execute(Max(iTimeout, 0))) if (!Execute(Max(iTimeout, 0)))
return false; return false;
// Calculate timeout // Calculate timeout
C4TimeMilliseconds tTime = GetTime(); C4TimeMilliseconds tTime = C4TimeMilliseconds::Now();
if (tTime >= tStopTime) if (tTime >= tStopTime)
break; break;
iTimeout = tStopTime - tTime; iTimeout = tStopTime - tTime;
@ -177,7 +177,7 @@ bool StdScheduler::ScheduleProcs(int iTimeout)
// Get timeout // Get timeout
int i; int i;
C4TimeMilliseconds tProcTick; C4TimeMilliseconds tProcTick;
C4TimeMilliseconds tNow = GetTime(); C4TimeMilliseconds tNow = C4TimeMilliseconds::Now();
for (i = 0; i < iProcCnt; i++) for (i = 0; i < iProcCnt; i++)
{ {
if(ppProcs[i]->IsScheduledExecution()) if(ppProcs[i]->IsScheduledExecution())
@ -234,7 +234,7 @@ bool StdScheduler::ScheduleProcs(int iTimeout)
} }
// Execute all processes with timeout // Execute all processes with timeout
tNow = GetTime(); tNow = C4TimeMilliseconds::Now();
for (i = 0; i < iProcCnt; i++) for (i = 0; i < iProcCnt; i++)
{ {
if(ppProcs[i]->IsScheduledExecution()) if(ppProcs[i]->IsScheduledExecution())
@ -272,7 +272,7 @@ bool StdScheduler::ScheduleProcs(int iTimeout)
if (cnt >= 0) if (cnt >= 0)
{ {
bool any_executed = false; bool any_executed = false;
tNow = GetTime(); tNow = C4TimeMilliseconds::Now();
// Which process? // Which process?
for (i = 0; i < iProcCnt; i++) for (i = 0; i < iProcCnt; i++)
{ {

View File

@ -102,7 +102,7 @@ public:
void SetDelay(uint32_t inDelay) { iDelay = inDelay; } void SetDelay(uint32_t inDelay) { iDelay = inDelay; }
bool CheckAndReset() bool CheckAndReset()
{ {
C4TimeMilliseconds tTime = GetTime(); C4TimeMilliseconds tTime = C4TimeMilliseconds::Now();
// first execution // first execution
if(!tLastTimer) if(!tLastTimer)
{ {

View File

@ -941,7 +941,7 @@ void C4AulExec::StartProfiling(C4AulScript *pProfiledScript)
fProfiling = true; fProfiling = true;
// resets profling times and starts recording the times // resets profling times and starts recording the times
this->pProfiledScript = pProfiledScript; this->pProfiledScript = pProfiledScript;
C4TimeMilliseconds tNow = GetTime(); C4TimeMilliseconds tNow = C4TimeMilliseconds::Now();
tDirectExecStart = tNow; // in case profiling is started from DirectExec tDirectExecStart = tNow; // in case profiling is started from DirectExec
tDirectExecTotal = 0; tDirectExecTotal = 0;
pProfiledScript->ResetProfilerTimes(); pProfiledScript->ResetProfilerTimes();
@ -974,7 +974,7 @@ void C4AulExec::PushContext(const C4AulScriptContext &rContext)
pCurCtx->dump(Buf); pCurCtx->dump(Buf);
} }
// Profiler: Safe time to measure difference afterwards // Profiler: Safe time to measure difference afterwards
if (fProfiling) pCurCtx->tTime = GetTime(); if (fProfiling) pCurCtx->tTime = C4TimeMilliseconds::Now();
} }
void C4AulExec::PopContext() void C4AulExec::PopContext()
@ -984,7 +984,7 @@ void C4AulExec::PopContext()
// Profiler adding up times // Profiler adding up times
if (fProfiling) if (fProfiling)
{ {
uint32_t dt = GetTime() - pCurCtx->tTime; uint32_t dt = C4TimeMilliseconds::Now() - pCurCtx->tTime;
if (pCurCtx->Func) if (pCurCtx->Func)
pCurCtx->Func->tProfileTime += dt; pCurCtx->Func->tProfileTime += dt;
} }

View File

@ -21,6 +21,7 @@
#define C4AULEXEC_H #define C4AULEXEC_H
#include <C4Aul.h> #include <C4Aul.h>
#include "C4TimeMilliseconds.h"
const int MAX_CONTEXT_STACK = 512; const int MAX_CONTEXT_STACK = 512;
const int MAX_VALUE_STACK = 1024; const int MAX_VALUE_STACK = 1024;
@ -65,8 +66,8 @@ public:
void StartProfiling(C4AulScript *pScript); // resets profling times and starts recording the times void StartProfiling(C4AulScript *pScript); // resets profling times and starts recording the times
void StopProfiling(); // stop the profiler and displays results void StopProfiling(); // stop the profiler and displays results
void AbortProfiling() { fProfiling=false; } void AbortProfiling() { fProfiling=false; }
inline void StartDirectExec() { if (fProfiling) tDirectExecStart = GetTime(); } inline void StartDirectExec() { if (fProfiling) tDirectExecStart = C4TimeMilliseconds::Now(); }
inline void StopDirectExec() { if (fProfiling) tDirectExecTotal += GetTime() - tDirectExecStart; } inline void StopDirectExec() { if (fProfiling) tDirectExecTotal += C4TimeMilliseconds::Now() - tDirectExecStart; }
int GetContextDepth() const { return pCurCtx - Contexts + 1; } int GetContextDepth() const { return pCurCtx - Contexts + 1; }
C4AulScriptContext *GetContext(int iLevel) { return iLevel >= 0 && iLevel < GetContextDepth() ? Contexts + iLevel : NULL; } C4AulScriptContext *GetContext(int iLevel) { return iLevel >= 0 && iLevel < GetContextDepth() ? Contexts + iLevel : NULL; }