Program Listing for File image.hpp¶
↰ Return to documentation for file (libtcod/image.hpp)
/* BSD 3-Clause License
*
* Copyright © 2008-2026, Jice and the libtcod contributors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
// clang-format off
#pragma once
#ifndef TCOD_IMAGE_HPP_
#define TCOD_IMAGE_HPP_
#include "color.hpp"
#include "console.hpp"
#include "image.h"
#include "matrix.hpp"
namespace tcod {
struct ImageDeleter {
void operator()(TCOD_Image* image) const { TCOD_image_delete(image); }
};
typedef std::unique_ptr<TCOD_Image, ImageDeleter> ImagePtr;
} // namespace tcod
class TCODConsole;
class TCODLIB_API TCODImage {
public :
TCODImage() noexcept = default;
TCODImage(int width, int height);
TCODImage(const char *filename);
TCODImage(const TCODConsole *console);
TCODImage(tcod::ImagePtr image) noexcept: data{image.release()}, deleteData{true} {};
TCODImage(const TCODImage&) = delete;
TCODImage& operator=(const TCODImage&) = delete;
TCODImage(TCODImage&& rhs) noexcept {
std::swap(data, rhs.data);
std::swap(deleteData, rhs.deleteData);
};
TCODImage& operator=(TCODImage&& rhs) noexcept {
std::swap(data, rhs.data);
std::swap(deleteData, rhs.deleteData);
return *this;
};
// clang-format on
explicit TCODImage(const tcod::Matrix<TCOD_ColorRGB, 2>& pixels)
: TCODImage(pixels.get_shape().at(0), pixels.get_shape().at(1)) {
for (int y = 0; y < pixels.get_shape().at(1); ++y) {
for (int x = 0; x < pixels.get_shape().at(0); ++x) {
putPixel(x, y, pixels[{x, y}]);
}
}
}
// clang-format off
void refreshConsole(const TCODConsole *console);
void getSize(int *w,int *h) const;
[[nodiscard]] auto getSize() const noexcept -> std::array<int, 2> {
std::array<int, 2> out{};
TCOD_image_get_size(data, &out[0], &out[1]);
return out;
}
TCODColor getPixel(int x, int y) const;
int getAlpha(int x,int y) const;
bool isPixelTransparent(int x, int y) const;
TCODColor getMipmapPixel(float x0,float y0, float x1, float y1);
void clear(const TCODColor col);
void putPixel(int x, int y, const TCODColor col);
void scale(int new_w, int new_h);
void hflip();
void vflip();
void rotate90(int numRotations=1);
void invert();
void save(const char *filename) const;
void blitRect(TCODConsole *console, int x, int y, int w=-1, int h=-1, TCOD_bkgnd_flag_t bkgnd_flag = TCOD_BKGND_SET ) const;
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 {
TCOD_image_blit_rect(data, &console, x, y, w, h, bkgnd_flag);
}
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;
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 {
TCOD_image_blit(data, &console, x, y, bkgnd_flag, scale_x, scale_y, angle);
}
void setKeyColor(const TCODColor keyColor);
void blit2x(TCODConsole *dest, int dx, int dy, int sx=0, int sy=0, int w=-1, int h=-1) const;
[[deprecated("This call is replaced by tcod::draw_quartergraphics.")]]
void blit2x(TCOD_Console& dest, int dx, int dy, int sx=0, int sy=0, int w=-1, int h=-1) const {
TCOD_image_blit_2x(data, &dest, dx, dy, sx, sy, w, h);
}
TCOD_Image* get_data() noexcept { return data; }
const TCOD_Image* get_data() const noexcept { return data; }
[[deprecated("This only makes a reference to the image data."
" If you intended to pass ownership then use `TCODImage{tcod::ImagePtr{image_ptr}}`")]]
TCODImage(TCOD_image_t img) : data(img), deleteData(false) {}
virtual ~TCODImage();
[[nodiscard]] operator TCOD_Image&() { return *data; }
[[nodiscard]] operator const TCOD_Image&() const { return *data; }
protected :
struct TCOD_Image *data{nullptr};
bool deleteData{false};
};
// clang-format on
namespace tcod {
inline 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}) {
TCOD_image_blit_2x(
&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));
}
} // namespace tcod
#endif // TCOD_IMAGE_HPP_