From 87a896f28ee8748851b1b11eee7c0eeaabc78b25 Mon Sep 17 00:00:00 2001 From: pionere Date: Thu, 29 Aug 2024 12:19:26 +0200 Subject: [PATCH] use arithmetic shift instead of signed div by 2 --- Source/DiabloUI/text_draw.cpp | 4 ++-- Source/control.cpp | 15 ++++++++------- Source/controls/modifier_hints.cpp | 8 ++++---- Source/debug.cpp | 4 ++++ Source/diablo.cpp | 2 +- Source/drlg_l4.cpp | 12 ++++++------ Source/gmenu.cpp | 9 +++++---- Source/scrollrt.cpp | 6 +++--- Source/storm/storm_svid.cpp | 4 ++-- tools/patcher/DiabloUI/text_draw.cpp | 4 ++-- tools/patcher/diablo.cpp | 2 +- 11 files changed, 38 insertions(+), 32 deletions(-) diff --git a/Source/DiabloUI/text_draw.cpp b/Source/DiabloUI/text_draw.cpp index d3dc081b0b0..9aa8cac7e89 100644 --- a/Source/DiabloUI/text_draw.cpp +++ b/Source/DiabloUI/text_draw.cpp @@ -10,7 +10,7 @@ DEVILUTION_BEGIN_NAMESPACE static int AlignXOffset(int flags, const SDL_Rect& dest, int w) { if (flags & UIS_HCENTER) - return (dest.w - w) / 2; + return (dest.w - w) >> 1; if (flags & UIS_RIGHT) return dest.w - w; return 0; @@ -57,7 +57,7 @@ void DrawArtStr(const char* text, const SDL_Rect& rect, int flags) } int x = rect.x + AlignXOffset(flags, rect, w) + SCREEN_X; - int y = rect.y + ((flags & UIS_VCENTER) ? (rect.h - h) / 2 : 0) + SCREEN_Y + h; + int y = rect.y + ((flags & UIS_VCENTER) ? ((rect.h - h) >> 1) : 0) + SCREEN_Y + h; y += dy; h += dy; diff --git a/Source/control.cpp b/Source/control.cpp index f3648296592..017c2f375e5 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -381,6 +381,7 @@ void DrawSkillIcons() case TGT_OBJECT: str = "Object"; numchar = lengthof("Object") - 1; + static_assert((lengthof("Object") - 1) * SMALL_FONT_HEIGHT <= 2 * SPLICON_WIDTH, "DrawSkillIcons uses unsigned division to calculate sy."); break; case TGT_OTHER: str = "Other"; @@ -399,12 +400,12 @@ void DrawSkillIcons() } // PrintSmallVerticalStr centered int sx = PANEL_X + PANEL_WIDTH - SMALL_FONT_HEIGHT - 2; - int sy = PANEL_Y + PANEL_HEIGHT - 2 * SPLICON_WIDTH + (2 * SPLICON_WIDTH - numchar * SMALL_FONT_HEIGHT) / 2; + int sy = PANEL_Y + PANEL_HEIGHT - 2 * SPLICON_WIDTH + (2 * SPLICON_WIDTH - numchar * SMALL_FONT_HEIGHT) / 2u; for (unsigned i = 0; i < numchar; i++) { sy += SMALL_FONT_HEIGHT; BYTE nCel = gbStdFontFrame[str[i]]; // PrintSmallChar(sx + (SMALL_FONT_WIDTH - smallFontWidth[nCel]) / 2, sy, str[i], COL_GOLD); - PrintSmallColorChar(sx + (13 - smallFontWidth[nCel]) / 2, sy, nCel, COL_GOLD); + PrintSmallColorChar(sx + (13 - smallFontWidth[nCel]) / 2u, sy, nCel, COL_GOLD); } } @@ -1462,7 +1463,7 @@ static int DrawTooltip2(const char* text1, const char* text2, int x, int y, BYTE if (y < 0) return result; - x -= width / 2; + x -= width / 2u; if (x < 0) { result = -x; x = 0; @@ -1532,8 +1533,8 @@ static POS32 GetMousePos(int x, int y) pos.y <<= 1; } - pos.x += SCREEN_WIDTH / 2; - pos.y += VIEWPORT_HEIGHT / 2; + pos.x += SCREEN_WIDTH / 2u; + pos.y += VIEWPORT_HEIGHT / 2u; return pos; } @@ -1569,7 +1570,7 @@ static int DrawTooltip(const char* text, int x, int y, BYTE col) if (y < 0) return result; - x -= width / 2; + x -= width / 2u; if (x < 0) { result = -x; x = 0; @@ -1831,7 +1832,7 @@ void DrawInfoStr() DrawTrigInfo(); } else if (pcursicon >= CURSOR_FIRSTITEM) { GetItemInfo(&myplr._pHoldItem); - DrawTooltip(infostr, MousePos.x + cursW / 2, MousePos.y - TOOLTIP_OFFSET, infoclr); + DrawTooltip(infostr, MousePos.x + cursW / 2u, MousePos.y - TOOLTIP_OFFSET, infoclr); } } diff --git a/Source/controls/modifier_hints.cpp b/Source/controls/modifier_hints.cpp index 903c6cba22c..14aa2e9740b 100644 --- a/Source/controls/modifier_hints.cpp +++ b/Source/controls/modifier_hints.cpp @@ -32,11 +32,11 @@ struct CircleMenuHint { , bottom(bottom) , left(left) { - int left_w = CalculateTextWidth(left); - int x_mid = left_w + SpaceWidth() * 5 / 2; + int left_w = CalculateTextWidth(left); // std::max(CalculateTextWidth(left), CalculateTextWidth(right)); + int x_mid = left_w + SpaceWidth() * 5 / 2u; width = 2 * x_mid; - top_offx = x_mid - CalculateTextWidth(top) / 2; - bottom_offx = x_mid - CalculateTextWidth(bottom) / 2; + top_offx = x_mid - CalculateTextWidth(top) / 2u; + bottom_offx = x_mid - CalculateTextWidth(bottom) / 2u; right_offx = left_w + SpaceWidth() * 5; } diff --git a/Source/debug.cpp b/Source/debug.cpp index a4bc889bce9..4acf72d072d 100644 --- a/Source/debug.cpp +++ b/Source/debug.cpp @@ -251,6 +251,10 @@ void ValidateData() if (gbStdFontFrame[i] >= lengthof(smallFontWidth)) app_fatal("Width of the small font %d ('%c') is undefined (frame number: %d).", i, i, gbStdFontFrame[i]); } + for (i = 0; i < lengthof(smallFontWidth); i++) { + if (smallFontWidth[i] > 13) + app_fatal("Width of the small font %d is too high.", i); // required by DrawSkillIcons + } if (GetHugeStringWidth("Pause") != 135) app_fatal("gmenu_draw_pause expects hardcoded width 135."); diff --git a/Source/diablo.cpp b/Source/diablo.cpp index 732b97f0d33..9a079492c18 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -141,7 +141,7 @@ static int diablo_parse_flags(int argc, char** argv) static void diablo_init_screen() { - MousePos.x = SCREEN_WIDTH / 2; + MousePos.x = SCREEN_WIDTH / 2u; MousePos.y = MAINMENU_TOP + MAINMENU_ITEM_HEIGHT / 2; #if HAS_GAMECTRL || HAS_JOYSTICK || HAS_KBCTRL || HAS_DPAD if (!sgbControllerActive) diff --git a/Source/drlg_l4.cpp b/Source/drlg_l4.cpp index 4a3ebe36d3e..846c990f910 100644 --- a/Source/drlg_l4.cpp +++ b/Source/drlg_l4.cpp @@ -1374,7 +1374,7 @@ static void L4RoomGen(int x, int y, int w, int h, bool dir) for (i = 20; i != 0; i--) { width = RandRange(2, 6) & ~1; height = RandRange(2, 6) & ~1; - ry = h / 2 + y - height / 2; + ry = h / 2u + y - height / 2u; rx = x - width; if (L4CheckVHall(x, ry - 1, height + 2) && L4CheckRoom(rx - 1, ry - 1, width + 1, height + 2)) /// BUGFIX: swap args 3 and 4 ("ch+2" and "cw+1") (fixed) @@ -1400,7 +1400,7 @@ static void L4RoomGen(int x, int y, int w, int h, bool dir) for (i = 20; i != 0; i--) { width = RandRange(2, 6) & ~1; height = RandRange(2, 6) & ~1; - rx = w / 2 + x - width / 2; + rx = w / 2u + x - width / 2u; ry = y - height; if (L4CheckHHall(y, rx - 1, width + 2) && L4CheckRoom(rx - 1, ry - 1, width + 2, height + 1)) @@ -1959,21 +1959,21 @@ static void DRLG_L4ThemeExitFix() y2 = themes[i]._tsy2; switch (random_(0, 4)) { case 0: - yy = (y1 + y2 + 1) / 2; + yy = (y1 + y2 + 1) / 2u; dungeon[x1][yy - 1] = 53; dungeon[x1][yy + 0] = 50; dungeon[x1][yy + 1] = 52; //dungeon[x2 - 1][yy - 1] = 54; break; case 1: - yy = (y1 + y2 + 1) / 2; + yy = (y1 + y2 + 1) / 2u; dungeon[x2][yy - 1] = 53; dungeon[x2][yy + 0] = 50; dungeon[x2][yy + 1] = 52; //dungeon[x2 - 1][yy - 1] = 54; break; case 2: - xx = (x1 + x2 + 1) / 2; + xx = (x1 + x2 + 1) / 2u; dungeon[xx - 1][y1] = 57; dungeon[xx + 0][y1] = 50; dungeon[xx + 1][y1] = 56; @@ -1981,7 +1981,7 @@ static void DRLG_L4ThemeExitFix() //dungeon[xx - 1][y2 - 1] = 58; break; case 3: - xx = (x1 + x2 + 1) / 2; + xx = (x1 + x2 + 1) / 2u; dungeon[xx - 1][y2] = 57; dungeon[xx + 0][y2] = 50; dungeon[xx + 1][y2] = 56; diff --git a/Source/gmenu.cpp b/Source/gmenu.cpp index d01b7dda045..a7395f7d74f 100644 --- a/Source/gmenu.cpp +++ b/Source/gmenu.cpp @@ -182,8 +182,8 @@ static TMenuItem* current_menu_item(bool activate) if (!(pItem->dwFlags & GMF_ENABLED)) { return NULL; } - w = gmenu_get_lfont(pItem) / 2; - if (abs(MousePos.x - SCREEN_WIDTH / 2) > w) + w = gmenu_get_lfont(pItem) / 2u; + if (abs(MousePos.x - (int)(SCREEN_WIDTH / 2u)) > w) return NULL; if (activate) @@ -285,7 +285,7 @@ static void gmenu_mouse_slider() TMenuItem* pItem; int offset; - offset = MousePos.x - (SCREEN_WIDTH / 2 - SLIDER_ROW_WIDTH / 2 + SLIDER_OFFSET + SLIDER_BORDER + SLIDER_BUTTON_WIDTH / 2); + offset = MousePos.x - (SCREEN_WIDTH / 2u - SLIDER_ROW_WIDTH / 2 + SLIDER_OFFSET + SLIDER_BORDER + SLIDER_BUTTON_WIDTH / 2); if (offset < 0) { if (offset < -(SLIDER_BUTTON_WIDTH / 2)) return; @@ -351,8 +351,9 @@ void gmenu_slider_set(TMenuItem* pItem, int min, int max, int value) int nSteps; //assert(pItem != NULL); + //assert(max > min); nSteps = pItem->wMenuParam1; - pItem->wMenuParam2 = ((max - min) / 2 + (value - min) * nSteps) / (max - min); + pItem->wMenuParam2 = ((max - min) / 2u + (value - min) * nSteps) / (max - min); } int gmenu_slider_get(TMenuItem* pItem, int min, int max) diff --git a/Source/scrollrt.cpp b/Source/scrollrt.cpp index 39963d35dc4..dc2dba75190 100644 --- a/Source/scrollrt.cpp +++ b/Source/scrollrt.cpp @@ -1253,14 +1253,14 @@ static void scrollrt_draw(int x, int y, int sx, int sy, int rows, int columns) */ static void Zoom() { - int wdt = SCREEN_WIDTH / 2; - int nSrcOff = SCREENXY(SCREEN_WIDTH / 2 - 1, VIEWPORT_HEIGHT / 2 - 1); + int wdt = SCREEN_WIDTH / 2u; + int nSrcOff = SCREENXY(SCREEN_WIDTH / 2u - 1, VIEWPORT_HEIGHT / 2u - 1); int nDstOff = SCREENXY(SCREEN_WIDTH - 1, VIEWPORT_HEIGHT - 1); BYTE* src = &gpBuffer[nSrcOff]; BYTE* dst = &gpBuffer[nDstOff]; - for (int hgt = 0; hgt < VIEWPORT_HEIGHT / 2; hgt++) { + for (unsigned hgt = 0; hgt < VIEWPORT_HEIGHT / 2u; hgt++) { for (int i = 0; i < wdt; i++) { *dst-- = *src; *dst-- = *src; diff --git a/Source/storm/storm_svid.cpp b/Source/storm/storm_svid.cpp index 617d4d8d125..c0db91f0b16 100644 --- a/Source/storm/storm_svid.cpp +++ b/Source/storm/storm_svid.cpp @@ -420,8 +420,8 @@ bool SVidPlayContinue() outputRect.w = SVidWidth * outputSurface->h / SVidHeight; outputRect.h = outputSurface->h; } - outputRect.x = (outputSurface->w - outputRect.w) / 2; - outputRect.y = (outputSurface->h - outputRect.h) / 2; + outputRect.x = (outputSurface->w - outputRect.w) >> 1; + outputRect.y = (outputSurface->h - outputRect.h) >> 1; if (isIndexedOutputFormat || outputSurface->w == static_cast(SVidWidth) diff --git a/tools/patcher/DiabloUI/text_draw.cpp b/tools/patcher/DiabloUI/text_draw.cpp index d3dc081b0b0..9aa8cac7e89 100644 --- a/tools/patcher/DiabloUI/text_draw.cpp +++ b/tools/patcher/DiabloUI/text_draw.cpp @@ -10,7 +10,7 @@ DEVILUTION_BEGIN_NAMESPACE static int AlignXOffset(int flags, const SDL_Rect& dest, int w) { if (flags & UIS_HCENTER) - return (dest.w - w) / 2; + return (dest.w - w) >> 1; if (flags & UIS_RIGHT) return dest.w - w; return 0; @@ -57,7 +57,7 @@ void DrawArtStr(const char* text, const SDL_Rect& rect, int flags) } int x = rect.x + AlignXOffset(flags, rect, w) + SCREEN_X; - int y = rect.y + ((flags & UIS_VCENTER) ? (rect.h - h) / 2 : 0) + SCREEN_Y + h; + int y = rect.y + ((flags & UIS_VCENTER) ? ((rect.h - h) >> 1) : 0) + SCREEN_Y + h; y += dy; h += dy; diff --git a/tools/patcher/diablo.cpp b/tools/patcher/diablo.cpp index f80fc6719ea..aa7c15b1d44 100644 --- a/tools/patcher/diablo.cpp +++ b/tools/patcher/diablo.cpp @@ -42,7 +42,7 @@ static int diablo_parse_flags(int argc, char** argv) static void diablo_init_screen() { - MousePos.x = SCREEN_WIDTH / 2; + MousePos.x = SCREEN_WIDTH / 2u; MousePos.y = MAINMENU_TOP + MAINMENU_ITEM_HEIGHT / 2; #if HAS_GAMECTRL || HAS_JOYSTICK || HAS_KBCTRL || HAS_DPAD if (!sgbControllerActive)