cmdfx 0.2.0
Lightweight game engine for your terminal
Loading...
Searching...
No Matches
util.h File Reference

Cross-Platform Utilities for CmdFX. More...

Go to the source code of this file.

Macros

#define MAX_INTERNAL_CMDFX_MUTEXES   128
 Maximum number of internal mutexes.
 

Functions

int CmdFX_getTickSpeed ()
 Gets the tick speed of cmdfx.
 
int CmdFX_setTickSpeed (int tickspeed)
 Sets the tick speed of cmdfx.
 
unsigned long currentTimeMillis ()
 Gets the current time in milliseconds.
 
unsigned long long currentTimeNanos ()
 Gets the current time in nanoseconds.
 
void sleepMillis (unsigned long millis)
 Pauses the program for a specified number of milliseconds.
 
void sleepNanos (unsigned long long nanos)
 Pauses the program for a specified number of nanoseconds.
 
double clamp_d (double value, double min, double max)
 Clamps a value between a minimum and maximum.
 
float clamp_f (float value, float min, float max)
 Clamps a value between a minimum and maximum.
 
int clamp_i (int value, int min, int max)
 Clamps a value between a minimum and maximum.
 
double lerp_d (double a, double b, double t)
 Linearly interpolates between two values.
 
float lerp_f (float a, float b, float t)
 Linearly interpolates between two values.
 
int lerp_i (int a, int b, double t)
 Linearly interpolates between two values.
 
void rgb_to_hsv (int rgb, double *h, double *s, double *v)
 Converts an RGB color to HSV.
 
int hsv_to_rgb (double h, double s, double v)
 Converts an HSV color to RGB.
 
int lerp_color (int rgb1, int rgb2, double t)
 Linearly interpolates between two colors.
 
int CmdFX_initThreadSafe ()
 Initializes thread-safe functions.
 
int CmdFX_isThreadSafeEnabled ()
 Checks if thread-safe functions are enabled.
 
int CmdFX_destroyThreadSafe ()
 Cleans up thread-safe functions.
 
void * CmdFX_getInternalMutex (int index)
 Gets an internal mutex.
 
int CmdFX_lockMutex (void *mutex)
 Locks a mutex.
 
int CmdFX_unlockMutex (void *mutex)
 Unlocks a mutex.
 
void CmdFX_tryLockMutex (int id)
 Tries to lock a mutex.
 
void CmdFX_tryUnlockMutex (int id)
 Tries to unlock a mutex.
 

Detailed Description

Cross-Platform Utilities for CmdFX.

Author
Gregory Mitchell (me@gm.nosp@m.itch.nosp@m.215.x.nosp@m.yz)
Version
0.1.0
Date
2025-01-21

Macro Definition Documentation

◆ MAX_INTERNAL_CMDFX_MUTEXES

#define MAX_INTERNAL_CMDFX_MUTEXES   128

Maximum number of internal mutexes.

This constant defines the maximum number of internal mutexes that can be created. It is used to limit the number of mutexes that can be created in the system.

Function Documentation

◆ clamp_d()

double clamp_d ( double value,
double min,
double max )

Clamps a value between a minimum and maximum.

Parameters
valueThe value to clamp.
minThe minimum value.
maxThe maximum value.
Returns
The clamped value.

◆ clamp_f()

float clamp_f ( float value,
float min,
float max )

Clamps a value between a minimum and maximum.

Parameters
valueThe value to clamp.
minThe minimum value.
maxThe maximum value.
Returns
The clamped value.

◆ clamp_i()

int clamp_i ( int value,
int min,
int max )

Clamps a value between a minimum and maximum.

Parameters
valueThe value to clamp.
minThe minimum value.
maxThe maximum value.
Returns
The clamped value.

◆ CmdFX_destroyThreadSafe()

int CmdFX_destroyThreadSafe ( )

Cleans up thread-safe functions.

This method cleans up internal mutexes and ensures that all thread-safe operations are properly terminated. It should be called after all unsafe functions have been used or at the end of the program.

Returns
0 if successful, -1 if an error occurred.

◆ CmdFX_getInternalMutex()

void * CmdFX_getInternalMutex ( int index)

Gets an internal mutex.

This method gets an internal mutex that can be used for synchronizing access to shared resources. The index should be between 0 and _MAX_INTERNAL_CMDFX_MUTEXES - 1.

Parameters
indexThe index of the mutex to get.
Returns
Pointer to the platform-specific mutex object.

◆ CmdFX_getTickSpeed()

