summaryrefslogtreecommitdiff
path: root/src/game.cpp
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2023-12-06 22:38:14 -0500
committerJonathan Bradley <jcb@pikum.xyz>2023-12-06 22:38:14 -0500
commite2e1d4646fa3a196b6247ba2dc04603d393df62f (patch)
tree74eb0b281f673697da5fdedf11ba49923a79747d /src/game.cpp
parenta7e6acea6f3d75ba162ac0bcedcf2900568b8ea6 (diff)
large camera refactor for saving and ui
Diffstat (limited to 'src/game.cpp')
-rw-r--r--src/game.cpp135
1 files changed, 133 insertions, 2 deletions
diff --git a/src/game.cpp b/src/game.cpp
index 6fab5f4..6c47381 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -34,6 +34,7 @@ const char *PKE_FILE_VERSION = ":0:";
const char *PKE_FILE_OBJ_END = "";
const char *PKE_FILE_OBJ_ENTITY_TYPE = "EntityType:";
const char *PKE_FILE_OBJ_INSTANCE = "Instance:";
+const char *PKE_FILE_OBJ_CAMERA = "Camera:";
const char *PKE_FILE_ENTITY_TYPE_MODELS_DIR = "ModelsDir: ";
const char *PKE_FILE_ENTITY_TYPE_MODEL_FILE = "ModelFile: ";
@@ -57,6 +58,41 @@ const char *PKE_FILE_INSTANCE_PHYSICS_MASS = "InstPos::Mass: ";
const char *PKE_FILE_INSTANCE_PHYSICS_COLLISION_LAYER = "InstPos::CollisionLayer: ";
const char *PKE_FILE_INSTANCE_PHYSICS_COLLISION_MASK = "InstPos::CollisionMask: ";
+const char *PKE_FILE_CAMERA_POS = "Cam::Pos: ";
+const char *PKE_FILE_CAMERA_ROT = "Cam::Rot: ";
+const char *PKE_FILE_CAMERA_TARGET = "Cam::Target: ";
+const char *PKE_FILE_CAMERA_TYPE = "Cam::Type: ";
+const char *PKE_FILE_CAMERA_ORIENTATION = "Cam::Orientation: ";
+
+void SerializeCamera(std::ofstream &stream, const PkeCamera &cam) {
+ PkeCamera c{};
+ if (cam.pos != c.pos) {
+ stream << PKE_FILE_CAMERA_POS << "["
+ << std::setw(10) << cam.pos[0] << ","
+ << std::setw(10) << cam.pos[1] << ","
+ << std::setw(10) << cam.pos[2] << "]" << std::endl;
+ }
+ if (cam.rot != c.rot) {
+ stream << PKE_FILE_CAMERA_ROT << "["
+ << std::setw(10) << cam.rot[0] << ","
+ << std::setw(10) << cam.rot[1] << ","
+ << std::setw(10) << cam.rot[2] << ","
+ << std::setw(10) << cam.rot[3] << "]" << std::endl;
+ }
+ if (cam.target != c.target) {
+ stream << PKE_FILE_CAMERA_TARGET << "["
+ << std::setw(10) << cam.target[0] << ","
+ << std::setw(10) << cam.target[1] << ","
+ << std::setw(10) << cam.target[2] << "]" << std::endl;
+ }
+ if (cam.type != c.type) {
+ stream << PKE_FILE_CAMERA_TYPE << int(static_cast<PkeCameraType_T>(cam.type)) << std::endl;
+ }
+ if (cam.orientation != c.orientation) {
+ stream << PKE_FILE_CAMERA_ORIENTATION << int(static_cast<PkeCameraOrientation_T>(cam.orientation)) << std::endl;
+ }
+}
+
void SerializeEntityType(std::ofstream &stream, const EntityType &et) {
char handleStr[19] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' };
snprintf(handleStr, 19, "0x%016lX",static_cast<EntityHandle_T>(et.entityHandle));
@@ -143,6 +179,79 @@ void SerializeInstance(std::ofstream &stream, const CompInstance &comp) {
}
}
+void ParseCamera(std::ifstream &stream) {
+ PkeCamera cam{};
+ while (stream.getline(readLine, readLineLength)) {
+ if (strcmp(readLine, PKE_FILE_OBJ_END) == 0) {
+ auto &rCam = PkeCamera_Register();
+ rCam.pos = cam.pos;
+ rCam.rot = cam.rot;
+ rCam.target = cam.target;
+ rCam.type = cam.type;
+ rCam.orientation = cam.orientation;
+ return;
+ }
+ if (strncmp(readLine, PKE_FILE_CAMERA_POS, strlen(PKE_FILE_CAMERA_POS)) == 0) {
+ uint64_t prefixLen = strlen(PKE_FILE_CAMERA_POS);
+ char *startingChar = strchr(readLine + prefixLen, '[') + 1;
+ assert(startingChar != nullptr);
+ char *pEnd = nullptr;
+ long index = 0;
+ do {
+ assert(index < 3);
+ STR2NUM_ERROR result = str2num(cam.pos[index], startingChar, pEnd);
+ assert(result == STR2NUM_ERROR::SUCCESS);
+ startingChar = pEnd + 1;
+ ++index;
+ } while (*pEnd != ']');
+ }
+ if (strncmp(readLine, PKE_FILE_CAMERA_ROT, strlen(PKE_FILE_CAMERA_ROT)) == 0) {
+ uint64_t prefixLen = strlen(PKE_FILE_CAMERA_ROT);
+ char *startingChar = strchr(readLine + prefixLen, '[') + 1;
+ assert(startingChar != nullptr);
+ char *pEnd = nullptr;
+ long index = 0;
+ do {
+ assert(index < 4);
+ STR2NUM_ERROR result = str2num(cam.rot[index], startingChar, pEnd);
+ assert(result == STR2NUM_ERROR::SUCCESS);
+ startingChar = pEnd + 1;
+ ++index;
+ } while (*pEnd != ']');
+ }
+ if (strncmp(readLine, PKE_FILE_CAMERA_TARGET, strlen(PKE_FILE_CAMERA_TARGET)) == 0) {
+ uint64_t prefixLen = strlen(PKE_FILE_CAMERA_TARGET);
+ char *startingChar = strchr(readLine + prefixLen, '[') + 1;
+ assert(startingChar != nullptr);
+ char *pEnd = nullptr;
+ long index = 0;
+ do {
+ assert(index < 3);
+ STR2NUM_ERROR result = str2num(cam.target[index], startingChar, pEnd);
+ assert(result == STR2NUM_ERROR::SUCCESS);
+ startingChar = pEnd + 1;
+ ++index;
+ } while (*pEnd != ']');
+ }
+ if (strncmp(readLine, PKE_FILE_CAMERA_TYPE, strlen(PKE_FILE_CAMERA_TYPE)) == 0) {
+ uint64_t prefixLen = strlen(PKE_FILE_CAMERA_TYPE);
+ PkeCameraType_T handle_t;
+ STR2NUM_ERROR result = str2num(handle_t, readLine + prefixLen);
+ assert(result == STR2NUM_ERROR::SUCCESS);
+ cam.type = PkeCameraType{handle_t};
+ continue;
+ }
+ if (strncmp(readLine, PKE_FILE_CAMERA_ORIENTATION, strlen(PKE_FILE_CAMERA_ORIENTATION)) == 0) {
+ uint64_t prefixLen = strlen(PKE_FILE_CAMERA_ORIENTATION);
+ PkeCameraOrientation_T handle_t;
+ STR2NUM_ERROR result = str2num(handle_t, readLine + prefixLen);
+ assert(result == STR2NUM_ERROR::SUCCESS);
+ cam.orientation = PkeCameraOrientation{handle_t};
+ continue;
+ }
+ }
+}
+
void ParseEntityType(std::ifstream &stream) {
EntityType et{};
while (stream.getline(readLine, readLineLength)) {
@@ -384,6 +493,20 @@ void Game_SaveSceneFile(const char *sceneFilePath) {
f << PKE_FILE_VERSION << std::endl;
f << "" << std::endl;
+ int64_t cameraBucketCount = PkeCamera_GetBucketCount();
+ for (long b = 0; b < cameraBucketCount; ++b) {
+ int64_t count;
+ auto *cameras = PkeCamera_GetCameras(b, count);
+ for (long i = 0; i < count; ++i) {
+ const auto &cam = cameras[i];
+ if (cam.handle == CameraHandle_MAX)
+ continue;
+ f << PKE_FILE_OBJ_CAMERA << std::endl;
+ SerializeCamera(f, cam);
+ f << PKE_FILE_OBJ_END << std::endl;
+ }
+ }
+
for (long i = 0; i < GlobalEntityTypes.Count(); ++i) {
f << PKE_FILE_OBJ_ENTITY_TYPE << std::endl;
const auto &et = GlobalEntityTypes[i];
@@ -418,11 +541,17 @@ void Game_SaveSceneFile(const char *sceneFilePath) {
void Game_LoadSceneFile(const char *sceneFilePath) {
std::ifstream f(sceneFilePath);
- assert(f.is_open());
+ if (!f.is_open()) {
+ fprintf(stderr, "Failed to load requested scene file: %s", sceneFilePath);
+ return;
+ }
memset(readLine, '\0', readLineLength);
while (f.getline(readLine, readLineLength)) {
- // EntityTypes
+ if (strcmp(PKE_FILE_OBJ_CAMERA, readLine) == 0) {
+ ParseCamera(f);
+ continue;
+ }
if (strcmp(PKE_FILE_OBJ_ENTITY_TYPE, readLine) == 0) {
ParseEntityType(f);
continue;
@@ -498,6 +627,7 @@ void Game_Main(PKEWindowProperties windowProps, const char *executablePath) {
fprintf(stdout, "Game_Main Entering\n");
try {
AM_Init();
+ PkeCamera_Init();
Physics_Init();
Game_Init();
ECS_Init();
@@ -604,6 +734,7 @@ void Game_Main(PKEWindowProperties windowProps, const char *executablePath) {
PkeInput_Teardown();
Physics_Teardown();
ECS_Teardown();
+ PkeCamera_Teardown();
AM_DebugPrint();
AM_Teardown();
DestroyWindow();