Compare commits
84 Commits
e1716a741f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 730a5b2b10 | |||
| 6135d60ddc | |||
| 4c2a883038 | |||
| c88b672f42 | |||
| a162002af2 | |||
| 9810fd51ab | |||
| 857c6b3d47 | |||
| e1498f538d | |||
| aa246eff94 | |||
| 5be21a21d5 | |||
| 8131bcd4d4 | |||
| 4ba11e3363 | |||
| acf2be3f66 | |||
| f5df0195e2 | |||
| d26995b48d | |||
| 617f8120ae | |||
| 06c517c9aa | |||
| 593ed6408c | |||
| fb7d3ed122 | |||
| 19b88ec858 | |||
| 160e65be7f | |||
| 079b0d2cf6 | |||
| 78f1310f41 | |||
| eb1974c113 | |||
| 551409a023 | |||
| a11e14daac | |||
| 7441e15e76 | |||
| 46506228a6 | |||
| 17c49c74cf | |||
| 3f8024d4db | |||
| 8675e44d28 | |||
| 1301d9a718 | |||
| da3db50ca8 | |||
| 2ca6780305 | |||
| be68fe5a35 | |||
| dc41c0e302 | |||
| 51388c90d5 | |||
| f8c9d33df2 | |||
| ed0420fdce | |||
| 47a6f396fa | |||
| 9edb2aa0c1 | |||
| 003b647d83 | |||
| 2849ff8844 | |||
| 9f3089742a | |||
| b286a9bbcd | |||
| 6204e745ba | |||
| bbe0e48d23 | |||
| 79054080c0 | |||
| 81024c4c09 | |||
| 9068d96130 | |||
| 6f47543720 | |||
| 5a08384ae1 | |||
| 45d8fda0e4 | |||
| a9e664492f | |||
| 3c8b6cb2cc | |||
| 2b3abbe13b | |||
| 241a52b94a | |||
| 82c300b077 | |||
| 0f8b629e20 | |||
| 36f6ac65f2 | |||
| a25871a849 | |||
| 57766a9104 | |||
| 3770ae1645 | |||
| d73edb403f | |||
| b14196ff0d | |||
| 88903fee94 | |||
| 1e8311fc04 | |||
| 2b78370cb8 | |||
| 8f78bba9e9 | |||
| 41a4be678e | |||
| 8b2b4b7c3d | |||
| 1f3a29f89d | |||
| c4c93097cd | |||
| eedb7769e6 | |||
| 98db62a4bc | |||
| df48c8e500 | |||
| db9cc0f4c6 | |||
| a79ee429b4 | |||
| 6acfca6d48 | |||
| 1cd6f4cb72 | |||
| 3271e8c7d6 | |||
| 0bcde064af | |||
| 957980b3c5 | |||
| 03eb328d81 |
@@ -0,0 +1,74 @@
|
|||||||
|
# Animation System
|
||||||
|
|
||||||
|
Source: `src/dusk/animation/`
|
||||||
|
|
||||||
|
Lightweight keyframe-based value interpolation using fixed-point math throughout.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Easing (`animation/easing.h`)
|
||||||
|
|
||||||
|
`easingApply(type, t)` applies an easing function to a normalized time value `t ∈ [0, FIXED_ONE]` and returns the eased value in the same range.
|
||||||
|
|
||||||
|
All functions are also callable directly:
|
||||||
|
|
||||||
|
```c
|
||||||
|
fixed_t t = FIXED(0.5f);
|
||||||
|
fixed_t out = easingApply(EASING_IN_OUT_CUBIC, t);
|
||||||
|
```
|
||||||
|
|
||||||
|
Available easing types (all in `easingtype_t`):
|
||||||
|
|
||||||
|
| Enum value | Curve |
|
||||||
|
|---|---|
|
||||||
|
| `EASING_LINEAR` | straight line |
|
||||||
|
| `EASING_IN_SINE` / `OUT` / `IN_OUT` | sinusoidal |
|
||||||
|
| `EASING_IN_QUAD` / `OUT` / `IN_OUT` | quadratic |
|
||||||
|
| `EASING_IN_CUBIC` / `OUT` / `IN_OUT` | cubic |
|
||||||
|
| `EASING_IN_QUART` / `OUT` / `IN_OUT` | quartic |
|
||||||
|
| `EASING_IN_BACK` / `OUT` / `IN_OUT` | overshoots slightly |
|
||||||
|
|
||||||
|
`EASING_FUNCTIONS[EASING_COUNT]` is a table of `easingfn_t` function pointers for when you need to pick an easing at runtime without a switch.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Keyframes (`animation/keyframe.h`)
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct {
|
||||||
|
fixed_t time; // time point this keyframe is at
|
||||||
|
fixed_t value; // output value at this time
|
||||||
|
easingtype_t easing; // easing to apply when interpolating toward the NEXT keyframe
|
||||||
|
} keyframe_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
Keyframe arrays should be sorted ascending by `time`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Animation (`animation/animation.h`)
|
||||||
|
|
||||||
|
`animation_t` wraps a keyframe array and provides value lookup:
|
||||||
|
|
||||||
|
```c
|
||||||
|
keyframe_t frames[] = {
|
||||||
|
{ FIXED(0.0f), FIXED(0.0f), EASING_LINEAR },
|
||||||
|
{ FIXED(1.0f), FIXED(1.0f), EASING_IN_OUT_CUBIC },
|
||||||
|
{ FIXED(2.0f), FIXED(0.0f), EASING_LINEAR },
|
||||||
|
};
|
||||||
|
|
||||||
|
animation_t anim;
|
||||||
|
animationInit(&anim, frames, 3);
|
||||||
|
|
||||||
|
fixed_t value = animationGetValue(&anim, FIXED(0.75f)); // interpolated
|
||||||
|
```
|
||||||
|
|
||||||
|
`animationGetValue` finds the surrounding keyframes for the given `time`, computes the local `t` within that segment, applies the keyframe's easing, and linearly interpolates between the two keyframe values.
|
||||||
|
|
||||||
|
The animation does not own the keyframe array — it holds a pointer. Pass a static or long-lived array.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Usage in the engine
|
||||||
|
|
||||||
|
Entity animations (`entityanim_t`) do NOT use this system — they use a simple countdown timer (`animTime`) and a state enum. The `animation_t` system is intended for property animation: UI transitions, camera easing, visual effects, anything that needs a time → value curve.
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
# Architecture
|
||||||
|
|
||||||
|
## Platform abstraction
|
||||||
|
|
||||||
|
Every subsystem that differs across platforms (display, input, asset loading, save, time, network, log) follows the same pattern:
|
||||||
|
|
||||||
|
1. `src/dusk/<subsystem>/<subsystem>platform.h` — included by the public header. Contains `#include "path/to/platform-specific-header.h"` resolved by the build system include path.
|
||||||
|
2. `src/dusk{platform}/<subsystem>/<subsystem>platform.h` — the actual platform-specific header included above (e.g. `src/duskgl/display/framebuffer/framebufferplatform.h`).
|
||||||
|
3. The shared header (`src/dusk/<subsystem>/<subsystem>.h`) `#error`s at compile time if the platform doesn't define the expected macros/types.
|
||||||
|
|
||||||
|
The active platform backends are selected by `DUSK_TARGET_SYSTEM` in CMake, which includes `cmake/targets/<system>.cmake`. That file sets compile definitions (`DUSK_LINUX`, `DUSK_SDL2`, `DUSK_OPENGL`, …) and links platform libraries.
|
||||||
|
|
||||||
|
Platform source directories:
|
||||||
|
- `src/duskgl/` — OpenGL rendering (used on Linux and as the GL layer for SDL2)
|
||||||
|
- `src/dusksdl2/` — SDL2 window/input/time (Linux desktop)
|
||||||
|
- `src/dusklinux/` — Linux filesystem/save/network
|
||||||
|
- `src/duskdolphin/` — GameCube & Wii (GX renderer, libogc)
|
||||||
|
- `src/duskpsp/` — PSP (GU renderer, PSPSDK)
|
||||||
|
- `src/duskvita/` — PS Vita
|
||||||
|
|
||||||
|
## Subsystem lifecycle
|
||||||
|
|
||||||
|
All subsystems follow `init → update (per frame) → dispose`. Engine initialization order matters and is centralized in `engine.c`:
|
||||||
|
|
||||||
|
```
|
||||||
|
systemInit → timeInit → consoleInit → inputInit → assetInit →
|
||||||
|
localeManagerInit → displayInit → uiInit → uiTextboxInit →
|
||||||
|
cutsceneInit → rpgInit → networkInit → sceneInit
|
||||||
|
```
|
||||||
|
|
||||||
|
Dispose runs in reverse. Each call uses `errorChain()` to propagate failures.
|
||||||
|
|
||||||
|
## Error handling
|
||||||
|
|
||||||
|
Functions that can fail return `errorret_t` (a code + pointer to thread-local error state). Three core macros:
|
||||||
|
|
||||||
|
```c
|
||||||
|
errorThrow("message %s", arg); // sets error, returns from current function
|
||||||
|
errorChain(someCall()); // if someCall() fails, propagates and returns
|
||||||
|
errorOk(); // returns success
|
||||||
|
```
|
||||||
|
|
||||||
|
Check with `errorIsOk(ret)` / `errorIsNotOk(ret)`. The error state carries file/function/line info for a stack-like trace.
|
||||||
|
|
||||||
|
## Fixed-point math
|
||||||
|
|
||||||
|
`fixed_t` is `int32_t` with Q24.8 format (8 fractional bits, ~0.004 resolution). Use it for all world/game values:
|
||||||
|
|
||||||
|
```c
|
||||||
|
fixed_t x = FIXED(1.5); // compile-time literal
|
||||||
|
fixed_t y = fixedFromI32(3); // runtime conversion
|
||||||
|
fixed_t z = fixedMul(x, y); // arithmetic
|
||||||
|
float_t f = fixedToFloat(z); // only where float is needed (e.g. GL uniforms)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Code generation from CSV
|
||||||
|
|
||||||
|
Several subsystems define their data in CSV files and have corresponding Python tools that generate C headers at build time (via CMake `add_custom_command`):
|
||||||
|
|
||||||
|
| CSV | Tool | Output |
|
||||||
|
|-----|------|--------|
|
||||||
|
| `src/dusk/input/input.csv` | `tools/input/csv/` | input action enum + names |
|
||||||
|
| `src/dusk/display/color.csv` | `tools/color/csv/` | color constants |
|
||||||
|
| `src/dusk/rpg/item/item.csv` | `tools/item/csv/` | item enum + metadata |
|
||||||
|
| `src/dusk/rpg/story/storyflag.csv` | `tools/story/csv/` | story flag enum + initial values |
|
||||||
|
|
||||||
|
Generated headers are written to `build-<target>/generated/` and included via `target_include_directories`.
|
||||||
|
|
||||||
|
## Asset system
|
||||||
|
|
||||||
|
Assets are packed into `dusk.dsk` (a zip archive) at build time from the `assets/` directory. At runtime `asset.c` opens the archive and serves files from it.
|
||||||
|
|
||||||
|
Loading is asynchronous: `assetLock()` registers a load request; the background thread calls the appropriate loader; call `assetRequireLoaded()` to block until ready. `assetUnlock()` / `assetUnlockEntry()` releases the entry so it can be reclaimed.
|
||||||
|
|
||||||
|
Loaders are registered per type (`assetloadertype_t`) and live under `src/dusk/asset/loader/`. Platform-specific asset init (finding the .dsk file) is in `src/dusk{platform}/asset/`.
|
||||||
|
|
||||||
|
## Display subsystem
|
||||||
|
|
||||||
|
The display system is currently organized around immediate GPU-style rendering: `mesh_t` (vertex buffers), `shader_t` (GLSL on GL / TEV state on Dolphin), `texture_t`, and `framebuffer_t`. See [display-refactor.md](display-refactor.md) for the planned move to a render-queue model (needed for a future Saturn port).
|
||||||
|
|
||||||
|
The `spritebatch_t` (`display/spritebatch/`) accumulates 2D quads and flushes in batches — the primary 2D drawing primitive used by the RPG layer.
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
# Asset System
|
||||||
|
|
||||||
|
Source: `src/dusk/asset/`
|
||||||
|
|
||||||
|
All game assets are packed into `dusk.dsk` (a zip archive) at build time and served from it at runtime. The asset system manages async loading, reference counting, and platform-specific archive location.
|
||||||
|
|
||||||
|
See [architecture.md](architecture.md#asset-system) for the high-level overview.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Asset archive
|
||||||
|
|
||||||
|
The archive is opened at `assetInit()`. `assetFileExists(filename)` checks for a file without loading it. The file path format inside the archive matches the layout of the `assets/` source directory.
|
||||||
|
|
||||||
|
On each platform, `assetInitPlatform()` locates the `.dsk` file (e.g. adjacent to the binary on Linux, on the SD card on PSP).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Entry lifecycle
|
||||||
|
|
||||||
|
An `assetentry_t` represents one file being managed by the system. States:
|
||||||
|
|
||||||
|
```
|
||||||
|
NOT_STARTED → PENDING_ASYNC → LOADING_ASYNC → PENDING_SYNC → LOADING_SYNC → LOADED
|
||||||
|
└→ ERROR
|
||||||
|
```
|
||||||
|
|
||||||
|
- **PENDING_ASYNC / LOADING_ASYNC**: the background thread is handling I/O (file reads, decompression).
|
||||||
|
- **PENDING_SYNC / LOADING_SYNC**: the main thread needs to finish loading (e.g. uploading to GPU), triggered during `assetUpdate()`.
|
||||||
|
|
||||||
|
The async/sync split exists because GPU operations must happen on the main thread.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Using assets
|
||||||
|
|
||||||
|
```c
|
||||||
|
// Acquire a loaded entry (blocks until loaded):
|
||||||
|
assetentry_t *entry = assetLock(filename, ASSET_LOADER_TYPE_TEXTURE, &input);
|
||||||
|
errorChain(assetRequireLoaded(entry));
|
||||||
|
|
||||||
|
// Use the loaded data:
|
||||||
|
texture_t *tex = &entry->data.texture.texture;
|
||||||
|
|
||||||
|
// Release when done:
|
||||||
|
assetUnlockEntry(entry);
|
||||||
|
```
|
||||||
|
|
||||||
|
`assetLock` finds-or-creates an entry and increments its reference count. `assetUnlock` / `assetUnlockEntry` decrements it; when it reaches zero the entry is reclaimed at the next `assetUpdate()`.
|
||||||
|
|
||||||
|
To subscribe to async completion instead of blocking:
|
||||||
|
```c
|
||||||
|
eventSubscribe(&entry->onLoaded, myCallback, myUser);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Loader types
|
||||||
|
|
||||||
|
| Type constant | File | Output struct accessed via |
|
||||||
|
|---|---|---|
|
||||||
|
| `ASSET_LOADER_TYPE_TEXTURE` | `.png` etc. | `entry->data.texture.texture` |
|
||||||
|
| `ASSET_LOADER_TYPE_TILESET` | tileset descriptor | `entry->data.tileset.tileset` |
|
||||||
|
| `ASSET_LOADER_TYPE_MESH` | mesh data | `entry->data.mesh.mesh` |
|
||||||
|
| `ASSET_LOADER_TYPE_LOCALE` | `.po` file | internal to locale manager |
|
||||||
|
| `ASSET_LOADER_TYPE_JSON` | `.json` | `entry->data.json.*` |
|
||||||
|
|
||||||
|
Each loader type registers `loadAsync`, `loadSync`, and `dispose` callbacks in `ASSET_LOADER_CALLBACKS[]`.
|
||||||
|
|
||||||
|
The async callback runs on the loader thread; the sync callback runs on the main thread during `assetUpdate()`. Most loaders do file I/O async and GPU upload sync.
|
||||||
|
|
||||||
|
### Error handling inside loaders
|
||||||
|
|
||||||
|
Use these macros instead of `errorThrow` / `errorChain` inside loader callbacks — they also set the entry state to ERROR:
|
||||||
|
|
||||||
|
```c
|
||||||
|
assetLoaderErrorChain(loading, someCall());
|
||||||
|
assetLoaderErrorThrow(loading, "Descriptive message");
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Low-level file I/O (`asset/assetfile.h`)
|
||||||
|
|
||||||
|
`assetfile_t` wraps a `zip_file_t` handle and provides streaming reads:
|
||||||
|
|
||||||
|
```c
|
||||||
|
assetFileInit(&file, "textures/player.png", NULL, NULL);
|
||||||
|
assetFileOpen(&file);
|
||||||
|
assetFileRead(&file, buffer, size);
|
||||||
|
assetFileClose(&file);
|
||||||
|
assetFileDispose(&file);
|
||||||
|
|
||||||
|
// Read entire file into a malloc'd buffer:
|
||||||
|
uint8_t *buf; size_t size;
|
||||||
|
assetFileReadEntire(&file, &buf, &size); // caller frees buf
|
||||||
|
```
|
||||||
|
|
||||||
|
For line-by-line text parsing (`assetfilelinereader_t`):
|
||||||
|
```c
|
||||||
|
assetFileLineReaderInit(&reader, &file, readBuf, readBufSize, outBuf, outBufSize);
|
||||||
|
while(!reader.eof) {
|
||||||
|
errorChain(assetFileLineReaderNext(&reader));
|
||||||
|
// reader.outBuffer contains the line, reader.lineLength its length
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Background loader thread
|
||||||
|
|
||||||
|
`assetUpdateAsync(thread)` is the thread entry point. It calls `assetUpdate()` in a loop, sleeping briefly between iterations, until `threadShouldStop()` returns true. The main thread also calls `assetUpdate()` once per frame to process the sync phase.
|
||||||
|
|
||||||
|
Up to `ASSET_LOADING_COUNT_MAX` (4) entries can be loading concurrently.
|
||||||
|
Up to `ASSET_ENTRY_COUNT_MAX` (128) entries can exist at once.
|
||||||
@@ -0,0 +1,714 @@
|
|||||||
|
# Display Layer Refactor
|
||||||
|
|
||||||
|
## Vision
|
||||||
|
|
||||||
|
The goal is to remove the implicit assumption that all platforms render
|
||||||
|
through a GL-like API, and replace it with a system where each platform
|
||||||
|
owns its rendering stack completely. The scene describes *what* to draw
|
||||||
|
in platform-neutral terms; the platform decides *how* to draw it.
|
||||||
|
|
||||||
|
This unlocks:
|
||||||
|
- Saturn (VDP1/VDP2 command-list, no Z-buffer, affine-only)
|
||||||
|
- PlayStation 1 (ordering table, affine textures, GTE fixed-point, CMake SDK)
|
||||||
|
- Nintendo 64 (RSP display list, hardware Z-buffer, perspective-correct,
|
||||||
|
real FPU -- closer to modern GL than to Saturn)
|
||||||
|
- SNES (PPU tile engine, Mode7 for overworld, no real 3D)
|
||||||
|
- Vulkan (explicit, modern, no legacy GL baggage)
|
||||||
|
- Native PSP GU (drop PSPGL which is just a compatibility shim)
|
||||||
|
- Legacy fixed-function GL as its own standalone target
|
||||||
|
- A real first-class 2D UI system not bolted onto 3D space
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Why
|
||||||
|
|
||||||
|
### The current abstraction assumes GPU-style rendering
|
||||||
|
|
||||||
|
The current display layer was designed around a GL-like mental model:
|
||||||
|
vertex buffers, shaders, Z-buffered triangle rasterization, and texture
|
||||||
|
objects. `duskgl` implements this with real OpenGL. `duskdolphin` does its
|
||||||
|
own GX thing but still matches the same interface (mesh, shader, texture,
|
||||||
|
framebuffer). PSP uses PSPGL -- a library that *emulates* GL on top of
|
||||||
|
the PSP's native GE/GU hardware, which is entirely different underneath.
|
||||||
|
|
||||||
|
Problems this creates:
|
||||||
|
|
||||||
|
**PSPGL is a lie.** The PSP has a native graphics engine (GE/GU) with its
|
||||||
|
own command list, its own vertex formats, and its own display list model.
|
||||||
|
PSPGL translates GL calls into GU calls, but imperfectly -- and we end up
|
||||||
|
paying the abstraction cost without getting GL correctness. Writing directly
|
||||||
|
to GU gives better performance, access to native formats, and correct
|
||||||
|
behavior on edge cases that PSPGL gets wrong.
|
||||||
|
|
||||||
|
**Legacy GL should not share code with modern GL.** The fixed-function
|
||||||
|
pipeline (no shaders, matrix stacks via glMatrixMode, glTexEnv) is
|
||||||
|
meaningfully different from modern GL (VAO/VBO, GLSL, explicit uniform
|
||||||
|
locations). Treating them as "the same thing with a flag" creates a tangle
|
||||||
|
of `#ifdef DUSK_OPENGL_LEGACY` guards throughout the rendering code.
|
||||||
|
They are separate targets and should be separate platform directories.
|
||||||
|
|
||||||
|
**Saturn cannot fit the model at all.** VDP1 is a command-list processor:
|
||||||
|
you write 32-byte command structs (sprites, quads, lines) into VRAM, then
|
||||||
|
poke a register to trigger execution. There are no vertex buffers, no
|
||||||
|
shaders, no Z-buffer. Depth is pure painter's algorithm -- command order
|
||||||
|
IS the depth. VDP2 composites up to 6 background planes at scanline time;
|
||||||
|
these are tile maps and rotation parameter tables, not meshes. Nothing
|
||||||
|
about the current API maps onto this hardware.
|
||||||
|
|
||||||
|
**SNES is even further removed.** The PPU renders tiles. VRAM holds 8x8
|
||||||
|
or 16x16 pixel tiles and tile maps; the PPU references these during
|
||||||
|
scanline rendering. There are no draw calls. Mode7 is an affine transform
|
||||||
|
applied to a single background layer (the basis for the overworld map and
|
||||||
|
road perspective effects). Sprites are entries in OAM (Object Attribute
|
||||||
|
Memory). The 65816 CPU writes to memory-mapped registers and VRAM; the
|
||||||
|
PPU does the rest. The concept of "mesh" or "shader" is meaningless here.
|
||||||
|
|
||||||
|
**Textures loaded as RGBA waste memory and exclude platforms.** Loading
|
||||||
|
every texture as 32-bit RGBA and converting at runtime is expensive on
|
||||||
|
memory-constrained platforms (Saturn has ~1 MB total RAM; SNES has 64 KB
|
||||||
|
VRAM) and simply wrong for platforms that have native formats incompatible
|
||||||
|
with RGBA (e.g., PSP's ABGR8888 / BGR5650, Saturn's RGB555 / CI4 / CI8,
|
||||||
|
SNES's 2bpp/4bpp/8bpp indexed). The asset pipeline must compile textures
|
||||||
|
to platform-native formats at build time.
|
||||||
|
|
||||||
|
**UI in 3D space is wasteful and limiting.** Currently UI elements are
|
||||||
|
rendered as geometry projected into screen space, going through the full
|
||||||
|
3D pipeline. On platforms with dedicated 2D hardware (Saturn VDP2,
|
||||||
|
SNES BG layers), this is actively wrong -- UI should map to a hardware
|
||||||
|
plane, not a 3D draw call. On modern platforms it should be a clean
|
||||||
|
screen-space pass that never touches the 3D depth buffer.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Current Model (Summary)
|
||||||
|
|
||||||
|
```
|
||||||
|
Scene
|
||||||
|
-> shaderBind(shader)
|
||||||
|
-> textureBind(texture)
|
||||||
|
-> meshDraw(mesh) <-- immediate draw call per object
|
||||||
|
-> meshDraw(mesh)
|
||||||
|
-> ...
|
||||||
|
Platform receives each draw call immediately.
|
||||||
|
Depth is handled by Z-buffer hardware.
|
||||||
|
All textures live in GPU memory as RGBA (or Dolphin's tiled RGBA).
|
||||||
|
UI is rendered as 3D geometry with an orthographic projection.
|
||||||
|
```
|
||||||
|
|
||||||
|
Key current concepts:
|
||||||
|
- `mesh_t` -- vertex array (triangles/quads), in GPU VBO (GL) or CPU
|
||||||
|
memory (Dolphin)
|
||||||
|
- `shader_t` -- GLSL program (modern GL), GL fixed-function state
|
||||||
|
(legacy GL), or GX matrix + TEV config (Dolphin)
|
||||||
|
- `texture_t` -- GPU texture handle (GL) or tiled CPU buffer (Dolphin);
|
||||||
|
always RGBA at the engine level
|
||||||
|
- `framebuffer_t` -- FBO (GL) or fixed hardware XFB (Dolphin)
|
||||||
|
- `spritebatch_t` -- accumulates 2D quads and flushes in batches of 32;
|
||||||
|
the only existing deferred-submission system in the engine
|
||||||
|
|
||||||
|
The spritebatch hints at the right model. Everything needs to work this way.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## The Core Shift: Platform-Native Rendering
|
||||||
|
|
||||||
|
### Before
|
||||||
|
|
||||||
|
```
|
||||||
|
src/dusk/ Core engine + GL-like rendering API definition
|
||||||
|
src/duskgl/ OpenGL implementation
|
||||||
|
src/dusksdl2/ SDL2 window/input (shared)
|
||||||
|
src/duskpsp/ PSP via PSPGL (shim over GU)
|
||||||
|
src/duskvita/ Vita via GL ES (similar path to duskgl)
|
||||||
|
src/duskdolphin/ GameCube/Wii via GX (already custom)
|
||||||
|
src/dusklinux/ Linux (uses dusksdl2 + duskgl)
|
||||||
|
```
|
||||||
|
|
||||||
|
### After
|
||||||
|
|
||||||
|
```
|
||||||
|
src/dusk/ Core engine logic + render intent API ONLY
|
||||||
|
src/dusksdl2/ SDL2 window/input (unchanged)
|
||||||
|
src/duskgl/ Modern OpenGL (Linux, Vita modern path)
|
||||||
|
src/duskgllegacy/ Fixed-function OpenGL (older hardware, PSP with PSPGL
|
||||||
|
as a last resort)
|
||||||
|
src/duskvulkan/ Vulkan (Linux modern, future)
|
||||||
|
src/duskpsp/ PSP native GU (no PSPGL, direct command lists)
|
||||||
|
src/duskvita/ Vita native GXM (TBD)
|
||||||
|
src/duskdolphin/ GameCube/Wii GX (already custom, mostly kept)
|
||||||
|
src/dusksaturn/ Saturn VDP1/VDP2 (new)
|
||||||
|
src/duskps1/ PlayStation 1 ordering table + GTE (new)
|
||||||
|
src/duskn64/ Nintendo 64 RSP/RDP display list (new)
|
||||||
|
src/dusksnes/ SNES PPU/Mode7 (new, extremely constrained)
|
||||||
|
```
|
||||||
|
|
||||||
|
`src/dusk/` no longer knows about meshes, shaders, or framebuffers.
|
||||||
|
It defines the *render intent* system: what the scene wants to draw.
|
||||||
|
Each platform directory is entirely self-contained and responsible for
|
||||||
|
translating intents to its native API.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Render Intent System (new)
|
||||||
|
|
||||||
|
Instead of the scene calling `meshDraw()` or `shaderBind()`, it submits
|
||||||
|
render intents into a `renderqueue_t`. An intent describes what should
|
||||||
|
appear on screen without prescribing how to draw it.
|
||||||
|
|
||||||
|
### Primitive intents (3D world)
|
||||||
|
|
||||||
|
```
|
||||||
|
RENDER_INTENT_QUAD -- textured quad, 4 vertices or transform + size
|
||||||
|
RENDER_INTENT_POLYGON -- filled polygon (convex, up to N vertices)
|
||||||
|
RENDER_INTENT_LINE -- line segment or polyline
|
||||||
|
RENDER_INTENT_SPRITE -- 2D billboard (always faces camera)
|
||||||
|
RENDER_INTENT_MESH -- arbitrary vertex array (GL/GX only; degraded
|
||||||
|
on command-list platforms)
|
||||||
|
```
|
||||||
|
|
||||||
|
Each intent carries: texture reference, color/tint, depth hint (for
|
||||||
|
painter's algorithm sorting), blend mode, and cull flags.
|
||||||
|
|
||||||
|
### Background plane intents (2D layers)
|
||||||
|
|
||||||
|
```
|
||||||
|
RENDER_INTENT_BGPLANE -- configure a background/tilemap layer
|
||||||
|
```
|
||||||
|
|
||||||
|
Carries: layer index, tile map data reference, scroll offset, palette,
|
||||||
|
and transform (for Mode7-style affine).
|
||||||
|
|
||||||
|
### UI intents (screen space)
|
||||||
|
|
||||||
|
```
|
||||||
|
RENDER_INTENT_UI_RECT -- solid colored rectangle
|
||||||
|
RENDER_INTENT_UI_SPRITE -- textured rectangle (UI image)
|
||||||
|
RENDER_INTENT_UI_TEXT -- text string at screen position
|
||||||
|
```
|
||||||
|
|
||||||
|
UI intents are always screen-space. They are never mixed into the 3D
|
||||||
|
world queue. See UI System section below.
|
||||||
|
|
||||||
|
### Platform translation
|
||||||
|
|
||||||
|
| Intent | Modern GL | PSP GU | Saturn VDP1 | PS1 OT | N64 RSP | SNES PPU |
|
||||||
|
|---|---|---|---|---|---|---|
|
||||||
|
| QUAD | VAO + glDraw | GU display list | distorted-sprite cmd | GPU quad packet | RSP display list | OAM + BG tile |
|
||||||
|
| POLYGON | VAO + glDraw | GU display list | polygon cmd | GPU poly packet | RSP display list | OAM |
|
||||||
|
| BGPLANE | fullscreen quad | fullscreen quad | VDP2 config | fullscreen quad | fullscreen quad | BG layer config |
|
||||||
|
| UI_SPRITE | 2D ortho quad | 2D GU quad | VDP2 BG plane | GPU rect packet | RDP rectangle | BG layer tile |
|
||||||
|
| MESH | VAO/VBO | GU buffers | (degrade: quads) | (degrade: tris/quads) | RSP display list | (not supported) |
|
||||||
|
|
||||||
|
Note: N64 supports both triangles and axis-aligned rectangles natively via
|
||||||
|
RDP. PS1 supports triangles and quads (4-vertex) natively, so neither needs
|
||||||
|
the dead-vertex trick that Saturn requires.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Asset Pipeline: Platform-Native Formats
|
||||||
|
|
||||||
|
### The problem
|
||||||
|
|
||||||
|
All textures currently enter the engine as RGBA and are converted at
|
||||||
|
runtime by each platform (Dolphin retiles to 4x4 blocks; GL uploads as-is).
|
||||||
|
This wastes memory and CPU time, and is impossible for platforms where RGBA
|
||||||
|
is not a valid intermediate format at all.
|
||||||
|
|
||||||
|
### The solution
|
||||||
|
|
||||||
|
The asset compiler (offline, run at build time) produces platform-specific
|
||||||
|
binary bundles. A texture asset has one source (PNG or similar) but N
|
||||||
|
compiled outputs, one per target.
|
||||||
|
|
||||||
|
### Texture formats by platform
|
||||||
|
|
||||||
|
| Platform | Native Formats | Notes |
|
||||||
|
|---|---|---|
|
||||||
|
| Modern GL | RGBA8, RGB8, BC1-BC7 (compressed) | Upload directly, GPU handles |
|
||||||
|
| Legacy GL | RGBA8, RGB8, CI8 (palette via extension) | No compressed formats |
|
||||||
|
| Vulkan | VkFormat variants (RGBA8, BC, ASTC) | Chosen at compile time |
|
||||||
|
| PSP GU | ABGR8888, BGR5650, ABGR1555, ABGR4444, CI4, CI8 | Native swizzled format |
|
||||||
|
| Saturn VDP1/VDP2 | RGB555, CI4, CI8 (15-bit palette in CRAM) | Big-endian, packed |
|
||||||
|
| PlayStation 1 | RGB555 / CI4 / CI8 (CLUT in VRAM) | Little-endian; VRAM flat; CLUT at coord |
|
||||||
|
| Nintendo 64 | RGBA16, RGBA32, IA4-IA16, I4-I8, CI4, CI8 | 4 KB TMEM; tiles must fit in TMEM banks |
|
||||||
|
| GameCube/Wii GX | I4, I8, IA4, IA8, RGB565, RGB5A3, RGBA8, CMPR | 4x4 tiled, big-endian |
|
||||||
|
| SNES PPU | 2bpp, 4bpp, 8bpp indexed (CGRAM palette) | Tile-packed, no direct access |
|
||||||
|
|
||||||
|
### Asset bundle structure
|
||||||
|
|
||||||
|
The `.dsk` bundle gains a platform tag. The loader picks the right section
|
||||||
|
at runtime (or the build produces a single-platform bundle for constrained
|
||||||
|
targets like SNES/Saturn where there is no spare storage for unused data).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## UI System (first-class)
|
||||||
|
|
||||||
|
### Current problem
|
||||||
|
|
||||||
|
UI elements go through the 3D pipeline: they are meshes with an orthographic
|
||||||
|
shader, rendered in the same pass as the world. This means:
|
||||||
|
- UI competes for Z-buffer depth with world geometry
|
||||||
|
- On Saturn/SNES, UI cannot use dedicated hardware planes
|
||||||
|
- Text rendering is tied to the sprite batch which is tied to the 3D pass
|
||||||
|
- No separation between "draw the world" and "draw the HUD"
|
||||||
|
|
||||||
|
### New model
|
||||||
|
|
||||||
|
UI is a completely separate rendering context. The world renders first,
|
||||||
|
then the UI renders on top. They share no state.
|
||||||
|
|
||||||
|
UI coordinates are always in screen space (pixels or a logical resolution
|
||||||
|
that the platform scales to its native display size). No camera matrix,
|
||||||
|
no projection, no depth buffer involvement.
|
||||||
|
|
||||||
|
### Platform mapping
|
||||||
|
|
||||||
|
| Platform | UI implementation |
|
||||||
|
|---|---|
|
||||||
|
| Modern GL | Separate 2D ortho pass, screen-space quads, no depth test |
|
||||||
|
| Legacy GL | Same, using fixed-function |
|
||||||
|
| PSP GU | Separate GU display list, 2D mode |
|
||||||
|
| Saturn | VDP2 background plane(s) dedicated to UI |
|
||||||
|
| PlayStation 1 | Separate GPU packet chain, no Z; ordered after world OT |
|
||||||
|
| Nintendo 64 | RDP rectangle commands in a separate display list segment |
|
||||||
|
| GameCube/Wii | GX 2D mode or dedicated GX pass |
|
||||||
|
| SNES | Dedicated BG layer(s) for HUD tiles |
|
||||||
|
|
||||||
|
On Saturn, the UI occupying VDP2 planes is a genuine hardware win -- the
|
||||||
|
PPU composites it for free at scanline time, costing zero VDP1 commands.
|
||||||
|
On SNES, the HUD must live in a BG layer because there is no alternative.
|
||||||
|
|
||||||
|
### UI API (proposed)
|
||||||
|
|
||||||
|
```c
|
||||||
|
uiBegin();
|
||||||
|
uiDrawRect(x, y, w, h, color);
|
||||||
|
uiDrawSprite(x, y, w, h, texture, uvMin, uvMax);
|
||||||
|
uiDrawText(x, y, font, string);
|
||||||
|
uiEnd(); // platform flushes UI to hardware
|
||||||
|
```
|
||||||
|
|
||||||
|
The `uiBegin`/`uiEnd` block collects intents; the platform submits them
|
||||||
|
at frame end in whatever way is appropriate.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## SNES / Mode7
|
||||||
|
|
||||||
|
SNES is the most constrained platform the engine will ever support and
|
||||||
|
needs its own section because it breaks assumptions that even Saturn keeps.
|
||||||
|
|
||||||
|
### Hardware
|
||||||
|
|
||||||
|
- **CPU**: 65816 @ ~3.58 MHz (16-bit, no FPU, no cache)
|
||||||
|
- **PPU**: Tile-based scanline renderer. VRAM holds tile graphics and
|
||||||
|
tile maps. BG layers reference tiles by index.
|
||||||
|
- **Mode7**: A single BG layer with a 2D affine matrix applied per
|
||||||
|
scanline. Used for overworld maps, road perspective (F-Zero), rotation
|
||||||
|
effects. The matrix is set via HDMA (scanline DMA) for per-scanline
|
||||||
|
variation, enabling horizon-perspective effects.
|
||||||
|
- **Sprites/OAM**: Up to 128 sprites (8x8, 16x16, 32x32, 64x64 pixels),
|
||||||
|
4bpp indexed, up to 8 per scanline.
|
||||||
|
- **Palette**: CGRAM holds 256 entries of 15-bit RGB (512 bytes total).
|
||||||
|
BG layers use sub-palettes of 4/16/256 colors depending on bit depth.
|
||||||
|
- **VRAM**: 64 KB (tiles + tile maps)
|
||||||
|
- **WRAM**: 128 KB work RAM + usually 8 KB SRAM on cart for saves
|
||||||
|
- **No frame buffer.** The PPU renders scanlines directly. You cannot
|
||||||
|
read back what was drawn.
|
||||||
|
- **No general-purpose draw calls.** You configure registers and VRAM
|
||||||
|
before the frame and the PPU does the rest.
|
||||||
|
|
||||||
|
### What "3D" means on SNES
|
||||||
|
|
||||||
|
True 3D is not possible. What can be approximated:
|
||||||
|
- **Overworld map**: Mode7 with a flat texture and HDMA scroll gives a
|
||||||
|
top-down perspective with a horizon line (the classic JRPG overworld).
|
||||||
|
- **Depth illusion**: Mode7 matrix manipulation can simulate a moving
|
||||||
|
camera over flat terrain. Objects are sprites placed at screen positions
|
||||||
|
calculated by software perspective projection.
|
||||||
|
- **Sprite scaling**: Software-scaled sprites using pre-rendered frames
|
||||||
|
or the RSP-style tricks used in Super FX games (Star Fox). Super FX
|
||||||
|
is a co-processor on the cartridge -- base SNES cannot do this.
|
||||||
|
- **Basic 3D effects**: Some games use HDMA color gradient + Mode7 floor
|
||||||
|
with overlaid sprites to create a pseudo-3D look.
|
||||||
|
|
||||||
|
The engine plan for SNES: Mode7 overworld (confirmed), sprite-based world
|
||||||
|
objects, BG layer UI. "Basic 3D effects" (pseudo-perspective with sprites)
|
||||||
|
is aspirational -- implementation complexity TBD.
|
||||||
|
|
||||||
|
### SNES constraints on the engine
|
||||||
|
|
||||||
|
- **No dynamic allocation.** With 128 KB WRAM, a general-purpose allocator
|
||||||
|
is risky. The engine memory system may need a static pool mode for SNES.
|
||||||
|
- **No floating point.** `float_t` must resolve to integer or fixed-point.
|
||||||
|
- **No scripting (JerryScript).** The JS engine requires far more than
|
||||||
|
128 KB RAM. SNES scenes must be compiled C.
|
||||||
|
- **Asset data in ROM, not a .dsk bundle.** SNES loads from cartridge ROM
|
||||||
|
mapped into the address space. The asset system needs a ROM-mapped loader.
|
||||||
|
- **Tile pipeline.** Textures must be pre-converted to SNES tile format
|
||||||
|
(2bpp/4bpp/8bpp, 8x8 pixel tiles, CGRAM palette) at build time. This
|
||||||
|
is a completely different asset output from every other platform.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Platform Inventory
|
||||||
|
|
||||||
|
A summary of what each platform's native rendering looks like after the
|
||||||
|
refactor, for reference when designing the intent API.
|
||||||
|
|
||||||
|
### Modern OpenGL (duskgl)
|
||||||
|
|
||||||
|
VAO + VBO mesh storage, GLSL shaders, FBO render targets, Z-buffer.
|
||||||
|
No fixed-function. Targets: Linux, possibly Vita (GXM is preferred).
|
||||||
|
|
||||||
|
### Legacy OpenGL (duskgllegacy)
|
||||||
|
|
||||||
|
Fixed-function pipeline: `glMatrixMode`, `glTexEnv`, client-side vertex
|
||||||
|
arrays. No VAO/VBO. Used for: very old desktop hardware, maybe PSP as
|
||||||
|
last resort (PSPGL is this). Targets: legacy desktop, embedded Linux.
|
||||||
|
|
||||||
|
### Vulkan (duskvulkan)
|
||||||
|
|
||||||
|
Explicit pipeline state objects, render passes, descriptor sets, command
|
||||||
|
buffers. Highest ceiling for performance and control. Targets: Linux
|
||||||
|
(modern), future platforms. Not immediate priority but the architecture
|
||||||
|
should not block it.
|
||||||
|
|
||||||
|
### PSP native GU (duskpsp)
|
||||||
|
|
||||||
|
The GE/GU is a display-list GPU. You build a command list in memory and
|
||||||
|
the GU DMA engine processes it asynchronously. Native vertex formats are
|
||||||
|
PSP-specific (ABGR byte order, swizzled textures for cache efficiency).
|
||||||
|
No PSPGL. Targets: PSP hardware and emulators.
|
||||||
|
|
||||||
|
### Vita (duskvita)
|
||||||
|
|
||||||
|
GXM is Sony's Vita GPU API -- closer to modern GL than GU, with explicit
|
||||||
|
shader binaries (.gxp), ring buffers, and GPU sync primitives.
|
||||||
|
|
||||||
|
### GameCube/Wii GX (duskdolphin)
|
||||||
|
|
||||||
|
Already a custom renderer. GX uses immediate-mode vertex submission
|
||||||
|
(`GX_Begin` / `GX_Position1x16` loops), TEV for texture compositing, and
|
||||||
|
hardware XFB double-buffering. Big-endian. Mostly kept as-is; may benefit
|
||||||
|
from being expressed in terms of render intents for consistency.
|
||||||
|
|
||||||
|
### Saturn VDP1/VDP2 (dusksaturn)
|
||||||
|
|
||||||
|
VDP1: command-list (32-byte structs), quad-based, affine texture mapping,
|
||||||
|
no Z-buffer (painter's algorithm). VDP2: up to 6 background planes
|
||||||
|
composited at scanline time. Big-endian dual SH-2, no FPU. Fixed-point
|
||||||
|
math required throughout.
|
||||||
|
|
||||||
|
### PlayStation 1 (duskps1)
|
||||||
|
|
||||||
|
MIPS R3000A @ 33.87 MHz, little-endian, no FPU. GTE (coprocessor 2)
|
||||||
|
handles fixed-point matrix math, perspective divide, and lighting.
|
||||||
|
GPU receives packets via DMA linked-list (the Ordering Table). Primitives:
|
||||||
|
triangles and quads natively (no dead-vertex needed). Texture mapping:
|
||||||
|
affine, same limitation as Saturn. No Z-buffer; depth is OT slot order.
|
||||||
|
VRAM is 1 MB flat (frame buffers + textures + CLUTs share it). SDK:
|
||||||
|
PSn00bSDK, which is CMake-native -- a direct fit for the dusk build system.
|
||||||
|
|
||||||
|
### Nintendo 64 (duskn64)
|
||||||
|
|
||||||
|
VR4300 @ 93.75 MHz, big-endian, real IEEE 754 FPU. Rendering is split
|
||||||
|
between the RSP (geometry: programmable MIPS SIMD, runs microcode up to
|
||||||
|
~1000 instructions in 4 KB IMEM) and the RDP (rasterization: fixed
|
||||||
|
hardware). RSP produces triangle commands from a CPU-built display list
|
||||||
|
in RDRAM. RDP features: perspective-correct texture mapping, bilinear
|
||||||
|
filtering, hardware Z-buffer. Primitives: triangles and axis-aligned rects.
|
||||||
|
TMEM is 4 KB on-chip texture cache; textures must be loaded into tiles
|
||||||
|
before drawing -- a significant memory management constraint.
|
||||||
|
SDK: libdragon (Unlicense, GCC 14, Makefile-based -- not CMake; this
|
||||||
|
requires a wrapper toolchain file for dusk's build system).
|
||||||
|
|
||||||
|
### SNES PPU/Mode7 (dusksnes)
|
||||||
|
|
||||||
|
Tile-based. VRAM holds tiles and tile maps. Mode7 provides affine transform
|
||||||
|
for one BG layer. Sprites via OAM. No frame buffer. All configuration is
|
||||||
|
memory-mapped registers. 65816 CPU, no FPU, extremely limited RAM.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Threading Model
|
||||||
|
|
||||||
|
### Current model
|
||||||
|
|
||||||
|
The engine uses OS threads for async asset loading (`assetXxxLoaderAsync`).
|
||||||
|
Platforms that have pthreads or an equivalent RTOS (Linux, PSP, Vita) run
|
||||||
|
worker threads that load data in the background while the game loop runs.
|
||||||
|
The main thread polls or blocks on completion.
|
||||||
|
|
||||||
|
### The problem
|
||||||
|
|
||||||
|
Several target platforms have no OS threading whatsoever, and others have
|
||||||
|
hardware-specific async mechanisms that are nothing like pthreads.
|
||||||
|
|
||||||
|
### Per-platform reality
|
||||||
|
|
||||||
|
| Platform | Threading | Async mechanism |
|
||||||
|
|---|---|---|
|
||||||
|
| Linux | pthreads | Worker threads (current) |
|
||||||
|
| Vita | SceKernelThread | Per-SDK threads |
|
||||||
|
| PSP | SceKernelThread | Per-SDK threads |
|
||||||
|
| GameCube/Wii | libogc LWP | Lightweight processes |
|
||||||
|
| Saturn | None (OS) | Slave SH-2 for fixed jobs; CD-ROM via interrupt/callback |
|
||||||
|
| PlayStation 1 | None (OS) | V-blank ISR, 7 DMA channels, CD-ROM callbacks |
|
||||||
|
| Nintendo 64 | libdragon preview only | PI DMA for cartridge; RSP for parallel compute |
|
||||||
|
| SNES | None | DMA (GPDMA/HDMA); NMI V-blank; SPC700 audio is a separate CPU |
|
||||||
|
|
||||||
|
**Saturn slave SH-2**: The second SH-2 is not a general-purpose thread.
|
||||||
|
It runs a fixed subroutine you hand-load. The typical use is offloading
|
||||||
|
heavy per-frame computation (geometry transforms, depth sort) while the
|
||||||
|
master SH-2 handles game logic. Communication is via shared WRAM with
|
||||||
|
cache-through addresses to avoid coherency bugs. There is no scheduler
|
||||||
|
and no yield -- it runs to completion.
|
||||||
|
|
||||||
|
**SNES DMA**: GPDMA copies blocks of data (ROM to WRAM, WRAM to VRAM)
|
||||||
|
and halts the CPU for the duration -- it is synchronous from the game's
|
||||||
|
perspective. HDMA runs per-scanline during H-blank, writing to PPU
|
||||||
|
registers without CPU involvement; this is how Mode7 perspective is
|
||||||
|
achieved. Neither is "async" in the programming sense.
|
||||||
|
|
||||||
|
**SNES NMI**: The V-blank NMI fires at the start of every V-blank period.
|
||||||
|
This is the only safe window to write to VRAM and PPU registers. All
|
||||||
|
critical PPU updates must complete within ~1.2ms (the V-blank window).
|
||||||
|
|
||||||
|
### Proposed model
|
||||||
|
|
||||||
|
Introduce a compile-time threading capability flag:
|
||||||
|
|
||||||
|
```
|
||||||
|
DUSK_THREAD_PTHREAD -- Linux, maybe Vita
|
||||||
|
DUSK_THREAD_SCEKERNEL -- PSP, Vita SDK
|
||||||
|
DUSK_THREAD_LWP -- GameCube/Wii libogc
|
||||||
|
DUSK_THREAD_SLAVE_SH2 -- Saturn slave CPU (job dispatch only)
|
||||||
|
DUSK_THREAD_NONE -- SNES (and Saturn master thread view)
|
||||||
|
```
|
||||||
|
|
||||||
|
The asset loader's async path is gated on having a threading capability.
|
||||||
|
When `DUSK_THREAD_NONE` is defined, `assetXxxLoaderAsync` either does not
|
||||||
|
exist or is an alias for the synchronous version. On Saturn, the slave SH-2
|
||||||
|
is exposed as a distinct API (`sh2JobDispatch`, `sh2JobWait`) used only for
|
||||||
|
compute-heavy work, not for I/O.
|
||||||
|
|
||||||
|
### Asset loading without threads
|
||||||
|
|
||||||
|
**Saturn**: CD-ROM access is initiated via SBL/CDC routines and completes
|
||||||
|
via interrupt callback. The engine's asset loading loop can poll the
|
||||||
|
callback flag in the main loop rather than blocking a thread. This is
|
||||||
|
interrupt-driven cooperative async, not preemptive.
|
||||||
|
|
||||||
|
**SNES**: There is no loading. Assets live in ROM, mapped directly into the
|
||||||
|
65816 address space. "Loading a texture" means computing a pointer into ROM
|
||||||
|
and copying the tile data to VRAM during V-blank via GPDMA. The asset system
|
||||||
|
on SNES is essentially a VRAM/CGRAM allocator and a DMA scheduler, not a
|
||||||
|
file loader.
|
||||||
|
|
||||||
|
### Asset system changes
|
||||||
|
|
||||||
|
The asset pipeline needs to accommodate three loading models:
|
||||||
|
|
||||||
|
1. **File-based** (Linux, PSP, Vita, Saturn CD): open file, read bytes,
|
||||||
|
close. Can be sync or thread-async.
|
||||||
|
2. **DMA/interrupt** (Saturn CD-ROM, GC DVD): initiate transfer, poll or
|
||||||
|
callback on completion, no thread blocked.
|
||||||
|
3. **ROM-mapped** (SNES): data is already in the address space; "loading"
|
||||||
|
is a VRAM DMA copy scheduled for V-blank, not file I/O.
|
||||||
|
|
||||||
|
The `assetstream_t` abstraction that currently wraps file I/O needs a third
|
||||||
|
backend for ROM-mapped data, and the async path needs to support
|
||||||
|
callback-based completion as an alternative to thread-based blocking.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## What Needs to Change
|
||||||
|
|
||||||
|
### 1. Render intent API (new, in src/dusk/)
|
||||||
|
|
||||||
|
Replace `mesh_t` / `shader_t` / `meshDraw()` as scene-facing APIs with
|
||||||
|
`renderqueue_t` and intent submission functions. `src/dusk/` defines the
|
||||||
|
intent types and submission API; platforms implement the flush.
|
||||||
|
|
||||||
|
### 2. Platform renderer directories
|
||||||
|
|
||||||
|
Move rendering implementations out of `duskgl/` as a shared layer and
|
||||||
|
into fully self-contained platform directories. `duskgl/` becomes the
|
||||||
|
*modern GL* platform only. Add `duskgllegacy/`, `duskvulkan/` as peers.
|
||||||
|
|
||||||
|
### 3. Asset pipeline: platform-native texture formats
|
||||||
|
|
||||||
|
The offline asset compiler must produce per-platform texture bundles in
|
||||||
|
native formats. The runtime texture loader expects pre-converted data,
|
||||||
|
not RGBA. `textureformat_t` grows to cover all platform formats but each
|
||||||
|
platform only ever sees the formats it natively supports.
|
||||||
|
|
||||||
|
### 4. UI system (first-class, separate from 3D)
|
||||||
|
|
||||||
|
New `src/dusk/ui/` subsystem with `uiBegin` / `uiEnd` and intent types
|
||||||
|
for rects, sprites, and text. Platforms implement the flush independently.
|
||||||
|
The 3D spritebatch is retired or scoped to world-space billboards only.
|
||||||
|
|
||||||
|
### 5. Fixed-point / no-FPU math
|
||||||
|
|
||||||
|
`float_t` needs a fixed-point mode. Proposed: define `fixed_t` as a
|
||||||
|
16.16 signed integer; define `DUSK_MATH_FIXED` for platforms that require
|
||||||
|
it (Saturn, SNES). Engine math utilities (`mathSin`, `mathCos`, etc.)
|
||||||
|
have fixed-point implementations selected by this flag. `float_t` on
|
||||||
|
FPU-less platforms becomes a typedef for `fixed_t`.
|
||||||
|
|
||||||
|
### 6. Background plane abstraction (bgplane_t)
|
||||||
|
|
||||||
|
New concept in `src/dusk/display/bgplane/`. A BG plane has a tile map or
|
||||||
|
bitmap source, scroll offsets, a palette reference, and optional affine
|
||||||
|
parameters (for Mode7-style use). On GL platforms: rendered as a
|
||||||
|
fullscreen textured quad or shader pass. On Saturn: VDP2 config. On SNES:
|
||||||
|
PPU BG layer config.
|
||||||
|
|
||||||
|
### 7. Memory system: static pool mode
|
||||||
|
|
||||||
|
For SNES (and possibly Saturn), the general-purpose allocator may be
|
||||||
|
unviable. A compile-time static pool mode (`DUSK_MEMORY_STATIC`) that uses
|
||||||
|
a fixed-size arena instead of dynamic allocation. All `memoryAllocate`
|
||||||
|
calls hit the pool; `memoryFree` is a no-op or a stack pop.
|
||||||
|
|
||||||
|
### 8. Script runtime: optional
|
||||||
|
|
||||||
|
JerryScript requires too much RAM for SNES and is marginal on Saturn.
|
||||||
|
The scripting system should be compile-time optional (`DUSK_SCRIPTING`),
|
||||||
|
not assumed present. SNES/Saturn scenes would be compiled C.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## What to Keep
|
||||||
|
|
||||||
|
- Platform macro abstraction pattern (`displayplatform.h`, etc.) -- works,
|
||||||
|
no reason to change.
|
||||||
|
- Directory structure convention for platform directories.
|
||||||
|
- Entity-component system -- platform-agnostic, unaffected.
|
||||||
|
- Asset loading + `.dsk` bundle concept (extended for platform formats).
|
||||||
|
- The broad subsystem layout: asset, input, display, log, network, save,
|
||||||
|
system, time.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Open Questions
|
||||||
|
|
||||||
|
1. **Render intent granularity**: How much does the intent API need to
|
||||||
|
express? A MESH intent works on GL/N64 but degrades poorly on Saturn
|
||||||
|
(must split into quads) and is impossible on SNES. Should MESH be a
|
||||||
|
valid intent with a "best effort" contract, or excluded from the portable
|
||||||
|
API entirely?
|
||||||
|
|
||||||
|
2. **Threading abstraction depth**: Should `DUSK_THREAD_SLAVE_SH2` be a
|
||||||
|
first-class concept in the engine's job system, or a Saturn-internal
|
||||||
|
implementation detail the core never sees? Same question applies to N64's
|
||||||
|
RSP as a compute co-processor.
|
||||||
|
|
||||||
|
3. **Asset loading async contract**: When a platform has no threads, should
|
||||||
|
`assetLoadAsync` be a no-op alias for `assetLoadSync`, or return
|
||||||
|
immediately with a completion flag to poll? The polling model is more
|
||||||
|
honest but requires all call sites to handle it.
|
||||||
|
|
||||||
|
4. **N64 build system**: libdragon uses GNU Make, not CMake. Options are:
|
||||||
|
(a) write a CMake toolchain file that wraps n64.mk, (b) maintain a
|
||||||
|
parallel Makefile just for N64, or (c) wait for upstream CMake support.
|
||||||
|
Which is acceptable long-term?
|
||||||
|
|
||||||
|
5. **N64 RSP microcode**: Standard libdragon microcodes (Fast3D/F3DEX2) or
|
||||||
|
Tiny3D (community microcode with full T&L + skinning)? Writing custom
|
||||||
|
microcode is powerful but limited to ~1000 MIPS SIMD instructions.
|
||||||
|
This decision gates what 3D features the N64 port can support.
|
||||||
|
|
||||||
|
6. **PSPGL fate**: Drop immediately in favor of native GU, or keep as a
|
||||||
|
fallback (`duskgllegacy`) while native GU is built? The two can coexist
|
||||||
|
during transition.
|
||||||
|
|
||||||
|
7. **Vulkan priority**: Design the intent API with Vulkan in mind from the
|
||||||
|
start, or add it later? Vulkan's explicit pipeline state model may
|
||||||
|
conflict with how stateful platforms (Saturn, SNES) expect things to work.
|
||||||
|
|
||||||
|
8. **Background planes on modern platforms**: Does `bgplane_t` degrade to a
|
||||||
|
fullscreen textured quad on GL/Vulkan/N64, or should modern platforms
|
||||||
|
support actual background scene rendering (3D world behind the foreground)?
|
||||||
|
|
||||||
|
9. **PS1 ordering table depth**: The OT is a fixed-size array (e.g. 4096
|
||||||
|
slots). Depth precision = number of slots. How deep should the engine's
|
||||||
|
default OT be, and should this be configurable per-scene?
|
||||||
|
|
||||||
|
10. **Fixed-point strategy**: Does `float_t` transparently become `fixed_t`
|
||||||
|
on FPU-less platforms (Saturn, PS1, SNES), or do we require explicit
|
||||||
|
`fixed_t` in math-heavy paths? Transparent is easiest to port; explicit
|
||||||
|
is faster.
|
||||||
|
|
||||||
|
11. **SNES V-blank budget**: All VRAM writes must finish within ~1.2ms.
|
||||||
|
Does the engine need a V-blank work queue with a budget checker, or is
|
||||||
|
this left to the game to manage manually?
|
||||||
|
|
||||||
|
12. **SNES scripting**: JerryScript is out. Pure compiled C, or a lighter
|
||||||
|
scripting layer (Lua is ~100 KB -- tight but possible)?
|
||||||
|
|
||||||
|
13. **Asset compiler**: New standalone tool, or an extension of the existing
|
||||||
|
asset pipeline? Part of the CMake build or a separate pre-build step?
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Proposed Sequence (Draft)
|
||||||
|
|
||||||
|
### Phase 1 -- Intent API (no behavior change)
|
||||||
|
1. Design and stabilize `renderqueue_t` and intent types
|
||||||
|
2. Refactor modern GL path to submit through render intents (same output,
|
||||||
|
new plumbing)
|
||||||
|
3. Refactor Dolphin path the same way
|
||||||
|
4. Validate no regressions on Linux + GameCube
|
||||||
|
|
||||||
|
### Phase 2 -- UI system
|
||||||
|
5. Extract UI rendering from the 3D path into `src/dusk/ui/`
|
||||||
|
6. Implement UI flush for GL and Dolphin
|
||||||
|
7. Wire existing UI elements through the new system
|
||||||
|
|
||||||
|
### Phase 3 -- Platform splits
|
||||||
|
8. Split `duskgl/` into `duskgl/` (modern) and `duskgllegacy/` (fixed-func)
|
||||||
|
9. Port PSP to native GU (`duskpsp/display/` rewrite, drop PSPGL dependency)
|
||||||
|
10. Stub `duskvulkan/` structure for future implementation
|
||||||
|
|
||||||
|
### Phase 4 -- Asset pipeline
|
||||||
|
11. Design platform-native texture format system
|
||||||
|
12. Extend asset compiler for per-platform output
|
||||||
|
13. Update texture loader to expect pre-converted data
|
||||||
|
|
||||||
|
### Phase 5 -- Saturn
|
||||||
|
14. CMake toolchain for SH-2 cross-compile (yaul / libyaul toolchain)
|
||||||
|
15. `src/dusksaturn/` -- input (SMPC), asset (CD-ROM), log, system
|
||||||
|
16. VDP1 backend for render queue (quads, polygons, painter's sort)
|
||||||
|
17. VDP2 backend for bgplane_t (tile maps, scroll, palette)
|
||||||
|
18. Fixed-point math mode (`DUSK_MATH_FIXED`)
|
||||||
|
19. UI backend (VDP2 plane(s))
|
||||||
|
|
||||||
|
### Phase 6 -- PlayStation 1
|
||||||
|
20. CMake toolchain wrapping PSn00bSDK (already CMake-native)
|
||||||
|
21. `src/duskps1/` -- input (BIOS pad), asset (CD-ROM libpsxcd), log, system
|
||||||
|
22. GTE integration for fixed-point math (reuse `DUSK_MATH_FIXED` path)
|
||||||
|
23. Ordering table builder for render queue (painter's sort, DMA linked-list)
|
||||||
|
24. GPU packet backend for intents (tris, quads, rects)
|
||||||
|
25. UI backend (separate GPU packet chain after world OT)
|
||||||
|
|
||||||
|
### Phase 7 -- Nintendo 64
|
||||||
|
26. CMake toolchain wrapping libdragon (n64.mk wrapper or toolchain file)
|
||||||
|
27. `src/duskn64/` -- input (N64 controller via PIF), asset (PI DMA /
|
||||||
|
DragonFS), log, system
|
||||||
|
28. RSP display list builder for render queue (Z-buffer path, no sorting)
|
||||||
|
29. TMEM tile management for textures
|
||||||
|
30. RDP rectangle backend for UI
|
||||||
|
31. Decide on RSP microcode (Tiny3D vs standard F3DEX2)
|
||||||
|
|
||||||
|
### Phase 8 -- SNES
|
||||||
|
32. SNES toolchain (cc65 or llvm-mos 65816 target)
|
||||||
|
33. Static memory pool mode (`DUSK_MEMORY_STATIC`)
|
||||||
|
34. PPU tile pipeline + VRAM management
|
||||||
|
35. Mode7 overworld implementation
|
||||||
|
36. OAM sprite system
|
||||||
|
37. BG layer UI
|
||||||
|
38. Scripting-optional build (`DUSK_SCRIPTING` off)
|
||||||
@@ -0,0 +1,215 @@
|
|||||||
|
# Display System
|
||||||
|
|
||||||
|
Source: `src/dusk/display/`
|
||||||
|
|
||||||
|
The display system is the rendering pipeline. It is abstracted across platforms via `displayplatform.h` — see [architecture.md](architecture.md) for the abstraction pattern. The current concrete backends are OpenGL (`src/duskgl/`) and GX/Dolphin (`src/duskdolphin/`).
|
||||||
|
|
||||||
|
For the planned render-queue refactor (required for Saturn), see [display-refactor.md](display-refactor.md).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Initialization order
|
||||||
|
|
||||||
|
Within the display system, init must follow this order (enforced in `engine.c`):
|
||||||
|
|
||||||
|
```
|
||||||
|
displayInit → uiInit → uiTextboxInit
|
||||||
|
```
|
||||||
|
|
||||||
|
Within `displayInit`, the platform typically initialises: framebuffer → screen → shader list → textures → spritebatch → text system.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## `display_t` / `displaystate_t`
|
||||||
|
|
||||||
|
`display_t DISPLAY` is the global display instance (type alias for `displayplatform_t`).
|
||||||
|
|
||||||
|
`displaystate_t` carries per-draw-call render state flags:
|
||||||
|
|
||||||
|
```c
|
||||||
|
DISPLAY_STATE_FLAG_CULL // face culling
|
||||||
|
DISPLAY_STATE_FLAG_DEPTH_TEST // depth testing
|
||||||
|
DISPLAY_STATE_FLAG_BLEND // alpha blending
|
||||||
|
```
|
||||||
|
|
||||||
|
Set state before drawing with `displaySetState(state)`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Screen (`display/screen/`)
|
||||||
|
|
||||||
|
`screen_t SCREEN` manages the logical viewport that game content renders into. On dynamic-size platforms (Linux/SDL2) the screen can differ from the native window/framebuffer resolution.
|
||||||
|
|
||||||
|
Screen modes:
|
||||||
|
```
|
||||||
|
SCREEN_MODE_BACKBUFFER — maps 1:1 to backbuffer
|
||||||
|
SCREEN_MODE_FIXED_SIZE — fixed pixel dimensions
|
||||||
|
SCREEN_MODE_ASPECT_RATIO — fixed aspect, scale to fit
|
||||||
|
SCREEN_MODE_FIXED_HEIGHT — fixed height, width scales
|
||||||
|
SCREEN_MODE_FIXED_WIDTH — fixed width, height scales
|
||||||
|
SCREEN_MODE_FIXED_VIEWPORT_HEIGHT — fixed viewport height
|
||||||
|
```
|
||||||
|
|
||||||
|
The linux target defines `DUSK_DISPLAY_SCREEN_HEIGHT=240`, producing a 240p fixed-height viewport.
|
||||||
|
|
||||||
|
Render loop usage:
|
||||||
|
```c
|
||||||
|
screenBind(); // set up viewport, projection
|
||||||
|
// ... draw game content ...
|
||||||
|
screenUnbind();
|
||||||
|
screenRender(); // blit to backbuffer / current framebuffer
|
||||||
|
```
|
||||||
|
|
||||||
|
`SCREEN.width` / `SCREEN.height` are the logical dimensions used for world-to-screen math — always prefer these over the framebuffer dimensions.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Framebuffer (`display/framebuffer/`)
|
||||||
|
|
||||||
|
`framebuffer_t FRAMEBUFFER_BACKBUFFER` is the platform backbuffer. `FRAMEBUFFER_BOUND` points to the currently-bound framebuffer (or `NULL` for backbuffer).
|
||||||
|
|
||||||
|
```c
|
||||||
|
frameBufferInitBackBuffer(); // called once at startup
|
||||||
|
frameBufferBind(fb); // NULL → backbuffer
|
||||||
|
frameBufferClear(FRAMEBUFFER_CLEAR_COLOR | FRAMEBUFFER_CLEAR_DEPTH, COLOR_BLACK);
|
||||||
|
frameBufferGetWidth(fb) / frameBufferGetHeight(fb) / frameBufferGetAspect(fb);
|
||||||
|
```
|
||||||
|
|
||||||
|
On platforms with `DUSK_DISPLAY_SIZE_DYNAMIC`, off-screen framebuffers can be created with `frameBufferInit(fb, w, h)` and disposed with `frameBufferDispose(fb)`. Fixed-resolution platforms (PSP, GameCube) only ever use the backbuffer.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Mesh (`display/mesh/`)
|
||||||
|
|
||||||
|
`mesh_t` is a vertex buffer. The type is `meshplatform_t` (e.g. a VAO+VBO on GL, a GX display list on Dolphin).
|
||||||
|
|
||||||
|
```c
|
||||||
|
meshInit(&mesh, MESH_PRIMITIVE_TYPE_TRIANGLES, vertexCount, verticesPtr);
|
||||||
|
meshFlush(&mesh, offset, count); // upload CPU vertices → GPU
|
||||||
|
meshDraw(&mesh, offset, count); // draw; pass -1 for count to draw all
|
||||||
|
meshDispose(&mesh);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key distinction**: `meshFlush` uploads data to GPU memory; `meshDraw` issues the draw call. For static geometry (chunk meshes) you call `meshFlush` once on load, then `meshDraw` every frame. For dynamic geometry (spritebatch) you `meshFlush` + `meshDraw` each frame.
|
||||||
|
|
||||||
|
`meshvertex_t` (`display/mesh/meshvertex.h`) contains:
|
||||||
|
- `float_t uv[2]` — texture coordinates
|
||||||
|
- `float_t pos[3]` — position
|
||||||
|
- Optionally `color_t color` if `MESH_ENABLE_COLOR` is defined (off by default)
|
||||||
|
|
||||||
|
Primitive mesh generators live alongside `mesh.h`: `quad.h`, `plane.h`, `cube.h`, `sphere.h`, `capsule.h`, `triprism.h`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Shader (`display/shader/`)
|
||||||
|
|
||||||
|
`shader_t` is `shaderplatform_t` (GLSL program on GL, TEV state block on Dolphin).
|
||||||
|
|
||||||
|
```c
|
||||||
|
shaderInit(&shader, &definition);
|
||||||
|
shaderBind(&shader);
|
||||||
|
shaderSetMatrix(&shader, "uModel", modelMat);
|
||||||
|
shaderSetTexture(&shader, "uTexture", &texture);
|
||||||
|
shaderSetColor(&shader, "uColor", COLOR_WHITE);
|
||||||
|
shaderSetMaterial(&shader, &material);
|
||||||
|
shaderDispose(&shader);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Shader list (`display/shader/shaderlist.h`)
|
||||||
|
|
||||||
|
The engine maintains a small set of built-in shaders in `SHADER_LIST_DEFS[]`. Currently only one is defined:
|
||||||
|
|
||||||
|
- `SHADER_LIST_SHADER_UNLIT` → `SHADER_UNLIT` — unlit textured/colored rendering, used for all world and entity drawing.
|
||||||
|
|
||||||
|
`shaderListInit()` compiles/uploads all built-in shaders and sets shared projection/view matrices. Call once after display init.
|
||||||
|
|
||||||
|
### Materials (`display/shader/shadermaterial.h`)
|
||||||
|
|
||||||
|
`shadermaterial_t` is a union of all shader-specific material structs. Currently only `shaderunlitmaterial_t`:
|
||||||
|
|
||||||
|
```c
|
||||||
|
shadermaterial_t mat = {
|
||||||
|
.unlit = {
|
||||||
|
.color = COLOR_WHITE,
|
||||||
|
.texture = &myTexture, // NULL for solid color
|
||||||
|
}
|
||||||
|
};
|
||||||
|
shaderSetMaterial(&SHADER_UNLIT, &mat);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Texture (`display/texture/`)
|
||||||
|
|
||||||
|
```c
|
||||||
|
textureInit(&texture, width, height, format, data);
|
||||||
|
textureDispose(&texture);
|
||||||
|
```
|
||||||
|
|
||||||
|
Width and height **must be powers of two** (asserted at init time).
|
||||||
|
|
||||||
|
`textureformat_t` is `textureformatplatform_t`. Supported formats vary by platform; the common ones are `TEXTURE_FORMAT_RGBA` and `TEXTURE_FORMAT_PALETTE`.
|
||||||
|
|
||||||
|
`texturedata_t` is a union:
|
||||||
|
```c
|
||||||
|
// RGBA:
|
||||||
|
data.rgbaColors = colorArray;
|
||||||
|
|
||||||
|
// Paletted:
|
||||||
|
data.paletted.indices = indexArray;
|
||||||
|
data.paletted.palette = &palette; // palette color count must be power of two
|
||||||
|
```
|
||||||
|
|
||||||
|
**Built-in textures** (defined in `texture.c`, no asset loading needed):
|
||||||
|
- `TEXTURE_WHITE` — 4×4 solid white
|
||||||
|
- `TEXTURE_TEST` — 4×4 black/magenta checkerboard
|
||||||
|
|
||||||
|
### Palette (`display/texture/palette.h`)
|
||||||
|
|
||||||
|
Up to `PALETTE_COUNT` (6) global palettes in `PALETTES[]`, each holding up to `PALETTE_COLOR_COUNT` (255) `color_t` entries.
|
||||||
|
|
||||||
|
### Tileset (`display/texture/tileset.h`)
|
||||||
|
|
||||||
|
A tileset slices a texture into a grid of equal-sized tiles. Used by fonts and UI frames. The tileset does not own the texture — it references a `texture_t *`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## SpriteBatch (`display/spritebatch/`)
|
||||||
|
|
||||||
|
The primary 2D/billboard drawing primitive. Accumulates `spritebatchsprite_t` quads and flushes them in batches of `SPRITEBATCH_FLUSH_COUNT` (16) sprites at a time.
|
||||||
|
|
||||||
|
```c
|
||||||
|
// Per frame:
|
||||||
|
spriteBatchClear();
|
||||||
|
spriteBatchBuffer(sprites, count, &SHADER_UNLIT, material); // auto-flushes when batch full
|
||||||
|
spriteBatchFlush(); // flush remaining
|
||||||
|
|
||||||
|
// Low-level: write directly to an external mesh (for baking static geometry):
|
||||||
|
spriteBatchBufferToMesh(sprites, count, vertices, verticesSize);
|
||||||
|
```
|
||||||
|
|
||||||
|
`spritebatchsprite_t`:
|
||||||
|
```c
|
||||||
|
typedef struct {
|
||||||
|
vec3 min, max; // 3D bounding box
|
||||||
|
vec2 uvMin, uvMax; // texture region
|
||||||
|
} spritebatchsprite_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
The global `SPRITEBATCH` and its vertex backing array `SPRITEBATCH_VERTICES[]` are defined externally to the struct to satisfy alignment requirements on certain platforms.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Text (`display/text/`)
|
||||||
|
|
||||||
|
Text rendering uses `FONT_DEFAULT` (loaded during `textInit()`), which references a texture and a tileset. Characters start at ASCII `!` (`TEXT_CHAR_START`).
|
||||||
|
|
||||||
|
```c
|
||||||
|
textDraw(x, y, "Hello", COLOR_WHITE, &FONT_DEFAULT);
|
||||||
|
textMeasure("Hello", &FONT_DEFAULT, &outWidth, &outHeight);
|
||||||
|
|
||||||
|
// Single-char sprite for manual layout:
|
||||||
|
spritebatchsprite_t s = textGetSprite(pos, 'A', &FONT_DEFAULT);
|
||||||
|
```
|
||||||
|
|
||||||
|
`font_t` holds a `texture_t *` and a `tileset_t *` — both are owned by the asset system, not the font struct.
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
# Input System
|
||||||
|
|
||||||
|
Source: `src/dusk/input/`
|
||||||
|
|
||||||
|
The input system decouples physical hardware buttons from logical game actions via a binding layer. Actions are defined in `src/dusk/input/input.csv` and code-generated into `inputaction_t` enum values — see [architecture.md](architecture.md#code-generation-from-csv).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Concepts
|
||||||
|
|
||||||
|
**Button** (`inputbutton_t`) — a physical input source: a keyboard scancode, gamepad button, gamepad axis, or pointer axis. The available button types depend on which `DUSK_INPUT_*` defines are active for the target platform.
|
||||||
|
|
||||||
|
**Action** (`inputaction_t`) — a logical game input (e.g. `INPUT_ACTION_UP`, `INPUT_ACTION_CONFIRM`, `INPUT_ACTION_RAGEQUIT`). Each action accumulates a float value `[0.0, 1.0]` from all buttons bound to it.
|
||||||
|
|
||||||
|
**Binding** — a many-to-one mapping from buttons to actions. Bindings are registered at runtime with `inputBind(button, action)`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Querying actions
|
||||||
|
|
||||||
|
```c
|
||||||
|
// Boolean helpers (current frame):
|
||||||
|
inputIsDown(action) // value > 0 this frame
|
||||||
|
inputPressed(action) // down this frame but not last
|
||||||
|
inputReleased(action) // down last frame but not this
|
||||||
|
|
||||||
|
// Last frame state:
|
||||||
|
inputWasDown(action)
|
||||||
|
|
||||||
|
// Raw float value:
|
||||||
|
inputGetCurrentValue(action) // [0.0, 1.0]
|
||||||
|
inputGetLastValue(action)
|
||||||
|
|
||||||
|
// Axis helpers — combine two opposing actions into a signed float:
|
||||||
|
float_t h = inputAxis(INPUT_ACTION_LEFT, INPUT_ACTION_RIGHT); // -1.0 to 1.0
|
||||||
|
inputAxis2D(negX, posX, negY, posY, result); // fills vec2
|
||||||
|
inputAngle2D(negX, posX, negY, posY, result); // atan2-based normalized direction
|
||||||
|
|
||||||
|
// Deadzone:
|
||||||
|
float_t clean = inputDeadzone(rawValue, 0.1f);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Dynamic values (`DUSK_TIME_DYNAMIC`)
|
||||||
|
|
||||||
|
On platforms with variable frame rates, each action also tracks `dynamicDelta`-scaled values:
|
||||||
|
|
||||||
|
```c
|
||||||
|
inputGetCurrentValueDynamic(action)
|
||||||
|
inputGetLastValueDynamic(action)
|
||||||
|
```
|
||||||
|
|
||||||
|
These account for the actual time elapsed since the last frame, so movement calculated from them is frame-rate independent.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Events on actions
|
||||||
|
|
||||||
|
Each `inputactiondata_t` exposes `onPressed` and `onReleased` events:
|
||||||
|
|
||||||
|
```c
|
||||||
|
eventSubscribe(&INPUT.actions[INPUT_ACTION_CONFIRM].onPressed, myCallback, myUser);
|
||||||
|
```
|
||||||
|
|
||||||
|
The callback signature is `void cb(void *params, void *user)`. `params` is always `NULL` for input events.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Buttons and bindings
|
||||||
|
|
||||||
|
Physical buttons are typed via `inputbuttontype_t`:
|
||||||
|
|
||||||
|
| Constant | When available | Payload |
|
||||||
|
|---|---|---|
|
||||||
|
| `INPUT_BUTTON_TYPE_KEYBOARD` | `DUSK_INPUT_KEYBOARD` | `inputscancode_t` |
|
||||||
|
| `INPUT_BUTTON_TYPE_GAMEPAD` | `DUSK_INPUT_GAMEPAD` | `inputgamepadbutton_t` |
|
||||||
|
| `INPUT_BUTTON_TYPE_GAMEPAD_AXIS` | `DUSK_INPUT_GAMEPAD` | axis + positive direction flag |
|
||||||
|
| `INPUT_BUTTON_TYPE_POINTER` | `DUSK_INPUT_POINTER` | `inputpointeraxis_t` |
|
||||||
|
|
||||||
|
Button names and default bindings are defined in `input.csv`. Look up a button by name:
|
||||||
|
```c
|
||||||
|
inputbutton_t btn = inputButtonGetByName("keyboard_w");
|
||||||
|
inputBind(btn, INPUT_ACTION_UP);
|
||||||
|
```
|
||||||
|
|
||||||
|
`INPUT_BUTTON_DATA[]` holds runtime state (current/last raw values) for every physical button.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Platform platform-specific reads
|
||||||
|
|
||||||
|
`inputButtonGetValuePlatform(button)` is the one required platform function — it returns the current raw `[0.0, 1.0]` value for a button. The platform implementations live in `src/dusk{platform}/input/`.
|
||||||
@@ -0,0 +1,149 @@
|
|||||||
|
# Cutscenes
|
||||||
|
|
||||||
|
Two distinct layers: a low-level engine sequencer (`src/dusk/cutscene/`) and a higher-level RPG wrapper (`src/dusk/rpg/cutscene/`). Almost all game code works with the RPG layer.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Engine sequencer (`src/dusk/cutscene/`)
|
||||||
|
|
||||||
|
`cutscene_t CUTSCENE` is a minimal event runner with up to `CUTSCENE_EVENT_COUNT_MAX` (16) `cutsceneevent_t` slots. Each event has three callbacks:
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct {
|
||||||
|
errorret_t (*onStart)(void);
|
||||||
|
errorret_t (*onUpdate)(void);
|
||||||
|
errorret_t (*onEnd)(void);
|
||||||
|
} cutsceneevent_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
API:
|
||||||
|
```c
|
||||||
|
cutscenePlay(events, count); // copy events array and start from index 0
|
||||||
|
cutsceneAdvance(); // end current event, start next (deactivates after last)
|
||||||
|
cutsceneStop(); // abort immediately
|
||||||
|
cutsceneIsActive(); // bool
|
||||||
|
```
|
||||||
|
|
||||||
|
This layer is primarily used by the RPG cutscene system — game code doesn't normally touch it directly.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## RPG cutscene layer (`src/dusk/rpg/cutscene/`)
|
||||||
|
|
||||||
|
### Data structures
|
||||||
|
|
||||||
|
A `cutscene_t` is just a pointer to an item array and a count:
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct cutscene_s {
|
||||||
|
const cutsceneitem_t *items;
|
||||||
|
uint8_t itemCount;
|
||||||
|
} cutscene_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
A `cutsceneitem_t` is a tagged union of all item types:
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct cutsceneitem_s {
|
||||||
|
cutsceneitemtype_t type;
|
||||||
|
union {
|
||||||
|
cutscenetext_t text; // display text in textbox
|
||||||
|
cutscenecallback_t callback; // call a void(*)(void) function
|
||||||
|
cutscenewait_t wait; // pause for a fixed_t duration (seconds)
|
||||||
|
const cutscene_t *cutscene; // nest another cutscene
|
||||||
|
};
|
||||||
|
} cutsceneitem_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Item types
|
||||||
|
|
||||||
|
| Type constant | Payload | Behaviour |
|
||||||
|
|---|---|---|
|
||||||
|
| `CUTSCENE_ITEM_TYPE_TEXT` | `cutscenetext_t` — `text[256]` + `rpgtextboxpos_t position` | Shows textbox; advances on player confirm input |
|
||||||
|
| `CUTSCENE_ITEM_TYPE_CALLBACK` | `cutscenecallback_t` (function pointer) | Calls the function once, then immediately advances |
|
||||||
|
| `CUTSCENE_ITEM_TYPE_WAIT` | `cutscenewait_t` (a `fixed_t` in seconds) | Counts down `animTime` each frame, then advances |
|
||||||
|
| `CUTSCENE_ITEM_TYPE_CUTSCENE` | `const cutscene_t *` | Plays the nested cutscene before continuing |
|
||||||
|
|
||||||
|
### Runtime state
|
||||||
|
|
||||||
|
`cutscenesystem_t CUTSCENE_SYSTEM` tracks:
|
||||||
|
- `scene` — pointer to the active `cutscene_t`
|
||||||
|
- `currentItem` — index into `scene->items[]`
|
||||||
|
- `data` — per-item runtime data (`cutsceneitemdata_t`, currently just `cutscenewaitdata_t`)
|
||||||
|
- `mode` — the current `cutscenemode_t`
|
||||||
|
|
||||||
|
API:
|
||||||
|
```c
|
||||||
|
cutsceneSystemStartCutscene(cutscene); // begin playing a cutscene
|
||||||
|
cutsceneSystemNext(); // advance to next item
|
||||||
|
cutsceneSystemUpdate(); // called each frame from rpgUpdate
|
||||||
|
cutsceneSystemGetCurrentItem(); // inspect active item
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cutscene mode (`cutscenemode.h`)
|
||||||
|
|
||||||
|
Each item can run in one of three modes:
|
||||||
|
|
||||||
|
```c
|
||||||
|
CUTSCENE_MODE_NONE // no cutscene active
|
||||||
|
CUTSCENE_MODE_FULL_FREEZE // pause everything (not yet used)
|
||||||
|
CUTSCENE_MODE_INPUT_FREEZE // player input blocked (default: CUTSCENE_MODE_INITIAL)
|
||||||
|
CUTSCENE_MODE_GAMEPLAY // player can still move during cutscene
|
||||||
|
```
|
||||||
|
|
||||||
|
`cutsceneModeIsInputAllowed()` is checked by `entityUpdate()` before invoking the movement callback — the player cannot walk when in INPUT_FREEZE mode.
|
||||||
|
|
||||||
|
### Defining a cutscene
|
||||||
|
|
||||||
|
Cutscenes are defined as `static const` arrays in header files under `rpg/cutscene/scene/`. Example (`testcutscene.h`):
|
||||||
|
|
||||||
|
```c
|
||||||
|
static const cutsceneitem_t MY_CUTSCENE_ITEMS[] = {
|
||||||
|
{
|
||||||
|
.type = CUTSCENE_ITEM_TYPE_TEXT,
|
||||||
|
.text = { .text = "Hello!", .position = RPG_TEXTBOX_POS_BOTTOM }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.type = CUTSCENE_ITEM_TYPE_WAIT,
|
||||||
|
.wait = FIXED(1.5f)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.type = CUTSCENE_ITEM_TYPE_CUTSCENE,
|
||||||
|
.cutscene = &ANOTHER_CUTSCENE
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static const cutscene_t MY_CUTSCENE = {
|
||||||
|
.items = MY_CUTSCENE_ITEMS,
|
||||||
|
.itemCount = sizeof(MY_CUTSCENE_ITEMS) / sizeof(cutsceneitem_t)
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
Attach to an NPC via its interact component:
|
||||||
|
```c
|
||||||
|
entity->interact.type = ENTITY_INTERACT_CUTSCENE;
|
||||||
|
entity->interact.data.cutscene = &MY_CUTSCENE;
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Textbox (`src/dusk/rpg/rpgtextbox.h`)
|
||||||
|
|
||||||
|
`rpgtextbox_t RPG_TEXTBOX` is the global textbox state:
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct {
|
||||||
|
rpgtextboxpos_t position; // RPG_TEXTBOX_POS_TOP or RPG_TEXTBOX_POS_BOTTOM
|
||||||
|
bool_t visible;
|
||||||
|
char_t text[RPG_TEXTBOX_MAX_CHARS]; // 256 chars
|
||||||
|
} rpgtextbox_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
API:
|
||||||
|
```c
|
||||||
|
rpgTextboxShow(position, text); // copies text, sets visible = true
|
||||||
|
rpgTextboxHide(); // sets visible = false
|
||||||
|
rpgTextboxIsVisible(); // bool
|
||||||
|
```
|
||||||
|
|
||||||
|
The textbox state is read by `ui/uitextbox.c` during the UI render pass to draw the dialogue box on screen. `rpgtextbox.c` itself does no rendering.
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
# Entities
|
||||||
|
|
||||||
|
Source: `src/dusk/rpg/entity/`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Storage
|
||||||
|
|
||||||
|
Entities live in a single fixed global array:
|
||||||
|
|
||||||
|
```c
|
||||||
|
entity_t ENTITIES[ENTITY_COUNT]; // ENTITY_COUNT = 64
|
||||||
|
```
|
||||||
|
|
||||||
|
A slot is "empty" when `entity->type == ENTITY_TYPE_NULL`. Never allocate entity memory dynamically — always find a free slot with `entityGetAvailable()`, which returns its index (`0xFF` if none free).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## `entity_t` structure
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct entity_s {
|
||||||
|
uint8_t id; // index in ENTITIES[]
|
||||||
|
entitytype_t type; // ENTITY_TYPE_NULL / PLAYER / NPC
|
||||||
|
entitytypedata_t data; // union: player_t | npc_t
|
||||||
|
|
||||||
|
entitydir_t direction; // facing direction (N/S/E/W)
|
||||||
|
fixed_t position[3]; // current sub-tile position (x, y, z)
|
||||||
|
fixed_t lastPosition[3]; // position before last move (for interpolation)
|
||||||
|
|
||||||
|
entityanim_t animation; // IDLE / TURN / WALK
|
||||||
|
fixed_t animTime; // countdown timer for current animation
|
||||||
|
|
||||||
|
entityinteract_t interact; // optional interact component
|
||||||
|
} entity_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Type system
|
||||||
|
|
||||||
|
Entity types are defined in `entitytype.h` using the enum+integer-typedef pattern:
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef enum { ENTITY_TYPE_NULL, ENTITY_TYPE_PLAYER, ENTITY_TYPE_NPC, ENTITY_TYPE_COUNT } entitytype_enum_t;
|
||||||
|
typedef uint8_t entitytype_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
Each type has a `entitycallback_t` entry in the `ENTITY_CALLBACKS[ENTITY_TYPE_COUNT]` static table:
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct {
|
||||||
|
void (*init)(entity_t *entity);
|
||||||
|
void (*movement)(entity_t *entity);
|
||||||
|
bool_t (*interact)(entity_t *player, entity_t *entity);
|
||||||
|
} entitycallback_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
Callbacks not applicable to a type are `NULL`; `entityUpdate()` guards against this before calling.
|
||||||
|
|
||||||
|
Type-specific data sits in `entitytypedata_t` (a union of `player_t` and `npc_t`). Currently both are stubs (`void *nothing`).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Direction (`entitydir.h`)
|
||||||
|
|
||||||
|
```c
|
||||||
|
ENTITY_DIR_NORTH / EAST / SOUTH / WEST
|
||||||
|
```
|
||||||
|
|
||||||
|
Aliases: `UP = NORTH`, `DOWN = SOUTH`, `LEFT = WEST`, `RIGHT = EAST`.
|
||||||
|
|
||||||
|
Utilities:
|
||||||
|
- `entityDirGetOpposite(dir)` — returns the opposite direction.
|
||||||
|
- `entityDirGetRelative(dir, &relX, &relY)` — fills in the ±1 XY delta for that direction.
|
||||||
|
- `assertValidEntityDir(dir, msg)` — assertion macro.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Animation (`entityanim.h`)
|
||||||
|
|
||||||
|
```c
|
||||||
|
ENTITY_ANIM_IDLE // standing still
|
||||||
|
ENTITY_ANIM_TURN // turning to a new direction (ENTITY_ANIM_TURN_DURATION = FIXED(0.06))
|
||||||
|
ENTITY_ANIM_WALK // mid-step (ENTITY_ANIM_WALK_DURATION = FIXED(0.1))
|
||||||
|
```
|
||||||
|
|
||||||
|
`entityAnimUpdate(entity)` decrements `animTime` each frame and transitions back to `IDLE` when it reaches zero.
|
||||||
|
|
||||||
|
`entityCanWalk(entity)` / `entityCanTurn(entity)` both return true only when `animation == ENTITY_ANIM_IDLE`.
|
||||||
|
|
||||||
|
The renderer interpolates between `lastPosition` and `position` using `animTime / WALK_DURATION` to produce smooth motion even at low frame rates.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Movement
|
||||||
|
|
||||||
|
`entityWalk(entity, direction)`:
|
||||||
|
|
||||||
|
1. Converts `entity->position` to a `worldpos_t` (truncates fractional part).
|
||||||
|
2. Applies the directional delta to get `newPos`.
|
||||||
|
3. Checks the current and target tiles for ramp raise/fall logic (see [world.md](world.md)).
|
||||||
|
4. Checks `ENTITIES[]` for another entity occupying `newPos` — blocks if found.
|
||||||
|
5. On success: copies `position` to `lastPosition`, updates `position` to `newPos` (via `worldPosToFixed`), sets `animation = ENTITY_ANIM_WALK`.
|
||||||
|
|
||||||
|
`entityTurn(entity, direction)`: sets `direction` and starts a brief turn animation.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Interaction (`entityinteract.h`)
|
||||||
|
|
||||||
|
The `entityinteract_t` component is embedded in every entity. It is optional — set `type = ENTITY_INTERACT_NULL` for non-interactable entities.
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef enum {
|
||||||
|
ENTITY_INTERACT_NULL,
|
||||||
|
ENTITY_INTERACT_CUTSCENE, // plays a cutscene_t *
|
||||||
|
ENTITY_INTERACT_PRINT, // prints a short message[32]
|
||||||
|
} entityinteracttype_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
`entityInteractWith(player, target)` dispatches:
|
||||||
|
1. If the interact component's `type != NULL`, handles it (starts the cutscene or prints the message).
|
||||||
|
2. Otherwise falls back to `ENTITY_CALLBACKS[type].interact` if set.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Player (`player.h` / `player.c`)
|
||||||
|
|
||||||
|
`playerInit()` is called via `ENTITY_CALLBACKS[ENTITY_TYPE_PLAYER].init`.
|
||||||
|
|
||||||
|
`playerInput(entity)` is the movement callback. It reads `PLAYER_INPUT_DIR_MAP[]` — a static table mapping input actions (`INPUT_ACTION_UP/DOWN/LEFT/RIGHT`) to entity directions — and calls `entityWalk` or `entityTurn` accordingly.
|
||||||
|
|
||||||
|
The player entity is normally `ENTITIES[0]` but there is no hardcoded assumption about its index beyond being initialized with `ENTITY_TYPE_PLAYER`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## NPC (`npc.h` / `npc.c`)
|
||||||
|
|
||||||
|
`npcInit()`, `npcMovement()`, and `npcInteract()` provide the NPC type callbacks. Currently stubs; movement does nothing, interact returns false.
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
# RPG Layer
|
||||||
|
|
||||||
|
The RPG layer lives in `src/dusk/rpg/` and is the game-logic tier above the engine. It is initialized and ticked by `engine.c` via `rpgInit` / `rpgUpdate` / `rpgDispose`. The `rpg_t` struct is currently a stub; all meaningful state lives in the subsystems below.
|
||||||
|
|
||||||
|
## Contents
|
||||||
|
|
||||||
|
- [world.md](world.md) — scene manager, overworld map, chunks, tiles, coordinate system, camera
|
||||||
|
- [entity.md](entity.md) — entity pool, types, direction, animation, interaction, player, NPC
|
||||||
|
- [cutscene.md](cutscene.md) — cutscene system, item types, mode control, textbox
|
||||||
|
- [story.md](story.md) — story flags, items, inventory, backpack, save system
|
||||||
|
|
||||||
|
## Scene system
|
||||||
|
|
||||||
|
The scene manager (`src/dusk/scene/`) sits above the RPG layer and owns the single active scene. Scenes are identified by `scenetype_t` and registered in `SCENE_TYPES[]` (`scene/scenetype.c`) as `scenecallbacks_t` (init / update / render / dispose).
|
||||||
|
|
||||||
|
`scenedata_t` is a union so all scene structs share memory. `sceneSet(type)` defers the transition — the old scene disposes before the new one inits.
|
||||||
|
|
||||||
|
Currently the only scene is `SCENE_TYPE_OVERWORLD` → `src/dusk/scene/overworld/sceneoverworld.c`.
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
# Story, Items & Save
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Story flags (`src/dusk/rpg/story/`)
|
||||||
|
|
||||||
|
Story flags are the primary mechanism for tracking game-world state (quest progress, one-time events, unlocks). Each flag is a `uint8_t` value (`storyflagvalue_t`), so they can hold booleans or small counts.
|
||||||
|
|
||||||
|
### Defining flags
|
||||||
|
|
||||||
|
Flags are defined in `src/dusk/rpg/story/storyflag.csv`:
|
||||||
|
|
||||||
|
```
|
||||||
|
id,description,initial
|
||||||
|
test,"Test flag for debugging purposes",1
|
||||||
|
```
|
||||||
|
|
||||||
|
The build tool generates:
|
||||||
|
- A `storyflag_t` enum (e.g. `STORY_FLAG_TEST`) in the generated header.
|
||||||
|
- `STORY_FLAG_VALUES[]` — the runtime array, pre-populated with the `initial` column values.
|
||||||
|
|
||||||
|
To add a flag: add a row to the CSV. The build re-runs the Python tool automatically on the next CMake build.
|
||||||
|
|
||||||
|
### Access
|
||||||
|
|
||||||
|
```c
|
||||||
|
storyflagvalue_t v = storyFlagGet(STORY_FLAG_TEST); // macro: array read
|
||||||
|
storyFlagSet(STORY_FLAG_TEST, 1); // function: also marks save dirty
|
||||||
|
```
|
||||||
|
|
||||||
|
`storyFlagGet` is a macro that directly indexes `STORY_FLAG_VALUES[]` — no function call overhead.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Items (`src/dusk/rpg/item/`)
|
||||||
|
|
||||||
|
### Item definitions
|
||||||
|
|
||||||
|
Items are defined in `src/dusk/rpg/item/item.csv`. The build tool generates `itemid_t` enum values and item metadata. `itemid_t` is a generated `uint8_t` typedef.
|
||||||
|
|
||||||
|
### Inventory (`inventory.h`)
|
||||||
|
|
||||||
|
`inventory_t` is a generic container backed by a caller-supplied `inventorystack_t` array:
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct {
|
||||||
|
itemid_t item;
|
||||||
|
uint8_t quantity; // max ITEM_STACK_QUANTITY_MAX (255)
|
||||||
|
} inventorystack_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
inventorystack_t *storage;
|
||||||
|
uint8_t storageSize;
|
||||||
|
} inventory_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
Key operations:
|
||||||
|
|
||||||
|
```c
|
||||||
|
inventoryInit(&inv, storageArray, size);
|
||||||
|
inventoryAdd(&inv, ITEM_POTION, 3);
|
||||||
|
inventoryRemove(&inv, ITEM_POTION);
|
||||||
|
inventorySet(&inv, ITEM_POTION, 10);
|
||||||
|
inventoryGetCount(&inv, ITEM_POTION); // returns 0 if not present
|
||||||
|
inventoryItemExists(&inv, ITEM_POTION);
|
||||||
|
inventoryIsFull(&inv);
|
||||||
|
inventorySort(&inv, INVENTORY_SORT_BY_ID, false);
|
||||||
|
```
|
||||||
|
|
||||||
|
`inventory_t` itself holds no data — the backing array is always external. This avoids fixed-size struct limits and lets different inventories (backpack, shop, chest) share the same logic.
|
||||||
|
|
||||||
|
### Backpack (`backpack.h`)
|
||||||
|
|
||||||
|
The player's inventory is the global `BACKPACK` instance:
|
||||||
|
|
||||||
|
```c
|
||||||
|
extern inventorystack_t BACKPACK_STORAGE[BACKPACK_STORAGE_SIZE_MAX]; // 20 slots
|
||||||
|
extern inventory_t BACKPACK;
|
||||||
|
|
||||||
|
backpackInit(); // wires BACKPACK_STORAGE into BACKPACK
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Save system (`src/dusk/save/`)
|
||||||
|
|
||||||
|
The save system is stubbed out — it exists and compiles but is commented out of engine init (`engine.c`). What follows describes the design as implemented.
|
||||||
|
|
||||||
|
### Slots
|
||||||
|
|
||||||
|
`save_t SAVE` holds `SAVE_FILE_COUNT_MAX` slots:
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct {
|
||||||
|
savefile_t files[SAVE_FILE_COUNT_MAX];
|
||||||
|
saveplatform_t platform; // platform-specific state (paths, card handles)
|
||||||
|
} save_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Streams
|
||||||
|
|
||||||
|
`savestream_t` (`save/savestream.h`) is a raw byte cursor used to serialize/deserialize `savefile_t`. Platform backends in `src/dusk{platform}/save/` implement the actual I/O:
|
||||||
|
- Linux: filesystem files in a save directory.
|
||||||
|
- GameCube/Wii: memory card via libogc.
|
||||||
|
|
||||||
|
### API
|
||||||
|
|
||||||
|
```c
|
||||||
|
saveInit();
|
||||||
|
saveLoad(slot); // reads platform storage → savefile_t
|
||||||
|
saveWrite(slot); // writes savefile_t → platform storage
|
||||||
|
saveDelete(slot);
|
||||||
|
saveExists(slot); // bool
|
||||||
|
saveGet(slot); // returns savefile_t *
|
||||||
|
saveDispose();
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Locale / i18n (`src/dusk/locale/`)
|
||||||
|
|
||||||
|
Translations are loaded from `.po` files in `assets/locale/` (e.g. `en_US.po`). `localemanager.c` manages the active locale and exposes a key→string lookup. `assetlocaleloader.c` parses the PO format via the asset system.
|
||||||
|
|
||||||
|
All player-visible strings must go through the locale system rather than being hardcoded. The locale is loaded asynchronously via the asset system so it is available before the first scene renders.
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
# World
|
||||||
|
|
||||||
|
Source: `src/dusk/rpg/overworld/`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Coordinate system
|
||||||
|
|
||||||
|
Three nested coordinate spaces, each defined in `worldpos.h`:
|
||||||
|
|
||||||
|
| Type | Description | Unit |
|
||||||
|
|---|---|---|
|
||||||
|
| `worldpos_t` | Tile-level absolute position `{x, y, z}` | `worldunit_t` (int16) |
|
||||||
|
| `chunkpos_t` | Chunk-grid position `{x, y, z}` | `chunkunit_t` (int16) |
|
||||||
|
| `fixed_t[3]` | Smooth sub-tile position used by entities | Q24.8 fixed-point |
|
||||||
|
|
||||||
|
One chunk = `CHUNK_WIDTH × CHUNK_HEIGHT × CHUNK_DEPTH` tiles (16 × 16 × 8).
|
||||||
|
The loaded world window = `MAP_CHUNK_WIDTH × MAP_CHUNK_HEIGHT × MAP_CHUNK_DEPTH` chunks (5 × 5 × 3).
|
||||||
|
|
||||||
|
Conversion helpers (all in `worldpos.c`):
|
||||||
|
|
||||||
|
```c
|
||||||
|
worldPosToChunkPos(&worldPos, &chunkPos); // tile → chunk grid
|
||||||
|
chunkPosToWorldPos(&chunkPos, &worldPos); // chunk grid → tile origin
|
||||||
|
worldPosToChunkTileIndex(&worldPos); // tile → index within its chunk
|
||||||
|
chunkPosToIndex(&chunkPos); // chunk grid → linear index in MAP.chunks[]
|
||||||
|
worldPosToFixed(&worldPos, fixedOut); // tile → entity fixed position
|
||||||
|
fixedToWorldPos(fixedPos); // entity fixed → tile (truncates frac)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Tiles
|
||||||
|
|
||||||
|
Defined in `tile.h` as a plain `tile_t` enum:
|
||||||
|
|
||||||
|
```
|
||||||
|
TILE_SHAPE_NULL — empty / unloaded
|
||||||
|
TILE_SHAPE_GROUND — solid flat tile
|
||||||
|
TILE_SHAPE_RAMP_* — directional ramps (N/S/E/W + diagonals NE/NW/SE/SW)
|
||||||
|
```
|
||||||
|
|
||||||
|
Key predicates:
|
||||||
|
- `tileIsWalkable(tile)` — true for GROUND and all ramp shapes.
|
||||||
|
- `tileIsRamp(tile)` — true only for ramp shapes.
|
||||||
|
|
||||||
|
Entity walk code (`entity.c`) checks both the current tile and the target tile to decide whether the entity steps forward flat, raises one Z level (walking up a ramp), or falls one Z level (stepping onto a downward ramp from above).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Chunks
|
||||||
|
|
||||||
|
`chunk_t` (`chunk.h`) holds:
|
||||||
|
- `position` — its `chunkpos_t` in the world grid
|
||||||
|
- `tiles[CHUNK_TILE_COUNT]` — flat array of `tile_t`, indexed by `chunkGetTileIndex()`
|
||||||
|
- `vertices[CHUNK_VERTEX_COUNT]` / `mesh` — pre-baked mesh uploaded to GPU on load
|
||||||
|
- `entities[CHUNK_ENTITY_COUNT_MAX]` — indices into `ENTITIES[]` currently in this chunk (sentinel `0xFF`)
|
||||||
|
- `testColor` — temporary debug color (checkerboard), will be replaced by real tileset data
|
||||||
|
|
||||||
|
Tile layout within a chunk is `z * W*H + y * W + x` (Z-major, row-major in XY).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Map
|
||||||
|
|
||||||
|
`map_t MAP` (`map.h`) is the single global map instance.
|
||||||
|
|
||||||
|
```c
|
||||||
|
chunk_t chunks[MAP_CHUNK_COUNT]; // flat storage — index is NOT world position
|
||||||
|
chunk_t *chunkOrder[MAP_CHUNK_COUNT]; // draw-order sorted pointers into chunks[]
|
||||||
|
chunkpos_t chunkPosition; // world-grid origin of the loaded window
|
||||||
|
bool_t loaded;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Load / unload
|
||||||
|
|
||||||
|
`mapInit()` allocates chunk meshes and performs the initial load of all chunks in the starting window.
|
||||||
|
|
||||||
|
`mapPositionSet(newPos)` shifts the window:
|
||||||
|
1. Determines which of the `MAP_CHUNK_COUNT` slots remain within the new window vs. fall outside it.
|
||||||
|
2. Calls `mapChunkUnload()` on every chunk that falls outside (nulls its entity slots, zeroes `vertCount`).
|
||||||
|
3. Reuses freed slots for newly-in-range chunks; calls `mapChunkLoad()` on each.
|
||||||
|
4. Rebuilds `chunkOrder[]` in XYZ order for the new position.
|
||||||
|
|
||||||
|
### Chunk load (current stub)
|
||||||
|
|
||||||
|
`mapChunkLoad()` currently:
|
||||||
|
- Fills all tiles with `TILE_SHAPE_GROUND`
|
||||||
|
- Assigns a checkerboard debug color based on chunk XY parity
|
||||||
|
- Bakes a flat sprite-batch quad mesh for the z=0 layer and uploads it via `meshFlush()`
|
||||||
|
- Skips mesh generation for z > 0 chunks (they're empty)
|
||||||
|
|
||||||
|
### Tile lookup
|
||||||
|
|
||||||
|
```c
|
||||||
|
tile_t mapGetTile(const worldpos_t position);
|
||||||
|
```
|
||||||
|
|
||||||
|
Converts `position` to its chunk, looks up the chunk in `chunkOrder`, then indexes into `chunk->tiles[]`. Returns `TILE_SHAPE_NULL` for any out-of-bounds position or when the map is not loaded.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Camera
|
||||||
|
|
||||||
|
`rpgcamera_t RPG_CAMERA` (`rpgcamera.h`) has two modes:
|
||||||
|
|
||||||
|
```c
|
||||||
|
RPG_CAMERA_MODE_FREE // free worldpos; camera.free holds the position
|
||||||
|
RPG_CAMERA_MODE_FOLLOW_ENTITY // tracks ENTITIES[followEntityId]
|
||||||
|
```
|
||||||
|
|
||||||
|
`rpgCameraGetPosition()` returns the active world tile position in either mode.
|
||||||
|
|
||||||
|
The scene renderer (`sceneoverworld.c`) uses `rpgCameraGetPosition()` to build the `glm_lookat` view matrix. When following an entity, it sub-tile interpolates between `entity->lastPosition` and `entity->position` using `entity->animTime / ENTITY_ANIM_WALK_DURATION` to smooth movement.
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
# Save & Locale
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Save system (`src/dusk/save/`)
|
||||||
|
|
||||||
|
Slot-based persistent storage. Currently disabled in engine init (commented out in `engine.c`) — the system is fully implemented but not yet wired up.
|
||||||
|
|
||||||
|
### Slots
|
||||||
|
|
||||||
|
Up to `SAVE_FILE_COUNT_MAX` (3) save slots. The global `SAVE` holds all slots:
|
||||||
|
|
||||||
|
```c
|
||||||
|
saveInit();
|
||||||
|
saveExists(slot); // bool_t — check before load
|
||||||
|
saveLoad(slot); // read from platform storage → SAVE.files[slot]
|
||||||
|
saveWrite(slot); // write SAVE.files[slot] → platform storage
|
||||||
|
saveDelete(slot);
|
||||||
|
savefile_t *f = saveGet(slot);
|
||||||
|
saveDispose();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Save file format
|
||||||
|
|
||||||
|
`savefile_t` is the serialized struct stored per slot. Currently minimal:
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct {
|
||||||
|
char_t header[3]; // "DSK"
|
||||||
|
uint32_t version; // SAVE_FILE_VERSION = 1
|
||||||
|
bool_t exists;
|
||||||
|
} savefile_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
Extend this struct to add game-specific save data (player position, story flags, etc.).
|
||||||
|
|
||||||
|
### Stream serialization (`save/savestream.h`)
|
||||||
|
|
||||||
|
`savestream_t` is a cursor used to read/write a save slot's bytes. It CRC32-checksums all data written through it and verifies the checksum on read.
|
||||||
|
|
||||||
|
Write a save:
|
||||||
|
```c
|
||||||
|
savestream_t stream;
|
||||||
|
// (platform opens stream for slot)
|
||||||
|
saveFileWriteHeader(&stream, SAVE_FILE_HEADER);
|
||||||
|
saveFileWriteVersion(&stream, SAVE_FILE_VERSION);
|
||||||
|
saveFileWriteBool(&stream, myFlag);
|
||||||
|
saveFileWriteInt32(&stream, myInt);
|
||||||
|
saveFileWriteString(&stream, myString, sizeof(myString));
|
||||||
|
saveStreamFinalizeWriteImpl(&stream); // writes CRC
|
||||||
|
```
|
||||||
|
|
||||||
|
Read a save:
|
||||||
|
```c
|
||||||
|
saveFileReadHeader(&stream, headerBuf);
|
||||||
|
saveFileReadVersion(&stream, &version);
|
||||||
|
saveFileReadBool(&stream, &myFlag);
|
||||||
|
saveFileReadInt32(&stream, &myInt);
|
||||||
|
saveFileReadString(&stream, myString, sizeof(myString));
|
||||||
|
saveStreamVerifyChecksumImpl(&stream, slot); // returns error if CRC mismatch
|
||||||
|
```
|
||||||
|
|
||||||
|
All multi-byte values are stored in little-endian byte order. The `saveFile*` macros are thin wrappers over the `*Impl` functions that integrate `errorChain` — always use the macros.
|
||||||
|
|
||||||
|
### Platform backends
|
||||||
|
|
||||||
|
Each `src/dusk{platform}/save/` provides `saveplatform_t` (e.g. a file path on Linux, a memory-card handle on GameCube). The stream implementations (`savestream{platform}.c`) do the actual I/O.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Locale / i18n (`src/dusk/locale/`)
|
||||||
|
|
||||||
|
Translations are stored as GNU `.po` files in `assets/locale/`. Only `en_US.po` currently exists.
|
||||||
|
|
||||||
|
### Loading
|
||||||
|
|
||||||
|
`localemanager_t LOCALE` tracks the active locale and its in-progress asset entry:
|
||||||
|
|
||||||
|
```c
|
||||||
|
localeManagerInit(); // loads en_US by default
|
||||||
|
localeManagerSetLocale(&LOCALE_EN_US); // switch locale (async load)
|
||||||
|
localeManagerDispose();
|
||||||
|
```
|
||||||
|
|
||||||
|
`LOCALE_EN_US` is a predefined `localeinfo_t` constant (`name = "en-US"`, `file = "locale/en_US.po"`).
|
||||||
|
|
||||||
|
### Looking up strings
|
||||||
|
|
||||||
|
```c
|
||||||
|
char_t buf[128];
|
||||||
|
localeManagerGetText("my.key", buf, sizeof(buf), 1, /* format args */ );
|
||||||
|
```
|
||||||
|
|
||||||
|
The macro handles plural forms and `printf`-style format arguments. Pass plural `1` for singular, any other value for plural.
|
||||||
|
|
||||||
|
`assetlocaleloader.c` parses the `.po` format (msgid / msgstr pairs) into a key→string table during the async asset load phase.
|
||||||
@@ -0,0 +1,382 @@
|
|||||||
|
# Coding Style
|
||||||
|
|
||||||
|
All source is C11. Everything below is derived from the existing codebase — match it exactly.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## File structure
|
||||||
|
|
||||||
|
### Headers (`.h`)
|
||||||
|
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "direct/dependency.h"
|
||||||
|
```
|
||||||
|
|
||||||
|
- `#pragma once` always, never `#ifndef` guards.
|
||||||
|
- No blank line between the license block and `#pragma once`.
|
||||||
|
- One blank line between `#pragma once` and the first `#include`.
|
||||||
|
|
||||||
|
### Sources (`.c`)
|
||||||
|
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "thisfile.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
```
|
||||||
|
|
||||||
|
- First include is always the matching `.h` for this `.c` file.
|
||||||
|
- Remaining includes follow with no separator unless logically grouped (then one blank line between groups — see [Include order](#include-order)).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Include order
|
||||||
|
|
||||||
|
In `.c` files, include in this order with a blank line between each group:
|
||||||
|
|
||||||
|
1. The matching header (e.g. `#include "entity.h"`)
|
||||||
|
2. Core utilities (`assert/assert.h`, `util/memory.h`, `util/math.h`, etc.)
|
||||||
|
3. Engine subsystems (`display/...`, `input/...`, etc.)
|
||||||
|
4. Domain subsystems (`rpg/...`, `scene/...`, etc.)
|
||||||
|
|
||||||
|
In `.h` files, only include what the header directly requires. Never include more than necessary to make the type definitions in that header compile.
|
||||||
|
|
||||||
|
All include paths are relative to `src/dusk/` (the root include directory). Use the full path:
|
||||||
|
```c
|
||||||
|
#include "rpg/overworld/map.h" // correct
|
||||||
|
#include "map.h" // wrong
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Line length
|
||||||
|
|
||||||
|
80-character limit. Break before it, not after.
|
||||||
|
|
||||||
|
Multi-parameter function signatures break one param per line, with 2-space indent, closing `)` on its own line before `{`:
|
||||||
|
|
||||||
|
```c
|
||||||
|
errorret_t textureInit(
|
||||||
|
texture_t *texture,
|
||||||
|
const int32_t width,
|
||||||
|
const int32_t height,
|
||||||
|
const textureformat_t format,
|
||||||
|
const texturedata_t data
|
||||||
|
) {
|
||||||
|
```
|
||||||
|
|
||||||
|
Same rule for calls that don't fit on one line:
|
||||||
|
|
||||||
|
```c
|
||||||
|
assertTrue(
|
||||||
|
data.paletted.palette->count ==
|
||||||
|
mathNextPowTwo(data.paletted.palette->count),
|
||||||
|
"Palette color count must be a power of 2"
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Indentation and spacing
|
||||||
|
|
||||||
|
- **2 spaces** per indent level. No tabs.
|
||||||
|
- No space between a control keyword and its `(`:
|
||||||
|
```c
|
||||||
|
if(x) { // correct
|
||||||
|
if (x) { // wrong
|
||||||
|
```
|
||||||
|
- No space between a function name and its `(` in either declarations or calls.
|
||||||
|
- Opening brace on the same line:
|
||||||
|
```c
|
||||||
|
void entityUpdate(entity_t *entity) {
|
||||||
|
if(x) {
|
||||||
|
for(int i = 0; i < n; i++) {
|
||||||
|
```
|
||||||
|
- Closing brace always on its own line, except `} else {` and `} while(...)`.
|
||||||
|
- One blank line between function definitions in `.c` files.
|
||||||
|
- No trailing whitespace.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Naming
|
||||||
|
|
||||||
|
| Kind | Convention | Example |
|
||||||
|
|---|---|---|
|
||||||
|
| Types (struct/union/typedef) | `snake_case_t` | `entity_t`, `worldpos_t` |
|
||||||
|
| Struct tags | `struct name_s` | `struct entity_s` |
|
||||||
|
| Union tags | `union name_u` | `union texturedata_u` |
|
||||||
|
| Enum tags (when typedef'd separately) | `name_enum_t` | `entitytype_enum_t` |
|
||||||
|
| Functions | `subsystemVerb` (camelCase, noun-first) | `entityInit`, `mapGetTile` |
|
||||||
|
| Macro constants | `UPPER_SNAKE_CASE` | `CHUNK_WIDTH`, `FIXED_ONE` |
|
||||||
|
| Function-like macros | `camelCase` (same as functions) | `errorThrow`, `assertNotNull` |
|
||||||
|
| Global subsystem instances | `UPPER_SNAKE_CASE` | `ENGINE`, `MAP`, `ENTITIES` |
|
||||||
|
| Local variables | `camelCase` | `tileNew`, `spriteCount` |
|
||||||
|
| Parameters | `camelCase` | `texture`, `worldPos` |
|
||||||
|
|
||||||
|
Subsystem prefix always comes first in function names: `textureInit`, `shaderBind`, `spriteBatchFlush`. The verb describes the action: `Init`, `Update`, `Dispose`, `Get`, `Set`, `Is`, etc.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Typedefs
|
||||||
|
|
||||||
|
### Structs
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct {
|
||||||
|
uint8_t id;
|
||||||
|
entitytype_t type;
|
||||||
|
} entity_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
Use a named tag (`struct entity_s`) only when forward declaration is required:
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct entity_s {
|
||||||
|
// ...
|
||||||
|
} entity_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Unions
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef union texturedata_u {
|
||||||
|
struct {
|
||||||
|
uint8_t *indices;
|
||||||
|
palette_t *palette;
|
||||||
|
} paletted;
|
||||||
|
color_t *rgbaColors;
|
||||||
|
} texturedata_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Enums
|
||||||
|
|
||||||
|
When the enum values need to be a compact integer (common for arrays and flags), declare the enum separately and typedef an integer type:
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef enum {
|
||||||
|
ENTITY_TYPE_NULL,
|
||||||
|
ENTITY_TYPE_PLAYER,
|
||||||
|
ENTITY_TYPE_NPC,
|
||||||
|
ENTITY_TYPE_COUNT
|
||||||
|
} entitytype_enum_t;
|
||||||
|
|
||||||
|
typedef uint8_t entitytype_t; // actual type used everywhere
|
||||||
|
```
|
||||||
|
|
||||||
|
Always include `_NULL` as the first value (zero) and `_COUNT` as the last value.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## `#define` constants
|
||||||
|
|
||||||
|
All-caps, underscores. Wrap multi-token expressions in parentheses:
|
||||||
|
|
||||||
|
```c
|
||||||
|
#define CHUNK_WIDTH 16
|
||||||
|
#define CHUNK_HEIGHT CHUNK_WIDTH
|
||||||
|
#define CHUNK_TILE_COUNT (CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH)
|
||||||
|
```
|
||||||
|
|
||||||
|
Multi-line macros: backslash continuation, body indented 2 spaces, closing line has no backslash:
|
||||||
|
|
||||||
|
```c
|
||||||
|
#define errorThrow(message, ...) \
|
||||||
|
return errorThrowImpl(\
|
||||||
|
&ERROR_STATE, ERROR_NOT_OK, __FILE__, __func__, __LINE__, (message), \
|
||||||
|
##__VA_ARGS__ \
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## `const` usage
|
||||||
|
|
||||||
|
Mark every pointer and value parameter `const` unless the function modifies it:
|
||||||
|
|
||||||
|
```c
|
||||||
|
void entityTurn(entity_t *entity, const entitydir_t direction);
|
||||||
|
errorret_t textureInit(texture_t *texture, const int32_t width, ...);
|
||||||
|
```
|
||||||
|
|
||||||
|
`entity_t *entity` is non-const because the function writes to it; `direction` is const because it is read-only.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## `void` in no-argument functions
|
||||||
|
|
||||||
|
Use `(void)` in definitions and declarations of zero-parameter functions:
|
||||||
|
|
||||||
|
```c
|
||||||
|
errorret_t engineUpdate(void);
|
||||||
|
errorret_t spriteBatchFlush(void);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Global subsystem state
|
||||||
|
|
||||||
|
Each subsystem exposes a single global instance declared `extern` in the header and defined (once) in the `.c` file:
|
||||||
|
|
||||||
|
```c
|
||||||
|
// entity.h
|
||||||
|
extern entity_t ENTITIES[ENTITY_COUNT];
|
||||||
|
|
||||||
|
// entity.c
|
||||||
|
entity_t ENTITIES[ENTITY_COUNT];
|
||||||
|
```
|
||||||
|
|
||||||
|
Never define a subsystem global as `static` in a header.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Assertions
|
||||||
|
|
||||||
|
Place assertions at the very top of a function, before any logic:
|
||||||
|
|
||||||
|
```c
|
||||||
|
void entityInit(entity_t *entity, const entitytype_t type) {
|
||||||
|
assertNotNull(entity, "Entity pointer cannot be NULL");
|
||||||
|
assertTrue(type < ENTITY_TYPE_COUNT, "Invalid entity type");
|
||||||
|
// ... actual logic
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Available assertion macros (from `assert/assert.h`):
|
||||||
|
- `assertNotNull(ptr, msg)`
|
||||||
|
- `assertNull(ptr, msg)`
|
||||||
|
- `assertTrue(expr, msg)`
|
||||||
|
- `assertFalse(expr, msg)`
|
||||||
|
- `assertUnreachable(msg)`
|
||||||
|
- `assertStringEqual(a, b, msg)`
|
||||||
|
- `assertIsMainThread(msg)` / `assertNotMainThread(msg)`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Error handling style
|
||||||
|
|
||||||
|
Functions that can fail return `errorret_t`. Three patterns:
|
||||||
|
|
||||||
|
```c
|
||||||
|
// Propagate a child call's failure and return from this function:
|
||||||
|
errorChain(someCall());
|
||||||
|
|
||||||
|
// Return success:
|
||||||
|
errorOk();
|
||||||
|
|
||||||
|
// Return failure:
|
||||||
|
errorThrow("Descriptive message %s", variable);
|
||||||
|
```
|
||||||
|
|
||||||
|
`errorChain` is used inline — do not capture the result first:
|
||||||
|
```c
|
||||||
|
errorChain(textureInitPlatform(texture, width, height, format, data)); // correct
|
||||||
|
errorret_t r = textureInitPlatform(...); errorChain(r); // wrong
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Struct initialization
|
||||||
|
|
||||||
|
Use C99 designated initializers for any struct literal with more than one field:
|
||||||
|
|
||||||
|
```c
|
||||||
|
static const entitycallback_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT] = {
|
||||||
|
[ENTITY_TYPE_NULL] = { NULL },
|
||||||
|
|
||||||
|
[ENTITY_TYPE_PLAYER] = {
|
||||||
|
.init = playerInit,
|
||||||
|
.movement = playerInput
|
||||||
|
},
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
```c
|
||||||
|
shadermaterial_t material = {
|
||||||
|
.unlit = {
|
||||||
|
.color = COLOR_WHITE,
|
||||||
|
.texture = NULL
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Fixed-size array iteration
|
||||||
|
|
||||||
|
Prefer pointer arithmetic with `do/while` over index loops for iterating through fixed global arrays:
|
||||||
|
|
||||||
|
```c
|
||||||
|
entity_t *ent = ENTITIES;
|
||||||
|
do {
|
||||||
|
if(ent->type == ENTITY_TYPE_NULL) continue;
|
||||||
|
// ...
|
||||||
|
} while(++ent, ent < &ENTITIES[ENTITY_COUNT]);
|
||||||
|
```
|
||||||
|
|
||||||
|
Use `for` loops when an index variable is actually needed.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Comments
|
||||||
|
|
||||||
|
Comments explain *why*, not *what*. One short inline comment is fine; multi-line block comments for non-obvious invariants only.
|
||||||
|
|
||||||
|
```c
|
||||||
|
// Walking up a ramp — only the direction the ramp faces is valid.
|
||||||
|
if(tileIsRamp(tileCurrent) && ...) {
|
||||||
|
```
|
||||||
|
|
||||||
|
Section labels inside long functions are acceptable:
|
||||||
|
|
||||||
|
```c
|
||||||
|
// Chunks
|
||||||
|
{
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entities
|
||||||
|
{
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Doc comments on public functions use Javadoc style with `@param` / `@return`:
|
||||||
|
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* Gets the tile at the given world position.
|
||||||
|
*
|
||||||
|
* @param position The world position.
|
||||||
|
* @return The tile at that position, or TILE_NULL if the chunk is unloaded.
|
||||||
|
*/
|
||||||
|
tile_t mapGetTile(const worldpos_t position);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Platform-conditional code
|
||||||
|
|
||||||
|
Use the `DUSK_*` compile-definition macros set by `cmake/targets/<target>.cmake`:
|
||||||
|
|
||||||
|
```c
|
||||||
|
#ifdef DUSK_THREAD_PTHREAD
|
||||||
|
#include "thread/thread.h"
|
||||||
|
extern pthread_t ASSERT_MAIN_THREAD_ID;
|
||||||
|
#endif
|
||||||
|
```
|
||||||
|
|
||||||
|
Never use `#ifdef __linux__`, `#ifdef _WIN32`, etc. directly — go through the engine macros.
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
# Engine Systems
|
||||||
|
|
||||||
|
Smaller systems that support the engine but don't warrant their own file each.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Time (`src/dusk/time/`)
|
||||||
|
|
||||||
|
`dusktime_t TIME` tracks fixed and dynamic delta time.
|
||||||
|
|
||||||
|
```c
|
||||||
|
TIME.delta // fixed_t: always DUSK_TIME_STEP (16ms default) on fixed-rate platforms
|
||||||
|
TIME.time // fixed_t: total elapsed time in seconds
|
||||||
|
```
|
||||||
|
|
||||||
|
On platforms with `DUSK_TIME_DYNAMIC` (Linux/SDL2):
|
||||||
|
```c
|
||||||
|
TIME.dynamicDelta // fixed_t: actual time since last frame
|
||||||
|
TIME.dynamicTime // fixed_t: total elapsed (dynamic)
|
||||||
|
TIME.dynamicUpdate // bool_t: true when a real tick occurred
|
||||||
|
```
|
||||||
|
|
||||||
|
Call `timeUpdate()` once per frame (before input/logic). `timeGetEpoch()` returns the current wall-clock time as a `dusktimeepoch_t`.
|
||||||
|
|
||||||
|
### Epoch time (`time/timeepoch.h`)
|
||||||
|
|
||||||
|
`dusktimeepoch_t` stores a Unix timestamp (double) with timezone offset. Utilities:
|
||||||
|
|
||||||
|
```c
|
||||||
|
dusktimeepoch_t e = timeGetEpoch();
|
||||||
|
timeEpochGetHours(e) // 0–23
|
||||||
|
timeEpochGetMinutes(e) // 0–59
|
||||||
|
timeEpochGetSeconds(e) // 0–59
|
||||||
|
timeEpochGetDayOfMonth(e) // 0–30
|
||||||
|
timeEpochGetMonth(e) // 0–11
|
||||||
|
timeEpochGetYear(e)
|
||||||
|
|
||||||
|
// Format: %Y year, %m month, %d day, %H hour, %M minute, %S second
|
||||||
|
timeEpochFormat(e, "%Y-%m-%d %H:%M:%S", buf, sizeof(buf));
|
||||||
|
|
||||||
|
// Compare: returns -1, 0, 1
|
||||||
|
timeEpochCompare(a, b);
|
||||||
|
|
||||||
|
// Timezone shift:
|
||||||
|
dusktimeepoch_t local = timeEpochSwitchTimeZone(e, offsetHours);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Thread (`src/dusk/thread/`)
|
||||||
|
|
||||||
|
Currently only the pthread backend (`DUSK_THREAD_PTHREAD`) exists. Thread objects are `thread_t` — used primarily by the asset loader.
|
||||||
|
|
||||||
|
```c
|
||||||
|
threadInit(&thread, myCallback); // myCallback: void (*)(thread_t *)
|
||||||
|
threadStart(&thread); // blocks until thread is RUNNING
|
||||||
|
threadStop(&thread); // requests stop, blocks until STOPPED
|
||||||
|
|
||||||
|
// Inside the thread callback:
|
||||||
|
while(!threadShouldStop(&thread)) { /* work */ }
|
||||||
|
```
|
||||||
|
|
||||||
|
State flow: `STOPPED → STARTING → RUNNING → (STOP_REQUESTED) → STOPPED`.
|
||||||
|
|
||||||
|
### Mutex (`thread/threadmutex.h`)
|
||||||
|
|
||||||
|
```c
|
||||||
|
threadMutexInit(&lock);
|
||||||
|
threadMutexLock(&lock);
|
||||||
|
threadMutexUnlock(&lock);
|
||||||
|
threadMutexTryLock(&lock); // non-blocking; returns false if already held
|
||||||
|
threadMutexWaitLock(&lock); // release lock and sleep until signalled
|
||||||
|
threadMutexSignal(&lock); // wake a thread waiting on this mutex
|
||||||
|
threadMutexDispose(&lock);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Thread-local storage
|
||||||
|
|
||||||
|
`THREAD_LOCAL` expands to `__thread` (GCC) on pthread platforms. Used for the per-thread error state (`ERROR_STATE` in `error/error.h`).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Event (`src/dusk/event/`)
|
||||||
|
|
||||||
|
A fixed-capacity multicast callback list.
|
||||||
|
|
||||||
|
```c
|
||||||
|
// Declare backing arrays (choose a size):
|
||||||
|
eventcallback_t cbs[4];
|
||||||
|
void *users[4];
|
||||||
|
event_t myEvent;
|
||||||
|
eventInit(&myEvent, cbs, users, 4);
|
||||||
|
|
||||||
|
eventSubscribe(&myEvent, myCallback, myUser);
|
||||||
|
eventUnsubscribe(&myEvent, myCallback);
|
||||||
|
eventInvoke(&myEvent, params); // calls all subscribers; params passed as-is
|
||||||
|
```
|
||||||
|
|
||||||
|
Callback signature: `void cb(void *params, void *user)`.
|
||||||
|
|
||||||
|
`event_t` does not own its callback/user arrays. Always declare them alongside the event in the same struct or as static arrays.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Console (`src/dusk/console/`)
|
||||||
|
|
||||||
|
`CONSOLE` is a scrolling in-game terminal for debug output. On POSIX platforms (`DUSK_CONSOLE_POSIX`) it also polls stdin in a thread so commands can be typed during a running session.
|
||||||
|
|
||||||
|
```c
|
||||||
|
consolePrint("Value is %d", x); // printf-style; thread-safe
|
||||||
|
consoleDraw(); // renders visible history lines to screen
|
||||||
|
```
|
||||||
|
|
||||||
|
Configuration constants (`consoledefs.h`):
|
||||||
|
- `CONSOLE_LINE_MAX` — 512 chars per line
|
||||||
|
- `CONSOLE_HISTORY_MAX` — 16 lines of scrollback
|
||||||
|
- `CONSOLE_EXEC_BUFFER_MAX` — 32 pending exec commands
|
||||||
|
|
||||||
|
`CONSOLE.visible` controls whether `consoleDraw()` actually renders anything.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Log (`src/dusk/log/`)
|
||||||
|
|
||||||
|
Two simple output functions, implemented per-platform:
|
||||||
|
|
||||||
|
```c
|
||||||
|
logDebug("format %s", arg); // debug output (stdout on Linux, debug channel on consoles)
|
||||||
|
logError("format %s", arg); // error output; may pause execution on some platforms
|
||||||
|
```
|
||||||
|
|
||||||
|
These go directly to the platform's native output and are not buffered by the console history.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## System / Platform (`src/dusk/system/`)
|
||||||
|
|
||||||
|
`systemInit()` runs platform-specific startup (e.g. Wii PAD init, PSP kernel setup). Must be the first call in `engineInit()`.
|
||||||
|
|
||||||
|
```c
|
||||||
|
systemplatform_t p = systemGetPlatform(); // SYSTEM_PLATFORM_LINUX, _PSP, etc.
|
||||||
|
systemdialogtype_t d = systemGetActiveDialogType();
|
||||||
|
// SYSTEM_DIALOG_TYPE_NONE / RENDER_BLOCKING / TICK_BLOCKING
|
||||||
|
```
|
||||||
|
|
||||||
|
The full platform list is defined via X-macro in `system/systemplatformlist.h`:
|
||||||
|
|
||||||
|
| Constant | Value |
|
||||||
|
|---|---|
|
||||||
|
| `SYSTEM_PLATFORM_LINUX` | 0 |
|
||||||
|
| `SYSTEM_PLATFORM_KNULLI` | 1 |
|
||||||
|
| `SYSTEM_PLATFORM_PSP` | 2 |
|
||||||
|
| `SYSTEM_PLATFORM_GAMECUBE` | 3 |
|
||||||
|
| `SYSTEM_PLATFORM_WII` | 4 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Network (`src/dusk/network/`)
|
||||||
|
|
||||||
|
`network_t NETWORK` manages platform connection state. The engine calls `networkInit` / `networkUpdate` / `networkDispose`; game code uses the request API:
|
||||||
|
|
||||||
|
```c
|
||||||
|
networkRequestConnection(onConnected, onFailed, onDisconnect, user);
|
||||||
|
networkRequestDisconnection(onComplete, user);
|
||||||
|
networkIsConnected(); // bool_t
|
||||||
|
```
|
||||||
|
|
||||||
|
State machine: `DISCONNECTED → CONNECTING → CONNECTED → DISCONNECTING → DISCONNECTED`.
|
||||||
|
|
||||||
|
Network address info (after connection):
|
||||||
|
```c
|
||||||
|
networkinfo_t info = networkGetInfo();
|
||||||
|
// info.type = NETWORK_TYPE_IPV4 / IPV6
|
||||||
|
// info.ipv4.ip[4] or info.ipv6.ip[16]
|
||||||
|
```
|
||||||
|
|
||||||
|
Platform backends implement `networkPlatformInit/Update/Dispose/IsConnected`. Currently only Linux (socket-based) and PSP/Vita are implemented.
|
||||||
+123
@@ -0,0 +1,123 @@
|
|||||||
|
# UI System
|
||||||
|
|
||||||
|
Source: `src/dusk/ui/`
|
||||||
|
|
||||||
|
The UI system is an immediate-mode layer drawn on top of the game scene each frame. Elements register themselves; `uiUpdate()` ticks all elements and `uiRender()` draws them. All coordinates are in screen pixels.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Lifecycle
|
||||||
|
|
||||||
|
```
|
||||||
|
uiInit() → uiTextboxInit() // init order matters — textbox depends on display being ready
|
||||||
|
uiUpdate() // each frame, before rendering
|
||||||
|
uiRender() // each frame, after scene render
|
||||||
|
uiDispose()
|
||||||
|
uiTextboxDispose()
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Element registration (`ui/uielement.h`)
|
||||||
|
|
||||||
|
Elements are stored in `UI_ELEMENTS[]`. Each has a type and a `draw` callback:
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct {
|
||||||
|
uielementtype_t type;
|
||||||
|
errorret_t (*draw)();
|
||||||
|
} uielement_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
Currently `UI_ELEMENT_TYPE_NATIVE` elements call their `draw` function directly. New debug/HUD elements are registered by adding to this array.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Textbox (`ui/uitextbox.h`)
|
||||||
|
|
||||||
|
`UI_TEXTBOX` is the global dialogue box. It word-wraps text, paginates it, and plays a typewriter scroll effect.
|
||||||
|
|
||||||
|
```c
|
||||||
|
uiTextboxSetText("Long dialogue string..."); // wraps to charsPerLine, paginates
|
||||||
|
uiTextboxUpdate(); // each frame: advance scroll, check input
|
||||||
|
uiTextboxDraw(); // draw box + text
|
||||||
|
|
||||||
|
// Pagination:
|
||||||
|
uiTextboxPageIsComplete() // true when all chars of current page are visible
|
||||||
|
uiTextboxHasNextPage()
|
||||||
|
uiTextboxNextPage()
|
||||||
|
|
||||||
|
// Subscibe to page events:
|
||||||
|
eventSubscribe(&UI_TEXTBOX.onPageComplete, cb, user);
|
||||||
|
eventSubscribe(&UI_TEXTBOX.onLastPage, cb, user);
|
||||||
|
```
|
||||||
|
|
||||||
|
`UI_TEXTBOX.advanceAction` defaults to the input action that advances dialogue — set it before calling `uiTextboxInit()` if the default doesn't suit.
|
||||||
|
|
||||||
|
Layout constants:
|
||||||
|
- `UI_TEXTBOX_TEXT_MAX` — 1024 chars
|
||||||
|
- `UI_TEXTBOX_LINES_MAX` — 64 lines
|
||||||
|
- `UI_TEXTBOX_LINES_PER_PAGE_MAX` — 3 lines visible at once
|
||||||
|
- `UI_TEXTBOX_SCROLL_CHARS_PER_TICK` — 1 char per tick (typewriter speed)
|
||||||
|
|
||||||
|
The textbox uses `UI_TEXTBOX.frame` (a `uiframe_t`) for its border rendering and `UI_TEXTBOX.font` for text.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## UI Frame (`ui/uiframe.h`)
|
||||||
|
|
||||||
|
9-slice bordered box rendered with a tileset:
|
||||||
|
|
||||||
|
```c
|
||||||
|
uiFrameInit(&frame);
|
||||||
|
uiFrameDraw(&frame, x, y, width, height);
|
||||||
|
uiFrameDispose(&frame);
|
||||||
|
```
|
||||||
|
|
||||||
|
The tileset is loaded from the asset system during `uiFrameInit()`. The 9 slices are arranged in the tileset grid as: top-left corner, top edge, top-right corner, left edge, fill, right edge, bottom-left, bottom edge, bottom-right.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Loading overlay (`ui/uiloading.h`)
|
||||||
|
|
||||||
|
`UI_LOADING` is a full-screen loading indicator with fade-in/fade-out transitions:
|
||||||
|
|
||||||
|
```c
|
||||||
|
uiLoadingShow(onShownCallback, user); // fade in; calls callback when fully opaque
|
||||||
|
uiLoadingHide(onHiddenCallback, user); // fade out; calls callback when fully transparent
|
||||||
|
uiLoadingUpdate(delta); // each frame
|
||||||
|
uiLoadingDraw(); // each frame, over everything
|
||||||
|
```
|
||||||
|
|
||||||
|
`UI_LOADING_FADE_DURATION` is `FIXED(0.5f)` seconds. Subscribe to `UI_LOADING.onTransitionEnd` for completion events.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Full-box overlay (`ui/uifullbox.h`)
|
||||||
|
|
||||||
|
Two global full-screen color overlays: `UI_FULLBOX_UNDER` (drawn before game content) and `UI_FULLBOX_OVER` (drawn after). Used for scene transitions (fade to black, etc.):
|
||||||
|
|
||||||
|
```c
|
||||||
|
uiFullboxTransition(
|
||||||
|
&UI_FULLBOX_OVER,
|
||||||
|
COLOR_TRANSPARENT, COLOR_BLACK,
|
||||||
|
FIXED(0.5f),
|
||||||
|
EASING_IN_OUT_CUBIC
|
||||||
|
);
|
||||||
|
eventSubscribe(&UI_FULLBOX_OVER.onTransitionEnd, myCallback, NULL);
|
||||||
|
|
||||||
|
uiFullboxUnderDraw(); // draw under layer
|
||||||
|
uiFullboxOverDraw(); // draw over layer
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## FPS counter (`ui/uifps.h`)
|
||||||
|
|
||||||
|
`UIFPS` tracks a rolling average FPS. `uiFPSDraw()` renders it in the corner. Currently drawn as part of the debug HUD (not wired to an element slot).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Player position HUD (`ui/uiplayerpos.h`)
|
||||||
|
|
||||||
|
`uiplayerpos.c` draws the player's current world tile coordinates. Debug overlay, currently drawn directly in the scene render.
|
||||||
+162
@@ -0,0 +1,162 @@
|
|||||||
|
# Utilities
|
||||||
|
|
||||||
|
Source: `src/dusk/util/`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## String (`util/string.h`)
|
||||||
|
|
||||||
|
**Always use these instead of stdlib equivalents** (`strcmp`, `strcpy`, `sprintf`, etc.).
|
||||||
|
|
||||||
|
```c
|
||||||
|
stringCopy(dest, src, destSize); // safe strncpy; always null-terminates
|
||||||
|
stringCompare(a, b); // -1 / 0 / 1
|
||||||
|
stringEquals(a, b); // bool_t
|
||||||
|
stringCompareInsensitive(a, b); // case-insensitive -1 / 0 / 1
|
||||||
|
stringTrim(str); // in-place strip leading/trailing whitespace
|
||||||
|
stringFindLastChar(str, c); // last occurrence pointer or NULL
|
||||||
|
stringFormat(dest, destSize, fmt, ...); // snprintf wrapper; pass NULL dest to get length
|
||||||
|
stringFormatVA(dest, destSize, fmt, args); // va_list version
|
||||||
|
|
||||||
|
stringIsWhitespace(c); // bool_t
|
||||||
|
|
||||||
|
// Parse:
|
||||||
|
stringToI32(str, &out) → bool_t
|
||||||
|
stringToI64(str, &out) → bool_t
|
||||||
|
stringToI16(str, &out) → bool_t
|
||||||
|
stringToU16(str, &out) → bool_t
|
||||||
|
stringToF32(str, &out) → bool_t
|
||||||
|
|
||||||
|
// Suffix checks:
|
||||||
|
stringEndsWith(str, suffix) → bool_t
|
||||||
|
stringEndsWithCaseInsensitive(str, suffix) → bool_t
|
||||||
|
stringIncludesString(haystack, needle) → bool_t
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Memory (`util/memory.h`)
|
||||||
|
|
||||||
|
**Always use these instead of `malloc`/`free`/`memcpy`/`memset` directly.**
|
||||||
|
|
||||||
|
```c
|
||||||
|
memoryAllocate(size) → void * // malloc + tracks pointer count
|
||||||
|
memoryAlign(alignment, size) → void * // aligned malloc
|
||||||
|
memoryFree(ptr) // free + decrements count
|
||||||
|
memoryReallocate(&ptr, size) // realloc
|
||||||
|
memoryResize(&ptr, oldSize, newSize) // realloc + copy (safe reshape)
|
||||||
|
memoryTrack(ptr) // track externally-malloc'd pointer
|
||||||
|
|
||||||
|
memoryCopy(dest, src, size) // memcpy
|
||||||
|
memoryMove(dest, src, size) // memmove
|
||||||
|
memorySet(dest, value, size) // memset
|
||||||
|
memoryZero(dest, size) // memset 0
|
||||||
|
memoryCompare(a, b, size) → int_t // memcmp
|
||||||
|
|
||||||
|
// Useful for uploading vertex data with different source/dest layouts:
|
||||||
|
memoryCopyInterleaved(dest, destStride, src, srcStride, elementSize, count);
|
||||||
|
memoryCopyRangeSafe(dest, start, end, sizeMax); // copy with bounds check
|
||||||
|
|
||||||
|
memoryGetAllocatedCount() → size_t // current malloc'd block count
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Math (`util/math.h`)
|
||||||
|
|
||||||
|
```c
|
||||||
|
mathNextPowTwo(value) → uint32_t // next power of two >= value
|
||||||
|
mathMax(a, b) // macro
|
||||||
|
mathMin(a, b) // macro
|
||||||
|
mathClamp(x, lower, upper) // macro
|
||||||
|
mathAbs(amt) // macro
|
||||||
|
mathModFloat(x, y) → float_t // always non-negative modulo
|
||||||
|
mathLerp(a, b, t) → float_t // linear interpolation (floats)
|
||||||
|
```
|
||||||
|
|
||||||
|
For fixed-point lerp use `fixedLerp(a, b, t)` from `util/fixed.h`.
|
||||||
|
|
||||||
|
`MATH_PI` is defined as `M_PI`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Fixed-point (`util/fixed.h`)
|
||||||
|
|
||||||
|
Q24.8 format: 24-bit integer part, 8-bit fractional part (`int32_t`). See [architecture.md](architecture.md#fixed-point-math) for the full API.
|
||||||
|
|
||||||
|
Quick reference:
|
||||||
|
```c
|
||||||
|
FIXED(1.5f) // compile-time literal
|
||||||
|
fixedFromI32(3) // runtime int → fixed
|
||||||
|
fixedToFloat(f) // fixed → float (only for GL/platform APIs)
|
||||||
|
fixedMul(a, b) // multiplication (not just addition)
|
||||||
|
fixedDiv(a, b) // division
|
||||||
|
fixedLerp(a, b, t) // lerp where t ∈ [0, FIXED_ONE]
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Array (`util/array.h`)
|
||||||
|
|
||||||
|
```c
|
||||||
|
arrayReverse(array, count, size); // in-place reverse; size = sizeof(element)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Sort (`util/sort.h`)
|
||||||
|
|
||||||
|
```c
|
||||||
|
sortQuick(array, count, size, compare); // quicksort
|
||||||
|
sortBubble(array, count, size, compare); // bubble sort (small arrays)
|
||||||
|
sort(array, count, size, compare); // macro alias for sortQuick
|
||||||
|
|
||||||
|
// Convenience uint8_t sorter:
|
||||||
|
sortArrayU8(array, count);
|
||||||
|
int sortArrayU8Compare(const void *a, const void *b); // comparator
|
||||||
|
```
|
||||||
|
|
||||||
|
`sortcompare_t` matches `qsort` comparator signature: `int (*)(const void *, const void *)`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Reference counting (`util/ref.h`)
|
||||||
|
|
||||||
|
```c
|
||||||
|
refInit(&ref, dataPtr, onLock, onUnlock, onAllUnlocked);
|
||||||
|
refLock(&ref); // increments count, calls onLock
|
||||||
|
bool_t hit_zero = refUnlock(&ref); // decrements; calls onAllUnlocked when 0
|
||||||
|
```
|
||||||
|
|
||||||
|
Used internally by the asset entry system to track how many callers hold a reference to a loaded asset entry.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## CRC32 (`util/crypt.h`)
|
||||||
|
|
||||||
|
```c
|
||||||
|
// One-shot:
|
||||||
|
uint32_t crc = cryptCRC32(data, size);
|
||||||
|
|
||||||
|
// Streaming:
|
||||||
|
uint32_t acc = cryptCRC32Begin();
|
||||||
|
cryptCRC32Update(&acc, chunk1, len1);
|
||||||
|
cryptCRC32Update(&acc, chunk2, len2);
|
||||||
|
uint32_t final = cryptCRC32End(acc);
|
||||||
|
```
|
||||||
|
|
||||||
|
Used by the save stream to checksum save files.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Endian (`util/endian.h`)
|
||||||
|
|
||||||
|
All serialized data (save files, asset headers) is little-endian. Convert to/from host byte order:
|
||||||
|
|
||||||
|
```c
|
||||||
|
uint32_t val = endianLittleToHost32(rawU32);
|
||||||
|
uint16_t val = endianLittleToHost16(rawU16);
|
||||||
|
float_t val = endianLittleToHostFloat(rawFloat);
|
||||||
|
```
|
||||||
|
|
||||||
|
`isHostLittleEndian()` returns a bool at runtime. The compile-time defines `DUSK_PLATFORM_ENDIAN_LITTLE` / `DUSK_PLATFORM_ENDIAN_BIG` are set by `cmake/targets/<target>.cmake`.
|
||||||
@@ -1,11 +1,8 @@
|
|||||||
name: Build Dusk
|
name: Build Dusk
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
tags:
|
||||||
- main
|
- '*'
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
jobs:
|
jobs:
|
||||||
run-tests:
|
run-tests:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -144,7 +141,7 @@ jobs:
|
|||||||
- name: Copy output files.
|
- name: Copy output files.
|
||||||
run: |
|
run: |
|
||||||
mkdir -p ./git-artifcats/Dusk/apps/Dusk
|
mkdir -p ./git-artifcats/Dusk/apps/Dusk
|
||||||
cp build-wii/Dusk.dol ./git-artifcats/Dusk/apps/Dusk/boot.dol
|
cp build-wii/boot.dol ./git-artifcats/Dusk/apps/Dusk/boot.dol
|
||||||
cp build-wii/dusk.dsk ./git-artifcats/Dusk/apps/Dusk/dusk.dsk
|
cp build-wii/dusk.dsk ./git-artifcats/Dusk/apps/Dusk/dusk.dsk
|
||||||
cp build-wii/meta.xml ./git-artifcats/Dusk/apps/Dusk/meta.xml
|
cp build-wii/meta.xml ./git-artifcats/Dusk/apps/Dusk/meta.xml
|
||||||
- name: Upload Wii binary
|
- name: Upload Wii binary
|
||||||
|
|||||||
@@ -106,3 +106,4 @@ yarn.lock
|
|||||||
/build*
|
/build*
|
||||||
/assets/test
|
/assets/test
|
||||||
/tools_old
|
/tools_old
|
||||||
|
/assets/test.png
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
# CLAUDE.md
|
||||||
|
|
||||||
|
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||||
|
|
||||||
|
## Project
|
||||||
|
|
||||||
|
Dusk is a C11 RPG game targeting resource-constrained hardware (PSP, GameCube, Wii, PS Vita, Knulli handhelds) and Linux/OpenGL. All game code lives in `src/dusk/`; platform-specific backends live in `src/dusk{platform}/` (e.g. `src/duskgl/`, `src/duskpsp/`, `src/duskdolphin/`).
|
||||||
|
|
||||||
|
Assets are zipped into `dusk.dsk` at build time and loaded at runtime via the asset system.
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Linux (host)
|
||||||
|
./scripts/build-linux.sh # outputs build-linux/Dusk
|
||||||
|
|
||||||
|
# Other targets (require Docker)
|
||||||
|
./scripts/build-psp-docker.sh
|
||||||
|
./scripts/build-gamecube-docker.sh
|
||||||
|
./scripts/build-wii-docker.sh
|
||||||
|
./scripts/build-knulli-docker.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
Each script is a thin wrapper around:
|
||||||
|
```bash
|
||||||
|
cmake -S . -B build-<target> -DDUSK_TARGET_SYSTEM=<target>
|
||||||
|
cmake --build build-<target> -- -j$(nproc)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./scripts/test-linux.sh # builds and runs all tests
|
||||||
|
|
||||||
|
# Manually run a single test binary after building:
|
||||||
|
./build-tests/test/<module>/test_<name>
|
||||||
|
```
|
||||||
|
|
||||||
|
Tests use [cmocka](https://cmocka.org/) and are only compiled when `DUSK_BUILD_TESTS=ON`. Test sources live under `test/`.
|
||||||
|
|
||||||
|
## Key conventions
|
||||||
|
|
||||||
|
- Use `stringCompare`, `stringCopy`, `stringEquals`, etc. from `util/string.h` — never `strcmp`, `strcpy`, and friends directly.
|
||||||
|
- All functions that can fail return `errorret_t`. Use `errorThrow(...)`, `errorChain(call())`, and `errorOk()` macros — see `error/error.h`.
|
||||||
|
- Positions and game-world values use `fixed_t` (Q24.8 fixed-point, `int32_t`) — not `float`. Use `FIXED(x)` for literals and the `fixedFrom*`/`fixedTo*` helpers in `util/fixed.h`.
|
||||||
|
|
||||||
|
## Coding style
|
||||||
|
|
||||||
|
See [`.claude/style.md`](.claude/style.md) for the full style guide: indentation, line length, naming, typedefs, defines, include order, `const` usage, assertion placement, error handling, struct initialization, and platform conditionals.
|
||||||
|
|
||||||
|
## Architecture & systems
|
||||||
|
|
||||||
|
| Doc | Covers |
|
||||||
|
|---|---|
|
||||||
|
| [`.claude/architecture.md`](.claude/architecture.md) | Platform abstraction pattern, subsystem lifecycle, error handling, code-generation pipeline |
|
||||||
|
| [`.claude/display.md`](.claude/display.md) | Rendering pipeline: display state, screen, framebuffer, mesh, shader, texture, spritebatch, text |
|
||||||
|
| [`.claude/input.md`](.claude/input.md) | Input actions, buttons, bindings, axis helpers, events |
|
||||||
|
| [`.claude/asset.md`](.claude/asset.md) | Asset archive, entry lifecycle, loader types, async/sync split, low-level file I/O |
|
||||||
|
| [`.claude/ui.md`](.claude/ui.md) | UI element system, textbox, frames, loading overlay, fullbox transitions, FPS counter |
|
||||||
|
| [`.claude/animation.md`](.claude/animation.md) | Keyframe animation, easing functions |
|
||||||
|
| [`.claude/systems.md`](.claude/systems.md) | Time, threading, mutex, events, console, logging, system/platform, network |
|
||||||
|
| [`.claude/util.md`](.claude/util.md) | String, memory, math, fixed-point, array, sort, ref counting, CRC32, endian |
|
||||||
|
| [`.claude/save.md`](.claude/save.md) | Save slots, stream serialization with CRC, locale/i18n |
|
||||||
|
| [`.claude/rpg/index.md`](.claude/rpg/index.md) | RPG layer overview → [world](.claude/rpg/world.md), [entities](.claude/rpg/entity.md), [cutscenes](.claude/rpg/cutscene.md), [story/items](.claude/rpg/story.md) |
|
||||||
|
| [`.claude/display-refactor.md`](.claude/display-refactor.md) | Planned render-queue refactor (Saturn port context) |
|
||||||
@@ -16,7 +16,6 @@ typedef enum {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
rpgcameramode_t mode;
|
rpgcameramode_t mode;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
worldpos_t free;
|
worldpos_t free;
|
||||||
struct {
|
struct {
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
// Copyright (c) 2026 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
const platformNames = {
|
||||||
|
[System.PLATFORM_LINUX]: 'Linux',
|
||||||
|
[System.PLATFORM_KNULLI]: 'Knulli',
|
||||||
|
[System.PLATFORM_PSP]: 'PSP',
|
||||||
|
[System.PLATFORM_GAMECUBE]: 'GameCube',
|
||||||
|
[System.PLATFORM_WII]: 'Wii',
|
||||||
|
};
|
||||||
|
|
||||||
|
Console.print('Platform: ' + (platformNames[System.platform] || 'Unknown'));
|
||||||
|
|
||||||
|
UIFullboxOver.setColor(Color.BLACK);
|
||||||
|
|
||||||
|
requireAsync('testscene.js').then(Scene.set).catch(err => {
|
||||||
|
Console.print('Error loading scene: ' + err);
|
||||||
|
Engine.exit();
|
||||||
|
});
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
// Copyright (c) 2026 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
const PLAYER_SPEED = 5.0;
|
||||||
|
// 1 world unit = 16 pixels.
|
||||||
|
const PIXEL_SCALE = 1.0 / 16.0;
|
||||||
|
// Player sprite is 32x32 px (test.png dimensions).
|
||||||
|
const PLAYER_W = 32 * PIXEL_SCALE;
|
||||||
|
const PLAYER_H = 32 * PIXEL_SCALE;
|
||||||
|
|
||||||
|
var player = {};
|
||||||
|
|
||||||
|
player.getAssets = () => {
|
||||||
|
return [
|
||||||
|
{ path: 'test.png', type: Asset.TYPE_TEXTURE, format: Texture.FORMAT_RGBA }
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
player.init = function(scene) {
|
||||||
|
var texture = scene.assets.getAssetByPath('test.png');
|
||||||
|
Console.print('Player init: got texture ' + texture);
|
||||||
|
|
||||||
|
_entity = Entity.create();
|
||||||
|
_position = _entity.add(Component.POSITION);
|
||||||
|
_physics = _entity.add(Component.PHYSICS);
|
||||||
|
|
||||||
|
_physics.bodyType = Physics.DYNAMIC;
|
||||||
|
_physics.shape = Physics.SHAPE_CUBE;
|
||||||
|
_physics.gravityScale = 1.0;
|
||||||
|
|
||||||
|
var r = _entity.add(Component.RENDERABLE);
|
||||||
|
r.texture = texture.texture;
|
||||||
|
r.type = Renderable.SPRITEBATCH;
|
||||||
|
r.color = new Color(220, 80, 80);
|
||||||
|
// Upright quad centered on X, bottom-aligned on Y.
|
||||||
|
r.sprites = [[-PLAYER_W/2, 0, 0, PLAYER_W/2, PLAYER_H, 0, 0, 1, 1, 0]];
|
||||||
|
|
||||||
|
_position.localPosition = new Vec3(0, PLAYER_H, 0);
|
||||||
|
};
|
||||||
|
|
||||||
|
player.getPosition = function() {
|
||||||
|
return _position;
|
||||||
|
};
|
||||||
|
|
||||||
|
player.update = function() {
|
||||||
|
if(!_physics) return;
|
||||||
|
var vx = Input.axis(INPUT_ACTION_LEFT, INPUT_ACTION_RIGHT) * PLAYER_SPEED;
|
||||||
|
var vz = Input.axis(INPUT_ACTION_DOWN, INPUT_ACTION_UP) * PLAYER_SPEED;
|
||||||
|
// Preserve vertical velocity so gravity and landing work correctly.
|
||||||
|
var vy = _physics.velocity.y;
|
||||||
|
_physics.velocity = new Vec3(vx, vy, vz);
|
||||||
|
};
|
||||||
|
|
||||||
|
player.dispose = function() {
|
||||||
|
Entity.dispose(_entity);
|
||||||
|
_entity = null;
|
||||||
|
_position = null;
|
||||||
|
_physics = null;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = player;
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
// Copyright (c) 2026 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
var scene = {};
|
||||||
|
|
||||||
|
// Pokemon DS-style camera: ~34 degrees elevation (atan(6/9)).
|
||||||
|
// CAM_HEIGHT / CAM_DIST ratio controls the tilt - keep it under 0.7 for
|
||||||
|
// the characteristically shallow DS angle.
|
||||||
|
const CAM_HEIGHT = 6;
|
||||||
|
const CAM_DIST = 9;
|
||||||
|
|
||||||
|
scene.init = async function() {
|
||||||
|
// Camera
|
||||||
|
scene.cam = Entity.create();
|
||||||
|
var camPos = scene.cam.add(Component.POSITION);
|
||||||
|
var cam = scene.cam.add(Component.CAMERA);
|
||||||
|
camPos.localPosition = new Vec3(3, 3, 3);
|
||||||
|
camPos.lookAt(new Vec3(0, 0, 0));
|
||||||
|
|
||||||
|
// Floor - large flat slab, no texture needed.
|
||||||
|
scene.floor = Entity.create();
|
||||||
|
var floorPos = scene.floor.add(Component.POSITION);
|
||||||
|
var floorR = scene.floor.add(Component.RENDERABLE);
|
||||||
|
floorR.type = Renderable.SHADER_MATERIAL;
|
||||||
|
floorR.color = Color.BLUE;
|
||||||
|
// floorPos.localScale = new Vec3(16, 0.2, 16);
|
||||||
|
// floorPos.localPosition = new Vec3(0, -0.1, 0);
|
||||||
|
|
||||||
|
await UIFullboxOver.transition(Color.BLACK, Color.TRANSPARENT, 1.0);
|
||||||
|
};
|
||||||
|
|
||||||
|
scene.update = function() {
|
||||||
|
};
|
||||||
|
|
||||||
|
scene.dispose = function() {
|
||||||
|
Entity.dispose(scene.floor);
|
||||||
|
Entity.dispose(scene.cam);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = scene;
|
||||||
@@ -1,15 +1,18 @@
|
|||||||
# Build type: FAT (SD/USB via libfat) or ISO (DVD disc via libogc DVD driver)
|
# Build type: DOL (SD/USB via libfat) or ISO (DVD disc via libogc DVD driver)
|
||||||
set(DUSK_DOLPHIN_BUILD_TYPE "FAT" CACHE STRING "Dolphin asset source: FAT (SD/USB) or ISO (DVD disc)")
|
set(DUSK_DOLPHIN_BUILD_TYPE "DOL" CACHE STRING "Dolphin asset source: DOL (SD/USB) or ISO (DVD disc)")
|
||||||
set_property(CACHE DUSK_DOLPHIN_BUILD_TYPE PROPERTY STRINGS "FAT" "ISO")
|
set_property(CACHE DUSK_DOLPHIN_BUILD_TYPE PROPERTY STRINGS "DOL" "ISO")
|
||||||
|
|
||||||
# Target definitions
|
# Numeric tokens so #if DUSK_DOLPHIN_BUILD_TYPE == DOL works in C.
|
||||||
|
# DUSK_DOLPHIN_BUILD_TYPE is passed without quotes so it expands to the identifier.
|
||||||
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||||
DUSK_DOLPHIN
|
DUSK_DOLPHIN
|
||||||
DUSK_INPUT_GAMEPAD
|
DUSK_INPUT_GAMEPAD
|
||||||
DUSK_DISPLAY_WIDTH=640
|
DUSK_DISPLAY_WIDTH=640
|
||||||
DUSK_DISPLAY_HEIGHT=480
|
DUSK_DISPLAY_HEIGHT=480
|
||||||
DUSK_THREAD_PTHREAD
|
DUSK_THREAD_PTHREAD
|
||||||
DUSK_DOLPHIN_BUILD_TYPE="${DUSK_DOLPHIN_BUILD_TYPE}"
|
DOL=1
|
||||||
|
ISO=2
|
||||||
|
DUSK_DOLPHIN_BUILD_TYPE=${DUSK_DOLPHIN_BUILD_TYPE}
|
||||||
)
|
)
|
||||||
|
|
||||||
# Custom compiler flags
|
# Custom compiler flags
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
|||||||
DUSK_OPENGL
|
DUSK_OPENGL
|
||||||
DUSK_OPENGL_ES
|
DUSK_OPENGL_ES
|
||||||
DUSK_LINUX
|
DUSK_LINUX
|
||||||
|
DUSK_KNULLI
|
||||||
DUSK_DISPLAY_SIZE_DYNAMIC
|
DUSK_DISPLAY_SIZE_DYNAMIC
|
||||||
DUSK_DISPLAY_WIDTH_DEFAULT=640
|
DUSK_DISPLAY_WIDTH_DEFAULT=640
|
||||||
DUSK_DISPLAY_HEIGHT_DEFAULT=480
|
DUSK_DISPLAY_HEIGHT_DEFAULT=480
|
||||||
|
|||||||
@@ -16,9 +16,6 @@ else()
|
|||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Export symbols so backtrace_symbols() can resolve function names.
|
|
||||||
target_link_options(${DUSK_LIBRARY_TARGET_NAME} PUBLIC -rdynamic)
|
|
||||||
|
|
||||||
# Link required libraries.
|
# Link required libraries.
|
||||||
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||||
SDL2
|
SDL2
|
||||||
@@ -29,6 +26,8 @@ target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
|||||||
# CURL::libcurl
|
# CURL::libcurl
|
||||||
)
|
)
|
||||||
|
|
||||||
|
set(DUSK_BACKTRACE ON CACHE BOOL "Enable backtrace support for assert failures.")
|
||||||
|
|
||||||
# Define platform-specific macros.
|
# Define platform-specific macros.
|
||||||
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||||
DUSK_SDL2
|
DUSK_SDL2
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
docker build -t dusk-dolphin -f docker/dolphin/Dockerfile .
|
docker build -t dusk-dolphin -f docker/dolphin/Dockerfile .
|
||||||
docker run --rm -v $(pwd):/workdir dusk-dolphin /bin/bash -c "./scripts/build-gamecube.sh"
|
docker run --rm -v "$(pwd):/workdir" dusk-dolphin /bin/bash -c "./scripts/build-gamecube.sh"
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
docker build -t dusk-dolphin -f docker/dolphin/Dockerfile .
|
docker build -t dusk-dolphin -f docker/dolphin/Dockerfile .
|
||||||
docker run --rm -v $(pwd):/workdir dusk-dolphin /bin/bash -c "./scripts/build-gamecube-iso.sh"
|
docker run --rm -v "$(pwd):/workdir" dusk-dolphin /bin/bash -c "./scripts/build-gamecube-iso.sh"
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ fi
|
|||||||
mkdir -p build-gamecube
|
mkdir -p build-gamecube
|
||||||
cmake -S. -Bbuild-gamecube \
|
cmake -S. -Bbuild-gamecube \
|
||||||
-DDUSK_TARGET_SYSTEM=gamecube \
|
-DDUSK_TARGET_SYSTEM=gamecube \
|
||||||
|
-DDUSK_DOLPHIN_BUILD_TYPE=DOL \
|
||||||
-DCMAKE_TOOLCHAIN_FILE="$DEVKITPRO/cmake/GameCube.cmake" \
|
-DCMAKE_TOOLCHAIN_FILE="$DEVKITPRO/cmake/GameCube.cmake" \
|
||||||
-DDKP_OGC_PLATFORM_LIBRARY=libogc2
|
-DDKP_OGC_PLATFORM_LIBRARY=libogc2
|
||||||
cd build-gamecube
|
cd build-gamecube
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
docker build -t dusk-knulli -f docker/knulli/Dockerfile .
|
docker build -t dusk-knulli -f docker/knulli/Dockerfile .
|
||||||
docker run --rm -v $(pwd):/workdir dusk-knulli /bin/bash -c "./scripts/build-knulli.sh"
|
docker run --rm -v "$(pwd):/workdir" dusk-knulli /bin/bash -c "./scripts/build-knulli.sh"
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
docker build -t dusk-linux -f docker/linux/Dockerfile .
|
docker build -t dusk-linux -f docker/linux/Dockerfile .
|
||||||
docker run --rm -v $(pwd):/workdir dusk-linux /bin/bash -c "./scripts/build-linux.sh"
|
docker run --rm -v "$(pwd):/workdir" dusk-linux /bin/bash -c "./scripts/build-linux.sh"
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
docker build -t dusk-psp -f docker/psp/Dockerfile .
|
docker build -t dusk-psp -f docker/psp/Dockerfile .
|
||||||
docker run --rm -v $(pwd):/workdir dusk-psp /bin/bash -c "./scripts/build-psp.sh"
|
docker run --rm -v "$(pwd):/workdir" dusk-psp /bin/bash -c "./scripts/build-psp.sh"
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
docker build -t dusk-vita -f docker/vita/Dockerfile .
|
docker build -t dusk-vita -f docker/vita/Dockerfile .
|
||||||
docker run --rm -v $(pwd):/workdir dusk-vita /bin/bash -c "./scripts/build-vita.sh"
|
docker run --rm -v "$(pwd):/workdir" dusk-vita /bin/bash -c "./scripts/build-vita.sh"
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
docker build -t dusk-dolphin -f docker/dolphin/Dockerfile .
|
docker build -t dusk-dolphin -f docker/dolphin/Dockerfile .
|
||||||
docker run --rm -v $(pwd):/workdir dusk-dolphin /bin/bash -c "./scripts/build-wii.sh"
|
docker run --rm -v "$(pwd):/workdir" dusk-dolphin /bin/bash -c "./scripts/build-wii.sh"
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
docker build -t dusk-dolphin -f docker/dolphin/Dockerfile .
|
docker build -t dusk-dolphin -f docker/dolphin/Dockerfile .
|
||||||
docker run --rm -v $(pwd):/workdir dusk-dolphin /bin/bash -c "./scripts/build-wii-iso.sh"
|
docker run --rm -v "$(pwd):/workdir" dusk-dolphin /bin/bash -c "./scripts/build-wii-iso.sh"
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ if [ -z "$DEVKITPRO" ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
mkdir -p build-wii
|
mkdir -p build-wii
|
||||||
cmake -S. -Bbuild-wii -DDUSK_TARGET_SYSTEM=wii -DCMAKE_TOOLCHAIN_FILE="$DEVKITPRO/cmake/Wii.cmake"
|
cmake -S. -Bbuild-wii \
|
||||||
|
-DDUSK_TARGET_SYSTEM=wii \
|
||||||
|
-DCMAKE_TOOLCHAIN_FILE="$DEVKITPRO/cmake/Wii.cmake" \
|
||||||
|
-DDUSK_DOLPHIN_BUILD_TYPE=DOL
|
||||||
cd build-wii
|
cd build-wii
|
||||||
make -j$(nproc) VERBOSE=1
|
make -j$(nproc) VERBOSE=1
|
||||||
|
mv Dusk.dol boot.dol
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
docker build -t dusk-linux -f docker/linux/Dockerfile .
|
docker build -t dusk-linux -f docker/linux/Dockerfile .
|
||||||
docker run --rm -v $(pwd):/workdir dusk-linux /bin/bash -c "./scripts/test-linux.sh"
|
docker run \
|
||||||
|
--rm \
|
||||||
|
-v "${GITHUB_WORKSPACE}:/workdir" \
|
||||||
|
dusk-linux \
|
||||||
|
/bin/bash -c "./scripts/test-linux.sh"
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
rm -rf build-tests
|
||||||
cmake -S . -B build-tests -DDUSK_BUILD_TESTS=ON -DDUSK_TARGET_SYSTEM=linux
|
cmake -S . -B build-tests -DDUSK_BUILD_TESTS=ON -DDUSK_TARGET_SYSTEM=linux
|
||||||
cmake --build build-tests -- -j$(nproc)
|
cmake --build build-tests -- -j$(nproc)
|
||||||
ctest --output-on-failure --test-dir build-tests
|
ctest --output-on-failure --test-dir build-tests
|
||||||
@@ -32,6 +32,13 @@ if(NOT yyjson_FOUND)
|
|||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if(DUSK_BACKTRACE)
|
||||||
|
target_link_options(${DUSK_LIBRARY_TARGET_NAME} PUBLIC -rdynamic)
|
||||||
|
target_compile_definitions(${DUSK_BINARY_TARGET_NAME} PUBLIC
|
||||||
|
DUSK_BACKTRACE
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
# Includes
|
# Includes
|
||||||
target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
|
target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
PUBLIC
|
PUBLIC
|
||||||
@@ -50,23 +57,19 @@ add_subdirectory(event)
|
|||||||
add_subdirectory(assert)
|
add_subdirectory(assert)
|
||||||
add_subdirectory(asset)
|
add_subdirectory(asset)
|
||||||
add_subdirectory(cutscene)
|
add_subdirectory(cutscene)
|
||||||
add_subdirectory(item)
|
|
||||||
add_subdirectory(story)
|
|
||||||
add_subdirectory(console)
|
add_subdirectory(console)
|
||||||
add_subdirectory(display)
|
add_subdirectory(display)
|
||||||
add_subdirectory(log)
|
add_subdirectory(log)
|
||||||
add_subdirectory(engine)
|
add_subdirectory(engine)
|
||||||
add_subdirectory(entity)
|
|
||||||
add_subdirectory(error)
|
add_subdirectory(error)
|
||||||
add_subdirectory(input)
|
add_subdirectory(input)
|
||||||
add_subdirectory(locale)
|
add_subdirectory(locale)
|
||||||
add_subdirectory(physics)
|
add_subdirectory(rpg)
|
||||||
add_subdirectory(scene)
|
add_subdirectory(scene)
|
||||||
add_subdirectory(system)
|
add_subdirectory(system)
|
||||||
add_subdirectory(time)
|
add_subdirectory(time)
|
||||||
add_subdirectory(ui)
|
add_subdirectory(ui)
|
||||||
add_subdirectory(network)
|
add_subdirectory(network)
|
||||||
add_subdirectory(overworld)
|
|
||||||
add_subdirectory(save)
|
add_subdirectory(save)
|
||||||
add_subdirectory(util)
|
add_subdirectory(util)
|
||||||
add_subdirectory(thread)
|
add_subdirectory(thread)
|
||||||
@@ -7,5 +7,4 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
|||||||
PUBLIC
|
PUBLIC
|
||||||
easing.c
|
easing.c
|
||||||
animation.c
|
animation.c
|
||||||
animationproperty.c
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -6,88 +6,54 @@
|
|||||||
#include "animation.h"
|
#include "animation.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
#include "util/math.h"
|
#include "util/fixed.h"
|
||||||
|
|
||||||
void animationInit(animation_t *anim) {
|
void animationInit(
|
||||||
memoryZero(anim, sizeof(animation_t));
|
animation_t *anim,
|
||||||
eventInit(&anim->onStart);
|
keyframe_t *keyframes,
|
||||||
eventInit(&anim->onComplete);
|
uint16_t keyframeCount
|
||||||
|
) {
|
||||||
|
assertNotNull(anim, "Animation pointer cannot be null.");
|
||||||
|
assertNotNull(keyframes, "Keyframes pointer cannot be null.");
|
||||||
|
assertTrue(keyframeCount > 0, "Keyframe count must be more than 0.");
|
||||||
|
|
||||||
|
anim->keyframes = keyframes;
|
||||||
|
anim->keyframeCount = keyframeCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
animationproperty_t *animationAddProperty(animation_t *anim, float_t *target) {
|
fixed_t animationGetValue(animation_t *anim, const fixed_t time) {
|
||||||
assertTrue(
|
assertNotNull(anim, "Animation pointer cannot be null.");
|
||||||
anim->propertyCount < ANIMATION_PROPERTY_COUNT_MAX,
|
assertNotNull(anim->keyframes, "Keyframes pointer cannot be null.");
|
||||||
"Property count exceeds ANIMATION_PROPERTY_COUNT_MAX"
|
assertTrue(anim->keyframeCount > 0, "Keyframe count invalid.");
|
||||||
|
assertTrue(time >= 0, "Time must be non-negative.");
|
||||||
|
|
||||||
|
keyframe_t *start;
|
||||||
|
keyframe_t *end;
|
||||||
|
keyframe_t *last = anim->keyframes + anim->keyframeCount - 1;
|
||||||
|
keyframe_t *current = anim->keyframes;
|
||||||
|
start = current;
|
||||||
|
|
||||||
|
do {
|
||||||
|
if(current->time > time) {
|
||||||
|
end = current;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
start = current;
|
||||||
|
current++;
|
||||||
|
|
||||||
|
if(current > last) {
|
||||||
|
end = start;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while(true);
|
||||||
|
|
||||||
|
fixed_t span = fixedSub(end->time, start->time);
|
||||||
|
fixed_t progress = span != 0
|
||||||
|
? fixedDiv(fixedSub(time, start->time), span)
|
||||||
|
: FIXED_ONE;
|
||||||
|
return fixedLerp(
|
||||||
|
start->value,
|
||||||
|
end->value,
|
||||||
|
easingApply(start->easing, progress)
|
||||||
);
|
);
|
||||||
animationproperty_t *prop = &anim->properties[anim->propertyCount++];
|
|
||||||
prop->keyframeCount = 0;
|
|
||||||
prop->target = target;
|
|
||||||
return prop;
|
|
||||||
}
|
|
||||||
|
|
||||||
void animationUpdate(animation_t *anim, float_t delta) {
|
|
||||||
assertNotNull(anim, "Animation cannot be null");
|
|
||||||
|
|
||||||
if(
|
|
||||||
(anim->flags & ANIMATION_FLAG_FINISHED) &&
|
|
||||||
!(anim->flags & ANIMATION_FLAG_LOOP_ENABLED)
|
|
||||||
) return;
|
|
||||||
|
|
||||||
bool_t wasStarted = (anim->flags & ANIMATION_FLAG_STARTED) != 0;
|
|
||||||
anim->flags |= ANIMATION_FLAG_STARTED;
|
|
||||||
anim->time += delta;
|
|
||||||
|
|
||||||
float_t duration = animationGetDuration(anim);
|
|
||||||
bool_t justFinished = false;
|
|
||||||
if(anim->time >= duration) {
|
|
||||||
if(anim->flags & ANIMATION_FLAG_LOOP_ENABLED) {
|
|
||||||
anim->time = mathModFloat(anim->time, duration);
|
|
||||||
} else {
|
|
||||||
anim->time = duration;
|
|
||||||
if(!(anim->flags & ANIMATION_FLAG_FINISHED)) {
|
|
||||||
anim->flags |= ANIMATION_FLAG_FINISHED;
|
|
||||||
justFinished = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(uint8_t i = 0; i < anim->propertyCount; i++) {
|
|
||||||
animationproperty_t *prop = &anim->properties[i];
|
|
||||||
if(prop->target != NULL) {
|
|
||||||
*prop->target = animationPropertyGetValue(prop, anim->time);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!wasStarted) eventInvoke(&anim->onStart, anim);
|
|
||||||
if(justFinished) eventInvoke(&anim->onComplete, anim);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void animationReset(animation_t *anim) {
|
|
||||||
anim->time = 0.0f;
|
|
||||||
anim->flags &= ~(ANIMATION_FLAG_FINISHED | ANIMATION_FLAG_STARTED);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool_t animationIsFinished(const animation_t *anim) {
|
|
||||||
return (anim->flags & ANIMATION_FLAG_FINISHED) != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool_t animationIsStarted(const animation_t *anim) {
|
|
||||||
return (anim->flags & ANIMATION_FLAG_STARTED) != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool_t animationIsLooping(const animation_t *anim) {
|
|
||||||
return (anim->flags & ANIMATION_FLAG_LOOP_ENABLED) != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
float_t animationGetDuration(const animation_t *anim) {
|
|
||||||
float_t duration = 0.0f;
|
|
||||||
for(uint8_t i = 0; i < anim->propertyCount; i++) {
|
|
||||||
animationproperty_t *prop = &anim->properties[i];
|
|
||||||
if(prop->keyframeCount == 0) continue;
|
|
||||||
float_t lastKeyframeTime =
|
|
||||||
prop->keyframes[prop->keyframeCount - 1].time;
|
|
||||||
if(lastKeyframeTime > duration) duration = lastKeyframeTime;
|
|
||||||
}
|
|
||||||
return duration;
|
|
||||||
}
|
}
|
||||||
@@ -4,87 +4,31 @@
|
|||||||
// https://opensource.org/licenses/MIT
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "animationproperty.h"
|
#include "keyframe.h"
|
||||||
#include "event/event.h"
|
|
||||||
|
|
||||||
#define ANIMATION_PROPERTY_COUNT_MAX 8
|
|
||||||
|
|
||||||
#define ANIMATION_FLAG_FINISHED (1 << 0)
|
|
||||||
#define ANIMATION_FLAG_STARTED (1 << 1)
|
|
||||||
#define ANIMATION_FLAG_LOOP_ENABLED (1 << 2)
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
animationproperty_t properties[ANIMATION_PROPERTY_COUNT_MAX];
|
keyframe_t *keyframes;
|
||||||
uint8_t propertyCount;
|
uint16_t keyframeCount;
|
||||||
float_t time;
|
|
||||||
uint8_t flags;
|
|
||||||
event_t onStart;
|
|
||||||
event_t onComplete;
|
|
||||||
} animation_t;
|
} animation_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes an animation.
|
* Initializes an animation.
|
||||||
*
|
*
|
||||||
* @param anim The animation to initialize.
|
* @param anim The animation to initialize.
|
||||||
|
* @param keyframes The keyframes to use for the animation.
|
||||||
|
* @param keyframeCount The number of keyframes in the animation.
|
||||||
*/
|
*/
|
||||||
void animationInit(animation_t *anim);
|
void animationInit(
|
||||||
|
animation_t *anim,
|
||||||
|
keyframe_t *keyframes,
|
||||||
|
uint16_t keyframeCount
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new animated property. The caller owns target and must keep it valid
|
* Gets the value of the animation at a given time.
|
||||||
* for the lifetime of the animation.
|
|
||||||
*
|
*
|
||||||
* @param anim The animation to add the property to.
|
* @param anim The animation to get the value from.
|
||||||
* @param target Pointer to the float to write interpolated values into.
|
* @param time The time at which to get the value, in seconds.
|
||||||
* @return A pointer to the new property.
|
* @return The value of the animation at the given time.
|
||||||
*/
|
*/
|
||||||
animationproperty_t *animationAddProperty(animation_t *anim, float_t *target);
|
fixed_t animationGetValue(animation_t *anim, const fixed_t time);
|
||||||
|
|
||||||
/**
|
|
||||||
* Advances the animation by delta seconds and writes interpolated values to
|
|
||||||
* each property's target pointer.
|
|
||||||
*
|
|
||||||
* @param anim The animation to update.
|
|
||||||
* @param delta The time to advance the animation by, in seconds.
|
|
||||||
*/
|
|
||||||
void animationUpdate(animation_t *anim, float_t delta);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resets the animation to the beginning, clearing Started and Finished flags.
|
|
||||||
*
|
|
||||||
* @param anim The animation to reset.
|
|
||||||
*/
|
|
||||||
void animationReset(animation_t *anim);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if the animation has finished (i.e. reached the end of its
|
|
||||||
* duration and is not looping).
|
|
||||||
*
|
|
||||||
* @param anim The animation to check.
|
|
||||||
* @return true if the animation has finished, false otherwise.
|
|
||||||
*/
|
|
||||||
bool_t animationIsFinished(const animation_t *anim);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if the animation has been started (i.e. animationUpdate has
|
|
||||||
* been called at least once).
|
|
||||||
*
|
|
||||||
* @param anim The animation to check.
|
|
||||||
* @return true if the animation has been started, false otherwise.
|
|
||||||
*/
|
|
||||||
bool_t animationIsStarted(const animation_t *anim);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if the animation is set to loop.
|
|
||||||
*
|
|
||||||
* @param anim The animation to check.
|
|
||||||
* @return true if the animation is set to loop, false otherwise.
|
|
||||||
*/
|
|
||||||
bool_t animationIsLooping(const animation_t *anim);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the total duration of the animation (based on the keyframes).
|
|
||||||
*
|
|
||||||
* @param anim The animation to get the duration of.
|
|
||||||
* @return The total duration of the animation, in seconds.
|
|
||||||
*/
|
|
||||||
float_t animationGetDuration(const animation_t *anim);
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2026 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "animationproperty.h"
|
|
||||||
#include "assert/assert.h"
|
|
||||||
|
|
||||||
void animationPropertyAddKeyframe(
|
|
||||||
animationproperty_t *prop,
|
|
||||||
const float_t time,
|
|
||||||
const float_t value,
|
|
||||||
const easingtype_t easing
|
|
||||||
) {
|
|
||||||
assertNotNull(prop, "Property cannot be null");
|
|
||||||
assertTrue(
|
|
||||||
prop->keyframeCount < ANIMATION_PROPERTY_KEYFRAME_COUNT_MAX,
|
|
||||||
"Too many keyframes added to property"
|
|
||||||
);
|
|
||||||
assertTrue(time >= 0.0f, "Keyframe time cannot be negative");
|
|
||||||
|
|
||||||
keyframe_t *frame = &prop->keyframes[prop->keyframeCount++];
|
|
||||||
frame->time = time;
|
|
||||||
frame->value = value;
|
|
||||||
frame->easing = easing;
|
|
||||||
}
|
|
||||||
|
|
||||||
float_t animationPropertyGetValue(
|
|
||||||
const animationproperty_t *prop,
|
|
||||||
const float_t time
|
|
||||||
) {
|
|
||||||
assertNotNull(prop, "Property cannot be null");
|
|
||||||
assertTrue(time >= 0.0f, "Time cannot be negative");
|
|
||||||
|
|
||||||
if(prop->keyframeCount == 0) return 0.0f;
|
|
||||||
|
|
||||||
uint8_t last = prop->keyframeCount - 1;
|
|
||||||
|
|
||||||
if(prop->keyframeCount == 1) return prop->keyframes[0].value;
|
|
||||||
if(time <= prop->keyframes[0].time) return prop->keyframes[0].value;
|
|
||||||
if(time >= prop->keyframes[last].time) return prop->keyframes[last].value;
|
|
||||||
|
|
||||||
for(uint8_t i = 0; i < last; i++) {
|
|
||||||
const keyframe_t *a = &prop->keyframes[i];
|
|
||||||
const keyframe_t *b = &prop->keyframes[i + 1];
|
|
||||||
if(time < a->time || time >= b->time) continue;
|
|
||||||
float_t t = (time - a->time) / (b->time - a->time);
|
|
||||||
t = easingApply(a->easing, t);
|
|
||||||
return a->value + (b->value - a->value) * t;
|
|
||||||
}
|
|
||||||
|
|
||||||
return prop->keyframes[last].value;
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2026 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "keyframe.h"
|
|
||||||
|
|
||||||
#define ANIMATION_PROPERTY_KEYFRAME_COUNT_MAX 16
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
keyframe_t keyframes[ANIMATION_PROPERTY_KEYFRAME_COUNT_MAX];
|
|
||||||
uint8_t keyframeCount;
|
|
||||||
float_t *target;
|
|
||||||
} animationproperty_t;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Appends a keyframe to a property. Keyframes must be added in ascending time
|
|
||||||
* order. Updates the animation's duration.
|
|
||||||
*
|
|
||||||
* @param property The property to add the keyframe to.
|
|
||||||
* @param time The time of the keyframe, in seconds.
|
|
||||||
* @param value The value of the keyframe.
|
|
||||||
* @param easing The easing type to use when interpolating to the next keyframe.
|
|
||||||
*/
|
|
||||||
void animationPropertyAddKeyframe(
|
|
||||||
animationproperty_t *property,
|
|
||||||
const float_t time,
|
|
||||||
const float_t value,
|
|
||||||
const easingtype_t easing
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the property's value at a given time.
|
|
||||||
*
|
|
||||||
* @param prop The property to get the value from.
|
|
||||||
* @param time The time at which to get the value, in seconds.
|
|
||||||
* @return The value of the property at the given time.
|
|
||||||
*/
|
|
||||||
float_t animationPropertyGetValue(
|
|
||||||
const animationproperty_t *prop,
|
|
||||||
const float_t time
|
|
||||||
);
|
|
||||||
+67
-47
@@ -5,7 +5,12 @@
|
|||||||
|
|
||||||
#include "easing.h"
|
#include "easing.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include <math.h>
|
#include "util/math.h"
|
||||||
|
#include "util/fixed.h"
|
||||||
|
|
||||||
|
#define EASING_C1 1.70158f
|
||||||
|
#define EASING_C2 (EASING_C1 * 1.525f)
|
||||||
|
#define EASING_C3 (EASING_C1 + 1.0f)
|
||||||
|
|
||||||
const easingfn_t EASING_FUNCTIONS[EASING_COUNT] = {
|
const easingfn_t EASING_FUNCTIONS[EASING_COUNT] = {
|
||||||
easingLinear,
|
easingLinear,
|
||||||
@@ -26,86 +31,101 @@ const easingfn_t EASING_FUNCTIONS[EASING_COUNT] = {
|
|||||||
easingInOutBack,
|
easingInOutBack,
|
||||||
};
|
};
|
||||||
|
|
||||||
float_t easingApply(const easingtype_t type, const float_t t) {
|
fixed_t easingApply(const easingtype_t type, const fixed_t t) {
|
||||||
assertTrue(type < EASING_COUNT, "Invalid easing type");
|
assertTrue(type < EASING_COUNT, "Invalid easing type");
|
||||||
return EASING_FUNCTIONS[type](t);
|
return EASING_FUNCTIONS[type](t);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingLinear(const float_t t) {
|
fixed_t easingLinear(const fixed_t t) {
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingInSine(const float_t t) {
|
fixed_t easingInSine(const fixed_t t) {
|
||||||
return 1.0f - cosf(t * EASING_PI * 0.5f);
|
float_t f = fixedToFloat(t);
|
||||||
|
return fixedFromFloat(1.0f - cosf(f * MATH_PI * 0.5f));
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingOutSine(const float_t t) {
|
fixed_t easingOutSine(const fixed_t t) {
|
||||||
return sinf(t * EASING_PI * 0.5f);
|
float_t f = fixedToFloat(t);
|
||||||
|
return fixedFromFloat(sinf(f * MATH_PI * 0.5f));
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingInOutSine(const float_t t) {
|
fixed_t easingInOutSine(const fixed_t t) {
|
||||||
return -(cosf(EASING_PI * t) - 1.0f) * 0.5f;
|
float_t f = fixedToFloat(t);
|
||||||
|
return fixedFromFloat(-(cosf(MATH_PI * f) - 1.0f) * 0.5f);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingInQuad(const float_t t) {
|
fixed_t easingInQuad(const fixed_t t) {
|
||||||
return t * t;
|
float_t f = fixedToFloat(t);
|
||||||
|
return fixedFromFloat(f * f);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingOutQuad(const float_t t) {
|
fixed_t easingOutQuad(const fixed_t t) {
|
||||||
float_t u = 1.0f - t;
|
float_t f = fixedToFloat(t);
|
||||||
return 1.0f - u * u;
|
float_t u = 1.0f - f;
|
||||||
|
return fixedFromFloat(1.0f - u * u);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingInOutQuad(const float_t t) {
|
fixed_t easingInOutQuad(const fixed_t t) {
|
||||||
if(t < 0.5f) return 2.0f * t * t;
|
float_t f = fixedToFloat(t);
|
||||||
float_t u = -2.0f * t + 2.0f;
|
if(f < 0.5f) return fixedFromFloat(2.0f * f * f);
|
||||||
return 1.0f - u * u * 0.5f;
|
float_t u = -2.0f * f + 2.0f;
|
||||||
|
return fixedFromFloat(1.0f - u * u * 0.5f);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingInCubic(const float_t t) {
|
fixed_t easingInCubic(const fixed_t t) {
|
||||||
return t * t * t;
|
float_t f = fixedToFloat(t);
|
||||||
|
return fixedFromFloat(f * f * f);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingOutCubic(const float_t t) {
|
fixed_t easingOutCubic(const fixed_t t) {
|
||||||
float_t u = 1.0f - t;
|
float_t f = fixedToFloat(t);
|
||||||
return 1.0f - u * u * u;
|
float_t u = 1.0f - f;
|
||||||
|
return fixedFromFloat(1.0f - u * u * u);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingInOutCubic(const float_t t) {
|
fixed_t easingInOutCubic(const fixed_t t) {
|
||||||
if(t < 0.5f) return 4.0f * t * t * t;
|
float_t f = fixedToFloat(t);
|
||||||
float_t u = -2.0f * t + 2.0f;
|
if(f < 0.5f) return fixedFromFloat(4.0f * f * f * f);
|
||||||
return 1.0f - u * u * u * 0.5f;
|
float_t u = -2.0f * f + 2.0f;
|
||||||
|
return fixedFromFloat(1.0f - u * u * u * 0.5f);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingInQuart(const float_t t) {
|
fixed_t easingInQuart(const fixed_t t) {
|
||||||
return t * t * t * t;
|
float_t f = fixedToFloat(t);
|
||||||
|
return fixedFromFloat(f * f * f * f);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingOutQuart(const float_t t) {
|
fixed_t easingOutQuart(const fixed_t t) {
|
||||||
float_t u = 1.0f - t;
|
float_t f = fixedToFloat(t);
|
||||||
return 1.0f - u * u * u * u;
|
float_t u = 1.0f - f;
|
||||||
|
return fixedFromFloat(1.0f - u * u * u * u);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingInOutQuart(const float_t t) {
|
fixed_t easingInOutQuart(const fixed_t t) {
|
||||||
if(t < 0.5f) return 8.0f * t * t * t * t;
|
float_t f = fixedToFloat(t);
|
||||||
float_t u = -2.0f * t + 2.0f;
|
if(f < 0.5f) return fixedFromFloat(8.0f * f * f * f * f);
|
||||||
return 1.0f - u * u * u * u * 0.5f;
|
float_t u = -2.0f * f + 2.0f;
|
||||||
|
return fixedFromFloat(1.0f - u * u * u * u * 0.5f);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingInBack(const float_t t) {
|
fixed_t easingInBack(const fixed_t t) {
|
||||||
return EASING_C3 * t * t * t - EASING_C1 * t * t;
|
float_t f = fixedToFloat(t);
|
||||||
|
return fixedFromFloat(EASING_C3 * f * f * f - EASING_C1 * f * f);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingOutBack(const float_t t) {
|
fixed_t easingOutBack(const fixed_t t) {
|
||||||
float_t u = t - 1.0f;
|
float_t f = fixedToFloat(t);
|
||||||
return 1.0f + EASING_C3 * u * u * u + EASING_C1 * u * u;
|
float_t u = f - 1.0f;
|
||||||
|
return fixedFromFloat(1.0f + EASING_C3 * u * u * u + EASING_C1 * u * u);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t easingInOutBack(const float_t t) {
|
fixed_t easingInOutBack(const fixed_t t) {
|
||||||
if(t < 0.5f) {
|
float_t f = fixedToFloat(t);
|
||||||
float_t u = 2.0f * t;
|
if(f < 0.5f) {
|
||||||
return u * u * ((EASING_C2 + 1.0f) * u - EASING_C2) * 0.5f;
|
float_t u = 2.0f * f;
|
||||||
|
return fixedFromFloat(u * u * ((EASING_C2 + 1.0f) * u - EASING_C2) * 0.5f);
|
||||||
}
|
}
|
||||||
float_t u = 2.0f * t - 2.0f;
|
float_t u = 2.0f * f - 2.0f;
|
||||||
return (u * u * ((EASING_C2 + 1.0f) * u + EASING_C2) + 2.0f) * 0.5f;
|
return fixedFromFloat((u * u * ((EASING_C2 + 1.0f) * u + EASING_C2) + 2.0f) * 0.5f);
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-25
@@ -5,11 +5,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "dusk.h"
|
#include "dusk.h"
|
||||||
|
#include "util/fixed.h"
|
||||||
#define EASING_PI 3.14159265358979323846f
|
|
||||||
#define EASING_C1 1.70158f
|
|
||||||
#define EASING_C2 (EASING_C1 * 1.525f)
|
|
||||||
#define EASING_C3 (EASING_C1 + 1.0f)
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
EASING_LINEAR,
|
EASING_LINEAR,
|
||||||
@@ -32,7 +28,7 @@ typedef enum {
|
|||||||
EASING_COUNT
|
EASING_COUNT
|
||||||
} easingtype_t;
|
} easingtype_t;
|
||||||
|
|
||||||
typedef float_t (*easingfn_t)(const float_t t);
|
typedef fixed_t (*easingfn_t)(const fixed_t t);
|
||||||
|
|
||||||
extern const easingfn_t EASING_FUNCTIONS[EASING_COUNT];
|
extern const easingfn_t EASING_FUNCTIONS[EASING_COUNT];
|
||||||
|
|
||||||
@@ -40,24 +36,24 @@ extern const easingfn_t EASING_FUNCTIONS[EASING_COUNT];
|
|||||||
* Applies the specified easing function to t.
|
* Applies the specified easing function to t.
|
||||||
*
|
*
|
||||||
* @param type The easing type to apply.
|
* @param type The easing type to apply.
|
||||||
* @param t The input time, in the range [0, 1].
|
* @param t The input progress in [0, FIXED_ONE].
|
||||||
* @return The eased value, in the range [0, 1].
|
* @return The eased value in [0, FIXED_ONE].
|
||||||
*/
|
*/
|
||||||
float_t easingApply(const easingtype_t type, const float_t t);
|
fixed_t easingApply(const easingtype_t type, const fixed_t t);
|
||||||
|
|
||||||
float_t easingLinear(const float_t t);
|
fixed_t easingLinear(const fixed_t t);
|
||||||
float_t easingInSine(const float_t t);
|
fixed_t easingInSine(const fixed_t t);
|
||||||
float_t easingOutSine(const float_t t);
|
fixed_t easingOutSine(const fixed_t t);
|
||||||
float_t easingInOutSine(const float_t t);
|
fixed_t easingInOutSine(const fixed_t t);
|
||||||
float_t easingInQuad(const float_t t);
|
fixed_t easingInQuad(const fixed_t t);
|
||||||
float_t easingOutQuad(const float_t t);
|
fixed_t easingOutQuad(const fixed_t t);
|
||||||
float_t easingInOutQuad(const float_t t);
|
fixed_t easingInOutQuad(const fixed_t t);
|
||||||
float_t easingInCubic(const float_t t);
|
fixed_t easingInCubic(const fixed_t t);
|
||||||
float_t easingOutCubic(const float_t t);
|
fixed_t easingOutCubic(const fixed_t t);
|
||||||
float_t easingInOutCubic(const float_t t);
|
fixed_t easingInOutCubic(const fixed_t t);
|
||||||
float_t easingInQuart(const float_t t);
|
fixed_t easingInQuart(const fixed_t t);
|
||||||
float_t easingOutQuart(const float_t t);
|
fixed_t easingOutQuart(const fixed_t t);
|
||||||
float_t easingInOutQuart(const float_t t);
|
fixed_t easingInOutQuart(const fixed_t t);
|
||||||
float_t easingInBack(const float_t t);
|
fixed_t easingInBack(const fixed_t t);
|
||||||
float_t easingOutBack(const float_t t);
|
fixed_t easingOutBack(const fixed_t t);
|
||||||
float_t easingInOutBack(const float_t t);
|
fixed_t easingInOutBack(const fixed_t t);
|
||||||
|
|||||||
@@ -5,9 +5,10 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "easing.h"
|
#include "easing.h"
|
||||||
|
#include "util/fixed.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
float_t time;
|
fixed_t time;
|
||||||
float_t value;
|
fixed_t value;
|
||||||
easingtype_t easing;
|
easingtype_t easing;
|
||||||
} keyframe_t;
|
} keyframe_t;
|
||||||
+55
-18
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2023 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
@@ -8,26 +8,19 @@
|
|||||||
#include "assert.h"
|
#include "assert.h"
|
||||||
#include "log/log.h"
|
#include "log/log.h"
|
||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
|
||||||
#ifdef DUSK_LINUX
|
#ifdef DUSK_THREAD_PTHREAD
|
||||||
#include <execinfo.h>
|
pthread_t ASSERT_MAIN_THREAD_ID = 0;
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
static void assertLogBacktrace(void) {
|
|
||||||
void *frames[64];
|
|
||||||
int count = backtrace(frames, 64);
|
|
||||||
char **symbols = backtrace_symbols(frames, count);
|
|
||||||
logError("Stack trace:\n");
|
|
||||||
if(symbols) {
|
|
||||||
for(int i = 0; i < count; i++) {
|
|
||||||
logError(" %s\n", symbols[i]);
|
|
||||||
}
|
|
||||||
free(symbols);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef DUSK_ASSERTIONS_FAKED
|
#ifndef DUSK_ASSERTIONS_FAKED
|
||||||
|
void assertInit(void) {
|
||||||
|
#ifdef DUSK_THREAD_PTHREAD
|
||||||
|
ASSERT_MAIN_THREAD_ID = pthread_self();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef DUSK_TEST_ASSERT
|
#ifdef DUSK_TEST_ASSERT
|
||||||
void assertTrueImpl(
|
void assertTrueImpl(
|
||||||
const char *file,
|
const char *file,
|
||||||
@@ -43,6 +36,25 @@
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
#ifdef DUSK_BACKTRACE
|
||||||
|
#include <execinfo.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static void assertLogBacktrace(void) {
|
||||||
|
void *frames[64];
|
||||||
|
int count = backtrace(frames, 64);
|
||||||
|
char **symbols = backtrace_symbols(frames, count);
|
||||||
|
memoryTrack(symbols);
|
||||||
|
logError("Stack trace:\n");
|
||||||
|
if(symbols) {
|
||||||
|
for(int i = 0; i < count; i++) {
|
||||||
|
logError(" %s\n", symbols[i]);
|
||||||
|
}
|
||||||
|
memoryFree(symbols);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void assertTrueImpl(
|
void assertTrueImpl(
|
||||||
const char *file,
|
const char *file,
|
||||||
const int32_t line,
|
const int32_t line,
|
||||||
@@ -56,7 +68,7 @@
|
|||||||
line,
|
line,
|
||||||
message
|
message
|
||||||
);
|
);
|
||||||
#ifdef DUSK_LINUX
|
#ifdef DUSK_BACKTRACE
|
||||||
assertLogBacktrace();
|
assertLogBacktrace();
|
||||||
#endif
|
#endif
|
||||||
abort();
|
abort();
|
||||||
@@ -130,4 +142,29 @@
|
|||||||
) {
|
) {
|
||||||
assertTrueImpl(file, line, stringCompare(a, b) == 0, message);
|
assertTrueImpl(file, line, stringCompare(a, b) == 0, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void assertIsMainThreadImpl(
|
||||||
|
const char *file,
|
||||||
|
const int32_t line,
|
||||||
|
const char *message
|
||||||
|
) {
|
||||||
|
#ifdef DUSK_THREAD_PTHREAD
|
||||||
|
assertTrueImpl(
|
||||||
|
file, line, pthread_self() == ASSERT_MAIN_THREAD_ID, message
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void assertNotMainThreadImpl(
|
||||||
|
const char *file,
|
||||||
|
const int32_t line,
|
||||||
|
const char *message
|
||||||
|
) {
|
||||||
|
#ifdef DUSK_THREAD_PTHREAD
|
||||||
|
assertTrueImpl(
|
||||||
|
file, line, pthread_self() != ASSERT_MAIN_THREAD_ID, message
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2023 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
@@ -16,7 +16,18 @@
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef DUSK_THREAD_PTHREAD
|
||||||
|
#include "thread/thread.h"
|
||||||
|
extern pthread_t ASSERT_MAIN_THREAD_ID;
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef DUSK_ASSERTIONS_FAKED
|
#ifndef DUSK_ASSERTIONS_FAKED
|
||||||
|
/**
|
||||||
|
* Initializes the assert system. Must be the very first call in engine
|
||||||
|
* startup.
|
||||||
|
*/
|
||||||
|
void assertInit(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assert a given value to be true.
|
* Assert a given value to be true.
|
||||||
*
|
*
|
||||||
@@ -121,6 +132,28 @@
|
|||||||
const char *message
|
const char *message
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the current thread is the main thread.
|
||||||
|
*
|
||||||
|
* @param message Message to throw against assertion failure.
|
||||||
|
*/
|
||||||
|
void assertIsMainThreadImpl(
|
||||||
|
const char *file,
|
||||||
|
const int32_t line,
|
||||||
|
const char *message
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the current thread is NOT the main thread.
|
||||||
|
*
|
||||||
|
* @param message Message to throw against assertion failure.
|
||||||
|
*/
|
||||||
|
void assertNotMainThreadImpl(
|
||||||
|
const char *file,
|
||||||
|
const int32_t line,
|
||||||
|
const char *message
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts a given value to be true.
|
* Asserts a given value to be true.
|
||||||
*
|
*
|
||||||
@@ -205,8 +238,28 @@
|
|||||||
#define assertStringEqual(a, b, message) \
|
#define assertStringEqual(a, b, message) \
|
||||||
assertStringEqualImpl(__FILE__, __LINE__, a, b, message)
|
assertStringEqualImpl(__FILE__, __LINE__, a, b, message)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the current thread is the main thread.
|
||||||
|
*
|
||||||
|
* @param message Message to throw against assertion failure.
|
||||||
|
*/
|
||||||
|
#define assertIsMainThread(message) \
|
||||||
|
assertIsMainThreadImpl(__FILE__, __LINE__, message)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the current thread is NOT the main thread.
|
||||||
|
*
|
||||||
|
* @param message Message to throw against assertion failure.
|
||||||
|
*/
|
||||||
|
#define assertNotMainThread(message) \
|
||||||
|
assertNotMainThreadImpl(__FILE__, __LINE__, message)
|
||||||
|
|
||||||
#else
|
#else
|
||||||
// If assertions are faked, we define the macros to do nothing.
|
// If assertions are faked, we define the macros to do nothing.
|
||||||
|
#define assertInit() ((void)0)
|
||||||
|
#define assertMainThreadInit() ((void)0)
|
||||||
|
#define assertIsMainThread(message) ((void)0)
|
||||||
|
#define assertNotMainThread(message) ((void)0)
|
||||||
#define assertTrue(x, message) ((void)0)
|
#define assertTrue(x, message) ((void)0)
|
||||||
#define assertFalse(x, message) ((void)0)
|
#define assertFalse(x, message) ((void)0)
|
||||||
#define assertUnreachable(message) ((void)0)
|
#define assertUnreachable(message) ((void)0)
|
||||||
@@ -215,11 +268,13 @@
|
|||||||
#define assertDeprecated(message) ((void)0)
|
#define assertDeprecated(message) ((void)0)
|
||||||
#define assertStrLenMax(str, len, message) ((void)0)
|
#define assertStrLenMax(str, len, message) ((void)0)
|
||||||
#define assertStrLenMin(str, len, message) ((void)0)
|
#define assertStrLenMin(str, len, message) ((void)0)
|
||||||
|
#define assertStringEqual(a, b, message) ((void)0)
|
||||||
|
#define assertIsMainThread(message) ((void)0)
|
||||||
|
#define assertNotMainThread(message) ((void)0)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Static Assertions
|
// Static Assertions
|
||||||
|
|
||||||
#define assertStructSize(struct, size) \
|
#define assertStructSize(struct, size) \
|
||||||
_Static_assert(sizeof(struct) == size, "Size of " #struct " must be " #size)
|
_Static_assert(sizeof(struct) == size, "Size of " #struct " must be " #size)
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,8 @@
|
|||||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
PUBLIC
|
PUBLIC
|
||||||
asset.c
|
asset.c
|
||||||
assetfile.c
|
|
||||||
assetcache.c
|
|
||||||
assetbatch.c
|
assetbatch.c
|
||||||
|
assetfile.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# Subdirs
|
# Subdirs
|
||||||
|
|||||||
+386
-16
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
@@ -11,17 +11,24 @@
|
|||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "engine/engine.h"
|
#include "engine/engine.h"
|
||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
|
#include "console/console.h"
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
asset_t ASSET;
|
asset_t ASSET;
|
||||||
|
|
||||||
errorret_t assetInit(void) {
|
errorret_t assetInit(void) {
|
||||||
memoryZero(&ASSET, sizeof(asset_t));
|
memoryZero(&ASSET, sizeof(asset_t));
|
||||||
|
|
||||||
|
for(size_t i = 0; i < ASSET_LOADING_COUNT_MAX; i++) {
|
||||||
|
threadMutexInit(&ASSET.loading[i].mutex);
|
||||||
|
}
|
||||||
|
|
||||||
// assetInitPlatform must either define ASSET.zip or throw an error.
|
// assetInitPlatform must either define ASSET.zip or throw an error.
|
||||||
errorChain(assetInitPlatform());
|
errorChain(assetInitPlatform());
|
||||||
assertNotNull(ASSET.zip, "Asset zip null without error.");
|
assertNotNull(ASSET.zip, "Asset zip null without error.");
|
||||||
|
threadInit(&ASSET.loadThread, assetUpdateAsync);
|
||||||
|
threadStart(&ASSET.loadThread);
|
||||||
|
|
||||||
assetCacheInit(&ASSET.cache);
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,26 +40,389 @@ bool_t assetFileExists(const char_t *filename) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetLoad(
|
assetentry_t * assetGetEntry(
|
||||||
const char_t *filename,
|
const char_t *name,
|
||||||
assetfileloader_t loader,
|
const assetloadertype_t type,
|
||||||
void *params,
|
assetloaderinput_t *input
|
||||||
void *output
|
|
||||||
) {
|
) {
|
||||||
assertStrLenMax(filename, ASSET_FILE_NAME_MAX, "Filename too long.");
|
// Is there an existing asset?
|
||||||
assertNotNull(output, "Output pointer cannot be NULL.");
|
assetentry_t *entry = ASSET.entries;
|
||||||
assertNotNull(loader, "Asset file loader cannot be NULL.");
|
do {
|
||||||
|
if(entry->type == ASSET_LOADER_TYPE_NULL) {
|
||||||
|
entry++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(stringEquals(entry->name, name)) {
|
||||||
|
assertTrue(entry->type == type, "Asset entry type mismatch.");
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
entry++;
|
||||||
|
} while(entry < ASSET.entries + ASSET_ENTRY_COUNT_MAX);
|
||||||
|
|
||||||
assetfile_t file;
|
// We did not find one existing, Find first available slot.
|
||||||
errorChain(assetFileInit(&file, filename, params, output));
|
entry = ASSET.entries;
|
||||||
errorChain(loader(&file));
|
do {
|
||||||
errorChain(assetFileDispose(&file));
|
if(entry->type != ASSET_LOADER_TYPE_NULL) {
|
||||||
|
entry++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(entry->state == ASSET_ENTRY_STATE_NOT_STARTED) {
|
||||||
|
assetEntryInit(entry, name, type, input);
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
entry++;
|
||||||
|
} while(entry < ASSET.entries + ASSET_ENTRY_COUNT_MAX);
|
||||||
|
|
||||||
|
assertUnreachable("No available asset entry slots.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t assetGetEntriesOfType(
|
||||||
|
assetentry_t **outEntries,
|
||||||
|
const assetloadertype_t type
|
||||||
|
) {
|
||||||
|
assertNotNull(outEntries, "Output entries cannot be NULL.");
|
||||||
|
|
||||||
|
uint32_t count = 0;
|
||||||
|
assetentry_t *entry = ASSET.entries;
|
||||||
|
do {
|
||||||
|
if(entry->type == type) {
|
||||||
|
outEntries[count++] = entry;
|
||||||
|
}
|
||||||
|
entry++;
|
||||||
|
} while(entry < ASSET.entries + ASSET_ENTRY_COUNT_MAX);
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetRequireLoaded(assetentry_t *entry) {
|
||||||
|
assertNotNull(entry, "Entry cannot be NULL.");
|
||||||
|
assertTrue(entry->type != ASSET_LOADER_TYPE_NULL, "Invalid loader type.");
|
||||||
|
assertIsMainThread("Currently only works on main thread.");
|
||||||
|
|
||||||
|
if(entry->state == ASSET_ENTRY_STATE_LOADED) {
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lock to prevent the reaper from collecting the entry mid-spin.
|
||||||
|
assetEntryLock(entry);
|
||||||
|
|
||||||
|
while(entry->state != ASSET_ENTRY_STATE_LOADED) {
|
||||||
|
usleep(1000);
|
||||||
|
errorret_t ret = assetUpdate();
|
||||||
|
if(errorIsNotOk(ret)) {
|
||||||
|
assetEntryUnlock(entry);
|
||||||
|
errorChain(ret);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assetEntryUnlock(entry);
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetDispose(void) {
|
errorret_t assetRequireDisposed(assetentry_t *entry) {
|
||||||
assetCacheDispose(&ASSET.cache);
|
assertNotNull(entry, "Entry cannot be NULL.");
|
||||||
|
assertTrue(entry->type != ASSET_LOADER_TYPE_NULL, "Invalid loader type.");
|
||||||
|
assertIsMainThread("Currently only works on main thread.");
|
||||||
|
|
||||||
|
if(entry->type == ASSET_LOADER_TYPE_NULL) {
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(
|
||||||
|
entry->state == ASSET_ENTRY_STATE_NOT_STARTED ||
|
||||||
|
entry->state == ASSET_ENTRY_STATE_ERROR
|
||||||
|
) {
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(
|
||||||
|
entry->refs.count == 0,
|
||||||
|
"Cannot require disposal of an entry with active references."
|
||||||
|
);
|
||||||
|
|
||||||
|
// Lock to prevent the reaper from collecting the entry mid-spin.
|
||||||
|
assetEntryLock(entry);
|
||||||
|
|
||||||
|
while(entry->type != ASSET_LOADER_TYPE_NULL) {
|
||||||
|
usleep(1000);
|
||||||
|
errorret_t ret = assetUpdate();
|
||||||
|
if(errorIsNotOk(ret)) {
|
||||||
|
assetEntryUnlock(entry);
|
||||||
|
errorChain(ret);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assetEntryUnlock(entry);
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
assetentry_t * assetLock(
|
||||||
|
const char_t *name,
|
||||||
|
const assetloadertype_t type,
|
||||||
|
assetloaderinput_t *input
|
||||||
|
) {
|
||||||
|
assetentry_t *entry = assetGetEntry(name, type, input);
|
||||||
|
assetEntryLock(entry);
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
void assetUnlock(const char_t *name) {
|
||||||
|
assertNotNull(name, "Name cannot be NULL.");
|
||||||
|
|
||||||
|
assetentry_t *entry = ASSET.entries;
|
||||||
|
do {
|
||||||
|
if(
|
||||||
|
entry->type != ASSET_LOADER_TYPE_NULL &&
|
||||||
|
stringEquals(entry->name, name)
|
||||||
|
) {
|
||||||
|
assetEntryUnlock(entry);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
entry++;
|
||||||
|
} while(entry < ASSET.entries + ASSET_ENTRY_COUNT_MAX);
|
||||||
|
|
||||||
|
assertUnreachable("Asset entry not found for unlock.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void assetUnlockEntry(assetentry_t *entry) {
|
||||||
|
assertNotNull(entry, "Entry cannot be NULL.");
|
||||||
|
assetEntryUnlock(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetUpdate(void) {
|
||||||
|
assertIsMainThread("assetUpdate must be called from the main thread.");
|
||||||
|
|
||||||
|
// Determine how many available loading slots we have.
|
||||||
|
assetloading_t *availableLoading[ASSET_LOADING_COUNT_MAX];
|
||||||
|
uint8_t availableLoadingCount = 0;
|
||||||
|
assetloading_t *loading = ASSET.loading;
|
||||||
|
assetentry_t *entry;
|
||||||
|
|
||||||
|
|
||||||
|
do {
|
||||||
|
// We only care about NULL entry references. Nothing async touches this so
|
||||||
|
// it's fine to use raw here.
|
||||||
|
if(loading->entry != NULL) {
|
||||||
|
loading++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
availableLoading[availableLoadingCount++] = loading;
|
||||||
|
loading++;
|
||||||
|
} while(loading < ASSET.loading + ASSET_LOADING_COUNT_MAX);
|
||||||
|
|
||||||
|
|
||||||
|
// Now we can check for pending asset entries, we can't do anything if there
|
||||||
|
// is no available slots though.
|
||||||
|
if(availableLoadingCount > 0) {
|
||||||
|
entry = ASSET.entries;
|
||||||
|
do {
|
||||||
|
// Is this asset "ready to start loading" ?
|
||||||
|
if(entry->type == ASSET_LOADER_TYPE_NULL) {
|
||||||
|
entry++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We only care about assets not started.
|
||||||
|
if(entry->state != ASSET_ENTRY_STATE_NOT_STARTED) {
|
||||||
|
entry++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pop a loading slot for this asset entry.
|
||||||
|
loading = availableLoading[--availableLoadingCount];
|
||||||
|
|
||||||
|
// Start loading this asset.
|
||||||
|
assetEntryStartLoading(entry, loading);
|
||||||
|
entry++;
|
||||||
|
|
||||||
|
// Did we run out of loading slots?
|
||||||
|
if(availableLoadingCount == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while(entry < ASSET.entries + ASSET_ENTRY_COUNT_MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now walk over all the loading slots and see what needs to be done.
|
||||||
|
loading = ASSET.loading;
|
||||||
|
do {
|
||||||
|
// Is the loading slot in use? Entry can only be modified synchronously.
|
||||||
|
if(loading->entry == NULL) {
|
||||||
|
loading++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lock the loading slot. This will prevent any async modifications.
|
||||||
|
threadMutexLock(&loading->mutex);
|
||||||
|
|
||||||
|
// Check the state of the entry.
|
||||||
|
switch(loading->entry->state) {
|
||||||
|
// This thing is pending synchronous loading.
|
||||||
|
case ASSET_ENTRY_STATE_PENDING_SYNC:
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_LOADING_SYNC;
|
||||||
|
// Unlock before calling loadSync. The sync loader may re-enter
|
||||||
|
// assetUpdate (e.g. a script loading another asset), and the async
|
||||||
|
// thread never touches LOADING_SYNC entries, so this is safe.
|
||||||
|
threadMutexUnlock(&loading->mutex);
|
||||||
|
|
||||||
|
errorret_t ret = (
|
||||||
|
ASSET_LOADER_CALLBACKS[loading->type].loadSync(loading)
|
||||||
|
);
|
||||||
|
|
||||||
|
// After a sync load, these are the only valid states.
|
||||||
|
assertTrue(
|
||||||
|
loading->entry->state == ASSET_ENTRY_STATE_LOADED ||
|
||||||
|
loading->entry->state == ASSET_ENTRY_STATE_ERROR ||
|
||||||
|
loading->entry->state == ASSET_ENTRY_STATE_PENDING_SYNC ||
|
||||||
|
loading->entry->state == ASSET_ENTRY_STATE_PENDING_ASYNC,
|
||||||
|
"Loader did not set entry state to loaded or error on finished load."
|
||||||
|
);
|
||||||
|
|
||||||
|
if(errorIsNotOk(ret)) {
|
||||||
|
errorCatch(errorPrint(ret));
|
||||||
|
assertTrue(
|
||||||
|
loading->entry->state == ASSET_ENTRY_STATE_ERROR,
|
||||||
|
"Loader did not set entry state to error on failed load."
|
||||||
|
);
|
||||||
|
} else if(loading->entry->state == ASSET_ENTRY_STATE_LOADED) {
|
||||||
|
eventInvoke(&loading->entry->onLoaded, loading->entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
loading++;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_ENTRY_STATE_LOADING_SYNC:
|
||||||
|
// A re-entrant assetUpdate call (e.g. from a script loading another
|
||||||
|
// asset) will see this entry mid-sync-load. Skip it.
|
||||||
|
threadMutexUnlock(&loading->mutex);
|
||||||
|
loading++;
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Done loading, we can just free it up.
|
||||||
|
case ASSET_ENTRY_STATE_LOADED:
|
||||||
|
loading->entry = NULL;
|
||||||
|
threadMutexUnlock(&loading->mutex);
|
||||||
|
loading++;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_ENTRY_STATE_ERROR: {
|
||||||
|
assetentry_t *errEntry = loading->entry;
|
||||||
|
loading->entry = NULL;
|
||||||
|
threadMutexUnlock(&loading->mutex);
|
||||||
|
eventInvoke(&errEntry->onError, errEntry);
|
||||||
|
errorThrow("Failed to load asset asynchronously.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
threadMutexUnlock(&loading->mutex);
|
||||||
|
loading++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} while(loading < ASSET.loading + ASSET_LOADING_COUNT_MAX);
|
||||||
|
|
||||||
|
|
||||||
|
// Reap unused entries.
|
||||||
|
entry = ASSET.entries;
|
||||||
|
do {
|
||||||
|
if(entry->state != ASSET_ENTRY_STATE_LOADED) {
|
||||||
|
entry++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(entry->type == ASSET_LOADER_TYPE_NULL) {
|
||||||
|
entry++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(entry->refs.count > 0) {
|
||||||
|
entry++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
consolePrint("Reaping asset %s", entry->name);
|
||||||
|
errorChain(assetEntryDispose(entry));
|
||||||
|
entry++;
|
||||||
|
} while(entry < ASSET.entries + ASSET_ENTRY_COUNT_MAX);
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
void assetUpdateAsync(thread_t *thread) {
|
||||||
|
assertNotMainThread("assetUpdateAsync must not run on the main thread.");
|
||||||
|
|
||||||
|
while(!threadShouldStop(thread)) {
|
||||||
|
// Walk over each asset
|
||||||
|
assetloading_t *loading;
|
||||||
|
loading = ASSET.loading;
|
||||||
|
|
||||||
|
do {
|
||||||
|
threadMutexLock(&loading->mutex);
|
||||||
|
|
||||||
|
if(loading->entry == NULL) {
|
||||||
|
threadMutexUnlock(&loading->mutex);
|
||||||
|
loading++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(loading->entry->state) {
|
||||||
|
case ASSET_ENTRY_STATE_PENDING_ASYNC:
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_LOADING_ASYNC;
|
||||||
|
assertNotNull(
|
||||||
|
ASSET_LOADER_CALLBACKS[loading->type].loadAsync,
|
||||||
|
"Loader does not support async loading."
|
||||||
|
);
|
||||||
|
errorret_t ret = (
|
||||||
|
ASSET_LOADER_CALLBACKS[loading->type].loadAsync(loading)
|
||||||
|
);
|
||||||
|
|
||||||
|
if(errorIsNotOk(ret)) {
|
||||||
|
errorCatch(errorPrint(ret));
|
||||||
|
assertTrue(
|
||||||
|
loading->entry->state == ASSET_ENTRY_STATE_ERROR,
|
||||||
|
"Loader did not set entry state to error on failed load."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
threadMutexUnlock(&loading->mutex);
|
||||||
|
loading++;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_ENTRY_STATE_LOADING_ASYNC:
|
||||||
|
assertUnreachable(
|
||||||
|
"Entry is in a pending async state still?"
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
threadMutexUnlock(&loading->mutex);
|
||||||
|
loading++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} while(loading < ASSET.loading + ASSET_LOADING_COUNT_MAX);
|
||||||
|
|
||||||
|
if(threadShouldStop(thread)) break;
|
||||||
|
usleep(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetDispose(void) {
|
||||||
|
assertIsMainThread("Must be called from the main thread.");
|
||||||
|
threadStop(&ASSET.loadThread);
|
||||||
|
|
||||||
|
// Dispose every non-null entry so type-specific dispose callbacks
|
||||||
|
// (e.g. assetScriptDispose freeing jerry values) run before the
|
||||||
|
// scripting engine is torn down.
|
||||||
|
assetentry_t *entry = ASSET.entries;
|
||||||
|
do {
|
||||||
|
if(entry->type != ASSET_LOADER_TYPE_NULL) {
|
||||||
|
errorChain(assetEntryDispose(entry));
|
||||||
|
}
|
||||||
|
entry++;
|
||||||
|
} while(entry < ASSET.entries + ASSET_ENTRY_COUNT_MAX);
|
||||||
|
|
||||||
|
// Cleanup zip file.
|
||||||
if(ASSET.zip != NULL) {
|
if(ASSET.zip != NULL) {
|
||||||
if(zip_close(ASSET.zip) != 0) {
|
if(zip_close(ASSET.zip) != 0) {
|
||||||
errorThrow("Failed to close asset zip archive.");
|
errorThrow("Failed to close asset zip archive.");
|
||||||
|
|||||||
+101
-14
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
@@ -9,7 +9,9 @@
|
|||||||
#include "error/error.h"
|
#include "error/error.h"
|
||||||
#include "asset/assetplatform.h"
|
#include "asset/assetplatform.h"
|
||||||
#include "assetfile.h"
|
#include "assetfile.h"
|
||||||
#include "assetcache.h"
|
#include "thread/thread.h"
|
||||||
|
#include "asset/loader/assetentry.h"
|
||||||
|
#include "asset/loader/assetloading.h"
|
||||||
|
|
||||||
#ifndef assetInitPlatform
|
#ifndef assetInitPlatform
|
||||||
#error "Platform must define assetInitPlatform function."
|
#error "Platform must define assetInitPlatform function."
|
||||||
@@ -21,16 +23,27 @@
|
|||||||
#define ASSET_FILE_NAME "dusk.dsk"
|
#define ASSET_FILE_NAME "dusk.dsk"
|
||||||
#define ASSET_HEADER_SIZE 3
|
#define ASSET_HEADER_SIZE 3
|
||||||
|
|
||||||
|
#define ASSET_LOADING_COUNT_MAX 4
|
||||||
|
#define ASSET_ENTRY_COUNT_MAX 128
|
||||||
|
|
||||||
typedef struct asset_s {
|
typedef struct asset_s {
|
||||||
zip_t *zip;
|
zip_t *zip;
|
||||||
assetplatform_t platform;
|
assetplatform_t platform;
|
||||||
assetcache_t cache;
|
|
||||||
|
// Background loading thread.
|
||||||
|
thread_t loadThread;
|
||||||
|
|
||||||
|
// Assets that are mid loading.
|
||||||
|
assetloading_t loading[ASSET_LOADING_COUNT_MAX];
|
||||||
|
assetentry_t entries[ASSET_ENTRY_COUNT_MAX];
|
||||||
} asset_t;
|
} asset_t;
|
||||||
|
|
||||||
extern asset_t ASSET;
|
extern asset_t ASSET;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the asset system.
|
* Initializes the asset system.
|
||||||
|
*
|
||||||
|
* @return An error code if the asset system could not be initialized properly.
|
||||||
*/
|
*/
|
||||||
errorret_t assetInit(void);
|
errorret_t assetInit(void);
|
||||||
|
|
||||||
@@ -43,21 +56,95 @@ errorret_t assetInit(void);
|
|||||||
bool_t assetFileExists(const char_t *filename);
|
bool_t assetFileExists(const char_t *filename);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads an asset by its filename,
|
* Gets, or creates, a new asset entry. Internal - prefer assetLock.
|
||||||
*
|
*
|
||||||
* @param filename The filename of the asset to retrieve.
|
* @param name Filename of the asset.
|
||||||
* @param loader Loader to use for loading the asset file.
|
* @param type Type of the asset.
|
||||||
* @param params Parameters to pass to the loader.
|
* @param input Loader-specific parameters.
|
||||||
* @param output The output pointer to store the loaded asset data.
|
|
||||||
* @return An error code if the asset could not be loaded.
|
|
||||||
*/
|
*/
|
||||||
errorret_t assetLoad(
|
assetentry_t * assetGetEntry(
|
||||||
const char_t *filename,
|
const char_t *name,
|
||||||
assetfileloader_t loader,
|
const assetloadertype_t type,
|
||||||
void *params,
|
assetloaderinput_t *input
|
||||||
void *output
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all asset entries of a given type.
|
||||||
|
*
|
||||||
|
* @param outEntries Output array to write the entries to.
|
||||||
|
* @param type Type of the asset entries to get.
|
||||||
|
* @return The number of entries written to outEntries.
|
||||||
|
*/
|
||||||
|
uint32_t assetGetEntriesOfType(
|
||||||
|
assetentry_t **outEntries,
|
||||||
|
const assetloadertype_t type
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets, creates, and locks an asset entry. The asset will begin loading on
|
||||||
|
* the next assetUpdate. Call assetUnlock when done to allow the entry to be
|
||||||
|
* reclaimed.
|
||||||
|
*
|
||||||
|
* @param name Filename of the asset.
|
||||||
|
* @param type Type of the asset.
|
||||||
|
* @param input Loader-specific parameters.
|
||||||
|
* @return The locked asset entry.
|
||||||
|
*/
|
||||||
|
assetentry_t * assetLock(
|
||||||
|
const char_t *name,
|
||||||
|
const assetloadertype_t type,
|
||||||
|
assetloaderinput_t *input
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Releases a lock on an asset entry by name. When all locks are released the
|
||||||
|
* entry will be reclaimed at the start of the next assetUpdate.
|
||||||
|
*
|
||||||
|
* @param name Filename of the asset to unlock.
|
||||||
|
*/
|
||||||
|
void assetUnlock(const char_t *name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Releases a lock on an asset entry by pointer. When all locks are released
|
||||||
|
* the entry will be reclaimed at the start of the next assetUpdate.
|
||||||
|
*
|
||||||
|
* @param entry The asset entry to unlock.
|
||||||
|
*/
|
||||||
|
void assetUnlockEntry(assetentry_t *entry);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Requires an asset entry to be loaded. This will block until the asset entry
|
||||||
|
* is fully loaded.
|
||||||
|
*
|
||||||
|
* @param entry The asset entry to require.
|
||||||
|
* @return An error code if the asset entry could not be loaded properly.
|
||||||
|
*/
|
||||||
|
errorret_t assetRequireLoaded(assetentry_t *entry);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Requires an asset entry to be disposed. This will block until the asset entry
|
||||||
|
* is fully disposed.
|
||||||
|
*
|
||||||
|
* @param entry The asset entry to require disposal of.
|
||||||
|
* @return An error code if the asset entry could not be disposed properly.
|
||||||
|
*/
|
||||||
|
errorret_t assetRequireDisposed(assetentry_t *entry);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the asset system.
|
||||||
|
*
|
||||||
|
* @return An error code if the asset system could not be updated properly.
|
||||||
|
*/
|
||||||
|
errorret_t assetUpdate(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the background asset loading thread. The thread runs assetUpdate
|
||||||
|
* in a loop with a short sleep until stopped.
|
||||||
|
*
|
||||||
|
* @param thread The thread runner.
|
||||||
|
*/
|
||||||
|
void assetUpdateAsync(thread_t *thread);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disposes/cleans up the asset system.
|
* Disposes/cleans up the asset system.
|
||||||
*
|
*
|
||||||
|
|||||||
+135
-51
@@ -7,75 +7,159 @@
|
|||||||
|
|
||||||
#include "assetbatch.h"
|
#include "assetbatch.h"
|
||||||
#include "asset.h"
|
#include "asset.h"
|
||||||
#include "util/memory.h"
|
|
||||||
#include "util/string.h"
|
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
void assetBatchInit(
|
void assetBatchInit(
|
||||||
assetbatch_t *batch,
|
assetbatch_t *batch,
|
||||||
assetbatchcomplete_t onComplete,
|
const uint16_t count,
|
||||||
assetbatcherror_t onError,
|
const assetbatchdesc_t *descs
|
||||||
void *context
|
|
||||||
) {
|
) {
|
||||||
|
assertNotNull(batch, "Batch cannot be NULL.");
|
||||||
|
assertNotNull(descs, "Descs cannot be NULL.");
|
||||||
|
assertTrue(count > 0, "Count must be greater than 0.");
|
||||||
|
assertTrue(
|
||||||
|
count <= ASSET_BATCH_COUNT_MAX, "Count exceeds ASSET_BATCH_COUNT_MAX."
|
||||||
|
);
|
||||||
|
|
||||||
memoryZero(batch, sizeof(assetbatch_t));
|
memoryZero(batch, sizeof(assetbatch_t));
|
||||||
batch->onComplete = onComplete;
|
batch->count = count;
|
||||||
batch->onError = onError;
|
|
||||||
batch->context = context;
|
eventInit(
|
||||||
|
&batch->onLoaded,
|
||||||
|
batch->onLoadedCallbacks, batch->onLoadedUsers, ASSET_BATCH_EVENT_MAX
|
||||||
|
);
|
||||||
|
eventInit(
|
||||||
|
&batch->onEntryLoaded,
|
||||||
|
batch->onEntryLoadedCallbacks,
|
||||||
|
batch->onEntryLoadedUsers,
|
||||||
|
ASSET_BATCH_EVENT_MAX
|
||||||
|
);
|
||||||
|
eventInit(
|
||||||
|
&batch->onError,
|
||||||
|
batch->onErrorCallbacks, batch->onErrorUsers, ASSET_BATCH_EVENT_MAX
|
||||||
|
);
|
||||||
|
eventInit(
|
||||||
|
&batch->onEntryError,
|
||||||
|
batch->onEntryErrorCallbacks,
|
||||||
|
batch->onEntryErrorUsers,
|
||||||
|
ASSET_BATCH_EVENT_MAX
|
||||||
|
);
|
||||||
|
|
||||||
|
for(uint16_t i = 0; i < count; i++) {
|
||||||
|
batch->inputs[i] = descs[i].input;
|
||||||
|
batch->entries[i] = assetLock(
|
||||||
|
descs[i].path, descs[i].type, &batch->inputs[i]
|
||||||
|
);
|
||||||
|
|
||||||
|
if(batch->entries[i]->state == ASSET_ENTRY_STATE_LOADED) {
|
||||||
|
// Already loaded (cached) - count it now, no subscription needed.
|
||||||
|
batch->loadedCount++;
|
||||||
|
} else if(batch->entries[i]->state == ASSET_ENTRY_STATE_ERROR) {
|
||||||
|
batch->errorCount++;
|
||||||
|
} else {
|
||||||
|
eventSubscribe(
|
||||||
|
&batch->entries[i]->onLoaded, assetBatchEntryOnLoadedCb, batch
|
||||||
|
);
|
||||||
|
eventSubscribe(
|
||||||
|
&batch->entries[i]->onError, assetBatchEntryOnErrorCb, batch
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void assetBatchAdd(
|
void assetBatchLock(assetbatch_t *batch) {
|
||||||
assetbatch_t *batch,
|
assertNotNull(batch, "Batch cannot be NULL.");
|
||||||
const char_t *path,
|
for(uint16_t i = 0; i < batch->count; i++) {
|
||||||
assetfileloader_t loader,
|
assetEntryLock(batch->entries[i]);
|
||||||
const void *params,
|
|
||||||
size_t paramsSize,
|
|
||||||
size_t dataSize,
|
|
||||||
assetcachedisposer_t disposer,
|
|
||||||
void **outPtr
|
|
||||||
) {
|
|
||||||
assertTrue(batch->count < ASSET_BATCH_MAX, "Asset batch is full.");
|
|
||||||
assertTrue(paramsSize <= ASSET_BATCH_PARAMS_SIZE, "Asset batch params too large.");
|
|
||||||
assertNotNull(outPtr, "Batch output pointer cannot be NULL.");
|
|
||||||
|
|
||||||
assetbatchitem_t *item = &batch->items[batch->count++];
|
|
||||||
stringCopy(item->path, path, ASSET_FILE_NAME_MAX);
|
|
||||||
item->loader = loader;
|
|
||||||
if (params != NULL && paramsSize > 0) {
|
|
||||||
memoryCopy(item->params, params, paramsSize);
|
|
||||||
}
|
}
|
||||||
item->dataSize = dataSize;
|
|
||||||
item->disposer = disposer;
|
|
||||||
item->outPtr = outPtr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetBatchLoad(assetbatch_t *batch) {
|
void assetBatchUnlock(assetbatch_t *batch) {
|
||||||
for (uint8_t i = 0; i < batch->count; i++) {
|
assertNotNull(batch, "Batch cannot be NULL.");
|
||||||
assetbatchitem_t *item = &batch->items[i];
|
for(uint16_t i = 0; i < batch->count; i++) {
|
||||||
|
assetEntryUnlock(batch->entries[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void *cached = assetCacheLookup(&ASSET.cache, item->path);
|
bool_t assetBatchIsLoaded(const assetbatch_t *batch) {
|
||||||
if (cached != NULL) {
|
assertNotNull(batch, "Batch cannot be NULL.");
|
||||||
assetCacheRetain(&ASSET.cache, item->path);
|
for(uint16_t i = 0; i < batch->count; i++) {
|
||||||
*item->outPtr = cached;
|
if(batch->entries[i]->state != ASSET_ENTRY_STATE_LOADED) return false;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void *data = memoryAllocate(item->dataSize);
|
bool_t assetBatchHasError(const assetbatch_t *batch) {
|
||||||
errorret_t err = assetLoad(item->path, item->loader, item->params, data);
|
assertNotNull(batch, "Batch cannot be NULL.");
|
||||||
if (err.code != ERROR_OK) {
|
for(uint16_t i = 0; i < batch->count; i++) {
|
||||||
memoryFree(data);
|
if(batch->entries[i]->state == ASSET_ENTRY_STATE_ERROR) return true;
|
||||||
if (batch->onError != NULL) {
|
|
||||||
batch->onError(batch, batch->context, err);
|
|
||||||
}
|
|
||||||
return errorChainImpl(err, __FILE__, __func__, __LINE__);
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
assetCacheInsert(&ASSET.cache, item->path, data, item->disposer);
|
errorret_t assetBatchRequireLoaded(assetbatch_t *batch) {
|
||||||
*item->outPtr = data;
|
assertNotNull(batch, "Batch cannot be NULL.");
|
||||||
}
|
|
||||||
|
|
||||||
if (batch->onComplete != NULL) {
|
bool_t allDone;
|
||||||
batch->onComplete(batch, batch->context);
|
do {
|
||||||
|
allDone = true;
|
||||||
|
for(uint16_t i = 0; i < batch->count; i++) {
|
||||||
|
const assetentrystate_t state = batch->entries[i]->state;
|
||||||
|
if(state == ASSET_ENTRY_STATE_ERROR) {
|
||||||
|
errorThrow("Asset '%s' failed to load.", batch->entries[i]->name);
|
||||||
}
|
}
|
||||||
|
if(state != ASSET_ENTRY_STATE_LOADED) {
|
||||||
|
allDone = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!allDone) {
|
||||||
|
usleep(1000);
|
||||||
|
errorChain(assetUpdate());
|
||||||
|
}
|
||||||
|
} while(!allDone);
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void assetBatchDispose(assetbatch_t *batch) {
|
||||||
|
assertNotNull(batch, "Batch cannot be NULL.");
|
||||||
|
for(uint16_t i = 0; i < batch->count; i++) {
|
||||||
|
if(batch->entries[i]) {
|
||||||
|
// Unsubscribe while we still hold a lock so the entry is live.
|
||||||
|
eventUnsubscribe(&batch->entries[i]->onLoaded, assetBatchEntryOnLoadedCb);
|
||||||
|
eventUnsubscribe(&batch->entries[i]->onError, assetBatchEntryOnErrorCb);
|
||||||
|
assetUnlockEntry(batch->entries[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
memoryZero(batch, sizeof(assetbatch_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
void assetBatchEntryOnLoadedCb(void *params, void *user) {
|
||||||
|
assetentry_t *entry = (assetentry_t *)params;
|
||||||
|
assetbatch_t *batch = (assetbatch_t *)user;
|
||||||
|
|
||||||
|
batch->loadedCount++;
|
||||||
|
eventInvoke(&batch->onEntryLoaded, entry);
|
||||||
|
|
||||||
|
if((uint16_t)(batch->loadedCount + batch->errorCount) >= batch->count) {
|
||||||
|
if(batch->errorCount == 0) {
|
||||||
|
eventInvoke(&batch->onLoaded, batch);
|
||||||
|
} else {
|
||||||
|
eventInvoke(&batch->onError, batch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void assetBatchEntryOnErrorCb(void *params, void *user) {
|
||||||
|
assetentry_t *entry = (assetentry_t *)params;
|
||||||
|
assetbatch_t *batch = (assetbatch_t *)user;
|
||||||
|
|
||||||
|
batch->errorCount++;
|
||||||
|
eventInvoke(&batch->onEntryError, entry);
|
||||||
|
|
||||||
|
if((uint16_t)(batch->loadedCount + batch->errorCount) >= batch->count) {
|
||||||
|
eventInvoke(&batch->onError, batch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+97
-63
@@ -6,85 +6,119 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "assetcache.h"
|
#include "asset/loader/assetentry.h"
|
||||||
#include "error/error.h"
|
#include "asset/loader/assetloader.h"
|
||||||
|
#include "event/event.h"
|
||||||
|
|
||||||
#ifndef ASSET_BATCH_MAX
|
#define ASSET_BATCH_COUNT_MAX 64
|
||||||
#define ASSET_BATCH_MAX 16
|
#define ASSET_BATCH_EVENT_MAX 4
|
||||||
#endif
|
|
||||||
|
|
||||||
#define ASSET_BATCH_PARAMS_SIZE 16
|
|
||||||
|
|
||||||
typedef struct assetbatch_s assetbatch_t;
|
|
||||||
|
|
||||||
typedef void (*assetbatchcomplete_t)(assetbatch_t *batch, void *context);
|
|
||||||
typedef void (*assetbatcherror_t)(
|
|
||||||
assetbatch_t *batch,
|
|
||||||
void *context,
|
|
||||||
errorret_t error
|
|
||||||
);
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char_t path[ASSET_FILE_NAME_MAX];
|
const char_t *path;
|
||||||
assetfileloader_t loader;
|
assetloadertype_t type;
|
||||||
uint8_t params[ASSET_BATCH_PARAMS_SIZE];
|
assetloaderinput_t input;
|
||||||
size_t dataSize;
|
} assetbatchdesc_t;
|
||||||
assetcachedisposer_t disposer;
|
|
||||||
void **outPtr;
|
|
||||||
} assetbatchitem_t;
|
|
||||||
|
|
||||||
struct assetbatch_s {
|
typedef struct {
|
||||||
assetbatchitem_t items[ASSET_BATCH_MAX];
|
assetentry_t *entries[ASSET_BATCH_COUNT_MAX];
|
||||||
uint8_t count;
|
assetloaderinput_t inputs[ASSET_BATCH_COUNT_MAX];
|
||||||
assetbatchcomplete_t onComplete;
|
uint16_t count;
|
||||||
assetbatcherror_t onError;
|
uint16_t loadedCount;
|
||||||
void *context;
|
uint16_t errorCount;
|
||||||
};
|
|
||||||
|
/** Fires once when every entry loaded. params = assetbatch_t * */
|
||||||
|
event_t onLoaded;
|
||||||
|
eventcallback_t onLoadedCallbacks[ASSET_BATCH_EVENT_MAX];
|
||||||
|
void *onLoadedUsers[ASSET_BATCH_EVENT_MAX];
|
||||||
|
|
||||||
|
/** Fires each time a single entry loads. params = assetentry_t * */
|
||||||
|
event_t onEntryLoaded;
|
||||||
|
eventcallback_t onEntryLoadedCallbacks[ASSET_BATCH_EVENT_MAX];
|
||||||
|
void *onEntryLoadedUsers[ASSET_BATCH_EVENT_MAX];
|
||||||
|
|
||||||
|
/** Fires when all entries finish (any with errors). params: assetbatch_t * */
|
||||||
|
event_t onError;
|
||||||
|
eventcallback_t onErrorCallbacks[ASSET_BATCH_EVENT_MAX];
|
||||||
|
void *onErrorUsers[ASSET_BATCH_EVENT_MAX];
|
||||||
|
|
||||||
|
/** Fires each time a single entry errors. params = assetentry_t * */
|
||||||
|
event_t onEntryError;
|
||||||
|
eventcallback_t onEntryErrorCallbacks[ASSET_BATCH_EVENT_MAX];
|
||||||
|
void *onEntryErrorUsers[ASSET_BATCH_EVENT_MAX];
|
||||||
|
} assetbatch_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a batch, optionally with completion and error callbacks.
|
* Initialises the batch from an array of descriptors. Each entry is locked
|
||||||
|
* and queued for loading immediately.
|
||||||
*
|
*
|
||||||
* @param batch The batch to initialize.
|
* @param batch Batch to initialise.
|
||||||
* @param onComplete Called when all items have loaded successfully. May be NULL.
|
* @param descs Array of entry descriptors (need not outlive this call).
|
||||||
* @param onError Called if any item fails to load. May be NULL.
|
* @param count Number of descriptors (must be <= ASSET_BATCH_COUNT_MAX).
|
||||||
* @param context Passed through to both callbacks.
|
|
||||||
*/
|
*/
|
||||||
void assetBatchInit(
|
void assetBatchInit(
|
||||||
assetbatch_t *batch,
|
assetbatch_t *batch,
|
||||||
assetbatchcomplete_t onComplete,
|
uint16_t count,
|
||||||
assetbatcherror_t onError,
|
const assetbatchdesc_t *descs
|
||||||
void *context
|
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an item to the batch. Params are copied into inline storage.
|
* Acquires one additional lock on every entry in the batch.
|
||||||
*
|
*
|
||||||
* @param batch The batch to add to.
|
* @param batch Batch to lock.
|
||||||
* @param path Asset path, used as the cache key.
|
|
||||||
* @param loader The loader function for this asset type.
|
|
||||||
* @param params Loader-specific parameters. Copied — safe to pass stack address.
|
|
||||||
* @param paramsSize Size of params in bytes. Must be <= ASSET_BATCH_PARAMS_SIZE.
|
|
||||||
* @param dataSize Size in bytes to allocate for the output struct.
|
|
||||||
* @param disposer Called on the data before freeing when released. May be NULL.
|
|
||||||
* @param outPtr Caller's pointer variable. Set to the cache-owned data on load.
|
|
||||||
*/
|
*/
|
||||||
void assetBatchAdd(
|
void assetBatchLock(assetbatch_t *batch);
|
||||||
assetbatch_t *batch,
|
|
||||||
const char_t *path,
|
|
||||||
assetfileloader_t loader,
|
|
||||||
const void *params,
|
|
||||||
size_t paramsSize,
|
|
||||||
size_t dataSize,
|
|
||||||
assetcachedisposer_t disposer,
|
|
||||||
void **outPtr
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads all items in the batch, checking the cache for each. Fires onComplete
|
* Releases one lock from every entry in the batch. When an entry's lock
|
||||||
* on success or onError on the first failure. Also returns the error for
|
* count reaches zero it will be reaped on the next assetUpdate.
|
||||||
* synchronous callers.
|
|
||||||
*
|
*
|
||||||
* @param batch The batch to execute.
|
* @param batch Batch to unlock.
|
||||||
* @return Error if any item failed to load.
|
|
||||||
*/
|
*/
|
||||||
errorret_t assetBatchLoad(assetbatch_t *batch);
|
void assetBatchUnlock(assetbatch_t *batch);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if every entry in the batch has finished loading.
|
||||||
|
*
|
||||||
|
* @param batch Batch to query.
|
||||||
|
*/
|
||||||
|
bool_t assetBatchIsLoaded(const assetbatch_t *batch);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if any entry in the batch is in an error state.
|
||||||
|
*
|
||||||
|
* @param batch Batch to query.
|
||||||
|
*/
|
||||||
|
bool_t assetBatchHasError(const assetbatch_t *batch);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Blocks until every entry is loaded. Returns an error if any entry fails.
|
||||||
|
*
|
||||||
|
* @param batch Batch to wait on.
|
||||||
|
*/
|
||||||
|
errorret_t assetBatchRequireLoaded(assetbatch_t *batch);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Releases the batch's lock on every entry and clears the batch. After this
|
||||||
|
* call the batch struct may be reused with assetBatchInit.
|
||||||
|
*
|
||||||
|
* @param batch Batch to dispose.
|
||||||
|
*/
|
||||||
|
void assetBatchDispose(assetbatch_t *batch);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event trampoline invoked when a batch entry finishes loading.
|
||||||
|
* Increments the loaded counter and fires batch-level events.
|
||||||
|
*
|
||||||
|
* @param params The loaded assetentry_t pointer.
|
||||||
|
* @param user The owning assetbatch_t pointer.
|
||||||
|
*/
|
||||||
|
void assetBatchEntryOnLoadedCb(void *params, void *user);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event trampoline invoked when a batch entry fails to load.
|
||||||
|
* Increments the error counter and fires batch-level events.
|
||||||
|
*
|
||||||
|
* @param params The errored assetentry_t pointer.
|
||||||
|
* @param user The owning assetbatch_t pointer.
|
||||||
|
*/
|
||||||
|
void assetBatchEntryOnErrorCb(void *params, void *user);
|
||||||
|
|||||||
@@ -1,81 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2026 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "assetcache.h"
|
|
||||||
#include "util/memory.h"
|
|
||||||
#include "util/string.h"
|
|
||||||
#include "assert/assert.h"
|
|
||||||
|
|
||||||
void assetCacheInit(assetcache_t *cache) {
|
|
||||||
memoryZero(cache, sizeof(assetcache_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
void *assetCacheLookup(assetcache_t *cache, const char_t *path) {
|
|
||||||
for(uint8_t i = 0; i < cache->count; i++) {
|
|
||||||
if(stringCompare(cache->entries[i].path, path) == 0) {
|
|
||||||
return cache->entries[i].data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void assetCacheInsert(
|
|
||||||
assetcache_t *cache,
|
|
||||||
const char_t *path,
|
|
||||||
void *data,
|
|
||||||
assetcachedisposer_t disposer
|
|
||||||
) {
|
|
||||||
assertTrue(cache->count < ASSET_CACHE_MAX, "Asset cache is full.");
|
|
||||||
|
|
||||||
assetcacheentry_t *entry = &cache->entries[cache->count++];
|
|
||||||
stringCopy(entry->path, path, ASSET_FILE_NAME_MAX);
|
|
||||||
entry->data = data;
|
|
||||||
entry->refcount = 1;
|
|
||||||
entry->disposer = disposer;
|
|
||||||
}
|
|
||||||
|
|
||||||
void assetCacheRetain(assetcache_t *cache, const char_t *path) {
|
|
||||||
for (uint8_t i = 0; i < cache->count; i++) {
|
|
||||||
if (stringCompare(cache->entries[i].path, path) == 0) {
|
|
||||||
cache->entries[i].refcount++;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assertTrue(false, "Asset not found in cache for retain.");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void assetCacheEntryDispose(assetcacheentry_t *entry) {
|
|
||||||
if (entry->disposer != NULL) {
|
|
||||||
entry->disposer(entry->data);
|
|
||||||
}
|
|
||||||
memoryFree(entry->data);
|
|
||||||
}
|
|
||||||
|
|
||||||
void assetCacheRelease(assetcache_t *cache, const char_t *path) {
|
|
||||||
for (uint8_t i = 0; i < cache->count; i++) {
|
|
||||||
if (stringCompare(cache->entries[i].path, path) == 0) {
|
|
||||||
assetcacheentry_t *entry = &cache->entries[i];
|
|
||||||
entry->refcount--;
|
|
||||||
if (entry->refcount == 0) {
|
|
||||||
assetCacheEntryDispose(entry);
|
|
||||||
for (uint8_t j = i; j < cache->count - 1; j++) {
|
|
||||||
cache->entries[j] = cache->entries[j + 1];
|
|
||||||
}
|
|
||||||
cache->count--;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assertTrue(false, "Asset not found in cache for release.");
|
|
||||||
}
|
|
||||||
|
|
||||||
void assetCacheDispose(assetcache_t *cache) {
|
|
||||||
for (uint8_t i = 0; i < cache->count; i++) {
|
|
||||||
assetCacheEntryDispose(&cache->entries[i]);
|
|
||||||
}
|
|
||||||
memoryZero(cache, sizeof(assetcache_t));
|
|
||||||
}
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2026 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "assetfile.h"
|
|
||||||
|
|
||||||
#ifndef ASSET_CACHE_MAX
|
|
||||||
#define ASSET_CACHE_MAX 64
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef void (*assetcachedisposer_t)(void *data);
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
char_t path[ASSET_FILE_NAME_MAX];
|
|
||||||
void *data;
|
|
||||||
uint32_t refcount;
|
|
||||||
assetcachedisposer_t disposer;
|
|
||||||
} assetcacheentry_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
assetcacheentry_t entries[ASSET_CACHE_MAX];
|
|
||||||
uint8_t count;
|
|
||||||
} assetcache_t;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes the asset cache.
|
|
||||||
*
|
|
||||||
* @param cache The cache to initialize.
|
|
||||||
*/
|
|
||||||
void assetCacheInit(assetcache_t *cache);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Looks up a cached asset by path.
|
|
||||||
*
|
|
||||||
* @param cache The cache to search.
|
|
||||||
* @param path The asset path to look up.
|
|
||||||
* @return Pointer to the cached data, or NULL if not found.
|
|
||||||
*/
|
|
||||||
void *assetCacheLookup(assetcache_t *cache, const char_t *path);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inserts a newly loaded asset into the cache with refcount 1.
|
|
||||||
*
|
|
||||||
* @param cache The cache to insert into.
|
|
||||||
* @param path The asset path key.
|
|
||||||
* @param data Heap-allocated asset data. The cache takes ownership.
|
|
||||||
* @param disposer Called with data before freeing when refcount reaches 0.
|
|
||||||
*/
|
|
||||||
void assetCacheInsert(
|
|
||||||
assetcache_t *cache,
|
|
||||||
const char_t *path,
|
|
||||||
void *data,
|
|
||||||
assetcachedisposer_t disposer
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Increments the refcount for a cached asset.
|
|
||||||
*
|
|
||||||
* @param cache The cache containing the asset.
|
|
||||||
* @param path The asset path.
|
|
||||||
*/
|
|
||||||
void assetCacheRetain(assetcache_t *cache, const char_t *path);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decrements the refcount. Disposes and frees the asset when it reaches 0.
|
|
||||||
*
|
|
||||||
* @param cache The cache containing the asset.
|
|
||||||
* @param path The asset path.
|
|
||||||
*/
|
|
||||||
void assetCacheRelease(assetcache_t *cache, const char_t *path);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Disposes all remaining cache entries and resets the cache.
|
|
||||||
*
|
|
||||||
* @param cache The cache to dispose.
|
|
||||||
*/
|
|
||||||
void assetCacheDispose(assetcache_t *cache);
|
|
||||||
@@ -117,6 +117,51 @@ errorret_t assetFileDispose(assetfile_t *file) {
|
|||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
errorret_t assetFileReadEntire(
|
||||||
|
assetfile_t *file,
|
||||||
|
uint8_t **outBuffer,
|
||||||
|
size_t *outSize
|
||||||
|
) {
|
||||||
|
assertNotNull(file, "Asset file cannot be NULL.");
|
||||||
|
assertNotNull(outBuffer, "outBuffer cannot be NULL.");
|
||||||
|
assertNotNull(outSize, "outSize cannot be NULL.");
|
||||||
|
assertTrue(
|
||||||
|
file->size > 0,
|
||||||
|
"Asset file has no size; call assetFileInit first."
|
||||||
|
);
|
||||||
|
|
||||||
|
// File should be closed currently.
|
||||||
|
assertNull(file->zipFile, "Asset file must be closed before reading entire.");
|
||||||
|
|
||||||
|
// Open file
|
||||||
|
errorret_t ret = assetFileOpen(file);
|
||||||
|
if(errorIsNotOk(ret)) {
|
||||||
|
errorChain(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set output.
|
||||||
|
size_t size = (size_t)file->size;
|
||||||
|
uint8_t *buffer = (uint8_t *)memoryAllocate(size);
|
||||||
|
|
||||||
|
// Read entire file.
|
||||||
|
ret = assetFileRead(file, buffer, size);
|
||||||
|
if(errorIsNotOk(ret)) {
|
||||||
|
memoryFree(buffer);
|
||||||
|
errorChain(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the file.
|
||||||
|
ret = assetFileClose(file);
|
||||||
|
if(errorIsNotOk(ret)) {
|
||||||
|
memoryFree(buffer);
|
||||||
|
errorChain(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
*outBuffer = buffer;
|
||||||
|
*outSize = size;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
// Line Reader;
|
// Line Reader;
|
||||||
void assetFileLineReaderInit(
|
void assetFileLineReaderInit(
|
||||||
assetfilelinereader_t *reader,
|
assetfilelinereader_t *reader,
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ typedef struct assetfile_s assetfile_t;
|
|||||||
|
|
||||||
typedef errorret_t (*assetfileloader_t)(assetfile_t *file);
|
typedef errorret_t (*assetfileloader_t)(assetfile_t *file);
|
||||||
|
|
||||||
|
// Describes a file not yet loaded.
|
||||||
typedef struct assetfile_s {
|
typedef struct assetfile_s {
|
||||||
char_t filename[ASSET_FILE_NAME_MAX];
|
char_t filename[ASSET_FILE_NAME_MAX];
|
||||||
void *params;
|
void *params;
|
||||||
@@ -91,6 +92,21 @@ errorret_t assetFileClose(assetfile_t *file);
|
|||||||
*/
|
*/
|
||||||
errorret_t assetFileDispose(assetfile_t *file);
|
errorret_t assetFileDispose(assetfile_t *file);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the entire contents of the asset file into a newly allocated buffer.
|
||||||
|
* The caller is responsible for freeing the buffer with memoryFree.
|
||||||
|
*
|
||||||
|
* @param file The asset file to read. Must be initialized but not open.
|
||||||
|
* @param outBuffer Receives a pointer to the allocated buffer.
|
||||||
|
* @param outSize Receives the number of bytes written to the buffer.
|
||||||
|
* @return An error code if the file could not be read.
|
||||||
|
*/
|
||||||
|
errorret_t assetFileReadEntire(
|
||||||
|
assetfile_t *file,
|
||||||
|
uint8_t **outBuffer,
|
||||||
|
size_t *outSize
|
||||||
|
);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
assetfile_t *file;
|
assetfile_t *file;
|
||||||
uint8_t *readBuffer;
|
uint8_t *readBuffer;
|
||||||
|
|||||||
@@ -4,6 +4,12 @@
|
|||||||
# https://opensource.org/licenses/MIT
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
# Sources
|
# Sources
|
||||||
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
|
PUBLIC
|
||||||
|
assetentry.c
|
||||||
|
assetloader.c
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# Subdirs
|
# Subdirs
|
||||||
add_subdirectory(display)
|
add_subdirectory(display)
|
||||||
|
|||||||
@@ -0,0 +1,104 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "assetentry.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
#include "util/string.h"
|
||||||
|
|
||||||
|
void assetEntryInit(
|
||||||
|
assetentry_t *entry,
|
||||||
|
const char_t *name,
|
||||||
|
const assetloadertype_t type,
|
||||||
|
assetloaderinput_t *input
|
||||||
|
) {
|
||||||
|
assertNotNull(entry, "Entry cannot be NULL");
|
||||||
|
assertStrLenMin(name, 1, "Name cannot be empty");
|
||||||
|
assertStrLenMax(name, ASSET_FILE_NAME_MAX - 1, "Name too long");
|
||||||
|
assertTrue(type != ASSET_LOADER_TYPE_NULL, "Invalid loader type.");
|
||||||
|
assertTrue(type < ASSET_LOADER_TYPE_COUNT, "Invalid loader type.");
|
||||||
|
assertIsMainThread("Must be called from the main thread.");
|
||||||
|
|
||||||
|
memoryZero(entry, sizeof(assetentry_t));
|
||||||
|
stringCopy(entry->name, name, ASSET_FILE_NAME_MAX);
|
||||||
|
entry->type = type;
|
||||||
|
entry->state = ASSET_ENTRY_STATE_NOT_STARTED;
|
||||||
|
if(input) {
|
||||||
|
entry->inputData = *input;
|
||||||
|
entry->input = &entry->inputData;
|
||||||
|
} else {
|
||||||
|
memoryZero(&entry->inputData, sizeof(assetloaderinput_t));
|
||||||
|
entry->input = NULL;
|
||||||
|
}
|
||||||
|
refInit(&entry->refs, entry, NULL, NULL, NULL);
|
||||||
|
|
||||||
|
eventInit(
|
||||||
|
&entry->onLoaded,
|
||||||
|
entry->onLoadedCallbacks, entry->onLoadedUsers,
|
||||||
|
ASSET_ENTRY_EVENT_MAX
|
||||||
|
);
|
||||||
|
eventInit(
|
||||||
|
&entry->onUnloaded,
|
||||||
|
entry->onUnloadedCallbacks, entry->onUnloadedUsers,
|
||||||
|
ASSET_ENTRY_EVENT_MAX
|
||||||
|
);
|
||||||
|
eventInit(
|
||||||
|
&entry->onError,
|
||||||
|
entry->onErrorCallbacks, entry->onErrorUsers,
|
||||||
|
ASSET_ENTRY_EVENT_MAX
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void assetEntryLock(assetentry_t *entry) {
|
||||||
|
assertNotNull(entry, "Entry cannot be NULL");
|
||||||
|
assertTrue(entry->type != ASSET_LOADER_TYPE_NULL, "Invalid loader type.");
|
||||||
|
|
||||||
|
refLock(&entry->refs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void assetEntryUnlock(assetentry_t *entry) {
|
||||||
|
assertNotNull(entry, "Entry cannot be NULL");
|
||||||
|
assertTrue(entry->type != ASSET_LOADER_TYPE_NULL, "Invalid loader type.");
|
||||||
|
|
||||||
|
refUnlock(&entry->refs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void assetEntryStartLoading(
|
||||||
|
assetentry_t *entry,
|
||||||
|
assetloading_t *loading
|
||||||
|
) {
|
||||||
|
assertNotNull(entry, "Entry cannot be NULL");
|
||||||
|
assertNotNull(loading, "Loading cannot be NULL");
|
||||||
|
assertTrue(entry->type != ASSET_LOADER_TYPE_NULL, "Invalid loader type.");
|
||||||
|
assertTrue(
|
||||||
|
entry->state == ASSET_ENTRY_STATE_NOT_STARTED,
|
||||||
|
"Can only start loading from NOT_STARTED state."
|
||||||
|
);
|
||||||
|
assertIsMainThread("Must be called from the main thread.");
|
||||||
|
|
||||||
|
entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
||||||
|
memoryZero(&loading->loading, sizeof(assetloaderloading_t));
|
||||||
|
loading->type = entry->type;
|
||||||
|
loading->entry = entry;
|
||||||
|
// At this point the asset manager will manage this thing's loading
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetEntryDispose(assetentry_t *entry) {
|
||||||
|
assertNotNull(entry, "Entry cannot be NULL");
|
||||||
|
assertTrue(entry->type != ASSET_LOADER_TYPE_NULL, "Invalid loader type.");
|
||||||
|
assertTrue(entry->type < ASSET_LOADER_TYPE_COUNT, "Invalid loader type.");
|
||||||
|
assertIsMainThread("Must be called from the main thread.");
|
||||||
|
assertTrue(
|
||||||
|
entry->refs.count == 0,
|
||||||
|
"Asset entry still refed at dispose time."
|
||||||
|
);
|
||||||
|
|
||||||
|
eventInvoke(&entry->onUnloaded, entry);
|
||||||
|
errorChain(ASSET_LOADER_CALLBACKS[entry->type].dispose(entry));
|
||||||
|
memoryZero(entry, sizeof(assetentry_t));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "asset/loader/assetloading.h"
|
||||||
|
#include "event/event.h"
|
||||||
|
#include "util/ref.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ASSET_ENTRY_STATE_NOT_STARTED,
|
||||||
|
ASSET_ENTRY_STATE_PENDING_ASYNC,
|
||||||
|
ASSET_ENTRY_STATE_LOADING_ASYNC,
|
||||||
|
ASSET_ENTRY_STATE_PENDING_SYNC,
|
||||||
|
ASSET_ENTRY_STATE_LOADING_SYNC,
|
||||||
|
ASSET_ENTRY_STATE_LOADED,
|
||||||
|
ASSET_ENTRY_STATE_ERROR
|
||||||
|
} assetentrystate_t;
|
||||||
|
|
||||||
|
/** Maximum number of subscribers for each per-entry event. */
|
||||||
|
#define ASSET_ENTRY_EVENT_MAX 2
|
||||||
|
|
||||||
|
typedef struct assetentry_s assetentry_t;
|
||||||
|
|
||||||
|
struct assetentry_s {
|
||||||
|
char_t name[ASSET_FILE_NAME_MAX];
|
||||||
|
assetloadertype_t type;
|
||||||
|
assetloaderoutput_t data;
|
||||||
|
assetentrystate_t state;
|
||||||
|
ref_t refs;
|
||||||
|
assetloaderinput_t *input;
|
||||||
|
assetloaderinput_t inputData;
|
||||||
|
/**
|
||||||
|
* Fired once when loading completes successfully (params = assetentry_t *).
|
||||||
|
* Always invoked on the main thread.
|
||||||
|
*/
|
||||||
|
event_t onLoaded;
|
||||||
|
eventcallback_t onLoadedCallbacks[ASSET_ENTRY_EVENT_MAX];
|
||||||
|
void *onLoadedUsers[ASSET_ENTRY_EVENT_MAX];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fired once when the entry is disposed/reaped (params = assetentry_t *).
|
||||||
|
* The asset data is still accessible when the callback runs.
|
||||||
|
* Always invoked on the main thread.
|
||||||
|
*/
|
||||||
|
event_t onUnloaded;
|
||||||
|
eventcallback_t onUnloadedCallbacks[ASSET_ENTRY_EVENT_MAX];
|
||||||
|
void *onUnloadedUsers[ASSET_ENTRY_EVENT_MAX];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fired once when loading fails (params = assetentry_t *).
|
||||||
|
* Always invoked on the main thread.
|
||||||
|
*/
|
||||||
|
event_t onError;
|
||||||
|
eventcallback_t onErrorCallbacks[ASSET_ENTRY_EVENT_MAX];
|
||||||
|
void *onErrorUsers[ASSET_ENTRY_EVENT_MAX];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes an asset entry with the given name and type. This does not load
|
||||||
|
* the asset.
|
||||||
|
*
|
||||||
|
* @param entry The asset entry to initialize.
|
||||||
|
* @param name The name of the asset, used as a key for loading and caching.
|
||||||
|
* @param type The type of asset this entry represents.
|
||||||
|
* @param input Data that will be passed to the loader about how it should load.
|
||||||
|
*/
|
||||||
|
void assetEntryInit(
|
||||||
|
assetentry_t *entry,
|
||||||
|
const char_t *name,
|
||||||
|
const assetloadertype_t type,
|
||||||
|
assetloaderinput_t *input
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Locks an asset entry, preventing it from being freed until it is unlocked.
|
||||||
|
*
|
||||||
|
* @param entry The asset entry to lock.
|
||||||
|
*/
|
||||||
|
void assetEntryLock(assetentry_t *entry);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unlocks an asset entry, allowing it to be freed if there are no more locks.
|
||||||
|
*
|
||||||
|
* @param entry The asset entry to unlock.
|
||||||
|
*/
|
||||||
|
void assetEntryUnlock(assetentry_t *entry);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts loading the given asset entry using an assetloading slot. This will
|
||||||
|
* be called by the asset manager when it deems it's a good time to begin the
|
||||||
|
* loading of this asset entry.
|
||||||
|
*
|
||||||
|
* Currently we return the error but in future this will not be returned.
|
||||||
|
*
|
||||||
|
* @param entry The asset entry to start loading.
|
||||||
|
* @param loading The assetloading slot to use for loading this asset entry.
|
||||||
|
* @return Any error that occurs during loading.
|
||||||
|
*/
|
||||||
|
void assetEntryStartLoading(assetentry_t *entry, assetloading_t *loading);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disposes an asset entry, freeing any resources it holds.
|
||||||
|
* Fires the onUnloaded event before releasing asset data.
|
||||||
|
*
|
||||||
|
* @param entry The asset entry to dispose.
|
||||||
|
* @return Any error that occurs during disposal.
|
||||||
|
*/
|
||||||
|
errorret_t assetEntryDispose(assetentry_t *entry);
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "assetloader.h"
|
||||||
|
|
||||||
|
assetloadercallbacks_t ASSET_LOADER_CALLBACKS[ASSET_LOADER_TYPE_COUNT] = {
|
||||||
|
[ASSET_LOADER_TYPE_NULL] = { 0 },
|
||||||
|
|
||||||
|
[ASSET_LOADER_TYPE_MESH] = {
|
||||||
|
.loadSync = assetMeshLoaderSync,
|
||||||
|
.loadAsync = assetMeshLoaderAsync,
|
||||||
|
.dispose = assetMeshDispose
|
||||||
|
},
|
||||||
|
|
||||||
|
[ASSET_LOADER_TYPE_TEXTURE] = {
|
||||||
|
.loadSync = assetTextureLoaderSync,
|
||||||
|
.loadAsync = assetTextureLoaderAsync,
|
||||||
|
.dispose = assetTextureDispose
|
||||||
|
},
|
||||||
|
|
||||||
|
[ASSET_LOADER_TYPE_TILESET] = {
|
||||||
|
.loadSync = assetTilesetLoaderSync,
|
||||||
|
.loadAsync = assetTilesetLoaderAsync,
|
||||||
|
.dispose = assetTilesetDispose
|
||||||
|
},
|
||||||
|
|
||||||
|
[ASSET_LOADER_TYPE_LOCALE] = {
|
||||||
|
.loadSync = assetLocaleLoaderSync,
|
||||||
|
.loadAsync = assetLocaleLoaderAsync,
|
||||||
|
.dispose = assetLocaleDispose
|
||||||
|
},
|
||||||
|
|
||||||
|
[ASSET_LOADER_TYPE_JSON] = {
|
||||||
|
.loadSync = assetJsonLoaderSync,
|
||||||
|
.loadAsync = assetJsonLoaderAsync,
|
||||||
|
.dispose = assetJsonDispose
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "asset/loader/display/assetmeshloader.h"
|
||||||
|
#include "asset/loader/display/assettextureloader.h"
|
||||||
|
#include "asset/loader/display/assettilesetloader.h"
|
||||||
|
#include "asset/loader/locale/assetlocaleloader.h"
|
||||||
|
#include "asset/loader/json/assetjsonloader.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ASSET_LOADER_TYPE_NULL,
|
||||||
|
|
||||||
|
ASSET_LOADER_TYPE_MESH,
|
||||||
|
ASSET_LOADER_TYPE_TEXTURE,
|
||||||
|
ASSET_LOADER_TYPE_TILESET,
|
||||||
|
ASSET_LOADER_TYPE_LOCALE,
|
||||||
|
ASSET_LOADER_TYPE_JSON,
|
||||||
|
|
||||||
|
ASSET_LOADER_TYPE_COUNT
|
||||||
|
} assetloadertype_t;
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
assetmeshloaderinput_t mesh;
|
||||||
|
assettextureloaderinput_t texture;
|
||||||
|
assettilesetloaderinput_t tileset;
|
||||||
|
assetlocaleloaderinput_t locale;
|
||||||
|
assetjsonloaderinput_t json;
|
||||||
|
} assetloaderinput_t;
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
assetmeshloaderloading_t mesh;
|
||||||
|
assettextureloaderloading_t texture;
|
||||||
|
assettilesetloaderloading_t tileset;
|
||||||
|
assetlocaleloaderloading_t locale;
|
||||||
|
assetjsonloaderloading_t json;
|
||||||
|
} assetloaderloading_t;
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
assetmeshoutput_t mesh;
|
||||||
|
assettextureoutput_t texture;
|
||||||
|
assettilesetoutput_t tileset;
|
||||||
|
assetlocaleoutput_t locale;
|
||||||
|
assetjsonoutput_t json;
|
||||||
|
} assetloaderoutput_t;
|
||||||
|
|
||||||
|
typedef struct assetloading_s assetloading_t;
|
||||||
|
typedef struct assetentry_s assetentry_t;
|
||||||
|
|
||||||
|
typedef errorret_t (assetloadersynccallback_t)(assetloading_t *loading);
|
||||||
|
typedef errorret_t (assetloaderasynccallback_t)(assetloading_t *loading);
|
||||||
|
typedef errorret_t (assetloaderdisposecallback_t)(assetentry_t *entry);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
assetloadersynccallback_t *loadSync;
|
||||||
|
assetloaderasynccallback_t *loadAsync;
|
||||||
|
assetloaderdisposecallback_t *dispose;
|
||||||
|
} assetloadercallbacks_t;
|
||||||
|
|
||||||
|
extern assetloadercallbacks_t ASSET_LOADER_CALLBACKS[ASSET_LOADER_TYPE_COUNT];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shorthand method to both chain an error (against the loader state) and to
|
||||||
|
* set the asset entry state to error.
|
||||||
|
*
|
||||||
|
* @param loading The asset loading slot.
|
||||||
|
* @param ret The error return value to check and chain if it's an error.
|
||||||
|
*/
|
||||||
|
#define assetLoaderErrorChain(loading, _expr) {\
|
||||||
|
errorret_t _alec = (_expr); \
|
||||||
|
if(errorIsNotOk(_alec)) { \
|
||||||
|
(loading)->entry->state = ASSET_ENTRY_STATE_ERROR; \
|
||||||
|
errorChain(_alec); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shorthand method to both throw an error (against the loader state) and to
|
||||||
|
* set the asset entry state to error.
|
||||||
|
*
|
||||||
|
* @param loading The asset loading slot.
|
||||||
|
* @param ... Format string and arguments for the error message.
|
||||||
|
*/
|
||||||
|
#define assetLoaderErrorThrow(loading, ...) {\
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_ERROR; \
|
||||||
|
errorThrow(__VA_ARGS__); \
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "assetloader.h"
|
||||||
|
#include "asset/assetfile.h"
|
||||||
|
#include "thread/threadmutex.h"
|
||||||
|
|
||||||
|
typedef struct assetentry_s assetentry_t;
|
||||||
|
|
||||||
|
typedef struct assetloading_s {
|
||||||
|
threadmutex_t mutex;
|
||||||
|
assetloadertype_t type;
|
||||||
|
assetentry_t *entry;
|
||||||
|
assetloaderloading_t loading;
|
||||||
|
} assetloading_t;
|
||||||
|
|
||||||
|
typedef errorret_t (assetloadingcallback_t)(assetloading_t *loading);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
assetloadingcallback_t *loadSync;
|
||||||
|
} assetloadingcallbacks_t;
|
||||||
@@ -5,72 +5,77 @@
|
|||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "asset/loader/display/assetmeshloader.h"
|
#include "assetmeshloader.h"
|
||||||
#include "asset/asset.h"
|
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "util/endian.h"
|
#include "util/endian.h"
|
||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
|
#include "asset/loader/assetloading.h"
|
||||||
|
#include "asset/loader/assetentry.h"
|
||||||
|
|
||||||
errorret_t assetMeshLoader(assetfile_t *file) {
|
errorret_t assetMeshLoaderAsync(assetloading_t *loading) {
|
||||||
assertNotNull(file, "Asset file cannot be null");
|
assertNotNull(loading, "Loading cannot be NULL");
|
||||||
|
|
||||||
assetmeshoutput_t *output = (assetmeshoutput_t *)file->output;
|
if(loading->loading.mesh.state != ASSET_MESH_LOADING_STATE_READ_FILE) {
|
||||||
assertNotNull(output, "Output cannot be null");
|
errorOk();
|
||||||
assertNotNull(output->outMesh, "Output mesh cannot be null");
|
}
|
||||||
assertNotNull(output->outVertices, "Output vertices cannot be null");
|
|
||||||
|
|
||||||
// STL file loading
|
assetmeshoutput_t *out = &loading->entry->data.mesh;
|
||||||
errorChain(assetFileOpen(file));
|
assetfile_t *file = &loading->loading.mesh.file;
|
||||||
|
assetmeshinputaxis_t axis = loading->entry->inputData.mesh;
|
||||||
|
|
||||||
// Skip the 80 byte header
|
assetLoaderErrorChain(loading,
|
||||||
errorChain(assetFileRead(file, NULL, 80));
|
assetFileInit(file, loading->entry->name, NULL, NULL)
|
||||||
if(file->lastRead != 80) errorThrow("Failed to skip STL header");
|
);
|
||||||
|
assetLoaderErrorChain(loading, assetFileOpen(file));
|
||||||
|
|
||||||
|
// Skip the 80-byte STL header.
|
||||||
|
assetLoaderErrorChain(loading, assetFileRead(file, NULL, 80));
|
||||||
|
if(file->lastRead != 80) {
|
||||||
|
assetLoaderErrorThrow(loading, "Failed to skip STL header.");
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t triangleCount;
|
uint32_t triangleCount;
|
||||||
errorChain(assetFileRead(file, &triangleCount, sizeof(uint32_t)));
|
assetLoaderErrorChain(loading,
|
||||||
if(file->lastRead != sizeof(uint32_t)) errorThrow("Failed read tri count");
|
assetFileRead(file, &triangleCount, sizeof(uint32_t))
|
||||||
// normalize
|
);
|
||||||
|
if(file->lastRead != sizeof(uint32_t)) {
|
||||||
|
assetLoaderErrorThrow(loading, "Failed to read tri count");
|
||||||
|
}
|
||||||
triangleCount = endianLittleToHost32(triangleCount);
|
triangleCount = endianLittleToHost32(triangleCount);
|
||||||
|
|
||||||
// Allocate mesh and vertex data
|
out->vertices = memoryAllocate(sizeof(meshvertex_t) * triangleCount * 3);
|
||||||
errorret_t ret;
|
meshvertex_t *verts = out->vertices;
|
||||||
meshvertex_t *verts = memoryAllocate(
|
|
||||||
sizeof(meshvertex_t) * triangleCount * 3
|
|
||||||
);
|
|
||||||
*output->outVertices = verts;
|
|
||||||
|
|
||||||
// Read triangle data
|
errorret_t ret;
|
||||||
for(uint32_t i = 0; i < triangleCount; i++) {
|
for(uint32_t i = 0; i < triangleCount; i++) {
|
||||||
assetmeshstltriangle_t triData;
|
assetmeshstltriangle_t triData;
|
||||||
ret = assetFileRead(file, &triData, sizeof(triData));
|
ret = assetFileRead(file, &triData, sizeof(triData));
|
||||||
if(ret.code != ERROR_OK) {
|
if(errorIsNotOk(ret)) {
|
||||||
memoryFree(verts);
|
memoryFree(verts);
|
||||||
errorChain(ret);
|
out->vertices = NULL;
|
||||||
|
assetLoaderErrorChain(loading, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(file->lastRead != sizeof(triData)) {
|
if(file->lastRead != sizeof(triData)) {
|
||||||
memoryFree(verts);
|
memoryFree(verts);
|
||||||
errorThrow("Failed to read triangle data");
|
out->vertices = NULL;
|
||||||
|
assetLoaderErrorThrow(loading, "Failed to read triangle data");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip normals, we don't use them
|
|
||||||
|
|
||||||
// Fix endianess of of data
|
|
||||||
for(uint8_t j = 0; j < 3; j++) {
|
for(uint8_t j = 0; j < 3; j++) {
|
||||||
#if MESH_ENABLE_COLOR
|
#if MESH_ENABLE_COLOR
|
||||||
verts[i * 3 + j].color.r = (uint8_t)(endianLittleToHostFloat(
|
verts[i * 3 + j].color.r = (
|
||||||
triData.normal[0]
|
(uint8_t)(endianLittleToHostFloat(triData.normal[0]) * 255.0f)
|
||||||
) * 255.0f);
|
);
|
||||||
verts[i * 3 + j].color.g = (uint8_t)(endianLittleToHostFloat(
|
verts[i * 3 + j].color.g = (
|
||||||
triData.normal[1]
|
(uint8_t)(endianLittleToHostFloat(triData.normal[1]) * 255.0f)
|
||||||
) * 255.0f);
|
);
|
||||||
verts[i * 3 + j].color.b = (uint8_t)(endianLittleToHostFloat(
|
verts[i * 3 + j].color.b = (
|
||||||
triData.normal[2]
|
(uint8_t)(endianLittleToHostFloat(triData.normal[2]) * 255.0f)
|
||||||
) * 255.0f);
|
);
|
||||||
verts[i * 3 + j].color.a = 0xFF;
|
verts[i * 3 + j].color.a = 0xFF;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
verts[i * 3 + j].uv[0] = 0.0f; // No UV data in STL, just set to 0
|
verts[i * 3 + j].uv[0] = 0.0f;
|
||||||
verts[i * 3 + j].uv[1] = 0.0f;
|
verts[i * 3 + j].uv[1] = 0.0f;
|
||||||
|
|
||||||
for(uint8_t k = 0; k < 3; k++) {
|
for(uint8_t k = 0; k < 3; k++) {
|
||||||
@@ -79,104 +84,97 @@ errorret_t assetMeshLoader(assetfile_t *file) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(output->inputAxis) {
|
switch(axis) {
|
||||||
case MESH_INPUT_AXIS_Z_UP:
|
case MESH_INPUT_AXIS_Z_UP: {
|
||||||
// Convert Z-Up to Y-Up
|
|
||||||
{
|
|
||||||
float_t temp = verts[i * 3 + j].pos[1];
|
float_t temp = verts[i * 3 + j].pos[1];
|
||||||
verts[i * 3 + j].pos[1] = verts[i * 3 + j].pos[2];
|
verts[i * 3 + j].pos[1] = verts[i * 3 + j].pos[2];
|
||||||
verts[i * 3 + j].pos[2] = temp;
|
verts[i * 3 + j].pos[2] = temp;
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case MESH_INPUT_AXIS_X_UP:
|
case MESH_INPUT_AXIS_X_UP: {
|
||||||
// Convert X-Up to Y-Up
|
|
||||||
{
|
|
||||||
float_t temp = verts[i * 3 + j].pos[0];
|
float_t temp = verts[i * 3 + j].pos[0];
|
||||||
verts[i * 3 + j].pos[0] = verts[i * 3 + j].pos[1];
|
verts[i * 3 + j].pos[0] = verts[i * 3 + j].pos[1];
|
||||||
verts[i * 3 + j].pos[1] = temp;
|
verts[i * 3 + j].pos[1] = temp;
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case MESH_INPUT_AXIS_Y_DOWN:
|
case MESH_INPUT_AXIS_Y_DOWN:
|
||||||
// Invert Y axis
|
|
||||||
verts[i * 3 + j].pos[1] = -verts[i * 3 + j].pos[1];
|
verts[i * 3 + j].pos[1] = -verts[i * 3 + j].pos[1];
|
||||||
break;
|
break;
|
||||||
|
case MESH_INPUT_AXIS_Z_DOWN: {
|
||||||
case MESH_INPUT_AXIS_Z_DOWN:
|
|
||||||
// Convert Z-Up to Y-Up and invert Y axis
|
|
||||||
{
|
|
||||||
float_t temp = verts[i * 3 + j].pos[1];
|
float_t temp = verts[i * 3 + j].pos[1];
|
||||||
verts[i * 3 + j].pos[1] = -verts[i * 3 + j].pos[2];
|
verts[i * 3 + j].pos[1] = -verts[i * 3 + j].pos[2];
|
||||||
verts[i * 3 + j].pos[2] = temp;
|
verts[i * 3 + j].pos[2] = temp;
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case MESH_INPUT_AXIS_X_DOWN:
|
case MESH_INPUT_AXIS_X_DOWN: {
|
||||||
// Convert X-Up to Y-Up and invert Y axis
|
|
||||||
{
|
|
||||||
float_t temp = verts[i * 3 + j].pos[0];
|
float_t temp = verts[i * 3 + j].pos[0];
|
||||||
verts[i * 3 + j].pos[0] = verts[i * 3 + j].pos[1];
|
verts[i * 3 + j].pos[0] = verts[i * 3 + j].pos[1];
|
||||||
verts[i * 3 + j].pos[1] = -temp;
|
verts[i * 3 + j].pos[1] = -temp;
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case MESH_INPUT_AXIS_Y_UP:
|
case MESH_INPUT_AXIS_Y_UP:
|
||||||
default:
|
default:
|
||||||
// No covnersion possible / Needed
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally, init mesh
|
|
||||||
ret = meshInit(
|
|
||||||
output->outMesh,
|
|
||||||
MESH_PRIMITIVE_TYPE_TRIANGLES,
|
|
||||||
triangleCount * 3,
|
|
||||||
verts
|
|
||||||
);
|
|
||||||
if(ret.code != ERROR_OK) {
|
|
||||||
memoryFree(verts);
|
|
||||||
errorChain(ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = assetFileClose(file);
|
ret = assetFileClose(file);
|
||||||
if(ret.code != ERROR_OK) {
|
if(errorIsNotOk(ret)) {
|
||||||
errorCatch(errorPrint(meshDispose(output->outMesh)));
|
|
||||||
memoryFree(verts);
|
memoryFree(verts);
|
||||||
errorChain(ret);
|
out->vertices = NULL;
|
||||||
|
assetLoaderErrorChain(loading, ret);
|
||||||
}
|
}
|
||||||
|
assetFileDispose(file);
|
||||||
|
|
||||||
|
loading->loading.mesh.triangleCount = triangleCount;
|
||||||
|
loading->loading.mesh.state = ASSET_MESH_LOADING_STATE_CREATE_MESH;
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetMeshLoadToOutput(
|
errorret_t assetMeshLoaderSync(assetloading_t *loading) {
|
||||||
const char_t *path,
|
assertNotNull(loading, "Loading cannot be NULL");
|
||||||
assetmeshoutput_t *output
|
assertTrue(loading->type == ASSET_LOADER_TYPE_MESH, "Invalid type.");
|
||||||
) {
|
|
||||||
assertNotNull(path, "Path cannot be null");
|
|
||||||
assertNotNull(output, "Output cannot be null");
|
|
||||||
assertNotNull(output->outMesh, "Output mesh cannot be null");
|
|
||||||
assertNotNull(output->outVertices, "Output vertices cannot be null");
|
|
||||||
|
|
||||||
return assetLoad(path, &assetMeshLoader, NULL, output);
|
switch(loading->loading.mesh.state) {
|
||||||
|
case ASSET_MESH_LOADING_STATE_INITIAL:
|
||||||
|
loading->loading.mesh.state = ASSET_MESH_LOADING_STATE_READ_FILE;
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_PENDING_ASYNC;
|
||||||
|
errorOk();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_MESH_LOADING_STATE_CREATE_MESH:
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
assetmeshoutput_t *out = &loading->entry->data.mesh;
|
||||||
|
assertNotNull(out->vertices, "Mesh vertices should have been loaded by now.");
|
||||||
|
|
||||||
|
errorret_t ret = meshInit(
|
||||||
|
&out->mesh,
|
||||||
|
MESH_PRIMITIVE_TYPE_TRIANGLES,
|
||||||
|
loading->loading.mesh.triangleCount * 3,
|
||||||
|
out->vertices
|
||||||
|
);
|
||||||
|
if(errorIsNotOk(ret)) {
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_ERROR;
|
||||||
|
memoryFree(out->vertices);
|
||||||
|
out->vertices = NULL;
|
||||||
|
assetLoaderErrorChain(loading, ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
|
||||||
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetMeshLoad(
|
errorret_t assetMeshDispose(assetentry_t *entry) {
|
||||||
const char_t *path,
|
assertNotNull(entry, "Asset entry cannot be NULL");
|
||||||
mesh_t *outMesh,
|
assertTrue(entry->type == ASSET_LOADER_TYPE_MESH, "Invalid type.");
|
||||||
meshvertex_t **outVertices,
|
errorChain(meshDispose(&entry->data.mesh.mesh));
|
||||||
const assetmeshinputaxis_t inputAxis
|
memoryFree(entry->data.mesh.vertices);
|
||||||
) {
|
errorOk();
|
||||||
assertNotNull(path, "Path cannot be null");
|
|
||||||
assertNotNull(outMesh, "Output mesh cannot be null");
|
|
||||||
assertNotNull(outVertices, "Output vertices cannot be null");
|
|
||||||
|
|
||||||
assetmeshoutput_t output = {
|
|
||||||
outMesh,
|
|
||||||
outVertices,
|
|
||||||
inputAxis
|
|
||||||
};
|
|
||||||
return assetMeshLoadToOutput(path, &output);
|
|
||||||
}
|
}
|
||||||
@@ -6,12 +6,15 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "asset/asset.h"
|
#include "asset/assetfile.h"
|
||||||
#include "display/mesh/mesh.h"
|
#include "display/mesh/mesh.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
typedef struct assetloading_s assetloading_t;
|
||||||
|
typedef struct assetentry_s assetentry_t;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
MESH_INPUT_AXIS_Y_UP,// Default
|
MESH_INPUT_AXIS_Y_UP,
|
||||||
MESH_INPUT_AXIS_Z_UP,
|
MESH_INPUT_AXIS_Z_UP,
|
||||||
MESH_INPUT_AXIS_X_UP,
|
MESH_INPUT_AXIS_X_UP,
|
||||||
|
|
||||||
@@ -20,10 +23,24 @@ typedef enum {
|
|||||||
MESH_INPUT_AXIS_X_DOWN,
|
MESH_INPUT_AXIS_X_DOWN,
|
||||||
} assetmeshinputaxis_t;
|
} assetmeshinputaxis_t;
|
||||||
|
|
||||||
|
typedef assetmeshinputaxis_t assetmeshloaderinput_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ASSET_MESH_LOADING_STATE_INITIAL,
|
||||||
|
ASSET_MESH_LOADING_STATE_READ_FILE,
|
||||||
|
ASSET_MESH_LOADING_STATE_CREATE_MESH,
|
||||||
|
ASSET_MESH_LOADING_STATE_DONE
|
||||||
|
} assetmeshloadingstate_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
mesh_t *outMesh;
|
assetfile_t file;
|
||||||
meshvertex_t **outVertices;
|
assetmeshloadingstate_t state;
|
||||||
assetmeshinputaxis_t inputAxis;
|
uint32_t triangleCount;
|
||||||
|
} assetmeshloaderloading_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
mesh_t mesh;
|
||||||
|
meshvertex_t *vertices;
|
||||||
} assetmeshoutput_t;
|
} assetmeshoutput_t;
|
||||||
|
|
||||||
#pragma pack(push, 1)
|
#pragma pack(push, 1)
|
||||||
@@ -36,26 +53,6 @@ typedef struct {
|
|||||||
|
|
||||||
assertStructSize(assetmeshstltriangle_t, 50);
|
assertStructSize(assetmeshstltriangle_t, 50);
|
||||||
|
|
||||||
/**
|
errorret_t assetMeshLoaderAsync(assetloading_t *loading);
|
||||||
* Loader for mesh assets.
|
errorret_t assetMeshLoaderSync(assetloading_t *loading);
|
||||||
*
|
errorret_t assetMeshDispose(assetentry_t *entry);
|
||||||
* @param file Asset file to load the mesh from.
|
|
||||||
* @return Any error that occurs during loading.
|
|
||||||
*/
|
|
||||||
errorret_t assetMeshLoader(assetfile_t *file);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handler for mesh assets.
|
|
||||||
*
|
|
||||||
* @param file Asset file to load the mesh from.
|
|
||||||
* @param outMesh Output mesh to load the data into.
|
|
||||||
* @param outVertices Output pointer to the vertex data, used for cleanup.
|
|
||||||
* @param inputAxis The axis orientation of the input mesh data.
|
|
||||||
* @return Any error that occurs during loading.
|
|
||||||
*/
|
|
||||||
errorret_t assetMeshLoad(
|
|
||||||
const char_t *path,
|
|
||||||
mesh_t *outMesh,
|
|
||||||
meshvertex_t **outVertices,
|
|
||||||
const assetmeshinputaxis_t inputAxis
|
|
||||||
);
|
|
||||||
|
|||||||
@@ -6,12 +6,13 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "assettextureloader.h"
|
#include "assettextureloader.h"
|
||||||
#include "asset/assetbatch.h"
|
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#include "stb_image.h"
|
#include "stb_image.h"
|
||||||
#include "log/log.h"
|
#include "log/log.h"
|
||||||
#include "util/endian.h"
|
#include "util/endian.h"
|
||||||
|
#include "asset/loader/assetloading.h"
|
||||||
|
#include "asset/loader/assetentry.h"
|
||||||
|
|
||||||
stbi_io_callbacks ASSET_TEXTURE_STB_CALLBACKS = {
|
stbi_io_callbacks ASSET_TEXTURE_STB_CALLBACKS = {
|
||||||
.read = assetTextureReader,
|
.read = assetTextureReader,
|
||||||
@@ -26,7 +27,7 @@ int assetTextureReader(void *user, char *data, int size) {
|
|||||||
assertNotNull(file, "Asset file in stb_image callbacks cannot be NULL.");
|
assertNotNull(file, "Asset file in stb_image callbacks cannot be NULL.");
|
||||||
|
|
||||||
errorret_t ret = assetFileRead(file, data, (size_t)size);
|
errorret_t ret = assetFileRead(file, data, (size_t)size);
|
||||||
if(ret.code != ERROR_OK) {
|
if(errorIsNotOk(ret)) {
|
||||||
errorCatch(errorPrint(ret));
|
errorCatch(errorPrint(ret));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -39,7 +40,7 @@ void assetTextureSkipper(void *user, int n) {
|
|||||||
assertNotNull(file, "Asset file in stb_image callbacks cannot be NULL.");
|
assertNotNull(file, "Asset file in stb_image callbacks cannot be NULL.");
|
||||||
|
|
||||||
errorret_t ret = assetFileRead(file, NULL, (size_t)n);
|
errorret_t ret = assetFileRead(file, NULL, (size_t)n);
|
||||||
if(ret.code != ERROR_OK) {
|
if(errorIsNotOk(ret)) {
|
||||||
errorCatch(errorPrint(ret));
|
errorCatch(errorPrint(ret));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,88 +52,120 @@ int assetTextureEOF(void *user) {
|
|||||||
return file->position >= file->size;
|
return file->position >= file->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetTextureLoader(assetfile_t *file) {
|
errorret_t assetTextureLoaderAsync(assetloading_t *loading) {
|
||||||
assertNotNull(file, "Asset file cannot be NULL.");
|
assertNotNull(loading, "Loading cannot be NULL");
|
||||||
assertNotNull(file->params, "Asset file parameters cannot be NULL.");
|
assertNotMainThread("Should be called from an async thread.");
|
||||||
assertNotNull(file->output, "Asset file output cannot be NULL.");
|
|
||||||
|
|
||||||
assettextureloaderparams_t *p = (assettextureloaderparams_t*)file->params;
|
// Only care about loading pixels.
|
||||||
assertNotNull(p, "Asset texture loader parameters cannot be NULL.");
|
if(loading->loading.texture.state != ASSET_TEXTURE_LOADING_STATE_LOAD_PIXELS){
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init the file
|
||||||
|
assertNull(
|
||||||
|
loading->loading.texture.data, "Pixels already defined?"
|
||||||
|
);
|
||||||
|
|
||||||
|
assetfile_t *file = &loading->loading.texture.file;
|
||||||
|
assetLoaderErrorChain(loading, assetFileInit(
|
||||||
|
file,
|
||||||
|
loading->entry->name,
|
||||||
|
NULL,
|
||||||
|
&loading->entry->data.texture
|
||||||
|
));
|
||||||
|
assetLoaderErrorChain(loading, assetFileOpen(file));
|
||||||
|
|
||||||
|
// Determine channels
|
||||||
int channelsDesired;
|
int channelsDesired;
|
||||||
switch(p->format) {
|
switch(loading->entry->inputData.texture) {
|
||||||
case TEXTURE_FORMAT_RGBA:
|
case TEXTURE_FORMAT_RGBA:
|
||||||
channelsDesired = 4;
|
channelsDesired = 4;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
errorThrow("Bad texture format.");
|
assetLoaderErrorThrow(loading, "Bad texture format.");
|
||||||
}
|
}
|
||||||
|
|
||||||
int width, height, channels;
|
// Load image pixels.
|
||||||
errorChain(assetFileOpen(file));
|
loading->loading.texture.data = stbi_load_from_callbacks(
|
||||||
uint8_t *data = stbi_load_from_callbacks(
|
|
||||||
&ASSET_TEXTURE_STB_CALLBACKS,
|
&ASSET_TEXTURE_STB_CALLBACKS,
|
||||||
file,
|
file,
|
||||||
&width,
|
&loading->loading.texture.width,
|
||||||
&height,
|
&loading->loading.texture.height,
|
||||||
&channels,
|
&loading->loading.texture.channels,
|
||||||
channelsDesired
|
channelsDesired
|
||||||
);
|
);
|
||||||
errorChain(assetFileClose(file));
|
|
||||||
|
|
||||||
if(data == NULL) {
|
// Close out the file.
|
||||||
|
assetLoaderErrorChain(loading, assetFileClose(file));
|
||||||
|
assetLoaderErrorChain(loading, assetFileDispose(file));
|
||||||
|
|
||||||
|
// Ensure we loaded correctly.
|
||||||
|
if(loading->loading.texture.data == NULL) {
|
||||||
const char_t *errorStr = stbi_failure_reason();
|
const char_t *errorStr = stbi_failure_reason();
|
||||||
errorThrow("Failed to load texture from file %s.", errorStr);
|
assetLoaderErrorThrow(
|
||||||
|
loading, "Failed to load texture from file %s.", errorStr
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fixes a specific bug probably with Dolphin but for now just assuming endian
|
// Fixes a specific bug probably with Dolphin but for now just assuming endian
|
||||||
if(!isHostLittleEndian()) {
|
if(!isHostLittleEndian()) {
|
||||||
stbi__vertical_flip(data, width, height, channelsDesired);
|
stbi__vertical_flip(
|
||||||
|
loading->loading.texture.data,
|
||||||
|
loading->loading.texture.width,
|
||||||
|
loading->loading.texture.height,
|
||||||
|
loading->loading.texture.channels
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
errorChain(textureInit(
|
loading->loading.texture.state = ASSET_TEXTURE_LOADING_STATE_CREATE_TEXTURE;
|
||||||
(texture_t*)file->output,
|
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
||||||
(int32_t)width, (int32_t)height,
|
|
||||||
p->format,
|
|
||||||
(texturedata_t){
|
|
||||||
.rgbaColors = (color_t*)data
|
|
||||||
}
|
|
||||||
));
|
|
||||||
|
|
||||||
stbi_image_free(data);
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetTextureLoad(
|
errorret_t assetTextureLoaderSync(assetloading_t *loading) {
|
||||||
const char_t *path,
|
assertNotNull(loading, "Loading cannot be NULL");
|
||||||
texture_t *out,
|
assertIsMainThread("Must be called from the main thread.");
|
||||||
const textureformat_t format
|
|
||||||
) {
|
|
||||||
assettextureloaderparams_t params = {
|
|
||||||
.format = format
|
|
||||||
};
|
|
||||||
return assetLoad(path, assetTextureLoader, ¶ms, out);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void assetTextureCacheDispose(void *data) {
|
switch(loading->loading.texture.state) {
|
||||||
errorCatch(textureDispose((texture_t*)data));
|
case ASSET_TEXTURE_LOADING_STATE_INITIAL:
|
||||||
}
|
loading->loading.texture.state = ASSET_TEXTURE_LOADING_STATE_LOAD_PIXELS;
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_PENDING_ASYNC;
|
||||||
|
errorOk();
|
||||||
|
break;
|
||||||
|
|
||||||
void assetBatchTexture(
|
case ASSET_TEXTURE_LOADING_STATE_CREATE_TEXTURE:
|
||||||
assetbatch_t *batch,
|
break;
|
||||||
const char_t *path,
|
|
||||||
textureformat_t format,
|
default:
|
||||||
texture_t **out
|
errorOk();
|
||||||
) {
|
}
|
||||||
assettextureloaderparams_t params = { .format = format };
|
|
||||||
assetBatchAdd(
|
// Create the texture.
|
||||||
batch,
|
assertNotNull(
|
||||||
path,
|
loading->loading.texture.data, "Pixels should have been loaded by now."
|
||||||
assetTextureLoader,
|
|
||||||
¶ms, sizeof(params),
|
|
||||||
sizeof(texture_t),
|
|
||||||
assetTextureCacheDispose,
|
|
||||||
(void**)out
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assetLoaderErrorChain(loading, textureInit(
|
||||||
|
(texture_t*)&loading->entry->data.texture,
|
||||||
|
loading->loading.texture.width,
|
||||||
|
loading->loading.texture.height,
|
||||||
|
loading->entry->inputData.texture,
|
||||||
|
(texturedata_t){
|
||||||
|
.rgbaColors = (color_t*)loading->loading.texture.data
|
||||||
|
}
|
||||||
|
));
|
||||||
|
|
||||||
|
// Free the pixels.
|
||||||
|
stbi_image_free(loading->loading.texture.data);
|
||||||
|
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetTextureDispose(assetentry_t *entry) {
|
||||||
|
assertNotNull(entry, "Asset entry cannot be NULL");
|
||||||
|
assertIsMainThread("Must be called from the main thread.");
|
||||||
|
|
||||||
|
return textureDispose(&entry->data.texture);
|
||||||
}
|
}
|
||||||
@@ -6,13 +6,29 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "asset/asset.h"
|
#include "asset/assetfile.h"
|
||||||
#include "asset/assetbatch.h"
|
|
||||||
#include "display/texture/texture.h"
|
#include "display/texture/texture.h"
|
||||||
|
|
||||||
|
typedef struct assetloading_s assetloading_t;
|
||||||
|
typedef struct assetentry_s assetentry_t;
|
||||||
|
typedef textureformat_t assettextureloaderinput_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ASSET_TEXTURE_LOADING_STATE_INITIAL,
|
||||||
|
ASSET_TEXTURE_LOADING_STATE_LOAD_PIXELS,
|
||||||
|
ASSET_TEXTURE_LOADING_STATE_CREATE_TEXTURE,
|
||||||
|
ASSET_TEXTURE_LOADING_STATE_DONE
|
||||||
|
} assettextureloadingstate_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
textureformat_t format;
|
assetfile_t file;
|
||||||
} assettextureloaderparams_t;
|
assettextureloadingstate_t state;
|
||||||
|
|
||||||
|
int channels, width, height;
|
||||||
|
uint8_t *data;
|
||||||
|
} assettextureloaderloading_t;
|
||||||
|
|
||||||
|
typedef texture_t assettextureoutput_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* STB image read callback for asset files.
|
* STB image read callback for asset files.
|
||||||
@@ -24,44 +40,42 @@ typedef struct {
|
|||||||
*/
|
*/
|
||||||
int assetTextureReader(void *user, char *data, int size);
|
int assetTextureReader(void *user, char *data, int size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* STB image skip callback for asset files.
|
||||||
|
*
|
||||||
|
* @param user User data passed to the callback, should be an assetfile_t*.
|
||||||
|
* @param n Number of bytes to skip in the file.
|
||||||
|
*/
|
||||||
void assetTextureSkipper(void *user, int n);
|
void assetTextureSkipper(void *user, int n);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* STB image EOF callback for asset files.
|
||||||
|
*
|
||||||
|
* @param user User data passed to the callback, should be an assetfile_t*.
|
||||||
|
* @return Non-zero if end of file has been reached, zero otherwise.
|
||||||
|
*/
|
||||||
int assetTextureEOF(void *user);
|
int assetTextureEOF(void *user);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for texture assets.
|
* Synchronous loader for texture assets.
|
||||||
*
|
*
|
||||||
* @param file Asset file to load the texture from.
|
* @param loading Loading information for the asset being loaded.
|
||||||
* @return Any error that occurs during loading.
|
* @return Error code indicating success or failure of the load operation.
|
||||||
*/
|
*/
|
||||||
errorret_t assetTextureLoader(assetfile_t *file);
|
errorret_t assetTextureLoaderAsync(assetloading_t *loading);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a texture from the specified path.
|
* Synchronous loader for texture assets.
|
||||||
*
|
*
|
||||||
* @param path Path to the texture asset.
|
* @param loading Loading information for the asset being loaded.
|
||||||
* @param out Output texture to load into.
|
* @return Error code indicating success or failure of the load operation.
|
||||||
* @param format Format of the texture to load.
|
|
||||||
* @return Any error that occurs during loading.
|
|
||||||
*/
|
*/
|
||||||
errorret_t assetTextureLoad(
|
errorret_t assetTextureLoaderSync(assetloading_t *loading);
|
||||||
const char_t *path,
|
|
||||||
texture_t *out,
|
|
||||||
const textureformat_t format
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a texture load request to a batch. The cache-owned texture_t* is
|
* Disposer for texture assets.
|
||||||
* written to *out when the batch completes.
|
|
||||||
*
|
*
|
||||||
* @param batch The batch to add to.
|
* @param entry Asset entry containing the texture to dispose.
|
||||||
* @param path Path to the texture asset.
|
* @return Error code indicating success or failure of the dispose operation.
|
||||||
* @param format Format of the texture to load.
|
|
||||||
* @param out Receives a pointer to the loaded texture on completion.
|
|
||||||
*/
|
*/
|
||||||
void assetBatchTexture(
|
errorret_t assetTextureDispose(assetentry_t *entry);
|
||||||
assetbatch_t *batch,
|
|
||||||
const char_t *path,
|
|
||||||
textureformat_t format,
|
|
||||||
texture_t **out
|
|
||||||
);
|
|
||||||
@@ -6,92 +6,119 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "assettilesetloader.h"
|
#include "assettilesetloader.h"
|
||||||
#include "asset/assetbatch.h"
|
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
#include "util/endian.h"
|
#include "util/endian.h"
|
||||||
|
#include "asset/loader/assetloading.h"
|
||||||
|
#include "asset/loader/assetentry.h"
|
||||||
|
|
||||||
errorret_t assetTilesetLoader(assetfile_t *file) {
|
errorret_t assetTilesetLoaderAsync(assetloading_t *loading) {
|
||||||
assertNotNull(file, "Asset file pointer for tileset loader is null.");
|
assertNotNull(loading, "Loading cannot be NULL");
|
||||||
|
assertNotMainThread("Should be called from an async thread.");
|
||||||
|
|
||||||
tileset_t *out = (tileset_t *)file->output;
|
if(loading->loading.tileset.state != ASSET_TILESET_LOADING_STATE_READ_FILE) {
|
||||||
assertNotNull(out, "Output pointer for tileset loader is null.");
|
errorOk();
|
||||||
|
|
||||||
uint8_t *entire = memoryAllocate(file->size);
|
|
||||||
errorChain(assetFileOpen(file));
|
|
||||||
errorChain(assetFileRead(file, entire, file->size));
|
|
||||||
errorChain(assetFileClose(file));
|
|
||||||
assertTrue(file->lastRead == file->size, "Failed to read entire file.");
|
|
||||||
|
|
||||||
|
|
||||||
if(
|
|
||||||
entire[0] != 'D' ||
|
|
||||||
entire[1] != 'T' ||
|
|
||||||
entire[2] != 'F'
|
|
||||||
) {
|
|
||||||
errorThrow("Invalid tileset header");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(entire[3] != 0x00) {
|
assertNull(loading->loading.tileset.data, "Data already defined?");
|
||||||
errorThrow("Unsupported tileset version");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fix endianness
|
assetfile_t *file = &loading->loading.tileset.file;
|
||||||
|
assetLoaderErrorChain(loading,
|
||||||
|
assetFileInit(file, loading->entry->name, NULL, NULL)
|
||||||
|
);
|
||||||
|
|
||||||
out->tileWidth = endianLittleToHost16(*(uint16_t *)(entire + 4));
|
uint8_t *data = memoryAllocate(file->size);
|
||||||
out->tileHeight = endianLittleToHost16(*(uint16_t *)(entire + 6));
|
assetLoaderErrorChain(loading, assetFileOpen(file));
|
||||||
out->columns = endianLittleToHost16(*(uint16_t *)(entire + 8));
|
assetLoaderErrorChain(loading, assetFileRead(file, data, file->size));
|
||||||
out->rows = endianLittleToHost16(*(uint16_t *)(entire + 10));
|
assetLoaderErrorChain(loading, assetFileClose(file));
|
||||||
// out->right = endianLittleToHost16(*(uint16_t *)(entire + 12));
|
assetLoaderErrorChain(loading, assetFileDispose(file));
|
||||||
// out->bottom = endianLittleToHost16(*(uint16_t *)(entire + 14));
|
assertTrue(
|
||||||
|
file->lastRead == file->size,
|
||||||
if(out->tileWidth == 0) {
|
"Failed to read entire tileset file."
|
||||||
errorThrow("Tile width cannot be 0");
|
);
|
||||||
}
|
|
||||||
if(out->tileHeight == 0) {
|
|
||||||
errorThrow("Tile height cannot be 0");
|
|
||||||
}
|
|
||||||
if(out->columns == 0) {
|
|
||||||
errorThrow("Column count cannot be 0");
|
|
||||||
}
|
|
||||||
if(out->rows == 0) {
|
|
||||||
errorThrow("Row count cannot be 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
out->uv[0] = endianLittleToHostFloat(*(float *)(entire + 16));
|
|
||||||
out->uv[1] = endianLittleToHostFloat(*(float *)(entire + 20));
|
|
||||||
|
|
||||||
if(out->uv[1] < 0.0f || out->uv[1] > 1.0f) {
|
|
||||||
errorThrow("Invalid v0 value in tileset");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup tileset
|
|
||||||
out->tileCount = out->columns * out->rows;
|
|
||||||
|
|
||||||
memoryFree(entire);
|
|
||||||
|
|
||||||
|
loading->loading.tileset.data = data;
|
||||||
|
loading->loading.tileset.state = ASSET_TILESET_LOADING_STATE_PARSE;
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetTilesetLoad(
|
errorret_t assetTilesetLoaderSync(assetloading_t *loading) {
|
||||||
const char_t *path,
|
assertNotNull(loading, "Loading cannot be NULL");
|
||||||
tileset_t *out
|
assertTrue(loading->type == ASSET_LOADER_TYPE_TILESET, "Invalid type.");
|
||||||
) {
|
assertIsMainThread("Must be called from the main thread.");
|
||||||
return assetLoad(path, assetTilesetLoader, NULL, out);
|
|
||||||
|
switch(loading->loading.tileset.state) {
|
||||||
|
case ASSET_TILESET_LOADING_STATE_INITIAL:
|
||||||
|
loading->loading.tileset.state = ASSET_TILESET_LOADING_STATE_READ_FILE;
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_PENDING_ASYNC;
|
||||||
|
errorOk();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_TILESET_LOADING_STATE_PARSE:
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t *data = loading->loading.tileset.data;
|
||||||
|
assertNotNull(data, "Tileset data should have been loaded by now.");
|
||||||
|
|
||||||
|
tileset_t *out = &loading->entry->data.tileset;
|
||||||
|
|
||||||
|
if(data[0] != 'D' || data[1] != 'T' || data[2] != 'F') {
|
||||||
|
memoryFree(data);
|
||||||
|
assetLoaderErrorThrow(loading, "Invalid tileset header");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(data[3] != 0x00) {
|
||||||
|
memoryFree(data);
|
||||||
|
assetLoaderErrorThrow(loading, "Unsupported tileset version");
|
||||||
|
}
|
||||||
|
|
||||||
|
out->tileWidth = endianLittleToHost16(*(uint16_t *)(data + 4));
|
||||||
|
out->tileHeight = endianLittleToHost16(*(uint16_t *)(data + 6));
|
||||||
|
out->columns = endianLittleToHost16(*(uint16_t *)(data + 8));
|
||||||
|
out->rows = endianLittleToHost16(*(uint16_t *)(data + 10));
|
||||||
|
|
||||||
|
if(out->tileWidth == 0) {
|
||||||
|
memoryFree(data);
|
||||||
|
assetLoaderErrorThrow(loading, "Tile width cannot be 0");
|
||||||
|
}
|
||||||
|
if(out->tileHeight == 0) {
|
||||||
|
memoryFree(data);
|
||||||
|
assetLoaderErrorThrow(loading, "Tile height cannot be 0");
|
||||||
|
}
|
||||||
|
if(out->columns == 0) {
|
||||||
|
memoryFree(data);
|
||||||
|
assetLoaderErrorThrow(loading, "Column count cannot be 0");
|
||||||
|
}
|
||||||
|
if(out->rows == 0) {
|
||||||
|
memoryFree(data);
|
||||||
|
assetLoaderErrorThrow(loading, "Row count cannot be 0");
|
||||||
|
}
|
||||||
|
|
||||||
|
out->uv[0] = endianLittleToHostFloat(*(float *)(data + 16));
|
||||||
|
out->uv[1] = endianLittleToHostFloat(*(float *)(data + 20));
|
||||||
|
|
||||||
|
if(out->uv[1] < 0.0f || out->uv[1] > 1.0f) {
|
||||||
|
memoryFree(data);
|
||||||
|
assetLoaderErrorThrow(loading, "Invalid v0 value in tileset");
|
||||||
|
}
|
||||||
|
|
||||||
|
out->tileCount = out->columns * out->rows;
|
||||||
|
memoryFree(data);
|
||||||
|
loading->loading.tileset.data = NULL;
|
||||||
|
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
|
||||||
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
void assetBatchTileset(
|
errorret_t assetTilesetDispose(assetentry_t *entry) {
|
||||||
assetbatch_t *batch,
|
assertNotNull(entry, "Entry cannot be NULL");
|
||||||
const char_t *path,
|
assertTrue(entry->type == ASSET_LOADER_TYPE_TILESET, "Invalid type.");
|
||||||
tileset_t **out
|
assertIsMainThread("Must be called from the main thread.");
|
||||||
) {
|
|
||||||
assetBatchAdd(
|
errorOk();
|
||||||
batch,
|
|
||||||
path,
|
|
||||||
assetTilesetLoader,
|
|
||||||
NULL, 0,
|
|
||||||
sizeof(tileset_t),
|
|
||||||
NULL,
|
|
||||||
(void**)out
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
@@ -6,40 +6,55 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "asset/asset.h"
|
#include "asset/assetfile.h"
|
||||||
#include "asset/assetbatch.h"
|
|
||||||
#include "display/texture/tileset.h"
|
#include "display/texture/tileset.h"
|
||||||
|
|
||||||
/**
|
typedef struct assetloading_s assetloading_t;
|
||||||
* Handler for tileset assets.
|
typedef struct assetentry_s assetentry_t;
|
||||||
*
|
|
||||||
* @param file Asset file to load the tileset from.
|
typedef struct {
|
||||||
* @return Any error that occurs during loading.
|
void *nothing;
|
||||||
*/
|
} assettilesetloaderinput_t;
|
||||||
errorret_t assetTilesetLoader(assetfile_t *file);
|
|
||||||
|
typedef enum {
|
||||||
|
ASSET_TILESET_LOADING_STATE_INITIAL,
|
||||||
|
ASSET_TILESET_LOADING_STATE_READ_FILE,
|
||||||
|
ASSET_TILESET_LOADING_STATE_PARSE,
|
||||||
|
ASSET_TILESET_LOADING_STATE_DONE
|
||||||
|
} assettilesetloadingstate_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
assetfile_t file;
|
||||||
|
assettilesetloadingstate_t state;
|
||||||
|
|
||||||
|
uint8_t *data;
|
||||||
|
} assettilesetloaderloading_t;
|
||||||
|
|
||||||
|
typedef tileset_t assettilesetoutput_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a tileset from the specified path.
|
* Asynchronous loader for tileset assets. Reads the raw DTF file bytes into
|
||||||
|
* the loading buffer so the sync phase can parse without blocking the main
|
||||||
|
* thread on I/O.
|
||||||
*
|
*
|
||||||
* @param path Path to the tileset asset.
|
* @param loading Loading information for the asset being loaded.
|
||||||
* @param out Output tileset to load into.
|
* @return Error code indicating success or failure of the load operation.
|
||||||
* @return Any error that occurs during loading.
|
|
||||||
*/
|
*/
|
||||||
errorret_t assetTilesetLoad(
|
errorret_t assetTilesetLoaderAsync(assetloading_t *loading);
|
||||||
const char_t *path,
|
|
||||||
tileset_t *out
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a tileset load request to a batch. The cache-owned tileset_t* is
|
* Synchronous loader for tileset assets. Parses the DTF binary previously
|
||||||
* written to *out when the batch completes.
|
* read by the async phase and populates the output tileset_t.
|
||||||
*
|
*
|
||||||
* @param batch The batch to add to.
|
* @param loading Loading information for the asset being loaded.
|
||||||
* @param path Path to the tileset asset.
|
* @return Error code indicating success or failure of the load operation.
|
||||||
* @param out Receives a pointer to the loaded tileset on completion.
|
|
||||||
*/
|
*/
|
||||||
void assetBatchTileset(
|
errorret_t assetTilesetLoaderSync(assetloading_t *loading);
|
||||||
assetbatch_t *batch,
|
|
||||||
const char_t *path,
|
/**
|
||||||
tileset_t **out
|
* Disposer for tileset assets.
|
||||||
);
|
*
|
||||||
|
* @param entry Asset entry containing the tileset to dispose.
|
||||||
|
* @return Error code indicating success or failure of the dispose operation.
|
||||||
|
*/
|
||||||
|
errorret_t assetTilesetDispose(assetentry_t *entry);
|
||||||
|
|||||||
@@ -8,46 +8,88 @@
|
|||||||
#include "assetjsonloader.h"
|
#include "assetjsonloader.h"
|
||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
|
#include "asset/loader/assetloading.h"
|
||||||
|
#include "asset/loader/assetentry.h"
|
||||||
|
|
||||||
errorret_t assetJsonLoadFileToDoc(assetfile_t *file, yyjson_doc **outDoc) {
|
errorret_t assetJsonLoaderAsync(assetloading_t *loading) {
|
||||||
assertNotNull(file, "Asset file pointer for JSON loader is null.");
|
assertNotNull(loading, "Loading cannot be NULL");
|
||||||
assertNotNull(outDoc, "Output pointer for JSON loader is null.");
|
assertNotMainThread("Async loader should not be on main thread.");
|
||||||
|
|
||||||
if(file->size > ASSET_JSON_FILE_SIZE_MAX) {
|
if(loading->loading.json.state != ASSET_JSON_LOADING_STATE_READ_FILE) {
|
||||||
errorThrow("JSON exceeds maximum allowed size");
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create buffer
|
assertNull(loading->loading.json.buffer, "Buffer already defined?");
|
||||||
uint8_t *buffer = memoryAllocate(file->size);
|
|
||||||
|
|
||||||
errorChain(assetFileOpen(file));
|
assetfile_t *file = &loading->loading.json.file;
|
||||||
|
assetLoaderErrorChain(loading,
|
||||||
// Read entire file
|
assetFileInit(file, loading->entry->name, NULL, NULL)
|
||||||
errorChain(assetFileRead(file, buffer, file->size));
|
|
||||||
assertTrue(file->lastRead == file->size, "Failed to read entire JSON file.");
|
|
||||||
|
|
||||||
errorChain(assetFileClose(file));
|
|
||||||
|
|
||||||
*outDoc = yyjson_read(
|
|
||||||
buffer,
|
|
||||||
file->size,
|
|
||||||
YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS
|
|
||||||
);
|
);
|
||||||
memoryFree(buffer);
|
|
||||||
|
|
||||||
if(!*outDoc) errorThrow("Failed to parse JSON");
|
if(file->size > ASSET_JSON_FILE_SIZE_MAX) {
|
||||||
|
assetLoaderErrorThrow(loading, "JSON exceeds maximum allowed size");
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t fileSize = (size_t)file->size;
|
||||||
|
uint8_t *buffer = memoryAllocate(fileSize);
|
||||||
|
assetLoaderErrorChain(loading, assetFileOpen(file));
|
||||||
|
assetLoaderErrorChain(loading, assetFileRead(file, buffer, fileSize));
|
||||||
|
assertTrue(file->lastRead == file->size, "Failed to read entire JSON file.");
|
||||||
|
assetLoaderErrorChain(loading, assetFileClose(file));
|
||||||
|
assetLoaderErrorChain(loading, assetFileDispose(file));
|
||||||
|
|
||||||
|
loading->loading.json.buffer = buffer;
|
||||||
|
loading->loading.json.size = fileSize;
|
||||||
|
loading->loading.json.state = ASSET_JSON_LOADING_STATE_PARSE;
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetJsonLoader(assetfile_t *file) {
|
errorret_t assetJsonLoaderSync(assetloading_t *loading) {
|
||||||
yyjson_doc **outDoc = (yyjson_doc **)file->output;
|
assertNotNull(loading, "Loading cannot be NULL");
|
||||||
assertNotNull(outDoc, "Output pointer for JSON loader is null.");
|
assertTrue(loading->type == ASSET_LOADER_TYPE_JSON, "Invalid type.");
|
||||||
return assetJsonLoadFileToDoc(file, outDoc);
|
assertIsMainThread("Must be called from the main thread.");
|
||||||
|
|
||||||
|
switch(loading->loading.json.state) {
|
||||||
|
case ASSET_JSON_LOADING_STATE_INITIAL:
|
||||||
|
loading->loading.json.state = ASSET_JSON_LOADING_STATE_READ_FILE;
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_PENDING_ASYNC;
|
||||||
|
errorOk();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_JSON_LOADING_STATE_PARSE:
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t *buffer = loading->loading.json.buffer;
|
||||||
|
assertNotNull(buffer, "JSON buffer should have been loaded by now.");
|
||||||
|
|
||||||
|
loading->entry->data.json = yyjson_read(
|
||||||
|
(char *)buffer,
|
||||||
|
loading->loading.json.size,
|
||||||
|
YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS
|
||||||
|
);
|
||||||
|
memoryFree(buffer);
|
||||||
|
loading->loading.json.buffer = NULL;
|
||||||
|
|
||||||
|
if(!loading->entry->data.json) {
|
||||||
|
assetLoaderErrorThrow(loading, "Failed to parse JSON");
|
||||||
|
}
|
||||||
|
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
|
||||||
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetJsonLoad(
|
errorret_t assetJsonDispose(assetentry_t *entry) {
|
||||||
const char_t *path,
|
assertNotNull(entry, "Asset entry cannot be NULL");
|
||||||
yyjson_doc **outDoc
|
assertTrue(entry->type == ASSET_LOADER_TYPE_JSON, "Invalid type.");
|
||||||
) {
|
assertIsMainThread("Must be called from the main thread.");
|
||||||
return assetLoad(path, assetJsonLoader, NULL, outDoc);
|
|
||||||
|
yyjson_doc_free(entry->data.json);
|
||||||
|
entry->data.json = NULL;
|
||||||
|
|
||||||
|
errorOk();
|
||||||
}
|
}
|
||||||
@@ -6,40 +6,32 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "asset/asset.h"
|
#include "asset/assetfile.h"
|
||||||
#include "yyjson.h"
|
#include "yyjson.h"
|
||||||
|
|
||||||
#define ASSET_JSON_FILE_SIZE_MAX 1024*256
|
#define ASSET_JSON_FILE_SIZE_MAX 1024*256
|
||||||
|
|
||||||
|
typedef struct assetloading_s assetloading_t;
|
||||||
|
typedef struct assetentry_s assetentry_t;
|
||||||
|
|
||||||
|
typedef struct { void *nothing; } assetjsonloaderinput_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ASSET_JSON_LOADING_STATE_INITIAL,
|
||||||
|
ASSET_JSON_LOADING_STATE_READ_FILE,
|
||||||
|
ASSET_JSON_LOADING_STATE_PARSE,
|
||||||
|
ASSET_JSON_LOADING_STATE_DONE
|
||||||
|
} assetjsonloadingstate_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
void *nothing;
|
assetfile_t file;
|
||||||
} assetjsonloaderparams_t;
|
assetjsonloadingstate_t state;
|
||||||
|
uint8_t *buffer;
|
||||||
|
size_t size;
|
||||||
|
} assetjsonloaderloading_t;
|
||||||
|
|
||||||
/**
|
typedef yyjson_doc * assetjsonoutput_t;
|
||||||
* Loads a JSON document from the specified asset file.
|
|
||||||
*
|
|
||||||
* @param file Asset file to load the JSON document from.
|
|
||||||
* @param outDoc Pointer to store the loaded JSON document.
|
|
||||||
* @return Any error that occurs during loading.
|
|
||||||
*/
|
|
||||||
errorret_t assetJsonLoadFileToDoc(assetfile_t *file, yyjson_doc **outDoc);
|
|
||||||
|
|
||||||
/**
|
errorret_t assetJsonLoaderAsync(assetloading_t *loading);
|
||||||
* Handler for locale assets.
|
errorret_t assetJsonLoaderSync(assetloading_t *loading);
|
||||||
*
|
errorret_t assetJsonDispose(assetentry_t *entry);
|
||||||
* @param file Asset file to load the locale from.
|
|
||||||
* @return Any error that occurs during loading.
|
|
||||||
*/
|
|
||||||
errorret_t assetJsonLoader(assetfile_t *file);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads a locale from the specified path.
|
|
||||||
*
|
|
||||||
* @param path Path to the locale asset.
|
|
||||||
* @param outDoc Pointer to store the loaded JSON document.
|
|
||||||
* @return Any error that occurs during loading.
|
|
||||||
*/
|
|
||||||
errorret_t assetJsonLoad(
|
|
||||||
const char_t *path,
|
|
||||||
yyjson_doc **outDoc
|
|
||||||
);
|
|
||||||
|
|||||||
@@ -11,38 +11,71 @@
|
|||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
|
|
||||||
errorret_t assetLocaleFileInit(
|
#include "asset/loader/assetloading.h"
|
||||||
assetlocalefile_t *localeFile,
|
#include "asset/loader/assetentry.h"
|
||||||
const char_t *path
|
|
||||||
) {
|
|
||||||
assertNotNull(localeFile, "Locale file cannot be NULL.");
|
|
||||||
assertNotNull(path, "Locale file path cannot be NULL.");
|
|
||||||
|
|
||||||
|
errorret_t assetLocaleLoaderAsync(assetloading_t *loading) {
|
||||||
|
assertNotNull(loading, "Loading cannot be NULL");
|
||||||
|
assertNotMainThread("Async loader should not be on main thread.");
|
||||||
|
|
||||||
|
if(loading->loading.locale.state != ASSET_LOCALE_LOADER_STATE_LOAD_HEADER) {
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
assetlocalefile_t *localeFile = &loading->entry->data.locale;
|
||||||
memoryZero(localeFile, sizeof(assetlocalefile_t));
|
memoryZero(localeFile, sizeof(assetlocalefile_t));
|
||||||
|
assetLoaderErrorChain(loading, assetFileInit(
|
||||||
|
&localeFile->file, loading->entry->name, NULL, NULL
|
||||||
|
));
|
||||||
|
assetLoaderErrorChain(loading, assetFileOpen(&localeFile->file));
|
||||||
|
|
||||||
// Init the asset file.
|
|
||||||
errorChain(assetFileInit(&localeFile->file, path, NULL, NULL));
|
|
||||||
|
|
||||||
// Open the file handle
|
|
||||||
errorChain(assetFileOpen(&localeFile->file));
|
|
||||||
|
|
||||||
// Get the blank key, this is basically the header info for po files
|
|
||||||
char_t buffer[1024];
|
char_t buffer[1024];
|
||||||
errorChain(assetLocaleGetString(localeFile, "", 0, buffer, sizeof(buffer)));
|
assetLoaderErrorChain(loading, assetLocaleGetString(
|
||||||
errorChain(assetLocaleParseHeader(localeFile, buffer, sizeof(buffer)));
|
localeFile, "", 0, buffer, sizeof(buffer)
|
||||||
|
));
|
||||||
|
assetLoaderErrorChain(loading, assetLocaleParseHeader(
|
||||||
|
localeFile, buffer, sizeof(buffer)
|
||||||
|
));
|
||||||
|
|
||||||
|
loading->loading.locale.state = ASSET_LOCALE_LOADER_STATE_DONE;
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetLocaleFileDispose(assetlocalefile_t *localeFile) {
|
errorret_t assetLocaleLoaderSync(assetloading_t *loading) {
|
||||||
assertNotNull(localeFile, "Locale file cannot be NULL.");
|
assertNotNull(loading, "Loading cannot be NULL");
|
||||||
|
assertTrue(loading->type == ASSET_LOADER_TYPE_LOCALE, "Invalid type.");
|
||||||
|
assertIsMainThread("Must be called from the main thread.");
|
||||||
|
|
||||||
|
switch(loading->loading.locale.state) {
|
||||||
|
case ASSET_LOCALE_LOADER_STATE_INITIAL:
|
||||||
|
loading->loading.locale.state = ASSET_LOCALE_LOADER_STATE_LOAD_HEADER;
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_PENDING_ASYNC;
|
||||||
|
errorOk();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_LOCALE_LOADER_STATE_DONE:
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleDispose(assetentry_t *entry) {
|
||||||
|
assertNotNull(entry, "Asset entry cannot be NULL");
|
||||||
|
assertTrue(entry->type == ASSET_LOADER_TYPE_LOCALE, "Invalid type.");
|
||||||
|
assertIsMainThread("Must be called from the main thread.");
|
||||||
|
|
||||||
|
assetlocalefile_t *localeFile = &entry->data.locale;
|
||||||
errorChain(assetFileClose(&localeFile->file));
|
errorChain(assetFileClose(&localeFile->file));
|
||||||
errorChain(assetFileDispose(&localeFile->file));
|
return assetFileDispose(&localeFile->file);
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// These functions probably need some cleaning;
|
||||||
errorret_t assetLocaleParseHeader(
|
errorret_t assetLocaleParseHeader(
|
||||||
assetlocalefile_t *localeFile,
|
assetlocalefile_t *localeFile,
|
||||||
char_t *headerBuffer,
|
char_t *headerBuffer,
|
||||||
@@ -307,13 +340,21 @@ errorret_t assetLocaleLineSkipBlanks(
|
|||||||
while(!reader->eof) {
|
while(!reader->eof) {
|
||||||
// Skip blank lines
|
// Skip blank lines
|
||||||
if(lineBuffer[0] == '\0') {
|
if(lineBuffer[0] == '\0') {
|
||||||
errorChain(assetFileLineReaderNext(reader));
|
errorret_t r = assetFileLineReaderNext(reader);
|
||||||
|
if(errorIsNotOk(r)) {
|
||||||
|
errorCatch(r);
|
||||||
|
break;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip comment lines
|
// Skip comment lines
|
||||||
if(lineBuffer[0] == '#') {
|
if(lineBuffer[0] == '#') {
|
||||||
errorChain(assetFileLineReaderNext(reader));
|
errorret_t r = assetFileLineReaderNext(reader);
|
||||||
|
if(errorIsNotOk(r)) {
|
||||||
|
errorCatch(r);
|
||||||
|
break;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,7 +370,11 @@ errorret_t assetLocaleLineSkipBlanks(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(onlySpaces) {
|
if(onlySpaces) {
|
||||||
errorChain(assetFileLineReaderNext(reader));
|
errorret_t r = assetFileLineReaderNext(reader);
|
||||||
|
if(errorIsNotOk(r)) {
|
||||||
|
errorCatch(r);
|
||||||
|
break;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -365,10 +410,18 @@ errorret_t assetLocaleLineUnbuffer(
|
|||||||
|
|
||||||
// Now start buffering lines out
|
// Now start buffering lines out
|
||||||
while(!reader->eof) {
|
while(!reader->eof) {
|
||||||
errorChain(assetFileLineReaderNext(reader));
|
errorret_t r = assetFileLineReaderNext(reader);
|
||||||
|
if(errorIsNotOk(r)) {
|
||||||
|
errorCatch(r);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Skip blank lines
|
// Skip blank lines
|
||||||
errorChain(assetLocaleLineSkipBlanks(reader, lineBuffer));
|
r = assetLocaleLineSkipBlanks(reader, lineBuffer);
|
||||||
|
if(errorIsNotOk(r)) {
|
||||||
|
errorCatch(r);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Skip starting spaces
|
// Skip starting spaces
|
||||||
char_t *ptr = (char_t *)lineBuffer;
|
char_t *ptr = (char_t *)lineBuffer;
|
||||||
@@ -593,7 +646,7 @@ errorret_t assetLocaleGetStringWithVA(
|
|||||||
tempBuffer,
|
tempBuffer,
|
||||||
bufferSize
|
bufferSize
|
||||||
);
|
);
|
||||||
if(ret.code != ERROR_OK) {
|
if(errorIsNotOk(ret)) {
|
||||||
memoryFree(tempBuffer);
|
memoryFree(tempBuffer);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -641,7 +694,7 @@ errorret_t assetLocaleGetStringWithArgs(
|
|||||||
format,
|
format,
|
||||||
bufferSize
|
bufferSize
|
||||||
);
|
);
|
||||||
if(ret.code != ERROR_OK) {
|
if(errorIsNotOk(ret)) {
|
||||||
memoryFree(format);
|
memoryFree(format);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -763,17 +816,24 @@ errorret_t assetLocaleGetStringWithArgs(
|
|||||||
case 'f':
|
case 'f':
|
||||||
if(
|
if(
|
||||||
args[nextArg].type != ASSET_LOCALE_ARG_FLOAT &&
|
args[nextArg].type != ASSET_LOCALE_ARG_FLOAT &&
|
||||||
args[nextArg].type != ASSET_LOCALE_ARG_INT
|
args[nextArg].type != ASSET_LOCALE_ARG_INT &&
|
||||||
|
args[nextArg].type != ASSET_LOCALE_ARG_FIXED
|
||||||
) {
|
) {
|
||||||
memoryFree(format);
|
memoryFree(format);
|
||||||
errorThrow("Expected float or int locale argument for ID: %s", id);
|
errorThrow(
|
||||||
|
"Expected float, fixed, or int locale argument for ID: %s",
|
||||||
|
id
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t floatValue = (
|
float_t floatValue;
|
||||||
args[nextArg].type == ASSET_LOCALE_ARG_FLOAT ?
|
if(args[nextArg].type == ASSET_LOCALE_ARG_FLOAT) {
|
||||||
args[nextArg].floatValue :
|
floatValue = args[nextArg].floatValue;
|
||||||
(float_t)args[nextArg].intValue
|
} else if(args[nextArg].type == ASSET_LOCALE_ARG_FIXED) {
|
||||||
);
|
floatValue = fixedToFloat(args[nextArg].fixedValue);
|
||||||
|
} else {
|
||||||
|
floatValue = (float_t)args[nextArg].intValue;
|
||||||
|
}
|
||||||
|
|
||||||
written = snprintf(
|
written = snprintf(
|
||||||
valueBuffer,
|
valueBuffer,
|
||||||
|
|||||||
@@ -6,10 +6,36 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "asset/asset.h"
|
#include "asset/assetfile.h"
|
||||||
|
#include "util/fixed.h"
|
||||||
|
|
||||||
|
typedef struct assetloading_s assetloading_t;
|
||||||
|
typedef struct assetentry_s assetentry_t;
|
||||||
|
|
||||||
|
/** Input passed to the locale loader - currently unused. */
|
||||||
|
typedef struct { void *nothing; } assetlocaleloaderinput_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ASSET_LOCALE_LOADER_STATE_INITIAL,
|
||||||
|
ASSET_LOCALE_LOADER_STATE_LOAD_HEADER,
|
||||||
|
ASSET_LOCALE_LOADER_STATE_DONE
|
||||||
|
} assetlocaleloaderstate_t;
|
||||||
|
|
||||||
|
/** Per-slot scratch data used while the locale file is loading. */
|
||||||
|
typedef struct {
|
||||||
|
assetlocaleloaderstate_t state;
|
||||||
|
} assetlocaleloaderloading_t;
|
||||||
|
|
||||||
|
/** Maximum number of distinct plural forms a locale file may declare. */
|
||||||
#define ASSET_LOCALE_FILE_PLURAL_FORM_COUNT 6
|
#define ASSET_LOCALE_FILE_PLURAL_FORM_COUNT 6
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comparison operator used in a plural-form expression.
|
||||||
|
*
|
||||||
|
* Each condition in the plural-form header is evaluated as
|
||||||
|
* `n <op> value`
|
||||||
|
* where `n` is the runtime plural count.
|
||||||
|
*/
|
||||||
typedef enum {
|
typedef enum {
|
||||||
ASSET_LOCALE_PLURAL_OP_EQUAL,
|
ASSET_LOCALE_PLURAL_OP_EQUAL,
|
||||||
ASSET_LOCALE_PLURAL_OP_NOT_EQUAL,
|
ASSET_LOCALE_PLURAL_OP_NOT_EQUAL,
|
||||||
@@ -19,50 +45,109 @@ typedef enum {
|
|||||||
ASSET_LOCALE_PLURAL_OP_GREATER_EQUAL
|
ASSET_LOCALE_PLURAL_OP_GREATER_EQUAL
|
||||||
} assetlocalepluraloperation_t;
|
} assetlocalepluraloperation_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Discriminator tag for a locale string argument.
|
||||||
|
* @see assetlocalearg_t
|
||||||
|
*/
|
||||||
typedef enum {
|
typedef enum {
|
||||||
ASSET_LOCALE_ARG_STRING,
|
ASSET_LOCALE_ARG_STRING,
|
||||||
ASSET_LOCALE_ARG_INT,
|
ASSET_LOCALE_ARG_INT,
|
||||||
ASSET_LOCALE_ARG_FLOAT
|
ASSET_LOCALE_ARG_FLOAT,
|
||||||
|
ASSET_LOCALE_ARG_FIXED
|
||||||
} assetlocaleargtype_t;
|
} assetlocaleargtype_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A single typed argument for locale string substitution.
|
||||||
|
*
|
||||||
|
* Used with @ref assetLocaleGetStringWithArgs to fill `%s`, `%d`, or `%f`
|
||||||
|
* placeholders inside a translated string.
|
||||||
|
*/
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
/** Which union member is active. */
|
||||||
assetlocaleargtype_t type;
|
assetlocaleargtype_t type;
|
||||||
union {
|
union {
|
||||||
const char_t *stringValue;
|
const char_t *stringValue;
|
||||||
int32_t intValue;
|
int32_t intValue;
|
||||||
float_t floatValue;
|
float_t floatValue;
|
||||||
|
fixed_t fixedValue;
|
||||||
};
|
};
|
||||||
} assetlocalearg_t;
|
} assetlocalearg_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runtime state for an open locale file.
|
||||||
|
*
|
||||||
|
* Loaded once by @ref assetLocaleLoaderSync and kept alive for the lifetime
|
||||||
|
* of the asset entry. The plural-form fields are populated from the PO header
|
||||||
|
* by @ref assetLocaleParseHeader.
|
||||||
|
*
|
||||||
|
* Plural evaluation works as a linear scan over `pluralStateCount - 1`
|
||||||
|
* conditions; if none match, `pluralDefaultIndex` is returned.
|
||||||
|
*/
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
/** Underlying file handle used to rewind and re-read the PO data. */
|
||||||
assetfile_t file;
|
assetfile_t file;
|
||||||
|
|
||||||
|
/** Comparison operator for each conditional plural clause. */
|
||||||
assetlocalepluraloperation_t pluralOps[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
assetlocalepluraloperation_t pluralOps[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||||
|
|
||||||
|
/** Right-hand value for each conditional plural clause. */
|
||||||
int32_t pluralValues[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
int32_t pluralValues[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||||
|
|
||||||
|
/** Form index returned when the corresponding condition is true. */
|
||||||
int32_t pluralIndices[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
int32_t pluralIndices[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||||
|
|
||||||
|
/** Total number of plural forms declared by `nplurals=`. */
|
||||||
uint8_t pluralStateCount;
|
uint8_t pluralStateCount;
|
||||||
|
|
||||||
|
/** Form index used when no conditional clause matches. */
|
||||||
uint8_t pluralDefaultIndex;
|
uint8_t pluralDefaultIndex;
|
||||||
} assetlocalefile_t;
|
} assetlocalefile_t;
|
||||||
|
|
||||||
/**
|
/** Convenience alias - the loaded output type of a locale asset entry. */
|
||||||
* Initialize a locale asset file.
|
typedef assetlocalefile_t assetlocaleoutput_t;
|
||||||
*
|
|
||||||
* @param localeFile The locale file to initialize.
|
|
||||||
* @param path The path to the locale file.
|
|
||||||
* @return An error code if a failure occurs.
|
|
||||||
*/
|
|
||||||
errorret_t assetLocaleFileInit(
|
|
||||||
assetlocalefile_t *localeFile,
|
|
||||||
const char_t *path
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispose of a locale asset file.
|
* Asynchronous loader callback. Opens the locale file, reads the PO header,
|
||||||
|
* and parses plural-form rules. All I/O happens here so the main thread is
|
||||||
|
* not blocked. Sets entry state to `ASSET_ENTRY_STATE_PENDING_SYNC` on
|
||||||
|
* success or `ASSET_ENTRY_STATE_ERROR` on failure.
|
||||||
*
|
*
|
||||||
* @param localeFile The locale file to dispose of.
|
* @param loading The loading slot for this asset entry.
|
||||||
* @return An error code if a failure occurs.
|
* @return OK on success, error otherwise.
|
||||||
*/
|
*/
|
||||||
errorret_t assetLocaleFileDispose(assetlocalefile_t *localeFile);
|
errorret_t assetLocaleLoaderAsync(assetloading_t *loading);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Synchronous loader callback. Confirms the async phase completed and marks
|
||||||
|
* the entry as `ASSET_ENTRY_STATE_LOADED`.
|
||||||
|
*
|
||||||
|
* @param loading The loading slot for this asset entry.
|
||||||
|
* @return OK on success, error otherwise.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleLoaderSync(assetloading_t *loading);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dispose callback. Closes the open file handle and zeros the locale data.
|
||||||
|
*
|
||||||
|
* @param entry The asset entry to dispose.
|
||||||
|
* @return OK on success, error otherwise.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleDispose(assetentry_t *entry);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses the `Plural-Forms:` line from a PO header string and populates the
|
||||||
|
* plural-form fields of `localeFile`. If no `Plural-Forms:` key is present
|
||||||
|
* the function returns OK without modifying the struct.
|
||||||
|
*
|
||||||
|
* Supports the `nplurals=N; plural=(<expr>);` syntax where `<expr>` is a
|
||||||
|
* chain of `n <op> value ? index : ...` ternary conditions ending with a
|
||||||
|
* fallback index.
|
||||||
|
*
|
||||||
|
* @param localeFile Struct whose plural fields will be filled in.
|
||||||
|
* @param headerBuffer The raw PO header string (msgstr of msgid "").
|
||||||
|
* @param headerBufferSize Size of `headerBuffer` in bytes.
|
||||||
|
* @return OK on success, error if the plural expression is malformed.
|
||||||
|
*/
|
||||||
errorret_t assetLocaleParseHeader(
|
errorret_t assetLocaleParseHeader(
|
||||||
assetlocalefile_t *localeFile,
|
assetlocalefile_t *localeFile,
|
||||||
char_t *headerBuffer,
|
char_t *headerBuffer,
|
||||||
@@ -70,11 +155,29 @@ errorret_t assetLocaleParseHeader(
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Skips blank lines and comment lines in the line reader.
|
* Evaluates which plural form index to use for a given count.
|
||||||
*
|
*
|
||||||
* @param reader Line reader to read from.
|
* Walks the conditions stored in `file` in order; returns the index for the
|
||||||
* @param lineBuffer Buffer to use for reading lines.
|
* first condition that matches `pluralCount`. Falls back to
|
||||||
* @return Any error that occurs during skipping.
|
* `file->pluralDefaultIndex` if none match.
|
||||||
|
*
|
||||||
|
* @param file Locale file with parsed plural rules.
|
||||||
|
* @param pluralCount The runtime count (e.g. number of items).
|
||||||
|
* @return Zero-based plural form index.
|
||||||
|
*/
|
||||||
|
uint8_t assetLocaleEvaluatePlural(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const int32_t pluralCount
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Advances the line reader past blank lines, comment lines (starting with
|
||||||
|
* `#`), and lines containing only spaces. Stops at the first content line or
|
||||||
|
* end of file.
|
||||||
|
*
|
||||||
|
* @param reader Line reader positioned at the current line.
|
||||||
|
* @param lineBuffer Output buffer that the reader writes each line into.
|
||||||
|
* @return OK on success, error if reading fails.
|
||||||
*/
|
*/
|
||||||
errorret_t assetLocaleLineSkipBlanks(
|
errorret_t assetLocaleLineSkipBlanks(
|
||||||
assetfilelinereader_t *reader,
|
assetfilelinereader_t *reader,
|
||||||
@@ -82,16 +185,19 @@ errorret_t assetLocaleLineSkipBlanks(
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unbuffers a potentially multi-line quoted string from the line reader.
|
* Reads a PO quoted string value from the current line and any immediately
|
||||||
|
* following continuation lines that begin with `"`.
|
||||||
*
|
*
|
||||||
* This will read lines until it finds a line that starts with a quote, then
|
* Handles standard C escape sequences (`\n`, `\t`, `\\`, `\"`).
|
||||||
* read until the closing quote.
|
|
||||||
*
|
*
|
||||||
* @param reader Line reader to read from.
|
* @param reader Line reader positioned at the line containing the opening
|
||||||
* @param lineBuffer Buffer to use for reading lines.
|
* quote (e.g. `msgstr "..."`).
|
||||||
* @param stringBuffer Buffer to write the unbuffered string to.
|
* @param lineBuffer Buffer filled on each @ref assetFileLineReaderNext
|
||||||
* @param stringBufferSize Size of the string buffer.
|
* call; also used to detect continuation lines.
|
||||||
* @return Any error that occurs during unbuffering.
|
* @param stringBuffer Destination for the unescaped string content.
|
||||||
|
* @param stringBufferSize Capacity of `stringBuffer` in bytes.
|
||||||
|
* @return OK on success, error if a quote or escape sequence is malformed or
|
||||||
|
* the buffer would overflow.
|
||||||
*/
|
*/
|
||||||
errorret_t assetLocaleLineUnbuffer(
|
errorret_t assetLocaleLineUnbuffer(
|
||||||
assetfilelinereader_t *reader,
|
assetfilelinereader_t *reader,
|
||||||
@@ -101,14 +207,19 @@ errorret_t assetLocaleLineUnbuffer(
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test function for locale asset loading.
|
* Looks up a translated string by message ID from the open locale file.
|
||||||
*
|
*
|
||||||
* @param file Asset file to test loading from.
|
* Rewinds the file and scans from the beginning on every call. For plural
|
||||||
* @param messageId The message ID to retrieve.
|
* entries (`msgid_plural`) the `pluralCount` is evaluated against the loaded
|
||||||
* @param pluralCount Count for formulating the plural variant.
|
* plural rules to select the correct `msgstr[N]` form.
|
||||||
* @param stringBuffer Buffer to write the retrieved string to.
|
*
|
||||||
* @param stringBufferSize Size of the string buffer.
|
* @param file Locale file to search. Must be open.
|
||||||
* @return Any error that occurs during testing.
|
* @param messageId PO message ID to find (`""` retrieves the header entry).
|
||||||
|
* @param pluralCount Count used to select the plural form (ignored for
|
||||||
|
* singular entries).
|
||||||
|
* @param stringBuffer Buffer to receive the translated string.
|
||||||
|
* @param stringBufferSize Capacity of `stringBuffer` in bytes.
|
||||||
|
* @return OK on success, error if the message ID is not found or I/O fails.
|
||||||
*/
|
*/
|
||||||
errorret_t assetLocaleGetString(
|
errorret_t assetLocaleGetString(
|
||||||
assetlocalefile_t *file,
|
assetlocalefile_t *file,
|
||||||
@@ -119,15 +230,21 @@ errorret_t assetLocaleGetString(
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test function for locale asset loading with a variable argument list.
|
* Looks up a translated string and formats it with a `printf`-style variadic
|
||||||
|
* argument list.
|
||||||
*
|
*
|
||||||
* @param file Asset file to test loading from.
|
* Retrieves the raw format string via @ref assetLocaleGetString then applies
|
||||||
* @param messageId The message ID to retrieve.
|
* `vsnprintf` with the provided arguments.
|
||||||
* @param pluralCount Count for formulating the plural variant.
|
*
|
||||||
* @param buffer Buffer to write the retrieved string to.
|
* @param file Locale file to search. Must be open.
|
||||||
* @param bufferSize Size of the buffer.
|
* @param messageId PO message ID to find.
|
||||||
* @param ... Additional arguments for formatting the string.
|
* @param pluralCount Count used to select the plural form.
|
||||||
* @return Any error that occurs during testing.
|
* @param buffer Buffer to receive the formatted string.
|
||||||
|
* @param bufferSize Capacity of `buffer` in bytes.
|
||||||
|
* @param ... Format arguments matching the specifiers in the translated
|
||||||
|
* string.
|
||||||
|
* @return OK on success, error if the message is not found or formatting
|
||||||
|
* fails.
|
||||||
*/
|
*/
|
||||||
errorret_t assetLocaleGetStringWithVA(
|
errorret_t assetLocaleGetStringWithVA(
|
||||||
assetlocalefile_t *file,
|
assetlocalefile_t *file,
|
||||||
@@ -139,16 +256,22 @@ errorret_t assetLocaleGetStringWithVA(
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test function for locale asset loading with a list of arguments.
|
* Looks up a translated string and substitutes typed arguments into its
|
||||||
|
* `%s`, `%d`, and `%f` placeholders.
|
||||||
*
|
*
|
||||||
* @param file Asset file to test loading from.
|
* Unlike @ref assetLocaleGetStringWithVA this variant accepts an explicit
|
||||||
* @param messageId The message ID to retrieve.
|
* array of @ref assetlocalearg_t values, which is safer to use from
|
||||||
* @param pluralCount Count for formulating the plural variant.
|
* generated or scripted call sites.
|
||||||
* @param buffer Buffer to write the retrieved string to.
|
*
|
||||||
* @param bufferSize Size of the buffer.
|
* @param file Locale file to search. Must be open.
|
||||||
* @param args List of arguments for formatting the string.
|
* @param messageId PO message ID to find.
|
||||||
* @param argCount Number of arguments in the list.
|
* @param pluralCount Count used to select the plural form.
|
||||||
* @return Any error that occurs during testing.
|
* @param buffer Buffer to receive the formatted string.
|
||||||
|
* @param bufferSize Capacity of `buffer` in bytes.
|
||||||
|
* @param args Array of typed arguments; may be NULL when `argCount` is 0.
|
||||||
|
* @param argCount Number of elements in `args`.
|
||||||
|
* @return OK on success, error if the message is not found, an argument type
|
||||||
|
* mismatches the specifier, or the buffer would overflow.
|
||||||
*/
|
*/
|
||||||
errorret_t assetLocaleGetStringWithArgs(
|
errorret_t assetLocaleGetStringWithArgs(
|
||||||
assetlocalefile_t *file,
|
assetlocalefile_t *file,
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ console_t CONSOLE;
|
|||||||
|
|
||||||
void consoleInit(void) {
|
void consoleInit(void) {
|
||||||
memoryZero(&CONSOLE, sizeof(console_t));
|
memoryZero(&CONSOLE, sizeof(console_t));
|
||||||
|
CONSOLE.visible = true;
|
||||||
|
|
||||||
#ifdef DUSK_CONSOLE_POSIX
|
#ifdef DUSK_CONSOLE_POSIX
|
||||||
threadMutexInit(&CONSOLE.printMutex);
|
threadMutexInit(&CONSOLE.printMutex);
|
||||||
@@ -69,7 +70,7 @@ errorret_t consoleDraw(void) {
|
|||||||
errorChain(textDraw(
|
errorChain(textDraw(
|
||||||
0, FONT_DEFAULT.tileset->tileHeight * i,
|
0, FONT_DEFAULT.tileset->tileHeight * i,
|
||||||
CONSOLE.line[i],
|
CONSOLE.line[i],
|
||||||
COLOR_WHITE,
|
COLOR_RED,
|
||||||
&FONT_DEFAULT
|
&FONT_DEFAULT
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
@@ -38,6 +38,10 @@ errorret_t displayInit(void) {
|
|||||||
&TEXTURE_WHITE, 4, 4,
|
&TEXTURE_WHITE, 4, 4,
|
||||||
TEXTURE_FORMAT_RGBA, (texturedata_t){ .rgbaColors = TEXTURE_WHITE_PIXELS }
|
TEXTURE_FORMAT_RGBA, (texturedata_t){ .rgbaColors = TEXTURE_WHITE_PIXELS }
|
||||||
));
|
));
|
||||||
|
errorChain(textureInit(
|
||||||
|
&TEXTURE_TEST, 4, 4,
|
||||||
|
TEXTURE_FORMAT_RGBA, (texturedata_t){ .rgbaColors = TEXTURE_TEST_PIXELS }
|
||||||
|
));
|
||||||
|
|
||||||
// Standard meshes
|
// Standard meshes
|
||||||
errorChain(quadInit());
|
errorChain(quadInit());
|
||||||
@@ -100,6 +104,8 @@ errorret_t displayDispose(void) {
|
|||||||
errorChain(spriteBatchDispose());
|
errorChain(spriteBatchDispose());
|
||||||
screenDispose();
|
screenDispose();
|
||||||
errorChain(textDispose());
|
errorChain(textDispose());
|
||||||
|
errorChain(textureDispose(&TEXTURE_WHITE));
|
||||||
|
errorChain(textureDispose(&TEXTURE_TEST));
|
||||||
|
|
||||||
#ifdef displayPlatformDispose
|
#ifdef displayPlatformDispose
|
||||||
displayPlatformDispose();
|
displayPlatformDispose();
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// Copyright (c) 2025 Dominic Masters
|
// Copyright (c) 2026 Dominic Masters
|
||||||
//
|
//
|
||||||
// This software is released under the MIT License.
|
// This software is released under the MIT License.
|
||||||
// https://opensource.org/licenses/MIT
|
// https://opensource.org/licenses/MIT
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ void planeBuffer(
|
|||||||
const float_t u0 = uvMin[0], u1 = uvMax[0];
|
const float_t u0 = uvMin[0], u1 = uvMax[0];
|
||||||
const float_t v0 = uvMin[1], v1 = uvMax[1];
|
const float_t v0 = uvMin[1], v1 = uvMax[1];
|
||||||
|
|
||||||
switch (axis) {
|
switch(axis) {
|
||||||
case PLANE_AXIS_XY: {
|
case PLANE_AXIS_XY: {
|
||||||
/* Flat in XY at z = min[2]; spans X and Y. */
|
/* Flat in XY at z = min[2]; spans X and Y. */
|
||||||
const float_t z = min[2];
|
const float_t z = min[2];
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
@@ -50,70 +50,95 @@ errorret_t spriteBatchBuffer(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Buffer the vertices.
|
// Buffer the vertices.
|
||||||
for(uint32_t i = 0; i < count; i++ ){
|
uint32_t remaining = count;
|
||||||
spritebatchsprite_t sprite = sprites[i];
|
do {
|
||||||
|
uint32_t spritesBeforeFlush = (
|
||||||
|
SPRITEBATCH_SPRITES_MAX_PER_FLUSH - SPRITEBATCH.spriteCount
|
||||||
|
);
|
||||||
|
|
||||||
|
if(spritesBeforeFlush == 0) {
|
||||||
|
// Flush if we have no capacity before flushing.
|
||||||
|
errorChain(spriteBatchFlush());
|
||||||
|
spritesBeforeFlush = SPRITEBATCH_SPRITES_MAX_PER_FLUSH;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Many we buffering?
|
||||||
|
const uint32_t batchCount = mathMin(
|
||||||
|
remaining,
|
||||||
|
spritesBeforeFlush
|
||||||
|
);
|
||||||
|
|
||||||
|
// Destination
|
||||||
meshvertex_t *v = &SPRITEBATCH_VERTICES[
|
meshvertex_t *v = &SPRITEBATCH_VERTICES[
|
||||||
(SPRITEBATCH.spriteCount + (SPRITEBATCH.spriteFlush *
|
(SPRITEBATCH.spriteCount + (SPRITEBATCH.spriteFlush *
|
||||||
SPRITEBATCH_SPRITES_MAX_PER_FLUSH)) * QUAD_VERTEX_COUNT
|
SPRITEBATCH_SPRITES_MAX_PER_FLUSH)) * QUAD_VERTEX_COUNT
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Buffer to the mesh vertices.
|
||||||
|
spriteBatchBufferToMesh(
|
||||||
|
sprites, batchCount, v, batchCount * QUAD_VERTEX_COUNT
|
||||||
|
);
|
||||||
|
SPRITEBATCH.spriteCount += batchCount;
|
||||||
|
remaining -= batchCount;
|
||||||
|
} while(remaining > 0);
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
void spriteBatchBufferToMesh(
|
||||||
|
const spritebatchsprite_t *sprites,
|
||||||
|
const uint32_t count,
|
||||||
|
meshvertex_t *vertices,
|
||||||
|
const uint32_t verticesSize
|
||||||
|
) {
|
||||||
|
assertNotNull(sprites, "Sprites cannot be null");
|
||||||
|
assertTrue(count > 0, "Count must be greater than zero");
|
||||||
|
assertNotNull(vertices, "Vertices cannot be null");
|
||||||
|
assertTrue(
|
||||||
|
verticesSize >= count * QUAD_VERTEX_COUNT, "Vertices array too small"
|
||||||
|
);
|
||||||
|
|
||||||
|
for(uint32_t i = 0; i < count; i++ ){
|
||||||
|
spritebatchsprite_t sprite = sprites[i];
|
||||||
|
meshvertex_t *v = &vertices[i * QUAD_VERTEX_COUNT];
|
||||||
|
|
||||||
// Buffer the quad
|
// Buffer the quad
|
||||||
v[0].pos[0] = sprite.min[0];
|
v[0].pos[0] = sprite.min[0];
|
||||||
v[0].pos[1] = sprite.min[1];
|
v[0].pos[1] = sprite.min[1];
|
||||||
v[0].pos[2] = sprite.min[2];
|
v[0].pos[2] = sprite.min[2];
|
||||||
|
|
||||||
v[0].uv[0] = sprite.uvMin[0];
|
v[0].uv[0] = sprite.uvMin[0];
|
||||||
v[0].uv[1] = sprite.uvMin[1];
|
v[0].uv[1] = sprite.uvMin[1];
|
||||||
|
|
||||||
|
|
||||||
v[1].pos[0] = sprite.max[0];
|
v[1].pos[0] = sprite.max[0];
|
||||||
v[1].pos[1] = sprite.min[1];
|
v[1].pos[1] = sprite.min[1];
|
||||||
v[1].pos[2] = sprite.min[2];
|
v[1].pos[2] = sprite.min[2];
|
||||||
|
|
||||||
v[1].uv[0] = sprite.uvMax[0];
|
v[1].uv[0] = sprite.uvMax[0];
|
||||||
v[1].uv[1] = sprite.uvMin[1];
|
v[1].uv[1] = sprite.uvMin[1];
|
||||||
|
|
||||||
|
|
||||||
v[2].pos[0] = sprite.max[0];
|
v[2].pos[0] = sprite.max[0];
|
||||||
v[2].pos[1] = sprite.max[1];
|
v[2].pos[1] = sprite.max[1];
|
||||||
v[2].pos[2] = sprite.max[2];
|
v[2].pos[2] = sprite.max[2];
|
||||||
|
|
||||||
v[2].uv[0] = sprite.uvMax[0];
|
v[2].uv[0] = sprite.uvMax[0];
|
||||||
v[2].uv[1] = sprite.uvMax[1];
|
v[2].uv[1] = sprite.uvMax[1];
|
||||||
|
|
||||||
|
|
||||||
v[3].pos[0] = sprite.min[0];
|
v[3].pos[0] = sprite.min[0];
|
||||||
v[3].pos[1] = sprite.min[1];
|
v[3].pos[1] = sprite.min[1];
|
||||||
v[3].pos[2] = sprite.min[2];
|
v[3].pos[2] = sprite.min[2];
|
||||||
|
|
||||||
v[3].uv[0] = sprite.uvMin[0];
|
v[3].uv[0] = sprite.uvMin[0];
|
||||||
v[3].uv[1] = sprite.uvMin[1];
|
v[3].uv[1] = sprite.uvMin[1];
|
||||||
|
|
||||||
|
|
||||||
v[4].pos[0] = sprite.max[0];
|
v[4].pos[0] = sprite.max[0];
|
||||||
v[4].pos[1] = sprite.max[1];
|
v[4].pos[1] = sprite.max[1];
|
||||||
v[4].pos[2] = sprite.max[2];
|
v[4].pos[2] = sprite.max[2];
|
||||||
|
|
||||||
v[4].uv[0] = sprite.uvMax[0];
|
v[4].uv[0] = sprite.uvMax[0];
|
||||||
v[4].uv[1] = sprite.uvMax[1];
|
v[4].uv[1] = sprite.uvMax[1];
|
||||||
|
|
||||||
|
|
||||||
v[5].pos[0] = sprite.min[0];
|
v[5].pos[0] = sprite.min[0];
|
||||||
v[5].pos[1] = sprite.max[1];
|
v[5].pos[1] = sprite.max[1];
|
||||||
v[5].pos[2] = sprite.max[2];
|
v[5].pos[2] = sprite.max[2];
|
||||||
|
|
||||||
v[5].uv[0] = sprite.uvMin[0];
|
v[5].uv[0] = sprite.uvMin[0];
|
||||||
v[5].uv[1] = sprite.uvMax[1];
|
v[5].uv[1] = sprite.uvMax[1];
|
||||||
|
|
||||||
// Do we need to flush?
|
|
||||||
SPRITEBATCH.spriteCount++;
|
|
||||||
if(SPRITEBATCH.spriteCount >= SPRITEBATCH_SPRITES_MAX_PER_FLUSH) {
|
|
||||||
errorChain(spriteBatchFlush());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void spriteBatchClear() {
|
void spriteBatchClear() {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
@@ -47,7 +47,7 @@ errorret_t spriteBatchInit();
|
|||||||
/**
|
/**
|
||||||
* Lowest-level buffer function. Writes sprites into the internal vertex buffer.
|
* Lowest-level buffer function. Writes sprites into the internal vertex buffer.
|
||||||
* Flushes automatically when the per-flush capacity is reached. Does not
|
* Flushes automatically when the per-flush capacity is reached. Does not
|
||||||
* modify material state — call spriteBatchSetState or use a high-level push
|
* modify material state - call spriteBatchSetState or use a high-level push
|
||||||
* function before buffering.
|
* function before buffering.
|
||||||
*
|
*
|
||||||
* @param sprites Pointer to the sprite array.
|
* @param sprites Pointer to the sprite array.
|
||||||
@@ -63,6 +63,26 @@ errorret_t spriteBatchBuffer(
|
|||||||
const shadermaterial_t material
|
const shadermaterial_t material
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Buffers an array of sprites to a given array of mesh vertices. This is the
|
||||||
|
* internal method that is used to buffer to the internal spritebatch mesh, but
|
||||||
|
* you can use it to achieve sprite buffering to a mesh you own.
|
||||||
|
*
|
||||||
|
* verticesSize is the size of the vertices array, we use this to ensure no
|
||||||
|
* buffer overflows.
|
||||||
|
*
|
||||||
|
* @param sprites Pointer to the sprite array.
|
||||||
|
* @param count Number of sprites to buffer.
|
||||||
|
* @param vertices Pointer to the vertex array to write to.
|
||||||
|
* @param verticesSize Size of the vertex array, in number of vertices.
|
||||||
|
*/
|
||||||
|
void spriteBatchBufferToMesh(
|
||||||
|
const spritebatchsprite_t *sprites,
|
||||||
|
const uint32_t count,
|
||||||
|
meshvertex_t *vertices,
|
||||||
|
const uint32_t verticesSize
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resets sprite and flush counters and clears the current material state.
|
* Resets sprite and flush counters and clears the current material state.
|
||||||
* Calling spriteBatchFlush after this renders nothing.
|
* Calling spriteBatchFlush after this renders nothing.
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
#include "display/spritebatch/spritebatch.h"
|
#include "display/spritebatch/spritebatch.h"
|
||||||
#include "asset/asset.h"
|
#include "asset/asset.h"
|
||||||
#include "asset/assetbatch.h"
|
|
||||||
#include "asset/loader/display/assettextureloader.h"
|
#include "asset/loader/display/assettextureloader.h"
|
||||||
#include "asset/loader/display/assettilesetloader.h"
|
#include "asset/loader/display/assettilesetloader.h"
|
||||||
#include "display/shader/shaderunlit.h"
|
#include "display/shader/shaderunlit.h"
|
||||||
@@ -18,19 +17,26 @@
|
|||||||
font_t FONT_DEFAULT;
|
font_t FONT_DEFAULT;
|
||||||
|
|
||||||
errorret_t textInit(void) {
|
errorret_t textInit(void) {
|
||||||
assetbatch_t batch;
|
assetloaderinput_t input = { .texture = TEXTURE_FORMAT_RGBA };
|
||||||
assetBatchInit(&batch, NULL, NULL, NULL);
|
assetentry_t *entryTexture = assetLock(
|
||||||
assetBatchTexture(&batch, "ui/minogram.png", TEXTURE_FORMAT_RGBA, &FONT_DEFAULT.texture);
|
"ui/minogram.png", ASSET_LOADER_TYPE_TEXTURE, &input
|
||||||
assetBatchTileset(&batch, "ui/minogram.dtf", &FONT_DEFAULT.tileset);
|
);
|
||||||
errorChain(assetBatchLoad(&batch));
|
assetentry_t *entryTileset = assetLock(
|
||||||
|
"ui/minogram.dtf", ASSET_LOADER_TYPE_TILESET, NULL
|
||||||
|
);
|
||||||
|
errorChain(assetRequireLoaded(entryTexture));
|
||||||
|
errorChain(assetRequireLoaded(entryTileset));
|
||||||
|
|
||||||
|
FONT_DEFAULT.texture = &entryTexture->data.texture;
|
||||||
|
FONT_DEFAULT.tileset = &entryTileset->data.tileset;
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t textDispose(void) {
|
errorret_t textDispose(void) {
|
||||||
assetCacheRelease(&ASSET.cache, "ui/minogram.png");
|
|
||||||
assetCacheRelease(&ASSET.cache, "ui/minogram.dtf");
|
|
||||||
FONT_DEFAULT.texture = NULL;
|
FONT_DEFAULT.texture = NULL;
|
||||||
FONT_DEFAULT.tileset = NULL;
|
FONT_DEFAULT.tileset = NULL;
|
||||||
|
assetUnlock("ui/minogram.png");
|
||||||
|
assetUnlock("ui/minogram.dtf");
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,13 +93,6 @@ errorret_t textDraw(
|
|||||||
float_t posX = x;
|
float_t posX = x;
|
||||||
float_t posY = y;
|
float_t posY = y;
|
||||||
|
|
||||||
errorChain(shaderSetTexture(&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, font->texture));
|
|
||||||
|
|
||||||
#if MESH_ENABLE_COLOR
|
|
||||||
#else
|
|
||||||
errorChain(shaderSetColor(&SHADER_UNLIT, SHADER_UNLIT_COLOR, color));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
char_t c;
|
char_t c;
|
||||||
int32_t i = 0;
|
int32_t i = 0;
|
||||||
while((c = text[i++]) != '\0') {
|
while((c = text[i++]) != '\0') {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
@@ -19,6 +19,14 @@ color_t TEXTURE_WHITE_PIXELS[4*4] = {
|
|||||||
COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
|
COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
texture_t TEXTURE_TEST;
|
||||||
|
color_t TEXTURE_TEST_PIXELS[4*4] = {
|
||||||
|
COLOR_BLACK, COLOR_MAGENTA, COLOR_BLACK, COLOR_MAGENTA,
|
||||||
|
COLOR_MAGENTA, COLOR_BLACK, COLOR_MAGENTA, COLOR_BLACK,
|
||||||
|
COLOR_BLACK, COLOR_MAGENTA, COLOR_BLACK, COLOR_MAGENTA,
|
||||||
|
COLOR_MAGENTA, COLOR_BLACK, COLOR_MAGENTA, COLOR_BLACK,
|
||||||
|
};
|
||||||
|
|
||||||
errorret_t textureInit(
|
errorret_t textureInit(
|
||||||
texture_t *texture,
|
texture_t *texture,
|
||||||
const int32_t width,
|
const int32_t width,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
@@ -30,6 +30,8 @@ typedef union texturedata_u {
|
|||||||
|
|
||||||
extern texture_t TEXTURE_WHITE;
|
extern texture_t TEXTURE_WHITE;
|
||||||
extern color_t TEXTURE_WHITE_PIXELS[4*4];
|
extern color_t TEXTURE_WHITE_PIXELS[4*4];
|
||||||
|
extern texture_t TEXTURE_TEST;
|
||||||
|
extern color_t TEXTURE_TEST_PIXELS[4*4];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a texture.
|
* Initializes a texture.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
|
|||||||
+12
-16
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "time/time.h"
|
#include "time/time.h"
|
||||||
#include "input/input.h"
|
#include "input/input.h"
|
||||||
#include "locale/localemanager.h"
|
#include "locale/localemanager.h"
|
||||||
|
#include "rpg/rpg.h"
|
||||||
#include "display/display.h"
|
#include "display/display.h"
|
||||||
#include "scene/scene.h"
|
#include "scene/scene.h"
|
||||||
#include "cutscene/cutscene.h"
|
#include "cutscene/cutscene.h"
|
||||||
@@ -17,18 +18,15 @@
|
|||||||
#include "ui/ui.h"
|
#include "ui/ui.h"
|
||||||
#include "ui/uitextbox.h"
|
#include "ui/uitextbox.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "entity/entitymanager.h"
|
|
||||||
#include "entity/component/physics/entityphysics.h"
|
|
||||||
#include "physics/physicsmanager.h"
|
|
||||||
#include "network/network.h"
|
#include "network/network.h"
|
||||||
#include "system/system.h"
|
#include "system/system.h"
|
||||||
#include "console/console.h"
|
#include "console/console.h"
|
||||||
#include "item/backpack.h"
|
|
||||||
#include "save/save.h"
|
#include "save/save.h"
|
||||||
|
|
||||||
engine_t ENGINE;
|
engine_t ENGINE;
|
||||||
|
|
||||||
errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
||||||
|
assertInit();
|
||||||
memoryZero(&ENGINE, sizeof(engine_t));
|
memoryZero(&ENGINE, sizeof(engine_t));
|
||||||
ENGINE.running = true;
|
ENGINE.running = true;
|
||||||
ENGINE.argc = argc;
|
ENGINE.argc = argc;
|
||||||
@@ -46,18 +44,15 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
|||||||
errorChain(displayInit());
|
errorChain(displayInit());
|
||||||
errorChain(uiInit());
|
errorChain(uiInit());
|
||||||
errorChain(uiTextboxInit());
|
errorChain(uiTextboxInit());
|
||||||
|
|
||||||
errorChain(cutsceneInit());
|
errorChain(cutsceneInit());
|
||||||
errorChain(sceneInit());
|
errorChain(rpgInit());
|
||||||
entityManagerInit();
|
|
||||||
backpackInit();
|
|
||||||
physicsManagerInit();
|
|
||||||
errorChain(networkInit());
|
errorChain(networkInit());
|
||||||
|
errorChain(sceneInit());
|
||||||
|
|
||||||
/* Run the init script. */
|
|
||||||
consolePrint("Engine initialized");
|
consolePrint("Engine initialized");
|
||||||
sceneSet(SCENE_TYPE_OVERWORLD);
|
sceneSet(SCENE_TYPE_OVERWORLD);
|
||||||
|
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,14 +62,15 @@ errorret_t engineUpdate(void) {
|
|||||||
timeUpdate();
|
timeUpdate();
|
||||||
inputUpdate();
|
inputUpdate();
|
||||||
consoleUpdate();
|
consoleUpdate();
|
||||||
entityManagerUpdate();
|
errorChain(rpgUpdate());
|
||||||
uiUpdate();
|
uiUpdate();
|
||||||
errorChain(uiTextboxUpdate());
|
errorChain(uiTextboxUpdate());
|
||||||
physicsManagerUpdate();
|
|
||||||
errorChain(displayUpdate());
|
|
||||||
errorChain(cutsceneUpdate());
|
errorChain(cutsceneUpdate());
|
||||||
errorChain(sceneUpdate());
|
errorChain(sceneUpdate());
|
||||||
|
errorChain(assetUpdate());
|
||||||
|
|
||||||
|
// Render
|
||||||
|
errorChain(displayUpdate());
|
||||||
if(inputPressed(INPUT_ACTION_RAGEQUIT)) ENGINE.running = false;
|
if(inputPressed(INPUT_ACTION_RAGEQUIT)) ENGINE.running = false;
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
@@ -86,9 +82,9 @@ void engineExit(void) {
|
|||||||
errorret_t engineDispose(void) {
|
errorret_t engineDispose(void) {
|
||||||
uiTextboxDispose();
|
uiTextboxDispose();
|
||||||
cutsceneDispose();
|
cutsceneDispose();
|
||||||
sceneDispose();
|
errorChain(sceneDispose());
|
||||||
errorChain(networkDispose());
|
errorChain(networkDispose());
|
||||||
entityManagerDispose();
|
errorChain(rpgDispose());
|
||||||
localeManagerDispose();
|
localeManagerDispose();
|
||||||
uiDispose();
|
uiDispose();
|
||||||
consoleDispose();
|
consoleDispose();
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* This software is released under the MIT License.
|
* This software is released under the MIT License.
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
|
|||||||
@@ -1,147 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2026 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "entitymanager.h"
|
|
||||||
#include "assert/assert.h"
|
|
||||||
#include "util/memory.h"
|
|
||||||
|
|
||||||
componentdefinition_t COMPONENT_DEFINITIONS[] = {
|
|
||||||
[COMPONENT_TYPE_NULL] = { 0 },
|
|
||||||
|
|
||||||
#define X(enm, type, field, iMethod, dMethod, rMethod) \
|
|
||||||
[COMPONENT_TYPE_##enm] = { \
|
|
||||||
.enumName = #enm, \
|
|
||||||
.name = #field, \
|
|
||||||
.init = iMethod, \
|
|
||||||
.dispose = dMethod, \
|
|
||||||
.render = rMethod \
|
|
||||||
},
|
|
||||||
|
|
||||||
#include "componentlist.h"
|
|
||||||
#undef X
|
|
||||||
|
|
||||||
[COMPONENT_TYPE_COUNT] = { 0 }
|
|
||||||
};
|
|
||||||
|
|
||||||
void componentInit(
|
|
||||||
const entityid_t entityId,
|
|
||||||
const componentid_t componentId,
|
|
||||||
const componenttype_t type
|
|
||||||
) {
|
|
||||||
assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB");
|
|
||||||
assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB");
|
|
||||||
assertTrue(type < COMPONENT_TYPE_COUNT, "Component type OOB");
|
|
||||||
assertTrue(type != COMPONENT_TYPE_NULL, "Cannot initialize null component");
|
|
||||||
|
|
||||||
componentindex_t index = componentGetIndex(entityId, componentId);
|
|
||||||
component_t *cmp = &ENTITY_MANAGER.components[index];
|
|
||||||
memoryZero(cmp, sizeof(component_t));
|
|
||||||
|
|
||||||
cmp->type = type;
|
|
||||||
if(COMPONENT_DEFINITIONS[type].init) {
|
|
||||||
COMPONENT_DEFINITIONS[type].init(entityId, componentId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void * componentGetData(
|
|
||||||
const entityid_t entityId,
|
|
||||||
const componentid_t componentId,
|
|
||||||
const componenttype_t type
|
|
||||||
) {
|
|
||||||
assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB");
|
|
||||||
assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB");
|
|
||||||
assertTrue(type < COMPONENT_TYPE_COUNT, "Component type OOB");
|
|
||||||
assertTrue(type != COMPONENT_TYPE_NULL, "Cannot get data of null component");
|
|
||||||
|
|
||||||
componentindex_t index = componentGetIndex(entityId, componentId);
|
|
||||||
component_t *cmp = &ENTITY_MANAGER.components[index];
|
|
||||||
assertTrue(cmp->type == type, "Component type mismatch");
|
|
||||||
|
|
||||||
return &cmp->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
componentindex_t componentGetIndex(
|
|
||||||
const entityid_t entityId,
|
|
||||||
const componentid_t componentId
|
|
||||||
) {
|
|
||||||
assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB");
|
|
||||||
assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB");
|
|
||||||
return (entityId * ENTITY_COMPONENT_COUNT_MAX) + componentId;
|
|
||||||
}
|
|
||||||
|
|
||||||
entityid_t componentGetEntitiesWithComponent(
|
|
||||||
const componenttype_t type,
|
|
||||||
entityid_t outEntities[ENTITY_COUNT_MAX],
|
|
||||||
componentid_t outComponents[ENTITY_COUNT_MAX]
|
|
||||||
) {
|
|
||||||
assertTrue(type < COMPONENT_TYPE_COUNT, "Component type OOB");
|
|
||||||
assertTrue(type != COMPONENT_TYPE_NULL, "Cannot check NULL type");
|
|
||||||
assertNotNull(outEntities, "Output entities array cannot be null");
|
|
||||||
assertNotNull(outComponents, "Output components array cannot be null");
|
|
||||||
|
|
||||||
entityid_t written = 0;
|
|
||||||
for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) {
|
|
||||||
componentid_t used = ENTITY_MANAGER.entitiesWithComponent[
|
|
||||||
type * ENTITY_COUNT_MAX + i
|
|
||||||
];
|
|
||||||
if(used == COMPONENT_ID_INVALID) continue;
|
|
||||||
assertTrue(
|
|
||||||
ENTITY_MANAGER.components[componentGetIndex(i, used)].type == type,
|
|
||||||
"Component type mismatch in entitiesWithComponent lookup"
|
|
||||||
);
|
|
||||||
assertTrue(
|
|
||||||
(ENTITY_MANAGER.entities[i].state & ENTITY_STATE_ACTIVE) != 0,
|
|
||||||
"Inactive entity in entitiesWithComponent lookup"
|
|
||||||
);
|
|
||||||
assertTrue(
|
|
||||||
used < ENTITY_COMPONENT_COUNT_MAX,
|
|
||||||
"Component ID OOB in entitiesWithComponent lookup"
|
|
||||||
);
|
|
||||||
assertTrue(
|
|
||||||
componentGetIndex(i,used) < ENTITY_COUNT_MAX*ENTITY_COMPONENT_COUNT_MAX,
|
|
||||||
"Component index OOB in entitiesWithComponent lookup"
|
|
||||||
);
|
|
||||||
assertTrue(
|
|
||||||
ENTITY_MANAGER.components[componentGetIndex(i,used)].type == type,
|
|
||||||
"Component type mismatch in entitiesWithComponent lookup"
|
|
||||||
);
|
|
||||||
outComponents[written] = used;
|
|
||||||
outEntities[written++] = i;
|
|
||||||
}
|
|
||||||
return written;
|
|
||||||
}
|
|
||||||
|
|
||||||
errorret_t componentRenderAll(void) {
|
|
||||||
for(entityid_t eid = 0; eid < ENTITY_COUNT_MAX; eid++) {
|
|
||||||
if(!(ENTITY_MANAGER.entities[eid].state & ENTITY_STATE_ACTIVE)) continue;
|
|
||||||
for(componentid_t cid = 0; cid < ENTITY_COMPONENT_COUNT_MAX; cid++) {
|
|
||||||
component_t *cmp = &ENTITY_MANAGER.components[componentGetIndex(eid, cid)];
|
|
||||||
if(cmp->type == COMPONENT_TYPE_NULL) continue;
|
|
||||||
if(!COMPONENT_DEFINITIONS[cmp->type].render) continue;
|
|
||||||
errorChain(COMPONENT_DEFINITIONS[cmp->type].render(eid, cid));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
errorOk();
|
|
||||||
}
|
|
||||||
|
|
||||||
void componentDispose(
|
|
||||||
const entityid_t entityId,
|
|
||||||
const componentid_t componentId
|
|
||||||
) {
|
|
||||||
assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB");
|
|
||||||
assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB");
|
|
||||||
|
|
||||||
componentindex_t index = componentGetIndex(entityId, componentId);
|
|
||||||
component_t *cmp = &ENTITY_MANAGER.components[index];
|
|
||||||
if(cmp->type == COMPONENT_TYPE_NULL) return;
|
|
||||||
|
|
||||||
if(COMPONENT_DEFINITIONS[cmp->type].dispose) {
|
|
||||||
COMPONENT_DEFINITIONS[cmp->type].dispose(entityId, componentId);
|
|
||||||
}
|
|
||||||
|
|
||||||
cmp->type = COMPONENT_TYPE_NULL;
|
|
||||||
}
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2026 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "entitybase.h"
|
|
||||||
|
|
||||||
#define X(enumName, type, field, init, dispose, render) \
|
|
||||||
// do nothing
|
|
||||||
#include "componentlist.h"
|
|
||||||
#undef X
|
|
||||||
|
|
||||||
typedef union {
|
|
||||||
#define X(enumName, type, field, init, dispose, render) type field;
|
|
||||||
#include "componentlist.h"
|
|
||||||
#undef X
|
|
||||||
} componentdata_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
const char_t *enumName;
|
|
||||||
const char_t *name;
|
|
||||||
void (*init)(const entityid_t, const componentid_t);
|
|
||||||
void (*dispose)(const entityid_t, const componentid_t);
|
|
||||||
errorret_t (*render)(const entityid_t, const componentid_t);
|
|
||||||
} componentdefinition_t;
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
COMPONENT_TYPE_NULL,
|
|
||||||
|
|
||||||
#define X(enumName, type, field, init, dispose, render) \
|
|
||||||
COMPONENT_TYPE_##enumName,
|
|
||||||
#include "componentlist.h"
|
|
||||||
#undef X
|
|
||||||
|
|
||||||
COMPONENT_TYPE_COUNT
|
|
||||||
} componenttype_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
componenttype_t type;
|
|
||||||
componentdata_t data;
|
|
||||||
} component_t;
|
|
||||||
|
|
||||||
extern componentdefinition_t COMPONENT_DEFINITIONS[];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a component of the given type for the entity with component ID.
|
|
||||||
*
|
|
||||||
* @param entityId The entity ID.
|
|
||||||
* @param componentId The component ID.
|
|
||||||
* @param type The type of the component to initialize.
|
|
||||||
*/
|
|
||||||
void componentInit(
|
|
||||||
const entityid_t entityId,
|
|
||||||
const componentid_t componentId,
|
|
||||||
const componenttype_t type
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the pointer to the data of a component for the entity with component ID.
|
|
||||||
*
|
|
||||||
* @param entityId The entity ID.
|
|
||||||
* @param componentId The component ID.
|
|
||||||
* @param type The type of the component to get, only used for assertion.
|
|
||||||
* @return A pointer to the component data.
|
|
||||||
*/
|
|
||||||
void * componentGetData(
|
|
||||||
const entityid_t entityId,
|
|
||||||
const componentid_t componentId,
|
|
||||||
const componenttype_t type
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the index of a component for the entity with component ID.
|
|
||||||
*
|
|
||||||
* @param entityId The entity ID.
|
|
||||||
* @param componentId The component ID.
|
|
||||||
* @return The index of the component in the component array.
|
|
||||||
*/
|
|
||||||
componentindex_t componentGetIndex(
|
|
||||||
const entityid_t entityId,
|
|
||||||
const componentid_t componentId
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the entity IDs of all entities with a component of the given type.
|
|
||||||
*
|
|
||||||
* @param type The type of the component to get entities for.
|
|
||||||
* @param outEntities An array to write the entity IDs to, must be at least
|
|
||||||
* ENTITY_COUNT_MAX in size.
|
|
||||||
* @param outComponents An array to write the component IDs to.
|
|
||||||
* @return The number of entity IDs written to outEntities.
|
|
||||||
*/
|
|
||||||
entityid_t componentGetEntitiesWithComponent(
|
|
||||||
const componenttype_t type,
|
|
||||||
entityid_t outEntities[ENTITY_COUNT_MAX],
|
|
||||||
componentid_t outComponents[ENTITY_COUNT_MAX]
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Disposes of a component for the entity with component ID.
|
|
||||||
*
|
|
||||||
* @param entityId The entity ID.
|
|
||||||
* @param componentId The component ID.
|
|
||||||
*/
|
|
||||||
void componentDispose(
|
|
||||||
const entityid_t entityId,
|
|
||||||
const componentid_t componentId
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calls the render callback on every active component that defines one.
|
|
||||||
* Iterates all active entities and all their component slots. No-op for
|
|
||||||
* components whose definition has render == NULL.
|
|
||||||
*
|
|
||||||
* @return Error state.
|
|
||||||
*/
|
|
||||||
errorret_t componentRenderAll(void);
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
# Copyright (c) 2026 Dominic Masters
|
|
||||||
#
|
|
||||||
# This software is released under the MIT License.
|
|
||||||
# https://opensource.org/licenses/MIT
|
|
||||||
|
|
||||||
add_subdirectory(display)
|
|
||||||
add_subdirectory(overworld)
|
|
||||||
add_subdirectory(physics)
|
|
||||||
add_subdirectory(script)
|
|
||||||
add_subdirectory(trigger)
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
# Copyright (c) 2026 Dominic Masters
|
|
||||||
#
|
|
||||||
# This software is released under the MIT License.
|
|
||||||
# https://opensource.org/licenses/MIT
|
|
||||||
|
|
||||||
# Sources
|
|
||||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
|
||||||
PUBLIC
|
|
||||||
entityposition.c
|
|
||||||
entitycamera.c
|
|
||||||
entityrenderable.c
|
|
||||||
)
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user