Codechange: simplify TileAdd by using simple asserts

This commit is contained in:
Rubidium 2024-01-17 04:06:51 +01:00 committed by rubidium42
parent 005892bfdb
commit df461b0329
2 changed files with 10 additions and 30 deletions

View File

@ -17,11 +17,6 @@
#include "safeguards.h" #include "safeguards.h"
#if defined(_MSC_VER)
/* Why the hell is that not in all MSVC headers?? */
extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned);
#endif
/* static */ uint Map::log_x; ///< 2^_map_log_x == _map_size_x /* static */ uint Map::log_x; ///< 2^_map_log_x == _map_size_x
/* static */ uint Map::log_y; ///< 2^_map_log_y == _map_size_y /* static */ uint Map::log_y; ///< 2^_map_log_y == _map_size_y
/* static */ uint Map::size_x; ///< Size of the map along the X /* static */ uint Map::size_x; ///< Size of the map along the X
@ -69,32 +64,18 @@ extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned);
#ifdef _DEBUG #ifdef _DEBUG
TileIndex TileAdd(TileIndex tile, TileIndexDiff add, TileIndex TileAdd(TileIndex tile, TileIndexDiff offset)
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; uint32_t x = TileX(tile) + dx;
y = TileY(tile) + dy; uint32_t y = TileY(tile) + dy;
if (x >= Map::SizeX() || y >= Map::SizeY()) { assert(x < Map::SizeX());
std::string message = fmt::format("TILE_ADD({}) when adding 0x{:04X} and 0x{:04X} failed", assert(y < Map::SizeY());
exp, tile, add); assert(TileXY(x, y) == Map::WrapToMap(tile + offset));
#if !defined(_MSC_VER)
fmt::print(stderr, "{}:{} {}\n", file, line, message);
#else
_assert(message.data(), (char*)file, line);
#endif
}
assert(TileXY(x, y) == Map::WrapToMap(tile + add));
return TileXY(x, y); return TileXY(x, y);
} }

View File

@ -465,9 +465,8 @@ inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
*/ */
# define TILE_ADD(x, y) ((x) + (y)) # define TILE_ADD(x, y) ((x) + (y))
#else #else
extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add, extern TileIndex TileAdd(TileIndex tile, TileIndexDiff offset);
const char *exp, const char *file, int line); # define TILE_ADD(x, y) (TileAdd((x), (y))
# define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__))
#endif #endif
/** /**