From e14cd758ad2257f137b7d4ad5e7c9bd61de88440 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Wed, 4 Dec 2024 18:32:03 +0300 Subject: [PATCH] engine: turn Platform_Sleep into an inline function that directly calls platform-specific delay functions --- engine/common/host.c | 4 ++-- engine/common/net_ws.c | 2 +- engine/common/system.c | 20 ++------------------ engine/common/system.h | 1 - engine/platform/dos/sys_dos.c | 6 +----- engine/platform/platform.h | 17 ++++++++++++++++- engine/platform/posix/sys_posix.c | 7 ++++--- engine/platform/sdl/sys_sdl.c | 4 ++-- engine/platform/win32/sys_win.c | 4 ++-- 9 files changed, 30 insertions(+), 35 deletions(-) diff --git a/engine/common/host.c b/engine/common/host.c index 758114737b..39e3b45cb3 100644 --- a/engine/common/host.c +++ b/engine/common/host.c @@ -652,10 +652,10 @@ static qboolean Host_Autosleep( double dt, double scale ) // if we have allocated time window, try to sleep if( timewindow > realsleeptime ) { - // Sys_Sleep isn't guaranteed to sleep an exact amount of milliseconds + // Platform_Sleep isn't guaranteed to sleep an exact amount of milliseconds // so we measure the real sleep time and use it to decrease the window double t1 = Sys_DoubleTime(), t2; - Sys_Sleep( sleep ); // in msec! + Platform_Sleep( sleep ); // in msec! t2 = Sys_DoubleTime(); realsleeptime = t2 - t1; diff --git a/engine/common/net_ws.c b/engine/common/net_ws.c index 70e80c7154..83ac15e172 100644 --- a/engine/common/net_ws.c +++ b/engine/common/net_ws.c @@ -1523,7 +1523,7 @@ static int NET_SendLong( netsrc_t sock, int net_socket, const char *buf, size_t total_sent += size; len -= size; packet_number++; - Sys_Sleep( 1 ); + Platform_Sleep( 1 ); } return total_sent; diff --git a/engine/common/system.c b/engine/common/system.c index 798aefd77d..cc50b3e245 100644 --- a/engine/common/system.c +++ b/engine/common/system.c @@ -117,22 +117,6 @@ char *Sys_GetClipboardData( void ) } #endif // XASH_DEDICATED -/* -================ -Sys_Sleep - -freeze application for some time -================ -*/ -void Sys_Sleep( int msec ) -{ - if( !msec ) - return; - - msec = Q_min( msec, 1000 ); - Platform_Sleep( msec ); -} - /* ================ Sys_GetCurrentUser @@ -370,7 +354,7 @@ static void Sys_WaitForQuit( void ) TranslateMessage( &msg ); DispatchMessage( &msg ); } - else Sys_Sleep( 20 ); + else Platform_Sleep( 20 ); } #endif } @@ -420,7 +404,7 @@ void Sys_Error( const char *error, ... ) return; // don't multiple executes // make sure that console received last message - if( host.change_game ) Sys_Sleep( 200 ); + if( host.change_game ) Platform_Sleep( 200 ); error_on_exit = 1; host.status = HOST_ERR_FATAL; diff --git a/engine/common/system.h b/engine/common/system.h index 08e5eaa3f0..086b27e1df 100644 --- a/engine/common/system.h +++ b/engine/common/system.h @@ -42,7 +42,6 @@ writes into struct by offsets not names ======================================================================== */ extern int error_on_exit; -void Sys_Sleep( int msec ); double Sys_DoubleTime( void ); char *Sys_GetClipboardData( void ); const char *Sys_GetCurrentUser( void ); diff --git a/engine/platform/dos/sys_dos.c b/engine/platform/dos/sys_dos.c index 8cfabeef21..48e579b44b 100644 --- a/engine/platform/dos/sys_dos.c +++ b/engine/platform/dos/sys_dos.c @@ -40,12 +40,8 @@ double Platform_DoubleTime( void ) { return 0.005*ticks; } - -void Platform_Sleep( int msec ) -{ - //usleep( msec * 1000 ); -} #endif // XASH_TIMER == TIMER_DOS + #define PIT_FREQUENCY 0x1234DDL #define frequency 140 #define counter PIT_FREQUENCY/frequency diff --git a/engine/platform/platform.h b/engine/platform/platform.h index fdbe9ea316..96c0e2c35b 100644 --- a/engine/platform/platform.h +++ b/engine/platform/platform.h @@ -31,7 +31,6 @@ GNU General Public License for more details. ============================================================================== */ double Platform_DoubleTime( void ); -void Platform_Sleep( int msec ); void Platform_ShellExecute( const char *path, const char *parms ); void Platform_MessageBox( const char *title, const char *message, qboolean parentMainWindow ); void Platform_SetStatus( const char *status ); @@ -52,11 +51,13 @@ void IOS_LaunchDialog( void ); #if XASH_POSIX void Posix_Daemonize( void ); void Posix_SetupSigtermHandling( void ); +void Posix_Sleep( int msec ); #endif #if XASH_SDL void SDLash_Init( void ); void SDLash_Shutdown( void ); +void SDLash_Sleep( int msec ); #endif #if XASH_ANDROID @@ -77,6 +78,7 @@ void Wcon_ShowConsole( qboolean show ); void Wcon_DisableInput( void ); char *Wcon_Input( void ); void Wcon_WinPrint( const char *pMsg ); +void Win32_Sleep( int msec ); #endif #if XASH_NSWITCH @@ -165,6 +167,19 @@ static inline void Platform_SetupSigtermHandling( void ) #endif } +static inline void Platform_Sleep( int msec ) +{ +#if XASH_TIMER == TIMER_SDL + SDLash_Sleep( msec ); +#elif XASH_TIMER == TIMER_POSIX + Posix_Sleep( msec ); +#elif XASH_TIMER == TIMER_WIN32 + Win32_Sleep( msec ); +#else + // stub +#endif +} + /* ============================================================================== diff --git a/engine/platform/posix/sys_posix.c b/engine/platform/posix/sys_posix.c index 222a57fde8..ec226a8bcb 100644 --- a/engine/platform/posix/sys_posix.c +++ b/engine/platform/posix/sys_posix.c @@ -153,7 +153,7 @@ static void Posix_SigtermCallback( int signal ) void Posix_SetupSigtermHandling( void ) { -#if !XASH_PSVITA +#if !XASH_PSVITA struct sigaction act = { 0 }; act.sa_handler = Posix_SigtermCallback; act.sa_flags = 0; @@ -172,9 +172,10 @@ double Platform_DoubleTime( void ) #endif return (double) ts.tv_sec + (double) ts.tv_nsec/1000000000.0; } +#endif // XASH_TIMER == TIMER_POSIX -void Platform_Sleep( int msec ) +void Posix_Sleep( int msec ) { usleep( msec * 1000 ); } -#endif // XASH_TIMER == TIMER_POSIX + diff --git a/engine/platform/sdl/sys_sdl.c b/engine/platform/sdl/sys_sdl.c index 4cc161a66e..9bbfa348d0 100644 --- a/engine/platform/sdl/sys_sdl.c +++ b/engine/platform/sdl/sys_sdl.c @@ -32,12 +32,12 @@ double Platform_DoubleTime( void ) CurrentTime = SDL_GetPerformanceCounter(); return (double)( CurrentTime - g_ClockStart ) / (double)( g_PerformanceFrequency ); } +#endif // XASH_TIMER == TIMER_SDL -void Platform_Sleep( int msec ) +void SDLash_Sleep( int msec ) { SDL_Delay( msec ); } -#endif // XASH_TIMER == TIMER_SDL #if XASH_MESSAGEBOX == MSGBOX_SDL void Platform_MessageBox( const char *title, const char *message, qboolean parentMainWindow ) diff --git a/engine/platform/win32/sys_win.c b/engine/platform/win32/sys_win.c index 4dc29187f8..335b0e6fcd 100644 --- a/engine/platform/win32/sys_win.c +++ b/engine/platform/win32/sys_win.c @@ -34,12 +34,12 @@ double Platform_DoubleTime( void ) return (double)( CurrentTime.QuadPart - g_ClockStart.QuadPart ) / (double)( g_PerformanceFrequency.QuadPart ); } +#endif // XASH_TIMER == TIMER_WIN32 -void Platform_Sleep( int msec ) +void Win32_Sleep( int msec ) { Sleep( msec ); } -#endif // XASH_TIMER == TIMER_WIN32 qboolean Platform_DebuggerPresent( void ) {