Optimize headers
[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 #ifdef _WIN32
39         #include <windows.h>
40         #define sleep_ms(x) Sleep(x)
41 #else
42         #include <unistd.h>
43         #define sleep_ms(x) usleep(x*1000)
44 #endif
45
46 namespace porting
47 {
48
49 /*
50         Signal handler (grabs Ctrl-C on POSIX systems)
51 */
52
53 void signal_handler_init(void);
54 // Returns a pointer to a bool.
55 // When the bool is true, program should quit.
56 bool * signal_handler_killstatus(void);
57
58 /*
59         Path of static data directory.
60 */
61 extern std::string path_share;
62
63 /*
64         Directory for storing user data. Examples:
65         Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
66         Linux: "~/.<PROJECT_NAME>"
67         Mac: "~/Library/Application Support/<PROJECT_NAME>"
68 */
69 extern std::string path_user;
70
71 /*
72         Get full path of stuff in data directory.
73         Example: "stone.png" -> "../data/stone.png"
74 */
75 std::string getDataPath(const char *subpath);
76
77 /*
78         Initialize path_share and path_user.
79 */
80 void initializePaths();
81
82 /*
83         Resolution is 10-20ms.
84         Remember to check for overflows.
85         Overflow can occur at any value higher than 10000000.
86 */
87 #ifdef _WIN32 // Windows
88         #include <windows.h>
89         inline u32 getTimeMs()
90         {
91                 return GetTickCount();
92         }
93 #else // Posix
94         #include <sys/time.h>
95         inline u32 getTimeMs()
96         {
97                 struct timeval tv;
98                 gettimeofday(&tv, NULL);
99                 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
100         }
101         /*#include <sys/timeb.h>
102         inline u32 getTimeMs()
103         {
104                 struct timeb tb;
105                 ftime(&tb);
106                 return tb.time * 1000 + tb.millitm;
107         }*/
108 #endif
109
110 } // namespace porting
111
112 #endif // PORTING_HEADER
113