Files
OpenRCT2-Web/src/openrct2/platform/Shared.cpp
T

276 lines
6.7 KiB
C++
Raw Normal View History

2014-10-06 17:36:58 +01:00
/*****************************************************************************
2019-03-17 08:16:15 +01:00
* Copyright (c) 2014-2019 OpenRCT2 developers
2015-09-22 19:05:00 +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 19:05:00 +02:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2014-10-06 17:36:58 +01:00
*****************************************************************************/
2017-06-11 23:26:14 +01:00
#include "../common.h"
#ifdef _WIN32
2018-07-21 16:17:06 +02:00
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
2017-06-11 23:26:14 +01:00
#else
2018-07-21 16:17:06 +02:00
# include <unistd.h>
2017-06-11 23:26:14 +01:00
#endif
2017-03-26 03:36:22 +01:00
#include "../Context.h"
2018-06-22 23:04:38 +02:00
#include "../Game.h"
2017-11-08 08:13:33 +01:00
#include "../OpenRCT2.h"
#include "../config/Config.h"
2018-01-05 21:17:33 +00:00
#include "../drawing/Drawing.h"
2018-01-05 21:11:19 +00:00
#include "../drawing/LightFX.h"
2018-01-06 18:32:25 +01:00
#include "../localisation/Currency.h"
#include "../localisation/Localisation.h"
2017-12-13 13:02:24 +01:00
#include "../util/Util.h"
2017-03-11 11:24:18 +00:00
#include "../world/Climate.h"
#include "Platform2.h"
2015-01-23 20:47:33 +00:00
2019-02-03 22:59:28 +01:00
#include <algorithm>
2018-06-22 23:04:38 +02:00
#include <stdlib.h>
#include <time.h>
#ifdef __APPLE__
2018-07-21 16:17:06 +02:00
# include <AvailabilityMacros.h>
# include <mach/mach_time.h>
# ifndef __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
# error Missing __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ define
# endif
#endif
#if defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200)
2018-06-22 23:04:38 +02:00
static mach_timebase_info_data_t _mach_base_info = {};
#endif
2018-10-09 20:42:02 +02:00
#ifdef _WIN32
2018-06-22 23:04:38 +02:00
char* strndup(const char* src, size_t size)
2017-11-07 20:26:12 +01:00
{
size_t len = strnlen(src, size);
2018-06-22 23:04:38 +02:00
char* dst = (char*)malloc(len + 1);
2017-11-07 20:26:12 +01:00
2018-01-29 17:06:01 +01:00
if (dst == nullptr)
2017-11-07 20:26:12 +01:00
{
2018-01-29 17:06:01 +01:00
return nullptr;
2017-11-07 20:26:12 +01:00
}
dst = (char*)std::memcpy(dst, src, len);
2017-11-07 20:26:12 +01:00
dst[len] = '\0';
return dst;
2017-11-07 20:26:12 +01:00
}
2018-06-22 23:04:38 +02:00
static uint32_t _frequency = 0;
static LARGE_INTEGER _entryTimestamp;
2018-10-09 20:42:02 +02:00
#endif // _WIN32
namespace Platform
{
rct2_date GetDateLocal()
{
auto time = std::time(nullptr);
auto localTime = std::localtime(&time);
rct2_date outDate;
outDate.day = localTime->tm_mday;
outDate.day_of_week = localTime->tm_wday;
outDate.month = localTime->tm_mon;
outDate.year = localTime->tm_year;
return outDate;
}
rct2_time GetTimeLocal()
{
auto time = std::time(nullptr);
auto localTime = std::localtime(&time);
rct2_time outTime;
outTime.hour = localTime->tm_hour;
outTime.minute = localTime->tm_min;
outTime.second = localTime->tm_sec;
return outTime;
}
} // namespace Platform
2018-06-22 23:04:38 +02:00
using update_palette_func = void (*)(const uint8_t*, int32_t, int32_t);
2015-02-12 20:51:40 +00:00
2020-05-27 20:12:01 +01:00
GamePalette gPalette;
2015-01-23 20:47:33 +00:00
2018-06-22 23:04:38 +02:00
void platform_update_palette(const uint8_t* colours, int32_t start_index, int32_t num_colours)
2015-02-12 20:51:40 +00:00
{
2017-06-06 23:24:18 +02:00
colours += start_index * 4;
2015-02-12 20:51:40 +00:00
for (int32_t i = start_index; i < num_colours + start_index; i++)
2017-11-08 08:13:33 +01:00
{
uint8_t r = colours[2];
uint8_t g = colours[1];
uint8_t b = colours[0];
2015-07-24 22:58:41 +01:00
2016-10-24 13:06:44 +01:00
#ifdef __ENABLE_LIGHTFX__
if (lightfx_is_available())
2017-06-06 23:24:18 +02:00
{
lightfx_apply_palette_filter(i, &r, &g, &b);
}
else
2016-10-17 18:43:44 +01:00
#endif
2017-06-06 23:24:18 +02:00
{
float night = gDayNightCycle;
2017-11-08 08:13:33 +01:00
if (night >= 0 && gClimateLightningFlash == 0)
{
2017-06-06 23:24:18 +02:00
r = lerp(r, soft_light(r, 8), night);
g = lerp(g, soft_light(g, 8), night);
b = lerp(b, soft_light(b, 128), night);
}
}
2015-07-24 22:58:41 +01:00
2020-05-27 20:12:01 +01:00
gPalette[i].Red = r;
gPalette[i].Green = g;
gPalette[i].Blue = b;
gPalette[i].Alpha = 0;
2017-06-06 23:24:18 +02:00
colours += 4;
}
2015-02-12 20:51:40 +00:00
// Fix #1749 and #6535: rainbow path, donut shop and pause button contain black spots that should be white.
2020-05-27 20:12:01 +01:00
gPalette[255].Alpha = 0;
gPalette[255].Red = 255;
gPalette[255].Green = 255;
gPalette[255].Blue = 255;
2017-11-08 08:13:33 +01:00
if (!gOpenRCT2Headless)
{
2017-06-06 23:24:18 +02:00
drawing_engine_set_palette(gPalette);
}
2015-02-12 20:51:40 +00:00
}
void platform_toggle_windowed_mode()
{
int32_t targetMode = gConfigGeneral.fullscreen_mode == 0 ? 2 : 0;
2017-06-06 23:24:18 +02:00
context_set_fullscreen_mode(targetMode);
gConfigGeneral.fullscreen_mode = targetMode;
config_save_default();
}
void platform_refresh_video(bool recreate_window)
2015-05-22 03:17:46 +01:00
{
if (recreate_window)
{
context_recreate_window();
}
else
{
drawing_engine_dispose();
drawing_engine_init();
drawing_engine_resize();
}
2017-06-06 23:24:18 +02:00
drawing_engine_set_palette(gPalette);
gfx_invalidate_screen();
2015-08-04 00:57:51 +02:00
}
2015-10-01 17:08:14 -06:00
static void platform_ticks_init()
{
2017-11-08 08:13:33 +01:00
#ifdef _WIN32
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
2020-05-11 14:32:56 +02:00
_frequency = static_cast<uint32_t>(freq.QuadPart / 1000);
QueryPerformanceCounter(&_entryTimestamp);
#endif
}
uint32_t platform_get_ticks()
2015-10-01 17:08:14 -06:00
{
2017-03-26 22:30:23 +01:00
#ifdef _WIN32
LARGE_INTEGER pfc;
QueryPerformanceCounter(&pfc);
LARGE_INTEGER runningDelta;
runningDelta.QuadPart = pfc.QuadPart - _entryTimestamp.QuadPart;
2020-05-11 14:32:56 +02:00
return static_cast<uint32_t>(runningDelta.QuadPart / _frequency);
2017-05-05 10:37:41 +02:00
#elif defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200)
2020-05-11 14:32:56 +02:00
return static_cast<uint32_t>(((mach_absolute_time() * _mach_base_info.numer) / _mach_base_info.denom) / 1000000);
2017-03-26 22:30:23 +01:00
#else
struct timespec ts;
2017-11-08 08:13:33 +01:00
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
{
2017-03-26 22:30:23 +01:00
log_fatal("clock_gettime failed");
exit(-1);
}
2020-04-22 17:09:29 +02:00
return static_cast<uint32_t>(ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
2017-03-26 22:30:23 +01:00
#endif
}
void platform_sleep(uint32_t ms)
2017-03-26 22:30:23 +01:00
{
2017-06-11 23:26:14 +01:00
#ifdef _WIN32
Sleep(ms);
#else
usleep(ms * 1000);
#endif
2015-10-02 09:44:18 -06:00
}
2016-01-12 15:40:12 +01:00
2018-06-22 23:04:38 +02:00
uint8_t platform_get_currency_value(const char* currCode)
2017-11-08 08:13:33 +01:00
{
2018-01-29 17:06:01 +01:00
if (currCode == nullptr || strlen(currCode) < 3)
2017-11-08 08:13:33 +01:00
{
return CURRENCY_POUNDS;
2017-06-06 23:24:18 +02:00
}
2016-11-13 20:17:49 +01:00
for (int32_t currency = 0; currency < CURRENCY_END; ++currency)
2017-11-08 08:13:33 +01:00
{
if (strncmp(currCode, CurrencyDescriptors[currency].isoCode, 3) == 0)
{
2017-06-06 23:24:18 +02:00
return currency;
}
}
2016-11-13 20:17:49 +01:00
2017-06-06 23:24:18 +02:00
return CURRENCY_POUNDS;
2016-01-12 15:40:12 +01:00
}
2019-02-03 22:59:28 +01:00
#ifndef _WIN32
std::string platform_sanitise_filename(const std::string& path)
{
2019-05-08 22:03:17 +01:00
static const std::array<std::string::value_type, 1> prohibited = { '/' };
2019-02-03 22:59:28 +01:00
auto sanitised = path;
std::replace_if(
sanitised.begin(), sanitised.end(),
2019-05-08 22:03:17 +01:00
[](const std::string::value_type& ch) -> bool {
2019-02-03 22:59:28 +01:00
return std::find(prohibited.begin(), prohibited.end(), ch) != prohibited.end();
},
'_');
return sanitised;
}
#endif
2017-06-15 14:22:15 +02:00
#ifndef __ANDROID__
2017-11-08 08:13:33 +01:00
float platform_get_default_scale()
{
2017-06-15 14:22:15 +02:00
return 1;
}
#endif
void core_init()
{
2017-06-06 23:24:18 +02:00
static bool initialised = false;
if (!initialised)
{
initialised = true;
2017-06-03 23:10:20 +01:00
#ifdef __ANDROID__
platform_android_init_class_loader();
#endif // __ANDROID__
platform_ticks_init();
2017-06-06 23:24:18 +02:00
bitcount_init();
mask_init();
2017-06-03 23:10:20 +01:00
2017-05-05 08:46:03 +02:00
#if defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200)
2017-06-06 23:24:18 +02:00
kern_return_t ret = mach_timebase_info(&_mach_base_info);
2017-11-08 08:13:33 +01:00
if (ret != 0)
{
2017-06-06 23:24:18 +02:00
log_fatal("Unable to get mach_timebase_info.");
exit(-1);
}
2017-05-05 00:40:07 +01:00
#endif
2017-06-06 23:24:18 +02:00
}
}