Skip to content
modkitv0.2

assets.h

#include <modkit/assets.h>42 functions · 5 structs · 4 enums · 16 typedefs · 4 macros

Unified asset loading system with path resolution.

Provides a unified interface for loading various asset types (models, textures, fonts, audio, files) with support for search paths to simplify asset management across platforms. Usage:

Functions

mk_asset_mount_validfunction

bool mk_asset_mount_valid(mk_asset_mount_t mount)

Perform the asset mount valid operation.

ParameterTypeDescription
mountmk_asset_mount_tValue for mount.

Returns True when the operation succeeds.

mk_asset_request_validfunction

bool mk_asset_request_valid(mk_asset_request_t req)

Request valid for the asset.

ParameterTypeDescription
reqmk_asset_request_tValue for req.

Returns True when the operation succeeds.

mk_assets_add_pathfunction

bool mk_assets_add_path(const char *path)

Add a search path for asset resolution.

Paths are searched in the order they were added.

ParameterTypeDescription
pathconst char *Directory path to search (will be copied)

Returns true if path was added, false if max paths reached

mk_assets_cancelfunction

void mk_assets_cancel(mk_asset_request_t request)

Cancel a pending async request.

Callback will NOT be called after cancellation.

ParameterTypeDescription
requestmk_asset_request_tRequest handle from async load call

mk_assets_clear_pathsfunction

void mk_assets_clear_paths(void)

Clear all search paths.

mk_assets_existsfunction

bool mk_assets_exists(const char *path)

Check if a file exists at the given path.

ParameterTypeDescription
pathconst char *Path to check (absolute or relative)

Returns true if file exists

mk_assets_get_pathfunction

const char * mk_assets_get_path(int index)

Get a search path by index.

ParameterTypeDescription
indexintPath index (0 to path_count-1)

Returns Path string or NULL if index is invalid

mk_assets_get_path_countfunction

int mk_assets_get_path_count(void)

Get number of registered search paths.

Returns The resulting value.

mk_assets_get_statefunction

mk_asset_state_t mk_assets_get_state(mk_asset_request_t request)

Get the current state of an async request.

ParameterTypeDescription
requestmk_asset_request_tRequest handle

Returns Current state or MK_ASSET_STATE_ERROR if invalid

mk_assets_initfunction

bool mk_assets_init(void)

Initialize the assets system.

Called automatically on first use.

Returns true on success

mk_assets_load_atlas_assetfunction

mk_atlas_t mk_assets_load_atlas_asset(const char *catalog_path)

Load an atlas through loose paths or mounted catalogs.

catalog_path is a base logical path. The loader reads <path>.png and <path>.json, uploads the image, and parses TexturePacker metadata.

ParameterTypeDescription
catalog_pathconst char *Value for catalog path.

Returns The resulting handle or value.

mk_assets_load_compute_shaderfunction

mk_shader_t mk_assets_load_compute_shader(const char *logical_id)

Load a cooked compute shader variant for the active bgfx renderer.

Declared catalog uniforms follow the same ownership rules as graphics shaders.

ParameterTypeDescription
logical_idconst char *Value for logical ID.

Returns The resulting handle or value.

mk_assets_load_filefunction

void * mk_assets_load_file(const char *path, size_t *out_size)

Load raw file data into memory using search paths.

The returned data must be freed by the caller using free().

ParameterTypeDescription
pathconst char *Relative or absolute path to file
out_sizesize_t *Pointer to receive file size (can be NULL)

Returns Pointer to file data or NULL on failure

mk_assets_load_file_asyncfunction

mk_asset_request_t mk_assets_load_file_async(const char *path, mk_asset_data_callback_t callback, mk_asset_progress_callback_t progress_cb, void *user_data)

Load a file asynchronously.

On web: uses Fetch API with progress tracking. On native: uses async I/O or processes in poll loop.

ParameterTypeDescription
pathconst char *Path to file (resolved via search paths)
callbackmk_asset_data_callback_tCalled when load completes (data must be freed by caller)
progress_cbmk_asset_progress_callback_tCalled during loading (can be NULL)
user_datavoid *Passed to callbacks

Returns Request handle for cancellation/status

