diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-21 20:54:14 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-21 21:40:21 -0400 |
| commit | 677a3cbec2f7908ee0897b97d508a6bd66a0a344 (patch) | |
| tree | fc88b21dd61dbb10dd8b5c8aef73702d15514f00 /src/game.cpp | |
| parent | cae76dd98e301a4560bb46ecb59b5952dff04149 (diff) | |
pke: first-pass level is a collection of scenes
Diffstat (limited to 'src/game.cpp')
| -rw-r--r-- | src/game.cpp | 57 |
1 files changed, 42 insertions, 15 deletions
diff --git a/src/game.cpp b/src/game.cpp index 3c39018..2edea8c 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -17,6 +17,7 @@ #include "player-input.hpp" #include "plugins.hpp" #include "project.hpp" +#include "scene.hpp" #include "static-ui.hpp" #include "thread-pool.hpp" #include "window.hpp" @@ -162,7 +163,7 @@ bool FindFirstInstanceHandle(void *handle, void *mapping) { InstMapping *inst_mapping = reinterpret_cast<InstMapping *>(mapping); return inst_mapping->origHandle == *reinterpret_cast<InstanceHandle *>(handle); } -void DeserializeCamera(PkeLevel *level, std::istream &stream) { +void DeserializeCamera(pke_scene *scene, std::istream &stream) { PkeCamera cam{}; InstanceHandle instanceHandle = InstanceHandle_MAX; InstanceHandle targetInstanceHandle = InstanceHandle_MAX; @@ -191,7 +192,7 @@ void DeserializeCamera(PkeLevel *level, std::istream &stream) { rCam.view = cam.view; rCam.isPrimary = cam.isPrimary; rCam.phys.targetInstHandle = targetInstanceHandle; - PkeLevel_RegisterCamera(level->levelHandle, rCam.camHandle); + pke_scene_register_camera(scene->scene_handle, rCam.camHandle); if (targetInstanceIndex > -1) { PkeCamera_TargetInstance(rCam.camHandle, ECS_GetInstance(loadFileInstanceMappings[targetInstanceIndex].newInstHandle)); } @@ -476,21 +477,27 @@ void Game_SaveSceneFile(const char *sceneFilePath) { } } -void Game_LoadSceneFile(PkeLevel *level, const char *sceneFilePath) { +void Game_LoadSceneFile(const char *sceneFilePath) { std::ifstream f(sceneFilePath); if (!f.is_open()) { - fprintf(stderr, "Failed to load requested scene file: %s\n", sceneFilePath); + fprintf(stderr, "Failed to load requested scene file: '%s'\n", sceneFilePath); return; } + // TODO scene name is in the file? + pke_scene *scn = pke_scene_get_by_name(sceneFilePath); + if (scn == nullptr) { + scn = pke_scene_create(sceneFilePath); + } + memset(readLine, '\0', readLineLength); while (f.getline(readLine, readLineLength)) { if (strcmp(PKE_FILE_OBJ_INSTANCE, readLine) == 0) { - DeserializeInstance(level, f); + DeserializeInstance(scn, f); continue; } if (strcmp(PKE_FILE_OBJ_CAMERA, readLine) == 0) { - DeserializeCamera(level, f); + DeserializeCamera(scn, f); continue; } } @@ -539,17 +546,18 @@ void Game_RecordImGui() { void Game_Tick(double delta) { pk_bucket_reset(pkeSettings.mem.bkt); - // TODO this should be removed in favor of storing the scene details inside a level definition - if (pkeSettings.rt.shouldLoadScene && pkeSettings.rt.sceneName) { - pkeSettings.rt.shouldLoadScene = false; - if (pkeSettings.rt.activeLevel != LevelHandle_MAX) { - pkeSettings.rt.previousLevel = pkeSettings.rt.activeLevel; - } - pkeSettings.rt.nextLevel = PkeLevel_Create(pkeSettings.rt.sceneName); - } if (pkeSettings.rt.nextLevel != LevelHandle_MAX) { // TODO async this - Game_LoadSceneFile(PkeLevel_Get(pkeSettings.rt.nextLevel), pkeSettings.rt.sceneName); + PkeLevel *lvl = PkeLevel_Get(pkeSettings.rt.activeLevel); + for (uint32_t i = 0; i < lvl->scene_instances.next; ++i) { + // TODO can't instantiate a scene that hasn't been loaded yet + /* + struct pke_scene *scene = pke_scene_get_by_handle(lvl->scene_instances[i].scene_handle); + if (scene != nullptr) { + Game_LoadSceneFile(scene->name); + } + */ + } pkeSettings.rt.activeLevel = pkeSettings.rt.nextLevel; pkeSettings.rt.nextLevel = LevelHandle_MAX; } @@ -593,6 +601,7 @@ void Game_Main(PKEWindowProperties windowProps, const char *executablePath) { Physics_Init(); PkeCamera_Init(); PkeLevel_Init(); + pke_scene_master_init(); Game_Init(); CreateWindow(windowProps); EntityType_Init(); @@ -610,6 +619,20 @@ void Game_Main(PKEWindowProperties windowProps, const char *executablePath) { } } + // + // at this point, everything is loaded or initialized. + // + + // if we were passed only a scene name, create a faux level. + if (!pkeSettings.args.levelName && pkeSettings.args.sceneName) { + pkeSettings.rt.nextLevel = PkeLevel_Create("faux-level")->levelHandle; + PkeLevel *lvl = PkeLevel_Get(pkeSettings.rt.nextLevel); + scene_instance si{}; + si.scene_handle = pke_scene_get_by_name(pkeSettings.args.sceneName)->scene_handle; + pk_arr_append_t(&lvl->scene_instances, si); + } + + // TODO remove me: temp stuff for testing pk_cstr test_text = cstring_to_pk_cstr("012\n3456789\tThe quick\r\nbrown fox jumped over the lazy dog."); // pk_cstr test_text = cstring_to_pk_cstr("%+-*0123456789$"); // pk_cstr test_text = cstring_to_pk_cstr("$#"); @@ -634,6 +657,7 @@ void Game_Main(PKEWindowProperties windowProps, const char *executablePath) { FontType_AddStringRender(FontTypeIndex{1}, pk_cstr_to_pk_str(&test_text2), &fr_set); } + // TODO remove me: temp stuff for testing pke_ui_box *ui_box = pke_ui_box_new_root(); ui_box->flags = PKE_UI_BOX_FLAG_POSITION_TYPE_DYNAMIC; ui_box->pos_top_left_x = 0.1; @@ -641,6 +665,7 @@ void Game_Main(PKEWindowProperties windowProps, const char *executablePath) { ui_box->max_width = 0.8; ui_box->max_height = 0.8; + // TODO remove me: temp stuff for testing pke_ui_box *c_ui_box = pke_ui_box_new_child(ui_box); c_ui_box->flags = PKE_UI_BOX_FLAG_POSITION_TYPE_STATIC; c_ui_box->pos_top_left_x = 20; @@ -747,6 +772,8 @@ void Game_Main(PKEWindowProperties windowProps, const char *executablePath) { FontType_Teardown(); pke_ui_teardown(); PkeInput_Teardown(); + pke_scene_master_teardown(); + PkeLevel_Teardown(); PkeCamera_Teardown(); Physics_Teardown(); ECS_Teardown(); |
