Skip to content
100 changes: 100 additions & 0 deletions src/OpenSHC/UI/Rendering/TextureRenderCore/loadGMFile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "../TextureRenderCore.func.hpp"

#include "OpenSHC/IO/LowLevelMemory.func.hpp"
#include "OpenSHC/IO/ResourceManager.func.hpp"
#include "OpenSHC/IO/FileResourceType.hpp"
#include "OpenSHC/IO/Graphics/GmImageType.hpp"
#include "OpenSHC/string-literals.hpp"

#include "OpenSHC/Globals/DAT_GMImageHeaders.hpp"
#include "OpenSHC/Globals/DAT_GMImageOffsets.hpp"
#include "OpenSHC/Globals/DAT_GMImageSizes.hpp"
#include "OpenSHC/Globals/DAT_LoadingBarProgress.hpp"
#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp"
#include "OpenSHC/Globals/DAT_PictureNumToGmIDArray_UNUSED.hpp"
#include "OpenSHC/Globals/DAT_ResourceManager.hpp"

namespace OpenSHC {
namespace UI {
namespace Rendering {

// FUNCTION: STRONGHOLDCRUSADER 0x004559B0
void TextureRenderCore::loadGMFile(GmID gmID, char* gmFileName)
{
// keep the window responsive while the files load
MSG message;
if (PeekMessageA(&message, NULL, 0, 0, 0) && GetMessageA(&message, NULL, 0, 0)) {
TranslateMessage(&message);
DispatchMessageA(&message);
}
BOOLEnum const flag = this->unknownSfxAndGmRelatedFlag;
int totalSize = 0;
MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)(
sizeof(this->gmFileHeaderColorpaletteArray[gmID]), 0, &this->gmFileHeaderColorpaletteArray[gmID]);
MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)(
1000, 0, this->gmFileNameArray_UNUSEDUnk_0x13179c[gmID]);
MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::copyData, DAT_LowLevelMemory::ptr)(
strlen(gmFileName), gmFileName, this->gmFileNameArray_UNUSEDUnk_0x13179c[gmID]);
MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::resolveResourceFileName, DAT_ResourceManager::ptr)(
OpenSHC::IO::FRT_GM, this->gmFileNameArray_UNUSEDUnk_0x13179c[gmID]);

// the header, the offsets, sizes and headers of its pictures, then their data
MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readFirstPartOfCurrentResourceIntoMemory,
DAT_ResourceManager::ptr)(&this->gmFileHeaderColorpaletteArray[gmID],
sizeof(this->gmFileHeaderColorpaletteArray[gmID]), (char*)s_gm1_005a5068);
MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readNextPartOfCurrentResourceIntoMemory,
DAT_ResourceManager::ptr)(&DAT_GMImageOffsets::instance[this->gmNumberOfProcessedPictures],
this->gmFileHeaderColorpaletteArray[gmID].numberOfPicturesInFile * 4, (char*)s_gm1_005a5068);
MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readNextPartOfCurrentResourceIntoMemory,
DAT_ResourceManager::ptr)(&DAT_GMImageSizes::instance[this->gmNumberOfProcessedPictures],
this->gmFileHeaderColorpaletteArray[gmID].numberOfPicturesInFile * 4, (char*)s_gm1_005a5068);
MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readNextPartOfCurrentResourceIntoMemory,
DAT_ResourceManager::ptr)(&DAT_GMImageHeaders::instance.imh[this->gmNumberOfProcessedPictures],
this->gmFileHeaderColorpaletteArray[gmID].numberOfPicturesInFile * 16, (char*)s_gm1_005a5068);
MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readNextPartOfCurrentResourceIntoMemory,
DAT_ResourceManager::ptr)(this->gmAndGfxImageDataBuffer,
this->gmFileHeaderColorpaletteArray[gmID].dataSize, (char*)s_gm1_005a5068);
if (this->gmFileHeaderColorpaletteArray[gmID].ImageType == OpenSHC::IO::Graphics::GIT_Animation) {
MACRO_CALL_MEMBER(
OpenSHC::UI::Rendering::TextureRenderCore_Func::transformGmColorTableFromRGB555To565IfRequired,
this)(gmID);
}

