diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-04-07 14:12:28 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-04-07 14:30:50 -0400 |
| commit | 7b43a9b51d9df0bc0e18102415f877772031f440 (patch) | |
| tree | bbbdc15d8972ee4ca4d4a8379ffddb0be62752e3 | |
| parent | ff63a4b2bf9f096f8cf8c6824e826b3b4d79e747 (diff) | |
pke: save and load camera target in scenes
| -rw-r--r-- | editor/editor.cpp | 8 | ||||
| -rw-r--r-- | src/camera.cpp | 7 | ||||
| -rw-r--r-- | src/camera.hpp | 3 | ||||
| -rw-r--r-- | src/serialization-camera.cpp | 51 | ||||
| -rw-r--r-- | src/serialization-component.cpp | 47 | ||||
| -rw-r--r-- | src/serialization.hpp | 2 | ||||
| -rw-r--r-- | src/window.cpp | 2 |
7 files changed, 47 insertions, 73 deletions
diff --git a/editor/editor.cpp b/editor/editor.cpp index 17a2913..72f35f6 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -342,7 +342,8 @@ void PkeEditor_Tick(double delta) { NullCameraInstance.bt.motionState->setWorldTransform(btfm); NullCameraInstance.bt.rigidBody->setWorldTransform(btfm); NullCameraInstance.bt.rigidBody->activate(); - NullCamera.phys.targetInstHandle = ActiveCamera->phys.targetInstHandle; + NullCamera.phys.target_inst_uuid = ActiveCamera->phys.target_inst_uuid; + NullCamera.phys.target_inst_handle = ActiveCamera->phys.target_inst_handle; NullCamera.type = ActiveCamera->type; NullCamera.view = ActiveCamera->view; NullCamera.stale = PKE_CAMERA_STALE_ALL; @@ -1119,7 +1120,8 @@ void RecordImGuiCameras() { instPos.scale = active_inst->bt.rigidBody->getCollisionShape()->getLocalScaling(); } auto &cam = PkeCamera_Register(pk_uuid_zed, instPos); - cam.phys.targetInstHandle = ActiveCamera->phys.targetInstHandle; + cam.phys.target_inst_uuid = ActiveCamera->phys.target_inst_uuid; + cam.phys.target_inst_handle = ActiveCamera->phys.target_inst_handle; cam.type = ActiveCamera->type; cam.view = ActiveCamera->view; cam.isPrimary = false; @@ -1167,7 +1169,7 @@ void RecordImGuiCameras() { ImGui::TableSetColumnIndex(1); ImGui::Text("0x%08X 0x%08X", cam.handle.bucketIndex, cam.handle.itemIndex); ImGui::TableSetColumnIndex(2); - ImGui::Text("0x%08X 0x%08X", cam.phys.targetInstHandle.bucketIndex, cam.phys.targetInstHandle.itemIndex); + ImGui::Text(pk_uuid_printf_format, pk_uuid_printf_var(cam.phys.target_inst_uuid)); ImGui::TableSetColumnIndex(3); ImGui::Text("%hhu", (unsigned char)cam.type); ImGui::TableSetColumnIndex(4); diff --git a/src/camera.cpp b/src/camera.cpp index de4dff5..1bc1465 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -106,7 +106,8 @@ void PkeCamera_TargetInstance(CameraHandle cameraHandle, CompInstance *inst) { trfm.setOrigin(trfm.getOrigin() + cameraOffset); trfm.setRotation(bQuatRot); - cam.phys.targetInstHandle = inst->instanceHandle; + cam.phys.target_inst_uuid = inst->uuid; + cam.phys.target_inst_handle = inst->instanceHandle; selfInstance->bt.motionState->setWorldTransform(trfm); selfInstance->bt.rigidBody->setWorldTransform(trfm); selfInstance->bt.rigidBody->setLinearVelocity(btVector3(0,0,0)); @@ -170,7 +171,7 @@ void PkeCamera_Destroy(CameraHandle cameraHandle) { cam.view = PkeCameraView_MAX; cam.stale = PkeCameraStaleFlags_MAX; cam.phys.instHandle = InstanceHandle_MAX; - cam.phys.targetInstHandle = InstanceHandle_MAX; + cam.phys.target_inst_uuid = pk_uuid_zed; cam.phys.constraint = CAFE_BABE(btTypedConstraint); } @@ -235,7 +236,7 @@ void PkeCamera_Tick(double delta) { cam.stale = cam.stale | PKE_CAMERA_STALE_POSROT; inst->isNeedingUpdated = false; } - if (cam.phys.targetInstHandle == InstanceHandle_MAX) continue; + if (cam.phys.target_inst_uuid == pk_uuid_zed) continue; cam.stale = cam.stale | PKE_CAMERA_STALE_POSROT; } } diff --git a/src/camera.hpp b/src/camera.hpp index e50ae54..cef3c12 100644 --- a/src/camera.hpp +++ b/src/camera.hpp @@ -33,7 +33,8 @@ struct PkeCamera : public Entity_Base { PkeCameraStaleFlags stale = PkeCameraStaleFlags_MAX; struct Phys { InstanceHandle instHandle = InstanceHandle_MAX; - InstanceHandle targetInstHandle = InstanceHandle_MAX; + InstanceHandle target_inst_handle = InstanceHandle_MAX; + pk_uuid target_inst_uuid = pk_uuid_zed; btTypedConstraint *constraint = nullptr; } phys; bool isPrimary = false; diff --git a/src/serialization-camera.cpp b/src/serialization-camera.cpp index 501be6e..0183e29 100644 --- a/src/serialization-camera.cpp +++ b/src/serialization-camera.cpp @@ -11,7 +11,6 @@ #include "vendor-glm-include.hpp" void pke_serialize_camera(srlztn_serialize_helper *h, const PkeCamera *cam) { - NULL_CHAR_ARR(handleStr, 23); PkeCamera c{}; if (cam->uuid != pk_uuid_zed && cam->uuid != pk_uuid_max) { h->o << SRLZTN_CAMERA_UUID << cam->uuid << std::endl; @@ -22,13 +21,8 @@ void pke_serialize_camera(srlztn_serialize_helper *h, const PkeCamera *cam) { if (cam->view != c.view) { h->o << SRLZTN_CAMERA_ORIENTATION << int(static_cast<PkeCameraView_T>(cam->view)) << std::endl; } - if (cam->phys.instHandle != InstanceHandle_MAX) { - snprintf(handleStr, 22, "0x%08X 0x%08X", cam->phys.instHandle.bucketIndex, cam->phys.instHandle.itemIndex); - h->o << SRLZTN_CAMERA_INSTANCE_HANDLE << handleStr << std::endl; - } - if (cam->phys.targetInstHandle != InstanceHandle_MAX) { - snprintf(handleStr, 22, "0x%08X 0x%08X", cam->phys.targetInstHandle.bucketIndex, cam->phys.targetInstHandle.itemIndex); - h->o << SRLZTN_CAMERA_TARGET_INSTANCE_UUID << handleStr << std::endl; + if (cam->phys.target_inst_uuid != pk_uuid_zed) { + h->o << SRLZTN_CAMERA_TARGET_INSTANCE_UUID << cam->phys.target_inst_uuid << std::endl; } if (cam->isPrimary != c.isPrimary) { h->o << SRLZTN_CAMERA_IS_PRIMARY << cam->isPrimary << std::endl; @@ -59,12 +53,11 @@ bool FindFirstInstanceHandle(void *handle, void *mapping) { return inst_mapping->serialized_uuid == *reinterpret_cast<pk_uuid *>(handle); } void pke_deserialize_camera(srlztn_deserialize_helper *h) { - PK_STN_RES stn_res; + PK_STN_RES stn_res = PK_STN_RES(0); PkeCamera cam{}; cam.type = PKE_CAMERA_TYPE_PERSPECTIVE; cam.view = PKE_CAMERA_VIEW_FREE; InstPos instPos; - InstanceHandle instanceHandle = InstanceHandle_MAX; pk_uuid target_uuid = pk_uuid_zed; glm::vec3 pos = glm::vec3(0); glm::quat quat_rot = glm::quat(0, 0, 0, 1); @@ -75,10 +68,12 @@ void pke_deserialize_camera(srlztn_deserialize_helper *h) { while (h->i->getline(h->read_line, h->read_line_len)) { if (strcmp(h->read_line, SRLZTN_OBJ_END) == 0) { - int64_t targetInstanceIndex = -1; - + uint32_t targetInstanceIndex = -1; if (target_uuid != pk_uuid_zed) { targetInstanceIndex = pk_arr_find_first_index(&h->mapping, &target_uuid, FindFirstInstanceHandle); + if (targetInstanceIndex == uint32_t(-1)) { + fprintf(stderr, "[pke_deserialize_camera] Camera has target instance uuid '" pk_uuid_printf_format "', but failed to find target instance.", pk_uuid_printf_var(target_uuid)); + } } btVector3 bt_pos; @@ -93,7 +88,8 @@ void pke_deserialize_camera(srlztn_deserialize_helper *h) { rCam.view = cam.view; rCam.isPrimary = cam.isPrimary; pke_scene_register_camera(h->scene->scene_handle, rCam.camHandle); - if (targetInstanceIndex > -1) { + if (targetInstanceIndex != uint32_t(-1)) { + rCam.phys.target_inst_handle = h->mapping[targetInstanceIndex].created_instance->instanceHandle; PkeCamera_TargetInstance(rCam.camHandle, h->mapping[targetInstanceIndex].created_instance); } if (rCam.isPrimary == true) { @@ -111,43 +107,34 @@ void pke_deserialize_camera(srlztn_deserialize_helper *h) { } if (strncmp(h->read_line, SRLZTN_CAMERA_TYPE, strlen(SRLZTN_CAMERA_TYPE)) == 0) { uint64_t prefixLen = strlen(SRLZTN_CAMERA_TYPE); - stn_res = pk_stn(&cam.type, h->read_line + prefixLen, nullptr); + PkeCameraType_T cam_type; + stn_res = pk_stn(&cam_type, h->read_line + prefixLen, nullptr); + cam.type = PkeCameraType(cam_type); if (stn_res != PK_STN_RES_SUCCESS) { - fprintf(stderr, "[%s] Err '%u' parsing camera type from: '%s'", __FILE__, stn_res, h->read_line); + fprintf(stderr, "[%s] Err '%u' parsing camera type from: '%s'\n", __FILE__, stn_res, h->read_line); } continue; } if (strncmp(h->read_line, SRLZTN_CAMERA_ORIENTATION, strlen(SRLZTN_CAMERA_ORIENTATION)) == 0) { uint64_t prefixLen = strlen(SRLZTN_CAMERA_ORIENTATION); - stn_res = pk_stn(&cam.view, h->read_line + prefixLen, nullptr); + PkeCameraView_T cam_view; + stn_res = pk_stn(&cam_view, h->read_line + prefixLen, nullptr); + cam.view = PkeCameraView(cam_view); if (stn_res != PK_STN_RES_SUCCESS) { - fprintf(stderr, "[%s] Err '%u' parsing camera view from: '%s'", __FILE__, stn_res, h->read_line); - } - continue; - } - if (strstr(h->read_line, SRLZTN_CAMERA_INSTANCE_HANDLE)) { - uint64_t prefixLen = strlen(SRLZTN_CAMERA_INSTANCE_HANDLE); - h->read_line[prefixLen + 10] = '\0'; - stn_res = pk_stn(&instanceHandle.bucketIndex, h->read_line + prefixLen, nullptr, 16); - if (stn_res != PK_STN_RES_SUCCESS) { - fprintf(stderr, "[%s] Err '%u' parsing camera instance handle from: '%s'", __FILE__, stn_res, h->read_line); - } - stn_res = pk_stn(&instanceHandle.itemIndex, h->read_line + prefixLen + 11, nullptr, 16); - if (stn_res != PK_STN_RES_SUCCESS) { - fprintf(stderr, "[%s] Err '%u' parsing camera instance handle from: '%s'", __FILE__, stn_res, h->read_line); + fprintf(stderr, "[%s] Err '%u' parsing camera view from: '%s'\n", __FILE__, stn_res, h->read_line); } continue; } if (strstr(h->read_line, SRLZTN_CAMERA_TARGET_INSTANCE_UUID)) { uint64_t prefixLen = strlen(SRLZTN_CAMERA_TARGET_INSTANCE_UUID); - (h->read_line + prefixLen) >> cam.uuid; + (h->read_line + prefixLen) >> target_uuid; continue; } if (strstr(h->read_line, SRLZTN_CAMERA_IS_PRIMARY)) { uint64_t prefixLen = strlen(SRLZTN_CAMERA_IS_PRIMARY); stn_res = pk_stn(&cam.isPrimary, h->read_line + prefixLen, nullptr); if (stn_res != PK_STN_RES_SUCCESS) { - fprintf(stderr, "[%s] Err '%u' parsing camera primary from: '%s'", __FILE__, stn_res, h->read_line); + fprintf(stderr, "[%s] Err '%u' parsing camera primary from: '%s'\n", __FILE__, stn_res, h->read_line); } continue; } diff --git a/src/serialization-component.cpp b/src/serialization-component.cpp index 9a29e67..8867339 100644 --- a/src/serialization-component.cpp +++ b/src/serialization-component.cpp @@ -88,8 +88,6 @@ bool pke_deserialize_inst_pos(srlztn_deserialize_helper *h, glm::vec3 &pos, glm: } bool pke_serialize_instance(srlztn_serialize_helper *h, const CompInstance *comp) { - NULL_CHAR_ARR(handle_str, 23); - EntityType *et = nullptr; if (comp->grBindsHandle != GrBindsHandle_MAX) { et = EntityType_FindByEntityHandle(ECS_GetGrBinds(comp->grBindsHandle)->entHandle); @@ -115,14 +113,6 @@ bool pke_serialize_instance(srlztn_serialize_helper *h, const CompInstance *comp BulletToGlm(comp->bt.rigidBody->getCollisionShape()->getLocalScaling(), scale); } - if (comp->instanceHandle != InstanceHandle_MAX) { - snprintf(handle_str, 22, "0x%08X 0x%08X", comp->instanceHandle.bucketIndex, comp->instanceHandle.itemIndex); - h->o << SRLZTN_INSTANCE_COMPONENT_ENTITY_HANDLE << handle_str << std::endl; - } - if (comp->entHandle != InstanceHandle_MAX) { - snprintf(handle_str, 22, "0x%08X 0x%08X", comp->entHandle.bucketIndex, comp->entHandle.itemIndex); - h->o << SRLZTN_INSTANCE_COMPONENT_HANDLE << handle_str << std::endl; - } if (comp->uuid != pk_uuid_zed && comp->uuid != pk_uuid_max) { h->o << SRLZTN_INSTANCE_COMPONENT_UUID << comp->uuid << std::endl; } @@ -151,7 +141,6 @@ bool pke_serialize_instance(srlztn_serialize_helper *h, const CompInstance *comp bool pke_deserialize_instance(srlztn_deserialize_helper *h) { uint64_t prefix_len; PK_STN_RES stn_res; - NULL_CHAR_ARR(handle_str, 21); EntityType *et_ptr = nullptr; float mass; InstPos inst_pos; @@ -190,29 +179,25 @@ bool pke_deserialize_instance(srlztn_deserialize_helper *h) { // 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 { - EntityType_CreateGenericInstance(et_ptr, h->scene, &comp, &inst_pos); - // fprintf(stdout ,"[Game::DeserializeInstance] Debug: entTypeCode '%s' does not have a registered callback func to handle instance creation.\n", entTypeCode); + 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); + } } return true; } - if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_ENTITY_HANDLE)) { - prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_ENTITY_HANDLE); - sprintf(handle_str, "%.10s%c%s", h->read_line + prefix_len, '\0', h->read_line + prefix_len + 10); - stn_res = pk_stn(&comp.entHandle.bucketIndex, handle_str, nullptr, 16); - if (stn_res != PK_STN_RES_SUCCESS) return false; - stn_res = pk_stn(&comp.entHandle.itemIndex, handle_str + 11, nullptr, 16); - if (stn_res != PK_STN_RES_SUCCESS) return false; - continue; - } - if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_HANDLE)) { - prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_HANDLE); - sprintf(handle_str, "%.10s%c%s", h->read_line + prefix_len, '\0', h->read_line + prefix_len + 10); - stn_res = pk_stn(&comp.instanceHandle.bucketIndex, handle_str, nullptr, 16); - if (stn_res != PK_STN_RES_SUCCESS) return false; - stn_res = pk_stn(&comp.instanceHandle.itemIndex, handle_str + 11, nullptr, 16); - if (stn_res != PK_STN_RES_SUCCESS) return false; - continue; - } if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_UUID)) { prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_UUID); (h->read_line + prefix_len) >> comp.uuid ; diff --git a/src/serialization.hpp b/src/serialization.hpp index 97bd4e8..4b0624c 100644 --- a/src/serialization.hpp +++ b/src/serialization.hpp @@ -26,11 +26,9 @@ inline const char* const SRLZTN_INSTANCE_COMPONENT_COLLISION_LAYER = "Collision inline const char* const SRLZTN_INSTANCE_COMPONENT_COLLISION_MASK = "CollisionMask: "; inline const char* const SRLZTN_INSTANCE_COMPONENT_COLLISION_CB_SIGNATURE = "CollisionCBSignature: "; -inline const char* const SRLZTN_CAMERA_TARGET = "Target: "; inline const char* const SRLZTN_CAMERA_TYPE = "Type: "; inline const char* const SRLZTN_CAMERA_UUID = "UUID: "; inline const char* const SRLZTN_CAMERA_ORIENTATION = "Orientation: "; -inline const char* const SRLZTN_CAMERA_INSTANCE_HANDLE = "InstanceHandle: "; inline const char* const SRLZTN_CAMERA_TARGET_INSTANCE_UUID = "TargetUUID: "; inline const char* const SRLZTN_CAMERA_IS_PRIMARY = "IsPrimary: "; diff --git a/src/window.cpp b/src/window.cpp index 24dc1bf..4a97d56 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1381,7 +1381,7 @@ void UpdateCamera() { UBO.view = glm::mat4_cast(gRot); } else if (bool(ActiveCamera->view == PKE_CAMERA_VIEW_TARGET)) { glm::vec3 gTargetPos{0.f, 0.f, 0.f}; - targetInst = ECS_GetInstance(ActiveCamera->phys.targetInstHandle); + targetInst = ECS_GetInstance(ActiveCamera->phys.target_inst_handle); if (targetInst != nullptr && targetInst != CAFE_BABE(CompInstance)) { btTransform targetTrfm; targetInst->bt.motionState->getWorldTransform(targetTrfm); |
