Skip to content

Commit 9ef94de

Browse files
author
ethanblazkowicz
committed
Decouple DPI_AWARE flag's dual responsibilities
- VirtualScreenWidth/Height now always return fbSize (physical) - VirtualScreenScaleFactor always returns real scale (no apiDpiAware gate) - VirtualMap/VirtualUnmap use VirtualScreenScaleFactor instead of raw vid.dpiScale - New APICoordScale returns 1.0 when apiDpiAware, dpiScale otherwise - Lua API functions use APICoordScale for input scaling and VirtualMap for output - GetCursorPos no longer double-divides by dpiScale after VirtualMap
1 parent c062b29 commit 9ef94de

4 files changed

Lines changed: 27 additions & 24 deletions

File tree

engine/render.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class r_IRenderer {
107107
virtual int VirtualScreenWidth() = 0;
108108
virtual int VirtualScreenHeight() = 0;
109109
virtual float VirtualScreenScaleFactor() = 0;
110+
virtual float APICoordScale() = 0;
110111
virtual void SetDpiScaleOverridePercent(int percent) = 0;
111112
virtual int DpiScaleOverridePercent() const = 0;
112113
virtual int VirtualMap(int properValue) = 0;

engine/render/r_main.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,23 +1864,18 @@ int r_renderer_c::DrawStringCursorIndex(int height, int font, const char* str, i
18641864
// ==============
18651865

18661866
int r_renderer_c::VirtualScreenWidth() {
1867-
int const properWidth = apiDpiAware ? sys->video->vid.fbSize[0] : sys->video->vid.size[0];
1868-
return VirtualMap(properWidth);
1867+
return sys->video->vid.fbSize[0];
18691868
}
18701869

18711870
int r_renderer_c::VirtualScreenHeight() {
1872-
int const properHeight = apiDpiAware ? sys->video->vid.fbSize[1] : sys->video->vid.size[1];
1873-
return VirtualMap(properHeight);
1871+
return sys->video->vid.fbSize[1];
18741872
}
18751873

18761874
float r_renderer_c::VirtualScreenScaleFactor() {
1877-
if (apiDpiAware) {
1878-
if (dpiScaleOverridePercent > 0) {
1879-
return dpiScaleOverridePercent / 100.0f;
1880-
}
1881-
return sys->video->vid.dpiScale;
1875+
if (dpiScaleOverridePercent > 0) {
1876+
return dpiScaleOverridePercent / 100.0f;
18821877
}
1883-
return 1.0f;
1878+
return sys->video->vid.dpiScale;
18841879
}
18851880

18861881
void r_renderer_c::SetDpiScaleOverridePercent(int percent) {
@@ -1895,14 +1890,21 @@ int r_renderer_c::VirtualMap(int properValue) {
18951890
if (apiDpiAware) {
18961891
return properValue;
18971892
}
1898-
return static_cast<int>(properValue / sys->video->vid.dpiScale);
1893+
return static_cast<int>(properValue / VirtualScreenScaleFactor());
18991894
}
19001895

19011896
int r_renderer_c::VirtualUnmap(int mappedValue) {
19021897
if (apiDpiAware) {
19031898
return mappedValue;
19041899
}
1905-
return static_cast<int>(mappedValue * sys->video->vid.dpiScale);
1900+
return static_cast<int>(mappedValue * VirtualScreenScaleFactor());
1901+
}
1902+
1903+
float r_renderer_c::APICoordScale() {
1904+
if (apiDpiAware) {
1905+
return 1.0f;
1906+
}
1907+
return VirtualScreenScaleFactor();
19061908
}
19071909

19081910
// =====

engine/render/r_main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class r_renderer_c: public r_IRenderer, public conCmdHandler_c {
100100
int VirtualScreenWidth();
101101
int VirtualScreenHeight();
102102
float VirtualScreenScaleFactor();
103+
float APICoordScale();
103104
void SetDpiScaleOverridePercent(int percent);
104105
int DpiScaleOverridePercent() const;
105106
int VirtualMap(int properValue);

ui_api.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,8 @@ static int l_RenderInit(lua_State* L)
722722
static int l_GetScreenSize(lua_State* L)
723723
{
724724
ui_main_c* ui = GetUIPtr(L);
725-
lua_pushinteger(L, ui->renderer->VirtualScreenWidth());
726-
lua_pushinteger(L, ui->renderer->VirtualScreenHeight());
725+
lua_pushinteger(L, ui->renderer->VirtualMap(ui->renderer->VirtualScreenWidth()));
726+
lua_pushinteger(L, ui->renderer->VirtualMap(ui->renderer->VirtualScreenHeight()));
727727
return 2;
728728
}
729729

@@ -793,7 +793,7 @@ static int l_SetViewport(lua_State* L)
793793
ui->LAssert(L, ui->renderer != NULL, "Renderer is not initialised");
794794
ui->LAssert(L, ui->renderEnable, "SetViewport() called outside of OnFrame");
795795
int n = lua_gettop(L);
796-
const float dpiScale = ui->renderer->VirtualScreenScaleFactor();
796+
const float dpiScale = ui->renderer->APICoordScale();
797797
if (n) {
798798
ui->LAssert(L, n >= 4, "Usage: SetViewport([x, y, width, height])");
799799
for (int i = 1; i <= 4; i++) {
@@ -935,7 +935,7 @@ static int l_DrawImage(lua_State* L)
935935
}
936936

937937
if (af & AF_XY) {
938-
const float dpiScale = ui->renderer->VirtualScreenScaleFactor();
938+
const float dpiScale = ui->renderer->APICoordScale();
939939
for (int i = k; i < k + 4; i++) {
940940
int isNum;
941941
lua_Number val = lua_tonumberx(L, i, &isNum);
@@ -1054,7 +1054,7 @@ static int l_DrawImageQuad(lua_State* L)
10541054
}
10551055

10561056
if (af & AF_XY) {
1057-
const float dpiScale = ui->renderer->VirtualScreenScaleFactor();
1057+
const float dpiScale = ui->renderer->APICoordScale();
10581058
for (int i = k; i < k + 8; i++) {
10591059
int isNum;
10601060
lua_Number val = lua_tonumberx(L, i, &isNum);
@@ -1144,7 +1144,7 @@ static int l_DrawString(lua_State* L)
11441144
ui->LAssert(L, lua_isstring(L, 6), "DrawString() argument 6: expected string, got %s", luaL_typename(L, 6));
11451145
static const char* alignMap[6] = { "LEFT", "CENTER", "RIGHT", "CENTER_X", "RIGHT_X", NULL };
11461146
static const char* fontMap[8] = { "FIXED", "VAR", "VAR BOLD", "FONTIN SC", "FONTIN SC ITALIC", "FONTIN", "FONTIN ITALIC", NULL };
1147-
const float dpiScale = ui->renderer->VirtualScreenScaleFactor();
1147+
const float dpiScale = ui->renderer->APICoordScale();
11481148
const float left = lua_tonumber(L, 1) * dpiScale;
11491149
const float top = lua_tonumber(L, 2) * dpiScale;
11501150
const lua_Number logicalHeight = lua_tonumber(L, 4);
@@ -1186,7 +1186,7 @@ static int l_DrawStringWidth(lua_State* L)
11861186
ui->LAssert(L, lua_isstring(L, 2), "DrawStringWidth() argument 2: expected string, got %s", luaL_typename(L, 2));
11871187
ui->LAssert(L, lua_isstring(L, 3), "DrawStringWidth() argument 3: expected string, got %s", luaL_typename(L, 3));
11881188
static const char* fontMap[8] = { "FIXED", "VAR", "VAR BOLD", "FONTIN SC", "FONTIN SC ITALIC", "FONTIN", "FONTIN ITALIC", NULL };
1189-
const float dpiScale = ui->renderer->VirtualScreenScaleFactor();
1189+
const float dpiScale = ui->renderer->APICoordScale();
11901190
const lua_Number logicalHeight = lua_tonumber(L, 1);
11911191
int scaledHeight = static_cast<int>(std::lround(logicalHeight * dpiScale));
11921192
if (scaledHeight <= 1) {
@@ -1208,7 +1208,7 @@ static int l_DrawStringCursorIndex(lua_State* L)
12081208
ui_main_c* ui = GetUIPtr(L);
12091209
ui->LAssert(L, ui->renderer != NULL, "Renderer is not initialised");
12101210
int n = lua_gettop(L);
1211-
const float dpiScale = ui->renderer->VirtualScreenScaleFactor();
1211+
const float dpiScale = ui->renderer->APICoordScale();
12121212
ui->LAssert(L, n >= 5, "Usage: DrawStringCursorIndex(height, font, text, cursorX, cursorY)");
12131213
ui->LAssert(L, lua_isnumber(L, 1), "DrawStringCursorIndex() argument 1: expected number, got %s", luaL_typename(L, 1));
12141214
ui->LAssert(L, lua_isstring(L, 2), "DrawStringCursorIndex() argument 2: expected string, got %s", luaL_typename(L, 2));
@@ -1534,17 +1534,16 @@ static int l_SetWindowTitle(lua_State* L)
15341534
static int l_GetCursorPos(lua_State* L)
15351535
{
15361536
ui_main_c* ui = GetUIPtr(L);
1537-
const float dpiScale = ui->renderer->VirtualScreenScaleFactor();
1538-
lua_pushinteger(L, (lua_Integer)std::lround(ui->renderer->VirtualMap(ui->cursorX) / dpiScale));
1539-
lua_pushinteger(L, (lua_Integer)std::lround(ui->renderer->VirtualMap(ui->cursorY) / dpiScale));
1537+
lua_pushinteger(L, (lua_Integer)std::lround(ui->renderer->VirtualMap(ui->cursorX)));
1538+
lua_pushinteger(L, (lua_Integer)std::lround(ui->renderer->VirtualMap(ui->cursorY)));
15401539
return 2;
15411540
}
15421541

15431542
static int l_SetCursorPos(lua_State* L)
15441543
{
15451544
ui_main_c* ui = GetUIPtr(L);
15461545
int n = lua_gettop(L);
1547-
const float dpiScale = ui->renderer->VirtualScreenScaleFactor();
1546+
const float dpiScale = ui->renderer->APICoordScale();
15481547
ui->LAssert(L, n >= 2, "Usage: SetCursorPos(x, y)");
15491548
ui->LAssert(L, lua_isnumber(L, 1), "SetCursorPos() argument 1: expected number, got %s", luaL_typename(L, 1));
15501549
ui->LAssert(L, lua_isnumber(L, 2), "SetCursorPos() argument 2: expected number, got %s", luaL_typename(L, 2));

0 commit comments

Comments
 (0)