diff --git a/source/graphics/Camera.cpp b/source/graphics/Camera.cpp index fe276e8a19..05d7009065 100644 --- a/source/graphics/Camera.cpp +++ b/source/graphics/Camera.cpp @@ -233,16 +233,17 @@ CCamera::Ray CCamera::BuildCameraRay(const int px, const int py) const return result; } -void CCamera::GetScreenCoordinates(const CVector3D& world, float& x, float& y) const +CVector2D CCamera::GetScreenCoordinates(const CVector3D& world) const { CMatrix3D transform = m_ProjMat * m_Orientation.GetInverse(); CVector4D screenspace = transform.Transform(CVector4D(world.X, world.Y, world.Z, 1.0f)); - x = screenspace.X / screenspace.W; - y = screenspace.Y / screenspace.W; + float x = screenspace.X / screenspace.W; + float y = screenspace.Y / screenspace.W; x = (x + 1) * 0.5f * m_ViewPort.m_Width; y = (1 - y) * 0.5f * m_ViewPort.m_Height; + return {x, y}; } CVector3D CCamera::GetWorldCoordinates(int px, int py, bool aboveWater) const diff --git a/source/graphics/Camera.h b/source/graphics/Camera.h index d994b00aa2..d99c718e40 100644 --- a/source/graphics/Camera.h +++ b/source/graphics/Camera.h @@ -26,6 +26,7 @@ #include "maths/BoundingBoxAligned.h" #include "maths/Frustum.h" #include "maths/Matrix3D.h" +#include "maths/Vector2D.h" #include @@ -97,7 +98,7 @@ class CCamera // General helpers that seem to fit here // Get the screen-space coordinates corresponding to a given world-space position - void GetScreenCoordinates(const CVector3D& world, float& x, float& y) const; + CVector2D GetScreenCoordinates(const CVector3D& world) const; // Get the point on the terrain corresponding to pixel (px,py) (or the mouse coordinates) // The aboveWater parameter determines whether we want to stop at the water plane or also get underwater points diff --git a/source/renderer/PatchRData.cpp b/source/renderer/PatchRData.cpp index 5a7126b464..31cfa6ef98 100644 --- a/source/renderer/PatchRData.cpp +++ b/source/renderer/PatchRData.cpp @@ -1363,10 +1363,10 @@ void CPatchRData::RenderPriorities(CTextRenderer& textRenderer) pos.X += TERRAIN_TILE_SIZE/4.f; pos.Z += TERRAIN_TILE_SIZE/4.f; - float x, y; - camera.GetScreenCoordinates(pos, x, y); + const CVector2D screenPos{camera.GetScreenCoordinates(pos)}; - textRenderer.PrintfAt(x, y, L"%d", m_Patch->m_MiniPatches[j][i].Priority); + textRenderer.PrintfAt(screenPos.X, screenPos.Y, L"%d", + m_Patch->m_MiniPatches[j][i].Priority); } } } diff --git a/source/simulation2/helpers/Selection.cpp b/source/simulation2/helpers/Selection.cpp index 13f4b6c9fa..b5cf67a49b 100644 --- a/source/simulation2/helpers/Selection.cpp +++ b/source/simulation2/helpers/Selection.cpp @@ -114,10 +114,9 @@ bool CheckEntityInRect(CEntityHandle handle, const CCamera& camera, int sx0, int return false; // Compare screen-space coordinates - float x, y; - camera.GetScreenCoordinates(position, x, y); - int ix = (int)x; - int iy = (int)y; + const CVector2D screenPos{camera.GetScreenCoordinates(position)}; + int ix = static_cast(screenPos.X); + int iy = static_cast(screenPos.Y); return sx0 <= ix && ix <= sx1 && sy0 <= iy && iy <= sy1; } diff --git a/source/soundmanager/scripting/SoundGroup.cpp b/source/soundmanager/scripting/SoundGroup.cpp index cef2c4899f..ecb8995828 100644 --- a/source/soundmanager/scripting/SoundGroup.cpp +++ b/source/soundmanager/scripting/SoundGroup.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2024 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -147,38 +147,37 @@ float CSoundGroup::RadiansOffCenter(const CVector3D& position, bool& onScreen, f const float yBufferSize = 15.f; const float radianCap = m_MaxStereoAngle; - float x, y; - g_Game->GetView()->GetCamera()->GetScreenCoordinates(position, x, y); + const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(position)}; onScreen = true; float answer = 0.f; - if (x < -xBufferSize) + if (screenPos.X < -xBufferSize) { onScreen = false; answer = -radianCap; } - else if (x > screenWidth + xBufferSize) + else if (screenPos.X > screenWidth + xBufferSize) { onScreen = false; answer = radianCap; } else { - if (x < 0 || x > screenWidth) + if (screenPos.X < 0 || screenPos.X > screenWidth) itemRollOff = MAX_ROLLOFF; - answer = radianCap * (x * 2 / screenWidth - 1); + answer = radianCap * (screenPos.X * 2 / screenWidth - 1); } - if (y < -yBufferSize) + if (screenPos.X < -yBufferSize) { onScreen = false; } - else if (y > screenHeight + yBufferSize) + else if (screenPos.Y > screenHeight + yBufferSize) { onScreen = false; } - else if (y < 0 || y > screenHeight) + else if (screenPos.Y < 0 || screenPos.Y > screenHeight) { itemRollOff = MAX_ROLLOFF; } diff --git a/source/tools/atlas/GameInterface/Handlers/CinemaHandler.cpp b/source/tools/atlas/GameInterface/Handlers/CinemaHandler.cpp index 0326993b74..250c9e6c2c 100644 --- a/source/tools/atlas/GameInterface/Handlers/CinemaHandler.cpp +++ b/source/tools/atlas/GameInterface/Handlers/CinemaHandler.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2024 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -292,9 +292,8 @@ static CVector3D GetNearestPointToScreenCoords(const CVector3D& base, const CVec float delta = (upper - lower) / 3.0; float middle1 = lower + delta, middle2 = lower + 2.0f * delta; CVector3D p1 = base + dir * middle1, p2 = base + dir * middle2; - CVector2D s1, s2; - g_Game->GetView()->GetCamera()->GetScreenCoordinates(p1, s1.X, s1.Y); - g_Game->GetView()->GetCamera()->GetScreenCoordinates(p2, s2.X, s2.Y); + const CVector2D s1{g_Game->GetView()->GetCamera()->GetScreenCoordinates(p1)}; + const CVector2D s2{g_Game->GetView()->GetCamera()->GetScreenCoordinates(p2)}; if ((s1 - screen).Length() < (s2 - screen).Length()) upper = middle2; else @@ -341,7 +340,7 @@ BEGIN_COMMAND(AddPathNode) fixed::FromFloat(focus.Z) ); spline.InsertNode(index + 1, target, CFixedVector3D(), fixed::FromInt(1)); - + spline.BuildSpline(); cmpCinemaManager->DeletePath(name); cmpCinemaManager->AddPath(CCinemaPath(data, positionSpline, targetSpline)); @@ -462,9 +461,8 @@ static bool isPathNodePicked(const TNSpline& spline, const CVector2D& cursor, At data.Position.Y.ToFloat(), data.Position.Z.ToFloat() ); - CVector2D screen_pos; - g_Game->GetView()->GetCamera()->GetScreenCoordinates(position, screen_pos.X, screen_pos.Y); - if ((screen_pos - cursor).Length() < MINIMAL_SCREEN_DISTANCE) + const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(position)}; + if ((screenPos - cursor).Length() < MINIMAL_SCREEN_DISTANCE) { node.index = i; node.targetNode = targetNode; @@ -508,9 +506,8 @@ QUERYHANDLER(PickPathNode) static bool isAxisPicked(const CVector3D& base, const CVector3D& direction, float length, const CVector2D& cursor) { CVector3D position = GetNearestPointToScreenCoords(base, direction, cursor, 0, length); - CVector2D screen_position; - g_Game->GetView()->GetCamera()->GetScreenCoordinates(position, screen_position.X, screen_position.Y); - return (cursor - screen_position).Length() < MINIMAL_SCREEN_DISTANCE; + const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(position)}; + return (cursor - screenPos).Length() < MINIMAL_SCREEN_DISTANCE; } QUERYHANDLER(PickAxis) diff --git a/source/tools/atlas/GameInterface/Handlers/ObjectHandlers.cpp b/source/tools/atlas/GameInterface/Handlers/ObjectHandlers.cpp index 06ea791c2a..67394ab1e5 100644 --- a/source/tools/atlas/GameInterface/Handlers/ObjectHandlers.cpp +++ b/source/tools/atlas/GameInterface/Handlers/ObjectHandlers.cpp @@ -667,11 +667,10 @@ QUERYHANDLER(PickObject) CFixedVector3D fixed = cmpPosition->GetPosition(); CVector3D centre = CVector3D(fixed.X.ToFloat(), fixed.Y.ToFloat(), fixed.Z.ToFloat()); - float cx, cy; - g_Game->GetView()->GetCamera()->GetScreenCoordinates(centre, cx, cy); + const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates(centre)}; - msg->offsetx = (int)(cx - x); - msg->offsety = (int)(cy - y); + msg->offsetx = static_cast(screenPos.X - x); + msg->offsety = static_cast(screenPos.Y - y); } } } diff --git a/source/tools/atlas/GameInterface/Misc.cpp b/source/tools/atlas/GameInterface/Misc.cpp index e8f619fa8f..f42cf2cc38 100644 --- a/source/tools/atlas/GameInterface/Misc.cpp +++ b/source/tools/atlas/GameInterface/Misc.cpp @@ -78,9 +78,13 @@ void AtlasMessage::Position::GetScreenSpace(float& x, float& y) const switch (type) { case 0: - g_Game->GetView()->GetCamera()->GetScreenCoordinates(CVector3D(type0.x, type0.y, type0.x), x, y); + { + const CVector2D screenPos{g_Game->GetView()->GetCamera()->GetScreenCoordinates( + CVector3D{type0.x, type0.y, type0.x})}; + x = screenPos.X; + y = screenPos.Y; break; - + } case 1: x = type1.x; y = type1.y;