e61914bad4
Register Entity, the generic Component wrapper, and Scene as JerryScript classes (modulelist.c ties them together, plus their .d.ts stubs), so scenes/entities can be built from script going forward instead of the JSON scene format. With scripting now the intended authoring path, drop the JSON serialize/deserialize machinery entirely: componentdefinition_t's serialize/deserialize callbacks, every component's *Serialize/ *Deserialize function, entitySerialize/entityDeserialize, sceneSerialize/sceneDeserialize, and the "prefabs/<name>.json"/ "scenes/<name>.json" asset fallback in entityPrefabResolveAndApply/ scenePrefabResolveAndApply (C-coded prefabs only now). physicsshape.c and its test are removed outright since they only existed for this. game.c's test scene now comes from the existing overworldSceneCreate() C builder instead of loading the now-deleted assets/scenes/test.json. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
166 lines
4.3 KiB
C
166 lines
4.3 KiB
C
/**
|
|
* Copyright (c) 2026 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "dusktest.h"
|
|
#include "util/memory.h"
|
|
#include "time/time.h"
|
|
#include "scene/scene.h"
|
|
#include "entity/entitymanager.h"
|
|
#include "entity/component/display/entityposition.h"
|
|
|
|
static void countingUpdateCallback(
|
|
entitymanager_t *mgr,
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
void *user
|
|
) {
|
|
uint32_t *counter = (uint32_t *)user;
|
|
(*counter)++;
|
|
}
|
|
|
|
static void test_sceneCreateDestroy(void **state) {
|
|
sceneInit();
|
|
|
|
sceneid_t id = sceneCreate();
|
|
assert_true(id != SCENE_ID_INVALID);
|
|
assert_non_null(sceneGetEntities(id));
|
|
|
|
sceneDestroy(id);
|
|
assert_int_equal(sceneGetActive(), SCENE_ID_INVALID);
|
|
|
|
sceneDispose();
|
|
assert_int_equal(memoryGetAllocatedCount(), 0);
|
|
}
|
|
|
|
static void test_sceneEntitiesAreIsolatedPerScene(void **state) {
|
|
sceneInit();
|
|
|
|
sceneid_t sceneA = sceneCreate();
|
|
sceneid_t sceneB = sceneCreate();
|
|
assert_true(sceneA != sceneB);
|
|
|
|
entitymanager_t *entitiesA = sceneGetEntities(sceneA);
|
|
entitymanager_t *entitiesB = sceneGetEntities(sceneB);
|
|
assert_true(entitiesA != entitiesB);
|
|
|
|
// Fill scene A completely; scene B must be entirely unaffected.
|
|
for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) {
|
|
assert_true(entityManagerAdd(entitiesA) != ENTITY_ID_INVALID);
|
|
}
|
|
expect_assert_failure(entityManagerAdd(entitiesA));
|
|
|
|
entityid_t entityB = entityManagerAdd(entitiesB);
|
|
assert_true(entityB != ENTITY_ID_INVALID);
|
|
|
|
sceneDispose();
|
|
assert_int_equal(memoryGetAllocatedCount(), 0);
|
|
}
|
|
|
|
static void test_sceneFixedUpdateOnlyTicksActiveScene(void **state) {
|
|
sceneInit();
|
|
timeInit();
|
|
|
|
sceneid_t sceneA = sceneCreate();
|
|
sceneid_t sceneB = sceneCreate();
|
|
|
|
uint32_t counterA = 0;
|
|
uint32_t counterB = 0;
|
|
|
|
entitymanager_t *entitiesA = sceneGetEntities(sceneA);
|
|
entityid_t entityA = entityManagerAdd(entitiesA);
|
|
entityUpdateAdd(
|
|
entitiesA, entityA, countingUpdateCallback, COMPONENT_ID_INVALID,
|
|
&counterA
|
|
);
|
|
|
|
entitymanager_t *entitiesB = sceneGetEntities(sceneB);
|
|
entityid_t entityB = entityManagerAdd(entitiesB);
|
|
entityUpdateAdd(
|
|
entitiesB, entityB, countingUpdateCallback, COMPONENT_ID_INVALID,
|
|
&counterB
|
|
);
|
|
|
|
sceneSetActive(sceneA);
|
|
sceneFixedUpdate();
|
|
|
|
assert_int_equal(counterA, 1);
|
|
assert_int_equal(counterB, 0);
|
|
|
|
sceneSetActive(sceneB);
|
|
sceneFixedUpdate();
|
|
|
|
assert_int_equal(counterA, 1);
|
|
assert_int_equal(counterB, 1);
|
|
|
|
sceneDispose();
|
|
assert_int_equal(memoryGetAllocatedCount(), 0);
|
|
}
|
|
|
|
static void test_sceneUpdateOnlyRunsFixedUpdateOnNonDynamicFrames(void **state) {
|
|
sceneInit();
|
|
timeInit();
|
|
|
|
sceneid_t scene = sceneCreate();
|
|
sceneSetActive(scene);
|
|
|
|
uint32_t counter = 0;
|
|
entitymanager_t *entities = sceneGetEntities(scene);
|
|
entityid_t entity = entityManagerAdd(entities);
|
|
entityUpdateAdd(
|
|
entities, entity, countingUpdateCallback, COMPONENT_ID_INVALID, &counter
|
|
);
|
|
|
|
#if DUSK_TIME_DYNAMIC
|
|
// Still accumulating toward the next fixed timestep: sceneUpdate must
|
|
// not run sceneFixedUpdate yet.
|
|
TIME.dynamicUpdate = true;
|
|
sceneUpdate();
|
|
assert_int_equal(counter, 0);
|
|
|
|
// A fixed timestep boundary was just crossed: sceneUpdate must run
|
|
// sceneFixedUpdate exactly once.
|
|
TIME.dynamicUpdate = false;
|
|
sceneUpdate();
|
|
assert_int_equal(counter, 1);
|
|
#else
|
|
// Non-dynamic-time platforms: every frame is a fixed timestep.
|
|
sceneUpdate();
|
|
assert_int_equal(counter, 1);
|
|
sceneUpdate();
|
|
assert_int_equal(counter, 2);
|
|
#endif
|
|
|
|
sceneDispose();
|
|
assert_int_equal(memoryGetAllocatedCount(), 0);
|
|
}
|
|
|
|
static void test_sceneDestroyClearsActive(void **state) {
|
|
sceneInit();
|
|
|
|
sceneid_t id = sceneCreate();
|
|
sceneSetActive(id);
|
|
assert_int_equal(sceneGetActive(), id);
|
|
|
|
sceneDestroy(id);
|
|
assert_int_equal(sceneGetActive(), SCENE_ID_INVALID);
|
|
|
|
sceneDispose();
|
|
assert_int_equal(memoryGetAllocatedCount(), 0);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
const struct CMUnitTest tests[] = {
|
|
cmocka_unit_test(test_sceneCreateDestroy),
|
|
cmocka_unit_test(test_sceneEntitiesAreIsolatedPerScene),
|
|
cmocka_unit_test(test_sceneFixedUpdateOnlyTicksActiveScene),
|
|
cmocka_unit_test(test_sceneUpdateOnlyRunsFixedUpdateOnNonDynamicFrames),
|
|
cmocka_unit_test(test_sceneDestroyClearsActive),
|
|
};
|
|
|
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
|
}
|