libtcod
Loading...
Searching...
No Matches
console_types.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 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
54typedef std::unique_ptr<struct TCOD_Console, ConsoleDeleter> ConsolePtr;
55
76class Console {
77 public:
78 TCODLIB_BEGIN_IGNORE_DEPRECATIONS // Remove this after references to TCOD_Console methods are removed.
79
82 Console() : Console{0, 0} {}
83
89 explicit Console(int width, int height) : console_(TCOD_console_new(width, height)) {
90 if (!console_) throw std::runtime_error(TCOD_get_error());
91 }
92
97 explicit Console(const std::array<int, 2>& size) : Console{size[0], size[1]} {}
98
101 explicit Console(const Console& other) : Console{other.console_->w, other.console_->h} {
102 std::copy(other.console_->begin(), other.console_->end(), console_->begin());
103 }
104
109 explicit Console(ConsolePtr ptr) : console_{std::move(ptr)} {
110 if (!console_) throw std::invalid_argument("Pointer must not be nullptr.");
111 }
112
117 explicit Console(TCOD_Console* ptr) : console_{ptr} {
118 if (!console_) throw std::invalid_argument("TCOD_Console pointer must not be nullptr.");
119 }
120
123 Console& operator=(const Console& rhs) {
124 if (console_->w != rhs.console_->w || console_->h != rhs.console_->h) {
125 *this = Console{{rhs.console_->w, rhs.console_->h}};
126 }
127 std::copy(rhs.console_->begin(), rhs.console_->end(), console_->begin());
128 return *this;
129 }
130
133 Console(Console&&) noexcept = default;
134
137 Console& operator=(Console&& rhs) noexcept {
138 swap(*this, rhs);
139 return *this;
140 }
141
144 ~Console() noexcept = default;
145
148 friend void swap(Console& lhs, Console& rhs) noexcept {
149 using std::swap;
150 swap(lhs.console_, rhs.console_);
151 }
152
155 [[nodiscard]] operator TCOD_Console&() { return *console_; }
156
159 [[nodiscard]] operator const TCOD_Console&() const { return *console_; }
160
163 [[nodiscard]] auto get() noexcept -> TCOD_Console* { return console_.get(); }
164
167 [[nodiscard]] auto get() const noexcept -> const TCOD_Console* { return console_.get(); }
168
173 auto release() noexcept -> TCOD_Console* { return console_.release(); }
174
177 [[nodiscard]] auto begin() noexcept -> TCOD_ConsoleTile* { return console_->tiles; }
178
181 [[nodiscard]] auto begin() const noexcept -> const TCOD_ConsoleTile* { return console_->tiles; }
182
185 [[nodiscard]] auto end() noexcept -> TCOD_ConsoleTile* { return console_->tiles + console_->elements; }
186
189 [[nodiscard]] auto end() const noexcept -> const TCOD_ConsoleTile* { return console_->tiles + console_->elements; }
190
193 [[nodiscard]] auto get_width() const noexcept -> int { return console_->w; }
194
197 [[nodiscard]] auto get_height() const noexcept -> int { return console_->h; }
198
207 [[nodiscard]] auto get_shape() const noexcept -> std::array<int, 2> { return {console_->w, console_->h}; }
208
222 void clear(const TCOD_ConsoleTile& tile = {0x20, {255, 255, 255, 255}, {0, 0, 0, 255}}) noexcept {
223 for (auto& it : *this) it = tile;
224 }
225
228 [[nodiscard]] auto operator[](const std::array<int, 2>& xy) noexcept -> TCOD_ConsoleTile& {
229 return console_->tiles[get_index(xy)];
230 }
231
234 [[nodiscard]] auto operator[](const std::array<int, 2>& xy) const noexcept -> const TCOD_ConsoleTile& {
235 return console_->tiles[get_index(xy)];
236 }
237
242 [[nodiscard]] auto at(const std::array<int, 2>& xy) -> TCOD_ConsoleTile& { return console_->tiles[bounds_check(xy)]; }
243
248 [[nodiscard]] auto at(const std::array<int, 2>& xy) const -> const TCOD_ConsoleTile& {
249 return console_->tiles[bounds_check(xy)];
250 }
251
256 [[nodiscard]] auto at(int x, int y) -> TCOD_ConsoleTile& { return at({x, y}); }
257
262 [[nodiscard]] auto at(int x, int y) const -> const TCOD_ConsoleTile& { return at({x, y}); }
263
266 [[nodiscard]] bool in_bounds(const std::array<int, 2>& xy) const noexcept { return console_->in_bounds(xy); }
267 TCODLIB_END_IGNORE_DEPRECATIONS
268
269 private:
270
275 auto bounds_check(const std::array<int, 2>& xy) const -> int {
276 if (!in_bounds(xy)) {
277 throw std::out_of_range(
278 std::string("Out of bounds lookup {") + std::to_string(xy[0]) + ", " + std::to_string(xy[1]) +
279 "} on console of shape {" + std::to_string(console_->w) + ", " + std::to_string(console_->h) + "}.");
280 }
281 return get_index(xy);
282 }
283
288 [[nodiscard]] auto get_index(const std::array<int, 2>& xy) const noexcept -> int {
289 return console_->w * xy[1] + xy[0];
290 }
291
292 ConsolePtr console_ = nullptr; // The owned TCOD_Console pointer.
293};
294} // namespace tcod
295#endif // TCOD_CONSOLE_TYPES_HPP_
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 color.hpp:45
std::unique_ptr< struct TCOD_Console, ConsoleDeleter > ConsolePtr
A unique pointer to a TCOD_Console.
Definition console_types.hpp:54
A libtcod console containing a grid of tiles with {ch, fg, bg} information.
Definition console.h:121
The raw data for a single TCOD_Console tile.
Definition console.h:87
Definition console_types.hpp:46