Skip to content

Commit

Permalink
use arithmetic shift instead of signed div by 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pionere committed Aug 29, 2024
1 parent 2211943 commit 87a896f
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Source/DiabloUI/text_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 8 additions & 7 deletions Source/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
8 changes: 4 additions & 4 deletions Source/controls/modifier_hints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions Source/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
2 changes: 1 addition & 1 deletion Source/diablo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions Source/drlg_l4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -1959,29 +1959,29 @@ 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;
//dungeon[xx + 0][y2 - 1] = 59;
//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;
Expand Down
9 changes: 5 additions & 4 deletions Source/gmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions Source/scrollrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Source/storm/storm_svid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(SVidWidth)
Expand Down
4 changes: 2 additions & 2 deletions tools/patcher/DiabloUI/text_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tools/patcher/diablo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 87a896f

Please sign in to comment.