Assets (loading + caching)
Mibo.Raylib provides a simple IAssets interface for loading and caching game assets. It wraps raylib's resource loader functions with automatic caching so you never load the same texture twice.
The IAssets interface
type IAssets =
abstract Texture: path: string -> Texture2D
abstract Font: path: string -> Font
abstract Sound: path: string -> Sound
abstract Model: path: string -> Model
abstract Get: key: string -> 'T voption
abstract Create: key: string * factory: (unit -> 'T) -> 'T
abstract GetOrCreate: key: string * factory: (unit -> 'T) -> 'T
abstract Clear: unit -> unit
abstract Dispose: unit -> unit
Each method loads the file on first call and caches it. Subsequent calls return the cached reference.
Usage
Access assets through the GameContext:
let init (ctx: GameContext): struct(Model * Cmd<Msg>) =
let player = ctx.Assets.Texture("sprites/player.png")
let font = ctx.Assets.Font("fonts/ui.ttf")
let bgm = ctx.Assets.Sound("audio/background.wav")
let enemyModel = ctx.Assets.Model("models/enemy.glb")
{ PlayerTex = player
Font = font
Bgm = bgm
Enemy = enemyModel }, Cmd.none
All these functions cache results automatically:
Method |
Returns |
Description |
|---|---|---|
|
|
2D image asset |
|
|
TrueType/bitmap font |
|
|
Audio effect |
|
|
3D model |
|
|
Retrieve cached custom asset |
|
|
Create and cache custom asset |
|
|
Get cached or create + cache |
|
|
Clear custom asset caches |
Cache Behavior
Automatic caching applies to: - All standard assets (texture, font, sound, model) - First call loads from disk; subsequent calls return cached reference
Clearing caches:
ctx.Assets.Dispose()
This unloads all GPU resources and clears all caches.
Performance Notes
- First load reads from disk; subsequent loads return cached reference
- No built-in eviction — caches grow with unique keys loaded
- GPU resources are created once and cached
For large games, consider chunked loading (per level/biome) with separate IAssets scopes.
Planned features
The following are not yet implemented but are planned:
- JSON helpers (JDeck integration for loading
.jsonfiles) - Custom file loaders (
fromCustom,fromCustomCache)
val string: value: 'T -> string
--------------------
type string = System.String
Mibo.Raylib