diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-05-09 20:40:56 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-05-09 20:40:56 -0400 |
| commit | ecf91229fb5c9150f2d60d97652bf0024a5c3435 (patch) | |
| tree | d8fcffa8d436222c4304d9f4d538165ddd734055 /src/serialization-component.cpp | |
| parent | 9693ff2d4be85d356e07e3192baaa2262a7140ff (diff) | |
pke-test-serialization: add more complex tests
Diffstat (limited to 'src/serialization-component.cpp')
| -rw-r--r-- | src/serialization-component.cpp | 147 |
1 files changed, 82 insertions, 65 deletions
diff --git a/src/serialization-component.cpp b/src/serialization-component.cpp index 7514a14..afa3b89 100644 --- a/src/serialization-component.cpp +++ b/src/serialization-component.cpp @@ -50,6 +50,12 @@ pk_handle pke_serialize_inst_pos(srlztn_serialize_helper *h, const glm::vec3 pos pk_arr_append_t(&kvec.arr, kve); } pk_arr_append_t(&h->kvp_containers, kvec); + // 2025-05-08 JCB + // this is dead code, but it's here as a reminder to not call pk_arr_reset + // so that the underlying data is not freed. + kvec.child_handles.data = nullptr; + kvec.children.data = nullptr; + kvec.arr.data = nullptr; return kvec.srlztn_handle; } @@ -58,10 +64,12 @@ void pke_deserialize_inst_pos(srlztn_deserialize_helper *h, pke_kve_container *k char *pEnd = nullptr; uint32_t i, index; const char *starting_char; + pke_kve *kve; PK_STN_RES stn_res; for (i = 0; i < kvec->arr.next; ++i) { - if (strstr(kvec->arr[i].key, SRLZTN_POSROT_POS)) { - starting_char = kvec->arr[i].val; + kve = &kvec->arr[i]; + if (strstr(kve->key, SRLZTN_POSROT_POS)) { + starting_char = kve->val; index = 0; do { assert(index < 3); @@ -70,9 +78,10 @@ void pke_deserialize_inst_pos(srlztn_deserialize_helper *h, pke_kve_container *k starting_char = pEnd + 1; ++index; } while (*pEnd != '\0'); + continue; } - if (strstr(kvec->arr[i].key, SRLZTN_POSROT_ROT)) { - starting_char = kvec->arr[i].val; + if (strstr(kve->key, SRLZTN_POSROT_ROT)) { + starting_char = kve->val; index = 0; do { assert(index < 4); @@ -81,9 +90,10 @@ void pke_deserialize_inst_pos(srlztn_deserialize_helper *h, pke_kve_container *k starting_char = pEnd + 1; ++index; } while (*pEnd != '\0'); + continue; } - if (strstr(kvec->arr[i].key, SRLZTN_POSROT_SCALE)) { - starting_char = kvec->arr[i].val; + if (strstr(kve->key, SRLZTN_POSROT_SCALE)) { + starting_char = kve->val; index = 0; do { assert(index < 3); @@ -92,6 +102,7 @@ void pke_deserialize_inst_pos(srlztn_deserialize_helper *h, pke_kve_container *k starting_char = pEnd + 1; ++index; } while (*pEnd != '\0'); + continue; } } } @@ -103,10 +114,9 @@ pk_handle pke_serialize_instance(srlztn_serialize_helper *h, const CompInstance pk_handle inst_pos_handle; pke_kve kve{}; pke_kve_container kvec{}; - CompInstance c{}; - glm::vec3 pos; - glm::quat quat_rot; - glm::vec3 scale; + glm::vec3 pos = glm::vec3(0,0,0); + glm::quat quat_rot = glm::quat(1,0,0,0); + glm::vec3 scale = glm::vec3(1,1,1); float mass; PhysicsCollision collisionLayer; PhysicsCollision collisionMask; @@ -120,7 +130,12 @@ pk_handle pke_serialize_instance(srlztn_serialize_helper *h, const CompInstance comp->bt.motionState->getWorldTransform(trans); BulletToGlm(trans.getOrigin(), pos); BulletToGlm(trans.getRotation(), quat_rot); - BulletToGlm(comp->bt.rigidBody->getCollisionShape()->getLocalScaling(), scale); + if (comp->bt.rigidBody != nullptr) { + auto a1 = comp->bt.rigidBody; + auto a2 = a1 != nullptr ? a1->getCollisionShape() : nullptr; + auto a3 = a2 != nullptr ? a2->getLocalScaling() : btVector3(1,1,1); + BulletToGlm(a3, scale); + } } inst_pos_handle = pke_serialize_inst_pos(h, pos, quat_rot, scale); @@ -154,7 +169,7 @@ pk_handle pke_serialize_instance(srlztn_serialize_helper *h, const CompInstance kve.end = SRLZTN_KVE_END; pk_arr_append_t(&kvec.arr, kve); } - if (mass != 1) { + { kve.key = SRLZTN_INSTANCE_COMPONENT_MASS; len = snprintf(NULL, 0, "%f", mass); s = pk_new<char>(len+1, h->bkt); @@ -163,20 +178,20 @@ pk_handle pke_serialize_instance(srlztn_serialize_helper *h, const CompInstance kve.end = SRLZTN_KVE_END; pk_arr_append_t(&kvec.arr, kve); } - if (collisionLayer != c.physicsLayer) { + { kve.key = SRLZTN_INSTANCE_COMPONENT_COLLISION_LAYER; - len = snprintf(NULL, 0, "%lu", static_cast<PhysicsCollision_T>(collisionLayer)); + len = snprintf(NULL, 0, "0x%.016lX", static_cast<PhysicsCollision_T>(collisionLayer)); s = pk_new<char>(len+1, h->bkt); - sprintf(s, "%lu", static_cast<PhysicsCollision_T>(collisionLayer)); + sprintf(s, "0x%.016lX", static_cast<PhysicsCollision_T>(collisionLayer)); kve.val = s; kve.end = SRLZTN_KVE_END; pk_arr_append_t(&kvec.arr, kve); } - if (collisionMask != c.physicsMask) { + { kve.key = SRLZTN_INSTANCE_COMPONENT_COLLISION_MASK; - len = snprintf(NULL, 0, "%lu", static_cast<PhysicsCollision_T>(collisionMask)); + len = snprintf(NULL, 0, "0x%.016lX", static_cast<PhysicsCollision_T>(collisionMask)); s = pk_new<char>(len+1, h->bkt); - sprintf(s, "%lu", static_cast<PhysicsCollision_T>(collisionMask)); + sprintf(s, "0x%.016lX", static_cast<PhysicsCollision_T>(collisionMask)); kve.val = s; kve.end = SRLZTN_KVE_END; pk_arr_append_t(&kvec.arr, kve); @@ -214,53 +229,6 @@ void pke_deserialize_instance(srlztn_deserialize_helper *h, pke_kve_container *k } for (i = 0; i < kvec->arr.next; ++i) { - if (strstr(SRLZTN_OBJ_END, kvec->arr[i].key)) { - if (et_ptr == nullptr) { - fprintf(stdout, "[Game::DeserializeInstance] Unknown EntityTypeCode, skipping instance.\n"); - break; - } - btVector3 bt_pos; - btQuaternion bt_quat; - GlmToBullet(pos, bt_pos); - GlmToBullet(quat_rot, bt_quat); - GlmToBullet(scale, inst_pos.scale); - inst_pos.mass = mass; - inst_pos.posRot.setIdentity(); - inst_pos.posRot.setOrigin(bt_pos); - inst_pos.posRot.setRotation(bt_quat); - if (et_ptr->createInstanceCallback.func != nullptr) { - /* TODO 2025-03-27 JCB - * We have not yet defined what the appropriate callback signature - * for creating an entity instance is. - * What should be passed as arguments? What would need to be passed - * that couldn't be accessed globally? - * Consider changing this callback to trigger after creating a - * generic instance, rather than *creating* it. - * Also consider just requiring a generic instance for any given - * EntityType. - */ - // typedef Entity_Base *CreateInst(); - // entity = reinterpret_cast<CreateInst*>(et_ptr->createInstanceCallback.func)(); - fprintf(stderr, "[%s] Attempted to call EntityType::createInstanceCallback and we have not yet defined a valid function signature", __FILE__); - } else { - srlztn_instance_mapping map{}; - map.serialized_uuid = comp.uuid; - map.created_entity = EntityType_CreateGenericInstance(et_ptr, h->scene, &comp, &inst_pos); - // TODO gross - pk_arr_t<CompInstance *> instances; - ECS_GetInstances(map.created_entity, instances); - for (uint32_t i = 0; i < instances.next; ++i) { - if (comp.uuid == instances[i]->uuid) { - map.created_instance = instances[i]; - } - } - if (map.created_instance == nullptr) { - fprintf(stderr, "[pke_deserialize_instance] Failed to find created instance for creating mapping"); - } else { - pk_arr_append(&h->mapping, &map); - } - } - } if (strstr(kvec->arr[i].key, SRLZTN_INSTANCE_COMPONENT_UUID)) { kvec->arr[i].val >> comp.uuid ; continue; @@ -295,5 +263,54 @@ void pke_deserialize_instance(srlztn_deserialize_helper *h, pke_kve_container *k continue; } } + if (et_ptr == nullptr) { + fprintf(stdout, "[Game::DeserializeInstance] Unknown EntityTypeCode, skipping instance.\n"); + } + btVector3 bt_pos; + btQuaternion bt_quat; + GlmToBullet(pos, bt_pos); + GlmToBullet(quat_rot, bt_quat); + GlmToBullet(scale, inst_pos.scale); + inst_pos.mass = mass; + inst_pos.posRot.setIdentity(); + inst_pos.posRot.setOrigin(bt_pos); + inst_pos.posRot.setRotation(bt_quat); + pk_arr_t<CompInstance *> instances; + srlztn_instance_mapping map{}; + map.serialized_uuid = comp.uuid; + if (et_ptr != nullptr) { + if (et_ptr->createInstanceCallback.func != nullptr) { + /* TODO 2025-03-27 JCB + * We have not yet defined what the appropriate callback signature + * for creating an entity instance is. + * What should be passed as arguments? What would need to be passed + * that couldn't be accessed globally? + * Consider changing this callback to trigger after creating a + * generic instance, rather than *creating* it. + * Also consider just requiring a generic instance for any given + * EntityType. + */ + // typedef Entity_Base *CreateInst(); + // entity = reinterpret_cast<CreateInst*>(et_ptr->createInstanceCallback.func)(); + fprintf(stderr, "[%s] Attempted to call EntityType::createInstanceCallback and we have not yet defined a valid function signature", __FILE__); + } else { + map.created_entity = EntityType_CreateGenericInstance(et_ptr, h->scene, &comp, &inst_pos); + } + } else { + map.created_entity = ECS_CreateGenericEntity(); + map.created_instance = ECS_CreateInstance(map.created_entity, comp.uuid, nullptr, &inst_pos); + } + ECS_GetInstances(map.created_entity, instances); + for (uint32_t i = 0; i < instances.next; ++i) { + if (comp.uuid == instances[i]->uuid) { + map.created_instance = instances[i]; + } + } + pk_arr_reset(&instances); + if (map.created_instance == nullptr) { + fprintf(stderr, "[pke_deserialize_instance] Failed to find created instance for creating mapping"); + } else { + pk_arr_append(&h->mapping, &map); + } return; } |
