]> git.jsancho.org Git - lugaru.git/blob - Source/WinDefs.cpp
CLEANED UP WHITESPACE
[lugaru.git] / Source / WinDefs.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15 See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21
22 /**> HEADER FILES <**/
23 #include "WinDefs.h"
24 #include <windows.h>
25 #include <errno.h>
26 #include <time.h>
27 #include <stdio.h>
28
29
30 class AppTime
31 {
32 public:
33     AppTime() {
34         counterRate = 1;
35         baseCounter = 0;
36         QueryPerformanceFrequency( (LARGE_INTEGER*)&counterRate);
37         QueryPerformanceCounter( (LARGE_INTEGER*)&baseCounter);
38     }
39     __int64 counterRate; // LARGE_INTEGER type has no math functions so use int64
40     __int64 baseCounter;
41 };
42 static AppTime g_appTime;
43
44
45
46 void CopyCStringToPascal( const char* src, unsigned char dst[256])
47 {
48     int len = strlen( src);
49     dst[0] = len;
50     memcpy( dst + 1, src, len);
51 }
52
53
54 void CopyPascalStringToC( const unsigned char* src, char* dst)
55 {
56     int len = src[0];
57     memcpy( dst, src + 1, len);
58     dst[len] = 0;
59 }
60
61
62 AbsoluteTime UpTime()
63 {
64     __int64 counter;
65     QueryPerformanceCounter( (LARGE_INTEGER*)&counter);
66
67     counter -= g_appTime.baseCounter;
68
69     AbsoluteTime time;
70     time.lo = (unsigned long)counter;
71     time.hi = (unsigned long)(counter >> 32);
72     return time;
73 }
74
75
76 Duration AbsoluteDeltaToDuration( AbsoluteTime& a, AbsoluteTime& b)
77 {
78     __int64 value = a.hi;
79     value <<= 32;
80     value |= a.lo;
81     __int64 value2 = b.hi;
82     value2 <<= 32;
83     value2 |= b.lo;
84     value -= value2;
85
86     if (value <= 0)
87         return durationImmediate;
88
89     __int64 frac = value % g_appTime.counterRate;
90     value /= g_appTime.counterRate;
91
92     Duration time;
93
94     if (value == 0) {
95         frac *= -1000000;
96         frac /= g_appTime.counterRate;
97         time = (Duration)frac;
98     } else {
99         frac *= 1000;
100         frac /= g_appTime.counterRate;
101         value *= 1000;
102         value += frac;
103         time = (Duration)value;
104     }
105
106     return time;
107 }
108
109
110 static char g_filename[ 256];
111 char* ConvertFileName( const char* orgfilename)
112 {
113     // translate filename into proper path name
114     if (orgfilename[ 0] == ':')
115         orgfilename++;
116     strcpy( g_filename, orgfilename);
117
118     for (int n = 0; g_filename[ n]; n++) {
119         if (g_filename[ n] == ':')
120             g_filename[ n] = '/';
121     }
122
123     return g_filename;
124 }
125
126 char* ConvertFileName( const char* orgfilename, const char* junk)
127 {
128     return ConvertFileName(orgfilename);
129 }