From e04ba31174a0bbeee698aa9bc823f7e84f3b22a8 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Wed, 25 Nov 2026 00:45:45 +0100 Subject: [PATCH] UI: TextureRenderCore's drawBitmapFaceWithBlendUnk at 100% Rewritten from the binary. gynt's draft a5ef76166e (sourcehold reimpl/TextureRenderCore, 87.1% there, 87.4% on main) was the starting point; its per-channel grouping and the signed division by 32 are kept. - Both loops are plain for loops over the face's 66 rows and 64 columns, as in drawBitmapFace. MSVC unrolls the RGB565 one by four itself, which gives the original's four pixels per iteration and its channel order. - In the RGB565 loop the surface pointer steps in both branches of the colour-key test, as the original steps it after every pixel. With one step behind the test, the unrolled copies write through offsets instead. - The RGB555 blend has one local per channel: as one expression, the RGB565 loop computes red before green. - The RGB555 path weighs the face by the strength, not 32 - strength, and leaves out the surface, as the original does; it is marked fixme. - The masks are main's ColorChannelMask constants. Narrow build with drawBitmapFace (100% on main): 100.0 raw and normalised, the original's 409 instructions. It needs no /GL line. Co-authored-by: Gynt Co-Authored-By: Claude Opus 5 Co-Authored-By: Claude Opus 5.5 --- .../drawBitmapFaceWithBlendUnk.cpp | 106 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/UI/Rendering/TextureRenderCore/drawBitmapFaceWithBlendUnk.cpp diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/drawBitmapFaceWithBlendUnk.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/drawBitmapFaceWithBlendUnk.cpp new file mode 100644 index 0000000000..4d6a78a7bb --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/drawBitmapFaceWithBlendUnk.cpp @@ -0,0 +1,106 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/Rendering/ColorChannelMask.hpp" +#include "OpenSHC/Rendering/ColorMode.hpp" +#include "OpenSHC/Rendering/Enums/RenderTarget.hpp" + +#include "OpenSHC/Globals/AlphaAndButtonSurfaceObj.hpp" +#include "OpenSHC/Globals/COL_MAGENTA.hpp" +#include "OpenSHC/Globals/DAT_TextureRenderCoreObject.hpp" +#include "OpenSHC/Globals/DAT_WindowAndDirectDraw.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + using OpenSHC::Rendering::CCM_RGB555_GREEN; + using OpenSHC::Rendering::CCM_RGB555_RED; + using OpenSHC::Rendering::CCM_RGB565_GREEN; + using OpenSHC::Rendering::CCM_RGB565_RED; + using OpenSHC::Rendering::CCM_RGB_BLUE; + + // Draws a face bitmap blended by a strength out of 32 (0: drawn plainly, 32: not drawn). + // FUNCTION: STRONGHOLDCRUSADER 0x0044CEB0 + void TextureRenderCore::drawBitmapFaceWithBlendUnk( + int bitmapFaceIndex, int xPos, int yPos, int colorOrBlendOrGammaUnk) + { + if (colorOrBlendOrGammaUnk == 0) { + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::drawBitmapFace, this)( + bitmapFaceIndex, xPos, yPos); + return; + } + if (colorOrBlendOrGammaUnk == 32) { + return; + } + // A face bitmap is 64 x 66 pixels; rowSkip is the surface's gap between two face rows + ushort* face = this->bitmapsFaces_0x94 + bitmapFaceIndex * 66 * 64; + // Before the switch, as in the original (in the RGB565 branch it loses the match) + int const faceWeight = 32 - colorOrBlendOrGammaUnk; + ushort* surface; + int rowSkip; + int lineByteWidth; + switch (this->drawBufferChoiceValue) { + case OpenSHC::Rendering::Enums::RT_MAP_GAME: + surface = DAT_WindowAndDirectDraw::instance.surfacePointer_mapGame; + rowSkip = 4056 - 64; + lineByteWidth = 4056 * 2; + break; + case OpenSHC::Rendering::Enums::RT_BUTTON_AND_ALPHA: + surface = AlphaAndButtonSurfaceObj::instance.surfacePtr; + rowSkip = AlphaAndButtonSurfaceObj::instance.currentImageWidth - 64; + lineByteWidth = AlphaAndButtonSurfaceObj::instance.currentImageWidth * 2; + break; + default: + surface = DAT_WindowAndDirectDraw::instance.surfacePointer_screenMenu; + rowSkip = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine / 2 - 64; + lineByteWidth = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine; + break; + } + DAT_TextureRenderCoreObject::instance.currentRenderSurface = surface; + ushort* pixel = (ushort*)((byte*)surface + lineByteWidth * yPos + xPos * 2); + if (DAT_WindowAndDirectDraw::instance.colorBitMode == OpenSHC::Rendering::RGB_565) { + // The face weighs 32 - strength and the surface under it the strength + for (int row = 0; row < 66; ++row) { + for (int column = 0; column < 64; ++column) { + ushort const color = *face; + ++face; + // ++pixel in both branches (a single ++pixel after the if loses the match) + if (color == (ushort)COL_MAGENTA::instance.shortValue) { + ++pixel; + continue; + } + ushort const surfaceColor = *pixel; + int const green = (((color & CCM_RGB565_GREEN) * faceWeight / 32) & CCM_RGB565_GREEN) + + (((surfaceColor & CCM_RGB565_GREEN) * colorOrBlendOrGammaUnk / 32) & CCM_RGB565_GREEN); + int const red = (((color & CCM_RGB565_RED) * faceWeight / 32) & CCM_RGB565_RED) + + (((surfaceColor & CCM_RGB565_RED) * colorOrBlendOrGammaUnk / 32) & CCM_RGB565_RED); + int const blue = (color & CCM_RGB_BLUE) * faceWeight / 32 + + (surfaceColor & CCM_RGB_BLUE) * colorOrBlendOrGammaUnk / 32; + *pixel = (ushort)(green | red | blue); + ++pixel; + } + pixel += rowSkip; + } + return; + } + // fixme: RGB555 weighs the face by the strength, not 32 - strength, and leaves out the surface under it + for (int row = 0; row < 66; ++row) { + for (int column = 0; column < 64; ++column) { + ushort const color = *face; + ++face; + if (color != (ushort)COL_MAGENTA::instance.shortValue) { + // One local per channel (one expression loses the match: the RGB565 loop then puts red first) + int const green = ((color & CCM_RGB555_GREEN) * colorOrBlendOrGammaUnk / 32) & CCM_RGB555_GREEN; + int const red = ((color & CCM_RGB555_RED) * colorOrBlendOrGammaUnk / 32) & CCM_RGB555_RED; + int const blue = (color & CCM_RGB_BLUE) * colorOrBlendOrGammaUnk / 32; + *pixel = (ushort)(green | red | blue); + } + ++pixel; + } + pixel += rowSkip; + } + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 3a2f5aa3d5..5984fed330 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -16937,7 +16937,7 @@ SHC_3BB0A8C1_0x0044CE00 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0044CE80 | 0.0% | Pending -SHC_3BB0A8C1_0x0044CEB0 | 0.0% | Pending +SHC_3BB0A8C1_0x0044CEB0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0044CF80 | 0.0% | Pending