-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtility.cpp
More file actions
104 lines (87 loc) · 2.56 KB
/
Utility.cpp
File metadata and controls
104 lines (87 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// ****************************************************************************
// File: Utility.cpp
// Desc: Utility functions
//
// ****************************************************************************
#include "stdafx.h"
// Integer to to double seconds
#define I2TIME(_int) ((double) (_int) * (double) ((double) 1.0 / (double) 1000.0))
// ==== Data ====
static ALIGN(16) double s_fTimeStampHolder = 0;
static DWORD s_dwLastTimeRead = 0;
// Trace output to debug channel
void Trace(LPCTSTR pszFormat, ...)
{
if(pszFormat)
{
va_list vl;
char szBuffer[4096];
va_start(vl, pszFormat);
::qvsnprintf(szBuffer, (sizeof(szBuffer) - 1), pszFormat, vl);
szBuffer[sizeof(szBuffer) - 1] = 0;
va_end(vl);
OutputDebugString(szBuffer);
}
}
// ****************************************************************************
// Func: GetTimeSamp()
// Desc: Get high precision elapsed seconds
//
// ****************************************************************************
TIMESTAMP GetTimeStamp()
{
LARGE_INTEGER tLarge;
QueryPerformanceCounter(&tLarge);
static ALIGN(16) TIMESTAMP s_ClockFreq;
if(s_ClockFreq == 0.0)
{
// Set the minimal timeBeginPeriod()
// The best ms accuracy of both "timeGetTime()" and "Sleep()"
int iPeriod = 1;
while(iPeriod < 20)
{
if(timeBeginPeriod(iPeriod) == TIMERR_NOERROR)
break;
};
LARGE_INTEGER tLarge;
QueryPerformanceFrequency(&tLarge);
s_ClockFreq = (TIMESTAMP) tLarge.QuadPart;
}
return((TIMESTAMP) tLarge.QuadPart / s_ClockFreq);
}
// Get delta time stamp, lower precision but much less overhead
TIMESTAMP GetTimeStampLow()
{
// Time with ms precision
DWORD dwTime = timeGetTime();
// Get delta time
DWORD dwDelta;
if(dwTime >= s_dwLastTimeRead)
dwDelta = (dwTime - s_dwLastTimeRead);
else
// Rolled over.. (happens every ~49.71 days of computer time)
dwDelta = (s_dwLastTimeRead - dwTime);
s_dwLastTimeRead = dwTime;
s_fTimeStampHolder += I2TIME(dwDelta);
return(s_fTimeStampHolder);
}
// ****************************************************************************
// Func: Log()
// Desc: Send text to a log file.
//
// ****************************************************************************
void Log(FILE *pLogFile, const char *format, ...)
{
if(pLogFile && format)
{
// Format string
va_list vl;
char str[4096] = {0};
va_start(vl, format);
_vsnprintf(str, (sizeof(str) - 1), format, vl);
va_end(vl);
// Out to file
qfputs(str, pLogFile);
qflush(pLogFile);
}
}