]> git.jsancho.org Git - lugaru.git/blob - Source/WinDefs.cpp
Cleanning up Sprite class. More can be done, but it's already prettier.
[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         {
35                 counterRate = 1;
36                 baseCounter = 0;
37                 QueryPerformanceFrequency( (LARGE_INTEGER*)&counterRate);
38                 QueryPerformanceCounter( (LARGE_INTEGER*)&baseCounter);
39         }
40         __int64 counterRate;            // LARGE_INTEGER type has no math functions so use int64
41         __int64 baseCounter;
42 };
43 static AppTime g_appTime;
44
45
46
47 void CopyCStringToPascal( const char* src, unsigned char dst[256])
48 {
49         int len = strlen( src);
50         dst[0] = len;
51         memcpy( dst + 1, src, len);
52 }
53
54
55 void CopyPascalStringToC( const unsigned char* src, char* dst)
56 {
57         int len = src[0];
58         memcpy( dst, src + 1, len);
59         dst[len] = 0;
60 }
61
62
63 AbsoluteTime UpTime()
64 {
65         __int64 counter;
66         QueryPerformanceCounter( (LARGE_INTEGER*)&counter);
67
68         counter -= g_appTime.baseCounter;
69
70         AbsoluteTime time;
71         time.lo = (unsigned long)counter;
72         time.hi = (unsigned long)(counter >> 32);
73         return time;
74 }
75
76
77 Duration AbsoluteDeltaToDuration( AbsoluteTime& a, AbsoluteTime& b)
78 {
79         __int64 value = a.hi;
80         value <<= 32;
81         value |= a.lo;
82         __int64 value2 = b.hi;
83         value2 <<= 32;
84         value2 |= b.lo;
85         value -= value2;
86
87         if (value <= 0)
88                 return durationImmediate;
89
90         __int64 frac = value % g_appTime.counterRate;
91         value /= g_appTime.counterRate;
92
93         Duration time;
94
95         if (value == 0)
96         {
97                 frac *= -1000000;
98                 frac /= g_appTime.counterRate;
99                 time = (Duration)frac;
100         }
101         else
102         {
103                 frac *= 1000;
104                 frac /= g_appTime.counterRate;
105                 value *= 1000;
106                 value += frac;
107                 time = (Duration)value;
108         }
109
110         return time;
111 }
112
113
114 static char g_filename[ 256];
115 char* ConvertFileName( const char* orgfilename)
116 {
117         // translate filename into proper path name
118         if (orgfilename[ 0] == ':')
119                 orgfilename++;
120         strcpy( g_filename, orgfilename);
121
122         for (int n = 0; g_filename[ n]; n++)
123         {
124                 if (g_filename[ n] == ':')
125                         g_filename[ n] = '/';
126         }
127
128         return g_filename;
129 }
130
131 char* ConvertFileName( const char* orgfilename, const char* junk)
132 {
133         return ConvertFileName(orgfilename);
134 }