Skip to content

Commit

Permalink
Fix percision lost when calculating the frame time. Started after ~4 …
Browse files Browse the repository at this point in the history
…minutes due to float percision limits, would be visible after ~18-36 hours when it would be off by 4-8ms.
  • Loading branch information
Jarcho committed Mar 22, 2023
1 parent cfc8e8b commit 8e3484f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/d2dx/RenderContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ void RenderContext::Present()
break;
}

double curTime = TimeEndMs(_timeStart);
_frameTimeMs = curTime - _prevTime;
auto curTime = TimeEnd(_timeStart);
_frameTimeMs = TimeToMs(curTime - _prevTime);
_prevTime = curTime;

if (_deviceContext1)
Expand Down
2 changes: 1 addition & 1 deletion src/d2dx/RenderContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ namespace d2dx
int64_t _timeStart;
bool _hasAdjustedWindowPlacement = false;

double _prevTime;
int64_t _prevTime;
double _frameTimeMs;
};
}
10 changes: 7 additions & 3 deletions src/d2dx/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ int64_t d2dx::TimeStart()
return (int64_t)li.QuadPart;
}

float d2dx::TimeEndMs(int64_t sinceThisTime)
int64_t d2dx::TimeEnd(int64_t sinceThisTime)
{
warmup();
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return li.QuadPart - sinceThisTime;
}

double d2dx::TimeToMs(int64_t time)
{
assert(_freq);
return (float)(double(li.QuadPart - sinceThisTime) / _freq);
return static_cast<double>(time) / _freq;
}

#define STATUS_SUCCESS (0x00000000)
Expand Down
3 changes: 2 additions & 1 deletion src/d2dx/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace d2dx
}

int64_t TimeStart();
float TimeEndMs(int64_t start);
int64_t TimeEnd(int64_t start);
double TimeToMs(int64_t time);


#ifdef NDEBUG
Expand Down

0 comments on commit 8e3484f

Please sign in to comment.