mk_assets_load_fontfunction

mk_font_t mk_assets_load_font(const char *path, float size)

Load a font using search paths.

ParameterTypeDescription
pathconst char *Relative or absolute path to font file
sizefloatFont size in pixels

Returns Font handle or MK_FONT_INVALID on failure

mk_assets_load_font_asyncfunction

mk_asset_request_t mk_assets_load_font_async(const char *path, float size, mk_asset_font_callback_t callback, mk_asset_progress_callback_t progress_cb, void *user_data)

Load a font asynchronously.

ParameterTypeDescription
pathconst char *Value for path.
sizefloatSize in bytes.
callbackmk_asset_font_callback_tCompletion callback.
progress_cbmk_asset_progress_callback_tValue for progress.
user_datavoid *Caller-provided context.

Returns The resulting handle or value.

mk_assets_load_font_exfunction

mk_font_t mk_assets_load_font_ex(const mk_font_load_desc_t *desc)

Load a raw or cooked font through asset search paths with explicit options.

desc->path is resolved by the asset system; desc->data is ignored.

ParameterTypeDescription
descconst mk_font_load_desc_t *Configuration descriptor.

Returns The resulting handle or value.

mk_assets_load_giffunction

mk_gif_t mk_assets_load_gif(const char *path, const mk_gif_desc *desc)

Load an animated GIF using asset search paths.

ParameterTypeDescription
pathconst char *Value for path.
descconst mk_gif_desc *Configuration descriptor.

Returns The resulting handle or value.

mk_assets_load_gif_asyncfunction

mk_asset_request_t mk_assets_load_gif_async(const char *path, const mk_gif_desc *desc, mk_asset_gif_callback_t callback, mk_asset_progress_callback_t progress_cb, void *user_data)

Load and decode an animated GIF asynchronously.

Native builds read, validate, and decode on the asset worker, then create the texture and invoke the callback on the main thread. Web builds perform validation, decode, and texture creation in the fetch completion path.

ParameterTypeDescription
pathconst char *Value for path.
descconst mk_gif_desc *Configuration descriptor.
callbackmk_asset_gif_callback_tCompletion callback.
progress_cbmk_asset_progress_callback_tValue for progress.
user_datavoid *Caller-provided context.

Returns The resulting handle or value.

mk_assets_load_shaderfunction

mk_shader_t mk_assets_load_shader(const char *logical_id)

Load a cooked graphics shader variant for the active bgfx renderer.

Declared catalog uniforms are created before linking and owned by the shader.

ParameterTypeDescription
logical_idconst char *Value for logical ID.

Returns The resulting handle or value.

mk_assets_load_soundfunction

mk_result mk_assets_load_sound(const char *path, const mk_sound_desc *desc, mk_sound_t *out_sound)

Load a sound using search paths.

Supports: WAV, MP3, OGG, FLAC

ParameterTypeDescription
pathconst char *Relative or absolute path to audio file
descconst mk_sound_desc *Sound loading descriptor. NULL inherits cooked streaming and spatial defaults for catalog audio, or ordinary defaults for uncooked files. A non-NULL descriptor overrides cooked policy.
out_soundmk_sound_t *Receives the sound handle

Returns MK_SUCCESS or an error result.

mk_assets_load_sound_asyncfunction

mk_asset_request_t mk_assets_load_sound_async(const char *path, const mk_sound_desc *desc, mk_asset_sound_callback_t callback, mk_asset_progress_callback_t progress_cb, void *user_data)

Load a sound asynchronously.

A NULL descriptor inherits cooked audio policy using the same rules as mk_assets_load_sound().

ParameterTypeDescription
pathconst char *Value for path.
descconst mk_sound_desc *Configuration descriptor.
callbackmk_asset_sound_callback_tCompletion callback.
progress_cbmk_asset_progress_callback_tValue for progress.
user_datavoid *Caller-provided context.

Returns The resulting handle or value.

mk_assets_load_stringfunction

char * mk_assets_load_string(const char *path)

Load file as null-terminated string using search paths.

The returned string must be freed by the caller using free().

ParameterTypeDescription
pathconst char *Relative or absolute path to file

