6#include "fifechan/backends/sdl3/image.hpp"
13#include <SDL3_image/SDL_image.h>
16#include "fifechan/backends/sdl3/imageloader.hpp"
17#include "fifechan/exception.hpp"
23 if (renderer !=
nullptr && surface !=
nullptr) {
32 SDL_SetSurfaceRLE(surface,
false);
34 if (SDL_GetPixelFormatDetails(surface->format)->bits_per_pixel != 32) {
35 SDL_Surface* converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA8888);
36 if (converted !=
nullptr) {
38 SDL_DestroySurface(surface);
45 Uint32 existingKey = 0;
46 if (SDL_GetSurfaceColorKey(surface, &existingKey) ==
false) {
52 mTexture = SDL_CreateTextureFromSurface(renderer, surface);
54 throwException(std::string(
"Failed to create texture: ") + SDL_GetError());
57 SDL_SetTextureBlendMode(
mTexture, SDL_BLENDMODE_BLEND);
58 SDL_SetTextureScaleMode(
mTexture, SDL_SCALEMODE_NEAREST);
62 SDL_GetTextureSize(
mTexture, &wFloat, &hFloat);
64 int const w =
static_cast<int>(wFloat);
65 int const h =
static_cast<int>(hFloat);
70 throwException(std::string(
"Failed to create transient surface: ") + SDL_GetError());
74 SDL_Surface* converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA8888);
75 if (converted !=
nullptr) {
77 SDL_DestroySurface(converted);
83 Uint32 transientKey = 0;
92 SDL_DestroySurface(surface);
94 }
else if (autoFree && surface !=
nullptr) {
95 SDL_DestroySurface(surface);
127 SDL_GetTextureSize(
mTexture, &wFloat, &hFloat);
129 return static_cast<int>(wFloat);
135 throwException(
"Trying to get the height of a non loaded image.");
140 SDL_GetTextureSize(
mTexture, &wFloat, &hFloat);
142 return static_cast<int>(hFloat);
172 throwException(
"Trying to convert a non loaded image to display format.");
181 throwException(std::string(
"Failed to create texture in convertToDisplayFormat: ") + SDL_GetError());
183 SDL_SetTextureBlendMode(
mTexture, SDL_BLENDMODE_BLEND);
184 SDL_SetTextureScaleMode(
mTexture, SDL_SCALEMODE_NEAREST);
uint8_t a
Alpha color component (0-255).
uint8_t b
Blue color component (0-255).
uint8_t g
Green color component (0-255).
uint8_t r
Red color component (0-255).
void convertToDisplayFormat() override
Converts the image, if possible, to display format.
Color getPixel(int x, int y) override
Gets the color of a pixel at coordinate (x, y) in the image.
virtual SDL_Surface * getSurface() const
Gets the SDL surface for the image.
Image(SDL_Surface *surface, bool autoFree, SDL_Renderer *renderer=nullptr)
Constructor.
int getWidth() const override
Gets the width of the image.
SDL_Texture * mTexture
SDL texture for accelerated rendering (primary storage).
int getHeight() const override
Gets the height of the image.
bool mAutoFree
Whether the transient surface should be freed on destruction.
SDL_Surface * mTransientSurface
Transient surface for pixel operations (getPixel/putPixel).
void putPixel(int x, int y, Color const &color) override
Puts a pixel with a certain color at coordinate (x, y).
void free() override
Frees an image.
virtual SDL_Texture * getTexture() const
Gets the SDL texture for the image.
SDL_Renderer * mRenderer
SDL renderer associated with the texture.
void throwException(std::string const &message, std::source_location location=std::source_location::current())
Throw an Exception capturing the current source location.