12 #ifndef TILEMATRIX_TYPE_HPP 13 #define TILEMATRIX_TYPE_HPP 28 template <
typename T, u
int N>
38 uint old_w = this->
area.
w / N;
39 uint old_h = this->
area.
h / N;
43 uint grid_x = (
TileX(tile) / N) * N;
44 uint grid_y = (
TileY(tile) / N) * N;
49 T *new_data = CallocT<T>(this->
area.
w / N * this->
area.
h / N);
56 for (uint row = 0; row < old_h; row++) {
57 MemCpyT(&new_data[(row + offs_y) * this->
area.
w / N + offs_x], &this->data[row * old_w], old_w);
62 this->
data = new_data;
66 static const uint GRID = N;
96 uint tile_x = (
TileX(tile) / N) * N;
97 uint tile_y = (
TileY(tile) / N) * N;
100 w +=
min(extend * N, tile_x);
101 h +=
min(extend * N, tile_y);
103 tile_x -=
min(extend * N, tile_x);
104 tile_y -=
min(extend * N, tile_y);
132 tile -= this->area.
tile;
133 uint x =
TileX(tile) / N;
134 uint y =
TileY(tile) / N;
136 return &this->data[y * this->area.
w / N + x];
142 return *this->
Get(tile);
static uint MapSizeX()
Get the size of the map along the X.
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
static uint MapSizeY()
Get the size of the map along the Y.
static TileArea GetAreaForTile(TileIndex tile, uint extend=0)
Get the area of the matrix square that contains a specific tile.
void Add(TileIndex tile)
Extend the coverage area to include a tile.
static uint TileX(TileIndex tile)
Get the X component of a tile.
uint16 w
The width of the area.
T * data
Pointer to data array.
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
void AllocateStorage(TileIndex tile)
Allocates space for a new tile in the matrix.
void Add(TileIndex to_add)
Add a single tile to a tile area; enlarge if needed.
Functions related to the allocation of memory.
Represents the covered area of e.g.
const TileArea & GetArea() const
Get the total covered area.
static T min(const T a, const T b)
Returns the minimum of two values.
TileArea area
Area covered by the matrix.
static void MemCpyT(T *destination, const T *source, size_t num=1)
Type-safe version of memcpy().
TileIndex tile
The base tile of the area.
A simple matrix that stores one value per N*N square of the map.
T & operator[](TileIndex tile)
Array access operator, see Get.
uint32 TileIndex
The index/ID of a Tile.
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Type for storing the 'area' of something uses on the map.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
uint16 h
The height of the area.
T * Get(TileIndex tile)
Get the value associated to a tile index.
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.