Returns Null-terminated string or NULL on failure

mk_assets_load_texturefunction

mk_texture_t mk_assets_load_texture(const char *path, uint32_t flags)

Load a texture using search paths.

Supports: PNG, JPG, TGA, BMP, HDR, EXR

ParameterTypeDescription
pathconst char *Relative or absolute path to texture file
flagsuint32_tLoading flags (MK_TEXTURE_*)

Returns Texture handle or MK_TEXTURE_INVALID on failure

mk_assets_load_texture_asyncfunction

mk_asset_request_t mk_assets_load_texture_async(const char *path, uint32_t flags, mk_asset_texture_callback_t callback, mk_asset_progress_callback_t progress_cb, void *user_data)

Load a texture asynchronously.

ParameterTypeDescription
pathconst char *Value for path.
flagsuint32_tValue for flags.
callbackmk_asset_texture_callback_tCompletion callback.
progress_cbmk_asset_progress_callback_tValue for progress.
user_datavoid *Caller-provided context.

Returns The resulting handle or value.

mk_assets_load_videofunction

mk_result mk_assets_load_video(const char *logical_id, const mk_video_desc *desc, mk_video_t *out_video)

Create and asynchronously open a video from a logical asset stream.

ParameterTypeDescription
logical_idconst char *Value for logical ID.
descconst mk_video_desc *Configuration descriptor.
out_videomk_video_t *Receives the video.

Returns MK_SUCCESS or an error result.

mk_assets_mountfunction

mk_asset_mount_t mk_assets_mount(const char *catalog_directory, int32_t priority)

Mount a cooked loose catalog directory.

Higher priorities shadow lower priorities; newer mounts win equal priorities.

ParameterTypeDescription
catalog_directoryconst char *Value for catalog directory.
priorityint32_tValue for priority.

Returns The resulting handle or value.

mk_assets_pollfunction

void mk_assets_poll(void)

Poll async asset loading.

Processes completed downloads and invokes callbacks. On native: also processes pending sync loads. Called automatically by mk_frame_begin() if using mk_app_run().

mk_assets_remove_pathfunction

bool mk_assets_remove_path(const char *path)

Remove a search path.

ParameterTypeDescription
pathconst char *Path to remove

Returns true if path was found and removed

mk_assets_replace_compute_shaderfunction

mk_result mk_assets_replace_compute_shader(const char *logical_id, mk_shader_t shader)

Compute equivalent of mk_assets_replace_shader().

ParameterTypeDescription
logical_idconst char *Value for logical ID.
shadermk_shader_tValue for shader.

Returns MK_SUCCESS or an error result.

mk_assets_replace_shaderfunction

mk_result mk_assets_replace_shader(const char *logical_id, mk_shader_t shader)

Replace a live shader's program from the latest cooked catalog variant without changing its handle.

Pipelines and materials keep referring to the same shader. Uniform declarations must remain compatible with the original catalog entry.

ParameterTypeDescription
logical_idconst char *Value for logical ID.
shadermk_shader_tValue for shader.

Returns MK_SUCCESS or an error result.

mk_assets_resolvefunction

bool mk_assets_resolve(const char *relative_path, char *out_path, size_t out_size)

Resolve a relative path to a physical path using search paths.

Tries each search path in order until the file is found. Mounted catalog objects and archive entries are intentionally not resolved.

ParameterTypeDescription
relative_pathconst char *Relative path to resolve
out_pathchar *Buffer to receive the resolved path
out_sizesize_tSize of the output buffer

Returns true if file was found and path resolved

mk_assets_shutdownfunction

void mk_assets_shutdown(void)

Shutdown the assets system.

Clears all search paths.

mk_assets_stream_closefunction

void mk_assets_stream_close(mk_asset_stream_t stream)

Close assets stream.

ParameterTypeDescription
streammk_asset_stream_tValue for stream.

mk_assets_stream_openfunction

mk_result mk_assets_stream_open(const char *path, mk_asset_stream_t *out_stream)

Open an asset without copying it into one large allocation.

Packed objects are read directly from their bounded, uncompressed ZIP entry. Cooked objects are SHA-256 verified on their first open per mounted snapshot.

