libtcod
Loading...
Searching...
No Matches
image.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// 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
54typedef std::unique_ptr<TCOD_Image, ImageDeleter> ImagePtr;
55} // namespace tcod
56class TCODConsole;
57
58class TCODLIB_API TCODImage {
59public :
66
68 TCODImage() noexcept = default;
69
85 TCODImage(int width, int height);
86
101 TCODImage(const char *filename);
102
118 TCODImage(const TCODConsole *console);
119
120
127 TCODImage(tcod::ImagePtr image) noexcept: data{image.release()}, deleteData{true} {};
128
129 TCODImage(const TCODImage&) = delete;
130 TCODImage& operator=(const TCODImage&) = delete;
131 TCODImage(TCODImage&& rhs) noexcept {
132 std::swap(data, rhs.data);
133 std::swap(deleteData, rhs.deleteData);
134 };
135 TCODImage& operator=(TCODImage&& rhs) noexcept {
136 std::swap(data, rhs.data);
137 std::swap(deleteData, rhs.deleteData);
138 return *this;
139 };
140
141 // clang-format on
142
149 explicit TCODImage(const tcod::Matrix<TCOD_ColorRGB, 2>& pixels)
150 : TCODImage(pixels.get_shape().at(0), pixels.get_shape().at(1)) {
151 for (int y = 0; y < pixels.get_shape().at(1); ++y) {
152 for (int x = 0; x < pixels.get_shape().at(0); ++x) {
153 putPixel(x, y, pixels[{x, y}]);
154 }
155 }
156 }
157 // clang-format off
158
182 void refreshConsole(const TCODConsole *console);
183
209 void getSize(int *w,int *h) const;
210
211
218 [[nodiscard]] auto getSize() const noexcept -> std::array<int, 2> {
219 std::array<int, 2> out{};
220 TCOD_image_get_size(data, &out[0], &out[1]);
221 return out;
222 }
245 TCODColor getPixel(int x, int y) const;
246
260 int getAlpha(int x,int y) const;
261
275 bool isPixelTransparent(int x, int y) const;
276
304 TCODColor getMipmapPixel(float x0,float y0, float x1, float y1);
305
319 void clear(const TCODColor col);
320
334 void putPixel(int x, int y, const TCODColor col);
335
347 void scale(int new_w, int new_h);
348
358 void hflip();
359
369 void vflip();
370
382 void rotate90(int numRotations=1);
383
393 void invert();
394
416 void save(const char *filename) const;
417
438 void blitRect(TCODConsole *console, int x, int y, int w=-1, int h=-1, TCOD_bkgnd_flag_t bkgnd_flag = TCOD_BKGND_SET ) const;
439 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 {
440 TCOD_image_blit_rect(data, &console, x, y, w, h, bkgnd_flag);
441 }
442
464 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;
465 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 {
466 TCOD_image_blit(data, &console, x, y, bkgnd_flag, scale_x, scale_y, angle);
467 }
468
493 void setKeyColor(const TCODColor keyColor);
494
520 void blit2x(TCODConsole *dest, int dx, int dy, int sx=0, int sy=0, int w=-1, int h=-1) const;
521 [[deprecated("This call is replaced by tcod::draw_quartergraphics.")]]
522 void blit2x(TCOD_Console& dest, int dx, int dy, int sx=0, int sy=0, int w=-1, int h=-1) const {
523 TCOD_image_blit_2x(data, &dest, dx, dy, sx, sy, w, h);
524 }
525
533 TCOD_Image* get_data() noexcept { return data; }
541 const TCOD_Image* get_data() const noexcept { return data; }
542
543 [[deprecated("This only makes a reference to the image data."
544 " If you intended to pass ownership then use `TCODImage{tcod::ImagePtr{image_ptr}}`")]]
545 TCODImage(TCOD_image_t img) : data(img), deleteData(false) {}
546 virtual ~TCODImage();
547
553 [[nodiscard]] operator TCOD_Image&() { return *data; }
554
560 [[nodiscard]] operator const TCOD_Image&() const { return *data; }
561
562protected :
563 struct TCOD_Image *data{nullptr};
564 bool deleteData{false};
565};
566// clang-format on
567namespace tcod {
568
584inline void draw_quartergraphics(
585 TCOD_Console& dest,
586 const TCOD_Image& source,
587 const std::array<int, 2>& dest_xy = {0, 0},
588 const std::array<int, 4>& src_rect = {0, 0, -1, -1}) {
589 TCOD_image_blit_2x(
590 &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));
591}
592} // namespace tcod
593#endif // TCOD_IMAGE_HPP_
Classic turn by turn game loop:TCODConsole::initRoot(80,50,"my game",false); while (!...
Definition console.hpp:137
constexpr const shape_type & get_shape() const noexcept
Return the shape of this matrix.
Definition matrix.hpp:180
Color handling module.
C++ console module.
void blit(TCOD_Console &dest, const TCOD_Console &source, const std::array< int, 2 > &dest_xy={0, 0}, std::array< int, 4 > source_rect={0, 0, 0, 0}, float foreground_alpha=1.0f, float background_alpha=1.0f)
Blit a region of tiles from one console to another.
Definition console.hpp:65
TCOD_bkgnd_flag_t
Background color blend modes.
Definition console.h:60
Image handling module.
The libtcod namespace.
Definition color.hpp:45
std::unique_ptr< TCOD_Image, ImageDeleter > ImagePtr
A unique pointer to a TCOD_Image.
Definition image.hpp:54
Definition image.h:54
Definition image.hpp:46