Skip to content

Commit c897435

Browse files
committed
feat(ultrawide): restore custom aspect hooks and reapply after startup
1 parent c8a32ef commit c897435

3 files changed

Lines changed: 73 additions & 41 deletions

File tree

src/component/ultrawide.cpp

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include <std_include.hpp>
22

33
#include "loader/component_loader.hpp"
4-
#include "component/command.hpp"
5-
#include "component/scheduler.hpp"
6-
#include "component/console.hpp"
4+
5+
#include "command.hpp"
6+
#include "console.hpp"
7+
#include "scheduler.hpp"
8+
79
#include "game/game.hpp"
810
#include "game/dvars.hpp"
911

@@ -23,9 +25,6 @@ namespace ultrawide
2325
constexpr auto renderer_monitor_aspect = 0x10E271B0;
2426
constexpr auto renderer_render_aspect = 0x10E271B4;
2527

26-
game::dvar_s* r_aspectRatioCustom = nullptr;
27-
game::dvar_s* r_aspectRatioCustomEnable = nullptr;
28-
game::dvar_s* r_ultrawideCustomMode = nullptr;
2928
bool sticky_custom_resolution_enabled = false;
3029
float sticky_custom_ratio = 16.0f / 9.0f;
3130
float original_aspect_ratio = 16.0f / 9.0f;
@@ -46,14 +45,14 @@ namespace ultrawide
4645

4746
void sync_sticky_custom_resolution_state()
4847
{
49-
if (!r_ultrawideCustomMode || !r_ultrawideCustomMode->current.string)
48+
if (!dvars::r_ultrawideCustomMode || !dvars::r_ultrawideCustomMode->current.string)
5049
{
5150
return;
5251
}
5352

5453
int width = 0;
5554
int height = 0;
56-
if (parse_resolution(r_ultrawideCustomMode->current.string, width, height))
55+
if (parse_resolution(dvars::r_ultrawideCustomMode->current.string, width, height))
5756
{
5857
sticky_custom_resolution_enabled = true;
5958
sticky_custom_ratio = static_cast<float>(width) / static_cast<float>(height);
@@ -65,6 +64,30 @@ namespace ultrawide
6564
}
6665
}
6766

67+
void set_bool_dvar(game::dvar_s* dvar, const bool value)
68+
{
69+
if (!dvar)
70+
{
71+
return;
72+
}
73+
74+
dvar->current.enabled = value;
75+
dvar->latched.enabled = value;
76+
dvar->reset.enabled = value;
77+
}
78+
79+
void set_float_dvar(game::dvar_s* dvar, const float value)
80+
{
81+
if (!dvar)
82+
{
83+
return;
84+
}
85+
86+
dvar->current.value = value;
87+
dvar->latched.value = value;
88+
dvar->reset.value = value;
89+
}
90+
6891
void set_custom_resolution(const std::string& resolution)
6992
{
7093
int width = 0;
@@ -132,24 +155,13 @@ namespace ultrawide
132155

133156
if (sticky_custom_resolution_enabled)
134157
{
135-
if (r_aspectRatioCustomEnable)
136-
{
137-
r_aspectRatioCustomEnable->current.enabled = true;
138-
r_aspectRatioCustomEnable->latched.enabled = true;
139-
r_aspectRatioCustomEnable->reset.enabled = true;
140-
}
141-
142-
if (r_aspectRatioCustom)
143-
{
144-
r_aspectRatioCustom->current.value = sticky_custom_ratio;
145-
r_aspectRatioCustom->latched.value = sticky_custom_ratio;
146-
r_aspectRatioCustom->reset.value = sticky_custom_ratio;
147-
}
158+
set_bool_dvar(dvars::r_aspectRatioCustomEnable, true);
159+
set_float_dvar(dvars::r_aspectRatioCustom, sticky_custom_ratio);
148160
}
149161

150-
const auto enabled = r_aspectRatioCustomEnable && r_aspectRatioCustomEnable->current.enabled;
151-
const auto custom_ratio = r_aspectRatioCustom
152-
? std::clamp(r_aspectRatioCustom->current.value, 4.0f / 3.0f, 63.0f / 9.0f)
162+
const auto enabled = dvars::r_aspectRatioCustomEnable && dvars::r_aspectRatioCustomEnable->current.enabled;
163+
const auto custom_ratio = dvars::r_aspectRatioCustom
164+
? std::clamp(dvars::r_aspectRatioCustom->current.value, 4.0f / 3.0f, 63.0f / 9.0f)
153165
: original_aspect_ratio;
154166

155167
*aspect_ratio = original_aspect_ratio;
@@ -181,19 +193,15 @@ namespace ultrawide
181193
const auto is_wide = enabled
182194
? (custom_ratio > 1.5f)
183195
: (original_renderer_aspect_ratio > 1.5f);
184-
widescreen->current.enabled = is_wide;
185-
widescreen->latched.enabled = is_wide;
186-
widescreen->reset.enabled = is_wide;
196+
set_bool_dvar(widescreen, is_wide);
187197
}
188198

