libtcod
Loading...
Searching...
No Matches
tileset.hpp
Go to the documentation of this file.
1/* BSD 3-Clause License
2 *
3 * Copyright © 2008-2025, 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
54static constexpr std::array<int, 256> CHARMAP_CP437 = TCOD_CHARMAP_CP437_;
61static constexpr std::array<int, 256> CHARMAP_TCOD = TCOD_CHARMAP_TCOD_;
62
64 void operator()(TCOD_Tileset* tileset) const { TCOD_tileset_delete(tileset); }
65};
66
73typedef std::unique_ptr<TCOD_Tileset, TilesetDeleter> TilesetPtr;
74
81class Tileset {
82 public:
83
86 Tileset() = default;
87
93 explicit Tileset(int tile_width, int tile_height) : tileset_{TCOD_tileset_new(tile_width, tile_height)} {
94 if (!tileset_) throw std::runtime_error(TCOD_get_error());
95 }
96
101 explicit Tileset(const std::array<int, 2>& tile_shape) : Tileset{tile_shape.at(0), tile_shape.at(1)} {}
102
107 explicit Tileset(TilesetPtr ptr) : tileset_{std::move(ptr)} {
108 if (!tileset_) throw std::invalid_argument("Pointer must not be nullptr.");
109 }
110
115 explicit Tileset(TCOD_Tileset* ptr) : tileset_{ptr} {
116 if (!tileset_) throw std::invalid_argument("Pointer must not be nullptr.");
117 }
118
123 [[nodiscard]] auto get_tile_width() const noexcept -> int { return tileset_->tile_width; }
124
129 [[nodiscard]] auto get_tile_height() const noexcept -> int { return tileset_->tile_height; }
130
135 [[nodiscard]] auto get_tile_shape() const noexcept -> std::array<int, 2> {
136 return {tileset_->tile_width, tileset_->tile_height};
137 }
138
143 [[nodiscard]] auto get() noexcept -> TCOD_Tileset* { return tileset_.get(); }
144
149 [[nodiscard]] auto get() const noexcept -> TCOD_Tileset* { return tileset_.get(); }
150
155 auto release() noexcept -> TCOD_Tileset* { return tileset_.release(); }
156
159 [[nodiscard]] operator TCOD_Tileset&() { return *tileset_; }
160
163 [[nodiscard]] operator const TCOD_Tileset&() const { return *tileset_; }
164
165 private:
166 TilesetPtr tileset_ = nullptr;
167};
168#ifndef TCOD_NO_PNG
189template <typename ArrayType>
190TCOD_NODISCARD inline auto load_tilesheet(
191 const std::filesystem::path& path, const std::array<int, 2>& columns_rows, const ArrayType& charmap) -> Tileset {
192 tcod::check_path(path);
194 path.string().c_str(), columns_rows.at(0), columns_rows.at(1), static_cast<int>(charmap.size()), charmap.data())};
195 if (!tileset) throw std::runtime_error(TCOD_get_error());
196 return Tileset{std::move(tileset)};
197}
198#endif // TCOD_NO_PNG
200} // namespace tcod
201#endif // LIBTCOD_TILESET_HPP_
A C++ Tileset container.
Definition tileset.hpp:81
Tileset(TCOD_Tileset *ptr)
Takes ownership of a raw TCOD_Tileset pointer.
Definition tileset.hpp:115
auto get_tile_shape() const noexcept -> std::array< int, 2 >
Get the {width, height} shape of tiles in this Tileset.
Definition tileset.hpp:135
Tileset(const std::array< int, 2 > &tile_shape)
Construct a new Tileset object with tiles of the given size.
Definition tileset.hpp:101
auto release() noexcept -> TCOD_Tileset *
Release ownership of this Tileset's TCOD_Tileset* and return the pointer.
Definition tileset.hpp:155
auto get() noexcept -> TCOD_Tileset *
Return a non-owning pointer to this objects TCOD_Tileset.
Definition tileset.hpp:143
Tileset(TilesetPtr ptr)
Pass ownership of a TilesetPtr to a new Tileset.
Definition tileset.hpp:107
auto get_tile_width() const noexcept -> int
Get the width of tiles in this Tileset.
Definition tileset.hpp:123
Tileset(int tile_width, int tile_height)
Construct a new Tileset object with tiles of the given size.
Definition tileset.hpp:93
Tileset()=default
Construct a new Tileset object.
auto get() const noexcept -> TCOD_Tileset *
Return a non-owning pointer to this objects TCOD_Tileset.
Definition tileset.hpp:149
auto get_tile_height() const noexcept -> int
Get the height of tiles in this Tileset.
Definition tileset.hpp:129
const char * TCOD_get_error(void)
Return the last error message.
Error handling module.
std::unique_ptr< TCOD_Tileset, TilesetDeleter > TilesetPtr
A unique pointer to a TCOD_Tileset.
Definition tileset.hpp:73
auto load_tilesheet(const std::filesystem::path &path, const std::array< int, 2 > &columns_rows, const ArrayType &charmap) -> Tileset
Load a tilesheet from a PNG file.
Definition tileset.hpp:190
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 bresenham.hpp:157
void check_path(const std::filesystem::path &path)
Throw an exception if the given path does not exist.
Definition error.hpp:78
A container for libtcod tileset graphics.
Definition tileset.h:62
Definition tileset.hpp:63
Tileset module.