Skip to content

Commit

Permalink
Fix middle frame
Browse files Browse the repository at this point in the history
  • Loading branch information
ZivDero committed Nov 27, 2024
1 parent 4863449 commit 55937ca
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
49 changes: 31 additions & 18 deletions src/extensions/animtype/animtypeext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ AnimTypeClassExtension::AnimTypeClassExtension(const AnimTypeClass *this_ptr) :
EndAnimsCount(),
EndAnimsMinimum(),
EndAnimsMaximum(),
BiggestFrameWidth(0),
BiggestFrameHeight(0)
MiddleFrame(-2)
{
//if (this_ptr) EXT_DEBUG_TRACE("AnimTypeClassExtension::AnimTypeClassExtension - Name: %s (0x%08X)\n", Name(), (uintptr_t)(This()));

Expand Down Expand Up @@ -370,27 +369,41 @@ bool AnimTypeClassExtension::Read_INI(CCINIClass &ini)
* A special value of "-1" will set the biggest frame to the actual middle frame
* of the shape file. This behavior was observed in Red Alert 2.
*/
if (This()->Image != nullptr && This()->Image->Get_Frame_Count() > 0) {
ShapeFileStruct *image = const_cast<ShapeFileStruct *>(This()->Image);

int biggest = ini.Get_Int_Clamp(ini_name, "MiddleFrame", -1, image->Get_Frame_Count()-1, This()->Biggest);
if (This()->Image && This()->Image->Get_Frame_Count() > 0) {
MiddleFrame = ini.Get_Int_Clamp(ini_name, "MiddleFrame", -2, This()->Image->Get_Frame_Count() - 1, MiddleFrame);
}

if (biggest == -1 && image->Get_Frame_Count() >= 2) {
IsInitialized = true;

This()->Biggest = image->Get_Frame_Count() / 2;
BiggestFrameWidth = image->Get_Frame_Data(This()->Biggest)->FrameWidth;
BiggestFrameHeight = image->Get_Frame_Data(This()->Biggest)->FrameHeight;
return true;
}

} else if (biggest != This()->Biggest) {

This()->Biggest = biggest;
BiggestFrameWidth = image->Get_Frame_Data(biggest)->FrameWidth;
BiggestFrameHeight = image->Get_Frame_Data(biggest)->FrameHeight;
}
/**
* Sets the biggest frame of the AnimType with our override.
*
* @author: ZivDero
*/
void AnimTypeClassExtension::Set_Biggest_Frame()
{
if (MiddleFrame == -1 && This()->Image && This()->Image->Get_Frame_Count() >= 2) {
This()->Biggest = This()->Image->Get_Frame_Count() / 2;
}

else if (MiddleFrame != -2) {
This()->Biggest = MiddleFrame;
}
}

IsInitialized = true;

return true;
/**
* Sets the biggest frame of the all AnimTypes with our override.
*
* @author: ZivDero
*/
void AnimTypeClassExtension::All_Set_Biggest_Frame()
{
for (int i = 0; i < AnimTypeExtensions.Count(); i++) {
AnimTypeExtensions[i]->Set_Biggest_Frame();
}
}

8 changes: 5 additions & 3 deletions src/extensions/animtype/animtypeext.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ AnimTypeClassExtension final : public ObjectTypeClassExtension

virtual bool Read_INI(CCINIClass &ini) override;

void Set_Biggest_Frame();
static void All_Set_Biggest_Frame();

public:
/**
* If the cell in which this animation is placed does not contain
Expand Down Expand Up @@ -126,8 +129,7 @@ AnimTypeClassExtension final : public ObjectTypeClassExtension
TypeList<int> EndAnimsMaximum;

/**
* The width and height of the biggest frame, if set by the user.
* The middle (biggest) frame, if set by the user.
*/
unsigned BiggestFrameWidth;
unsigned BiggestFrameHeight;
int MiddleFrame;
};
23 changes: 23 additions & 0 deletions src/extensions/animtype/animtypeext_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "fatal.h"
#include "debughandler.h"
#include "asserthandler.h"
#include "extension.h"

#include "hooker.h"
#include "hooker_macros.h"
Expand Down Expand Up @@ -112,6 +113,26 @@ DECLARE_PATCH(_AnimTypeClass_Get_Image_Data_Assertion_Patch)
}


/**
* Sets the biggest frame with our override after Init because it reloads the image.
*
* @author: ZivDero
*/
DECLARE_PATCH(_AnimTypeClass_Init_MiddleFrame_Patch)
{
AnimTypeClassExtension::All_Set_Biggest_Frame();

// Reconstruct the epilogue
_asm
{
pop esi
pop ebp
add esp, 0x204
retn
}
}


/**
* Main function for patching the hooks.
*/
Expand All @@ -127,4 +148,6 @@ void AnimTypeClassExtension_Hooks()
Patch_Jump(0x00419B40, &AnimTypeClassExt::_Free_Image);
Patch_Jump(0x004187DB, &_AnimTypeClass_DTOR_Free_Image_Patch);
Patch_Jump(0x00419C0B, &_AnimTypeClass_SDDTOR_Free_Image_Patch);
Patch_Jump(0x004188E6, &_AnimTypeClass_Init_MiddleFrame_Patch);
Patch_Jump(0x004189C9, &_AnimTypeClass_Init_MiddleFrame_Patch);
}

0 comments on commit 55937ca

Please sign in to comment.