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