Add cutsceneAppend/cutsceneAppendNext for mutating a running cutscene
cutsceneAppend adds items to the end of a cutscene_t's item array; cutsceneAppendNext inserts them at a given index instead (e.g. CUTSCENE_SYSTEM.currentItem + 1, to run immediately after the currently-executing item), shifting everything from that index onward to make room. Both are capacity-bounded against the caller- supplied itemsMax, matching cutsceneParseDoc's convention of never assuming ownership of the backing buffer's size. cutscene_t.items drops its const qualifier to allow this in-place mutation; cutsceneitem_t backing arrays in the test fixtures that get assigned into a mutable cutscene_t.items are no longer declared const either, since a const source would otherwise silently violate the field's new type. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
@@ -105,3 +105,41 @@ errorret_t cutsceneParseDoc(
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void cutsceneAppend(
|
||||
cutscene_t *scene,
|
||||
const size_t itemsMax,
|
||||
const cutsceneitem_t *items,
|
||||
const uint8_t count
|
||||
) {
|
||||
assertNotNull(scene, "Scene cannot be NULL");
|
||||
cutsceneAppendNext(scene, itemsMax, scene->itemCount, items, count);
|
||||
}
|
||||
|
||||
void cutsceneAppendNext(
|
||||
cutscene_t *scene,
|
||||
const size_t itemsMax,
|
||||
const uint8_t atIndex,
|
||||
const cutsceneitem_t *items,
|
||||
const uint8_t count
|
||||
) {
|
||||
assertNotNull(scene, "Scene cannot be NULL");
|
||||
assertNotNull(items, "Items cannot be NULL");
|
||||
assertTrue(count > 0, "Count cannot be 0");
|
||||
assertTrue(atIndex <= scene->itemCount, "atIndex out of bounds");
|
||||
assertTrue(
|
||||
(size_t)scene->itemCount + count <= itemsMax,
|
||||
"Cutscene item append exceeds itemsMax"
|
||||
);
|
||||
|
||||
const size_t tailCount = (size_t)scene->itemCount - atIndex;
|
||||
if(tailCount > 0) {
|
||||
memoryMove(
|
||||
&scene->items[atIndex + count],
|
||||
&scene->items[atIndex],
|
||||
tailCount * sizeof(cutsceneitem_t)
|
||||
);
|
||||
}
|
||||
memoryCopy(&scene->items[atIndex], items, count * sizeof(cutsceneitem_t));
|
||||
scene->itemCount += count;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#include "rpg/cutscene/cutscenepause.h"
|
||||
|
||||
typedef struct cutscene_s {
|
||||
const cutsceneitem_t *items;
|
||||
cutsceneitem_t *items;
|
||||
uint8_t itemCount;
|
||||
cutscenepause_t pause;
|
||||
} cutscene_t;
|
||||
@@ -66,3 +66,41 @@ errorret_t cutsceneParseDoc(
|
||||
cutsceneitem_t *items,
|
||||
size_t itemsMax
|
||||
);
|
||||
|
||||
/**
|
||||
* Appends items onto the end of scene->items, growing scene->itemCount.
|
||||
* scene->items must point at a buffer with capacity itemsMax - asserts if
|
||||
* appending would exceed it.
|
||||
*
|
||||
* @param scene Cutscene to append to.
|
||||
* @param itemsMax Capacity of scene->items.
|
||||
* @param items Items to append, copied in order.
|
||||
* @param count Number of entries in items.
|
||||
*/
|
||||
void cutsceneAppend(
|
||||
cutscene_t *scene,
|
||||
const size_t itemsMax,
|
||||
const cutsceneitem_t *items,
|
||||
const uint8_t count
|
||||
);
|
||||
|
||||
/**
|
||||
* Same as cutsceneAppend, but inserts items starting at atIndex instead of
|
||||
* at the end - e.g. pass CUTSCENE_SYSTEM.currentItem + 1 to make these
|
||||
* items run immediately after the currently-executing one. Every existing
|
||||
* item from atIndex onward shifts right by count to make room.
|
||||
*
|
||||
* @param scene Cutscene to insert into.
|
||||
* @param itemsMax Capacity of scene->items.
|
||||
* @param atIndex Index the first inserted item should end up at - must be
|
||||
* <= scene->itemCount.
|
||||
* @param items Items to insert, copied in order.
|
||||
* @param count Number of entries in items.
|
||||
*/
|
||||
void cutsceneAppendNext(
|
||||
cutscene_t *scene,
|
||||
const size_t itemsMax,
|
||||
const uint8_t atIndex,
|
||||
const cutsceneitem_t *items,
|
||||
const uint8_t count
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user