Remove global g_xres and x_yres

Instead query g_VideoMode where needed. As both g_xres and g_yres aren't
global anymore remove static from UpdateRenderer. While at it use more
desicriptive names: GetXRes -> GetWindowWidth, GetYRes ->
GetWindowHeight.

This reduces the amount of variables tracking width and height by one
each.

Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
This commit is contained in:
Ralph Sennhauser
2026-07-07 19:03:08 +02:00
parent b588b62304
commit 99bbc24265
17 changed files with 50 additions and 63 deletions
+5 -6
View File
@@ -32,6 +32,7 @@
#include "ps/Game.h"
#include "ps/Globals.h"
#include "ps/Hotkey.h"
#include "ps/VideoMode.h"
#include "ps/World.h"
#include "renderer/Renderer.h"
#include "renderer/SceneRenderer.h"
@@ -47,8 +48,6 @@
#include <functional>
#include <string>
extern int g_xres, g_yres;
// Maximum distance outside the edge of the map that the camera's
// focus point can be moved
static const float CAMERA_EDGE_MARGIN = 2.0f * TERRAIN_TILE_SIZE;
@@ -105,8 +104,8 @@ CCameraController::CCameraController(CCamera& camera)
SViewPort vp;
vp.m_X = 0;
vp.m_Y = 0;
vp.m_Width = g_xres;
vp.m_Height = g_yres;
vp.m_Width = g_VideoMode.GetWindowWidth();
vp.m_Height = g_VideoMode.GetWindowHeight();
m_Camera.SetViewPort(vp);
SetCameraProjection();
@@ -206,12 +205,12 @@ void CCameraController::Update(const float deltaRealTime)
if (g_mouse_active && m_ViewScrollMouseDetectDistance > 0)
{
if (g_mouse_x >= g_xres - m_ViewScrollMouseDetectDistance && g_mouse_x < g_xres)
if (g_mouse_x >= g_VideoMode.GetWindowWidth() - m_ViewScrollMouseDetectDistance && g_mouse_x < g_VideoMode.GetWindowWidth())
moveRightward += m_ViewScrollSpeed * deltaRealTime;
else if (g_mouse_x < m_ViewScrollMouseDetectDistance && g_mouse_x >= 0)
moveRightward -= m_ViewScrollSpeed * deltaRealTime;
if (g_mouse_y >= g_yres - m_ViewScrollMouseDetectDistance && g_mouse_y < g_yres)
if (g_mouse_y >= g_VideoMode.GetWindowHeight() - m_ViewScrollMouseDetectDistance && g_mouse_y < g_VideoMode.GetWindowHeight())
moveForward -= m_ViewScrollSpeed * deltaRealTime;
else if (g_mouse_y < m_ViewScrollMouseDetectDistance && g_mouse_y >= 0)
moveForward += m_ViewScrollSpeed * deltaRealTime;
+1 -1
View File
@@ -461,7 +461,7 @@ IGUIObject* CGUI::FindObjectUnderMouse()
CSize2D CGUI::GetWindowSize() const
{
return CSize2D{static_cast<float>(g_xres) / g_VideoMode.GetScale(), static_cast<float>(g_yres) / g_VideoMode.GetScale() };
return CSize2D{static_cast<float>(g_VideoMode.GetWindowWidth()) / g_VideoMode.GetScale(), static_cast<float>(g_VideoMode.GetWindowHeight()) / g_VideoMode.GetScale() };
}
void CGUI::SendFocusMessage(EGUIMessageType msgType)
+1 -3
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -34,8 +34,6 @@
#include <optional>
#include <utility>
extern int g_xres, g_yres;
// TODO Gee: CRect => CPoint ?
void SGenerateTextImage::SetupSpriteCall(
const bool left, CGUIText::SSpriteCall& spriteCall, const float width, const float y,
+2 -2
View File
@@ -152,13 +152,13 @@ void CConsole::Init()
m_HistoryFile = L"config/console.txt";
LoadHistory();
UpdateScreenSize(g_xres, g_yres);
UpdateScreenSize(g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight());
// Calculate and store the line spacing.
const CFontMetrics font{CStrIntern(m_consoleFont)};
m_FontHeight = font.GetHeight();
m_FontWidth = font.GetCharacterWidth(L'C');
m_CharsPerPage = static_cast<size_t>(g_xres / m_FontWidth);
m_CharsPerPage = static_cast<size_t>(g_VideoMode.GetWindowWidth() / m_FontWidth);
// Fonts constains two dimensions: the full height, and the cap height.
// We are adding some offset to move the text up a bit, so it looks better in the console.
m_FontOffset = font.GetCapHeight() / 2.f;
-2
View File
@@ -30,8 +30,6 @@
// (these variables are documented in the header.)
bool g_PauseOnFocusLoss = false;
int g_xres, g_yres;
bool g_Quickstart = false;
bool g_DisableAudio = false;
+1 -3
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -21,8 +21,6 @@
// flag to pause the game on window focus loss
extern bool g_PauseOnFocusLoss;
extern int g_xres, g_yres;
extern bool g_Quickstart;
extern bool g_DisableAudio;
+1 -1
View File
@@ -285,7 +285,7 @@ void WriteSystemInfo(Renderer::Backend::IDevice* device, const utsname& un)
// graphics
fprintf(f, "Video Card : %s\n", device->GetName().c_str());
fprintf(f, "Video Driver : %s\n", device->GetDriverInformation().c_str());
fprintf(f, "Video Mode : %dx%d:%d\n", g_VideoMode.GetXRes(), g_VideoMode.GetYRes(), g_VideoMode.GetBPP());
fprintf(f, "Video Mode : %dx%d:%d\n", g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight(), g_VideoMode.GetBPP());
#if CONFIG2_AUDIO
if (g_SoundManager)
+4 -5
View File
@@ -27,6 +27,7 @@
#include "lib/timer.h"
#include "maths/Matrix3D.h"
#include "ps/Game.h"
#include "ps/VideoMode.h"
#include <SDL_events.h>
#include <SDL_mouse.h>
@@ -39,8 +40,6 @@
// Same with right-click for finger 1.
#define EMULATE_FINGERS_WITH_MOUSE 0
extern int g_xres, g_yres;
// NOTE: All this code is currently just a basic prototype for testing;
// it might need significant redesigning for proper usage.
@@ -279,11 +278,11 @@ Input::Reaction CTouchInput::HandleEvent(const SDL_Event& ev)
ev.tfinger.x, ev.tfinger.y, ev.tfinger.dx, ev.tfinger.dy, ev.tfinger.pressure);
if (ev.type == SDL_FINGERDOWN)
OnFingerDown(ev.tfinger.fingerId, g_xres * ev.tfinger.x, g_yres * ev.tfinger.y);
OnFingerDown(ev.tfinger.fingerId, g_VideoMode.GetWindowWidth() * ev.tfinger.x, g_VideoMode.GetWindowHeight() * ev.tfinger.y);
else if (ev.type == SDL_FINGERUP)
OnFingerUp(ev.tfinger.fingerId, g_xres * ev.tfinger.x, g_yres * ev.tfinger.y);
OnFingerUp(ev.tfinger.fingerId, g_VideoMode.GetWindowWidth() * ev.tfinger.x, g_VideoMode.GetWindowHeight() * ev.tfinger.y);
else if (ev.type == SDL_FINGERMOTION)
OnFingerMotion(ev.tfinger.fingerId, g_xres * ev.tfinger.x, g_yres * ev.tfinger.y);
OnFingerMotion(ev.tfinger.fingerId, g_VideoMode.GetWindowWidth() * ev.tfinger.x, g_VideoMode.GetWindowHeight() * ev.tfinger.y);
return Input::Reaction::HANDLED;
}
}
+6 -9
View File
@@ -550,9 +550,6 @@ bool CVideoMode::SetVideoMode(int w, int h, int bpp, bool fullscreen)
m_IsFullscreen = fullscreen;
g_xres = m_CurrentW;
g_yres = m_CurrentH;
return true;
}
@@ -733,9 +730,9 @@ void CVideoMode::RecreateSwapChain()
if (m_BackendDevice)
{
char nameBuffer[64];
snprintf(nameBuffer, std::size(nameBuffer), "SwapChain: %dx%d", g_xres, g_yres);
snprintf(nameBuffer, std::size(nameBuffer), "SwapChain: %dx%d", m_CurrentW, m_CurrentH);
m_SwapChain = m_BackendDevice->CreateSwapChain(
nameBuffer, m_Window, g_xres, g_yres, m_ConfigVSync, std::move(m_SwapChain));
nameBuffer, m_Window, m_CurrentW, m_CurrentH, m_ConfigVSync, std::move(m_SwapChain));
}
}
@@ -876,8 +873,8 @@ void CVideoMode::UpdateRenderer(int w, int h)
if (w < 2) w = 2; // avoid GL errors caused by invalid sizes
if (h < 2) h = 2;
g_xres = w;
g_yres = h;
m_CurrentW = w;
m_CurrentH = h;
SViewPort vp = { 0, 0, w, h };
@@ -915,13 +912,13 @@ int CVideoMode::GetBestBPP()
return 32;
}
int CVideoMode::GetXRes() const
int CVideoMode::GetWindowWidth() const
{
ENSURE(m_IsInitialised);
return m_CurrentW;
}
int CVideoMode::GetYRes() const
int CVideoMode::GetWindowHeight() const
{
ENSURE(m_IsInitialised);
return m_CurrentH;
+3 -3
View File
@@ -88,10 +88,10 @@ public:
* This should be called after the GL context has been resized.
* This can also be used when the GL context is managed externally, not via SDL.
*/
static void UpdateRenderer(int w, int h);
void UpdateRenderer(int windowWidth, int windowHeight);
int GetXRes() const;
int GetYRes() const;
int GetWindowWidth() const;
int GetWindowHeight() const;
int GetBPP() const;
bool IsVSyncEnabled() const;
+7 -7
View File
@@ -447,7 +447,7 @@ CRenderer::CRenderer(Renderer::Backend::IDevice* device)
// Create terrain related stuff.
new CTerrainTextureManager(device);
Open(g_xres, g_yres);
Open(g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight());
// Setup lighting environment. Since the Renderer accesses the
// lighting environment through a pointer, this has to be done before
@@ -697,7 +697,7 @@ void CRenderer::RenderFrameImpl(
void CRenderer::RenderFrame2D(const bool renderGUI, const bool renderLogger)
{
CCanvas2D canvas(g_xres, g_yres, g_VideoMode.GetScale(), m->deviceCommandContext.get());
CCanvas2D canvas(g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight(), g_VideoMode.GetScale(), m->deviceCommandContext.get());
m->sceneRenderer.RenderTextOverlays(canvas);
@@ -745,7 +745,7 @@ void CRenderer::RenderScreenShot(const bool needsPresent)
VfsPath filename;
vfs::NextNumberedFilename(g_VFS, filenameFormat, g_NextScreenShotNumber, filename);
const size_t width = static_cast<size_t>(g_xres), height = static_cast<size_t>(g_yres);
const size_t width = static_cast<size_t>(g_VideoMode.GetWindowWidth()), height = static_cast<size_t>(g_VideoMode.GetWindowHeight());
const size_t bpp = 24;
const size_t img_size = width * height * bpp / 8;
@@ -804,12 +804,12 @@ void CRenderer::RenderBigScreenShot(const bool needsPresent)
return;
}
if (g_xres < tileWidth && g_yres < tileHeight)
if (g_VideoMode.GetWindowWidth() < tileWidth && g_VideoMode.GetWindowHeight() < tileHeight)
{
LOGWARNING(
"The window size is too small for a big screenshot, increase the"
" window size %dx%d or decrease the tile size %dx%d",
g_xres, g_yres, tileWidth, tileHeight);
g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight(), tileWidth, tileHeight);
return;
}
@@ -897,8 +897,8 @@ void CRenderer::RenderBigScreenShot(const bool needsPresent)
// Restore the viewport settings
{
g_Renderer.Resize(g_xres, g_yres);
SViewPort vp = { 0, 0, g_xres, g_yres };
g_Renderer.Resize(g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight());
SViewPort vp = { 0, 0, g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight() };
g_Game->GetView()->SetViewport(vp);
g_Game->GetView()->SetCamera(oldCamera);
}
+4 -5
View File
@@ -38,6 +38,7 @@
#include "ps/CStrIntern.h"
#include "ps/CStrInternStatic.h"
#include "ps/Profile.h"
#include "ps/VideoMode.h"
#include "renderer/DebugRenderer.h"
#include "renderer/Renderer.h"
#include "renderer/Scene.h"
@@ -53,8 +54,6 @@
#include <iterator>
#include <limits>
extern int g_xres, g_yres;
// For debugging
static const bool g_DisablePreciseIntersections = false;
@@ -258,9 +257,9 @@ void SilhouetteRenderer::ComputeSubmissions(const CCamera& camera)
#if 0
// For debugging ray-patch intersections - casts a ton of rays and draws
// a sphere where they intersect
for (int y = 0; y < g_yres; y += 8)
for (int y = 0; y < g_VideoMode.GetWindowHeight(); y += 8)
{
for (int x = 0; x < g_xres; x += 8)
for (int x = 0; x < g_VideoMode.GetWindowWidth(); x += 8)
{
SOverlaySphere sphere;
sphere.m_Color = CColor(1, 0, 0, 1);
@@ -483,7 +482,7 @@ void SilhouetteRenderer::RenderDebugOverlays(
CMatrix3D m;
m.SetIdentity();
m.Scale(1.0f, -1.f, 1.0f);
m.Translate(0.0f, (float)g_yres, -1000.0f);
m.Translate(0.0f, static_cast<float>(g_VideoMode.GetWindowHeight()), -1000.0f);
CMatrix3D proj;
proj.SetOrtho(0.f, g_MaxCoord, 0.f, g_MaxCoord, -1.f, 1000.f);
@@ -30,6 +30,7 @@
#include "ps/Game.h"
#include "ps/GameSetup/Config.h"
#include "ps/Pyrogenesis.h"
#include "ps/VideoMode.h"
#include "scriptinterface/FunctionWrapper.h"
#include "scriptinterface/Object.h"
#include "scriptinterface/Request.h"
@@ -118,12 +119,12 @@ std::vector<entity_id_t> PickPlayerEntitiesInRect(int x0, int y0, int x1, int y1
std::vector<entity_id_t> PickPlayerEntitiesOnScreen(int player)
{
return EntitySelection::PickEntitiesInRect(*g_Game->GetSimulation2(), g_Game->GetView()->GetCamera(), 0, 0, g_xres, g_yres, player, false);
return EntitySelection::PickEntitiesInRect(*g_Game->GetSimulation2(), g_Game->GetView()->GetCamera(), 0, 0, g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight(), player, false);
}
std::vector<entity_id_t> PickNonGaiaEntitiesOnScreen()
{
return EntitySelection::PickNonGaiaEntitiesInRect(*g_Game->GetSimulation2(), g_Game->GetView()->GetCamera(), 0, 0, g_xres, g_yres, false);
return EntitySelection::PickNonGaiaEntitiesInRect(*g_Game->GetSimulation2(), g_Game->GetView()->GetCamera(), 0, 0, g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight(), false);
}
std::vector<entity_id_t> GetEntitiesWithStaticObstructionOnScreen()
@@ -136,7 +137,9 @@ std::vector<entity_id_t> GetEntitiesWithStaticObstructionOnScreen()
return cmpObstruction->GetObstructionType() == ICmpObstruction::STATIC;
}
};
return EntitySelection::GetEntitiesWithComponentInRect<StaticObstructionFilter>(*g_Game->GetSimulation2(), IID_Obstruction, g_Game->GetView()->GetCamera(), 0, 0, g_xres, g_yres);
return EntitySelection::GetEntitiesWithComponentInRect<StaticObstructionFilter>(*g_Game->GetSimulation2(),
IID_Obstruction, g_Game->GetView()->GetCamera(), 0, 0,
g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight());
}
JS::Value GetEdgesOfStaticObstructionsOnScreenNearTo(const Script::Interface& scriptInterface, entity_pos_t x, entity_pos_t z)
@@ -81,8 +81,6 @@
class CTerrainTextureEntry;
extern int g_xres, g_yres;
struct ActorViewerImpl : public Scene
{
NONCOPYABLE(ActorViewerImpl);
@@ -578,7 +576,7 @@ void ActorViewer::Render()
sceneRenderer.RenderSceneOverlays(deviceCommandContext);
{
CCanvas2D canvas(g_xres, g_yres, g_VideoMode.GetScale(), deviceCommandContext);
CCanvas2D canvas(g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight(), g_VideoMode.GetScale(), deviceCommandContext);
g_Logger->Render(canvas);
g_ProfileViewer.RenderProfile(canvas);
}
@@ -215,7 +215,7 @@ MESSAGEHANDLER(SetCanvas)
{
// Need to set the canvas size before possibly doing any rendering,
// else we'll get GL errors when trying to render to 0x0
CVideoMode::UpdateRenderer(msg->width, msg->height);
g_VideoMode.UpdateRenderer(msg->width, msg->height);
g_AtlasGameLoop->glCanvas = msg->canvas;
Atlas_GLSetCurrent(const_cast<void*>(g_AtlasGameLoop->glCanvas));
@@ -224,7 +224,7 @@ MESSAGEHANDLER(SetCanvas)
MESSAGEHANDLER(ResizeScreen)
{
CVideoMode::UpdateRenderer(msg->width, msg->height);
g_VideoMode.UpdateRenderer(msg->width, msg->height);
#if OS_MACOSX
// OS X seems to require this to update the GL canvas
@@ -125,8 +125,8 @@ MESSAGEHANDLER(GuiMouseButtonEvent)
ev.button.clicks = msg->clicks;
float x, y;
msg->pos->GetScreenSpace(x, y);
ev.button.x = static_cast<u16>(Clamp<int>(x, 0, g_xres));
ev.button.y = static_cast<u16>(Clamp<int>(y, 0, g_yres));
ev.button.x = static_cast<u16>(Clamp<int>(x, 0, g_VideoMode.GetWindowWidth()));
ev.button.y = static_cast<u16>(Clamp<int>(y, 0, g_VideoMode.GetWindowHeight()));
g_VideoMode.m_InputManager.DispatchEvent(ev);
}
@@ -136,8 +136,8 @@ MESSAGEHANDLER(GuiMouseMotionEvent)
ev.type = SDL_MOUSEMOTION;
float x, y;
msg->pos->GetScreenSpace(x, y);
ev.motion.x = static_cast<u16>(Clamp<int>(x, 0, g_xres));
ev.motion.y = static_cast<u16>(Clamp<int>(y, 0, g_yres));
ev.motion.x = static_cast<u16>(Clamp<int>(x, 0, g_VideoMode.GetWindowWidth()));
ev.motion.y = static_cast<u16>(Clamp<int>(y, 0, g_VideoMode.GetWindowHeight()));
g_VideoMode.m_InputManager.DispatchEvent(ev);
}
+2 -4
View File
@@ -54,8 +54,6 @@
extern void (*Atlas_GLSwapBuffers)(void* context);
extern int g_xres, g_yres;
//////////////////////////////////////////////////////////////////////////
void AtlasView::SetParam(const std::wstring& /*name*/, bool /*value*/)
@@ -93,7 +91,7 @@ void AtlasViewActor::Update(float realFrameLength)
void AtlasViewActor::Render()
{
SViewPort vp = { 0, 0, g_xres, g_yres };
SViewPort vp = { 0, 0, g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight() };
CCamera camera{GetCamera()};
camera.SetViewPort(vp);
camera.SetPerspectiveProjection(2.f, 512.f, DEGTORAD(20.f));
@@ -246,7 +244,7 @@ void AtlasViewGame::Render()
if (!swapChain || !swapChain->IsValid() || !swapChain->AcquireNextBackbuffer())
return;
SViewPort vp = { 0, 0, g_xres, g_yres };
SViewPort vp = { 0, 0, g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight() };
CCamera camera{GetCamera()};
camera.SetViewPort(vp);
camera.SetProjectionFromCamera(g_Game->GetView()->GetCamera());