Skip to content

Commit

Permalink
Lazily create gameplay cameras
Browse files Browse the repository at this point in the history
  • Loading branch information
ajweeks committed Aug 5, 2024
1 parent c84dcfb commit 259f820
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 26 deletions.
25 changes: 23 additions & 2 deletions FlexEngine/include/Cameras/CameraManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -33,6 +33,8 @@ namespace flex
BaseCamera* PushCameraByName(const std::string& name, bool bAlignWithPrevious, bool bInitialize);
void PopCamera(bool bAlignWithCurrent = false);

template<typename T>
T* GetOrCreateCameraByName(const std::string& name);
BaseCamera* GetCameraByName(const std::string& name);

void DrawImGuiObjects();
Expand All @@ -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<BaseCamera*> m_CameraStack;
std::stack<u32> m_CameraStack;
// All cameras, unordered
std::vector<BaseCamera*> m_Cameras;

bool m_bInitialized = false;

};

template<typename T>
T* CameraManager::GetOrCreateCameraByName(const std::string& name)
{
BaseCamera* camera = GetCameraByName(name);

if (camera != nullptr)
{
return static_cast<T*>(camera);
}
else
{
T* newCamera = new T();
return static_cast<T*>(g_CameraManager->AddCamera(newCamera, false));
}
}

} // namespace flex
38 changes: 33 additions & 5 deletions FlexEngine/src/Cameras/CameraManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -76,7 +76,11 @@ namespace flex
{
SetCamera(camera, false);
}

return camera;
}

return nullptr;
}

BaseCamera* CameraManager::SetCamera(BaseCamera* camera, bool bAlignWithPrevious)
Expand All @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -269,6 +274,22 @@ namespace flex
}
}

if (ImGui::TreeNode("Camera Stack"))
{
std::stack<u32> 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", &currentCamera->moveSpeed, 1.0f, 750.0f);

real turnSpeed = glm::degrees(currentCamera->mouseRotationSpeed);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 1 addition & 5 deletions FlexEngine/src/Cameras/FirstPersonCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ namespace flex
{
if (!m_bInitialized)
{
if (m_Player == nullptr)
{
FindPlayer();
}

FindPlayer();
Update();

BaseCamera::Initialize();
Expand Down
5 changes: 1 addition & 4 deletions FlexEngine/src/Cameras/OverheadCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ namespace flex
{
if (!m_bInitialized)
{
if (m_Player0 == nullptr)
{
FindPlayer();
}
FindPlayer();

m_PlayerPosRollingAvg = RollingAverage<glm::vec3>(15, SamplingType::LINEAR);
m_PlayerForwardRollingAvg = RollingAverage<glm::vec3>(30, SamplingType::LINEAR);
Expand Down
1 change: 1 addition & 0 deletions FlexEngine/src/Cameras/VehicleCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 0 additions & 8 deletions FlexEngine/src/FlexEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions FlexEngine/src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ namespace flex
}
else
{
terminalCam = static_cast<TerminalCamera*>(g_CameraManager->GetCameraByName("terminal"));
terminalCam = g_CameraManager->GetOrCreateCameraByName<TerminalCamera>("terminal");
g_CameraManager->AlignCameras(currCam, terminalCam);
terminalCam->SetTerminal(terminal);
g_CameraManager->PushCamera(terminalCam, true, true);
Expand Down Expand Up @@ -1315,7 +1315,7 @@ namespace flex
// Switch to vehicle cam
if (cam->type != CameraType::VEHICLE)
{
vehicleCamera = static_cast<VehicleCamera*>(g_CameraManager->GetCameraByName("vehicle"));
vehicleCamera = g_CameraManager->GetOrCreateCameraByName<VehicleCamera>("vehicle");
g_CameraManager->PushCamera(vehicleCamera, false, true);
}

Expand Down

0 comments on commit 259f820

Please sign in to comment.