engine: rework timer stuff, move to platform. Move debugger present checks to platform
This commit is contained in:
parent
f0d0861160
commit
7c772d6bfd
8 changed files with 198 additions and 110 deletions
|
@ -21,7 +21,7 @@ GNU General Public License for more details.
|
||||||
/*
|
/*
|
||||||
===================================================================
|
===================================================================
|
||||||
|
|
||||||
SETUP BACKENDS DEFINATIONS
|
SETUP BACKENDS DEFINITIONS
|
||||||
|
|
||||||
===================================================================
|
===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -23,9 +23,6 @@ Sys_Crash
|
||||||
Crash handler, called from system
|
Crash handler, called from system
|
||||||
================
|
================
|
||||||
*/
|
*/
|
||||||
#define DEBUG_BREAK
|
|
||||||
/// TODO: implement on windows too
|
|
||||||
|
|
||||||
#if XASH_CRASHHANDLER == CRASHHANDLER_DBGHELP || XASH_CRASHHANDLER == CRASHHANDLER_WIN32
|
#if XASH_CRASHHANDLER == CRASHHANDLER_DBGHELP || XASH_CRASHHANDLER == CRASHHANDLER_WIN32
|
||||||
#if XASH_CRASHHANDLER == CRASHHANDLER_DBGHELP
|
#if XASH_CRASHHANDLER == CRASHHANDLER_DBGHELP
|
||||||
#pragma comment( lib, "dbghelp" )
|
#pragma comment( lib, "dbghelp" )
|
||||||
|
|
|
@ -16,7 +16,6 @@ GNU General Public License for more details.
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "mathlib.h"
|
#include "mathlib.h"
|
||||||
#include "platform/platform.h"
|
#include "platform/platform.h"
|
||||||
#include <time.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#ifdef XASH_SDL
|
#ifdef XASH_SDL
|
||||||
|
@ -37,9 +36,6 @@ GNU General Public License for more details.
|
||||||
|
|
||||||
qboolean error_on_exit = false; // arg for exit();
|
qboolean error_on_exit = false; // arg for exit();
|
||||||
#define DEBUG_BREAK
|
#define DEBUG_BREAK
|
||||||
#if defined _WIN32 && !defined XASH_SDL
|
|
||||||
#include <winbase.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
================
|
================
|
||||||
|
@ -48,103 +44,25 @@ Sys_DoubleTime
|
||||||
*/
|
*/
|
||||||
double GAME_EXPORT Sys_DoubleTime( void )
|
double GAME_EXPORT Sys_DoubleTime( void )
|
||||||
{
|
{
|
||||||
#if XASH_TIMER == TIMER_WIN32
|
return Platform_DoubleTime();
|
||||||
static LARGE_INTEGER g_PerformanceFrequency;
|
|
||||||
static LARGE_INTEGER g_ClockStart;
|
|
||||||
LARGE_INTEGER CurrentTime;
|
|
||||||
|
|
||||||
if( !g_PerformanceFrequency.QuadPart )
|
|
||||||
{
|
|
||||||
QueryPerformanceFrequency( &g_PerformanceFrequency );
|
|
||||||
QueryPerformanceCounter( &g_ClockStart );
|
|
||||||
}
|
|
||||||
QueryPerformanceCounter( &CurrentTime );
|
|
||||||
|
|
||||||
return (double)( CurrentTime.QuadPart - g_ClockStart.QuadPart ) / (double)( g_PerformanceFrequency.QuadPart );
|
|
||||||
#elif XASH_TIMER == TIMER_SDL
|
|
||||||
static longtime_t g_PerformanceFrequency;
|
|
||||||
static longtime_t g_ClockStart;
|
|
||||||
longtime_t CurrentTime;
|
|
||||||
|
|
||||||
if( !g_PerformanceFrequency )
|
|
||||||
{
|
|
||||||
g_PerformanceFrequency = SDL_GetPerformanceFrequency();
|
|
||||||
g_ClockStart = SDL_GetPerformanceCounter();
|
|
||||||
}
|
|
||||||
CurrentTime = SDL_GetPerformanceCounter();
|
|
||||||
return (double)( CurrentTime - g_ClockStart ) / (double)( g_PerformanceFrequency );
|
|
||||||
#elif XASH_TIMER == TIMER_LINUX
|
|
||||||
static longtime_t g_PerformanceFrequency;
|
|
||||||
static longtime_t g_ClockStart;
|
|
||||||
longtime_t CurrentTime;
|
|
||||||
struct timespec ts;
|
|
||||||
|
|
||||||
if( !g_PerformanceFrequency )
|
|
||||||
{
|
|
||||||
struct timespec res;
|
|
||||||
if( !clock_getres(CLOCK_MONOTONIC, &res) )
|
|
||||||
g_PerformanceFrequency = 1000000000LL/res.tv_nsec;
|
|
||||||
}
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
||||||
return (double) ts.tv_sec + (double) ts.tv_nsec/1000000000.0;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef GDB_BREAK
|
#if defined __linux__ || ( defined _WIN32 && !defined XASH_64BIT )
|
||||||
#include <fcntl.h>
|
#undef DEBUG_BREAK
|
||||||
qboolean Sys_DebuggerPresent( void )
|
qboolean Sys_DebuggerPresent(); // see sys_linux.c
|
||||||
{
|
#ifdef _MSC_VER
|
||||||
char buf[1024];
|
#define DEBUG_BREAK \
|
||||||
|
if( Sys_IsDebuggerPresent() ) \
|
||||||
int status_fd = open( "/proc/self/status", O_RDONLY );
|
_asm{ int 3 }
|
||||||
if ( status_fd == -1 )
|
#elif __i386__
|
||||||
return 0;
|
#define DEBUG_BREAK \
|
||||||
|
|
||||||
ssize_t num_read = read( status_fd, buf, sizeof( buf ) );
|
|
||||||
|
|
||||||
if ( num_read > 0 )
|
|
||||||
{
|
|
||||||
static const char TracerPid[] = "TracerPid:";
|
|
||||||
const byte *tracer_pid;
|
|
||||||
|
|
||||||
buf[num_read] = 0;
|
|
||||||
tracer_pid = (const byte*)Q_strstr( buf, TracerPid );
|
|
||||||
if( !tracer_pid )
|
|
||||||
return false;
|
|
||||||
//printf( "%s\n", tracer_pid );
|
|
||||||
while( *tracer_pid < '0' || *tracer_pid > '9' )
|
|
||||||
if( *tracer_pid++ == '\n' )
|
|
||||||
return false;
|
|
||||||
//printf( "%s\n", tracer_pid );
|
|
||||||
return !!Q_atoi( (const char*)tracer_pid );
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#undef DEBUG_BREAK
|
|
||||||
#ifdef __i386__
|
|
||||||
#define DEBUG_BREAK \
|
|
||||||
if( Sys_DebuggerPresent() ) \
|
if( Sys_DebuggerPresent() ) \
|
||||||
asm volatile("int $3;")
|
asm volatile("int $3;")
|
||||||
#else
|
#else
|
||||||
#define DEBUG_BREAK \
|
#define DEBUG_BREAK \
|
||||||
if( Sys_DebuggerPresent() ) \
|
if( Sys_DebuggerPresent() ) \
|
||||||
raise( SIGINT )
|
raise( SIGINT )
|
||||||
#endif
|
#endif
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined _WIN32 && !defined XASH_64BIT
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
|
|
||||||
|
|
||||||
BOOL WINAPI IsDebuggerPresent(void);
|
|
||||||
#define DEBUG_BREAK if( IsDebuggerPresent() ) \
|
|
||||||
_asm{ int 3 }
|
|
||||||
#else
|
|
||||||
#define DEBUG_BREAK if( IsDebuggerPresent() ) \
|
|
||||||
asm volatile("int $3;")
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef XASH_DEDICATED
|
#ifndef XASH_DEDICATED
|
||||||
|
@ -192,13 +110,7 @@ void Sys_Sleep( int msec )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
msec = min( msec, 1000 );
|
msec = min( msec, 1000 );
|
||||||
#if XASH_TIMER == TIMER_WIN32
|
Platform_Sleep( msec );
|
||||||
Sleep( msec );
|
|
||||||
#elif XASH_TIMER == TIMER_SDL
|
|
||||||
SDL_Delay( msec );
|
|
||||||
#elif XASH_TIMER == TIMER_LINUX
|
|
||||||
usleep( msec * 1000 );
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
73
engine/platform/linux/sys_linux.c
Normal file
73
engine/platform/linux/sys_linux.c
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
/*
|
||||||
|
sys_linux.c - Linux system utils
|
||||||
|
Copyright (C) 2018 a1batross
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include "platform/platform.h"
|
||||||
|
|
||||||
|
#if XASH_TIMER == TIMER_LINUX
|
||||||
|
double Platform_DoubleTime( void )
|
||||||
|
{
|
||||||
|
static longtime_t g_PerformanceFrequency;
|
||||||
|
static longtime_t g_ClockStart;
|
||||||
|
longtime_t CurrentTime;
|
||||||
|
struct timespec ts;
|
||||||
|
|
||||||
|
if( !g_PerformanceFrequency )
|
||||||
|
{
|
||||||
|
struct timespec res;
|
||||||
|
if( !clock_getres(CLOCK_MONOTONIC, &res) )
|
||||||
|
g_PerformanceFrequency = 1000000000LL/res.tv_nsec;
|
||||||
|
}
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||||
|
return (double) ts.tv_sec + (double) ts.tv_nsec/1000000000.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Platform_Sleep( int msec )
|
||||||
|
{
|
||||||
|
usleep( msec * 1000 );
|
||||||
|
}
|
||||||
|
#endif // XASH_TIMER == TIMER_LINUX
|
||||||
|
|
||||||
|
qboolean Sys_DebuggerPresent( void )
|
||||||
|
{
|
||||||
|
char buf[1024];
|
||||||
|
|
||||||
|
int status_fd = open( "/proc/self/status", O_RDONLY );
|
||||||
|
if ( status_fd == -1 )
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
ssize_t num_read = read( status_fd, buf, sizeof( buf ) );
|
||||||
|
|
||||||
|
if ( num_read > 0 )
|
||||||
|
{
|
||||||
|
static const char TracerPid[] = "TracerPid:";
|
||||||
|
const byte *tracer_pid;
|
||||||
|
|
||||||
|
buf[num_read] = 0;
|
||||||
|
tracer_pid = (const byte*)Q_strstr( buf, TracerPid );
|
||||||
|
if( !tracer_pid )
|
||||||
|
return false;
|
||||||
|
//printf( "%s\n", tracer_pid );
|
||||||
|
while( *tracer_pid < '0' || *tracer_pid > '9' )
|
||||||
|
if( *tracer_pid++ == '\n' )
|
||||||
|
return false;
|
||||||
|
//printf( "%s\n", tracer_pid );
|
||||||
|
return !!Q_atoi( (const char*)tracer_pid );
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
|
@ -17,6 +17,23 @@ GNU General Public License for more details.
|
||||||
#ifndef PLATFORM_H
|
#ifndef PLATFORM_H
|
||||||
#define PLATFORM_H
|
#define PLATFORM_H
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
#include "system.h"
|
||||||
|
#include "defaults.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
==============================================================================
|
||||||
|
|
||||||
|
SYSTEM UTILS
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
*/
|
||||||
|
double Platform_DoubleTime( void );
|
||||||
|
void Platform_Sleep( int msec );
|
||||||
|
// commented out, as this is an optional feature or maybe implemented in system API directly
|
||||||
|
// see system.c
|
||||||
|
// qboolean Sys_DebuggerPresent( void );
|
||||||
|
|
||||||
/*
|
/*
|
||||||
==============================================================================
|
==============================================================================
|
||||||
|
|
||||||
|
|
39
engine/platform/sdl/sys_sdl.c
Normal file
39
engine/platform/sdl/sys_sdl.c
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
/*
|
||||||
|
sys_sdl.c - SDL2 system utils
|
||||||
|
Copyright (C) 2018 a1batross
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <SDL.h>
|
||||||
|
#include "platform/platform.h"
|
||||||
|
|
||||||
|
#if XASH_TIMER == TIMER_SDL
|
||||||
|
double Platform_DoubleTime( void )
|
||||||
|
{
|
||||||
|
static longtime_t g_PerformanceFrequency;
|
||||||
|
static longtime_t g_ClockStart;
|
||||||
|
longtime_t CurrentTime;
|
||||||
|
|
||||||
|
if( !g_PerformanceFrequency )
|
||||||
|
{
|
||||||
|
g_PerformanceFrequency = SDL_GetPerformanceFrequency();
|
||||||
|
g_ClockStart = SDL_GetPerformanceCounter();
|
||||||
|
}
|
||||||
|
CurrentTime = SDL_GetPerformanceCounter();
|
||||||
|
return (double)( CurrentTime - g_ClockStart ) / (double)( g_PerformanceFrequency );
|
||||||
|
}
|
||||||
|
|
||||||
|
void Platform_Sleep( int msec )
|
||||||
|
{
|
||||||
|
SDL_Delay( msec );
|
||||||
|
}
|
||||||
|
#endif // XASH_TIMER == TIMER_SDL
|
47
engine/platform/win32/sys_win.c
Normal file
47
engine/platform/win32/sys_win.c
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
/*
|
||||||
|
sys_win.c - win32 system utils
|
||||||
|
Copyright (C) 2018 a1batross
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
#include "platform/platform.h"
|
||||||
|
|
||||||
|
#if XASH_TIMER == TIMER_WIN32
|
||||||
|
double Platform_DoubleTime( void )
|
||||||
|
{
|
||||||
|
static LARGE_INTEGER g_PerformanceFrequency;
|
||||||
|
static LARGE_INTEGER g_ClockStart;
|
||||||
|
LARGE_INTEGER CurrentTime;
|
||||||
|
|
||||||
|
if( !g_PerformanceFrequency.QuadPart )
|
||||||
|
{
|
||||||
|
QueryPerformanceFrequency( &g_PerformanceFrequency );
|
||||||
|
QueryPerformanceCounter( &g_ClockStart );
|
||||||
|
}
|
||||||
|
QueryPerformanceCounter( &CurrentTime );
|
||||||
|
|
||||||
|
return (double)( CurrentTime.QuadPart - g_ClockStart.QuadPart ) / (double)( g_PerformanceFrequency.QuadPart );
|
||||||
|
}
|
||||||
|
|
||||||
|
void Platform_Sleep( int msec )
|
||||||
|
{
|
||||||
|
Sleep( msec );
|
||||||
|
}
|
||||||
|
#endif // XASH_TIMER == TIMER_WIN32
|
||||||
|
|
||||||
|
qboolean Sys_DebuggerPresent( void )
|
||||||
|
{
|
||||||
|
return IsDebuggerPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,6 +74,9 @@ def build(bld):
|
||||||
libs += ['USER32', 'SHELL32', 'GDI32', 'ADVAPI32', 'DBGHELP']
|
libs += ['USER32', 'SHELL32', 'GDI32', 'ADVAPI32', 'DBGHELP']
|
||||||
source += bld.path.ant_glob(['platform/win32/*.c'])
|
source += bld.path.ant_glob(['platform/win32/*.c'])
|
||||||
|
|
||||||
|
if bld.env.DEST_OS == 'linux':
|
||||||
|
source += bld.path.ant_glob(['platform/linux/*.c'])
|
||||||
|
|
||||||
# add client files and sdl2 library
|
# add client files and sdl2 library
|
||||||
if not bld.env.DEDICATED:
|
if not bld.env.DEDICATED:
|
||||||
libs.append( 'SDL2' )
|
libs.append( 'SDL2' )
|
||||||
|
|
Loading…
Add table
Reference in a new issue