ParameterTypeDescription
pathconst char *Value for path.
out_streammk_asset_stream_t *Receives the stream.

Returns MK_SUCCESS or an error result.

mk_assets_stream_readfunction

mk_result mk_assets_stream_read(mk_asset_stream_t stream, void *data, size_t byte_count, size_t *out_bytes_read)

Read up to byte_count bytes.

ParameterTypeDescription
streammk_asset_stream_tValue for stream.
datavoid *Data buffer.
byte_countsize_tNumber of byte entries.
out_bytes_readsize_t *Receives the bytes read.

Returns MK_SUCCESS or an error result.

mk_assets_stream_seekfunction

mk_result mk_assets_stream_seek(mk_asset_stream_t stream, int64_t offset, mk_asset_seek_origin_t origin)

Seek within the stream.

ParameterTypeDescription
streammk_asset_stream_tValue for stream.
offsetint64_tValue for offset.
originmk_asset_seek_origin_tValue for origin.

Returns MK_SUCCESS or an error result.

mk_assets_stream_sizefunction

uint64_t mk_assets_stream_size(mk_asset_stream_t stream)

Perform the assets stream size operation.

ParameterTypeDescription
streammk_asset_stream_tValue for stream.

Returns The resulting value.

mk_assets_stream_tellfunction

uint64_t mk_assets_stream_tell(mk_asset_stream_t stream)

Perform the assets stream tell operation.

ParameterTypeDescription
streammk_asset_stream_tValue for stream.

Returns The resulting value.

mk_assets_unmountfunction

bool mk_assets_unmount(mk_asset_mount_t mount)

Release one reference to a mount.

In-flight async loads retain their snapshot.

ParameterTypeDescription
mountmk_asset_mount_tValue for mount.

Returns True when the operation succeeds.

mk_assets_unwatchfunction

bool mk_assets_unwatch(mk_asset_mount_t mount)

Stop watching a mount.

ParameterTypeDescription
mountmk_asset_mount_tValue for mount.

Returns True when the operation succeeds.

mk_assets_watchfunction

bool mk_assets_watch(mk_asset_mount_t mount, mk_asset_watch_callback_t callback, void *user_data)

Watch a loose catalog for atomic catalog replacements on native desktop.

Polling is 200 ms with a 100 ms quiet debounce. The callback runs from mk_assets_poll() on the main thread; change strings live only for the call. Calling watch again replaces the callback. Unsupported platforms return false.

ParameterTypeDescription
mountmk_asset_mount_tValue for mount.
callbackmk_asset_watch_callback_tCompletion callback.
user_datavoid *Caller-provided context.

Returns True when the operation succeeds.

Structs

mk_asset_changestruct

Data for asset change.

FieldTypeDescription
idconst char *The ID.
typemk_asset_type_tThe type.
kindmk_asset_change_kind_tThe kind.

mk_asset_mountstruct

Data for asset mount.

FieldTypeDescription
indexuint32_tThe index.
generationuint32_tThe generation.

mk_asset_progressstruct

Asset loading progress info.

FieldTypeDescription
pathconst char *Asset path being loaded.
bytes_loadedsize_tBytes downloaded so far.
bytes_totalsize_tTotal bytes (0 if unknown).
percentfloat0.0 to 1.0 (-1 if unknown)
statemk_asset_state_tCurrent state.
errorconst char *Error message if state == ERROR.

mk_asset_requeststruct

Async request handle for tracking operations.

FieldTypeDescription
iduint32_tThe ID.

mk_asset_streamstruct

Opaque, seekable byte stream over a loose or packed asset.

FieldTypeDescription
implvoid *The impl.

Enums

mk_asset_change_kindenum

Values for asset change kind.

ValueDescription
MK_ASSET_CHANGE_ADDEDSelects added.
MK_ASSET_CHANGE_MODIFIEDSelects modified.
MK_ASSET_CHANGE_REMOVEDSelects removed.

mk_asset_seek_originenum

Values for asset seek origin.

ValueDescription
MK_ASSET_SEEK_BEGINSelects begin.
MK_ASSET_SEEK_CURRENTSelects current.
MK_ASSET_SEEK_ENDSelects end.

