Class Console

Class Documentation

class Console

A managed libtcod console containing a grid of tiles with {ch, fg, bg} information.

Note that all tile references are to TCOD_ConsoleTile structs and will include an alpha channel.

auto console = tcod::Console{80, 50};
console.at({1, 1}).ch = '@';  // Bounds-checked references to a tile.
console[{1, 1}].bg = {0, 0, 255, 255};  // Access a tile without bounds checking, colors are RGBA.
if (console.in_bounds({100, 100})) {}  // Test if an index is in bounds.
for (auto& tile : console) tile.fg = {255, 255, 0, 255};  // Iterate over all tiles on a console.
for (auto& tile : console) tile = {0x20, {255, 255, 255, 255}, {0, 0, 0, 255}};  // Same as clearing all tiles.
for (int y = 0; y < console.get_height(); ++y) {
  for (int x = 0; x < console.get_width(); ++x) {
    auto& tile = console.at({x, y});  // Iterate over the coordinates of a console.
  }
}

New in version 1.19.

Public Functions

inline Console()

Default initializer.

inline explicit Console(int width, int height)

Create a new Console with the given size.

Parameters
  • width – The number of columns in the new console.

  • height – The number of rows in the new console.

inline explicit Console(const std::array<int, 2> &size)

Create a new Console with the given size.

Parameters

size – The new console size of {width, height}.

inline explicit Console(const Console &other)

Clone the shape and tile data of a Console.

inline explicit Console(ConsolePtr ptr)

Pass ownership of a ConsolePtr to a new Console.

Parameters

ptr – A tcod::ConsolePtr, must not be nullptr.

inline explicit Console(TCOD_Console *ptr)

Takes ownership of a raw TCOD_Console pointer.

Parameters

ptr – A pointer which will now be managed by this Console object. Must not be nullptr.

inline Console &operator=(const Console &rhs)

Copy the shape and tile data of another console.

Console(Console&&) noexcept = default

Standard move constructor.

inline Console &operator=(Console &&rhs) noexcept

Standard move assignment.

~Console() noexcept = default

Standard destructor.

inline operator TCOD_Console&()

Allow implicit conversions to a TCOD_Console reference.

inline operator const TCOD_Console&() const

Allow implicit conversions to a const TCOD_Console reference.

inline auto get() noexcept -> TCOD_Console*

Return a pointer to the internal TCOD_Console struct.

inline auto get() const noexcept -> const TCOD_Console*

Return a const pointer to the internal TCOD_Console struct.

inline auto release() noexcept -> TCOD_Console*

Release ownership of this Console’s TCOD_Console* and return the pointer.

Using this Console afterwards is undefined.

inline auto begin() noexcept -> TCOD_ConsoleTile*

Return a pointer to the beginning of this consoles tile data.

inline auto begin() const noexcept -> const TCOD_ConsoleTile*

Return a const pointer to the beginning of this consoles tile data.

inline auto end() noexcept -> TCOD_ConsoleTile*

Return a pointer to the end of this consoles tile data.

inline auto end() const noexcept -> const TCOD_ConsoleTile*

Return a const pointer to the end of this consoles tile data.

inline auto get_width() const noexcept -> int

Return the width of this console.

inline auto get_height() const noexcept -> int

Return the height of this console.

inline auto get_shape() const noexcept -> std::array<int, 2>

Return the {width, height} shape of this console as a std::array<int, 2>.

auto console = tcod::Console{80, 50};
auto same_size = tcod::Console{console.get_shape()}  // New console with the same shape of the previous one.
inline 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.

// New consoles start already cleared with the space character, a white foreground, and a black background.
auto console = tcod::Console{80, 50};
console.clear()  // Clear with the above mentioned defaults.
console.clear({0x20, {255, 255, 255, 255}, {0, 0, 0, 255}});  // Same as the above.
console.clear({0x20, tcod::ColorRGB{255, 255, 255}, tcod::ColorRGB{0, 0, 0}})  // Also same as the above.

Parameters

tile – A TCOD_ConsoleTile reference which will be used to clear the console.

inline auto operator[](const std::array<int, 2> &xy) noexcept -> TCOD_ConsoleTile&

Return a reference to the tile at xy.

inline auto operator[](const std::array<int, 2> &xy) const noexcept -> const TCOD_ConsoleTile&

Return a constant reference to the tile at xy.

inline auto at(const std::array<int, 2> &xy) -> TCOD_ConsoleTile&

Return a reference to the tile at xy.

Throws

std::out_of_range – if the index is out-of-bounds

inline auto at(const std::array<int, 2> &xy) const -> const TCOD_ConsoleTile&

Return a constant reference to the tile at xy.

Throws

std::out_of_range – if the index is out-of-bounds

inline auto at(int x, int y) -> TCOD_ConsoleTile&

Return a reference to the tile at x,y.

Throws

std::out_of_range – if the index is out-of-bounds

inline auto at(int x, int y) const -> const TCOD_ConsoleTile&

Return a constant reference to the tile at x,y.

Throws

std::out_of_range – if the index is out-of-bounds

inline bool in_bounds(const std::array<int, 2> &xy) const noexcept

Return true if xy are within the bounds of this console.

Friends

inline friend void swap(Console &lhs, Console &rhs) noexcept

Swap two console objects.