2 Commits
Author SHA1 Message Date
YourWishesandClaude Sonnet 5 9a24b3d7db Bring the entity-component system online: rendering, scene manager, scripting
entityRenderAll() (src/dusk/entity/entityrender.c) ports ac2's
scene.c:sceneRender() camera/view-matrix/draw-loop logic as a standalone
function - find the active camera, bind SHADER_UNLIT with its projection/
view, draw every COMPONENT_TYPE_RENDERABLE entity. display.c calls it each
frame between the screen clear and screenUnbind(), the same spot the old
sceneRender() used to sit.

A single-scene scene manager (src/dusk/scene/, ported from ac2 minus its
prefab system and physics/trigger stepping - that module hasn't been
ported yet) sits alongside this but isn't wired into the engine loop yet;
only one scene can ever exist (no SCENE_COUNT_MAX array).

entitymanager.c/h removed entirely: its three arrays (entities/components/
entitiesWithComponent) are now plain globals in entity.h/.c
(ENTITIES/COMPONENTS/ENTITIES_WITH_COMPONENT), and every entity/component
function across the whole ECS (entity, component, position, camera,
renderable, physics, trigger, animation) drops its entitymanager_t *mgr
parameter to operate on them directly - there was only ever one instance of
that struct in the whole engine, so the indirection bought nothing.

New duskscript bindings (src/dusk/script/native/entity/) expose entityAdd,
entityAddComponent/entityGetComponent, entitySetName/entityGetName/
entityFindByName, entityDispose/entityDisposeDeep,
entityPositionSetLocalPosition/entityPositionLookAt,
entityRenderableSetMesh (by name - cube/plane/sphere/capsule/triprism/quad),
and COMPONENT_TYPE_*/ENTITY_ID_INVALID/COMPONENT_ID_INVALID constants.
assets/init.ds uses them to spawn a camera (via LookAt from (3,3,3) at the
origin), a cube and a plane as the engine's first real content on the new
ECS.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-10-15 09:08:20 -05:00
YourWishesandClaude Sonnet 5 fdfdfa3a66 Fix the build after archiving rpg/ui/scene: drop their last live dependents
rpg/ui/scene moved to archive/ earlier left a handful of otherwise-generic
systems with dead references into them:
- asset/loader/chunk existed solely to load rpg overworld map chunks
  (tile_t, worldpos_t) - deleted outright, along with its
  ASSET_LOADER_TYPE_CHUNK entry in assetloader.c/h.
- save/slot wrapped party_t/storyflagval_t/map-name/cutscene-path, which is
  100% rpg save data - deleted outright (save.c/h, savedevice.c/h and the
  Linux save backend keep their generic settings/device-discovery logic,
  just lose the slot read/write pieces that only existed for it).
