Refactor min/max usage

This commit is contained in:
Alexander Batalov 2023-01-03 23:00:38 +03:00
parent a5cefd6c8b
commit 9ee4cb4a26
12 changed files with 57 additions and 97 deletions

View File

@ -5,6 +5,7 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include "art.h"
@ -4937,13 +4938,6 @@ static char* _itostndn(int value, char* dest)
// 0x43AAEC
static int characterEditorDrawCardWithOptions(int graphicId, const char* name, const char* attributes, char* description)
{
unsigned char* ptr;
int v9;
int x;
int y;
short beginnings[WORD_WRAP_MAX_COUNT];
short beginningsCount;
FrmImage frmImage;
int fid = buildFid(OBJ_TYPE_SKILLDEX, graphicId, 0, 0, 0);
if (!frmImage.lock(fid)) {
@ -4957,20 +4951,20 @@ static int characterEditorDrawCardWithOptions(int graphicId, const char* name, c
gCharacterEditorWindowBuffer + 640 * 309 + 484,
640);
v9 = 150;
ptr = frmImage.getData();
for (y = 0; y < frmImage.getHeight(); y++) {
for (x = 0; x < frmImage.getWidth(); x++) {
if (HighRGB(*ptr) < 2 && v9 >= x) {
v9 = x;
int extraDescriptionWidth = 150;
unsigned char* data = frmImage.getData();
for (int y = 0; y < frmImage.getHeight(); y++) {
for (int x = 0; x < frmImage.getWidth(); x++) {
if (HighRGB(*data) < 2) {
extraDescriptionWidth = std::min(extraDescriptionWidth, x);
}
ptr++;
data++;
}
}
v9 -= 8;
if (v9 < 0) {
v9 = 0;
extraDescriptionWidth -= 8;
if (extraDescriptionWidth < 0) {
extraDescriptionWidth = 0;
}
fontSetCurrent(102);
@ -4985,20 +4979,21 @@ static int characterEditorDrawCardWithOptions(int graphicId, const char* name, c
fontDrawText(gCharacterEditorWindowBuffer + 640 * (268 + nameFontLineHeight - attributesFontLineHeight) + 348 + nameWidth + 8, attributes, 640, 640, _colorTable[0]);
}
y = nameFontLineHeight;
windowDrawLine(gCharacterEditorWindow, 348, y + 272, 613, y + 272, _colorTable[0]);
windowDrawLine(gCharacterEditorWindow, 348, y + 273, 613, y + 273, _colorTable[0]);
windowDrawLine(gCharacterEditorWindow, 348, nameFontLineHeight + 272, 613, nameFontLineHeight + 272, _colorTable[0]);
windowDrawLine(gCharacterEditorWindow, 348, nameFontLineHeight + 273, 613, nameFontLineHeight + 273, _colorTable[0]);
fontSetCurrent(101);
int descriptionFontLineHeight = fontGetLineHeight();
if (wordWrap(description, v9 + 136, beginnings, &beginningsCount) != 0) {
short beginnings[WORD_WRAP_MAX_COUNT];
short beginningsCount;
if (wordWrap(description, extraDescriptionWidth + 136, beginnings, &beginningsCount) != 0) {
// TODO: Leaking graphic handle.
return -1;
}
y = 315;
int y = 315;
for (short i = 0; i < beginningsCount - 1; i++) {
short beginning = beginnings[i];
short ending = beginnings[i + 1];
@ -6660,9 +6655,7 @@ static int perkDialogDrawCard(int frmId, const char* name, const char* rank, cha
unsigned char* stride = data;
for (int x = 0; x < frmImage.getWidth(); x++) {
if (HighRGB(*stride) < 2) {
if (extraDescriptionWidth > x) {
extraDescriptionWidth = x;
}
extraDescriptionWidth = std::min(extraDescriptionWidth, x);
}
stride++;
}

View File

@ -2,6 +2,8 @@
#include <string.h>
#include <algorithm>
#include "art.h"
#include "color.h"
#include "cycle.h"
@ -112,7 +114,7 @@ void creditsOpen(const char* filePath, int backgroundFid, bool useReversedStyle)
fontSetCurrent(gCreditsWindowNameFont);
int nameFontLineHeight = fontGetLineHeight();
int lineHeight = nameFontLineHeight + (titleFontLineHeight >= nameFontLineHeight ? titleFontLineHeight - nameFontLineHeight : 0);
int lineHeight = std::max(titleFontLineHeight, nameFontLineHeight);
int stringBufferSize = windowWidth * lineHeight;
unsigned char* stringBuffer = (unsigned char*)internal_malloc(stringBufferSize);
if (stringBuffer != NULL) {

View File

@ -178,7 +178,7 @@ int showDialogBox(const char* title, const char** body, int bodyLength, int x, i
int linesCount = 0;
for (int index = 0; index < bodyLength; index++) {
// NOTE: Calls [fontGetStringWidth] twice because of [max] macro.
// NOTE: Originally there is no `max` macro.
maximumLineWidth = std::max(fontGetStringWidth(body[index]), maximumLineWidth);
linesCount++;
}

View File

@ -5,6 +5,8 @@
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <fpattern.h>
#include "platform_compat.h"
@ -818,10 +820,7 @@ static bool dfileReadCompressed(DFile* stream, void* ptr, size_t size)
if (stream->decompressionStream->avail_in == 0) {
// No more unprocessed data, request next chunk.
size_t bytesToRead = stream->entry->dataSize - stream->compressedBytesRead;
if (bytesToRead > DFILE_DECOMPRESSION_BUFFER_SIZE) {
bytesToRead = DFILE_DECOMPRESSION_BUFFER_SIZE;
}
size_t bytesToRead = std::min(DFILE_DECOMPRESSION_BUFFER_SIZE, stream->entry->dataSize - stream->compressedBytesRead);
if (fread(stream->decompressionBuffer, bytesToRead, 1, stream->stream) != 1) {
break;

View File

@ -4,6 +4,8 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include "actions.h"
#include "animation.h"
#include "art.h"
@ -802,22 +804,17 @@ void gameMouseRefresh()
char formattedActionPoints[8];
int color;
int v6 = _make_path(gDude, gDude->tile, gGameMouseHexCursor->tile, NULL, 1);
if (v6) {
int distance = _make_path(gDude, gDude->tile, gGameMouseHexCursor->tile, NULL, 1);
if (distance != 0) {
if (!isInCombat()) {
formattedActionPoints[0] = '\0';
color = _colorTable[31744];
} else {
int v7 = critterGetMovementPointCostAdjustedForCrippledLegs(gDude, v6);
int v8;
if (v7 - _combat_free_move >= 0) {
v8 = v7 - _combat_free_move;
} else {
v8 = 0;
}
int actionPointsMax = critterGetMovementPointCostAdjustedForCrippledLegs(gDude, distance);
int actionPointsRequired = std::max(0, actionPointsMax - _combat_free_move);
if (v8 <= gDude->data.critter.combat.ap) {
snprintf(formattedActionPoints, sizeof(formattedActionPoints), "%d", v8);
if (actionPointsRequired <= gDude->data.critter.combat.ap) {
snprintf(formattedActionPoints, sizeof(formattedActionPoints), "%d", actionPointsRequired);
color = _colorTable[32767];
} else {
snprintf(formattedActionPoints, sizeof(formattedActionPoints), "%c", 'X');

View File

@ -407,9 +407,8 @@ void grayscalePaletteUpdate(int a1, int a2)
{
if (a1 >= 0 && a2 <= 255) {
for (int index = a1; index <= a2; index++) {
// NOTE: The only way to explain so much calls to `Color2RGB` with
// the same repeated pattern is by the use of min/max macros.
// NOTE: Calls `Color2RGB` many times due to `min` and `max` macro
// uses.
int v1 = std::max((Color2RGB(index) & 0x7C00) >> 10, std::max((Color2RGB(index) & 0x3E0) >> 5, Color2RGB(index) & 0x1F));
int v2 = std::min((Color2RGB(index) & 0x7C00) >> 10, std::min((Color2RGB(index) & 0x3E0) >> 5, Color2RGB(index) & 0x1F));
int v3 = v1 + v2;

View File

@ -1355,7 +1355,6 @@ int _intface_update_ammo_lights()
int ratio = 0;
if (p->isWeapon != 0) {
// calls sub_478674 twice, probably because if min/max kind macro
int maximum = ammoGetCapacity(p->item);
if (maximum > 0) {
int current = ammoGetQuantity(p->item);
@ -1363,7 +1362,6 @@ int _intface_update_ammo_lights()
}
} else {
if (itemGetType(p->item) == ITEM_TYPE_MISC) {
// calls sub_4793D0 twice, probably because if min/max kind macro
int maximum = miscItemGetMaxCharges(p->item);
if (maximum > 0) {
int current = miscItemGetCharges(p->item);

View File

@ -1947,8 +1947,6 @@ static void _display_inventory_info(Object* item, int quantity, unsigned char* d
v9 -= 1;
}
// NOTE: Checking for quantity twice probably means inlined function
// or some macro expansion.
if (quantity > 1) {
if (v9 > 99999) {
v9 = 99999;

View File

@ -1,6 +1,6 @@
#include "light.h"
#include <math.h>
#include <algorithm>
#include "map_defs.h"
#include "object.h"
@ -55,7 +55,7 @@ void lightSetLightLevel(int lightLevel, bool shouldUpdateScreen)
}
}
// TODO: Looks strange - it tries to clamp intensity as light level?
// 0x47A980
int _light_get_tile(int elevation, int tile)
{
if (!elevationIsValid(elevation)) {
@ -66,13 +66,7 @@ int _light_get_tile(int elevation, int tile)
return 0;
}
int result = gLightIntensity[elevation][tile];
if (result >= 0x10000) {
result = 0x10000;
}
return result;
return std::min(gLightIntensity[elevation][tile], LIGHT_LEVEL_MAX);
}
// 0x47A9C4

View File

@ -3,6 +3,8 @@
#include <assert.h>
#include <string.h>
#include <algorithm>
#include "animation.h"
#include "art.h"
#include "color.h"
@ -808,13 +810,8 @@ void _obj_render_pre_roof(Rect* rect, int elevation)
? gObjectListHeadByTile[topLeftTile + offsets[offsetIndex]]
: NULL;
if (objectListNode != NULL) {
// NOTE: calls _light_get_tile two times, probably result of min/max macro
int tileLight = _light_get_tile(elevation, objectListNode->obj->tile);
if (tileLight >= ambientLight) {
light = tileLight;
} else {
light = ambientLight;
}
// NOTE: Calls `_light_get_tile` twice.
light = std::max(ambientLight, _light_get_tile(elevation, objectListNode->obj->tile));
}
while (objectListNode != NULL) {
@ -852,13 +849,8 @@ void _obj_render_pre_roof(Rect* rect, int elevation)
ObjectListNode* objectListNode = _renderTable[i];
if (objectListNode != NULL) {
// NOTE: calls _light_get_tile two times, probably result of min/max macro
int tileLight = _light_get_tile(elevation, objectListNode->obj->tile);
if (tileLight >= ambientLight) {
light = tileLight;
} else {
light = ambientLight;
}
// NOTE: Calls `_light_get_tile` twice.
light = std::max(ambientLight, _light_get_tile(elevation, objectListNode->obj->tile));
}
while (objectListNode != NULL) {
@ -1743,34 +1735,28 @@ void _obj_rebuild_all_light()
// 0x48AC90
int objectSetLight(Object* obj, int lightDistance, int lightIntensity, Rect* rect)
{
int v7;
Rect new_rect;
if (obj == NULL) {
return -1;
}
v7 = _obj_turn_off_light(obj, rect);
int rc = _obj_turn_off_light(obj, rect);
if (lightIntensity > 0) {
if (lightDistance >= 8) {
lightDistance = 8;
}
obj->lightDistance = std::min(lightDistance, 8);
obj->lightIntensity = lightIntensity;
obj->lightDistance = lightDistance;
if (rect != NULL) {
v7 = _obj_turn_on_light(obj, &new_rect);
rectUnion(rect, &new_rect, rect);
Rect tempRect;
rc = _obj_turn_on_light(obj, &tempRect);
rectUnion(rect, &tempRect, rect);
} else {
v7 = _obj_turn_on_light(obj, NULL);
rc = _obj_turn_on_light(obj, NULL);
}
} else {
obj->lightIntensity = 0;
obj->lightDistance = 0;
}
return v7;
return rc;
}
// 0x48AD04

View File

@ -206,11 +206,8 @@ int textObjectAdd(Object* object, char* string, int font, int color, int a5, Rec
char c = *ending;
*ending = '\0';
// NOTE: Calls [fontGetStringWidth] twice, probably result of using min/max macro
int width = fontGetStringWidth(beginning);
if (width >= textObject->width) {
textObject->width = width;
}
// NOTE: Calls `fontGetStringWidth` twice.
textObject->width = std::max(textObject->width, fontGetStringWidth(beginning));
*ending = c;
}

View File

@ -4,6 +4,8 @@
#include <math.h>
#include <string.h>
#include <algorithm>
#include "art.h"
#include "color.h"
#include "config.h"
@ -1660,13 +1662,8 @@ static void tileRenderFloor(int fid, int x, int y, Rect* rect)
int parity = tile & 1;
int ambientIntensity = lightGetLightLevel();
for (int i = 0; i < 10; i++) {
// NOTE: calling _light_get_tile two times, probably a result of using __min kind macro
int tileIntensity = _light_get_tile(elev, tile + _verticies[i].offsets[parity]);
if (tileIntensity <= ambientIntensity) {
tileIntensity = ambientIntensity;
}
_verticies[i].intensity = tileIntensity;
// NOTE: Calls `_light_get_tile` twice.
_verticies[i].intensity = std::max(_light_get_tile(elev, tile + _verticies[i].offsets[parity]), ambientIntensity);
}
int v23 = 0;