summaryrefslogtreecommitdiff
path: root/src/game.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.cpp')
-rw-r--r--src/game.cpp258
1 files changed, 254 insertions, 4 deletions
diff --git a/src/game.cpp b/src/game.cpp
index 69e728d..e921b2f 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -1,6 +1,7 @@
#include "game.hpp"
+#include "camera.hpp"
#include "components.hpp"
#include "dynamic-array.hpp"
#include "entities.hpp"
@@ -9,7 +10,9 @@
#include "imgui.h"
#include "player-input.hpp"
#include "vendor/glm_include.hpp"
+#include "window.hpp"
+#include <GLFW/glfw3.h>
#include <cstring>
#include <iomanip>
@@ -20,6 +23,35 @@ char readLine[readLineLength];
const char *levelName = "demo-level";
+PkeCamera cameraDefault {
+ .pos = glm::vec3(-40.f, -40.f, -40.f),
+ .rot = glm::quat(1.f, 0.f, 0.f, 0.f),
+ .target = glm::vec3(0.f),
+ .type = PKE_CAMERA_TYPE_PERSPECTIVE,
+ .orientation = PKE_CAMERA_ORIENTATION_TARGET,
+ .stale = PKE_CAMERA_STALE_ALL,
+};
+PkeCamera cameraDbg {
+ .pos = glm::vec3(4.f, 4.f, 4.f),
+ .rot = glm::quat(1.f, 0.f, 0.f, 0.f),
+ .target = glm::vec3(0.f),
+ .type = PKE_CAMERA_TYPE_PERSPECTIVE,
+ .orientation = PKE_CAMERA_ORIENTATION_FREE,
+ .stale = PKE_CAMERA_STALE_ALL,
+};
+
+const char *dbgCtrl_CameraLeft = "debug-camera-left";
+const char *dbgCtrl_CameraRight = "debug-camera-right";
+const char *dbgCtrl_CameraForward = "debug-camera-forward";
+const char *dbgCtrl_CameraBack = "debug-camera-back";
+const char *dbgCtrl_CameraUp = "debug-camera-up";
+const char *dbgCtrl_CameraDown = "debug-camera-down";
+const char *dbgCtrl_CameraRotCC = "debug-camera-rot-counter-clockwise";
+const char *dbgCtrl_CameraRotC = "debug-camera-rot-clockwise";
+const char *dbgCtrl_CameraRot = "debug-camera-rot";
+const char *dbgCtrl_UnlockCamera = "debug-camera-unlock";
+InputActionSetHandle debugControlsHandle = InputActionSetHandle_MAX;
+
struct EntityTypeInstanceCreateInfo {
EntityHandle entityTypeEntityHandle;
};
@@ -54,6 +86,11 @@ EntityHandle selectedEntity = EntityHandle_MAX;
bool shouldCreateEntityType = false;
EntityType *entityTypeToCreate = nullptr;
+bool shouldInitEditor = true;
+bool shouldLockCamera = false;
+bool shouldUnlockCamera = false;
+bool shouldTeardownEditor = false;
+
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));
@@ -335,11 +372,36 @@ void LoadSceneFile(const char *sceneFilePath) {
f.close();
}
+void setupEditor() {
+ shouldInitEditor = false;
+ PkeInput_ActivateSet(debugControlsHandle);
+}
+
+void teardownEditor() {
+ shouldTeardownEditor = false;
+ PkeInput_DeactivateSet(debugControlsHandle);
+}
+
void Game_Tick(double delta) {
/*
* ECS_Tick() gets called first because it updates the public
* `EntitiesToBeRemoved` for all other ticks to use.
*/
+ if (shouldInitEditor) {
+ setupEditor();
+ }
+ if (shouldUnlockCamera) {
+ shouldUnlockCamera = false;
+ glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
+ cameraDbg.pos = ActiveCamera->pos;
+ if (ActiveCamera->orientation == cameraDbg.orientation) {
+ cameraDbg.rot = ActiveCamera->rot;
+ } else {
+ cameraDbg.rot = glm::quat(UBO.view);
+ }
+ cameraDbg.stale = PKE_CAMERA_STALE_ALL;
+ ActiveCamera = &cameraDbg;
+ }
ECS_Tick_Early(delta);
if (shouldCreateEntityType) {
assert(entityTypeToCreate != nullptr);
@@ -361,12 +423,133 @@ void Game_Tick(double delta) {
ECS_CreateInstance(newEntity, createInfo.entityTypeEntityHandle);
}
PkeInput_Tick(delta);
- static double accDelta = 0.0;
- accDelta += delta;
- UBO.model = glm::rotate(glm::mat4(1.0f), (float)accDelta * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
- UBO.view = glm::lookAt(glm::vec3(4.0f, 4.0f, 4.0f), glm::vec3(0), glm::vec3(0.0f, 0.0f, 1.0f));
+ PkeInputEventHolder holder = PkeInput_Query(dbgCtrl_UnlockCamera);
+ if (holder.type != InputEventHash{0}) {
+ PkeKeyEvent *keyEsc;
+ keyEsc = static_cast<PkeKeyEvent *>(holder.ptr);
+ if (keyEsc->isPressed || keyEsc->pressCount > 0) {
+ pkeSettings.editorSettings.isUsingDebugCamera = false;
+ shouldLockCamera = true;
+ }
+ }
+
+ if (shouldLockCamera) {
+ shouldLockCamera = false;
+ glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
+ cameraDefault.stale = PKE_CAMERA_STALE_ALL;
+ ActiveCamera = &cameraDefault;
+ }
+
+ if (pkeSettings.editorSettings.isUsingDebugCamera) {
+ holder = PkeInput_Query(dbgCtrl_CameraRot);
+ if (holder.type != InputEventHash{0}) {
+ const PkeCursorPosEvent *posEvent = static_cast<PkeCursorPosEvent *>(holder.ptr);
+ if (posEvent->xMotion || posEvent->yMotion) {
+ glm::vec3 axis1Heading = glm::conjugate(cameraDbg.rot) * glm::vec3(1.f, 0.f, 0.f);
+ glm::vec3 axis2Heading = glm::conjugate(cameraDbg.rot) * glm::vec3(0.f, 1.f, 0.f);
+ // glm::vec3 axis1Heading = cameraDbg.rot * glm::vec3(1.f, 0.f, 0.f);
+ // glm::vec3 axis2Heading = cameraDbg.rot * glm::vec3(0.f, 1.f, 0.f);
+ glm::quat pitch = glm::angleAxis(float(posEvent->yMotion) * 0.01f, glm::normalize(axis1Heading));
+ glm::quat yaw = glm::angleAxis(float(posEvent->xMotion) * 0.01f, glm::normalize(axis2Heading));
+ glm::quat rot = cameraDbg.rot * pitch * yaw;
+ // rot = glm::normalize(rot);
+ auto eul = glm::eulerAngles(rot);
+ cameraDbg.rot = glm::quat(eul);
+ cameraDbg.stale = cameraDbg.stale | PKE_CAMERA_STALE_ROT;
+ }
+ }
+
+ double leftCount = 0;
+ double rightCount = 0;
+ double forwardCount = 0;
+ double backCount = 0;
+ double upCount = 0;
+ double downCount = 0;
+ double rotCCCount = 0;
+ double rotCCount = 0;
+
+ holder = PkeInput_Query(dbgCtrl_CameraLeft);
+ if (holder.type != InputEventHash{0}) {
+ PkeKeyEvent *keyLeft;
+ keyLeft = static_cast<PkeKeyEvent *>(holder.ptr);
+ leftCount = keyLeft->isPressed ? 1 : 0;
+ }
+
+ holder = PkeInput_Query(dbgCtrl_CameraRight);
+ if (holder.type != InputEventHash{0}) {
+ PkeKeyEvent *keyRight;
+ keyRight = static_cast<PkeKeyEvent *>(holder.ptr);
+ rightCount = keyRight->isPressed ? 1 : 0;
+ }
+
+ holder = PkeInput_Query(dbgCtrl_CameraForward);
+ if (holder.type != InputEventHash{0}) {
+ PkeKeyEvent *keyForward;
+ keyForward = static_cast<PkeKeyEvent *>(holder.ptr);
+ forwardCount = keyForward->isPressed ? 1 : 0;
+ }
+
+ holder = PkeInput_Query(dbgCtrl_CameraBack);
+ if (holder.type != InputEventHash{0}) {
+ PkeKeyEvent *keyBack;
+ keyBack = static_cast<PkeKeyEvent *>(holder.ptr);
+ backCount = keyBack->isPressed ? 1 : 0;
+ }
+
+ holder = PkeInput_Query(dbgCtrl_CameraUp);
+ if (holder.type != InputEventHash{0}) {
+ PkeKeyEvent *keyUp;
+ keyUp = static_cast<PkeKeyEvent *>(holder.ptr);
+ upCount = keyUp->isPressed ? 1 : 0;
+ }
+
+ holder = PkeInput_Query(dbgCtrl_CameraDown);
+ if (holder.type != InputEventHash{0}) {
+ PkeKeyEvent *keyDown;
+ keyDown = static_cast<PkeKeyEvent *>(holder.ptr);
+ downCount = keyDown->isPressed ? 1 : 0;
+ }
+
+ holder = PkeInput_Query(dbgCtrl_CameraRotCC);
+ if (holder.type != InputEventHash{0}) {
+ PkeKeyEvent *keyRotCC;
+ keyRotCC = static_cast<PkeKeyEvent *>(holder.ptr);
+ rotCCCount = keyRotCC->isPressed ? 1 : 0;
+ }
+
+ holder = PkeInput_Query(dbgCtrl_CameraRotC);
+ if (holder.type != InputEventHash{0}) {
+ PkeKeyEvent *keyRotC;
+ keyRotC = static_cast<PkeKeyEvent *>(holder.ptr);
+ rotCCount = keyRotC->isPressed ? 1 : 0;
+ }
+
+ double accelerated = delta * 10.f;
+ double axis1 = -(leftCount * accelerated) + (rightCount * accelerated);
+ double axis2 = (forwardCount * accelerated) + -(backCount * accelerated);
+ double axis3 = -(upCount * accelerated) + (downCount * accelerated);
+ if (axis1 != 0 || axis2 != 0 || axis3 != 0) {
+ glm::vec3 axis1Heading = glm::conjugate(cameraDbg.rot) * glm::vec3(-axis1, 0.f, 0.f);
+ glm::vec3 axis2Heading = glm::conjugate(cameraDbg.rot) * glm::vec3(0.f, 0.f, axis2);
+ glm::vec3 axis3Heading = glm::conjugate(cameraDbg.rot) * glm::vec3(0.f, axis3, 0.f);
+ cameraDbg.pos += glm::vec3(axis1Heading + axis2Heading + axis3Heading);
+ cameraDbg.stale = cameraDbg.stale | PKE_CAMERA_STALE_POS;
+ }
+
+ double axis4 = -(rotCCCount * delta) + (rotCCount * delta);
+ if (axis4 != 0.0) {
+ cameraDbg.rot = glm::quat(glm::vec3(0.f, 0.f, axis4)) * cameraDbg.rot;
+ cameraDbg.stale = cameraDbg.stale | PKE_CAMERA_STALE_ROT;
+ }
+ } else {
+ cameraDefault.target = glm::vec3(0.f, 0.f, 0.f);
+ cameraDefault.stale = cameraDefault.stale | PKE_CAMERA_STALE_ROT;
+ }
EntityType_Tick_Late(delta);
ECS_Tick_Late(delta);
+ if (shouldTeardownEditor) {
+ teardownEditor();
+ }
}
void RecordImGui_GLM(const char *label, glm::mat4 &mat) {
@@ -397,6 +580,13 @@ void RecordImGuiEditorWrapper() {
shouldRecreateSwapchain = true;
}
// ImGui::Checkbox("Uncap Tickrate", &pkeSettings.isTickrateUnlocked);
+ if (ImGui::Checkbox("Use Debug Camera", &pkeSettings.editorSettings.isUsingDebugCamera)) {
+ if (pkeSettings.editorSettings.isUsingDebugCamera) {
+ shouldUnlockCamera = true;
+ } else {
+ shouldLockCamera = true;
+ }
+ }
ImGuiIO &io = ImGui::GetIO();
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
@@ -678,8 +868,68 @@ void Game_Init() {
memset(consoleBuffer[i], '\0', consoleLineLength);
}
Event_RegisterCallback("RenderImGui", RecordImGuiEditor);
+
+ PkeInputSet debugControlsSet;
+ debugControlsSet.title = "debug-editor-controls";
+ debugControlsSet.actionCount = 10;
+ debugControlsSet.actions = Pke_New<PkeInputAction>(debugControlsSet.actionCount);
+
+ debugControlsSet.actions[0].name = dbgCtrl_CameraLeft;
+ debugControlsSet.actions[0].primaryHash = PkeInputEventMask {
+ .computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
+ .button = GLFW_KEY_A,
+ };
+ debugControlsSet.actions[1].name = dbgCtrl_CameraRight;
+ debugControlsSet.actions[1].primaryHash = PkeInputEventMask {
+ .computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
+ .button = GLFW_KEY_D,
+ };
+ debugControlsSet.actions[2].name = dbgCtrl_CameraForward;
+ debugControlsSet.actions[2].primaryHash = PkeInputEventMask {
+ .computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
+ .button = GLFW_KEY_W,
+ };
+ debugControlsSet.actions[3].name = dbgCtrl_CameraBack;
+ debugControlsSet.actions[3].primaryHash = PkeInputEventMask {
+ .computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
+ .button = GLFW_KEY_S,
+ };
+ debugControlsSet.actions[4].name = dbgCtrl_CameraUp;
+ debugControlsSet.actions[4].primaryHash = PkeInputEventMask {
+ .computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
+ .button = GLFW_KEY_R,
+ };
+ debugControlsSet.actions[5].name = dbgCtrl_CameraDown;
+ debugControlsSet.actions[5].primaryHash = PkeInputEventMask {
+ .computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
+ .button = GLFW_KEY_F,
+ };
+ debugControlsSet.actions[6].name = dbgCtrl_CameraRotCC;
+ debugControlsSet.actions[6].primaryHash = PkeInputEventMask {
+ .computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
+ .button = GLFW_KEY_Q,
+ };
+ debugControlsSet.actions[7].name = dbgCtrl_CameraRotC;
+ debugControlsSet.actions[7].primaryHash = PkeInputEventMask {
+ .computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
+ .button = GLFW_KEY_E,
+ };
+ debugControlsSet.actions[8].name = dbgCtrl_CameraRot;
+ debugControlsSet.actions[8].primaryHash = PkeInputEventMask {
+ .computedHash = PKE_INPUT_HASH_ALL_CURSOR_POS_EVENTS,
+ };
+ debugControlsSet.actions[9].name = dbgCtrl_UnlockCamera;
+ debugControlsSet.actions[9].primaryHash = PkeInputEventMask {
+ .computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
+ .button = GLFW_KEY_ESCAPE,
+ };
+
+ debugControlsHandle = PkeInput_RegisterSet(debugControlsSet);
+
+ ActiveCamera = &cameraDefault;
}
void Game_Teardown() {
+ PkeInput_UnregisterSet(debugControlsHandle);
entityInstancesToCreate.~DynArray();
}