OpenTTD Source: src/tilematrix_type.hpp Source File

OpenTTD Source 13.2.1

tilematrix_type.hpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef TILEMATRIX_TYPE_HPP
11 #define TILEMATRIX_TYPE_HPP
12
13 #include "core/alloc_func.hpp"
14 #include "tilearea_type.h"
15
26 template <typename T, uint N>
27 class TileMatrix {
28
32 void AllocateStorage(TileIndex tile)
33 {
34 uint old_left = TileX(this->area.tile) / N;
35 uint old_top = TileY(this->area.tile) / N;
36 uint old_w = this->area.w / N;
37 uint old_h = this->area.h / N;
38
39 /* Add the square the tile is in to the tile area. We do this
40 * by adding top-left and bottom-right of the square. */
41 uint grid_x = (TileX(tile) / N) * N;
42 uint grid_y = (TileY(tile) / N) * N;
43 this->area.Add(TileXY(grid_x, grid_y));
44 this->area.Add(TileXY(grid_x + N - 1, grid_y + N - 1));
45
46 /* Allocate new storage. */
47 T *new_data = CallocT<T>(this->area.w / N * this->area.h / N);
48
49 if (old_w > 0) {
50 /* Copy old data if present. */
51 uint offs_x = old_left - TileX(this->area.tile) / N;
52 uint offs_y = old_top - TileY(this->area.tile) / N;
53
54 for (uint row = 0; row < old_h; row++) {
55 MemCpyT(&new_data[(row + offs_y) * this->area.w / N + offs_x], &this->data[row * old_w], old_w);
56 }
57 }
58
59 free(this->data);
60 this->data = new_data;
61 }
62
63 public:
64 static const uint GRID = N;
65
66 TileArea area;
67
68 T *data;
69
70 TileMatrix() : area(INVALID_TILE, 0, 0), data(nullptr) {}
71
72 ~TileMatrix()
73 {
74 free(this->data);
75 }
76
81 const TileArea& GetArea() const
82 {
83 return this->area;
84 }
85
92 static TileArea GetAreaForTile(TileIndex tile, uint extend = 0)
93 {
94 uint tile_x = (TileX(tile) / N) * N;
95 uint tile_y = (TileY(tile) / N) * N;
96 uint w = N, h = N;
97
98 w += std::min(extend * N, tile_x);
99 h += std::min(extend * N, tile_y);
100
101 tile_x -= std::min(extend * N, tile_x);
102 tile_y -= std::min(extend * N, tile_y);
103
104 w += std::min(extend * N, MapSizeX() - tile_x - w);
105 h += std::min(extend * N, MapSizeY() - tile_y - h);
106
107 return TileArea(TileXY(tile_x, tile_y), w, h);
108 }
109
114 void Add(TileIndex tile)
115 {
116 if (!this->area.Contains(tile)) {
117 this->AllocateStorage(tile);
118 }
119 }
120
126 T *Get(TileIndex tile)
127 {
128 this->Add(tile);
129
130 tile -= this->area.tile;
131 uint x = TileX(tile) / N;
132 uint y = TileY(tile) / N;
133
134 return &this->data[y * this->area.w / N + x];
135 }
136
138 inline T &operator[](TileIndex tile)
139 {
140 return *this->Get(tile);
141 }
142 };
143
144 #endif /* TILEMATRIX_TYPE_HPP */
TileMatrix::operator[]
T & operator[](TileIndex tile)
Array access operator, see Get.
Definition: tilematrix_type.hpp:138
TileMatrix
A simple matrix that stores one value per N*N square of the map.
Definition: tilematrix_type.hpp:27
INVALID_TILE
static constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:108
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
TileMatrix::GetArea
const TileArea & GetArea() const
Get the total covered area.
Definition: tilematrix_type.hpp:81
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
OrthogonalTileArea::Add
void Add(TileIndex to_add)
Add a single tile to a tile area; enlarge if needed.
Definition: tilearea.cpp:43
MemCpyT
static void MemCpyT(T *destination, const T *source, size_t num=1)
Type-safe version of memcpy().
Definition: mem_func.hpp:23
TileMatrix::area
TileArea area
Area covered by the matrix.
Definition: tilematrix_type.hpp:66
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
MapSizeX
static uint MapSizeX()
Get the size of the map along the X.
Definition: map_func.h:72
TileMatrix::Get
T * Get(TileIndex tile)
Get the value associated to a tile index.
Definition: tilematrix_type.hpp:126
OrthogonalTileArea::w
uint16 w
The width of the area.
Definition: tilearea_type.h:20
OrthogonalTileArea
Represents the covered area of e.g.
Definition: tilearea_type.h:18
MapSizeY
static uint MapSizeY()
Get the size of the map along the Y.
Definition: map_func.h:82
OrthogonalTileArea::h
uint16 h
The height of the area.
Definition: tilearea_type.h:21
TileMatrix::GetAreaForTile
static TileArea GetAreaForTile(TileIndex tile, uint extend=0)
Get the area of the matrix square that contains a specific tile.
Definition: tilematrix_type.hpp:92
tilearea_type.h
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:163
alloc_func.hpp
TileMatrix::Add
void Add(TileIndex tile)
Extend the coverage area to include a tile.
Definition: tilematrix_type.hpp:114
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:19
TileArea
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition: tilearea_type.h:102
TileMatrix::data
T * data
Pointer to data array.
Definition: tilematrix_type.hpp:68
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:470
TileMatrix::AllocateStorage
void AllocateStorage(TileIndex tile)
Allocates space for a new tile in the matrix.
Definition: tilematrix_type.hpp:32
OrthogonalTileArea::Contains
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition: tilearea.cpp:104