- locale/localemanager.c and test/CMakeLists.txt/test/save/* had a few
  stray includes and test cases that only existed for the now-gone code.

Also restores assets/locale/en_US.po (copied back from archive/assets/) -
locale init still needs a real locale file to boot into, even with the
asset-moving/archiving done separately.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-10-15 09:08:02 -05:00
72 changed files with 1360 additions and 2583 deletions

No files matched your search

+19 -1
View File
@@ -1,2 +1,20 @@
// Entry point, run once by the engine after every other system is ready. // Entry point, run once by the engine after every other system is ready.
consolePrint("Test");
var camera = entityAdd();
var cameraPosition = entityAddComponent(camera, COMPONENT_TYPE_POSITION);
entityAddComponent(camera, COMPONENT_TYPE_CAMERA);
entityPositionLookAt(camera, cameraPosition, 3, 3, 3, 0, 0, 0);
var cube = entityAdd();
var cubePosition = entityAddComponent(cube, COMPONENT_TYPE_POSITION);
var cubeRenderable = entityAddComponent(cube, COMPONENT_TYPE_RENDERABLE);
entityRenderableSetMesh(cube, cubeRenderable, "cube");
entityPositionSetLocalPosition(cube, cubePosition, 0, 0, 0);
var plane = entityAdd();
var planePosition = entityAddComponent(plane, COMPONENT_TYPE_POSITION);
var planeRenderable = entityAddComponent(plane, COMPONENT_TYPE_RENDERABLE);
entityRenderableSetMesh(plane, planeRenderable, "plane");
entityPositionSetLocalPosition(plane, planePosition, 0, 0, 0);
consolePrint("Camera, cube and plane created");
+289
View File
@@ -0,0 +1,289 @@
msgid ""
msgstr ""
"Project-Id-Version: ExampleApp 1.0\n"
"Language: en\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Plural-Forms: nplurals=4; plural=(n==1 ? 0 : n==2 ? 1 : (n<7 ? 2 : 3));\n"
# Initial Scene
msgid "initial.checking_save.title"
msgstr "Checking for save data"
msgid "initial.checking_save.message"
msgstr "Please wait..."
msgid "initial.no_device.title"
msgstr "No Save Device Found"
msgid "initial.no_device.message"
msgstr "Could not find a save device, ensure it is connected and try again. You can continue, but progress will not be saved."
msgid "initial.no_device.retry"
msgstr "Try again"
msgid "initial.no_device.continue"
msgstr "Continue without saving"
# Main Menu Scene
msgid "main_menu.start_game"
msgstr "Start Game"
msgid "main_menu.options"
msgstr "Options"
msgid "main_menu.quit"
msgstr "Quit Game"
msgid "main_menu.quit_confirm"
msgstr "Are you sure you want to quit?"
msgid "main_menu.checking_save.title"
msgstr "Checking for save data"
msgid "main_menu.checking_save.message"
msgstr "Please wait..."
msgid "main_menu.no_device.title"
msgstr "No Save Device Found"
msgid "main_menu.no_device.message"
msgstr "Could not find a save device, ensure it is connected and try again. You can continue, but progress will not be saved."
msgid "main_menu.no_device.retry"
msgstr "Try again"
msgid "main_menu.no_device.continue"
msgstr "Continue without saving"
msgid "main_menu.save_load_error.title"
msgstr "Error"
msgid "main_menu.save_load_error.message"
msgstr "Failed to load save data. Please try again."
msgid "main_menu.save_load_error.retry"
msgstr "Try Again"
# Select Save Screen
msgid "ui.select_save.title"
msgstr "Select Save"
msgid "ui.select_save.empty"
msgstr "Empty Slot"
msgid "ui.select_save.corrupt"
msgstr "Corrupted Save"
msgid "ui.select_save.corrupt_warning"
msgstr "This save file is corrupted and could not be loaded. Please contact support if this issue persists."
msgid "ui.select_save.slot_format"
msgstr "%s Lv.%d %s"
msgid "ui.select_save.delete_mode"
msgstr "Delete a Save"
msgid "ui.select_save.delete_confirm"
msgstr "Are you sure you want to delete this save?"
msgid "ui.select_save.name_title"
msgstr "Enter game save file name"
msgid "ui.save_slot.number_format"
msgstr "Slot %d"
#: ui/menu.c:10
msgid "ui.title"
msgstr ""
"Welcome"
msgid "save.linux.mkdirp_failed"
msgstr "Failed to create save directory, check the disk is not full or write-protected."
#: src/dusk/ui/frame/settings/uisettings.c
msgid "ui.settings.tabs.general"
msgstr "General"
#: src/dusk/ui/frame/settings/uisettings.c
msgid "ui.settings.tabs.input"
msgstr "Input"
#: src/dusk/ui/frame/settings/uisettings.c
msgid "ui.settings.tabs.display"
msgstr "Display"
#: src/dusk/ui/frame/settings/uisettings.c
msgid "ui.settings.tabs.audio"
msgstr "Audio"
msgid "ui.settings.input.deadzone"
msgstr "Deadzone"
#: src/dusk/ui/frame/settings/uisettingsgeneral.c
msgid "ui.settings.general.language"
msgstr "Language"
msgid "ui.settings.general.language_detail"
msgstr "Takes effect after restarting the application."
#: src/dusk/ui/frame/settings/uisettings.c
msgid "ui.settings.apply"
msgstr "Apply"
#: src/dusk/ui/frame/uiconfirm.c
msgid "ui.confirm.discard_changes"
msgstr "Discard unsaved changes?"
#: src/dusk/ui/frame/game/uigamemenu.c
msgid "ui.game_menu.characters"
msgstr "Characters"
#: src/dusk/ui/frame/game/uigamemenu.c
msgid "ui.game_menu.items"
msgstr "Items"
#: src/dusk/ui/frame/game/uigamemenu.c
msgid "ui.game_menu.settings"
msgstr "Settings"
#: src/dusk/ui/frame/game/uigamemenu.c
msgid "ui.game_menu.save"
msgstr "Save"
#: src/dusk/ui/frame/game/uigamemenu.c
msgid "ui.game_menu.save_success"
msgstr "Game saved."
#: src/dusk/ui/frame/game/uigamemenu.c
msgid "ui.game_menu.save_cancelled"
msgstr "Save cancelled."
#: src/dusk/ui/frame/game/uigamemenu.c
msgid "ui.game_menu.save_unavailable"
msgstr "Can't save - no save device found."
#: src/dusk/ui/frame/game/uigamemenu.c
msgid "ui.game_menu.save_temporary"
msgstr "This session is temporary - no save device was found, so saving is disabled."
#: src/dusk/ui/frame/game/uigamemenu.c
msgid "ui.game_menu.save_create_confirm"
msgstr "No save data found. Create a new save?"
#: src/dusk/ui/frame/game/uigamemenu.c
msgid "ui.game_menu.save_failed_format"
msgstr "Save failed: %s"
#: src/dusk/ui/frame/game/uigamemenu.c
msgid "ui.game_menu.save_check_failed_format"
msgstr "Can't save: %s"
#: src/dusk/ui/frame/initial/uiinitialnocard.c
msgid "ui.initial.no_card.message"
msgstr "No save device found. You can continue, but\nprogress will not be saved."
#: src/dusk/ui/frame/initial/uiinitialnocard.c
msgid "ui.initial.no_card.retry"
msgstr "Retry"
#: src/dusk/ui/frame/initial/uiinitialnocard.c
msgid "ui.initial.no_card.continue"
msgstr "Continue Anyway"
#: src/dusk/ui/frame/initial/uiinitialcreatesave.c
msgid "ui.initial.create_save.message"
msgstr "No save data found. Create a new save?"
#: src/dusk/ui/frame/initial/uiinitialcreatesave.c
msgid "ui.initial.create_save.yes"
msgstr "Yes"
#: src/dusk/ui/frame/initial/uiinitialcreatesave.c
msgid "ui.initial.create_save.no"
msgstr "No"
#: src/dusk/ui/frame/uiconfirm.c
msgid "ui.confirm.confirm"
msgstr "Confirm"
#: src/dusk/ui/frame/uiconfirm.c
msgid "ui.confirm.cancel"
msgstr "Cancel"
#: src/dusk/ui/frame/battle/uibattlemenu.c
msgid "ui.battle.menu.attack"
msgstr "Attack"
#: src/dusk/ui/frame/battle/uibattlemenu.c
msgid "ui.battle.menu.flee"
msgstr "Flee"
#: src/dusk/ui/frame/battle/uibattlemenu.c
msgid "ui.battle.menu.target_format"
msgstr "Enemy %u (%u/%u HP)"
#: src/dusk/ui/frame/battle/uibattlehud.c
msgid "ui.battle.hud.hp_format"
msgstr "HP %u/%u"
#: src/dusk/ui/frame/battle/uibattlehud.c
msgid "ui.battle.hud.mp_format"
msgstr "MP %u/%u"
#: src/dusk/ui/frame/settings/uisettingsaudio.c
msgid "ui.settings.audio.placeholder"
msgstr "No audio settings yet"
#: src/dusk/ui/frame/settings/uisettingsdisplay.c
msgid "ui.settings.display.placeholder"
msgstr "No display settings yet"
#: src/dusk/ui/frame/settings/uisettingsinput.c
msgid "ui.settings.input.placeholder"
msgstr "No input settings yet"
#: src/dusk/ui/frame/backpack/uibackpack.c
msgid "ui.backpack.category_format"
msgstr "Category %u"
#: src/dusk/ui/overlay/uiloading.c
msgid "ui.loading.text"
msgstr "loading"
#: src/dusk/ui/overlay/uiautosave.c
msgid "ui.autosave.saving"
msgstr "SAVING"
#
# Items
#
msgid "item.potion.name"
msgstr "Potion"
msgid "item.potato.name"
msgstr "Potato"
msgid "item.apple.name"
msgstr "Apple"
#
# Moves
#
msgid "move.fire.name"
msgstr "Fire"
msgid "move.slash.name"
msgstr "Slash"
+1 -3
View File
@@ -82,12 +82,10 @@ add_subdirectory(error)
add_subdirectory(input) add_subdirectory(input)
add_subdirectory(locale) add_subdirectory(locale)
add_subdirectory(perf) add_subdirectory(perf)
add_subdirectory(rpg)
add_subdirectory(script)
add_subdirectory(scene) add_subdirectory(scene)
add_subdirectory(script)
add_subdirectory(system) add_subdirectory(system)
add_subdirectory(time) add_subdirectory(time)
add_subdirectory(ui)
add_subdirectory(save) add_subdirectory(save)
add_subdirectory(util) add_subdirectory(util)
add_subdirectory(thread) add_subdirectory(thread)
-1
View File
@@ -15,7 +15,6 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
add_subdirectory(display) add_subdirectory(display)
add_subdirectory(locale) add_subdirectory(locale)
add_subdirectory(json) add_subdirectory(json)
add_subdirectory(chunk)
add_subdirectory(dmf) add_subdirectory(dmf)
add_subdirectory(wav) add_subdirectory(wav)
add_subdirectory(mp3) add_subdirectory(mp3)
-6
View File
@@ -46,12 +46,6 @@ assetloadercallbacks_t ASSET_LOADER_CALLBACKS[ASSET_LOADER_TYPE_COUNT] = {
.dispose = assetJsonDispose .dispose = assetJsonDispose
}, },
[ASSET_LOADER_TYPE_CHUNK] = {
.loadSync = assetChunkLoaderSync,
.loadAsync = assetChunkLoaderAsync,
.dispose = assetChunkDispose
},
[ASSET_LOADER_TYPE_WAV] = { [ASSET_LOADER_TYPE_WAV] = {
.loadSync = assetWavLoaderSync, .loadSync = assetWavLoaderSync,
.loadAsync = assetWavLoaderAsync, .loadAsync = assetWavLoaderAsync,
-5
View File
@@ -12,7 +12,6 @@
#include "asset/loader/display/assettilesetloader.h" #include "asset/loader/display/assettilesetloader.h"
#include "asset/loader/locale/assetlocaleloader.h" #include "asset/loader/locale/assetlocaleloader.h"
#include "asset/loader/json/assetjsonloader.h" #include "asset/loader/json/assetjsonloader.h"
#include "asset/loader/chunk/assetchunkloader.h"
#include "asset/loader/wav/assetwavloader.h" #include "asset/loader/wav/assetwavloader.h"
#include "asset/loader/mp3/assetmp3loader.h" #include "asset/loader/mp3/assetmp3loader.h"
@@ -25,7 +24,6 @@ typedef enum {
ASSET_LOADER_TYPE_TILESET, ASSET_LOADER_TYPE_TILESET,
ASSET_LOADER_TYPE_LOCALE, ASSET_LOADER_TYPE_LOCALE,
ASSET_LOADER_TYPE_JSON, ASSET_LOADER_TYPE_JSON,
ASSET_LOADER_TYPE_CHUNK,
ASSET_LOADER_TYPE_WAV, ASSET_LOADER_TYPE_WAV,
ASSET_LOADER_TYPE_MP3, ASSET_LOADER_TYPE_MP3,
@@ -39,7 +37,6 @@ typedef union {
assettilesetloaderloading_t tileset; assettilesetloaderloading_t tileset;
assetlocaleloaderloading_t locale; assetlocaleloaderloading_t locale;
assetjsonloaderloading_t json; assetjsonloaderloading_t json;
assetchunkloaderloading_t chunk;
assetwavloaderloading_t wav; assetwavloaderloading_t wav;
assetmp3loaderloading_t mp3; assetmp3loaderloading_t mp3;
} assetloaderloading_t; } assetloaderloading_t;
@@ -51,7 +48,6 @@ typedef union {
assettilesetoutput_t tileset; assettilesetoutput_t tileset;
assetlocaleoutput_t locale; assetlocaleoutput_t locale;
assetjsonoutput_t json; assetjsonoutput_t json;
assetchunkoutput_t chunk;
assetwavoutput_t wav; assetwavoutput_t wav;
assetmp3output_t mp3; assetmp3output_t mp3;
} assetloaderoutput_t; } assetloaderoutput_t;
@@ -61,7 +57,6 @@ typedef union {
assettilesetloaderinput_t tileset; assettilesetloaderinput_t tileset;
assetlocaleloaderinput_t locale; assetlocaleloaderinput_t locale;
assetjsonloaderinput_t json; assetjsonloaderinput_t json;
assetchunkloaderinput_t chunk;
assetwavloaderinput_t wav; assetwavloaderinput_t wav;
assetmp3loaderinput_t mp3; assetmp3loaderinput_t mp3;
} assetloaderinput_t; } assetloaderinput_t;
@@ -1,484 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "assetchunkloader.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "util/string.h"
#include "asset/asset.h"
#include "asset/loader/assetloading.h"
#include "asset/loader/assetentry.h"
#include "asset/loader/assetloader.h"
#include "asset/loader/json/assetjsonloader.h"
#include "display/color.h"
#include "display/texture/texture.h"
#include "rpg/overworld/worldpos.h"
#include "yyjson.h"
#define ASSET_CHUNK_TERRAIN_TEXTURE "tiles.png"
// Per-shape corner Z offsets (sw, se, ne, nw) as offsets from a tile's own
// base Z, ported 1:1 from tools/asset/chunk/__main__.py's _RAMP_CORNERS.
// TILE_SHAPE_NULL's row is never read - assetChunkTilesHaveTerrain/
// assetChunkBuildTerrain skip those tiles outright.
static const float_t CHUNK_RAMP_CORNERS[TILE_SHAPE_COUNT][4] = {
[TILE_SHAPE_GROUND] = {0.0f, 0.0f, 0.0f, 0.0f},
[TILE_SHAPE_RAMP_NORTH] = {0.0f, 0.0f, 1.0f, 1.0f},
[TILE_SHAPE_RAMP_SOUTH] = {1.0f, 1.0f, 0.0f, 0.0f},
[TILE_SHAPE_RAMP_EAST] = {0.0f, 1.0f, 1.0f, 0.0f},
[TILE_SHAPE_RAMP_WEST] = {1.0f, 0.0f, 0.0f, 1.0f},
[TILE_SHAPE_RAMP_NORTHEAST] = {0.0f, 0.0f, 1.0f, 0.0f},
[TILE_SHAPE_RAMP_NORTHWEST] = {0.0f, 0.0f, 0.0f, 1.0f},
[TILE_SHAPE_RAMP_SOUTHEAST] = {0.0f, 1.0f, 0.0f, 0.0f},
[TILE_SHAPE_RAMP_SOUTHWEST] = {1.0f, 0.0f, 0.0f, 0.0f},
[TILE_SHAPE_RAMP_NORTHEAST_INNER] = {0.0f, 1.0f, 1.0f, 1.0f},
[TILE_SHAPE_RAMP_NORTHWEST_INNER] = {1.0f, 0.0f, 1.0f, 1.0f},
[TILE_SHAPE_RAMP_SOUTHEAST_INNER] = {1.0f, 1.0f, 1.0f, 0.0f},
[TILE_SHAPE_RAMP_SOUTHWEST_INNER] = {1.0f, 1.0f, 0.0f, 1.0f},
};
errorret_t assetChunkLoaderAsync(assetloading_t *loading) {
assertNotNull(loading, "Loading cannot be NULL");
assertNotMainThread("Should be called from an async thread.");
errorOk();
}
static bool_t assetChunkTilesHaveTerrain(const tile_t *tiles) {
for(uint32_t i = 0; i < CHUNK_TILE_COUNT; i++) {
if(tiles[i].shape != TILE_SHAPE_NULL) return true;
}
return false;
}
// Reads the "tiles" array - a dense, fixed-size CHUNK_TILE_COUNT grid,
// one slot per (x, y) column at index x + y * CHUNK_WIDTH (position is
// implicit in the index, not stored). Each slot is either `null` (empty
// column, already zeroed = TILE_SHAPE_NULL) or a 2-element [type, z]
// array.
static void assetChunkParseTiles(yyjson_val *root, tile_t *tiles) {
memoryZero(tiles, CHUNK_TILE_COUNT * sizeof(tile_t));
yyjson_val *tilesArr = yyjson_obj_get(root, "tiles");
if(!yyjson_is_arr(tilesArr)) return;
size_t idx, len;
yyjson_val *tileVal;
yyjson_arr_foreach(tilesArr, idx, len, tileVal) {
if(idx >= CHUNK_TILE_COUNT) break;
if(!yyjson_is_arr(tileVal) || yyjson_arr_size(tileVal) < 2) continue;
int32_t type = (int32_t)yyjson_get_int(yyjson_arr_get(tileVal, 0));
int32_t z = (int32_t)yyjson_get_int(yyjson_arr_get(tileVal, 1));
tiles[idx] = (tile_t){ .shape = (tileshape_t)type, .z = (uint8_t)z };
}
}
// Resolves "meshes" (external building references, by bare filename) into
// out->modelNames/meshOffsets starting at *ioIndex, advancing it as
// entries are written. Filenames are resolved as
// "models/buildings/<basename>.json" - a hardcoded stand-in for
// tools/asset/chunk/__main__.py's find_model() recursive search, valid
// as long as every building model lives flat under that one directory
// (true today - see the raw-json-maps plan notes for the tradeoff).
static void assetChunkParseMeshes(
yyjson_val *root, assetchunkoutput_t *out, uint8_t *ioIndex
) {
yyjson_val *meshesArr = yyjson_obj_get(root, "meshes");
if(!yyjson_is_arr(meshesArr)) return;
size_t idx, len;
yyjson_val *meshVal;
yyjson_arr_foreach(meshesArr, idx, len, meshVal) {
if(*ioIndex >= CHUNK_MESH_COUNT_MAX) break;
yyjson_val *fileVal = yyjson_obj_get(meshVal, "file");
if(!fileVal || !yyjson_is_str(fileVal)) continue;
char_t baseName[CHUNK_MESH_NAME_MAX];
stringCopy(baseName, yyjson_get_str(fileVal), sizeof(baseName));
char_t *dot = stringFindLastChar(baseName, '.');
if(dot != NULL) *dot = '\0';
uint8_t m = *ioIndex;
stringFormat(
out->modelNames[m], CHUNK_MESH_NAME_MAX,
"models/buildings/%s.json", baseName
);
vec3 offset = {0.0f, 0.0f, 0.0f};
yyjson_val *posArr = yyjson_obj_get(meshVal, "pos");
if(yyjson_is_arr(posArr) && yyjson_arr_size(posArr) >= 3) {
offset[0] = (float_t)yyjson_get_num(yyjson_arr_get(posArr, 0));
offset[1] = (float_t)yyjson_get_num(yyjson_arr_get(posArr, 1));
offset[2] = (float_t)yyjson_get_num(yyjson_arr_get(posArr, 2));
}
memoryCopy(out->meshOffsets[m], offset, sizeof(vec3));
(*ioIndex)++;
}
}
static void assetChunkParseEntities(yyjson_val *root, assetchunkoutput_t *out) {
out->entitySpawnCount = 0;
yyjson_val *arr = yyjson_obj_get(root, "entities");
if(!yyjson_is_arr(arr)) return;
size_t idx, len;
yyjson_val *val;
yyjson_arr_foreach(arr, idx, len, val) {
if(out->entitySpawnCount >= CHUNK_ENTITY_SPAWN_COUNT_MAX) break;
const char_t *type = yyjson_get_str(yyjson_obj_get(val, "type"));
yyjson_val *posArr = yyjson_obj_get(val, "pos");
if(type == NULL || !yyjson_is_arr(posArr) || yyjson_arr_size(posArr) < 3) {
continue;
}
worldpos_t pos = {
.x = (worldunit_t)yyjson_get_int(yyjson_arr_get(posArr, 0)),
.y = (worldunit_t)yyjson_get_int(yyjson_arr_get(posArr, 1)),
.z = (worldunit_t)yyjson_get_int(yyjson_arr_get(posArr, 2)),
};
chunkentityspawn_t *spawn = &out->entitySpawns[out->entitySpawnCount];
if(stringEquals(type, "global")) {
spawn->kind = CHUNK_ENTITY_SPAWN_KIND_GLOBAL;
spawn->globalId =
(uint16_t)yyjson_get_int(yyjson_obj_get(val, "globalId"));
spawn->itemId = 0;
spawn->itemQuantity = 0;
} else if(stringEquals(type, "item")) {
spawn->kind = CHUNK_ENTITY_SPAWN_KIND_ITEM;
spawn->globalId = 0;
spawn->itemId = (uint16_t)yyjson_get_int(yyjson_obj_get(val, "itemId"));
spawn->itemQuantity =
(uint8_t)yyjson_get_int(yyjson_obj_get(val, "quantity"));
} else {
continue;
}
spawn->position = pos;
out->entitySpawnCount++;
}
}
static void assetChunkParseAreas(yyjson_val *root, assetchunkoutput_t *out) {
out->areaSpawnCount = 0;
yyjson_val *arr = yyjson_obj_get(root, "areas");
if(!yyjson_is_arr(arr)) return;
size_t idx, len;
yyjson_val *val;
yyjson_arr_foreach(arr, idx, len, val) {
if(out->areaSpawnCount >= CHUNK_AREA_COUNT_MAX) break;
yyjson_val *minArr = yyjson_obj_get(val, "min");
yyjson_val *maxArr = yyjson_obj_get(val, "max");
if(
!yyjson_is_arr(minArr) || yyjson_arr_size(minArr) < 3 ||
!yyjson_is_arr(maxArr) || yyjson_arr_size(maxArr) < 3
) continue;
chunkareaspawn_t *area = &out->areaSpawns[out->areaSpawnCount];
area->min = (worldpos_t){
.x = (worldunit_t)yyjson_get_int(yyjson_arr_get(minArr, 0)),
.y = (worldunit_t)yyjson_get_int(yyjson_arr_get(minArr, 1)),
.z = (worldunit_t)yyjson_get_int(yyjson_arr_get(minArr, 2)),
};
area->max = (worldpos_t){
.x = (worldunit_t)yyjson_get_int(yyjson_arr_get(maxArr, 0)),
.y = (worldunit_t)yyjson_get_int(yyjson_arr_get(maxArr, 1)),
.z = (worldunit_t)yyjson_get_int(yyjson_arr_get(maxArr, 2)),
};
area->callbackId =
(uint16_t)yyjson_get_int(yyjson_obj_get(val, "callbackId"));
area->notify = (uint8_t)yyjson_get_int(yyjson_obj_get(val, "notify"));
area->trigger = (uint8_t)yyjson_get_int(yyjson_obj_get(val, "trigger"));
out->areaSpawnCount++;
}
}
// Six vertices (2 CCW triangles) for one tile's quad, matching
// tools/asset/chunk/__main__.py's _tile_quad. Corner order: SW=(x,y),
// SE=(x+1,y), NE=(x+1,y+1), NW=(x,y+1). NORTH=+Y, EAST=+X.
static void assetChunkAppendTileQuad(
meshvertex_t *verts, uint32_t *vertCount,
const uint32_t x, const uint32_t y,
const float_t sw, const float_t se, const float_t ne, const float_t nw
) {
const float_t u0 = (float_t)x / (float_t)CHUNK_WIDTH;
const float_t u1 = (float_t)(x + 1) / (float_t)CHUNK_WIDTH;
const float_t v0 = (float_t)y / (float_t)CHUNK_HEIGHT;
const float_t v1 = (float_t)(y + 1) / (float_t)CHUNK_HEIGHT;
const float_t fx = (float_t)x;
const float_t fy = (float_t)y;
meshvertex_t *v = verts + *vertCount;
v[0] = (meshvertex_t){ .uv = {u0, v0}, .pos = {fx, fy, sw} };
v[1] = (meshvertex_t){ .uv = {u1, v0}, .pos = {fx + 1, fy, se} };
v[2] = (meshvertex_t){ .uv = {u1, v1}, .pos = {fx + 1, fy + 1, ne} };
v[3] = (meshvertex_t){ .uv = {u0, v0}, .pos = {fx, fy, sw} };
v[4] = (meshvertex_t){ .uv = {u1, v1}, .pos = {fx + 1, fy + 1, ne} };
v[5] = (meshvertex_t){ .uv = {u0, v1}, .pos = {fx, fy + 1, nw} };
*vertCount += 6;
}
// Builds a mesh_t straight from the tile grid - one quad per non-null
// tile column, shaped by CHUNK_RAMP_CORNERS - the runtime equivalent of
// tools/asset/chunk/__main__.py's build_terrain_verts. Only called once
// assetChunkTilesHaveTerrain has confirmed at least one real tile exists.
// Mirrors assetMeshLoaderSync's meshInit/meshFlush/free-after-upload
// sequence (assetmeshloader.c) since this produces the same
// assetmeshoutput_t shape, just from generated verts instead of a file.
static errorret_t assetChunkBuildTerrain(
const tile_t *tiles, assetmeshoutput_t *out
) {
meshvertex_t *verts = memoryAllocate(
CHUNK_TILE_COUNT * 6 * sizeof(meshvertex_t)
);
uint32_t vertCount = 0;
for(uint32_t y = 0; y < CHUNK_HEIGHT; y++) {
for(uint32_t x = 0; x < CHUNK_WIDTH; x++) {
const tile_t *tile = &tiles[x + y * CHUNK_WIDTH];
if(tile->shape == TILE_SHAPE_NULL) continue;
const float_t *corners = CHUNK_RAMP_CORNERS[tile->shape];
const float_t fz = (float_t)tile->z;
assetChunkAppendTileQuad(
verts, &vertCount, x, y,
(fz + corners[0]) * WORLD_LAYER_HEIGHT,
(fz + corners[1]) * WORLD_LAYER_HEIGHT,
(fz + corners[2]) * WORLD_LAYER_HEIGHT,
(fz + corners[3]) * WORLD_LAYER_HEIGHT
);
}
}
assertTrue(vertCount > 0, "Terrain build called with no terrain tiles.");
out->vertices = verts;
errorret_t ret = meshInit(
&out->mesh, MESH_PRIMITIVE_TYPE_TRIANGLES, (int32_t)vertCount, verts
);
if(errorIsNotOk(ret)) {
memoryFree(out->vertices);
out->vertices = NULL;
errorChain(ret);
}
out->meshInitialized = true;
ret = meshFlush(&out->mesh, 0, (int32_t)vertCount);
if(errorIsNotOk(ret)) {
meshDispose(&out->mesh);
out->meshInitialized = false;
memoryFree(out->vertices);
out->vertices = NULL;
errorChain(ret);
}
#if defined(DUSK_OPENGL) && !defined(DUSK_OPENGL_LEGACY)
memoryFree(out->vertices);
out->vertices = NULL;
#endif
errorOk();
}
errorret_t assetChunkLoaderSync(assetloading_t *loading) {
assertNotNull(loading, "Loading cannot be NULL");
assertTrue(loading->type == ASSET_LOADER_TYPE_CHUNK, "Invalid type.");
assertIsMainThread("Must be called from the main thread.");
assetchunkoutput_t *out = &loading->entry->data.chunk;
switch(loading->loading.chunk.state) {
case ASSET_CHUNK_LOADING_STATE_INITIAL:
loading->loading.chunk.state = ASSET_CHUNK_LOADING_STATE_PARSE;
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
errorOk();
case ASSET_CHUNK_LOADING_STATE_PARSE:
break;
case ASSET_CHUNK_LOADING_STATE_LOAD_MODELS:
while(loading->loading.chunk.modelIndex < out->meshCount) {
uint8_t m = loading->loading.chunk.modelIndex;
if(out->modelEntries[m] == NULL) {
out->modelEntries[m] = assetLock(
out->modelNames[m], ASSET_LOADER_TYPE_MODEL, NULL
);
assertNotNull(out->modelEntries[m], "Failed to lock model.");
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
errorOk();
}
if(out->modelEntries[m]->state == ASSET_ENTRY_STATE_ERROR) {
assetLoaderErrorThrow(
loading, "Model failed to load: %s", out->modelNames[m]
);
}
if(out->modelEntries[m]->state != ASSET_ENTRY_STATE_LOADED) {
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
errorOk();
}
loading->loading.chunk.modelIndex++;
}
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
errorOk();
default:
errorOk();
}
// --- PARSE: lock+require the chunk's own JSON as a sub-asset, exactly
// like assetmodelloader.c's ASSET_MODEL_LOADING_STATE_LOCK_ASSETS does
// for its model descriptor, then read everything out of it before
// unlocking (the yyjson_doc/vals only stay valid while jsonEntry does).
char_t jsonKey[ASSET_FILE_NAME_MAX];
stringFormat(jsonKey, sizeof(jsonKey), "json:%s", loading->entry->name);
assetloaderinput_t jsonInput;
memoryZero(&jsonInput, sizeof(jsonInput));
stringCopy(
jsonInput.json.path, loading->entry->name, ASSET_FILE_NAME_MAX
);
assetentry_t *jsonEntry =
assetLock(jsonKey, ASSET_LOADER_TYPE_JSON, &jsonInput);
errorret_t jsonRet = assetRequireLoaded(jsonEntry);
if(errorIsNotOk(jsonRet)) {
assetUnlockEntry(jsonEntry);
assetLoaderErrorThrow(
loading, "Failed to load chunk JSON: %s", loading->entry->name
);
}
yyjson_val *root = yyjson_doc_get_root(jsonEntry->data.json);
tile_t tiles[CHUNK_TILE_COUNT];
assetChunkParseTiles(root, tiles);
out->hasTerrain = assetChunkTilesHaveTerrain(tiles);
uint8_t meshIndex = out->hasTerrain ? 1 : 0;
assetChunkParseMeshes(root, out, &meshIndex);
out->meshCount = meshIndex;
assetChunkParseEntities(root, out);
assetChunkParseAreas(root, out);
assetUnlockEntry(jsonEntry);
out->tiles = memoryAllocate(CHUNK_TILE_COUNT * sizeof(tile_t));
memoryCopy(out->tiles, tiles, CHUNK_TILE_COUNT * sizeof(tile_t));
// Terrain: build the mesh, then wrap it in synthetic (non-cache,
// non-refcounted) mesh/model entries so modelEntries[0] behaves exactly
// like a real file-backed model entry for every downstream consumer -
// see the header's doc comment on hasTerrain/terrainMeshEntry.
if(out->hasTerrain) {
assetmeshoutput_t terrainMesh;
memoryZero(&terrainMesh, sizeof(terrainMesh));
errorret_t terrainRet = assetChunkBuildTerrain(out->tiles, &terrainMesh);
if(errorIsNotOk(terrainRet)) {
memoryFree(out->tiles);
out->tiles = NULL;
assetLoaderErrorThrow(
loading, "Failed to build chunk terrain mesh: %s",
loading->entry->name
);
}
assetloaderinput_t texInput = { .texture = TEXTURE_FORMAT_RGBA };
assetentry_t *texEntry = assetLock(
ASSET_CHUNK_TERRAIN_TEXTURE, ASSET_LOADER_TYPE_TEXTURE, &texInput
);
errorret_t texRet = assetRequireLoaded(texEntry);
if(errorIsNotOk(texRet)) {
assetUnlockEntry(texEntry);
meshDispose(&terrainMesh.mesh);
memoryFree(out->tiles);
out->tiles = NULL;
assetLoaderErrorThrow(
loading, "Failed to load chunk terrain texture: %s",
loading->entry->name
);
}
out->terrainTexEntry = texEntry;
out->terrainMeshEntry = memoryAllocate(sizeof(assetentry_t));
memoryZero(out->terrainMeshEntry, sizeof(assetentry_t));
out->terrainMeshEntry->type = ASSET_LOADER_TYPE_MESH;
out->terrainMeshEntry->state = ASSET_ENTRY_STATE_LOADED;
out->terrainMeshEntry->data.mesh = terrainMesh;
out->terrainModelEntry = memoryAllocate(sizeof(assetentry_t));
memoryZero(out->terrainModelEntry, sizeof(assetentry_t));
out->terrainModelEntry->type = ASSET_LOADER_TYPE_MODEL;
out->terrainModelEntry->state = ASSET_ENTRY_STATE_LOADED;
out->terrainModelEntry->data.model.meshEntry = out->terrainMeshEntry;
out->terrainModelEntry->data.model.texEntry = out->terrainTexEntry;
out->terrainModelEntry->data.model.color = COLOR_WHITE;
out->modelEntries[0] = out->terrainModelEntry;
}
if(out->meshCount == 0) {
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
errorOk();
}
loading->loading.chunk.state = ASSET_CHUNK_LOADING_STATE_LOAD_MODELS;
loading->loading.chunk.modelIndex = out->hasTerrain ? 1 : 0;
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
errorOk();
}
errorret_t assetChunkDispose(assetentry_t *entry) {
assertNotNull(entry, "Entry cannot be NULL");
assertTrue(entry->type == ASSET_LOADER_TYPE_CHUNK, "Invalid type.");
assertIsMainThread("Must be called from the main thread.");
assetchunkoutput_t *out = &entry->data.chunk;
if(out->tiles != NULL) {
memoryFree(out->tiles);
out->tiles = NULL;
}
for(uint8_t m = 0; m < out->meshCount; m++) {
if(out->modelEntries[m] == NULL) continue;
if(out->hasTerrain && m == 0) continue; // synthetic - freed below.
assetUnlockEntry(out->modelEntries[m]);
out->modelEntries[m] = NULL;
}
if(out->hasTerrain) {
if(out->terrainMeshEntry != NULL) {
assetmeshoutput_t *meshOut = &out->terrainMeshEntry->data.mesh;
if(meshOut->meshInitialized) {
meshDispose(&meshOut->mesh);
meshOut->meshInitialized = false;
}
if(meshOut->vertices != NULL) {
memoryFree(meshOut->vertices);
meshOut->vertices = NULL;
}
memoryFree(out->terrainMeshEntry);
out->terrainMeshEntry = NULL;
}
if(out->terrainModelEntry != NULL) {
memoryFree(out->terrainModelEntry);
out->terrainModelEntry = NULL;
}
if(out->terrainTexEntry != NULL) {
assetUnlockEntry(out->terrainTexEntry);
out->terrainTexEntry = NULL;
}
out->modelEntries[0] = NULL;
out->hasTerrain = false;
}
errorOk();
}
@@ -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 "asset/assetfile.h"
#include "rpg/overworld/chunk.h"
typedef struct assetloading_s assetloading_t;
typedef struct assetentry_s assetentry_t;
typedef struct {
void *nothing;
} assetchunkloaderinput_t;
typedef enum {
ASSET_CHUNK_LOADING_STATE_INITIAL,
ASSET_CHUNK_LOADING_STATE_PARSE,
ASSET_CHUNK_LOADING_STATE_LOAD_MODELS,
ASSET_CHUNK_LOADING_STATE_DONE
} assetchunkloadingstate_t;
typedef struct {
assetchunkloadingstate_t state;
// Index into assetchunkoutput_t.modelNames/modelEntries the external
// (non-terrain) model polling loop has reached - see
// ASSET_CHUNK_LOADING_STATE_LOAD_MODELS.
uint8_t modelIndex;
} assetchunkloaderloading_t;
typedef enum {
CHUNK_ENTITY_SPAWN_KIND_GLOBAL,
CHUNK_ENTITY_SPAWN_KIND_ITEM
} chunkentityspawnkind_t;
typedef struct {
chunkentityspawnkind_t kind;
uint16_t globalId; // Valid when kind == CHUNK_ENTITY_SPAWN_KIND_GLOBAL.
uint16_t itemId; // Valid when kind == CHUNK_ENTITY_SPAWN_KIND_ITEM.
uint8_t itemQuantity; // Valid when kind == CHUNK_ENTITY_SPAWN_KIND_ITEM.
worldpos_t position;
} chunkentityspawn_t;
typedef struct {
worldpos_t min;
worldpos_t max;
uint16_t callbackId; // Index into MAP_AREA_CALLBACK_LIST.
uint8_t notify;
uint8_t trigger;
} chunkareaspawn_t;
typedef struct {
tile_t *tiles;
uint8_t meshCount;
char_t modelNames[CHUNK_MESH_COUNT_MAX][CHUNK_MESH_NAME_MAX];
vec3 meshOffsets[CHUNK_MESH_COUNT_MAX];
assetentry_t *modelEntries[CHUNK_MESH_COUNT_MAX];
// Terrain mesh generated at load time from the tile grid (no baked
// .dmf/.json - see assetChunkBuildTerrain in assetchunkloader.c). NULL
// (with hasTerrain false) for a chunk with no non-null tiles, in which
// case modelEntries[0] (if meshCount > 0) is the first external mesh
// instead - matches the old baked pipeline's "only write a terrain
// model if it has vertices" behavior.
//
// terrainMeshEntry/terrainModelEntry are heap-allocated, plain
// assetentry_t values that are never passed to assetLock/assetGetEntry/
// assetUnlockEntry (only a real, name-keyed, ref-counted entry may be)
// - they exist purely so modelEntries[0] can point at something with
// the same ->state/->data.model/->data.mesh.mesh shape every other
// model/mesh entry has, so chunk.c/map.c/sceneoverworld.c don't need to
// know terrain isn't file-backed. Freed directly (not assetUnlockEntry)
// in assetChunkDispose. terrainTexEntry is a real, ref-counted texture
// entry (the shared "tiles.png") and must be assetUnlockEntry'd there.
bool_t hasTerrain;
assetentry_t *terrainMeshEntry;
assetentry_t *terrainModelEntry;
assetentry_t *terrainTexEntry;
uint8_t entitySpawnCount;
chunkentityspawn_t entitySpawns[CHUNK_ENTITY_SPAWN_COUNT_MAX];
uint8_t areaSpawnCount;
chunkareaspawn_t areaSpawns[CHUNK_AREA_COUNT_MAX];
} assetchunkoutput_t;
/**
* Async loader stub for chunk assets. Chunk loading is performed entirely
* on the main thread (it delegates to the JSON asset loader and to
* meshInit/meshFlush, both main-thread-only); this callback is never
* reached.
*
* @param loading Loading information for the asset being loaded.
* @return Error code indicating success or failure of the load operation.
*/
errorret_t assetChunkLoaderAsync(assetloading_t *loading);
/**
* Synchronous loader for chunk assets. Locks the chunk's JSON descriptor
* as a sub-asset, parses tiles/external mesh references/entity spawns/
* area spawns, generates the terrain mesh directly from the tile grid,
* and locks/waits for every referenced model (and the terrain's shared
* tileset texture) to finish loading.
*
* @param loading Loading information for the asset being loaded.
* @return Error code indicating success or failure of the load operation.
*/
errorret_t assetChunkLoaderSync(assetloading_t *loading);
/**
* Disposer for chunk assets.
*
* @param entry Asset entry containing the chunk data to dispose.
* @return Error code indicating success or failure of the dispose operation.
*/
errorret_t assetChunkDispose(assetentry_t *entry);
+4 -4
View File
@@ -7,7 +7,8 @@
#include "display/display.h" #include "display/display.h"
#include "display/framebuffer/framebuffer.h" #include "display/framebuffer/framebuffer.h"
#include "scene/scene.h" #include "entity/entity.h"
#include "entity/entityrender.h"
#include "display/spritebatch/spritebatch.h" #include "display/spritebatch/spritebatch.h"
#include "display/mesh/quad.h" #include "display/mesh/quad.h"
#include "display/mesh/cube.h" #include "display/mesh/cube.h"
@@ -16,7 +17,6 @@
#include "display/mesh/capsule.h" #include "display/mesh/capsule.h"
#include "display/mesh/triprism.h" #include "display/mesh/triprism.h"
#include "display/screen/screen.h" #include "display/screen/screen.h"
#include "ui/ui.h"
#include "display/text/text.h" #include "display/text/text.h"
#include "assert/assert.h" #include "assert/assert.h"
#include "util/memory.h" #include "util/memory.h"
@@ -76,14 +76,14 @@ errorret_t displayUpdate(void) {
spriteBatchClear(); spriteBatchClear();
errorChain(frameBufferBind(NULL)); errorChain(frameBufferBind(NULL));
// Bind screen and render scene // Bind screen and render the entity world
errorChain(screenBind()); errorChain(screenBind());
frameBufferClear( frameBufferClear(
FRAMEBUFFER_CLEAR_COLOR | FRAMEBUFFER_CLEAR_DEPTH, FRAMEBUFFER_CLEAR_COLOR | FRAMEBUFFER_CLEAR_DEPTH,
SCREEN.background SCREEN.background
); );
errorChain(sceneRender()); errorChain(entityRenderAll());
// Finish up // Finish up
screenUnbind(); screenUnbind();
+4 -14
View File
@@ -11,11 +11,9 @@
#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 "entity/entity.h"
#include "asset/asset.h" #include "asset/asset.h"
#include "ui/ui.h"
#include "assert/assert.h" #include "assert/assert.h"
#ifdef DUSK_NETWORK #ifdef DUSK_NETWORK
#include "network/network.h" #include "network/network.h"
@@ -48,13 +46,11 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
errorChain(localeManagerInit()); errorChain(localeManagerInit());
errorChain(displayInit()); errorChain(displayInit());
errorChain(audioInit()); errorChain(audioInit());
errorChain(rpgInit()); entityManagerInit();
errorChain(uiInit());
#ifdef DUSK_NETWORK #ifdef DUSK_NETWORK
errorChain(networkInit()); errorChain(networkInit());
#endif #endif
errorChain(scriptInit()); errorChain(scriptInit());
errorChain(sceneInit());
// Runs once every other system is ready, so init.ds can use any of them. // Runs once every other system is ready, so init.ds can use any of them.
scripterror_t scriptInitError; scripterror_t scriptInitError;
@@ -70,8 +66,6 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
consolePrint("Assertions real"); consolePrint("Assertions real");
#endif #endif
sceneSet(SCENE_TYPE_INITIAL);
errorOk(); errorOk();
} }
@@ -85,10 +79,8 @@ errorret_t engineUpdate(void) {
inputUpdate(); inputUpdate();
consoleUpdate(); consoleUpdate();
errorChain(audioUpdateEarly()); errorChain(audioUpdateEarly());
errorChain(rpgUpdate());
errorChain(sceneUpdate());
errorChain(assetUpdate()); errorChain(assetUpdate());
errorChain(uiUpdate()); entityManagerUpdate();
// Render // Render
errorChain(displayUpdate()); errorChain(displayUpdate());
@@ -102,14 +94,12 @@ void engineExit(void) {
} }
errorret_t engineDispose(void) { errorret_t engineDispose(void) {
errorChain(sceneDispose());
errorChain(scriptDispose()); errorChain(scriptDispose());
entityManagerDispose();
#ifdef DUSK_NETWORK #ifdef DUSK_NETWORK
errorChain(networkDispose()); errorChain(networkDispose());
#endif #endif
errorChain(rpgDispose());
localeManagerDispose(); localeManagerDispose();
errorChain(uiDispose());
errorChain(audioDispose()); errorChain(audioDispose());
consoleDispose(); consoleDispose();
errorChain(displayDispose()); errorChain(displayDispose());
+1 -1
View File
@@ -7,7 +7,7 @@
target_sources(${DUSK_LIBRARY_TARGET_NAME} target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC PUBLIC
entity.c entity.c
entitymanager.c entityrender.c
component.c component.c
) )
+13 -22
View File
@@ -5,7 +5,7 @@
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
#include "entitymanager.h" #include "entity.h"
#include "assert/assert.h" #include "assert/assert.h"
#include "util/memory.h" #include "util/memory.h"
@@ -28,41 +28,37 @@ componentdefinition_t COMPONENT_DEFINITIONS[] = {
}; };
void componentInit( void componentInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const componenttype_t type const componenttype_t type
) { ) {
assertNotNull(mgr, "Entity manager cannot be null");
assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB"); assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB");
assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB"); assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB");
assertTrue(type < COMPONENT_TYPE_COUNT, "Component type OOB"); assertTrue(type < COMPONENT_TYPE_COUNT, "Component type OOB");
assertTrue(type != COMPONENT_TYPE_NULL, "Cannot initialize null component"); assertTrue(type != COMPONENT_TYPE_NULL, "Cannot initialize null component");
componentindex_t index = componentGetIndex(entityId, componentId); componentindex_t index = componentGetIndex(entityId, componentId);
component_t *cmp = &mgr->components[index]; component_t *cmp = &COMPONENTS[index];
memoryZero(cmp, sizeof(component_t)); memoryZero(cmp, sizeof(component_t));
cmp->type = type; cmp->type = type;
if(COMPONENT_DEFINITIONS[type].init) { if(COMPONENT_DEFINITIONS[type].init) {
COMPONENT_DEFINITIONS[type].init(mgr, entityId, componentId); COMPONENT_DEFINITIONS[type].init(entityId, componentId);
} }
} }
void * componentGetData( void * componentGetData(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const componenttype_t type const componenttype_t type
) { ) {
assertNotNull(mgr, "Entity manager cannot be null");
assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB"); assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB");
assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB"); assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB");
assertTrue(type < COMPONENT_TYPE_COUNT, "Component type OOB"); assertTrue(type < COMPONENT_TYPE_COUNT, "Component type OOB");
assertTrue(type != COMPONENT_TYPE_NULL, "Cannot get data of null component"); assertTrue(type != COMPONENT_TYPE_NULL, "Cannot get data of null component");
componentindex_t index = componentGetIndex(entityId, componentId); componentindex_t index = componentGetIndex(entityId, componentId);
component_t *cmp = &mgr->components[index]; component_t *cmp = &COMPONENTS[index];
assertTrue(cmp->type == type, "Component type mismatch"); assertTrue(cmp->type == type, "Component type mismatch");
return &cmp->data; return &cmp->data;
@@ -78,12 +74,10 @@ componentindex_t componentGetIndex(
} }
entityid_t componentGetEntitiesWithComponent( entityid_t componentGetEntitiesWithComponent(
entitymanager_t *mgr,
const componenttype_t type, const componenttype_t type,
entityid_t outEntities[ENTITY_COUNT_MAX], entityid_t outEntities[ENTITY_COUNT_MAX],
componentid_t outComponents[ENTITY_COUNT_MAX] componentid_t outComponents[ENTITY_COUNT_MAX]
) { ) {
assertNotNull(mgr, "Entity manager cannot be null");
assertTrue(type < COMPONENT_TYPE_COUNT, "Component type OOB"); assertTrue(type < COMPONENT_TYPE_COUNT, "Component type OOB");
assertTrue(type != COMPONENT_TYPE_NULL, "Cannot check NULL type"); assertTrue(type != COMPONENT_TYPE_NULL, "Cannot check NULL type");
assertNotNull(outEntities, "Output entities array cannot be null"); assertNotNull(outEntities, "Output entities array cannot be null");
@@ -91,16 +85,16 @@ entityid_t componentGetEntitiesWithComponent(
entityid_t written = 0; entityid_t written = 0;
for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) { for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) {
componentid_t used = mgr->entitiesWithComponent[ componentid_t used = ENTITIES_WITH_COMPONENT[
type * ENTITY_COUNT_MAX + i type * ENTITY_COUNT_MAX + i
]; ];
if(used == COMPONENT_ID_INVALID) continue; if(used == COMPONENT_ID_INVALID) continue;
assertTrue( assertTrue(
mgr->components[componentGetIndex(i, used)].type == type, COMPONENTS[componentGetIndex(i, used)].type == type,
"Component type mismatch in entitiesWithComponent lookup" "Component type mismatch in entitiesWithComponent lookup"
); );
assertTrue( assertTrue(
(mgr->entities[i].state & ENTITY_STATE_ACTIVE) != 0, (ENTITIES[i].state & ENTITY_STATE_ACTIVE) != 0,
"Inactive entity in entitiesWithComponent lookup" "Inactive entity in entitiesWithComponent lookup"
); );
assertTrue( assertTrue(
@@ -117,35 +111,32 @@ entityid_t componentGetEntitiesWithComponent(
return written; return written;
} }
errorret_t componentRenderAll(entitymanager_t *mgr) { errorret_t componentRenderAll(void) {
assertNotNull(mgr, "Entity manager cannot be null");
for(entityid_t eid = 0; eid < ENTITY_COUNT_MAX; eid++) { for(entityid_t eid = 0; eid < ENTITY_COUNT_MAX; eid++) {
if(!(mgr->entities[eid].state & ENTITY_STATE_ACTIVE)) continue; if(!(ENTITIES[eid].state & ENTITY_STATE_ACTIVE)) continue;
for(componentid_t cid = 0; cid < ENTITY_COMPONENT_COUNT_MAX; cid++) { for(componentid_t cid = 0; cid < ENTITY_COMPONENT_COUNT_MAX; cid++) {
component_t *cmp = &mgr->components[componentGetIndex(eid, cid)]; component_t *cmp = &COMPONENTS[componentGetIndex(eid, cid)];
if(cmp->type == COMPONENT_TYPE_NULL) continue; if(cmp->type == COMPONENT_TYPE_NULL) continue;
if(!COMPONENT_DEFINITIONS[cmp->type].render) continue; if(!COMPONENT_DEFINITIONS[cmp->type].render) continue;
errorChain(COMPONENT_DEFINITIONS[cmp->type].render(mgr, eid, cid)); errorChain(COMPONENT_DEFINITIONS[cmp->type].render(eid, cid));
} }
} }
errorOk(); errorOk();
} }
void componentDispose( void componentDispose(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
assertNotNull(mgr, "Entity manager cannot be null");
assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB"); assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB");
assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB"); assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB");
componentindex_t index = componentGetIndex(entityId, componentId); componentindex_t index = componentGetIndex(entityId, componentId);
component_t *cmp = &mgr->components[index]; component_t *cmp = &COMPONENTS[index];
if(cmp->type == COMPONENT_TYPE_NULL) return; if(cmp->type == COMPONENT_TYPE_NULL) return;
if(COMPONENT_DEFINITIONS[cmp->type].dispose) { if(COMPONENT_DEFINITIONS[cmp->type].dispose) {
COMPONENT_DEFINITIONS[cmp->type].dispose(mgr, entityId, componentId); COMPONENT_DEFINITIONS[cmp->type].dispose(entityId, componentId);
} }
cmp->type = COMPONENT_TYPE_NULL; cmp->type = COMPONENT_TYPE_NULL;
+1 -14
View File
@@ -23,12 +23,10 @@ typedef union {
/** /**
* Callback signature for a component's init/dispose hooks. * Callback signature for a component's init/dispose hooks.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
*/ */
typedef void (*componentcallback_t)( typedef void (*componentcallback_t)(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -36,13 +34,11 @@ typedef void (*componentcallback_t)(
/** /**
* Callback signature for a component's render hook. * Callback signature for a component's render hook.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return Error state. * @return Error state.
*/ */
typedef errorret_t (*componentcallbackerror_t)( typedef errorret_t (*componentcallbackerror_t)(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -76,13 +72,11 @@ extern componentdefinition_t COMPONENT_DEFINITIONS[];
/** /**
* Initializes a component of the given type for the entity with component ID. * Initializes a component of the given type for the entity with component ID.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param type The type of the component to initialize. * @param type The type of the component to initialize.
*/ */
void componentInit( void componentInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const componenttype_t type const componenttype_t type
@@ -91,14 +85,12 @@ void componentInit(
/** /**
* Gets the pointer to the data of a component for the entity with component ID. * Gets the pointer to the data of a component for the entity with component ID.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param type The type of the component to get, only used for assertion. * @param type The type of the component to get, only used for assertion.
* @return A pointer to the component data. * @return A pointer to the component data.
*/ */
void * componentGetData( void * componentGetData(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const componenttype_t type const componenttype_t type
@@ -119,7 +111,6 @@ componentindex_t componentGetIndex(
/** /**
* Gets the entity IDs of all entities with a component of the given type. * Gets the entity IDs of all entities with a component of the given type.
* *
* @param mgr The entity manager to search.
* @param type The type of the component to get entities for. * @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 * @param outEntities An array to write the entity IDs to, must be at least
* ENTITY_COUNT_MAX in size. * ENTITY_COUNT_MAX in size.
@@ -127,7 +118,6 @@ componentindex_t componentGetIndex(
* @return The number of entity IDs written to outEntities. * @return The number of entity IDs written to outEntities.
*/ */
entityid_t componentGetEntitiesWithComponent( entityid_t componentGetEntitiesWithComponent(
entitymanager_t *mgr,
const componenttype_t type, const componenttype_t type,
entityid_t outEntities[ENTITY_COUNT_MAX], entityid_t outEntities[ENTITY_COUNT_MAX],
componentid_t outComponents[ENTITY_COUNT_MAX] componentid_t outComponents[ENTITY_COUNT_MAX]
@@ -136,12 +126,10 @@ entityid_t componentGetEntitiesWithComponent(
/** /**
* Disposes of a component for the entity with component ID. * Disposes of a component for the entity with component ID.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
*/ */
void componentDispose( void componentDispose(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -151,7 +139,6 @@ void componentDispose(
* Iterates all active entities and all their component slots. No-op for * Iterates all active entities and all their component slots. No-op for
* components whose definition has render == NULL. * components whose definition has render == NULL.
* *
* @param mgr The entity manager to render.
* @return Error state. * @return Error state.
*/ */
errorret_t componentRenderAll(entitymanager_t *mgr); errorret_t componentRenderAll(void);
@@ -6,53 +6,49 @@
*/ */
#include "entityanimation.h" #include "entityanimation.h"
#include "entity/entitymanager.h" #include "entity/entity.h"
#include "util/memory.h" #include "util/memory.h"
#include "time/time.h" #include "time/time.h"
void entityAnimationInit( void entityAnimationInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityanimation_t *animComp = entityAnimationGet(mgr, entityId, componentId); entityanimation_t *animComp = entityAnimationGet(entityId, componentId);
memoryZero(animComp, sizeof(entityanimation_t)); memoryZero(animComp, sizeof(entityanimation_t));
animComp->speed = 1.0f; animComp->speed = 1.0f;
entityUpdateAdd(mgr, entityId, entityAnimationUpdate, componentId, NULL); entityUpdateAdd(entityId, entityAnimationUpdate, componentId, NULL);
} }
entityanimation_t *entityAnimationGet( entityanimation_t *entityAnimationGet(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
return componentGetData( return componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_ANIMATION entityId, componentId, COMPONENT_TYPE_ANIMATION
); );
} }
void entityAnimationSetKeyframes( void entityAnimationSetKeyframes(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
keyframe_t *keyframes, keyframe_t *keyframes,
uint16_t *keyframeCounts, uint16_t *keyframeCounts,
const uint16_t layerCount const uint16_t layerCount
) { ) {
entityanimation_t *animComp = entityAnimationGet(mgr, entityId, componentId); entityanimation_t *animComp = entityAnimationGet(entityId, componentId);
animationInit(&animComp->anim, keyframes, keyframeCounts, layerCount); animationInit(&animComp->anim, keyframes, keyframeCounts, layerCount);
animComp->speed = 1.0f; animComp->speed = 1.0f;
animComp->playing = false; animComp->playing = false;
} }
void entityAnimationPlay( void entityAnimationPlay(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityanimation_t *animComp = entityAnimationGet(mgr, entityId, componentId); entityanimation_t *animComp = entityAnimationGet(entityId, componentId);
animComp->anim.time = 0.0f; animComp->anim.time = 0.0f;
// Clear the internal completed latch so a previously-finished // Clear the internal completed latch so a previously-finished
// (non-looping) animation can actually advance again. // (non-looping) animation can actually advance again.
@@ -61,61 +57,55 @@ void entityAnimationPlay(
} }
void entityAnimationStop( void entityAnimationStop(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityanimation_t *animComp = entityAnimationGet(mgr, entityId, componentId); entityanimation_t *animComp = entityAnimationGet(entityId, componentId);
animComp->playing = false; animComp->playing = false;
} }
bool_t entityAnimationIsPlaying( bool_t entityAnimationIsPlaying(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityanimation_t *animComp = entityAnimationGet(mgr, entityId, componentId); entityanimation_t *animComp = entityAnimationGet(entityId, componentId);
return animComp->playing; return animComp->playing;
} }
void entityAnimationSetLoop( void entityAnimationSetLoop(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const bool_t loop const bool_t loop
) { ) {
entityanimation_t *animComp = entityAnimationGet(mgr, entityId, componentId); entityanimation_t *animComp = entityAnimationGet(entityId, componentId);
if(loop) animComp->anim.flags |= ANIMATION_FLAG_LOOP; if(loop) animComp->anim.flags |= ANIMATION_FLAG_LOOP;
else animComp->anim.flags &= ~ANIMATION_FLAG_LOOP; else animComp->anim.flags &= ~ANIMATION_FLAG_LOOP;
} }
void entityAnimationSetSpeed( void entityAnimationSetSpeed(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const float_t speed const float_t speed
) { ) {
entityanimation_t *animComp = entityAnimationGet(mgr, entityId, componentId); entityanimation_t *animComp = entityAnimationGet(entityId, componentId);
animComp->speed = speed; animComp->speed = speed;
} }
float_t entityAnimationGetValue( float_t entityAnimationGetValue(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const uint16_t channelIndex const uint16_t channelIndex
) { ) {
entityanimation_t *animComp = entityAnimationGet(mgr, entityId, componentId); entityanimation_t *animComp = entityAnimationGet(entityId, componentId);
return animationGetLayerValue(&animComp->anim, channelIndex); return animationGetLayerValue(&animComp->anim, channelIndex);
} }
void entityAnimationUpdate( void entityAnimationUpdate(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
void *user void *user
) { ) {
entityanimation_t *animComp = entityAnimationGet(mgr, entityId, componentId); entityanimation_t *animComp = entityAnimationGet(entityId, componentId);
if(!animComp->playing) return; if(!animComp->playing) return;
animationUpdate(&animComp->anim, TIME.delta * animComp->speed); animationUpdate(&animComp->anim, TIME.delta * animComp->speed);
} }
@@ -34,12 +34,10 @@ typedef struct {
* entityAnimationUpdate as an update callback. Call * entityAnimationUpdate as an update callback. Call
* entityAnimationSetKeyframes() before entityAnimationPlay(). * entityAnimationSetKeyframes() before entityAnimationPlay().
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
*/ */
void entityAnimationInit( void entityAnimationInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -48,14 +46,12 @@ void entityAnimationInit(
* Gets the underlying animation structure (temporarily) for the given * Gets the underlying animation structure (temporarily) for the given
* entity. Prefer the dedicated getters/setters where possible. * entity. Prefer the dedicated getters/setters where possible.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return The animation component data for the given entity and * @return The animation component data for the given entity and
* component ID. * component ID.
*/ */
entityanimation_t *entityAnimationGet( entityanimation_t *entityAnimationGet(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -66,7 +62,6 @@ entityanimation_t *entityAnimationGet(
* they point to are not copied, and must outlive this component (e.g. * they point to are not copied, and must outlive this component (e.g.
* static/const arrays owned by the caller). * static/const arrays owned by the caller).
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param keyframes Flat array of keyframe_t, one contiguous run per layer * @param keyframes Flat array of keyframe_t, one contiguous run per layer
@@ -76,7 +71,6 @@ entityanimation_t *entityAnimationGet(
* @param layerCount The number of layers/channels. * @param layerCount The number of layers/channels.
*/ */
void entityAnimationSetKeyframes( void entityAnimationSetKeyframes(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
keyframe_t *keyframes, keyframe_t *keyframes,
@@ -87,12 +81,10 @@ void entityAnimationSetKeyframes(
/** /**
* Starts (or restarts) playback from time 0. * Starts (or restarts) playback from time 0.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
*/ */
void entityAnimationPlay( void entityAnimationPlay(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -100,12 +92,10 @@ void entityAnimationPlay(
/** /**
* Stops playback without resetting the current time. * Stops playback without resetting the current time.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
*/ */
void entityAnimationStop( void entityAnimationStop(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -113,13 +103,11 @@ void entityAnimationStop(
/** /**
* Checks whether the animation is currently playing. * Checks whether the animation is currently playing.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return True if playing. * @return True if playing.
*/ */
bool_t entityAnimationIsPlaying( bool_t entityAnimationIsPlaying(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -128,13 +116,11 @@ bool_t entityAnimationIsPlaying(
* Sets whether the animation loops on reaching its final keyframe, rather * Sets whether the animation loops on reaching its final keyframe, rather
* than stopping there. * than stopping there.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param loop True to loop, false to stop at the end. * @param loop True to loop, false to stop at the end.
*/ */
void entityAnimationSetLoop( void entityAnimationSetLoop(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const bool_t loop const bool_t loop
@@ -143,13 +129,11 @@ void entityAnimationSetLoop(
/** /**
* Sets the animation's playback rate multiplier. * Sets the animation's playback rate multiplier.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param speed The new playback rate; 1.0 = normal speed. * @param speed The new playback rate; 1.0 = normal speed.
*/ */
void entityAnimationSetSpeed( void entityAnimationSetSpeed(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const float_t speed const float_t speed
@@ -159,7 +143,6 @@ void entityAnimationSetSpeed(
* Evaluates one of the animation's channels at its current playback * Evaluates one of the animation's channels at its current playback
* time, regardless of whether it's currently playing. * time, regardless of whether it's currently playing.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param channelIndex The channel to evaluate, in [0, channelCount) as * @param channelIndex The channel to evaluate, in [0, channelCount) as
@@ -167,7 +150,6 @@ void entityAnimationSetSpeed(
* @return That channel's value at the current time. * @return That channel's value at the current time.
*/ */
float_t entityAnimationGetValue( float_t entityAnimationGetValue(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const uint16_t channelIndex const uint16_t channelIndex
@@ -178,13 +160,11 @@ float_t entityAnimationGetValue(
* (a no-op if not playing). Registered automatically as an update * (a no-op if not playing). Registered automatically as an update
* callback by entityAnimationInit. * callback by entityAnimationInit.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param user Unused. * @param user Unused.
*/ */
void entityAnimationUpdate( void entityAnimationUpdate(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
void *user void *user
@@ -5,18 +5,16 @@
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
#include "entity/entitymanager.h"
#include "entity/entity.h" #include "entity/entity.h"
#include "entity/component/display/entityposition.h" #include "entity/component/display/entityposition.h"
#include "display/screen/screen.h" #include "display/screen/screen.h"
void entityCameraInit( void entityCameraInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entitycamera_t *cam = (entitycamera_t *)componentGetData( entitycamera_t *cam = (entitycamera_t *)componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_CAMERA entityId, componentId, COMPONENT_TYPE_CAMERA
); );
cam->nearClip = 0.1f; cam->nearClip = 0.1f;
cam->farClip = 5000.0f; cam->farClip = 5000.0f;
@@ -25,13 +23,12 @@ void entityCameraInit(
} }
void entityCameraGetProjection( void entityCameraGetProjection(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
mat4 out mat4 out
) { ) {
entitycamera_t *cam = (entitycamera_t *)componentGetData( entitycamera_t *cam = (entitycamera_t *)componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_CAMERA entityId, componentId, COMPONENT_TYPE_CAMERA
); );
if( if(
@@ -64,26 +61,25 @@ void entityCameraGetProjection(
} }
} }
entityid_t entityCameraGetCurrent(entitymanager_t *mgr) { entityid_t entityCameraGetCurrent(void) {
entityid_t camEnts[ENTITY_COUNT_MAX]; entityid_t camEnts[ENTITY_COUNT_MAX];
componentid_t camComps[ENTITY_COUNT_MAX]; componentid_t camComps[ENTITY_COUNT_MAX];
entityid_t count = componentGetEntitiesWithComponent( entityid_t count = componentGetEntitiesWithComponent(
mgr, COMPONENT_TYPE_CAMERA, camEnts, camComps COMPONENT_TYPE_CAMERA, camEnts, camComps
); );
if(count == 0) return ENTITY_ID_INVALID; if(count == 0) return ENTITY_ID_INVALID;
return camEnts[0]; return camEnts[0];
} }
void entityCameraGetForward( void entityCameraGetForward(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
vec2 out vec2 out
) { ) {
componentid_t posComp = entityGetComponent( componentid_t posComp = entityGetComponent(
mgr, entityId, COMPONENT_TYPE_POSITION entityId, COMPONENT_TYPE_POSITION
); );
mat4 transform; mat4 transform;
entityPositionGetTransform(mgr, entityId, posComp, transform); entityPositionGetTransform(entityId, posComp, transform);
// transform is an object->world matrix; column 2 is the entity's local Z // transform is an object->world matrix; column 2 is the entity's local Z
// axis expressed in world space. Cameras look down their local -Z. // axis expressed in world space. Cameras look down their local -Z.
float_t fx = -transform[2][0]; float_t fx = -transform[2][0];
@@ -95,15 +91,14 @@ void entityCameraGetForward(
} }
void entityCameraGetRight( void entityCameraGetRight(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
vec2 out vec2 out
) { ) {
componentid_t posComp = entityGetComponent( componentid_t posComp = entityGetComponent(
mgr, entityId, COMPONENT_TYPE_POSITION entityId, COMPONENT_TYPE_POSITION
); );
mat4 transform; mat4 transform;
entityPositionGetTransform(mgr, entityId, posComp, transform); entityPositionGetTransform(entityId, posComp, transform);
// transform is an object->world matrix; column 0 is the entity's local X // transform is an object->world matrix; column 0 is the entity's local X
// (right) axis expressed in world space. // (right) axis expressed in world space.
float_t rx = transform[0][0]; float_t rx = transform[0][0];
@@ -115,7 +110,6 @@ void entityCameraGetRight(
} }
void entityCameraLookAtPixelPerfect( void entityCameraLookAtPixelPerfect(
entitymanager_t *mgr,
const entityid_t ent, const entityid_t ent,
const componentid_t posComp, const componentid_t posComp,
const componentid_t camComp, const componentid_t camComp,
@@ -124,7 +118,7 @@ void entityCameraLookAtPixelPerfect(
const float_t scale const float_t scale
) { ) {
entitycamera_t *cam = (entitycamera_t *)componentGetData( entitycamera_t *cam = (entitycamera_t *)componentGetData(
mgr, ent, camComp, COMPONENT_TYPE_CAMERA ent, camComp, COMPONENT_TYPE_CAMERA
); );
float_t dist = ( float_t dist = (
(float_t)SCREEN.height / (2.0f * scale * tanf(cam->perspective.fov * 0.5f)) (float_t)SCREEN.height / (2.0f * scale * tanf(cam->perspective.fov * 0.5f))
@@ -136,5 +130,5 @@ void entityCameraLookAtPixelPerfect(
point[2] + eyeOffset[2] point[2] + eyeOffset[2]
}; };
vec3 up = { 0.0f, 0.0f, -1.0f }; vec3 up = { 0.0f, 0.0f, -1.0f };
entityPositionLookAt(mgr, ent, posComp, eye, (float_t *)point, up); entityPositionLookAt(ent, posComp, eye, (float_t *)point, up);
} }
@@ -37,12 +37,10 @@ typedef struct {
/** /**
* Initializes an entity camera component. * Initializes an entity camera component.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
*/ */
void entityCameraInit( void entityCameraInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -50,13 +48,11 @@ void entityCameraInit(
/** /**
* Renders out the projection matrix for the given camera. * Renders out the projection matrix for the given camera.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param out The output projection matrix. * @param out The output projection matrix.
*/ */
void entityCameraGetProjection( void entityCameraGetProjection(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
mat4 out mat4 out
@@ -66,21 +62,18 @@ void entityCameraGetProjection(
* Returns the entity ID of the first active camera, or ENTITY_ID_INVALID if * Returns the entity ID of the first active camera, or ENTITY_ID_INVALID if
* none are active. * none are active.
* *
* @param mgr The entity manager to search.
*/ */
entityid_t entityCameraGetCurrent(entitymanager_t *mgr); entityid_t entityCameraGetCurrent(void);
/** /**
* Gets the camera's horizontal forward direction (XZ plane) from its * Gets the camera's horizontal forward direction (XZ plane) from its
* position component. Automatically finds the position component on the * position component. Automatically finds the position component on the
* entity. * entity.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The camera entity ID. * @param entityId The camera entity ID.
* @param out Output vec2: {forwardX, forwardZ} normalized. * @param out Output vec2: {forwardX, forwardZ} normalized.
*/ */
void entityCameraGetForward( void entityCameraGetForward(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
vec2 out vec2 out
); );
@@ -89,12 +82,10 @@ void entityCameraGetForward(
* Gets the camera's horizontal right direction (XZ plane) from its position * Gets the camera's horizontal right direction (XZ plane) from its position
* component. Automatically finds the position component on the entity. * component. Automatically finds the position component on the entity.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The camera entity ID. * @param entityId The camera entity ID.
* @param out Output vec2: {rightX, rightZ} normalized. * @param out Output vec2: {rightX, rightZ} normalized.
*/ */
void entityCameraGetRight( void entityCameraGetRight(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
vec2 out vec2 out
); );
@@ -103,7 +94,6 @@ void entityCameraGetRight(
* Positions the camera to look at a 3D point at a pixel-perfect distance * Positions the camera to look at a 3D point at a pixel-perfect distance
* derived from the camera's FOV and screen height. * derived from the camera's FOV and screen height.
* *
* @param mgr The entity manager that owns the entity.
* @param ent The camera entity ID. * @param ent The camera entity ID.
* @param posComp The position component ID. * @param posComp The position component ID.
* @param camComp The camera component ID. * @param camComp The camera component ID.
@@ -112,7 +102,6 @@ void entityCameraGetRight(
* @param scale Pixels per world unit. 1.0 = pixel perfect, 2.0 = 2px per unit. * @param scale Pixels per world unit. 1.0 = pixel perfect, 2.0 = 2px per unit.
*/ */
void entityCameraLookAtPixelPerfect( void entityCameraLookAtPixelPerfect(
entitymanager_t *mgr,
const entityid_t ent, const entityid_t ent,
const componentid_t posComp, const componentid_t posComp,
const componentid_t camComp, const componentid_t camComp,
@@ -5,16 +5,15 @@
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
#include "entity/entitymanager.h" #include "entity/entity.h"
#include "assert/assert.h" #include "assert/assert.h"
void entityPositionInit( void entityPositionInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
pos->flags = 0; pos->flags = 0;
@@ -29,7 +28,6 @@ void entityPositionInit(
} }
void entityPositionLookAt( void entityPositionLookAt(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 eye, vec3 eye,
@@ -37,7 +35,7 @@ void entityPositionLookAt(
vec3 up vec3 up
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
// glm_lookat() produces a view matrix (world -> eye space). Every other // glm_lookat() produces a view matrix (world -> eye space). Every other
// setter treats localTransform as this entity's placement in world space // setter treats localTransform as this entity's placement in world space
@@ -50,19 +48,18 @@ void entityPositionLookAt(
pos->flags = (pos->flags | ENTITY_POSITION_FLAG_PRS_DIRTY) pos->flags = (pos->flags | ENTITY_POSITION_FLAG_PRS_DIRTY)
& ~(ENTITY_POSITION_FLAG_ROTATION_DIRTY | & ~(ENTITY_POSITION_FLAG_ROTATION_DIRTY |
ENTITY_POSITION_FLAG_POSITION_DIRTY); ENTITY_POSITION_FLAG_POSITION_DIRTY);
entityPositionMarkDirty(mgr, pos); entityPositionMarkDirty(pos);
} }
void entityPositionGetTransform( void entityPositionGetTransform(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
mat4 dest mat4 dest
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
entityPositionEnsureWorld(mgr, pos); entityPositionEnsureWorld(pos);
glm_mat4_copy( glm_mat4_copy(
pos->parentEntityId == ENTITY_ID_INVALID pos->parentEntityId == ENTITY_ID_INVALID
? pos->localTransform : pos->worldTransform, ? pos->localTransform : pos->worldTransform,
@@ -71,71 +68,67 @@ void entityPositionGetTransform(
} }
void entityPositionGetLocalTransform( void entityPositionGetLocalTransform(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
mat4 dest mat4 dest
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
entityPositionEnsureLocal(pos); entityPositionEnsureLocal(pos);
glm_mat4_copy(pos->localTransform, dest); glm_mat4_copy(pos->localTransform, dest);
} }
void entityPositionGetLocalPosition( void entityPositionGetLocalPosition(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
entityPositionEnsurePRS(pos); entityPositionEnsurePRS(pos);
glm_vec3_copy(pos->position, dest); glm_vec3_copy(pos->position, dest);
} }
void entityPositionGetWorldPosition( void entityPositionGetWorldPosition(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
if(pos->parentEntityId == ENTITY_ID_INVALID) { if(pos->parentEntityId == ENTITY_ID_INVALID) {
entityPositionEnsurePRS(pos); entityPositionEnsurePRS(pos);
glm_vec3_copy(pos->position, dest); glm_vec3_copy(pos->position, dest);
return; return;
} }
entityPositionEnsureWorld(mgr, pos); entityPositionEnsureWorld(pos);
dest[0] = pos->worldTransform[3][0]; dest[0] = pos->worldTransform[3][0];
dest[1] = pos->worldTransform[3][1]; dest[1] = pos->worldTransform[3][1];
dest[2] = pos->worldTransform[3][2]; dest[2] = pos->worldTransform[3][2];
} }
void entityPositionSetWorldPosition( void entityPositionSetWorldPosition(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 position vec3 position
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
if(pos->parentEntityId == ENTITY_ID_INVALID) { if(pos->parentEntityId == ENTITY_ID_INVALID) {
glm_vec3_copy(position, pos->position); glm_vec3_copy(position, pos->position);
pos->flags = (pos->flags | ENTITY_POSITION_FLAG_POSITION_DIRTY) pos->flags = (pos->flags | ENTITY_POSITION_FLAG_POSITION_DIRTY)
& ~ENTITY_POSITION_FLAG_PRS_DIRTY; & ~ENTITY_POSITION_FLAG_PRS_DIRTY;
entityPositionMarkDirty(mgr, pos); entityPositionMarkDirty(pos);
return; return;
} }
entityposition_t *parent = componentGetData( entityposition_t *parent = componentGetData(
mgr, pos->parentEntityId, pos->parentComponentId, COMPONENT_TYPE_POSITION pos->parentEntityId, pos->parentComponentId, COMPONENT_TYPE_POSITION
); );
entityPositionEnsureWorld(mgr, parent); entityPositionEnsureWorld(parent);
mat4 invParent; mat4 invParent;
glm_mat4_inv(parent->worldTransform, invParent); glm_mat4_inv(parent->worldTransform, invParent);
vec3 localPos; vec3 localPos;
@@ -143,52 +136,49 @@ void entityPositionSetWorldPosition(
glm_vec3_copy(localPos, pos->position); glm_vec3_copy(localPos, pos->position);
pos->flags = (pos->flags | ENTITY_POSITION_FLAG_POSITION_DIRTY) pos->flags = (pos->flags | ENTITY_POSITION_FLAG_POSITION_DIRTY)
& ~ENTITY_POSITION_FLAG_PRS_DIRTY; & ~ENTITY_POSITION_FLAG_PRS_DIRTY;
entityPositionMarkDirty(mgr, pos); entityPositionMarkDirty(pos);
} }
void entityPositionSetLocalPosition( void entityPositionSetLocalPosition(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 position vec3 position
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
glm_vec3_copy(position, pos->position); glm_vec3_copy(position, pos->position);
pos->flags = (pos->flags | ENTITY_POSITION_FLAG_POSITION_DIRTY) pos->flags = (pos->flags | ENTITY_POSITION_FLAG_POSITION_DIRTY)
& ~ENTITY_POSITION_FLAG_PRS_DIRTY; & ~ENTITY_POSITION_FLAG_PRS_DIRTY;
entityPositionMarkDirty(mgr, pos); entityPositionMarkDirty(pos);
} }
void entityPositionGetLocalRotation( void entityPositionGetLocalRotation(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
entityPositionEnsurePRS(pos); entityPositionEnsurePRS(pos);
glm_vec3_copy(pos->rotation, dest); glm_vec3_copy(pos->rotation, dest);
} }
void entityPositionGetWorldRotation( void entityPositionGetWorldRotation(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
if(pos->parentEntityId == ENTITY_ID_INVALID) { if(pos->parentEntityId == ENTITY_ID_INVALID) {
entityPositionEnsurePRS(pos); entityPositionEnsurePRS(pos);
glm_vec3_copy(pos->rotation, dest); glm_vec3_copy(pos->rotation, dest);
return; return;
} }
entityPositionEnsureWorld(mgr, pos); entityPositionEnsureWorld(pos);
const float_t (*wt)[4] = pos->worldTransform; const float_t (*wt)[4] = pos->worldTransform;
const float_t sx = sqrtf( const float_t sx = sqrtf(
wt[0][0]*wt[0][0] + wt[0][1]*wt[0][1] + wt[0][2]*wt[0][2] wt[0][0]*wt[0][0] + wt[0][1]*wt[0][1] + wt[0][2]*wt[0][2]
@@ -219,40 +209,38 @@ void entityPositionGetWorldRotation(
} }
void entityPositionSetLocalRotation( void entityPositionSetLocalRotation(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 rotation vec3 rotation
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
glm_vec3_copy(rotation, pos->rotation); glm_vec3_copy(rotation, pos->rotation);
pos->flags = (pos->flags | ENTITY_POSITION_FLAG_ROTATION_DIRTY) pos->flags = (pos->flags | ENTITY_POSITION_FLAG_ROTATION_DIRTY)
& ~ENTITY_POSITION_FLAG_PRS_DIRTY; & ~ENTITY_POSITION_FLAG_PRS_DIRTY;
entityPositionMarkDirty(mgr, pos); entityPositionMarkDirty(pos);
} }
void entityPositionSetWorldRotation( void entityPositionSetWorldRotation(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 rotation vec3 rotation
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
if(pos->parentEntityId == ENTITY_ID_INVALID) { if(pos->parentEntityId == ENTITY_ID_INVALID) {
glm_vec3_copy(rotation, pos->rotation); glm_vec3_copy(rotation, pos->rotation);
pos->flags = (pos->flags | ENTITY_POSITION_FLAG_ROTATION_DIRTY) pos->flags = (pos->flags | ENTITY_POSITION_FLAG_ROTATION_DIRTY)
& ~ENTITY_POSITION_FLAG_PRS_DIRTY; & ~ENTITY_POSITION_FLAG_PRS_DIRTY;
entityPositionMarkDirty(mgr, pos); entityPositionMarkDirty(pos);
return; return;
} }
entityposition_t *parent = componentGetData( entityposition_t *parent = componentGetData(
mgr, pos->parentEntityId, pos->parentComponentId, COMPONENT_TYPE_POSITION pos->parentEntityId, pos->parentComponentId, COMPONENT_TYPE_POSITION
); );
entityPositionEnsureWorld(mgr, parent); entityPositionEnsureWorld(parent);
// Build target world rotation matrix (unit scale) from XYZ euler. // Build target world rotation matrix (unit scale) from XYZ euler.
const float_t c0 = cosf(rotation[0]), s0 = sinf(rotation[0]); const float_t c0 = cosf(rotation[0]), s0 = sinf(rotation[0]);
@@ -319,37 +307,35 @@ void entityPositionSetWorldRotation(
} }
pos->flags = (pos->flags | ENTITY_POSITION_FLAG_ROTATION_DIRTY) pos->flags = (pos->flags | ENTITY_POSITION_FLAG_ROTATION_DIRTY)
& ~ENTITY_POSITION_FLAG_PRS_DIRTY; & ~ENTITY_POSITION_FLAG_PRS_DIRTY;
entityPositionMarkDirty(mgr, pos); entityPositionMarkDirty(pos);
} }
void entityPositionGetLocalScale( void entityPositionGetLocalScale(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
entityPositionEnsurePRS(pos); entityPositionEnsurePRS(pos);
glm_vec3_copy(pos->scale, dest); glm_vec3_copy(pos->scale, dest);
} }
void entityPositionGetWorldScale( void entityPositionGetWorldScale(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
if(pos->parentEntityId == ENTITY_ID_INVALID) { if(pos->parentEntityId == ENTITY_ID_INVALID) {
entityPositionEnsurePRS(pos); entityPositionEnsurePRS(pos);
glm_vec3_copy(pos->scale, dest); glm_vec3_copy(pos->scale, dest);
return; return;
} }
entityPositionEnsureWorld(mgr, pos); entityPositionEnsureWorld(pos);
const float_t (*wt)[4] = pos->worldTransform; const float_t (*wt)[4] = pos->worldTransform;
dest[0] = sqrtf(wt[0][0]*wt[0][0] + wt[0][1]*wt[0][1] + wt[0][2]*wt[0][2]); dest[0] = sqrtf(wt[0][0]*wt[0][0] + wt[0][1]*wt[0][1] + wt[0][2]*wt[0][2]);
dest[1] = sqrtf(wt[1][0]*wt[1][0] + wt[1][1]*wt[1][1] + wt[1][2]*wt[1][2]); dest[1] = sqrtf(wt[1][0]*wt[1][0] + wt[1][1]*wt[1][1] + wt[1][2]*wt[1][2]);
@@ -357,40 +343,38 @@ void entityPositionGetWorldScale(
} }
void entityPositionSetLocalScale( void entityPositionSetLocalScale(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 scale vec3 scale
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
glm_vec3_copy(scale, pos->scale); glm_vec3_copy(scale, pos->scale);
pos->flags = (pos->flags | ENTITY_POSITION_FLAG_ROTATION_DIRTY) pos->flags = (pos->flags | ENTITY_POSITION_FLAG_ROTATION_DIRTY)
& ~ENTITY_POSITION_FLAG_PRS_DIRTY; & ~ENTITY_POSITION_FLAG_PRS_DIRTY;
entityPositionMarkDirty(mgr, pos); entityPositionMarkDirty(pos);
} }
void entityPositionSetWorldScale( void entityPositionSetWorldScale(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 scale vec3 scale
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
if(pos->parentEntityId == ENTITY_ID_INVALID) { if(pos->parentEntityId == ENTITY_ID_INVALID) {
glm_vec3_copy(scale, pos->scale); glm_vec3_copy(scale, pos->scale);
pos->flags = (pos->flags | ENTITY_POSITION_FLAG_ROTATION_DIRTY) pos->flags = (pos->flags | ENTITY_POSITION_FLAG_ROTATION_DIRTY)
& ~ENTITY_POSITION_FLAG_PRS_DIRTY; & ~ENTITY_POSITION_FLAG_PRS_DIRTY;
entityPositionMarkDirty(mgr, pos); entityPositionMarkDirty(pos);
return; return;
} }
entityposition_t *parent = componentGetData( entityposition_t *parent = componentGetData(
mgr, pos->parentEntityId, pos->parentComponentId, COMPONENT_TYPE_POSITION pos->parentEntityId, pos->parentComponentId, COMPONENT_TYPE_POSITION
); );
entityPositionEnsureWorld(mgr, parent); entityPositionEnsureWorld(parent);
const float_t (*pt)[4] = parent->worldTransform; const float_t (*pt)[4] = parent->worldTransform;
const float_t psx = sqrtf( const float_t psx = sqrtf(
pt[0][0]*pt[0][0] + pt[0][1]*pt[0][1] + pt[0][2]*pt[0][2] pt[0][0]*pt[0][0] + pt[0][1]*pt[0][1] + pt[0][2]*pt[0][2]
@@ -406,24 +390,23 @@ void entityPositionSetWorldScale(
pos->scale[2] = psz > 0.0f ? scale[2] / psz : scale[2]; pos->scale[2] = psz > 0.0f ? scale[2] / psz : scale[2];
pos->flags = (pos->flags | ENTITY_POSITION_FLAG_ROTATION_DIRTY) pos->flags = (pos->flags | ENTITY_POSITION_FLAG_ROTATION_DIRTY)
& ~ENTITY_POSITION_FLAG_PRS_DIRTY; & ~ENTITY_POSITION_FLAG_PRS_DIRTY;
entityPositionMarkDirty(mgr, pos); entityPositionMarkDirty(pos);
} }
void entityPositionSetParent( void entityPositionSetParent(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const entityid_t parentEntityId, const entityid_t parentEntityId,
const componentid_t parentComponentId const componentid_t parentComponentId
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
// Remove from old parent's child list. // Remove from old parent's child list.
if(pos->parentEntityId != ENTITY_ID_INVALID) { if(pos->parentEntityId != ENTITY_ID_INVALID) {
entityposition_t *oldParent = componentGetData( entityposition_t *oldParent = componentGetData(
mgr, pos->parentEntityId, pos->parentComponentId, pos->parentEntityId, pos->parentComponentId,
COMPONENT_TYPE_POSITION COMPONENT_TYPE_POSITION
); );
for(uint8_t i = 0; i < oldParent->childCount; i++) { for(uint8_t i = 0; i < oldParent->childCount; i++) {
@@ -447,7 +430,7 @@ void entityPositionSetParent(
// Register with new parent. // Register with new parent.
if(parentEntityId != ENTITY_ID_INVALID) { if(parentEntityId != ENTITY_ID_INVALID) {
entityposition_t *parent = componentGetData( entityposition_t *parent = componentGetData(
mgr, parentEntityId, parentComponentId, COMPONENT_TYPE_POSITION parentEntityId, parentComponentId, COMPONENT_TYPE_POSITION
); );
if(parent->childCount < ENTITY_POSITION_CHILDREN_MAX) { if(parent->childCount < ENTITY_POSITION_CHILDREN_MAX) {
parent->childEntityIds[parent->childCount] = entityId; parent->childEntityIds[parent->childCount] = entityId;
@@ -456,62 +439,59 @@ void entityPositionSetParent(
} }
} }
entityPositionMarkDirty(mgr, pos); entityPositionMarkDirty(pos);
} }
entityid_t entityPositionGetParent( entityid_t entityPositionGetParent(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityposition_t *pos = componentGetData( entityposition_t *pos = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
return pos->parentEntityId; return pos->parentEntityId;
} }
entityposition_t *entityPositionGet( entityposition_t *entityPositionGet(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
return componentGetData( return componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_POSITION entityId, componentId, COMPONENT_TYPE_POSITION
); );
} }
void entityPositionRebuild(entitymanager_t *mgr, entityposition_t *pos) { void entityPositionRebuild(entityposition_t *pos) {
pos->flags = ( pos->flags = (
pos->flags | pos->flags |
ENTITY_POSITION_FLAG_ROTATION_DIRTY | ENTITY_POSITION_FLAG_ROTATION_DIRTY |
ENTITY_POSITION_FLAG_POSITION_DIRTY ENTITY_POSITION_FLAG_POSITION_DIRTY
) & ~ENTITY_POSITION_FLAG_PRS_DIRTY; ) & ~ENTITY_POSITION_FLAG_PRS_DIRTY;
entityPositionMarkDirty(mgr, pos); entityPositionMarkDirty(pos);
} }
void entityPositionMarkDirty(entitymanager_t *mgr, entityposition_t *pos) { void entityPositionMarkDirty(entityposition_t *pos) {
if(pos->flags & ENTITY_POSITION_FLAG_WORLD_DIRTY) return; if(pos->flags & ENTITY_POSITION_FLAG_WORLD_DIRTY) return;
pos->flags |= ENTITY_POSITION_FLAG_WORLD_DIRTY; pos->flags |= ENTITY_POSITION_FLAG_WORLD_DIRTY;
for(uint8_t i = 0; i < pos->childCount; i++) { for(uint8_t i = 0; i < pos->childCount; i++) {
entityposition_t *child = componentGetData( entityposition_t *child = componentGetData(
mgr, pos->childEntityIds[i], pos->childComponentIds[i], pos->childEntityIds[i], pos->childComponentIds[i],
COMPONENT_TYPE_POSITION COMPONENT_TYPE_POSITION
); );
entityPositionMarkDirty(mgr, child); entityPositionMarkDirty(child);
} }
} }
void entityPositionDisposeDeep( void entityPositionDisposeDeep(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityposition_t *pos = entityPositionGet(mgr, entityId, componentId); entityposition_t *pos = entityPositionGet(entityId, componentId);
// Detach from parent so the parent's child list stays consistent. // Detach from parent so the parent's child list stays consistent.
if(pos->parentEntityId != ENTITY_ID_INVALID) { if(pos->parentEntityId != ENTITY_ID_INVALID) {
entityPositionSetParent( entityPositionSetParent(
mgr, entityId, componentId, ENTITY_ID_INVALID, COMPONENT_ID_INVALID entityId, componentId, ENTITY_ID_INVALID, COMPONENT_ID_INVALID
); );
} }
@@ -526,16 +506,16 @@ void entityPositionDisposeDeep(
// Sever child's parent link so it won't try to modify our disposed // Sever child's parent link so it won't try to modify our disposed
// data. // data.
entityposition_t *child = entityPositionGet( entityposition_t *child = entityPositionGet(
mgr, childEntityIds[i], childComponentIds[i] childEntityIds[i], childComponentIds[i]
); );
child->parentEntityId = ENTITY_ID_INVALID; child->parentEntityId = ENTITY_ID_INVALID;
child->parentComponentId = COMPONENT_ID_INVALID; child->parentComponentId = COMPONENT_ID_INVALID;
} }
entityDispose(mgr, entityId); entityDispose(entityId);
for(uint8_t i = 0; i < childCount; i++) { for(uint8_t i = 0; i < childCount; i++) {
entityPositionDisposeDeep(mgr, childEntityIds[i], childComponentIds[i]); entityPositionDisposeDeep(childEntityIds[i], childComponentIds[i]);
} }
} }
@@ -642,7 +622,7 @@ void entityPositionEnsureLocal(entityposition_t *pos) {
); );
} }
void entityPositionEnsureWorld(entitymanager_t *mgr, entityposition_t *pos) { void entityPositionEnsureWorld(entityposition_t *pos) {
if(!(pos->flags & ENTITY_POSITION_FLAG_WORLD_DIRTY)) return; if(!(pos->flags & ENTITY_POSITION_FLAG_WORLD_DIRTY)) return;
entityPositionEnsureLocal(pos); entityPositionEnsureLocal(pos);
@@ -650,10 +630,10 @@ void entityPositionEnsureWorld(entitymanager_t *mgr, entityposition_t *pos) {
// Parented: world = parent.world x local. worldTransform must be // Parented: world = parent.world x local. worldTransform must be
// written because children (and this node's getters) read it. // written because children (and this node's getters) read it.
entityposition_t *parent = componentGetData( entityposition_t *parent = componentGetData(
mgr, pos->parentEntityId, pos->parentComponentId, pos->parentEntityId, pos->parentComponentId,
COMPONENT_TYPE_POSITION COMPONENT_TYPE_POSITION
); );
entityPositionEnsureWorld(mgr, parent); entityPositionEnsureWorld(parent);
glm_mat4_mul( glm_mat4_mul(
parent->worldTransform, pos->localTransform, pos->worldTransform parent->worldTransform, pos->localTransform, pos->worldTransform
); );
@@ -83,12 +83,10 @@ typedef struct {
* Initializes the entity position component, setting identity transforms and * Initializes the entity position component, setting identity transforms and
* zeroing all parent/child state. * zeroing all parent/child state.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
*/ */
void entityPositionInit( void entityPositionInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -99,7 +97,6 @@ void entityPositionInit(
* setter), not as a view matrix -- invert entityPositionGetTransform()'s * setter), not as a view matrix -- invert entityPositionGetTransform()'s
* result to get a view matrix for rendering. * result to get a view matrix for rendering.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param eye The eye/camera position. * @param eye The eye/camera position.
@@ -107,7 +104,6 @@ void entityPositionInit(
* @param up The up vector. * @param up The up vector.
*/ */
void entityPositionLookAt( void entityPositionLookAt(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 eye, vec3 eye,
@@ -118,13 +114,11 @@ void entityPositionLookAt(
/** /**
* Gets the world-space transform matrix, recomputing it lazily if dirty. * Gets the world-space transform matrix, recomputing it lazily if dirty.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param dest Destination matrix. * @param dest Destination matrix.
*/ */
void entityPositionGetTransform( void entityPositionGetTransform(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
mat4 dest mat4 dest
@@ -133,13 +127,11 @@ void entityPositionGetTransform(
/** /**
* Gets the local transform matrix (does not include parent transforms). * Gets the local transform matrix (does not include parent transforms).
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param dest Destination matrix. * @param dest Destination matrix.
*/ */
void entityPositionGetLocalTransform( void entityPositionGetLocalTransform(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
mat4 dest mat4 dest
@@ -150,13 +142,11 @@ void entityPositionGetLocalTransform(
* first if ENTITY_POSITION_FLAG_PRS_DIRTY is set; never triggers a matrix * first if ENTITY_POSITION_FLAG_PRS_DIRTY is set; never triggers a matrix
* rebuild or world-transform update. * rebuild or world-transform update.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param dest Destination vector. * @param dest Destination vector.
*/ */
void entityPositionGetLocalPosition( void entityPositionGetLocalPosition(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
@@ -166,13 +156,11 @@ void entityPositionGetLocalPosition(
* Gets the world-space position. For parentless entities this is the same as * Gets the world-space position. For parentless entities this is the same as
* the local position. * the local position.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param dest Destination vector. * @param dest Destination vector.
*/ */
void entityPositionGetWorldPosition( void entityPositionGetWorldPosition(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
@@ -183,13 +171,11 @@ void entityPositionGetWorldPosition(
* to entityPositionSetLocalPosition. For parented entities the position is * to entityPositionSetLocalPosition. For parented entities the position is
* converted to local space via the inverted parent world transform. * converted to local space via the inverted parent world transform.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param position The desired world-space position. * @param position The desired world-space position.
*/ */
void entityPositionSetWorldPosition( void entityPositionSetWorldPosition(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 position vec3 position
@@ -199,13 +185,11 @@ void entityPositionSetWorldPosition(
* Sets the local position, marks localTransform and worldTransform (self + * Sets the local position, marks localTransform and worldTransform (self +
* descendants) dirty. * descendants) dirty.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param position The new local position. * @param position The new local position.
*/ */
void entityPositionSetLocalPosition( void entityPositionSetLocalPosition(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 position vec3 position
@@ -215,13 +199,11 @@ void entityPositionSetLocalPosition(
* Gets the cached local euler rotation (XYZ, radians). Decomposes * Gets the cached local euler rotation (XYZ, radians). Decomposes
* localTransform first if ENTITY_POSITION_FLAG_PRS_DIRTY is set. * localTransform first if ENTITY_POSITION_FLAG_PRS_DIRTY is set.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param dest Destination vector. * @param dest Destination vector.
*/ */
void entityPositionGetLocalRotation( void entityPositionGetLocalRotation(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
@@ -232,13 +214,11 @@ void entityPositionGetLocalRotation(
* world transform. For parentless entities this is the same as local * world transform. For parentless entities this is the same as local
* rotation. * rotation.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param dest Destination vector. * @param dest Destination vector.
*/ */
void entityPositionGetWorldRotation( void entityPositionGetWorldRotation(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
@@ -247,13 +227,11 @@ void entityPositionGetWorldRotation(
/** /**
* Sets the local euler rotation (XYZ, radians) and marks transforms dirty. * Sets the local euler rotation (XYZ, radians) and marks transforms dirty.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param rotation The new local rotation. * @param rotation The new local rotation.
*/ */
void entityPositionSetLocalRotation( void entityPositionSetLocalRotation(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 rotation vec3 rotation
@@ -265,13 +243,11 @@ void entityPositionSetLocalRotation(
* parented entities the rotation is converted to local space by removing * parented entities the rotation is converted to local space by removing
* the parent world rotation. * the parent world rotation.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param rotation The desired world-space euler rotation. * @param rotation The desired world-space euler rotation.
*/ */
void entityPositionSetWorldRotation( void entityPositionSetWorldRotation(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 rotation vec3 rotation
@@ -281,13 +257,11 @@ void entityPositionSetWorldRotation(
* Gets the cached local scale. Decomposes localTransform first if * Gets the cached local scale. Decomposes localTransform first if
* ENTITY_POSITION_FLAG_PRS_DIRTY is set. * ENTITY_POSITION_FLAG_PRS_DIRTY is set.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param dest Destination vector. * @param dest Destination vector.
*/ */
void entityPositionGetLocalScale( void entityPositionGetLocalScale(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
@@ -297,13 +271,11 @@ void entityPositionGetLocalScale(
* Gets the world-space scale by extracting column lengths from the world * Gets the world-space scale by extracting column lengths from the world
* transform. For parentless entities this is the same as local scale. * transform. For parentless entities this is the same as local scale.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param dest Destination vector. * @param dest Destination vector.
*/ */
void entityPositionGetWorldScale( void entityPositionGetWorldScale(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
@@ -312,13 +284,11 @@ void entityPositionGetWorldScale(
/** /**
* Sets the local scale and marks transforms dirty. * Sets the local scale and marks transforms dirty.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param scale The new local scale. * @param scale The new local scale.
*/ */
void entityPositionSetLocalScale( void entityPositionSetLocalScale(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 scale vec3 scale
@@ -329,13 +299,11 @@ void entityPositionSetLocalScale(
* entityPositionSetLocalScale. For parented entities the scale is converted * entityPositionSetLocalScale. For parented entities the scale is converted
* to local space by dividing by the parent world scale (assumes no shear). * to local space by dividing by the parent world scale (assumes no shear).
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param scale The desired world-space scale. * @param scale The desired world-space scale.
*/ */
void entityPositionSetWorldScale( void entityPositionSetWorldScale(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 scale vec3 scale
@@ -345,14 +313,12 @@ void entityPositionSetWorldScale(
* Sets the parent of this entity's position component. * Sets the parent of this entity's position component.
* Pass ENTITY_ID_INVALID / COMPONENT_ID_INVALID to detach from any parent. * Pass ENTITY_ID_INVALID / COMPONENT_ID_INVALID to detach from any parent.
* *
* @param mgr The entity manager that owns both entities.
* @param entityId The child entity ID. * @param entityId The child entity ID.
* @param componentId The child component ID. * @param componentId The child component ID.
* @param parentEntityId The parent entity ID. * @param parentEntityId The parent entity ID.
* @param parentComponentId The parent component ID. * @param parentComponentId The parent component ID.
*/ */
void entityPositionSetParent( void entityPositionSetParent(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const entityid_t parentEntityId, const entityid_t parentEntityId,
@@ -362,13 +328,11 @@ void entityPositionSetParent(
/** /**
* Gets the entity ID of this position component's parent. * Gets the entity ID of this position component's parent.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return The parent entity ID, or ENTITY_ID_INVALID if unparented. * @return The parent entity ID, or ENTITY_ID_INVALID if unparented.
*/ */
entityid_t entityPositionGetParent( entityid_t entityPositionGetParent(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -379,13 +343,11 @@ entityid_t entityPositionGetParent(
* set ENTITY_POSITION_FLAG_WORLD_DIRTY on self and descendants. After * set ENTITY_POSITION_FLAG_WORLD_DIRTY on self and descendants. After
* modifying PRS directly, call entityPositionRebuild() instead. * modifying PRS directly, call entityPositionRebuild() instead.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return Pointer to the component data. * @return Pointer to the component data.
*/ */
entityposition_t *entityPositionGet( entityposition_t *entityPositionGet(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -397,31 +359,27 @@ entityposition_t *entityPositionGet(
* ENTITY_POSITION_FLAG_PRS_DIRTY, propagates ENTITY_POSITION_FLAG_WORLD_DIRTY * ENTITY_POSITION_FLAG_PRS_DIRTY, propagates ENTITY_POSITION_FLAG_WORLD_DIRTY
* to self and all descendants. * to self and all descendants.
* *
* @param mgr The entity manager that owns pos and its descendants.
* @param pos The position component whose PRS was modified. * @param pos The position component whose PRS was modified.
*/ */
void entityPositionRebuild(entitymanager_t *mgr, entityposition_t *pos); void entityPositionRebuild(entityposition_t *pos);
/** /**
* Sets ENTITY_POSITION_FLAG_WORLD_DIRTY on this node and all descendants, * Sets ENTITY_POSITION_FLAG_WORLD_DIRTY on this node and all descendants,
* indicating that worldTransform must be recomputed before it is read. * indicating that worldTransform must be recomputed before it is read.
* Call this after modifying localTransform directly. * Call this after modifying localTransform directly.
* *
* @param mgr The entity manager that owns pos and its descendants.
* @param pos The position component to mark dirty. * @param pos The position component to mark dirty.
*/ */
void entityPositionMarkDirty(entitymanager_t *mgr, entityposition_t *pos); void entityPositionMarkDirty(entityposition_t *pos);
/** /**
* Disposes this entity and all of its position-component descendants * Disposes this entity and all of its position-component descendants
* recursively. Detaches from any parent before destroying. * recursively. Detaches from any parent before destroying.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The root entity ID. * @param entityId The root entity ID.
* @param componentId The root position component ID. * @param componentId The root position component ID.
*/ */
void entityPositionDisposeDeep( void entityPositionDisposeDeep(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -454,7 +412,6 @@ void entityPositionEnsureLocal(entityposition_t *pos);
* Internal. Recomputes worldTransform from the parent chain if * Internal. Recomputes worldTransform from the parent chain if
* ENTITY_POSITION_FLAG_WORLD_DIRTY is set. * ENTITY_POSITION_FLAG_WORLD_DIRTY is set.
* *
* @param mgr The entity manager that owns pos and its ancestors.
* @param pos The position component to update. * @param pos The position component to update.
*/ */
void entityPositionEnsureWorld(entitymanager_t *mgr, entityposition_t *pos); void entityPositionEnsureWorld(entityposition_t *pos);
@@ -6,7 +6,7 @@
*/ */
#include "entityrenderable.h" #include "entityrenderable.h"
#include "entity/entitymanager.h" #include "entity/entity.h"
#include "display/shader/shadermaterial.h" #include "display/shader/shadermaterial.h"
#include "display/shader/shaderunlit.h" #include "display/shader/shaderunlit.h"
#include "display/display.h" #include "display/display.h"
@@ -15,12 +15,11 @@
#include "assert/assert.h" #include "assert/assert.h"
void entityRenderableInit( void entityRenderableInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityrenderable_t *r = componentGetData( entityrenderable_t *r = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_RENDERABLE entityId, componentId, COMPONENT_TYPE_RENDERABLE
); );
memoryZero(r, sizeof(entityrenderable_t)); memoryZero(r, sizeof(entityrenderable_t));
r->type = ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL; r->type = ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL;
@@ -34,44 +33,40 @@ void entityRenderableInit(
} }
void entityRenderableDispose( void entityRenderableDispose(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
} }
void entityRenderableSetType( void entityRenderableSetType(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const entityrenderabletype_t type const entityrenderabletype_t type
) { ) {
entityrenderable_t *r = componentGetData( entityrenderable_t *r = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_RENDERABLE entityId, componentId, COMPONENT_TYPE_RENDERABLE
); );
r->type = type; r->type = type;
} }
void entityRenderableSetPriority( void entityRenderableSetPriority(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const int8_t priority const int8_t priority
) { ) {
entityrenderable_t *r = componentGetData( entityrenderable_t *r = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_RENDERABLE entityId, componentId, COMPONENT_TYPE_RENDERABLE
); );
r->priority = priority; r->priority = priority;
} }
void entityRenderableSetColor( void entityRenderableSetColor(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const color_t color const color_t color
) { ) {
entityrenderable_t *r = componentGetData( entityrenderable_t *r = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_RENDERABLE entityId, componentId, COMPONENT_TYPE_RENDERABLE
); );
assertTrue( assertTrue(
r->type == ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL, r->type == ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL,
@@ -81,7 +76,6 @@ void entityRenderableSetColor(
} }
void entityRenderableSetMesh( void entityRenderableSetMesh(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const uint8_t slot, const uint8_t slot,
@@ -91,7 +85,7 @@ void entityRenderableSetMesh(
assertTrue(slot < ENTITY_RENDERABLE_MESHES_MAX, "Mesh slot out of bounds"); assertTrue(slot < ENTITY_RENDERABLE_MESHES_MAX, "Mesh slot out of bounds");
entityrenderable_t *r = componentGetData( entityrenderable_t *r = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_RENDERABLE entityId, componentId, COMPONENT_TYPE_RENDERABLE
); );
assertTrue( assertTrue(
r->type == ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL, r->type == ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL,
@@ -107,11 +101,9 @@ void entityRenderableSetMesh(
} }
void entityRenderableSetDraw( void entityRenderableSetDraw(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
errorret_t (*draw)( errorret_t (*draw)(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
void *user void *user
@@ -120,7 +112,7 @@ void entityRenderableSetDraw(
) { ) {
assertNotNull(draw, "Draw callback cannot be null"); assertNotNull(draw, "Draw callback cannot be null");
entityrenderable_t *r = componentGetData( entityrenderable_t *r = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_RENDERABLE entityId, componentId, COMPONENT_TYPE_RENDERABLE
); );
r->type = ENTITY_RENDERABLE_TYPE_CUSTOM; r->type = ENTITY_RENDERABLE_TYPE_CUSTOM;
r->data.custom.draw = draw; r->data.custom.draw = draw;
@@ -128,12 +120,11 @@ void entityRenderableSetDraw(
} }
errorret_t entityRenderableDraw( errorret_t entityRenderableDraw(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityrenderable_t *r = componentGetData( entityrenderable_t *r = componentGetData(
mgr, entityId, componentId, COMPONENT_TYPE_RENDERABLE entityId, componentId, COMPONENT_TYPE_RENDERABLE
); );
switch(r->type) { switch(r->type) {
case ENTITY_RENDERABLE_TYPE_SPRITEBATCH: case ENTITY_RENDERABLE_TYPE_SPRITEBATCH:
@@ -142,7 +133,7 @@ errorret_t entityRenderableDraw(
return entityRenderableDrawMaterial(&r->data.material); return entityRenderableDrawMaterial(&r->data.material);
case ENTITY_RENDERABLE_TYPE_CUSTOM: case ENTITY_RENDERABLE_TYPE_CUSTOM:
return entityRenderableDrawCustom( return entityRenderableDrawCustom(
mgr, entityId, componentId, &r->data.custom entityId, componentId, &r->data.custom
); );
default: default:
assertUnreachable("Invalid renderable type"); assertUnreachable("Invalid renderable type");
@@ -183,10 +174,9 @@ errorret_t entityRenderableDrawMaterial(const entityrenderablematerial_t *m) {
} }
errorret_t entityRenderableDrawCustom( errorret_t entityRenderableDrawCustom(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const entityrenderablecustom_t *custom const entityrenderablecustom_t *custom
) { ) {
return custom->draw(mgr, entityId, componentId, custom->drawUser); return custom->draw(entityId, componentId, custom->drawUser);
} }
@@ -40,7 +40,6 @@ typedef struct {
typedef struct { typedef struct {
errorret_t (*draw)( errorret_t (*draw)(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
void *user void *user
@@ -70,12 +69,10 @@ typedef struct {
* ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL with the unlit shader, a white * ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL with the unlit shader, a white
* cube, and depth-test enabled. * cube, and depth-test enabled.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity to initialize the component for. * @param entityId The entity to initialize the component for.
* @param componentId The renderable component of the entity. * @param componentId The renderable component of the entity.
*/ */
void entityRenderableInit( void entityRenderableInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -83,12 +80,10 @@ void entityRenderableInit(
/** /**
* Disposes the entity renderable component. * Disposes the entity renderable component.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity to dispose the component for. * @param entityId The entity to dispose the component for.
* @param componentId The renderable component of the entity. * @param componentId The renderable component of the entity.
*/ */
void entityRenderableDispose( void entityRenderableDispose(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -97,13 +92,11 @@ void entityRenderableDispose(
* Sets the rendering type for the renderable component. Resets * Sets the rendering type for the renderable component. Resets
* type-specific data to zero. * type-specific data to zero.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity to configure. * @param entityId The entity to configure.
* @param componentId The renderable component. * @param componentId The renderable component.
* @param type The rendering type to use. * @param type The rendering type to use.
*/ */
void entityRenderableSetType( void entityRenderableSetType(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const entityrenderabletype_t type const entityrenderabletype_t type
@@ -113,13 +106,11 @@ void entityRenderableSetType(
* Sets the render priority. 0 = auto (derived from type/flags). Higher * Sets the render priority. 0 = auto (derived from type/flags). Higher
* values render later (on top). Use non-zero to force ordering. * values render later (on top). Use non-zero to force ordering.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity to configure. * @param entityId The entity to configure.
* @param componentId The renderable component. * @param componentId The renderable component.
* @param priority The priority value, or 0 for auto. * @param priority The priority value, or 0 for auto.
*/ */
void entityRenderableSetPriority( void entityRenderableSetPriority(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const int8_t priority const int8_t priority
@@ -130,13 +121,11 @@ void entityRenderableSetPriority(
* (or defaults to) ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL -- asserts * (or defaults to) ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL -- asserts
* otherwise. * otherwise.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity to configure. * @param entityId The entity to configure.
* @param componentId The renderable component. * @param componentId The renderable component.
* @param color The color to tint the material with. * @param color The color to tint the material with.
*/ */
void entityRenderableSetColor( void entityRenderableSetColor(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const color_t color const color_t color
@@ -147,7 +136,6 @@ void entityRenderableSetColor(
* count override). Only meaningful when the renderable is (or defaults * count override). Only meaningful when the renderable is (or defaults
* to) ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL -- asserts otherwise. * to) ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL -- asserts otherwise.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity to configure. * @param entityId The entity to configure.
* @param componentId The renderable component. * @param componentId The renderable component.
* @param slot Index into the material's meshes array (0 to * @param slot Index into the material's meshes array (0 to
@@ -155,7 +143,6 @@ void entityRenderableSetColor(
* @param mesh The mesh to draw in that slot. * @param mesh The mesh to draw in that slot.
*/ */
void entityRenderableSetMesh( void entityRenderableSetMesh(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const uint8_t slot, const uint8_t slot,
@@ -166,18 +153,15 @@ void entityRenderableSetMesh(
* Sets the draw callback, switching the type to * Sets the draw callback, switching the type to
* ENTITY_RENDERABLE_TYPE_CUSTOM. * ENTITY_RENDERABLE_TYPE_CUSTOM.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity to configure. * @param entityId The entity to configure.
* @param componentId The renderable component of the entity. * @param componentId The renderable component of the entity.
* @param draw The draw callback to assign. * @param draw The draw callback to assign.
* @param user Userdata passed to the callback. * @param user Userdata passed to the callback.
*/ */
void entityRenderableSetDraw( void entityRenderableSetDraw(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
errorret_t (*draw)( errorret_t (*draw)(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
void *user void *user
@@ -188,13 +172,11 @@ void entityRenderableSetDraw(
/** /**
* Draws the entity using its renderable component data. * Draws the entity using its renderable component data.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity to draw. * @param entityId The entity to draw.
* @param componentId The renderable component of the entity. * @param componentId The renderable component of the entity.
* @return Any error state that happened. * @return Any error state that happened.
*/ */
errorret_t entityRenderableDraw( errorret_t entityRenderableDraw(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -220,14 +202,12 @@ errorret_t entityRenderableDrawMaterial(const entityrenderablematerial_t *m);
/** /**
* Internal. Invokes a custom-type renderable's draw callback. * Internal. Invokes a custom-type renderable's draw callback.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity being drawn. * @param entityId The entity being drawn.
* @param componentId The renderable component of the entity. * @param componentId The renderable component of the entity.
* @param custom The custom draw data. * @param custom The custom draw data.
* @return Error state. * @return Error state.
*/ */
errorret_t entityRenderableDrawCustom( errorret_t entityRenderableDrawCustom(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const entityrenderablecustom_t *custom const entityrenderablecustom_t *custom
@@ -6,15 +6,14 @@
*/ */
#include "entityphysics.h" #include "entityphysics.h"
#include "entity/entitymanager.h" #include "entity/entity.h"
#include "util/memory.h" #include "util/memory.h"
void entityPhysicsInit( void entityPhysicsInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityphysics_t *phys = entityPhysicsGet(mgr, entityId, componentId); entityphysics_t *phys = entityPhysicsGet(entityId, componentId);
memoryZero(phys, sizeof(entityphysics_t)); memoryZero(phys, sizeof(entityphysics_t));
@@ -30,106 +29,95 @@ void entityPhysicsInit(
} }
entityphysics_t *entityPhysicsGet( entityphysics_t *entityPhysicsGet(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
return componentGetData(mgr, entityId, componentId, COMPONENT_TYPE_PHYSICS); return componentGetData(entityId, componentId, COMPONENT_TYPE_PHYSICS);
} }
void entityPhysicsSetShape( void entityPhysicsSetShape(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const physicsshape_t shape const physicsshape_t shape
) { ) {
entityphysics_t *phys = entityPhysicsGet(mgr, entityId, componentId); entityphysics_t *phys = entityPhysicsGet(entityId, componentId);
phys->shape = shape; phys->shape = shape;
} }
physicsshape_t entityPhysicsGetShape( physicsshape_t entityPhysicsGetShape(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityphysics_t *phys = entityPhysicsGet(mgr, entityId, componentId); entityphysics_t *phys = entityPhysicsGet(entityId, componentId);
return phys->shape; return phys->shape;
} }
void entityPhysicsGetVelocity( void entityPhysicsGetVelocity(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
) { ) {
entityphysics_t *phys = entityPhysicsGet(mgr, entityId, componentId); entityphysics_t *phys = entityPhysicsGet(entityId, componentId);
glm_vec3_copy(phys->velocity, dest); glm_vec3_copy(phys->velocity, dest);
} }
void entityPhysicsSetVelocity( void entityPhysicsSetVelocity(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 velocity vec3 velocity
) { ) {
entityphysics_t *phys = entityPhysicsGet(mgr, entityId, componentId); entityphysics_t *phys = entityPhysicsGet(entityId, componentId);
glm_vec3_copy(velocity, phys->velocity); glm_vec3_copy(velocity, phys->velocity);
} }
void entityPhysicsApplyImpulse( void entityPhysicsApplyImpulse(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 impulse vec3 impulse
) { ) {
entityphysics_t *phys = entityPhysicsGet(mgr, entityId, componentId); entityphysics_t *phys = entityPhysicsGet(entityId, componentId);
if(phys->type == PHYSICS_BODY_STATIC) return; if(phys->type == PHYSICS_BODY_STATIC) return;
glm_vec3_add(phys->velocity, impulse, phys->velocity); glm_vec3_add(phys->velocity, impulse, phys->velocity);
} }
bool_t entityPhysicsIsOnGround( bool_t entityPhysicsIsOnGround(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityphysics_t *phys = entityPhysicsGet(mgr, entityId, componentId); entityphysics_t *phys = entityPhysicsGet(entityId, componentId);
return phys->onGround; return phys->onGround;
} }
void entityPhysicsSetBodyType( void entityPhysicsSetBodyType(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const physicsbodytype_t type const physicsbodytype_t type
) { ) {
entityphysics_t *phys = entityPhysicsGet(mgr, entityId, componentId); entityphysics_t *phys = entityPhysicsGet(entityId, componentId);
phys->type = type; phys->type = type;
} }
physicsbodytype_t entityPhysicsGetBodyType( physicsbodytype_t entityPhysicsGetBodyType(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityphysics_t *phys = entityPhysicsGet(mgr, entityId, componentId); entityphysics_t *phys = entityPhysicsGet(entityId, componentId);
return phys->type; return phys->type;
} }
void entityPhysicsSetCollideMask( void entityPhysicsSetCollideMask(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const uint32_t mask const uint32_t mask
) { ) {
entityphysics_t *phys = entityPhysicsGet(mgr, entityId, componentId); entityphysics_t *phys = entityPhysicsGet(entityId, componentId);
phys->collideMask = mask; phys->collideMask = mask;
} }
uint32_t entityPhysicsGetCollideMask( uint32_t entityPhysicsGetCollideMask(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entityphysics_t *phys = entityPhysicsGet(mgr, entityId, componentId); entityphysics_t *phys = entityPhysicsGet(entityId, componentId);
return phys->collideMask; return phys->collideMask;
} }
@@ -31,12 +31,10 @@ typedef struct {
* Initializes the physics component: defaults to a dynamic 1x1x1 cube * Initializes the physics component: defaults to a dynamic 1x1x1 cube
* body, zero velocity, unit gravity scale, and onGround false. * body, zero velocity, unit gravity scale, and onGround false.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
*/ */
void entityPhysicsInit( void entityPhysicsInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -46,13 +44,11 @@ void entityPhysicsInit(
* This is really just intended for doing operations faster than using the * This is really just intended for doing operations faster than using the
* getters and setters, but it is preferred that you use those. * getters and setters, but it is preferred that you use those.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return The physics component data for the given entity and component ID. * @return The physics component data for the given entity and component ID.
*/ */
entityphysics_t *entityPhysicsGet( entityphysics_t *entityPhysicsGet(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -62,13 +58,11 @@ entityphysics_t *entityPhysicsGet(
* state, so if you change from a cube to a sphere, it will keep the same * state, so if you change from a cube to a sphere, it will keep the same
* velocity and onGround state. * velocity and onGround state.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param shape The new shape to set on the physics body. * @param shape The new shape to set on the physics body.
*/ */
void entityPhysicsSetShape( void entityPhysicsSetShape(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const physicsshape_t shape const physicsshape_t shape
@@ -77,13 +71,11 @@ void entityPhysicsSetShape(
/** /**
* Gets the shape of the entity's physics body. * Gets the shape of the entity's physics body.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return The shape of the physics body. * @return The shape of the physics body.
*/ */
physicsshape_t entityPhysicsGetShape( physicsshape_t entityPhysicsGetShape(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -91,13 +83,11 @@ physicsshape_t entityPhysicsGetShape(
/** /**
* Gets the velocity of the entity's physics body. * Gets the velocity of the entity's physics body.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param dest The destination vec3 to write the velocity to. * @param dest The destination vec3 to write the velocity to.
*/ */
void entityPhysicsGetVelocity( void entityPhysicsGetVelocity(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 dest vec3 dest
@@ -107,13 +97,11 @@ void entityPhysicsGetVelocity(
* Sets the velocity of the entity's physics body. This is not an impulse, so * Sets the velocity of the entity's physics body. This is not an impulse, so
* it will be affected by mass and drag. * it will be affected by mass and drag.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param velocity The new velocity to set on the physics body. * @param velocity The new velocity to set on the physics body.
*/ */
void entityPhysicsSetVelocity( void entityPhysicsSetVelocity(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 velocity vec3 velocity
@@ -124,13 +112,11 @@ void entityPhysicsSetVelocity(
* velocity change that is not affected by mass or drag. No-op on STATIC * velocity change that is not affected by mass or drag. No-op on STATIC
* bodies. * bodies.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param impulse The impulse to apply to the physics body. * @param impulse The impulse to apply to the physics body.
*/ */
void entityPhysicsApplyImpulse( void entityPhysicsApplyImpulse(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
vec3 impulse vec3 impulse
@@ -140,13 +126,11 @@ void entityPhysicsApplyImpulse(
* Returns true if the entity's physics body rested on a surface during the * Returns true if the entity's physics body rested on a surface during the
* last physicsWorldStep. * last physicsWorldStep.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return True if the body is on the ground, false otherwise. * @return True if the body is on the ground, false otherwise.
*/ */
bool_t entityPhysicsIsOnGround( bool_t entityPhysicsIsOnGround(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -154,13 +138,11 @@ bool_t entityPhysicsIsOnGround(
/** /**
* Sets the body type of the entity's physics body. * Sets the body type of the entity's physics body.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param type The body type to set. * @param type The body type to set.
*/ */
void entityPhysicsSetBodyType( void entityPhysicsSetBodyType(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const physicsbodytype_t type const physicsbodytype_t type
@@ -169,13 +151,11 @@ void entityPhysicsSetBodyType(
/** /**
* Gets the body type of the entity's physics body. * Gets the body type of the entity's physics body.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return The body type of the physics body. * @return The body type of the physics body.
*/ */
physicsbodytype_t entityPhysicsGetBodyType( physicsbodytype_t entityPhysicsGetBodyType(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -184,13 +164,11 @@ physicsbodytype_t entityPhysicsGetBodyType(
* Sets the collision mask of the entity's physics body. Two bodies only * Sets the collision mask of the entity's physics body. Two bodies only
* collide if their masks share at least one set bit. * collide if their masks share at least one set bit.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param mask The new collision mask to set on the physics body. * @param mask The new collision mask to set on the physics body.
*/ */
void entityPhysicsSetCollideMask( void entityPhysicsSetCollideMask(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const uint32_t mask const uint32_t mask
@@ -199,13 +177,11 @@ void entityPhysicsSetCollideMask(
/** /**
* Gets the collision mask of the entity's physics body. * Gets the collision mask of the entity's physics body.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return The collision mask of the physics body. * @return The collision mask of the physics body.
*/ */
uint32_t entityPhysicsGetCollideMask( uint32_t entityPhysicsGetCollideMask(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -6,15 +6,14 @@
*/ */
#include "entitytrigger.h" #include "entitytrigger.h"
#include "entity/entitymanager.h" #include "entity/entity.h"
#include "util/memory.h" #include "util/memory.h"
void entityTriggerInit( void entityTriggerInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
memoryZero(trig, sizeof(entitytrigger_t)); memoryZero(trig, sizeof(entitytrigger_t));
@@ -26,78 +25,70 @@ void entityTriggerInit(
} }
entitytrigger_t *entityTriggerGet( entitytrigger_t *entityTriggerGet(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
return componentGetData(mgr, entityId, componentId, COMPONENT_TYPE_TRIGGER); return componentGetData(entityId, componentId, COMPONENT_TYPE_TRIGGER);
} }
void entityTriggerSetShape( void entityTriggerSetShape(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const physicsshape_t shape const physicsshape_t shape
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
trig->shape = shape; trig->shape = shape;
} }
physicsshape_t entityTriggerGetShape( physicsshape_t entityTriggerGetShape(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
return trig->shape; return trig->shape;
} }
void entityTriggerSetCollideMask( void entityTriggerSetCollideMask(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const uint32_t mask const uint32_t mask
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
trig->collideMask = mask; trig->collideMask = mask;
} }
uint32_t entityTriggerGetCollideMask( uint32_t entityTriggerGetCollideMask(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
return trig->collideMask; return trig->collideMask;
} }
uint8_t entityTriggerGetOccupantCount( uint8_t entityTriggerGetOccupantCount(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
return trig->occupantCount; return trig->occupantCount;
} }
entityid_t entityTriggerGetOccupantEntityId( entityid_t entityTriggerGetOccupantEntityId(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const uint8_t index const uint8_t index
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
if(index >= trig->occupantCount) return ENTITY_ID_INVALID; if(index >= trig->occupantCount) return ENTITY_ID_INVALID;
return trig->occupants[index].entityId; return trig->occupants[index].entityId;
} }
bool_t entityTriggerIsOccupyingEntity( bool_t entityTriggerIsOccupyingEntity(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const entityid_t otherEntityId const entityid_t otherEntityId
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
for(uint8_t i = 0; i < trig->occupantCount; i++) { for(uint8_t i = 0; i < trig->occupantCount; i++) {
if(trig->occupants[i].entityId == otherEntityId) return true; if(trig->occupants[i].entityId == otherEntityId) return true;
} }
@@ -20,7 +20,6 @@
* Callback invoked for a trigger's onEnter/onActive/onMove/onLeave events. * Callback invoked for a trigger's onEnter/onActive/onMove/onLeave events.
* See entityTriggerOnEnterAdd() and its onActive/onMove/onLeave siblings. * See entityTriggerOnEnterAdd() and its onActive/onMove/onLeave siblings.
* *
* @param mgr The entity manager that owns both entities.
* @param triggerEntityId The entity ID of the trigger volume. * @param triggerEntityId The entity ID of the trigger volume.
* @param triggerComponentId The TRIGGER component ID. * @param triggerComponentId The TRIGGER component ID.
* @param otherEntityId The entity ID that entered/is inside/left the * @param otherEntityId The entity ID that entered/is inside/left the
@@ -29,7 +28,6 @@
* @param user The user pointer passed to the matching *Add() call. * @param user The user pointer passed to the matching *Add() call.
*/ */
typedef void (*triggercallback_t)( typedef void (*triggercallback_t)(
entitymanager_t *mgr,
const entityid_t triggerEntityId, const entityid_t triggerEntityId,
const componentid_t triggerComponentId, const componentid_t triggerComponentId,
const entityid_t otherEntityId, const entityid_t otherEntityId,
@@ -93,12 +91,10 @@ typedef struct {
* Initializes the trigger component: defaults to a 1x1x1 cube volume, no * Initializes the trigger component: defaults to a 1x1x1 cube volume, no
* tracked occupants, and no event subscribers. * tracked occupants, and no event subscribers.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
*/ */
void entityTriggerInit( void entityTriggerInit(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -107,13 +103,11 @@ void entityTriggerInit(
* Gets the underlying trigger structure (temporarily) for the given * Gets the underlying trigger structure (temporarily) for the given
* entity. Prefer the dedicated getters/setters where possible. * entity. Prefer the dedicated getters/setters where possible.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return The trigger component data for the given entity and component ID. * @return The trigger component data for the given entity and component ID.
*/ */
entitytrigger_t *entityTriggerGet( entitytrigger_t *entityTriggerGet(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -121,13 +115,11 @@ entitytrigger_t *entityTriggerGet(
/** /**
* Sets the shape of the trigger volume. * Sets the shape of the trigger volume.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param shape The new shape to set on the trigger volume. * @param shape The new shape to set on the trigger volume.
*/ */
void entityTriggerSetShape( void entityTriggerSetShape(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const physicsshape_t shape const physicsshape_t shape
@@ -136,13 +128,11 @@ void entityTriggerSetShape(
/** /**
* Gets the shape of the trigger volume. * Gets the shape of the trigger volume.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return The shape of the trigger volume. * @return The shape of the trigger volume.
*/ */
physicsshape_t entityTriggerGetShape( physicsshape_t entityTriggerGetShape(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -151,13 +141,11 @@ physicsshape_t entityTriggerGetShape(
* Sets the collision mask of the trigger volume. An entity's PHYSICS body * Sets the collision mask of the trigger volume. An entity's PHYSICS body
* is only detected if their masks share at least one set bit. * is only detected if their masks share at least one set bit.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param mask The new collision mask to set on the trigger volume. * @param mask The new collision mask to set on the trigger volume.
*/ */
void entityTriggerSetCollideMask( void entityTriggerSetCollideMask(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const uint32_t mask const uint32_t mask
@@ -166,13 +154,11 @@ void entityTriggerSetCollideMask(
/** /**
* Gets the collision mask of the trigger volume. * Gets the collision mask of the trigger volume.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return The collision mask of the trigger volume. * @return The collision mask of the trigger volume.
*/ */
uint32_t entityTriggerGetCollideMask( uint32_t entityTriggerGetCollideMask(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -180,13 +166,11 @@ uint32_t entityTriggerGetCollideMask(
/** /**
* Gets the number of entities currently overlapping this trigger. * Gets the number of entities currently overlapping this trigger.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @return The number of currently tracked occupants. * @return The number of currently tracked occupants.
*/ */
uint8_t entityTriggerGetOccupantCount( uint8_t entityTriggerGetOccupantCount(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId const componentid_t componentId
); );
@@ -195,7 +179,6 @@ uint8_t entityTriggerGetOccupantCount(
* Gets the entity ID of the occupant at the given index, in [0, * Gets the entity ID of the occupant at the given index, in [0,
* entityTriggerGetOccupantCount()). Order is not stable across steps. * entityTriggerGetOccupantCount()). Order is not stable across steps.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param index The occupant index. * @param index The occupant index.
@@ -203,7 +186,6 @@ uint8_t entityTriggerGetOccupantCount(
* of range. * of range.
*/ */
entityid_t entityTriggerGetOccupantEntityId( entityid_t entityTriggerGetOccupantEntityId(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const uint8_t index const uint8_t index
@@ -212,14 +194,12 @@ entityid_t entityTriggerGetOccupantEntityId(
/** /**
* Checks whether a specific entity is currently overlapping this trigger. * Checks whether a specific entity is currently overlapping this trigger.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param otherEntityId The entity ID to check for. * @param otherEntityId The entity ID to check for.
* @return True if otherEntityId is a currently tracked occupant. * @return True if otherEntityId is a currently tracked occupant.
*/ */
bool_t entityTriggerIsOccupyingEntity( bool_t entityTriggerIsOccupyingEntity(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const entityid_t otherEntityId const entityid_t otherEntityId
@@ -9,13 +9,12 @@
#include "assert/assert.h" #include "assert/assert.h"
void entityTriggerOnEnterAdd( void entityTriggerOnEnterAdd(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback, const triggercallback_t callback,
void *user void *user
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
assertTrue( assertTrue(
trig->onEnterCount < ENTITY_TRIGGER_CALLBACK_COUNT_MAX, trig->onEnterCount < ENTITY_TRIGGER_CALLBACK_COUNT_MAX,
"Trigger onEnter callback slots full" "Trigger onEnter callback slots full"
@@ -26,12 +25,11 @@ void entityTriggerOnEnterAdd(
} }
void entityTriggerOnEnterRemove( void entityTriggerOnEnterRemove(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback const triggercallback_t callback
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
for(uint8_t i = 0; i < trig->onEnterCount; i++) { for(uint8_t i = 0; i < trig->onEnterCount; i++) {
if(trig->onEnter[i] != callback) continue; if(trig->onEnter[i] != callback) continue;
trig->onEnterCount--; trig->onEnterCount--;
@@ -44,13 +42,12 @@ void entityTriggerOnEnterRemove(
} }
void entityTriggerOnActiveAdd( void entityTriggerOnActiveAdd(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback, const triggercallback_t callback,
void *user void *user
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
assertTrue( assertTrue(
trig->onActiveCount < ENTITY_TRIGGER_CALLBACK_COUNT_MAX, trig->onActiveCount < ENTITY_TRIGGER_CALLBACK_COUNT_MAX,
"Trigger onActive callback slots full" "Trigger onActive callback slots full"
@@ -61,12 +58,11 @@ void entityTriggerOnActiveAdd(
} }
void entityTriggerOnActiveRemove( void entityTriggerOnActiveRemove(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback const triggercallback_t callback
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
for(uint8_t i = 0; i < trig->onActiveCount; i++) { for(uint8_t i = 0; i < trig->onActiveCount; i++) {
if(trig->onActive[i] != callback) continue; if(trig->onActive[i] != callback) continue;
trig->onActiveCount--; trig->onActiveCount--;
@@ -79,13 +75,12 @@ void entityTriggerOnActiveRemove(
} }
void entityTriggerOnMoveAdd( void entityTriggerOnMoveAdd(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback, const triggercallback_t callback,
void *user void *user
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
assertTrue( assertTrue(
trig->onMoveCount < ENTITY_TRIGGER_CALLBACK_COUNT_MAX, trig->onMoveCount < ENTITY_TRIGGER_CALLBACK_COUNT_MAX,
"Trigger onMove callback slots full" "Trigger onMove callback slots full"
@@ -96,12 +91,11 @@ void entityTriggerOnMoveAdd(
} }
void entityTriggerOnMoveRemove( void entityTriggerOnMoveRemove(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback const triggercallback_t callback
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
for(uint8_t i = 0; i < trig->onMoveCount; i++) { for(uint8_t i = 0; i < trig->onMoveCount; i++) {
if(trig->onMove[i] != callback) continue; if(trig->onMove[i] != callback) continue;
trig->onMoveCount--; trig->onMoveCount--;
@@ -114,13 +108,12 @@ void entityTriggerOnMoveRemove(
} }
void entityTriggerOnLeaveAdd( void entityTriggerOnLeaveAdd(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback, const triggercallback_t callback,
void *user void *user
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
assertTrue( assertTrue(
trig->onLeaveCount < ENTITY_TRIGGER_CALLBACK_COUNT_MAX, trig->onLeaveCount < ENTITY_TRIGGER_CALLBACK_COUNT_MAX,
"Trigger onLeave callback slots full" "Trigger onLeave callback slots full"
@@ -131,12 +124,11 @@ void entityTriggerOnLeaveAdd(
} }
void entityTriggerOnLeaveRemove( void entityTriggerOnLeaveRemove(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback const triggercallback_t callback
) { ) {
entitytrigger_t *trig = entityTriggerGet(mgr, entityId, componentId); entitytrigger_t *trig = entityTriggerGet(entityId, componentId);
for(uint8_t i = 0; i < trig->onLeaveCount; i++) { for(uint8_t i = 0; i < trig->onLeaveCount; i++) {
if(trig->onLeave[i] != callback) continue; if(trig->onLeave[i] != callback) continue;
trig->onLeaveCount--; trig->onLeaveCount--;
@@ -149,72 +141,68 @@ void entityTriggerOnLeaveRemove(
} }
void entityTriggerFireEnter( void entityTriggerFireEnter(
entitymanager_t *mgr,
const entityid_t triggerEntityId, const entityid_t triggerEntityId,
const componentid_t triggerComponentId, const componentid_t triggerComponentId,
const entityid_t otherEntityId, const entityid_t otherEntityId,
const componentid_t otherComponentId const componentid_t otherComponentId
) { ) {
entitytrigger_t *trig = entityTriggerGet( entitytrigger_t *trig = entityTriggerGet(
mgr, triggerEntityId, triggerComponentId triggerEntityId, triggerComponentId
); );
for(uint8_t i = 0; i < trig->onEnterCount; i++) { for(uint8_t i = 0; i < trig->onEnterCount; i++) {
trig->onEnter[i]( trig->onEnter[i](
mgr, triggerEntityId, triggerComponentId, triggerEntityId, triggerComponentId,
otherEntityId, otherComponentId, trig->onEnterUser[i] otherEntityId, otherComponentId, trig->onEnterUser[i]
); );
} }
} }
void entityTriggerFireActive( void entityTriggerFireActive(
entitymanager_t *mgr,
const entityid_t triggerEntityId, const entityid_t triggerEntityId,
const componentid_t triggerComponentId, const componentid_t triggerComponentId,
const entityid_t otherEntityId, const entityid_t otherEntityId,
const componentid_t otherComponentId const componentid_t otherComponentId
) { ) {
entitytrigger_t *trig = entityTriggerGet( entitytrigger_t *trig = entityTriggerGet(
mgr, triggerEntityId, triggerComponentId triggerEntityId, triggerComponentId
); );
for(uint8_t i = 0; i < trig->onActiveCount; i++) { for(uint8_t i = 0; i < trig->onActiveCount; i++) {
trig->onActive[i]( trig->onActive[i](
mgr, triggerEntityId, triggerComponentId, triggerEntityId, triggerComponentId,
otherEntityId, otherComponentId, trig->onActiveUser[i] otherEntityId, otherComponentId, trig->onActiveUser[i]
); );
} }
} }
void entityTriggerFireMove( void entityTriggerFireMove(
entitymanager_t *mgr,
const entityid_t triggerEntityId, const entityid_t triggerEntityId,
const componentid_t triggerComponentId, const componentid_t triggerComponentId,
const entityid_t otherEntityId, const entityid_t otherEntityId,
const componentid_t otherComponentId const componentid_t otherComponentId
) { ) {
entitytrigger_t *trig = entityTriggerGet( entitytrigger_t *trig = entityTriggerGet(
mgr, triggerEntityId, triggerComponentId triggerEntityId, triggerComponentId
); );
for(uint8_t i = 0; i < trig->onMoveCount; i++) { for(uint8_t i = 0; i < trig->onMoveCount; i++) {
trig->onMove[i]( trig->onMove[i](
mgr, triggerEntityId, triggerComponentId, triggerEntityId, triggerComponentId,
otherEntityId, otherComponentId, trig->onMoveUser[i] otherEntityId, otherComponentId, trig->onMoveUser[i]
); );
} }
} }
void entityTriggerFireLeave( void entityTriggerFireLeave(
entitymanager_t *mgr,
const entityid_t triggerEntityId, const entityid_t triggerEntityId,
const componentid_t triggerComponentId, const componentid_t triggerComponentId,
const entityid_t otherEntityId, const entityid_t otherEntityId,
const componentid_t otherComponentId const componentid_t otherComponentId
) { ) {
entitytrigger_t *trig = entityTriggerGet( entitytrigger_t *trig = entityTriggerGet(
mgr, triggerEntityId, triggerComponentId triggerEntityId, triggerComponentId
); );
for(uint8_t i = 0; i < trig->onLeaveCount; i++) { for(uint8_t i = 0; i < trig->onLeaveCount; i++) {
trig->onLeave[i]( trig->onLeave[i](
mgr, triggerEntityId, triggerComponentId, triggerEntityId, triggerComponentId,
otherEntityId, otherComponentId, trig->onLeaveUser[i] otherEntityId, otherComponentId, trig->onLeaveUser[i]
); );
} }
@@ -12,14 +12,12 @@
* Subscribes a callback to a trigger's onEnter event, fired the step an * Subscribes a callback to a trigger's onEnter event, fired the step an
* entity begins overlapping the trigger volume. * entity begins overlapping the trigger volume.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param callback The callback to invoke. * @param callback The callback to invoke.
* @param user Arbitrary pointer forwarded to the callback unchanged. * @param user Arbitrary pointer forwarded to the callback unchanged.
*/ */
void entityTriggerOnEnterAdd( void entityTriggerOnEnterAdd(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback, const triggercallback_t callback,
@@ -30,13 +28,11 @@ void entityTriggerOnEnterAdd(
* Unsubscribes a previously added onEnter callback. No-op if not * Unsubscribes a previously added onEnter callback. No-op if not
* currently subscribed. * currently subscribed.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param callback The callback that was passed to entityTriggerOnEnterAdd(). * @param callback The callback that was passed to entityTriggerOnEnterAdd().
*/ */
void entityTriggerOnEnterRemove( void entityTriggerOnEnterRemove(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback const triggercallback_t callback
@@ -47,14 +43,12 @@ void entityTriggerOnEnterRemove(
* entity continues overlapping the trigger volume (starting the step * entity continues overlapping the trigger volume (starting the step
* after onEnter fired). * after onEnter fired).
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param callback The callback to invoke. * @param callback The callback to invoke.
* @param user Arbitrary pointer forwarded to the callback unchanged. * @param user Arbitrary pointer forwarded to the callback unchanged.
*/ */
void entityTriggerOnActiveAdd( void entityTriggerOnActiveAdd(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback, const triggercallback_t callback,
@@ -65,13 +59,11 @@ void entityTriggerOnActiveAdd(
* Unsubscribes a previously added onActive callback. No-op if not * Unsubscribes a previously added onActive callback. No-op if not
* currently subscribed. * currently subscribed.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param callback The callback that was passed to entityTriggerOnActiveAdd(). * @param callback The callback that was passed to entityTriggerOnActiveAdd().
*/ */
void entityTriggerOnActiveRemove( void entityTriggerOnActiveRemove(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback const triggercallback_t callback
@@ -82,14 +74,12 @@ void entityTriggerOnActiveRemove(
* onActive on any step where the overlapping entity's position changed * onActive on any step where the overlapping entity's position changed
* since the previous step. * since the previous step.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param callback The callback to invoke. * @param callback The callback to invoke.
* @param user Arbitrary pointer forwarded to the callback unchanged. * @param user Arbitrary pointer forwarded to the callback unchanged.
*/ */
void entityTriggerOnMoveAdd( void entityTriggerOnMoveAdd(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback, const triggercallback_t callback,
@@ -100,13 +90,11 @@ void entityTriggerOnMoveAdd(
* Unsubscribes a previously added onMove callback. No-op if not currently * Unsubscribes a previously added onMove callback. No-op if not currently
* subscribed. * subscribed.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param callback The callback that was passed to entityTriggerOnMoveAdd(). * @param callback The callback that was passed to entityTriggerOnMoveAdd().
*/ */
void entityTriggerOnMoveRemove( void entityTriggerOnMoveRemove(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback const triggercallback_t callback
@@ -116,14 +104,12 @@ void entityTriggerOnMoveRemove(
* Subscribes a callback to a trigger's onLeave event, fired the step an * Subscribes a callback to a trigger's onLeave event, fired the step an
* entity stops overlapping the trigger volume. * entity stops overlapping the trigger volume.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param callback The callback to invoke. * @param callback The callback to invoke.
* @param user Arbitrary pointer forwarded to the callback unchanged. * @param user Arbitrary pointer forwarded to the callback unchanged.
*/ */
void entityTriggerOnLeaveAdd( void entityTriggerOnLeaveAdd(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback, const triggercallback_t callback,
@@ -134,13 +120,11 @@ void entityTriggerOnLeaveAdd(
* Unsubscribes a previously added onLeave callback. No-op if not * Unsubscribes a previously added onLeave callback. No-op if not
* currently subscribed. * currently subscribed.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity ID. * @param entityId The entity ID.
* @param componentId The component ID. * @param componentId The component ID.
* @param callback The callback that was passed to entityTriggerOnLeaveAdd(). * @param callback The callback that was passed to entityTriggerOnLeaveAdd().
*/ */
void entityTriggerOnLeaveRemove( void entityTriggerOnLeaveRemove(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
const triggercallback_t callback const triggercallback_t callback
@@ -150,14 +134,12 @@ void entityTriggerOnLeaveRemove(
* Internal. Invokes every subscribed onEnter callback for a trigger. * Internal. Invokes every subscribed onEnter callback for a trigger.
* Called by triggerSystemStep() when an entity begins overlapping. * Called by triggerSystemStep() when an entity begins overlapping.
* *
* @param mgr The entity manager that owns both entities.
* @param triggerEntityId The entity ID of the trigger volume. * @param triggerEntityId The entity ID of the trigger volume.
* @param triggerComponentId The TRIGGER component ID. * @param triggerComponentId The TRIGGER component ID.
* @param otherEntityId The entity ID that entered the trigger volume. * @param otherEntityId The entity ID that entered the trigger volume.
* @param otherComponentId The other entity's POSITION component ID. * @param otherComponentId The other entity's POSITION component ID.
*/ */
void entityTriggerFireEnter( void entityTriggerFireEnter(
entitymanager_t *mgr,
const entityid_t triggerEntityId, const entityid_t triggerEntityId,
const componentid_t triggerComponentId, const componentid_t triggerComponentId,
const entityid_t otherEntityId, const entityid_t otherEntityId,
@@ -168,14 +150,12 @@ void entityTriggerFireEnter(
* Internal. Invokes every subscribed onActive callback for a trigger. * Internal. Invokes every subscribed onActive callback for a trigger.
* Called by triggerSystemStep() for every entity still overlapping. * Called by triggerSystemStep() for every entity still overlapping.
* *
* @param mgr The entity manager that owns both entities.
* @param triggerEntityId The entity ID of the trigger volume. * @param triggerEntityId The entity ID of the trigger volume.
* @param triggerComponentId The TRIGGER component ID. * @param triggerComponentId The TRIGGER component ID.
* @param otherEntityId The entity ID still inside the trigger volume. * @param otherEntityId The entity ID still inside the trigger volume.
* @param otherComponentId The other entity's POSITION component ID. * @param otherComponentId The other entity's POSITION component ID.
*/ */
void entityTriggerFireActive( void entityTriggerFireActive(
entitymanager_t *mgr,
const entityid_t triggerEntityId, const entityid_t triggerEntityId,
const componentid_t triggerComponentId, const componentid_t triggerComponentId,
const entityid_t otherEntityId, const entityid_t otherEntityId,
@@ -187,14 +167,12 @@ void entityTriggerFireActive(
* Called by triggerSystemStep() when an overlapping entity's position * Called by triggerSystemStep() when an overlapping entity's position
* changed since the previous step. * changed since the previous step.
* *
* @param mgr The entity manager that owns both entities.
* @param triggerEntityId The entity ID of the trigger volume. * @param triggerEntityId The entity ID of the trigger volume.
* @param triggerComponentId The TRIGGER component ID. * @param triggerComponentId The TRIGGER component ID.
* @param otherEntityId The entity ID that moved within the trigger volume. * @param otherEntityId The entity ID that moved within the trigger volume.
* @param otherComponentId The other entity's POSITION component ID. * @param otherComponentId The other entity's POSITION component ID.
*/ */
void entityTriggerFireMove( void entityTriggerFireMove(
entitymanager_t *mgr,
const entityid_t triggerEntityId, const entityid_t triggerEntityId,
const componentid_t triggerComponentId, const componentid_t triggerComponentId,
const entityid_t otherEntityId, const entityid_t otherEntityId,
@@ -205,14 +183,12 @@ void entityTriggerFireMove(
* Internal. Invokes every subscribed onLeave callback for a trigger. * Internal. Invokes every subscribed onLeave callback for a trigger.
* Called by triggerSystemStep() when an entity stops overlapping. * Called by triggerSystemStep() when an entity stops overlapping.
* *
* @param mgr The entity manager that owns both entities.
* @param triggerEntityId The entity ID of the trigger volume. * @param triggerEntityId The entity ID of the trigger volume.
* @param triggerComponentId The TRIGGER component ID. * @param triggerComponentId The TRIGGER component ID.
* @param otherEntityId The entity ID that left the trigger volume. * @param otherEntityId The entity ID that left the trigger volume.
* @param otherComponentId The other entity's POSITION component ID. * @param otherComponentId The other entity's POSITION component ID.
*/ */
void entityTriggerFireLeave( void entityTriggerFireLeave(
entitymanager_t *mgr,
const entityid_t triggerEntityId, const entityid_t triggerEntityId,
const componentid_t triggerComponentId, const componentid_t triggerComponentId,
const entityid_t otherEntityId, const entityid_t otherEntityId,
+80 -40
View File
@@ -5,14 +5,63 @@
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
#include "entitymanager.h" #include "entity.h"
#include "component/display/entityposition.h" #include "component/display/entityposition.h"
#include "util/memory.h" #include "util/memory.h"
#include "util/string.h" #include "util/string.h"
#include "assert/assert.h" #include "assert/assert.h"
#include "console/console.h"
void entityInit(entitymanager_t *mgr, const entityid_t entityId) { entity_t ENTITIES[ENTITY_COUNT_MAX];
entity_t *ent = &mgr->entities[entityId]; component_t COMPONENTS[ENTITY_COUNT_MAX * ENTITY_COMPONENT_COUNT_MAX];
componentid_t ENTITIES_WITH_COMPONENT[COMPONENT_TYPE_COUNT * ENTITY_COUNT_MAX];
void entityManagerInit(void) {
memoryZero(ENTITIES, sizeof(ENTITIES));
memoryZero(COMPONENTS, sizeof(COMPONENTS));
memorySet(
ENTITIES_WITH_COMPONENT, COMPONENT_ID_INVALID,
sizeof(ENTITIES_WITH_COMPONENT)
);
size_t totalSize =
sizeof(ENTITIES) + sizeof(COMPONENTS) + sizeof(ENTITIES_WITH_COMPONENT);
consolePrint(
"Entity manager size: %zu bytes (%.2f KB)",
totalSize,
totalSize / 1024.0f
);
}
entityid_t entityManagerAdd(void) {
for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) {
if((ENTITIES[i].state & ENTITY_STATE_ACTIVE) != 0) continue;
entityInit(i);
return i;
}
assertUnreachable("No more entity IDs available");
return ENTITY_ID_INVALID;
}
void entityManagerUpdate(void) {
entityid_t i = 0;
while(i < ENTITY_COUNT_MAX) {
if((ENTITIES[i].state & ENTITY_STATE_ACTIVE) != 0) {
entityUpdate(i);
}
i++;
}
}
void entityManagerDispose(void) {
for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) {
if((ENTITIES[i].state & ENTITY_STATE_ACTIVE) == 0) continue;
entityDispose(i);
}
}
void entityInit(const entityid_t entityId) {
entity_t *ent = &ENTITIES[entityId];
memoryZero(ent, sizeof(entity_t)); memoryZero(ent, sizeof(entity_t));
@@ -22,7 +71,7 @@ void entityInit(entitymanager_t *mgr, const entityid_t entityId) {
compType < COMPONENT_TYPE_COUNT; compType < COMPONENT_TYPE_COUNT;
compType++ compType++
) { ) {
mgr->entitiesWithComponent[ ENTITIES_WITH_COMPONENT[
compType * ENTITY_COUNT_MAX + entityId compType * ENTITY_COUNT_MAX + entityId
] = COMPONENT_ID_INVALID; ] = COMPONENT_ID_INVALID;
} }
@@ -31,7 +80,6 @@ void entityInit(entitymanager_t *mgr, const entityid_t entityId) {
} }
componentid_t entityAddComponent( componentid_t entityAddComponent(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componenttype_t type const componenttype_t type
) { ) {
@@ -39,15 +87,15 @@ componentid_t entityAddComponent(
for(componentid_t i = 0; i < ENTITY_COMPONENT_COUNT_MAX; i++) { for(componentid_t i = 0; i < ENTITY_COMPONENT_COUNT_MAX; i++) {
compInd = componentGetIndex(entityId, i); compInd = componentGetIndex(entityId, i);
if(mgr->components[compInd].type != COMPONENT_TYPE_NULL) { if(COMPONENTS[compInd].type != COMPONENT_TYPE_NULL) {
assertTrue( assertTrue(
mgr->components[compInd].type != type, COMPONENTS[compInd].type != type,
"Entity already has component of this type" "Entity already has component of this type"
); );
continue; continue;
} }
componentInit(mgr, entityId, i, type); componentInit(entityId, i, type);
mgr->entitiesWithComponent[ ENTITIES_WITH_COMPONENT[
type * ENTITY_COUNT_MAX + entityId type * ENTITY_COUNT_MAX + entityId
] = i; ] = i;
return i; return i;
@@ -58,98 +106,93 @@ componentid_t entityAddComponent(
} }
componentid_t entityGetComponent( componentid_t entityGetComponent(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componenttype_t type const componenttype_t type
) { ) {
componentid_t compId = mgr->entitiesWithComponent[ componentid_t compId = ENTITIES_WITH_COMPONENT[
type * ENTITY_COUNT_MAX + entityId type * ENTITY_COUNT_MAX + entityId
]; ];
if(compId == COMPONENT_ID_INVALID) return compId; if(compId == COMPONENT_ID_INVALID) return compId;
assertTrue( assertTrue(
mgr->components[componentGetIndex(entityId, compId)].type == type, COMPONENTS[componentGetIndex(entityId, compId)].type == type,
"Component type mismatch" "Component type mismatch"
); );
return compId; return compId;
} }
void entitySetName( void entitySetName(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const char_t *name const char_t *name
) { ) {
stringCopy(mgr->entities[entityId].name, name, ENTITY_NAME_MAX); stringCopy(ENTITIES[entityId].name, name, ENTITY_NAME_MAX);
} }
const char_t *entityGetName( const char_t *entityGetName(
entitymanager_t *mgr,
const entityid_t entityId const entityid_t entityId
) { ) {
return mgr->entities[entityId].name; return ENTITIES[entityId].name;
} }
entityid_t entityFindByName( entityid_t entityFindByName(
entitymanager_t *mgr,
const char_t *name const char_t *name
) { ) {
for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) { for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) {
if(!(mgr->entities[i].state & ENTITY_STATE_ACTIVE)) continue; if(!(ENTITIES[i].state & ENTITY_STATE_ACTIVE)) continue;
if(stringEquals(mgr->entities[i].name, name)) return i; if(stringEquals(ENTITIES[i].name, name)) return i;
} }
return ENTITY_ID_INVALID; return ENTITY_ID_INVALID;
} }
void entityDisposeDeep(entitymanager_t *mgr, const entityid_t entityId) { void entityDisposeDeep(const entityid_t entityId) {
componentid_t posComp = entityGetComponent( componentid_t posComp = entityGetComponent(
mgr, entityId, COMPONENT_TYPE_POSITION entityId, COMPONENT_TYPE_POSITION
); );
if(posComp != COMPONENT_ID_INVALID) { if(posComp != COMPONENT_ID_INVALID) {
entityPositionDisposeDeep(mgr, entityId, posComp); entityPositionDisposeDeep(entityId, posComp);
} else { } else {
entityDispose(mgr, entityId); entityDispose(entityId);
} }
} }
void entityUpdate(entitymanager_t *mgr, const entityid_t entityId) { void entityUpdate(const entityid_t entityId) {
entity_t *ent = &mgr->entities[entityId]; entity_t *ent = &ENTITIES[entityId];
for(uint8_t i = 0; i < ent->updateCount; i++) { for(uint8_t i = 0; i < ent->updateCount; i++) {
ent->onUpdate[i]( ent->onUpdate[i](
mgr, entityId, ent->updateComponentId[i], ent->updateUser[i] entityId, ent->updateComponentId[i], ent->updateUser[i]
); );
} }
} }
void entityDispose(entitymanager_t *mgr, const entityid_t entityId) { void entityDispose(const entityid_t entityId) {
componentindex_t compInd; componentindex_t compInd;
entity_t *ent = &mgr->entities[entityId]; entity_t *ent = &ENTITIES[entityId];
for(uint8_t i = 0; i < ent->disposeCount; i++) { for(uint8_t i = 0; i < ent->disposeCount; i++) {
ent->onDispose[i]( ent->onDispose[i](
mgr, entityId, ent->disposeComponentId[i], ent->disposeUser[i] entityId, ent->disposeComponentId[i], ent->disposeUser[i]
); );
} }
for(componentid_t i = 0; i < ENTITY_COMPONENT_COUNT_MAX; i++) { for(componentid_t i = 0; i < ENTITY_COMPONENT_COUNT_MAX; i++) {
compInd = componentGetIndex(entityId, i); compInd = componentGetIndex(entityId, i);
componenttype_t type = mgr->components[compInd].type; componenttype_t type = COMPONENTS[compInd].type;
if(type == COMPONENT_TYPE_NULL) continue; if(type == COMPONENT_TYPE_NULL) continue;
mgr->entitiesWithComponent[ ENTITIES_WITH_COMPONENT[
type * ENTITY_COUNT_MAX + entityId type * ENTITY_COUNT_MAX + entityId
] = COMPONENT_ID_INVALID; ] = COMPONENT_ID_INVALID;
componentDispose(mgr, entityId, i); componentDispose(entityId, i);
} }
ent->state = 0; ent->state = 0;
} }
void entityUpdateAdd( void entityUpdateAdd(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const entitycallback_t callback, const entitycallback_t callback,
const componentid_t componentId, const componentid_t componentId,
void *user void *user
) { ) {
entity_t *ent = &mgr->entities[entityId]; entity_t *ent = &ENTITIES[entityId];
assertTrue( assertTrue(
ent->updateCount < ENTITY_UPDATE_CALLBACK_COUNT_MAX, ent->updateCount < ENTITY_UPDATE_CALLBACK_COUNT_MAX,
"Entity update callback slots full" "Entity update callback slots full"
@@ -161,11 +204,10 @@ void entityUpdateAdd(
} }
void entityUpdateRemove( void entityUpdateRemove(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const entitycallback_t callback const entitycallback_t callback
) { ) {
entity_t *ent = &mgr->entities[entityId]; entity_t *ent = &ENTITIES[entityId];
for(uint8_t i = 0; i < ent->updateCount; i++) { for(uint8_t i = 0; i < ent->updateCount; i++) {
if(ent->onUpdate[i] != callback) continue; if(ent->onUpdate[i] != callback) continue;
ent->updateCount--; ent->updateCount--;
@@ -179,13 +221,12 @@ void entityUpdateRemove(
} }
void entityDisposeAdd( void entityDisposeAdd(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const entitycallback_t callback, const entitycallback_t callback,
const componentid_t componentId, const componentid_t componentId,
void *user void *user
) { ) {
entity_t *ent = &mgr->entities[entityId]; entity_t *ent = &ENTITIES[entityId];
assertTrue( assertTrue(
ent->disposeCount < ENTITY_DISPOSE_CALLBACK_COUNT_MAX, ent->disposeCount < ENTITY_DISPOSE_CALLBACK_COUNT_MAX,
"Entity dispose callback slots full" "Entity dispose callback slots full"
@@ -197,11 +238,10 @@ void entityDisposeAdd(
} }
void entityDisposeRemove( void entityDisposeRemove(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const entitycallback_t callback const entitycallback_t callback
) { ) {
entity_t *ent = &mgr->entities[entityId]; entity_t *ent = &ENTITIES[entityId];
for(uint8_t i = 0; i < ent->disposeCount; i++) { for(uint8_t i = 0; i < ent->disposeCount; i++) {
if(ent->onDispose[i] != callback) continue; if(ent->onDispose[i] != callback) continue;
ent->disposeCount--; ent->disposeCount--;
+32 -27
View File
@@ -16,7 +16,6 @@
#define ENTITY_NAME_MAX 32 #define ENTITY_NAME_MAX 32
typedef void (*entitycallback_t)( typedef void (*entitycallback_t)(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componentid_t componentId, const componentid_t componentId,
void *user void *user
@@ -35,24 +34,49 @@ typedef struct {
void *disposeUser[ENTITY_DISPOSE_CALLBACK_COUNT_MAX]; void *disposeUser[ENTITY_DISPOSE_CALLBACK_COUNT_MAX];
} entity_t; } entity_t;
extern entity_t ENTITIES[ENTITY_COUNT_MAX];
extern component_t COMPONENTS[ENTITY_COUNT_MAX * ENTITY_COMPONENT_COUNT_MAX];
extern componentid_t ENTITIES_WITH_COMPONENT[COMPONENT_TYPE_COUNT * ENTITY_COUNT_MAX];
/**
* Initializes the entity system, marking all entities and components as
* unused.
*/
void entityManagerInit(void);
/**
* Adds / Reserves a new entity ID.
*
* @return The new entity ID.
*/
entityid_t entityManagerAdd(void);
/**
* Updates all active entities.
*/
void entityManagerUpdate(void);
/**
* Disposes of the entity system, in turn freeing all entities and
* components.
*/
void entityManagerDispose(void);
/** /**
* Initializes an entity with the given ID. * Initializes an entity with the given ID.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The ID of the entity to initialize. * @param entityId The ID of the entity to initialize.
*/ */
void entityInit(entitymanager_t *mgr, const entityid_t entityId); void entityInit(const entityid_t entityId);
/** /**
* Adds a component of the given type to the entity with the given ID. * Adds a component of the given type to the entity with the given ID.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The ID of the entity to add the component to. * @param entityId The ID of the entity to add the component to.
* @param type The type of the component to add. * @param type The type of the component to add.
* @return The ID of the entity with component. * @return The ID of the entity with component.
*/ */
componentid_t entityAddComponent( componentid_t entityAddComponent(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componenttype_t type const componenttype_t type
); );
@@ -61,13 +85,11 @@ componentid_t entityAddComponent(
* Gets the ID of the component of the given type on the entity with the * Gets the ID of the component of the given type on the entity with the
* given ID, or COMPONENT_ID_INVALID if the entity lacks the component. * given ID, or COMPONENT_ID_INVALID if the entity lacks the component.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The ID of the entity to get the component from. * @param entityId The ID of the entity to get the component from.
* @param type The type of the component to get. * @param type The type of the component to get.
* @return The ID of the component. * @return The ID of the component.
*/ */
componentid_t entityGetComponent( componentid_t entityGetComponent(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const componenttype_t type const componenttype_t type
); );
@@ -78,13 +100,11 @@ componentid_t entityGetComponent(
* entity referenceable by name, e.g. as a parent via * entity referenceable by name, e.g. as a parent via
* entityPositionSetParent(). * entityPositionSetParent().
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The ID of the entity to name. * @param entityId The ID of the entity to name.
* @param name The name to give the entity. Must be shorter than * @param name The name to give the entity. Must be shorter than
* ENTITY_NAME_MAX. * ENTITY_NAME_MAX.
*/ */
void entitySetName( void entitySetName(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const char_t *name const char_t *name
); );
@@ -92,65 +112,56 @@ void entitySetName(
/** /**
* Gets an entity's name, or an empty string if it has none. * Gets an entity's name, or an empty string if it has none.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The ID of the entity to get the name of. * @param entityId The ID of the entity to get the name of.
* @return The entity's name. * @return The entity's name.
*/ */
const char_t *entityGetName( const char_t *entityGetName(
entitymanager_t *mgr,
const entityid_t entityId const entityid_t entityId
); );
/** /**
* Finds the first active entity with the given name. * Finds the first active entity with the given name.
* *
* @param mgr The entity manager to search.
* @param name The name to search for. * @param name The name to search for.
* @return The entity ID, or ENTITY_ID_INVALID if no active entity matches. * @return The entity ID, or ENTITY_ID_INVALID if no active entity matches.
*/ */
entityid_t entityFindByName( entityid_t entityFindByName(
entitymanager_t *mgr,
const char_t *name const char_t *name
); );
/** /**
* Runs all registered update callbacks for the entity. * Runs all registered update callbacks for the entity.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The ID of the entity to update. * @param entityId The ID of the entity to update.
*/ */
void entityUpdate(entitymanager_t *mgr, const entityid_t entityId); void entityUpdate(const entityid_t entityId);
/** /**
* Disposes of an entity with the given ID. Fires all dispose callbacks * Disposes of an entity with the given ID. Fires all dispose callbacks
* before cleaning up components and state. * before cleaning up components and state.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The ID of the entity to dispose of. * @param entityId The ID of the entity to dispose of.
*/ */
void entityDispose(entitymanager_t *mgr, const entityid_t entityId); void entityDispose(const entityid_t entityId);
/** /**
* Disposes of an entity and all of its position-component descendants * Disposes of an entity and all of its position-component descendants
* recursively. If the entity has no position component, behaves like * recursively. If the entity has no position component, behaves like
* entityDispose. * entityDispose.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The root entity ID. * @param entityId The root entity ID.
*/ */
void entityDisposeDeep(entitymanager_t *mgr, const entityid_t entityId); void entityDisposeDeep(const entityid_t entityId);
/** /**
* Registers an update callback, invoked each time entityUpdate is called. * Registers an update callback, invoked each time entityUpdate is called.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity to register on. * @param entityId The entity to register on.
* @param callback The function to call. * @param callback The function to call.
* @param componentId The component ID passed back to the callback. * @param componentId The component ID passed back to the callback.
* @param user Userdata passed back to the callback. * @param user Userdata passed back to the callback.
*/ */
void entityUpdateAdd( void entityUpdateAdd(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const entitycallback_t callback, const entitycallback_t callback,
const componentid_t componentId, const componentid_t componentId,
@@ -160,12 +171,10 @@ void entityUpdateAdd(
/** /**
* Removes a previously registered update callback. * Removes a previously registered update callback.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity to remove from. * @param entityId The entity to remove from.
* @param callback The function to remove. * @param callback The function to remove.
*/ */
void entityUpdateRemove( void entityUpdateRemove(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const entitycallback_t callback const entitycallback_t callback
); );
@@ -174,14 +183,12 @@ void entityUpdateRemove(
* Registers a dispose callback, invoked at the start of entityDispose * Registers a dispose callback, invoked at the start of entityDispose
* before any component or state cleanup. * before any component or state cleanup.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity to register on. * @param entityId The entity to register on.
* @param callback The function to call. * @param callback The function to call.
* @param componentId The component ID passed back to the callback. * @param componentId The component ID passed back to the callback.
* @param user Userdata passed back to the callback. * @param user Userdata passed back to the callback.
*/ */
void entityDisposeAdd( void entityDisposeAdd(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const entitycallback_t callback, const entitycallback_t callback,
const componentid_t componentId, const componentid_t componentId,
@@ -191,12 +198,10 @@ void entityDisposeAdd(
/** /**
* Removes a previously registered dispose callback. * Removes a previously registered dispose callback.
* *
* @param mgr The entity manager that owns the entity.
* @param entityId The entity to remove from. * @param entityId The entity to remove from.
* @param callback The function to remove. * @param callback The function to remove.
*/ */
void entityDisposeRemove( void entityDisposeRemove(
entitymanager_t *mgr,
const entityid_t entityId, const entityid_t entityId,
const entitycallback_t callback const entitycallback_t callback
); );
-5
View File
@@ -17,8 +17,3 @@
typedef uint8_t entityid_t; typedef uint8_t entityid_t;
typedef uint8_t componentid_t; typedef uint8_t componentid_t;
typedef uint16_t componentindex_t; typedef uint16_t componentindex_t;
// Forward declared here (rather than in component.h) so every entity/
// component header can reference entitymanager_t* regardless of include
// order, without needing a full entitymanager.h include.
typedef struct entitymanager_t entitymanager_t;
-56
View File
@@ -1,56 +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"
#include "console/console.h"
void entityManagerInit(entitymanager_t *mgr) {
assertNotNull(mgr, "Entity manager cannot be null");
memoryZero(mgr, sizeof(entitymanager_t));
memorySet(
mgr->entitiesWithComponent, COMPONENT_ID_INVALID,
sizeof(componentid_t) * COMPONENT_TYPE_COUNT * ENTITY_COUNT_MAX
);
consolePrint(
"Entity manager size: %zu bytes (%.2f KB)",
sizeof(entitymanager_t),
sizeof(entitymanager_t) / 1024.0f
);
}
entityid_t entityManagerAdd(entitymanager_t *mgr) {
assertNotNull(mgr, "Entity manager cannot be null");
for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) {
if((mgr->entities[i].state & ENTITY_STATE_ACTIVE) != 0) continue;
entityInit(mgr, i);
return i;
}
assertUnreachable("No more entity IDs available");
return ENTITY_ID_INVALID;
}
void entityManagerUpdate(entitymanager_t *mgr) {
assertNotNull(mgr, "Entity manager cannot be null");
entityid_t i = 0;
while(i < ENTITY_COUNT_MAX) {
if((mgr->entities[i].state & ENTITY_STATE_ACTIVE) != 0) {
entityUpdate(mgr, i);
}
i++;
}
}
void entityManagerDispose(entitymanager_t *mgr) {
assertNotNull(mgr, "Entity manager cannot be null");
for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) {
if((mgr->entities[i].state & ENTITY_STATE_ACTIVE) == 0) continue;
entityDispose(mgr, i);
}
}
-46
View File
@@ -1,46 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "entity.h"
typedef struct entitymanager_t {
entity_t entities[ENTITY_COUNT_MAX];
component_t components[ENTITY_COUNT_MAX * ENTITY_COMPONENT_COUNT_MAX];
componentid_t entitiesWithComponent[COMPONENT_TYPE_COUNT * ENTITY_COUNT_MAX];
} entitymanager_t;
/**
* Initializes an entity manager, marking all entities and components as
* unused.
*
* @param mgr The entity manager to initialize.
*/
void entityManagerInit(entitymanager_t *mgr);
/**
* Adds / Reserves a new entity ID in the given manager.
*
* @param mgr The entity manager to allocate the entity in.
* @return The new entity ID.
*/
entityid_t entityManagerAdd(entitymanager_t *mgr);
/**
* Updates all active entities in the given manager.
*
* @param mgr The entity manager to update.
*/
void entityManagerUpdate(entitymanager_t *mgr);
/**
* Disposes of the entity manager, in turn freeing all entities and
* components.
*
* @param mgr The entity manager to dispose.
*/
void entityManagerDispose(entitymanager_t *mgr);
+64
View File
@@ -0,0 +1,64 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "entityrender.h"
#include "component/display/entitycamera.h"
#include "component/display/entityposition.h"
#include "component/display/entityrenderable.h"
#include "display/shader/shaderunlit.h"
errorret_t entityRenderAll(void) {
entityid_t camEntity = entityCameraGetCurrent();
if(camEntity == ENTITY_ID_INVALID) errorOk();
componentid_t camComp = entityGetComponent(
camEntity, COMPONENT_TYPE_CAMERA
);
componentid_t camPosComp = entityGetComponent(
camEntity, COMPONENT_TYPE_POSITION
);
mat4 proj;
entityCameraGetProjection(camEntity, camComp, proj);
mat4 view;
glm_mat4_identity(view);
if(camPosComp != COMPONENT_ID_INVALID) {
mat4 camTransform;
entityPositionGetTransform(camEntity, camPosComp, camTransform);
// Position components always store world-space placement (eye ->
// world), regardless of how they were set (SetPosition/Rotation or
// LookAt) -- invert to get the view matrix (world -> eye).
glm_mat4_inv(camTransform, view);
}
errorChain(shaderBind(&SHADER_UNLIT));
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj));
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, view));
entityid_t renderEntities[ENTITY_COUNT_MAX];
componentid_t renderComponents[ENTITY_COUNT_MAX];
entityid_t renderCount = componentGetEntitiesWithComponent(
COMPONENT_TYPE_RENDERABLE, renderEntities, renderComponents
);
for(entityid_t i = 0; i < renderCount; i++) {
mat4 model;
componentid_t posComp = entityGetComponent(
renderEntities[i], COMPONENT_TYPE_POSITION
);
if(posComp == COMPONENT_ID_INVALID) {
glm_mat4_identity(model);
} else {
entityPositionGetTransform(renderEntities[i], posComp, model);
}
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, model));
errorChain(entityRenderableDraw(renderEntities[i], renderComponents[i]));
}
errorOk();
}
+18
View File
@@ -0,0 +1,18 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "entity.h"
#include "error/error.h"
/**
* Renders every COMPONENT_TYPE_RENDERABLE entity, from the view of its
* first COMPONENT_TYPE_CAMERA entity. A no-op if there is no camera.
*
* @return Error state.
*/
errorret_t entityRenderAll(void);
-4
View File
@@ -9,7 +9,6 @@
#include "util/memory.h" #include "util/memory.h"
#include "util/string.h" #include "util/string.h"
#include "assert/assert.h" #include "assert/assert.h"
#include "ui/ui.h"
#include "system/system.h" #include "system/system.h"
#include "console/console.h" #include "console/console.h"
@@ -39,9 +38,6 @@ errorret_t localeManagerSetLocale(const localeinfo_t *locale) {
assetEntryLock(LOCALE.entry); assetEntryLock(LOCALE.entry);
errorChain(assetRequireLoaded(LOCALE.entry)); errorChain(assetRequireLoaded(LOCALE.entry));
// TODO : Trigger UI update.
errorChain(uiUpdateTranslations());
errorOk(); errorOk();
} }
-1
View File
@@ -11,5 +11,4 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
) )
# Subdirs # Subdirs
add_subdirectory(slot)
add_subdirectory(settings) add_subdirectory(settings)
+1 -80
View File
@@ -8,7 +8,6 @@
#include "save.h" #include "save.h"
#include "util/memory.h" #include "util/memory.h"
#include "assert/assert.h" #include "assert/assert.h"
#include "rpg/cutscene/cutscenesystem.h"
#include "console/console.h" #include "console/console.h"
save_t SAVE; save_t SAVE;
@@ -17,16 +16,9 @@ errorret_t saveInit() {
memoryZero(&SAVE, sizeof(save_t)); memoryZero(&SAVE, sizeof(save_t));
SAVE.deviceCurrent = 0xFF;// No current device. SAVE.deviceCurrent = 0xFF;// No current device.
SAVE.slotCurrent = 0xFF;// No current slot.
// Initialize the slots and settings // Initialize the settings
saveSettingsInit(&SAVE.settings); saveSettingsInit(&SAVE.settings);
errorChain(saveSlotInit(&SAVE.slot));
// Update caches to match default data.
for(uint8_t i = 0; i < SAVE_SLOT_COUNT; i++) {
SAVE.caches[i] = SAVE.slot.cachedData;
}
// Start by initializing each of the save devices. // Start by initializing each of the save devices.
savedevice_t *device = &SAVE.devices[0]; savedevice_t *device = &SAVE.devices[0];
@@ -67,10 +59,6 @@ errorret_t saveUpdate() {
} else { } else {
// Device was found, cache in the data. // Device was found, cache in the data.
errorChain(saveLoadSettings()); errorChain(saveLoadSettings());
errorChain(saveLoadAllSlots());
// Reset slot, implying no slot has been selected.
SAVE.slotCurrent = 0xFF;
// TEST: write settings back out immediately once a device is // TEST: write settings back out immediately once a device is
// selected, to confirm the write path actually works on hardware. // selected, to confirm the write path actually works on hardware.
@@ -166,10 +154,6 @@ void saveOnDeviceAvailabilityChecked(savedevice_t *device, void *user) {
SAVE.deviceCurrent = (uint8_t)(device - &SAVE.devices[0]); SAVE.deviceCurrent = (uint8_t)(device - &SAVE.devices[0]);
} }
bool_t saveCanSave() {
return CUTSCENE_SYSTEM.scene == NULL;
}
errorret_t saveSaveSettings() { errorret_t saveSaveSettings() {
assertTrue(SAVE.deviceCurrent != 0xFF, "No current device"); assertTrue(SAVE.deviceCurrent != 0xFF, "No current device");
@@ -204,69 +188,6 @@ errorret_t saveLoadSettings() {
errorOk(); errorOk();
} }
errorret_t saveSaveSlot() {
assertTrue(SAVE.deviceCurrent != 0xFF, "No current device");
assertTrue(SAVE.slotCurrent < SAVE_SLOT_COUNT, "Invalid slot index");
errorChain(saveDeviceSlotWrite(
&SAVE.devices[SAVE.deviceCurrent],
&SAVE.slot,
SAVE.slotCurrent
));
// Update cache
SAVE.caches[SAVE.slotCurrent] = SAVE.slot.cachedData;
SAVE.slotDirty = false;
errorOk();
}
errorret_t saveLoadSlot() {
assertTrue(SAVE.slotCurrent < SAVE_SLOT_COUNT, "Invalid slot index");
if(SAVE.deviceCurrent == 0xFF) {
// No save device available, so we assume an empty slot.
errorChain(saveSlotInit(&SAVE.slot));
SAVE.caches[SAVE.slotCurrent] = SAVE.slot.cachedData;
errorOk();
}
errorret_t ret = saveDeviceSlotRead(
&SAVE.devices[SAVE.deviceCurrent],
&SAVE.slot,
SAVE.slotCurrent
);
if(errorIsNotOk(ret)) {
// Corrupt or incompatible (e.g. wrong/missing schema version) slot
// file - don't take the whole game down over it, treat it the same as
// an empty/unused slot.
errorCatch(errorPrint(ret));
consolePrint(
"Save slot %u could not be loaded, treating as empty.\n",
(uint32_t)SAVE.slotCurrent
);
errorChain(saveSlotInit(&SAVE.slot));
SAVE.slot.cachedData.corrupt = true;
}
// Update cache
SAVE.caches[SAVE.slotCurrent] = SAVE.slot.cachedData;
SAVE.slotDirty = false;
errorOk();
}
errorret_t saveLoadAllSlots() {
for(uint8_t i = 0; i < SAVE_SLOT_COUNT; i++) {
SAVE.slotCurrent = i;
errorChain(saveSlotInit(&SAVE.slot));
errorChain(saveLoadSlot());// Load slot updates the cache.
}
errorOk();
}
errorret_t saveDispose() { errorret_t saveDispose() {
// Dispose each device. // Dispose each device.
savedevice_t *device = &SAVE.devices[0]; savedevice_t *device = &SAVE.devices[0];
-37
View File
@@ -7,18 +7,12 @@
#pragma once #pragma once
#include "savedevice.h" #include "savedevice.h"
#include "save/slot/saveslot.h"
#include "save/settings/savesettings.h" #include "save/settings/savesettings.h"
typedef struct { typedef struct {
// File state // File state
savesettings_t settings; savesettings_t settings;
saveslotcache_t caches[SAVE_SLOT_COUNT];
saveslot_t slot;
uint8_t slotCurrent;
bool_t settingsDirty; bool_t settingsDirty;
bool_t slotDirty;
// Device state // Device state
savedevice_t devices[SAVE_DEVICE_COUNT]; savedevice_t devices[SAVE_DEVICE_COUNT];
@@ -67,14 +61,6 @@ void saveFindAvailableDevice(
*/ */
void saveOnDeviceAvailabilityChecked(savedevice_t *device, void *user); void saveOnDeviceAvailabilityChecked(savedevice_t *device, void *user);
/**
* Whether the game is in a state where saving is currently allowed.
* Currently just checks that no cutscene is running.
*
* @return True if saving is currently allowed, false otherwise.
*/
bool_t saveCanSave();
/** /**
* Saves the current settings to the current device. * Saves the current settings to the current device.
* *
@@ -89,29 +75,6 @@ errorret_t saveSaveSettings();
*/ */
errorret_t saveLoadSettings(); errorret_t saveLoadSettings();
/**
* Saves the current slot to the current device, at the current slot index.
*
* @return Error state if any.
*/
errorret_t saveSaveSlot();
/**
* Loads the current slot from the current device, at the current slot index.
*
* @return Error state if any.
*/
errorret_t saveLoadSlot();
/**
* Resets and loads every slot from the current device, updating the caches
* along the way. Leaves SAVE.slotCurrent pointing at the last slot index
* once done.
*
* @return Error state if any.
*/
errorret_t saveLoadAllSlots();
/** /**
* Disposes of the save system. * Disposes of the save system.
* *
-34
View File
@@ -473,40 +473,6 @@ errorret_t saveDeviceRawFetchItem(
#endif// defined(SAVE_DEVICE_DATA_RAW) #endif// defined(SAVE_DEVICE_DATA_RAW)
errorret_t saveDeviceSlotWrite(
savedevice_t *device,
saveslot_t *slot,
const uint8_t slotIndex
) {
assertNotNull(device, "device cannot be null");
assertNotNull(slot, "slot cannot be null");
#if defined(SAVE_DEVICE_DATA_RAW)
errorChain(saveDeviceRawStoreItem(device, NULL, slot, slotIndex));
#elif defined(saveDeviceSlotWritePlatform)
errorChain(saveDeviceSlotWritePlatform(device, slot, slotIndex));
#endif
errorOk();
}
errorret_t saveDeviceSlotRead(
savedevice_t *device,
saveslot_t *slot,
const uint8_t slotIndex
) {
assertNotNull(device, "device cannot be null");
assertNotNull(slot, "slot cannot be null");
#if defined(SAVE_DEVICE_DATA_RAW)
errorChain(saveDeviceRawFetchItem(device, NULL, slot, slotIndex));
#elif defined(saveDeviceSlotReadPlatform)
errorChain(saveDeviceSlotReadPlatform(device, slot, slotIndex));
#endif
errorOk();
}
errorret_t saveDeviceSettingsWrite( errorret_t saveDeviceSettingsWrite(
savedevice_t *device, savedevice_t *device,
savesettings_t *settings savesettings_t *settings
-29
View File
@@ -44,7 +44,6 @@
typedef struct savedevice_s savedevice_t; typedef struct savedevice_s savedevice_t;
typedef struct savesettings_s savesettings_t; typedef struct savesettings_s savesettings_t;
typedef struct saveslot_s saveslot_t;
typedef void (*savedevicestatecallback_t)(savedevice_t *device, void *user); typedef void (*savedevicestatecallback_t)(savedevice_t *device, void *user);
@@ -102,34 +101,6 @@ void saveDeviceCheckAvailability(
void *user void *user
); );
/**
* Writes the given save slot to the device at the given slot index.
*
* @param device The save device to write to.
* @param slot The save slot to write.
* @param slotIndex The slot index to write to.
* @return Error state if any.
*/
errorret_t saveDeviceSlotWrite(
savedevice_t *device,
saveslot_t *slot,
const uint8_t slotIndex
);
/**
* Reads a save slot from the device at the given slot index.
*
* @param device The save device to read from.
* @param slot The save slot to read into.
* @param slotIndex The slot index to read from.
* @return Error state if any.
*/
errorret_t saveDeviceSlotRead(
savedevice_t *device,
saveslot_t *slot,
const uint8_t slotIndex
);
/** /**
* Writes the given save settings to the device. * Writes the given save settings to the device.
* *
-92
View File
@@ -1,92 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "saveslot.h"
#include "save/slot/saveslotcurrent.h"
#include "save/save.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "util/string.h"
errorret_t saveSlotInit(saveslot_t *slot) {
assertNotNull(slot, "Slot cannot be null");
// Zero the save slot.
memoryZero(slot, sizeof(saveslot_t));
slot->version = 1;
// Set the default story flags.
for(storyflag_t flag = 0; flag < STORY_FLAG_COUNT; flag++) {
slot->storyFlags[flag] = STORY_FLAG_MAP[flag].initial;
}
// Set the default party.
errorChain(partyInit(&slot->party));
// Set the default current cutscene.
stringCopy(
slot->currentCutscene, SAVE_SLOT_CUTSCENE_DEFAULT,
sizeof(slot->currentCutscene) - 1
);
errorOk();
}
errorret_t saveSlotNewGame(void) {
errorChain(saveSlotInit(&SAVE.slot));
stringCopy(
SAVE.slot.cachedData.mapName, SAVE_SLOT_MAP_NAME_DEFAULT,
sizeof(SAVE.slot.cachedData.mapName)
);
errorOk();
}
void saveSlotSetCurrentCutscene(const char_t *file) {
assertNotNull(file, "File cannot be null");
stringCopy(
SAVE.slot.currentCutscene, file, sizeof(SAVE.slot.currentCutscene) - 1
);
SAVE.slotDirty = true;
}
bool_t saveSlotInUse(saveslotcache_t *slot) {
assertNotNull(slot, "Slot cannot be null");
return slot->name[0] != '\0';
}
bool_t saveSlotHasSaved(saveslotcache_t *slot) {
assertNotNull(slot, "Slot cannot be null");
return slot->time.time != 0;
}
errorret_t saveSlotWriteJSON(
saveslot_t *slot,
yyjson_mut_doc *doc,
yyjson_mut_val *object
) {
errorChain(saveSlotCurrentWriteJSON(slot, doc, object));
errorOk();
}
errorret_t saveSlotReadJSON(saveslot_t *slot, yyjson_val *object) {
assertNotNull(slot, "Slot cannot be null");
assertNotNull(object, "Object cannot be null");
// saveSlotInit() now takes slot directly, so this can reset through it
// rather than duplicating the memSet/version stub - saveSlotCurrent
// ReadJSON() fully overwrites every field it sets anyway (including
// party.members/order), so seeding a fresh party.jsonc roster here only
// to replace it is wasted work, but keeps this in one reset path rather
// than two subtly different ones.
errorChain(saveSlotInit(slot));
errorChain(saveSlotCurrentReadJSON(slot, object));
errorOk();
}
-117
View File
@@ -1,117 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "save/savedevice.h"
#include "rpg/overworld/map.h"
#include "rpg/battle/party.h"
#include "rpg/story/story.h"
#include "time/timeepoch.h"
#include "asset/assetfile.h"
#include "yyjson.h"
#define SAVE_SLOT_NAME_LENGTH 8
#ifndef SAVE_SLOT_COUNT
#define SAVE_SLOT_COUNT 3
#endif
#define SAVE_SLOT_MAP_NAME_DEFAULT "overworld"
#define SAVE_SLOT_CUTSCENE_NAME_MAX ASSET_FILE_NAME_MAX
#define SAVE_SLOT_CUTSCENE_DEFAULT "cutscenes/new_game.jsonc"
typedef struct {
char_t name[SAVE_SLOT_NAME_LENGTH + 1];// 8 characters + null terminator
dusktimeepoch_t time;
int32_t playerLevel;
char_t mapName[MAP_NAME_MAX];
bool_t corrupt;
} saveslotcache_t;
typedef struct saveslot_s {
uint8_t version;
uint8_t dataType;
saveslotcache_t cachedData;
party_t party;
storyflagval_t storyFlags[STORY_FLAG_COUNT_MAX];
// Asset path of the cutscene currently in progress for this slot (e.g.
// resumed on load) - defaults to SAVE_SLOT_CUTSCENE_DEFAULT on a fresh
// slot (see saveSlotInit).
char_t currentCutscene[SAVE_SLOT_CUTSCENE_NAME_MAX];
} saveslot_t;
/**
* Inits slot with the default state, this is functionally "new game" but
* will not set the player name, as that is what we use to determine if a
* save slot is "in use" or not. Seeds the party roster from party.jsonc
* (see partyInit).
*
* @param slot The save slot to initialize.
* @return Error if the starting party roster (party.jsonc) fails to
* load/parse.
*/
errorret_t saveSlotInit(saveslot_t *slot);
/**
* Sets up the current save slot (SAVE.slot) for a brand new game: calls
* saveSlotInit (which seeds the starting party roster) and sets the
* default starting map. Use this (not saveSlotInit directly) whenever a
* new save is actually being created for a player. Callers creating a
* new save at a specific index should set SAVE.slotCurrent first.
*
* @return Error if saveSlotInit fails.
*/
errorret_t saveSlotNewGame(void);
/**
* Sets the current save slot's (SAVE.slot) in-progress cutscene and
* dirties the save (SAVE.slotDirty), same as storySetFlagValue - so the
* next save write persists it. Use this (not writing SAVE.slot.
* currentCutscene directly) anywhere gameplay needs to change it.
*
* @param file Asset path of the cutscene now in progress, or "" for none.
*/
void saveSlotSetCurrentCutscene(const char_t *file);
/**
* Checks if the save slot is in use, this is determined by checking if the
* player name is set or not.
*
* @param slot The save slot to check.
* @return True if the save slot is in use, false otherwise.
*/
bool_t saveSlotInUse(saveslotcache_t *slot);
/**
* Returns whether or not the given save slot has ever saved (has an epoc non 0)
*
* @param slot The save slot to check.
* @return True if the save slot has ever saved, false otherwise.
*/
bool_t saveSlotHasSaved(saveslotcache_t *slot);
/**
* Writes the given save slot out to the given JSON object.
*
* @param slot The save slot to write.
* @param doc The mutable JSON document that owns the object.
* @param object The mutable JSON object to write into.
* @return Error state if any.
*/
errorret_t saveSlotWriteJSON(
saveslot_t *slot,
yyjson_mut_doc *doc,
yyjson_mut_val *object
);
/**
* Reads the given save slot in from the given JSON object.
*
* @param slot The save slot to read into.
* @param object The JSON object to read from.
* @return Error state if any.
*/
errorret_t saveSlotReadJSON(saveslot_t *slot, yyjson_val *object);
-79
View File
@@ -1,79 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "saveslotcurrent.h"
#include "assert/assert.h"
#include "util/string.h"
#include "util/memory.h"
#include "time/time.h"
errorret_t saveSlotCurrentWriteJSON(
saveslot_t *slot,
yyjson_mut_doc *doc,
yyjson_mut_val *object
) {
assertNotNull(slot, "Slot cannot be null");
assertNotNull(doc, "Doc cannot be null");
assertNotNull(object, "Object cannot be null");
// Stamped on the stable struct (not the throwaway ver1 copy below) so the
// fresh timestamp survives the write - saveSlotHasSaved() depends on it.
slot->cachedData.time = timeGetEpoch();
saveslotver1_t ver1;
stringCopy(ver1.name, slot->cachedData.name, sizeof(ver1.name));
ver1.time = slot->cachedData.time;
ver1.playerLevel = slot->cachedData.playerLevel;
stringCopy(ver1.mapName, slot->cachedData.mapName, sizeof(ver1.mapName));
memoryCopy(
ver1.partyMembers, slot->party.members, sizeof(ver1.partyMembers)
);
memoryCopy(ver1.partyOrder, slot->party.order, sizeof(ver1.partyOrder));
memoryCopy(ver1.storyFlags, slot->storyFlags, sizeof(ver1.storyFlags));
stringCopy(
ver1.currentCutscene, slot->currentCutscene, sizeof(ver1.currentCutscene)
);
errorChain(saveSlotVer1WriteJSON(&ver1, doc, object));
errorOk();
}
errorret_t saveSlotCurrentReadJSON(saveslot_t *slot, yyjson_val *object) {
assertNotNull(slot, "Slot cannot be null");
assertNotNull(object, "Object cannot be null");
// Zeroed up front: saveSlotVer1ReadJSON returns early (leaving everything
// past "name" untouched) for a blank/never-used slot, so this must not be
// left as uninitialized stack garbage.
saveslotver1_t ver1;
memoryZero(&ver1, sizeof(ver1));
errorChain(saveSlotVer1ReadJSON(&ver1, object));
stringCopy(slot->cachedData.name, ver1.name, sizeof(slot->cachedData.name));
slot->cachedData.time = ver1.time;
slot->cachedData.playerLevel = ver1.playerLevel;
stringCopy(
slot->cachedData.mapName, ver1.mapName, sizeof(slot->cachedData.mapName)
);
memoryCopy(
slot->party.members, ver1.partyMembers, sizeof(slot->party.members)
);
memoryCopy(slot->party.order, ver1.partyOrder, sizeof(slot->party.order));
memoryCopy(slot->storyFlags, ver1.storyFlags, sizeof(slot->storyFlags));
stringCopy(
slot->currentCutscene, ver1.currentCutscene,
sizeof(slot->currentCutscene)
);
errorOk();
}
-37
View File
@@ -1,37 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "save/slot/saveslot.h"
#include "save/slot/saveslotver1.h"
/**
* Converts the given save slot to whichever schema SAVE_SLOT_CURRENT
* (saveslotver.h) selects and writes it out to the given JSON object.
* Update this (only) when bumping to a new SAVE_SLOT_VER_N.
*
* @param slot The save slot to write.
* @param doc The mutable JSON document that owns the object.
* @param object The mutable JSON object to write into.
* @return Error state if any.
*/
errorret_t saveSlotCurrentWriteJSON(
saveslot_t *slot,
yyjson_mut_doc *doc,
yyjson_mut_val *object
);
/**
* Reads whichever schema SAVE_SLOT_CURRENT (saveslotver.h) selects from the
* given JSON object and converts it into the given save slot. Update this
* (only) when bumping to a new SAVE_SLOT_VER_N.
*
* @param slot The save slot to read into.
* @param object The JSON object to read from.
* @return Error state if any.
*/
errorret_t saveSlotCurrentReadJSON(saveslot_t *slot, yyjson_val *object);
-17
View File
@@ -1,17 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
// One constant per save slot JSON schema this codebase has ever shipped.
// Never remove or renumber an existing entry - old save files on disk may
// still declare it.
#define SAVE_SLOT_VER_1 1
// The schema saveSlotCurrentWriteJSON()/saveSlotCurrentReadJSON() use today.
// Bump this to a new SAVE_SLOT_VER_N when the slot JSON shape changes.
#define SAVE_SLOT_CURRENT SAVE_SLOT_VER_1
-105
View File
@@ -1,105 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "saveslotver1.h"
#include "assert/assert.h"
#include "save/savejson.h"
errorret_t saveSlotVer1WriteJSON(
saveslotver1_t *slot,
yyjson_mut_doc *doc,
yyjson_mut_val *object
) {
assertNotNull(slot, "Slot cannot be null");
assertNotNull(doc, "Doc cannot be null");
assertNotNull(object, "Object cannot be null");
writeVersion(SAVE_SLOT_CURRENT);
writeString("name", slot->name);
writeTime("time", slot->time);
writeInt32("playerLevel", slot->playerLevel);
writeString("mapName", slot->mapName);
yyjson_mut_val *partyMembersArr = yyjson_mut_arr(doc);
for(uint8_t i = 0; i < PARTY_MEMBER_COUNT_MAX; i++) {
yyjson_mut_val *memberObj =
battleFighterSerialize(&slot->partyMembers[i], doc);
yyjson_mut_arr_add_val(partyMembersArr, memberObj);
}
writeValue("partyMembers", partyMembersArr);
writeUInt8Array("partyOrder", slot->partyOrder, PARTY_ACTIVE_SIZE_MAX);
yyjson_mut_val *storyFlagsObj = yyjson_mut_obj_add_obj(doc, object, "storyFlags");
for(storyflag_t flag = 0; flag < STORY_FLAG_COUNT; flag++) {
yyjson_mut_obj_add_uint(
doc, storyFlagsObj, storyGetNameByFlag(flag), slot->storyFlags[flag]
);
}
writeString("currentCutscene", slot->currentCutscene);
errorOk();
}
errorret_t saveSlotVer1ReadJSON(saveslotver1_t *slot, yyjson_val *object) {
assertNotNull(slot, "Slot cannot be null");
assertNotNull(object, "Object cannot be null");
char_t saveJsonStringBuffer[SAVE_JSON_STRING_BUFFER_SIZE];
requireVersion(SAVE_SLOT_CURRENT);
readString("name", slot->name, "", SAVE_SLOT_VER1_NAME_LENGTH);
if(slot->name[0] == '\0') errorOk();
readTime("time", slot->time);
readInt32("playerLevel", slot->playerLevel, 1);
requireStrMin("mapName", 1);
readString(
"mapName", slot->mapName, "", SAVE_SLOT_VER1_MAP_NAME_MAX - 1
);
requireArrayOfSize(
"partyMembers", partyMembersArr, PARTY_MEMBER_COUNT_MAX
);
size_t memberIdx, memberLen;
yyjson_val *memberObj;
yyjson_arr_foreach(
partyMembersArr, memberIdx, memberLen, memberObj
) {
battlefighter_t *member = &slot->partyMembers[memberIdx];
member->id = (uint8_t)memberIdx;
errorChain(battleFighterParse(member, memberObj));
}
readUInt8Array(
"partyOrder", slot->partyOrder, PARTY_ACTIVE_SIZE_MAX,
PARTY_ORDER_EMPTY
);
yyjson_val *storyFlagsObj = yyjson_obj_get(object, "storyFlags");
if(storyFlagsObj != NULL) {
yyjson_obj_iter iter = yyjson_obj_iter_with(storyFlagsObj);
yyjson_val *flagKey;
while((flagKey = yyjson_obj_iter_next(&iter))) {
storyflag_t flag = storyGetFlagByName(yyjson_get_str(flagKey));
if(flag >= STORY_FLAG_COUNT) continue;// Flag no longer exists, ignore
slot->storyFlags[flag] =
(storyflagval_t)yyjson_get_uint(yyjson_obj_iter_get_val(flagKey));
}
}
readString(
"currentCutscene", slot->currentCutscene, SAVE_SLOT_VER1_CUTSCENE_DEFAULT,
SAVE_SLOT_VER1_CUTSCENE_NAME_MAX - 1
);
errorOk();
}
-86
View File
@@ -1,86 +0,0 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#include "time/timeepoch.h"
#include "save/slot/saveslotver.h"
#include "rpg/battle/party.h"
#include "rpg/story/story.h"
#include "yyjson.h"
// This schema's own frozen copies of save/slot/saveslot.h's field sizes -
// never reference SAVE_SLOT_NAME_LENGTH/MAP_NAME_MAX (or any other live
// constant) here. Once shipped, this file's on-disk shape must never change
// underneath old save files, even if the live engine constants do. The
// party arrays below are the exception - saveSlotVer1ReadJSON() checks their
// JSON array lengths explicitly, so PARTY_MEMBER_COUNT_MAX/
// PARTY_ACTIVE_SIZE_MAX (rpg/battle/party.h) can be referenced directly.
// storyFlags is a further exception, for a different reason - it's written
// keyed by flag name (not position), so its on-disk shape is decoupled from
// STORY_FLAG_COUNT_MAX entirely; a save simply loses whatever flags no
// longer resolve by name (see storyGetFlagByName) if that cap ever shrinks.
//
// partyMembers is a THIRD exception, deliberately broken pre-release: its
// per-member shape now comes from battleFighterSerialize/battleFighterParse
// (rpg/battle/fighter/battlefighter.h) - the shared fighter JSON format
// used everywhere else (party.jsonc, BATTLE_START enemies) - instead of its
// own hand-rolled field set. This is a real on-disk break (different key
// names, no more "statAttack" etc.) with no migration - an old v1 save
// simply reads back as corrupt. Acceptable only because nothing has
// shipped yet; don't repeat this once real saves exist without a version
// bump instead.
#define SAVE_SLOT_VER1_NAME_LENGTH 8
#define SAVE_SLOT_VER1_MAP_NAME_MAX 10
#define SAVE_SLOT_VER1_CUTSCENE_NAME_MAX 128
// Own frozen copy of save/slot/saveslot.h's SAVE_SLOT_CUTSCENE_DEFAULT, for
// the same reason as the sizes above - never reference the live constant.
#define SAVE_SLOT_VER1_CUTSCENE_DEFAULT "cutscenes/new_game.jsonc"
// Independent from saveslotcache_t (save/slot/saveslot.h) by design - this
// is what version 1 actually put on disk, frozen forever. saveSlotCurrent
// WriteJSON()/ReadJSON() (saveslotcurrent.c) manually copy field-by-field
// between the two, since they're separate types that merely happen to
// currently look alike.
typedef struct saveslotver1_s {
char_t name[SAVE_SLOT_VER1_NAME_LENGTH + 1];
dusktimeepoch_t time;
int32_t playerLevel;
char_t mapName[SAVE_SLOT_VER1_MAP_NAME_MAX];
battlefighter_t partyMembers[PARTY_MEMBER_COUNT_MAX];
uint8_t partyOrder[PARTY_ACTIVE_SIZE_MAX];
storyflagval_t storyFlags[STORY_FLAG_COUNT_MAX];
char_t currentCutscene[SAVE_SLOT_VER1_CUTSCENE_NAME_MAX];
} saveslotver1_t;
/**
* Writes the given v1 save slot data out to the given JSON object, stamping
* SAVE_SLOT_CURRENT as the "version" field.
*
* @param slot The v1 save slot data to write.
* @param doc The mutable JSON document that owns the object.
* @param object The mutable JSON object to write into.
* @return Error state if any.
*/
errorret_t saveSlotVer1WriteJSON(
saveslotver1_t *slot,
yyjson_mut_doc *doc,
yyjson_mut_val *object
);
/**
* Reads v1 save slot data in from the given JSON object. Throws if the
* JSON's "version" is missing or doesn't match SAVE_SLOT_CURRENT.
*
* @param slot The v1 save slot data to read into.
* @param object The JSON object to read from.
* @return Error state if any.
*/
errorret_t saveSlotVer1ReadJSON(saveslotver1_t *slot, yyjson_val *object);
@@ -3,8 +3,7 @@
# 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
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME} target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC PUBLIC
assetchunkloader.c scene.c
) )
+75
View File
@@ -0,0 +1,75 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "scene.h"
#include "entity/entity.h"
#include "entity/entityrender.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "time/time.h"
scenemanager_t SCENE_MANAGER;
errorret_t sceneInit(void) {
memoryZero(&SCENE_MANAGER, sizeof(scenemanager_t));
SCENE_MANAGER.active = SCENE_ID_INVALID;
errorOk();
}
sceneid_t sceneCreate(void) {
assertTrue(!SCENE_MANAGER.scene.used, "Scene is already in use");
SCENE_MANAGER.scene.used = true;
return 0;
}
void sceneDestroy(const sceneid_t id) {
assertTrue(id == 0, "Scene ID OOB");
if(!SCENE_MANAGER.scene.used) return;
SCENE_MANAGER.scene.used = false;
if(SCENE_MANAGER.active == id) SCENE_MANAGER.active = SCENE_ID_INVALID;
}
void sceneSetActive(const sceneid_t id) {
assertTrue(
id == SCENE_ID_INVALID || id == 0, "Scene ID OOB"
);
SCENE_MANAGER.active = id;
}
sceneid_t sceneGetActive(void) {
return SCENE_MANAGER.active;
}
errorret_t sceneUpdate(void) {
#if DUSK_TIME_DYNAMIC
if(TIME.dynamicUpdate) errorOk();
#endif
errorChain(sceneFixedUpdate());
errorOk();
}
errorret_t sceneFixedUpdate(void) {
if(SCENE_MANAGER.active == SCENE_ID_INVALID) errorOk();
entityManagerUpdate();
errorOk();
}
errorret_t sceneRender(void) {
if(SCENE_MANAGER.active != SCENE_ID_INVALID) {
errorChain(entityRenderAll());
}
errorOk();
}
errorret_t sceneDispose(void) {
sceneDestroy(0);
errorOk();
}
+106
View File
@@ -0,0 +1,106 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "scenebase.h"
#include "error/error.h"
typedef struct {
bool_t used;
} scene_t;
typedef struct {
scene_t scene;
sceneid_t active;
} scenemanager_t;
extern scenemanager_t SCENE_MANAGER;
/**
* Initialises the scene manager. No scenes are created or active until
* sceneCreate()/sceneSetActive() are called.
*
* @return An error if the init failed, or errorOk() if it succeeded.
*/
errorret_t sceneInit(void);
/**
* Creates the single scene. There is no per-scene entity pool -- entities
* are global (see entity.h) -- so this just marks the scene as used. Not
* made active automatically.
*
* @return The ID of the new scene (always 0).
*/
sceneid_t sceneCreate(void);
/**
* Destroys the scene. Does not touch the global entity arrays -- entity
* lifecycle is the engine's job (entityManagerInit()/entityManagerDispose()),
* not scoped to scene create/destroy. If the scene was active, no scene is
* active afterwards.
*
* @param id The ID of the scene to destroy. Must be 0.
*/
void sceneDestroy(const sceneid_t id);
/**
* Sets which scene is active. Only the active scene is ticked and rendered
* by sceneUpdate()/sceneRender(). Previously active scenes are left running
* in the background (paused, not disposed) until sceneDestroy() is called.
*
* @param id The ID of the scene to make active, or SCENE_ID_INVALID to make
* no scene active.
*/
void sceneSetActive(const sceneid_t id);
/**
* Gets the ID of the currently active scene.
*
* @return The active scene ID, or SCENE_ID_INVALID if none is active.
*/
sceneid_t sceneGetActive(void);
/**
* Called every frame (every call to engineUpdate). On a non-dynamic-time
* platform (DUSK_TIME_DYNAMIC undefined) every frame is itself a fixed
* timestep, so this calls sceneFixedUpdate() every time. On a
* dynamic-time platform, most calls are sub-steps still accumulating
* toward the next fixed timestep (TIME.dynamicUpdate is true) and do
* nothing here; this calls sceneFixedUpdate() only on the one call where
* TIME.dynamicUpdate is false, i.e. exactly once per fixed timestep.
*
* @return An error if the update failed, or errorOk() if it succeeded.
*/
errorret_t sceneUpdate(void);
/**
* Ticks the active scene's entities (update callbacks) by one fixed
* timestep (TIME.delta). Called by sceneUpdate() at the fixed-timestep
* cadence described there -- never once per rendered frame on a
* dynamic-time platform, so game logic stays frame-rate independent and
* deterministic. Does not step physics or triggers yet -- physicsworld_t/
* triggersystem_t haven't been ported from ac2 onto this branch, so a
* scene's entities can't collide or trigger each other until that lands.
*
* @return An error if the update failed, or errorOk() if it succeeded.
*/
errorret_t sceneFixedUpdate(void);
/**
* Renders the active scene's entities, from the view of its active
* camera (see entityRenderAll).
*
* @return An error if the render failed, or errorOk() if it succeeded.
*/
errorret_t sceneRender(void);
/**
* Destroys the scene.
*
* @return An error if the dispose failed, or errorOk() if it succeeded.
*/
errorret_t sceneDispose(void);
+13
View File
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
#define SCENE_ID_INVALID 0xFF
typedef uint8_t sceneid_t;
+3
View File
@@ -7,3 +7,6 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC PUBLIC
scriptnativeconsole.c scriptnativeconsole.c
) )
# Subdirs
add_subdirectory(entity)
@@ -3,10 +3,10 @@
# 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
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME} target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC PUBLIC
saveslot.c scriptnativeentity.c
saveslotcurrent.c scriptnativeentitymanager.c
saveslotver1.c scriptnativeentityposition.c
scriptnativeentityrenderable.c
) )
@@ -0,0 +1,126 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "scriptnativeentity.h"
#include "entity/entity.h"
#include "util/memory.h"
#include "assert/assert.h"
SCRIPT_NATIVE(scriptNativeEntityAddComponent) {
entityid_t entityId;
componenttype_t type;
componentid_t componentId;
scriptArgCount(runtime, argc, 2, 2);
entityId = (entityid_t)scriptArgInteger(runtime, args, argc, 0);
type = (componenttype_t)scriptArgInteger(runtime, args, argc, 1);
componentId = entityAddComponent(entityId, type);
return scriptNumber((float)componentId);
}
SCRIPT_NATIVE(scriptNativeEntityGetComponent) {
entityid_t entityId;
componenttype_t type;
componentid_t componentId;
scriptArgCount(runtime, argc, 2, 2);
entityId = (entityid_t)scriptArgInteger(runtime, args, argc, 0);
type = (componenttype_t)scriptArgInteger(runtime, args, argc, 1);
componentId = entityGetComponent(entityId, type);
return scriptNumber((float)componentId);
}
SCRIPT_NATIVE(scriptNativeEntitySetName) {
entityid_t entityId;
scriptstring_t *name;
char_t buffer[ENTITY_NAME_MAX];
uint16_t length;
scriptArgCount(runtime, argc, 2, 2);
entityId = (entityid_t)scriptArgInteger(runtime, args, argc, 0);
name = scriptArgString(runtime, args, argc, 1);
// name->data isn't null terminated - entitySetName needs a C string.
length = name->length;
if(length > ENTITY_NAME_MAX - 1) length = ENTITY_NAME_MAX - 1;
if(length) memoryCopy(buffer, name->data, length);
buffer[length] = 0;
entitySetName(entityId, buffer);
return scriptUndefined();
}
SCRIPT_NATIVE(scriptNativeEntityGetName) {
entityid_t entityId;
scriptArgCount(runtime, argc, 1, 1);
entityId = (entityid_t)scriptArgInteger(runtime, args, argc, 0);
return scriptStringFromC(
runtime, entityGetName(entityId)
);
}
SCRIPT_NATIVE(scriptNativeEntityFindByName) {
scriptstring_t *name;
char_t buffer[ENTITY_NAME_MAX];
uint16_t length;
entityid_t entityId;
scriptArgCount(runtime, argc, 1, 1);
name = scriptArgString(runtime, args, argc, 0);
length = name->length;
if(length > ENTITY_NAME_MAX - 1) length = ENTITY_NAME_MAX - 1;
if(length) memoryCopy(buffer, name->data, length);
buffer[length] = 0;
entityId = entityFindByName(buffer);
return scriptNumber((float)entityId);
}
SCRIPT_NATIVE(scriptNativeEntityDispose) {
entityid_t entityId;
scriptArgCount(runtime, argc, 1, 1);
entityId = (entityid_t)scriptArgInteger(runtime, args, argc, 0);
entityDispose(entityId);
return scriptUndefined();
}
SCRIPT_NATIVE(scriptNativeEntityDisposeDeep) {
entityid_t entityId;
scriptArgCount(runtime, argc, 1, 1);
entityId = (entityid_t)scriptArgInteger(runtime, args, argc, 0);
entityDisposeDeep(entityId);
return scriptUndefined();
}
void scriptNativeEntityRegister(scriptruntime_t *runtime) {
assertNotNull(runtime, "scriptNativeEntityRegister needs a runtime, not NULL");
scriptBuiltinAdd(runtime, "entityAddComponent", scriptNativeEntityAddComponent);
scriptBuiltinAdd(runtime, "entityGetComponent", scriptNativeEntityGetComponent);
scriptBuiltinAdd(runtime, "entitySetName", scriptNativeEntitySetName);
scriptBuiltinAdd(runtime, "entityGetName", scriptNativeEntityGetName);
scriptBuiltinAdd(runtime, "entityFindByName", scriptNativeEntityFindByName);
scriptBuiltinAdd(runtime, "entityDispose", scriptNativeEntityDispose);
scriptBuiltinAdd(runtime, "entityDisposeDeep", scriptNativeEntityDisposeDeep);
scriptBuiltinAddInt(runtime, "ENTITY_ID_INVALID", ENTITY_ID_INVALID);
scriptBuiltinAddInt(runtime, "COMPONENT_ID_INVALID", COMPONENT_ID_INVALID);
#define X(enumName, type, field, init, dispose, render) \
scriptBuiltinAddInt(runtime, "COMPONENT_TYPE_" #enumName, COMPONENT_TYPE_##enumName);
#include "entity/componentlist.h"
#undef X
}
@@ -0,0 +1,107 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/script.h"
#include "entity/entity.h"
/**
* Registers the entity natives (entityAddComponent, entityGetComponent,
* entitySetName, entityGetName, entityFindByName, entityDispose,
* entityDisposeDeep) and the ENTITY_ID_INVALID/COMPONENT_ID_INVALID and
* COMPONENT_TYPE_* constants they take, against the engine's global
* entity/component arrays (entity.h). See scriptNativeEntityManagerRegister
* for the entityAdd() native.
*
* @param runtime The runtime to add them to.
*/
void scriptNativeEntityRegister(scriptruntime_t *runtime);
/**
* Native implementation of entityAddComponent(entityId, type): adds a
* component of the given COMPONENT_TYPE_* to the entity.
*
* @param runtime The runtime.
* @param args The arguments: an entity ID and a component type.
* @param argc Number of arguments.
* @param self The native function value.
* @return The new component's ID.
*/
SCRIPT_NATIVE(scriptNativeEntityAddComponent);
/**
* Native implementation of entityGetComponent(entityId, type): finds the
* entity's component of the given COMPONENT_TYPE_*, or COMPONENT_ID_INVALID
* if it has none.
*
* @param runtime The runtime.
* @param args The arguments: an entity ID and a component type.
* @param argc Number of arguments.
* @param self The native function value.
* @return The component's ID, or COMPONENT_ID_INVALID.
*/
SCRIPT_NATIVE(scriptNativeEntityGetComponent);
/**
* Native implementation of entitySetName(entityId, name): names an entity,
* making it referenceable by entityFindByName.
*
* @param runtime The runtime.
* @param args The arguments: an entity ID and a name.
* @param argc Number of arguments.
* @param self The native function value.
* @return undefined.
*/
SCRIPT_NATIVE(scriptNativeEntitySetName);
/**
* Native implementation of entityGetName(entityId): gets an entity's name,
* or an empty string if it has none.
*
* @param runtime The runtime.
* @param args The arguments: an entity ID.
* @param argc Number of arguments.
* @param self The native function value.
* @return The entity's name.
*/
SCRIPT_NATIVE(scriptNativeEntityGetName);
/**
* Native implementation of entityFindByName(name): finds the first active
* entity with the given name, or ENTITY_ID_INVALID if none matches.
*
* @param runtime The runtime.
* @param args The arguments: a name.
* @param argc Number of arguments.
* @param self The native function value.
* @return The entity's ID, or ENTITY_ID_INVALID.
*/
SCRIPT_NATIVE(scriptNativeEntityFindByName);
/**
* Native implementation of entityDispose(entityId): disposes an entity and
* its components.
*
* @param runtime The runtime.
* @param args The arguments: an entity ID.
* @param argc Number of arguments.
* @param self The native function value.
* @return undefined.
*/
SCRIPT_NATIVE(scriptNativeEntityDispose);
/**
* Native implementation of entityDisposeDeep(entityId): disposes an entity
* and, recursively, every position-component descendant of it.
*
* @param runtime The runtime.
* @param args The arguments: an entity ID.
* @param argc Number of arguments.
* @param self The native function value.
* @return undefined.
*/
SCRIPT_NATIVE(scriptNativeEntityDisposeDeep);
@@ -0,0 +1,20 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "scriptnativeentitymanager.h"
#include "assert/assert.h"
SCRIPT_NATIVE(scriptNativeEntityAdd) {
scriptArgCount(runtime, argc, 0, 0);
return scriptNumber((float)entityManagerAdd());
}
void scriptNativeEntityManagerRegister(scriptruntime_t *runtime) {
assertNotNull(runtime, "scriptNativeEntityManagerRegister needs a runtime, not NULL");
scriptBuiltinAdd(runtime, "entityAdd", scriptNativeEntityAdd);
}
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/script.h"
#include "entity/entity.h"
/**
* Registers the entityAdd() native.
*
* @param runtime The runtime to add it to.
*/
void scriptNativeEntityManagerRegister(scriptruntime_t *runtime);
/**
* Native implementation of entityAdd(): reserves a new entity ID.
*
* @param runtime The runtime.
* @param args Unused, takes no arguments.
* @param argc Number of arguments.
* @param self The native function value.
* @return The new entity's ID.
*/
SCRIPT_NATIVE(scriptNativeEntityAdd);
@@ -0,0 +1,61 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "scriptnativeentityposition.h"
#include "entity/entity.h"
#include "assert/assert.h"
SCRIPT_NATIVE(scriptNativeEntityPositionSetLocalPosition) {
entityid_t entityId;
componentid_t componentId;
vec3 position;
scriptArgCount(runtime, argc, 5, 5);
entityId = (entityid_t)scriptArgInteger(runtime, args, argc, 0);
componentId = (componentid_t)scriptArgInteger(runtime, args, argc, 1);
position[0] = scriptArgNumber(runtime, args, argc, 2).as.number;
position[1] = scriptArgNumber(runtime, args, argc, 3).as.number;
position[2] = scriptArgNumber(runtime, args, argc, 4).as.number;
entityPositionSetLocalPosition(
entityId, componentId, position
);
return scriptUndefined();
}
SCRIPT_NATIVE(scriptNativeEntityPositionLookAt) {
entityid_t entityId;
componentid_t componentId;
vec3 eye;
vec3 target;
vec3 up = { 0.0f, 1.0f, 0.0f };
scriptArgCount(runtime, argc, 8, 8);
entityId = (entityid_t)scriptArgInteger(runtime, args, argc, 0);
componentId = (componentid_t)scriptArgInteger(runtime, args, argc, 1);
eye[0] = scriptArgNumber(runtime, args, argc, 2).as.number;
eye[1] = scriptArgNumber(runtime, args, argc, 3).as.number;
eye[2] = scriptArgNumber(runtime, args, argc, 4).as.number;
target[0] = scriptArgNumber(runtime, args, argc, 5).as.number;
target[1] = scriptArgNumber(runtime, args, argc, 6).as.number;
target[2] = scriptArgNumber(runtime, args, argc, 7).as.number;
entityPositionLookAt(entityId, componentId, eye, target, up);
return scriptUndefined();
}
void scriptNativeEntityPositionRegister(scriptruntime_t *runtime) {
assertNotNull(runtime, "scriptNativeEntityPositionRegister needs a runtime, not NULL");
scriptBuiltinAdd(
runtime, "entityPositionSetLocalPosition",
scriptNativeEntityPositionSetLocalPosition
);
scriptBuiltinAdd(
runtime, "entityPositionLookAt", scriptNativeEntityPositionLookAt
);
}
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/script.h"
#include "entity/component/display/entityposition.h"
/**
* Registers the entityPositionSetLocalPosition and entityPositionLookAt
* natives.
*
* @param runtime The runtime to add it to.
*/
void scriptNativeEntityPositionRegister(scriptruntime_t *runtime);
/**
* Native implementation of
* entityPositionSetLocalPosition(entityId, componentId, x, y, z).
*
* @param runtime The runtime.
* @param args The arguments: an entity ID, a position component ID, and x/y/z.
* @param argc Number of arguments.
* @param self The native function value.
* @return undefined.
*/
SCRIPT_NATIVE(scriptNativeEntityPositionSetLocalPosition);
/**
* Native implementation of
* entityPositionLookAt(entityId, componentId, eyeX, eyeY, eyeZ, targetX,
* targetY, targetZ): places the entity at eye, facing target, with world
* up (0, 1, 0) - SCRIPT_PARAMS_MAX leaves no room for an up argument too.
*
* @param runtime The runtime.
* @param args The arguments: an entity ID, a position component ID, and the
* eye and target points.
* @param argc Number of arguments.
* @param self The native function value.
* @return undefined.
*/
SCRIPT_NATIVE(scriptNativeEntityPositionLookAt);
@@ -0,0 +1,71 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "scriptnativeentityrenderable.h"
#include "entity/entity.h"
#include "error/scriptraise.h"
#include "display/mesh/cube.h"
#include "display/mesh/plane.h"
#include "display/mesh/sphere.h"
#include "display/mesh/capsule.h"
#include "display/mesh/triprism.h"
#include "display/mesh/quad.h"
#include "util/memory.h"
#include "util/string.h"
#include "assert/assert.h"
#define SCRIPT_NATIVE_ENTITY_RENDERABLE_MESH_NAME_MAX 16
// Scripts don't have mesh assets to point at yet, so this just names the
// engine's built in primitive shapes.
static mesh_t *scriptNativeEntityRenderableResolveMesh(const char_t *name) {
if(stringEquals(name, "cube")) return &CUBE_MESH_SIMPLE;
if(stringEquals(name, "plane")) return &PLANE_MESH_SIMPLE;
if(stringEquals(name, "sphere")) return &SPHERE_MESH_SIMPLE;
if(stringEquals(name, "capsule")) return &CAPSULE_MESH_SIMPLE;
if(stringEquals(name, "triprism")) return &TRIPRISM_MESH_SIMPLE;
if(stringEquals(name, "quad")) return &QUAD_MESH_SIMPLE;
return NULL;
}
SCRIPT_NATIVE(scriptNativeEntityRenderableSetMesh) {
entityid_t entityId;
componentid_t componentId;
scriptstring_t *nameArg;
char_t name[SCRIPT_NATIVE_ENTITY_RENDERABLE_MESH_NAME_MAX];
uint16_t length;
mesh_t *mesh;
scriptArgCount(runtime, argc, 3, 3);
entityId = (entityid_t)scriptArgInteger(runtime, args, argc, 0);
componentId = (componentid_t)scriptArgInteger(runtime, args, argc, 1);
nameArg = scriptArgString(runtime, args, argc, 2);
// nameArg->data isn't null terminated - need a C string to compare.
length = nameArg->length;
if(length > SCRIPT_NATIVE_ENTITY_RENDERABLE_MESH_NAME_MAX - 1) {
length = SCRIPT_NATIVE_ENTITY_RENDERABLE_MESH_NAME_MAX - 1;
}
if(length) memoryCopy(name, nameArg->data, length);
name[length] = 0;
mesh = scriptNativeEntityRenderableResolveMesh(name);
if(!mesh) scriptRaise(runtime, "Unknown mesh '%s'", name);
entityRenderableSetMesh(
entityId, componentId, 0, mesh
);
return scriptUndefined();
}
void scriptNativeEntityRenderableRegister(scriptruntime_t *runtime) {
assertNotNull(runtime, "scriptNativeEntityRenderableRegister needs a runtime, not NULL");
scriptBuiltinAdd(
runtime, "entityRenderableSetMesh", scriptNativeEntityRenderableSetMesh
);
}
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/script.h"
#include "entity/component/display/entityrenderable.h"
/**
* Registers the entityRenderableSetMesh native.
*
* @param runtime The runtime to add it to.
*/
void scriptNativeEntityRenderableRegister(scriptruntime_t *runtime);
/**
* Native implementation of
* entityRenderableSetMesh(entityId, componentId, name): sets slot 0 of a
* material renderable to one of the engine's built in primitive meshes, by
* name - "cube", "plane", "sphere", "capsule", "triprism" or "quad".
*
* @param runtime The runtime.
* @param args The arguments: an entity ID, a renderable component ID, and a
* mesh name.
* @param argc Number of arguments.
* @param self The native function value.
* @return undefined.
*/
SCRIPT_NATIVE(scriptNativeEntityRenderableSetMesh);
+8
View File
@@ -7,6 +7,10 @@
#include "script.h" #include "script.h"
#include "native/scriptnativeconsole.h" #include "native/scriptnativeconsole.h"
#include "native/entity/scriptnativeentity.h"
#include "native/entity/scriptnativeentitymanager.h"
#include "native/entity/scriptnativeentityposition.h"
#include "native/entity/scriptnativeentityrenderable.h"
#include "builtin/scriptbuiltinregister.h" #include "builtin/scriptbuiltinregister.h"
#include "console/console.h" #include "console/console.h"
#include "asset/asset.h" #include "asset/asset.h"
@@ -68,6 +72,10 @@ errorret_t scriptInit(void) {
scriptBuiltinsRegister(SCRIPT.runtime); scriptBuiltinsRegister(SCRIPT.runtime);
scriptNativeConsoleRegister(SCRIPT.runtime); scriptNativeConsoleRegister(SCRIPT.runtime);
scriptNativeEntityManagerRegister(SCRIPT.runtime);
scriptNativeEntityRegister(SCRIPT.runtime);
scriptNativeEntityPositionRegister(SCRIPT.runtime);
scriptNativeEntityRenderableRegister(SCRIPT.runtime);
scriptVmAttach(SCRIPT.runtime); scriptVmAttach(SCRIPT.runtime);
scriptRuntimeSetPrintHandler(SCRIPT.runtime, scriptPrintHandler); scriptRuntimeSetPrintHandler(SCRIPT.runtime, scriptPrintHandler);
scriptRuntimeSetLoadHandler(SCRIPT.runtime, scriptLoadHandler); scriptRuntimeSetLoadHandler(SCRIPT.runtime, scriptLoadHandler);
-87
View File
@@ -7,7 +7,6 @@
#include "save/savedevice.h" #include "save/savedevice.h"
#include "save/savedevicelinux.h" #include "save/savedevicelinux.h"
#include "save/slot/saveslot.h"
#include "save/settings/savesettings.h" #include "save/settings/savesettings.h"
#include "save/savejson.h" #include "save/savejson.h"
#include "asset/asset.h" #include "asset/asset.h"
@@ -86,92 +85,6 @@ errorret_t saveDeviceLinuxGetSettingsPath(
errorOk(); errorOk();
} }
errorret_t saveDeviceLinuxGetSlotPath(
char_t *dest,
const size_t destSize,
const uint8_t slotIndex
) {
assertNotNull(dest, "dest cannot be null");
char_t directory[SAVE_DEVICE_LINUX_PATH_MAX];
errorChain(saveDeviceLinuxGetDirectory(directory, sizeof(directory)));
char_t filename[SAVE_SLOT_NAME_LENGTH + 16];
stringFormat(
filename, sizeof(filename) - 1,
SAVE_DEVICE_LINUX_SLOT_FILENAME_FORMAT, (uint32_t)slotIndex
);
stringFormat(dest, destSize, "%s/%s", directory, filename);
errorOk();
}
errorret_t saveDeviceLinuxSlotWrite(
savedevice_t *device,
saveslot_t *slot,
uint8_t slotIndex
) {
assertNotNull(device, "device cannot be null");
assertNotNull(slot, "slot cannot be null");
char_t path[SAVE_DEVICE_LINUX_PATH_MAX];
errorChain(saveDeviceLinuxGetSlotPath(path, sizeof(path), slotIndex));
writeInit();
errorret_t writeResult = saveSlotWriteJSON(slot, doc, object);
if(errorIsNotOk(writeResult)) {
yyjson_mut_doc_free(doc);
errorChain(writeResult);
}
yyjson_write_err writeErr;
bool_t written = yyjson_mut_write_file(path, doc, 0, NULL, &writeErr);
yyjson_mut_doc_free(doc);
if(!written) {
errorThrow(
"Failed to write save slot file: %s (%s)", path, writeErr.msg
);
}
errorOk();
}
errorret_t saveDeviceLinuxSlotRead(
savedevice_t *device,
saveslot_t *slot,
uint8_t slotIndex
) {
assertNotNull(device, "device cannot be null");
assertNotNull(slot, "slot cannot be null");
char_t path[SAVE_DEVICE_LINUX_PATH_MAX];
errorChain(saveDeviceLinuxGetSlotPath(path, sizeof(path), slotIndex));
yyjson_read_err readErr;
yyjson_doc *jsonDoc = yyjson_read_file(path, 0, NULL, &readErr);
if(jsonDoc == NULL) {
// No save file at this slot yet, leave the slot as-is.
if(readErr.code == YYJSON_READ_ERROR_FILE_OPEN) errorOk();
errorThrow(
"Failed to parse save slot file: %s (%s)", path, readErr.msg
);
}
yyjson_val *object = yyjson_doc_get_root(jsonDoc);
if(object == NULL) {
yyjson_doc_free(jsonDoc);
errorThrow("Save slot file missing root object: %s", path);
}
errorret_t readResult = saveSlotReadJSON(slot, object);
yyjson_doc_free(jsonDoc);
errorChain(readResult);
errorOk();
}
errorret_t saveDeviceLinuxSettingsWrite( errorret_t saveDeviceLinuxSettingsWrite(
savedevice_t *device, savedevice_t *device,
savesettings_t *settings savesettings_t *settings
-45
View File
@@ -14,7 +14,6 @@
// settings and each save slot as their own separate file. // settings and each save slot as their own separate file.
#define SAVE_DEVICE_LINUX_SAVES_SUBDIR_NAME "saves" #define SAVE_DEVICE_LINUX_SAVES_SUBDIR_NAME "saves"
#define SAVE_DEVICE_LINUX_SETTINGS_FILENAME "settings.json" #define SAVE_DEVICE_LINUX_SETTINGS_FILENAME "settings.json"
#define SAVE_DEVICE_LINUX_SLOT_FILENAME_FORMAT "slot%u.json"
#define SAVE_DEVICE_LINUX_PATH_MAX FILENAME_MAX #define SAVE_DEVICE_LINUX_PATH_MAX FILENAME_MAX
typedef struct { typedef struct {
@@ -22,7 +21,6 @@ typedef struct {
} savedeviceplatform_t; } savedeviceplatform_t;
typedef struct savedevice_s savedevice_t; typedef struct savedevice_s savedevice_t;
typedef struct saveslot_s saveslot_t;
typedef struct savesettings_s savesettings_t; typedef struct savesettings_s savesettings_t;
/** /**
@@ -71,49 +69,6 @@ errorret_t saveDeviceLinuxGetSettingsPath(
const size_t destSize const size_t destSize
); );
/**
* Builds the absolute path to the given save slot's file.
*
* @param dest The destination buffer to write the path into.
* @param destSize The size of the destination buffer, exc. null terminator.
* @param slotIndex The slot index to build the path for.
* @return Error state if any.
*/
errorret_t saveDeviceLinuxGetSlotPath(
char_t *dest,
const size_t destSize,
const uint8_t slotIndex
);
/**
* Writes the given save slot out to its file on disk.
*
* @param device The save device to write to.
* @param slot The save slot to write.
* @param slotIndex The slot index to write to.
* @return Error state if any.
*/
errorret_t saveDeviceLinuxSlotWrite(
savedevice_t *device,
saveslot_t *slot,
uint8_t slotIndex
);
/**
* Reads a save slot in from its file on disk. If no file exists yet for the
* given slot index, this is not an error - the slot is simply left as-is.
*
* @param device The save device to read from.
* @param slot The save slot to read into.
* @param slotIndex The slot index to read from.
* @return Error state if any.
*/
errorret_t saveDeviceLinuxSlotRead(
savedevice_t *device,
saveslot_t *slot,
uint8_t slotIndex
);
/** /**
* Writes the given save settings out to the settings file on disk. * Writes the given save settings out to the settings file on disk.
* *
-2
View File
@@ -14,7 +14,5 @@
#define saveDevicePlatformUpdate saveDeviceLinuxUpdate #define saveDevicePlatformUpdate saveDeviceLinuxUpdate
#define saveDeviceCheckAvailabilityPlatform saveDeviceLinuxCheckAvailability #define saveDeviceCheckAvailabilityPlatform saveDeviceLinuxCheckAvailability
#define saveDevicePlatformDispose saveDeviceLinuxDispose #define saveDevicePlatformDispose saveDeviceLinuxDispose
#define saveDeviceSlotWritePlatform saveDeviceLinuxSlotWrite
#define saveDeviceSlotReadPlatform saveDeviceLinuxSlotRead
#define saveDeviceSettingsWritePlatform saveDeviceLinuxSettingsWrite #define saveDeviceSettingsWritePlatform saveDeviceLinuxSettingsWrite
#define saveDeviceSettingsReadPlatform saveDeviceLinuxSettingsRead #define saveDeviceSettingsReadPlatform saveDeviceLinuxSettingsRead
-2
View File
@@ -13,8 +13,6 @@ endif()
add_subdirectory(error) add_subdirectory(error)
add_subdirectory(thread) add_subdirectory(thread)
add_subdirectory(display) add_subdirectory(display)
add_subdirectory(rpg)
add_subdirectory(item)
add_subdirectory(save) add_subdirectory(save)
add_subdirectory(time) add_subdirectory(time)
add_subdirectory(util) add_subdirectory(util)
-2
View File
@@ -7,8 +7,6 @@ include(dusktest)
# Tests # Tests
dusktest(test_savesettings.c) dusktest(test_savesettings.c)
dusktest(test_saveslot.c)
target_sources(test_saveslot PRIVATE savetestfixture.c)
dusktest(test_savedevicelinux.c) dusktest(test_savedevicelinux.c)
target_sources(test_savedevicelinux PRIVATE savetestfixture.c) target_sources(test_savedevicelinux PRIVATE savetestfixture.c)
-52
View File
@@ -10,9 +10,6 @@
#include "savetestfixture.h" #include "savetestfixture.h"
#include "assert/assert.h" #include "assert/assert.h"
#include "asset/asset.h"
#include "asset/loader/assetloader.h"
#include "asset/loader/assetentry.h"
#include "util/memory.h" #include "util/memory.h"
#include "util/string.h" #include "util/string.h"
#include <ftw.h> #include <ftw.h>
@@ -25,51 +22,6 @@ static char_t SAVE_TEST_FIXTURE_HOME[PATH_MAX];
static char_t SAVE_TEST_FIXTURE_REAL_HOME[PATH_MAX]; static char_t SAVE_TEST_FIXTURE_REAL_HOME[PATH_MAX];
static bool_t SAVE_TEST_FIXTURE_HAD_REAL_HOME; static bool_t SAVE_TEST_FIXTURE_HAD_REAL_HOME;
static const char_t *SAVE_TEST_FIXTURE_PARTY_JSON =
"{\"members\":{\"hero\":{"
"\"attack\":10,\"defense\":5,\"magic\":0,\"speed\":10,\"luck\":0,"
"\"healthMax\":30,\"mpMax\":10"
"}}}";
static yyjson_doc *SAVE_TEST_FIXTURE_PARTY_DOC = NULL;
errorret_t saveTestFixturePartyJsonLoadSyncStub(assetloading_t *loading) {
loading->entry->data.json = SAVE_TEST_FIXTURE_PARTY_DOC;
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
errorOk();
}
errorret_t saveTestFixturePartyJsonDisposeStub(assetentry_t *entry) {
errorOk();
}
void saveTestFixtureStubPartyAsset(void) {
memoryZero(&ASSET, sizeof(ASSET));
for(size_t i = 0; i < ASSET_LOADING_COUNT_MAX; i++) {
threadMutexInit(&ASSET.loading[i].mutex);
}
if(SAVE_TEST_FIXTURE_PARTY_DOC == NULL) {
SAVE_TEST_FIXTURE_PARTY_DOC = yyjson_read(
SAVE_TEST_FIXTURE_PARTY_JSON, strlen(SAVE_TEST_FIXTURE_PARTY_JSON), 0
);
assertNotNull(
SAVE_TEST_FIXTURE_PARTY_DOC, "Failed to parse party JSON fixture"
);
}
ASSET_LOADER_CALLBACKS[ASSET_LOADER_TYPE_JSON].loadSync =
saveTestFixturePartyJsonLoadSyncStub;
ASSET_LOADER_CALLBACKS[ASSET_LOADER_TYPE_JSON].dispose =
saveTestFixturePartyJsonDisposeStub;
}
void saveTestFixtureUnstubPartyAsset(void) {
if(SAVE_TEST_FIXTURE_PARTY_DOC == NULL) return;
yyjson_doc_free(SAVE_TEST_FIXTURE_PARTY_DOC);
SAVE_TEST_FIXTURE_PARTY_DOC = NULL;
}
static int_t saveTestFixtureRemoveEntry( static int_t saveTestFixtureRemoveEntry(
const char *path, const char *path,
const struct stat *statBuffer, const struct stat *statBuffer,
@@ -97,8 +49,6 @@ int saveTestFixtureSetup(void **state) {
assertTrue(setenv("HOME", SAVE_TEST_FIXTURE_HOME, 1) == 0, "setenv failed"); assertTrue(setenv("HOME", SAVE_TEST_FIXTURE_HOME, 1) == 0, "setenv failed");
saveTestFixtureStubPartyAsset();
errorret_t ret = saveInit(); errorret_t ret = saveInit();
assertTrue(errorIsOk(ret), "saveInit failed in test fixture"); assertTrue(errorIsOk(ret), "saveInit failed in test fixture");
@@ -106,8 +56,6 @@ int saveTestFixtureSetup(void **state) {
} }
int saveTestFixtureTeardown(void **state) { int saveTestFixtureTeardown(void **state) {
saveTestFixtureUnstubPartyAsset();
nftw( nftw(
SAVE_TEST_FIXTURE_HOME, saveTestFixtureRemoveEntry, 16, SAVE_TEST_FIXTURE_HOME, saveTestFixtureRemoveEntry, 16,
FTW_DEPTH | FTW_PHYS FTW_DEPTH | FTW_PHYS
-18
View File
@@ -35,21 +35,3 @@ int saveTestFixtureTeardown(void **state);
* filesystem permission tricks. Must be called after saveTestFixtureSetup. * filesystem permission tricks. Must be called after saveTestFixtureSetup.
*/ */
void saveTestFixtureBlockSaveDirectory(void); void saveTestFixtureBlockSaveDirectory(void);
/**
* Stubs the JSON asset loader's sync callback to hand back a small
* in-memory party.jsonc-shaped doc instead of doing a real file/zip read -
* saveInit()/saveSlotInit() seed the party roster via partyInit(), which
* loads party.jsonc through the real asset system, and there's no real
* asset bundle available under the test harness. No thread, no real I/O.
* Resets ASSET to a fresh, otherwise-empty state each call, so it's safe
* to call once per test. Called automatically by saveTestFixtureSetup.
*/
void saveTestFixtureStubPartyAsset(void);
/**
* Frees the in-memory doc created by saveTestFixtureStubPartyAsset. Safe
* to call even if that function was never called. Called automatically
* by saveTestFixtureTeardown.
*/
void saveTestFixtureUnstubPartyAsset(void);
-221
View File
@@ -8,7 +8,6 @@
#include "dusktest.h" #include "dusktest.h"
#include "savetestfixture.h" #include "savetestfixture.h"
#include "save/save.h" #include "save/save.h"
#include "save/savedevicelinux.h"
#include "util/memory.h" #include "util/memory.h"
#include "util/string.h" #include "util/string.h"
#include <stdio.h> #include <stdio.h>
@@ -54,16 +53,10 @@ static void test_saveInit_setsDefaults(void **state) {
// saveTestFixtureSetup already called the real saveInit() - just assert // saveTestFixtureSetup already called the real saveInit() - just assert
// on the state it left behind. // on the state it left behind.
assert_int_equal(SAVE.deviceCurrent, 0xFF); assert_int_equal(SAVE.deviceCurrent, 0xFF);
assert_int_equal(SAVE.slotCurrent, 0xFF);
assert_false(SAVE.findingAvailableDevice); assert_false(SAVE.findingAvailableDevice);
assert_false(SAVE.noAvailableDeviceFound); assert_false(SAVE.noAvailableDeviceFound);
assert_true(SAVE.settings.audioMasterVolume == 1.0f); assert_true(SAVE.settings.audioMasterVolume == 1.0f);
assert_int_equal(SAVE.devices[0].state, SAVE_DEVICE_STATE_UNKNOWN); assert_int_equal(SAVE.devices[0].state, SAVE_DEVICE_STATE_UNKNOWN);
for(uint8_t i = 0; i < SAVE_SLOT_COUNT; i++) {
assert_int_equal(SAVE.caches[i].name[0], '\0');
assert_int_equal(SAVE.caches[i].playerLevel, 0);
}
} }
// ============================================================ // ============================================================
@@ -281,180 +274,6 @@ static void test_saveLoadSettings_readsAndClearsDirtyFlag(void **state) {
assert_false(SAVE.settingsDirty); assert_false(SAVE.settingsDirty);
} }
// ============================================================
// saveSaveSlot / saveLoadSlot
// ============================================================
static void test_saveSaveSlot_noCurrentDeviceAsserts(void **state) {
expect_assert_failure(saveSaveSlot());
}
static void test_saveSaveSlot_invalidSlotIndexAsserts(void **state) {
makeDeviceAvailable();
// slotCurrent is still 0xFF (unset) from saveInit().
expect_assert_failure(saveSaveSlot());
}
static void test_saveSaveSlot_success(void **state) {
makeDeviceAvailable();
SAVE.slotCurrent = 1;
saveSlotInit(&SAVE.slot);
stringCopy(
SAVE.slot.cachedData.name, "Hero", sizeof(SAVE.slot.cachedData.name)
);
SAVE.slot.cachedData.playerLevel = 12;
SAVE.slotDirty = true;
errorret_t ret = saveSaveSlot();
assert_true(errorIsOk(ret));
assert_false(SAVE.slotDirty);
assert_true(stringEquals(SAVE.caches[1].name, "Hero"));
assert_int_equal(SAVE.caches[1].playerLevel, 12);
// No pre-init needed - saveSlotReadJSON() (via saveDeviceSlotRead) fully
// resets/overwrites its destination struct regardless of prior content.
saveslot_t onDisk;
errorret_t readRet = saveDeviceSlotRead(&SAVE.devices[0], &onDisk, 1);
assert_true(errorIsOk(readRet));
assert_true(stringEquals(onDisk.cachedData.name, "Hero"));
assert_int_equal(onDisk.cachedData.playerLevel, 12);
}
static void test_saveLoadSlot_noCurrentDeviceAsserts(void **state) {
expect_assert_failure(saveLoadSlot());
}
static void test_saveLoadSlot_invalidSlotIndexAsserts(void **state) {
makeDeviceAvailable();
expect_assert_failure(saveLoadSlot());
}
static void test_saveLoadSlot_success(void **state) {
makeDeviceAvailable();
// Write slot 2's on-disk file via SAVE.slot itself (saveSlotInit() only
// ever operates on SAVE.slot now), then reset it again below - the
// write and the load-under-test don't overlap in time.
SAVE.slotCurrent = 2;
saveSlotInit(&SAVE.slot);
stringCopy(
SAVE.slot.cachedData.name, "Zelda", sizeof(SAVE.slot.cachedData.name)
);
SAVE.slot.cachedData.playerLevel = 30;
errorret_t writeRet = saveDeviceSlotWrite(&SAVE.devices[0], &SAVE.slot, 2);
assert_true(errorIsOk(writeRet));
SAVE.slotCurrent = 2;
saveSlotInit(&SAVE.slot);
SAVE.slotDirty = true;
errorret_t ret = saveLoadSlot();
assert_true(errorIsOk(ret));
assert_false(SAVE.slotDirty);
assert_true(stringEquals(SAVE.slot.cachedData.name, "Zelda"));
assert_int_equal(SAVE.slot.cachedData.playerLevel, 30);
assert_true(stringEquals(SAVE.caches[2].name, "Zelda"));
assert_int_equal(SAVE.caches[2].playerLevel, 30);
}
static void test_saveLoadSlot_deviceReadFails_cacheStaysStale(void **state) {
makeDeviceAvailable();
char_t slotPath[512];
errorret_t pathRet = saveDeviceLinuxGetSlotPath(
slotPath, sizeof(slotPath), 0
);
assert_true(errorIsOk(pathRet));
FILE *file = fopen(slotPath, "w");
assert_non_null(file);
fprintf(file, "not valid json {{{");
fclose(file);
SAVE.slotCurrent = 0;
saveSlotInit(&SAVE.slot);
SAVE.caches[0].playerLevel = 77;// sentinel
errorret_t ret = saveLoadSlot();
assert_true(errorIsNotOk(ret));
errorCatch(ret);
// The device read failed before the cache-sync line ever ran.
assert_int_equal(SAVE.caches[0].playerLevel, 77);
}
// ============================================================
// saveLoadAllSlots
// ============================================================
static void test_saveLoadAllSlots_loadsAllAndEndsAtLastIndex(void **state) {
makeDeviceAvailable();
for(uint8_t i = 0; i < SAVE_SLOT_COUNT; i++) {
SAVE.slotCurrent = i;
saveSlotInit(&SAVE.slot);
SAVE.slot.cachedData.playerLevel = (int32_t)(i + 1);
errorret_t saveRet = saveSaveSlot();
assert_true(errorIsOk(saveRet));
}
errorret_t ret = saveLoadAllSlots();
assert_true(errorIsOk(ret));
assert_int_equal(SAVE.slotCurrent, SAVE_SLOT_COUNT - 1);
for(uint8_t i = 0; i < SAVE_SLOT_COUNT; i++) {
assert_int_equal(SAVE.caches[i].playerLevel, (int32_t)(i + 1));
}
}
static void test_saveLoadAllSlots_missingFileLeavesInitDefaults(void **state) {
makeDeviceAvailable();
SAVE.slotCurrent = 0;
saveSlotInit(&SAVE.slot);
SAVE.slot.cachedData.playerLevel = 5;
errorret_t saveRet = saveSaveSlot();
assert_true(errorIsOk(saveRet));
// Slots 1 and 2 are never written - no file exists for them.
errorret_t ret = saveLoadAllSlots();
assert_true(errorIsOk(ret));
assert_int_equal(SAVE.caches[0].playerLevel, 5);
// Missing-file slots fall back to saveSlotInit()'s default (0), not the
// JSON-missing-field default of 1 that a corrupt-but-present file with no
// playerLevel key would produce - these are two different defaults.
assert_int_equal(SAVE.caches[1].playerLevel, 0);
assert_int_equal(SAVE.caches[2].playerLevel, 0);
}
static void test_saveLoadAllSlots_middleSlotCorrupt_stopsAtFailingIndex(
void **state
) {
makeDeviceAvailable();
SAVE.slotCurrent = 0;
saveSlotInit(&SAVE.slot);
errorret_t saveRet = saveSaveSlot();
assert_true(errorIsOk(saveRet));
char_t slotPath[512];
errorret_t pathRet = saveDeviceLinuxGetSlotPath(
slotPath, sizeof(slotPath), 1
);
assert_true(errorIsOk(pathRet));
FILE *file = fopen(slotPath, "w");
assert_non_null(file);
fprintf(file, "not valid json {{{");
fclose(file);
errorret_t ret = saveLoadAllSlots();
assert_true(errorIsNotOk(ret));
errorCatch(ret);
// The loop bails out as soon as slot 1 fails - it never reaches slot 2.
assert_int_equal(SAVE.slotCurrent, 1);
}
// ============================================================ // ============================================================
// saveDispose // saveDispose
// ============================================================ // ============================================================
@@ -566,46 +385,6 @@ int main(void) {
saveTestFixtureSetup, saveTestFixtureTeardown saveTestFixtureSetup, saveTestFixtureTeardown
), ),
cmocka_unit_test_setup_teardown(
test_saveSaveSlot_noCurrentDeviceAsserts,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_saveSaveSlot_invalidSlotIndexAsserts,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_saveSaveSlot_success, saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_saveLoadSlot_noCurrentDeviceAsserts,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_saveLoadSlot_invalidSlotIndexAsserts,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_saveLoadSlot_success, saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_saveLoadSlot_deviceReadFails_cacheStaysStale,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_saveLoadAllSlots_loadsAllAndEndsAtLastIndex,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_saveLoadAllSlots_missingFileLeavesInitDefaults,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_saveLoadAllSlots_middleSlotCorrupt_stopsAtFailingIndex,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown( cmocka_unit_test_setup_teardown(
test_saveDispose_succeeds, saveTestFixtureSetup, saveTestFixtureTeardown test_saveDispose_succeeds, saveTestFixtureSetup, saveTestFixtureTeardown
), ),
+2 -60
View File
@@ -8,9 +8,7 @@
#include "dusktest.h" #include "dusktest.h"
#include "savetestfixture.h" #include "savetestfixture.h"
#include "save/savedevice.h" #include "save/savedevice.h"
#include "save/slot/saveslot.h"
#include "save/settings/savesettings.h" #include "save/settings/savesettings.h"
#include "save/save.h"
#include "util/memory.h" #include "util/memory.h"
#include "util/string.h" #include "util/string.h"
@@ -150,28 +148,10 @@ static void test_saveDeviceCheckAvailability_resolvesSynchronouslyOnLinux(
} }
// ============================================================ // ============================================================
// saveDeviceSlotWrite / saveDeviceSlotRead / saveDeviceSettingsWrite / // saveDeviceSettingsWrite / saveDeviceSettingsRead - dispatch to the
// saveDeviceSettingsRead - dispatch to the platform's four hooks. // platform's hooks.
// ============================================================ // ============================================================
static void test_saveDeviceSlotWrite_nullAsserts(void **state) {
savedevice_t device;
// Content is never touched - both calls assert on a NULL argument
// before either device or slot would actually be read.
saveslot_t slot;
expect_assert_failure(saveDeviceSlotWrite(NULL, &slot, 0));
expect_assert_failure(saveDeviceSlotWrite(&device, NULL, 0));
}
static void test_saveDeviceSlotRead_nullAsserts(void **state) {
savedevice_t device;
saveslot_t slot;
expect_assert_failure(saveDeviceSlotRead(NULL, &slot, 0));
expect_assert_failure(saveDeviceSlotRead(&device, NULL, 0));
}
static void test_saveDeviceSettingsWrite_nullAsserts(void **state) { static void test_saveDeviceSettingsWrite_nullAsserts(void **state) {
savedevice_t device; savedevice_t device;
savesettings_t settings; savesettings_t settings;
@@ -190,32 +170,6 @@ static void test_saveDeviceSettingsRead_nullAsserts(void **state) {
expect_assert_failure(saveDeviceSettingsRead(&device, NULL)); expect_assert_failure(saveDeviceSettingsRead(&device, NULL));
} }
static void test_saveDeviceSlot_dispatchesToPlatform(void **state) {
savedevice_t device;
errorret_t initRet = saveDeviceInit(&device);
assert_true(errorIsOk(initRet));
// The save directory must exist before a write can succeed - normally
// saveDeviceCheckAvailability does this via the platform's mkdirp.
saveDeviceCheckAvailability(&device, updateCallback, NULL);
saveDeviceUpdate(&device);
// saveSlotInit() only ever operates on SAVE.slot now, so that's the
// write source here.
saveSlotInit(&SAVE.slot);
SAVE.slot.cachedData.playerLevel = 21;
errorret_t writeRet = saveDeviceSlotWrite(&device, &SAVE.slot, 0);
assert_true(errorIsOk(writeRet));
// No pre-init needed - saveSlotReadJSON() (via saveDeviceSlotRead) fully
// resets/overwrites its destination struct regardless of prior content.
saveslot_t read;
errorret_t readRet = saveDeviceSlotRead(&device, &read, 0);
assert_true(errorIsOk(readRet));
assert_int_equal(read.cachedData.playerLevel, 21);
}
static void test_saveDeviceSettings_dispatchesToPlatform(void **state) { static void test_saveDeviceSettings_dispatchesToPlatform(void **state) {
savedevice_t device; savedevice_t device;
errorret_t initRet = saveDeviceInit(&device); errorret_t initRet = saveDeviceInit(&device);
@@ -303,14 +257,6 @@ int main(void) {
saveTestFixtureSetup, saveTestFixtureTeardown saveTestFixtureSetup, saveTestFixtureTeardown
), ),
cmocka_unit_test_setup_teardown(
test_saveDeviceSlotWrite_nullAsserts,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_saveDeviceSlotRead_nullAsserts,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown( cmocka_unit_test_setup_teardown(
test_saveDeviceSettingsWrite_nullAsserts, test_saveDeviceSettingsWrite_nullAsserts,
saveTestFixtureSetup, saveTestFixtureTeardown saveTestFixtureSetup, saveTestFixtureTeardown
@@ -319,10 +265,6 @@ int main(void) {
test_saveDeviceSettingsRead_nullAsserts, test_saveDeviceSettingsRead_nullAsserts,
saveTestFixtureSetup, saveTestFixtureTeardown saveTestFixtureSetup, saveTestFixtureTeardown
), ),
cmocka_unit_test_setup_teardown(
test_saveDeviceSlot_dispatchesToPlatform,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown( cmocka_unit_test_setup_teardown(
test_saveDeviceSettings_dispatchesToPlatform, test_saveDeviceSettings_dispatchesToPlatform,
saveTestFixtureSetup, saveTestFixtureTeardown saveTestFixtureSetup, saveTestFixtureTeardown
-149
View File
@@ -9,9 +9,7 @@
#include "savetestfixture.h" #include "savetestfixture.h"
#include "save/savedevice.h" #include "save/savedevice.h"
#include "save/savedevicelinux.h" #include "save/savedevicelinux.h"
#include "save/slot/saveslot.h"
#include "save/settings/savesettings.h" #include "save/settings/savesettings.h"
#include "save/save.h"
#include "util/memory.h" #include "util/memory.h"
#include "util/string.h" #include "util/string.h"
#include <stdio.h> #include <stdio.h>
@@ -86,27 +84,6 @@ static void test_getSettingsPath_homeUnset_errors(void **state) {
errorCatch(ret); errorCatch(ret);
} }
static void test_getSlotPath_success(void **state) {
char_t path[512];
errorret_t ret = saveDeviceLinuxGetSlotPath(path, sizeof(path), 2);
assert_true(errorIsOk(ret));
char_t expected[512];
stringFormat(
expected, sizeof(expected), "%s/.dusk/saves/slot2.json", getenv("HOME")
);
assert_true(stringEquals(path, expected));
}
static void test_getSlotPath_homeUnset_errors(void **state) {
unsetenv("HOME");
char_t path[512];
errorret_t ret = saveDeviceLinuxGetSlotPath(path, sizeof(path), 0);
assert_true(errorIsNotOk(ret));
errorCatch(ret);
}
// ============================================================ // ============================================================
// saveDeviceLinuxCheckAvailability // saveDeviceLinuxCheckAvailability
// ============================================================ // ============================================================
@@ -175,105 +152,6 @@ static void test_checkAvailability_mkdirpFails(void **state) {
assert_true(g_callbackFired); assert_true(g_callbackFired);
} }
// ============================================================
// saveDeviceLinuxSlotWrite / saveDeviceLinuxSlotRead
// ============================================================
static void test_slotWrite_nullAsserts(void **state) {
savedevice_t device;
// Content is never touched - both calls assert on a NULL argument
// before either device or slot would actually be read.
saveslot_t slot;
expect_assert_failure(saveDeviceLinuxSlotWrite(NULL, &slot, 0));
expect_assert_failure(saveDeviceLinuxSlotWrite(&device, NULL, 0));
}
static void test_slotRead_nullAsserts(void **state) {
savedevice_t device;
saveslot_t slot;
expect_assert_failure(saveDeviceLinuxSlotRead(NULL, &slot, 0));
expect_assert_failure(saveDeviceLinuxSlotRead(&device, NULL, 0));
}
static void test_slotWriteRead_roundTrip(void **state) {
savedevice_t device;
memoryZero(&device, sizeof(device));
// The save directory must exist before a write can succeed - normally
// saveDeviceLinuxCheckAvailability does this via mkdirp.
checkAvailabilityAndDrain(&device);
// saveSlotInit() only ever operates on SAVE.slot now, so that's the
// write source here.
saveSlotInit(&SAVE.slot);
stringCopy(
SAVE.slot.cachedData.name, "Hero", sizeof(SAVE.slot.cachedData.name)
);
SAVE.slot.cachedData.playerLevel = 9;
errorret_t writeRet = saveDeviceLinuxSlotWrite(&device, &SAVE.slot, 1);
assert_true(errorIsOk(writeRet));
// No pre-init needed - saveSlotReadJSON() (via saveDeviceLinuxSlotRead)
// fully resets/overwrites its destination struct regardless of prior
// content.
saveslot_t read;
errorret_t readRet = saveDeviceLinuxSlotRead(&device, &read, 1);
assert_true(errorIsOk(readRet));
assert_true(stringEquals(read.cachedData.name, "Hero"));
assert_int_equal(read.cachedData.playerLevel, 9);
}
static void test_slotRead_noFileYet_leavesSlotUntouched(void **state) {
savedevice_t device;
memoryZero(&device, sizeof(device));
// Deliberately not saveSlotInit()'d - a missing slot file means
// saveDeviceLinuxSlotRead never even calls saveSlotReadJSON, so slot
// must come in exactly as the caller left it and go out unchanged.
saveslot_t slot;
memoryZero(&slot, sizeof(slot));
stringCopy(slot.cachedData.name, "sentinel", sizeof(slot.cachedData.name));
slot.cachedData.playerLevel = 555;
errorret_t ret = saveDeviceLinuxSlotRead(&device, &slot, 2);
assert_true(errorIsOk(ret));
assert_true(stringEquals(slot.cachedData.name, "sentinel"));
assert_int_equal(slot.cachedData.playerLevel, 555);
}
static void test_slotRead_corruptFile_errors(void **state) {
savedevice_t device;
memoryZero(&device, sizeof(device));
// The directory must exist before we can drop a file into it - normally
// saveDeviceLinuxCheckAvailability does this via mkdirp.
savedevice_t tmpDevice;
memoryZero(&tmpDevice, sizeof(tmpDevice));
tmpDevice.state = SAVE_DEVICE_STATE_CHECKING_AVAILABILITY;
saveDeviceLinuxCheckAvailability(&tmpDevice);
assert_int_equal(tmpDevice.state, SAVE_DEVICE_STATE_AVAILABLE);
char_t slotPath[512];
errorret_t pathRet =
saveDeviceLinuxGetSlotPath(slotPath, sizeof(slotPath), 0);
assert_true(errorIsOk(pathRet));
FILE *file = fopen(slotPath, "w");
assert_non_null(file);
fprintf(file, "not valid json {{{");
fclose(file);
saveslot_t slot;
errorret_t ret = saveDeviceLinuxSlotRead(&device, &slot, 0);
assert_true(errorIsNotOk(ret));
errorCatch(ret);
}
// ============================================================ // ============================================================
// saveDeviceLinuxSettingsWrite / saveDeviceLinuxSettingsRead // saveDeviceLinuxSettingsWrite / saveDeviceLinuxSettingsRead
// ============================================================ // ============================================================
@@ -381,14 +259,6 @@ int main(void) {
test_getSettingsPath_homeUnset_errors, test_getSettingsPath_homeUnset_errors,
saveTestFixtureSetup, saveTestFixtureTeardown saveTestFixtureSetup, saveTestFixtureTeardown
), ),
cmocka_unit_test_setup_teardown(
test_getSlotPath_success, saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_getSlotPath_homeUnset_errors,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown( cmocka_unit_test_setup_teardown(
test_checkAvailability_wrongStateAsserts, test_checkAvailability_wrongStateAsserts,
saveTestFixtureSetup, saveTestFixtureTeardown saveTestFixtureSetup, saveTestFixtureTeardown
@@ -410,25 +280,6 @@ int main(void) {
saveTestFixtureSetup, saveTestFixtureTeardown saveTestFixtureSetup, saveTestFixtureTeardown
), ),
cmocka_unit_test_setup_teardown(
test_slotWrite_nullAsserts, saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_slotRead_nullAsserts, saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_slotWriteRead_roundTrip,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_slotRead_noFileYet_leavesSlotUntouched,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown(
test_slotRead_corruptFile_errors,
saveTestFixtureSetup, saveTestFixtureTeardown
),
cmocka_unit_test_setup_teardown( cmocka_unit_test_setup_teardown(
test_settingsWrite_nullAsserts, test_settingsWrite_nullAsserts,
saveTestFixtureSetup, saveTestFixtureTeardown saveTestFixtureSetup, saveTestFixtureTeardown