LCOV - code coverage report
Current view: top level - source/tools/atlas/GameInterface - ActorViewer.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 272 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 26 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "ActorViewer.h"
      21             : 
      22             : #include "graphics/Camera.h"
      23             : #include "graphics/Canvas2D.h"
      24             : #include "graphics/ColladaManager.h"
      25             : #include "graphics/Color.h"
      26             : #include "graphics/LOSTexture.h"
      27             : #include "graphics/MeshManager.h"
      28             : #include "graphics/MiniMapTexture.h"
      29             : #include "graphics/MiniPatch.h"
      30             : #include "graphics/Model.h"
      31             : #include "graphics/ModelAbstract.h"
      32             : #include "graphics/ObjectManager.h"
      33             : #include "graphics/Overlay.h"
      34             : #include "graphics/ParticleManager.h"
      35             : #include "graphics/Patch.h"
      36             : #include "graphics/SkeletonAnimManager.h"
      37             : #include "graphics/Terrain.h"
      38             : #include "graphics/TerrainTextureManager.h"
      39             : #include "graphics/TerritoryTexture.h"
      40             : #include "graphics/Unit.h"
      41             : #include "graphics/UnitManager.h"
      42             : #include "lib/debug.h"
      43             : #include "lib/posix/posix_types.h"
      44             : #include "maths/BoundingBoxAligned.h"
      45             : #include "maths/Fixed.h"
      46             : #include "maths/FixedVector3D.h"
      47             : #include "maths/Matrix3D.h"
      48             : #include "maths/Vector3D.h"
      49             : #include "ps/CLogger.h"
      50             : #include "ps/CStr.h"
      51             : #include "ps/Filesystem.h"
      52             : #include "ps/ProfileViewer.h"
      53             : #include "ps/VideoMode.h"
      54             : #include "renderer/Renderer.h"
      55             : #include "renderer/RenderingOptions.h"
      56             : #include "renderer/Scene.h"
      57             : #include "renderer/SceneRenderer.h"
      58             : #include "renderer/SkyManager.h"
      59             : #include "renderer/WaterManager.h"
      60             : #include "renderer/backend/IDeviceCommandContext.h"
      61             : #include "renderer/backend/IFramebuffer.h"
      62             : #include "renderer/backend/ISwapChain.h"
      63             : #include "scriptinterface/Interface.h"
      64             : #include "simulation2/Simulation2.h"
      65             : #include "simulation2/components/ICmpAttack.h"
      66             : #include "simulation2/components/ICmpOwnership.h"
      67             : #include "simulation2/components/ICmpPosition.h"
      68             : #include "simulation2/components/ICmpRangeManager.h"
      69             : #include "simulation2/components/ICmpTerrain.h"
      70             : #include "simulation2/components/ICmpUnitMotion.h"
      71             : #include "simulation2/components/ICmpVisual.h"
      72             : #include "simulation2/components/ICmpWaterManager.h"
      73             : #include "simulation2/helpers/Position.h"
      74             : #include "simulation2/helpers/Render.h"
      75             : #include "simulation2/system/Component.h"
      76             : #include "tools/atlas/GameInterface/View.h"
      77             : 
      78             : #include <cstddef>
      79             : #include <memory>
      80             : #include <vector>
      81             : 
      82             : class CTerrainTextureEntry;
      83             : 
      84             : struct ActorViewerImpl : public Scene
      85             : {
      86             :     NONCOPYABLE(ActorViewerImpl);
      87             : public:
      88           0 :     ActorViewerImpl() :
      89           0 :         Entity(INVALID_ENTITY),
      90           0 :         Terrain(),
      91           0 :         ColladaManager(g_VFS),
      92           0 :         MeshManager(ColladaManager),
      93           0 :         SkeletonAnimManager(ColladaManager),
      94           0 :         UnitManager(),
      95           0 :         Simulation2{&UnitManager, *g_ScriptContext, &Terrain, CSimulation2::DEFAULT_SCRIPTS},
      96           0 :         ObjectManager(MeshManager, SkeletonAnimManager, Simulation2),
      97           0 :         LOSTexture(Simulation2),
      98           0 :         TerritoryTexture(Simulation2),
      99           0 :         MiniMapTexture(g_VideoMode.GetBackendDevice(), Simulation2)
     100             :     {
     101           0 :         UnitManager.SetObjectManager(ObjectManager);
     102           0 :     }
     103             : 
     104             :     entity_id_t Entity;
     105             :     CStrW CurrentUnitID;
     106             :     CStr CurrentUnitAnim;
     107             :     float CurrentSpeed;
     108             :     bool WalkEnabled;
     109             :     bool GroundEnabled;
     110             :     bool WaterEnabled;
     111             :     bool ShadowsEnabled;
     112             : 
     113             :     // Whether shadows, sky and water are enabled outside of the actor viewer.
     114             :     bool OldShadows;
     115             :     bool OldSky;
     116             :     bool OldWater;
     117             : 
     118             :     bool SelectionBoxEnabled;
     119             :     bool AxesMarkerEnabled;
     120             :     int PropPointsMode; // 0 disabled, 1 for point markers, 2 for point markers + axes
     121             : 
     122             :     CTerrain Terrain;
     123             : 
     124             :     CColladaManager ColladaManager;
     125             :     CMeshManager MeshManager;
     126             :     CSkeletonAnimManager SkeletonAnimManager;
     127             :     CUnitManager UnitManager;
     128             :     CSimulation2 Simulation2;
     129             :     CObjectManager ObjectManager; // Keep this after Simulation2 - it needs it for initialisation.
     130             :     CLOSTexture LOSTexture;
     131             :     CTerritoryTexture TerritoryTexture;
     132             :     CMiniMapTexture MiniMapTexture;
     133             : 
     134             :     SOverlayLine SelectionBoxOverlay;
     135             :     SOverlayLine AxesMarkerOverlays[3];
     136             : 
     137             :     std::vector<CModelAbstract*> PropModels;
     138             :     std::vector<SOverlayLine> PropPointOverlays;
     139             : 
     140             :     // Simplistic implementation of the Scene interface
     141           0 :     virtual void EnumerateObjects(const CFrustum& frustum, SceneCollector* c)
     142             :     {
     143           0 :         if (GroundEnabled)
     144             :         {
     145           0 :             for (ssize_t pj = 0; pj < Terrain.GetPatchesPerSide(); ++pj)
     146           0 :                 for (ssize_t pi = 0; pi < Terrain.GetPatchesPerSide(); ++pi)
     147           0 :                     c->Submit(Terrain.GetPatch(pi, pj));
     148             :         }
     149             : 
     150           0 :         CmpPtr<ICmpVisual> cmpVisual(Simulation2, Entity);
     151           0 :         if (cmpVisual)
     152             :         {
     153             :             // add selection box outlines manually
     154           0 :             if (SelectionBoxEnabled)
     155             :             {
     156           0 :                 SelectionBoxOverlay.m_Color = CColor(35/255.f, 86/255.f, 188/255.f, .75f); // pretty blue
     157           0 :                 SelectionBoxOverlay.m_Thickness = 0.1f;
     158             : 
     159           0 :                 SimRender::ConstructBoxOutline(cmpVisual->GetSelectionBox(), SelectionBoxOverlay);
     160           0 :                 c->Submit(&SelectionBoxOverlay);
     161             :             }
     162             : 
     163             :             // add origin axis thingy
     164           0 :             if (AxesMarkerEnabled)
     165             :             {
     166           0 :                 CMatrix3D worldSpaceAxes;
     167             :                 // offset from the ground a little bit to prevent fighting with the floor texture (also note: SetTranslation
     168             :                 // sets the identity 3x3 transformation matrix, which are the world axes)
     169           0 :                 worldSpaceAxes.SetTranslation(cmpVisual->GetPosition() + CVector3D(0, 0.02f, 0));
     170           0 :                 SimRender::ConstructAxesMarker(worldSpaceAxes, AxesMarkerOverlays[0], AxesMarkerOverlays[1], AxesMarkerOverlays[2]);
     171             : 
     172           0 :                 c->Submit(&AxesMarkerOverlays[0]);
     173           0 :                 c->Submit(&AxesMarkerOverlays[1]);
     174           0 :                 c->Submit(&AxesMarkerOverlays[2]);
     175             :             }
     176             : 
     177             :             // add prop point overlays
     178           0 :             if (PropPointsMode > 0 && PropModels.size() > 0)
     179             :             {
     180           0 :                 PropPointOverlays.clear(); // doesn't clear capacity, but should be ok since the number of prop points is usually pretty limited
     181           0 :                 for (size_t i = 0; i < PropModels.size(); ++i)
     182             :                 {
     183           0 :                     CModelAbstract* model = PropModels[i];
     184           0 :                     if (model) // should always be the case
     185             :                     {
     186             :                         // prop point positions are automatically updated during animations etc. by CModel::ValidatePosition
     187           0 :                         const CMatrix3D& propCoordSystem = model->GetTransform();
     188             : 
     189           0 :                         SOverlayLine pointGimbal;
     190           0 :                         pointGimbal.m_Color = CColor(1.f, 0.f, 1.f, 1.f);
     191           0 :                         SimRender::ConstructGimbal(propCoordSystem.GetTranslation(), 0.05f, pointGimbal);
     192           0 :                         PropPointOverlays.push_back(pointGimbal);
     193             : 
     194           0 :                         if (PropPointsMode > 1)
     195             :                         {
     196             :                             // scale the prop axes coord system down a bit to distinguish them from the main world-space axes markers
     197           0 :                             CMatrix3D displayCoordSystem = propCoordSystem;
     198           0 :                             displayCoordSystem.Scale(0.5f, 0.5f, 0.5f);
     199             :                             // revert translation scaling
     200           0 :                             displayCoordSystem._14 = propCoordSystem._14;
     201           0 :                             displayCoordSystem._24 = propCoordSystem._24;
     202           0 :                             displayCoordSystem._34 = propCoordSystem._34;
     203             : 
     204             :                             // construct an XYZ axes marker for the prop's coordinate system
     205           0 :                             SOverlayLine xAxis, yAxis, zAxis;
     206           0 :                             SimRender::ConstructAxesMarker(displayCoordSystem, xAxis, yAxis, zAxis);
     207           0 :                             PropPointOverlays.push_back(xAxis);
     208           0 :                             PropPointOverlays.push_back(yAxis);
     209           0 :                             PropPointOverlays.push_back(zAxis);
     210           0 :                         }
     211           0 :                     }
     212             :                 }
     213             : 
     214           0 :                 for (size_t i = 0; i < PropPointOverlays.size(); ++i)
     215             :                 {
     216           0 :                     c->Submit(&PropPointOverlays[i]);
     217             :                 }
     218             :             }
     219             :         }
     220             : 
     221             :         // send a RenderSubmit message so the components can submit their visuals to the renderer
     222           0 :         Simulation2.RenderSubmit(*c, frustum, false);
     223           0 :     }
     224             : 
     225           0 :     virtual CLOSTexture& GetLOSTexture()
     226             :     {
     227           0 :         return LOSTexture;
     228             :     }
     229             : 
     230           0 :     virtual CTerritoryTexture& GetTerritoryTexture()
     231             :     {
     232           0 :         return TerritoryTexture;
     233             :     }
     234             : 
     235           0 :     virtual CMiniMapTexture& GetMiniMapTexture()
     236             :     {
     237           0 :         return MiniMapTexture;
     238             :     }
     239             : 
     240             :     /**
     241             :      * Recursively fetches the props of the currently displayed entity model and its submodels, and stores them for rendering.
     242             :      */
     243             :     void UpdatePropList();
     244             :     void UpdatePropListRecursive(CModelAbstract* model);
     245             : 
     246             : };
     247             : 
     248           0 : void ActorViewerImpl::UpdatePropList()
     249             : {
     250           0 :     PropModels.clear();
     251             : 
     252           0 :     CmpPtr<ICmpVisual> cmpVisual(Simulation2, Entity);
     253           0 :     if (cmpVisual)
     254             :     {
     255           0 :         CUnit* unit = cmpVisual->GetUnit();
     256           0 :         if (unit)
     257             :         {
     258           0 :             CModelAbstract& modelAbstract = unit->GetModel();
     259           0 :             UpdatePropListRecursive(&modelAbstract);
     260             :         }
     261             :     }
     262           0 : }
     263             : 
     264           0 : void ActorViewerImpl::UpdatePropListRecursive(CModelAbstract* modelAbstract)
     265             : {
     266           0 :     ENSURE(modelAbstract);
     267             : 
     268           0 :     CModel* model = modelAbstract->ToCModel();
     269           0 :     if (model)
     270             :     {
     271           0 :         std::vector<CModel::Prop>& modelProps = model->GetProps();
     272           0 :         for (CModel::Prop& modelProp : modelProps)
     273             :         {
     274           0 :             PropModels.push_back(modelProp.m_Model.get());
     275           0 :             if (modelProp.m_Model)
     276           0 :                 UpdatePropListRecursive(modelProp.m_Model.get());
     277             :         }
     278             :     }
     279           0 : }
     280             : 
     281           0 : ActorViewer::ActorViewer()
     282           0 :     : m(*new ActorViewerImpl())
     283             : {
     284           0 :     m.WalkEnabled = false;
     285           0 :     m.GroundEnabled = true;
     286           0 :     m.WaterEnabled = false;
     287           0 :     m.ShadowsEnabled = g_RenderingOptions.GetShadows();
     288           0 :     m.SelectionBoxEnabled = false;
     289           0 :     m.AxesMarkerEnabled = false;
     290           0 :     m.PropPointsMode = 0;
     291             : 
     292             :     // Create a tiny empty piece of terrain, just so we can put shadows
     293             :     // on it without having to think too hard
     294           0 :     m.Terrain.Initialize(2, NULL);
     295           0 :     CTerrainTextureEntry* tex = g_TexMan.FindTexture("whiteness");
     296           0 :     if (tex)
     297             :     {
     298           0 :         for (ssize_t pi = 0; pi < m.Terrain.GetPatchesPerSide(); ++pi)
     299             :         {
     300           0 :             for (ssize_t pj = 0; pj < m.Terrain.GetPatchesPerSide(); ++pj)
     301             :             {
     302           0 :                 CPatch* patch = m.Terrain.GetPatch(pi, pj);
     303           0 :                 for (ssize_t i = 0; i < PATCH_SIZE; ++i)
     304             :                 {
     305           0 :                     for (ssize_t j = 0; j < PATCH_SIZE; ++j)
     306             :                     {
     307           0 :                         CMiniPatch& mp = patch->m_MiniPatches[i][j];
     308           0 :                         mp.Tex = tex;
     309           0 :                         mp.Priority = 0;
     310             :                     }
     311             :                 }
     312             :             }
     313             :         }
     314             :     }
     315             :     else
     316             :     {
     317           0 :         debug_warn(L"Failed to load whiteness texture");
     318             :     }
     319             : 
     320             :     // Prepare the simulation
     321           0 :     m.Simulation2.ResetState();
     322             : 
     323             :     // Set player data
     324           0 :     m.Simulation2.SetMapSettings(m.Simulation2.GetPlayerDefaults());
     325           0 :     m.Simulation2.LoadPlayerSettings(true);
     326             : 
     327             :     // Tell the simulation we've already loaded the terrain
     328           0 :     CmpPtr<ICmpTerrain> cmpTerrain(m.Simulation2, SYSTEM_ENTITY);
     329           0 :     if (cmpTerrain)
     330           0 :         cmpTerrain->ReloadTerrain(false);
     331             : 
     332             :     // Remove FOW since we're in Atlas
     333           0 :     CmpPtr<ICmpRangeManager> cmpRangeManager(m.Simulation2, SYSTEM_ENTITY);
     334           0 :     if (cmpRangeManager)
     335           0 :         cmpRangeManager->SetLosRevealWholeMapForAll(true);
     336             : 
     337           0 :     m.Simulation2.InitGame();
     338           0 : }
     339             : 
     340           0 : ActorViewer::~ActorViewer()
     341             : {
     342           0 :     delete &m;
     343           0 : }
     344             : 
     345           0 : CSimulation2* ActorViewer::GetSimulation2()
     346             : {
     347           0 :     return &m.Simulation2;
     348             : }
     349             : 
     350           0 : entity_id_t ActorViewer::GetEntity()
     351             : {
     352           0 :     return m.Entity;
     353             : }
     354             : 
     355           0 : void ActorViewer::UnloadObjects()
     356             : {
     357           0 :     m.ObjectManager.UnloadObjects();
     358           0 : }
     359             : 
     360           0 : void ActorViewer::SetActor(const CStrW& name, const CStr& animation, player_id_t playerID)
     361             : {
     362           0 :     bool needsAnimReload = false;
     363             : 
     364           0 :     CStrW id = name;
     365             : 
     366             :     // Recreate the entity, if we don't have one or if the new one is different
     367           0 :     if (m.Entity == INVALID_ENTITY || id != m.CurrentUnitID)
     368             :     {
     369             :         // Delete the old entity (if any)
     370           0 :         if (m.Entity != INVALID_ENTITY)
     371             :         {
     372           0 :             m.Simulation2.DestroyEntity(m.Entity);
     373           0 :             m.Simulation2.FlushDestroyedEntities();
     374           0 :             m.Entity = INVALID_ENTITY;
     375             :         }
     376             : 
     377             :         // Clear particles associated with deleted entity
     378           0 :         g_Renderer.GetSceneRenderer().GetParticleManager().ClearUnattachedEmitters();
     379             : 
     380             :         // If there's no actor to display, return with nothing loaded
     381           0 :         if (id.empty())
     382           0 :             return;
     383             : 
     384           0 :         m.Entity = m.Simulation2.AddEntity(L"actorviewer|" + id);
     385           0 :         if (m.Entity == INVALID_ENTITY)
     386             :             return;
     387             : 
     388           0 :         CmpPtr<ICmpPosition> cmpPosition(m.Simulation2, m.Entity);
     389           0 :         if (cmpPosition)
     390             :         {
     391           0 :             ssize_t c = TERRAIN_TILE_SIZE * m.Terrain.GetPatchesPerSide()*PATCH_SIZE/2;
     392           0 :             cmpPosition->JumpTo(entity_pos_t::FromInt(c), entity_pos_t::FromInt(c));
     393           0 :             cmpPosition->SetYRotation(entity_angle_t::Pi());
     394             :         }
     395             : 
     396           0 :         CmpPtr<ICmpOwnership> cmpOwnership(m.Simulation2, m.Entity);
     397           0 :         if (cmpOwnership)
     398           0 :             cmpOwnership->SetOwner(playerID);
     399             : 
     400             :         needsAnimReload = true;
     401             :     }
     402             : 
     403           0 :     if (animation != m.CurrentUnitAnim)
     404             :         needsAnimReload = true;
     405             : 
     406           0 :     if (needsAnimReload)
     407             :     {
     408             :         // Emulate the typical simulation animation behaviour.
     409           0 :         CStr anim = animation.LowerCase();
     410           0 :         float speed = 1.0f;
     411             :         // Speed will be ignored if we have a repeat time.
     412           0 :         float repeatTime = 0.0f;
     413           0 :         m.CurrentSpeed = 0.0f;
     414           0 :         if (anim == "walk")
     415             :         {
     416           0 :             CmpPtr<ICmpUnitMotion> cmpUnitMotion(m.Simulation2, m.Entity);
     417           0 :             if (cmpUnitMotion)
     418           0 :                 speed  = cmpUnitMotion->GetWalkSpeed().ToFloat();
     419             :             else
     420             :                 speed = 7.f; // Typical unit walk speed.
     421           0 :             m.CurrentSpeed = speed;
     422             :         }
     423           0 :         else if (anim == "run")
     424             :         {
     425           0 :             CmpPtr<ICmpUnitMotion> cmpUnitMotion(m.Simulation2, m.Entity);
     426           0 :             if (cmpUnitMotion)
     427           0 :                 speed = cmpUnitMotion->GetWalkSpeed().ToFloat() * cmpUnitMotion->GetRunMultiplier().ToFloat();
     428             :             else
     429             :                 speed = 12.f; // Typical unit run speed.
     430             : 
     431           0 :             m.CurrentSpeed = speed;
     432             :         }
     433           0 :         else if (anim.starts_with("attack_"))
     434             :         {
     435           0 :             CmpPtr<ICmpAttack> cmpAttack(m.Simulation2, m.Entity);
     436           0 :             if (cmpAttack)
     437           0 :                 for (const CStr& type : cmpAttack->GetAttackTypes())
     438           0 :                     if (anim == "attack_" + type.LowerCase())
     439             :                     {
     440           0 :                         repeatTime = GetRepeatTimeByAttackType(type);
     441             :                         break;
     442           0 :                     }
     443             :         }
     444             : 
     445           0 :         CmpPtr<ICmpVisual> cmpVisual(m.Simulation2, m.Entity);
     446           0 :         if (cmpVisual)
     447             :         {
     448             :             // TODO: SetEntitySelection(anim)
     449           0 :             cmpVisual->SelectAnimation(anim, false, fixed::FromFloat(speed));
     450           0 :             if (repeatTime > 0.0f)
     451           0 :                 cmpVisual->SetAnimationSyncRepeat(fixed::FromFloat(repeatTime));
     452             :         }
     453             : 
     454             :         // update prop list for new entity/animation (relies on needsAnimReload also getting called for entire entity changes)
     455           0 :         m.UpdatePropList();
     456           0 :     }
     457             : 
     458           0 :     m.CurrentUnitID = id;
     459           0 :     m.CurrentUnitAnim = animation;
     460           0 : }
     461             : 
     462           0 : void ActorViewer::SetEnabled(bool enabled)
     463             : {
     464           0 :     if (enabled)
     465             :     {
     466             :         // Set shadows, sky and water.
     467           0 :         m.OldShadows = g_RenderingOptions.GetShadows();
     468           0 :         SetShadowsEnabled(m.ShadowsEnabled);
     469             : 
     470           0 :         m.OldSky = g_Renderer.GetSceneRenderer().GetSkyManager().IsSkyVisible();
     471           0 :         g_Renderer.GetSceneRenderer().GetSkyManager().SetSkyVisible(false);
     472             : 
     473           0 :         m.OldWater = g_Renderer.GetSceneRenderer().GetWaterManager().m_RenderWater;
     474           0 :         g_Renderer.GetSceneRenderer().GetWaterManager().m_RenderWater = m.WaterEnabled;
     475             :     }
     476             :     else
     477             :     {
     478             :         // Restore the old renderer state
     479           0 :         SetShadowsEnabled(m.OldShadows);
     480           0 :         g_Renderer.GetSceneRenderer().GetSkyManager().SetSkyVisible(m.OldSky);
     481           0 :         g_Renderer.GetSceneRenderer().GetWaterManager().m_RenderWater = m.OldWater;
     482             :     }
     483           0 : }
     484             : 
     485           0 : void ActorViewer::SetWalkEnabled(bool enabled)    { m.WalkEnabled = enabled; }
     486           0 : void ActorViewer::SetGroundEnabled(bool enabled)  { m.GroundEnabled = enabled; }
     487           0 : void ActorViewer::SetWaterEnabled(bool enabled)
     488             : {
     489           0 :     m.WaterEnabled = enabled;
     490             :     // Adjust water level
     491           0 :     entity_pos_t waterLevel = entity_pos_t::FromFloat(enabled ? 10.f : 0.f);
     492           0 :     CmpPtr<ICmpWaterManager> cmpWaterManager(m.Simulation2, SYSTEM_ENTITY);
     493           0 :     if (cmpWaterManager)
     494           0 :         cmpWaterManager->SetWaterLevel(waterLevel);
     495           0 : }
     496           0 : void ActorViewer::SetShadowsEnabled(bool enabled) {
     497           0 :     g_RenderingOptions.SetShadows(enabled);
     498           0 :     m.ShadowsEnabled = enabled;
     499           0 : }
     500           0 : void ActorViewer::ToggleShadows()
     501             : {
     502           0 :     SetShadowsEnabled(!m.ShadowsEnabled);
     503           0 : }
     504           0 : void ActorViewer::SetBoundingBoxesEnabled(bool enabled) { m.SelectionBoxEnabled = enabled; }
     505           0 : void ActorViewer::SetAxesMarkerEnabled(bool enabled)    { m.AxesMarkerEnabled = enabled; }
     506           0 : void ActorViewer::SetPropPointsMode(int mode)           { m.PropPointsMode = mode; }
     507             : 
     508           0 : void ActorViewer::SetStatsEnabled(bool enabled)
     509             : {
     510           0 :     if (enabled)
     511           0 :         g_ProfileViewer.ShowTable("renderer");
     512             :     else
     513           0 :         g_ProfileViewer.ShowTable("");
     514           0 : }
     515             : 
     516           0 : float ActorViewer::GetRepeatTimeByAttackType(const std::string& type) const
     517             : {
     518           0 :     CmpPtr<ICmpAttack> cmpAttack(m.Simulation2, m.Entity);
     519           0 :     if (cmpAttack)
     520           0 :         return cmpAttack->GetRepeatTime(type);
     521             : 
     522             :     return 0.0f;
     523             : }
     524             : 
     525           0 : void ActorViewer::Render()
     526             : {
     527             :     // TODO: ActorViewer should reuse CRenderer code and not duplicate it.
     528             : 
     529           0 :     Renderer::Backend::ISwapChain* swapChain{
     530           0 :         g_VideoMode.GetOrCreateSwapChain()};
     531           0 :     if (!swapChain || !swapChain->IsValid())
     532           0 :         return;
     533             : 
     534           0 :     CSceneRenderer& sceneRenderer = g_Renderer.GetSceneRenderer();
     535             : 
     536             :     // Set simulation context for rendering purposes
     537           0 :     sceneRenderer.SetSimulation(&m.Simulation2);
     538             : 
     539             :     // Find the centre of the interesting region, in the middle of the patch
     540             :     // and half way up the model (assuming there is one)
     541           0 :     CVector3D centre;
     542           0 :     CmpPtr<ICmpVisual> cmpVisual(m.Simulation2, m.Entity);
     543           0 :     if (cmpVisual)
     544           0 :         cmpVisual->GetBounds().GetCenter(centre);
     545             :     else
     546             :         centre.Y = 0.f;
     547           0 :     centre.X = centre.Z = TERRAIN_TILE_SIZE * m.Terrain.GetPatchesPerSide()*PATCH_SIZE/2;
     548             : 
     549           0 :     CCamera camera = AtlasView::GetView_Actor()->GetCamera();
     550           0 :     camera.m_Orientation.Translate(centre.X, centre.Y, centre.Z);
     551           0 :     camera.UpdateFrustum();
     552             : 
     553           0 :     sceneRenderer.SetSceneCamera(camera, camera);
     554             : 
     555           0 :     g_Renderer.BeginFrame();
     556             : 
     557           0 :     Renderer::Backend::IDeviceCommandContext* deviceCommandContext =
     558           0 :         g_Renderer.GetDeviceCommandContext();
     559             : 
     560           0 :     sceneRenderer.EnumerateSceneObjects(m);
     561           0 :     sceneRenderer.PrepareSubmissions(deviceCommandContext);
     562             : 
     563           0 :     Renderer::Backend::IFramebuffer* backbuffer =
     564           0 :         swapChain->GetCurrentBackbuffer(
     565             :             Renderer::Backend::AttachmentLoadOp::DONT_CARE,
     566             :             Renderer::Backend::AttachmentStoreOp::STORE,
     567             :             Renderer::Backend::AttachmentLoadOp::CLEAR,
     568             :             Renderer::Backend::AttachmentStoreOp::DONT_CARE);
     569           0 :     deviceCommandContext->BeginFramebufferPass(backbuffer);
     570             : 
     571           0 :     Renderer::Backend::IDeviceCommandContext::Rect viewportRect{};
     572           0 :     viewportRect.width = backbuffer->GetWidth();
     573           0 :     viewportRect.height = backbuffer->GetHeight();
     574           0 :     deviceCommandContext->SetViewports(1, &viewportRect);
     575             : 
     576           0 :     sceneRenderer.RenderSubmissions(deviceCommandContext);
     577           0 :     sceneRenderer.RenderSceneOverlays(deviceCommandContext);
     578             : 
     579           0 :     {
     580           0 :         CCanvas2D canvas(g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight(), g_VideoMode.GetScale(), deviceCommandContext);
     581           0 :         g_Logger->Render(canvas);
     582           0 :         g_ProfileViewer.RenderProfile(canvas);
     583           0 :     }
     584             : 
     585           0 :     deviceCommandContext->EndFramebufferPass();
     586             : 
     587           0 :     g_Renderer.EndFrame();
     588           0 : }
     589             : 
     590           0 : void ActorViewer::Update(float simFrameLength, float realFrameLength)
     591             : {
     592           0 :     m.Simulation2.Update((int)(simFrameLength*1000));
     593           0 :     m.Simulation2.Interpolate(simFrameLength, 0, realFrameLength);
     594             : 
     595           0 :     if (m.WalkEnabled && m.CurrentSpeed)
     596             :     {
     597           0 :         CmpPtr<ICmpPosition> cmpPosition(m.Simulation2, m.Entity);
     598           0 :         if (cmpPosition)
     599             :         {
     600             :             // Move the model by speed*simFrameLength forwards
     601           0 :             float z = cmpPosition->GetPosition().Z.ToFloat();
     602           0 :             z -= m.CurrentSpeed*simFrameLength;
     603             :             // Wrap at the edges, so it doesn't run off into the horizon
     604           0 :             ssize_t c = TERRAIN_TILE_SIZE * m.Terrain.GetPatchesPerSide()*PATCH_SIZE/2;
     605           0 :             if (z < c - TERRAIN_TILE_SIZE*PATCH_SIZE * 0.1f)
     606           0 :                 z = c + TERRAIN_TILE_SIZE*PATCH_SIZE * 0.1f;
     607           0 :             cmpPosition->JumpTo(cmpPosition->GetPosition().X, entity_pos_t::FromFloat(z));
     608             :         }
     609             :     }
     610           0 : }

Generated by: LCOV version 1.16