]> git.jsancho.org Git - lugaru.git/blob - Source/MacCompatibility.cpp
Added braces to all statements with clang-tidy and ran clang-format again
[lugaru.git] / Source / MacCompatibility.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "MacCompatibility.hpp"
22
23 #include <cerrno>
24 #include <cstdio>
25 #include <cstdlib>
26 #include <cstring>
27 #include <ctime>
28
29 #ifdef WIN32
30 #include <windows.h>
31 #endif
32
33 #if PLATFORM_UNIX
34 #include <assert.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <unistd.h>
38
39 typedef long long __int64;
40 typedef __int64 LARGE_INTEGER;
41 static int QueryPerformanceFrequency(LARGE_INTEGER* liptr)
42 {
43     assert(sizeof(__int64) == 8);
44     assert(sizeof(LARGE_INTEGER) == 8);
45     *liptr = 1000;
46     return (1);
47 }
48
49 static int QueryPerformanceCounter(LARGE_INTEGER* liptr)
50 {
51     struct timeval tv;
52     gettimeofday(&tv, NULL);
53     *liptr = ((((LARGE_INTEGER)tv.tv_sec) * 1000) +
54               (((LARGE_INTEGER)tv.tv_usec) / 1000));
55     return (1);
56 }
57 #endif
58
59 class AppTime
60 {
61   public:
62     AppTime()
63     {
64         counterRate = 1;
65         baseCounter = 0;
66         QueryPerformanceFrequency((LARGE_INTEGER*)&counterRate);
67         QueryPerformanceCounter((LARGE_INTEGER*)&baseCounter);
68     }
69     __int64 counterRate; // LARGE_INTEGER type has no math functions so use int64
70     __int64 baseCounter;
71 };
72 static AppTime g_appTime;
73
74 AbsoluteTime UpTime()
75 {
76     __int64 counter;
77     QueryPerformanceCounter((LARGE_INTEGER*)&counter);
78
79     counter -= g_appTime.baseCounter;
80
81     AbsoluteTime time;
82     time.lo = (unsigned long)counter;
83     time.hi = (unsigned long)(counter >> 32);
84     return time;
85 }
86
87 Duration AbsoluteDeltaToDuration(AbsoluteTime& a, AbsoluteTime& b)
88 {
89     __int64 value = a.hi;
90     value <<= 32;
91     value |= a.lo;
92     __int64 value2 = b.hi;
93     value2 <<= 32;
94     value2 |= b.lo;
95     value -= value2;
96
97     if (value <= 0) {
98         return durationImmediate;
99     }
100
101     __int64 frac = value % g_appTime.counterRate;
102     value /= g_appTime.counterRate;
103
104     Duration time;
105
106     if (value == 0) {
107         frac *= -1000000;
108         frac /= g_appTime.counterRate;
109         time = (Duration)frac;
110     } else {
111         frac *= 1000;
112         frac /= g_appTime.counterRate;
113         value *= 1000;
114         value += frac;
115         time = (Duration)value;
116     }
117
118     return time;
119 }