29 int const bpp = surface->format->BytesPerPixel;
31 SDL_LockSurface(surface);
33 std::ptrdiff_t
const offset = (
static_cast<std::ptrdiff_t
>(y) *
static_cast<std::ptrdiff_t
>(surface->pitch)) +
34 (
static_cast<std::ptrdiff_t
>(x) *
static_cast<std::ptrdiff_t
>(bpp));
36 std::span<Uint8 const>
const pixels(
37 reinterpret_cast<Uint8*
>(surface->pixels),
38 static_cast<size_t>(surface->h) *
static_cast<size_t>(surface->pitch));
39 size_t const idx =
static_cast<size_t>(offset);
41 unsigned int color = 0U;
50 std::memcpy(&tmp, &pixels[idx],
sizeof(Uint16));
51 color =
static_cast<unsigned int>(tmp);
56 if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
57 color = (
static_cast<unsigned int>(pixels[idx]) << 16) |
58 (
static_cast<unsigned int>(pixels[idx + 1]) << 8) |
static_cast<unsigned int>(pixels[idx + 2]);
60 color =
static_cast<unsigned int>(pixels[idx]) | (
static_cast<unsigned int>(pixels[idx + 1]) << 8) |
61 (
static_cast<unsigned int>(pixels[idx + 2]) << 16);
67 std::memcpy(&tmp, &pixels[idx],
sizeof(Uint32));
68 color =
static_cast<unsigned int>(tmp);
81 SDL_GetRGBA(color, surface->format, &r, &g, &b, &a);
82 SDL_UnlockSurface(surface);
97 int const bpp = surface->format->BytesPerPixel;
99 SDL_LockSurface(surface);
101 std::ptrdiff_t
const offset = (
static_cast<std::ptrdiff_t
>(y) *
static_cast<std::ptrdiff_t
>(surface->pitch)) +
102 (
static_cast<std::ptrdiff_t
>(x) *
static_cast<std::ptrdiff_t
>(bpp));
104 std::span<Uint8>
const pixels(
105 reinterpret_cast<Uint8*
>(surface->pixels),
106 static_cast<size_t>(surface->h) *
static_cast<size_t>(surface->pitch));
107 size_t const idx =
static_cast<size_t>(offset);
109 Uint32
const pixel = SDL_MapRGB(surface->format, color.
r, color.
g, color.
b);
113 pixels[idx] =
static_cast<Uint8
>(pixel);
117 Uint16 tmp =
static_cast<Uint16
>(pixel);
118 std::memcpy(&pixels[idx], &tmp,
sizeof(Uint16));
123 if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
124 pixels[idx] =
static_cast<Uint8
>((pixel >> 16) & 0xff);
125 pixels[idx + 1] =
static_cast<Uint8
>((pixel >> 8) & 0xff);
126 pixels[idx + 2] =
static_cast<Uint8
>(pixel & 0xff);
128 pixels[idx] =
static_cast<Uint8
>(pixel & 0xff);
129 pixels[idx + 1] =
static_cast<Uint8
>((pixel >> 8) & 0xff);
130 pixels[idx + 2] =
static_cast<Uint8
>((pixel >> 16) & 0xff);
136 std::memcpy(&pixels[idx], &tmp,
sizeof(Uint32));
144 SDL_UnlockSurface(surface);
169 inline T
SDLBlendColor(T src, T dst,
unsigned char alpha, SDL_PixelFormat
const * f)
171 return (
SDLBlend(src & f->Rmask, dst & f->Rmask, alpha) & f->Rmask) |
172 (
SDLBlend(src & f->Gmask, dst & f->Gmask, alpha) & f->Gmask) |
173 (
SDLBlend(src & f->Bmask, dst & f->Bmask, alpha) & f->Bmask);
200 if (color.
a == 255) {
205 int const bpp = surface->format->BytesPerPixel;
208 if (color.
a == 255) {
213 SDL_LockSurface(surface);
215 std::ptrdiff_t
const offset = (
static_cast<std::ptrdiff_t
>(y) *
static_cast<std::ptrdiff_t
>(surface->pitch)) +
216 (
static_cast<std::ptrdiff_t
>(x) *
static_cast<std::ptrdiff_t
>(bpp));
218 std::span<Uint8>
const pixels(
219 reinterpret_cast<Uint8*
>(surface->pixels),
220 static_cast<size_t>(surface->h) *
static_cast<size_t>(surface->pitch));
221 size_t const idx =
static_cast<size_t>(offset);
225 Uint32
const pixel = SDL_MapRGB(surface->format, color.
r, color.
g, color.
b);
227 SDL_Color
const * colors = surface->format->palette->colors;
228 SDL_Color
const & sourceColor = colors[pixel];
229 SDL_Color
const & destColor = colors[pixels[idx]];
231 pixels[idx] =
static_cast<Uint8
>(SDL_MapRGB(
233 SDLBlend(sourceColor.r, destColor.r, color.
a),
234 SDLBlend(sourceColor.g, destColor.g, color.
a),
235 SDLBlend(sourceColor.b, destColor.b, color.
a)));
239 Uint32
const pixel = SDL_MapRGB(surface->format, color.
r, color.
g, color.
b);
241 std::memcpy(&dest, &pixels[idx],
sizeof(Uint16));
243 std::memcpy(&pixels[idx], &result,
sizeof(Uint16));
247 if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
259 Uint32
const pixel = SDL_MapRGB(surface->format, color.
r, color.
g, color.
b);
261 std::memcpy(&dest, &pixels[idx],
sizeof(Uint32));
263 std::memcpy(&pixels[idx], &result,
sizeof(Uint32));
271 SDL_UnlockSurface(surface);
T SDLBlendColor(T src, T dst, unsigned char alpha, SDL_PixelFormat const *f)
Blends two packed color values together using the supplied pixel format.
void SDLputPixelAlpha(SDL_Surface *surface, int x, int y, Color const &color)
Puts a pixel on an SDL_Surface with alpha blending.
void SDLputPixel(SDL_Surface *surface, int x, int y, Color const &color)
Puts a pixel on an SDL_Surface.