Restore ECS
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
include(dusktest)
|
||||
|
||||
# Tests
|
||||
dusktest(test_entitymanager.c)
|
||||
dusktest(test_entityposition.c)
|
||||
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* 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 "entity/entitymanager.h"
|
||||
#include "entity/component/display/entityposition.h"
|
||||
|
||||
static void test_entityManagerAddIsolatesEntities(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityid_t a = entityManagerAdd(&mgr);
|
||||
entityid_t b = entityManagerAdd(&mgr);
|
||||
|
||||
assert_true(a != ENTITY_ID_INVALID);
|
||||
assert_true(b != ENTITY_ID_INVALID);
|
||||
assert_true(a != b);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_entityManagerFullAsserts(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) {
|
||||
assert_true(entityManagerAdd(&mgr) != ENTITY_ID_INVALID);
|
||||
}
|
||||
|
||||
expect_assert_failure(entityManagerAdd(&mgr));
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_entityAddGetDisposeComponent(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityid_t entity = entityManagerAdd(&mgr);
|
||||
assert_int_equal(
|
||||
entityGetComponent(&mgr, entity, COMPONENT_TYPE_POSITION),
|
||||
COMPONENT_ID_INVALID
|
||||
);
|
||||
|
||||
componentid_t posComp = entityAddComponent(
|
||||
&mgr, entity, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
assert_true(posComp != COMPONENT_ID_INVALID);
|
||||
assert_int_equal(
|
||||
entityGetComponent(&mgr, entity, COMPONENT_TYPE_POSITION), posComp
|
||||
);
|
||||
|
||||
entityposition_t *pos = entityPositionGet(&mgr, entity, posComp);
|
||||
assert_non_null(pos);
|
||||
|
||||
entityDispose(&mgr, entity);
|
||||
assert_int_equal(
|
||||
entityGetComponent(&mgr, entity, COMPONENT_TYPE_POSITION),
|
||||
COMPONENT_ID_INVALID
|
||||
);
|
||||
assert_true((mgr.entities[entity].state & ENTITY_STATE_ACTIVE) == 0);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_entityAddDuplicateComponentAsserts(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityid_t entity = entityManagerAdd(&mgr);
|
||||
entityAddComponent(&mgr, entity, COMPONENT_TYPE_POSITION);
|
||||
expect_assert_failure(
|
||||
entityAddComponent(&mgr, entity, COMPONENT_TYPE_POSITION)
|
||||
);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_entityManagerAddIsolatesEntities),
|
||||
cmocka_unit_test(test_entityManagerFullAsserts),
|
||||
cmocka_unit_test(test_entityAddGetDisposeComponent),
|
||||
cmocka_unit_test(test_entityAddDuplicateComponentAsserts),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* 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 "entity/entitymanager.h"
|
||||
#include "entity/component/display/entityposition.h"
|
||||
|
||||
static void test_entityPositionLocalGetSet(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityid_t entity = entityManagerAdd(&mgr);
|
||||
componentid_t comp = entityAddComponent(
|
||||
&mgr, entity, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
|
||||
vec3 setPos = { 1.0f, 2.0f, 3.0f };
|
||||
entityPositionSetLocalPosition(&mgr, entity, comp, setPos);
|
||||
|
||||
vec3 getPos;
|
||||
entityPositionGetLocalPosition(&mgr, entity, comp, getPos);
|
||||
assert_float_equal(getPos[0], 1.0f, 0.0001f);
|
||||
assert_float_equal(getPos[1], 2.0f, 0.0001f);
|
||||
assert_float_equal(getPos[2], 3.0f, 0.0001f);
|
||||
|
||||
// World position of a parentless entity matches local position.
|
||||
vec3 worldPos;
|
||||
entityPositionGetWorldPosition(&mgr, entity, comp, worldPos);
|
||||
assert_float_equal(worldPos[0], 1.0f, 0.0001f);
|
||||
assert_float_equal(worldPos[1], 2.0f, 0.0001f);
|
||||
assert_float_equal(worldPos[2], 3.0f, 0.0001f);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_entityPositionParentChildWorldTransform(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityid_t parent = entityManagerAdd(&mgr);
|
||||
componentid_t parentComp = entityAddComponent(
|
||||
&mgr, parent, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
|
||||
entityid_t child = entityManagerAdd(&mgr);
|
||||
componentid_t childComp = entityAddComponent(
|
||||
&mgr, child, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
|
||||
vec3 parentPos = { 10.0f, 0.0f, 0.0f };
|
||||
entityPositionSetLocalPosition(&mgr, parent, parentComp, parentPos);
|
||||
|
||||
vec3 childLocalPos = { 1.0f, 0.0f, 0.0f };
|
||||
entityPositionSetLocalPosition(&mgr, child, childComp, childLocalPos);
|
||||
|
||||
entityPositionSetParent(&mgr, child, childComp, parent, parentComp);
|
||||
|
||||
vec3 childWorldPos;
|
||||
entityPositionGetWorldPosition(&mgr, child, childComp, childWorldPos);
|
||||
assert_float_equal(childWorldPos[0], 11.0f, 0.0001f);
|
||||
assert_float_equal(childWorldPos[1], 0.0f, 0.0001f);
|
||||
assert_float_equal(childWorldPos[2], 0.0f, 0.0001f);
|
||||
|
||||
// Moving the parent should lazily update the child's world position too.
|
||||
vec3 parentPosMoved = { 20.0f, 5.0f, 0.0f };
|
||||
entityPositionSetLocalPosition(&mgr, parent, parentComp, parentPosMoved);
|
||||
entityPositionGetWorldPosition(&mgr, child, childComp, childWorldPos);
|
||||
assert_float_equal(childWorldPos[0], 21.0f, 0.0001f);
|
||||
assert_float_equal(childWorldPos[1], 5.0f, 0.0001f);
|
||||
assert_float_equal(childWorldPos[2], 0.0f, 0.0001f);
|
||||
|
||||
// Detaching does not preserve world position -- the child's local
|
||||
// position (never touched since entityPositionSetLocalPosition above) is
|
||||
// now its world position too, since it no longer has a parent.
|
||||
entityPositionSetParent(
|
||||
&mgr, child, childComp, ENTITY_ID_INVALID, COMPONENT_ID_INVALID
|
||||
);
|
||||
vec3 parentPosMovedAgain = { 100.0f, 100.0f, 100.0f };
|
||||
entityPositionSetLocalPosition(
|
||||
&mgr, parent, parentComp, parentPosMovedAgain
|
||||
);
|
||||
entityPositionGetWorldPosition(&mgr, child, childComp, childWorldPos);
|
||||
assert_float_equal(childWorldPos[0], childLocalPos[0], 0.0001f);
|
||||
assert_float_equal(childWorldPos[1], childLocalPos[1], 0.0001f);
|
||||
assert_float_equal(childWorldPos[2], childLocalPos[2], 0.0001f);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_entityPositionDisposeDeep(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityid_t parent = entityManagerAdd(&mgr);
|
||||
componentid_t parentComp = entityAddComponent(
|
||||
&mgr, parent, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
|
||||
entityid_t child = entityManagerAdd(&mgr);
|
||||
componentid_t childComp = entityAddComponent(
|
||||
&mgr, child, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
|
||||
entityPositionSetParent(&mgr, child, childComp, parent, parentComp);
|
||||
|
||||
entityPositionDisposeDeep(&mgr, parent, parentComp);
|
||||
|
||||
assert_true((mgr.entities[parent].state & ENTITY_STATE_ACTIVE) == 0);
|
||||
assert_true((mgr.entities[child].state & ENTITY_STATE_ACTIVE) == 0);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_entityPositionLocalGetSet),
|
||||
cmocka_unit_test(test_entityPositionParentChildWorldTransform),
|
||||
cmocka_unit_test(test_entityPositionDisposeDeep),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user