189199
if (auto* const is_widescreen = game::Dvar_FindVar("r_isWideScreen"))
190200
{
191201
const auto is_wide = enabled
192202
? (custom_ratio > 1.5f)
193203
: (original_renderer_aspect_ratio > 1.5f);
194-
is_widescreen->current.enabled = is_wide;
195-
is_widescreen->latched.enabled = is_wide;
196-
is_widescreen->reset.enabled = is_wide;
204+
set_bool_dvar(is_widescreen, is_wide);
197205
}
198206
}
199207
}
@@ -205,30 +213,29 @@ namespace ultrawide
205213
{
206214
scheduler::once([]()
207215
{
208-
// Allow valid r_customMode values even when the engine thinks it's in fullscreen.
209216
utils::hook::nop(game::game_offset(custom_mode_fullscreen_gate), 2);
210217

211-
r_aspectRatioCustomEnable = dvars::Dvar_RegisterBool(
218+
dvars::r_aspectRatioCustomEnable = dvars::Dvar_RegisterBool(
212219
"r_aspectRatioCustomEnable", 0,
213220
"Enable custom ultrawide aspect ratio overrides.",
214221
game::dvar_flags::saved);
215222

216-
r_aspectRatioCustom = dvars::Dvar_RegisterFloat(
223+
dvars::r_aspectRatioCustom = dvars::Dvar_RegisterFloat(
217224
"r_aspectRatioCustom",
218225
"Screen aspect ratio override. Divide width by height to get the aspect ratio value. Example: 21 / 9 = 2.3333",
219226
16.0f / 9.0f, 4.0f / 3.0f, 63.0f / 9.0f,
220227
game::dvar_flags::saved);
221228

222-
r_ultrawideCustomMode = dvars::Dvar_RegisterString(
229+
dvars::r_ultrawideCustomMode = dvars::Dvar_RegisterString(
223230
"r_ultrawideCustomMode",
224231
"disabled",
225232
"Saved ultrawide custom resolution in WxH format for Consolation.",
226233
game::dvar_flags::saved);
227234

228-
sticky_custom_resolution_enabled = r_aspectRatioCustomEnable && r_aspectRatioCustomEnable->current.enabled;
229-
if (r_aspectRatioCustom)
235+
sticky_custom_resolution_enabled = dvars::r_aspectRatioCustomEnable && dvars::r_aspectRatioCustomEnable->current.enabled;
236+
if (dvars::r_aspectRatioCustom)
230237
{
231-
sticky_custom_ratio = std::clamp(r_aspectRatioCustom->current.value, 4.0f / 3.0f, 63.0f / 9.0f);
238+
sticky_custom_ratio = std::clamp(dvars::r_aspectRatioCustom->current.value, 4.0f / 3.0f, 63.0f / 9.0f);
232239
}
233240
sync_sticky_custom_resolution_state();
234241

@@ -260,8 +267,8 @@ namespace ultrawide
260267

261268
console::info(
262269
"dumpultrawide: enabled=%d custom=%.6f cl_ingame=%d keyCatchers=0x%X aspect=%.6f renderer=%.6f monitor=%.6f render=%.6f wideScreen=%d r_isWideScreen=%d\n",
263-
r_aspectRatioCustomEnable ? static_cast<int>(r_aspectRatioCustomEnable->current.enabled) : -1,
264-
r_aspectRatioCustom ? r_aspectRatioCustom->current.value : -1.0f,
270+
dvars::r_aspectRatioCustomEnable ? static_cast<int>(dvars::r_aspectRatioCustomEnable->current.enabled) : -1,
271+
dvars::r_aspectRatioCustom ? dvars::r_aspectRatioCustom->current.value : -1.0f,
265272
cl_ingame ? static_cast<int>(cl_ingame->current.enabled) : -1,
266273
game::keyCatchers ? *game::keyCatchers : 0,
267274
aspect_ratio ? *aspect_ratio : -1.0f,
@@ -271,14 +278,24 @@ namespace ultrawide
271278
widescreen ? static_cast<int>(widescreen->current.enabled) : -1,
272279
is_widescreen ? static_cast<int>(is_widescreen->current.enabled) : -1);
273280
});
281+
}, scheduler::main);
274282

283+
scheduler::once([]()
284+
{
275285
apply_custom_aspect_ratio();
276-
}, scheduler::pipeline::main);
286+
}, scheduler::main, 3000ms);
277287

278288
scheduler::loop([]()
279289
{
280290
apply_custom_aspect_ratio();
281-
}, scheduler::pipeline::main, 16ms);
291+
}, scheduler::main, 250ms);
292+
}
293+
294+
void pre_destroy() override
295+
{
296+
dvars::r_aspectRatioCustomEnable = nullptr;
297+
dvars::r_aspectRatioCustom = nullptr;
298+
dvars::r_ultrawideCustomMode = nullptr;
282299
}
283300
};
284301
}

src/component/ultrawide.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
namespace ultrawide
4+
{
5+
void set_custom_resolution_command(const std::string& resolution);
6+
void reset_custom_resolution_command();
7+
void dump_ultrawide_command();
8+
void apply_ultrawide_now_command();
9+
}

src/game/dvars.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ namespace dvars
3232
game::dvar_s* cg_drawVersion = nullptr;
3333
game::dvar_s* cg_drawVersionX = nullptr;
3434
game::dvar_s* cg_drawVersionY = nullptr;
35+
game::dvar_s* r_aspectRatioCustomEnable = nullptr;
36+
game::dvar_s* r_aspectRatioCustom = nullptr;
37+
game::dvar_s* r_ultrawideCustomMode = nullptr;
3538

3639
std::string dvar_get_vector_domain(const int components, const game::DvarLimits& domain)
3740
{
@@ -293,6 +296,9 @@ namespace dvars
293296
cg_drawVersion = nullptr;
294297
cg_drawVersionX = nullptr;
295298
cg_drawVersionY = nullptr;
299+
r_aspectRatioCustomEnable = nullptr;
300+
r_aspectRatioCustom = nullptr;
301+
r_ultrawideCustomMode = nullptr;
296302
});
297303
}
298304
};

0 commit comments

Comments
 (0)