From f7e4e0c42de1453cd0481267d70293bd35a7be41 Mon Sep 17 00:00:00 2001 From: Fabian Greffrath Date: Fri, 20 Sep 2024 11:46:04 +0200 Subject: [PATCH] factor out all truecolor blending functions into their own source file --- src/CMakeLists.txt | 1 + src/Makefile.am | 1 + src/i_truecolor.c | 114 +++++++++++++++++++++++++++++++++++++++++++++ src/i_truecolor.h | 43 +++++++++++++++++ src/i_video.c | 79 ------------------------------- src/i_video.h | 1 + src/v_trans.h | 10 ---- 7 files changed, 160 insertions(+), 89 deletions(-) create mode 100644 src/i_truecolor.c create mode 100644 src/i_truecolor.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bb2625d08..83976bcb9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -66,6 +66,7 @@ set(GAME_SOURCE_FILES i_sdlsound.c i_sound.c i_sound.h i_timer.c i_timer.h + i_truecolor.c i_truecolor.h i_video.c i_video.h i_videohr.c i_videohr.h i_winmusic.c diff --git a/src/Makefile.am b/src/Makefile.am index 7a9a5a479..b5b900951 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -83,6 +83,7 @@ i_sdlmusic.c \ i_sdlsound.c \ i_sound.c i_sound.h \ i_timer.c i_timer.h \ +i_truecolor.c i_truecolor.h \ i_video.c i_video.h \ i_videohr.c i_videohr.h \ i_winmusic.c \ diff --git a/src/i_truecolor.c b/src/i_truecolor.c new file mode 100644 index 000000000..fb315b9f5 --- /dev/null +++ b/src/i_truecolor.c @@ -0,0 +1,114 @@ +// +// Copyright(C) 1993-1996 Id Software, Inc. +// Copyright(C) 2005-2014 Simon Howard +// Copyright(C) 2015-2024 Fabian Greffrath +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// DESCRIPTION: +// [crispy] Truecolor rendering +// + +#include "config.h" + +#ifdef CRISPY_TRUECOLOR + +#include "crispy.h" +#include "i_truecolor.h" + +const uint32_t (*blendfunc) (const uint32_t fg, const uint32_t bg) = I_BlendOverTranmap; + +typedef union +{ + uint32_t i; + struct { + uint8_t b; + uint8_t g; + uint8_t r; + uint8_t a; + }; +} tcpixel_t; + +const uint32_t I_BlendAdd (const uint32_t bg_i, const uint32_t fg_i) +{ + tcpixel_t bg, fg, ret; + + bg.i = bg_i; + fg.i = fg_i; + + ret.a = 0xFFU; + ret.r = MIN(bg.r + fg.r, 0xFFU); + ret.g = MIN(bg.g + fg.g, 0xFFU); + ret.b = MIN(bg.b + fg.b, 0xFFU); + + return ret.i; +} + +const uint32_t I_BlendDark (const uint32_t bg_i, const int d) +{ + tcpixel_t bg, ret; + + bg.i = bg_i; + + ret.a = 0xFFU; + ret.r = (bg.r * d) >> 8; + ret.g = (bg.g * d) >> 8; + ret.b = (bg.b * d) >> 8; + + return ret.i; +} + +const uint32_t I_BlendOver (const uint32_t bg_i, const uint32_t fg_i, const int amount) +{ + tcpixel_t bg, fg, ret; + + bg.i = bg_i; + fg.i = fg_i; + + ret.a = 0xFFU; + ret.r = (amount * fg.r + (0XFFU - amount) * bg.r) >> 8; + ret.g = (amount * fg.g + (0XFFU - amount) * bg.g) >> 8; + ret.b = (amount * fg.b + (0XFFU - amount) * bg.b) >> 8; + + return ret.i; +} + +// [crispy] TRANMAP blending emulation, used for Doom +const uint32_t I_BlendOverTranmap (const uint32_t bg, const uint32_t fg) +{ + return I_BlendOver(bg, fg, 0xA8); // 168 (66% opacity) +} + +// [crispy] TINTTAB blending emulation, used for Heretic and Hexen +const uint32_t I_BlendOverTinttab (const uint32_t bg, const uint32_t fg) +{ + return I_BlendOver(bg, fg, 0x60); // 96 (38% opacity) +} + +// [crispy] More opaque ("Alt") TINTTAB blending emulation, used for Hexen's MF_ALTSHADOW drawing +const uint32_t I_BlendOverAltTinttab (const uint32_t bg, const uint32_t fg) +{ + return I_BlendOver(bg, fg, 0x8E); // 142 (56% opacity) +} + +// [crispy] More opaque XLATAB blending emulation, used for Strife +const uint32_t I_BlendOverXlatab (const uint32_t bg, const uint32_t fg) +{ + return I_BlendOver(bg, fg, 0xC0); // 192 (75% opacity) +} + +// [crispy] Less opaque ("Alt") XLATAB blending emulation, used for Strife +const uint32_t I_BlendOverAltXlatab (const uint32_t bg, const uint32_t fg) +{ + return I_BlendOver(bg, fg, 0x40); // 64 (25% opacity) +} + +#endif diff --git a/src/i_truecolor.h b/src/i_truecolor.h new file mode 100644 index 000000000..a7d0194f3 --- /dev/null +++ b/src/i_truecolor.h @@ -0,0 +1,43 @@ +// +// Copyright(C) 1993-1996 Id Software, Inc. +// Copyright(C) 2005-2014 Simon Howard +// Copyright(C) 2015-2024 Fabian Greffrath +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// DESCRIPTION: +// [crispy] Truecolor rendering +// + +#ifndef __I_TRUECOLOR__ +#define __I_TRUECOLOR__ + +#include "config.h" + +#ifdef CRISPY_TRUECOLOR + +#include + +extern const uint32_t (*blendfunc) (const uint32_t fg, const uint32_t bg); + +const uint32_t I_BlendAdd (const uint32_t bg_i, const uint32_t fg_i); +const uint32_t I_BlendDark (const uint32_t bg_i, const int d); +const uint32_t I_BlendOver (const uint32_t bg_i, const uint32_t fg_i, const int amount); + +const uint32_t I_BlendOverTranmap (const uint32_t bg, const uint32_t fg); +const uint32_t I_BlendOverTinttab (const uint32_t bg, const uint32_t fg); +const uint32_t I_BlendOverAltTinttab (const uint32_t bg, const uint32_t fg); +const uint32_t I_BlendOverXlatab (const uint32_t bg, const uint32_t fg); +const uint32_t I_BlendOverAltXlatab (const uint32_t bg, const uint32_t fg); + +#endif + +#endif diff --git a/src/i_video.c b/src/i_video.c index e7ef10ecd..f4f81e7f2 100644 --- a/src/i_video.c +++ b/src/i_video.c @@ -2107,87 +2107,8 @@ void I_BindVideoVariables(void) } #ifdef CRISPY_TRUECOLOR -#define amask (0xFF000000U) -#define rmask (0x00FF0000U) -#define gmask (0x0000FF00U) -#define bmask (0x000000FFU) - -const pixel_t I_BlendAdd (const pixel_t bg, const pixel_t fg) -{ - uint32_t r, g, b; - - if ((r = (fg & rmask) + (bg & rmask)) > rmask) r = rmask; - if ((g = (fg & gmask) + (bg & gmask)) > gmask) g = gmask; - if ((b = (fg & bmask) + (bg & bmask)) > bmask) b = bmask; - - return amask | r | g | b; -} - -// [crispy] http://stereopsis.com/doubleblend.html -const pixel_t I_BlendDark (const pixel_t bg, const int d) -{ - const uint32_t ag = (bg & 0xff00ff00) >> 8; - const uint32_t rb = bg & 0x00ff00ff; - - uint32_t sag = d * ag; - uint32_t srb = d * rb; - - sag = sag & 0xff00ff00; - srb = (srb >> 8) & 0x00ff00ff; - - return amask | sag | srb; -} - -// [crispy] Main overlay blending function -const pixel_t I_BlendOver (const pixel_t bg, const pixel_t fg, const int amount) -{ - const uint32_t r = ((amount * (fg & rmask) + (0xff - amount) * (bg & rmask)) >> 8) & rmask; - const uint32_t g = ((amount * (fg & gmask) + (0xff - amount) * (bg & gmask)) >> 8) & gmask; - const uint32_t b = ((amount * (fg & bmask) + (0xff - amount) * (bg & bmask)) >> 8) & bmask; - - return amask | r | g | b; -} - -// [crispy] TRANMAP blending emulation, used for Doom -const pixel_t I_BlendOverTranmap (const pixel_t bg, const pixel_t fg) -{ - return I_BlendOver(bg, fg, 0xA8); // 168 (66% opacity) -} - -// [crispy] TINTTAB blending emulation, used for Heretic and Hexen -const pixel_t I_BlendOverTinttab (const pixel_t bg, const pixel_t fg) -{ - return I_BlendOver(bg, fg, 0x60); // 96 (38% opacity) -} - -// [crispy] More opaque ("Alt") TINTTAB blending emulation, used for Hexen's MF_ALTSHADOW drawing -const pixel_t I_BlendOverAltTinttab (const pixel_t bg, const pixel_t fg) -{ - return I_BlendOver(bg, fg, 0x8E); // 142 (56% opacity) -} - -// [crispy] More opaque XLATAB blending emulation, used for Strife -const pixel_t I_BlendOverXlatab (const pixel_t bg, const pixel_t fg) -{ - return I_BlendOver(bg, fg, 0xC0); // 192 (75% opacity) -} - -// [crispy] Less opaque ("Alt") XLATAB blending emulation, used for Strife -const pixel_t I_BlendOverAltXlatab (const pixel_t bg, const pixel_t fg) -{ - return I_BlendOver(bg, fg, 0x40); // 64 (25% opacity) -} - -const pixel_t (*blendfunc) (const pixel_t fg, const pixel_t bg) = I_BlendOverTranmap; - const pixel_t I_MapRGB (const uint8_t r, const uint8_t g, const uint8_t b) { -/* - return amask | - (((r * rmask) >> 8) & rmask) | - (((g * gmask) >> 8) & gmask) | - (((b * bmask) >> 8) & bmask); -*/ return SDL_MapRGB(argbbuffer->format, r, g, b); } #endif diff --git a/src/i_video.h b/src/i_video.h index 98b3c3cef..348b8f5de 100644 --- a/src/i_video.h +++ b/src/i_video.h @@ -21,6 +21,7 @@ #define __I_VIDEO__ #include "doomtype.h" +#include "i_truecolor.h" #include "m_fixed.h" // [crispy] fixed_t #include "crispy.h" diff --git a/src/v_trans.h b/src/v_trans.h index f5433c13b..9fab1571d 100644 --- a/src/v_trans.h +++ b/src/v_trans.h @@ -58,16 +58,6 @@ extern char **crstr; #ifndef CRISPY_TRUECOLOR extern byte *tranmap; -#else -extern const pixel_t (*blendfunc) (const pixel_t fg, const pixel_t bg); -extern const pixel_t I_BlendAdd (const pixel_t bg, const pixel_t fg); -extern const pixel_t I_BlendDark (const pixel_t bg, const int d); -extern const pixel_t I_BlendOver (const pixel_t bg, const pixel_t fg, const int amount); -extern const pixel_t I_BlendOverTranmap (const pixel_t bg, const pixel_t fg); -extern const pixel_t I_BlendOverTinttab (const pixel_t bg, const pixel_t fg); -extern const pixel_t I_BlendOverAltTinttab (const pixel_t bg, const pixel_t fg); -extern const pixel_t I_BlendOverXlatab (const pixel_t bg, const pixel_t fg); -extern const pixel_t I_BlendOverAltXlatab (const pixel_t bg, const pixel_t fg); #endif int V_GetPaletteIndex(byte *palette, int r, int g, int b);