Skip to content

Commit 1cf140c

Browse files
committed
Fixed some bugs re improved timers
1 parent b44c7b5 commit 1cf140c

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

mainfrm.cpp

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,16 @@ void ListAccelerators (CDocument * pDoc, const int iType);
3838

3939
static TCHAR BASED_CODE szCtrlBars[] = _T("CtrlBars");
4040

41-
LARGE_INTEGER hp_timeLastTimerFired;
42-
CTime timeLastTimerFired;
41+
// we need two timers, because processing a tick shouldn't reset timer processing
42+
// nor vice-versa
43+
44+
// for normal timers
45+
LARGE_INTEGER hp_timeLastTimerFired; // if we have high-resolution timer
46+
CTime timeLastTimerFired; // otherwise
47+
48+
// for OnPluginTick
49+
LARGE_INTEGER hp_timeLastTickFired; // if we have high-resolution timer
50+
CTime timeLastTickFired; // otherwise
4351

4452
int ActivityToolBarResourceNames [6] = {
4553
IDB_ACTIVITY_TOOLBAR_0,
@@ -407,9 +415,15 @@ int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
407415

408416
// initialize timer fallback
409417
if (App.m_iCounterFrequency)
418+
{
410419
QueryPerformanceCounter (&hp_timeLastTimerFired);
420+
QueryPerformanceCounter (&hp_timeLastTickFired);
421+
}
411422
else
423+
{
412424
timeLastTimerFired = CTime::GetCurrentTime();
425+
timeLastTickFired = CTime::GetCurrentTime();
426+
}
413427

414428
return 0;
415429
} // CMainFrame::OnCreate
@@ -2256,36 +2270,50 @@ void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
22562270

22572271
void CMainFrame::CheckTimerFallback ()
22582272
{
2259-
double fElapsedTime;
2273+
double fElapsedTime; // normal timers
2274+
double fElapsedTick; // OnPluginTick timer
22602275

22612276
if (App.m_iCounterFrequency)
22622277
{
22632278
LARGE_INTEGER timeNow;
22642279

22652280
QueryPerformanceCounter (&timeNow);
22662281

2267-
LONGLONG iTimeTaken = iTimeTaken = timeNow.QuadPart - hp_timeLastTimerFired.QuadPart;
2282+
LONGLONG iTimeTaken;
2283+
2284+
// normal timers
2285+
iTimeTaken = timeNow.QuadPart - hp_timeLastTimerFired.QuadPart;
2286+
fElapsedTime = ((double) iTimeTaken) / ((double) App.m_iCounterFrequency);
22682287

2269-
fElapsedTime = ((double) iTimeTaken) /
2270-
((double) App.m_iCounterFrequency);
2288+
// OnPluginTick timer
2289+
iTimeTaken = timeNow.QuadPart - hp_timeLastTickFired.QuadPart;
2290+
fElapsedTick = ((double) iTimeTaken) / ((double) App.m_iCounterFrequency);
22712291
}
2272-
else
2292+
else // 1-second resolution if no high-resolution timer available
22732293
{
2274-
fElapsedTime = (double) timeLastTimerFired.GetTime () - (double) CTime::GetCurrentTime().GetTime ();
2294+
fElapsedTime = (double) CTime::GetCurrentTime().GetTime () - (double) timeLastTimerFired.GetTime ();
2295+
fElapsedTick = (double) CTime::GetCurrentTime().GetTime () - (double) timeLastTickFired.GetTime ();
22752296
}
22762297

22772298
// first do tick timers
22782299

2279-
if (fElapsedTime > 0.040) // more than 40 milliseconds has elapsed
2300+
if (fElapsedTick > 0.040) // more than 40 milliseconds has elapsed
22802301
{
22812302
for (POSITION pos = App.m_pWorldDocTemplate->GetFirstDocPosition(); pos;)
22822303
{
22832304
CMUSHclientDoc * pDoc = (CMUSHclientDoc*) App.m_pWorldDocTemplate->GetNextDoc(pos);
22842305
pDoc->CheckTickTimers ();
2285-
}
2286-
}
2306+
} // end for
2307+
2308+
// so we know when we prccessed this tick
2309+
if (App.m_iCounterFrequency)
2310+
QueryPerformanceCounter (&hp_timeLastTickFired);
2311+
else
2312+
timeLastTickFired = CTime::GetCurrentTime();
2313+
2314+
} // if time for OnPluginTick
22872315

2288-
double fInterval = 0.10; // 10th of a second
2316+
double fInterval = 0.10; // 10th of a second (if m_nTimerInterval is zero)
22892317

22902318
// if non-zero interval is seconds
22912319
if (App.m_nTimerInterval)

0 commit comments

Comments
 (0)