diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-04-07 11:27:00 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-04-07 11:27:00 -0400 |
| commit | ff63a4b2bf9f096f8cf8c6824e826b3b4d79e747 (patch) | |
| tree | bc5834ffecfa87ad70aac530a14acf38245c515b | |
| parent | 2f57eefb01c478ffe62845b8051bc82036cbb819 (diff) | |
pke: saving and loading scenes works
| -rw-r--r-- | editor/editor-io.cpp | 6 | ||||
| -rw-r--r-- | editor/editor.cpp | 33 | ||||
| -rw-r--r-- | src/font.cpp | 18 | ||||
| -rw-r--r-- | src/game.cpp | 4 | ||||
| -rw-r--r-- | src/serialization-camera.cpp | 22 | ||||
| -rw-r--r-- | src/serialization-component.cpp | 28 | ||||
| -rw-r--r-- | src/serialization.cpp | 3 | ||||
| -rw-r--r-- | src/serialization.hpp | 2 |
8 files changed, 65 insertions, 51 deletions
diff --git a/editor/editor-io.cpp b/editor/editor-io.cpp index 1a80d2d..7b93b51 100644 --- a/editor/editor-io.cpp +++ b/editor/editor-io.cpp @@ -8,7 +8,6 @@ #include <fstream> void pke_editor_scene_save(const char *file_path) { - std::ostringstream stream{}; srlztn_serialize_helper *helper = pke_serialize_init(pkeSettings.mem.bkt); bool failed = false; @@ -27,7 +26,7 @@ void pke_editor_scene_save(const char *file_path) { if (!f.is_open()) { failed = true; } else { - f << stream.str(); + f << helper->o.str(); } f.flush(); f.close(); @@ -39,7 +38,7 @@ void pke_editor_scene_save(const char *file_path) { strncpy(errFileName + strlen(file_path), ".err", 256 - strlen(file_path)); std::ofstream errF(file_path); if (errF.is_open()) { - errF << stream.str(); + errF << helper->o.str(); errF.flush(); errF.close(); fprintf(stderr, "Failed to save scene file '%s', partial output saved to '%s'\n", file_path, errFileName); @@ -57,6 +56,7 @@ void pke_editor_scene_load(const char *file_path) { return; } srlztn_deserialize_helper *helper = pke_deserialize_init(pkeSettings.mem.bkt); + helper->i = &f; // TODO scene name is in the file? helper->scene = pke_scene_get_by_name(file_path); if (helper->scene == nullptr) { diff --git a/editor/editor.cpp b/editor/editor.cpp index 48e5cc1..17a2913 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -189,6 +189,15 @@ void PkeEditor_Tick(double delta) { } else { sprintf(file_path, "%.16s", editor_mstr.active_scene->name); } + int len = strlen(file_path); + if (strstr(file_path, ".pstf") == nullptr && len < 251) { + file_path[len+0] = '.'; + file_path[len+1] = 'p'; + file_path[len+2] = 's'; + file_path[len+3] = 't'; + file_path[len+4] = 'f'; + file_path[len+5] = '\0'; + } pke_editor_scene_save(file_path); shouldRebuildProjectDir = true; } @@ -1093,19 +1102,22 @@ void RecordImGuiAssets() { } void RecordImGuiCameras() { - CompInstance *activeInst = nullptr; - // CompInstance *activeTargetInst = nullptr; + CompInstance *active_inst = nullptr; if (!ImGui::Begin("Cameras")) { ImGui::End(); return; } - activeInst = ECS_GetInstance(ActiveCamera->phys.instHandle); - // activeTargetInst = ECS_GetInstance(ActiveCamera->phys.targetInstHandle); + active_inst = ECS_GetInstance(ActiveCamera->phys.instHandle); if (ImGui::Button("Create")) { InstPos instPos{}; instPos.mass = 1.f; - activeInst->bt.motionState->getWorldTransform(instPos.posRot); - instPos.scale = activeInst->bt.rigidBody->getCollisionShape()->getLocalScaling(); + if (active_inst == nullptr) { + instPos.posRot.setIdentity(); + instPos.scale = btVector3(1, 1, 1); + } else { + active_inst->bt.motionState->getWorldTransform(instPos.posRot); + instPos.scale = active_inst->bt.rigidBody->getCollisionShape()->getLocalScaling(); + } auto &cam = PkeCamera_Register(pk_uuid_zed, instPos); cam.phys.targetInstHandle = ActiveCamera->phys.targetInstHandle; cam.type = ActiveCamera->type; @@ -1168,8 +1180,13 @@ void RecordImGuiCameras() { if (ImGui::Button("Update Position")) { InstPos instPos{}; instPos.mass = 1.f; - activeInst->bt.motionState->getWorldTransform(instPos.posRot); - instPos.scale = activeInst->bt.rigidBody->getCollisionShape()->getLocalScaling(); + if (active_inst == nullptr) { + NullCameraInstance.bt.motionState->getWorldTransform(instPos.posRot); + instPos.scale = NullCameraInstance.bt.rigidBody->getCollisionShape()->getLocalScaling(); + } else { + active_inst->bt.motionState->getWorldTransform(instPos.posRot); + instPos.scale = active_inst->bt.rigidBody->getCollisionShape()->getLocalScaling(); + } CompInstance *camInst = ECS_GetInstance(cam.phys.instHandle); ECS_UpdateInstance(camInst, instPos, true); } diff --git a/src/font.cpp b/src/font.cpp index 64728ee..aa7fbe6 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -394,55 +394,55 @@ void FontType_Deserialize(std::istream &stream) { } if (strstr(readLine, PKE_PROJECT_FONT_MSDF_MIN_SCALE)) { uint64_t prefixLen = strlen(PKE_PROJECT_FONT_MSDF_MIN_SCALE); - auto result = pk_stn_float(&msdf.minimum_scale, readLine + prefixLen); + auto result = pk_stn(&msdf.minimum_scale, readLine + prefixLen, nullptr); assert(result == PK_STN_RES_SUCCESS); continue; } if (strstr(readLine, PKE_PROJECT_FONT_MSDF_PX_RANGE)) { uint64_t prefixLen = strlen(PKE_PROJECT_FONT_MSDF_PX_RANGE); - auto result = pk_stn_float(&msdf.px_range, readLine + prefixLen); + auto result = pk_stn(&msdf.px_range, readLine + prefixLen, nullptr); assert(result == PK_STN_RES_SUCCESS); continue; } if (strstr(readLine, PKE_PROJECT_FONT_SPACING_GEOMETRY_SCALE)) { uint64_t prefixLen = strlen(PKE_PROJECT_FONT_SPACING_GEOMETRY_SCALE); - auto result = pk_stn_double(&sp.geometry_scale, readLine + prefixLen); + auto result = pk_stn(&sp.geometry_scale, readLine + prefixLen, nullptr); assert(result == PK_STN_RES_SUCCESS); continue; } if (strstr(readLine, PKE_PROJECT_FONT_SPACING_EM_SIZE)) { uint64_t prefixLen = strlen(PKE_PROJECT_FONT_SPACING_EM_SIZE); - auto result = pk_stn_double(&sp.em_size, readLine + prefixLen); + auto result = pk_stn(&sp.em_size, readLine + prefixLen, nullptr); assert(result == PK_STN_RES_SUCCESS); continue; } if (strstr(readLine, PKE_PROJECT_FONT_SPACING_ASCENDER_Y)) { uint64_t prefixLen = strlen(PKE_PROJECT_FONT_SPACING_ASCENDER_Y); - auto result = pk_stn_double(&sp.ascender_y, readLine + prefixLen); + auto result = pk_stn(&sp.ascender_y, readLine + prefixLen, nullptr); assert(result == PK_STN_RES_SUCCESS); continue; } if (strstr(readLine, PKE_PROJECT_FONT_SPACING_DESCENDER_Y)) { uint64_t prefixLen = strlen(PKE_PROJECT_FONT_SPACING_DESCENDER_Y); - auto result = pk_stn_double(&sp.descender_y, readLine + prefixLen); + auto result = pk_stn(&sp.descender_y, readLine + prefixLen, nullptr); assert(result == PK_STN_RES_SUCCESS); continue; } if (strstr(readLine, PKE_PROJECT_FONT_SPACING_LINE_HEIGHT)) { uint64_t prefixLen = strlen(PKE_PROJECT_FONT_SPACING_LINE_HEIGHT); - auto result = pk_stn_double(&sp.line_height, readLine + prefixLen); + auto result = pk_stn(&sp.line_height, readLine + prefixLen, nullptr); assert(result == PK_STN_RES_SUCCESS); continue; } if (strstr(readLine, PKE_PROJECT_FONT_SPACING_UNDERLINE_Y)) { uint64_t prefixLen = strlen(PKE_PROJECT_FONT_SPACING_UNDERLINE_Y); - auto result = pk_stn_double(&sp.underline_y, readLine + prefixLen); + auto result = pk_stn(&sp.underline_y, readLine + prefixLen, nullptr); assert(result == PK_STN_RES_SUCCESS); continue; } if (strstr(readLine, PKE_PROJECT_FONT_SPACING_UNDERLINE_THICKNESS)) { uint64_t prefixLen = strlen(PKE_PROJECT_FONT_SPACING_UNDERLINE_THICKNESS); - auto result = pk_stn_double(&sp.underline_thickness, readLine + prefixLen); + auto result = pk_stn(&sp.underline_thickness, readLine + prefixLen, nullptr); assert(result == PK_STN_RES_SUCCESS); continue; } diff --git a/src/game.cpp b/src/game.cpp index 5e5e1b0..f5afa82 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2,7 +2,6 @@ #include "game.hpp" #include "camera.hpp" -#include "serialization-component.hpp" #include "components.hpp" #include "ecs.hpp" #include "entities.hpp" @@ -10,17 +9,14 @@ #include "font.hpp" #include "game-settings.hpp" #include "game-type-defs.hpp" -#include "helpers.hpp" #include "imgui.h" #include "level-types.hpp" #include "level.hpp" -#include "math-helpers.hpp" #include "physics.hpp" #include "player-input.hpp" #include "plugins.hpp" #include "project.hpp" #include "scene.hpp" -#include "serialization.hpp" #include "static-ui.hpp" #include "thread-pool.hpp" #include "window.hpp" diff --git a/src/serialization-camera.cpp b/src/serialization-camera.cpp index abf404b..501be6e 100644 --- a/src/serialization-camera.cpp +++ b/src/serialization-camera.cpp @@ -61,16 +61,18 @@ bool FindFirstInstanceHandle(void *handle, void *mapping) { void pke_deserialize_camera(srlztn_deserialize_helper *h) { PK_STN_RES stn_res; 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::quat quat_rot; - glm::vec3 scale; + glm::vec3 pos = glm::vec3(0); + glm::quat quat_rot = glm::quat(0, 0, 0, 1); + glm::vec3 scale = glm::vec3(1); instPos.mass = 1; instPos.posRot.setIdentity(); instPos.scale = btVector3(1, 1, 1); - while (h->i.getline(h->read_line, h->read_line_len)) { + while (h->i->getline(h->read_line, h->read_line_len)) { if (strcmp(h->read_line, SRLZTN_OBJ_END) == 0) { int64_t targetInstanceIndex = -1; @@ -109,7 +111,7 @@ 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); + stn_res = pk_stn(&cam.type, h->read_line + prefixLen, nullptr); if (stn_res != PK_STN_RES_SUCCESS) { fprintf(stderr, "[%s] Err '%u' parsing camera type from: '%s'", __FILE__, stn_res, h->read_line); } @@ -117,7 +119,7 @@ void pke_deserialize_camera(srlztn_deserialize_helper *h) { } 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); + stn_res = pk_stn(&cam.view, h->read_line + prefixLen, nullptr); if (stn_res != PK_STN_RES_SUCCESS) { fprintf(stderr, "[%s] Err '%u' parsing camera view from: '%s'", __FILE__, stn_res, h->read_line); } @@ -126,11 +128,11 @@ void pke_deserialize_camera(srlztn_deserialize_helper *h) { 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); + 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); + 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); } @@ -141,9 +143,9 @@ void pke_deserialize_camera(srlztn_deserialize_helper *h) { (h->read_line + prefixLen) >> cam.uuid; continue; } - if (strncmp(h->read_line, SRLZTN_CAMERA_IS_PRIMARY, strlen(SRLZTN_CAMERA_IS_PRIMARY)) == 0) { + 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); + 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); } diff --git a/src/serialization-component.cpp b/src/serialization-component.cpp index 34eb758..9a29e67 100644 --- a/src/serialization-component.cpp +++ b/src/serialization-component.cpp @@ -47,7 +47,7 @@ bool pke_deserialize_inst_pos(srlztn_deserialize_helper *h, glm::vec3 &pos, glm: long index = 0; do { assert(index < 3); - stn_res = pk_stn_float_e(&pos[index], starting_char, &pEnd); + stn_res = pk_stn(&pos[index], starting_char, &pEnd); if (stn_res != PK_STN_RES_SUCCESS) return false; starting_char = pEnd + 1; ++index; @@ -62,7 +62,7 @@ bool pke_deserialize_inst_pos(srlztn_deserialize_helper *h, glm::vec3 &pos, glm: long index = 0; do { assert(index < 4); - stn_res = pk_stn_float_e(&quat_rot[index], starting_char, &pEnd); + stn_res = pk_stn(&quat_rot[index], starting_char, &pEnd); if (stn_res != PK_STN_RES_SUCCESS) return false; starting_char = pEnd + 1; ++index; @@ -77,7 +77,7 @@ bool pke_deserialize_inst_pos(srlztn_deserialize_helper *h, glm::vec3 &pos, glm: long index = 0; do { assert(index < 3); - stn_res = pk_stn_float_e(&scale[index], starting_char, &pEnd); + stn_res = pk_stn(&scale[index], starting_char, &pEnd); if (stn_res != PK_STN_RES_SUCCESS) return false; starting_char = pEnd + 1; ++index; @@ -156,11 +156,11 @@ bool pke_deserialize_instance(srlztn_deserialize_helper *h) { float mass; InstPos inst_pos; CompInstance comp{}; - glm::vec3 pos; - glm::quat quat_rot; - glm::vec3 scale; + glm::vec3 pos = glm::vec3(0); + glm::quat quat_rot = glm::quat(0, 0, 0, 1); + glm::vec3 scale = glm::vec3(1); comp.collisionCallback.name[0] = '\0'; - while (h->i.getline(h->read_line, h->read_line_len)) { + while (h->i->getline(h->read_line, h->read_line_len)) { if (strstr(SRLZTN_OBJ_END, h->read_line)) { if (et_ptr == nullptr) { fprintf(stdout, "[Game::DeserializeInstance] Unknown EntityTypeCode, skipping instance.\n"); @@ -198,18 +198,18 @@ bool pke_deserialize_instance(srlztn_deserialize_helper *h) { 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, 16); + 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, 16); + 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, 16); + 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, 16); + stn_res = pk_stn(&comp.instanceHandle.itemIndex, handle_str + 11, nullptr, 16); if (stn_res != PK_STN_RES_SUCCESS) return false; continue; } @@ -227,19 +227,19 @@ bool pke_deserialize_instance(srlztn_deserialize_helper *h) { } if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_MASS)) { prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_MASS); - stn_res = pk_stn(&mass, h->read_line + prefix_len); + stn_res = pk_stn(&mass, h->read_line + prefix_len, nullptr); if (stn_res != PK_STN_RES_SUCCESS) return false; continue; } if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_COLLISION_LAYER)) { prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_COLLISION_LAYER); - stn_res = pk_stn(&comp.physicsLayer, h->read_line + prefix_len, 10); + stn_res = pk_stn(&comp.physicsLayer, h->read_line + prefix_len, nullptr, 10); if (stn_res != PK_STN_RES_SUCCESS) return false; continue; } if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_COLLISION_MASK)) { prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_COLLISION_MASK); - stn_res = pk_stn(&comp.physicsMask, h->read_line + prefix_len, 10); + stn_res = pk_stn(&comp.physicsMask, h->read_line + prefix_len, nullptr, 10); if (stn_res != PK_STN_RES_SUCCESS) return false; continue; } diff --git a/src/serialization.cpp b/src/serialization.cpp index d7dd312..507a238 100644 --- a/src/serialization.cpp +++ b/src/serialization.cpp @@ -18,7 +18,6 @@ srlztn_deserialize_helper *pke_deserialize_init(pk_membucket *bkt) { helper->bkt = bkt; helper->read_line = nullptr; // TODO custom allocator - helper->i = {}; helper->mapping = {bkt}; return helper; } @@ -81,7 +80,7 @@ void pke_deserialize_file_scene(srlztn_deserialize_helper *h) { h->read_line = pk_new<char>(h->read_line_len, h->bkt); memset(h->read_line, '\0', h->read_line_len); - while (h->i.getline(h->read_line, h->read_line_len)) { + while (h->i->getline(h->read_line, h->read_line_len)) { if (strcmp(SRLZTN_OBJ_INSTANCE, h->read_line) == 0) { pke_deserialize_instance(h); continue; diff --git a/src/serialization.hpp b/src/serialization.hpp index f4180e4..97bd4e8 100644 --- a/src/serialization.hpp +++ b/src/serialization.hpp @@ -50,7 +50,7 @@ struct srlztn_deserialize_helper { pke_scene *scene; pk_membucket *bkt; char *read_line; - std::istringstream i; + std::istream *i; pk_arr_t<srlztn_instance_mapping> mapping; }; |