mk_asset_stateenum

Asset loading state.

ValueDescription
MK_ASSET_STATE_PENDINGQueued but not started.
MK_ASSET_STATE_LOADINGCurrently downloading/loading.
MK_ASSET_STATE_COMPLETELoaded successfully.
MK_ASSET_STATE_ERRORFailed to load.

mk_asset_typeenum

Asset type enumeration for generic asset handling.

ValueDescription
MK_ASSET_MODELSelects model.
MK_ASSET_TEXTURESelects texture.
MK_ASSET_FONTSelects font.
MK_ASSET_AUDIOSelects audio.
MK_ASSET_JSONSelects JSON.
MK_ASSET_BINARYSelects binary.
MK_ASSET_SHADERSelects shader.
MK_ASSET_IBLSelects ibl.
MK_ASSET_MIDISelects MIDI.
MK_ASSET_SOUNDFONTSelects soundfont.
MK_ASSET_VIDEOSelects video.

Typedefs

mk_asset_change_kind_ttypedef

typedef enum mk_asset_change_kind mk_asset_change_kind_t

Values for asset change kind.

mk_asset_change_ttypedef

typedef struct mk_asset_change mk_asset_change_t

Data for asset change.

mk_asset_data_callback_ttypedef

typedef void(*) mk_asset_data_callback_t(const char *path, void *data, size_t size, void *user_data)

Generic async callback - receives raw data.

On success: data is non-NULL and must be freed by caller. On error: data is NULL.

mk_asset_font_callback_ttypedef

typedef void(*) mk_asset_font_callback_t(const char *path, mk_font_t font, void *user_data)

Type used for asset font callback.

mk_asset_gif_callback_ttypedef

typedef void(*) mk_asset_gif_callback_t(const char *path, mk_gif_t gif, void *user_data)

Type used for asset gif callback.

mk_asset_mount_ttypedef

typedef struct mk_asset_mount mk_asset_mount_t

Data for asset mount.

mk_asset_progress_callback_ttypedef

typedef void(*) mk_asset_progress_callback_t(const mk_asset_progress_t *progress, void *user_data)

Progress callback for async loading.

mk_asset_progress_ttypedef

typedef struct mk_asset_progress mk_asset_progress_t

Asset loading progress info.

mk_asset_request_ttypedef

typedef struct mk_asset_request mk_asset_request_t

Async request handle for tracking operations.

mk_asset_seek_origin_ttypedef

typedef enum mk_asset_seek_origin mk_asset_seek_origin_t

Values for asset seek origin.

mk_asset_sound_callback_ttypedef

typedef void(*) mk_asset_sound_callback_t(const char *path, mk_sound_t sound, void *user_data)

Type used for asset sound callback.

mk_asset_state_ttypedef

typedef enum mk_asset_state mk_asset_state_t

Asset loading state.

mk_asset_stream_ttypedef

typedef struct mk_asset_stream mk_asset_stream_t

Opaque, seekable byte stream over a loose or packed asset.

mk_asset_texture_callback_ttypedef

typedef void(*) mk_asset_texture_callback_t(const char *path, mk_texture_t texture, void *user_data)

Typed callbacks for specific asset types.

mk_asset_type_ttypedef

typedef enum mk_asset_type mk_asset_type_t

Asset type enumeration for generic asset handling.

mk_asset_watch_callback_ttypedef

typedef void(*) mk_asset_watch_callback_t(mk_asset_mount_t mount, const mk_asset_change_t *changes, size_t change_count, void *user_data)

Type used for asset watch callback.

Macros

MK_ASSET_MOUNT_INVALIDdefine

MK_ASSET_MOUNT_INVALID

Invalid sentinel for asset mount.

MK_ASSET_REQUEST_INVALIDdefine

MK_ASSET_REQUEST_INVALID

Invalid sentinel for asset request.

MK_ASSETS_MAX_PATH_LENdefine

MK_ASSETS_MAX_PATH_LEN

Maximum supported assets path len.

MK_ASSETS_MAX_PATHSdefine

MK_ASSETS_MAX_PATHS

Maximum supported assets paths.