DDIO: new crossplatform SleepMS() function

Use C++ chrono and thread for crossplatform sleep function.
This commit is contained in:
Azamat H. Hackimov 2024-08-11 14:22:25 +03:00
parent 45ff821328
commit 16a6866885
15 changed files with 58 additions and 107 deletions

View File

@ -798,9 +798,10 @@
* $NoKeywords: $
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <cstdio>
#include <cstring>
#include "chrono_timer.h"
#include "gameloop.h"
#include "game.h"
#include "render.h"
@ -814,56 +815,39 @@
#include "player.h"
#include "damage.h"
#include "ship.h"
#include "objinit.h"
#include "gameevent.h"
#include "gametexture.h"
#include "AIMain.h"
#include "ddvid.h"
#include "ddio.h"
#include "hud.h"
#include "terrain.h"
#include "BOA.h"
#include "lighting.h"
#include "findintersection.h"
#include "soar.h"
#include "multi.h"
#include "hud.h"
#include "bsp.h"
#include "gauges.h"
#include "SmallViews.h"
#include "newui.h"
#include "Inventory.h"
#include "physics.h"
#include "Controller.h"
#include "controls.h"
#include "gamesequence.h"
#include "cockpit.h"
#include "help.h"
#include "game.h"
#include "aipath.h"
#include "game2dll.h"
#include "Mission.h"
#include "object_lighting.h"
#include "fireball.h"
#include "weather.h"
#include "stringtable.h"
#include "streamaudio.h"
#include "voice.h"
#include "soundload.h"
#include "sounds.h"
#include "ambient.h"
#include "ship.h"
#include "config.h"
#include "matcen.h"
#include "dedicated_server.h"
#include "D3ForceFeedback.h"
#include "levelgoal.h"
#include "demofile.h"
#include "pilot.h"
#include "rtperformance.h"
#include "demofile.h"
#include "d3music.h"
// #include "gamespy.h"
#include "osiris_dll.h"
#include "aiambient.h"
#include "marker.h"
@ -871,7 +855,6 @@
#include "postrender.h"
#include "debuggraph.h"
#include "gamesave.h"
#include "psrand.h"
#include "spew.h"
#include "grtext.h"
#include "gamefont.h"
@ -3041,7 +3024,7 @@ void GameFrame(void) {
Cinematic_Frame();
} else {
Sleep(3);
D3::ChronoTimer::SleepMS(3);
}
// do music always.
@ -3089,7 +3072,7 @@ void GameFrame(void) {
if ((current_timer - last_timer) < Min_allowed_frametime) {
sleeptime = (uint32_t)Min_allowed_frametime - (current_timer - last_timer);
// mprintf(0,"Sleeping for %d ms\n",sleeptime);
Sleep(sleeptime);
D3::ChronoTimer::SleepMS(sleeptime);
}
static int graph_id = -2;

View File

@ -492,9 +492,10 @@
* $NoKeywords: $
*/
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <cstdlib>
#include <cstring>
#include "chrono_timer.h"
#include "mono.h"
#include "renderer.h"
#include "render.h"
@ -2557,7 +2558,7 @@ void TelComHandleAllEvents(tTelComInfo *tcs) {
volatile float next_time = last_frame_time + (1 / TELCOM_FRAMERATE_CAP);
if (curr_time < next_time) {
Sleep((next_time - curr_time) * 1000.0f);
D3::ChronoTimer::SleepMS((int)((next_time - curr_time) * 1000.0f));
}
if (last_frame_time == 0) {
frame_time = 0.1f;

View File

@ -1642,7 +1642,10 @@
*
*/
#include "pstypes.h"
#include <algorithm>
#include <cstring>
#include "chrono_timer.h"
#include "pserror.h"
#include "player.h"
#include "game.h"
@ -1667,24 +1670,15 @@
#include "fireball.h"
#include "Mission.h"
#include "LoadLevel.h"
#include "gamecinematics.h"
#include "init.h"
#include "sounds.h"
#include "weapon.h"
#include "stringtable.h"
#include "dedicated_server.h"
#include "demofile.h"
#include "args.h"
#include "ui.h"
#include "newui.h"
#include "multi_dll_mgr.h"
#include "BOA.h"
#include "attach.h"
#include "mission_download.h"
// #include "gamespy.h"
#include "multi_world_state.h"
#include "ObjScript.h"
#include "audiotaunts.h"
@ -1695,25 +1689,14 @@
#include "spew.h"
#include "physics.h"
#include "SmallViews.h"
#include "demofile.h"
#include "debuggraph.h"
#include "levelgoal.h"
#include "osiris_share.h"
#include "cockpit.h"
#include "hud.h"
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include "psrand.h"
#include "../md5/md5.h"
void MultiProcessShipChecksum(MD5 *md5, int ship_index);
#include <algorithm>
player_pos_suppress Player_pos_fix[MAX_PLAYERS];
// Define this if you want to have secondaries be sent as reliable packets
@ -1866,7 +1849,7 @@ void BailOnMultiplayer(const char *message) {
ShowProgressScreen(message);
MultiLeaveGame();
Sleep(2000);
D3::ChronoTimer::SleepMS(2000);
}
// Adds the trunctuated position data to an outgoing packet
@ -3537,7 +3520,7 @@ void MultiSendConnectBail() {
if (wait_to_send) {
for (int t = 0; t < 10; t++) {
nw_DoNetworkIdle();
Sleep(100);
D3::ChronoTimer::SleepMS(100);
}
}
@ -3555,7 +3538,7 @@ void MultiDoConnectBail() {
return;
ShowProgressScreen(TXT_MULTI_SERVERCHANGEA, TXT_MULTI_SERVERCHANGEB);
Sleep(3000);
D3::ChronoTimer::SleepMS(3000);
MultiLeaveGame();
SetFunctionMode(MENU_MODE);
}
@ -3633,7 +3616,7 @@ void MultiSendLevelEnded(int success, int next_level) {
// Do this so it gets sent off now.
for (int t = 0; t < 10; t++) {
nw_DoNetworkIdle();
Sleep(100);
D3::ChronoTimer::SleepMS(100);
}
}
@ -3717,7 +3700,7 @@ void MultiDoServerQuit(uint8_t *data) {
}
mprintf(0, "Server quitting!\n");
MultiLeaveGame();
Sleep(2000);
D3::ChronoTimer::SleepMS(2000);
}
void MultiDoDisconnect(uint8_t *data) {
@ -3762,7 +3745,7 @@ void MultiDoServerRejectedChecksum(uint8_t *data) {
SKIP_HEADER(data, &count);
ShowProgressScreen(TXT_MLTLEVELNOMATCH);
MultiLeaveGame();
Sleep(2000);
D3::ChronoTimer::SleepMS(2000);
}
// Lets us know if the server says its ok to join

View File

@ -134,6 +134,7 @@
*
*/
#include "chrono_timer.h"
#include "multi.h"
#include "multi_client.h"
#include "game.h"
@ -297,7 +298,7 @@ int ServerTimedOut() {
if (!nw_CheckReliableSocket(NetPlayers[Player_num].reliable_socket)) {
mprintf(0, "Reliable connection to the server was broken. Disconnecting.\n");
ShowProgressScreen(TXT_RELIABLE_OVERRUN);
Sleep(1000);
D3::ChronoTimer::SleepMS(1000);
return 1;
}
if ((timer_GetTime() - Netgame.last_server_time) > SERVER_DISCONNECT_TIME)
@ -345,7 +346,7 @@ void MultiDoClientFrame() {
mprintf(0, "Server disconnected while waiting for gametime!\n");
MultiLeaveGame();
ShowProgressScreen(TXT_MLTDISCFRMSERV);
Sleep(2000);
D3::ChronoTimer::SleepMS(2000);
} else if ((timer_GetTime() - Last_gametime_req) > NET_CLIENT_GAMETIME_REQ_RETRY) {
NetPlayers[Player_num].sequence = NETSEQ_NEED_GAMETIME;
}
@ -486,7 +487,7 @@ void MultiDoClientFrame() {
mprintf(0, "Server disconnected!\n");
MultiLeaveGame();
ShowProgressScreen(TXT_MLTDISCFRMSERV);
Sleep(2000);
D3::ChronoTimer::SleepMS(2000);
}
}
}

View File

@ -253,22 +253,18 @@
*
*/
#include "chrono_timer.h"
#include "multi.h"
#include "multi_server.h"
#include "player.h"
#include "game.h"
#include "ddio.h"
#include "Mission.h"
// #include "gametrack.h"
#include "stringtable.h"
#include "pilot.h"
#include "ship.h"
#include "args.h"
#include "ui.h"
#include "newui.h"
#include "multi_dll_mgr.h"
#include "LoadLevel.h"
// #define USE_DIRECTPLAY
@ -375,10 +371,10 @@ int TryToJoinServer(network_address *addr) {
char str[255];
snprintf(str, sizeof(str), "%s", TXT(Join_response_strings[Ok_to_join]));
ShowProgressScreen(str);
Sleep(2000);
D3::ChronoTimer::SleepMS(2000);
} else {
ShowProgressScreen(TXT_MLTNORESPONSE);
Sleep(2000);
D3::ChronoTimer::SleepMS(2000);
}
return 0;
} else
@ -643,11 +639,11 @@ int MultiPollForLevelInfo() {
if (!Got_level_info) {
ShowProgressScreen(TXT_MLTNOLEVELINFO);
MultiLeaveGame();
Sleep(2000);
D3::ChronoTimer::SleepMS(2000);
} else if (Got_level_info < 0) {
ShowProgressScreen(TXT(Join_response_strings[-Got_level_info]));
MultiLeaveGame();
Sleep(2000);
D3::ChronoTimer::SleepMS(2000);
} else {
Got_level_info = 0;
return 1;
@ -864,7 +860,7 @@ void MultiCloseGame() {
}
#endif
Sleep(1000); // Sleep for one second
D3::ChronoTimer::SleepMS(1000); // Sleep for one second
if (Netgame.local_role == LR_SERVER) {
int i;
@ -880,7 +876,7 @@ void MultiCloseGame() {
for (i = 0; i < 100; i++) {
nw_DoNetworkIdle();
Sleep(10);
D3::ChronoTimer::SleepMS(10);
}
NetPlayers[Player_num].flags &= ~NPF_CONNECTED;

View File

@ -276,13 +276,14 @@
*
* $NoKeywords: $
*/
#include "chrono_timer.h"
#include "ui.h"
#include "newui.h"
#include "game.h"
#include "gamefont.h"
#include "multi.h"
#include "multi_client.h"
#include "manage.h"
#include "Mission.h"
#include "pilot.h"
#include "pstypes.h"
@ -298,8 +299,6 @@
#include "multi_server.h"
#include "multi_ui.h"
#include "ship.h"
#include "soundload.h"
#include "spew.h"
#include "DllWrappers.h"
#include "appdatabase.h"
#include "module.h"
@ -815,7 +814,7 @@ int PollUI(void) {
// Limit this to a fixed framerate
if (UI_LastPoll) {
if ((timer_GetTime() - UI_LastPoll) < MIN_FRAMETIME)
Sleep((timer_GetTime() - UI_LastPoll) * 1000);
D3::ChronoTimer::SleepMS((timer_GetTime() - UI_LastPoll) * 1000);
}
UI_LastPoll = timer_GetTime();

View File

@ -85,6 +85,9 @@
#ifndef _multi_dll_header
#define _multi_dll_header
#include "newui.h"
#include "ui.h"
#define MT_EVT_LOGIN 1
#define MT_EVT_FIRST_FRAME 2
#define MT_EVT_FRAME 3

View File

@ -17,6 +17,8 @@
*/
#include <chrono>
#include <thread>
#include "chrono_timer.h"
namespace D3 {
@ -38,4 +40,8 @@ int64_t ChronoTimer::GetTimeUS() {
.count();
}
void ChronoTimer::SleepMS(int delay) {
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
}
} // namespace D3

View File

@ -41,6 +41,9 @@ public:
/// Get time in microseconds after class initialization (i.e. application start)
static int64_t GetTimeUS();
/// Sleep for delay milliseconds
static void SleepMS(int delay);
};
}

View File

@ -25,7 +25,6 @@
#define HGLOBAL void *
void Sleep(int millis);
char *strupr(char *string);
#define _stat stat

View File

@ -71,13 +71,13 @@
#include <cstdlib>
#include <cctype>
#if defined(POSIX)
#include <sys/time.h>
#include <termios.h>
#else
#include "winsock.h"
#endif
#include "application.h"
#include "chrono_timer.h"
#include "lnxapp.h"
#ifdef buttons // termios.h defines buttons, but SDL's headers use that symbol.
@ -95,13 +95,6 @@ bool con_Create(int flags);
void con_Destroy();
void con_Defer();
void Sleep(int millis) {
struct timeval tv{};
tv.tv_sec = 0;
tv.tv_usec = millis * 1000;
select(0, nullptr, nullptr, nullptr, &tv);
}
char *strupr(char *string) {
while (string && *string) {
*string = toupper(*string);
@ -211,7 +204,7 @@ void oeLnxApplication::set_defer_handler(void (*func)(bool)) { m_DeferFunc = fun
// delays app for a certain amount of time
void oeLnxApplication::delay(float secs) {
int msecs = (int)(secs * 1000.0f);
Sleep(msecs);
D3::ChronoTimer::SleepMS(msecs);
}
// Function to get the flags

View File

@ -441,9 +441,7 @@
#include <cstring>
#include <filesystem>
#if defined(POSIX)
#include "linux_fix.h"
#endif
#include "chrono_timer.h"
#include "descent.h"
#include "manage.h"
#include "pserror.h"
@ -1470,7 +1468,7 @@ int SwitcherooFiles(const char *name, char *tempname) {
}*/
int num_tries = 0;
while (!ddio_DeleteFile(name) && num_tries < MAX_TRIES) {
Sleep(100);
D3::ChronoTimer::SleepMS(100);
num_tries++;
}
if (num_tries >= MAX_TRIES) {
@ -1481,7 +1479,7 @@ int SwitcherooFiles(const char *name, char *tempname) {
}
num_tries = 0;
while ((rename(tempname, name)) && num_tries <= MAX_TRIES) {
Sleep(100);
D3::ChronoTimer::SleepMS(100);
num_tries++;
}
if (num_tries >= MAX_TRIES) {

View File

@ -7,5 +7,6 @@ set(CPPS
add_library(inetfile STATIC ${HEADERS} ${CPPS})
target_link_libraries(inetfile PRIVATE
ddio
mem
)

View File

@ -131,24 +131,8 @@
#include "inetgetfile.h"
#include "Chttpget.h"
#ifndef WIN32
#include "chrono_timer.h"
#include "mem.h"
#else
#define mem_malloc(a) malloc(a)
#define mem_free(a) free(a)
#endif
#if defined(POSIX)
#include "SDL_thread.h"
inline void Sleep(int millis) {
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = millis * 1000;
select(0, NULL, NULL, NULL, &tv);
}
#endif
#define NW_AGHBN_CANCEL 1
#define NW_AGHBN_LOOKUP 2
@ -185,7 +169,7 @@ void ChttpGet::AbortGet() {
#endif
m_Aborting = true;
while (!m_Aborted)
Sleep(50); // Wait for the thread to end
D3::ChronoTimer::SleepMS(50); // Wait for the thread to end
#ifdef WIN32
OutputDebugString("Aborted....\n");
#endif

View File

@ -80,6 +80,7 @@
#include "inetgetfile.h"
#include "CFtp.h"
#include "Chttpget.h"
#include "chrono_timer.h"
extern char *Proxy_server;
extern int16_t Proxy_port;
@ -160,7 +161,7 @@ InetGetFile::InetGetFile(char *URL, char *localfile, char *proxyip, int16_t prox
} else {
m_HardError = INET_ERROR_CANT_PARSE_URL;
}
Sleep(1000);
D3::ChronoTimer::SleepMS(1000);
}
InetGetFile::InetGetFile(char *URL, char *localfile) {
@ -193,7 +194,7 @@ InetGetFile::InetGetFile(char *URL, char *localfile) {
} else {
m_HardError = INET_ERROR_CANT_PARSE_URL;
}
Sleep(1000);
D3::ChronoTimer::SleepMS(1000);
}
InetGetFile::~InetGetFile() {