Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/build/
/build/
/build-SimpleGraphic
/.idea
2 changes: 2 additions & 0 deletions engine/render.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class r_IRenderer {
virtual int VirtualScreenWidth() = 0;
virtual int VirtualScreenHeight() = 0;
virtual float VirtualScreenScaleFactor() = 0;
virtual void SetDpiScaleOverridePercent(int percent) = 0;
virtual int DpiScaleOverridePercent() const = 0;
virtual int VirtualMap(int properValue) = 0;
virtual int VirtualUnmap(int mappedValue) = 0;

Expand Down
56 changes: 31 additions & 25 deletions engine/render/r_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>

// =======
// Classes
Expand Down Expand Up @@ -144,12 +145,12 @@ int r_font_c::StringWidthInternal(f_fontHeight_s* fh, std::u32string_view str, i
int heightIdx = (int)(std::find(fontHeights, fontHeights + numFontHeight, fh) - fontHeights);
auto tofuFont = FindSmallerFontHeight(height, heightIdx, tofuSizeReduction);

auto measureCodepoint = [](f_fontHeight_s* fh, char32_t cp) {
auto& glyph = fh->Glyph((char)(unsigned char)cp);
auto measureCodepoint = [](f_fontHeight_s* glyphFh, char32_t cp) {
auto& glyph = glyphFh->Glyph((char)(unsigned char)cp);
return glyph.width + glyph.spLeft + glyph.spRight;
};

int width = 0;
float width = 0.0f;
for (size_t idx = 0; idx < str.size();) {
auto ch = str[idx];
int escLen = IsColorEscape(str.substr(idx));
Expand All @@ -158,23 +159,26 @@ int r_font_c::StringWidthInternal(f_fontHeight_s* fh, std::u32string_view str, i
}
else if (ch >= (unsigned)fh->numGlyph) {
auto tofu = BuildTofuString(ch);
for (auto ch : tofu) {
width += measureCodepoint(tofuFont.fh, ch);
for (auto cp : tofu) {
width += (float)measureCodepoint(tofuFont.fh, cp);
width = std::ceil(width);
}
++idx;
}
else if (ch == U'\t') {
auto& glyph = fh->Glyph(' ');
int spWidth = glyph.width + glyph.spLeft + glyph.spRight;
width += (int)(spWidth * 4 * scale);
width += (float)(spWidth << 2) * scale;
width = std::ceil(width);
++idx;
}
else {
width += (int)(measureCodepoint(fh, ch) * scale);
width += (float)measureCodepoint(fh, ch) * scale;
width = std::ceil(width);
++idx;
}
}
return width;
return (int)width;
}

int r_font_c::StringWidth(int height, std::u32string_view str)
Expand Down Expand Up @@ -203,12 +207,12 @@ size_t r_font_c::StringCursorInternal(f_fontHeight_s* fh, std::u32string_view st
int heightIdx = (int)(std::find(fontHeights, fontHeights + numFontHeight, fh) - fontHeights);
auto tofuFont = FindSmallerFontHeight(height, heightIdx, tofuSizeReduction);

auto measureCodepoint = [](f_fontHeight_s* fh, char32_t cp) {
auto& glyph = fh->Glyph((char)(unsigned char)cp);
auto measureCodepoint = [](f_fontHeight_s* glyphFh, char32_t cp) {
auto& glyph = glyphFh->Glyph((char)(unsigned char)cp);
return glyph.width + glyph.spLeft + glyph.spRight;
};

int x = 0;
float x = 0.0f;
auto I = str.begin();
auto lineEnd = std::find(I, str.end(), U'\n');
while (I != lineEnd) {
Expand All @@ -219,32 +223,34 @@ size_t r_font_c::StringCursorInternal(f_fontHeight_s* fh, std::u32string_view st
}
else if (*I >= (unsigned)fh->numGlyph) {
auto tofu = BuildTofuString(*I);
int tofuWidth = 0;
for (auto ch : tofu) {
tofuWidth += measureCodepoint(tofuFont.fh, ch);
}
int halfWidth = (int)(tofuWidth / 2.0f);
x += halfWidth;
if (curX <= x) {
break;
for (auto cp : tofu) {
x += (float)measureCodepoint(tofuFont.fh, cp);
x = std::ceil(x);
if (curX <= x) {
return std::distance(str.begin(), I);
}
}
x += tofuWidth - halfWidth;
++I;
}
else if (*I == U'\t') {
auto& glyph = fh->Glyph(' ');
int spWidth = glyph.width + glyph.spLeft + glyph.spRight;
int fullWidth = (int)(spWidth * 4 * scale);
int halfWidth = (int)(fullWidth / 2.0f);
float fullWidth = (float)(glyph.width + glyph.spLeft + glyph.spRight) * 4.0f * scale;
float halfWidth = std::ceil(fullWidth * 0.5f);
x += halfWidth;
x = std::ceil(x);
if (curX <= x) {
break;
}
x += fullWidth - halfWidth;
x = std::ceil(x);
if (curX <= x) {
break;
}
++I;
}
else {
x += (int)(measureCodepoint(fh, *I) * scale);
x += (float)measureCodepoint(fh, *I) * scale;
x = std::ceil(x);
if (curX <= x) {
break;
}
Expand Down Expand Up @@ -339,7 +345,7 @@ void r_font_c::DrawTextLine(scp_t pos, int align, int height, col4_t col, std::u

// Calculate the string position
float x = pos[X];
float y = pos[Y];
float y = std::floor(pos[Y]);
if (align != F_LEFT) {
// Calculate the real width of the string
float width = (float)StringWidthInternal(fh, str, height, scale);
Expand Down
20 changes: 18 additions & 2 deletions engine/render/r_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1852,20 +1852,36 @@ int r_renderer_c::DrawStringCursorIndex(int height, int font, const char* str, i
// ==============

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

int r_renderer_c::VirtualScreenHeight() {
return VirtualMap(sys->video->vid.size[1]);
int const properHeight = apiDpiAware ? sys->video->vid.fbSize[1] : sys->video->vid.size[1];
return VirtualMap(properHeight);
}

float r_renderer_c::VirtualScreenScaleFactor() {
if (apiDpiAware) {
if (dpiScaleOverridePercent > 0) {
return (float)dpiScaleOverridePercent / 100.0f;
}
return sys->video->vid.dpiScale;
}
return 1.0f;
}

void r_renderer_c::SetDpiScaleOverridePercent(int percent) {
if (percent < 0) {
percent = 0;
}
dpiScaleOverridePercent = percent;
}

int r_renderer_c::DpiScaleOverridePercent() const {
return dpiScaleOverridePercent;
}

int r_renderer_c::VirtualMap(int properValue) {
if (apiDpiAware) {
return properValue;
Expand Down
5 changes: 4 additions & 1 deletion engine/render/r_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class r_renderer_c: public r_IRenderer, public conCmdHandler_c {
int VirtualScreenWidth();
int VirtualScreenHeight();
float VirtualScreenScaleFactor();
void SetDpiScaleOverridePercent(int percent);
int DpiScaleOverridePercent() const;
int VirtualMap(int properValue);
int VirtualUnmap(int mappedValue);

Expand Down Expand Up @@ -126,7 +128,7 @@ class r_renderer_c: public r_IRenderer, public conCmdHandler_c {
PFNGLINSERTEVENTMARKEREXTPROC glInsertEventMarkerEXT = nullptr;
PFNGLPUSHGROUPMARKEREXTPROC glPushGroupMarkerEXT = nullptr;
PFNGLPOPGROUPMARKEREXTPROC glPopGroupMarkerEXT = nullptr;

conVar_c* r_compress = nullptr;
conVar_c* r_screenshotFormat = nullptr;
conVar_c* r_layerDebug = nullptr;
Expand Down Expand Up @@ -173,6 +175,7 @@ class r_renderer_c: public r_IRenderer, public conCmdHandler_c {
};

bool apiDpiAware{};
int dpiScaleOverridePercent = 0;
RenderTarget rttMain[2];
int presentRtt = 0;

Expand Down
67 changes: 53 additions & 14 deletions ui_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <filesystem>
#include <fstream>
#include <zlib.h>
#include <cmath>

#include "core/core_tex_manipulation.h"

Expand Down Expand Up @@ -1068,11 +1069,14 @@ static int l_DrawString(lua_State* L)
static const char* alignMap[6] = { "LEFT", "CENTER", "RIGHT", "CENTER_X", "RIGHT_X", NULL };
static const char* fontMap[4] = { "FIXED", "VAR", "VAR BOLD", NULL };
const float dpiScale = ui->renderer->VirtualScreenScaleFactor();
const float left = (float)lua_tonumber(L, 1) * dpiScale;
const float top = (float)lua_tonumber(L, 2) * dpiScale;
const int scaledHeight = (int)std::lround((double)lua_tointeger(L, 4) * (double)dpiScale);
ui->renderer->DrawString(
(float)lua_tonumber(L, 1) * dpiScale,
(float)lua_tonumber(L, 2) * dpiScale,
left,
top,
luaL_checkoption(L, 3, "LEFT", alignMap),
(int)lua_tointeger(L, 4) * dpiScale,
scaledHeight,
NULL,
luaL_checkoption(L, 5, "FIXED", fontMap),
lua_tostring(L, 6)
Expand All @@ -1091,9 +1095,13 @@ static int l_DrawStringWidth(lua_State* L)
ui->LAssert(L, lua_isstring(L, 3), "DrawStringWidth() argument 3: expected string, got %s", luaL_typename(L, 3));
static const char* fontMap[4] = { "FIXED", "VAR", "VAR BOLD", NULL };
const float dpiScale = ui->renderer->VirtualScreenScaleFactor();
lua_pushinteger(L, ui->renderer->DrawStringWidth((int)lua_tointeger(L, 1) * dpiScale,
luaL_checkoption(L, 2, "FIXED", fontMap),
lua_tostring(L, 3)));
const double logicalHeight = lua_tonumber(L, 1);
const int scaledHeight = (int)std::lround(logicalHeight * dpiScale);
double const physicalWidth = (double)ui->renderer->DrawStringWidth(
scaledHeight,
luaL_checkoption(L, 2, "FIXED", fontMap),
lua_tostring(L, 3));
lua_pushnumber(L, physicalWidth / dpiScale);
return 1;
}

Expand All @@ -1110,10 +1118,37 @@ static int l_DrawStringCursorIndex(lua_State* L)
ui->LAssert(L, lua_isnumber(L, 4), "DrawStringCursorIndex() argument 4: expected number, got %s", luaL_typename(L, 4));
ui->LAssert(L, lua_isnumber(L, 5), "DrawStringCursorIndex() argument 5: expected number, got %s", luaL_typename(L, 5));
static const char* fontMap[4] = { "FIXED", "VAR", "VAR BOLD", NULL };
lua_pushinteger(L, ui->renderer->DrawStringCursorIndex((int)lua_tointeger(L, 1) * dpiScale,
luaL_checkoption(L, 2, "FIXED", fontMap),
lua_tostring(L, 3),
(int)lua_tointeger(L, 4) * dpiScale, (int)lua_tointeger(L, 5) * dpiScale) + 1);
const double logicalHeight = lua_tonumber(L, 1);
const double logicalCursorX = lua_tonumber(L, 4);
const double logicalCursorY = lua_tonumber(L, 5);
const int scaledHeight = (int)std::lround(logicalHeight * dpiScale);
const int scaledCursorX = (int)std::lround(logicalCursorX * dpiScale);
const int scaledCursorY = (int)std::lround(logicalCursorY * dpiScale);
lua_pushinteger(L, ui->renderer->DrawStringCursorIndex(
scaledHeight,
luaL_checkoption(L, 2, "FIXED", fontMap),
lua_tostring(L, 3),
scaledCursorX, scaledCursorY) + 1);
return 1;
}

static int l_SetDPIScaleOverridePercent(lua_State* L)
{
ui_main_c* ui = GetUIPtr(L);
ui->LAssert(L, ui->renderer != NULL, "Renderer is not initialised");
int n = lua_gettop(L);
ui->LAssert(L, n >= 1, "Usage: SetDPIScaleOverridePercent(percent)");
ui->LAssert(L, lua_isnumber(L, 1), "SetDPIScaleOverridePercent() argument 1: expected number, got %s", luaL_typename(L, 1));
int percent = (int)lua_tointeger(L, 1);
ui->renderer->SetDpiScaleOverridePercent(percent);
return 0;
}

static int l_GetDPIScaleOverridePercent(lua_State* L)
{
ui_main_c* ui = GetUIPtr(L);
ui->LAssert(L, ui->renderer != NULL, "Renderer is not initialised");
lua_pushinteger(L, ui->renderer->DpiScaleOverridePercent());
return 1;
}

Expand Down Expand Up @@ -1396,8 +1431,8 @@ static int l_GetCursorPos(lua_State* L)
{
ui_main_c* ui = GetUIPtr(L);
const float dpiScale = ui->renderer->VirtualScreenScaleFactor();
lua_pushinteger(L, ui->renderer->VirtualMap(ui->cursorX) / dpiScale);
lua_pushinteger(L, ui->renderer->VirtualMap(ui->cursorY) / dpiScale);
lua_pushinteger(L, (lua_Integer)std::lround((double)ui->renderer->VirtualMap(ui->cursorX) / (double)dpiScale));
lua_pushinteger(L, (lua_Integer)std::lround((double)ui->renderer->VirtualMap(ui->cursorY) / (double)dpiScale));
return 2;
}

Expand All @@ -1409,8 +1444,10 @@ static int l_SetCursorPos(lua_State* L)
ui->LAssert(L, n >= 2, "Usage: SetCursorPos(x, y)");
ui->LAssert(L, lua_isnumber(L, 1), "SetCursorPos() argument 1: expected number, got %s", luaL_typename(L, 1));
ui->LAssert(L, lua_isnumber(L, 2), "SetCursorPos() argument 2: expected number, got %s", luaL_typename(L, 2));
int x = ui->renderer->VirtualUnmap((int)lua_tointeger(L, 1) * dpiScale);
int y = ui->renderer->VirtualUnmap((int)lua_tointeger(L, 2) * dpiScale);
const int scaledX = (int)std::lround((double)lua_tointeger(L, 1) * (double)dpiScale);
const int scaledY = (int)std::lround((double)lua_tointeger(L, 2) * (double)dpiScale);
int x = ui->renderer->VirtualUnmap(scaledX);
int y = ui->renderer->VirtualUnmap(scaledY);
ui->sys->video->SetRelativeCursor(x, y);
return 0;
}
Expand Down Expand Up @@ -2129,6 +2166,8 @@ int ui_main_c::InitAPI(lua_State* L)
ADDFUNC(SetViewport);
ADDFUNC(SetBlendMode);
ADDFUNC(SetDrawColor);
ADDFUNC(SetDPIScaleOverridePercent);
ADDFUNC(GetDPIScaleOverridePercent);
ADDFUNC(DrawImage);
ADDFUNC(DrawImageQuad);
ADDFUNC(DrawString);
Expand Down
Loading