From 3a031128f7bc09d303bf24cbdcb17c01245ccf9d Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Mon, 22 Apr 2019 22:12:08 +0000 Subject: [PATCH] Refactor and cleanup of CGameView. Commented By: asterix, elexis, Stan, wraitii Differential Revision: https://code.wildfiregames.com/D1571 This was SVN commit r22214. --- source/graphics/GameView.cpp | 68 +++++---------- source/graphics/GameView.h | 86 ++++++++----------- .../scripting/JSInterface_GameView.cpp | 6 +- source/ps/SavedGame.cpp | 17 ++-- 4 files changed, 71 insertions(+), 106 deletions(-) diff --git a/source/graphics/GameView.cpp b/source/graphics/GameView.cpp index 44ab8b441b..1b2711c570 100644 --- a/source/graphics/GameView.cpp +++ b/source/graphics/GameView.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 Wildfire Games. +/* 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 @@ -77,7 +77,7 @@ public: { } - float GetSmoothedValue() + float GetSmoothedValue() const { return m_Current; } @@ -98,7 +98,7 @@ public: m_Current += value; } - float GetValue() + float GetValue() const { return m_Target; } @@ -114,7 +114,7 @@ public: if (fabs(m_Target - m_Current) < m_MinDelta) return 0.0f; - double p = pow((double)m_Smoothness, 10.0 * (double)time); + double p = pow(static_cast(m_Smoothness), 10.0 * static_cast(time)); // (add the factor of 10 so that smoothnesses don't have to be tiny numbers) double delta = (m_Target - m_Current) * (1.0 - p); @@ -124,13 +124,13 @@ public: void ClampSmoothly(float min, float max) { - m_Target = Clamp(m_Target, (double)min, (double)max); + m_Target = Clamp(m_Target, static_cast(min), static_cast(max)); } // Wrap so 'target' is in the range [min, max] void Wrap(float min, float max) { - double t = fmod(m_Target - min, (double)(max - min)); + double t = fmod(m_Target - min, static_cast(max - min)); if (t < 0) t += max - min; t += min; @@ -139,14 +139,14 @@ public: m_Target = t; } + float m_Smoothness; + private: double m_Target; // the value which m_Current is tending towards double m_Current; // (We use double because the extra precision is worthwhile here) float m_MinDelta; // cutoff where we stop moving (to avoid ugly shimmering effects) -public: - float m_Smoothness; }; class CGameViewImpl @@ -312,7 +312,7 @@ public: }; #define IMPLEMENT_BOOLEAN_SETTING(NAME) \ -bool CGameView::Get##NAME##Enabled() \ +bool CGameView::Get##NAME##Enabled() const \ { \ return m->NAME; \ } \ @@ -356,10 +356,10 @@ CGameView::CGameView(CGame *pGame): m(new CGameViewImpl(pGame)) { SViewPort vp; - vp.m_X=0; - vp.m_Y=0; - vp.m_Width=g_xres; - vp.m_Height=g_yres; + vp.m_X = 0; + vp.m_Y = 0; + vp.m_Width = g_xres; + vp.m_Height = g_yres; m->ViewCamera.SetViewPort(vp); m->ViewCamera.SetProjection(m->ViewNear, m->ViewFar, m->ViewFOV); @@ -383,7 +383,7 @@ void CGameView::SetViewport(const SViewPort& vp) m->ViewCamera.SetProjection(m->ViewNear, m->ViewFar, m->ViewFOV); } -CObjectManager& CGameView::GetObjectManager() const +CObjectManager& CGameView::GetObjectManager() { return m->ObjectManager; } @@ -464,7 +464,6 @@ void CGameView::RegisterInit() // CGameView init RegMemFun(this, &CGameView::Initialize, L"CGameView init", 1); - // previously done by CGameView::InitResources RegMemFun(g_TexMan.GetSingletonPtr(), &CTerrainTextureManager::LoadTerrainTextures, L"LoadTerrainTextures", 60); RegMemFun(g_Renderer.GetSingletonPtr(), &CRenderer::LoadAlphaMaps, L"LoadAlphaMaps", 5); } @@ -893,46 +892,23 @@ void CGameView::Update(const float deltaRealTime) m->ViewCamera.UpdateFrustum(); } -float CGameView::GetCameraX() +CVector3D CGameView::GetCameraPivot() const { - CCamera targetCam = m->ViewCamera; - CVector3D pivot = GetSmoothPivot(targetCam); - return pivot.X; + return GetSmoothPivot(m->ViewCamera); } -float CGameView::GetCameraZ() +CVector3D CGameView::GetCameraPosition() const { - CCamera targetCam = m->ViewCamera; - CVector3D pivot = GetSmoothPivot(targetCam); - return pivot.Z; + return CVector3D(m->PosX.GetValue(), m->PosY.GetValue(), m->PosZ.GetValue()); } -float CGameView::GetCameraPosX() +CVector3D CGameView::GetCameraRotation() const { - return m->PosX.GetValue(); + // The angle of rotation around the Z axis is not used. + return CVector3D(m->RotateX.GetValue(), m->RotateY.GetValue(), 0.0f); } -float CGameView::GetCameraPosY() -{ - return m->PosY.GetValue(); -} - -float CGameView::GetCameraPosZ() -{ - return m->PosZ.GetValue(); -} - -float CGameView::GetCameraRotX() -{ - return m->RotateX.GetValue(); -} - -float CGameView::GetCameraRotY() -{ - return m->RotateY.GetValue(); -} - -float CGameView::GetCameraZoom() +float CGameView::GetCameraZoom() const { return m->Zoom.GetValue(); } diff --git a/source/graphics/GameView.h b/source/graphics/GameView.h index 8fa60876b1..870380b0d0 100644 --- a/source/graphics/GameView.h +++ b/source/graphics/GameView.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* 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 @@ -30,34 +30,11 @@ class CCinemaManager; class CVector3D; struct SViewPort; -class JSObject; - class CGameViewImpl; class CGameView : private Scene { NONCOPYABLE(CGameView); -private: - CGameViewImpl* m; - - // Check whether lighting environment has changed and update vertex data if necessary - void CheckLightEnv(); - -public: - //BEGIN: Implementation of Scene - virtual void EnumerateObjects(const CFrustum& frustum, SceneCollector* c); - virtual CLOSTexture& GetLOSTexture(); - virtual CTerritoryTexture& GetTerritoryTexture(); - //END: Implementation of Scene - -private: - // InitResources(): Load all graphics resources (textures, actor objects and - // alpha maps) required by the game - //void InitResources(); - - // UnloadResources(): Unload all graphics resources loaded by InitResources - void UnloadResources(); - public: CGameView(CGame *pGame); ~CGameView(); @@ -67,8 +44,6 @@ public: void RegisterInit(); int Initialize(); - CObjectManager& GetObjectManager() const; - /** * Updates all the view information (i.e. rotate camera, scroll, whatever). This will *not* change any * World information - only the *presentation*. @@ -82,29 +57,25 @@ public: InReaction HandleEvent(const SDL_Event_* ev); - float GetCameraX(); - float GetCameraZ(); - float GetCameraPosX(); - float GetCameraPosY(); - float GetCameraPosZ(); - float GetCameraRotX(); - float GetCameraRotY(); - float GetCameraZoom(); - void SetCamera(CVector3D Pos, float RotX, float RotY, float Zoom); - void MoveCameraTarget(const CVector3D& target); - void ResetCameraTarget(const CVector3D& target); - void ResetCameraAngleZoom(); - void CameraFollow(entity_id_t entity, bool firstPerson); - entity_id_t GetFollowedEntity(); - - CVector3D GetSmoothPivot(CCamera &camera) const; - + CVector3D GetCameraPivot() const; + CVector3D GetCameraPosition() const; + CVector3D GetCameraRotation() const; + float GetCameraZoom() const; float GetNear() const; float GetFar() const; float GetFOV() const; + void SetCamera(CVector3D Pos, float RotX, float RotY, float Zoom); + void MoveCameraTarget(const CVector3D& target); + void ResetCameraTarget(const CVector3D& target); + void CameraFollow(entity_id_t entity, bool firstPerson); + entity_id_t GetFollowedEntity(); + + // Set projection of current camera using near, far, and FOV values + void SetCameraProjection(); + #define DECLARE_BOOLEAN_SETTING(NAME) \ - bool Get##NAME##Enabled(); \ + bool Get##NAME##Enabled() const; \ void Set##NAME##Enabled(bool Enabled); DECLARE_BOOLEAN_SETTING(Culling); @@ -113,13 +84,28 @@ public: #undef DECLARE_BOOLEAN_SETTING - // Set projection of current camera using near, far, and FOV values - void SetCameraProjection(); - - CCamera *GetCamera(); + CCamera* GetCamera(); CCinemaManager* GetCinema(); + CObjectManager& GetObjectManager(); - JSObject* GetScript(); + // Implementations of Scene + void EnumerateObjects(const CFrustum& frustum, SceneCollector* c) override; + CLOSTexture& GetLOSTexture() override; + CTerritoryTexture& GetTerritoryTexture() override; + +private: + // Unloads all graphics resources loaded by RegisterInit. + void UnloadResources(); + + // Checks whether lighting environment has changed and update vertex data if necessary. + void CheckLightEnv(); + + CVector3D GetSmoothPivot(CCamera &camera) const; + void ResetCameraAngleZoom(); + + CGameViewImpl* m; }; + extern InReaction game_view_handler(const SDL_Event_* ev); -#endif + +#endif // INCLUDED_GAMEVIEW diff --git a/source/graphics/scripting/JSInterface_GameView.cpp b/source/graphics/scripting/JSInterface_GameView.cpp index 739650cb9a..00f27db14f 100644 --- a/source/graphics/scripting/JSInterface_GameView.cpp +++ b/source/graphics/scripting/JSInterface_GameView.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 Wildfire Games. +/* 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 @@ -77,7 +77,7 @@ float JSI_GameView::CameraGetX(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) if (!g_Game || !g_Game->GetView()) return -1; - return g_Game->GetView()->GetCameraX(); + return g_Game->GetView()->GetCameraPivot().X; } /** @@ -88,7 +88,7 @@ float JSI_GameView::CameraGetZ(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) if (!g_Game || !g_Game->GetView()) return -1; - return g_Game->GetView()->GetCameraZ(); + return g_Game->GetView()->GetCameraPivot().Z; } /** diff --git a/source/ps/SavedGame.cpp b/source/ps/SavedGame.cpp index 8febce5272..56806a447c 100644 --- a/source/ps/SavedGame.cpp +++ b/source/ps/SavedGame.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2018 Wildfire Games. +/* 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 @@ -21,10 +21,11 @@ #include "graphics/GameView.h" #include "gui/GUIManager.h" +#include "i18n/L10n.h" #include "lib/allocators/shared_ptr.h" #include "lib/file/archive/archive_zip.h" -#include "i18n/L10n.h" #include "lib/utf8.h" +#include "maths/Vector3D.h" #include "ps/CLogger.h" #include "ps/Filesystem.h" #include "ps/Game.h" @@ -94,11 +95,13 @@ Status SavedGames::Save(const CStrW& name, const CStrW& description, CSimulation // get some camera data JS::RootedValue cameraMetadata(cx); simulation.GetScriptInterface().Eval("({})", &cameraMetadata); - simulation.GetScriptInterface().SetProperty(cameraMetadata, "PosX", g_Game->GetView()->GetCameraPosX()); - simulation.GetScriptInterface().SetProperty(cameraMetadata, "PosY", g_Game->GetView()->GetCameraPosY()); - simulation.GetScriptInterface().SetProperty(cameraMetadata, "PosZ", g_Game->GetView()->GetCameraPosZ()); - simulation.GetScriptInterface().SetProperty(cameraMetadata, "RotX", g_Game->GetView()->GetCameraRotX()); - simulation.GetScriptInterface().SetProperty(cameraMetadata, "RotY", g_Game->GetView()->GetCameraRotY()); + const CVector3D cameraPosition = g_Game->GetView()->GetCameraPosition(); + simulation.GetScriptInterface().SetProperty(cameraMetadata, "PosX", cameraPosition.X); + simulation.GetScriptInterface().SetProperty(cameraMetadata, "PosY", cameraPosition.Y); + simulation.GetScriptInterface().SetProperty(cameraMetadata, "PosZ", cameraPosition.Z); + const CVector3D cameraRotation = g_Game->GetView()->GetCameraRotation(); + simulation.GetScriptInterface().SetProperty(cameraMetadata, "RotX", cameraRotation.X); + simulation.GetScriptInterface().SetProperty(cameraMetadata, "RotY", cameraRotation.Y); simulation.GetScriptInterface().SetProperty(cameraMetadata, "Zoom", g_Game->GetView()->GetCameraZoom()); simulation.GetScriptInterface().SetProperty(guiMetadata, "camera", cameraMetadata); simulation.GetScriptInterface().SetProperty(metadata, "gui", guiMetadata);