Merge pull request #442 from kwolekr/mingw_compile_fix
[oweals/minetest.git] / src / porting.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /*
21         Random portability stuff
22 */
23
24 #ifndef PORTING_HEADER
25 #define PORTING_HEADER
26
27 #include <string>
28 #include "irrlichttypes.h" // u32
29 #include "debug.h"
30 #include "constants.h"
31
32 #ifdef _MSC_VER
33         #define SWPRINTF_CHARSTRING L"%S"
34 #else
35         #define SWPRINTF_CHARSTRING L"%s"
36 #endif
37
38 //currently not needed
39 //template<typename T> struct alignment_trick { char c; T member; };
40 //#define ALIGNOF(type) offsetof (alignment_trick<type>, member)
41
42 #ifdef _WIN32
43         #include <windows.h>
44         
45         #define sleep_ms(x) Sleep(x)
46 #else
47         #include <unistd.h>
48         #include <stdint.h> //for uintptr_t
49         
50         #define sleep_ms(x) usleep(x*1000)
51 #endif
52
53 #ifdef _MSC_VER
54         #define ALIGNOF(x) __alignof(x)
55         #define strtok_r(x, y, z) strtok_s(x, y, z)
56         #define strtof(x, y) (float)strtod(x, y)
57         #define strtoll(x, y, z) _strtoi64(x, y, z)
58         #define strtoull(x, y, z) _strtoui64(x, y, z)
59 #else
60         #define ALIGNOF(x) __alignof__(x)
61 #endif
62
63 #ifdef __MINGW32__
64         #define strtok_r(x, y, z) mystrtok_r(x, y, z)
65 #endif
66
67 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
68
69 namespace porting
70 {
71
72 /*
73         Signal handler (grabs Ctrl-C on POSIX systems)
74 */
75
76 void signal_handler_init(void);
77 // Returns a pointer to a bool.
78 // When the bool is true, program should quit.
79 bool * signal_handler_killstatus(void);
80
81 /*
82         Path of static data directory.
83 */
84 extern std::string path_share;
85
86 /*
87         Directory for storing user data. Examples:
88         Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
89         Linux: "~/.<PROJECT_NAME>"
90         Mac: "~/Library/Application Support/<PROJECT_NAME>"
91 */
92 extern std::string path_user;
93
94 /*
95         Get full path of stuff in data directory.
96         Example: "stone.png" -> "../data/stone.png"
97 */
98 std::string getDataPath(const char *subpath);
99
100 /*
101         Initialize path_share and path_user.
102 */
103 void initializePaths();
104
105 /*
106         Resolution is 10-20ms.
107         Remember to check for overflows.
108         Overflow can occur at any value higher than 10000000.
109 */
110 #ifdef _WIN32 // Windows
111         #include <windows.h>
112         inline u32 getTimeMs()
113         {
114                 return GetTickCount();
115         }
116 #else // Posix
117         #include <sys/time.h>
118         inline u32 getTimeMs()
119         {
120                 struct timeval tv;
121                 gettimeofday(&tv, NULL);
122                 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
123         }
124         /*#include <sys/timeb.h>
125         inline u32 getTimeMs()
126         {
127                 struct timeb tb;
128                 ftime(&tb);
129                 return tb.time * 1000 + tb.millitm;
130         }*/
131 #endif
132
133 } // namespace porting
134
135 #endif // PORTING_HEADER
136