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 "GameView.h"
21 :
22 : #include "graphics/Camera.h"
23 : #include "graphics/CameraController.h"
24 : #include "graphics/CinemaManager.h"
25 : #include "graphics/ColladaManager.h"
26 : #include "graphics/ICameraController.h"
27 : #include "graphics/LOSTexture.h"
28 : #include "graphics/MeshManager.h"
29 : #include "graphics/MiniMapTexture.h"
30 : #include "graphics/ObjectManager.h"
31 : #include "graphics/Patch.h"
32 : #include "graphics/SkeletonAnimManager.h"
33 : #include "graphics/Terrain.h"
34 : #include "graphics/TerrainTextureManager.h"
35 : #include "graphics/TerritoryTexture.h"
36 : #include "graphics/Unit.h"
37 : #include "graphics/UnitManager.h"
38 : #include "lib/external_libraries/libsdl.h"
39 : #include "lib/posix/posix_types.h"
40 : #include "lib/timer.h"
41 : #include "lobby/XmppClient.h"
42 : #include "maths/BoundingBoxAligned.h"
43 : #include "maths/Frustum.h"
44 : #include "maths/Vector3D.h"
45 : #include "ps/Filesystem.h"
46 : #include "ps/Game.h"
47 : #include "ps/Globals.h"
48 : #include "ps/Hotkey.h"
49 : #include "ps/Input.h"
50 : #include "ps/Loader.h"
51 : #include "ps/Profile.h"
52 : #include "ps/TouchInput.h"
53 : #include "ps/VideoMode.h"
54 : #include "ps/World.h"
55 : #include "renderer/Renderer.h"
56 : #include "renderer/SceneRenderer.h"
57 : #include "renderer/WaterManager.h"
58 : #include "simulation2/Simulation2.h"
59 :
60 : #include <SDL_events.h>
61 : #include <memory>
62 : #include <string>
63 :
64 : namespace Renderer::Backend { class IDevice; }
65 : namespace Renderer::Backend { class IDeviceCommandContext; }
66 :
67 : class CGameViewImpl
68 : {
69 : NONCOPYABLE(CGameViewImpl);
70 : public:
71 0 : CGameViewImpl(Renderer::Backend::IDevice* device, CGame* game)
72 0 : : Game(game),
73 0 : ColladaManager(g_VFS), MeshManager(ColladaManager), SkeletonAnimManager(ColladaManager),
74 0 : ObjectManager(MeshManager, SkeletonAnimManager, *game->GetSimulation2()),
75 0 : LOSTexture(*game->GetSimulation2()),
76 0 : TerritoryTexture(*game->GetSimulation2()),
77 0 : MiniMapTexture(device, *game->GetSimulation2()),
78 0 : ViewCamera(),
79 0 : CullCamera(),
80 0 : LockCullCamera(false),
81 0 : Culling(true),
82 0 : CameraController(new CCameraController(ViewCamera))
83 : {
84 0 : }
85 :
86 : CGame* Game;
87 : CColladaManager ColladaManager;
88 : CMeshManager MeshManager;
89 : CSkeletonAnimManager SkeletonAnimManager;
90 : CObjectManager ObjectManager;
91 : CLOSTexture LOSTexture;
92 : CTerritoryTexture TerritoryTexture;
93 : CMiniMapTexture MiniMapTexture;
94 :
95 : /**
96 : * this camera controls the eye position when rendering
97 : */
98 : CCamera ViewCamera;
99 :
100 : /**
101 : * this camera controls the frustum that is used for culling
102 : * and shadow calculations
103 : *
104 : * Note that all code that works with camera movements should only change
105 : * m_ViewCamera. The render functions automatically sync the cull camera to
106 : * the view camera depending on the value of m_LockCullCamera.
107 : */
108 : CCamera CullCamera;
109 :
110 : /**
111 : * When @c true, the cull camera is locked in place.
112 : * When @c false, the cull camera follows the view camera.
113 : *
114 : * Exposed to JS as gameView.lockCullCamera
115 : */
116 : bool LockCullCamera;
117 :
118 : /**
119 : * When @c true, culling is enabled so that only models that have a chance of
120 : * being visible are sent to the renderer.
121 : * Otherwise, the entire world is sent to the renderer.
122 : *
123 : * Exposed to JS as gameView.culling
124 : */
125 : bool Culling;
126 :
127 : CCinemaManager CinemaManager;
128 :
129 : /**
130 : * Controller of the view's camera. We use a std::unique_ptr for an easy
131 : * on the fly replacement. It's guaranteed that the pointer is never nulllptr.
132 : */
133 : std::unique_ptr<ICameraController> CameraController;
134 :
135 : struct InputHandler
136 : {
137 : CGameViewImpl& gameView;
138 : Input::Reaction operator()(const SDL_Event& ev);
139 : };
140 : Input::Handler<InputHandler> m_InputHandler{g_VideoMode.m_InputManager, Input::Slot::GAME_VIEW,
141 : {*this}};
142 : };
143 :
144 : #define IMPLEMENT_BOOLEAN_SETTING(NAME) \
145 : bool CGameView::Get##NAME##Enabled() const \
146 : { \
147 : return m->NAME; \
148 : } \
149 : \
150 : void CGameView::Set##NAME##Enabled(bool Enabled) \
151 : { \
152 : m->NAME = Enabled; \
153 : }
154 :
155 0 : IMPLEMENT_BOOLEAN_SETTING(Culling);
156 0 : IMPLEMENT_BOOLEAN_SETTING(LockCullCamera);
157 :
158 0 : bool CGameView::GetConstrainCameraEnabled() const
159 : {
160 0 : return m->CameraController->GetConstrainCamera();
161 : }
162 :
163 0 : void CGameView::SetConstrainCameraEnabled(bool enabled)
164 : {
165 0 : m->CameraController->SetConstrainCamera(enabled);
166 0 : }
167 :
168 : #undef IMPLEMENT_BOOLEAN_SETTING
169 :
170 0 : CGameView::CGameView(Renderer::Backend::IDevice* device, CGame *pGame):
171 0 : m(new CGameViewImpl(device, pGame))
172 : {
173 0 : m->CullCamera = m->ViewCamera;
174 0 : g_Renderer.GetSceneRenderer().SetSceneCamera(m->ViewCamera, m->CullCamera);
175 0 : }
176 :
177 0 : CGameView::~CGameView()
178 : {
179 0 : UnloadResources();
180 :
181 0 : delete m;
182 0 : }
183 :
184 0 : void CGameView::SetViewport(const SViewPort& vp)
185 : {
186 0 : m->CameraController->SetViewport(vp);
187 0 : }
188 :
189 0 : CObjectManager& CGameView::GetObjectManager()
190 : {
191 0 : return m->ObjectManager;
192 : }
193 :
194 0 : const CCamera& CGameView::GetCamera() const
195 : {
196 0 : return m->ViewCamera;
197 : }
198 :
199 0 : const CCamera& CGameView::GetCullCamera() const
200 : {
201 0 : return m->CullCamera;
202 : }
203 :
204 0 : void CGameView::SetCamera(const CCamera& camera)
205 : {
206 0 : m->ViewCamera = camera;
207 :
208 0 : UpdateCullCamera();
209 0 : }
210 :
211 0 : CCinemaManager* CGameView::GetCinema()
212 : {
213 0 : return &m->CinemaManager;
214 : }
215 :
216 0 : CLOSTexture& CGameView::GetLOSTexture()
217 : {
218 0 : return m->LOSTexture;
219 : }
220 :
221 0 : CTerritoryTexture& CGameView::GetTerritoryTexture()
222 : {
223 0 : return m->TerritoryTexture;
224 : }
225 :
226 0 : CMiniMapTexture& CGameView::GetMiniMapTexture()
227 : {
228 0 : return m->MiniMapTexture;
229 : }
230 :
231 0 : void CGameView::RegisterInit()
232 : {
233 : // CGameView init
234 0 : PS::Loader::Register(std::bind_front([](ICameraController* cameraController) -> PS::Loader::Task
235 : {
236 : cameraController->LoadConfig();
237 : co_return 0;
238 0 : }, m->CameraController.get()), L"CGameView init", 1);
239 :
240 0 : PS::Loader::Register([]
241 : {
242 0 : return g_TexMan.LoadTerrainTextures();
243 : }, L"TerrainTextures", 61);
244 0 : }
245 :
246 0 : void CGameView::EnumerateSceneObjects()
247 : {
248 0 : g_Renderer.GetSceneRenderer().EnumerateSceneObjects(*this);
249 0 : }
250 :
251 : ///////////////////////////////////////////////////////////
252 : // This callback is part of the Scene interface
253 : // Submit all objects visible in the given frustum
254 0 : void CGameView::EnumerateObjects(const CFrustum& frustum, SceneCollector* c)
255 : {
256 0 : {
257 0 : PROFILE3("submit terrain");
258 :
259 0 : const CTerrain& terrain = m->Game->GetWorld()->GetTerrain();
260 0 : float waterHeight = g_Renderer.GetSceneRenderer().GetWaterManager().m_WaterHeight + 0.001f;
261 0 : const ssize_t patchesPerSide = terrain.GetPatchesPerSide();
262 :
263 : // find out which patches will be drawn
264 0 : for (ssize_t j=0; j<patchesPerSide; ++j)
265 : {
266 0 : for (ssize_t i=0; i<patchesPerSide; ++i)
267 : {
268 0 : CPatch* const patch = terrain.GetPatch(i,j); // can't fail
269 :
270 : // If the patch is underwater, calculate a bounding box that also contains the water plane
271 0 : CBoundingBoxAligned bounds = patch->GetWorldBounds();
272 0 : if(bounds[1].Y < waterHeight)
273 0 : bounds[1].Y = waterHeight;
274 :
275 0 : if (!m->Culling || frustum.IsBoxVisible(bounds))
276 0 : c->Submit(patch);
277 : }
278 : }
279 0 : }
280 :
281 0 : m->Game->GetSimulation2()->RenderSubmit(*c, frustum, m->Culling);
282 0 : }
283 :
284 0 : void CGameView::UnloadResources()
285 : {
286 0 : g_TexMan.UnloadTerrainTextures();
287 0 : g_Renderer.GetSceneRenderer().GetWaterManager().UnloadWaterTextures();
288 0 : }
289 :
290 0 : void CGameView::Update(const float deltaRealTime)
291 : {
292 0 : m->MiniMapTexture.Update(deltaRealTime);
293 :
294 0 : UpdateCamera(deltaRealTime);
295 0 : UpdateCullCamera();
296 0 : }
297 :
298 0 : void CGameView::UpdateCamera(const float deltaRealTime)
299 : {
300 0 : m->CinemaManager.Update(deltaRealTime, m->ViewCamera);
301 0 : if (m->CinemaManager.IsPlaying())
302 : return;
303 : // If camera movement is being handled by the touch-input system,
304 : // then we should stop to avoid conflicting with it
305 0 : if (g_TouchInput.IsEnabled())
306 : return;
307 :
308 0 : if (!g_app_has_focus)
309 : return;
310 :
311 0 : m->CameraController->Update(deltaRealTime);
312 : }
313 :
314 0 : void CGameView::UpdateCullCamera()
315 : {
316 0 : if (!m->LockCullCamera)
317 0 : m->CullCamera = m->ViewCamera;
318 0 : }
319 :
320 0 : CVector3D CGameView::GetCameraPivot() const
321 : {
322 0 : return m->CameraController->GetCameraPivot();
323 : }
324 :
325 0 : CVector3D CGameView::GetCameraPosition() const
326 : {
327 0 : return m->CameraController->GetCameraPosition();
328 : }
329 :
330 0 : CVector3D CGameView::GetCameraRotation() const
331 : {
332 0 : return m->CameraController->GetCameraRotation();
333 : }
334 :
335 0 : float CGameView::GetCameraZoom() const
336 : {
337 0 : return m->CameraController->GetCameraZoom();
338 : }
339 :
340 0 : void CGameView::SetCamera(const CVector3D& pos, float rotX, float rotY, float zoom)
341 : {
342 0 : m->CameraController->SetCamera(pos, rotX, rotY, zoom);
343 :
344 0 : UpdateCullCamera();
345 0 : }
346 :
347 0 : void CGameView::MoveCameraTarget(const CVector3D& target)
348 : {
349 0 : m->CameraController->MoveCameraTarget(target);
350 0 : }
351 :
352 0 : void CGameView::LoadCameraConfig()
353 : {
354 0 : m->CameraController->LoadConfig();
355 0 : }
356 :
357 0 : void CGameView::ResetCameraTarget(const CVector3D& target)
358 : {
359 0 : m->CameraController->ResetCameraTarget(target);
360 0 : }
361 :
362 0 : void CGameView::FollowEntity(entity_id_t entity, bool firstPerson)
363 : {
364 0 : m->CameraController->FollowEntity(entity, firstPerson);
365 0 : }
366 :
367 0 : entity_id_t CGameView::GetFollowedEntity()
368 : {
369 0 : return m->CameraController->GetFollowedEntity();
370 : }
371 :
372 0 : Input::Reaction CGameViewImpl::InputHandler::operator()(const SDL_Event& ev)
373 : {
374 : // put any events that must be processed even if inactive here
375 0 : if (!g_app_has_focus || !g_Game || !g_Game->IsGameStarted() || g_Game->GetView()->GetCinema()->IsPlaying())
376 0 : return Input::Reaction::PASS;
377 :
378 0 : switch(ev.type)
379 : {
380 0 : case SDL_HOTKEYPRESS:
381 0 : {
382 0 : std::string hotkey = static_cast<const char*>(ev.user.data1);
383 0 : CSceneRenderer& sceneRenderer = g_Renderer.GetSceneRenderer();
384 0 : if (hotkey == "wireframe")
385 : {
386 0 : if (g_XmppClient && g_rankedGame == true)
387 : break;
388 0 : else if (sceneRenderer.GetModelRenderMode() == SOLID)
389 : {
390 0 : sceneRenderer.SetTerrainRenderMode(EDGED_FACES);
391 0 : sceneRenderer.SetWaterRenderMode(EDGED_FACES);
392 0 : sceneRenderer.SetModelRenderMode(EDGED_FACES);
393 0 : sceneRenderer.SetOverlayRenderMode(EDGED_FACES);
394 : }
395 0 : else if (sceneRenderer.GetModelRenderMode() == EDGED_FACES)
396 : {
397 0 : sceneRenderer.SetTerrainRenderMode(WIREFRAME);
398 0 : sceneRenderer.SetWaterRenderMode(WIREFRAME);
399 0 : sceneRenderer.SetModelRenderMode(WIREFRAME);
400 0 : sceneRenderer.SetOverlayRenderMode(WIREFRAME);
401 : }
402 : else
403 : {
404 0 : sceneRenderer.SetTerrainRenderMode(SOLID);
405 0 : sceneRenderer.SetWaterRenderMode(SOLID);
406 0 : sceneRenderer.SetModelRenderMode(SOLID);
407 0 : sceneRenderer.SetOverlayRenderMode(SOLID);
408 : }
409 0 : return Input::Reaction::HANDLED;
410 : }
411 0 : }
412 : }
413 :
414 0 : return gameView.CameraController->HandleEvent(ev);
415 : }
|