LCOV - code coverage report
Current view: top level - source/tools/atlas/GameInterface - View.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 262 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 49 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 "View.h"
      21             : 
      22             : #include "graphics/Canvas2D.h"
      23             : #include "graphics/CinemaManager.h"
      24             : #include "graphics/Color.h"
      25             : #include "graphics/GameView.h"
      26             : #include "lib/debug.h"
      27             : #include "lib/utf8.h"
      28             : #include "maths/MathUtil.h"
      29             : #include "maths/Matrix3D.h"
      30             : #include "maths/Vector2D.h"
      31             : #include "ps/CStr.h"
      32             : #include "ps/CLogger.h"
      33             : #include "ps/ConfigDB.h"
      34             : #include "ps/Game.h"
      35             : #include "ps/VideoMode.h"
      36             : #include "renderer/DebugRenderer.h"
      37             : #include "renderer/Renderer.h"
      38             : #include "renderer/SceneRenderer.h"
      39             : #include "renderer/backend/ISwapChain.h"
      40             : #include "simulation2/Simulation2.h"
      41             : #include "simulation2/components/ICmpRangeManager.h"
      42             : #include "simulation2/components/ICmpParticleManager.h"
      43             : #include "simulation2/components/ICmpPathfinder.h"
      44             : #include "simulation2/system/Component.h"
      45             : #include "soundmanager/ISoundManager.h"
      46             : #include "tools/atlas/GameInterface/ActorViewer.h"
      47             : #include "tools/atlas/GameInterface/GameLoop.h"
      48             : #include "tools/atlas/GameInterface/Messages.h"
      49             : #include "tools/atlas/GameInterface/SimState.h"
      50             : 
      51             : #include <sstream>
      52             : #include <utility>
      53             : #include <vector>
      54             : 
      55             : extern void (*Atlas_GLSwapBuffers)(void* context);
      56             : 
      57             : //////////////////////////////////////////////////////////////////////////
      58             : 
      59           0 : void AtlasView::SetParam(const std::wstring& /*name*/, bool /*value*/)
      60             : {
      61           0 : }
      62             : 
      63           0 : void AtlasView::SetParam(const std::wstring& /*name*/, const AtlasMessage::Color& /*value*/)
      64             : {
      65           0 : }
      66             : 
      67           0 : void AtlasView::SetParam(const std::wstring& /*name*/, const std::wstring& /*value*/)
      68             : {
      69           0 : }
      70             : 
      71           0 : void AtlasView::SetParam(const std::wstring& /*name*/, int /*value*/)
      72             : {
      73           0 : }
      74             : 
      75             : //////////////////////////////////////////////////////////////////////////
      76             : 
      77           0 : AtlasViewActor::AtlasViewActor()
      78           0 : : m_SpeedMultiplier(1.f), m_ActorViewer(new ActorViewer())
      79             : {
      80           0 : }
      81             : 
      82           0 : AtlasViewActor::~AtlasViewActor()
      83             : {
      84           0 :     delete m_ActorViewer;
      85           0 : }
      86             : 
      87           0 : void AtlasViewActor::Update(float realFrameLength)
      88             : {
      89           0 :     m_ActorViewer->Update(realFrameLength * m_SpeedMultiplier, realFrameLength);
      90           0 : }
      91             : 
      92           0 : void AtlasViewActor::Render()
      93             : {
      94           0 :     SViewPort vp = { 0, 0, g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight() };
      95           0 :     CCamera camera{GetCamera()};
      96           0 :     camera.SetViewPort(vp);
      97           0 :     camera.SetPerspectiveProjection(2.f, 512.f, DEGTORAD(20.f));
      98           0 :     camera.UpdateFrustum();
      99           0 :     SetCamera(camera);
     100             : 
     101           0 :     m_ActorViewer->Render();
     102           0 :     Atlas_GLSwapBuffers((void*)g_AtlasGameLoop->glCanvas);
     103           0 : }
     104             : 
     105           0 : const CCamera& AtlasViewActor::GetCamera() const
     106             : {
     107           0 :     return m_Camera;
     108             : }
     109             : 
     110           0 : void AtlasViewActor::SetCamera(const CCamera& camera)
     111             : {
     112           0 :     m_Camera = camera;
     113           0 : }
     114             : 
     115           0 : CSimulation2* AtlasViewActor::GetSimulation2()
     116             : {
     117           0 :     return m_ActorViewer->GetSimulation2();
     118             : }
     119             : 
     120           0 : entity_id_t AtlasViewActor::GetEntityId(AtlasMessage::ObjectID)
     121             : {
     122           0 :     return m_ActorViewer->GetEntity();
     123             : }
     124             : 
     125           0 : bool AtlasViewActor::GetSmoothFramerate() const
     126             : {
     127           0 :     if (m_SpeedMultiplier != 0.f)
     128             :         return true;
     129             : 
     130           0 :     return m_SmoothFramerate;
     131             : }
     132             : 
     133           0 : void AtlasViewActor::SetSmoothFramerate(const bool enabled)
     134             : {
     135           0 :     m_SmoothFramerate = enabled;
     136           0 : }
     137             : 
     138           0 : void AtlasViewActor::SetEnabled(bool enabled)
     139             : {
     140           0 :     m_ActorViewer->SetEnabled(enabled);
     141           0 : }
     142             : 
     143           0 : void AtlasViewActor::SetSpeedMultiplier(float speedMultiplier)
     144             : {
     145           0 :     m_SpeedMultiplier = speedMultiplier;
     146           0 : }
     147             : 
     148           0 : ActorViewer& AtlasViewActor::GetActorViewer()
     149             : {
     150           0 :     return *m_ActorViewer;
     151             : }
     152             : 
     153           0 : void AtlasViewActor::SetParam(const std::wstring& name, bool value)
     154             : {
     155           0 :     if (name == L"wireframe")
     156           0 :         g_Renderer.GetSceneRenderer().SetModelRenderMode(value ? WIREFRAME : SOLID);
     157           0 :     else if (name == L"walk")
     158           0 :         m_ActorViewer->SetWalkEnabled(value);
     159           0 :     else if (name == L"ground")
     160           0 :         m_ActorViewer->SetGroundEnabled(value);
     161             :     // TODO: this causes corruption of WaterManager's global state
     162             :     //  which should be asociated with terrain or simulation instead
     163             :     //  see https://gitea.wildfiregames.com/0ad/0ad/issues/2692
     164             :     //else if (name == L"water")
     165             :         //m_ActorViewer->SetWaterEnabled(value);
     166           0 :     else if (name == L"shadows")
     167           0 :         m_ActorViewer->ToggleShadows();
     168           0 :     else if (name == L"stats")
     169           0 :         m_ActorViewer->SetStatsEnabled(value);
     170           0 :     else if (name == L"bounding_box")
     171           0 :         m_ActorViewer->SetBoundingBoxesEnabled(value);
     172           0 :     else if (name == L"axes_marker")
     173           0 :         m_ActorViewer->SetAxesMarkerEnabled(value);
     174           0 : }
     175             : 
     176           0 : void AtlasViewActor::SetParam(const std::wstring& name, int value)
     177             : {
     178           0 :     if (name == L"prop_points")
     179           0 :         m_ActorViewer->SetPropPointsMode(value);
     180           0 : }
     181             : 
     182           0 : void AtlasViewActor::SetParam(const std::wstring& /*name*/, const AtlasMessage::Color& /*value*/)
     183             : {
     184           0 : }
     185             : 
     186             : 
     187             : //////////////////////////////////////////////////////////////////////////
     188             : 
     189           0 : AtlasViewGame::AtlasViewGame()
     190           0 :     : m_SpeedMultiplier(0.f), m_IsTesting(false), m_DrawMoveTool(false)
     191             : {
     192           0 :     ENSURE(g_Game);
     193           0 : }
     194             : 
     195           0 : AtlasViewGame::~AtlasViewGame()
     196             : {
     197           0 :     for (const std::pair<const std::wstring, SimState*>& p : m_SavedStates)
     198           0 :         delete p.second;
     199           0 : }
     200             : 
     201           0 : CSimulation2* AtlasViewGame::GetSimulation2()
     202             : {
     203           0 :     return g_Game->GetSimulation2();
     204             : }
     205             : 
     206           0 : void AtlasViewGame::Update(float realFrameLength)
     207             : {
     208           0 :     const float actualFrameLength = realFrameLength * m_SpeedMultiplier;
     209             : 
     210             :     // Clean up any entities destroyed during UI message processing
     211           0 :     g_Game->GetSimulation2()->FlushDestroyedEntities();
     212             : 
     213           0 :     if (m_SpeedMultiplier == 0.f)
     214             :     {
     215             :         // Update unit interpolation
     216           0 :         g_Game->Interpolate(0.0, realFrameLength);
     217             :     }
     218             :     else
     219             :     {
     220             :         // Update the whole world
     221             :         // (Tell the game update not to interpolate graphics - we'll do that
     222             :         // ourselves)
     223           0 :         g_Game->Update(actualFrameLength, false);
     224             : 
     225             :         // Interpolate the graphics - we only want to do this once per visual frame,
     226             :         // not in every call to g_Game->Update
     227           0 :         g_Game->Interpolate(actualFrameLength, realFrameLength);
     228             :     }
     229             : 
     230             :     // Run sound idle tasks every frame.
     231           0 :     if (g_SoundManager)
     232           0 :         g_SoundManager->IdleTask();
     233             : 
     234             :     // Cinematic motion should be independent of simulation update, so we can
     235             :     // preview the cinematics by themselves
     236           0 :     CCamera camera{g_Game->GetView()->GetCamera()};
     237           0 :     g_Game->GetView()->GetCinema()->Update(realFrameLength, camera);
     238           0 :     g_Game->GetView()->SetCamera(camera);
     239           0 : }
     240             : 
     241           0 : void AtlasViewGame::Render()
     242             : {
     243           0 :     Renderer::Backend::ISwapChain* swapChain{g_VideoMode.GetOrCreateSwapChain()};
     244           0 :     if (!swapChain || !swapChain->IsValid() || !swapChain->AcquireNextBackbuffer())
     245           0 :         return;
     246             : 
     247           0 :     SViewPort vp = { 0, 0, g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight() };
     248           0 :     CCamera camera{GetCamera()};
     249           0 :     camera.SetViewPort(vp);
     250           0 :     camera.SetProjectionFromCamera(g_Game->GetView()->GetCamera());
     251           0 :     camera.UpdateFrustum();
     252           0 :     SetCamera(camera);
     253             : 
     254           0 :     g_Renderer.RenderFrame(false);
     255           0 :     Atlas_GLSwapBuffers((void*)g_AtlasGameLoop->glCanvas);
     256             :     // In case of atlas the swapchain's present will do only internal stuff
     257             :     // without calling a real backbuffer swap.
     258           0 :     swapChain->Present();
     259           0 : }
     260             : 
     261           0 : void AtlasViewGame::DrawCinemaPathTool(Renderer::Backend::IDeviceCommandContext& deviceCommandContext)
     262             : {
     263           0 :     if (!m_DrawMoveTool)
     264           0 :         return;
     265             : 
     266           0 :     const CVector3D focus = m_MoveTool;
     267           0 :     const CVector3D camera = GetCamera().GetOrientation().GetTranslation();
     268           0 :     const float scale = (focus - camera).Length();
     269           0 :     const float axisLength = scale / 10.0f;
     270           0 :     const float lineWidth = scale / 1e3f;
     271             : 
     272           0 :     g_Renderer.GetDebugRenderer().DrawLine(
     273             :         deviceCommandContext,
     274           0 :         focus, focus + CVector3D(axisLength, 0, 0),
     275           0 :         CColor(1.0f, 0.0f, 0.0f, 1.0f), lineWidth, false);
     276           0 :     g_Renderer.GetDebugRenderer().DrawLine(
     277             :         deviceCommandContext,
     278           0 :         focus, focus + CVector3D(0, axisLength, 0),
     279           0 :         CColor(0.0f, 1.0f, 0.0f, 1.0f), lineWidth, false);
     280           0 :     g_Renderer.GetDebugRenderer().DrawLine(
     281             :         deviceCommandContext,
     282           0 :         focus, focus + CVector3D(0, 0, axisLength),
     283           0 :         CColor(0.0f, 0.0f, 1.0f, 1.0f), lineWidth, false);
     284             : }
     285             : 
     286           0 : void AtlasViewGame::DrawOverlays(CCanvas2D& canvas)
     287             : {
     288           0 :     if (m_Bandbox.left >= m_Bandbox.right || m_Bandbox.top >= m_Bandbox.bottom)
     289           0 :         return;
     290             : 
     291           0 :     const std::vector<CVector2D> outerPoints = {
     292           0 :         m_Bandbox.TopLeft() + CVector2D(-1.0f, -1.0f),
     293           0 :         m_Bandbox.TopRight() + CVector2D(1.0f, -1.0f),
     294           0 :         m_Bandbox.BottomRight() + CVector2D(1.0f, 1.0f),
     295           0 :         m_Bandbox.BottomLeft() + CVector2D(-1.0f, 1.0f),
     296           0 :         m_Bandbox.TopLeft() + CVector2D(-1.0f, -1.0f)
     297           0 :     };
     298           0 :     canvas.DrawLine(outerPoints, 1.5f, CColor(0.0f, 0.0f, 0.0f, 1.0f));
     299             : 
     300           0 :     const std::vector<CVector2D> innerPoints = {
     301           0 :         m_Bandbox.TopLeft(),
     302           0 :         m_Bandbox.TopRight(),
     303           0 :         m_Bandbox.BottomRight(),
     304           0 :         m_Bandbox.BottomLeft(),
     305           0 :         m_Bandbox.TopLeft()
     306           0 :     };
     307           0 :     canvas.DrawLine(innerPoints, 1.5f, CColor(1.0f, 1.0f, 1.0f, 1.0f));
     308           0 : }
     309             : 
     310           0 : void AtlasViewGame::SetParam(const std::wstring& name, bool value)
     311             : {
     312           0 :     if (name == L"priorities")
     313           0 :         g_Renderer.GetSceneRenderer().SetDisplayTerrainPriorities(value);
     314           0 :     else if (name == L"movetool")
     315           0 :         m_DrawMoveTool = value;
     316           0 : }
     317             : 
     318           0 : void AtlasViewGame::SetParam(const std::wstring& name, float value)
     319             : {
     320           0 :     if (name == L"movetool_x")
     321           0 :         m_MoveTool.X = value;
     322           0 :     else if (name == L"movetool_y")
     323           0 :         m_MoveTool.Y = value;
     324           0 :     else if (name == L"movetool_z")
     325           0 :         m_MoveTool.Z = value;
     326           0 : }
     327             : 
     328           0 : void AtlasViewGame::SetParam(const std::wstring& name, const std::wstring& value)
     329             : {
     330           0 :     if (name == L"passability")
     331             :     {
     332           0 :         m_DisplayPassability = CStrW(value).ToUTF8();
     333             : 
     334           0 :         CmpPtr<ICmpPathfinder> cmpPathfinder(*GetSimulation2(), SYSTEM_ENTITY);
     335           0 :         if (cmpPathfinder)
     336             :         {
     337           0 :             if (!value.empty())
     338           0 :                 cmpPathfinder->SetAtlasOverlay(true, cmpPathfinder->GetPassabilityClass(m_DisplayPassability));
     339             :             else
     340           0 :                 cmpPathfinder->SetAtlasOverlay(false);
     341             :         }
     342             :     }
     343           0 : }
     344             : 
     345           0 : const CCamera& AtlasViewGame::GetCamera() const
     346             : {
     347           0 :     return g_Game->GetView()->GetCamera();
     348             : }
     349             : 
     350           0 : void AtlasViewGame::SetCamera(const CCamera& camera)
     351             : {
     352           0 :     g_Game->GetView()->SetCamera(camera);
     353           0 : }
     354             : 
     355           0 : bool AtlasViewGame::GetSmoothFramerate() const
     356             : {
     357           0 :     if (g_Game->GetView()->GetCinema()->IsPlaying())
     358             :         return true;
     359             : 
     360           0 :     if (m_SpeedMultiplier != 0.f)
     361             :         return true;
     362             : 
     363           0 :     return m_SmoothFramerate;
     364             : }
     365             : 
     366           0 : void AtlasViewGame::SetSmoothFramerate(const bool enabled)
     367             : {
     368           0 :     m_SmoothFramerate = enabled;
     369           0 : }
     370             : 
     371           0 : void AtlasViewGame::SetSpeedMultiplier(float speed)
     372             : {
     373           0 :     m_SpeedMultiplier = speed;
     374           0 : }
     375             : 
     376           0 : void AtlasViewGame::SetTesting(bool testing)
     377             : {
     378           0 :     m_IsTesting = testing;
     379             :     // If we're testing, particles should freeze on pause (like in-game), otherwise they keep going
     380           0 :     CmpPtr<ICmpParticleManager> cmpParticleManager(*GetSimulation2(), SYSTEM_ENTITY);
     381           0 :     if (cmpParticleManager)
     382           0 :         cmpParticleManager->SetUseSimTime(m_IsTesting);
     383           0 : }
     384             : 
     385           0 : void AtlasViewGame::SaveState(const std::wstring& label)
     386             : {
     387           0 :     delete m_SavedStates[label]; // in case it already exists
     388           0 :     m_SavedStates[label] = SimState::Freeze();
     389           0 : }
     390             : 
     391           0 : void AtlasViewGame::RestoreState(const std::wstring& label)
     392             : {
     393           0 :     SimState* simState = m_SavedStates[label];
     394           0 :     if (! simState)
     395           0 :         return;
     396             : 
     397           0 :     simState->Thaw();
     398             : 
     399             :     // LOS override setting isn't part of the sim state as such we have to set
     400             :     // it again when restoring a game.
     401           0 :     CmpPtr<ICmpRangeManager> cmpRangeManager{*g_Game->GetSimulation2(), SYSTEM_ENTITY};
     402           0 :     if (cmpRangeManager)
     403           0 :         cmpRangeManager->SetLosRevealWholeMapForAll(true);
     404             : }
     405             : 
     406           0 : std::wstring AtlasViewGame::DumpState(bool binary)
     407             : {
     408           0 :     std::stringstream stream;
     409           0 :     if (binary)
     410             :     {
     411           0 :         if (! g_Game->GetSimulation2()->SerializeState(stream))
     412           0 :             return L"(internal error)";
     413             :         // We can't return raw binary data, because we want to handle it with wxJS which
     414             :         // doesn't like \0 bytes in strings, so return it as hex
     415           0 :         static const char digits[] = "0123456789abcdef";
     416           0 :         std::string str = stream.str();
     417           0 :         std::wstring ret;
     418           0 :         ret.reserve(str.length()*3);
     419           0 :         for (size_t i = 0; i < str.length(); ++i)
     420             :         {
     421           0 :             ret += digits[(unsigned char)str[i] >> 4];
     422           0 :             ret += digits[(unsigned char)str[i] & 0x0f];
     423           0 :             ret += ' ';
     424             :         }
     425           0 :         return ret;
     426           0 :     }
     427             :     else
     428             :     {
     429           0 :         if (! g_Game->GetSimulation2()->DumpDebugState(stream))
     430           0 :             return L"(internal error)";
     431           0 :         return wstring_from_utf8(stream.str());
     432             :     }
     433           0 : }
     434             : 
     435           0 : void AtlasViewGame::SetBandbox(bool visible, float x0, float y0, float x1, float y1)
     436             : {
     437           0 :     if (visible)
     438             :     {
     439             :         // Make sure corners are arranged in correct order
     440           0 :         if (x0 > x1)
     441           0 :             std::swap(x0, x1);
     442           0 :         if (y0 > y1)
     443           0 :             std::swap(y0, y1);
     444             : 
     445           0 :         const float scale{g_ConfigDB.Get("gui.scale", 0.0f)};
     446           0 :         m_Bandbox = CRect(x0 / scale, y0 / scale, x1 / scale, y1 / scale);
     447             :     }
     448             :     else
     449             :     {
     450           0 :         m_Bandbox = CRect{};
     451             :     }
     452           0 : }
     453             : 
     454             : //////////////////////////////////////////////////////////////////////////
     455             : 
     456             : AtlasViewNone* view_None = NULL;
     457             : AtlasViewGame* view_Game = NULL;
     458             : AtlasViewActor* view_Actor = NULL;
     459             : 
     460           0 : AtlasView::~AtlasView()
     461             : {
     462           0 : }
     463             : 
     464           0 : AtlasView* AtlasView::GetView(int /*eRenderView*/ view)
     465             : {
     466           0 :     switch (view)
     467             :     {
     468           0 :     case AtlasMessage::eRenderView::NONE:  return AtlasView::GetView_None();
     469           0 :     case AtlasMessage::eRenderView::GAME:  return AtlasView::GetView_Game();
     470           0 :     case AtlasMessage::eRenderView::ACTOR: return AtlasView::GetView_Actor();
     471           0 :     default:
     472           0 :         debug_warn(L"Invalid view type");
     473           0 :         return AtlasView::GetView_None();
     474             :     }
     475             : }
     476             : 
     477           0 : AtlasView* AtlasView::GetView_None()
     478             : {
     479           0 :     if (! view_None)
     480           0 :         view_None = new AtlasViewNone();
     481           0 :     return view_None;
     482             : }
     483             : 
     484           0 : AtlasViewGame* AtlasView::GetView_Game()
     485             : {
     486           0 :     if (! view_Game)
     487           0 :         view_Game = new AtlasViewGame();
     488           0 :     return view_Game;
     489             : }
     490             : 
     491           0 : AtlasViewActor* AtlasView::GetView_Actor()
     492             : {
     493           0 :     try
     494             :     {
     495           0 :         if (!view_Actor)
     496           0 :             view_Actor = new AtlasViewActor();
     497             :     }
     498           0 :     catch (const CSimulation2::LoadScriptError& e)
     499             :     {
     500           0 :         LOGERROR("{}", e.what());
     501           0 :     }
     502           0 :     return view_Actor;
     503             : }
     504             : 
     505           0 : void AtlasView::DestroyViews()
     506             : {
     507           0 :     delete view_None; view_None = NULL;
     508           0 :     delete view_Game; view_Game = NULL;
     509           0 :     delete view_Actor; view_Actor = NULL;
     510           0 : }

Generated by: LCOV version 1.16