Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

factor out all truecolor blending functions into their own source file #1230

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
114 changes: 114 additions & 0 deletions src/i_truecolor.c
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions src/i_truecolor.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>

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
79 changes: 0 additions & 79 deletions src/i_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/i_video.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define __I_VIDEO__

#include "doomtype.h"
#include "i_truecolor.h"
#include "m_fixed.h" // [crispy] fixed_t
#include "crispy.h"

Expand Down
10 changes: 0 additions & 10 deletions src/v_trans.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading