1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-23 01:59:43 +01:00

Use SDL_GetRGBA and SDL_MapRGBA to read and draw pixels

Honestly not too sure why we ever wrote the mask handling logic
ourselves instead of using SDL functions. Hell, we even used SDL_MapRGB
for Graphics::getRGB before.
This commit is contained in:
Misa 2023-01-02 10:44:35 -08:00
parent 8dc088896f
commit b0ca322b3f

View file

@ -94,11 +94,6 @@ static void DrawPixel(SDL_Surface* surface, const int x, const int y, const SDL_
const SDL_PixelFormat* fmt = surface->format;
const int bpp = fmt->BytesPerPixel;
Uint32* pixel = (Uint32*) ((Uint8*) surface->pixels + y * surface->pitch + x * bpp);
const Uint32 packed =
(color.r << fmt->Rshift) |
(color.g << fmt->Gshift) |
(color.b << fmt->Bshift) |
(color.a << fmt->Ashift);
switch (bpp)
{
@ -109,8 +104,7 @@ static void DrawPixel(SDL_Surface* surface, const int x, const int y, const SDL_
return;
case 4:
*pixel = packed;
break;
*pixel = SDL_MapRGBA(fmt, color.r, color.g, color.b, color.a);
}
}
@ -119,32 +113,20 @@ SDL_Color ReadPixel(const SDL_Surface* surface, const int x, const int y)
const SDL_PixelFormat* fmt = surface->format;
const int bpp = surface->format->BytesPerPixel;
const Uint32* pixel = (Uint32*) ((Uint8*) surface->pixels + y * surface->pitch + x * bpp);
SDL_Color color = {0, 0, 0, 0};
switch (bpp)
{
case 1:
case 2:
case 3:
{
SDL_assert(0 && "Non-32-bit colors not supported!");
const SDL_Color color = {0, 0, 0, 0};
return color;
}
break;
case 4:
{
const SDL_Color color = {
(Uint8) ((*pixel & fmt->Rmask) >> fmt->Rshift),
(Uint8) ((*pixel & fmt->Gmask) >> fmt->Gshift),
(Uint8) ((*pixel & fmt->Bmask) >> fmt->Bshift),
(Uint8) ((*pixel & fmt->Amask) >> fmt->Ashift)
};
return color;
}
SDL_GetRGBA(*pixel, fmt, &color.r, &color.g, &color.b, &color.a);
}
/* shouldn't happen, but avoids warnings */
const SDL_Color color = {0, 0, 0, 0};
return color;
}