Skip to content

Commit

Permalink
Should fix #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan committed Oct 11, 2014
1 parent caef04c commit a3f5faf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
26 changes: 17 additions & 9 deletions src/main.cpp
Expand Up @@ -28,7 +28,11 @@
extern void *pAMXFunctions;
logprintf_t logprintf;
std::map<int, struct timer*> timers;
unsigned long long startTime = 0;
#ifdef WIN32
LARGE_INTEGER startTime;
#else
struct timespec startTime;
#endif
int lastTimerId = 1;

const AMX_NATIVE_INFO NATIVES[] = {
Expand All @@ -51,23 +55,25 @@ const AMX_NATIVE_INFO NATIVES[] = {

#ifdef WIN32
unsigned long long freq;
unsigned long long getMsTime() {
unsigned long long getRelativeMsTime() {
LARGE_INTEGER t;
QueryPerformanceCounter(&t);
return t.QuadPart / freq;
return (t.QuadPart - startTime.QuadPart) / freq;
}
#else
unsigned long long getMsTime() {
unsigned long long getRelativeMsTime() {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
t.tv_sec -= startTime.tv_sec;
t.tv_nsec -= startTime.tv_nsec;
if (t.tv_nsec < 0) {
t.tv_sec -= 1;
t.tv_nsec += 1000000000;
}
return t.tv_sec * 1000 + t.tv_nsec / 1000000;
}
#endif

unsigned long long getRelativeMsTime() {
return getMsTime() - startTime;
}

int createTimer(AMX *amx, cell playerid, cell funcname, cell interval, cell delay, cell repeat, cell format, cell *params) {
struct timer *t = (struct timer*) malloc(sizeof(struct timer));
if (t == NULL) {
Expand Down Expand Up @@ -230,8 +236,10 @@ PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData) {
LARGE_INTEGER t;
QueryPerformanceFrequency(&t);
freq = t.QuadPart / 1000;
QueryPerformanceCounter(&startTime);
#else
clock_gettime(CLOCK_MONOTONIC, &startTime);
#endif
startTime = getMsTime();
logprintf(" >> TimerFix " PLUGIN_VERSION " successfully loaded.");
return true;
}
Expand Down
6 changes: 5 additions & 1 deletion src/main.h
Expand Up @@ -65,7 +65,11 @@ struct timer {
};

extern std::map<int, struct timer*> timers;
extern unsigned long long startTime;
#ifdef WIN32
extern LARGE_INTEGER startTime;
#else
extern struct timespec startTime;
#endif

extern unsigned long long getMsTime();
extern unsigned long long getRelativeMsTime();
Expand Down

0 comments on commit a3f5faf

Please sign in to comment.