2014-10-06 18:36:58 +02:00
|
|
|
/*****************************************************************************
|
2018-06-15 14:07:34 +02:00
|
|
|
* Copyright (c) 2014-2018 OpenRCT2 developers
|
2015-09-22 22:45:03 +02:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
2015-09-22 22:45:03 +02:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2014-10-06 18:36:58 +02:00
|
|
|
*****************************************************************************/
|
2015-09-22 22:45:03 +02:00
|
|
|
|
2014-10-06 18:36:58 +02:00
|
|
|
#ifndef _COMMON_H_
|
|
|
|
#define _COMMON_H_
|
|
|
|
|
2016-11-30 12:07:10 +01:00
|
|
|
#ifndef _USE_MATH_DEFINES
|
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
#endif
|
|
|
|
#undef M_PI
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2018-03-13 23:13:59 +01:00
|
|
|
#include <ctime>
|
2016-11-30 12:07:10 +01:00
|
|
|
#endif
|
|
|
|
|
2018-06-22 23:25:16 +02:00
|
|
|
#include "Diagnostic.h"
|
|
|
|
|
2018-03-13 23:13:59 +01:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
2016-11-30 12:07:10 +01:00
|
|
|
|
2018-06-22 23:25:16 +02:00
|
|
|
using utf8 = char;
|
|
|
|
using utf8string = utf8*;
|
|
|
|
using const_utf8string = const utf8*;
|
2017-06-12 19:00:15 +02:00
|
|
|
#ifdef _WIN32
|
2018-02-14 09:42:26 +01:00
|
|
|
using utf16 = wchar_t;
|
|
|
|
using utf16string = utf16*;
|
2016-11-30 12:07:10 +01:00
|
|
|
#endif
|
|
|
|
|
2017-12-07 02:18:21 +01:00
|
|
|
// Define MAX_PATH for various headers that don't want to include system headers
|
|
|
|
// just for MAX_PATH
|
2017-12-07 02:07:26 +01:00
|
|
|
#ifndef MAX_PATH
|
|
|
|
#define MAX_PATH 260
|
|
|
|
#endif
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
using codepoint_t = uint32_t;
|
|
|
|
using colour_t = uint8_t;
|
2016-11-30 12:07:10 +01:00
|
|
|
|
2018-06-22 23:25:16 +02:00
|
|
|
#define rol8(x, shift) (((uint8_t)(x) << (shift)) | ((uint8_t)(x) >> (8 - (shift))))
|
|
|
|
#define ror8(x, shift) (((uint8_t)(x) >> (shift)) | ((uint8_t)(x) << (8 - (shift))))
|
|
|
|
#define rol16(x, shift) (((uint16_t)(x) << (shift)) | ((uint16_t)(x) >> (16 - (shift))))
|
|
|
|
#define ror16(x, shift) (((uint16_t)(x) >> (shift)) | ((uint16_t)(x) << (16 - (shift))))
|
|
|
|
#define rol32(x, shift) (((uint32_t)(x) << (shift)) | ((uint32_t)(x) >> (32 - (shift))))
|
|
|
|
#define ror32(x, shift) (((uint32_t)(x) >> (shift)) | ((uint32_t)(x) << (32 - (shift))))
|
|
|
|
#define rol64(x, shift) (((uint64_t)(x) << (shift)) | ((uint64_t)(x) >> (64 - (shift))))
|
|
|
|
#define ror64(x, shift) (((uint64_t)(x) >> (shift)) | ((uint64_t)(x) << (64 - (shift))))
|
2016-11-30 12:07:10 +01:00
|
|
|
|
|
|
|
// Rounds an integer down to the given power of 2. y must be a power of 2.
|
2018-06-22 23:25:16 +02:00
|
|
|
#define floor2(x, y) ((x) & (~((y)-1)))
|
2016-11-30 12:07:10 +01:00
|
|
|
|
|
|
|
// Rounds an integer up to the given power of 2. y must be a power of 2.
|
2018-06-22 23:25:16 +02:00
|
|
|
#define ceil2(x, y) (((x) + (y)-1) & (~((y)-1)))
|
2016-11-30 12:07:10 +01:00
|
|
|
|
2017-02-25 17:07:08 +01:00
|
|
|
// Gets the name of a symbol as a C string
|
|
|
|
#define nameof(symbol) #symbol
|
|
|
|
|
2016-11-30 12:07:10 +01:00
|
|
|
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
|
|
|
#include <unistd.h>
|
|
|
|
#define STUB() log_warning("Function %s at %s:%d is a stub.\n", __PRETTY_FUNCTION__, __FILE__, __LINE__)
|
|
|
|
#define _strcmpi _stricmp
|
|
|
|
#define _stricmp(x, y) strcasecmp((x), (y))
|
|
|
|
#define _strnicmp(x, y, n) strncasecmp((x), (y), (n))
|
|
|
|
#define _strdup(x) strdup((x))
|
|
|
|
|
|
|
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
|
|
#define RCT2_ENDIANESS __ORDER_LITTLE_ENDIAN__
|
2018-06-20 17:28:51 +02:00
|
|
|
#define LOBYTE(w) ((uint8_t)(w))
|
2018-06-22 23:25:16 +02:00
|
|
|
#define HIBYTE(w) ((uint8_t)(((uint16_t)(w) >> 8) & 0xFF))
|
2016-11-30 12:07:10 +01:00
|
|
|
#endif // __BYTE_ORDER__
|
|
|
|
|
|
|
|
#ifndef RCT2_ENDIANESS
|
|
|
|
#error Unknown endianess!
|
|
|
|
#endif // RCT2_ENDIANESS
|
|
|
|
|
|
|
|
#endif // defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
|
|
|
|
2018-06-22 23:25:16 +02:00
|
|
|
#if !( \
|
|
|
|
(defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 700) \
|
|
|
|
|| (defined(__APPLE__) && defined(__MACH__)) || defined(__ANDROID_API__) || defined(__FreeBSD__))
|
|
|
|
char* strndup(const char* src, size_t size);
|
2016-11-30 12:07:10 +01:00
|
|
|
#endif // !(POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700)
|
|
|
|
|
|
|
|
// BSD and macOS have MAP_ANON instead of MAP_ANONYMOUS
|
|
|
|
#ifndef MAP_ANONYMOUS
|
|
|
|
#define MAP_ANONYMOUS MAP_ANON
|
|
|
|
#endif
|
|
|
|
|
2018-06-22 23:25:16 +02:00
|
|
|
#define OPENRCT2_MASTER_SERVER_URL "https://servers.openrct2.io"
|
2016-11-30 12:07:10 +01:00
|
|
|
|
|
|
|
// Time (represented as number of 100-nanosecond intervals since 0001-01-01T00:00:00Z)
|
2018-06-20 17:28:51 +02:00
|
|
|
using datetime64 = uint64_t;
|
2016-11-30 12:07:10 +01:00
|
|
|
|
|
|
|
#define DATETIME64_MIN ((datetime64)0)
|
|
|
|
|
|
|
|
// Represent fixed point numbers. dp = decimal point
|
2018-06-20 17:28:51 +02:00
|
|
|
using fixed8_1dp = uint8_t;
|
|
|
|
using fixed8_2dp = uint8_t;
|
|
|
|
using fixed16_1dp = int16_t;
|
|
|
|
using fixed16_2dp = int16_t;
|
|
|
|
using fixed32_1dp = int32_t;
|
|
|
|
using fixed32_2dp = int32_t;
|
|
|
|
using fixed64_1dp = int64_t;
|
2016-11-30 12:07:10 +01:00
|
|
|
|
|
|
|
// Money is stored as a multiple of 0.10.
|
2018-02-14 09:42:26 +01:00
|
|
|
using money8 = fixed8_1dp;
|
|
|
|
using money16 = fixed16_1dp;
|
|
|
|
using money32 = fixed32_1dp;
|
|
|
|
using money64 = fixed64_1dp;
|
2016-11-30 12:07:10 +01:00
|
|
|
|
|
|
|
// Construct a fixed point number. For example, to create the value 3.65 you
|
|
|
|
// would write FIXED_2DP(3,65)
|
2018-06-22 23:25:16 +02:00
|
|
|
#define FIXED_XDP(x, whole, fraction) ((whole) * (10 * (x)) + (fraction))
|
|
|
|
#define FIXED_1DP(whole, fraction) FIXED_XDP(1, whole, fraction)
|
|
|
|
#define FIXED_2DP(whole, fraction) FIXED_XDP(10, whole, fraction)
|
2016-11-30 12:07:10 +01:00
|
|
|
|
|
|
|
// Construct a money value in the format MONEY(10,70) to represent 10.70. Fractional part must be two digits.
|
2018-06-22 23:25:16 +02:00
|
|
|
#define MONEY(whole, fraction) ((whole)*10 + ((fraction) / 10))
|
2016-11-30 12:07:10 +01:00
|
|
|
|
2018-06-22 23:25:16 +02:00
|
|
|
#define MONEY_FREE MONEY(0, 00)
|
|
|
|
#define MONEY16_UNDEFINED (money16)(uint16_t) 0xFFFF
|
|
|
|
#define MONEY32_UNDEFINED ((money32)0x80000000)
|
2016-11-30 12:07:10 +01:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
using BannerIndex = uint8_t;
|
2018-06-09 13:51:45 +02:00
|
|
|
|
2018-02-14 09:42:26 +01:00
|
|
|
using EMPTY_ARGS_VOID_POINTER = void();
|
2018-06-22 23:25:16 +02:00
|
|
|
using rct_string_id = uint16_t;
|
|
|
|
|
|
|
|
#define SafeFree(x) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
free(x); \
|
|
|
|
(x) = nullptr; \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define SafeDelete(x) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
delete (x); \
|
|
|
|
(x) = nullptr; \
|
|
|
|
} while (false)
|
|
|
|
#define SafeDeleteArray(x) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
delete[](x); \
|
|
|
|
(x) = nullptr; \
|
|
|
|
} while (false)
|
2015-09-05 00:27:47 +02:00
|
|
|
|
2015-09-12 19:17:25 +02:00
|
|
|
#ifndef interface
|
2018-06-22 23:25:16 +02:00
|
|
|
#define interface struct
|
2015-09-12 19:17:25 +02:00
|
|
|
#endif
|
2015-09-05 00:27:47 +02:00
|
|
|
#define abstract = 0
|
2015-08-29 03:05:21 +02:00
|
|
|
|
2017-12-30 00:09:17 +01:00
|
|
|
#if defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
|
2018-06-22 23:25:16 +02:00
|
|
|
#define OPENRCT2_X86
|
2017-12-30 00:09:17 +01:00
|
|
|
#elif defined(_MSC_VER) && (_MSC_VER >= 1500) && (defined(_M_X64) || defined(_M_IX86)) // VS2008
|
2018-06-22 23:25:16 +02:00
|
|
|
#define OPENRCT2_X86
|
2017-12-30 00:09:17 +01:00
|
|
|
#endif
|
|
|
|
|
2016-04-22 08:59:50 +02:00
|
|
|
#if defined(__i386__) || defined(_M_IX86)
|
2016-04-21 22:18:59 +02:00
|
|
|
#define PLATFORM_X86
|
|
|
|
#endif
|
|
|
|
|
2016-06-03 00:18:20 +02:00
|
|
|
#if defined(__LP64__) || defined(_WIN64)
|
2018-06-22 23:25:16 +02:00
|
|
|
#define PLATFORM_64BIT
|
2016-06-03 00:18:20 +02:00
|
|
|
#else
|
2018-06-22 23:25:16 +02:00
|
|
|
#define PLATFORM_32BIT
|
2016-06-03 00:18:20 +02:00
|
|
|
#endif
|
|
|
|
|
2016-06-01 16:40:31 +02:00
|
|
|
// C99's restrict keywords guarantees the pointer in question, for the whole of its lifetime,
|
|
|
|
// will be the only way to access a given memory region. In other words: there is no other pointer
|
|
|
|
// aliasing the same memory area. Using it lets compiler generate better code. If your compiler
|
|
|
|
// does not support it, feel free to drop it, at some performance hit.
|
2018-03-13 23:42:29 +01:00
|
|
|
#ifdef _MSC_VER
|
2018-06-22 23:25:16 +02:00
|
|
|
#define RESTRICT __restrict
|
2016-06-01 16:40:31 +02:00
|
|
|
#else
|
2018-06-22 23:25:16 +02:00
|
|
|
#define RESTRICT __restrict__
|
2016-06-01 16:40:31 +02:00
|
|
|
#endif
|
|
|
|
|
2016-06-03 10:54:44 +02:00
|
|
|
#define assert_struct_size(x, y) static_assert(sizeof(x) == (y), "Improper struct size")
|
2016-06-02 20:40:29 +02:00
|
|
|
|
2016-04-21 22:18:59 +02:00
|
|
|
#ifdef PLATFORM_X86
|
2018-06-22 23:25:16 +02:00
|
|
|
#ifndef FASTCALL
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define FASTCALL __attribute__((fastcall))
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
#define FASTCALL __fastcall
|
|
|
|
#else
|
|
|
|
#pragma message "Not using fastcall calling convention, please check your compiler support"
|
|
|
|
#define FASTCALL
|
|
|
|
#endif
|
|
|
|
#endif // FASTCALL
|
|
|
|
#else // PLATFORM_X86
|
|
|
|
#define FASTCALL
|
2016-04-21 22:18:59 +02:00
|
|
|
#endif // PLATFORM_X86
|
2016-02-20 11:12:00 +01:00
|
|
|
|
2016-09-18 00:12:25 +02:00
|
|
|
/**
|
|
|
|
* x86 register structure, only used for easy interop to RCT2 code.
|
|
|
|
*/
|
|
|
|
#pragma pack(push, 1)
|
2018-06-22 23:25:16 +02:00
|
|
|
struct registers
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t eax;
|
|
|
|
int16_t ax;
|
2018-06-22 23:25:16 +02:00
|
|
|
struct
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
char al;
|
|
|
|
char ah;
|
|
|
|
};
|
|
|
|
};
|
2018-06-22 23:25:16 +02:00
|
|
|
union
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t ebx;
|
|
|
|
int16_t bx;
|
2018-06-22 23:25:16 +02:00
|
|
|
struct
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
char bl;
|
|
|
|
char bh;
|
|
|
|
};
|
|
|
|
};
|
2018-06-22 23:25:16 +02:00
|
|
|
union
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t ecx;
|
|
|
|
int16_t cx;
|
2018-06-22 23:25:16 +02:00
|
|
|
struct
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
char cl;
|
|
|
|
char ch;
|
|
|
|
};
|
|
|
|
};
|
2018-06-22 23:25:16 +02:00
|
|
|
union
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t edx;
|
|
|
|
int16_t dx;
|
2018-06-22 23:25:16 +02:00
|
|
|
struct
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
char dl;
|
|
|
|
char dh;
|
|
|
|
};
|
|
|
|
};
|
2018-06-22 23:25:16 +02:00
|
|
|
union
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t esi;
|
|
|
|
int16_t si;
|
2017-06-06 23:24:18 +02:00
|
|
|
};
|
2018-06-22 23:25:16 +02:00
|
|
|
union
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t edi;
|
|
|
|
int16_t di;
|
2017-06-06 23:24:18 +02:00
|
|
|
};
|
2018-06-22 23:25:16 +02:00
|
|
|
union
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t ebp;
|
|
|
|
int16_t bp;
|
2017-06-06 23:24:18 +02:00
|
|
|
};
|
2018-02-14 09:42:26 +01:00
|
|
|
};
|
2016-09-18 00:12:25 +02:00
|
|
|
assert_struct_size(registers, 7 * 4);
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
2015-09-22 22:45:03 +02:00
|
|
|
#endif
|