int CmdFX_getTickSpeed ( )

Gets the tick speed of cmdfx.

The tick speed controls the rate of which ticking happens, such as the physics engine. The default value is 12.

Returns
The cmdfx Tick Speed

◆ CmdFX_initThreadSafe()

int CmdFX_initThreadSafe ( )

Initializes thread-safe functions.

This method initializes internal mutexes and ensures thread-safe operations. It should be called before using any unsafe functions that may be accessed by multiple threads or at the start of the program.

Returns
0 if successful, -1 if an error occurred.

◆ CmdFX_isThreadSafeEnabled()

int CmdFX_isThreadSafeEnabled ( )

Checks if thread-safe functions are enabled.

This method checks if thread-safe functions are enabled. It returns 1 if they are enabled, 0 if they are not.

Returns
1 if thread-safe functions are enabled, 0 otherwise.

◆ CmdFX_lockMutex()

int CmdFX_lockMutex ( void * mutex)

Locks a mutex.

This method locks a mutex, blocking the calling thread until the mutex is available.

Parameters
mutexPointer to the platform-specific mutex object.
Returns
0 if successful, -1 if an error occurred.

◆ CmdFX_setTickSpeed()

int CmdFX_setTickSpeed ( int tickspeed)

Sets the tick speed of cmdfx.

The tick speed controls the rate of which ticking happens, such as the physics engine. The default value is 12.

Parameters
tickspeedThe new tickspeed to set
Returns
0 if successful, -1 if an error happened

◆ CmdFX_tryLockMutex()

void CmdFX_tryLockMutex ( int id)

Tries to lock a mutex.

This method tries to lock a mutex without blocking the calling thread. If the mutex is already locked, it returns immediately.

It will also initialize the mutex if it is not already initialized.

Parameters
idThe ID of the mutex to lock.

◆ CmdFX_tryUnlockMutex()

void CmdFX_tryUnlockMutex ( int id)

Tries to unlock a mutex.

This method tries to unlock a mutex without blocking the calling thread. If the mutex is already unlocked, it returns immediately.

Parameters
idThe ID of the mutex to unlock.

◆ CmdFX_unlockMutex()

int CmdFX_unlockMutex ( void * mutex)

Unlocks a mutex.

This method unlocks a mutex, allowing other threads to access the shared resource.

Parameters
mutexPointer to the platform-specific mutex object.
Returns
0 if successful, -1 if an error occurred.

◆ currentTimeMillis()

unsigned long currentTimeMillis ( )

Gets the current time in milliseconds.

Returns
The current time in milliseconds.

◆ currentTimeNanos()

unsigned long long currentTimeNanos ( )

Gets the current time in nanoseconds.

Returns
The current time in nanoseconds.

◆ hsv_to_rgb()

int hsv_to_rgb ( double h,
double s,
double v )

Converts an HSV color to RGB.

Parameters
hThe hue.
sThe saturation.
vThe value.
Returns
The RGB color.

◆ lerp_color()

int lerp_color ( int rgb1,
int rgb2,
double t )

Linearly interpolates between two colors.

Parameters
rgb1The first color in RGB format.
rgb2The second color in RGB format.
tThe interpolation factor.
Returns
The interpolated color.

◆ lerp_d()

double lerp_d ( double a,
double b,
double t )

Linearly interpolates between two values.

Parameters
aThe first value.
bThe second value.
tThe interpolation factor.
Returns
The interpolated value.

◆ lerp_f()

float lerp_f ( float a,
float b,
float t )

Linearly interpolates between two values.

Parameters
aThe first value.
bThe second value.
tThe interpolation factor.
Returns
The interpolated value.

◆ lerp_i()

int lerp_i ( int a,
int b,
double t )

Linearly interpolates between two values.

Parameters
aThe first value.
bThe second value.
tThe interpolation factor.
Returns
The interpolated value.

◆ rgb_to_hsv()

void rgb_to_hsv ( int rgb,
double * h,
double * s,
double * v )

Converts an RGB color to HSV.

Parameters
rgbThe RGB color.
hThe pointer to the hue.
sThe pointer to the saturation.
vThe pointer to the value.

◆ sleepMillis()

void sleepMillis ( unsigned long millis)

Pauses the program for a specified number of milliseconds.

Parameters
millisThe number of milliseconds to pause.

◆ sleepNanos()

void sleepNanos ( unsigned long long nanos)

Pauses the program for a specified number of nanoseconds.

Parameters
nanosThe number of nanoseconds to pause.