From a2388f6a8403b597595d3f300a00760f92ca06b0 Mon Sep 17 00:00:00 2001 From: MainMemory Date: Sun, 26 Nov 2017 20:52:11 -0600 Subject: [PATCH] Moving widescreen functions to separate file. --- SonicRModLoader/SonicRModLoader.vcxproj | 2 + .../SonicRModLoader.vcxproj.filters | 6 + SonicRModLoader/Widescreen.cpp | 186 ++++++++++++++++++ SonicRModLoader/Widescreen.h | 9 + SonicRModLoader/dllmain.cpp | 184 +---------------- 5 files changed, 204 insertions(+), 183 deletions(-) create mode 100644 SonicRModLoader/Widescreen.cpp create mode 100644 SonicRModLoader/Widescreen.h diff --git a/SonicRModLoader/SonicRModLoader.vcxproj b/SonicRModLoader/SonicRModLoader.vcxproj index de8642e..6527c06 100644 --- a/SonicRModLoader/SonicRModLoader.vcxproj +++ b/SonicRModLoader/SonicRModLoader.vcxproj @@ -103,6 +103,7 @@ + @@ -116,6 +117,7 @@ Create Create + diff --git a/SonicRModLoader/SonicRModLoader.vcxproj.filters b/SonicRModLoader/SonicRModLoader.vcxproj.filters index 9a0b865..50ff39d 100644 --- a/SonicRModLoader/SonicRModLoader.vcxproj.filters +++ b/SonicRModLoader/SonicRModLoader.vcxproj.filters @@ -48,6 +48,9 @@ Header Files + + Header Files + @@ -74,5 +77,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/SonicRModLoader/Widescreen.cpp b/SonicRModLoader/Widescreen.cpp new file mode 100644 index 0000000..c637f0c --- /dev/null +++ b/SonicRModLoader/Widescreen.cpp @@ -0,0 +1,186 @@ +#include "stdafx.h" +#include "Widescreen.h" +#include "SonicRModLoader.h" + +// Widescreen support +int MapWidthTo640() { + int HorizontalResolution = *(int*)0x5F3874; + int VerticalResolution = *(int*)0x75353C; + float AspRatio = (float)HorizontalResolution / (float)VerticalResolution; + return (int)(480 * AspRatio); +} + +int GetExtraSpace() { + return (MapWidthTo640() - 640); +} + +void D3D_RenderHUD_MainTimer_AlignRight( + int XPos, int YPos, float ZPos, int Time, int Unk +) { + int *SpriteXOff = (int*)0x72E010; + int tmp = *SpriteXOff; + // Right side of screen? + if (*SpriteXOff + 1 >= (HorizontalResolution >> 1)) { + *SpriteXOff = HorizontalResolution >> 1; + } + else { + *SpriteXOff = 0; + } + int WidthRatio = 0; + if (MP_WindowCount == 2 && MP_HUD2PSplit == 1) { + WidthRatio = 1; + } + + D3D_RenderHUD_MainTimer( + XPos + (GetExtraSpace() >> WidthRatio), YPos, ZPos, Time, Unk + ); + + *SpriteXOff = tmp; +} + +void D3D_RenderHUD_LapTimer_AlignRight(int XPos, int YPos, int Time) { + int *SpriteXOff = (int*)0x72E010; + int tmp = *SpriteXOff; + // Right side of screen? + if (*SpriteXOff + 1 >= (HorizontalResolution >> 1)) { + *SpriteXOff = HorizontalResolution >> 1; + } + else { + *SpriteXOff = 0; + } + int WidthRatio = 0; + if (MP_WindowCount == 2 && MP_HUD2PSplit == 1) { + WidthRatio = 1; + } + + D3D_RenderHUD_LapTimer( + //(int)((WidthRatio * HorizontalResolution)) - (640 * WidthRatio - XPos), + XPos + (GetExtraSpace() >> WidthRatio), YPos, Time + ); + + *SpriteXOff = tmp; +} + +void D3D_Render2DObject_AlignLeft( + int XPos, int YPos, float ZPos, int XScale, int YScale, int TexPage, + int TexXOff, int TexYOff, int TexWidth, int TexHeight, int TexTint +) { + // We run into clipping issues, so we have to disable our centering hack + // to left-align HUD elements + int *SpriteXOff = (int*)0x72E010; + int tmp = *SpriteXOff; + // Right side of screen? + if (*SpriteXOff + 1 >= (HorizontalResolution >> 1)) { + *SpriteXOff = HorizontalResolution >> 1; + } + else { + *SpriteXOff = 0; + } + + D3D_Render2DObject( + XPos, YPos, ZPos, XScale, YScale, TexPage, + TexXOff, TexYOff, TexWidth, TexHeight, TexTint + ); + + *SpriteXOff = tmp; +} + +void D3D_Render2DObject_AlignRight( + int XPos, int YPos, float ZPos, int XScale, int YScale, int TexPage, + int TexXOff, int TexYOff, int TexWidth, int TexHeight, int TexTint +) { + int *SpriteXOff = (int*)0x72E010; + int tmp = *SpriteXOff; + // Right side of screen? + if (*SpriteXOff + 1 >= (HorizontalResolution >> 1)) { + *SpriteXOff = HorizontalResolution >> 1; + } + else { + *SpriteXOff = 0; + } + int WidthRatio = 0; + if (MP_WindowCount == 2 && MP_HUD2PSplit == 1) { + WidthRatio = 1; + } + + D3D_Render2DObject( + XPos + (GetExtraSpace() >> WidthRatio), + YPos, ZPos, XScale, YScale, TexPage, + TexXOff, TexYOff, TexWidth, TexHeight, TexTint + ); + + *SpriteXOff = tmp; +} + +void D3D_Render2DObject_AlignCenter( + int XPos, int YPos, float ZPos, int XScale, int YScale, int TexPage, + int TexXOff, int TexYOff, int TexWidth, int TexHeight, int TexTint +) { + int *SpriteXOff = (int*)0x72E010; + int tmp = *SpriteXOff; + int HorizOffset = 0; + int MPScaler = 0; + int *XStretch = (int*)0x7BCB88; + int ExpectedXScale = (int)(VerticalResolution * (16.0f / 15.0f)); + bool NeedsHalving = (*XStretch != ExpectedXScale); + + if (*SpriteXOff + 1 >= (HorizontalResolution >> 1)) { + HorizOffset = HorizontalResolution; + } + if ((MP_WindowCount == 2 && MP_HUD2PSplit == 1) || NeedsHalving) { + HorizOffset = HorizOffset >> 2; + MPScaler = 1; // Divide center by 2 + } + + *SpriteXOff = HorizOffset + (tmp >> MPScaler); + D3D_Render2DObject( + XPos, YPos, ZPos, XScale, YScale, TexPage, + TexXOff, TexYOff, TexWidth, TexHeight, TexTint + ); + + *SpriteXOff = tmp; +} + +void D3D_Render2DObject_AlignAuto( + int XPos, int YPos, float ZPos, int XScale, int YScale, int TexPage, + int TexXOff, int TexYOff, int TexWidth, int TexHeight, int TexTint +) { + int CompVal = 320; + if (MP_WindowCount == 2 && MP_HUD2PSplit == 1) { + CompVal = 120; + } + if (XPos < CompVal) { + D3D_Render2DObject_AlignLeft( + XPos, YPos, ZPos, XScale, YScale, TexPage, + TexXOff, TexYOff, TexWidth, TexHeight, TexTint + ); + } + else { + D3D_Render2DObject_AlignRight( + XPos, YPos, ZPos, XScale, YScale, TexPage, + TexXOff, TexYOff, TexWidth, TexHeight, TexTint + ); + } +} + +void Render_SetViewport_FixUp() { + int *XStretch = (int*)0x7BCB88; + int *XOff = (int*)0x7AF248; + // donor address (doesn't seem to effect anything) + int *SpriteXOff = (int*)0x72E010; + float AspRatio = (float)HorizontalResolution / (float)VerticalResolution; + int ExpectedXScale = HorizontalResolution * 0.8; + bool NeedsHalving = (*XStretch != ExpectedXScale); + //float WidthAdjRatio = (4.0f / 3.0f) / AspRatio; + + float InvAspRatio = (float)VerticalResolution / (float)HorizontalResolution; + *XStretch = (int)(VerticalResolution * (16.0f / 15.0f)); + + // Vertical split requires halved aspect ratio + if (NeedsHalving && MP_WindowCount > 1) { + *XStretch /= 2; + } + + // Simplified form of (HorizontalResolution - (VerticalResolution * (4.0f/3.0f))) / 2 + *SpriteXOff = *XOff + (int)(((0.5f) * HorizontalResolution) - ((2.0f / 3.0f) * VerticalResolution)); +} \ No newline at end of file diff --git a/SonicRModLoader/Widescreen.h b/SonicRModLoader/Widescreen.h new file mode 100644 index 0000000..bf899b7 --- /dev/null +++ b/SonicRModLoader/Widescreen.h @@ -0,0 +1,9 @@ +#pragma once + +void D3D_RenderHUD_MainTimer_AlignRight(int XPos, int YPos, float ZPos, int Time, int Unk); +void D3D_RenderHUD_LapTimer_AlignRight(int XPos, int YPos, int Time); +void D3D_Render2DObject_AlignLeft(int XPos, int YPos, float ZPos, int XScale, int YScale, int TexPage, int TexXOff, int TexYOff, int TexWidth, int TexHeight, int TexTint); +void D3D_Render2DObject_AlignRight(int XPos, int YPos, float ZPos, int XScale, int YScale, int TexPage, int TexXOff, int TexYOff, int TexWidth, int TexHeight, int TexTint); +void D3D_Render2DObject_AlignCenter(int XPos, int YPos, float ZPos, int XScale, int YScale, int TexPage, int TexXOff, int TexYOff, int TexWidth, int TexHeight, int TexTint); +void D3D_Render2DObject_AlignAuto(int XPos, int YPos, float ZPos, int XScale, int YScale, int TexPage, int TexXOff, int TexYOff, int TexWidth, int TexHeight, int TexTint); +void Render_SetViewport_FixUp(); diff --git a/SonicRModLoader/dllmain.cpp b/SonicRModLoader/dllmain.cpp index d8f6fb9..61547d3 100644 --- a/SonicRModLoader/dllmain.cpp +++ b/SonicRModLoader/dllmain.cpp @@ -19,6 +19,7 @@ #include "FileReplacement.h" #include "Events.h" #include "Music.h" +#include "Widescreen.h" #include "SonicRModLoader.h" using std::ifstream; @@ -204,189 +205,6 @@ static LRESULT CALLBACK WrapperWndProc(HWND wrapper, UINT uMsg, WPARAM wParam, L return DefWindowProc(wrapper, uMsg, wParam, lParam); } -// Widescreen support -int MapWidthTo640() { - int HorizontalResolution = *(int*)0x5F3874; - int VerticalResolution = *(int*)0x75353C; - float AspRatio = (float)HorizontalResolution / (float)VerticalResolution; - return (int)(480 * AspRatio); -} - -int GetExtraSpace() { - return (MapWidthTo640() - 640); -} - -void D3D_RenderHUD_MainTimer_AlignRight( - int XPos, int YPos, float ZPos, int Time, int Unk -) { - int *SpriteXOff = (int*)0x72E010; - int tmp = *SpriteXOff; - // Right side of screen? - if (*SpriteXOff + 1 >= (HorizontalResolution >> 1)) { - *SpriteXOff = HorizontalResolution >> 1; - } - else { - *SpriteXOff = 0; - } - int WidthRatio = 0; - if (MP_WindowCount == 2 && MP_HUD2PSplit == 1) { - WidthRatio = 1; - } - - D3D_RenderHUD_MainTimer( - XPos + (GetExtraSpace() >> WidthRatio), YPos, ZPos, Time, Unk - ); - - *SpriteXOff = tmp; -} - -void D3D_RenderHUD_LapTimer_AlignRight(int XPos, int YPos, int Time) { - int *SpriteXOff = (int*)0x72E010; - int tmp = *SpriteXOff; - // Right side of screen? - if (*SpriteXOff + 1 >= (HorizontalResolution >> 1)) { - *SpriteXOff = HorizontalResolution >> 1; - } - else { - *SpriteXOff = 0; - } - int WidthRatio = 0; - if (MP_WindowCount == 2 && MP_HUD2PSplit == 1) { - WidthRatio = 1; - } - - D3D_RenderHUD_LapTimer( - //(int)((WidthRatio * HorizontalResolution)) - (640 * WidthRatio - XPos), - XPos + (GetExtraSpace() >> WidthRatio), YPos, Time - ); - - *SpriteXOff = tmp; -} - -void D3D_Render2DObject_AlignLeft( - int XPos, int YPos, float ZPos, int XScale, int YScale, int TexPage, - int TexXOff, int TexYOff, int TexWidth, int TexHeight, int TexTint -) { - // We run into clipping issues, so we have to disable our centering hack - // to left-align HUD elements - int *SpriteXOff = (int*)0x72E010; - int tmp = *SpriteXOff; - // Right side of screen? - if (*SpriteXOff + 1 >= (HorizontalResolution >> 1)) { - *SpriteXOff = HorizontalResolution >> 1; - } - else { - *SpriteXOff = 0; - } - - D3D_Render2DObject( - XPos, YPos, ZPos, XScale, YScale, TexPage, - TexXOff, TexYOff, TexWidth, TexHeight, TexTint - ); - - *SpriteXOff = tmp; -} - -void D3D_Render2DObject_AlignRight( - int XPos, int YPos, float ZPos, int XScale, int YScale, int TexPage, - int TexXOff, int TexYOff, int TexWidth, int TexHeight, int TexTint -) { - int *SpriteXOff = (int*)0x72E010; - int tmp = *SpriteXOff; - // Right side of screen? - if (*SpriteXOff + 1 >= (HorizontalResolution >> 1)) { - *SpriteXOff = HorizontalResolution >> 1; - } - else { - *SpriteXOff = 0; - } - int WidthRatio = 0; - if (MP_WindowCount == 2 && MP_HUD2PSplit == 1) { - WidthRatio = 1; - } - - D3D_Render2DObject( - XPos + (GetExtraSpace() >> WidthRatio), - YPos, ZPos, XScale, YScale, TexPage, - TexXOff, TexYOff, TexWidth, TexHeight, TexTint - ); - - *SpriteXOff = tmp; -} - -void D3D_Render2DObject_AlignCenter( - int XPos, int YPos, float ZPos, int XScale, int YScale, int TexPage, - int TexXOff, int TexYOff, int TexWidth, int TexHeight, int TexTint -) { - int *SpriteXOff = (int*)0x72E010; - int tmp = *SpriteXOff; - int HorizOffset = 0; - int MPScaler = 0; - int *XStretch = (int*)0x7BCB88; - int ExpectedXScale = (int)(VerticalResolution * (16.0f / 15.0f)); - bool NeedsHalving = (*XStretch != ExpectedXScale); - - if (*SpriteXOff + 1 >= (HorizontalResolution >> 1)) { - HorizOffset = HorizontalResolution; - } - if ((MP_WindowCount == 2 && MP_HUD2PSplit == 1) || NeedsHalving) { - HorizOffset = HorizOffset >> 2; - MPScaler = 1; // Divide center by 2 - } - - *SpriteXOff = HorizOffset + (tmp >> MPScaler); - D3D_Render2DObject( - XPos, YPos, ZPos, XScale, YScale, TexPage, - TexXOff, TexYOff, TexWidth, TexHeight, TexTint - ); - - *SpriteXOff = tmp; -} - -void D3D_Render2DObject_AlignAuto( - int XPos, int YPos, float ZPos, int XScale, int YScale, int TexPage, - int TexXOff, int TexYOff, int TexWidth, int TexHeight, int TexTint -) { - int CompVal = 320; - if (MP_WindowCount == 2 && MP_HUD2PSplit == 1) { - CompVal = 120; - } - if (XPos < CompVal) { - D3D_Render2DObject_AlignLeft( - XPos, YPos, ZPos, XScale, YScale, TexPage, - TexXOff, TexYOff, TexWidth, TexHeight, TexTint - ); - } - else { - D3D_Render2DObject_AlignRight( - XPos, YPos, ZPos, XScale, YScale, TexPage, - TexXOff, TexYOff, TexWidth, TexHeight, TexTint - ); - } -} - -void Render_SetViewport_FixUp() { - int *XStretch = (int*)0x7BCB88; - int *XOff = (int*)0x7AF248; - // donor address (doesn't seem to effect anything) - int *SpriteXOff = (int*)0x72E010; - float AspRatio = (float)HorizontalResolution / (float)VerticalResolution; - int ExpectedXScale = HorizontalResolution * 0.8; - bool NeedsHalving = (*XStretch != ExpectedXScale); - //float WidthAdjRatio = (4.0f / 3.0f) / AspRatio; - - float InvAspRatio = (float)VerticalResolution / (float)HorizontalResolution; - *XStretch = (int)(VerticalResolution * (16.0f/15.0f)); - - // Vertical split requires halved aspect ratio - if (NeedsHalving && MP_WindowCount > 1) { - *XStretch /= 2; - } - - // Simplified form of (HorizontalResolution - (VerticalResolution * (4.0f/3.0f))) / 2 - *SpriteXOff = *XOff + (int)(((0.5f) * HorizontalResolution) - ((2.0f / 3.0f) * VerticalResolution)); -} - void __cdecl SetPresentParameters(D3DPRESENT_PARAMETERS *pp, D3DFORMAT bufferFormat, D3DFORMAT depthStencilFormat, int bufferWidth, signed int bufferHeight, int refreshRate, int windowed) { memset(pp, 0, sizeof(D3DPRESENT_PARAMETERS));