1
0
forked from 0ad/0ad

Splits CameraController and adds ICameraController interface

Comments By: Stan
Differential Revision: https://code.wildfiregames.com/D2478
This was SVN commit r23272.
This commit is contained in:
Vladislav Belov 2019-12-21 00:44:18 +00:00
parent 091f3167e0
commit e269466f51
5 changed files with 136 additions and 40 deletions

View File

@ -48,7 +48,7 @@ extern int g_xres, g_yres;
static const float CAMERA_EDGE_MARGIN = 2.0f * TERRAIN_TILE_SIZE;
CCameraController::CCameraController(CCamera& camera)
: m_Camera(camera),
: ICameraController(camera),
m_ConstrainCamera(true),
m_FollowEntity(INVALID_ENTITY),
m_FollowFirstPerson(false),
@ -102,6 +102,8 @@ CCameraController::CCameraController(CCamera& camera)
m_Camera.UpdateFrustum();
}
CCameraController::~CCameraController() = default;
void CCameraController::LoadConfig()
{
CFG_GET_VAL("view.scroll.speed", m_ViewScrollSpeed);

View File

@ -18,42 +18,40 @@
#ifndef INCLUDED_CAMERACONTROLLER
#define INCLUDED_CAMERACONTROLLER
#include "graphics/Camera.h"
#include "graphics/ICameraController.h"
#include "graphics/SmoothedValue.h"
#include "simulation2/system/Entity.h"
#include "lib/input.h" // InReaction - can't forward-declare enum
class CCameraController
class CCameraController : public ICameraController
{
NONCOPYABLE(CCameraController);
public:
CCameraController(CCamera& camera);
~CCameraController() override;
void LoadConfig();
void LoadConfig() override;
InReaction HandleEvent(const SDL_Event_* ev);
InReaction HandleEvent(const SDL_Event_* ev) override;
CVector3D GetCameraPivot() const;
CVector3D GetCameraPosition() const;
CVector3D GetCameraRotation() const;
float GetCameraZoom() const;
CVector3D GetCameraPivot() const override;
CVector3D GetCameraPosition() const override;
CVector3D GetCameraRotation() const override;
float GetCameraZoom() const override;
void SetCamera(const CVector3D& pos, float rotX, float rotY, float zoom);
void MoveCameraTarget(const CVector3D& target);
void ResetCameraTarget(const CVector3D& target);
void FollowEntity(entity_id_t entity, bool firstPerson);
entity_id_t GetFollowedEntity();
void SetCamera(const CVector3D& pos, float rotX, float rotY, float zoom) override;
void MoveCameraTarget(const CVector3D& target) override;
void ResetCameraTarget(const CVector3D& target) override;
void FollowEntity(entity_id_t entity, bool firstPerson) override;
entity_id_t GetFollowedEntity() override;
void Update(const float deltaRealTime);
void SetViewport(const SViewPort& vp);
void Update(const float deltaRealTime) override;
void SetViewport(const SViewPort& vp) override;
bool GetConstrainCamera() const
bool GetConstrainCamera() const override
{
return m_ConstrainCamera;
}
void SetConstrainCamera(bool constrain)
void SetConstrainCamera(bool constrain) override
{
m_ConstrainCamera = constrain;
}
@ -71,8 +69,6 @@ private:
*/
void SetCameraProjection();
CCamera& m_Camera;
/**
* Whether the camera movement should be constrained by min/max limits
* and terrain avoidance.

View File

@ -61,6 +61,8 @@
#include "simulation2/components/ICmpPosition.h"
#include "simulation2/components/ICmpRangeManager.h"
#include <memory>
class CGameViewImpl
{
NONCOPYABLE(CGameViewImpl);
@ -75,7 +77,7 @@ public:
CullCamera(),
LockCullCamera(false),
Culling(true),
CameraController(ViewCamera)
CameraController(new CCameraController(ViewCamera))
{
}
@ -127,7 +129,11 @@ public:
CCinemaManager CinemaManager;
CCameraController CameraController;
/**
* Controller of the view's camera. We use a std::unique_ptr for an easy
* on the fly replacement. It's guaranteed that the pointer is never nulllptr.
*/
std::unique_ptr<ICameraController> CameraController;
};
#define IMPLEMENT_BOOLEAN_SETTING(NAME) \
@ -146,12 +152,12 @@ IMPLEMENT_BOOLEAN_SETTING(LockCullCamera);
bool CGameView::GetConstrainCameraEnabled() const
{
return m->CameraController.GetConstrainCamera();
return m->CameraController->GetConstrainCamera();
}
void CGameView::SetConstrainCameraEnabled(bool enabled)
{
m->CameraController.SetConstrainCamera(enabled);
m->CameraController->SetConstrainCamera(enabled);
}
#undef IMPLEMENT_BOOLEAN_SETTING
@ -172,7 +178,7 @@ CGameView::~CGameView()
void CGameView::SetViewport(const SViewPort& vp)
{
m->CameraController.SetViewport(vp);
m->CameraController->SetViewport(vp);
}
CObjectManager& CGameView::GetObjectManager()
@ -202,7 +208,7 @@ CTerritoryTexture& CGameView::GetTerritoryTexture()
int CGameView::Initialize()
{
m->CameraController.LoadConfig();
m->CameraController->LoadConfig();
return 0;
}
@ -309,52 +315,52 @@ void CGameView::Update(const float deltaRealTime)
if (m->CinemaManager.IsEnabled())
return;
m->CameraController.Update(deltaRealTime);
m->CameraController->Update(deltaRealTime);
}
CVector3D CGameView::GetCameraPivot() const
{
return m->CameraController.GetCameraPivot();
return m->CameraController->GetCameraPivot();
}
CVector3D CGameView::GetCameraPosition() const
{
return m->CameraController.GetCameraPosition();
return m->CameraController->GetCameraPosition();
}
CVector3D CGameView::GetCameraRotation() const
{
return m->CameraController.GetCameraRotation();
return m->CameraController->GetCameraRotation();
}
float CGameView::GetCameraZoom() const
{
return m->CameraController.GetCameraZoom();
return m->CameraController->GetCameraZoom();
}
void CGameView::SetCamera(const CVector3D& pos, float rotX, float rotY, float zoom)
{
m->CameraController.SetCamera(pos, rotX, rotY, zoom);
m->CameraController->SetCamera(pos, rotX, rotY, zoom);
}
void CGameView::MoveCameraTarget(const CVector3D& target)
{
m->CameraController.MoveCameraTarget(target);
m->CameraController->MoveCameraTarget(target);
}
void CGameView::ResetCameraTarget(const CVector3D& target)
{
m->CameraController.ResetCameraTarget(target);
m->CameraController->ResetCameraTarget(target);
}
void CGameView::FollowEntity(entity_id_t entity, bool firstPerson)
{
m->CameraController.FollowEntity(entity, firstPerson);
m->CameraController->FollowEntity(entity, firstPerson);
}
entity_id_t CGameView::GetFollowedEntity()
{
return m->CameraController.GetFollowedEntity();
return m->CameraController->GetFollowedEntity();
}
InReaction game_view_handler(const SDL_Event_* ev)
@ -402,5 +408,5 @@ InReaction CGameView::HandleEvent(const SDL_Event_* ev)
}
}
return m->CameraController.HandleEvent(ev);
return m->CameraController->HandleEvent(ev);
}

