From 4bb31eb98d344980be8fccc1b718150bbae1f09e Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Fri, 6 Nov 2026 19:10:19 -0600 Subject: [PATCH] refactor(coord): push script coordinates through one engine chain Every script coordinate the engine pushes (GetLeft, GetStringWidth, GetFont's height, GetCursorPosition, OnCursorChanged) is DivideByScreenScaleX(GetScaledScreenAspect() * [0x007FFD68] * value): 0x0041AD70, then the 1024.0f read from .rdata, then 0x0041AE40, with the quotient stored from ST0 as a double. Game::InternalToPixel did the same arithmetic from the globals in double precision, while the edit-box caret, the font height getters and texture tiling each kept a private copy of the engine chain. InternalToPixel now makes the engine's calls in the engine's order, rounding the float product once as the engine's fstp dword does, and the three modules use it. PixelToInternal reads the 1024 from the same .rdata float, so the UI_COORD_SCALE_UNIT constant is gone. --- src/Game.cpp | 18 +++++++++++++----- src/Game.h | 6 ++++-- src/Offsets.h | 1 - src/editbox/IconRows.cpp | 9 ++------- src/fontstring/FontHeight.cpp | 15 ++------------- src/texture/Tile.cpp | 8 +------- 6 files changed, 22 insertions(+), 35 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index 047c523d..d4ea7a4b 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -30,15 +30,23 @@ float PixelToInternal(float px) { const float div = Read(Offsets::VAR_COORD_ASPECT_COMPENSATION); if (div == 0.0f) return 0.0f; - return px * mul / (div * static_cast(Offsets::UI_COORD_SCALE_UNIT)); + return px * mul / (div * Read(Offsets::FLOAT_SCRIPT_COORD_SCALE)); } double InternalToPixel(float internal) { - const float mul = Read(Offsets::VAR_COORD_DDC_WIDTH_SCALE); - const float div = Read(Offsets::VAR_COORD_ASPECT_COMPENSATION); - if (mul == 0.0f) + using GetScaledScreenAspect_t = float(__cdecl *)(); + // Declared double so the quotient is read from ST0, as the engine's + // pushes store it with `fstp qword`, not narrowed to float. + using DivideByScreenScaleX_t = double(__stdcall *)(float); + if (Read(Offsets::VAR_COORD_DDC_WIDTH_SCALE) == 0.0f) return 0.0; - return static_cast(internal) * div * Offsets::UI_COORD_SCALE_UNIT / mul; + // `aspect * 1024` is exact, so the float product rounds once, as the + // engine's `fstp dword` does. + const float aspect = + reinterpret_cast(Offsets::FUN_COORD_GET_SCALED_SCREEN_ASPECT)(); + const float scaled = aspect * Read(Offsets::FLOAT_SCRIPT_COORD_SCALE) * internal; + return reinterpret_cast(Offsets::FUN_COORD_DIVIDE_BY_SCREEN_SCALE_X)( + scaled); } bool StringToFlag(const char *text) { diff --git a/src/Game.h b/src/Game.h index e49ba365..7e19532b 100644 --- a/src/Game.h +++ b/src/Game.h @@ -89,8 +89,10 @@ void *ResolveUnitToken(const char *token); // UI pixels <-> internal layout (anchor) units. Script_SetPoint converts its // pixel offsets with `px * [VAR_COORD_DDC_WIDTH_SCALE] / ([VAR_COORD_ASPECT_COMPENSATION] -// * UI_COORD_SCALE_UNIT)`; Script_GetLeft and Script_GetStringWidth push the -// inverse. A region's LOCAL pixels must be multiplied by its effective scale +// * [FLOAT_SCRIPT_COORD_SCALE])`; every script coordinate push (GetLeft, +// GetStringWidth, GetFont's height, GetCursorPosition, OnCursorChanged) is the +// inverse, `DivideByScreenScaleX(GetScaledScreenAspect() * 1024 * value)`, +// which InternalToPixel calls in that order. A region's LOCAL pixels must be multiplied by its effective scale // (OFF_REGION_EFFECTIVE_SCALE) first. Both return 0 while the scale globals // are unset, so an early caller gets a zero offset rather than a wild one. float PixelToInternal(float px); diff --git a/src/Offsets.h b/src/Offsets.h index e7648e8e..5339d9ca 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -750,7 +750,6 @@ enum Offsets { VAR_COORD_DDC_WIDTH_SCALE = 0x00832A44, VAR_COORD_DDC_HEIGHT_SCALE = 0x00832A48, VAR_COORD_ASPECT_COMPENSATION = 0x00832A4C, - UI_COORD_SCALE_UNIT = 1024, // DAT_007ffd68 // `SetUiParentScale` — `void __stdcall(float scale)`, `ret 4`. The one // setter of UIParent's scale: raises the value to 0.64, calls SetScale // with force (0x0076AC10), drains the layout queue (0x00768ED0), and does diff --git a/src/editbox/IconRows.cpp b/src/editbox/IconRows.cpp index 575fde0f..0a146e93 100644 --- a/src/editbox/IconRows.cpp +++ b/src/editbox/IconRows.cpp @@ -65,8 +65,6 @@ using SetPoint_t = void(__thiscall *)(void *anchor, int point, void *relAnchor, int flag); using Resize_t = void(__fastcall *)(void *anchor, void *edx, int force); using Extent_t = float(__fastcall *)(void *anchor, void *edx); -using ScreenAspect_t = float(__cdecl *)(); -using DivideByScreenScaleX_t = float(__stdcall *)(float value); using FireVariadic_t = void(__cdecl *)(void *frame, uint32_t *slotPtr, const char *fmt, ...); PlaceCaret_t g_origPlaceCaret = nullptr; @@ -280,11 +278,8 @@ void __fastcall PlaceCaret_h(void *editBox) { void **vtable = *static_cast(anchor); const float width = reinterpret_cast(vtable[7])(anchor, nullptr); // +0x1C GetWidth const float height = reinterpret_cast(vtable[8])(anchor, nullptr); // +0x20 GetHeight - const auto aspect = reinterpret_cast(Offsets::FUN_COORD_GET_SCALED_SCREEN_ASPECT); - const auto divide = reinterpret_cast(Offsets::FUN_COORD_DIVIDE_BY_SCREEN_SCALE_X); - const float factor = Game::Read(Offsets::FLOAT_SCRIPT_COORD_SCALE); - const auto push = [&](float value) { return static_cast(divide(aspect() * factor * value)); }; - const double args[4] = {push(x), push(y), push(width), push(height)}; + const double args[4] = {Game::InternalToPixel(x), Game::InternalToPixel(y), Game::InternalToPixel(width), + Game::InternalToPixel(height)}; void *L = Game::Lua::State(); const int savedTop = (L != nullptr) ? Game::Lua::GetTop(L) : 0; reinterpret_cast(Offsets::FUN_FRAME_RUN_SCRIPT_VARIADIC)( diff --git a/src/fontstring/FontHeight.cpp b/src/fontstring/FontHeight.cpp index ef5bd1d8..494274cd 100644 --- a/src/fontstring/FontHeight.cpp +++ b/src/fontstring/FontHeight.cpp @@ -39,19 +39,8 @@ namespace { // FUN_FONTSTRING_FONT_HEIGHT — __thiscall(fs, scaled-on-stack); dummy-EDX form. using FsFontHeight_t = float(__fastcall *)(void *fs, void *edx, int scaled); -using GetScaledScreenAspect_t = float(__cdecl *)(); -// Declared double so the quotient is read from ST0 with `fstp qword`, as -// GetFont stores it, not narrowed to float. -using DivideByScreenScaleX_t = double(__stdcall *)(float); - -// GetFont's push (0x0079D4A5..0x0079D4C4). `aspect * 1024` is exact, so the -// float product rounds once, as GetFont's `fstp dword` does. -double ToUiHeight(float internal) { - const float aspect = - reinterpret_cast(Offsets::FUN_COORD_GET_SCALED_SCREEN_ASPECT)(); - const float scaled = aspect * static_cast(Offsets::UI_COORD_SCALE_UNIT) * internal; - return reinterpret_cast(Offsets::FUN_COORD_DIVIDE_BY_SCREEN_SCALE_X)(scaled); -} +// GetFont's push (0x0079D4A5..0x0079D4C4). +double ToUiHeight(float internal) { return Game::InternalToPixel(internal); } void *FontStringSelf(void *L) { return Game::Lua::ResolveFontString(L); } void *FontSelf(void *L) { return Game::Lua::ResolveTypedObject(L, 1, Offsets::VAR_FONT_LUA_TYPE_ID); } diff --git a/src/texture/Tile.cpp b/src/texture/Tile.cpp index d452baec..e567d819 100644 --- a/src/texture/Tile.cpp +++ b/src/texture/Tile.cpp @@ -65,8 +65,6 @@ namespace Texture::Tile { namespace { using GetRect_t = int(__fastcall *)(void *anchor, void *edx, float *outRect); -using ScreenAspect_t = float(__cdecl *)(); -using DivideByScreenScaleX_t = float(__stdcall *)(float value); using SetTexCoord_t = void(__fastcall *)(void *texture, void *edx, const float *values); using Emit_t = void(__fastcall *)(void *texture, void *edx, void *batch); @@ -74,11 +72,7 @@ using Emit_t = void(__fastcall *)(void *texture, void *edx, void *batch); constexpr float kWholeImage[8] = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f}; // A length in rect units as script units. -float ScriptUnits(float length) { - const auto aspect = reinterpret_cast(Offsets::FUN_COORD_GET_SCALED_SCREEN_ASPECT); - const auto divide = reinterpret_cast(Offsets::FUN_COORD_DIVIDE_BY_SCREEN_SCALE_X); - return divide(aspect() * Game::Read(Offsets::FLOAT_SCRIPT_COORD_SCALE) * length); -} +float ScriptUnits(float length) { return static_cast(Game::InternalToPixel(length)); } // 4.3.4's rule on one axis (0 x, 1 y) of the four corner pairs. void Count(float *coords, int axis, float length, uint32_t texels) {