5std::vector<std::string> Image::paths = {
"./"};
16Image::Image(std::string filename) {
24 std::string error = std::string(
"Error loading image: ") +
27 throw std::runtime_error(error);
43 std::string error = std::string(
"Error loading image: ") +
46 throw std::runtime_error(error);
51 SDL_FreeSurface(
image);
58 SDL_FreeSurface(
image);
69 Uint32 rmask, gmask, bmask, amask;
70#if SDL_BYTEORDER == SDL_BIG_ENDIAN
83 rmask, gmask, bmask, amask);
86 "empty image: ") + SDL_GetError());
99 SDL_FreeSurface(
image);
105 if((filename.length() > 1 && filename[0] ==
'/') ||
106 (filename.length() > 2 && filename[1] ==
':')) {
108 image = IMG_Load(filename.c_str());
111 for(
unsigned int i = 0; i < paths.size(); i++) {
112 image = IMG_Load((paths[i] + filename).c_str());
119 + filename + std::string(
" - ") + IMG_GetError());
141 SDL_FreeSurface(
image);
147 Uint32 rmask, gmask, bmask, amask;
148#if SDL_BYTEORDER == SDL_BIG_ENDIAN
165 this->bytes_per_pixel = 0;
166 this->data =
nullptr;
177 "Unable to allocate memory"));
180 this->bytes_per_pixel = 0;
189 rmask, gmask, bmask, amask);
193 "image from buffer: ") + SDL_GetError());
195 this->data =
nullptr;
198 this->bytes_per_pixel = 0;
206 int ret = SDL_SaveBMP(
image, filename.c_str());
209 "image to ") + filename + std::string(
": ") + SDL_GetError());
216TTF_Font *Image::open_font_file(
const std::string& font_file,
unsigned int size) {
218 if((font_file.length() > 1 && font_file[0] ==
'/') ||
219 (font_file.length() > 2 && font_file[1] ==
':')) {
221 font = TTF_OpenFont(font_file.c_str(), size);
224 for(
unsigned int i = 0; i < paths.size(); i++) {
225 font = TTF_OpenFont((paths[i] + font_file).c_str(), size);
233void Image::close_font_file(TTF_Font *font_file) {
234 TTF_CloseFont(font_file);
237bool Image::create_text(
const std::string& text,
238 TTF_Font *font, Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
242 SDL_FreeSurface(
image);
247 SDL_Color color = {r, g, b, a};
248 image = TTF_RenderUTF8_Blended(font, text.c_str(), color);
251 std::string(
"TTF_RenderUTF8_Blended failed: ") +
263bool Image::create_text(
const std::string& text,
const std::string& font_file,
264 unsigned int size, Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
265 TTF_Font *font = open_font_file(font_file, size);
268 + font_file + std::string(
" failed."));
272 bool ret = create_text(text, font, r, g, b, a);
277 close_font_file(font);
295 " failed: ") + SDL_GetError());
299 if(SDL_BlitSurface(src.
image,
nullptr,
image, &dst_rect) < 0) {
301 " failed: ") + SDL_GetError());
308 const SDL_Rect& src_rect) {
314 " failed: ") + SDL_GetError());
318 if(SDL_BlitSurface(src.
image, &src_rect,
image, &dst_rect) < 0) {
320 " failed: ") + SDL_GetError());
334 unsigned int bpp =
image->format->BytesPerPixel;
336 std::unique_ptr<unsigned char[]> buf = std::make_unique<unsigned char[]>(
width *
height * bpp);
338 for(
unsigned int y = 0; y <
height; y++) {
339 for(
unsigned int x = 0; x <
width; x++) {
340 for(
unsigned int c = 0; c < bpp; c++) {
341 buf[(y *
width + x) * bpp + c] =
354 unsigned int bpp =
image->format->BytesPerPixel;
356 std::unique_ptr<unsigned char[]> buf = std::make_unique<unsigned char[]>(
width *
height * bpp);
358 for(
unsigned int y = 0; y <
height; y++) {
359 for(
unsigned int x = 0; x <
width; x++) {
360 for(
unsigned int c = 0; c < bpp; c++) {
361 buf[(y *
width + x) * bpp + c] =
362 ((
unsigned char *)
image->pixels)[(y *
width + (
width - x - 1)) * bpp + c];
371 SDL_SetColorKey(
image, enable, SDL_MapRGB(
image->format, r, g, b));
375 if(path[path.length() - 1] !=
'/')
378 if(std::find(paths.begin(), paths.end(), path) == paths.end())
379 paths.push_back(path);
static std::vector< std::string > error_string
A list of all error strings.
void * data
The pixel data.
bool save(const std::string &filename)
Saves the image as a file.
bool copy_from(const Image &src, int x, int y)
Copies an image into the current image.
unsigned int height
The width of the image surface.
bool create_empty(unsigned int width, unsigned int height)
Creates an empty image.
SDL_Surface * image
The image surface.
void set_color_key(int r, int g, int b, bool enable=true)
Sets a color to be transparent.
static void add_path(std::string path)
Adds a path to the list of paths to be searched for image files.
void vertical_flip()
Flips the image along its x-axis.
bool load(const std::string &filename)
Loads a new image file.
void horizontal_flip()
Flips the image along its y-axis.
unsigned int bytes_per_pixel
The number of bytes that make up a single pixel.
unsigned int width
The width of the image surface.