libtcod
Loading...
Searching...
No Matches
image.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// clang-format off
35#pragma once
36#ifndef TCOD_IMAGE_HPP_
37#define TCOD_IMAGE_HPP_
38
39#include "color.hpp"
40
41#include "console.hpp"
42#include "image.h"
43#include "matrix.hpp"
44
45namespace tcod {
47 void operator()(TCOD_Image* image) const { TCOD_image_delete(image); }
48};
49
56typedef std::unique_ptr<TCOD_Image, ImageDeleter> ImagePtr;
57} // namespace tcod
58class TCODConsole;
59
60class TCODLIB_API TCODImage {
61public :
68
70 TCODImage() noexcept = default;
71
87 TCODImage(int width, int height);
88
103 TCODImage(const char *filename);
104
120 TCODImage(const TCODConsole *console);
121
122
129 TCODImage(tcod::ImagePtr image) noexcept: data{image.release()}, deleteData{true} {};
130
131 TCODImage(const TCODImage&) = delete;
132 TCODImage& operator=(const TCODImage&) = delete;
133 TCODImage(TCODImage&& rhs) noexcept {
134 std::swap(data, rhs.data);
135 std::swap(deleteData, rhs.deleteData);
136 };
137 TCODImage& operator=(TCODImage&& rhs) noexcept {
138 std::swap(data, rhs.data);
139 std::swap(deleteData, rhs.deleteData);
140 return *this;
141 };
142
143 // clang-format on
144
152 : TCODImage(pixels.get_shape().at(0), pixels.get_shape().at(1)) {
153 for (int y = 0; y < pixels.get_shape().at(1); ++y) {
154 for (int x = 0; x < pixels.get_shape().at(0); ++x) {
155 putPixel(x, y, pixels[{x, y}]);
156 }
157 }
158 }
159 // clang-format off
160
184 void refreshConsole(const TCODConsole *console);
185
211 void getSize(int *w,int *h) const;
212
213
220 [[nodiscard]] auto getSize() const noexcept -> std::array<int, 2> {
221 std::array<int, 2> out{};
222 TCOD_image_get_size(data, &out[0], &out[1]);
223 return out;
224 }
225
247 TCODColor getPixel(int x, int y) const;
248
262 int getAlpha(int x,int y) const;
263
277 bool isPixelTransparent(int x, int y) const;
278
306 TCODColor getMipmapPixel(float x0,float y0, float x1, float y1);
307
321 void clear(const TCODColor col);
322
336 void putPixel(int x, int y, const TCODColor col);
337
349 void scale(int new_w, int new_h);
350
360 void hflip();
361
371 void vflip();
372
384 void rotate90(int numRotations=1);
385
395 void invert();
396
418 void save(const char *filename) const;
419
440 void blitRect(TCODConsole *console, int x, int y, int w=-1, int h=-1, TCOD_bkgnd_flag_t bkgnd_flag = TCOD_BKGND_SET ) const;
441 void blitRect(TCOD_Console &console, int x, int y, int w=-1, int h=-1, TCOD_bkgnd_flag_t bkgnd_flag = TCOD_BKGND_SET ) const {
442 TCOD_image_blit_rect(data, &console, x, y, w, h, bkgnd_flag);
443 }
444
466 void blit(TCODConsole *console, float x, float y, TCOD_bkgnd_flag_t bkgnd_flag = TCOD_BKGND_SET, float scale_x=1.0f, float scale_y=1.0f, float angle=0.0f) const;
467 void blit(TCOD_Console& console, float x, float y, TCOD_bkgnd_flag_t bkgnd_flag = TCOD_BKGND_SET, float scale_x=1.0f, float scale_y=1.0f, float angle=0.0f) const {
468 TCOD_image_blit(data, &console, x, y, bkgnd_flag, scale_x, scale_y, angle);
469 }
470
495 void setKeyColor(const TCODColor keyColor);
496
522 void blit2x(TCODConsole *dest, int dx, int dy, int sx=0, int sy=0, int w=-1, int h=-1) const;
523 [[deprecated("This call is replaced by tcod::draw_quartergraphics.")]]
524 void blit2x(TCOD_Console& dest, int dx, int dy, int sx=0, int sy=0, int w=-1, int h=-1) const {
525 TCOD_image_blit_2x(data, &dest, dx, dy, sx, sy, w, h);
526 }
527
535 TCOD_Image* get_data() noexcept { return data; }
543 const TCOD_Image* get_data() const noexcept { return data; }
544
545 [[deprecated("This only makes a reference to the image data."
546 " If you intended to pass ownership then use `TCODImage{tcod::ImagePtr{image_ptr}}`")]]
547 TCODImage(TCOD_image_t img) : data(img), deleteData(false) {}
548 virtual ~TCODImage();
549
555 [[nodiscard]] operator TCOD_Image&() { return *data; }
556
562 [[nodiscard]] operator const TCOD_Image&() const { return *data; }
563
564protected :
565 struct TCOD_Image *data{nullptr};
566 bool deleteData{false};
567};
568// clang-format on
569namespace tcod {
570
589 TCOD_Console& dest,
590 const TCOD_Image& source,
591 const std::array<int, 2>& dest_xy = {0, 0},
592 const std::array<int, 4>& src_rect = {0, 0, -1, -1}) {
593 TCOD_image_blit_2x(
594 &source, &dest, dest_xy.at(0), dest_xy.at(1), src_rect.at(0), src_rect.at(1), src_rect.at(2), src_rect.at(3));
595}
596} // namespace tcod
597#endif // TCOD_IMAGE_HPP_
Definition color.hpp:221
Classic turn by turn game loop:TCODConsole::initRoot(80,50,"my game",false); while (!...
Definition console.hpp:137
Definition image.hpp:60
void blit2x(TCODConsole *dest, int dx, int dy, int sx=0, int sy=0, int w=-1, int h=-1) const
Eventually, you can use some special characters in the libtcod fonts :
auto getSize() const noexcept -> std::array< int, 2 >
Get the {width, height} of this image.
Definition image.hpp:220
bool isPixelTransparent(int x, int y) const
You can use this simpler version (for images with alpha layer, returns true only if alpha == 0) :
void blit(TCODConsole *console, float x, float y, TCOD_bkgnd_flag_t bkgnd_flag=TCOD_BKGND_SET, float scale_x=1.0f, float scale_y=1.0f, float angle=0.0f) const
This function allows you to specify the floating point coordinates of the center of the image,...
void hflip()
void rotate90(int numRotations=1)
Rotate the image clockwise by increment of 90 degrees.
void blitRect(TCODConsole *console, int x, int y, int w=-1, int h=-1, TCOD_bkgnd_flag_t bkgnd_flag=TCOD_BKGND_SET) const
This function blits a rectangular part of the image on a console without scaling it or rotating it.
TCODImage(const tcod::Matrix< TCOD_ColorRGB, 2 > &pixels)
Construct a new TCODImage object from a Matrix of pixels.
Definition image.hpp:151
int getAlpha(int x, int y) const
If you have set a key color for this image with setKeyColor, or if this image was created from a 32 b...
void setKeyColor(const TCODColor keyColor)
When blitting an image, you can define a key color that will be ignored by the blitting function.
void vflip()
TCOD_Image * get_data() noexcept
Return the pointer to this objects TCOD_Image data.
Definition image.hpp:535
void scale(int new_w, int new_h)
You can resize an image and scale its content.
void putPixel(int x, int y, const TCODColor col)
void clear(const TCODColor col)
You can fill the whole image with a color with :
TCODColor getMipmapPixel(float x0, float y0, float x1, float y1)
This method uses mipmaps to get the average color of an arbitrary rectangular region of the image.
void save(const char *filename) const
void invert()
TCODImage() noexcept=default
Default constructs an image. This will be in a partially invalid state until assigned a real image.
void getSize(int *w, int *h) const
You can read the size of an image in pixels with this function.
const TCOD_Image * get_data() const noexcept
Return the const pointer to this objects TCOD_Image data.
Definition image.hpp:543
TCODColor getPixel(int x, int y) const
You can read the colors from an image with this function.
void refreshConsole(const TCODConsole *console)
If you need to refresh the image with the console's new content, you don't have to delete it and crea...
A template container for holding a multi-dimensional array of items.
Definition matrix.hpp:139
constexpr const shape_type & get_shape() const noexcept
Return the shape of this matrix.
Definition matrix.hpp:180
Color handling module.
C++ console module.
TCOD_bkgnd_flag_t
Background color blend modes.
Definition console.h:60
Image handling module.
The libtcod namespace.
Definition bresenham.hpp:157
std::unique_ptr< TCOD_Image, ImageDeleter > ImagePtr
A unique pointer to a TCOD_Image.
Definition image.hpp:56
void draw_quartergraphics(TCOD_Console &dest, const TCOD_Image &source, const std::array< int, 2 > &dest_xy={0, 0}, const std::array< int, 4 > &src_rect={0, 0, -1, -1})
Draw a double resolution image on a console using quadrant character glyphs.
Definition image.hpp:588
A libtcod console containing a grid of tiles with {ch, fg, bg} information.
Definition console.h:125
Definition image.h:54
Definition image.hpp:46