libtcod
Loading...
Searching...
No Matches
console_types.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 TCOD_CONSOLE_TYPES_HPP_
36#define TCOD_CONSOLE_TYPES_HPP_
37
38#include <memory>
39#include <stdexcept>
40#include <string>
41#include <utility>
42
43#include "console.h"
44
45namespace tcod {
47 void operator()(TCOD_Console* console) const { TCOD_console_delete(console); }
48};
49
56typedef std::unique_ptr<struct TCOD_Console, ConsoleDeleter> ConsolePtr;
57
80class Console {
81 public:
82 TCODLIB_BEGIN_IGNORE_DEPRECATIONS // Remove this after references to TCOD_Console methods are removed.
83
86 Console() : Console{0, 0} {}
87
93 explicit Console(int width, int height) : console_(TCOD_console_new(width, height)) {
94 if (!console_) throw std::runtime_error(TCOD_get_error());
95 }
96
101 explicit Console(const std::array<int, 2>& size) : Console{size[0], size[1]} {}
102
105 explicit Console(const Console& other) : Console{other.console_->w, other.console_->h} {
106 std::copy(other.console_->begin(), other.console_->end(), console_->begin());
107 }
108
113 explicit Console(ConsolePtr ptr) : console_{std::move(ptr)} {
114 if (!console_) throw std::invalid_argument("Pointer must not be nullptr.");
115 }
116
121 explicit Console(TCOD_Console* ptr) : console_{ptr} {
122 if (!console_) throw std::invalid_argument("TCOD_Console pointer must not be nullptr.");
123 }
124
127 Console& operator=(const Console& rhs) {
128 if (console_->w != rhs.console_->w || console_->h != rhs.console_->h) {
129 *this = Console{{rhs.console_->w, rhs.console_->h}};
130 }
131 std::copy(rhs.console_->begin(), rhs.console_->end(), console_->begin());
132 return *this;
133 }
134
137 Console(Console&&) noexcept = default;
138
141 Console& operator=(Console&& rhs) noexcept {
142 swap(*this, rhs);
143 return *this;
144 }
145
148 ~Console() noexcept = default;
149
152 friend void swap(Console& lhs, Console& rhs) noexcept {
153 using std::swap;
154 swap(lhs.console_, rhs.console_);
155 }
156
159 [[nodiscard]] operator TCOD_Console&() { return *console_; }
160
163 [[nodiscard]] operator const TCOD_Console&() const { return *console_; }
164
167 [[nodiscard]] auto get() noexcept -> TCOD_Console* { return console_.get(); }
168
171 [[nodiscard]] auto get() const noexcept -> const TCOD_Console* { return console_.get(); }
172
177 auto release() noexcept -> TCOD_Console* { return console_.release(); }
178
181 [[nodiscard]] auto begin() noexcept -> TCOD_ConsoleTile* { return console_->tiles; }
182
185 [[nodiscard]] auto begin() const noexcept -> const TCOD_ConsoleTile* { return console_->tiles; }
186
189 [[nodiscard]] auto end() noexcept -> TCOD_ConsoleTile* { return console_->tiles + console_->elements; }
190
193 [[nodiscard]] auto end() const noexcept -> const TCOD_ConsoleTile* { return console_->tiles + console_->elements; }
194
197 [[nodiscard]] auto get_width() const noexcept -> int { return console_->w; }
198
201 [[nodiscard]] auto get_height() const noexcept -> int { return console_->h; }
202
211 [[nodiscard]] auto get_shape() const noexcept -> std::array<int, 2> { return {console_->w, console_->h}; }
212
226 void clear(const TCOD_ConsoleTile& tile = {0x20, {255, 255, 255, 255}, {0, 0, 0, 255}}) noexcept {
227 for (auto& it : *this) it = tile;
228 }
229
232 [[nodiscard]] auto operator[](const std::array<int, 2>& xy) noexcept -> TCOD_ConsoleTile& {
233 return console_->tiles[get_index(xy)];
234 }
235
238 [[nodiscard]] auto operator[](const std::array<int, 2>& xy) const noexcept -> const TCOD_ConsoleTile& {
239 return console_->tiles[get_index(xy)];
240 }
241
246 [[nodiscard]] auto at(const std::array<int, 2>& xy) -> TCOD_ConsoleTile& { return console_->tiles[bounds_check(xy)]; }
247
252 [[nodiscard]] auto at(const std::array<int, 2>& xy) const -> const TCOD_ConsoleTile& {
253 return console_->tiles[bounds_check(xy)];
254 }
255
260 [[nodiscard]] auto at(int x, int y) -> TCOD_ConsoleTile& { return at({x, y}); }
261
266 [[nodiscard]] auto at(int x, int y) const -> const TCOD_ConsoleTile& { return at({x, y}); }
267
270 [[nodiscard]] bool in_bounds(const std::array<int, 2>& xy) const noexcept { return console_->in_bounds(xy); }
271 TCODLIB_END_IGNORE_DEPRECATIONS
272
273 private:
274
279 auto bounds_check(const std::array<int, 2>& xy) const -> int {
280 if (!in_bounds(xy)) {
281 throw std::out_of_range(
282 std::string("Out of bounds lookup {") + std::to_string(xy[0]) + ", " + std::to_string(xy[1]) +
283 "} on console of shape {" + std::to_string(console_->w) + ", " + std::to_string(console_->h) + "}.");
284 }
285 return get_index(xy);
286 }
287
292 [[nodiscard]] auto get_index(const std::array<int, 2>& xy) const noexcept -> int {
293 return console_->w * xy[1] + xy[0];
294 }
295
296 ConsolePtr console_ = nullptr; // The owned TCOD_Console pointer.
297};
298} // namespace tcod
299#endif // TCOD_CONSOLE_TYPES_HPP_
auto get_width() const noexcept -> int
Return the width of this console.
Definition console_types.hpp:197
auto at(int x, int y) const -> const TCOD_ConsoleTile &
Return a constant reference to the tile at x,y.
Definition console_types.hpp:266
auto at(const std::array< int, 2 > &xy) -> TCOD_ConsoleTile &
Return a reference to the tile at xy.
Definition console_types.hpp:246
auto at(int x, int y) -> TCOD_ConsoleTile &
Return a reference to the tile at x,y.
Definition console_types.hpp:260
auto at(const std::array< int, 2 > &xy) const -> const TCOD_ConsoleTile &
Return a constant reference to the tile at xy.
Definition console_types.hpp:252
Console & operator=(const Console &rhs)
Copy the shape and tile data of another console.
Definition console_types.hpp:127
void clear(const TCOD_ConsoleTile &tile={0x20, {255, 255, 255, 255}, {0, 0, 0, 255}}) noexcept
Clear a console by setting all tiles to the provided TCOD_ConsoleTile object.
Definition console_types.hpp:226
auto get_shape() const noexcept -> std::array< int, 2 >
Return the {width, height} shape of this console as a std::array<int, 2>.
Definition console_types.hpp:211
auto operator[](const std::array< int, 2 > &xy) noexcept -> TCOD_ConsoleTile &
Return a reference to the tile at xy.
Definition console_types.hpp:232
Console(const Console &other)
Clone the shape and tile data of a Console.
Definition console_types.hpp:105
auto begin() const noexcept -> const TCOD_ConsoleTile *
Return a const pointer to the beginning of this consoles tile data.
Definition console_types.hpp:185
Console(TCOD_Console *ptr)
Takes ownership of a raw TCOD_Console pointer.
Definition console_types.hpp:121
Console(int width, int height)
Create a new Console with the given size.
Definition console_types.hpp:93
auto get() const noexcept -> const TCOD_Console *
Return a const pointer to the internal TCOD_Console struct.
Definition console_types.hpp:171
auto end() noexcept -> TCOD_ConsoleTile *
Return a pointer to the end of this consoles tile data.
Definition console_types.hpp:189
~Console() noexcept=default
Standard destructor.
auto get() noexcept -> TCOD_Console *
Return a pointer to the internal TCOD_Console struct.
Definition console_types.hpp:167
auto end() const noexcept -> const TCOD_ConsoleTile *
Return a const pointer to the end of this consoles tile data.
Definition console_types.hpp:193
TCODLIB_BEGIN_IGNORE_DEPRECATIONS Console()
Default initializer.
Definition console_types.hpp:86
friend void swap(Console &lhs, Console &rhs) noexcept
Swap two console objects.
Definition console_types.hpp:152
Console(const std::array< int, 2 > &size)
Create a new Console with the given size.
Definition console_types.hpp:101
bool in_bounds(const std::array< int, 2 > &xy) const noexcept
Return true if xy are within the bounds of this console.
Definition console_types.hpp:270
auto get_height() const noexcept -> int
Return the height of this console.
Definition console_types.hpp:201
Console(ConsolePtr ptr)
Pass ownership of a ConsolePtr to a new Console.
Definition console_types.hpp:113
auto operator[](const std::array< int, 2 > &xy) const noexcept -> const TCOD_ConsoleTile &
Return a constant reference to the tile at xy.
Definition console_types.hpp:238
auto begin() noexcept -> TCOD_ConsoleTile *
Return a pointer to the beginning of this consoles tile data.
Definition console_types.hpp:181
Console(Console &&) noexcept=default
Standard move constructor.
auto release() noexcept -> TCOD_Console *
Release ownership of this Console's TCOD_Console* and return the pointer.
Definition console_types.hpp:177
Various console functions.
const char * TCOD_get_error(void)
Return the last error message.
void TCOD_console_delete(TCOD_Console *console)
Delete a console.
TCOD_Console * TCOD_console_new(int w, int h)
Return a new console with a specific number of columns and rows.
The libtcod namespace.
Definition bresenham.hpp:157
std::unique_ptr< struct TCOD_Console, ConsoleDeleter > ConsolePtr
A unique pointer to a TCOD_Console.
Definition console_types.hpp:56
A libtcod console containing a grid of tiles with {ch, fg, bg} information.
Definition console.h:125
The raw data for a single TCOD_Console tile.
Definition console.h:89
Definition console_types.hpp:46