libtcod
Loading...
Searching...
No Matches
tileset.hpp
Go to the documentation of this file.
1/* BSD 3-Clause License
2 *
3 * Copyright © 2008-2026, Jice and the libtcod contributors.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
34#pragma once
35#ifndef LIBTCOD_TILESET_HPP_
36#define LIBTCOD_TILESET_HPP_
37#include <array>
38#include <filesystem>
39#include <memory>
40
41#include "error.hpp"
42#include "tileset.h"
43
44namespace tcod {
48
52static constexpr std::array<int, 256> CHARMAP_CP437 = TCOD_CHARMAP_CP437_;
57static constexpr std::array<int, 256> CHARMAP_TCOD = TCOD_CHARMAP_TCOD_;
58
59struct TilesetDeleter {
60 void operator()(TCOD_Tileset* tileset) const { TCOD_tileset_delete(tileset); }
61};
67typedef std::unique_ptr<TCOD_Tileset, TilesetDeleter> TilesetPtr;
68
73class Tileset {
74 public:
75
78 Tileset() = default;
79
85 explicit Tileset(int tile_width, int tile_height) : tileset_{TCOD_tileset_new(tile_width, tile_height)} {
86 if (!tileset_) throw std::runtime_error(TCOD_get_error());
87 }
88
93 explicit Tileset(const std::array<int, 2>& tile_shape) : Tileset{tile_shape.at(0), tile_shape.at(1)} {}
94
99 explicit Tileset(TilesetPtr ptr) : tileset_{std::move(ptr)} {
100 if (!tileset_) throw std::invalid_argument("Pointer must not be nullptr.");
101 }
102
107 explicit Tileset(TCOD_Tileset* ptr) : tileset_{ptr} {
108 if (!tileset_) throw std::invalid_argument("Pointer must not be nullptr.");
109 }
110
115 [[nodiscard]] auto get_tile_width() const noexcept -> int { return tileset_->tile_width; }
116
121 [[nodiscard]] auto get_tile_height() const noexcept -> int { return tileset_->tile_height; }
122
127 [[nodiscard]] auto get_tile_shape() const noexcept -> std::array<int, 2> {
128 return {tileset_->tile_width, tileset_->tile_height};
129 }
130
135 [[nodiscard]] auto get() noexcept -> TCOD_Tileset* { return tileset_.get(); }
136
141 [[nodiscard]] auto get() const noexcept -> TCOD_Tileset* { return tileset_.get(); }
142
147 auto release() noexcept -> TCOD_Tileset* { return tileset_.release(); }
148
151 [[nodiscard]] operator TCOD_Tileset&() { return *tileset_; }
152
155 [[nodiscard]] operator const TCOD_Tileset&() const { return *tileset_; }
156
157 private:
158 TilesetPtr tileset_ = nullptr;
159};
160#ifndef TCOD_NO_PNG
179template <typename ArrayType>
180TCOD_NODISCARD inline auto load_tilesheet(
181 const std::filesystem::path& path, const std::array<int, 2>& columns_rows, const ArrayType& charmap) -> Tileset {
182 tcod::check_path(path);
183 TilesetPtr tileset{TCOD_tileset_load(
184 path.string().c_str(), columns_rows.at(0), columns_rows.at(1), static_cast<int>(charmap.size()), charmap.data())};
185 if (!tileset) throw std::runtime_error(TCOD_get_error());
186 return Tileset{std::move(tileset)};
187}
188#endif // TCOD_NO_PNG
190} // namespace tcod
191#endif // LIBTCOD_TILESET_HPP_
const char * TCOD_get_error(void)
Return the last error message.
Error handling module.
void TCOD_tileset_delete(TCOD_Tileset *tileset)
Delete a tile-set.
TCOD_Tileset * TCOD_tileset_new(int tile_width, int tile_height)
Create a new tile-set with the given tile size.
TCOD_Tileset * TCOD_tileset_load(const char *filename, int columns, int rows, int n, const int *charmap)
Load a PNG font as a tilesheet and return a TCOD_Tileset.
The libtcod namespace.
Definition color.hpp:45
void check_path(const std::filesystem::path &path)
Throw an exception if the given path does not exist.
Definition error.hpp:78
Tileset module.