Skip to content

Commit

Permalink
moar cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
gafferongames committed Dec 25, 2023
1 parent 6f32a45 commit f0a5e60
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 182 deletions.
189 changes: 7 additions & 182 deletions yojimbo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,188 +90,6 @@ void ShutdownYojimbo()

// ---------------------------------------------------------------------------------

// ---------------------------------------------------------------------------------

#include "yojimbo.h"
#include "netcode.h"
#include "reliable.h"
#include <stdarg.h>
#include <stdio.h>

static void default_assert_handler( const char * condition, const char * function, const char * file, int line )
{
// We use YOJIMBO_LOG_LEVEL_NONE because it's lower than YOJIMBO_LOG_LEVEL_ERROR, so even if you suppress errors (by setting
// yojimbo_log_level(YOJIMBO_LOG_LEVEL_NONE)), this will still be logged.
yojimbo_printf( YOJIMBO_LOG_LEVEL_NONE, "assert failed: ( %s ), function %s, file %s, line %d\n", condition, function, file, line );
#if defined( __GNUC__ )
__builtin_trap();
#elif defined( _MSC_VER )
__debugbreak();
#endif
}

static int log_level = 0;
static int (*printf_function)( const char *, ... ) = printf;
void (*yojimbo_assert_function)( const char *, const char *, const char * file, int line ) = default_assert_handler;

void yojimbo_log_level( int level )
{
log_level = level;
netcode_log_level( level );
reliable_log_level( level );
}

void yojimbo_set_printf_function( int (*function)( const char *, ... ) )
{
yojimbo_assert( function );
printf_function = function;
netcode_set_printf_function( function );
reliable_set_printf_function( function );
}

void yojimbo_set_assert_function( void (*function)( const char *, const char *, const char * file, int line ) )
{
yojimbo_assert_function = function;
netcode_set_assert_function( function );
reliable_set_assert_function( function );
}

#if YOJIMBO_ENABLE_LOGGING

void yojimbo_printf( int level, const char * format, ... )
{
if ( level > log_level )
return;
va_list args;
va_start( args, format );
char buffer[4*1024];
vsnprintf( buffer, sizeof(buffer), format, args );
printf_function( "%s", buffer );
va_end( args );
}

#else // #if YOJIMBO_ENABLE_LOGGING

void yojimbo_printf( int level, const char * format, ... )
{
(void) level;
(void) format;
}

#endif // #if YOJIMBO_ENABLE_LOGGING

#if __APPLE__

// ===============================
// MacOS
// ===============================

#include <unistd.h>
#include <mach/mach.h>
#include <mach/mach_time.h>

void yojimbo_sleep( double time )
{
usleep( (int) ( time * 1000000 ) );
}

double yojimbo_time()
{
static uint64_t start = 0;

static mach_timebase_info_data_t timebase_info;

if ( start == 0 )
{
mach_timebase_info( &timebase_info );
start = mach_absolute_time();
return 0.0;
}

uint64_t current = mach_absolute_time();

if ( current < start )
current = start;

return ( double( current - start ) * double( timebase_info.numer ) / double( timebase_info.denom ) ) / 1000000000.0;
}

#elif __linux

// ===============================
// Linux
// ===============================

#include <unistd.h>
#include <time.h>

void yojimbo_sleep( double time )
{
usleep( (int) ( time * 1000000 ) );
}

double yojimbo_time()
{
static double start = -1;

if ( start == -1 )
{
timespec ts;
clock_gettime( CLOCK_MONOTONIC_RAW, &ts );
start = ts.tv_sec + double( ts.tv_nsec ) / 1000000000.0;
return 0.0;
}

timespec ts;
clock_gettime( CLOCK_MONOTONIC_RAW, &ts );
double current = ts.tv_sec + double( ts.tv_nsec ) / 1000000000.0;
if ( current < start )
current = start;
return current - start;
}

#elif defined(_WIN32)

// ===============================
// Windows
// ===============================

#define NOMINMAX
#include <windows.h>

void yojimbo_sleep( double time )
{
const int milliseconds = time * 1000;
Sleep( milliseconds );
}

static bool timer_initialized = false;
static LARGE_INTEGER timer_frequency;
static LARGE_INTEGER timer_start;

double yojimbo_time()
{
if ( !timer_initialized )
{
QueryPerformanceFrequency( &timer_frequency );
QueryPerformanceCounter( &timer_start );
timer_initialized = true;
}
LARGE_INTEGER now;
QueryPerformanceCounter( &now );
if ( now.QuadPart < timer_start.QuadPart )
now.QuadPart = timer_start.QuadPart;
return double( now.QuadPart - timer_start.QuadPart ) / double( timer_frequency.QuadPart );
}

#else

#error unsupported platform!

#endif

// ---------------------------------------------------------------------------------

namespace yojimbo
{
void ChannelPacketData::Initialize()
Expand Down Expand Up @@ -2067,6 +1885,8 @@ namespace yojimbo

// ---------------------------------------------------------------------------------

#include "reliable.h"

namespace yojimbo
{
BaseClient::BaseClient( Allocator & allocator, const ClientServerConfig & config, Adapter & adapter, double time ) : m_config( config )
Expand Down Expand Up @@ -2313,9 +2133,14 @@ namespace yojimbo
reliable_endpoint_bandwidth( m_endpoint, &info.sentBandwidth, &info.receivedBandwidth, &info.ackedBandwidth );
}
}
}

// ------------------------------------------------------------------------------------------------------------------

#include "netcode.h"

namespace yojimbo
{
Client::Client( Allocator & allocator, const Address & address, const ClientServerConfig & config, Adapter & adapter, double time )
: BaseClient( allocator, config, adapter, time ), m_config( config ), m_address( address )
{
Expand Down
Loading

0 comments on commit f0a5e60

Please sign in to comment.