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

View File

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