Skip to content

Commit 76f7cc2

Browse files
authored
Fix HUD size when playing on widescreen (PR #3444, Fixes #848)
1 parent f1f341e commit 76f7cc2

File tree

7 files changed

+118
-32
lines changed

7 files changed

+118
-32
lines changed

Client/game_sa/CHudSA.cpp

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*****************************************************************************
22
*
3-
* PROJECT: Multi Theft Auto v1.0
3+
* PROJECT: Multi Theft Auto
44
* LICENSE: See LICENSE in the top level directory
55
* FILE: game_sa/CHudSA.cpp
66
* PURPOSE: HUD display
@@ -19,8 +19,8 @@
1919

2020
extern CGameSA* pGame;
2121

22-
char szVehicleName[50] = {'\0'};
23-
char szZoneName[50] = {'\0'};
22+
static float radarAltimeterFix = 0.0014625f; // Changes altimeter width (and maybe x pos)
23+
static constexpr float aspectRatioMultiplicatorUntouched = 0.0015625f; // (1 / 640)
2424

2525
static ComponentProperties componentProperties;
2626

@@ -56,23 +56,18 @@ CHudSA::CHudSA()
5656
{
5757
InitComponentList();
5858

59-
// Set the default values
60-
m_fSniperCrosshairScale = 210.0f;
59+
m_pfAspectRatioMultiplicatorX = reinterpret_cast<float*>(VAR_AspectRatioMultX);
60+
m_pfAspectRatioMultiplicatorY = reinterpret_cast<float*>(VAR_AspectRatioMult);
6161

62-
m_pfCameraCrosshairScale = (float*)VAR_CameraCrosshairScale;
63-
MemPut<float>(m_pfCameraCrosshairScale, 192.0f);
64-
m_pfAspectRatioMultiplicator = (float*)VAR_AspectRatioMult;
65-
MemPut<float>(m_pfAspectRatioMultiplicator, 0.002232143f);
62+
MemPut<float>(0x866C84, 640.0f / ((4.0f / 3.0f) * 448.0f)); // 0x866C84: Weapon sprite x position
63+
MemPut<float>(m_pfAspectRatioMultiplicatorX, 0.0015625f); // (1 / 640)
64+
MemPut<float>(m_pfAspectRatioMultiplicatorY, 0.002232143f); // (1 / 448)
6665

67-
UpdateStreetchCalculations();
66+
MemPut<const float*>(0x58B141, &aspectRatioMultiplicatorUntouched); // Vehicle name x pos
67+
MemPut<const float*>(0x58AE4C, &aspectRatioMultiplicatorUntouched); // Area name x pos
68+
MemPut<float*>(0x58A6E0, &radarAltimeterFix); // Fix radar altimeter
6869

69-
// Patch xrefs to 0x863B34, because this variable seems to be shared (2 other functions without any context access to it; probably a compiler optimization)
70-
MemPut<DWORD>(0x58E7D4 + 2, (DWORD)&m_fSniperCrosshairScale);
71-
MemPut<DWORD>(0x58E7EA + 2, (DWORD)&m_fSniperCrosshairScale);
72-
MemPut<DWORD>(0x53E3ED + 2, (DWORD)&m_fSniperCrosshairScale);
73-
MemPut<DWORD>(0x53E41A + 2, (DWORD)&m_fSniperCrosshairScale);
74-
MemPut<DWORD>(0x53E488 + 2, (DWORD)&m_fSniperCrosshairScale);
75-
MemPut<DWORD>(0x53E4BF + 2, (DWORD)&m_fSniperCrosshairScale);
70+
UpdateStreetchCalculations();
7671

7772
// Initalize default data
7873
componentProperties.hpBar = MapGet(defaultComponentProperties, HUD_HEALTH);
@@ -198,8 +193,8 @@ bool CHudSA::IsComponentVisible(eHudComponent component)
198193

199194
void CHudSA::UpdateStreetchCalculations()
200195
{
201-
calcStreetchX = rsGlobal->maximumWidth * (*reinterpret_cast<float*>(VAR_AspectRatioMultX));
202-
calcStreetchY = rsGlobal->maximumHeight * (*m_pfAspectRatioMultiplicator);
196+
calcStreetchX = rsGlobal->maximumWidth * (*m_pfAspectRatioMultiplicatorX);
197+
calcStreetchY = rsGlobal->maximumHeight * (*m_pfAspectRatioMultiplicatorY);
203198

204199
SComponentPlacement& hpPlacement = componentProperties.hpBar.placement;
205200
hpPlacement.height = calcStreetchY * 9.0f;
@@ -265,14 +260,13 @@ void CHudSA::AdjustComponents(float fAspectRatio)
265260
// Fix for #7400 (HUD elements do not scale correctly for widescreen)
266261
// 0x859524: GTA multiplies all HUD and menu transformation variables by this floating point value. It is equal to 1/448, so just translate it to 16/10 /
267262
// 16/9
268-
MemPut<float>(m_pfAspectRatioMultiplicator, 0.002232143f / (4.0f / 3.0f) * fAspectRatio);
269-
270-
// Set the sniper crosshair scale (fix for #7659)
271-
m_fSniperCrosshairScale = 210.0f * (4.0f / 3.0f) / fAspectRatio;
272-
273-
// Set the camera crosshair scale (same display flaw as in #7659)
274-
MemPut<float>(m_pfCameraCrosshairScale, 192.0f * (4.0f / 3.0f) / fAspectRatio);
263+
const float ratio = (640.0f / (fAspectRatio * 448.0f));
275264

265+
radarAltimeterFix = ratio * 0.00147f;
266+
MemPut<float>(m_pfAspectRatioMultiplicatorX, ratio * 0.0015625f);
267+
MemPut<float>(m_pfAspectRatioMultiplicatorY, ratio * 0.002232143f);
268+
MemPut<float>(0x866C84, ratio * 0.17343046f); // 0x866C84: Weapon sprite x position
269+
276270
UpdateStreetchCalculations();
277271
}
278272

@@ -282,9 +276,10 @@ void CHudSA::AdjustComponents(float fAspectRatio)
282276
void CHudSA::ResetComponentAdjustment()
283277
{
284278
// Restore default values (4:3 aspect ratio)
285-
MemPut<float>(m_pfAspectRatioMultiplicator, 0.002232143f);
286-
MemPut<float>(m_pfCameraCrosshairScale, 192.0f);
287-
m_fSniperCrosshairScale = 210.0f;
279+
radarAltimeterFix = 0.0014625f;
280+
MemPut<float>(m_pfAspectRatioMultiplicatorX, 0.0015625f); // (1 / 640)
281+
MemPut<float>(m_pfAspectRatioMultiplicatorY, 0.002232143f); // (1 / 448)
282+
MemPut<float>(0x866C84, 0.17343046f); // 0x866C84: Weapon sprite x position
288283

289284
UpdateStreetchCalculations();
290285
}

Client/game_sa/CHudSA.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ class CHudSA : public CHud
273273
private:
274274
std::map<eHudComponent, SHudComponent> m_HudComponentMap;
275275

276-
float* m_pfAspectRatioMultiplicator;
276+
float* m_pfAspectRatioMultiplicatorX;
277+
float* m_pfAspectRatioMultiplicatorY;
277278
float* m_pfCameraCrosshairScale;
278279
float m_fSniperCrosshairScale;
279280

Client/game_sa/CSettingsSA.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CSettingsSA::CSettingsSA()
5151
HookInstall(HOOKPOS_StoreShadowForVehicle, (DWORD)HOOK_StoreShadowForVehicle, 9);
5252
m_iDesktopWidth = 0;
5353
m_iDesktopHeight = 0;
54-
MemPut<BYTE>(0x6FF420, 0xC3); // Truncate CalculateAspectRatio
54+
MemPut<BYTE>(0x6FF420, 0xC3);
5555

5656
MemPut(0x732926, &ms_fVehicleLODDistance);
5757
MemPut(0x732940, &ms_fTrainPlaneLODDistance);

Client/multiplayer_sa/CMultiplayerSA.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,7 @@ void CMultiplayerSA::InitHooks()
15911591

15921592
InitHooks_Streaming();
15931593
InitHooks_FrameRateFixes();
1594+
InitHooks_WidescreenFix();
15941595
InitHooks_ProjectileCollisionFix();
15951596
InitHooks_ObjectStreamerOptimization();
15961597

Client/multiplayer_sa/CMultiplayerSA.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class CMultiplayerSA : public CMultiplayer
7777
void InitHooks_FixLineOfSightArgs();
7878
void InitHooks_Streaming();
7979
void InitHooks_FrameRateFixes();
80+
void InitHooks_WidescreenFix();
8081
void InitHooks_ProjectileCollisionFix();
8182
void InitHooks_ObjectStreamerOptimization();
8283
void InitHooks_Postprocess();
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*****************************************************************************
2+
*
3+
* PROJECT: Multi Theft Auto
4+
* LICENSE: See LICENSE in the top level directory
5+
* FILE: multiplayer_sa/CMultiplayerSA_WidescreenFix.cpp
6+
*
7+
* Multi Theft Auto is available from https://www.multitheftauto.com/
8+
*
9+
*****************************************************************************/
10+
11+
// NOTE: This code is based on ThirteenAG's fix, which is licensed under the MIT license.
12+
#include "StdInc.h"
13+
14+
#ifndef M_PI
15+
#define M_PI 3.14159265358979323846
16+
#endif
17+
18+
static constexpr float CameraWidth = 0.01403292f;
19+
static constexpr float SkyMultiFix = 10.0f;
20+
21+
static float* ScreenAspectRatio = reinterpret_cast<float*>(0xC3EFA4);
22+
static float* ScreenFieldOfView = reinterpret_cast<float*>(0x8D5038);
23+
24+
static float AdjustFOV(float factor, float aspectRatio) noexcept
25+
{
26+
return std::round((2.0f * std::atan(((aspectRatio) / (4.0f / 3.0f)) *
27+
std::tan(factor / 2.0f * ((float)M_PI / 180.0f)))) *
28+
(180.0f / (float)M_PI) * 100.0f) / 100.0f;
29+
}
30+
31+
static void SetFOV(float factor)
32+
{
33+
*ScreenFieldOfView = AdjustFOV(factor, *ScreenAspectRatio);
34+
}
35+
36+
static constexpr std::uintptr_t RETURN_CCamera_Find3rdPersonCamTargetVector = 0x5149A6;
37+
static void __declspec(naked) CalculateAimingPoint()
38+
{
39+
_asm
40+
{
41+
fstp st
42+
mov edx, [ScreenAspectRatio]
43+
fmul [edx]
44+
mov edi, [esp + 1Ch + 14h]
45+
mov edx, edi
46+
jmp RETURN_CCamera_Find3rdPersonCamTargetVector
47+
}
48+
}
49+
50+
static void InstallAspectRatioFixes()
51+
{
52+
// Disables jump instructions in GetVideoModeList
53+
MemSet((void*)0x745BD1, 0x90, 2);
54+
MemSet((void*)0x745BD9, 0x90, 2);
55+
56+
// Proportional coronas, skips multiplication by ScreenY in CCoronas::Render
57+
MemSet((void*)0x6FB2C9, 0x90, 4);
58+
59+
// Load ScreenY value from the stack
60+
MemPut<BYTE>(0x6FB2BD, 0x6C); // CCoronas::Render
61+
MemPut<BYTE>(0x6FB2DC, 0x78); // CCoronas::Render
62+
MemPut<BYTE>(0x713BE5, 0x20); // CClouds::Render
63+
MemPut<BYTE>(0x713B6D, 0x38); // CClouds::Render
64+
MemPut<BYTE>(0x713CFB, 0x38); // CClouds::Render
65+
MemPut<BYTE>(0x713EFC, 0x30); // CClouds::Render
66+
MemPut<BYTE>(0x714004, 0x38); // CClouds::Render
67+
}
68+
69+
static void InstallFieldOfViewFixes()
70+
{
71+
// Fix sky blurring white in ultrawide screens
72+
MemPut<const float*>(0x714843, &SkyMultiFix);
73+
MemPut<const float*>(0x714860, &SkyMultiFix);
74+
75+
HookInstall(0x6FF410, reinterpret_cast<DWORD>(SetFOV), 9);
76+
HookInstall(0x51499E, reinterpret_cast<DWORD>(CalculateAimingPoint), 6);
77+
78+
// Skips division by CDraw::ms_fAspectRatio
79+
MemSet((void*)0x50AD79, 0x90, 6);
80+
MemPut<const float*>(0x50AD5B, &CameraWidth); // CCamera::Find3rdPersonQuickAimPitch
81+
MemPut<const float*>(0x51498F, &CameraWidth); // CCamera::Find3rdPersonCamTargetVector
82+
}
83+
84+
void CMultiplayerSA::InitHooks_WidescreenFix()
85+
{
86+
InstallAspectRatioFixes();
87+
InstallFieldOfViewFixes();
88+
}

Client/sdk/game/CSettings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class CGameSettings
145145

146146
virtual float GetAspectRatioValue() = 0;
147147
virtual eAspectRatio GetAspectRatio() = 0;
148-
virtual void SetAspectRatio(eAspectRatio aspectRatio, bool bAdjustmentEnabled = true) = 0;
148+
virtual void SetAspectRatio(eAspectRatio aspectRatio, bool isAdjustmentEnabled) = 0;
149149

150150
virtual bool IsGrassEnabled() = 0;
151151
virtual void SetGrassEnabled(bool bEnable) = 0;

0 commit comments

Comments
 (0)