1 /*****************************************************************************
2  * Copyright (c) 2014-2020 OpenRCT2 developers
3  *
4  * For a complete list of all authors, please refer to contributors.md
5  * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
6  *
7  * OpenRCT2 is licensed under the GNU General Public License version 3.
8  *****************************************************************************/
9 
10 #pragma once
11 
12 #ifndef _USE_MATH_DEFINES
13 #    define _USE_MATH_DEFINES
14 #endif
15 #undef M_PI
16 
17 #ifdef _MSC_VER
18 #    include <ctime>
19 #endif
20 
21 #include "Diagnostic.h"
22 
23 #include <cassert>
24 #include <cstddef>
25 #include <cstdint>
26 
27 using utf8 = char;
28 using utf8string = utf8*;
29 using const_utf8string = const utf8*;
30 
31 // Define MAX_PATH for various headers that don't want to include system headers
32 // just for MAX_PATH
33 #ifndef MAX_PATH
34 #    define MAX_PATH 260
35 #endif
36 
37 using codepoint_t = uint32_t;
38 using colour_t = uint8_t;
39 
40 // Gets the name of a symbol as a C string
41 #define nameof(symbol) #symbol
42 
43 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
44 #    include <unistd.h>
45 #    define STUB() log_warning("Function %s at %s:%d is a stub.", __PRETTY_FUNCTION__, __FILE__, __LINE__)
46 #    define _strcmpi _stricmp
47 #    define _stricmp(x, y) strcasecmp((x), (y))
48 #    define _strnicmp(x, y, n) strncasecmp((x), (y), (n))
49 #    define _strdup(x) strdup((x))
50 
51 #    if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
52 #        define RCT2_ENDIANNESS __ORDER_LITTLE_ENDIAN__
53 #        define LOBYTE(w) (static_cast<uint8_t>(w))
54 #        define HIBYTE(w) (static_cast<uint8_t>((static_cast<uint16_t>(w) >> 8) & 0xFF))
55 #    endif // __BYTE_ORDER__
56 
57 #    ifndef RCT2_ENDIANNESS
58 #        error Unknown endianness!
59 #    endif // RCT2_ENDIANNESS
60 
61 #endif // defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
62 
63 #ifdef _WIN32
64 char* strndup(const char* src, size_t size);
65 #endif
66 
67 // BSD and macOS have MAP_ANON instead of MAP_ANONYMOUS
68 #ifndef MAP_ANONYMOUS
69 #    define MAP_ANONYMOUS MAP_ANON
70 #endif
71 
72 #define OPENRCT2_MASTER_SERVER_URL "https://servers.openrct2.io"
73 
74 // Time (represented as number of 100-nanosecond intervals since 0001-01-01T00:00:00Z)
75 using datetime64 = uint64_t;
76 
77 constexpr const datetime64 DATETIME64_MIN = 0;
78 
79 // Represent fixed point numbers. dp = decimal point
80 using fixed8_1dp = uint8_t;
81 using fixed8_2dp = uint8_t;
82 using fixed16_1dp = int16_t;
83 using fixed16_2dp = int16_t;
84 using fixed32_1dp = int32_t;
85 using fixed32_2dp = int32_t;
86 using fixed64_1dp = int64_t;
87 
88 // Money is stored as a multiple of 0.10.
89 using money8 = fixed8_1dp;
90 using money16 = fixed16_1dp;
91 using money32 = fixed32_1dp;
92 using money64 = fixed64_1dp;
93 
94 // Construct a fixed point number. For example, to create the value 3.65 you
95 // would write FIXED_2DP(3,65)
96 #define FIXED_XDP(x, whole, fraction) ((whole) * (10 * (x)) + (fraction))
97 #define FIXED_1DP(whole, fraction) FIXED_XDP(1, whole, fraction)
98 #define FIXED_2DP(whole, fraction) FIXED_XDP(10, whole, fraction)
99 
100 // Construct a money value in the format MONEY(10,70) to represent 10.70. Fractional part must be two digits.
101 #define MONEY(whole, fraction) ((whole)*10 + ((fraction) / 10))
102 
103 #define MONEY_FREE MONEY(0, 00)
104 #define MONEY16_UNDEFINED static_cast<money16>(static_cast<uint16_t>(0xFFFF))
105 #define MONEY32_UNDEFINED (static_cast<money32>(0x80000000))
106 #define MONEY64_UNDEFINED (static_cast<money64>(0x8000000000000000))
107 
ToMoney64(money32 value)108 constexpr money64 ToMoney64(money32 value)
109 {
110     return value == MONEY32_UNDEFINED ? MONEY64_UNDEFINED : value;
111 }
112 
ToMoney64(money16 value)113 constexpr money64 ToMoney64(money16 value)
114 {
115     return value == MONEY16_UNDEFINED ? MONEY64_UNDEFINED : value;
116 }
117 
ToMoney32(money64 value)118 constexpr money32 ToMoney32(money64 value)
119 {
120     return value == MONEY64_UNDEFINED ? MONEY32_UNDEFINED : static_cast<money32>(value);
121 }
122 
ToMoney16(money64 value)123 constexpr money16 ToMoney16(money64 value)
124 {
125     return value == MONEY64_UNDEFINED ? MONEY16_UNDEFINED : static_cast<money16>(value);
126 }
127 
128 using EMPTY_ARGS_VOID_POINTER = void();
129 using rct_string_id = uint16_t;
130 
131 constexpr uint16_t SPRITE_INDEX_NULL = 0xFFFF;
132 
133 #define SafeFree(x)                                                                                                            \
134     do                                                                                                                         \
135     {                                                                                                                          \
136         free(x);                                                                                                               \
137         (x) = nullptr;                                                                                                         \
138     } while (false)
139 
140 #define SafeDelete(x)                                                                                                          \
141     do                                                                                                                         \
142     {                                                                                                                          \
143         delete (x);                                                                                                            \
144         (x) = nullptr;                                                                                                         \
145     } while (false)
146 #define SafeDeleteArray(x)                                                                                                     \
147     do                                                                                                                         \
148     {                                                                                                                          \
149         delete[](x);                                                                                                           \
150         (x) = nullptr;                                                                                                         \
151     } while (false)
152 
153 #define abstract = 0
154 
155 #if defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
156 #    define OPENRCT2_X86
157 #elif defined(_MSC_VER) && (_MSC_VER >= 1500) && (defined(_M_X64) || defined(_M_IX86)) // VS2008
158 #    define OPENRCT2_X86
159 #endif
160 
161 #if defined(__i386__) || defined(_M_IX86)
162 #    define PLATFORM_X86
163 #endif
164 
165 #if defined(__LP64__) || defined(_WIN64)
166 #    define PLATFORM_64BIT
167 #else
168 #    define PLATFORM_32BIT
169 #endif
170 
171 // C99's restrict keywords guarantees the pointer in question, for the whole of its lifetime,
172 // will be the only way to access a given memory region. In other words: there is no other pointer
173 // aliasing the same memory area. Using it lets compiler generate better code. If your compiler
174 // does not support it, feel free to drop it, at some performance hit.
175 #ifdef _MSC_VER
176 #    define RESTRICT __restrict
177 #else
178 #    define RESTRICT __restrict__
179 #endif
180 
181 #define assert_struct_size(x, y) static_assert(sizeof(x) == (y), "Improper struct size")
182 
183 #ifdef PLATFORM_X86
184 #    ifndef FASTCALL
185 #        ifdef __GNUC__
186 #            define FASTCALL __attribute__((fastcall))
187 #        elif defined(_MSC_VER)
188 #            define FASTCALL __fastcall
189 #        else
190 #            pragma message "Not using fastcall calling convention, please check your compiler support"
191 #            define FASTCALL
192 #        endif
193 #    endif // FASTCALL
194 #else      // PLATFORM_X86
195 #    define FASTCALL
196 #endif // PLATFORM_X86
197