libtcod
Loading...
Searching...
No Matches
console_printing.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_PRINTING_HPP_
36#define TCOD_CONSOLE_PRINTING_HPP_
37
38#include <stdio.h>
39
40#include <array>
41#include <cstdarg>
42#include <optional>
43#include <stdexcept>
44#include <string>
45#include <string_view>
46
47#include "console_printing.h"
48
49namespace tcod {
50#ifndef TCOD_NO_UNICODE
53
75inline void print(
76 TCOD_Console& console,
77 const std::array<int, 2>& xy,
78 std::string_view str,
79 std::optional<TCOD_ColorRGB> fg,
80 std::optional<TCOD_ColorRGB> bg,
81 TCOD_alignment_t alignment = TCOD_LEFT,
82 TCOD_bkgnd_flag_t flag = TCOD_BKGND_SET) {
83 const TCOD_ColorRGB* fg_ptr = fg ? &fg.value() : nullptr;
84 const TCOD_ColorRGB* bg_ptr = bg ? &bg.value() : nullptr;
86 TCOD_console_printn(&console, xy.at(0), xy.at(1), str.size(), str.data(), fg_ptr, bg_ptr, flag, alignment));
87}
88
114inline int print_rect(
115 TCOD_Console& console,
116 const std::array<int, 4>& rect,
117 std::string_view str,
118 std::optional<TCOD_ColorRGB> fg,
119 std::optional<TCOD_ColorRGB> bg,
120 TCOD_alignment_t alignment = TCOD_LEFT,
121 TCOD_bkgnd_flag_t flag = TCOD_BKGND_SET) {
123 &console,
124 rect.at(0),
125 rect.at(1),
126 rect.at(2),
127 rect.at(3),
128 str.size(),
129 str.data(),
130 fg ? &fg.value() : nullptr,
131 bg ? &bg.value() : nullptr,
132 flag,
133 alignment));
134}
135
154inline int get_height_rect(int width, std::string_view str) {
155 return check_throw_error(TCOD_console_get_height_rect_wn(width, str.size(), str.data()));
156}
157TCODLIB_BEGIN_IGNORE_DEPRECATIONS
158[[deprecated("It is recommended that you print your own banners for frames.")]] inline void print_frame(
159 struct TCOD_Console& console,
160 const std::array<int, 4>& rect,
161 std::string_view title,
162 const TCOD_ColorRGB* fg,
163 const TCOD_ColorRGB* bg,
164 TCOD_bkgnd_flag_t flag = TCOD_BKGND_SET,
165 bool clear = true) {
166 check_throw_error(TCOD_console_printn_frame(
167 &console, rect.at(0), rect.at(1), rect.at(2), rect.at(3), title.size(), title.data(), fg, bg, flag, clear));
168}
169TCODLIB_END_IGNORE_DEPRECATIONS
171#endif // TCOD_NO_UNICODE
172
197template <typename... T>
198inline std::string stringf(const char* format, T... args) {
199 const int str_length = snprintf(nullptr, 0, format, args...);
200 if (str_length < 0) throw std::runtime_error("Failed to format string.");
201 std::string out(str_length, '\0');
202 snprintf(&out[0], str_length + 1, format, args...);
203 return out;
204}
205} // namespace tcod
206#endif // TCOD_CONSOLE_PRINTING_HPP_
Console string printing module.
TCOD_alignment_t
Print justification options.
Definition console.h:81
TCOD_bkgnd_flag_t
Background color blend modes.
Definition console.h:60
TCOD_Error TCOD_console_printn(TCOD_Console *console, int x, int y, size_t n, const char *str, const TCOD_ColorRGB *fg, const TCOD_ColorRGB *bg, TCOD_bkgnd_flag_t flag, TCOD_alignment_t alignment)
Print a string of a specified length to a console.
int print_rect(TCOD_Console &console, const std::array< int, 4 > &rect, std::string_view str, std::optional< TCOD_ColorRGB > fg, std::optional< TCOD_ColorRGB > bg, TCOD_alignment_t alignment=TCOD_LEFT, TCOD_bkgnd_flag_t flag=TCOD_BKGND_SET)
Print a string to a console constrained to a bounding box.
Definition console_printing.hpp:114
void print(TCOD_Console &console, const std::array< int, 2 > &xy, std::string_view str, std::optional< TCOD_ColorRGB > fg, std::optional< TCOD_ColorRGB > bg, TCOD_alignment_t alignment=TCOD_LEFT, TCOD_bkgnd_flag_t flag=TCOD_BKGND_SET)
Print a string to a console.
Definition console_printing.hpp:75
int TCOD_console_printn_rect(TCOD_Console *console, int x, int y, int width, int height, size_t n, const char *str, const TCOD_ColorRGB *fg, const TCOD_ColorRGB *bg, TCOD_bkgnd_flag_t flag, TCOD_alignment_t alignment)
Print a string of a specified length in a bounding box to a console.
int get_height_rect(int width, std::string_view str)
Return the height of the word-wrapped text with the given width.
Definition console_printing.hpp:154
int TCOD_console_get_height_rect_wn(int width, size_t n, const char *str)
Return the height of the word-wrapped text with the given width.
The libtcod namespace.
Definition bresenham.hpp:157
TCODLIB_END_IGNORE_DEPRECATIONS std::string stringf(const char *format, T... args)
Return a formatted string as a std::string object.
Definition console_printing.hpp:198
int check_throw_error(int error)
Check and throw error messages.
Definition error.hpp:57
A 3-channel RGB color struct.
Definition color.h:50
A libtcod console containing a grid of tiles with {ch, fg, bg} information.
Definition console.h:125