1
0
Fork 0

Codechange: use std::source_location over __FILE__ and __LINE__ for TILE_ADD(XY)

pull/12259/head
Rubidium 2024-01-17 04:06:51 +01:00
parent 9c95fbdb07
commit 4dc99fc8d9
2 changed files with 27 additions and 31 deletions

View File

@ -69,32 +69,26 @@ extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned);
#ifdef _DEBUG
TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
const char *exp, const char *file, int line)
TileIndex TILE_ADD(TileIndex tile, TileIndexDiff offset, std::source_location location)
{
int dx;
int dy;
uint x;
uint y;
dx = add & Map::MaxX();
int dx = offset & Map::MaxX();
if (dx >= (int)Map::SizeX() / 2) dx -= Map::SizeX();
dy = (add - dx) / (int)Map::SizeX();
int dy = (offset - dx) / (int)Map::SizeX();
x = TileX(tile) + dx;
y = TileY(tile) + dy;
uint x = TileX(tile) + dx;
uint y = TileY(tile) + dy;
if (x >= Map::SizeX() || y >= Map::SizeY()) {
std::string message = fmt::format("TILE_ADD({}) when adding 0x{:04X} and 0x{:04X} failed",
exp, tile, add);
std::string message = fmt::format("TILE_ADD when adding 0x{:04X} and 0x{:04X} failed",
tile, offset);
#if !defined(_MSC_VER)
fmt::print(stderr, "{}:{} {}\n", file, line, message);
fmt::print(stderr, "{}:{}:{} {}\n", location.file_name(), location.line(), location.column(), message);
#else
_assert(message.data(), (char*)file, line);
_assert(message.data(), (char*)location.file_name(), location.line());
#endif
}
assert(TileXY(x, y) == Map::WrapToMap(tile + add));
assert(TileXY(x, y) == Map::WrapToMap(tile + offset));
return TileXY(x, y);
}

View File

@ -455,29 +455,31 @@ inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
}
/**
* Adds a given offset to a tile.
*
* @param tile The tile to add an offset to.
* @param offset The offset to add.
* @return The resulting tile.
*/
#ifndef _DEBUG
/**
* Adds two tiles together.
*
* @param x One tile
* @param y Another tile to add
* @return The resulting tile(index)
*/
# define TILE_ADD(x, y) ((x) + (y))
constexpr TileIndex TILE_ADD(TileIndex tile, TileIndexDiff offset, [[maybe_unused]] const std::source_location location = std::source_location::current()) { return tile + offset; }
#else
extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
const char *exp, const char *file, int line);
# define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__))
TileIndex TILE_ADD(TileIndex tile, TileIndexDiff offset, const std::source_location location = std::source_location::current());
#endif
/**
* Adds a given offset to a tile.
*
* @param tile The tile to add an offset on it
* @param x The x offset to add to the tile
* @param y The y offset to add to the tile
* @param tile The tile to add an offset to.
* @param x The x offset to add to the tile.
* @param y The y offset to add to the tile.
* @return The resulting tile.
*/
#define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y))
inline TileIndex TILE_ADDXY(TileIndex tile, int x, int y, const std::source_location location = std::source_location::current())
{
return TILE_ADD(tile, TileDiffXY(x, y), location);
}
TileIndex TileAddWrap(TileIndex tile, int addx, int addy);