From b0ca322b3f9aaa4eeb06c28569d0f2d66940f882 Mon Sep 17 00:00:00 2001 From: Misa Date: Mon, 2 Jan 2023 10:44:35 -0800 Subject: [PATCH] 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. --- desktop_version/src/GraphicsUtil.cpp | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/desktop_version/src/GraphicsUtil.cpp b/desktop_version/src/GraphicsUtil.cpp index 92ca330d..23f5c143 100644 --- a/desktop_version/src/GraphicsUtil.cpp +++ b/desktop_version/src/GraphicsUtil.cpp @@ -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; }