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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#include "os.h"
#include "irrString.h"
#include "irrMath.h"
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
#include <SDL_endian.h>
#define bswap_16(X) SDL_Swap16(X)
#define bswap_32(X) SDL_Swap32(X)
#define bswap_64(X) SDL_Swap64(X)
#elif defined(_IRR_WINDOWS_API_) && defined(_MSC_VER)
#include <stdlib.h>
#define bswap_16(X) _byteswap_ushort(X)
#define bswap_32(X) _byteswap_ulong(X)
#define bswap_64(X) _byteswap_uint64(X)
#define localtime _localtime_s
#elif defined(_IRR_OSX_PLATFORM_)
#include <libkern/OSByteOrder.h>
#define bswap_16(X) OSReadSwapInt16(&X,0)
#define bswap_32(X) OSReadSwapInt32(&X,0)
#define bswap_64(X) OSReadSwapInt64(&X,0)
#elif defined(__FreeBSD__)
#include <sys/endian.h>
#define bswap_16(X) bswap16(X)
#define bswap_32(X) bswap32(X)
#define bswap_64(X) bswap64(X)
#elif defined(__OpenBSD__)
#include <endian.h>
#define bswap_16(X) letoh16(X)
#define bswap_32(X) letoh32(X)
#define bswap_64(X) letoh64(X)
#elif !defined(_IRR_SOLARIS_PLATFORM_) && !defined(__PPC__) && !defined(_IRR_WINDOWS_API_)
#include <byteswap.h>
#else
#define bswap_16(X) ((((X)&0xFF) << 8) | (((X)&0xFF00) >> 8))
#define bswap_32(X) ((((X)&0x000000FF) << 24) | (((X)&0xFF000000) >> 24) | (((X)&0x0000FF00) << 8) | (((X) &0x00FF0000) >> 8))
#define bswap_64(X) ((((X)&0x00000000000000FF) << 56) | (((X)&0xFF00000000000000) >> 56) | (((X)&0x000000000000FF00) << 40) | (((X)&0x00FF000000000000) >> 40) | (((X)&0x0000000000FF0000) << 24) | (((X)&0x0000FF0000000000) >> 24) | (((X)&0x00000000FF000000) << 8) | (((X) &0x000000FF00000000) >> 8))
#endif
namespace irr
{
namespace os
{
u16 Byteswap::byteswap(u16 num) {return bswap_16(num);}
s16 Byteswap::byteswap(s16 num) {return bswap_16(num);}
u32 Byteswap::byteswap(u32 num) {return bswap_32(num);}
s32 Byteswap::byteswap(s32 num) {return bswap_32(num);}
u64 Byteswap::byteswap(u64 num) {return bswap_64(num);}
s64 Byteswap::byteswap(s64 num) {return bswap_64(num);}
f32 Byteswap::byteswap(f32 num) {u32 tmp=IR(num); tmp=bswap_32(tmp); return (FR(tmp));}
// prevent accidental byte swapping of chars
u8 Byteswap::byteswap(u8 num) {return num;}
c8 Byteswap::byteswap(c8 num) {return num;}
}
}
#if defined(_IRR_WINDOWS_API_)
// ----------------------------------------------------------------
// Windows specific functions
// ----------------------------------------------------------------
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <time.h>
namespace irr
{
namespace os
{
//! prints a debuginfo string
void Printer::print(const c8* message, ELOG_LEVEL ll)
{
core::stringc tmp(message);
tmp += "\n";
OutputDebugStringA(tmp.c_str());
printf("%s", tmp.c_str());
}
static LARGE_INTEGER HighPerformanceFreq;
static BOOL HighPerformanceTimerSupport = FALSE;
void Timer::initTimer(bool usePerformanceTimer)
{
if (usePerformanceTimer)
HighPerformanceTimerSupport = QueryPerformanceFrequency(&HighPerformanceFreq);
else
HighPerformanceTimerSupport = FALSE;
initVirtualTimer();
}
u32 Timer::getRealTime()
{
if (HighPerformanceTimerSupport)
{
LARGE_INTEGER nTime;
BOOL queriedOK = QueryPerformanceCounter(&nTime);
if(queriedOK)
return u32((nTime.QuadPart) * 1000 / HighPerformanceFreq.QuadPart);
}
return GetTickCount();
}
} // end namespace os
#elif defined( _IRR_ANDROID_PLATFORM_ )
// ----------------------------------------------------------------
// Android version
// ----------------------------------------------------------------
#include <android/log.h>
namespace irr
{
namespace os
{
//! prints a debuginfo string
void Printer::print(const c8* message, ELOG_LEVEL ll)
{
android_LogPriority LogLevel = ANDROID_LOG_UNKNOWN;
switch (ll)
{
case ELL_DEBUG:
LogLevel = ANDROID_LOG_DEBUG;
break;
case ELL_INFORMATION:
LogLevel = ANDROID_LOG_INFO;
break;
case ELL_WARNING:
LogLevel = ANDROID_LOG_WARN;
break;
case ELL_ERROR:
LogLevel = ANDROID_LOG_ERROR;
break;
default: // ELL_NONE
LogLevel = ANDROID_LOG_VERBOSE;
break;
}
// Android logcat restricts log-output and cuts the rest of the message away. But we want it all.
// On my device max-len is 1023 (+ 0 byte). Some websites claim a limit of 4096 so maybe different numbers on different devices.
const size_t maxLogLen = 1023;
size_t msgLen = strlen(message);
size_t start = 0;
while ( msgLen-start > maxLogLen )
{
__android_log_print(LogLevel, "Irrlicht", "%.*s\n", maxLogLen, &message[start]);
start += maxLogLen;
}
__android_log_print(LogLevel, "Irrlicht", "%s\n", &message[start]);
}
void Timer::initTimer(bool usePerformanceTimer)
{
initVirtualTimer();
}
u32 Timer::getRealTime()
{
timeval tv;
gettimeofday(&tv, 0);
return (u32)(tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}
} // end namespace os
#elif defined(_IRR_EMSCRIPTEN_PLATFORM_)
// ----------------------------------------------------------------
// emscripten version
// ----------------------------------------------------------------
#include <emscripten.h>
#include <time.h>
#include <sys/time.h>
namespace irr
{
namespace os
{
//! prints a debuginfo string
void Printer::print(const c8* message, ELOG_LEVEL ll)
{
int log_level;
switch (ll)
{
case ELL_DEBUG:
log_level=0;
break;
case ELL_INFORMATION:
log_level=0;
break;
case ELL_WARNING:
log_level=EM_LOG_WARN;
break;
case ELL_ERROR:
log_level=EM_LOG_ERROR;
break;
default: // ELL_NONE
log_level=0;
break;
}
emscripten_log(log_level, "%s", message); // Note: not adding \n as emscripten_log seems to do that already.
}
void Timer::initTimer(bool usePerformanceTimer)
{
initVirtualTimer();
}
u32 Timer::getRealTime()
{
double time = emscripten_get_now();
return (u32)(time);
}
} // end namespace os
#else
// ----------------------------------------------------------------
// linux/ansi version
// ----------------------------------------------------------------
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
namespace irr
{
namespace os
{
//! prints a debuginfo string
void Printer::print(const c8* message, ELOG_LEVEL ll)
{
printf("%s\n", message);
}
void Timer::initTimer(bool usePerformanceTimer)
{
initVirtualTimer();
}
u32 Timer::getRealTime()
{
timeval tv;
gettimeofday(&tv, 0);
return (u32)(tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}
} // end namespace os
#endif // end linux / emscripten / android / windows
namespace os
{
// The platform independent implementation of the printer
ILogger* Printer::Logger = 0;
void Printer::log(const c8* message, ELOG_LEVEL ll)
{
if (Logger)
Logger->log(message, ll);
}
void Printer::log(const wchar_t* message, ELOG_LEVEL ll)
{
if (Logger)
Logger->log(message, ll);
}
void Printer::log(const c8* message, const c8* hint, ELOG_LEVEL ll)
{
if (Logger)
Logger->log(message, hint, ll);
}
void Printer::log(const c8* message, const io::path& hint, ELOG_LEVEL ll)
{
if (Logger)
Logger->log(message, hint.c_str(), ll);
}
// ------------------------------------------------------
// virtual timer implementation
f32 Timer::VirtualTimerSpeed = 1.0f;
s32 Timer::VirtualTimerStopCounter = 0;
u32 Timer::LastVirtualTime = 0;
u32 Timer::StartRealTime = 0;
u32 Timer::StaticTime = 0;
//! Get real time and date in calendar form
ITimer::RealTimeDate Timer::getRealTimeAndDate()
{
time_t rawtime;
time(&rawtime);
struct tm * timeinfo;
timeinfo = localtime(&rawtime);
// init with all 0 to indicate error
ITimer::RealTimeDate date;
memset(&date, 0, sizeof(date));
// at least Windows returns NULL on some illegal dates
if (timeinfo)
{
// set useful values if succeeded
date.Hour=(u32)timeinfo->tm_hour;
date.Minute=(u32)timeinfo->tm_min;
date.Second=(u32)timeinfo->tm_sec;
date.Day=(u32)timeinfo->tm_mday;
date.Month=(u32)timeinfo->tm_mon+1;
date.Year=(u32)timeinfo->tm_year+1900;
date.Weekday=(ITimer::EWeekday)timeinfo->tm_wday;
date.Yearday=(u32)timeinfo->tm_yday+1;
date.IsDST=timeinfo->tm_isdst != 0;
}
return date;
}
//! returns current virtual time
u32 Timer::getTime()
{
if (isStopped())
return LastVirtualTime;
return LastVirtualTime + (u32)((StaticTime - StartRealTime) * VirtualTimerSpeed);
}
//! ticks, advances the virtual timer
void Timer::tick()
{
StaticTime = getRealTime();
}
//! sets the current virtual time
void Timer::setTime(u32 time)
{
StaticTime = getRealTime();
LastVirtualTime = time;
StartRealTime = StaticTime;
}
//! stops the virtual timer
void Timer::stopTimer()
{
if (!isStopped())
{
// stop the virtual timer
LastVirtualTime = getTime();
}
--VirtualTimerStopCounter;
}
//! starts the virtual timer
void Timer::startTimer()
{
++VirtualTimerStopCounter;
if (!isStopped())
{
// restart virtual timer
setTime(LastVirtualTime);
}
}
//! sets the speed of the virtual timer
void Timer::setSpeed(f32 speed)
{
setTime(getTime());
VirtualTimerSpeed = speed;
if (VirtualTimerSpeed < 0.0f)
VirtualTimerSpeed = 0.0f;
}
//! gets the speed of the virtual timer
f32 Timer::getSpeed()
{
return VirtualTimerSpeed;
}
//! returns if the timer currently is stopped
bool Timer::isStopped()
{
return VirtualTimerStopCounter < 0;
}
void Timer::initVirtualTimer()
{
StaticTime = getRealTime();
StartRealTime = StaticTime;
}
} // end namespace os
} // end namespace irr
|