Kohi Game Engine
platform.h File Reference

This file contains the platform layer, or at least the interface to it. Each platform should provide its own implementation of this in a .c file, and should be compiled exclusively of the rest. More...

#include "defines.h"

Go to the source code of this file.

Data Structures

struct  platform_system_config
 
struct  dynamic_library_function
 
struct  dynamic_library
 

Typedefs

typedef struct platform_system_config platform_system_config
 
typedef struct dynamic_library_function dynamic_library_function
 
typedef struct dynamic_library dynamic_library
 
typedef enum platform_error_code platform_error_code
 

Enumerations

enum  platform_error_code {
  PLATFORM_ERROR_SUCCESS = 0 , PLATFORM_ERROR_UNKNOWN = 1 , PLATFORM_ERROR_FILE_NOT_FOUND = 2 , PLATFORM_ERROR_FILE_LOCKED = 3 ,
  PLATFORM_ERROR_FILE_EXISTS = 4
}
 

Functions

b8 platform_system_startup (u64 *memory_requirement, void *state, void *config)
 Performs startup routines within the platform layer. Should be called twice, once to obtain the memory requirement (with state=0), then a second time passing an allocated block of memory to state. More...
 
void platform_system_shutdown (void *plat_state)
 Shuts down the platform layer. More...
 
b8 platform_pump_messages (void)
 Performs any platform-specific message pumping that is required for windowing, etc. More...
 
void * platform_allocate (u64 size, b8 aligned)
 Performs platform-specific memory allocation of the given size. More...
 
void platform_free (void *block, b8 aligned)
 Frees the given block of memory. More...
 
void * platform_zero_memory (void *block, u64 size)
 Performs platform-specific zeroing out of the given block of memory. More...
 
void * platform_copy_memory (void *dest, const void *source, u64 size)
 Copies the bytes of memory in source to dest, of the given size. More...
 
void * platform_set_memory (void *dest, i32 value, u64 size)
 Sets the bytes of memory to the given value. More...
 
void platform_console_write (const char *message, u8 colour)
 Performs platform-specific printing to the console of the given message and colour code (if supported). More...
 
void platform_console_write_error (const char *message, u8 colour)
 Performs platform-specific printing to the error console of the given message and colour code (if supported). More...
 
f64 platform_get_absolute_time (void)
 Gets the absolute time since the application started. More...
 
KAPI void platform_sleep (u64 ms)
 Sleep on the thread for the provided milliseconds. This blocks the main thread. Should only be used for giving time back to the OS for unused update power. Therefore it is not exported. Times are approximate. More...
 
i32 platform_get_processor_count (void)
 Obtains the number of logical processor cores. More...
 
KAPI void platform_get_handle_info (u64 *out_size, void *memory)
 Obtains the required memory amount for platform-specific handle data, and optionally obtains a copy of that data. Call twice, once with memory=0 to obtain size, then a second time where memory = allocated block. More...
 
KAPI b8 platform_dynamic_library_load (const char *name, dynamic_library *out_library)
 Loads a dynamic library. More...
 
KAPI b8 platform_dynamic_library_unload (dynamic_library *library)
 Unloads the given dynamic library. More...
 
KAPI b8 platform_dynamic_library_load_function (const char *name, dynamic_library *library)
 Loads an exported function of the given name from the provided loaded library. More...
 
KAPI const char * platform_dynamic_library_extension (void)
 Returns the file extension for the current platform. More...
 
KAPI const char * platform_dynamic_library_prefix (void)
 Returns a file prefix for libraries for the current platform. More...
 
KAPI platform_error_code platform_copy_file (const char *source, const char *dest, b8 overwrite_if_exists)
 Copies file at source to destination, optionally overwriting. More...
 
KAPI b8 platform_watch_file (const char *file_path, u32 *out_watch_id)
 Watch a file at the given path. More...
 
KAPI b8 platform_unwatch_file (u32 watch_id)
 Stops watching the file with the given watch identifier. More...
 

Detailed Description

This file contains the platform layer, or at least the interface to it. Each platform should provide its own implementation of this in a .c file, and should be compiled exclusively of the rest.

Author
Travis Vroman (travi.nosp@m.s@ko.nosp@m.hieng.nosp@m.ine..nosp@m.com)
Version
1.0
Date
2022-01-10

Typedef Documentation

◆ dynamic_library

◆ dynamic_library_function

◆ platform_error_code

◆ platform_system_config

Enumeration Type Documentation

◆ platform_error_code

Enumerator
PLATFORM_ERROR_SUCCESS 
PLATFORM_ERROR_UNKNOWN 
PLATFORM_ERROR_FILE_NOT_FOUND 
PLATFORM_ERROR_FILE_LOCKED 
PLATFORM_ERROR_FILE_EXISTS 

Function Documentation

◆ platform_allocate()

void* platform_allocate ( u64  size,
b8  aligned 
)

Performs platform-specific memory allocation of the given size.

Parameters
sizeThe size of the allocation in bytes.
alignedIndicates if the allocation should be aligned.
Returns
A pointer to a block of allocated memory.

◆ platform_console_write()

void platform_console_write ( const char *  message,
u8  colour 
)

Performs platform-specific printing to the console of the given message and colour code (if supported).

Parameters
messageThe message to be printed.
colourThe colour to print the text in (if supported).