View File

@ -0,0 +1,27 @@
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "ICameraController.h"
ICameraController::ICameraController(CCamera& camera)
: m_Camera(camera)
{
}
ICameraController::~ICameraController() = default;

View File

@ -0,0 +1,65 @@
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_ICAMERACONTROLLER
#define INCLUDED_ICAMERACONTROLLER
#include "graphics/Camera.h"
#include "simulation2/system/Entity.h"
#include "lib/input.h" // InReaction - can't forward-declare enum
/**
* @interface ICameraController defines a camera controller interface. The camera object
* is owned by the camera controller's owner. It is therefore guaranteed that the lifetime
* of the camera is at least the same as the lifetime of the camera controller.
* The camera object is stored by reference, ensuring that the camera controller has full
* control of the camera object during its own lifetime.
*/
class ICameraController
{
NONCOPYABLE(ICameraController);
public:
explicit ICameraController(CCamera& camera);
virtual ~ICameraController();
virtual void LoadConfig() = 0;
virtual InReaction HandleEvent(const SDL_Event_* ev) = 0;
virtual CVector3D GetCameraPivot() const = 0;
virtual CVector3D GetCameraPosition() const = 0;
virtual CVector3D GetCameraRotation() const = 0;
virtual float GetCameraZoom() const = 0;
virtual void SetCamera(const CVector3D& pos, float rotX, float rotY, float zoom) = 0;
virtual void MoveCameraTarget(const CVector3D& target) = 0;
virtual void ResetCameraTarget(const CVector3D& target) = 0;
virtual void FollowEntity(entity_id_t entity, bool firstPerson) = 0;
virtual entity_id_t GetFollowedEntity() = 0;
virtual void Update(const float deltaRealTime) = 0;
virtual void SetViewport(const SViewPort& vp) = 0;
virtual bool GetConstrainCamera() const = 0;
virtual void SetConstrainCamera(bool constrain) = 0;
protected:
CCamera& m_Camera;
};
#endif // INCLUDED_ICAMERACONTROLLER