2 Commits
16 changed files with 195 additions and 527 deletions

No files matched your search

+1 -12
View File
@@ -27,17 +27,7 @@ target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
)
set(DUSK_BACKTRACE ON CACHE BOOL "Enable backtrace support for assert failures.")
# Records the file/line of every memoryAllocate() call into a fixed-size
# tracking table (removed again on memoryFree()) so a leak still present at
# engineDispose()'s memoryGetAllocatedCount() check can be traced back to
# where it was allocated. Linux only for now.
set(DUSK_TRACK_MEMORY OFF CACHE BOOL "Track memoryAllocate() call sites to help find memory leaks.")
if(DUSK_TRACK_MEMORY)
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
DUSK_TRACK_MEMORY
)
endif()
set(DUSK_TRACK_MEMORY ON CACHE BOOL "Track memoryAllocate() call sites to help find memory leaks.")
# Define platform-specific macros.
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
@@ -56,5 +46,4 @@ target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
DUSK_TIME_DYNAMIC
DUSK_NETWORK_IPV6
DUSK_THREAD_PTHREAD
DUSK_TRACK_MEMORY
)
+6
View File
@@ -50,6 +50,12 @@ if(DUSK_BACKTRACE)
)
endif()
if(DUSK_TRACK_MEMORY)
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
DUSK_TRACK_MEMORY
)
endif()
# Includes
target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
@@ -76,20 +76,13 @@ size_t assetCutsceneComputePoolSize(yyjson_val *itemsArr) {
return size;
}
void assetCutsceneFreeParsed(assetcutsceneoutput_t *out) {
if(out->items != NULL) {
memoryFree(out->items);
out->items = NULL;
}
if(out->pool != NULL) {
memoryFree(out->pool);
out->pool = NULL;
}
if(out->doc != NULL) {
yyjson_doc_free(out->doc);
out->doc = NULL;
}
}
// Raw file-read scratch space - fixed-size (ASSET_CUTSCENE_FILE_SIZE_MAX is
// already a hard cap below) rather than malloc'd per load. Safe to share:
// ASSET only ever runs one async load at a time on its single load thread
// (see assetUpdateAsync), and yyjson_read copies everything it needs out
// of this buffer into the doc before this function returns, so it's free
// to be overwritten by the next load.
static uint8_t CUTSCENE_FILE_BUFFER[ASSET_CUTSCENE_FILE_SIZE_MAX];
errorret_t assetCutsceneLoaderAsync(assetloading_t *loading) {
assertNotNull(loading, "Loading cannot be NULL");
@@ -113,9 +106,10 @@ errorret_t assetCutsceneLoaderAsync(assetloading_t *loading) {
}
size_t fileSize = (size_t)file->size;
uint8_t *buffer = memoryAllocate(fileSize);
assetLoaderErrorChain(loading, assetFileOpen(file));
assetLoaderErrorChain(loading, assetFileRead(file, buffer, fileSize));
assetLoaderErrorChain(
loading, assetFileRead(file, CUTSCENE_FILE_BUFFER, fileSize)
);
assertTrue(
file->lastRead == file->size,
"Failed to read entire cutscene file."
@@ -127,48 +121,55 @@ errorret_t assetCutsceneLoaderAsync(assetloading_t *loading) {
// type's load callback just parses fields - see cutsceneCutsceneLoad's
// doc comment for how CUTSCENE items avoid needing one), so it happens
// right here instead of bouncing to the sync phase.
assetcutsceneoutput_t *out = &loading->entry->data.cutscene;
yyjson_doc *doc = yyjson_read(
(char *)buffer, fileSize,
(char *)CUTSCENE_FILE_BUFFER, fileSize,
YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS
);
memoryFree(buffer);
if(doc == NULL) {
assetLoaderErrorThrow(loading, "Failed to parse cutscene JSON");
}
out->doc = doc;
yyjson_val *root = yyjson_doc_get_root(doc);
yyjson_val *itemsArr = yyjson_obj_get(root, "items");
if(!yyjson_is_arr(itemsArr)) {
assetCutsceneFreeParsed(out);
yyjson_doc_free(doc);
assetLoaderErrorThrow(loading, "Cutscene JSON is missing an 'items' array");
}
size_t itemCount = yyjson_arr_size(itemsArr);
if(itemCount > UINT8_MAX) {
assetCutsceneFreeParsed(out);
assetLoaderErrorThrow(loading, "Cutscene has more than 255 items");
if(itemCount > CUTSCENE_LOADED_ITEMS_MAX) {
yyjson_doc_free(doc);
assetLoaderErrorThrow(
loading, "Cutscene has more than CUTSCENE_LOADED_ITEMS_MAX items"
);
}
cutscenepause_t pause = CUTSCENE_PAUSE_DEFAULT;
yyjson_val *pauseArr = yyjson_obj_get(root, "pause");
if(yyjson_is_arr(pauseArr)) {
if(!cutsceneJsonParsePauseFlags(pauseArr, &pause)) {
assetCutsceneFreeParsed(out);
yyjson_doc_free(doc);
assetLoaderErrorThrow(
loading, "Cutscene JSON has an invalid 'pause' flag"
);
}
}
out->items = memoryAllocate(itemCount * sizeof(cutsceneitem_t));
memoryZero(out->items, itemCount * sizeof(cutsceneitem_t));
size_t poolSize = assetCutsceneComputePoolSize(itemsArr);
out->pool = poolSize > 0 ? memoryAllocate(poolSize) : NULL;
if(poolSize > CUTSCENE_LOADED_POOL_SIZE_MAX) {
yyjson_doc_free(doc);
assetLoaderErrorThrow(
loading, "Cutscene needs more pool space than CUTSCENE_LOADED_POOL_SIZE_MAX"
);
}
// Everything structural has validated by this point, so it's safe to
// start overwriting the shared CUTSCENE_LOADED_ITEMS/POOL buffers - a
// per-item parse failure past this point still leaves them partially
// overwritten, but that's fine: any load failure here already sends the
// game into the fatal-error overlay (see cutsceneSystemLoad/
// cutsceneCutsceneResolve), so nothing keeps running against stale data.
memoryZero(CUTSCENE_LOADED_ITEMS, itemCount * sizeof(cutsceneitem_t));
size_t poolOffset = 0;
yyjson_arr_iter itemIter = yyjson_arr_iter_with(itemsArr);
@@ -176,19 +177,24 @@ errorret_t assetCutsceneLoaderAsync(assetloading_t *loading) {
uint8_t index = 0;
while((itemVal = yyjson_arr_iter_next(&itemIter)) != NULL) {
errorret_t itemResult = assetCutsceneParseItem(
loading, itemVal, &out->items[index], out->pool, &poolOffset, index
loading, itemVal, &CUTSCENE_LOADED_ITEMS[index],
CUTSCENE_LOADED_POOL, &poolOffset, index
);
if(errorIsNotOk(itemResult)) {
assetCutsceneFreeParsed(out);
yyjson_doc_free(doc);
errorChain(itemResult);
}
index++;
}
out->cutscene.items = out->items;
out->cutscene.itemCount = (uint8_t)itemCount;
out->cutscene.pause = pause;
out->cutscene.dataSize = 0;
// Takes ownership of doc - marker/cutscene-reference name fields point
// directly into it (see cutsceneCutsceneRefLoad/cutsceneMarkerLoad), so
// it must outlive this load, replacing whatever the previously loaded
// cutscene left behind.
cutsceneLoadedSetDoc(doc);
CUTSCENE_LOADED_SCENE.itemCount = (uint8_t)itemCount;
CUTSCENE_LOADED_SCENE.pause = pause;
CUTSCENE_LOADED_SCENE.dataSize = 0;
loading->loading.cutscene.state = ASSET_CUTSCENE_LOADING_STATE_PARSE;
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
@@ -224,7 +230,10 @@ errorret_t assetCutsceneDispose(assetentry_t *entry) {
assertTrue(entry->type == ASSET_LOADER_TYPE_CUTSCENE, "Invalid type.");
assertIsMainThread("Must be called from the main thread.");
assetCutsceneFreeParsed(&entry->data.cutscene);
// Nothing to free here - a loaded cutscene's data lives in the fixed,
// shared CUTSCENE_LOADED_SCENE/ITEMS/POOL globals (see cutscenesystem.h),
// not per-entry storage; this entry only ever drove the read+parse
// pipeline into them.
errorOk();
}
@@ -8,6 +8,7 @@
#pragma once
#include "asset/assetfile.h"
#include "rpg/cutscene/cutscene.h"
#include "rpg/cutscene/cutscenesystem.h"
#include "rpg/cutscene/item/cutsceneitem.h"
#include "rpg/cutscene/item/json/cutscenejsonpool.h"
#include "yyjson.h"
@@ -33,10 +34,13 @@ typedef struct {
} assetcutsceneloaderloading_t;
typedef struct {
cutscene_t cutscene;
cutsceneitem_t *items;
uint8_t *pool;
yyjson_doc *doc;
// Nothing - a loaded cutscene's data lives entirely in the fixed,
// shared CUTSCENE_LOADED_SCENE/ITEMS/POOL globals (see cutscenesystem.h)
// that assetCutsceneLoaderAsync parses directly into, not per-entry
// storage. This entry only ever exists to drive the read+parse
// pipeline, and is unlocked (see cutsceneSystemLoad/
// cutsceneCutsceneResolve) the instant that finishes.
uint8_t unused;
} assetcutsceneoutput_t;
/**
@@ -67,22 +71,15 @@ errorret_t assetCutsceneParseItem(
const uint8_t index
);
/**
* Frees whatever of out->items/out->pool/out->doc is currently non-NULL
* and clears them - shared by both the normal disposer and every parse
* failure path's cleanup.
*
* @param out The output to free.
*/
void assetCutsceneFreeParsed(assetcutsceneoutput_t *out);
/**
* Asynchronous loader for cutscene assets. Reads the raw JSONC file bytes
* and, in the same call, parses them into a heap-allocated cutsceneitem_t
* array + pool - none of that work touches the main thread (every item
* type's load callback is pure parsing now; CUTSCENE items defer their
* nested asset resolution to cutsceneCutsceneStart), so doing it all here
* keeps a large cutscene from ever blocking a frame.
* and, in the same call, parses them directly into the fixed, shared
* CUTSCENE_LOADED_ITEMS/CUTSCENE_LOADED_POOL buffers (see cutscenesystem.h)
* - no heap allocation at all, since only one cutscene is ever resident.
* None of that work touches the main thread (every item type's load
* callback is pure parsing now; CUTSCENE items defer their nested asset
* resolution to cutsceneCutsceneStart), so doing it all here keeps a large
* cutscene from ever blocking a frame.
*
* @param loading Loading information for the asset being loaded.
* @return Error code indicating success or failure of the load operation.
-17
View File
@@ -62,20 +62,3 @@ typedef struct cutscene_s {
.type = CUTSCENE_ITEM_TYPE_CUTSCENE, \
.cutsceneRef = { .cutscene = CUTSCENE_REFERENCE(CUTSCENE), .name = NULL } \
}
/**
* Splices another CUTSCENE(NAME, ...)-declared cutscene's items into this
* one in place, resuming right after this item once the referenced
* cutscene's items run out - unlike CUTSCENE_CUTSCENE (a one-way jump that
* never returns). Has no dedicated item header of its own, same rationale
* as CUTSCENE_CUTSCENE - cutsceneInsertStart/Update/Load are defined
* inline in cutsceneitem.h/.c, tightly bound to the asset/loader system.
*
* @param CUTSCENE The referenced cutscene's NAME, as passed to
* CUTSCENE(...).
*/
#define CUTSCENE_INSERT(CUTSCENE) \
{ \
.type = CUTSCENE_ITEM_TYPE_INSERT, \
.insert = { .cutscene = CUTSCENE_REFERENCE(CUTSCENE), .name = NULL } \
}
+23 -40
View File
@@ -15,6 +15,21 @@
cutscenesystem_t CUTSCENE_SYSTEM;
cutsceneitem_t CUTSCENE_LOADED_ITEMS[CUTSCENE_LOADED_ITEMS_MAX];
uint8_t CUTSCENE_LOADED_POOL[CUTSCENE_LOADED_POOL_SIZE_MAX];
cutscene_t CUTSCENE_LOADED_SCENE = {
.items = CUTSCENE_LOADED_ITEMS,
.itemCount = 0,
.pause = CUTSCENE_PAUSE_NONE,
.dataSize = 0
};
static yyjson_doc *CUTSCENE_LOADED_DOC = NULL;
void cutsceneLoadedSetDoc(yyjson_doc *doc) {
if(CUTSCENE_LOADED_DOC != NULL) yyjson_doc_free(CUTSCENE_LOADED_DOC);
CUTSCENE_LOADED_DOC = doc;
}
void cutsceneSystemInit() {
memoryZero(&CUTSCENE_SYSTEM, sizeof(cutscenesystem_t));
}
@@ -40,7 +55,6 @@ void cutsceneSystemPrepare(
CUTSCENE_SYSTEM.textCache[0] = '\0';
CUTSCENE_SYSTEM.currentItem = 0xFF;// Set to 0xFF so Next wraps to 0.
CUTSCENE_SYSTEM.onComplete = NULL;
CUTSCENE_SYSTEM.insertStackSize = 0;
}
void cutsceneSystemStartCutscene(const cutscene_t *cutscene) {
@@ -70,13 +84,18 @@ void cutsceneSystemLoad(const char_t *file) {
assetentry_t *entry = assetLock(path, ASSET_LOADER_TYPE_CUTSCENE, NULL);
errorret_t result = assetRequireLoaded(entry);
// Released the instant loading finishes - the loader already parsed
// everything we need into CUTSCENE_LOADED_SCENE/ITEMS/POOL, so nothing
// further depends on this entry (see cutscenesystem.h's doc comment on
// CUTSCENE_LOADED_ITEMS_MAX for why only one cutscene stays resident).
assetUnlockEntry(entry);
if(errorIsNotOk(result)) {
errorCatch(errorPrint(result));
uiFatalErrorOpen(NULL);
return;
}
cutsceneSystemStartCutscene(&entry->data.cutscene.cutscene);
cutsceneSystemStartCutscene(&CUTSCENE_LOADED_SCENE);
}
void cutsceneRestart(void) {
@@ -108,21 +127,6 @@ void cutsceneSystemNext() {
CUTSCENE_SYSTEM.currentItem++;
// Run off the end of the current scene? If it was spliced in via
// CUTSCENE_ITEM_TYPE_INSERT, pop back to whatever it was inserted into
// and resume right after the INSERT item - possibly chaining through
// several pops if that lands on the end of its own parent in turn (an
// insert as the very last item, or a nested insert-of-an-insert).
while(
CUTSCENE_SYSTEM.currentItem >= CUTSCENE_SYSTEM.scene->itemCount &&
CUTSCENE_SYSTEM.insertStackSize > 0
) {
cutsceneinsertframe_t frame =
CUTSCENE_SYSTEM.insertStack[--CUTSCENE_SYSTEM.insertStackSize];
CUTSCENE_SYSTEM.scene = frame.scene;
CUTSCENE_SYSTEM.currentItem = frame.currentItem + 1;
}
// End of the cutscene?
if(
CUTSCENE_SYSTEM.currentItem >= CUTSCENE_SYSTEM.scene->itemCount
@@ -145,7 +149,6 @@ void cutsceneSystemNext() {
CUTSCENE_SYSTEM.textMiniLastCreated = CUTSCENE_TEXT_MINI_LAST_CREATED;
CUTSCENE_SYSTEM.textCache[0] = '\0';
CUTSCENE_SYSTEM.onComplete = NULL;
CUTSCENE_SYSTEM.insertStackSize = 0;
if(onComplete != NULL) onComplete(userData);
return;
@@ -157,27 +160,6 @@ void cutsceneSystemNext() {
cutsceneItemStart(item, &CUTSCENE_SYSTEM.data);
}
void cutsceneSystemInsertCutscene(const cutscene_t *cutscene) {
assertNotNull(
CUTSCENE_SYSTEM.scene,
"cutsceneSystemInsertCutscene called with no cutscene running"
);
assertTrue(
CUTSCENE_SYSTEM.insertStackSize < CUTSCENE_INSERT_STACK_MAX,
"Cutscene INSERT nesting exceeds CUTSCENE_INSERT_STACK_MAX"
);
CUTSCENE_SYSTEM.insertStack[CUTSCENE_SYSTEM.insertStackSize++] =
(cutsceneinsertframe_t){
.scene = CUTSCENE_SYSTEM.scene,
.currentItem = CUTSCENE_SYSTEM.currentItem
};
CUTSCENE_SYSTEM.scene = cutscene;
CUTSCENE_SYSTEM.currentItem = 0xFF;// Set to 0xFF so Next wraps to 0.
cutsceneSystemNext();
}
void cutsceneSystemSetOnComplete(cutscenecallback_t onComplete) {
assertNotNull(
CUTSCENE_SYSTEM.scene,
@@ -297,5 +279,6 @@ void cutsceneSystemDispose() {
CUTSCENE_SYSTEM.textMiniLastCreated = CUTSCENE_TEXT_MINI_LAST_CREATED;
CUTSCENE_SYSTEM.textCache[0] = '\0';
CUTSCENE_SYSTEM.onComplete = NULL;
CUTSCENE_SYSTEM.insertStackSize = 0;
cutsceneLoadedSetDoc(NULL);
}
+31 -40
View File
@@ -26,17 +26,38 @@ typedef struct entity_s entity_t;
// CUTSCENE_ITEM_TYPE_KEYBOARD item is never truncated caching it here.
#define CUTSCENE_TEXT_CACHE_MAX 64
// Maximum nesting depth of CUTSCENE_ITEM_TYPE_INSERT items - how many
// "return to the item after this one" frames cutsceneSystemInsertCutscene
// may have pushed at once (see CUTSCENE_SYSTEM.insertStack).
#define CUTSCENE_INSERT_STACK_MAX 4
// Only one JSON-authored cutscene is ever loaded/playing at a time - a
// CUTSCENE item hands control off completely (see cutsceneCutsceneStart)
// and cutsceneSystemLoad/cutsceneCutsceneResolve release their asset lock
// the instant loading finishes - so there's no benefit to malloc'ing a
// fresh items/pool buffer per load. Instead the asset loader
// (assetCutsceneLoaderAsync) parses directly into these fixed, static
// buffers. Bump these if a real cutscene ever needs more; C-authored
// cutscenes (the CUTSCENE(...) macro) don't use these at all - they're
// compiled directly into their own static const arrays.
#define CUTSCENE_LOADED_ITEMS_MAX 128
#define CUTSCENE_LOADED_POOL_SIZE_MAX 4096
// One "return address" pushed by cutsceneSystemInsertCutscene - the scene
// and item index to resume at once the inserted cutscene's items run out.
typedef struct {
const cutscene_t *scene;
uint8_t currentItem;
} cutsceneinsertframe_t;
extern cutsceneitem_t CUTSCENE_LOADED_ITEMS[CUTSCENE_LOADED_ITEMS_MAX];
extern uint8_t CUTSCENE_LOADED_POOL[CUTSCENE_LOADED_POOL_SIZE_MAX];
// The cutscene_t for whatever was last parsed into CUTSCENE_LOADED_ITEMS/
// CUTSCENE_LOADED_POOL - .items always points at CUTSCENE_LOADED_ITEMS;
// itemCount/pause/dataSize are overwritten by the loader on each load.
extern cutscene_t CUTSCENE_LOADED_SCENE;
/**
* Frees whatever yyjson_doc currently backs CUTSCENE_LOADED_ITEMS' string
* fields (marker/cutscene-reference names point directly into it - see
* cutsceneCutsceneRefLoad/cutsceneMarkerLoad) and takes ownership of doc
* instead. Called by the cutscene asset loader once a fresh parse into
* CUTSCENE_LOADED_ITEMS/CUTSCENE_LOADED_POOL completes successfully, and
* from cutsceneSystemDispose to release the last one at shutdown.
*
* @param doc The new doc to take ownership of (NULL just frees/clears the
* current one).
*/
void cutsceneLoadedSetDoc(yyjson_doc *doc);
typedef struct {
const cutscene_t *scene;
@@ -64,12 +85,6 @@ typedef struct {
// See cutsceneSystemSetOnComplete.
cutscenecallback_t onComplete;
// Return stack pushed by CUTSCENE_ITEM_TYPE_INSERT items - see
// cutsceneSystemInsertCutscene. Reset (insertStackSize = 0) whenever a
// cutscene freshly starts or the system is disposed.
cutsceneinsertframe_t insertStack[CUTSCENE_INSERT_STACK_MAX];
uint8_t insertStackSize;
} cutscenesystem_t;
extern cutscenesystem_t CUTSCENE_SYSTEM;
@@ -114,30 +129,6 @@ void cutsceneSystemStartCutsceneAndGoToMarker(
const char_t *marker
);
/**
* Splices cutscene's items into the running cutscene in place, resuming
* the current cutscene right after the CUTSCENE_ITEM_TYPE_INSERT item that
* called this once cutscene's items run out - unlike
* cutsceneSystemStartCutscene (a one-way jump that replaces the running
* cutscene outright). Pushes a return frame onto
* CUTSCENE_SYSTEM.insertStack; cutsceneSystemNext pops it once cutscene
* ends. Deliberately does not touch pause flags, interact entities,
* "last created" state, the text cache or userData - those all keep
* whatever the outer cutscene set, since this is meant to feel like
* pasting cutscene's items in place rather than starting an independent
* cutscene. Because of this, cutscene's own .pause/.dataSize are ignored.
* Asserts if no cutscene is currently running, or if inserts are already
* nested CUTSCENE_INSERT_STACK_MAX deep.
*
* Note: cutsceneRestart() resets the insert stack (via
* cutsceneSystemPrepare), so a RESTART item used from inside an inserted
* cutscene restarts just that inserted cutscene and does not return to
* whatever it was inserted into.
*
* @param cutscene The cutscene whose items to splice in.
*/
void cutsceneSystemInsertCutscene(const cutscene_t *cutscene);
/**
* Locks, loads and immediately starts a cutscene asset by file name,
* e.g. cutsceneSystemLoad("main_menu.jsonc") loads and starts
+14 -53
View File
@@ -333,13 +333,6 @@ cutsceneitemcallbacks_t CUTSCENE_ITEM_CALLBACKS[CUTSCENE_ITEM_TYPE_COUNT] = {
.update = cutsceneRegularBattleUpdate,
.jsonName = "REGULAR_BATTLE",
.load = cutsceneItemLoadNoop
},
[CUTSCENE_ITEM_TYPE_INSERT] = {
.init = cutsceneInsertStart,
.update = cutsceneInsertUpdate,
.jsonName = "INSERT",
.load = cutsceneInsertLoad
}
};
@@ -380,10 +373,13 @@ void cutsceneCutsceneStart(
) {
const cutscene_t *cutscene = item->cutsceneRef.cutscene;
if(cutscene == NULL && item->cutsceneRef.name != NULL) {
// Not cached back into item - with only one shared, fixed load buffer
// (see cutscenesystem.h's CUTSCENE_LOADED_ITEMS_MAX doc comment),
// every JSON-authored reference resolves to the same fixed address
// every time anyway, and item itself lives inside that very buffer -
// caching a pointer into it here would go stale (or, worse, get
// clobbered mid-resolve) the moment a later load overwrites it.
cutscene = cutsceneCutsceneResolve(item->cutsceneRef.name);
// Cache the resolved pointer back so replaying this same item (e.g.
// a looping intro) never re-resolves/re-locks the asset again.
((cutsceneitem_t *)item)->cutsceneRef.cutscene = cutscene;
}
if(cutscene != NULL) cutsceneSystemStartCutscene(cutscene);
}
@@ -401,13 +397,16 @@ const cutscene_t * cutsceneCutsceneResolve(const char_t *name) {
assetentry_t *entry = assetLock(path, ASSET_LOADER_TYPE_CUTSCENE, NULL);
errorret_t result = assetRequireLoaded(entry);
// Released the instant loading finishes - see cutsceneSystemLoad's doc
// comment for why nothing further depends on this entry once loaded.
assetUnlockEntry(entry);
if(errorIsNotOk(result)) {
errorCatch(errorPrint(result));
uiFatalErrorOpen(NULL);
return NULL;
}
return &entry->data.cutscene.cutscene;
return &CUTSCENE_LOADED_SCENE;
}
errorret_t cutsceneCutsceneLoad(
@@ -415,18 +414,10 @@ errorret_t cutsceneCutsceneLoad(
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
) {
return cutsceneCutsceneRefLoad(itemObj, &item->cutsceneRef, "CUTSCENE");
}
errorret_t cutsceneCutsceneRefLoad(
yyjson_val *itemObj,
cutscenecutsceneref_t *outRef,
const char_t *itemTypeLabel
) {
const char_t *name = yyjson_get_str(yyjson_obj_get(itemObj, "name"));
if(name == NULL) {
errorThrow("Cutscene item (%s): missing 'name'", itemTypeLabel);
errorThrow("Cutscene item (CUTSCENE): missing 'name'");
}
// Only validate that the eventual "cutscenes/<name>.jsonc" path would
@@ -434,41 +425,11 @@ errorret_t cutsceneCutsceneRefLoad(
// actually runs (see cutsceneCutsceneStart/cutsceneCutsceneResolve).
int32_t pathLen = stringFormat(NULL, 0, "cutscenes/%s.jsonc", name);
if(pathLen < 0 || (size_t)pathLen >= ASSET_FILE_NAME_MAX) {
errorThrow("Cutscene item (%s): 'name' too long", itemTypeLabel);
errorThrow("Cutscene item (CUTSCENE): 'name' too long");
}
outRef->cutscene = NULL;
outRef->name = name;
item->cutsceneRef.cutscene = NULL;
item->cutsceneRef.name = name;
errorOk();
}
void cutsceneInsertStart(
const cutsceneitem_t *item,
cutsceneitemdata_t *data
) {
const cutscene_t *cutscene = item->insert.cutscene;
if(cutscene == NULL && item->insert.name != NULL) {
cutscene = cutsceneCutsceneResolve(item->insert.name);
// Cache the resolved pointer back so replaying this same item never
// re-resolves/re-locks the asset again, same as cutsceneCutsceneStart.
((cutsceneitem_t *)item)->insert.cutscene = cutscene;
}
if(cutscene != NULL) cutsceneSystemInsertCutscene(cutscene);
}
bool_t cutsceneInsertUpdate(
const cutsceneitem_t *item,
cutsceneitemdata_t *data
) {
return false;
}
errorret_t cutsceneInsertLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
) {
return cutsceneCutsceneRefLoad(itemObj, &item->insert, "INSERT");
}
+9 -77
View File
@@ -57,18 +57,15 @@
typedef struct cutscene_s cutscene_t;
// A CUTSCENE (or INSERT) item's target. C-authored cutscenes (the
// CUTSCENE_CUTSCENE/CUTSCENE_INSERT macros in cutscene.h) resolve cutscene
// at compile time and leave name NULL. JSON-authored cutscenes
// (cutsceneCutsceneRefLoad) do the opposite - only name is set at parse
// time, since resolving/locking the referenced asset there would eagerly
// load every cutscene a cutscene merely references, even ones never
// played. cutsceneCutsceneStart/cutsceneInsertStart resolve name to
// cutscene the first time this item actually runs (via
// cutsceneCutsceneResolve) and cache the result back into this field so
// replaying the same item never re-resolves it. Shared by both CUTSCENE
// (item->cutsceneRef) and INSERT (item->insert), since both need exactly
// this "resolved pointer, or a name to lazily resolve" shape.
// A CUTSCENE item's target. C-authored cutscenes (the CUTSCENE_CUTSCENE
// macro in cutscene.h) resolve cutscene at compile time and leave name
// NULL. JSON-authored cutscenes (cutsceneCutsceneLoad) do the opposite -
// only name is set at parse time, since resolving/locking the referenced
// asset there would eagerly load every cutscene a cutscene merely
// references, even ones never played. cutsceneCutsceneStart resolves
// name to cutscene the first time this item actually runs (via
// cutsceneCutsceneResolve) and caches the result back into this field so
// replaying the same item never re-resolves it.
typedef struct {
const cutscene_t *cutscene;
const char_t *name;
@@ -84,7 +81,6 @@ struct cutsceneitem_s {
cutscenecallback_t callback;
cutscenewait_t wait;
cutscenecutsceneref_t cutsceneRef;
cutscenecutsceneref_t insert;
cutsceneentityteleport_t entityTeleport;
cutsceneentitywalkto_t entityWalkTo;
cutscenefade_t fade;
@@ -266,67 +262,3 @@ errorret_t cutsceneCutsceneLoad(
uint8_t *pool,
size_t *poolOffset
);
/**
* Shared JSON parsing body for both CUTSCENE and INSERT items - both need
* exactly a 'name' string field, validated to fit the eventual
* "cutscenes/<name>.jsonc" asset path. Only stores the name (see
* cutsceneCutsceneLoad for why resolving is deferred).
*
* @param itemObj The item's JSON object.
* @param outRef Destination ref, already zeroed by the caller.
* @param itemTypeLabel Item type name used only in the error message on
* failure (e.g. "CUTSCENE" or "INSERT").
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneCutsceneRefLoad(
yyjson_val *itemObj,
cutscenecutsceneref_t *outRef,
const char_t *itemTypeLabel
);
/**
* Starts an INSERT item, splicing the referenced cutscene's items into the
* running cutscene in place - unlike CUTSCENE (a one-way jump that replaces
* the running cutscene outright), control returns to the item right after
* this one once the inserted items run out. If item->insert.cutscene hasn't
* been resolved yet (a JSON-authored reference, only known by name so far),
* resolves and caches it first via cutsceneCutsceneResolve, same as
* cutsceneCutsceneStart.
*
* @param item The cutscene item.
* @param data Runtime data storage.
*/
void cutsceneInsertStart(
const cutsceneitem_t *item,
cutsceneitemdata_t *data
);
/**
* Updates an INSERT item. By the time this would run, control has already
* moved on to the inserted items, so this always reports incomplete.
*
* @param item The cutscene item.
* @param data Runtime data storage.
* @returns false always.
*/
bool_t cutsceneInsertUpdate(
const cutsceneitem_t *item,
cutsceneitemdata_t *data
);
/**
* Parses an INSERT cutscene item's JSON fields into item.
*
* @param itemObj The item's JSON object.
* @param item Destination item, already zeroed by the caller.
* @param pool Unused (INSERT needs no pool storage).
* @param poolOffset Unused (INSERT needs no pool storage).
* @return Error code indicating success or failure of the parse.
*/
errorret_t cutsceneInsertLoad(
yyjson_val *itemObj,
cutsceneitem_t *item,
uint8_t *pool,
size_t *poolOffset
);
@@ -63,7 +63,6 @@ typedef enum {
CUTSCENE_ITEM_TYPE_IDLE,
CUTSCENE_ITEM_TYPE_UI_SHOW,
CUTSCENE_ITEM_TYPE_REGULAR_BATTLE,
CUTSCENE_ITEM_TYPE_INSERT,
CUTSCENE_ITEM_TYPE_COUNT
} cutsceneitemtype_t;
@@ -22,8 +22,8 @@ ENTITY_GLOBAL_CALLBACK(3) {
}
ENTITY_GLOBAL_CALLBACK(4) {
create->entity->interact.type = ENTITY_INTERACT_CUTSCENE;
create->entity->interact.data.cutscene = cutsceneCutsceneResolve("battletest");
create->entity->interact.type = ENTITY_INTERACT_CUTSCENE_NAME;
create->entity->interact.data.cutsceneName = "battletest";
}
static const entityglobaldef_t ENTITY_GLOBAL_LIST[] = {
@@ -27,6 +27,15 @@ void entityInteractWith(entity_t *player, entity_t *target) {
);
break;
case ENTITY_INTERACT_CUTSCENE_NAME: {
const cutscene_t *cutscene =
cutsceneCutsceneResolve(target->interact.data.cutsceneName);
if(cutscene != NULL) {
cutsceneSystemStartCutsceneWith(cutscene, player, target);
}
break;
}
case ENTITY_INTERACT_PRINT:
uiTextboxMainSetText(target->interact.data.message);
@@ -18,6 +18,7 @@ typedef enum {
ENTITY_INTERACT_NULL = 0,
ENTITY_INTERACT_CUTSCENE,
ENTITY_INTERACT_CUTSCENE_NAME,
ENTITY_INTERACT_PRINT,
ENTITY_INTERACT_CALLBACK,
@@ -28,7 +29,18 @@ typedef enum {
* Per-type data for an entity's interact component.
*/
typedef union {
// A compile-time cutscene_t* (e.g. CUTSCENE_REFERENCE(...)) - safe to
// hold onto indefinitely, since C-authored cutscenes are compiled
// directly into their own static const arrays, never the shared
// CUTSCENE_LOADED_ITEMS buffer (see cutscenesystem.h).
const cutscene_t *cutscene;
// A bare cutscene name (as passed to cutsceneCutsceneResolve), resolved
// lazily at interact time rather than once up front - only one
// JSON-authored cutscene is ever resident at a time (see
// CUTSCENE_LOADED_ITEMS_MAX's doc comment), so resolving early and
// holding onto the result here would go stale the moment any other
// cutscene loads in the meantime.
const char_t *cutsceneName;
const char_t *message;
void (*callback)(entity_t *player, entity_t *target);
} entityinteractdata_t;
+17 -21
View File
@@ -26,21 +26,15 @@
#define UI_MAIN_MENU_INDEX_QUIT 2
uimainmenu_t UI_MAIN_MENU;
assetentry_t *MAIN_MENU_CUTSCENE_ENTRY = NULL;
cutscene_t *uiMainMenuLoadCutscene(void) {
if(MAIN_MENU_CUTSCENE_ENTRY == NULL) {
MAIN_MENU_CUTSCENE_ENTRY = assetLock(
"cutscenes/main_menu.jsonc", ASSET_LOADER_TYPE_CUTSCENE, NULL
);
}
errorret_t result = assetRequireLoaded(MAIN_MENU_CUTSCENE_ENTRY);
if(errorIsNotOk(result)) {
errorCatch(errorPrint(result));
assertTrue(false, "Failed to load main menu cutscene asset");
}
return &MAIN_MENU_CUTSCENE_ENTRY->data.cutscene.cutscene;
const cutscene_t *uiMainMenuLoadCutscene(void) {
// Delegates entirely to cutsceneCutsceneResolve rather than keeping our
// own locked-forever entry, since only one JSON-authored cutscene is
// ever resident at a time (see CUTSCENE_LOADED_ITEMS_MAX's doc comment
// in cutscenesystem.h) - caching an entry pointer here across calls
// would go stale the moment any other cutscene loads in the meantime.
// May return NULL on failure (already reported via uiFatalErrorOpen).
return cutsceneCutsceneResolve("main_menu");
}
void uiMainMenuSelectSaveResult(const uint8_t slotIndex, void *user) {
@@ -79,9 +73,9 @@ void uiMainMenuOpenSelectSave(void *userData) {
void uiMainMenuStartGame(void) {
if(CUTSCENE_SYSTEM.scene == NULL) {
cutsceneSystemStartCutsceneAndGoToMarker(
uiMainMenuLoadCutscene(), "NEW_GAME"
);
const cutscene_t *cutscene = uiMainMenuLoadCutscene();
if(cutscene == NULL) return;
cutsceneSystemStartCutsceneAndGoToMarker(cutscene, "NEW_GAME");
} else {
cutsceneGoTo("NEW_GAME");
}
@@ -91,9 +85,9 @@ void uiMainMenuStartGame(void) {
void uiMainMenuOptions(void) {
if(CUTSCENE_SYSTEM.scene == NULL) {
cutsceneSystemStartCutsceneAndGoToMarker(
uiMainMenuLoadCutscene(), "OPTIONS"
);
const cutscene_t *cutscene = uiMainMenuLoadCutscene();
if(cutscene == NULL) return;
cutsceneSystemStartCutsceneAndGoToMarker(cutscene, "OPTIONS");
} else {
cutsceneGoTo("OPTIONS");
}
@@ -101,7 +95,9 @@ void uiMainMenuOptions(void) {
void uiMainMenuQuit(void) {
if(CUTSCENE_SYSTEM.scene == NULL) {
cutsceneSystemStartCutsceneAndGoToMarker(uiMainMenuLoadCutscene(), "QUIT");
const cutscene_t *cutscene = uiMainMenuLoadCutscene();
if(cutscene == NULL) return;
cutsceneSystemStartCutsceneAndGoToMarker(cutscene, "QUIT");
} else {
cutsceneGoTo("QUIT");
}
+9 -8
View File
@@ -39,17 +39,18 @@ errorret_t uiMainMenuInit(void);
void uiMainMenuOpen(void);
/**
* Lazily locks/loads the main menu cutscene asset (assets/cutscenes/
* Loads (or re-loads) the main menu cutscene asset (assets/cutscenes/
* main_menu.jsonc, parsed directly at load time rather than compiled in)
* and returns it, asserting on failure. Kept resident for the rest of the
* process once loaded - the persistent cutscene is what actually drives
* this whole screen (opening this panel is its first item's job, see
* UI_SHOW/rpg/cutscene/item/ui/cutsceneuishow.h), this is just the shared
* accessor for the "Start Game" flow below to jump/restart it.
* and returns it - the shared accessor for the "Start Game"/Options/Quit
* flows below to jump/restart it. Only one JSON-authored cutscene is ever
* resident at a time (see CUTSCENE_LOADED_ITEMS_MAX's doc comment in
* cutscenesystem.h), so this resolves fresh on every call rather than
* caching anything.
*
* @return The main menu cutscene.
* @return The main menu cutscene, or NULL on failure (already reported via
* uiFatalErrorOpen).
*/
cutscene_t *uiMainMenuLoadCutscene(void);
const cutscene_t *uiMainMenuLoadCutscene(void);
/**
* Starts the "Start Game" flow: jumps the already-running main menu
-200
View File
@@ -177,201 +177,6 @@ static void test_cutsceneSystemDisposeResetsState(void **state) {
assert_null(CUTSCENE_SYSTEM.entityInteracted);
}
// --- CUTSCENE_INSERT ---------------------------------------------------
static uint8_t insertOrderLog[8];
static uint8_t insertOrderLogCount;
static void recordOrder0(void *userData) { insertOrderLog[insertOrderLogCount++] = 0; }
static void recordOrder1(void *userData) { insertOrderLog[insertOrderLogCount++] = 1; }
static void recordOrder2(void *userData) { insertOrderLog[insertOrderLogCount++] = 2; }
static void recordOrderA(void *userData) { insertOrderLog[insertOrderLogCount++] = 3; }
static void recordOrderB(void *userData) { insertOrderLog[insertOrderLogCount++] = 4; }
CUTSCENE(TEST_INSERT_SNIPPET, 0, NONE,
CUTSCENE_CALLBACK(recordOrderA),
CUTSCENE_WAIT(2.0f),
CUTSCENE_CALLBACK(recordOrderB)
);
CUTSCENE(TEST_INSERT_OUTER, 0, NONE,
CUTSCENE_CALLBACK(recordOrder0),
CUTSCENE_WAIT(1.0f),
CUTSCENE_CALLBACK(recordOrder1),
CUTSCENE_INSERT(TEST_INSERT_SNIPPET),
CUTSCENE_CALLBACK(recordOrder2)
);
static void test_cutsceneInsertSplicesItemsInPlaceThenResumes(void **state) {
insertOrderLogCount = 0;
cutsceneSystemInit();
cutsceneSystemStartCutscene(&CUTSCENE_TEST_INSERT_OUTER);
TIME.delta = 1.5f;// elapses the outer WAIT(1.0)
cutsceneSystemUpdate();// order0 done -> starts the outer WAIT
cutsceneSystemUpdate();// the WAIT elapses -> starts order1
cutsceneSystemUpdate();// order1 done -> starts the INSERT, cascading into orderA
assert_ptr_equal(CUTSCENE_SYSTEM.scene, &CUTSCENE_TEST_INSERT_SNIPPET);
TIME.delta = 2.5f;// elapses the snippet's WAIT(2.0)
cutsceneSystemUpdate();// orderA done -> starts snippet's WAIT
cutsceneSystemUpdate();// snippet's WAIT elapses -> starts orderB
cutsceneSystemUpdate();// orderB done -> pops back, starts order2
assert_ptr_equal(CUTSCENE_SYSTEM.scene, &CUTSCENE_TEST_INSERT_OUTER);
cutsceneSystemUpdate();// order2 done -> cutscene ends naturally
assert_null(CUTSCENE_SYSTEM.scene);
const uint8_t expected[] = { 0, 1, 3, 4, 2 };
assert_int_equal(insertOrderLogCount, sizeof(expected));
assert_memory_equal(insertOrderLog, expected, sizeof(expected));
}
CUTSCENE(TEST_INSERT_AS_LAST_ITEM_SNIPPET, 0, NONE,
CUTSCENE_CALLBACK(recordOrderA)
);
CUTSCENE(TEST_INSERT_AS_LAST_ITEM_OUTER, 0, NONE,
CUTSCENE_CALLBACK(recordOrder0),
CUTSCENE_INSERT(TEST_INSERT_AS_LAST_ITEM_SNIPPET)
);
static void test_cutsceneInsertAsLastItemEndsCutsceneNaturally(
void **state
) {
insertOrderLogCount = 0;
cutsceneSystemInit();
cutsceneSystemStartCutscene(&CUTSCENE_TEST_INSERT_AS_LAST_ITEM_OUTER);
cutsceneSystemUpdate();// order0 done -> starts INSERT, cascades to orderA
assert_ptr_equal(
CUTSCENE_SYSTEM.scene, &CUTSCENE_TEST_INSERT_AS_LAST_ITEM_SNIPPET
);
// orderA is the snippet's only item, and the snippet is the outer's last
// item -- finishing it should pop straight through to the natural end
// of the whole cutscene, not get stuck.
cutsceneSystemUpdate();
assert_null(CUTSCENE_SYSTEM.scene);
const uint8_t expected[] = { 0, 3 };
assert_int_equal(insertOrderLogCount, sizeof(expected));
assert_memory_equal(insertOrderLog, expected, sizeof(expected));
}
CUTSCENE(TEST_INSERT_NESTED_INNER, 0, NONE,
CUTSCENE_CALLBACK(recordOrderA)
);
// INSERT is MID's first item, so entering MID (from OUTER) cascades
// straight through into INNER within the same Start chain, one level
// deeper than a single insert.
CUTSCENE(TEST_INSERT_NESTED_MID, 0, NONE,
CUTSCENE_INSERT(TEST_INSERT_NESTED_INNER),
CUTSCENE_CALLBACK(recordOrderB)
);
CUTSCENE(TEST_INSERT_NESTED_OUTER, 0, NONE,
CUTSCENE_CALLBACK(recordOrder0),
CUTSCENE_INSERT(TEST_INSERT_NESTED_MID),
CUTSCENE_CALLBACK(recordOrder2)
);
static void test_cutsceneInsertNestsThroughMultipleLevels(void **state) {
insertOrderLogCount = 0;
cutsceneSystemInit();
cutsceneSystemStartCutscene(&CUTSCENE_TEST_INSERT_NESTED_OUTER);
// order0 done -> starts INSERT(MID), cascading through INSERT(INNER) ->
// orderA, all within this one Update call's Start chain.
cutsceneSystemUpdate();
assert_ptr_equal(CUTSCENE_SYSTEM.scene, &CUTSCENE_TEST_INSERT_NESTED_INNER);
cutsceneSystemUpdate();// orderA done -> pops to MID, starts orderB
assert_ptr_equal(CUTSCENE_SYSTEM.scene, &CUTSCENE_TEST_INSERT_NESTED_MID);
cutsceneSystemUpdate();// orderB done -> pops to OUTER, starts order2
assert_ptr_equal(CUTSCENE_SYSTEM.scene, &CUTSCENE_TEST_INSERT_NESTED_OUTER);
cutsceneSystemUpdate();// order2 done -> ends naturally
assert_null(CUTSCENE_SYSTEM.scene);
const uint8_t expected[] = { 0, 3, 4, 2 };
assert_int_equal(insertOrderLogCount, sizeof(expected));
assert_memory_equal(insertOrderLog, expected, sizeof(expected));
}
CUTSCENE(TEST_INSERT_OVERFLOW_LEVEL5, 0, NONE,
CUTSCENE_CALLBACK(recordOrderA)
);
CUTSCENE(TEST_INSERT_OVERFLOW_LEVEL4, 0, NONE,
CUTSCENE_INSERT(TEST_INSERT_OVERFLOW_LEVEL5)
);
CUTSCENE(TEST_INSERT_OVERFLOW_LEVEL3, 0, NONE,
CUTSCENE_INSERT(TEST_INSERT_OVERFLOW_LEVEL4)
);
CUTSCENE(TEST_INSERT_OVERFLOW_LEVEL2, 0, NONE,
CUTSCENE_INSERT(TEST_INSERT_OVERFLOW_LEVEL3)
);
CUTSCENE(TEST_INSERT_OVERFLOW_LEVEL1, 0, NONE,
CUTSCENE_INSERT(TEST_INSERT_OVERFLOW_LEVEL2)
);
CUTSCENE(TEST_INSERT_OVERFLOW_LEVEL0, 0, NONE,
CUTSCENE_INSERT(TEST_INSERT_OVERFLOW_LEVEL1)
);
static void test_cutsceneInsertAssertsWhenNestingExceedsStackMax(
void **state
) {
cutsceneSystemInit();
// 5 levels of INSERT deep, one past CUTSCENE_INSERT_STACK_MAX (4) --
// all cascade synchronously within this one call, since each level's
// only item is the INSERT starting immediately.
expect_assert_failure(
cutsceneSystemStartCutscene(&CUTSCENE_TEST_INSERT_OVERFLOW_LEVEL0)
);
}
static void noopInsertCallback(void *userData) {}
CUTSCENE(TEST_INSERT_PERSIST_SNIPPET, 0, ALL,
CUTSCENE_CALLBACK(noopInsertCallback)
);
CUTSCENE(TEST_INSERT_PERSIST_OUTER, 0, NPC,
CUTSCENE_INSERT(TEST_INSERT_PERSIST_SNIPPET)
);
static void test_cutsceneInsertDoesNotResetPauseOrInteractEntities(
void **state
) {
cutsceneSystemInit();
entityInit(&ENTITIES[3], ENTITY_TYPE_PLAYER);
entityInit(&ENTITIES[4], ENTITY_TYPE_NPC);
cutsceneSystemStartCutsceneWith(
&CUTSCENE_TEST_INSERT_PERSIST_OUTER, &ENTITIES[3], &ENTITIES[4]
);
// The INSERT item's Start already cascaded into the snippet by now.
assert_ptr_equal(
CUTSCENE_SYSTEM.scene, &CUTSCENE_TEST_INSERT_PERSIST_SNIPPET
);
assert_ptr_equal(CUTSCENE_SYSTEM.entityInteract, &ENTITIES[3]);
assert_ptr_equal(CUTSCENE_SYSTEM.entityInteracted, &ENTITIES[4]);
// The snippet declares ALL, but INSERT doesn't apply it -- the outer
// cutscene's NPC pause, already in effect, stands.
assert_int_equal(CUTSCENE_SYSTEM.pause, CUTSCENE_PAUSE_NPC);
}
int main(int argc, char** argv) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_cutsceneSystemStartSetsUpInitialItem),
@@ -383,11 +188,6 @@ int main(int argc, char** argv) {
cmocka_unit_test(test_cutsceneSystemGetAreaId),
cmocka_unit_test(test_cutsceneSystemGetTextMiniId),
cmocka_unit_test(test_cutsceneSystemDisposeResetsState),
cmocka_unit_test(test_cutsceneInsertSplicesItemsInPlaceThenResumes),
cmocka_unit_test(test_cutsceneInsertAsLastItemEndsCutsceneNaturally),
cmocka_unit_test(test_cutsceneInsertNestsThroughMultipleLevels),
cmocka_unit_test(test_cutsceneInsertAssertsWhenNestingExceedsStackMax),
cmocka_unit_test(test_cutsceneInsertDoesNotResetPauseOrInteractEntities),
};
return cmocka_run_group_tests(tests, NULL, NULL);