diff --git a/FlexEngine/include/Cameras/CameraManager.hpp b/FlexEngine/include/Cameras/CameraManager.hpp index a37ab492..d6fbecff 100644 --- a/FlexEngine/include/Cameras/CameraManager.hpp +++ b/FlexEngine/include/Cameras/CameraManager.hpp @@ -22,7 +22,7 @@ namespace flex BaseCamera* CurrentCamera() const; - void AddCamera(BaseCamera* camera, bool bSwitchTo = false); + BaseCamera* AddCamera(BaseCamera* camera, bool bSwitchTo = false); // Clears stack and pushes the given camera onto it, then returns a pointer to it BaseCamera* SetCamera(BaseCamera* camera, bool bAlignWithPrevious); @@ -33,6 +33,8 @@ namespace flex BaseCamera* PushCameraByName(const std::string& name, bool bAlignWithPrevious, bool bInitialize); void PopCamera(bool bAlignWithCurrent = false); + template + T* GetOrCreateCameraByName(const std::string& name); BaseCamera* GetCameraByName(const std::string& name); void DrawImGuiObjects(); @@ -46,14 +48,33 @@ namespace flex i32 GetCameraIndex(BaseCamera* camera); + BaseCamera* GetCamera(u32 index) const; + // TODO: Roll custom stack class // Stack containing temporary cameras, the topmost of which is the current camera // Always contains at least one element - std::stack m_CameraStack; + std::stack m_CameraStack; // All cameras, unordered std::vector m_Cameras; bool m_bInitialized = false; }; + + template + T* CameraManager::GetOrCreateCameraByName(const std::string& name) + { + BaseCamera* camera = GetCameraByName(name); + + if (camera != nullptr) + { + return static_cast(camera); + } + else + { + T* newCamera = new T(); + return static_cast(g_CameraManager->AddCamera(newCamera, false)); + } + } + } // namespace flex diff --git a/FlexEngine/src/Cameras/CameraManager.cpp b/FlexEngine/src/Cameras/CameraManager.cpp index 9884451b..8293eec0 100644 --- a/FlexEngine/src/Cameras/CameraManager.cpp +++ b/FlexEngine/src/Cameras/CameraManager.cpp @@ -60,10 +60,10 @@ namespace flex BaseCamera* CameraManager::CurrentCamera() const { - return m_CameraStack.top(); + return GetCamera(m_CameraStack.top()); } - void CameraManager::AddCamera(BaseCamera* camera, bool bSwitchTo) + BaseCamera* CameraManager::AddCamera(BaseCamera* camera, bool bSwitchTo) { CHECK_NE(camera, nullptr); @@ -76,7 +76,11 @@ namespace flex { SetCamera(camera, false); } + + return camera; } + + return nullptr; } BaseCamera* CameraManager::SetCamera(BaseCamera* camera, bool bAlignWithPrevious) @@ -102,7 +106,7 @@ namespace flex const i32 numCameras = (i32)m_Cameras.size(); - const i32 desiredIndex = GetCameraIndex(m_CameraStack.top()) + deltaIndex; + const i32 desiredIndex = GetCameraIndex(GetCamera(m_CameraStack.top())) + deltaIndex; i32 newIndex; i32 offset = 0; do @@ -147,13 +151,14 @@ namespace flex BaseCamera* pActiveCam = nullptr; if (!m_CameraStack.empty()) { - pActiveCam = m_CameraStack.top(); + pActiveCam = GetCamera(m_CameraStack.top()); pActiveCam->OnDepossess(); pActiveCam->Destroy(); } - m_CameraStack.push(camera); + i32 cameraIndex = GetCameraIndex(camera); + m_CameraStack.push((u32)cameraIndex); if (bAlignWithPrevious && pActiveCam != nullptr) { @@ -269,6 +274,22 @@ namespace flex } } + if (ImGui::TreeNode("Camera Stack")) + { + std::stack stackCpy = m_CameraStack; // Very gross, please replace container type + while (!stackCpy.empty()) + { + BaseCamera* camera = GetCamera(stackCpy.top()); + stackCpy.pop(); + + ImGui::PushID(camera); + ImGui::Text("%s", camera->GetName().c_str()); + ImGui::PopID(); + } + + ImGui::TreePop(); + } + ImGui::SliderFloat("Move speed", ¤tCamera->moveSpeed, 1.0f, 750.0f); real turnSpeed = glm::degrees(currentCamera->mouseRotationSpeed); @@ -328,6 +349,13 @@ namespace flex return -1; } + BaseCamera* CameraManager::GetCamera(u32 index) const + { + CHECK_LT(index, (u32)m_Cameras.size()); + + return m_Cameras[index]; + } + void CameraManager::AlignCameras(BaseCamera* from, BaseCamera* to) { to->position = from->position; diff --git a/FlexEngine/src/Cameras/FirstPersonCamera.cpp b/FlexEngine/src/Cameras/FirstPersonCamera.cpp index fc864419..f5458cc3 100644 --- a/FlexEngine/src/Cameras/FirstPersonCamera.cpp +++ b/FlexEngine/src/Cameras/FirstPersonCamera.cpp @@ -33,11 +33,7 @@ namespace flex { if (!m_bInitialized) { - if (m_Player == nullptr) - { - FindPlayer(); - } - + FindPlayer(); Update(); BaseCamera::Initialize(); diff --git a/FlexEngine/src/Cameras/OverheadCamera.cpp b/FlexEngine/src/Cameras/OverheadCamera.cpp index f706166f..07d2ed08 100644 --- a/FlexEngine/src/Cameras/OverheadCamera.cpp +++ b/FlexEngine/src/Cameras/OverheadCamera.cpp @@ -35,10 +35,7 @@ namespace flex { if (!m_bInitialized) { - if (m_Player0 == nullptr) - { - FindPlayer(); - } + FindPlayer(); m_PlayerPosRollingAvg = RollingAverage(15, SamplingType::LINEAR); m_PlayerForwardRollingAvg = RollingAverage(30, SamplingType::LINEAR); diff --git a/FlexEngine/src/Cameras/VehicleCamera.cpp b/FlexEngine/src/Cameras/VehicleCamera.cpp index 7eb2625e..66c9f6c6 100644 --- a/FlexEngine/src/Cameras/VehicleCamera.cpp +++ b/FlexEngine/src/Cameras/VehicleCamera.cpp @@ -27,6 +27,7 @@ namespace flex m_TargetFollowDist(256), m_LastLookOffset(VEC2_ZERO) { + bDEBUGCyclable = false; bPossessPlayer = true; m_SpeedFactors.overrideMin = 0.0f; m_SpeedFactors.overrideMax = 1.0f; diff --git a/FlexEngine/src/FlexEngine.cpp b/FlexEngine/src/FlexEngine.cpp index d73d895a..666a5b73 100644 --- a/FlexEngine/src/FlexEngine.cpp +++ b/FlexEngine/src/FlexEngine.cpp @@ -27,8 +27,6 @@ IGNORE_WARNINGS_POP #include "Cameras/DebugCamera.hpp" #include "Cameras/FirstPersonCamera.hpp" #include "Cameras/OverheadCamera.hpp" -#include "Cameras/TerminalCamera.hpp" -#include "Cameras/VehicleCamera.hpp" #include "ConfigFileManager.hpp" #include "Editor.hpp" #include "Graphics/Renderer.hpp" @@ -2279,12 +2277,6 @@ namespace flex OverheadCamera* overheadCamera = new OverheadCamera(); g_CameraManager->AddCamera(overheadCamera, false); - - TerminalCamera* terminalCamera = new TerminalCamera(); - g_CameraManager->AddCamera(terminalCamera, false); - - VehicleCamera* vehicleCamera = new VehicleCamera(); - g_CameraManager->AddCamera(vehicleCamera, false); } #if COMPILE_RENDERDOC_API diff --git a/FlexEngine/src/Player.cpp b/FlexEngine/src/Player.cpp index 58a8d8de..6abc7487 100644 --- a/FlexEngine/src/Player.cpp +++ b/FlexEngine/src/Player.cpp @@ -1275,7 +1275,7 @@ namespace flex } else { - terminalCam = static_cast(g_CameraManager->GetCameraByName("terminal")); + terminalCam = g_CameraManager->GetOrCreateCameraByName("terminal"); g_CameraManager->AlignCameras(currCam, terminalCam); terminalCam->SetTerminal(terminal); g_CameraManager->PushCamera(terminalCam, true, true); @@ -1315,7 +1315,7 @@ namespace flex // Switch to vehicle cam if (cam->type != CameraType::VEHICLE) { - vehicleCamera = static_cast(g_CameraManager->GetCameraByName("vehicle")); + vehicleCamera = g_CameraManager->GetOrCreateCameraByName("vehicle"); g_CameraManager->PushCamera(vehicleCamera, false, true); }