8f8fa8f8d1
- Add Scene.set(module) so JerryScript can switch scenes via
require()'d {init, update, dispose} modules; wire it into
engineUpdate() and rework overworldscene.js/init.js to the new shape.
- Fix scriptManagerExecFile() never pushing its own file's directory
onto the require() dir stack, breaking relative require() calls from
a top-level entry script (e.g. init.js -> ./overworldscene.js).
- Cache sprite geometry across frames for uislider/uitab/uiframe
(dialogs/textbox/settings) and give uitextbox a per-page glyph cache
instead of one spriteBatchBuffer call per visible character. uiconsole
drops its alloc/free vertex buffer for a fixed-size one. uifps skips
rebuilding its label when the text hasn't changed.
- Add PSP_OPTIMIZATION_PLAN.md and a handful of UI/spritebatch unit
tests covering the new caching logic.
- Add Dolphin/PPSSPP emulator smoke-test scripts (+ Docker variants and
CI jobs) for GameCube/Wii/PSP.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
322 lines
11 KiB
C
322 lines
11 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.h"
|
|
#include "entity/component/display/entityposition.h"
|
|
#include "entity/component/physics/entityphysics.h"
|
|
#include "entity/component/display/entityrenderable.h"
|
|
#include "display/mesh/plane.h"
|
|
#include "display/mesh/capsule.h"
|
|
#include "script/scriptmanager.h"
|
|
#include "script/module/scene/modulescene.h"
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#ifndef DUSK_ASSETS_DIR
|
|
#error "DUSK_ASSETS_DIR must be defined"
|
|
#endif
|
|
|
|
static char_t *readFile(const char_t *relativePath) {
|
|
char_t path[512];
|
|
snprintf(path, sizeof(path), "%s/%s", DUSK_ASSETS_DIR, relativePath);
|
|
|
|
FILE *f = fopen(path, "rb");
|
|
assert_non_null(f);
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
long size = ftell(f);
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
char_t *buf = (char_t *)memoryAllocate((size_t)size + 1);
|
|
size_t read = fread(buf, 1, (size_t)size, f);
|
|
fclose(f);
|
|
buf[read] = '\0';
|
|
|
|
return buf;
|
|
}
|
|
|
|
// Runs the real, shipped overworldscene.js through Scene.set() the same
|
|
// way require()/init.js would -- not a copy embedded in this test, so
|
|
// this actually verifies what ships.
|
|
static errorret_t installOverworldScene(void) {
|
|
char_t *fileSrc = readFile("scripts/overworldscene.js");
|
|
|
|
const char_t *prefix = "var module = { exports: {} };\n(function(module){\n";
|
|
const char_t *suffix = "\n})(module);\nScene.set(module.exports);";
|
|
size_t wrappedLen = strlen(prefix) + strlen(fileSrc) + strlen(suffix);
|
|
char_t *wrapped = (char_t *)memoryAllocate(wrappedLen + 1);
|
|
snprintf(
|
|
wrapped, wrappedLen + 1, "%s%s%s", prefix, fileSrc, suffix
|
|
);
|
|
memoryFree(fileSrc);
|
|
|
|
errorret_t ret = scriptManagerExec(wrapped, NULL);
|
|
memoryFree(wrapped);
|
|
return ret;
|
|
}
|
|
|
|
static int overworld_setup(void **state) {
|
|
sceneInit();
|
|
|
|
errorret_t ret = scriptManagerInit();
|
|
if(errorIsNotOk(ret)) { errorCatch(ret); return -1; }
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int overworld_teardown(void **state) {
|
|
errorret_t ret = scriptManagerDispose();
|
|
if(errorIsNotOk(ret)) errorCatch(ret);
|
|
|
|
sceneDispose();
|
|
|
|
// JerryScript defers freeing native-wrapped handles (Entity/Scene/
|
|
// Position/Physics/Renderable instances) until GC/jerry_cleanup() runs,
|
|
// so the leak check can only be meaningful after scriptManagerDispose()
|
|
// has actually run above -- not inside the test body.
|
|
assert_int_equal(memoryGetAllocatedCount(), 0);
|
|
return 0;
|
|
}
|
|
|
|
static void test_overworldscene_builds_expected_entities(void **state) {
|
|
errorret_t ret = installOverworldScene();
|
|
assert_true(errorIsOk(ret));
|
|
|
|
sceneid_t sceneId = sceneGetActive();
|
|
assert_true(sceneId != SCENE_ID_INVALID);
|
|
entitymanager_t *mgr = sceneGetEntities(sceneId);
|
|
|
|
// Entity 0: camera. Position + Camera components, initial orbit
|
|
// position placed with angle=0 (Time.delta defaults to 0 with no
|
|
// timeInit()/timeUpdate() in this test), i.e. eye=(radius, height, 0).
|
|
entityid_t camEntity = 0;
|
|
componentid_t camPos = entityGetComponent(
|
|
mgr, camEntity, COMPONENT_TYPE_POSITION
|
|
);
|
|
assert_true(camPos != COMPONENT_ID_INVALID);
|
|
assert_true(
|
|
entityGetComponent(mgr, camEntity, COMPONENT_TYPE_CAMERA) !=
|
|
COMPONENT_ID_INVALID
|
|
);
|
|
|
|
vec3 camPosition;
|
|
entityPositionGetLocalPosition(mgr, camEntity, camPos, camPosition);
|
|
assert_float_equal(camPosition[0], 18.0f, 0.0001f);
|
|
assert_float_equal(camPosition[1], 10.0f, 0.0001f);
|
|
assert_float_equal(camPosition[2], 0.0f, 0.0001f);
|
|
|
|
// Entity 1: static ground plane.
|
|
entityid_t planeEntity = 1;
|
|
componentid_t planePos = entityGetComponent(
|
|
mgr, planeEntity, COMPONENT_TYPE_POSITION
|
|
);
|
|
assert_true(planePos != COMPONENT_ID_INVALID);
|
|
|
|
vec3 planePosition, planeScale;
|
|
entityPositionGetLocalPosition(mgr, planeEntity, planePos, planePosition);
|
|
entityPositionGetLocalScale(mgr, planeEntity, planePos, planeScale);
|
|
assert_float_equal(planePosition[0], -10.0f, 0.0001f);
|
|
assert_float_equal(planePosition[1], 0.0f, 0.0001f);
|
|
assert_float_equal(planePosition[2], -10.0f, 0.0001f);
|
|
assert_float_equal(planeScale[0], 20.0f, 0.0001f);
|
|
assert_float_equal(planeScale[1], 1.0f, 0.0001f);
|
|
assert_float_equal(planeScale[2], 20.0f, 0.0001f);
|
|
|
|
componentid_t planePhysics = entityGetComponent(
|
|
mgr, planeEntity, COMPONENT_TYPE_PHYSICS
|
|
);
|
|
assert_true(planePhysics != COMPONENT_ID_INVALID);
|
|
assert_int_equal(
|
|
entityPhysicsGetBodyType(mgr, planeEntity, planePhysics),
|
|
PHYSICS_BODY_STATIC
|
|
);
|
|
physicsshape_t planeShape = entityPhysicsGetShape(
|
|
mgr, planeEntity, planePhysics
|
|
);
|
|
assert_int_equal(planeShape.type, PHYSICS_SHAPE_PLANE);
|
|
assert_float_equal(planeShape.data.plane.normal[0], 0.0f, 0.0001f);
|
|
assert_float_equal(planeShape.data.plane.normal[1], 1.0f, 0.0001f);
|
|
assert_float_equal(planeShape.data.plane.normal[2], 0.0f, 0.0001f);
|
|
assert_float_equal(planeShape.data.plane.distance, 0.0f, 0.0001f);
|
|
|
|
componentid_t planeRenderable = entityGetComponent(
|
|
mgr, planeEntity, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
assert_true(planeRenderable != COMPONENT_ID_INVALID);
|
|
entityrenderable_t *planeR = componentGetData(
|
|
mgr, planeEntity, planeRenderable, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
assert_ptr_equal(planeR->data.material.meshes[0], &PLANE_MESH_SIMPLE);
|
|
assert_int_equal(planeR->data.material.material.unlit.color.r, 128);
|
|
assert_int_equal(planeR->data.material.material.unlit.color.g, 128);
|
|
assert_int_equal(planeR->data.material.material.unlit.color.b, 128);
|
|
assert_int_equal(planeR->data.material.material.unlit.color.a, 255);
|
|
|
|
// Entity 2: player. Dynamic capsule body (default body type), PLAYER
|
|
// component present.
|
|
entityid_t playerEntity = 2;
|
|
componentid_t playerPos = entityGetComponent(
|
|
mgr, playerEntity, COMPONENT_TYPE_POSITION
|
|
);
|
|
assert_true(playerPos != COMPONENT_ID_INVALID);
|
|
|
|
vec3 playerPosition;
|
|
entityPositionGetLocalPosition(mgr, playerEntity, playerPos, playerPosition);
|
|
assert_float_equal(playerPosition[0], 0.0f, 0.0001f);
|
|
assert_float_equal(playerPosition[1], 2.0f, 0.0001f);
|
|
assert_float_equal(playerPosition[2], 0.0f, 0.0001f);
|
|
|
|
componentid_t playerPhysics = entityGetComponent(
|
|
mgr, playerEntity, COMPONENT_TYPE_PHYSICS
|
|
);
|
|
assert_true(playerPhysics != COMPONENT_ID_INVALID);
|
|
assert_int_equal(
|
|
entityPhysicsGetBodyType(mgr, playerEntity, playerPhysics),
|
|
PHYSICS_BODY_DYNAMIC
|
|
);
|
|
physicsshape_t playerShape = entityPhysicsGetShape(
|
|
mgr, playerEntity, playerPhysics
|
|
);
|
|
assert_int_equal(playerShape.type, PHYSICS_SHAPE_CAPSULE);
|
|
assert_float_equal(playerShape.data.capsule.radius, 0.5f, 0.0001f);
|
|
assert_float_equal(playerShape.data.capsule.halfHeight, 0.5f, 0.0001f);
|
|
|
|
componentid_t playerRenderable = entityGetComponent(
|
|
mgr, playerEntity, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
assert_true(playerRenderable != COMPONENT_ID_INVALID);
|
|
entityrenderable_t *playerR = componentGetData(
|
|
mgr, playerEntity, playerRenderable, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
assert_ptr_equal(playerR->data.material.meshes[0], &CAPSULE_MESH_SIMPLE);
|
|
assert_int_equal(playerR->data.material.material.unlit.color.r, 0);
|
|
assert_int_equal(playerR->data.material.material.unlit.color.g, 0);
|
|
assert_int_equal(playerR->data.material.material.unlit.color.b, 255);
|
|
assert_int_equal(playerR->data.material.material.unlit.color.a, 255);
|
|
|
|
assert_true(
|
|
entityGetComponent(mgr, playerEntity, COMPONENT_TYPE_PLAYER) !=
|
|
COMPONENT_ID_INVALID
|
|
);
|
|
}
|
|
|
|
static void test_overworldscene_update_orbits_camera(void **state) {
|
|
errorret_t ret = installOverworldScene();
|
|
assert_true(errorIsOk(ret));
|
|
|
|
sceneid_t sceneId = sceneGetActive();
|
|
entitymanager_t *mgr = sceneGetEntities(sceneId);
|
|
entityid_t camEntity = 0;
|
|
componentid_t camPos = entityGetComponent(
|
|
mgr, camEntity, COMPONENT_TYPE_POSITION
|
|
);
|
|
|
|
// Drive one frame with a known delta and confirm the camera orbited by
|
|
// exactly angle = delta * speed (0.1 * 0.5 = 0.05 rad).
|
|
TIME.delta = 0.1f;
|
|
ret = moduleSceneUpdateCurrent();
|
|
assert_true(errorIsOk(ret));
|
|
|
|
vec3 camPosition;
|
|
entityPositionGetLocalPosition(mgr, camEntity, camPos, camPosition);
|
|
const float_t expectedAngle = 0.1f * 0.5f;
|
|
assert_float_equal(
|
|
camPosition[0], cosf(expectedAngle) * 18.0f, 0.0001f
|
|
);
|
|
assert_float_equal(camPosition[1], 10.0f, 0.0001f);
|
|
assert_float_equal(
|
|
camPosition[2], sinf(expectedAngle) * 18.0f, 0.0001f
|
|
);
|
|
|
|
TIME.delta = 0.0f;
|
|
}
|
|
|
|
static void test_scene_set_switches_and_disposes(void **state) {
|
|
errorret_t ret = installOverworldScene();
|
|
assert_true(errorIsOk(ret));
|
|
assert_true(sceneGetActive() != SCENE_ID_INVALID);
|
|
|
|
// Install a second, trivial scene module in place of the first. This
|
|
// must: call the first module's dispose(), destroy its scene, then
|
|
// create+activate a new one and call the new module's init().
|
|
ret = scriptManagerExec(
|
|
"var switchState = { updateCount: 0, disposed: false };\n"
|
|
"Scene.set({\n"
|
|
" init: function() { var e = new Entity(); e.add(POSITION); },\n"
|
|
" update: function() { switchState.updateCount++; },\n"
|
|
" dispose: function() { switchState.disposed = true; }\n"
|
|
"});",
|
|
NULL
|
|
);
|
|
assert_true(errorIsOk(ret));
|
|
|
|
sceneid_t secondSceneId = sceneGetActive();
|
|
assert_true(secondSceneId != SCENE_ID_INVALID);
|
|
|
|
entitymanager_t *mgr = sceneGetEntities(secondSceneId);
|
|
assert_true(
|
|
entityGetComponent(mgr, 0, COMPONENT_TYPE_POSITION) !=
|
|
COMPONENT_ID_INVALID
|
|
);
|
|
// The first module's plane entity (entity 1, POSITION+PHYSICS+
|
|
// RENDERABLE) must be gone -- proves the old scene was actually torn
|
|
// down, not just deactivated. (Scene IDs are a small reused pool --
|
|
// SCENE_COUNT_MAX slots -- so secondSceneId == firstSceneId here is
|
|
// expected, not a bug: sceneCreate() picks the first free slot, and
|
|
// destroying firstSceneId frees that exact slot right back up.)
|
|
assert_true(
|
|
entityGetComponent(mgr, 1, COMPONENT_TYPE_POSITION) ==
|
|
COMPONENT_ID_INVALID
|
|
);
|
|
|
|
ret = moduleSceneUpdateCurrent();
|
|
assert_true(errorIsOk(ret));
|
|
|
|
jerry_value_t result;
|
|
ret = scriptManagerExec("switchState.updateCount", &result);
|
|
assert_true(errorIsOk(ret));
|
|
assert_true(jerry_value_is_number(result));
|
|
assert_int_equal((int)jerry_value_as_number(result), 1);
|
|
jerry_value_free(result);
|
|
|
|
// Switching again must call the second module's dispose().
|
|
ret = scriptManagerExec(
|
|
"Scene.set({ init: function() {} });", NULL
|
|
);
|
|
assert_true(errorIsOk(ret));
|
|
|
|
ret = scriptManagerExec("switchState.disposed", &result);
|
|
assert_true(errorIsOk(ret));
|
|
assert_true(jerry_value_is_true(result));
|
|
jerry_value_free(result);
|
|
}
|
|
|
|
int main(void) {
|
|
assertInit();
|
|
const struct CMUnitTest tests[] = {
|
|
cmocka_unit_test_setup_teardown(
|
|
test_overworldscene_builds_expected_entities,
|
|
overworld_setup, overworld_teardown
|
|
),
|
|
cmocka_unit_test_setup_teardown(
|
|
test_overworldscene_update_orbits_camera,
|
|
overworld_setup, overworld_teardown
|
|
),
|
|
cmocka_unit_test_setup_teardown(
|
|
test_scene_set_switches_and_disposes,
|
|
overworld_setup, overworld_teardown
|
|
),
|
|
};
|
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
|
}
|