diff --git a/cmake/compiler-flags-gl.txt b/cmake/compiler-flags-gl.txt index 7a4a662301..82b685cb99 100644 --- a/cmake/compiler-flags-gl.txt +++ b/cmake/compiler-flags-gl.txt @@ -111,6 +111,8 @@ src/OpenSHC/UI/Rendering/RenderButtonImageWithBlending.cpp src/OpenSHC/UI/Helpers/SetTribesPatrolButtonPressed.cpp src/OpenSHC/UI/MenuItems/BuildMenu/MenuItemActionHandler_BuildMenu_UnitActionButtons.cpp src/OpenSHC/UI/Rendering/RenderCurrentNotActiveButtonWithPossibleAlphaTexOnCurrentSurfaceUnk.cpp +src/OpenSHC/UI/Rendering/TextureRenderCore/transformTilesetToRGB565.cpp +src/OpenSHC/UI/Rendering/TextureRenderCore/transformTileObjectToRGB565.cpp src/OpenSHC/UI/MenuItems/BuildingAndStatusMenu/MenuItemRenderFunction_BuildingAndStatusMenu_DrawbridgeOpenClose.cpp src/OpenSHC/UI/MenuItems/BuildingAndStatusMenu/MenuItemRenderFunction_BuildingAndStatusMenu_GatehouseOpenClose.cpp src/OpenSHC/UI/MenuItems/BuildingAndStatusMenu/MenuItemRenderFunction_BuildingAndStatusMenu_WorkshopWeaponSelection.cpp diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/Constructor_TextureRenderCore.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/Constructor_TextureRenderCore.cpp new file mode 100644 index 0000000000..07920fbee7 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/Constructor_TextureRenderCore.cpp @@ -0,0 +1,74 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/OS.func.hpp" +#include "OpenSHC/Rendering/Enums/RenderTarget.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x00455050 + TextureRenderCore* TextureRenderCore::Constructor_TextureRenderCore( + int processedImageDataBufferSize, int gmAndGfxImageDataBufferSize, int unknownMemSize) + { + // the buffer sizes are kept here and allocated below; the second unknown buffer is half the first + this->gmProcessedImageDataBufferSize_0x74 = processedImageDataBufferSize; + this->gmAndGfxImageDataBufferSize_0x8c = gmAndGfxImageDataBufferSize; + if (unknownMemSize != 0) { + this->unknownMemSize_1_0x7c = unknownMemSize; + this->unknownMemSize_2_0x84 = unknownMemSize / 2; + } else { + this->unknownMemSize_1_0x7c = 0; + this->unknownMemSize_2_0x84 = 0; + } + this->bufferAllocStateUnk_0x0 = 0; + this->drawBufferChoiceValue = OpenSHC::Rendering::Enums::RT_MAP_GAME; + this->currentRenderSurfaceIdentifierUnk_0x8 = OpenSHC::Rendering::Enums::RT_SCREEN_MENU; + this->field_0x128 = 0; + this->field_0x12c = 0; + this->field_0x2c = 1; + this->isZoom2 = 0; + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::clearSomeMemory, this)(); + this->unknownSfxAndGmRelatedFlag = FALSE; + // bufferAllocStateUnk_0x0 records a failed allocation: 1 for an image buffer, 2 and 3 for the other two + this->gmProcessedImageData + = (byte*)MACRO_CALL(OpenSHC::OS_Func::_malloc)(this->gmProcessedImageDataBufferSize_0x74); + if (!this->gmProcessedImageData) { + this->bufferAllocStateUnk_0x0 = 1; + return this; + } + if (unknownMemSize != 0) { + this->unknownMemPtr_1_0x80 = MACRO_CALL(OpenSHC::OS_Func::_malloc)(this->unknownMemSize_1_0x7c); + if (!this->unknownMemPtr_1_0x80) { + this->bufferAllocStateUnk_0x0 = 2; + return this; + } + this->address2 = MACRO_CALL(OpenSHC::OS_Func::_malloc)(this->unknownMemSize_2_0x84); + if (!this->address2) { + this->bufferAllocStateUnk_0x0 = 3; + return this; + } + } else { + this->unknownMemPtr_1_0x80 = NULL; + this->address2 = NULL; + } + this->gmAndGfxImageDataBuffer + = (byte*)MACRO_CALL(OpenSHC::OS_Func::_malloc)(this->gmAndGfxImageDataBufferSize_0x8c); + if (!this->gmAndGfxImageDataBuffer) { + this->bufferAllocStateUnk_0x0 = 1; + return this; + } + this->totalLoadedGfx = 0; + this->colorPaletteOverride = 0; + // room for one 64 x 66 RGB565 face bitmap per entry of bitmapFaceSizes, and their sizes, none loaded yet + this->bitmapsFaces_0x94 = (ushort*)MACRO_CALL(OpenSHC::OS_Func::_malloc)( + countof(this->bitmapFaceSizes) * 66 * 64 * sizeof(ushort)); + for (int i = 0; i < countof(this->bitmapFaceSizes); ++i) { + this->bitmapFaceSizes[i] = 0; + } + return this; + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/RGB555To565Asm.hpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/RGB555To565Asm.hpp new file mode 100644 index 0000000000..7449abd5a9 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/RGB555To565Asm.hpp @@ -0,0 +1,91 @@ +/** + HAND WRITTEN - not generated. + + path: 'OpenSHC/UI/Rendering/TextureRenderCore/RGB555To565Asm.hpp' + + Converting RGB555 pixels to RGB565 in place is handwritten assembly in the original, and it is a + MACRO: the identical instruction stream appears, never as a call, as the whole loop in + transformRawWithMarkerUnkToRGB555To565 (0x0044C940), transformTileObjectToRGB565 (0x0044CAE0) and + transformRawToRGB555To565 (0x0044CB60), and as the pixel body twice in transformTgxFromRGB555ToRGB565 + (0x0044C9C0: at 0x0044CA08 with the marker test, at 0x0044CA6D without). It is recognisable by + + - "cmp edx, 0" where MSVC would emit "test edx, edx", + - the pixel read with "mov ax, word ptr [esi]" and every channel computed in 16-bit registers, + where MSVC would emit movzx and work in 32 bits, + - the loop closed with an unconditional "jmp" back to its test. + + Red goes into a dead parameter's stack slot, as in the original. + + The macros expect these names in the enclosing function: + + ushort _blue declared after the pixel pointer + ushort _green declared after _blue + RED the dead parameter that holds red: imageOffset in the loops, tgxDataPtr in + 0x0044C9C0 + + and the loops also + + ushort* _colorPtr the first pixel, gmProcessedImageData + imageOffset, declared first + int imageOffset the parameter + + MACRO_RGB555_TO_565_PIXEL converts the pixel at [esi], then steps edx back by 2 and esi on by 2. With + MACRO_ASM_SKIP_MARKER_TEST and MACRO_ASM_SKIP_MARKER_LABEL it leaves pixels equal to 0xf81f untouched; + that is magenta already encoded as RGB565, the transparency marker. COUNT is the buffer size in bytes. + All of them clobber eax and ebx, the loops also edx and esi. They define assembler labels, so a loop + can be expanded once per function, and the pixel macro more than once, but with the skip label once. +*/ + +#pragma once + +// clang-format off + +#define MACRO_ASM_NONE +#define MACRO_ASM_SKIP_MARKER_TEST \ + __asm cmp ax, 0xf81f \ + __asm je skip_color +#define MACRO_ASM_SKIP_MARKER_LABEL \ + __asm skip_color: + +#define MACRO_RGB555_TO_565_PIXEL(RED, SKIP_TEST, SKIP_LABEL) \ + __asm mov ax, word ptr [esi] \ + SKIP_TEST \ + __asm mov bx, ax \ + __asm and bx, 0x1f \ + __asm mov _blue, bx \ + __asm mov bx, ax \ + __asm and bx, 0x3e0 \ + __asm shr bx, 5 \ + __asm mov _green, bx \ + __asm mov bx, ax \ + __asm and bx, 0x7c00 \ + __asm shr bx, 10 \ + __asm mov word ptr RED, bx \ + __asm mov ax, _blue \ + __asm mov bx, _green \ + __asm shl bx, 6 \ + __asm add ax, bx \ + __asm mov bx, word ptr RED \ + __asm shl bx, 11 \ + __asm add ax, bx \ + __asm mov word ptr [esi], ax \ + SKIP_LABEL \ + __asm sub edx, 2 \ + __asm add esi, 2 + +#define MACRO_RGB555_TO_565_LOOP_IMPL(COUNT, SKIP_TEST, SKIP_LABEL) \ + __asm mov esi, _colorPtr \ + __asm mov edx, COUNT \ + __asm next_color: \ + __asm cmp edx, 0 \ + __asm jle done \ + MACRO_RGB555_TO_565_PIXEL(imageOffset, SKIP_TEST, SKIP_LABEL) \ + __asm jmp next_color \ + __asm done: + +#define MACRO_RGB555_TO_565_LOOP(COUNT) \ + MACRO_RGB555_TO_565_LOOP_IMPL(COUNT, MACRO_ASM_NONE, MACRO_ASM_NONE) + +#define MACRO_RGB555_TO_565_LOOP_SKIPPING_MARKER(COUNT) \ + MACRO_RGB555_TO_565_LOOP_IMPL(COUNT, MACRO_ASM_SKIP_MARKER_TEST, MACRO_ASM_SKIP_MARKER_LABEL) + +// clang-format on diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/checkIfGfxTgxStartsWithTransparentPixels.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/checkIfGfxTgxStartsWithTransparentPixels.cpp index 246f0c37b5..3010f5c990 100644 --- a/src/OpenSHC/UI/Rendering/TextureRenderCore/checkIfGfxTgxStartsWithTransparentPixels.cpp +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/checkIfGfxTgxStartsWithTransparentPixels.cpp @@ -1,7 +1,7 @@ -#include "OpenSHC/IO/Graphics/GfxRef.hpp" +#include "../TextureRenderCore.func.hpp" + #include "OpenSHC/IO/Graphics/TgxToken.hpp" -#include "OpenSHC/UI/Rendering/TextureRenderCore.hpp" #include "OpenSHC/Globals/DAT_TextureRenderCoreObject.hpp" @@ -13,7 +13,7 @@ namespace UI { BOOLEnum TextureRenderCore::checkIfGfxTgxStartsWithTransparentPixels(int gfxIndex) { int byteIndex = this->loadedGfxArray[gfxIndex].offsetInBuffer + 8; - byte checkByte = ((byte*)this->gmAndGfxImageDataBuffer)[byteIndex]; + byte checkByte = this->gmAndGfxImageDataBuffer[byteIndex]; if ((checkByte & OpenSHC::IO::Graphics::TT_TGX_PIXEL_HEADER) == OpenSHC::IO::Graphics::TT_TRANSPARENT_PIXELS) { return TRUE; diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/drawBitmapFace.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/drawBitmapFace.cpp new file mode 100644 index 0000000000..071ad53ca4 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/drawBitmapFace.cpp @@ -0,0 +1,59 @@ +#include "../TextureRenderCore.func.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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x0044CD40 + void TextureRenderCore::drawBitmapFace(int bitmapFaceIndex, int xPos, int yPos) + { + // A face bitmap is 64 x 66 pixels; rowSkip is the surface's gap between two face rows + int rowSkip; + int lineByteWidth; + ushort* face = this->bitmapsFaces_0x94 + bitmapFaceIndex * 66 * 64; + // Stored through the global in each arm (a surface local: quick_diff 0.9804 -> 0.7255; this->: 0.5524) + switch (this->drawBufferChoiceValue) { + case OpenSHC::Rendering::Enums::RT_MAP_GAME: + DAT_TextureRenderCoreObject::instance.currentRenderSurface + = DAT_WindowAndDirectDraw::instance.surfacePointer_mapGame; + rowSkip = 4056 - 64; + lineByteWidth = 4056 * 2; + break; + case OpenSHC::Rendering::Enums::RT_BUTTON_AND_ALPHA: + DAT_TextureRenderCoreObject::instance.currentRenderSurface + = AlphaAndButtonSurfaceObj::instance.surfacePtr; + rowSkip = AlphaAndButtonSurfaceObj::instance.currentImageWidth - 64; + lineByteWidth = AlphaAndButtonSurfaceObj::instance.currentImageWidth * 2; + break; + default: + DAT_TextureRenderCoreObject::instance.currentRenderSurface + = DAT_WindowAndDirectDraw::instance.surfacePointer_screenMenu; + rowSkip = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine / 2 - 64; + lineByteWidth = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine; + break; + } + ushort* pixel = (ushort*)((byte*)DAT_TextureRenderCoreObject::instance.currentRenderSurface + + lineByteWidth * yPos + xPos * 2); + 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) { + *pixel = color; + } + ++pixel; + } + pixel += rowSkip; + } + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/drawTgxGmOnFlaggedSurface.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/drawTgxGmOnFlaggedSurface.cpp new file mode 100644 index 0000000000..07266caf06 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/drawTgxGmOnFlaggedSurface.cpp @@ -0,0 +1,35 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/Graphics/GmImageType.hpp" + +#include "OpenSHC/Globals/DAT_GMImageHeaders.hpp" +#include "OpenSHC/Globals/DAT_GMImageOffsets.hpp" +#include "OpenSHC/Globals/GMFirstPictureIndex.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x004554A0 + void TextureRenderCore::drawTgxGmOnFlaggedSurface(GmID gmId, int imageIndexInGm, int xPos, int yPos) + { + // picture imageIndexInGm of the gm file, counted from 1 + int const imageIndex = GMFirstPictureIndex::instance[gmId] + imageIndexInGm - 1; + ushort* const source = (ushort*)(this->gmProcessedImageData + DAT_GMImageOffsets::instance[imageIndex]); + // interface elements and compressed images draw the same way + // two tests as in the original (one test with ||: quick_diff 1.0000 -> 0.3014) + if (this->gmFileHeaderColorpaletteArray[gmId].ImageType == OpenSHC::IO::Graphics::GIT_InterfaceElement) { + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::drawTgxOnFlaggedSurface, this)(xPos, + yPos, DAT_GMImageHeaders::instance.imh[imageIndex].width, + DAT_GMImageHeaders::instance.imh[imageIndex].height, source); + } + if (this->gmFileHeaderColorpaletteArray[gmId].ImageType == OpenSHC::IO::Graphics::GIT_CompressedImage) { + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::drawTgxOnFlaggedSurface, this)(xPos, + yPos, DAT_GMImageHeaders::instance.imh[imageIndex].width, + DAT_GMImageHeaders::instance.imh[imageIndex].height, source); + } + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/drawTgxOnFlaggedSurface.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/drawTgxOnFlaggedSurface.cpp new file mode 100644 index 0000000000..0bb3ffc9c9 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/drawTgxOnFlaggedSurface.cpp @@ -0,0 +1,247 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/Graphics/TgxToken.hpp" +#include "OpenSHC/Rendering/Enums/RenderTarget.hpp" + +#include "OpenSHC/Globals/DAT_TextureRenderCoreObject.hpp" +#include "OpenSHC/Globals/DAT_WindowAndDirectDraw.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + using OpenSHC::IO::Graphics::TT_REPEATING_PIXELS; + using OpenSHC::IO::Graphics::TT_STREAM_OF_PIXELS; + using OpenSHC::IO::Graphics::TT_TGX_PIXEL_HEADER; + using OpenSHC::IO::Graphics::TT_TGX_PIXEL_LENGTH; + using OpenSHC::IO::Graphics::TT_TRANSPARENT_PIXELS; + + // The drawing loop is handwritten assembly in the original, transcribed here from the binary. + + // FUNCTION: STRONGHOLDCRUSADER 0x00454A60 + void TextureRenderCore::drawTgxOnFlaggedSurface( + int xPos, int yPos, int gfxWidth, int gfxHeight, ushort* tgxSourcePtr) + { + // decodes a TGX picture onto the current render surface, clipped to renderingRect_16c854 + int const top = this->renderingRect_16c854.top; + int const left = this->renderingRect_16c854.left; + int const bottom = this->renderingRect_16c854.bottom; + int const right = this->renderingRect_16c854.right; + // declared before the switch because both cases assign them; lineJump is from one row's end to the next + int lineJump; + int lineBytes; + switch (this->currentRenderSurfaceIdentifierUnk_0x8) { + case OpenSHC::Rendering::Enums::RT_MAP_GAME: + DAT_TextureRenderCoreObject::instance.currentRenderSurface + = DAT_WindowAndDirectDraw::instance.surfacePointer_mapGame; + // the map surface is 4056 pixels wide + lineJump = (4056 - gfxWidth) * 2; + lineBytes = 4056 * 2; + break; + case OpenSHC::Rendering::Enums::RT_SCREEN_MENU: + DAT_TextureRenderCoreObject::instance.currentRenderSurface + = DAT_WindowAndDirectDraw::instance.surfacePointer_screenMenu; + lineJump = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine - gfxWidth * 2; + lineBytes = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine; + break; + } + if (gfxHeight + yPos > bottom) { + gfxHeight = bottom - yPos; + if (gfxHeight <= 0) { + return; + } + } + // clang-format off + __asm { + mov esi, tgxSourcePtr + mov edi, DAT_TextureRenderCoreObject::instance.currentRenderSurface + mov eax, xPos + add eax, eax + add edi, eax + mov eax, yPos + mov ebx, top + cmp eax, ebx + jge add_y_offset + sub ebx, eax + mov eax, ebx + cmp gfxHeight, eax + jle done + sub gfxHeight, eax + skip_token: + mov ecx, 0 + mov bl, byte ptr [esi] + mov cl, byte ptr [esi] + and bl, TT_TGX_PIXEL_HEADER + and cl, TT_TGX_PIXEL_LENGTH + add esi, 1 + cmp bl, TT_TRANSPARENT_PIXELS + je skip_token + cmp bl, TT_STREAM_OF_PIXELS + je skip_stream + cmp bl, TT_REPEATING_PIXELS + je skip_repeating + jmp skip_newline + skip_stream: + add ecx, 1 + add ecx, ecx + add esi, ecx + jmp skip_token + skip_repeating: + add esi, 2 + jmp skip_token + skip_newline: + sub eax, 1 + cmp eax, 0 + jg skip_token + mov eax, top + add_y_offset: + mov edx, lineBytes + mul edx + add edi, eax + mov edx, gfxHeight + mov ebx, xPos + draw_token: + mov ecx, 0 + mov al, byte ptr [esi] + mov cl, byte ptr [esi] + and al, TT_TGX_PIXEL_HEADER + and cl, TT_TGX_PIXEL_LENGTH + add esi, 1 + cmp al, TT_TRANSPARENT_PIXELS + je draw_transparent + cmp al, TT_STREAM_OF_PIXELS + je draw_stream + cmp al, TT_REPEATING_PIXELS + je draw_repeating + mov ebx, xPos + add edi, lineJump + sub edx, 1 + cmp edx, 0 + jg draw_token + jmp done + draw_transparent: + add ecx, 1 + add ebx, ecx + add ecx, ecx + add edi, ecx + jmp draw_token + draw_repeating: + mov eax, 0 + add ecx, 1 + cmp ebx, right + jl repeating_clip + repeating_advance: + add ebx, ecx + add ecx, ecx + add edi, ecx + add esi, 2 + jmp draw_token + repeating_clip: + cmp ebx, left + jge repeating_right + mov eax, ecx + add eax, ebx + cmp eax, left + jle repeating_advance + mov eax, left + sub eax, ebx + sub ecx, eax + add ebx, eax + add eax, eax + add edi, eax + repeating_right: + mov eax, ecx + add eax, ebx + cmp eax, right + jl repeating_copy + push edx + mov edx, eax + sub edx, right + sub ecx, edx + mov ax, word ptr [esi] + repeating_clip_loop: + mov word ptr [edi], ax + add edi, 2 + add ebx, 1 + sub ecx, 1 + jne repeating_clip_loop + add ebx, edx + add edx, edx + add edi, edx + pop edx + add esi, 2 + jmp draw_token + repeating_copy: + mov ax, word ptr [esi] + repeating_copy_loop: + mov word ptr [edi], ax + add edi, 2 + add ebx, 1 + sub ecx, 1 + jne repeating_copy_loop + add esi, 2 + jmp draw_token + draw_stream: + add ecx, 1 + cmp ebx, right + jl stream_clip + stream_advance: + add ebx, ecx + add ecx, ecx + add esi, ecx + add edi, ecx + jmp draw_token + stream_clip: + cmp ebx, left + jge stream_right + mov eax, ecx + add eax, ebx + cmp eax, left + jle stream_advance + mov eax, left + sub eax, ebx + sub ecx, eax + add ebx, eax + add eax, eax + add esi, eax + add edi, eax + stream_right: + mov eax, ecx + add eax, ebx + cmp eax, right + jl stream_copy + push edx + mov edx, eax + sub edx, right + sub ecx, edx + stream_clip_loop: + mov ax, word ptr [esi] + mov word ptr [edi], ax + add esi, 2 + add edi, 2 + add ebx, 1 + sub ecx, 1 + jne stream_clip_loop + add ebx, edx + add edx, edx + add esi, edx + add edi, edx + pop edx + jmp draw_token + stream_copy: + mov ax, word ptr [esi] + mov word ptr [edi], ax + add esi, 2 + add edi, 2 + add ebx, 1 + sub ecx, 1 + jne stream_copy + jmp draw_token + done: + } + // clang-format on + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/loadCampaignMapGfxUnk.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/loadCampaignMapGfxUnk.cpp new file mode 100644 index 0000000000..7228260217 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/loadCampaignMapGfxUnk.cpp @@ -0,0 +1,51 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/ResourceManager.func.hpp" +#include "OpenSHC/Rendering/ColorMode.hpp" +#include "OpenSHC/string-literals.hpp" + +#include "OpenSHC/Globals/DAT_BlendingDefinedData.hpp" +#include "OpenSHC/Globals/DAT_ResourceManager.hpp" +#include "OpenSHC/Globals/DAT_WindowAndDirectDraw.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // magenta as RGB555 with the top bit set, and as RGB565 + enum { MAGENTA_555 = 1 << 15 | 31 << 10 | 31, MAGENTA_565 = 31 << 11 | 31 }; + + // FUNCTION: STRONGHOLDCRUSADER 0x00454CB0 + void TextureRenderCore::loadCampaignMapGfxUnk() + { + // the campaign map's six palettes (.act files, 256 RGB triples each), as screen colours + for (int map = 0; map < countof(DAT_BlendingDefinedData::instance.campaign_map_england); ++map) { + MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::resolveResourceFileName, DAT_ResourceManager::ptr)( + OpenSHC::IO::FRT_GFX8, (char const*)DAT_BlendingDefinedData::instance.campaign_map_england[map]); + byte palette[256][3]; + // "act" + MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readFirstPartOfCurrentResourceIntoMemory, + DAT_ResourceManager::ptr)(palette, sizeof(palette), (char*)s_act_005a5064); + for (int i = 0; i < countof(palette); ++i) { + // green read first (red first: quick_diff 0.9797 -> 0.8716) + ushort const green = palette[i][1]; + ushort const red = palette[i][0]; + ushort const blue = palette[i][2] >> 3; + ushort colour; + if (DAT_WindowAndDirectDraw::instance.colorBitMode == OpenSHC::Rendering::RGB_565) { + colour = ((red & ~7) * 32 + (green & ~3)) * 8 + blue; + } else { + colour = (((red & ~7) * 32 + (green & ~7)) * 4 + blue) | 1 << 15; + } + // RGB555 magenta (top bit set) becomes the RGB565 marker; the test runs in both modes + if (colour == MAGENTA_555) { + colour = MAGENTA_565; + } + this->campaignMapColorMapsUnk_0x16c886[map][i] = colour; + } + } + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGFX8.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGFX8.cpp new file mode 100644 index 0000000000..c97e11c2e1 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGFX8.cpp @@ -0,0 +1,40 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/ResourceManager.func.hpp" + +#include "OpenSHC/Globals/DAT_ResourceManager.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x00454700 + int TextureRenderCore::loadGFX8(char* gfx8Filename) + { + MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::resolveResourceFileName, DAT_ResourceManager::ptr)( + OpenSHC::IO::FRT_GFX8, gfx8Filename); + int const size = MACRO_CALL_MEMBER( + OpenSHC::IO::ResourceManager_Func::getCurrentResourceSize, DAT_ResourceManager::ptr)(); + int const offset = this->loadedGfxArray[this->totalLoadedGfx].offsetInBuffer; + // Only if it still fits into the buffer's 18250000 bytes + if (offset + size > 18250000) { + return -1; + } + byte* const destination = this->gmAndGfxImageDataBuffer + offset; + if (!MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readCurrentResourceIntoDestination, + DAT_ResourceManager::ptr)(destination, size)) { + return -1; + } + this->loadedGfxArray[this->totalLoadedGfx + 1].offsetInBuffer + = this->loadedGfxArray[this->totalLoadedGfx].offsetInBuffer + + DAT_ResourceManager::instance.loadPositionInCurrentResource; + // The file starts with the image's width and height + this->loadedGfxArray[this->totalLoadedGfx].width = ((int*)destination)[0]; + this->loadedGfxArray[this->totalLoadedGfx].height = ((int*)destination)[1]; + ++this->totalLoadedGfx; + return this->totalLoadedGfx - 1; + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGfxAtBufferEnd.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGfxAtBufferEnd.cpp new file mode 100644 index 0000000000..2847f57d28 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGfxAtBufferEnd.cpp @@ -0,0 +1,48 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/ResourceManager.func.hpp" + +#include "OpenSHC/Globals/DAT_ResourceManager.hpp" +#include "OpenSHC/Globals/DAT_WindowAndDirectDraw.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x004547C0 + int TextureRenderCore::loadGfxAtBufferEnd(char* fileName) + { + // loads a gfx file (the menu edges, for example), packed backwards from the end of the buffer's 18250000 + // bytes + MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::resolveResourceFileName, DAT_ResourceManager::ptr)( + OpenSHC::IO::FRT_GFX, fileName); + int const size = MACRO_CALL_MEMBER( + OpenSHC::IO::ResourceManager_Func::getCurrentResourceSize, DAT_ResourceManager::ptr)(); + byte* const destination = this->gmAndGfxImageDataBuffer + - this->loadedGfxArray[this->backwardsLoadedGfxIndex_0x16C850].backwardsOffsetInBuffer - size + + 18250000; + if (!MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readCurrentResourceIntoDestination, + DAT_ResourceManager::ptr)(destination, size)) { + return -1; + } + // the picture's 8-byte header holds its width and height + if (DAT_WindowAndDirectDraw::instance.colorBitMode == OpenSHC::Rendering::RGB_565) { + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::transformTgxFromRGB555ToRGB565, this)( + (ushort*)(destination + 8), DAT_ResourceManager::instance.loadPositionInCurrentResource - 8); + } + this->loadedGfxArray[this->backwardsLoadedGfxIndex_0x16C850].offsetInBuffer = 18250000 + - this->loadedGfxArray[this->backwardsLoadedGfxIndex_0x16C850].backwardsOffsetInBuffer - size; + this->loadedGfxArray[this->backwardsLoadedGfxIndex_0x16C850 - 1].backwardsOffsetInBuffer + = this->loadedGfxArray[this->backwardsLoadedGfxIndex_0x16C850].backwardsOffsetInBuffer + + DAT_ResourceManager::instance.loadPositionInCurrentResource; + this->loadedGfxArray[this->backwardsLoadedGfxIndex_0x16C850].backwardsOffsetInBuffer + = this->loadedGfxArray[this->backwardsLoadedGfxIndex_0x16C850 - 1].backwardsOffsetInBuffer; + this->loadedGfxArray[this->backwardsLoadedGfxIndex_0x16C850].width = ((int*)destination)[0]; + this->loadedGfxArray[this->backwardsLoadedGfxIndex_0x16C850].height = ((int*)destination)[1]; + --this->backwardsLoadedGfxIndex_0x16C850; + return this->backwardsLoadedGfxIndex_0x16C850 + 1; + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGfxFile.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGfxFile.cpp new file mode 100644 index 0000000000..bc9a067b15 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGfxFile.cpp @@ -0,0 +1,45 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/ResourceManager.func.hpp" + +#include "OpenSHC/Globals/DAT_ResourceManager.hpp" +#include "OpenSHC/Globals/DAT_WindowAndDirectDraw.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x00454620 + int TextureRenderCore::loadGfxFile(char const* tgxFileName) + { + // loads a gfx file after the ones loaded so far, if it still fits into the buffer's 18250000 bytes + MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::resolveResourceFileName, DAT_ResourceManager::ptr)( + OpenSHC::IO::FRT_GFX, tgxFileName); + int const size = MACRO_CALL_MEMBER( + OpenSHC::IO::ResourceManager_Func::getCurrentResourceSize, DAT_ResourceManager::ptr)(); + int const offset = this->loadedGfxArray[this->totalLoadedGfx].offsetInBuffer; + if (offset + size > 18250000) { + return -1; + } + byte* const destination = this->gmAndGfxImageDataBuffer + offset; + if (!MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readCurrentResourceIntoDestination, + DAT_ResourceManager::ptr)(destination, size)) { + return -1; + } + // the picture's 8-byte header holds its width and height + if (DAT_WindowAndDirectDraw::instance.colorBitMode == OpenSHC::Rendering::RGB_565) { + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::transformTgxFromRGB555ToRGB565, this)( + (ushort*)(destination + 8), DAT_ResourceManager::instance.loadPositionInCurrentResource - 8); + } + this->loadedGfxArray[this->totalLoadedGfx + 1].offsetInBuffer + = this->loadedGfxArray[this->totalLoadedGfx].offsetInBuffer + + DAT_ResourceManager::instance.loadPositionInCurrentResource; + this->loadedGfxArray[this->totalLoadedGfx].width = ((int*)destination)[0]; + this->loadedGfxArray[this->totalLoadedGfx].height = ((int*)destination)[1]; + ++this->totalLoadedGfx; + return this->totalLoadedGfx - 1; + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGmFiles.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGmFiles.cpp new file mode 100644 index 0000000000..43075de955 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGmFiles.cpp @@ -0,0 +1,62 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/Graphics/GmID.hpp" +#include "OpenSHC/string-literals.hpp" + +#include "OpenSHC/Globals/DAT_LoadingBarProgress.hpp" +#include "OpenSHC/Globals/DAT_TextManagerObject.hpp" +#include "OpenSHC/Globals/GMFirstPictureIndex.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x00455C60 + void TextureRenderCore::loadGmFiles(char* fileNameArray) + { + // Up to 240 names of 1000 bytes; the loading bar gets one step per file plus one + // A found flag, not n + 1 after a plain loop (n + 1: quick_diff 0.9538 -> 0.9385) + int loadingBarSteps = 0; + BOOLEnum found = FALSE; + for (char* fileName = fileNameArray; loadingBarSteps < 240; fileName += 1000) { + ++loadingBarSteps; + // "null" ends the list + if (!strcmp(fileName, s_null_005a506c)) { + found = TRUE; + break; + } + } + if (!found) { + ++loadingBarSteps; + } + DAT_LoadingBarProgress::instance = loadingBarSteps; + this->gmNumberOfProcessedPictures = 1; + this->gmFileID = 1; + this->gmSizeOfProcessedPictures_0x44 = 0; + // The count steps first (++i in the for header: quick_diff 0.9538 -> 0.9385); the names are walked, as + // the original steps a register and then the parameter's slot (indexing: quick_diff 0.9538 -> 0.7313) + for (int i = 0; i < 240;) { + ++i; + // "null" ends the list + if (!strcmp(fileNameArray, s_null_005a506c)) { + break; + } + GMFirstPictureIndex::instance[this->gmFileID] = this->gmNumberOfProcessedPictures; + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::loadGMFile, this)( + (GmID)this->gmFileID, fileNameArray); + fileNameArray += 1000; + this->gmNumberOfProcessedPictures + += this->gmFileHeaderColorpaletteArray[this->gmFileID].numberOfPicturesInFile; + ++this->gmFileID; + } + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::loadCampaignMapGfxUnk, this)(); + // The font's .gm1 holds five font sets of the same size + DAT_TextManagerObject::instance.sizeOfOneFontSet_0x3c + = this->gmFileHeaderColorpaletteArray[OpenSHC::IO::Graphics::GID_FONT_STRONGHOLD_AA] + .numberOfPicturesInFile + / 5; + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/renderBlendedBlackBox.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderBlendedBlackBox.cpp new file mode 100644 index 0000000000..445a56112c --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderBlendedBlackBox.cpp @@ -0,0 +1,127 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/UI/Rendering.func.hpp" +#include "OpenSHC/Rendering/ColorMode.hpp" + +#include "OpenSHC/Globals/DAT_BlendFilterArrays.hpp" +#include "OpenSHC/Globals/DAT_PencilRenderCore.hpp" +#include "OpenSHC/Globals/DAT_WindowAndDirectDraw.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // The pixel loops are handwritten assembly in the original, transcribed here from the binary. + + // FUNCTION: STRONGHOLDCRUSADER 0x0044F020 + void TextureRenderCore::renderBlendedBlackBox( + int drawX, int drawY, int drawXEnd, int drawYEnd, int blendStrengthUnk) + { + // darkens the box, both ends included, on the pencil surface; black pixels stay black + // bytes from the end of one row of the box to the start of the next + int const lineJump = DAT_PencilRenderCore::instance.horizontalByteSize - 2 + (drawX - drawXEnd) * 2; + // strengths above 32 count down again: 33 is 31, 64 and above are 0 + if (blendStrengthUnk > 64) { + blendStrengthUnk = 64; + } + if (blendStrengthUnk > 32) { + blendStrengthUnk = 64 - blendStrengthUnk; + } + // built on first use: the full-strength table's last entry is 0 until then + if (DAT_BlendFilterArrays::instance[32][31][0] == 0) { + MACRO_CALL(OpenSHC::UI::Rendering_Func::InitBlendFilterArraysUnk)(); + } + // the strength's table row; MSVC gives it the parameter's stack slot, as in the original + short* const blendTable = DAT_BlendFilterArrays::instance[blendStrengthUnk][0]; + if (DAT_WindowAndDirectDraw::instance.colorBitMode == OpenSHC::Rendering::RGB_555) { + // clang-format off + __asm { + mov edi, DAT_PencilRenderCore::instance.surfacePtr + mov eax, drawX + add eax, eax + add edi, eax + mov eax, drawY + mov edx, DAT_PencilRenderCore::instance.horizontalByteSize + mul edx + add edi, eax + row_555: + mov ecx, drawX + mov esi, blendTable + pixel_555: + cmp ecx, drawXEnd + jg row_end_555 + mov dx, word ptr [edi] + cmp dx, 0 + je next_555 + movzx ebx, dx + and bx, 0x1f + mov ax, word ptr [esi + ebx*8] + mov bx, dx + shr bx, 5 + and bx, 0x1f + or ax, word ptr [esi + ebx*8 + 2] + shr dx, 10 + and edx, 0x1f + or ax, word ptr [esi + edx*8 + 4] + mov word ptr [edi], ax + next_555: + add edi, 2 + add ecx, 1 + jmp pixel_555 + row_end_555: + add drawY, 1 + add edi, lineJump + mov eax, drawY + cmp eax, drawYEnd + jle row_555 + } + // clang-format on + } else { + // clang-format off + __asm { + mov edi, DAT_PencilRenderCore::instance.surfacePtr + mov eax, drawX + add eax, eax + add edi, eax + mov eax, drawY + mov edx, DAT_PencilRenderCore::instance.horizontalByteSize + mul edx + add edi, eax + row_565: + mov ecx, drawX + mov esi, blendTable + pixel_565: + cmp ecx, drawXEnd + jg row_end_565 + mov dx, word ptr [edi] + cmp dx, 0 + je next_565 + movzx ebx, dx + and bx, 0x1f + mov ax, word ptr [esi + ebx*8] + mov bx, dx + shr bx, 5 + and bx, 0x3f + or ax, word ptr [esi + ebx*8 + 2] + shr dx, 11 + and edx, 0x1f + or ax, word ptr [esi + edx*8 + 4] + mov word ptr [edi], ax + next_565: + add edi, 2 + add ecx, 1 + jmp pixel_565 + row_end_565: + add drawY, 1 + add edi, lineJump + mov eax, drawY + cmp eax, drawYEnd + jle row_565 + } + // clang-format on + } + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/renderFacesSmallUnk.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderFacesSmallUnk.cpp new file mode 100644 index 0000000000..65c9d59008 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderFacesSmallUnk.cpp @@ -0,0 +1,55 @@ +#include "../TextureRenderCore.func.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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x0044CE00 + void TextureRenderCore::renderFacesSmallUnk(int facesIndex, int drawX, int drawY) + { + // A face bitmap is 64 x 66 pixels; the small face draws every other pixel of every other row + ushort* face = this->bitmapsFaces_0x94 + facesIndex * 66 * 64; + ushort* surface; + int lineByteWidth; + switch (this->drawBufferChoiceValue) { + case OpenSHC::Rendering::Enums::RT_MAP_GAME: + surface = DAT_WindowAndDirectDraw::instance.surfacePointer_mapGame; + lineByteWidth = 4056 * 2; + break; + case OpenSHC::Rendering::Enums::RT_BUTTON_AND_ALPHA: + surface = AlphaAndButtonSurfaceObj::instance.surfacePtr; + lineByteWidth = AlphaAndButtonSurfaceObj::instance.currentImageWidth * 2; + break; + default: + surface = DAT_WindowAndDirectDraw::instance.surfacePointer_screenMenu; + lineByteWidth = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine; + break; + } + // Stored through the global (this->: quick_diff 0.9903 -> 0.8155) + DAT_TextureRenderCoreObject::instance.currentRenderSurface = surface; + ushort* pixel = (ushort*)((byte*)surface + lineByteWidth * drawY + drawX * 2); + for (int row = 0; row < 33; ++row) { + ushort* const rowStart = pixel; + // != keeps the column loop rolled (column < 32: quick_diff 0.9903 -> 0.8348) + for (int column = 0; column != 32; ++column) { + if (*face != (ushort)COL_MAGENTA::instance.shortValue) { + *pixel = *face; + } + ++pixel; + face += 2; + } + face += 64; + pixel = rowStart + lineByteWidth / 2; + } + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/renderGM.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderGM.cpp new file mode 100644 index 0000000000..e00352149d --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderGM.cpp @@ -0,0 +1,40 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/Graphics/GmImageType.hpp" + +#include "OpenSHC/Globals/DAT_GMImageHeaders.hpp" +#include "OpenSHC/Globals/DAT_GMImageOffsets.hpp" +#include "OpenSHC/Globals/GMFirstPictureIndex.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x00455300 + void TextureRenderCore::renderGM(eGM gmID, int imageID, int drawX, int drawY) + { + // imageID counts from 1 within the gm file + int const imageIndex = GMFirstPictureIndex::instance[gmID] + imageID - 1; + ushort* const source = (ushort*)(this->gmProcessedImageData + DAT_GMImageOffsets::instance[imageIndex]); + OpenSHC::IO::Graphics::GmImageTypeInt const imageType = this->gmFileHeaderColorpaletteArray[gmID].ImageType; + // One call per arm, as the original (one test with ||: quick_diff 1.0000 -> 0.6585) + if (imageType == OpenSHC::IO::Graphics::GIT_InterfaceElement) { + MACRO_CALL_MEMBER( + OpenSHC::UI::Rendering::TextureRenderCore_Func::renderFunctionResponsibleForManyGameObjects, this)( + drawX, drawY, DAT_GMImageHeaders::instance.imh[imageIndex].width, + DAT_GMImageHeaders::instance.imh[imageIndex].height, source); + } else if (imageType == OpenSHC::IO::Graphics::GIT_CompressedImage) { + MACRO_CALL_MEMBER( + OpenSHC::UI::Rendering::TextureRenderCore_Func::renderFunctionResponsibleForManyGameObjects, this)( + drawX, drawY, DAT_GMImageHeaders::instance.imh[imageIndex].width, + DAT_GMImageHeaders::instance.imh[imageIndex].height, source); + } else if (imageType == OpenSHC::IO::Graphics::GIT_Animation) { + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::renderUnitAnimationUnk, this)(drawX, + drawY, DAT_GMImageHeaders::instance.imh[imageIndex].width, + DAT_GMImageHeaders::instance.imh[imageIndex].height, (byte*)source); + } + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/renderGMWithAlphaMask.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderGMWithAlphaMask.cpp new file mode 100644 index 0000000000..39f27c4351 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderGMWithAlphaMask.cpp @@ -0,0 +1,57 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Rendering/Enums/RenderTarget.hpp" + +#include "OpenSHC/Globals/AlphaAndButtonSurfaceObj.hpp" +#include "OpenSHC/Globals/COL_MAGENTA.hpp" +#include "OpenSHC/Globals/DAT_GMImageHeaders.hpp" +#include "OpenSHC/Globals/DAT_GMImageOffsets.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_TextureRenderCoreObject.hpp" +#include "OpenSHC/Globals/GMFirstPictureIndex.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x004557B0 + void TextureRenderCore::renderGMWithAlphaMask( + GmID gmID, int imageID, int xPos, int yPos, GmID maskGmID, int alphaImageID, int blendStrength) + { + byte* const imageData = this->gmProcessedImageData; + // Read before the zoom test (read at its use: quick_diff 1.0000 -> 0.9091) + ushort* const alphaSurface = AlphaAndButtonSurfaceObj::instance.surfacePtr; + // With isZoom2 set the image is drawn blended, without the mask + if (DAT_TextureRenderCoreObject::instance.isZoom2) { + int const picture = GMFirstPictureIndex::instance[gmID] + imageID - 1; + MACRO_CALL_MEMBER( + OpenSHC::UI::Rendering::TextureRenderCore_Func::renderInterfaceOrBuildingOccupationArea, this)(xPos, + yPos, DAT_GMImageHeaders::instance.imh[picture].width, + DAT_GMImageHeaders::instance.imh[picture].height, + (ushort*)(imageData + DAT_GMImageOffsets::instance[picture]), blendStrength); + return; + } + // Clear the alpha surface to magenta, draw the mask into it, then draw the image through it + int const maskPicture = GMFirstPictureIndex::instance[maskGmID] + alphaImageID - 1; + AlphaAndButtonSurfaceObj::instance.currentImageWidth = DAT_GMImageHeaders::instance.imh[maskPicture].width; + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ShortValue, DAT_LowLevelMemory::ptr)( + DAT_GMImageHeaders::instance.imh[maskPicture].height + * AlphaAndButtonSurfaceObj::instance.currentImageWidth * 2, + COL_MAGENTA::instance.shortValue, alphaSurface); + RenderTargetInt const savedTarget = DAT_TextureRenderCoreObject::instance.drawBufferChoiceValue; + DAT_TextureRenderCoreObject::instance.drawBufferChoiceValue + = OpenSHC::Rendering::Enums::RT_BUTTON_AND_ALPHA; + // The original draws the mask through the global instance (with this: quick_diff 1.0000 -> 0.7841) + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::renderGM, + DAT_TextureRenderCoreObject::ptr)((eGM)maskGmID, alphaImageID, 0, 0); + int const picture = GMFirstPictureIndex::instance[gmID] + imageID - 1; + DAT_TextureRenderCoreObject::instance.drawBufferChoiceValue = savedTarget; + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::renderGmWithPreparedAlphaMask, this)(xPos, + yPos, DAT_GMImageHeaders::instance.imh[picture].width, DAT_GMImageHeaders::instance.imh[picture].height, + (ushort*)(imageData + DAT_GMImageOffsets::instance[picture]), blendStrength); + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/renderGMWithBlending.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderGMWithBlending.cpp new file mode 100644 index 0000000000..b12de8049d --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderGMWithBlending.cpp @@ -0,0 +1,64 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/Graphics/GmImageType.hpp" + +#include "OpenSHC/Globals/DAT_GMImageHeaders.hpp" +#include "OpenSHC/Globals/DAT_GMImageOffsets.hpp" +#include "OpenSHC/Globals/GMFirstPictureIndex.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x00455390 + void TextureRenderCore::renderGMWithBlending(GmID GmID, int imageID, int drawX, int drawY, int blendStrengthUnk) + { + // picture imageID of the gm file, counted from 1; a blend strength of 32 draws nothing, 0 draws unblended + int const imageIndex = GMFirstPictureIndex::instance[GmID] + imageID - 1; + ushort* const source = (ushort*)(this->gmProcessedImageData + DAT_GMImageOffsets::instance[imageIndex]); + if (blendStrengthUnk == 32) { + return; + } + // one branch per image type, the type read in each test (|| and a local: quick_diff 1.0000 -> 0.8000) + if (blendStrengthUnk == 0) { + if (this->gmFileHeaderColorpaletteArray[GmID].ImageType + == OpenSHC::IO::Graphics::GIT_InterfaceElement) { + MACRO_CALL_MEMBER( + OpenSHC::UI::Rendering::TextureRenderCore_Func::renderFunctionResponsibleForManyGameObjects, + this)(drawX, drawY, DAT_GMImageHeaders::instance.imh[imageIndex].width, + DAT_GMImageHeaders::instance.imh[imageIndex].height, source); + } else if (this->gmFileHeaderColorpaletteArray[GmID].ImageType + == OpenSHC::IO::Graphics::GIT_CompressedImage) { + MACRO_CALL_MEMBER( + OpenSHC::UI::Rendering::TextureRenderCore_Func::renderFunctionResponsibleForManyGameObjects, + this)(drawX, drawY, DAT_GMImageHeaders::instance.imh[imageIndex].width, + DAT_GMImageHeaders::instance.imh[imageIndex].height, source); + } else if (this->gmFileHeaderColorpaletteArray[GmID].ImageType + == OpenSHC::IO::Graphics::GIT_Animation) { + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::renderUnitAnimationUnk, this)( + drawX, drawY, DAT_GMImageHeaders::instance.imh[imageIndex].width, + DAT_GMImageHeaders::instance.imh[imageIndex].height, (byte*)source); + } + return; + } + if (this->gmFileHeaderColorpaletteArray[GmID].ImageType == OpenSHC::IO::Graphics::GIT_InterfaceElement) { + MACRO_CALL_MEMBER( + OpenSHC::UI::Rendering::TextureRenderCore_Func::renderInterfaceOrBuildingOccupationArea, this)( + drawX, drawY, DAT_GMImageHeaders::instance.imh[imageIndex].width, + DAT_GMImageHeaders::instance.imh[imageIndex].height, source, blendStrengthUnk); + } else if (this->gmFileHeaderColorpaletteArray[GmID].ImageType + == OpenSHC::IO::Graphics::GIT_CompressedImage) { + MACRO_CALL_MEMBER( + OpenSHC::UI::Rendering::TextureRenderCore_Func::renderInterfaceOrBuildingOccupationArea, this)( + drawX, drawY, DAT_GMImageHeaders::instance.imh[imageIndex].width, + DAT_GMImageHeaders::instance.imh[imageIndex].height, source, blendStrengthUnk); + } else if (this->gmFileHeaderColorpaletteArray[GmID].ImageType == OpenSHC::IO::Graphics::GIT_Animation) { + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::renderUnitAnimationWithBlendingUnk, + this)(drawX, drawY, DAT_GMImageHeaders::instance.imh[imageIndex].width, + DAT_GMImageHeaders::instance.imh[imageIndex].height, (byte*)source, blendStrengthUnk); + } + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/renderGfxTgxWithBlending.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderGfxTgxWithBlending.cpp new file mode 100644 index 0000000000..f44c07273e --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderGfxTgxWithBlending.cpp @@ -0,0 +1,28 @@ +#include "../TextureRenderCore.func.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x004549F0 + void TextureRenderCore::renderGfxTgxWithBlending(int gfxIndex, int x, int y, int blendStrengthUnk) + { + // Saved before the image address is worked out (address first: quick_diff 1.0000 -> 0.9211) + RenderTargetInt const savedTarget = this->drawBufferChoiceValue; + // The pixels follow the 8-byte header (width and height) + ushort* const source + = (ushort*)(this->gmAndGfxImageDataBuffer + this->loadedGfxArray[gfxIndex].offsetInBuffer + 8); + // Only blend strengths 0-31 are drawn (the target restored on both paths: quick_diff 1.0000 -> 0.8947) + if (blendStrengthUnk < 0 || blendStrengthUnk > 31) { + return; + } + this->drawBufferChoiceValue = this->currentRenderSurfaceIdentifierUnk_0x8; + MACRO_CALL_MEMBER( + OpenSHC::UI::Rendering::TextureRenderCore_Func::renderInterfaceOrBuildingOccupationArea, this)(x, y, + this->loadedGfxArray[gfxIndex].width, this->loadedGfxArray[gfxIndex].height, source, blendStrengthUnk); + this->drawBufferChoiceValue = savedTarget; + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/renderLoadedGfx.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderLoadedGfx.cpp new file mode 100644 index 0000000000..f92952f6e8 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderLoadedGfx.cpp @@ -0,0 +1,25 @@ +#include "../TextureRenderCore.func.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x00454900 + void TextureRenderCore::renderLoadedGfx(int loadedGfxIndex, int xPos, int yPos) + { + // draws a gfx file loaded by loadGfxFile or loadGfxAtBufferEnd, on the surface + // currentRenderSurfaceIdentifierUnk_0x8 names for this one call + RenderTargetInt const savedTarget = this->drawBufferChoiceValue; + // pixels after the 8-byte header, named before the target switch (inline: quick_diff 1.0000 -> 0.7500) + ushort* const source + = (ushort*)(this->gmAndGfxImageDataBuffer + this->loadedGfxArray[loadedGfxIndex].offsetInBuffer + 8); + this->drawBufferChoiceValue = this->currentRenderSurfaceIdentifierUnk_0x8; + MACRO_CALL_MEMBER( + OpenSHC::UI::Rendering::TextureRenderCore_Func::renderFunctionResponsibleForManyGameObjects, this)(xPos, + yPos, this->loadedGfxArray[loadedGfxIndex].width, this->loadedGfxArray[loadedGfxIndex].height, source); + this->drawBufferChoiceValue = savedTarget; + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/renderTgxWithColorAndBlendingUnk.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderTgxWithColorAndBlendingUnk.cpp new file mode 100644 index 0000000000..45ca237dc9 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderTgxWithColorAndBlendingUnk.cpp @@ -0,0 +1,327 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/Graphics/TgxToken.hpp" +#include "OpenSHC/Rendering/ColorMode.hpp" +#include "OpenSHC/Rendering/Enums/RenderTarget.hpp" + +#include "OpenSHC/Globals/DAT_TextureRenderCoreObject.hpp" +#include "OpenSHC/Globals/DAT_WindowAndDirectDraw.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + using OpenSHC::IO::Graphics::TT_REPEATING_PIXELS; + using OpenSHC::IO::Graphics::TT_STREAM_OF_PIXELS; + using OpenSHC::IO::Graphics::TT_TGX_PIXEL_HEADER; + using OpenSHC::IO::Graphics::TT_TGX_PIXEL_LENGTH; + using OpenSHC::IO::Graphics::TT_TRANSPARENT_PIXELS; + + // The drawing loop is handwritten assembly in the original, transcribed here from the binary. + + // FUNCTION: STRONGHOLDCRUSADER 0x0044F850 + void TextureRenderCore::renderTgxWithColorAndBlendingUnk( + int xPos, int yPos, int width, int height, ushort* imageSource, ushort fillColorUnk, int blendStrengthUnk) + { + // blends fillColorUnk into the surface under a TGX picture, by each pixel's green channel times + // (32 - blendStrengthUnk) / 32; clipped to the surface's height range + // declared before the ifs because both arms assign them + int greenShift; + int redShift; + int lineJump; + int lineBytes; + int heightStart; + int heightEnd; + // inverted in the parameter's own slot (a fresh local: reccmp 100.0% -> 93.6%) + blendStrengthUnk = 32 - blendStrengthUnk; + // map arm first (== with the arms swapped: quick_diff 0.8678 -> 0.8319) + if (this->drawBufferChoiceValue != OpenSHC::Rendering::Enums::RT_SCREEN_MENU) { + DAT_TextureRenderCoreObject::instance.currentRenderSurface + = DAT_WindowAndDirectDraw::instance.surfacePointer_mapGame; + // the map surface is 4056 pixels wide + lineJump = (4056 - width) * 2; + lineBytes = 4056 * 2; + heightStart = this->mapGameSurfaceHeightRange.start; + heightEnd = this->mapGameSurfaceHeightRange.end; + } else { + DAT_TextureRenderCoreObject::instance.currentRenderSurface + = DAT_WindowAndDirectDraw::instance.surfacePointer_screenMenu; + lineJump = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine - width * 2; + lineBytes = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine; + heightStart = this->screenMenuSurfaceHeightRange.start; + heightEnd = this->screenMenuSurfaceHeightRange.end; + } + if (height + yPos > heightEnd) { + height = heightEnd - yPos; + if (height <= 0) { + return; + } + } + if (DAT_WindowAndDirectDraw::instance.colorBitMode == OpenSHC::Rendering::RGB_555) { + greenShift = 5; + redShift = 10; + } else { + greenShift = 6; + redShift = 11; + } + // clang-format off + __asm { + mov esi, imageSource + mov edi, DAT_TextureRenderCoreObject::instance.currentRenderSurface + mov eax, xPos + cmp eax, 0 + jl done + add eax, eax + add edi, eax + mov eax, yPos + mov ebx, heightStart + cmp eax, ebx + jge add_y_offset + sub ebx, eax + mov eax, ebx + cmp height, eax + jle done + sub height, eax + skip_token: + mov ecx, 0 + mov bl, byte ptr [esi] + mov cl, byte ptr [esi] + and bl, TT_TGX_PIXEL_HEADER + and cl, TT_TGX_PIXEL_LENGTH + add esi, 1 + cmp bl, TT_TRANSPARENT_PIXELS + je skip_token + cmp bl, TT_STREAM_OF_PIXELS + je skip_stream + cmp bl, TT_REPEATING_PIXELS + je skip_repeating + jmp skip_newline + skip_stream: + add ecx, 1 + add ecx, ecx + add esi, ecx + jmp skip_token + skip_repeating: + add esi, 2 + jmp skip_token + skip_newline: + sub eax, 1 + cmp eax, 0 + jg skip_token + mov eax, heightStart + add_y_offset: + mov edx, lineBytes + mul edx + add edi, eax + mov edx, height + draw_token: + push edx + mov ecx, 0 + mov al, byte ptr [esi] + mov cl, byte ptr [esi] + and al, TT_TGX_PIXEL_HEADER + and cl, TT_TGX_PIXEL_LENGTH + add esi, 1 + cmp al, TT_TRANSPARENT_PIXELS + je draw_transparent + cmp al, TT_STREAM_OF_PIXELS + je draw_stream + cmp al, TT_REPEATING_PIXELS + je draw_repeating + pop edx + add edi, lineJump + sub edx, 1 + cmp edx, 0 + jg draw_token + jmp done + draw_transparent: + add ecx, 1 + add ecx, ecx + add edi, ecx + pop edx + jmp draw_token + draw_repeating: + mov edx, 0 + mov dx, word ptr [esi] + add esi, 2 + add ecx, 1 + mov bx, word ptr fillColorUnk + push cx + mov cx, word ptr greenShift + shr dx, cl + and dx, 0x1f + pop cx + mov eax, blendStrengthUnk + mul dx + shr ax, 5 + mov dx, ax + cmp dx, 0x1f + je repeating_opaque + repeating_blend: + push bx + push cx + mov ax, bx + push bx + and ax, 0x1f + mov cx, word ptr [edi] + and cx, 0x1f + sub ax, cx + imul ax, dx + shr ax, 5 + mov bx, word ptr [edi] + and bx, 0x1f + add ax, bx + and ax, 0x1f + pop bx + push ax + mov ax, bx + push bx + mov cx, word ptr greenShift + shr ax, cl + and ax, 0x1f + mov bx, word ptr [edi] + shr bx, cl + and bx, 0x1f + sub ax, bx + imul ax, dx + shr ax, 5 + mov bx, word ptr [edi] + shr bx, cl + and bx, 0x1f + add ax, bx + and ax, 0x1f + shl ax, cl + pop bx + push ax + mov ax, bx + mov cx, word ptr redShift + shr ax, cl + and ax, 0x1f + mov bx, word ptr [edi] + shr bx, cl + and bx, 0x1f + sub ax, bx + imul ax, dx + shr ax, 5 + mov bx, word ptr [edi] + shr bx, cl + and bx, 0x1f + add ax, bx + and ax, 0x1f + shl ax, cl + mov bx, ax + pop ax + add bx, ax + pop ax + add bx, ax + pop cx + mov word ptr [edi], bx + pop bx + add edi, 2 + sub ecx, 1 + jne repeating_blend + pop edx + jmp draw_token + repeating_opaque: + mov word ptr [edi], bx + add edi, 2 + sub ecx, 1 + jne repeating_opaque + pop edx + jmp draw_token + draw_stream: + add ecx, 1 + stream_pixel: + mov dx, word ptr [esi] + push cx + mov cx, word ptr greenShift + shr dx, cl + and dx, 0x1f + pop cx + mov eax, blendStrengthUnk + mul dx + shr ax, 5 + mov dx, ax + mov ax, word ptr fillColorUnk + cmp dx, 0x1f + je stream_opaque + mov bx, ax + push cx + mov ax, bx + push bx + and ax, 0x1f + mov cx, word ptr [edi] + and cx, 0x1f + sub ax, cx + imul ax, dx + shr ax, 5 + mov bx, word ptr [edi] + and bx, 0x1f + add ax, bx + and ax, 0x1f + pop bx + push ax + mov ax, bx + push bx + mov cx, word ptr greenShift + shr ax, cl + and ax, 0x1f + mov bx, word ptr [edi] + shr bx, cl + and bx, 0x1f + sub ax, bx + imul ax, dx + shr ax, 5 + mov bx, word ptr [edi] + shr bx, cl + and bx, 0x1f + add ax, bx + and ax, 0x1f + shl ax, cl + pop bx + push ax + mov ax, bx + mov cx, word ptr redShift + shr ax, cl + and ax, 0x1f + mov bx, word ptr [edi] + shr bx, cl + and bx, 0x1f + sub ax, bx + imul ax, dx + shr ax, 5 + mov bx, word ptr [edi] + shr bx, cl + and bx, 0x1f + add ax, bx + and ax, 0x1f + shl ax, cl + mov bx, ax + pop ax + add bx, ax + pop ax + add bx, ax + pop cx + mov word ptr [edi], bx + add esi, 2 + add edi, 2 + sub ecx, 1 + jne stream_pixel + pop edx + jmp draw_token + stream_opaque: + mov word ptr [edi], ax + add esi, 2 + add edi, 2 + sub ecx, 1 + jne stream_pixel + pop edx + jmp draw_token + done: + } + // clang-format on + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/renderTgxWithColorUnk.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderTgxWithColorUnk.cpp new file mode 100644 index 0000000000..cb08a350b9 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderTgxWithColorUnk.cpp @@ -0,0 +1,156 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/Graphics/TgxToken.hpp" +#include "OpenSHC/Rendering/Enums/RenderTarget.hpp" + +#include "OpenSHC/Globals/DAT_TextureRenderCoreObject.hpp" +#include "OpenSHC/Globals/DAT_WindowAndDirectDraw.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + using OpenSHC::IO::Graphics::TT_REPEATING_PIXELS; + using OpenSHC::IO::Graphics::TT_STREAM_OF_PIXELS; + using OpenSHC::IO::Graphics::TT_TGX_PIXEL_HEADER; + using OpenSHC::IO::Graphics::TT_TGX_PIXEL_LENGTH; + using OpenSHC::IO::Graphics::TT_TRANSPARENT_PIXELS; + + // FUNCTION: STRONGHOLDCRUSADER 0x0044F6F0 + void TextureRenderCore::renderTgxWithColorUnk( + int xPos, int yPos, int width, int height, ushort* imageSource, ushort fillColorUnk) + { + // Draws the TGX image's shape in one colour: every stream or repeating pixel becomes fillColorUnk + // Declared before the if because both arms assign them; lineJump is from one row's end to the next + int lineJump; + int lineBytes; + int heightStart; + int heightEnd; + // Map arm first (== with the arms swapped: quick_diff 0.8016 -> 0.7115) + if (this->drawBufferChoiceValue != OpenSHC::Rendering::Enums::RT_SCREEN_MENU) { + DAT_TextureRenderCoreObject::instance.currentRenderSurface + = DAT_WindowAndDirectDraw::instance.surfacePointer_mapGame; + // The map surface is 4056 pixels wide + lineJump = (4056 - width) * 2; + heightStart = this->mapGameSurfaceHeightRange.start; + lineBytes = 4056 * 2; + heightEnd = this->mapGameSurfaceHeightRange.end; + } else { + DAT_TextureRenderCoreObject::instance.currentRenderSurface + = DAT_WindowAndDirectDraw::instance.surfacePointer_screenMenu; + lineJump = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine - width * 2; + lineBytes = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine; + heightStart = this->screenMenuSurfaceHeightRange.start; + heightEnd = this->screenMenuSurfaceHeightRange.end; + } + if (height + yPos > heightEnd) { + height = heightEnd - yPos; + if (height <= 0) { + return; + } + } + // The token loop is handwritten assembly in the original + // clang-format off + __asm { + mov esi, imageSource + mov edi, DAT_TextureRenderCoreObject::instance.currentRenderSurface + mov eax, xPos + cmp eax, 0 + jl done + add eax, eax + add edi, eax + mov eax, yPos + mov ebx, heightStart + cmp eax, ebx + jge add_y_offset + + // Rows above heightStart: skip their tokens without drawing + sub ebx, eax + mov eax, ebx + cmp height, eax + jle done + sub height, eax + skip_token: + mov ecx, 0 + mov bl, byte ptr [esi] + mov cl, byte ptr [esi] + and bl, TT_TGX_PIXEL_HEADER + and cl, TT_TGX_PIXEL_LENGTH + add esi, 1 + cmp bl, TT_TRANSPARENT_PIXELS + je skip_token + cmp bl, TT_STREAM_OF_PIXELS + je skip_stream + cmp bl, TT_REPEATING_PIXELS + je skip_repeating + jmp skip_newline + skip_stream: + add ecx, 1 + add ecx, ecx + add esi, ecx + jmp skip_token + skip_repeating: + add esi, 2 + jmp skip_token + skip_newline: + sub eax, 1 + cmp eax, 0 + jg skip_token + mov eax, heightStart + + add_y_offset: + mov edx, lineBytes + mul edx + add edi, eax + mov edx, height + draw_token: + mov ecx, 0 + mov al, byte ptr [esi] + mov cl, byte ptr [esi] + and al, TT_TGX_PIXEL_HEADER + and cl, TT_TGX_PIXEL_LENGTH + add esi, 1 + cmp al, TT_TRANSPARENT_PIXELS + je draw_transparent + cmp al, TT_STREAM_OF_PIXELS + je draw_stream + cmp al, TT_REPEATING_PIXELS + je draw_repeating + add edi, lineJump + sub edx, 1 + cmp edx, 0 + jg draw_token + jmp done + draw_transparent: + add ecx, 1 + add ecx, ecx + add edi, ecx + jmp draw_token + draw_repeating: + add esi, 2 + add ecx, 1 + mov ax, fillColorUnk + repeating_pixel: + mov word ptr [edi], ax + add edi, 2 + sub ecx, 1 + jne repeating_pixel + jmp draw_token + draw_stream: + add ecx, 1 + mov ax, fillColorUnk + stream_pixel: + mov word ptr [edi], ax + add esi, 2 + add edi, 2 + sub ecx, 1 + jne stream_pixel + jmp draw_token + done: + } + // clang-format on + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/transformRawToRGB555To565.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/transformRawToRGB555To565.cpp new file mode 100644 index 0000000000..6c677c7e25 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/transformRawToRGB555To565.cpp @@ -0,0 +1,22 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/UI/Rendering/TextureRenderCore/RGB555To565Asm.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x0044CB60 + void TextureRenderCore::transformRawToRGB555To565(int imageOffset, int imageSize) + { + // The conversion loop is handwritten assembly in the original, see RGB555To565Asm.hpp. + ushort* _colorPtr = (ushort*)(this->gmProcessedImageData + imageOffset); + // written by the asm below + ushort _blue; + ushort _green; + MACRO_RGB555_TO_565_LOOP(imageSize) + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/transformRawWithMarkerUnkToRGB555To565.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/transformRawWithMarkerUnkToRGB555To565.cpp new file mode 100644 index 0000000000..f8c9f7cc78 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/transformRawWithMarkerUnkToRGB555To565.cpp @@ -0,0 +1,22 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/UI/Rendering/TextureRenderCore/RGB555To565Asm.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x0044C940 + void TextureRenderCore::transformRawWithMarkerUnkToRGB555To565(int imageOffset, int imageSize) + { + // The conversion loop is handwritten assembly in the original, see RGB555To565Asm.hpp. + ushort* _colorPtr = (ushort*)(this->gmProcessedImageData + imageOffset); + // written by the asm below + ushort _blue; + ushort _green; + MACRO_RGB555_TO_565_LOOP_SKIPPING_MARKER(imageSize) + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/transformTgxCompressedImageToRGB565.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/transformTgxCompressedImageToRGB565.cpp index 4038a632a7..11e3a8693d 100644 --- a/src/OpenSHC/UI/Rendering/TextureRenderCore/transformTgxCompressedImageToRGB565.cpp +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/transformTgxCompressedImageToRGB565.cpp @@ -1,7 +1,5 @@ #include "../TextureRenderCore.func.hpp" -#include "OpenSHC/UI/Rendering/TextureRenderCore.func.hpp" - #include "OpenSHC/Globals/DAT_GMImageOffsets.hpp" #include "OpenSHC/Globals/DAT_GMImageSizes.hpp" #include "OpenSHC/Globals/DAT_TextureRenderCoreObject.hpp" @@ -14,7 +12,7 @@ namespace UI { void TextureRenderCore::transformTgxCompressedImageToRGB565(int imageIndex) { MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::transformTgxFromRGB555ToRGB565, this)( - (ushort*)((byte*)this->gmProcessedImageData + DAT_GMImageOffsets::instance[imageIndex]), + (ushort*)(this->gmProcessedImageData + DAT_GMImageOffsets::instance[imageIndex]), DAT_GMImageSizes::instance[imageIndex]); } diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/transformTgxFromRGB555ToRGB565.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/transformTgxFromRGB555ToRGB565.cpp new file mode 100644 index 0000000000..832f2d442e --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/transformTgxFromRGB555ToRGB565.cpp @@ -0,0 +1,76 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/Graphics/TgxToken.hpp" +#include "OpenSHC/UI/Rendering/TextureRenderCore/RGB555To565Asm.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + using OpenSHC::IO::Graphics::TT_NEWLINE; + using OpenSHC::IO::Graphics::TT_REPEATING_PIXELS; + using OpenSHC::IO::Graphics::TT_TGX_PIXEL_HEADER; + using OpenSHC::IO::Graphics::TT_TGX_PIXEL_LENGTH; + using OpenSHC::IO::Graphics::TT_TRANSPARENT_PIXELS; + + // FUNCTION: STRONGHOLDCRUSADER 0x0044C9C0 + void TextureRenderCore::transformTgxFromRGB555ToRGB565(ushort* tgxDataPtr, int tgxByteSize) + { + // Copied in C++: the original copies the parameter before saving ebx and esi + ushort* const tgxPtr = tgxDataPtr; + // Written by the asm only; red waits in tgxDataPtr's slot, dead once copied + ushort _blue; + ushort _green; + // Handwritten in the original; a token's top three bits are its kind, the low five its run length - 1 + // clang-format off + __asm { + mov esi, tgxPtr + mov edx, tgxByteSize + + next_token: + cmp edx, 0 + jle done + mov ecx, 0 + mov al, byte ptr [esi] + mov cl, byte ptr [esi] + and al, TT_TGX_PIXEL_HEADER + and cl, TT_TGX_PIXEL_LENGTH + add ecx, 1 + sub edx, 1 + add esi, 1 + cmp al, TT_NEWLINE + je newline + cmp al, TT_REPEATING_PIXELS + je repeating_pixels + cmp al, TT_TRANSPARENT_PIXELS + je transparent_pixels + + stream_pixel: + } + MACRO_RGB555_TO_565_PIXEL(tgxDataPtr, MACRO_ASM_SKIP_MARKER_TEST, MACRO_ASM_SKIP_MARKER_LABEL) + __asm { + sub ecx, 1 + cmp ecx, 0 + jle next_token + jmp stream_pixel + + repeating_pixels: + } + MACRO_RGB555_TO_565_PIXEL(tgxDataPtr, MACRO_ASM_NONE, MACRO_ASM_NONE) + __asm { + jmp next_token + + transparent_pixels: + jmp next_token + + newline: + jmp next_token + + done: + } + // clang-format on + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/transformTileObjectToRGB565.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/transformTileObjectToRGB565.cpp new file mode 100644 index 0000000000..fdfda12bc9 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/transformTileObjectToRGB565.cpp @@ -0,0 +1,23 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/UI/Rendering/TextureRenderCore/RGB555To565Asm.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x0044CAE0 + void TextureRenderCore::transformTileObjectToRGB565(int imageOffset) + { + // The conversion loop is handwritten assembly in the original, see RGB555To565Asm.hpp. + ushort* _colorPtr = (ushort*)(this->gmProcessedImageData + imageOffset); + // written by the asm below + ushort _blue; + ushort _green; + // a tile object is 512 bytes, 256 pixels + MACRO_RGB555_TO_565_LOOP(512) + } + + } +} +} diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/transformTilesetToRGB565.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/transformTilesetToRGB565.cpp new file mode 100644 index 0000000000..8d939b9f96 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/transformTilesetToRGB565.cpp @@ -0,0 +1,26 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/Globals/DAT_GMImageHeaders.hpp" +#include "OpenSHC/Globals/DAT_GMImageOffsets.hpp" +#include "OpenSHC/Globals/DAT_GMImageSizes.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x00455290 + void TextureRenderCore::transformTilesetToRGB565(int imageIndex) + { + // A tile object is a 512-byte tile, followed by TGX data when its direction is set + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::transformTileObjectToRGB565, this)( + DAT_GMImageOffsets::instance[imageIndex]); + if (DAT_GMImageHeaders::instance.imh[imageIndex].direction != 0) { + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::transformTgxFromRGB555ToRGB565, this)( + (ushort*)(this->gmProcessedImageData + DAT_GMImageOffsets::instance[imageIndex] + 512), + DAT_GMImageSizes::instance[imageIndex] - 512); + } + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 6e036f9340..ebe443b73f 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -16903,13 +16903,13 @@ SHC_3BB0A8C1_0x0044C850 | 0.0% | Pending SHC_3BB0A8C1_0x0044C8F0 | 0.0% | Pending -SHC_3BB0A8C1_0x0044C940 | 0.0% | Pending +SHC_3BB0A8C1_0x0044C940 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0044C9C0 | 0.0% | Pending +SHC_3BB0A8C1_0x0044C9C0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0044CAE0 | 0.0% | Pending +SHC_3BB0A8C1_0x0044CAE0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0044CB60 | 0.0% | Pending +SHC_3BB0A8C1_0x0044CB60 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0044CBE0 | 100.0% | Reimplemented @@ -16927,13 +16927,13 @@ SHC_3BB0A8C1_0x0044CD10 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0044CD30 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0044CD40 | 0.0% | Pending +SHC_3BB0A8C1_0x0044CD40 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0044CDC0 | 0.0% | Pending SHC_3BB0A8C1_0x0044CDD0 | 0.0% | Pending -SHC_3BB0A8C1_0x0044CE00 | 0.0% | Pending +SHC_3BB0A8C1_0x0044CE00 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0044CE80 | 0.0% | Pending @@ -16953,13 +16953,13 @@ SHC_3BB0A8C1_0x0044E510 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0044E630 | 0.0% | Pending -SHC_3BB0A8C1_0x0044F020 | 0.0% | Pending +SHC_3BB0A8C1_0x0044F020 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0044F170 | 0.0% | Pending -SHC_3BB0A8C1_0x0044F6F0 | 0.0% | Pending +SHC_3BB0A8C1_0x0044F6F0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0044F850 | 0.0% | Pending +SHC_3BB0A8C1_0x0044F850 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0044FBF0 | 0.0% | Pending @@ -16973,15 +16973,15 @@ SHC_3BB0A8C1_0x00453B00 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00454080 | 0.0% | Pending -SHC_3BB0A8C1_0x00454620 | 0.0% | Pending +SHC_3BB0A8C1_0x00454620 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00454700 | 0.0% | Pending +SHC_3BB0A8C1_0x00454700 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004547C0 | 0.0% | Pending +SHC_3BB0A8C1_0x004547C0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004548D0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00454900 | 0.0% | Pending +SHC_3BB0A8C1_0x00454900 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00454960 | 100.0% | Reimplemented @@ -16989,11 +16989,11 @@ SHC_3BB0A8C1_0x00454990 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004549C0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004549F0 | 0.0% | Pending +SHC_3BB0A8C1_0x004549F0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00454A60 | 0.0% | Pending +SHC_3BB0A8C1_0x00454A60 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00454CB0 | 0.0% | Pending +SHC_3BB0A8C1_0x00454CB0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00454CE0 | 0.0% | Pending @@ -17005,25 +17005,25 @@ SHC_3BB0A8C1_0x00454EF0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00454F00 | 0.0% | Pending -SHC_3BB0A8C1_0x00455050 | 0.0% | Pending +SHC_3BB0A8C1_0x00455050 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00455250 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00455270 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00455290 | 0.0% | Pending +SHC_3BB0A8C1_0x00455290 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004552E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00455300 | 0.0% | Pending +SHC_3BB0A8C1_0x00455300 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00455390 | 0.0% | Pending +SHC_3BB0A8C1_0x00455390 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004554A0 | 0.0% | Pending +SHC_3BB0A8C1_0x004554A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00455540 | 0.0% | Pending -SHC_3BB0A8C1_0x004557B0 | 0.0% | Pending +SHC_3BB0A8C1_0x004557B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004558E0 | 0.0% | Pending @@ -17033,7 +17033,7 @@ SHC_3BB0A8C1_0x004559B0 | 0.0% | Pending SHC_3BB0A8C1_0x00455B70 | 0.0% | Pending -SHC_3BB0A8C1_0x00455C60 | 0.0% | Pending +SHC_3BB0A8C1_0x00455C60 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00455D40 | 100.0% | Reimplemented diff --git a/tools/import/ghidra-importer.py b/tools/import/ghidra-importer.py index d38fca89f3..5ffdf5d3c4 100644 --- a/tools/import/ghidra-importer.py +++ b/tools/import/ghidra-importer.py @@ -335,6 +335,10 @@ def write_patches(output_dir: pathlib.Path): (output_dir / "OpenSHC/Map/Navigation/PathFindingState/" / "LinkageNeighbourAsm.hpp").write_bytes( (pathlib.Path(__file__).parent / "injected-headers" / "LinkageNeighbourAsm.hpp").read_bytes() ) + + (output_dir / "OpenSHC/UI/Rendering/TextureRenderCore/" / "RGB555To565Asm.hpp").write_bytes( + (pathlib.Path(__file__).parent / "injected-headers" / "RGB555To565Asm.hpp").read_bytes() + ) pass # Overwrite hpp files in this function if not args.dry_run: diff --git a/tools/import/injected-headers/RGB555To565Asm.hpp b/tools/import/injected-headers/RGB555To565Asm.hpp new file mode 100644 index 0000000000..7449abd5a9 --- /dev/null +++ b/tools/import/injected-headers/RGB555To565Asm.hpp @@ -0,0 +1,91 @@ +/** + HAND WRITTEN - not generated. + + path: 'OpenSHC/UI/Rendering/TextureRenderCore/RGB555To565Asm.hpp' + + Converting RGB555 pixels to RGB565 in place is handwritten assembly in the original, and it is a + MACRO: the identical instruction stream appears, never as a call, as the whole loop in + transformRawWithMarkerUnkToRGB555To565 (0x0044C940), transformTileObjectToRGB565 (0x0044CAE0) and + transformRawToRGB555To565 (0x0044CB60), and as the pixel body twice in transformTgxFromRGB555ToRGB565 + (0x0044C9C0: at 0x0044CA08 with the marker test, at 0x0044CA6D without). It is recognisable by + + - "cmp edx, 0" where MSVC would emit "test edx, edx", + - the pixel read with "mov ax, word ptr [esi]" and every channel computed in 16-bit registers, + where MSVC would emit movzx and work in 32 bits, + - the loop closed with an unconditional "jmp" back to its test. + + Red goes into a dead parameter's stack slot, as in the original. + + The macros expect these names in the enclosing function: + + ushort _blue declared after the pixel pointer + ushort _green declared after _blue + RED the dead parameter that holds red: imageOffset in the loops, tgxDataPtr in + 0x0044C9C0 + + and the loops also + + ushort* _colorPtr the first pixel, gmProcessedImageData + imageOffset, declared first + int imageOffset the parameter + + MACRO_RGB555_TO_565_PIXEL converts the pixel at [esi], then steps edx back by 2 and esi on by 2. With + MACRO_ASM_SKIP_MARKER_TEST and MACRO_ASM_SKIP_MARKER_LABEL it leaves pixels equal to 0xf81f untouched; + that is magenta already encoded as RGB565, the transparency marker. COUNT is the buffer size in bytes. + All of them clobber eax and ebx, the loops also edx and esi. They define assembler labels, so a loop + can be expanded once per function, and the pixel macro more than once, but with the skip label once. +*/ + +#pragma once + +// clang-format off + +#define MACRO_ASM_NONE +#define MACRO_ASM_SKIP_MARKER_TEST \ + __asm cmp ax, 0xf81f \ + __asm je skip_color +#define MACRO_ASM_SKIP_MARKER_LABEL \ + __asm skip_color: + +#define MACRO_RGB555_TO_565_PIXEL(RED, SKIP_TEST, SKIP_LABEL) \ + __asm mov ax, word ptr [esi] \ + SKIP_TEST \ + __asm mov bx, ax \ + __asm and bx, 0x1f \ + __asm mov _blue, bx \ + __asm mov bx, ax \ + __asm and bx, 0x3e0 \ + __asm shr bx, 5 \ + __asm mov _green, bx \ + __asm mov bx, ax \ + __asm and bx, 0x7c00 \ + __asm shr bx, 10 \ + __asm mov word ptr RED, bx \ + __asm mov ax, _blue \ + __asm mov bx, _green \ + __asm shl bx, 6 \ + __asm add ax, bx \ + __asm mov bx, word ptr RED \ + __asm shl bx, 11 \ + __asm add ax, bx \ + __asm mov word ptr [esi], ax \ + SKIP_LABEL \ + __asm sub edx, 2 \ + __asm add esi, 2 + +#define MACRO_RGB555_TO_565_LOOP_IMPL(COUNT, SKIP_TEST, SKIP_LABEL) \ + __asm mov esi, _colorPtr \ + __asm mov edx, COUNT \ + __asm next_color: \ + __asm cmp edx, 0 \ + __asm jle done \ + MACRO_RGB555_TO_565_PIXEL(imageOffset, SKIP_TEST, SKIP_LABEL) \ + __asm jmp next_color \ + __asm done: + +#define MACRO_RGB555_TO_565_LOOP(COUNT) \ + MACRO_RGB555_TO_565_LOOP_IMPL(COUNT, MACRO_ASM_NONE, MACRO_ASM_NONE) + +#define MACRO_RGB555_TO_565_LOOP_SKIPPING_MARKER(COUNT) \ + MACRO_RGB555_TO_565_LOOP_IMPL(COUNT, MACRO_ASM_SKIP_MARKER_TEST, MACRO_ASM_SKIP_MARKER_LABEL) + +// clang-format on