// copy each picture's data behind the ones processed so far; a picture that shares another one's data
// points at it instead
int offset = DAT_GMImageOffsets::instance[this->gmNumberOfProcessedPictures];
for (int picture = this->gmNumberOfProcessedPictures; picture
< this->gmFileHeaderColorpaletteArray[gmID].numberOfPicturesInFile + this->gmNumberOfProcessedPictures;
++picture) {
DAT_PictureNumToGmIDArray_UNUSED::instance[picture] = gmID;
if ((flag && DAT_GMImageHeaders::instance.imh[picture].animatedColor != 0)
|| (DAT_GMImageHeaders::instance.imh[picture].animatedColor & 4) != 0) {
if ((DAT_GMImageHeaders::instance.imh[picture].animatedColor & 4) != 0) {
DAT_GMImageOffsets::instance[picture] = DAT_GMImageOffsets::instance[picture - 1];
} else {
DAT_GMImageOffsets::instance[picture] = DAT_GMImageOffsets::instance
[DAT_GMImageHeaders::instance.imh[picture].alternativeImageIndexUnk + picture];
}
DAT_GMImageSizes::instance[picture]
= DAT_GMImageSizes::instance[DAT_GMImageHeaders::instance.imh[picture].alternativeImageIndexUnk
+ picture];
} else {
MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::copyData, DAT_LowLevelMemory::ptr)(
DAT_GMImageSizes::instance[picture],
this->gmAndGfxImageDataBuffer + DAT_GMImageOffsets::instance[picture],
this->gmProcessedImageData + this->gmSizeOfProcessedPictures_0x44 + offset);
DAT_GMImageOffsets::instance[picture] = this->gmSizeOfProcessedPictures_0x44 + offset;
totalSize += DAT_GMImageSizes::instance[picture];
offset += DAT_GMImageSizes::instance[picture];
MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::adaptGmColorsToRGB565IfRequired,
this)(gmID, picture);
}
}
this->gmSizeOfProcessedPictures_0x44 += totalSize;
MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::drawLoadingBarUnk, this)(
gmID, DAT_LoadingBarProgress::instance + 180);
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "../TextureRenderCore.func.hpp"

#include "OpenSHC/Rendering/ScreenResolutionEnum.hpp"

#include "OpenSHC/Globals/COL_MAGENTA.hpp"
#include "OpenSHC/Globals/DAT_BlendingDefinedData.hpp"
#include "OpenSHC/Globals/DAT_MenuHandlerState.hpp"
#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp"
#include "OpenSHC/Globals/DAT_WindowAndDirectDraw.hpp"

namespace OpenSHC {
namespace UI {
namespace Rendering {

// The copy loop is handwritten assembly in the original, transcribed here from the binary.

// FUNCTION: STRONGHOLDCRUSADER 0x00454F00
void TextureRenderCore::moveOverlappingMenuPartsToMapSurface()
{
// copies the parts of the open menu tab that reach over the map from the menu surface onto the map
// surface, all but the magenta pixels; the tab's records hold x, y, width and height, -1 ends them and
// -2 marks the ones that only apply above 800x600
int const tab = this->activeMenuTabIndex;
if (tab == 0) {
return;
}
if (DAT_BlendingDefinedData::instance.field_0x3014[(tab - 1) * 5][0] == -1) {
return;
}
// the asm below names COL_MAGENTA, which inline asm can only do once C++ code has instantiated it
(void)sizeof(COL_MAGENTA::instance);
// the index in a local: the original computes it again here (inline: 98 instructions to its 100)
int const record = (tab - 1) * 5;
int* width = &DAT_BlendingDefinedData::instance.field_0x3014[record][2];
int* x = &DAT_BlendingDefinedData::instance.field_0x3014[record][0];
int* height = &DAT_BlendingDefinedData::instance.field_0x3014[record][3];
int* y = &DAT_BlendingDefinedData::instance.field_0x3014[record][1];
do {
if (*x == -2) {
if (DAT_WindowAndDirectDraw::instance.currentGameResolution == OpenSHC::Rendering::SRE_800x600) {
return;
}
y += 4;
height += 4;
width += 4;
x += 4;
}
ushort* const screen = (ushort*)((byte*)DAT_WindowAndDirectDraw::instance.surfacePointer_screenMenu
+ (*y + DAT_MenuHandlerState::instance.y)
* DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine
+ (DAT_MenuHandlerState::instance.x + *x) * 2);
// the map surface is 4056 pixels wide
ushort* const map = DAT_WindowAndDirectDraw::instance.surfacePointer_mapGame
+ (*y + DAT_ViewportRenderState::instance.viewportState.currentCameraOffsetY
+ DAT_MenuHandlerState::instance.y)
* 4056
+ *x + DAT_ViewportRenderState::instance.viewportState.currentCameraOffsetX
+ DAT_MenuHandlerState::instance.x;
int const mapLineJump = (4056 - *width) * 2;
int const screenLineJump = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine - *width * 2;
int const partHeight = *height;
int const partWidth = *width;
// clang-format off
__asm {
mov esi, screen
mov edi, map
mov ecx, partHeight
mov bx, COL_MAGENTA::instance.shortValue
rowLoop:
mov edx, partWidth
pixelLoop:
mov ax, word ptr [esi]
cmp ax, bx
je skipPixel
mov word ptr [edi], ax
skipPixel:
add esi, 2
add edi, 2
sub edx, 1
jne pixelLoop
add esi, screenLineJump
add edi, mapLineJump
sub ecx, 1
jne rowLoop
}
// clang-format on
y += 4;
height += 4;
width += 4;
x += 4;
} while (*x != -1);
}

}
}
}
Loading
Loading