◆ platform_console_write_error()

void platform_console_write_error ( const char *  message,
u8  colour 
)

Performs platform-specific printing to the error console of the given message and colour code (if supported).

Parameters
messageThe message to be printed.
colourThe colour to print the text in (if supported).

◆ platform_copy_file()

KAPI platform_error_code platform_copy_file ( const char *  source,
const char *  dest,
b8  overwrite_if_exists 
)

Copies file at source to destination, optionally overwriting.

Parameters
sourceThe source file path.
destThe destination file path.
overwrite_if_existsIndicates if the file should be overwritten if it exists.
Returns
An error code indicating success or failure.

◆ platform_copy_memory()

void* platform_copy_memory ( void *  dest,
const void *  source,
u64  size 
)

Copies the bytes of memory in source to dest, of the given size.

Parameters
destThe destination memory block.
sourceThe source memory block.
sizeThe size of data to be copied.
Returns
A pointer to the destination block of memory.

◆ platform_dynamic_library_extension()

KAPI const char* platform_dynamic_library_extension ( void  )

Returns the file extension for the current platform.

◆ platform_dynamic_library_load()

KAPI b8 platform_dynamic_library_load ( const char *  name,
dynamic_library out_library 
)

Loads a dynamic library.

Parameters
nameThe name of the library file, excluding the extension. Required.
out_libraryA pointer to hold the loaded library. Required.
Returns
True on success; otherwise false.

◆ platform_dynamic_library_load_function()

KAPI b8 platform_dynamic_library_load_function ( const char *  name,
dynamic_library library 
)

Loads an exported function of the given name from the provided loaded library.

Parameters
nameThe function name to be loaded.
libraryA pointer to the library to load the function from.
Returns
True on success; otherwise false.

◆ platform_dynamic_library_prefix()

KAPI const char* platform_dynamic_library_prefix ( void  )

Returns a file prefix for libraries for the current platform.

◆ platform_dynamic_library_unload()

KAPI b8 platform_dynamic_library_unload ( dynamic_library library)

Unloads the given dynamic library.

Parameters
libraryA pointer to the loaded library. Required.
Returns
True on success; otherwise false.

◆ platform_free()

void platform_free ( void *  block,
b8  aligned 
)

Frees the given block of memory.

Parameters
blockThe block to be freed.
alignedIndicates if the block of memory is aligned.

◆ platform_get_absolute_time()

f64 platform_get_absolute_time ( void  )

Gets the absolute time since the application started.

Returns
The absolute time since the application started.

◆ platform_get_handle_info()

KAPI void platform_get_handle_info ( u64 out_size,
void *  memory 
)

Obtains the required memory amount for platform-specific handle data, and optionally obtains a copy of that data. Call twice, once with memory=0 to obtain size, then a second time where memory = allocated block.

Parameters
out_sizeA pointer to hold the memory requirement.
memoryAllocated block of memory.

◆ platform_get_processor_count()

i32 platform_get_processor_count ( void  )

Obtains the number of logical processor cores.

Returns
The number of logical processor cores.

◆ platform_pump_messages()

b8 platform_pump_messages ( void  )

Performs any platform-specific message pumping that is required for windowing, etc.

Returns
True on success; otherwise false.

◆ platform_set_memory()

void* platform_set_memory ( void *  dest,
i32  value,
u64  size 
)

Sets the bytes of memory to the given value.

Parameters
destThe destination block of memory.
valueThe value to be set.
sizeThe size of data to set.
Returns
A pointer to the set block of memory.

◆ platform_sleep()

KAPI void platform_sleep ( u64  ms)

Sleep on the thread for the provided milliseconds. This blocks the main thread. Should only be used for giving time back to the OS for unused update power. Therefore it is not exported. Times are approximate.

Parameters
msThe number of milliseconds to sleep for.

◆ platform_system_shutdown()

void platform_system_shutdown ( void *  plat_state)

Shuts down the platform layer.

Parameters
plat_stateA pointer to the platform layer state.

◆ platform_system_startup()

b8 platform_system_startup ( u64 memory_requirement,
void *  state,
void *  config 
)

Performs startup routines within the platform layer. Should be called twice, once to obtain the memory requirement (with state=0), then a second time passing an allocated block of memory to state.

Parameters
memory_requirementA pointer to hold the memory requirement in bytes.
stateA pointer to a block of memory to hold state. If obtaining memory requirement only, pass 0.
configA pointer to a configuration platform_system_config structure required by this system.
Returns
True on success; otherwise false.

◆ platform_unwatch_file()

KAPI b8 platform_unwatch_file ( u32  watch_id)

Stops watching the file with the given watch identifier.

Parameters
watch_idThe watch identifier
Returns
True on success; otherwise false.

◆ platform_watch_file()

KAPI b8 platform_watch_file ( const char *  file_path,
u32 out_watch_id 
)

Watch a file at the given path.

Parameters
file_pathThe file path. Required.
out_watch_idA pointer to hold the watch identifier.
Returns
True on success; otherwise false.

◆ platform_zero_memory()

void* platform_zero_memory ( void *  block,
u64  size 
)

Performs platform-specific zeroing out of the given block of memory.

Parameters
blockThe block to be zeroed out.
sizeThe size of data to zero out.
Returns
A pointer to the zeroed out block of memory.