libtcod
Loading...
Searching...
No Matches
console.h
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_H_
36#define TCOD_CONSOLE_H_
37#include <stdbool.h>
38#ifdef __cplusplus
39#include <algorithm>
40#include <array>
41#include <memory>
42#include <optional>
43#include <stdexcept>
44#include <utility>
45
46#include "error.hpp"
47#endif // __cplusplus
48
49#include "color.h"
50#include "config.h"
51#include "error.h"
52#include "tileset.h"
55
60typedef enum TCOD_bkgnd_flag_t {
61 TCOD_BKGND_NONE,
62 TCOD_BKGND_SET,
63 TCOD_BKGND_MULTIPLY,
64 TCOD_BKGND_LIGHTEN,
65 TCOD_BKGND_DARKEN,
66 TCOD_BKGND_SCREEN,
67 TCOD_BKGND_COLOR_DODGE,
68 TCOD_BKGND_COLOR_BURN,
69 TCOD_BKGND_ADD,
70 TCOD_BKGND_ADDA,
71 TCOD_BKGND_BURN,
72 TCOD_BKGND_OVERLAY,
73 TCOD_BKGND_ALPH,
74 TCOD_BKGND_DEFAULT
76
81typedef enum TCOD_alignment_t { TCOD_LEFT, TCOD_RIGHT, TCOD_CENTER } TCOD_alignment_t;
82
89typedef struct TCOD_ConsoleTile {
90#ifdef __cplusplus
92 TCODLIB_BEGIN_IGNORE_DEPRECATIONS
93 [[deprecated]] bool operator==(const TCOD_ConsoleTile& rhs) const noexcept {
94 return ch == rhs.ch && fg == rhs.fg && bg == rhs.bg;
95 }
96 [[deprecated]] bool operator!=(const TCOD_ConsoleTile& rhs) const noexcept { return !(*this == rhs); }
97 TCODLIB_END_IGNORE_DEPRECATIONS
99#endif // __cplusplus
100
103 int ch;
104
108
113
126#ifdef __cplusplus
128 TCODLIB_BEGIN_IGNORE_DEPRECATIONS
129
132 [[nodiscard]] [[deprecated]] auto begin() noexcept -> TCOD_ConsoleTile* { return tiles; }
133
136 [[nodiscard]] [[deprecated]] auto begin() const noexcept -> const TCOD_ConsoleTile* { return tiles; }
137
140 [[nodiscard]] [[deprecated]] auto end() noexcept -> TCOD_ConsoleTile* { return tiles + elements; }
141
144 [[nodiscard]] [[deprecated]] auto end() const noexcept -> const TCOD_ConsoleTile* { return tiles + elements; }
145
150 [[deprecated]] void clear(const TCOD_ConsoleTile& tile = {0x20, {255, 255, 255, 255}, {0, 0, 0, 255}}) noexcept {
151 for (auto& it : *this) it = tile;
152 }
153
156 [[nodiscard]] [[deprecated]] auto operator[](const std::array<int, 2>& xy) noexcept -> TCOD_ConsoleTile& {
157 return tiles[get_index(xy)];
158 }
159
162 [[nodiscard]] [[deprecated]] auto operator[](const std::array<int, 2>& xy) const noexcept -> const TCOD_ConsoleTile& {
163 return tiles[get_index(xy)];
164 }
165
170 [[nodiscard]] [[deprecated]] auto at(const std::array<int, 2>& xy) -> TCOD_ConsoleTile& {
171 return tiles[bounds_check(xy)];
172 }
173
178 [[nodiscard]] [[deprecated]] auto at(const std::array<int, 2>& xy) const -> const TCOD_ConsoleTile& {
179 return tiles[bounds_check(xy)];
180 }
181
186 [[nodiscard]] [[deprecated]] auto at(int x, int y) -> TCOD_ConsoleTile& { return at({x, y}); }
187
192 [[nodiscard]] [[deprecated]] auto at(int x, int y) const -> const TCOD_ConsoleTile& { return at({x, y}); }
193
198 [[nodiscard]] [[deprecated]] int get_index(const std::array<int, 2>& xy) const noexcept { return w * xy[1] + xy[0]; }
199
202 [[nodiscard]] [[deprecated]] bool in_bounds(const std::array<int, 2>& xy) const noexcept {
203 return 0 <= xy[0] && xy[0] < w && 0 <= xy[1] && xy[1] < h;
204 }
206
207 private:
208
213 int bounds_check(const std::array<int, 2>& xy) const {
214 if (!in_bounds(xy)) {
215 throw std::out_of_range(
216 std::string("Out of bounds lookup {") + std::to_string(xy[0]) + ", " + std::to_string(xy[1]) +
217 "} on console of shape {" + std::to_string(w) + ", " + std::to_string(h) + "}.");
218 }
219 return get_index(xy);
220 }
221 TCODLIB_END_IGNORE_DEPRECATIONS
222
223 public:
224#endif // __cplusplus
226 int w, h;
234 TCOD_color_t fore, back;
238 TCOD_color_t key_color;
254 void* userdata;
256 void (*on_delete)(struct TCOD_Console* self);
257};
258typedef struct TCOD_Console TCOD_Console;
259typedef struct TCOD_Console* TCOD_console_t;
260#ifdef __cplusplus
261extern "C" {
262#endif // __cplusplus
270TCOD_PUBLIC TCOD_NODISCARD TCOD_Console* TCOD_console_new(int w, int h);
274TCOD_PUBLIC TCOD_NODISCARD int TCOD_console_get_width(const TCOD_Console* con);
278TCOD_PUBLIC TCOD_NODISCARD int TCOD_console_get_height(const TCOD_Console* con);
279TCOD_PUBLIC void TCOD_console_set_key_color(TCOD_Console* con, TCOD_color_t col);
302TCOD_PUBLIC void TCOD_console_blit(
303 const TCOD_Console* __restrict src,
304 int xSrc,
305 int ySrc,
306 int wSrc,
307 int hSrc,
308 TCOD_Console* __restrict dst,
309 int xDst,
310 int yDst,
311 float foreground_alpha,
312 float background_alpha);
313TCOD_PUBLIC void TCOD_console_blit_key_color(
314 const TCOD_Console* __restrict src,
315 int xSrc,
316 int ySrc,
317 int wSrc,
318 int hSrc,
319 TCOD_Console* __restrict dst,
320 int xDst,
321 int yDst,
322 float foreground_alpha,
323 float background_alpha,
324 const TCOD_color_t* key_color);
333TCOD_PUBLIC void TCOD_console_delete(TCOD_Console* console);
334
335TCOD_DEPRECATED("Console defaults have been deprecated.")
336TCOD_PUBLIC void TCOD_console_set_default_background(TCOD_Console* con, TCOD_color_t col);
337TCOD_DEPRECATED("Console defaults have been deprecated.")
338TCOD_PUBLIC void TCOD_console_set_default_foreground(TCOD_Console* con, TCOD_color_t col);
342TCOD_PUBLIC void TCOD_console_clear(TCOD_Console* con);
352TCOD_DEPRECATED("Replace with TCOD_console_put_rgb.")
354 TCOD_Console* con, int x, int y, TCOD_color_t col, TCOD_bkgnd_flag_t flag);
363TCOD_DEPRECATED("Replace with TCOD_console_put_rgb.")
364TCOD_PUBLIC void TCOD_console_set_char_foreground(TCOD_Console* con, int x, int y, TCOD_color_t col);
373TCOD_DEPRECATED("Replace with TCOD_console_put_rgb.")
374TCOD_PUBLIC void TCOD_console_set_char(TCOD_Console* con, int x, int y, int c);
384TCOD_DEPRECATED("Replace with TCOD_console_put_rgb.")
385TCOD_PUBLIC void TCOD_console_put_char(TCOD_Console* con, int x, int y, int c, TCOD_bkgnd_flag_t flag);
396TCOD_DEPRECATED("Replace with TCOD_console_put_rgb.")
397TCOD_PUBLIC void TCOD_console_put_char_ex(TCOD_Console* con, int x, int y, int c, TCOD_color_t fore, TCOD_color_t back);
404TCOD_DEPRECATED("Console defaults have been deprecated.")
409TCOD_DEPRECATED("Console defaults have been deprecated.")
417TCOD_DEPRECATED("Console defaults have been deprecated.")
422TCOD_DEPRECATED("Console defaults have been deprecated.")
424TCOD_DEPRECATED("Console defaults have been deprecated.")
425TCOD_PUBLIC TCOD_NODISCARD TCOD_color_t TCOD_console_get_default_background(TCOD_Console* con);
426TCOD_DEPRECATED("Console defaults have been deprecated.")
427TCOD_PUBLIC TCOD_NODISCARD TCOD_color_t TCOD_console_get_default_foreground(TCOD_Console* con);
436TCOD_PUBLIC TCOD_NODISCARD TCOD_color_t TCOD_console_get_char_background(const TCOD_Console* con, int x, int y);
445TCOD_PUBLIC TCOD_NODISCARD TCOD_color_t TCOD_console_get_char_foreground(const TCOD_Console* con, int x, int y);
454TCOD_PUBLIC TCOD_NODISCARD int TCOD_console_get_char(const TCOD_Console* con, int x, int y);
455void TCOD_console_resize_(TCOD_Console* console, int width, int height);
457#ifdef __cplusplus
458} // extern "C"
459#endif // __cplusplus
460#endif // TCOD_CONSOLE_H_
Color handling module.
Libtcod config header.
Error handling module.
Error handling module.
int TCOD_console_get_width(const TCOD_Console *con)
Return the width of a console.
TCOD_color_t TCOD_console_get_char_background(const TCOD_Console *con, int x, int y)
Return the background color of a console at x,y.
TCOD_color_t TCOD_console_get_char_foreground(const TCOD_Console *con, int x, int y)
Return the foreground color of a console at x,y.
void TCOD_console_put_char_ex(TCOD_Console *con, int x, int y, int c, TCOD_color_t fore, TCOD_color_t back)
Draw a character on the console with the given colors.
void TCOD_console_set_background_flag(TCOD_Console *con, TCOD_bkgnd_flag_t flag)
Set a consoles default background flag.
void TCOD_console_blit(const TCOD_Console *src, int xSrc, int ySrc, int wSrc, int hSrc, TCOD_Console *dst, int xDst, int yDst, float foreground_alpha, float background_alpha)
Blit from one console to another.
TCOD_alignment_t
Print justification options.
Definition console.h:81
int TCOD_console_get_char(const TCOD_Console *con, int x, int y)
Return a character code of a console at x,y.
TCOD_bkgnd_flag_t
Background color blend modes.
Definition console.h:60
void TCOD_console_delete(TCOD_Console *console)
Delete a console.
void TCOD_console_set_char_background(TCOD_Console *con, int x, int y, TCOD_color_t col, TCOD_bkgnd_flag_t flag)
Blend a background color onto a console tile.
void TCOD_console_put_char(TCOD_Console *con, int x, int y, int c, TCOD_bkgnd_flag_t flag)
Draw a character on a console using the default colors.
int TCOD_console_get_height(const TCOD_Console *con)
Return the height of a console.
void TCOD_console_set_alignment(TCOD_Console *con, TCOD_alignment_t alignment)
Set a consoles default alignment.
TCOD_alignment_t TCOD_console_get_alignment(TCOD_Console *con)
Return a consoles default alignment.
TCOD_Console * TCOD_console_new(int w, int h)
Return a new console with a specific number of columns and rows.
void TCOD_console_set_char(TCOD_Console *con, int x, int y, int c)
Change a character on a console tile, without changing its colors.
TCOD_bkgnd_flag_t TCOD_console_get_background_flag(TCOD_Console *con)
Return a consoles default background flag.
void TCOD_console_clear(TCOD_Console *con)
Clear a console to its default colors and the space character code.
void TCOD_console_set_char_foreground(TCOD_Console *con, int x, int y, TCOD_color_t col)
Change the foreground color of a console tile.
A 4-channel RGBA color struct.
Definition color.h:92
A libtcod console containing a grid of tiles with {ch, fg, bg} information.
Definition console.h:125
void(* on_delete)(struct TCOD_Console *self)
Internal use.
Definition console.h:256
TCOD_bkgnd_flag_t bkgnd_flag
Default background operator for print & print_rect functions.
Definition console.h:230
TCOD_color_t key_color
The current key color for this console.
Definition console.h:238
TCOD_color_t fore
Foreground (text) and background colors.
Definition console.h:234
int elements
The total length of the tiles array.
Definition console.h:246
int w
Console width and height in tiles.
Definition console.h:226
bool has_key_color
True if a key color is being used.
Definition console.h:236
TCOD_ConsoleTile * tiles
A contiguous array of console tiles.
Definition console.h:228
TCOD_alignment_t alignment
Default alignment for print & print_rect functions.
Definition console.h:232
void * userdata
A userdata attribute which can be repurposed.
Definition console.h:254
The raw data for a single TCOD_Console tile.
Definition console.h:89
TCOD_ColorRGBA fg
The tile glyph color, rendered on top of the background.
Definition console.h:107
int ch
The Unicode codepoint for this tile.
Definition console.h:103
TCOD_ColorRGBA bg
The tile background color, rendered behind the glyph.
Definition console.h:111
Tileset module.