From 73fb132e414d0df4e6fbc1a158fda06aecf25971 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sun, 26 Mar 2017 22:30:23 +0100 Subject: [PATCH] Replace some SDL functions with our own --- src/openrct2/core/Diagnostics.cpp | 6 ++-- src/openrct2/game.c | 4 +-- src/openrct2/interface/chat.c | 5 +-- src/openrct2/network/NetworkConnection.cpp | 6 ++-- .../network/NetworkServerAdvertiser.cpp | 8 ++--- src/openrct2/network/NetworkTypes.h | 7 ++-- src/openrct2/network/network.cpp | 32 +++++++++---------- src/openrct2/network/twitch.cpp | 5 +-- src/openrct2/platform/crash.cpp | 3 +- src/openrct2/platform/platform.h | 1 + src/openrct2/platform/shared.c | 17 +++++++++- src/openrct2/rct2.c | 2 +- src/openrct2/scenario/scenario.c | 2 +- src/openrct2/util/sawyercoding.h | 1 - src/openrct2/util/util.c | 3 -- src/openrct2/world/mapgen.c | 3 +- 16 files changed, 60 insertions(+), 45 deletions(-) diff --git a/src/openrct2/core/Diagnostics.cpp b/src/openrct2/core/Diagnostics.cpp index 3f8cbcf87e..1d2c94941f 100644 --- a/src/openrct2/core/Diagnostics.cpp +++ b/src/openrct2/core/Diagnostics.cpp @@ -14,9 +14,7 @@ *****************************************************************************/ #pragma endregion -#include - -#if defined(DEBUG) && defined(__WINDOWS__) +#if defined(DEBUG) && defined(_WIN32) #define WIN32_LEAN_AND_MEAN #include #undef GetMessage @@ -29,7 +27,7 @@ namespace Debug void Break() { #if defined(DEBUG) -#if defined(__WINDOWS__) +#if defined(_WIN32) if (IsDebuggerPresent()) { DebugBreak(); diff --git a/src/openrct2/game.c b/src/openrct2/game.c index 582d3673a5..e12c5a83bc 100644 --- a/src/openrct2/game.c +++ b/src/openrct2/game.c @@ -427,7 +427,7 @@ void game_logic_update() // start autosave timer after update if (gLastAutoSaveUpdate == AUTOSAVE_PAUSE) - gLastAutoSaveUpdate = SDL_GetTicks(); + gLastAutoSaveUpdate = platform_get_ticks(); } /** @@ -585,7 +585,7 @@ sint32 game_do_command_p(sint32 command, sint32 *eax, sint32 *ebx, sint32 *ecx, // start autosave timer after game command if (gLastAutoSaveUpdate == AUTOSAVE_PAUSE) - gLastAutoSaveUpdate = SDL_GetTicks(); + gLastAutoSaveUpdate = platform_get_ticks(); return cost; } diff --git a/src/openrct2/interface/chat.c b/src/openrct2/interface/chat.c index 9cf2c2f7a6..2dfd7b021a 100644 --- a/src/openrct2/interface/chat.c +++ b/src/openrct2/interface/chat.c @@ -131,7 +131,8 @@ void chat_draw(rct_drawpixelinfo * dpi) // Draw chat history for (sint32 i = 0; i < CHAT_HISTORY_SIZE; i++, y -= stringHeight) { - if (!gChatOpen && SDL_TICKS_PASSED(SDL_GetTicks(), chat_history_get_time(i) + 10000)) { + uint32 expireTime = chat_history_get_time(i) + 10000; + if (!gChatOpen && platform_get_ticks() > expireTime) { break; } @@ -174,7 +175,7 @@ void chat_history_add(const char *src) sint32 index = _chatHistoryIndex % CHAT_HISTORY_SIZE; memset(_chatHistory[index], 0, CHAT_INPUT_SIZE); memcpy(_chatHistory[index], src, min(strlen(src), CHAT_INPUT_SIZE - 1)); - _chatHistoryTime[index] = SDL_GetTicks(); + _chatHistoryTime[index] = platform_get_ticks(); _chatHistoryIndex++; Mixer_Play_Effect(SOUND_NEWS_ITEM, 0, SDL_MIX_MAXVOLUME, 0, 1.5f, true); network_append_chat_log(src); diff --git a/src/openrct2/network/NetworkConnection.cpp b/src/openrct2/network/NetworkConnection.cpp index 75e8649ebf..54fa4904aa 100644 --- a/src/openrct2/network/NetworkConnection.cpp +++ b/src/openrct2/network/NetworkConnection.cpp @@ -85,7 +85,7 @@ sint32 NetworkConnection::ReadPacket() } if (InboundPacket.BytesTransferred == sizeof(InboundPacket.Size) + InboundPacket.Size) { - _lastPacketTime = SDL_GetTicks(); + _lastPacketTime = platform_get_ticks(); return NETWORK_READPACKET_SUCCESS; } } @@ -150,13 +150,13 @@ void NetworkConnection::SendQueuedPackets() void NetworkConnection::ResetLastPacketTime() { - _lastPacketTime = SDL_GetTicks(); + _lastPacketTime = platform_get_ticks(); } bool NetworkConnection::ReceivedPacketRecently() { #ifndef DEBUG - if (SDL_TICKS_PASSED(SDL_GetTicks(), _lastPacketTime + 7000)) + if (platform_get_ticks() > _lastPacketTime + 7000) { return false; } diff --git a/src/openrct2/network/NetworkServerAdvertiser.cpp b/src/openrct2/network/NetworkServerAdvertiser.cpp index 13b02df084..3fd3440121 100644 --- a/src/openrct2/network/NetworkServerAdvertiser.cpp +++ b/src/openrct2/network/NetworkServerAdvertiser.cpp @@ -77,13 +77,13 @@ public: { switch (_status) { case ADVERTISE_STATUS_UNREGISTERED: - if (_lastAdvertiseTime == 0 || SDL_TICKS_PASSED(SDL_GetTicks(), _lastAdvertiseTime + MASTER_SERVER_REGISTER_TIME)) + if (_lastAdvertiseTime == 0 || platform_get_ticks() > _lastAdvertiseTime + MASTER_SERVER_REGISTER_TIME) { SendRegistration(); } break; case ADVERTISE_STATUS_REGISTERED: - if (SDL_TICKS_PASSED(SDL_GetTicks(), _lastHeartbeatTime + MASTER_SERVER_HEARTBEAT_TIME)) + if (platform_get_ticks() > _lastHeartbeatTime + MASTER_SERVER_HEARTBEAT_TIME) { SendHeartbeat(); } @@ -97,7 +97,7 @@ public: private: void SendRegistration() { - _lastAdvertiseTime = SDL_GetTicks(); + _lastAdvertiseTime = platform_get_ticks(); // Send the registration request http_request_t request = { 0 }; @@ -139,7 +139,7 @@ private: request.root = jsonBody; request.type = HTTP_DATA_JSON; - _lastHeartbeatTime = SDL_GetTicks(); + _lastHeartbeatTime = platform_get_ticks(); http_request_async(&request, [](http_response_t *response) -> void { if (response == nullptr) diff --git a/src/openrct2/network/NetworkTypes.h b/src/openrct2/network/NetworkTypes.h index 5bfbacedaf..04e7d7b222 100644 --- a/src/openrct2/network/NetworkTypes.h +++ b/src/openrct2/network/NetworkTypes.h @@ -80,7 +80,7 @@ struct ByteSwapT<2> { static uint16 SwapBE(uint16 value) { - return SDL_SwapBE16(value); + return (uint16)((value << 8) | (value >> 8)); } }; @@ -89,7 +89,10 @@ struct ByteSwapT<4> { static uint32 SwapBE(uint32 value) { - return SDL_SwapBE32(value); + return (uint32)(((value << 24) | + ((value << 8) & 0x00FF0000) | + ((value >> 8) & 0x0000FF00) | + (value >> 24))); } }; diff --git a/src/openrct2/network/network.cpp b/src/openrct2/network/network.cpp index 6efd69232e..f84b6b89fa 100644 --- a/src/openrct2/network/network.cpp +++ b/src/openrct2/network/network.cpp @@ -14,8 +14,6 @@ *****************************************************************************/ #pragma endregion -#include - #include "../core/Guard.hpp" #include "../OpenRCT2.h" @@ -380,10 +378,12 @@ void Network::UpdateServer() it++; } } - if (SDL_TICKS_PASSED(SDL_GetTicks(), last_tick_sent_time + 25)) { + + uint32 ticks = platform_get_ticks(); + if (ticks > last_tick_sent_time + 25) { Server_Send_TICK(); } - if (SDL_TICKS_PASSED(SDL_GetTicks(), last_ping_sent_time + 3000)) { + if (ticks > last_ping_sent_time + 3000) { Server_Send_PING(); Server_Send_PINGLIST(); } @@ -427,7 +427,7 @@ void Network::UpdateClient() window_network_status_open(str_connecting, []() -> void { gNetwork.Close(); }); - server_connect_time = SDL_GetTicks(); + server_connect_time = platform_get_ticks(); } break; } @@ -1044,7 +1044,7 @@ void Network::Server_Send_GAMECMD(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx void Network::Server_Send_TICK() { - last_tick_sent_time = SDL_GetTicks(); + last_tick_sent_time = platform_get_ticks(); std::unique_ptr packet(NetworkPacket::Allocate()); *packet << (uint32)NETWORK_COMMAND_TICK << (uint32)gCurrentTicks << (uint32)gScenarioSrand0; uint32 flags = 0; @@ -1085,11 +1085,11 @@ void Network::Client_Send_PING() void Network::Server_Send_PING() { - last_ping_sent_time = SDL_GetTicks(); + last_ping_sent_time = platform_get_ticks(); std::unique_ptr packet(NetworkPacket::Allocate()); *packet << (uint32)NETWORK_COMMAND_PING; for (auto it = client_connection_list.begin(); it != client_connection_list.end(); it++) { - (*it)->PingTime = SDL_GetTicks(); + (*it)->PingTime = platform_get_ticks(); } SendPacketToClients(*packet, true); } @@ -1254,7 +1254,7 @@ void Network::ProcessGameCommandQueue() NetworkPlayer* player = GetPlayerByID(gc.playerid); if (player) { player->LastAction = NetworkActions::FindCommand(command); - player->LastActionTime = SDL_GetTicks(); + player->LastActionTime = platform_get_ticks(); player->AddMoneySpent(cost); } } @@ -1860,7 +1860,7 @@ void Network::Server_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket sint32 commandCommand = args[4]; - uint32 ticks = SDL_GetTicks(); //tick count is different by time last_action_time is set, keep same value. + uint32 ticks = platform_get_ticks(); //tick count is different by time last_action_time is set, keep same value. // Check if player's group permission allows command to run NetworkGroup* group = GetGroupByID(connection.Player->Group); @@ -1875,7 +1875,7 @@ void Network::Server_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket if (commandCommand == GAME_COMMAND_PLACE_SCENERY) { if ( ticks - connection.Player->LastPlaceSceneryTime < ACTION_COOLDOWN_TIME_PLACE_SCENERY && - // Incase SDL_GetTicks() wraps after ~49 days, ignore larger logged times. + // Incase platform_get_ticks() wraps after ~49 days, ignore larger logged times. ticks > connection.Player->LastPlaceSceneryTime ) { if (!(group->CanPerformCommand(MISC_COMMAND_TOGGLE_SCENERY_CLUSTER))) { @@ -1889,7 +1889,7 @@ void Network::Server_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket else if (commandCommand == GAME_COMMAND_DEMOLISH_RIDE) { if ( ticks - connection.Player->LastDemolishRideTime < ACTION_COOLDOWN_TIME_DEMOLISH_RIDE && - // Incase SDL_GetTicks() wraps after ~49 days, ignore larger logged times. + // Incase platform_get_ticks() wraps after ~49 days, ignore larger logged times. ticks > connection.Player->LastDemolishRideTime ) { Server_Send_SHOWERROR(connection, STR_CANT_DO_THIS, STR_NETWORK_ACTION_RATE_LIMIT_MESSAGE); @@ -1912,7 +1912,7 @@ void Network::Server_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket } connection.Player->LastAction = NetworkActions::FindCommand(commandCommand); - connection.Player->LastActionTime = SDL_GetTicks(); + connection.Player->LastActionTime = platform_get_ticks(); connection.Player->AddMoneySpent(cost); if (commandCommand == GAME_COMMAND_PLACE_SCENERY) { @@ -1986,7 +1986,7 @@ void Network::Client_Handle_PING(NetworkConnection& connection, NetworkPacket& p void Network::Server_Handle_PING(NetworkConnection& connection, NetworkPacket& packet) { - sint32 ping = SDL_GetTicks() - connection.PingTime; + sint32 ping = platform_get_ticks() - connection.PingTime; if (ping < 0) { ping = 0; } @@ -2204,7 +2204,7 @@ void network_add_player_money_spent(uint32 index, money32 cost) sint32 network_get_player_last_action(uint32 index, sint32 time) { - if (time && SDL_TICKS_PASSED(SDL_GetTicks(), gNetwork.player_list[index]->LastActionTime + time)) { + if (time && platform_get_ticks() > gNetwork.player_list[index]->LastActionTime + time) { return -999; } return gNetwork.player_list[index]->LastAction; @@ -2213,7 +2213,7 @@ sint32 network_get_player_last_action(uint32 index, sint32 time) void network_set_player_last_action(uint32 index, sint32 command) { gNetwork.player_list[index]->LastAction = NetworkActions::FindCommand(command); - gNetwork.player_list[index]->LastActionTime = SDL_GetTicks(); + gNetwork.player_list[index]->LastActionTime = platform_get_ticks(); } rct_xyz16 network_get_player_last_action_coord(uint32 index) diff --git a/src/openrct2/network/twitch.cpp b/src/openrct2/network/twitch.cpp index b1ac609c00..d6259dd74d 100644 --- a/src/openrct2/network/twitch.cpp +++ b/src/openrct2/network/twitch.cpp @@ -43,6 +43,7 @@ extern "C" #include "../localisation/localisation.h" #include "../management/news_item.h" #include "../peep/peep.h" + #include "../platform/platform.h" #include "../rct2.h" #include "../util/util.h" #include "../world/sprite.h" @@ -142,7 +143,7 @@ namespace Twitch switch (_twitchState) { case TWITCH_STATE_LEFT: { - uint32 currentTime = SDL_GetTicks(); + uint32 currentTime = platform_get_ticks(); uint32 timeSinceLastPulse = currentTime - _twitchLastPulseTick; if (_twitchLastPulseTick == 0 || timeSinceLastPulse > PulseTime) { @@ -153,7 +154,7 @@ namespace Twitch } case TWITCH_STATE_JOINED: { - uint32 currentTime = SDL_GetTicks(); + uint32 currentTime = platform_get_ticks(); uint32 timeSinceLastPulse = currentTime - _twitchLastPulseTick; if (_twitchLastPulseTick == 0 || timeSinceLastPulse > PulseTime) { _twitchLastPulseTick = currentTime; diff --git a/src/openrct2/platform/crash.cpp b/src/openrct2/platform/crash.cpp index f9960e3dd9..0a8c343f32 100644 --- a/src/openrct2/platform/crash.cpp +++ b/src/openrct2/platform/crash.cpp @@ -14,14 +14,13 @@ *****************************************************************************/ #pragma endregion -#include #include "crash.h" #ifdef USE_BREAKPAD #include #include -#if defined(__WINDOWS__) +#if defined(_WIN32) #include #include #include diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index 87843a2549..ac93a317c5 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -188,6 +188,7 @@ void platform_get_cursor_position(sint32 *x, sint32 *y); void platform_get_cursor_position_scaled(sint32 *x, sint32 *y); void platform_set_cursor_position(sint32 x, sint32 y); uint32 platform_get_ticks(); +void platform_sleep(uint32 ms); void platform_resolve_user_data_path(); void platform_resolve_openrct_data_path(); void platform_get_openrct_data_path(utf8 *outPath, size_t outSize); diff --git a/src/openrct2/platform/shared.c b/src/openrct2/platform/shared.c index cb03671919..e3115f5a8d 100644 --- a/src/openrct2/platform/shared.c +++ b/src/openrct2/platform/shared.c @@ -14,6 +14,7 @@ *****************************************************************************/ #pragma endregion +#include #include "../audio/audio.h" #include "../audio/AudioMixer.h" #include "../config/Config.h" @@ -761,7 +762,21 @@ void platform_set_cursor_position(sint32 x, sint32 y) uint32 platform_get_ticks() { - return SDL_GetTicks(); +#ifdef _WIN32 + return GetTickCount(); +#else + struct timespec ts; + if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { + log_fatal("clock_gettime failed"); + exit(-1); + } + return (uint32)(ts.tv_nsec / 1000000); +#endif +} + +void platform_sleep(uint32 ms) +{ + SDL_Delay(ms); } uint8 platform_get_currency_value(const char *currCode) { diff --git a/src/openrct2/rct2.c b/src/openrct2/rct2.c index ff5761e6f8..b24d470d6f 100644 --- a/src/openrct2/rct2.c +++ b/src/openrct2/rct2.c @@ -380,7 +380,7 @@ sint32 check_file_paths() void rct2_update() { - sint32 tickCount = SDL_GetTicks(); + sint32 tickCount = platform_get_ticks(); gTicksSinceLastUpdate = min(tickCount - gLastTickCount, 500); gLastTickCount = tickCount; if (game_is_not_paused()) { diff --git a/src/openrct2/scenario/scenario.c b/src/openrct2/scenario/scenario.c index 5910e84e68..946140c4fe 100644 --- a/src/openrct2/scenario/scenario.c +++ b/src/openrct2/scenario/scenario.c @@ -362,7 +362,7 @@ void scenario_autosave_check() if (gLastAutoSaveUpdate == AUTOSAVE_PAUSE) return; // Milliseconds since last save - uint32 timeSinceSave = SDL_GetTicks() - gLastAutoSaveUpdate; + uint32 timeSinceSave = platform_get_ticks() - gLastAutoSaveUpdate; bool shouldSave = false; switch (gConfigGeneral.autosave_frequency) { diff --git a/src/openrct2/util/sawyercoding.h b/src/openrct2/util/sawyercoding.h index 73571111fc..f289154387 100644 --- a/src/openrct2/util/sawyercoding.h +++ b/src/openrct2/util/sawyercoding.h @@ -17,7 +17,6 @@ #ifndef _SAWYERCODING_H_ #define _SAWYERCODING_H_ -#include #include "../common.h" #pragma pack(push, 1) diff --git a/src/openrct2/util/util.c b/src/openrct2/util/util.c index 3ab73ff708..a7a4433e83 100644 --- a/src/openrct2/util/util.c +++ b/src/openrct2/util/util.c @@ -14,10 +14,7 @@ *****************************************************************************/ #pragma endregion -// Include common.h before SDL, otherwise M_PI gets redefined #include "../common.h" - -#include #include "../core/Guard.hpp" #include "../localisation/localisation.h" #include "../platform/platform.h" diff --git a/src/openrct2/world/mapgen.c b/src/openrct2/world/mapgen.c index 1aa8f6e1c9..d61fd1f3a5 100644 --- a/src/openrct2/world/mapgen.c +++ b/src/openrct2/world/mapgen.c @@ -22,6 +22,7 @@ #include "../game.h" #include "../localisation/string_ids.h" #include "../object.h" +#include "../platform/platform.h" #include "../util/util.h" #include "../windows/error.h" #include "map.h" @@ -142,7 +143,7 @@ void mapgen_generate(mapgen_settings *settings) sint32 x, y, mapSize, floorTexture, wallTexture, waterLevel; rct_map_element *mapElement; - util_srand((sint32)SDL_GetTicks()); + util_srand((sint32)platform_get_ticks()); mapSize = settings->mapSize; floorTexture = settings->floor;