summaryrefslogtreecommitdiff
path: root/editor
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-09-09 16:05:14 -0400
committerJonathan Bradley <jcb@pikum.xyz>2025-09-09 16:05:14 -0400
commit5daa12fed0449a7811fc25ec62236bc060a56fa7 (patch)
tree5af5ff1c4de2ab49dd4d1db9934b3b4ee34a54c2 /editor
parentc40277cd665e04d300ea839a1dd2ff675655f5fb (diff)
pke: first-pass promote pke_level over pke_scene
Major runtime ownership overhaul (scene -> level). Major ecs 'marked for removal' overhaul, ensuring that entities and their components are actually being removed when levels are unloaded.
Diffstat (limited to 'editor')
-rw-r--r--editor/editor-io.cpp10
-rw-r--r--editor/editor-io.hpp4
-rw-r--r--editor/editor-types.cpp2
-rw-r--r--editor/editor-types.hpp4
-rw-r--r--editor/editor.cpp85
5 files changed, 42 insertions, 63 deletions
diff --git a/editor/editor-io.cpp b/editor/editor-io.cpp
index ae928e1..27d84fb 100644
--- a/editor/editor-io.cpp
+++ b/editor/editor-io.cpp
@@ -2,7 +2,6 @@
#include "editor-io.hpp"
#include "game-settings.hpp"
-#include "scene.hpp"
#include "serialization.hpp"
#include <fstream>
@@ -51,18 +50,13 @@ void pke_editor_scene_save(const char *file_path) {
pke_serialize_teardown(helper);
}
-void pke_editor_scene_load(const char *file_path) {
+void pke_editor_scene_load(pke_level *level, const char *file_path) {
std::ifstream f(file_path);
if (!f.is_open()) {
fprintf(stderr, "Failed to load requested scene file: '%s'\n", file_path);
return;
}
- srlztn_deserialize_helper *helper = pke_deserialize_init(pkeSettings.mem_bkt.game_transient);
- // TODO scene name is in the file?
- helper->scene = pke_scene_get_by_name(file_path);
- if (helper->scene == nullptr) {
- helper->scene = pke_scene_create(file_path);
- }
+ srlztn_deserialize_helper *helper = pke_deserialize_init(level, pkeSettings.mem_bkt.game_transient);
pke_deserialize_scene_from_stream(f, helper);
pke_deserialize_scene(helper);
diff --git a/editor/editor-io.hpp b/editor/editor-io.hpp
index 07e9354..171b67c 100644
--- a/editor/editor-io.hpp
+++ b/editor/editor-io.hpp
@@ -1,8 +1,10 @@
#ifndef PKE_EDITOR_EDITOR_IO_HPP
#define PKE_EDITOR_EDITOR_IO_HPP
+#include "level-types.hpp"
+
void pke_editor_scene_save(const char *file_path);
-void pke_editor_scene_load(const char *file_path);
+void pke_editor_scene_load(pke_level *level, const char *file_path);
void pke_editor_project_save(const char *file_path);
void pke_editor_project_load(const char *file_path);
diff --git a/editor/editor-types.cpp b/editor/editor-types.cpp
index b9d278a..7e2c392 100644
--- a/editor/editor-types.cpp
+++ b/editor/editor-types.cpp
@@ -1,4 +1,4 @@
#include "editor-types.hpp"
-struct editor_master editor_mstr;
+struct editor_master editor_mstr{};
diff --git a/editor/editor-types.hpp b/editor/editor-types.hpp
index ff8239b..f84b7d3 100644
--- a/editor/editor-types.hpp
+++ b/editor/editor-types.hpp
@@ -6,8 +6,8 @@
// TODO editor state (scene vs level)
struct editor_master {
- pke_scene *active_scene;
- pk_str target_scene_path;
+ pke_scene *active_scene = nullptr;
+ pk_str target_scene_path = {};
bool shouldLoadScene = false;
bool shouldSaveScene = false;
};
diff --git a/editor/editor.cpp b/editor/editor.cpp
index ab8598c..2a4860a 100644
--- a/editor/editor.cpp
+++ b/editor/editor.cpp
@@ -12,6 +12,7 @@
#include "game-settings.hpp"
#include "game.hpp"
#include "imgui.h"
+#include "level-types.hpp"
#include "level.hpp"
#include "math-helpers.hpp"
#include "msdf-atlas-gen/msdf-atlas-gen.h"
@@ -35,6 +36,7 @@
#include <cstdio>
#include <filesystem>
#include <future>
+#include <fstream>
#include <regex>
#ifdef WIN32
@@ -84,7 +86,7 @@ CameraHandle selectedCamera = CameraHandle_MAX;
static pke_ui_box *selected_ui_box = NULL;
const char* const newSceneName = "newScene.pstf";
-bool shouldOpenLoadSceneDialog = false;
+const char* const editorLevelName = "editor_level";
bool shouldOpenSaveSceneDialog = false;
bool shouldOpenNewScene = false;
bool shouldSaveProjectFile = false;
@@ -108,6 +110,7 @@ glm::vec3 unproject(glm::vec3 windowCoords) {
}
void PkeEditor_Tick(double delta) {
+ const char *scene_to_load_or_create_path = nullptr;
if (shouldRunCurrentScene) {
shouldRunCurrentScene = false;
auto *task = pk_new<std::packaged_task<void()>>();
@@ -142,48 +145,18 @@ void PkeEditor_Tick(double delta) {
}
if (shouldDisableEditor) {
PkeEditor_Teardown();
+ return;
}
if (shouldOpenNewScene) {
shouldOpenNewScene = false;
- if (editor_mstr.active_scene != nullptr) {
- // TODO move most of this to the level
- pke_ui_box_count_T box_count;
- pke_ui_box **root_boxes = pke_ui_get_root_boxes(&box_count);
- for (pke_ui_box_count_T i = 0; i < box_count; ++i) {
- ECS_MarkForRemoval(root_boxes[i]);
- }
- pke_scene_remove(editor_mstr.active_scene->scene_handle);
- }
- ActiveCamera = &NullCamera;
- editor_mstr.active_scene = pke_scene_create(newSceneName);
- return;
+ editor_mstr.shouldLoadScene = false;
+ editor_mstr.shouldSaveScene = false;
+ scene_to_load_or_create_path = newSceneName;
}
if (shouldSaveProjectFile) {
shouldSaveProjectFile = false;
PkeProject_Save();
}
- if (shouldOpenLoadSceneDialog) {
- shouldOpenLoadSceneDialog = false;
- auto *task = pk_new<std::packaged_task<void()>>();
- new (task) std::packaged_task<void()>( [] {
- // TODO move most of this to the level
- pke_ui_box_count_T box_count;
- pke_ui_box **root_boxes = pke_ui_get_root_boxes(&box_count);
- for (pke_ui_box_count_T i = 0; i < box_count; ++i) {
- ECS_MarkForRemoval(root_boxes[i]);
- }
- const char * patterns[1] = {"*.pstf"};
- char *selectedScene = tinyfd_openFileDialog(nullptr, "cafebabe.pstf", 1, patterns, "Pke Scene Text File", 0);
- if (editor_mstr.active_scene != nullptr) {
- pke_scene_remove(editor_mstr.active_scene->scene_handle);
- }
- ActiveCamera = &NullCamera;
- editor_mstr.active_scene = pke_scene_create(selectedScene);
- return;
- });
- PkeThreads_Enqueue(threadPoolHandle, task);
- }
-
if (shouldOpenSaveSceneDialog) {
assert(editor_mstr.active_scene != nullptr);
shouldOpenSaveSceneDialog = false;
@@ -225,27 +198,36 @@ void PkeEditor_Tick(double delta) {
shouldRebuildProjectDir = true;
}
if (editor_mstr.target_scene_path.val != nullptr) {
- // TODO move most of this to the level
- pke_ui_box_count_T box_count;
- pke_ui_box **root_boxes = pke_ui_get_root_boxes(&box_count);
- for (pke_ui_box_count_T i = 0; i < box_count; ++i) {
- ECS_MarkForRemoval(root_boxes[i]);
+ scene_to_load_or_create_path = editor_mstr.target_scene_path.val;
+ }
+ if (scene_to_load_or_create_path != nullptr) {
+ ActiveCamera = &NullCamera;
+
+ pkeSettings.rt.nextLevel = pke_level_create(editorLevelName, pk_uuid_zed, pk_uuid_zed);
+ editor_mstr.active_scene = pke_scene_create(scene_to_load_or_create_path);
+ scene_instance si{};
+ si.scene_handle = editor_mstr.active_scene->scene_handle;
+ pk_arr_append_t(&pkeSettings.rt.nextLevel->scene_instances, si);
+
+ if (scene_to_load_or_create_path == newSceneName) {
+ return;
}
- if (editor_mstr.active_scene != nullptr) {
- pke_scene_remove(editor_mstr.active_scene->scene_handle);
+
+ std::filesystem::path p(scene_to_load_or_create_path);
+ std::ifstream f(p);
+ if (f.good()) {
+ pke_editor_scene_load(pkeSettings.rt.nextLevel, p.c_str());
}
- ActiveCamera = &NullCamera;
- pke_editor_scene_load(editor_mstr.target_scene_path.val);
- std::filesystem::path p(editor_mstr.target_scene_path.val);
- editor_mstr.active_scene = pke_scene_get_by_name(p.stem().c_str());
- if (editor_mstr.active_scene) {
+
+ if (editor_mstr.target_scene_path.reserved > 0) {
editor_mstr.active_scene->file_path = editor_mstr.target_scene_path;
+ editor_mstr.target_scene_path = {};
}
- editor_mstr.target_scene_path = {};
+
return;
}
- if (selectedEntity && pk_arr_find_first_index(&EntitiesToBeRemoved, ECS_GetEntity(selectedEntity->entHandle), ecs_pk_arr_find_first_matching_pointer) != uint32_t(-1)) {
+ if (selectedEntity && ECS_GetEntity(selectedEntity->entHandle)->isMarkedForRemoval == true) {
selectedEntity = nullptr;
}
@@ -681,7 +663,7 @@ void RecordImGuiEditorWrapper() {
* - the goal is not to prevent a specific scene name,
* I just want to know if they clicked the "New Scene" button
*/
- ImGui::BeginDisabled(editor_mstr.active_scene && editor_mstr.active_scene->name == newSceneName);
+ ImGui::BeginDisabled(editor_mstr.active_scene && (editor_mstr.active_scene->name == newSceneName || editor_mstr.active_scene->file_path.val == nullptr));
if (ImGui::MenuItem("Save")) {
shouldSaveProjectFile = true;
editor_mstr.shouldSaveScene = true;
@@ -1153,7 +1135,7 @@ void RecordImGuiCameras() {
cam.type = ActiveCamera->type;
cam.view = ActiveCamera->view;
cam.isPrimary = false;
- pke_scene_register_camera(editor_mstr.active_scene->scene_handle, cam.camHandle);
+ pke_level_register_camera(pkeSettings.rt.activeLevel, &cam);
}
static ImGuiTableFlags tableFlags{
@@ -1317,6 +1299,7 @@ void RecordImGuiUITree() {
box = pke_ui_box_new_child(selected_ui_box, type);
} else {
box = pke_ui_box_new_root(type);
+ pke_level_register_root_ui_box(pkeSettings.rt.activeLevel, box);
}
box->flags = PKE_UI_BOX_FLAG_POSITION_TYPE_DYNAMIC;
box->flags |= PKE_UI_BOX_FLAG_CENTER_BOTH;