diff --git a/Windows_Libs/Dev/.clang-format b/Windows_Libs/Dev/.clang-format new file mode 100644 index 0000000..bb62f30 --- /dev/null +++ b/Windows_Libs/Dev/.clang-format @@ -0,0 +1,13 @@ +# Format Style Options - Created with Clang Power Tools +--- +AccessModifierOffset: -4 +AlignConsecutiveMacros: true +AllowShortFunctionsOnASingleLine: Empty +AllowShortEnumsOnASingleLine: false +AlwaysBreakTemplateDeclarations: Yes +ColumnLimit: 150 +EmptyLineBeforeAccessModifier: Never +IndentAccessModifiers: false +IndentWidth: 4 +NamespaceIndentation: All +BreakBeforeBraces: Allman \ No newline at end of file diff --git a/Windows_Libs/Dev/Input/4J_Input.cpp b/Windows_Libs/Dev/Input/4J_Input.cpp index 571158a..0cd18f5 100644 --- a/Windows_Libs/Dev/Input/4J_Input.cpp +++ b/Windows_Libs/Dev/Input/4J_Input.cpp @@ -1,153 +1,153 @@ #include "4J_Input.h" -#include "INP_Main.h" #include "INP_Keyboard.h" +#include "INP_Main.h" C_4JInput InputManager; CKeyboard InternalKeyboard; void C_4JInput::Initialise(int iInputStateC, unsigned char ucMapC, unsigned char ucActionC, unsigned char ucMenuActionC) { - InternalInputManager.Initialise(iInputStateC, ucMapC, ucActionC, ucMenuActionC); + InternalInputManager.Initialise(iInputStateC, ucMapC, ucActionC, ucMenuActionC); } void C_4JInput::Tick(void) { - InternalInputManager.Tick(); + InternalInputManager.Tick(); } void C_4JInput::SetDeadzoneAndMovementRange(unsigned int uiDeadzone, unsigned int uiMovementRangeMax) { - InternalInputManager.SetDeadzoneAndMovementRange(uiDeadzone, uiMovementRangeMax); + InternalInputManager.SetDeadzoneAndMovementRange(uiDeadzone, uiMovementRangeMax); } void C_4JInput::SetGameJoypadMaps(unsigned char ucMap, unsigned char ucAction, unsigned int uiActionVal) { - InternalInputManager.SetGameJoypadMaps(ucMap, ucAction, uiActionVal); + InternalInputManager.SetGameJoypadMaps(ucMap, ucAction, uiActionVal); } unsigned int C_4JInput::GetGameJoypadMaps(unsigned char ucMap, unsigned char ucAction) { - return InternalInputManager.GetGameJoypadMaps(ucMap, ucAction); + return InternalInputManager.GetGameJoypadMaps(ucMap, ucAction); } void C_4JInput::SetJoypadMapVal(int iPad, unsigned char ucMap) { - InternalInputManager.SetJoypadMapVal(iPad, ucMap); + InternalInputManager.SetJoypadMapVal(iPad, ucMap); } unsigned char C_4JInput::GetJoypadMapVal(int iPad) { - return InternalInputManager.GetJoypadMapVal(iPad); + return InternalInputManager.GetJoypadMapVal(iPad); } void C_4JInput::SetJoypadSensitivity(int iPad, float fSensitivity) { - InternalInputManager.SetJoypadSensitivity(iPad, fSensitivity); + InternalInputManager.SetJoypadSensitivity(iPad, fSensitivity); } unsigned int C_4JInput::GetValue(int iPad, unsigned char ucAction, bool bRepeat) { - return InternalInputManager.GetValue(iPad, ucAction, bRepeat); + return InternalInputManager.GetValue(iPad, ucAction, bRepeat); } bool C_4JInput::ButtonPressed(int iPad, unsigned char ucAction) { - return InternalInputManager.ButtonPressed(iPad, ucAction); + return InternalInputManager.ButtonPressed(iPad, ucAction); } bool C_4JInput::ButtonReleased(int iPad, unsigned char ucAction) { - return InternalInputManager.ButtonReleased(iPad, ucAction); + return InternalInputManager.ButtonReleased(iPad, ucAction); } bool C_4JInput::ButtonDown(int iPad, unsigned char ucAction) { - return InternalInputManager.ButtonDown(iPad, ucAction); + return InternalInputManager.ButtonDown(iPad, ucAction); } void C_4JInput::SetJoypadStickAxisMap(int iPad, unsigned int uiFrom, unsigned int uiTo) { - InternalInputManager.SetJoypadStickAxisMap(iPad, uiFrom, uiTo); + InternalInputManager.SetJoypadStickAxisMap(iPad, uiFrom, uiTo); } void C_4JInput::SetJoypadStickTriggerMap(int iPad, unsigned int uiFrom, unsigned int uiTo) { - InternalInputManager.SetJoypadStickTriggerMap(iPad, uiFrom, uiTo); + InternalInputManager.SetJoypadStickTriggerMap(iPad, uiFrom, uiTo); } void C_4JInput::SetKeyRepeatRate(float fRepeatDelaySecs, float fRepeatRateSecs) { - InternalInputManager.SetKeyRepeatRate(fRepeatDelaySecs, fRepeatRateSecs); + InternalInputManager.SetKeyRepeatRate(fRepeatDelaySecs, fRepeatRateSecs); } -void C_4JInput::SetDebugSequence(const char* chSequenceA, int(*Func)(LPVOID), LPVOID lpParam) +void C_4JInput::SetDebugSequence(const char *chSequenceA, int (*Func)(LPVOID), LPVOID lpParam) { - InternalInputManager.SetDebugSequence(chSequenceA, Func, lpParam); + InternalInputManager.SetDebugSequence(chSequenceA, Func, lpParam); } FLOAT C_4JInput::GetIdleSeconds(int iPad) { - return InternalInputManager.GetIdleSeconds(iPad); + return InternalInputManager.GetIdleSeconds(iPad); } bool C_4JInput::IsPadConnected(int iPad) { - return InternalInputManager.IsPadConnected(iPad); + return InternalInputManager.IsPadConnected(iPad); } float C_4JInput::GetJoypadStick_LX(int iPad, bool bCheckMenuDisplay) { - return InternalInputManager.GetJoypadStick_LX(iPad, bCheckMenuDisplay); + return InternalInputManager.GetJoypadStick_LX(iPad, bCheckMenuDisplay); } float C_4JInput::GetJoypadStick_LY(int iPad, bool bCheckMenuDisplay) { - return InternalInputManager.GetJoypadStick_LY(iPad, bCheckMenuDisplay); + return InternalInputManager.GetJoypadStick_LY(iPad, bCheckMenuDisplay); } float C_4JInput::GetJoypadStick_RX(int iPad, bool bCheckMenuDisplay) { - return InternalInputManager.GetJoypadStick_RX(iPad, bCheckMenuDisplay); + return InternalInputManager.GetJoypadStick_RX(iPad, bCheckMenuDisplay); } float C_4JInput::GetJoypadStick_RY(int iPad, bool bCheckMenuDisplay) { - return InternalInputManager.GetJoypadStick_RY(iPad, bCheckMenuDisplay); + return InternalInputManager.GetJoypadStick_RY(iPad, bCheckMenuDisplay); } unsigned char C_4JInput::GetJoypadLTrigger(int iPad, bool bCheckMenuDisplay) { - return InternalInputManager.GetJoypadLTrigger(iPad, bCheckMenuDisplay); + return InternalInputManager.GetJoypadLTrigger(iPad, bCheckMenuDisplay); } unsigned char C_4JInput::GetJoypadRTrigger(int iPad, bool bCheckMenuDisplay) { - return InternalInputManager.GetJoypadRTrigger(iPad, bCheckMenuDisplay); + return InternalInputManager.GetJoypadRTrigger(iPad, bCheckMenuDisplay); } void C_4JInput::SetMenuDisplayed(int iPad, bool bVal) { - InternalInputManager.SetMenuDisplayed(iPad, bVal); + InternalInputManager.SetMenuDisplayed(iPad, bVal); } -EKeyboardResult C_4JInput::RequestKeyboard(LPCWSTR Title, LPCWSTR Text, DWORD dwPad, UINT uiMaxChars, int(*Func)(LPVOID, const bool), LPVOID lpParam, C_4JInput::EKeyboardMode eMode) +EKeyboardResult C_4JInput::RequestKeyboard(LPCWSTR Title, LPCWSTR Text, DWORD dwPad, UINT uiMaxChars, int (*Func)(LPVOID, const bool), LPVOID lpParam, + C_4JInput::EKeyboardMode eMode) { - return InternalKeyboard.RequestKeyboard(Title, Text, dwPad, uiMaxChars, Func, lpParam, eMode); + return InternalKeyboard.RequestKeyboard(Title, Text, dwPad, uiMaxChars, Func, lpParam, eMode); } -void C_4JInput::GetText(uint16_t* UTF16String) +void C_4JInput::GetText(uint16_t *UTF16String) { - InternalKeyboard.GetText(UTF16String); + InternalKeyboard.GetText(UTF16String); } - -bool C_4JInput::VerifyStrings(WCHAR** pwStringA, int iStringC, int(*Func)(LPVOID, STRING_VERIFY_RESPONSE*), LPVOID lpParam) +bool C_4JInput::VerifyStrings(WCHAR **pwStringA, int iStringC, int (*Func)(LPVOID, STRING_VERIFY_RESPONSE *), LPVOID lpParam) { - return true; + return true; } -void C_4JInput::CancelQueuedVerifyStrings(int(*Func)(LPVOID, STRING_VERIFY_RESPONSE*), LPVOID lpParam) {} +void C_4JInput::CancelQueuedVerifyStrings(int (*Func)(LPVOID, STRING_VERIFY_RESPONSE *), LPVOID lpParam) {} void C_4JInput::CancelAllVerifyInProgress(void) {} -//bool C_4JInput::InputDetected(DWORD dwUserIndex,WCHAR *pwchInput) {} \ No newline at end of file +// bool C_4JInput::InputDetected(DWORD dwUserIndex,WCHAR *pwchInput) {} \ No newline at end of file diff --git a/Windows_Libs/Dev/Input/INP_ForceFeedback.cpp b/Windows_Libs/Dev/Input/INP_ForceFeedback.cpp index d8b5709..181004a 100644 --- a/Windows_Libs/Dev/Input/INP_ForceFeedback.cpp +++ b/Windows_Libs/Dev/Input/INP_ForceFeedback.cpp @@ -2,7 +2,7 @@ void CForceFeedback::Initialise(int iInputStateC, unsigned char ucMapC, unsigned char ucActionC, unsigned char ucMenuActionC) { - m_Effects = new FF_EFFECT[MAX_EFFECTS]; + m_Effects = new FF_EFFECT[MAX_EFFECTS]; m_EffectC = 0; LARGE_INTEGER qwTicksPerSec; @@ -12,16 +12,13 @@ void CForceFeedback::Initialise(int iInputStateC, unsigned char ucMapC, unsigned m_unkBool = 0; } -void CForceFeedback::RumbleEnable(int iQuadrant, bool bRumbleEnabled) -{ - -} +void CForceFeedback::RumbleEnable(int iQuadrant, bool bRumbleEnabled) {} void CForceFeedback::CreateEffect(void) { - assert(m_EffectCm_uiDurationMs = 5; pEffect->m_uiMagnitude = 500; pEffect->m_uiEffectType = 0; @@ -33,18 +30,18 @@ void CForceFeedback::CreateEffect(void) void CForceFeedback::AddEffectInstance(unsigned int uiEffect) { - FF_EFFECT_INSTANCE* pEffectInst = new FF_EFFECT_INSTANCE(); - pEffectInst->m_Effect = &m_Effects[uiEffect]; + FF_EFFECT_INSTANCE *pEffectInst = new FF_EFFECT_INSTANCE(); + pEffectInst->m_Effect = &m_Effects[uiEffect]; pEffectInst->m_uiInst = 0; - m_EffectList.AddToTail(pEffectInst); + m_EffectList.AddToTail(pEffectInst); } void CForceFeedback::SetEffectInstanceState(unsigned int uiEffectInstance, unsigned int uiState) { int iCount = 0; - LinkedList::_LL_NODE* pNode; - for (pNode = m_EffectList.m_Head; ; pNode = pNode->m_Next) + LinkedList::_LL_NODE *pNode; + for (pNode = m_EffectList.m_Head;; pNode = pNode->m_Next) { if ((iCount++) == uiEffectInstance) break; @@ -52,34 +49,28 @@ void CForceFeedback::SetEffectInstanceState(unsigned int uiEffectInstance, unsig pNode->GetDataAs()->m_bIsActive = true; } -void CForceFeedback::ProcessEventInstance(FF_EFFECT_INSTANCE* pEffectInst) -{ +void CForceFeedback::ProcessEventInstance(FF_EFFECT_INSTANCE *pEffectInst) {} -} - -void CForceFeedback::PauseEventInstance(FF_EFFECT_INSTANCE* pEffectInst) -{ - -} +void CForceFeedback::PauseEventInstance(FF_EFFECT_INSTANCE *pEffectInst) {} void CForceFeedback::AddRumble(unsigned int uiPad, WORD leftMotorSpeed, WORD rightMotorSpeed, float fSeconds) { - RUMBLE_EFFECT* pRumble = new RUMBLE_EFFECT(); + RUMBLE_EFFECT *pRumble = new RUMBLE_EFFECT(); - LARGE_INTEGER qwCurrentTime; - QueryPerformanceCounter(&qwCurrentTime); + LARGE_INTEGER qwCurrentTime; + QueryPerformanceCounter(&qwCurrentTime); - pRumble->m_TimeLeft = (m_TicksPerSecond * fSeconds) + qwCurrentTime.QuadPart; - pRumble->m_RumbleData.wLeftMotorSpeed = leftMotorSpeed; - pRumble->m_RumbleData.wRightMotorSpeed = rightMotorSpeed; - pRumble->m_Pad = uiPad; + pRumble->m_TimeLeft = (m_TicksPerSecond * fSeconds) + qwCurrentTime.QuadPart; + pRumble->m_RumbleData.wLeftMotorSpeed = leftMotorSpeed; + pRumble->m_RumbleData.wRightMotorSpeed = rightMotorSpeed; + pRumble->m_Pad = uiPad; - m_RumbleList.AddToTail(pRumble); + m_RumbleList.AddToTail(pRumble); } void CForceFeedback::Tick(void) { - for (LinkedList::_LL_NODE* pEffectNode = m_EffectList.m_Head; pEffectNode; pEffectNode = pEffectNode->m_Next) + for (LinkedList::_LL_NODE *pEffectNode = m_EffectList.m_Head; pEffectNode; pEffectNode = pEffectNode->m_Next) { if (pEffectNode->GetDataAs()->m_bIsActive) { @@ -90,21 +81,21 @@ void CForceFeedback::Tick(void) LARGE_INTEGER qwCurrentTime; QueryPerformanceCounter(&qwCurrentTime); - LinkedList::_LL_NODE* pRumbleNode = m_RumbleList.m_Head; + LinkedList::_LL_NODE *pRumbleNode = m_RumbleList.m_Head; while (pRumbleNode) { if (pRumbleNode->GetDataAs()->m_TimeLeft <= qwCurrentTime.QuadPart) { - RUMBLE_EFFECT* pRumble = pRumbleNode->GetDataAs(); + RUMBLE_EFFECT *pRumble = pRumbleNode->GetDataAs(); pRumble->m_RumbleData.wLeftMotorSpeed = 0; pRumble->m_RumbleData.wRightMotorSpeed = 0; XInputSetState(pRumble->m_Pad, &pRumble->m_RumbleData); - m_RumbleList.RemoveNode(pRumbleNode); + m_RumbleList.RemoveNode(pRumbleNode); - LinkedList::_LL_NODE* pTemp = pRumbleNode; + LinkedList::_LL_NODE *pTemp = pRumbleNode; pRumbleNode = pRumbleNode->m_Next; delete pTemp->m_pvData; diff --git a/Windows_Libs/Dev/Input/INP_ForceFeedback.h b/Windows_Libs/Dev/Input/INP_ForceFeedback.h index 404b662..008e8b1 100644 --- a/Windows_Libs/Dev/Input/INP_ForceFeedback.h +++ b/Windows_Libs/Dev/Input/INP_ForceFeedback.h @@ -6,56 +6,55 @@ class CForceFeedback { public: + enum FF_PARAMS + { + FF_PARAMS_RAMP, + FF_PARAMS_PERIODIC + }; - enum FF_PARAMS - { - FF_PARAMS_RAMP, - FF_PARAMS_PERIODIC - }; + struct RUMBLE_EFFECT + { + DWORD m_Pad; + uint64_t m_TimeLeft; + XINPUT_VIBRATION m_RumbleData; + WORD unk; + }; - struct RUMBLE_EFFECT - { - DWORD m_Pad; - uint64_t m_TimeLeft; - XINPUT_VIBRATION m_RumbleData; - WORD unk; - }; + struct FF_EFFECT + { + unsigned int m_uiEffectType; + unsigned int m_uiDurationMs; + BYTE gap8[4]; + unsigned int m_uiMagnitude; + BYTE gap10[8]; + DWORD dword18; + DWORD dword1C; + }; - struct FF_EFFECT - { - unsigned int m_uiEffectType; - unsigned int m_uiDurationMs; - BYTE gap8[4]; - unsigned int m_uiMagnitude; - BYTE gap10[8]; - DWORD dword18; - DWORD dword1C; - }; + struct FF_EFFECT_INSTANCE + { + bool m_bIsActive; + unsigned int m_uiInst; + CForceFeedback::FF_EFFECT *m_Effect; + }; - struct FF_EFFECT_INSTANCE - { - bool m_bIsActive; - unsigned int m_uiInst; - CForceFeedback::FF_EFFECT* m_Effect; - }; + void Initialise(int iInputStateC, unsigned char ucMapC, unsigned char ucActionC, unsigned char ucMenuActionC); + void RumbleEnable(int iQuadrant, bool bRumbleEnabled); + void CreateEffect(void); + void AddEffectInstance(unsigned int uiEffect); + void SetEffectInstanceState(unsigned int uiEffectInstance, unsigned int uiState); - void Initialise(int iInputStateC, unsigned char ucMapC, unsigned char ucActionC, unsigned char ucMenuActionC); - void RumbleEnable(int iQuadrant, bool bRumbleEnabled); - void CreateEffect(void); - void AddEffectInstance(unsigned int uiEffect); - void SetEffectInstanceState(unsigned int uiEffectInstance, unsigned int uiState); + void ProcessEventInstance(FF_EFFECT_INSTANCE *pEffectInst); + void PauseEventInstance(FF_EFFECT_INSTANCE *pEffectInst); - void ProcessEventInstance(FF_EFFECT_INSTANCE* pEffectInst); - void PauseEventInstance(FF_EFFECT_INSTANCE* pEffectInst); + void AddRumble(unsigned int uiPad, WORD leftMotorSpeed, WORD rightMotorSpeed, float fSeconds); - void AddRumble(unsigned int uiPad, WORD leftMotorSpeed, WORD rightMotorSpeed, float fSeconds); + void Tick(void); - void Tick(void); - - LinkedList m_EffectList; - FF_EFFECT* m_Effects; - int m_EffectC; - LinkedList m_RumbleList; - float m_TicksPerSecond; - bool m_unkBool; + LinkedList m_EffectList; + FF_EFFECT *m_Effects; + int m_EffectC; + LinkedList m_RumbleList; + float m_TicksPerSecond; + bool m_unkBool; }; \ No newline at end of file diff --git a/Windows_Libs/Dev/Input/INP_Keyboard.cpp b/Windows_Libs/Dev/Input/INP_Keyboard.cpp index 7ccc128..6c3f55d 100644 --- a/Windows_Libs/Dev/Input/INP_Keyboard.cpp +++ b/Windows_Libs/Dev/Input/INP_Keyboard.cpp @@ -1,24 +1,22 @@ #include "INP_Keyboard.h" -void CKeyboard::Tick(void) -{ +void CKeyboard::Tick(void) {} +EKeyboardResult CKeyboard::RequestKeyboard(LPCWSTR Title, LPCWSTR Text, DWORD dwPad, UINT uiMaxChars, int (*Func)(LPVOID, const bool), LPVOID lpParam, + C_4JInput::EKeyboardMode eMode) +{ + ClearGlobalText(); + SeedEditBox(); + Func(lpParam, C_4JInput::EKeyboardMode_Numeric); + + return EKeyboard_ResultAccept; } -EKeyboardResult CKeyboard::RequestKeyboard(LPCWSTR Title, LPCWSTR Text, DWORD dwPad, UINT uiMaxChars, int(*Func)(LPVOID, const bool), LPVOID lpParam, C_4JInput::EKeyboardMode eMode) -{ - ClearGlobalText(); - SeedEditBox(); - Func(lpParam, C_4JInput::EKeyboardMode_Numeric); - - return EKeyboard_ResultAccept; -} - -void CKeyboard::GetText(uint16_t* UTF16String) +void CKeyboard::GetText(uint16_t *UTF16String) { - uint16_t* keyString = GetGlobalText(); - for (unsigned int index = 0; keyString[index] && index < 0x200; index++) - { - UTF16String[index] = keyString[index]; - } + uint16_t *keyString = GetGlobalText(); + for (unsigned int index = 0; keyString[index] && index < 0x200; index++) + { + UTF16String[index] = keyString[index]; + } } \ No newline at end of file diff --git a/Windows_Libs/Dev/Input/INP_Keyboard.h b/Windows_Libs/Dev/Input/INP_Keyboard.h index 37648a7..37c7308 100644 --- a/Windows_Libs/Dev/Input/INP_Keyboard.h +++ b/Windows_Libs/Dev/Input/INP_Keyboard.h @@ -2,28 +2,25 @@ #include "4J_Input.h" void ClearGlobalText(); -uint16_t* GetGlobalText(); +uint16_t *GetGlobalText(); void SeedEditBox(); class CKeyboard { public: + enum EKeyboardProcessState + { + }; - enum EKeyboardProcessState - { + struct SKeyboardInfo + { + }; - }; + void Tick(void); - struct SKeyboardInfo - { + EKeyboardResult RequestKeyboard(LPCWSTR Title, LPCWSTR Text, DWORD dwPad, UINT uiMaxChars, int (*Func)(LPVOID, const bool), LPVOID lpParam, + C_4JInput::EKeyboardMode eMode); + void GetText(uint16_t *UTF16String); - }; - - void Tick(void); - - EKeyboardResult RequestKeyboard(LPCWSTR Title, LPCWSTR Text, DWORD dwPad, UINT uiMaxChars, int(*Func)(LPVOID, const bool), LPVOID lpParam, C_4JInput::EKeyboardMode eMode); - void GetText(uint16_t* UTF16String); - - - BYTE gap0[0x68]; + BYTE gap0[0x68]; }; \ No newline at end of file diff --git a/Windows_Libs/Dev/Input/INP_Main.cpp b/Windows_Libs/Dev/Input/INP_Main.cpp index 6dfa3ff..1c109c3 100644 --- a/Windows_Libs/Dev/Input/INP_Main.cpp +++ b/Windows_Libs/Dev/Input/INP_Main.cpp @@ -4,733 +4,735 @@ CInput InternalInputManager; CInput::CInput() { - m_ForceFeedback = CForceFeedback(); - m_Keyboard = CKeyboard(); + m_ForceFeedback = CForceFeedback(); + m_Keyboard = CKeyboard(); - m_uiSigninJoypadMask = 0; - for (int i = 0; i < 4; ++i) - m_bIsMenuDisplayed[i] = 0; + m_uiSigninJoypadMask = 0; + for (int i = 0; i < 4; ++i) + m_bIsMenuDisplayed[i] = 0; - InitTime(); + InitTime(); - m_fRepeatDelaySecs = 0.5f; - m_fRepeatRateSecs = 0.8f; - m_sDebugSequenceName = 0; - m_uiDebugSequenceIndex = 0; - m_pDebugSequenceFn = 0; + m_fRepeatDelaySecs = 0.5f; + m_fRepeatRateSecs = 0.8f; + m_sDebugSequenceName = 0; + m_uiDebugSequenceIndex = 0; + m_pDebugSequenceFn = 0; } void CInput::Initialise(int iInputStateC, unsigned char ucMapC, unsigned char ucActionC, unsigned char ucMenuActionC) { - assert(iInputStateC>0); - assert(ucMapC>0); - assert(ucActionC>0); + assert(iInputStateC > 0); + assert(ucMapC > 0); + assert(ucActionC > 0); - ZeroMemory(m_Joypads, sizeof(m_Joypads)); + ZeroMemory(m_Joypads, sizeof(m_Joypads)); - m_ucInputStateC = iInputStateC; + m_ucInputStateC = iInputStateC; - m_iDeadzone = 0x2710; - m_iMovementRangeMax = 0x7FFF; + m_iDeadzone = 0x2710; + m_iMovementRangeMax = 0x7FFF; - m_iEffectiveRange = m_iMovementRangeMax - m_iDeadzone; - m_iHalfRange = m_iEffectiveRange / 2; - m_fEffectiveRange = (float)m_iEffectiveRange; + m_iEffectiveRange = m_iMovementRangeMax - m_iDeadzone; + m_iHalfRange = m_iEffectiveRange / 2; + m_fEffectiveRange = (float)m_iEffectiveRange; - for (int i = 0; i < MAX_JOYPADS; i++) - { - m_Joypads[i].m_pInputStates = new XINPUT_STATE[m_ucInputStateC]; - m_Joypads[i].m_ucInputStateIndex = -1; - m_Joypads[i].m_pfLeftThumbXAxisMap = &m_Joypads[i].m_fNormalizedLeftThumbX; - m_Joypads[i].m_pfLeftThumbYAxisMap = &m_Joypads[i].m_fNormalizedLeftThumbY; - m_Joypads[i].m_pfRightThumbXAxisMap = &m_Joypads[i].m_fNormalizedRightThumbX; - m_Joypads[i].m_pfRightThumbYAxisMap = &m_Joypads[i].m_fNormalizedRightThumbY; - m_Joypads[i].m_pucLeftTriggerAxisMap = &m_Joypads[i].m_ucLeftTriggerState; - m_Joypads[i].m_pucRightTriggerAxisMap = &m_Joypads[i].m_ucRightTriggerState; - m_Joypads[i].m_fSensitivity = 1.0f; - } + for (int i = 0; i < MAX_JOYPADS; i++) + { + m_Joypads[i].m_pInputStates = new XINPUT_STATE[m_ucInputStateC]; + m_Joypads[i].m_ucInputStateIndex = -1; + m_Joypads[i].m_pfLeftThumbXAxisMap = &m_Joypads[i].m_fNormalizedLeftThumbX; + m_Joypads[i].m_pfLeftThumbYAxisMap = &m_Joypads[i].m_fNormalizedLeftThumbY; + m_Joypads[i].m_pfRightThumbXAxisMap = &m_Joypads[i].m_fNormalizedRightThumbX; + m_Joypads[i].m_pfRightThumbYAxisMap = &m_Joypads[i].m_fNormalizedRightThumbY; + m_Joypads[i].m_pucLeftTriggerAxisMap = &m_Joypads[i].m_ucLeftTriggerState; + m_Joypads[i].m_pucRightTriggerAxisMap = &m_Joypads[i].m_ucRightTriggerState; + m_Joypads[i].m_fSensitivity = 1.0f; + } - m_JoypadMap = (unsigned int**)operator new[](ucMapC * sizeof(int*)); - for (int i = 0; i < ucMapC; i++) - { - m_JoypadMap[i] = (unsigned int*)operator new[](ucActionC * sizeof(int)); - } + m_JoypadMap = (unsigned int **)operator new[](ucMapC * sizeof(int *)); + for (int i = 0; i < ucMapC; i++) + { + m_JoypadMap[i] = (unsigned int *)operator new[](ucActionC * sizeof(int)); + } - m_ucJoypadMapC = ucMapC; - m_ucJoypadMapActionC = ucActionC; - m_ucMenuActionC = ucMenuActionC; - m_bJoypadMapArrayIsSetup = false; + m_ucJoypadMapC = ucMapC; + m_ucJoypadMapActionC = ucActionC; + m_ucMenuActionC = ucMenuActionC; + m_bJoypadMapArrayIsSetup = false; - m_ForceFeedback.Initialise(iInputStateC, ucMapC, ucActionC, ucMenuActionC); + m_ForceFeedback.Initialise(iInputStateC, ucMapC, ucActionC, ucMenuActionC); } void CInput::Tick(void) { - UpdateJoypads(); - m_ForceFeedback.Tick(); - m_Keyboard.Tick(); + UpdateJoypads(); + m_ForceFeedback.Tick(); + m_Keyboard.Tick(); } -void CInput::SetJoypadValues(JOYPAD* pThisPad) +void CInput::SetJoypadValues(JOYPAD *pThisPad) { - XINPUT_GAMEPAD* pGamePad = &pThisPad->m_pInputStates[pThisPad->m_ucInputStateIndex].Gamepad; + XINPUT_GAMEPAD *pGamePad = &pThisPad->m_pInputStates[pThisPad->m_ucInputStateIndex].Gamepad; - if (pGamePad->bLeftTrigger > 0x7Fu) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_LT; - } - if (pGamePad->bRightTrigger > 0x7Fu) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_RT; - } + if (pGamePad->bLeftTrigger > 0x7Fu) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_LT; + } + if (pGamePad->bRightTrigger > 0x7Fu) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_RT; + } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_RB; - } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_LB; - } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_RB; + } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_LB; + } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_A) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_A; - } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_B) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_B; - } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_A) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_A; + } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_B) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_B; + } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_X) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_X; - } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_Y) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_Y; - } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_X) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_X; + } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_Y) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_Y; + } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_START) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_START; - } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_BACK) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_BACK; - } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_START) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_START; + } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_BACK) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_BACK; + } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_LEFT_THUMB) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_RTHUMB; - } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_LTHUMB; - } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_LEFT_THUMB) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_RTHUMB; + } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_LTHUMB; + } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_DPAD_UP) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_DPAD_UP; - } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_DPAD_DOWN; - } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_DPAD_LEFT; - } - if ((pGamePad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) != 0) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_DPAD_RIGHT; - } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_DPAD_UP) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_DPAD_UP; + } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_DPAD_DOWN; + } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_DPAD_LEFT; + } + if ((pGamePad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) != 0) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_DPAD_RIGHT; + } - if (pGamePad->sThumbLX > m_iHalfRange + m_iDeadzone) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_LSTICK_RIGHT; - } - if (pGamePad->sThumbLX < -m_iDeadzone - m_iHalfRange) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_LSTICK_LEFT; - } + if (pGamePad->sThumbLX > m_iHalfRange + m_iDeadzone) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_LSTICK_RIGHT; + } + if (pGamePad->sThumbLX < -m_iDeadzone - m_iHalfRange) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_LSTICK_LEFT; + } - if (pGamePad->sThumbRY < -m_iDeadzone - m_iHalfRange) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_RSTICK_DOWN; - } - if (pGamePad->sThumbRY > m_iHalfRange + m_iDeadzone) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_RSTICK_UP; - } - if (pGamePad->sThumbRX > m_iHalfRange + m_iDeadzone) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_RSTICK_RIGHT; - } - if (pGamePad->sThumbRX < -m_iDeadzone - m_iHalfRange) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_RSTICK_LEFT; - } + if (pGamePad->sThumbRY < -m_iDeadzone - m_iHalfRange) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_RSTICK_DOWN; + } + if (pGamePad->sThumbRY > m_iHalfRange + m_iDeadzone) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_RSTICK_UP; + } + if (pGamePad->sThumbRX > m_iHalfRange + m_iDeadzone) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_RSTICK_RIGHT; + } + if (pGamePad->sThumbRX < -m_iDeadzone - m_iHalfRange) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_RSTICK_LEFT; + } - if (pGamePad->sThumbLY < -m_iDeadzone - m_iHalfRange) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_LSTICK_DOWN; - } - if (pGamePad->sThumbLY > m_iHalfRange + m_iDeadzone) - { - pThisPad->m_uiButtons |= _360_JOY_BUTTON_LSTICK_UP; - } + if (pGamePad->sThumbLY < -m_iDeadzone - m_iHalfRange) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_LSTICK_DOWN; + } + if (pGamePad->sThumbLY > m_iHalfRange + m_iDeadzone) + { + pThisPad->m_uiButtons |= _360_JOY_BUTTON_LSTICK_UP; + } - pThisPad->m_ucLeftTriggerState = pGamePad->bLeftTrigger; - pThisPad->m_ucRightTriggerState = pGamePad->bRightTrigger; + pThisPad->m_ucLeftTriggerState = pGamePad->bLeftTrigger; + pThisPad->m_ucRightTriggerState = pGamePad->bRightTrigger; - if (pGamePad->sThumbRX <= m_iDeadzone) - { - if (pGamePad->sThumbRX >= -m_iDeadzone) - pThisPad->m_iRightThumbX = 0; - else - pThisPad->m_iRightThumbX = m_iDeadzone + pGamePad->sThumbRX; - } - else - { - pThisPad->m_iRightThumbX = pGamePad->sThumbRX - m_iDeadzone; - } - if (pGamePad->sThumbRY <= m_iDeadzone) - { - if (pGamePad->sThumbRY >= -m_iDeadzone) - pThisPad->m_iRightThumbY = 0; - else - pThisPad->m_iRightThumbY = m_iDeadzone + pGamePad->sThumbRY; - } - else - { - pThisPad->m_iRightThumbY = pGamePad->sThumbRY - m_iDeadzone; - } - if (pGamePad->sThumbLX <= m_iDeadzone) - { - if (pGamePad->sThumbLX >= -m_iDeadzone) - pThisPad->m_iLeftThumbX = 0; - else - pThisPad->m_iLeftThumbX = m_iDeadzone + pGamePad->sThumbLX; - } - else - { - pThisPad->m_iLeftThumbX = pGamePad->sThumbLX - m_iDeadzone; - } - if (pGamePad->sThumbLY <= m_iDeadzone) - { - if (pGamePad->sThumbLY >= -m_iDeadzone) - pThisPad->m_iLeftThumbY = 0; - else - pThisPad->m_iLeftThumbY = m_iDeadzone + pGamePad->sThumbLY; - } - else - { - pThisPad->m_iLeftThumbY = pGamePad->sThumbLY - m_iDeadzone; - } + if (pGamePad->sThumbRX <= m_iDeadzone) + { + if (pGamePad->sThumbRX >= -m_iDeadzone) + pThisPad->m_iRightThumbX = 0; + else + pThisPad->m_iRightThumbX = m_iDeadzone + pGamePad->sThumbRX; + } + else + { + pThisPad->m_iRightThumbX = pGamePad->sThumbRX - m_iDeadzone; + } + if (pGamePad->sThumbRY <= m_iDeadzone) + { + if (pGamePad->sThumbRY >= -m_iDeadzone) + pThisPad->m_iRightThumbY = 0; + else + pThisPad->m_iRightThumbY = m_iDeadzone + pGamePad->sThumbRY; + } + else + { + pThisPad->m_iRightThumbY = pGamePad->sThumbRY - m_iDeadzone; + } + if (pGamePad->sThumbLX <= m_iDeadzone) + { + if (pGamePad->sThumbLX >= -m_iDeadzone) + pThisPad->m_iLeftThumbX = 0; + else + pThisPad->m_iLeftThumbX = m_iDeadzone + pGamePad->sThumbLX; + } + else + { + pThisPad->m_iLeftThumbX = pGamePad->sThumbLX - m_iDeadzone; + } + if (pGamePad->sThumbLY <= m_iDeadzone) + { + if (pGamePad->sThumbLY >= -m_iDeadzone) + pThisPad->m_iLeftThumbY = 0; + else + pThisPad->m_iLeftThumbY = m_iDeadzone + pGamePad->sThumbLY; + } + else + { + pThisPad->m_iLeftThumbY = pGamePad->sThumbLY - m_iDeadzone; + } - pThisPad->m_iNormalizedLeftThumbX = pThisPad->m_iLeftThumbX / m_iEffectiveRange; - pThisPad->m_iNormalizedLeftThumbY = pThisPad->m_iLeftThumbY / m_iEffectiveRange; - pThisPad->m_iNormalizedRightThumbX = pThisPad->m_iRightThumbX / m_iEffectiveRange; - pThisPad->m_iNormalizedRightThumbY = pThisPad->m_iRightThumbY / m_iEffectiveRange; + pThisPad->m_iNormalizedLeftThumbX = pThisPad->m_iLeftThumbX / m_iEffectiveRange; + pThisPad->m_iNormalizedLeftThumbY = pThisPad->m_iLeftThumbY / m_iEffectiveRange; + pThisPad->m_iNormalizedRightThumbX = pThisPad->m_iRightThumbX / m_iEffectiveRange; + pThisPad->m_iNormalizedRightThumbY = pThisPad->m_iRightThumbY / m_iEffectiveRange; - pThisPad->m_fNormalizedLeftThumbX = pThisPad->m_iLeftThumbX / m_fEffectiveRange; - pThisPad->m_fNormalizedLeftThumbY = pThisPad->m_iLeftThumbY / m_fEffectiveRange; - pThisPad->m_fNormalizedRightThumbX = pThisPad->m_iRightThumbX / m_fEffectiveRange; - pThisPad->m_fNormalizedRightThumbY = pThisPad->m_iRightThumbY / m_fEffectiveRange; + pThisPad->m_fNormalizedLeftThumbX = pThisPad->m_iLeftThumbX / m_fEffectiveRange; + pThisPad->m_fNormalizedLeftThumbY = pThisPad->m_iLeftThumbY / m_fEffectiveRange; + pThisPad->m_fNormalizedRightThumbX = pThisPad->m_iRightThumbX / m_fEffectiveRange; + pThisPad->m_fNormalizedRightThumbY = pThisPad->m_iRightThumbY / m_fEffectiveRange; } void CInput::SetDeadzoneAndMovementRange(unsigned int uiDeadzone, unsigned int uiMovementRangeMax) { - m_iDeadzone = uiDeadzone; - m_iMovementRangeMax = uiMovementRangeMax; - m_iEffectiveRange = uiMovementRangeMax - uiDeadzone; - m_iHalfRange = m_iEffectiveRange / 2; + m_iDeadzone = uiDeadzone; + m_iMovementRangeMax = uiMovementRangeMax; + m_iEffectiveRange = uiMovementRangeMax - uiDeadzone; + m_iHalfRange = m_iEffectiveRange / 2; } void CInput::SetJoypadSensitivity(int iPad, float fSensitivity) { - m_Joypads[iPad].m_fSensitivity = fSensitivity; + m_Joypads[iPad].m_fSensitivity = fSensitivity; } void CInput::SetGameJoypadMaps(unsigned char ucMap, unsigned char ucAction, unsigned int uiActionVal) { - assert(ucMap this->m_ucMenuActionC) - { - return 0; - } + if (m_bIsMenuDisplayed[iPad] && ucAction > this->m_ucMenuActionC) + { + return 0; + } - unsigned char ucMap = this->m_Joypads[iPad].m_ucMappingValue; - unsigned int uiVal = this->m_JoypadMap[ucMap][ucAction] & this->m_Joypads[iPad].m_uiButtons; - if (!uiVal || !bRepeat) - { - return uiVal; - } + unsigned char ucMap = this->m_Joypads[iPad].m_ucMappingValue; + unsigned int uiVal = this->m_JoypadMap[ucMap][ucAction] & this->m_Joypads[iPad].m_uiButtons; + if (!uiVal || !bRepeat) + { + return uiVal; + } - int iKeyVal = 0; - for (int iKey = this->m_JoypadMap[ucMap][ucAction]; (iKey & 1) == 0; iKey >>= 1) - { - iKeyVal++; - } + int iKeyVal = 0; + for (int iKey = this->m_JoypadMap[ucMap][ucAction]; (iKey & 1) == 0; iKey >>= 1) + { + iKeyVal++; + } - if (m_fRepeatDelaySecs <= m_Timers[iPad][iKeyVal].m_fTotalHoldTime || m_Timers[iPad][iKeyVal].m_bInitialPressHandled) - { - if (m_Timers[iPad][iKeyVal].m_fTotalHoldTime < m_fRepeatDelaySecs) - { - return 0; - } - else if (m_Timers[iPad][iKeyVal].m_fLastRepeatTime == 0.0f) - { - m_Timers[iPad][iKeyVal].m_fLastRepeatTime = m_fRepeatDelaySecs; - return uiVal; - } - else if ((m_Timers[iPad][iKeyVal].m_fTotalHoldTime - m_Timers[iPad][iKeyVal].m_fLastRepeatTime) <= m_fRepeatRateSecs) - { - return 0; - } - else - { - m_Timers[iPad][iKeyVal].m_fLastRepeatTime = m_Timers[iPad][iKeyVal].m_fLastRepeatTime + m_fRepeatRateSecs; - return uiVal; - } - } - else - { - m_Timers[iPad][iKeyVal].m_bInitialPressHandled = true; - return uiVal; - } + if (m_fRepeatDelaySecs <= m_Timers[iPad][iKeyVal].m_fTotalHoldTime || m_Timers[iPad][iKeyVal].m_bInitialPressHandled) + { + if (m_Timers[iPad][iKeyVal].m_fTotalHoldTime < m_fRepeatDelaySecs) + { + return 0; + } + else if (m_Timers[iPad][iKeyVal].m_fLastRepeatTime == 0.0f) + { + m_Timers[iPad][iKeyVal].m_fLastRepeatTime = m_fRepeatDelaySecs; + return uiVal; + } + else if ((m_Timers[iPad][iKeyVal].m_fTotalHoldTime - m_Timers[iPad][iKeyVal].m_fLastRepeatTime) <= m_fRepeatRateSecs) + { + return 0; + } + else + { + m_Timers[iPad][iKeyVal].m_fLastRepeatTime = m_Timers[iPad][iKeyVal].m_fLastRepeatTime + m_fRepeatRateSecs; + return uiVal; + } + } + else + { + m_Timers[iPad][iKeyVal].m_bInitialPressHandled = true; + return uiVal; + } } bool CInput::IsSet(int iPad, unsigned char ucAction) { - return (!m_bIsMenuDisplayed[iPad] || ucAction <= m_ucMenuActionC) && (m_JoypadMap[m_Joypads[iPad].m_ucMappingValue][ucAction] & m_Joypads[iPad].m_uiButtons) != 0; + return (!m_bIsMenuDisplayed[iPad] || ucAction <= m_ucMenuActionC) && + (m_JoypadMap[m_Joypads[iPad].m_ucMappingValue][ucAction] & m_Joypads[iPad].m_uiButtons) != 0; } bool CInput::ButtonPressed(int iPad, unsigned char ucAction) { - if (ucAction == 255) - { - return m_Joypads[iPad].m_uiButtonsPressed != 0; - } + if (ucAction == 255) + { + return m_Joypads[iPad].m_uiButtonsPressed != 0; + } - if (m_bIsMenuDisplayed[iPad] && ucAction > m_ucMenuActionC) - { - return 0; - } + if (m_bIsMenuDisplayed[iPad] && ucAction > m_ucMenuActionC) + { + return 0; + } - return (m_JoypadMap[m_Joypads[iPad].m_ucMappingValue][ucAction] & m_Joypads[iPad].m_uiButtonsPressed) != 0; + return (m_JoypadMap[m_Joypads[iPad].m_ucMappingValue][ucAction] & m_Joypads[iPad].m_uiButtonsPressed) != 0; } bool CInput::ButtonReleased(int iPad, unsigned char ucAction) { - return (!m_bIsMenuDisplayed[iPad] || ucAction <= m_ucMenuActionC) && (m_JoypadMap[m_Joypads[iPad].m_ucMappingValue][ucAction] & m_Joypads[iPad].m_uiButtonsReleased) != 0; + return (!m_bIsMenuDisplayed[iPad] || ucAction <= m_ucMenuActionC) && + (m_JoypadMap[m_Joypads[iPad].m_ucMappingValue][ucAction] & m_Joypads[iPad].m_uiButtonsReleased) != 0; } bool CInput::ButtonDown(int iPad, unsigned char ucAction) { - if (ucAction == 255) - { - return m_Joypads[iPad].m_uiButtons != 0; - } + if (ucAction == 255) + { + return m_Joypads[iPad].m_uiButtons != 0; + } - if (m_bIsMenuDisplayed[iPad] && ucAction > m_ucMenuActionC) - { - return 0; - } + if (m_bIsMenuDisplayed[iPad] && ucAction > m_ucMenuActionC) + { + return 0; + } - return (m_JoypadMap[m_Joypads[iPad].m_ucMappingValue][ucAction] & m_Joypads[iPad].m_uiButtons) != 0; + return (m_JoypadMap[m_Joypads[iPad].m_ucMappingValue][ucAction] & m_Joypads[iPad].m_uiButtons) != 0; } float CInput::GetJoypadStick_Menu_LX(unsigned char ucPad) { - return m_Joypads[ucPad].m_iNormalizedLeftThumbX; + return m_Joypads[ucPad].m_iNormalizedLeftThumbX; } float CInput::GetJoypadStick_Menu_LY(unsigned char ucPad) { - return m_Joypads[ucPad].m_iNormalizedLeftThumbY; + return m_Joypads[ucPad].m_iNormalizedLeftThumbY; } float CInput::GetJoypadStick_Menu_RX(unsigned char ucPad) { - return m_Joypads[ucPad].m_iNormalizedRightThumbX; + return m_Joypads[ucPad].m_iNormalizedRightThumbX; } float CInput::GetJoypadStick_Menu_RY(unsigned char ucPad) { - return m_Joypads[ucPad].m_iNormalizedRightThumbY; + return m_Joypads[ucPad].m_iNormalizedRightThumbY; } unsigned char CInput::GetJoypadLTrigger_Menu(unsigned char ucPad) { - return m_Joypads[ucPad].m_ucLeftTriggerState; + return m_Joypads[ucPad].m_ucLeftTriggerState; } unsigned char CInput::GetJoypadRTrigger_Menu(unsigned char ucPad) { - return m_Joypads[ucPad].m_ucRightTriggerState; + return m_Joypads[ucPad].m_ucRightTriggerState; } float CInput::GetJoypadStick_LX(int iPad, bool bCheckMenuDisplay) { - if (bCheckMenuDisplay && m_bIsMenuDisplayed[iPad]) - { - return 0.0f; - } - - return *m_Joypads[iPad].m_pfLeftThumbXAxisMap * m_Joypads[iPad].m_fSensitivity; + if (bCheckMenuDisplay && m_bIsMenuDisplayed[iPad]) + { + return 0.0f; + } + + return *m_Joypads[iPad].m_pfLeftThumbXAxisMap * m_Joypads[iPad].m_fSensitivity; } float CInput::GetJoypadStick_LY(int iPad, bool bCheckMenuDisplay) { - if (bCheckMenuDisplay && m_bIsMenuDisplayed[iPad]) - { - return 0.0f; - } + if (bCheckMenuDisplay && m_bIsMenuDisplayed[iPad]) + { + return 0.0f; + } - return *m_Joypads[iPad].m_pfLeftThumbYAxisMap * m_Joypads[iPad].m_fSensitivity; + return *m_Joypads[iPad].m_pfLeftThumbYAxisMap * m_Joypads[iPad].m_fSensitivity; } float CInput::GetJoypadStick_RX(int iPad, bool bCheckMenuDisplay) { - if (bCheckMenuDisplay && m_bIsMenuDisplayed[iPad]) - { - return 0.0f; - } + if (bCheckMenuDisplay && m_bIsMenuDisplayed[iPad]) + { + return 0.0f; + } - return *m_Joypads[iPad].m_pfRightThumbXAxisMap * m_Joypads[iPad].m_fSensitivity; + return *m_Joypads[iPad].m_pfRightThumbXAxisMap * m_Joypads[iPad].m_fSensitivity; } float CInput::GetJoypadStick_RY(int iPad, bool bCheckMenuDisplay) { - if (bCheckMenuDisplay && m_bIsMenuDisplayed[iPad]) - { - return 0.0f; - } + if (bCheckMenuDisplay && m_bIsMenuDisplayed[iPad]) + { + return 0.0f; + } - return *m_Joypads[iPad].m_pfRightThumbYAxisMap * m_Joypads[iPad].m_fSensitivity; + return *m_Joypads[iPad].m_pfRightThumbYAxisMap * m_Joypads[iPad].m_fSensitivity; } unsigned char CInput::GetJoypadLTrigger(int iPad, bool bCheckMenuDisplay) { - if (bCheckMenuDisplay && m_bIsMenuDisplayed[iPad]) - { - return 0; - } + if (bCheckMenuDisplay && m_bIsMenuDisplayed[iPad]) + { + return 0; + } - return *m_Joypads[iPad].m_pucLeftTriggerAxisMap; + return *m_Joypads[iPad].m_pucLeftTriggerAxisMap; } unsigned char CInput::GetJoypadRTrigger(int iPad, bool bCheckMenuDisplay) { - if (bCheckMenuDisplay && m_bIsMenuDisplayed[iPad]) - { - return 0; - } + if (bCheckMenuDisplay && m_bIsMenuDisplayed[iPad]) + { + return 0; + } - return *m_Joypads[iPad].m_pucRightTriggerAxisMap; + return *m_Joypads[iPad].m_pucRightTriggerAxisMap; } void CInput::SetMenuDisplayed(int iPad, bool bVal) { - m_bIsMenuDisplayed[iPad] = bVal; + m_bIsMenuDisplayed[iPad] = bVal; } void CInput::SetKeyRepeatRate(float fRepeatDelaySecs, float fRepeatRateSecs) { - m_fRepeatDelaySecs = fRepeatDelaySecs; - m_fRepeatRateSecs = fRepeatRateSecs; + m_fRepeatDelaySecs = fRepeatDelaySecs; + m_fRepeatRateSecs = fRepeatRateSecs; } -void CInput::SetDebugSequence(const char* chSequenceA, int(*Func)(LPVOID), LPVOID lpParam) +void CInput::SetDebugSequence(const char *chSequenceA, int (*Func)(LPVOID), LPVOID lpParam) { - int iLen = strlen(chSequenceA); + int iLen = strlen(chSequenceA); - m_sDebugSequenceName = (char*)operator new[](iLen); - strcpy(m_sDebugSequenceName, chSequenceA); + m_sDebugSequenceName = (char *)operator new[](iLen); + strcpy(m_sDebugSequenceName, chSequenceA); - m_pDebugSequenceFn = Func; - m_pDebugSequenceParam = lpParam; + m_pDebugSequenceFn = Func; + m_pDebugSequenceParam = lpParam; } FLOAT CInput::GetIdleSeconds(int iPad) { - LARGE_INTEGER qwDeltaTime; - QueryPerformanceCounter(&qwDeltaTime); + LARGE_INTEGER qwDeltaTime; + QueryPerformanceCounter(&qwDeltaTime); - LARGE_INTEGER fElapsedTime = this->m_LastActivityTime[iPad]; - uint64_t qwNewTime = qwDeltaTime.QuadPart - fElapsedTime.QuadPart; + LARGE_INTEGER fElapsedTime = this->m_LastActivityTime[iPad]; + uint64_t qwNewTime = qwDeltaTime.QuadPart - fElapsedTime.QuadPart; - return this->m_fTickToSeconds * (qwDeltaTime.LowPart - fElapsedTime.LowPart); + return this->m_fTickToSeconds * (qwDeltaTime.LowPart - fElapsedTime.LowPart); } bool CInput::UpdateJoypads() { - assert(m_bJoypadMapArrayIsSetup); + assert(m_bJoypadMapArrayIsSetup); - for (int i = 0; i < MAX_JOYPADS; i++) - { - JOYPAD* pThisPad = &m_Joypads[i]; + for (int i = 0; i < MAX_JOYPADS; i++) + { + JOYPAD *pThisPad = &m_Joypads[i]; - pThisPad->m_uiOldButtons = pThisPad->m_uiButtons; - pThisPad->m_uiButtons = 0; - pThisPad->m_iRightThumbY = 0; - pThisPad->m_iRightThumbX = 0; - pThisPad->m_iLeftThumbY = 0; - pThisPad->m_iLeftThumbX = 0; - pThisPad->m_ucLeftTriggerState = 0; - pThisPad->m_ucRightTriggerState = 0; - } + pThisPad->m_uiOldButtons = pThisPad->m_uiButtons; + pThisPad->m_uiButtons = 0; + pThisPad->m_iRightThumbY = 0; + pThisPad->m_iRightThumbX = 0; + pThisPad->m_iLeftThumbY = 0; + pThisPad->m_iLeftThumbX = 0; + pThisPad->m_ucLeftTriggerState = 0; + pThisPad->m_ucRightTriggerState = 0; + } - for (int i = 0; i < MAX_JOYPADS; i++) - { - if (!m_Joypads[i].m_bIsDisabled) - { - JOYPAD* pThisPad = &m_Joypads[i]; + for (int i = 0; i < MAX_JOYPADS; i++) + { + if (!m_Joypads[i].m_bIsDisabled) + { + JOYPAD *pThisPad = &m_Joypads[i]; - bool bOldIsConnected = pThisPad->m_bIsConnected; + bool bOldIsConnected = pThisPad->m_bIsConnected; - pThisPad->m_ucInputStateIndex = pThisPad->m_ucInputStateIndex++; - if (pThisPad->m_ucInputStateIndex == m_ucInputStateC) - { - pThisPad->m_ucInputStateIndex = 0; - } + pThisPad->m_ucInputStateIndex = pThisPad->m_ucInputStateIndex++; + if (pThisPad->m_ucInputStateIndex == m_ucInputStateC) + { + pThisPad->m_ucInputStateIndex = 0; + } - pThisPad->m_bIsConnected = XInputGetState(i, &pThisPad->m_pInputStates[pThisPad->m_ucInputStateIndex]) == 0; + pThisPad->m_bIsConnected = XInputGetState(i, &pThisPad->m_pInputStates[pThisPad->m_ucInputStateIndex]) == 0; - bool bWasConnected = bOldIsConnected && !pThisPad->m_bIsConnected; - pThisPad->m_bWasConnected = bWasConnected; + bool bWasConnected = bOldIsConnected && !pThisPad->m_bIsConnected; + pThisPad->m_bWasConnected = bWasConnected; - bool bHasConnected = !bOldIsConnected && pThisPad->m_bIsConnected; - pThisPad->m_bHasConnected = bHasConnected; + bool bHasConnected = !bOldIsConnected && pThisPad->m_bIsConnected; + pThisPad->m_bHasConnected = bHasConnected; - if (pThisPad->m_bIsConnected) - { - SetJoypadValues(pThisPad); + if (pThisPad->m_bIsConnected) + { + SetJoypadValues(pThisPad); - pThisPad->m_uiButtonsPressed = ~pThisPad->m_uiOldButtons & pThisPad->m_uiButtons; - pThisPad->m_uiButtonsReleased = ~pThisPad->m_uiButtons & pThisPad->m_uiOldButtons; + pThisPad->m_uiButtonsPressed = ~pThisPad->m_uiOldButtons & pThisPad->m_uiButtons; + pThisPad->m_uiButtonsReleased = ~pThisPad->m_uiButtons & pThisPad->m_uiOldButtons; - for (int bit = 0; bit < 24; ++bit) - { - if ((pThisPad->m_uiButtonsPressed & (1 << bit)) != 0) - { - GetStartTime(i, bit); - } - else if ((pThisPad->m_uiButtons & (1 << bit)) != 0) - { - UpdateTime(i, bit); - } - } - } - else if (pThisPad->m_bWasConnected) - { - ClearJoypadValues(pThisPad); - } + for (int bit = 0; bit < 24; ++bit) + { + if ((pThisPad->m_uiButtonsPressed & (1 << bit)) != 0) + { + GetStartTime(i, bit); + } + else if ((pThisPad->m_uiButtons & (1 << bit)) != 0) + { + UpdateTime(i, bit); + } + } + } + else if (pThisPad->m_bWasConnected) + { + ClearJoypadValues(pThisPad); + } - if (m_pDebugSequenceFn && - (pThisPad->m_uiButtonsPressed & (_360_JOY_BUTTON_A | _360_JOY_BUTTON_B | _360_JOY_BUTTON_X | _360_JOY_BUTTON_Y | _360_JOY_BUTTON_RB | _360_JOY_BUTTON_LB)) != 0) - { - char chSeq; + if (m_pDebugSequenceFn && (pThisPad->m_uiButtonsPressed & (_360_JOY_BUTTON_A | _360_JOY_BUTTON_B | _360_JOY_BUTTON_X | _360_JOY_BUTTON_Y | + _360_JOY_BUTTON_RB | _360_JOY_BUTTON_LB)) != 0) + { + char chSeq; - if ((pThisPad->m_uiButtonsPressed & _360_JOY_BUTTON_A) == _360_JOY_BUTTON_A) - { - chSeq = 'A'; - } - else if ((pThisPad->m_uiButtonsPressed & _360_JOY_BUTTON_B) == _360_JOY_BUTTON_B) - { - chSeq = 'B'; - } - else if ((pThisPad->m_uiButtonsPressed & _360_JOY_BUTTON_X) == _360_JOY_BUTTON_X) - { - chSeq = 'X'; - } - else if ((pThisPad->m_uiButtonsPressed & _360_JOY_BUTTON_Y) == _360_JOY_BUTTON_Y) - { - chSeq = 'Y'; - } - else if ((pThisPad->m_uiButtonsPressed & _360_JOY_BUTTON_LB) == _360_JOY_BUTTON_LB) - { - chSeq = 'L'; - } - else if ((pThisPad->m_uiButtonsPressed & _360_JOY_BUTTON_RB) == _360_JOY_BUTTON_RB) - { - chSeq = 'R'; - } + if ((pThisPad->m_uiButtonsPressed & _360_JOY_BUTTON_A) == _360_JOY_BUTTON_A) + { + chSeq = 'A'; + } + else if ((pThisPad->m_uiButtonsPressed & _360_JOY_BUTTON_B) == _360_JOY_BUTTON_B) + { + chSeq = 'B'; + } + else if ((pThisPad->m_uiButtonsPressed & _360_JOY_BUTTON_X) == _360_JOY_BUTTON_X) + { + chSeq = 'X'; + } + else if ((pThisPad->m_uiButtonsPressed & _360_JOY_BUTTON_Y) == _360_JOY_BUTTON_Y) + { + chSeq = 'Y'; + } + else if ((pThisPad->m_uiButtonsPressed & _360_JOY_BUTTON_LB) == _360_JOY_BUTTON_LB) + { + chSeq = 'L'; + } + else if ((pThisPad->m_uiButtonsPressed & _360_JOY_BUTTON_RB) == _360_JOY_BUTTON_RB) + { + chSeq = 'R'; + } - if (m_sDebugSequenceName[m_uiDebugSequenceIndex] == chSeq) - { - if (!m_sDebugSequenceName[m_uiDebugSequenceIndex++]) - { - m_uiDebugSequenceIndex = 0; - (m_pDebugSequenceFn)(m_pDebugSequenceParam); - } - } - else - { - m_uiDebugSequenceIndex = 0; - } - } - } - } + if (m_sDebugSequenceName[m_uiDebugSequenceIndex] == chSeq) + { + if (!m_sDebugSequenceName[m_uiDebugSequenceIndex++]) + { + m_uiDebugSequenceIndex = 0; + (m_pDebugSequenceFn)(m_pDebugSequenceParam); + } + } + else + { + m_uiDebugSequenceIndex = 0; + } + } + } + } - return true; + return true; } -void CInput::ClearJoypadValues(JOYPAD* pThisPad) +void CInput::ClearJoypadValues(JOYPAD *pThisPad) { - pThisPad->m_uiButtonsPressed = 0; - pThisPad->m_uiButtonsReleased = 0; - pThisPad->m_uiButtons = 0; - pThisPad->m_ucLeftTriggerState = 0; - pThisPad->m_ucRightTriggerState = 0; - pThisPad->m_iRightThumbX = 0; - pThisPad->m_iRightThumbY = 0; - pThisPad->m_iLeftThumbX = 0; - pThisPad->m_iLeftThumbY = 0; - pThisPad->m_iNormalizedLeftThumbX = 0; - pThisPad->m_iNormalizedLeftThumbY = 0; - pThisPad->m_iNormalizedRightThumbX = 0; - pThisPad->m_iNormalizedRightThumbY = 0; - pThisPad->m_fNormalizedLeftThumbX = 0.0f; - pThisPad->m_fNormalizedLeftThumbY = 0.0f; - pThisPad->m_fNormalizedRightThumbX = 0.0f; - pThisPad->m_fNormalizedRightThumbY = 0.0f; + pThisPad->m_uiButtonsPressed = 0; + pThisPad->m_uiButtonsReleased = 0; + pThisPad->m_uiButtons = 0; + pThisPad->m_ucLeftTriggerState = 0; + pThisPad->m_ucRightTriggerState = 0; + pThisPad->m_iRightThumbX = 0; + pThisPad->m_iRightThumbY = 0; + pThisPad->m_iLeftThumbX = 0; + pThisPad->m_iLeftThumbY = 0; + pThisPad->m_iNormalizedLeftThumbX = 0; + pThisPad->m_iNormalizedLeftThumbY = 0; + pThisPad->m_iNormalizedRightThumbX = 0; + pThisPad->m_iNormalizedRightThumbY = 0; + pThisPad->m_fNormalizedLeftThumbX = 0.0f; + pThisPad->m_fNormalizedLeftThumbY = 0.0f; + pThisPad->m_fNormalizedRightThumbX = 0.0f; + pThisPad->m_fNormalizedRightThumbY = 0.0f; } void CInput::InitTime(void) { - LARGE_INTEGER qwTicksPerSec; - QueryPerformanceFrequency(&qwTicksPerSec); + LARGE_INTEGER qwTicksPerSec; + QueryPerformanceFrequency(&qwTicksPerSec); - m_fTickToSeconds = 1.0 / (float)(int)qwTicksPerSec.LowPart; - for (int i = 0; i < MAX_JOYPADS; i++) - { - m_LastActivityTime[i].QuadPart = 0; - } + m_fTickToSeconds = 1.0 / (float)(int)qwTicksPerSec.LowPart; + for (int i = 0; i < MAX_JOYPADS; i++) + { + m_LastActivityTime[i].QuadPart = 0; + } } void CInput::GetStartTime(int iPad, int iKey) { - QueryPerformanceCounter(&m_Timers[iPad][iKey].m_qwStartTime); + QueryPerformanceCounter(&m_Timers[iPad][iKey].m_qwStartTime); - m_Timers[iPad][iKey].m_qwTotalHoldTicks.QuadPart = 0; - m_Timers[iPad][iKey].m_fTotalHoldTime = 0; - m_Timers[iPad][iKey].m_fLastRepeatTime = 0; - m_Timers[iPad][iKey].m_bInitialPressHandled = false; + m_Timers[iPad][iKey].m_qwTotalHoldTicks.QuadPart = 0; + m_Timers[iPad][iKey].m_fTotalHoldTime = 0; + m_Timers[iPad][iKey].m_fLastRepeatTime = 0; + m_Timers[iPad][iKey].m_bInitialPressHandled = false; - m_LastActivityTime[iPad].QuadPart = m_Timers[iPad][iKey].m_qwStartTime.QuadPart; + m_LastActivityTime[iPad].QuadPart = m_Timers[iPad][iKey].m_qwStartTime.QuadPart; } void CInput::UpdateTime(int iPad, int iKey) { - LARGE_INTEGER qwDeltaTime; - QueryPerformanceCounter(&qwDeltaTime); - - uint64_t qwNewTime = qwDeltaTime.QuadPart - m_Timers[iPad][iKey].m_qwStartTime.QuadPart; - m_Timers[iPad][iKey].m_qwTotalHoldTicks.QuadPart += qwNewTime; + LARGE_INTEGER qwDeltaTime; + QueryPerformanceCounter(&qwDeltaTime); - m_Timers[iPad][iKey].m_qwStartTime = qwDeltaTime; - m_Timers[iPad][iKey].m_fTotalHoldTime = m_Timers[iPad][iKey].m_fTotalHoldTime + (m_fTickToSeconds * qwNewTime); + uint64_t qwNewTime = qwDeltaTime.QuadPart - m_Timers[iPad][iKey].m_qwStartTime.QuadPart; + m_Timers[iPad][iKey].m_qwTotalHoldTicks.QuadPart += qwNewTime; - m_LastActivityTime[iPad].QuadPart = m_Timers[iPad][iKey].m_qwStartTime.QuadPart; + m_Timers[iPad][iKey].m_qwStartTime = qwDeltaTime; + m_Timers[iPad][iKey].m_fTotalHoldTime = m_Timers[iPad][iKey].m_fTotalHoldTime + (m_fTickToSeconds * qwNewTime); + + m_LastActivityTime[iPad].QuadPart = m_Timers[iPad][iKey].m_qwStartTime.QuadPart; } \ No newline at end of file diff --git a/Windows_Libs/Dev/Input/INP_Main.h b/Windows_Libs/Dev/Input/INP_Main.h index 9634830..47d28df 100644 --- a/Windows_Libs/Dev/Input/INP_Main.h +++ b/Windows_Libs/Dev/Input/INP_Main.h @@ -7,155 +7,152 @@ class CInput { public: + struct JOYPAD + { + bool m_bIsDisabled; + bool m_bIsConnected; + bool m_bWasConnected; + bool m_bHasConnected; + unsigned int m_uiButtons; + unsigned int m_uiOldButtons; + int m_uiButtonsPressed; + int m_uiButtonsReleased; - struct JOYPAD - { - bool m_bIsDisabled; - bool m_bIsConnected; - bool m_bWasConnected; - bool m_bHasConnected; + int m_iRightThumbX; + int m_iRightThumbY; + int m_iLeftThumbX; + int m_iLeftThumbY; - unsigned int m_uiButtons; - unsigned int m_uiOldButtons; - int m_uiButtonsPressed; - int m_uiButtonsReleased; + DWORD m_iNormalizedRightThumbX; + DWORD m_iNormalizedRightThumbY; + DWORD m_iNormalizedLeftThumbX; + DWORD m_iNormalizedLeftThumbY; - int m_iRightThumbX; - int m_iRightThumbY; - int m_iLeftThumbX; - int m_iLeftThumbY; + float m_fNormalizedRightThumbX; + float m_fNormalizedRightThumbY; + float m_fNormalizedLeftThumbX; + float m_fNormalizedLeftThumbY; - DWORD m_iNormalizedRightThumbX; - DWORD m_iNormalizedRightThumbY; - DWORD m_iNormalizedLeftThumbX; - DWORD m_iNormalizedLeftThumbY; + unsigned char m_ucLeftTriggerState; + unsigned char m_ucRightTriggerState; - float m_fNormalizedRightThumbX; - float m_fNormalizedRightThumbY; - float m_fNormalizedLeftThumbX; - float m_fNormalizedLeftThumbY; + float *m_pfLeftThumbXAxisMap; + float *m_pfLeftThumbYAxisMap; + float *m_pfRightThumbXAxisMap; + float *m_pfRightThumbYAxisMap; - unsigned char m_ucLeftTriggerState; - unsigned char m_ucRightTriggerState; + float m_fSensitivity; - float* m_pfLeftThumbXAxisMap; - float* m_pfLeftThumbYAxisMap; - float* m_pfRightThumbXAxisMap; - float* m_pfRightThumbYAxisMap; + BYTE *m_pucLeftTriggerAxisMap; + BYTE *m_pucRightTriggerAxisMap; - float m_fSensitivity; + unsigned char m_ucMappingValue; + unsigned char m_ucInputStateIndex; + XINPUT_STATE *m_pInputStates; + }; - BYTE* m_pucLeftTriggerAxisMap; - BYTE* m_pucRightTriggerAxisMap; + struct JOYPADS + { + }; - unsigned char m_ucMappingValue; - unsigned char m_ucInputStateIndex; - XINPUT_STATE* m_pInputStates; - }; + struct TimeInfo + { + LARGE_INTEGER m_qwStartTime; + LARGE_INTEGER m_qwTotalHoldTicks; + float m_fTotalHoldTime; + float m_fLastRepeatTime; + bool m_bInitialPressHandled; + }; - struct JOYPADS - { + CInput(); - }; + void Initialise(int iInputStateC, unsigned char ucMapC, unsigned char ucActionC, unsigned char ucMenuActionC); + void Tick(void); - struct TimeInfo - { - LARGE_INTEGER m_qwStartTime; - LARGE_INTEGER m_qwTotalHoldTicks; - float m_fTotalHoldTime; - float m_fLastRepeatTime; - bool m_bInitialPressHandled; - }; + void SetJoypadValues(JOYPAD *pThisPad); + void SetDeadzoneAndMovementRange(unsigned int uiDeadzone, unsigned int uiMovementRangeMax); + void SetJoypadSensitivity(int iPad, float fSensitivity); - CInput(); + void SetGameJoypadMaps(unsigned char ucMap, unsigned char ucAction, unsigned int uiActionVal); + unsigned int GetGameJoypadMaps(unsigned char ucMap, unsigned char ucAction); + void SetJoypadMapVal(int iPad, unsigned char ucMap); + unsigned char GetJoypadMapVal(int iPad); - void Initialise(int iInputStateC, unsigned char ucMapC, unsigned char ucActionC, unsigned char ucMenuActionC); - void Tick(void); + void SetJoypadStickAxisMap(int iPad, unsigned int uiFrom, unsigned int uiTo); + void SetJoypadStickTriggerMap(int iPad, unsigned int uiFrom, unsigned int uiTo); - void SetJoypadValues(JOYPAD* pThisPad); - void SetDeadzoneAndMovementRange(unsigned int uiDeadzone, unsigned int uiMovementRangeMax); - void SetJoypadSensitivity(int iPad, float fSensitivity); + bool IsPadConnected(int iPad); + void SetSigninJoypadMask(unsigned int mask); + unsigned int GetValue(int iPad, unsigned char ucAction, bool bRepeat = false); + bool IsSet(int iPad, unsigned char ucAction); - void SetGameJoypadMaps(unsigned char ucMap, unsigned char ucAction, unsigned int uiActionVal); - unsigned int GetGameJoypadMaps(unsigned char ucMap, unsigned char ucAction); - void SetJoypadMapVal(int iPad, unsigned char ucMap); - unsigned char GetJoypadMapVal(int iPad); + bool ButtonPressed(int iPad, unsigned char ucAction = 255); // toggled + bool ButtonReleased(int iPad, unsigned char ucAction); // toggled + bool ButtonDown(int iPad, unsigned char ucAction = 255); // button held down - void SetJoypadStickAxisMap(int iPad, unsigned int uiFrom, unsigned int uiTo); - void SetJoypadStickTriggerMap(int iPad, unsigned int uiFrom, unsigned int uiTo); + float GetJoypadStick_Menu_LX(unsigned char ucPad); + float GetJoypadStick_Menu_LY(unsigned char ucPad); + float GetJoypadStick_Menu_RX(unsigned char ucPad); + float GetJoypadStick_Menu_RY(unsigned char ucPad); + unsigned char GetJoypadLTrigger_Menu(unsigned char ucPad); + unsigned char GetJoypadRTrigger_Menu(unsigned char ucPad); - bool IsPadConnected(int iPad); - void SetSigninJoypadMask(unsigned int mask); - unsigned int GetValue(int iPad, unsigned char ucAction, bool bRepeat = false); - bool IsSet(int iPad, unsigned char ucAction); + float GetJoypadStick_LX(int iPad, bool bCheckMenuDisplay = true); + float GetJoypadStick_LY(int iPad, bool bCheckMenuDisplay = true); + float GetJoypadStick_RX(int iPad, bool bCheckMenuDisplay = true); + float GetJoypadStick_RY(int iPad, bool bCheckMenuDisplay = true); + unsigned char GetJoypadLTrigger(int iPad, bool bCheckMenuDisplay = true); + unsigned char GetJoypadRTrigger(int iPad, bool bCheckMenuDisplay = true); - bool ButtonPressed(int iPad, unsigned char ucAction = 255); // toggled - bool ButtonReleased(int iPad, unsigned char ucAction); //toggled - bool ButtonDown(int iPad, unsigned char ucAction = 255); // button held down + void SetMenuDisplayed(int iPad, bool bVal); - float GetJoypadStick_Menu_LX(unsigned char ucPad); - float GetJoypadStick_Menu_LY(unsigned char ucPad); - float GetJoypadStick_Menu_RX(unsigned char ucPad); - float GetJoypadStick_Menu_RY(unsigned char ucPad); - unsigned char GetJoypadLTrigger_Menu(unsigned char ucPad); - unsigned char GetJoypadRTrigger_Menu(unsigned char ucPad); + void SetKeyRepeatRate(float fRepeatDelaySecs, float fRepeatRateSecs); + void SetDebugSequence(const char *chSequenceA, int (*Func)(LPVOID), LPVOID lpParam); + FLOAT GetIdleSeconds(int iPad); - float GetJoypadStick_LX(int iPad, bool bCheckMenuDisplay = true); - float GetJoypadStick_LY(int iPad, bool bCheckMenuDisplay = true); - float GetJoypadStick_RX(int iPad, bool bCheckMenuDisplay = true); - float GetJoypadStick_RY(int iPad, bool bCheckMenuDisplay = true); - unsigned char GetJoypadLTrigger(int iPad, bool bCheckMenuDisplay = true); - unsigned char GetJoypadRTrigger(int iPad, bool bCheckMenuDisplay = true); + bool UpdateJoypads(void); + void ClearJoypadValues(JOYPAD *pThisPad); - void SetMenuDisplayed(int iPad, bool bVal); + void InitTime(void); + void GetStartTime(int iPad, int iKey); + void UpdateTime(int iPad, int iKey); - void SetKeyRepeatRate(float fRepeatDelaySecs, float fRepeatRateSecs); - void SetDebugSequence(const char* chSequenceA, int(*Func)(LPVOID), LPVOID lpParam); - FLOAT GetIdleSeconds(int iPad); + BYTE gap0[16]; - bool UpdateJoypads(void); - void ClearJoypadValues(JOYPAD* pThisPad); + JOYPAD m_Joypads[MAX_JOYPADS]; - void InitTime(void); - void GetStartTime(int iPad, int iKey); - void UpdateTime(int iPad, int iKey); + int m_iDeadzone; + int m_iMovementRangeMax; + int m_iEffectiveRange; + int m_iHalfRange; + float m_fEffectiveRange; - BYTE gap0[16]; + unsigned char m_ucInputStateC; + unsigned char m_ucJoypadMapC; + unsigned char m_ucJoypadMapActionC; + unsigned char m_ucMenuActionC; - JOYPAD m_Joypads[MAX_JOYPADS]; + unsigned int **m_JoypadMap; - int m_iDeadzone; - int m_iMovementRangeMax; - int m_iEffectiveRange; - int m_iHalfRange; - float m_fEffectiveRange; + bool m_bJoypadMapArrayIsSetup; + unsigned int m_uiSigninJoypadMask; + bool m_bIsMenuDisplayed[MAX_JOYPADS]; - unsigned char m_ucInputStateC; - unsigned char m_ucJoypadMapC; - unsigned char m_ucJoypadMapActionC; - unsigned char m_ucMenuActionC; + CForceFeedback m_ForceFeedback; + CKeyboard m_Keyboard; - unsigned int** m_JoypadMap; + float m_fTickToSeconds; + float m_fRepeatDelaySecs; + float m_fRepeatRateSecs; - bool m_bJoypadMapArrayIsSetup; - unsigned int m_uiSigninJoypadMask; - bool m_bIsMenuDisplayed[MAX_JOYPADS]; + TimeInfo m_Timers[MAX_JOYPADS][24]; + LARGE_INTEGER m_LastActivityTime[MAX_JOYPADS]; - CForceFeedback m_ForceFeedback; - CKeyboard m_Keyboard; - - float m_fTickToSeconds; - float m_fRepeatDelaySecs; - float m_fRepeatRateSecs; - - TimeInfo m_Timers[MAX_JOYPADS][24]; - LARGE_INTEGER m_LastActivityTime[MAX_JOYPADS]; - - char* m_sDebugSequenceName; - unsigned int m_uiDebugSequenceIndex; - int (*m_pDebugSequenceFn)(void*); - LPVOID m_pDebugSequenceParam; + char *m_sDebugSequenceName; + unsigned int m_uiDebugSequenceIndex; + int (*m_pDebugSequenceFn)(void *); + LPVOID m_pDebugSequenceParam; }; // Singleton diff --git a/Windows_Libs/Dev/Input/LinkedList.cpp b/Windows_Libs/Dev/Input/LinkedList.cpp index 79ddd00..f7ca9bf 100644 --- a/Windows_Libs/Dev/Input/LinkedList.cpp +++ b/Windows_Libs/Dev/Input/LinkedList.cpp @@ -7,9 +7,9 @@ LinkedList::LinkedList() this->m_Tail = nullptr; } -void LinkedList::AddToHead(void* pvData) +void LinkedList::AddToHead(void *pvData) { - _LL_NODE* pNode = new _LL_NODE(); + _LL_NODE *pNode = new _LL_NODE(); pNode->m_pvData = pvData; if (!m_Tail) @@ -27,9 +27,9 @@ void LinkedList::AddToHead(void* pvData) m_NodeC++; } -void LinkedList::AddToTail(void* pvData) +void LinkedList::AddToTail(void *pvData) { - _LL_NODE* pNode = new _LL_NODE(); + _LL_NODE *pNode = new _LL_NODE(); pNode->m_pvData = pvData; if (!m_Head) @@ -37,7 +37,8 @@ void LinkedList::AddToTail(void* pvData) m_Head = pNode; } - if (m_Tail) { + if (m_Tail) + { m_Tail->m_Next = pNode; pNode->m_Prev = m_Tail; } @@ -49,9 +50,9 @@ void LinkedList::AddToTail(void* pvData) m_NodeC++; } -void LinkedList::RemoveNode(_LL_NODE* pNodeToRemove) +void LinkedList::RemoveNode(_LL_NODE *pNodeToRemove) { - _LL_NODE* pCurrentNode; + _LL_NODE *pCurrentNode; for (pCurrentNode = m_Head; pCurrentNode != pNodeToRemove; pCurrentNode = pCurrentNode->m_Next) { ; @@ -61,7 +62,7 @@ void LinkedList::RemoveNode(_LL_NODE* pNodeToRemove) { m_Head = m_Head->m_Next; } - else + else { pCurrentNode->m_Prev->m_Next = pCurrentNode->m_Next; @@ -74,9 +75,9 @@ void LinkedList::RemoveNode(_LL_NODE* pNodeToRemove) m_NodeC--; } -LinkedList::_LL_NODE* LinkedList::RemoveHeadNode() +LinkedList::_LL_NODE *LinkedList::RemoveHeadNode() { - _LL_NODE* pHeadNode = m_Head; + _LL_NODE *pHeadNode = m_Head; if (m_NodeC > 0) { @@ -87,11 +88,11 @@ LinkedList::_LL_NODE* LinkedList::RemoveHeadNode() return pHeadNode; } -void LinkedList::ClearList() +void LinkedList::ClearList() { - while (true) + while (true) { - _LL_NODE* pHeadNode = RemoveHeadNode(); + _LL_NODE *pHeadNode = RemoveHeadNode(); if (!pHeadNode) { break; diff --git a/Windows_Libs/Dev/Input/LinkedList.h b/Windows_Libs/Dev/Input/LinkedList.h index 73281bb..7b42aa7 100644 --- a/Windows_Libs/Dev/Input/LinkedList.h +++ b/Windows_Libs/Dev/Input/LinkedList.h @@ -3,29 +3,27 @@ class LinkedList { public: + struct _LL_NODE + { + template + T *GetDataAs() + { + return (T *)m_pvData; + } + void *m_pvData; + _LL_NODE *m_Next; + _LL_NODE *m_Prev; + }; - struct _LL_NODE - { - template - T* GetDataAs() - { - return (T*)m_pvData; - } + int m_NodeC; + _LL_NODE *m_Head; + _LL_NODE *m_Tail; - void* m_pvData; - _LL_NODE* m_Next; - _LL_NODE* m_Prev; - }; - - int m_NodeC; - _LL_NODE* m_Head; - _LL_NODE* m_Tail; - - LinkedList(); - void AddToHead(void* pvData); - void AddToTail(void* pvData); - void RemoveNode(_LL_NODE* pNodeToRemove); - _LL_NODE* RemoveHeadNode(); - void ClearList(); + LinkedList(); + void AddToHead(void *pvData); + void AddToTail(void *pvData); + void RemoveNode(_LL_NODE *pNodeToRemove); + _LL_NODE *RemoveHeadNode(); + void ClearList(); }; \ No newline at end of file diff --git a/Windows_Libs/Dev/Render/4J_Render.cpp b/Windows_Libs/Dev/Render/4J_Render.cpp index 2089c6a..38fe1da 100644 --- a/Windows_Libs/Dev/Render/4J_Render.cpp +++ b/Windows_Libs/Dev/Render/4J_Render.cpp @@ -2,8 +2,11 @@ #include "4J_Render.h" #include "Renderer.h" +C4JRender RenderManager; + void C4JRender::Tick() { + InternalRenderManager.CBuffTick(); } void C4JRender::UpdateGamma(unsigned short usGamma) @@ -474,5 +477,3 @@ void C4JRender::Resume() { InternalRenderManager.Resume(); } - -C4JRender RenderManager; diff --git a/Windows_Libs/Dev/Render/Renderer.h b/Windows_Libs/Dev/Render/Renderer.h index 92340ba..4910405 100644 --- a/Windows_Libs/Dev/Render/Renderer.h +++ b/Windows_Libs/Dev/Render/Renderer.h @@ -9,441 +9,439 @@ class Renderer { public: - struct Context; - struct CommandBuffer; - - void Tick(); - void UpdateGamma(unsigned short usGamma); - void MatrixMode(int type); - void MatrixSetIdentity(); - void MatrixTranslate(float x, float y, float z); - void MatrixRotate(float angle, float x, float y, float z); - void MatrixScale(float x, float y, float z); - void MatrixPerspective(float fovy, float aspect, float zNear, float zFar); - void MatrixOrthogonal(float left, float right, float bottom, float top, float zNear, float zFar); - void MatrixPop(); - void MatrixPush(); - void MatrixMult(float* mat); - const float* MatrixGet(int type); - void Set_matrixDirty(); - void Initialise(ID3D11Device* pDevice, IDXGISwapChain* pSwapChain); - ID3D11DeviceContext* InitialiseContext(bool fromPresent); - void StartFrame(); - void DoScreenGrabOnNextPresent(); - void Present(); - void Clear(int flags, D3D11_RECT* pRect); - void SetClearColour(const float colourRGBA[4]); - bool IsWidescreen(); - bool IsHiDef(); - void CaptureThumbnail(ImageFileBuffer* pngOut); - void CaptureScreen(ImageFileBuffer* jpgOut, XSOCIAL_PREVIEWIMAGE* previewOut); - void BeginConditionalSurvey(int identifier); - void EndConditionalSurvey(); - void BeginConditionalRendering(int identifier); - void EndConditionalRendering(); - void DrawVertices(C4JRender::ePrimitiveType PrimitiveType, int count, void* dataIn, C4JRender::eVertexType vType, C4JRender::ePixelShaderType psType); - void DrawVertexBuffer(C4JRender::ePrimitiveType PrimitiveType, int count, ID3D11Buffer* buffer, C4JRender::eVertexType vType, C4JRender::ePixelShaderType psType); - void CBuffLockStaticCreations(); - int CBuffCreate(int count); - void CBuffDelete(int first, int count); - void CBuffStart(int index, bool full); - void CBuffClear(int index); - int CBuffSize(int index); - void CBuffEnd(); - bool CBuffCall(int index, bool full); - void CBuffTick(); - void CBuffDeferredModeStart(); - void CBuffDeferredModeEnd(); - int TextureCreate(); - void TextureFree(int idx); - void TextureBind(int idx); - void TextureBindVertex(int idx); - void TextureSetTextureLevels(int levels); - int TextureGetTextureLevels(); - void TextureSetParam(int param, int value); - void TextureDynamicUpdateStart(); - void TextureDynamicUpdateEnd(); - void TextureData(int width, int height, void* data, int level, C4JRender::eTextureFormat format); - void TextureDataUpdate(int xoffset, int yoffset, int width, int height, void* data, int level); - HRESULT LoadTextureData(const char* szFilename, D3DXIMAGE_INFO* pSrcInfo, int** ppDataOut); - HRESULT LoadTextureData(BYTE* pbData, DWORD dwBytes, D3DXIMAGE_INFO* pSrcInfo, int** ppDataOut); - HRESULT SaveTextureData(const char* szFilename, D3DXIMAGE_INFO* pSrcInfo, int* ppDataOut); - HRESULT SaveTextureDataToMemory(void* pOutput, int outputCapacity, int* outputLength, int width, int height, int* ppDataIn); - void TextureGetStats(); - ID3D11ShaderResourceView* TextureGetTexture(int idx); - void StateSetColour(float r, float g, float b, float a); - void StateSetDepthMask(bool enable); - void StateSetBlendEnable(bool enable); - void StateSetBlendFunc(int src, int dst); - void StateSetBlendFactor(unsigned int colour); - void StateSetAlphaFunc(int func, float param); - void StateSetDepthFunc(int func); - void StateSetFaceCull(bool enable); - void StateSetFaceCullCW(bool enable); - void StateSetLineWidth(float width); - void StateSetWriteEnable(bool red, bool green, bool blue, bool alpha); - void StateSetDepthTestEnable(bool enable); - void StateSetAlphaTestEnable(bool enable); - void StateSetDepthSlopeAndBias(float slope, float bias); - void StateSetFogEnable(bool enable); - void StateSetFogMode(int mode); - void StateSetFogNearDistance(float dist); - void StateSetFogFarDistance(float dist); - void StateSetFogDensity(float density); - void StateSetFogColour(float red, float green, float blue); - void StateSetLightingEnable(bool enable); - void StateSetVertexTextureUV(float u, float v); - void StateSetLightColour(int light, float red, float green, float blue); - void StateSetLightAmbientColour(float red, float green, float blue); - void StateSetLightDirection(int light, float x, float y, float z); - void StateSetLightEnable(int light, bool enable); - void StateSetViewport(C4JRender::eViewportType viewportType); - void StateSetEnableViewportClipPlanes(bool enable); - void StateSetTexGenCol(int col, float x, float y, float z, float w, bool eyeSpace); - void StateSetStencil(D3D11_COMPARISON_FUNC function, uint8_t stencil_ref, uint8_t stencil_func_mask, uint8_t stencil_write_mask); - void StateSetForceLOD(int LOD); - void BeginEvent(LPCWSTR eventName); - void EndEvent(); - void Suspend(); - bool Suspended(); - void Resume(); - void StateUpdate(); + struct Context; + struct CommandBuffer; + void Tick(); + void UpdateGamma(unsigned short usGamma); + void MatrixMode(int type); + void MatrixSetIdentity(); + void MatrixTranslate(float x, float y, float z); + void MatrixRotate(float angle, float x, float y, float z); + void MatrixScale(float x, float y, float z); + void MatrixPerspective(float fovy, float aspect, float zNear, float zFar); + void MatrixOrthogonal(float left, float right, float bottom, float top, float zNear, float zFar); + void MatrixPop(); + void MatrixPush(); + void MatrixMult(float *mat); + const float *MatrixGet(int type); + void Set_matrixDirty(); + void Initialise(ID3D11Device *pDevice, IDXGISwapChain *pSwapChain); + ID3D11DeviceContext *InitialiseContext(bool fromPresent); + void StartFrame(); + void DoScreenGrabOnNextPresent(); + void Present(); + void Clear(int flags, D3D11_RECT *pRect); + void SetClearColour(const float colourRGBA[4]); + bool IsWidescreen(); + bool IsHiDef(); + void CaptureThumbnail(ImageFileBuffer *pngOut); + void CaptureScreen(ImageFileBuffer *jpgOut, XSOCIAL_PREVIEWIMAGE *previewOut); + void BeginConditionalSurvey(int identifier); + void EndConditionalSurvey(); + void BeginConditionalRendering(int identifier); + void EndConditionalRendering(); + void DrawVertices(C4JRender::ePrimitiveType PrimitiveType, int count, void *dataIn, C4JRender::eVertexType vType, + C4JRender::ePixelShaderType psType); + void DrawVertexBuffer(C4JRender::ePrimitiveType PrimitiveType, int count, ID3D11Buffer *buffer, C4JRender::eVertexType vType, + C4JRender::ePixelShaderType psType); + void CBuffLockStaticCreations(); + int CBuffCreate(int count); + void CBuffDelete(int first, int count); + void CBuffStart(int index, bool full); + void CBuffClear(int index); + int CBuffSize(int index); + void CBuffEnd(); + bool CBuffCall(int index, bool full); + void CBuffTick(); + void CBuffDeferredModeStart(); + void CBuffDeferredModeEnd(); + int TextureCreate(); + void TextureFree(int idx); + void TextureBind(int idx); + void TextureBindVertex(int idx); + void TextureSetTextureLevels(int levels); + int TextureGetTextureLevels(); + void TextureSetParam(int param, int value); + void TextureDynamicUpdateStart(); + void TextureDynamicUpdateEnd(); + void TextureData(int width, int height, void *data, int level, C4JRender::eTextureFormat format); + void TextureDataUpdate(int xoffset, int yoffset, int width, int height, void *data, int level); + HRESULT LoadTextureData(const char *szFilename, D3DXIMAGE_INFO *pSrcInfo, int **ppDataOut); + HRESULT LoadTextureData(BYTE *pbData, DWORD dwBytes, D3DXIMAGE_INFO *pSrcInfo, int **ppDataOut); + HRESULT SaveTextureData(const char *szFilename, D3DXIMAGE_INFO *pSrcInfo, int *ppDataOut); + HRESULT SaveTextureDataToMemory(void *pOutput, int outputCapacity, int *outputLength, int width, int height, int *ppDataIn); + void TextureGetStats(); + ID3D11ShaderResourceView *TextureGetTexture(int idx); + void StateSetColour(float r, float g, float b, float a); + void StateSetDepthMask(bool enable); + void StateSetBlendEnable(bool enable); + void StateSetBlendFunc(int src, int dst); + void StateSetBlendFactor(unsigned int colour); + void StateSetAlphaFunc(int func, float param); + void StateSetDepthFunc(int func); + void StateSetFaceCull(bool enable); + void StateSetFaceCullCW(bool enable); + void StateSetLineWidth(float width); + void StateSetWriteEnable(bool red, bool green, bool blue, bool alpha); + void StateSetDepthTestEnable(bool enable); + void StateSetAlphaTestEnable(bool enable); + void StateSetDepthSlopeAndBias(float slope, float bias); + void StateSetFogEnable(bool enable); + void StateSetFogMode(int mode); + void StateSetFogNearDistance(float dist); + void StateSetFogFarDistance(float dist); + void StateSetFogDensity(float density); + void StateSetFogColour(float red, float green, float blue); + void StateSetLightingEnable(bool enable); + void StateSetVertexTextureUV(float u, float v); + void StateSetLightColour(int light, float red, float green, float blue); + void StateSetLightAmbientColour(float red, float green, float blue); + void StateSetLightDirection(int light, float x, float y, float z); + void StateSetLightEnable(int light, bool enable); + void StateSetViewport(C4JRender::eViewportType viewportType); + void StateSetEnableViewportClipPlanes(bool enable); + void StateSetTexGenCol(int col, float x, float y, float z, float w, bool eyeSpace); + void StateSetStencil(D3D11_COMPARISON_FUNC function, uint8_t stencil_ref, uint8_t stencil_func_mask, uint8_t stencil_write_mask); + void StateSetForceLOD(int LOD); + void BeginEvent(LPCWSTR eventName); + void EndEvent(); + void Suspend(); + bool Suspended(); + void Resume(); + void StateUpdate(); private: - void SetupShaders(); - void ConvertLinearToPng(ImageFileBuffer* pngOut, unsigned char* linearData, unsigned int width, unsigned int height); - void DrawVertexSetup(C4JRender::eVertexType vType, C4JRender::ePixelShaderType psType, C4JRender::ePrimitiveType primitiveType, int* count, bool* drawIndexed); - void UpdateTexGenState(); - void UpdateLightingState(); - void UpdateViewportState(); - void UpdateFogState(); - void UpdateTextureState(bool vertexSampler); - void MultWithStack(DirectX::XMMATRIX matrix); - ID3D11DepthStencilState* GetManagedDepthStencilState(); - ID3D11BlendState* GetManagedBlendState(); - ID3D11RasterizerState* GetManagedRasterizerState(); - ID3D11SamplerState* GetManagedSamplerState(); - void DeleteInternalBuffer(int index); - Renderer::Context& getContext(); - static D3D11_PRIMITIVE_TOPOLOGY* m_Topologies; - static DXGI_FORMAT textureFormats[C4JRender::MAX_TEXTURE_FORMATS]; - + void SetupShaders(); + void ConvertLinearToPng(ImageFileBuffer *pngOut, unsigned char *linearData, unsigned int width, unsigned int height); + void DrawVertexSetup(C4JRender::eVertexType vType, C4JRender::ePixelShaderType psType, C4JRender::ePrimitiveType primitiveType, int *count, + bool *drawIndexed); + void UpdateTexGenState(); + void UpdateLightingState(); + void UpdateViewportState(); + void UpdateFogState(); + void UpdateTextureState(bool vertexSampler); + void MultWithStack(DirectX::XMMATRIX matrix); + ID3D11DepthStencilState *GetManagedDepthStencilState(); + ID3D11BlendState *GetManagedBlendState(); + ID3D11RasterizerState *GetManagedRasterizerState(); + ID3D11SamplerState *GetManagedSamplerState(); + void DeleteInternalBuffer(int index); + Renderer::Context &getContext(); + static D3D11_PRIMITIVE_TOPOLOGY *m_Topologies; + static DXGI_FORMAT textureFormats[C4JRender::MAX_TEXTURE_FORMATS]; public: + struct Texture + { + bool allocated; + ID3D11Texture2D *texture; + ID3D11ShaderResourceView *view; + DWORD textureFlags; + DWORD mipLevels; + DWORD textureFormat; + DWORD samplerParams; + }; - struct Texture - { - bool allocated; - ID3D11Texture2D* texture; - ID3D11ShaderResourceView* view; - DWORD textureFlags; - DWORD mipLevels; - DWORD textureFormat; - DWORD samplerParams; - }; + struct TexgenCBuffer + { + DirectX::XMMATRIX unk0; + DirectX::XMMATRIX unk1; + }; - struct TexgenCBuffer - { - DirectX::XMMATRIX unk0; - DirectX::XMMATRIX unk1; - }; + enum eCommandType + { + COMMAND_ADD_MATRIX, + COMMAND_ADD_VERTICES, + COMMAND_BIND_TEXTURE, + COMMAND_SET_COLOR, + COMMAND_SET_DEPTH_FUNC, + COMMAND_SET_DEPTH_MASK, + COMMAND_SET_DEPTH_TEST, + COMMAND_SET_LIGHTING_ENABLE, + COMMAND_SET_LIGHT_ENABLE, + COMMAND_SET_LIGHT_DIRECTION, + COMMAND_SET_LIGHT_COLOUR, + COMMAND_SET_LIGHT_AMBIENT_COLOUR, + COMMAND_SET_BLEND_ENABLE, + COMMAND_SET_BLEND_FUNC, + COMMAND_SET_BLEND_FACTOR, + COMMAND_SET_FACE_CULL, + }; - enum eCommandType - { - COMMAND_ADD_MATRIX, - COMMAND_ADD_VERTICES, - COMMAND_BIND_TEXTURE, - COMMAND_SET_COLOR, - COMMAND_SET_DEPTH_FUNC, - COMMAND_SET_DEPTH_MASK, - COMMAND_SET_DEPTH_TEST, - COMMAND_SET_LIGHTING_ENABLE, - COMMAND_SET_LIGHT_ENABLE, - COMMAND_SET_LIGHT_DIRECTION, - COMMAND_SET_LIGHT_COLOUR, - COMMAND_SET_LIGHT_AMBIENT_COLOUR, - COMMAND_SET_BLEND_ENABLE, - COMMAND_SET_BLEND_FUNC, - COMMAND_SET_BLEND_FACTOR, - COMMAND_SET_FACE_CULL, - }; + struct CommandBuffer + { + CommandBuffer(bool full); + ~CommandBuffer(); + void StartRecording(); + void EndRecording(ID3D11Device *device); + std::uint64_t GetAllocated(); + bool IsBusy(); + void AddMatrix(const float *matrix); + void AddVertices(unsigned int stride, unsigned int count, void *dataIn, Renderer::Context &context); + void BindTexture(int idx); + void SetColor(float r, float g, float b, float a); + void SetDepthFunc(int func); + void SetDepthMask(bool enable); + void SetDepthTestEnable(bool enable); + void SetLightingEnable(bool enable); + void SetLightEnable(int light, bool enable); + void SetLightDirection(int light, float x, float y, float z); + void SetLightColour(int light, float r, float g, float b); + void SetLightAmbientColour(float r, float g, float b); + void SetBlendEnable(bool enable); + void SetBlendFunc(int src, int dst); + void SetBlendFactor(unsigned int factor); + void SetFaceCull(bool enable); + void Render(C4JRender::eVertexType vertexType, Renderer::Context &context, int primitiveType); - struct CommandBuffer - { - CommandBuffer(bool full); - ~CommandBuffer(); - void StartRecording(); - void EndRecording(ID3D11Device* device); - std::uint64_t GetAllocated(); - bool IsBusy(); - void AddMatrix(const float* matrix); - void AddVertices(unsigned int stride, unsigned int count, void* dataIn, Renderer::Context& context); - void BindTexture(int idx); - void SetColor(float r, float g, float b, float a); - void SetDepthFunc(int func); - void SetDepthMask(bool enable); - void SetDepthTestEnable(bool enable); - void SetLightingEnable(bool enable); - void SetLightEnable(int light, bool enable); - void SetLightDirection(int light, float x, float y, float z); - void SetLightColour(int light, float r, float g, float b); - void SetLightAmbientColour(float r, float g, float b); - void SetBlendEnable(bool enable); - void SetBlendFunc(int src, int dst); - void SetBlendFactor(unsigned int factor); - void SetFaceCull(bool enable); - void Render(C4JRender::eVertexType vertexType, Renderer::Context& context, int primitiveType); + struct Command + { + Renderer::eCommandType m_command_type; + BYTE commandPadding[12]; - struct Command - { - Renderer::eCommandType m_command_type; - BYTE commandPadding[12]; + union + { + BYTE data[64]; - union - { - BYTE data[64]; + struct + { + float m_matrix[16]; + // DirectX::XMMATRIX m_matrix; + } add_matrix; - struct - { - float m_matrix[16]; - //DirectX::XMMATRIX m_matrix; - } add_matrix; + struct + { + unsigned int m_vertex_index_start; + unsigned int m_vertex_count; + } add_vertices; - struct - { - unsigned int m_vertex_index_start; - unsigned int m_vertex_count; - } add_vertices; + struct + { + unsigned int m_texture_index; + } bind_texture; - struct - { - unsigned int m_texture_index; - } bind_texture; + struct + { + float m_color[4]; + } set_color; - struct - { - float m_color[4]; - } set_color; + struct + { + int m_depth_func; + } set_depth_func; - struct - { - int m_depth_func; - } set_depth_func; + struct + { + bool m_enable; + } set_depth_mask; - struct - { - bool m_enable; - } set_depth_mask; + struct + { + bool m_enable; + } set_depth_test; - struct - { - bool m_enable; - } set_depth_test; + struct + { + bool m_enable; + } set_lighting_enable; - struct - { - bool m_enable; - } set_lighting_enable; + struct + { + int m_light_index; + bool m_enable; + } set_light_enable; - struct - { - int m_light_index; - bool m_enable; - } set_light_enable; + struct + { + int m_light_index; + float padding[3]; + float m_direction[4]; + } set_light_direction; - struct - { - int m_light_index; - float padding[3]; - float m_direction[4]; - } set_light_direction; + struct + { + int m_light_index; + float m_color[3]; + } set_light_colour; - struct - { - int m_light_index; - float m_color[3]; - } set_light_colour; + struct + { + BYTE padding; + float m_color[3]; + } set_light_ambient_colour; - struct - { - BYTE padding; - float m_color[3]; - } set_light_ambient_colour; + struct + { + bool m_enable; + } set_blend_enable; - struct - { - bool m_enable; - } set_blend_enable; + struct + { + int m_src; + int m_dst; + } set_blend_func; - struct - { - int m_src; - int m_dst; - } set_blend_func; + struct + { + unsigned int m_blend_factor; + } set_blend_factor; - struct - { - unsigned int m_blend_factor; - } set_blend_factor; + struct + { + bool m_enable; + } set_face_cull; + }; + }; + ID3D11Buffer *m_vertexBuffer; + void *m_vertexData; + std::uint64_t m_vertexDataLength; + std::vector m_commands; + std::uint64_t m_allocated; + BYTE isActive; + BYTE paddingAfterActive[7]; + }; - struct - { - bool m_enable; - } set_face_cull; - }; - }; - ID3D11Buffer* m_vertexBuffer; - void* m_vertexData; - std::uint64_t m_vertexDataLength; - std::vector m_commands; - std::uint64_t m_allocated; - BYTE isActive; - BYTE paddingAfterActive[7]; + struct DeferredCBuff + { + Renderer::CommandBuffer *m_command_buf; + int m_vertex_index; + int m_vertex_type; + int m_primitive_type; + DirectX::XMMATRIX m_matrix; + }; + struct Context + { + Context(ID3D11Device *device, ID3D11DeviceContext *deviceContext); + ID3D11DeviceContext *m_pDeviceContext; + ID3DUserDefinedAnnotation *userAnnotation; + DWORD contextStateFlags; + BYTE paddingAfterFlags[12]; + DirectX::XMMATRIX matrixStacks[4][16]; + bool matrixDirty[4]; + DWORD matrixStackDepth[4]; + DWORD matrixModeType; + DWORD boundTextureIndex; + BYTE faceCullEnabled; + BYTE depthTestEnabled; + BYTE alphaTestEnabled; + float alphaReference; + BYTE depthWriteEnabled; + BYTE fogEnabled; + BYTE paddingAfterFogFlags[2]; + float fogNearDistance; + float fogFarDistance; + float fogDensity; + float fogColourRed; + float fogColourBlue; + float fogColourGreen; + DWORD fogMode; + BYTE lightingEnabled; + BYTE lightEnabled[2]; + BYTE lightingDirty; + DWORD forcedLOD; + BYTE paddingAfterForceLOD[4]; + DirectX::XMFLOAT4 lightDirection[2]; + DirectX::XMFLOAT4 lightColour[2]; + DirectX::XMFLOAT4 lightAmbientColour; + ID3D11Buffer *cbMatrix0; + ID3D11Buffer *cbMatrix1; + ID3D11Buffer *cbMatrix2; + ID3D11Buffer *cbMatrix3; + ID3D11Buffer *cbVertexTexcoord; + ID3D11Buffer *cbFogParams; + ID3D11Buffer *cbLighting; + ID3D11Buffer *cbTexGen; + ID3D11Buffer *cbAux0; + ID3D11Buffer *cbAux1; + ID3D11Buffer *cbColour; + ID3D11Buffer *cbFogColour; + ID3D11Buffer *cbAux2; + ID3D11Buffer *cbAlphaTest; + ID3D11Buffer *cbAux3; + ID3D11Buffer *cbAux4; + uint64_t dynamicVertexBase; + DWORD dynamicVertexOffset; + BYTE paddingAfterDynamicOffset[4]; + ID3D11Buffer *dynamicVertexBuffer; + BYTE paddingBeforeTexGen[8]; + DirectX::XMMATRIX texGenMatrices[2]; + Renderer::CommandBuffer *commandBuffer; + DWORD recordingBufferIndex; + DWORD recordingVertexType; + DWORD recordingPrimitiveType; + BYTE deferredModeEnabled; + BYTE paddingAfterDeferredModeEnabled[3]; + std::vector deferredBuffers; + D3D11_BLEND_DESC blendDesc; + D3D11_DEPTH_STENCIL_DESC depthStencilDesc; + D3D11_RASTERIZER_DESC rasterizerDesc; + float blendFactor[4]; + DWORD reservedContext0; + DWORD reservedContext1; + }; - }; + static DWORD tlsIdx; + static unsigned int s_auiWidths[MAX_MIP_LEVELS + 1]; + static unsigned int s_auiHeights[MAX_MIP_LEVELS + 1]; + static D3D11_INPUT_ELEMENT_DESC g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1[5]; + static D3D11_INPUT_ELEMENT_DESC g_vertex_PTN_Elements_Compressed[2]; + static D3D11_PRIMITIVE_TOPOLOGY g_topologies[C4JRender::PRIMITIVE_TYPE_COUNT]; + static int totalAlloc; + static _RTL_CRITICAL_SECTION totalAllocCS; - struct DeferredCBuff - { - Renderer::CommandBuffer* m_command_buf; - int m_vertex_index; - int m_vertex_type; - int m_primitive_type; - DirectX::XMMATRIX m_matrix; - }; - - struct Context - { - Context(ID3D11Device* device, ID3D11DeviceContext* deviceContext); - ID3D11DeviceContext* m_pDeviceContext; - ID3DUserDefinedAnnotation* userAnnotation; - DWORD contextStateFlags; - BYTE paddingAfterFlags[12]; - DirectX::XMMATRIX matrixStacks[4][16]; - bool matrixDirty[4]; - DWORD matrixStackDepth[4]; - DWORD matrixModeType; - DWORD boundTextureIndex; - BYTE faceCullEnabled; - BYTE depthTestEnabled; - BYTE alphaTestEnabled; - float alphaReference; - BYTE depthWriteEnabled; - BYTE fogEnabled; - BYTE paddingAfterFogFlags[2]; - float fogNearDistance; - float fogFarDistance; - float fogDensity; - float fogColourRed; - float fogColourBlue; - float fogColourGreen; - DWORD fogMode; - BYTE lightingEnabled; - BYTE lightEnabled[2]; - BYTE lightingDirty; - DWORD forcedLOD; - BYTE paddingAfterForceLOD[4]; - DirectX::XMFLOAT4 lightDirection[2]; - DirectX::XMFLOAT4 lightColour[2]; - DirectX::XMFLOAT4 lightAmbientColour; - ID3D11Buffer* cbMatrix0; - ID3D11Buffer* cbMatrix1; - ID3D11Buffer* cbMatrix2; - ID3D11Buffer* cbMatrix3; - ID3D11Buffer* cbVertexTexcoord; - ID3D11Buffer* cbFogParams; - ID3D11Buffer* cbLighting; - ID3D11Buffer* cbTexGen; - ID3D11Buffer* cbAux0; - ID3D11Buffer* cbAux1; - ID3D11Buffer* cbColour; - ID3D11Buffer* cbFogColour; - ID3D11Buffer* cbAux2; - ID3D11Buffer* cbAlphaTest; - ID3D11Buffer* cbAux3; - ID3D11Buffer* cbAux4; - uint64_t dynamicVertexBase; - DWORD dynamicVertexOffset; - BYTE paddingAfterDynamicOffset[4]; - ID3D11Buffer* dynamicVertexBuffer; - BYTE paddingBeforeTexGen[8]; - DirectX::XMMATRIX texGenMatrices[2]; - Renderer::CommandBuffer* commandBuffer; - DWORD recordingBufferIndex; - DWORD recordingVertexType; - DWORD recordingPrimitiveType; - BYTE deferredModeEnabled; - BYTE paddingAfterDeferredModeEnabled[3]; - std::vector deferredBuffers; - D3D11_BLEND_DESC blendDesc; - D3D11_DEPTH_STENCIL_DESC depthStencilDesc; - D3D11_RASTERIZER_DESC rasterizerDesc; - float blendFactor[4]; - DWORD reservedContext0; - DWORD reservedContext1; - }; - - static DWORD tlsIdx; - static unsigned int s_auiWidths[MAX_MIP_LEVELS + 1]; - static unsigned int s_auiHeights[MAX_MIP_LEVELS + 1]; - static D3D11_INPUT_ELEMENT_DESC g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1[5]; - static D3D11_INPUT_ELEMENT_DESC g_vertex_PTN_Elements_Compressed[2]; - static D3D11_PRIMITIVE_TOPOLOGY g_topologies[C4JRender::PRIMITIVE_TYPE_COUNT]; - static int totalAlloc; - static _RTL_CRITICAL_SECTION totalAllocCS; - - float m_fClearColor[4]; - ID3D11Device* m_pDevice; - ID3D11DeviceContext* m_pDeviceContext; - IDXGISwapChain* m_pSwapChain; - ID3D11RenderTargetView* renderTargetView; - ID3D11RenderTargetView* renderTargetViews[4]; - ID3D11ShaderResourceView* renderTargetShaderResourceView; - ID3D11ShaderResourceView* renderTargetShaderResourceViews[4]; - ID3D11Texture2D* renderTargetTextures[4]; - ID3D11DepthStencilView* depthStencilView; - ID3D11VertexShader** vertexShaderTable; - ID3D11VertexShader* screenSpaceVertexShader; - ID3D11VertexShader* screenClearVertexShader; - ID3D11PixelShader** pixelShaderTable; - ID3D11PixelShader* screenSpacePixelShader; - ID3D11PixelShader* screenClearPixelShader; - unsigned int* vertexStrideTable; - ID3D11InputLayout** inputLayoutTable; - ID3D11Buffer* quadIndexBuffer; - ID3D11Buffer* fanIndexBuffer; - DWORD defaultTextureIndex; - WORD reservedRendererWord0; - BYTE paddingAfterRendererWord0[2]; - DWORD presentCount; - BYTE rendererFlag0; - BYTE paddingAfterRendererFlag0[3]; - _RTL_CRITICAL_SECTION rtl_critical_section100; - DWORD activeVertexType; - DWORD activePixelType; - C4JRender::eViewportType m_ViewportType; - BYTE reservedRendererByte0; - BYTE paddingAfterViewportType[3]; - Renderer::Texture m_textures[512]; - DWORD backBufferWidth; - DWORD backBufferHeight; - BYTE reservedRendererByte1; - BYTE paddingAfterRendererByte1[3]; - DWORD reservedRendererDword1; - void* reservedRendererPtr2; - void* reservedRendererPtr3; - uint64_t reservedRendererPtr1; - void* reservedRendererPtr4; - void* reservedRendererPtr5; - void* reservedRendererPtr6; - DWORD reservedRendererDword2; - DWORD reservedRendererDword3; - std::unordered_map managedBlendStates; - std::unordered_map managedDepthStencilStates; - std::unordered_map managedSamplerStates; - std::unordered_map managedRasterizerStates; - BYTE shouldScreenGrabNextFrame; - BYTE suspended; - BYTE paddingAfterSuspendState[2]; + float m_fClearColor[4]; + ID3D11Device *m_pDevice; + ID3D11DeviceContext *m_pDeviceContext; + IDXGISwapChain *m_pSwapChain; + ID3D11RenderTargetView *renderTargetView; + ID3D11RenderTargetView *renderTargetViews[4]; + ID3D11ShaderResourceView *renderTargetShaderResourceView; + ID3D11ShaderResourceView *renderTargetShaderResourceViews[4]; + ID3D11Texture2D *renderTargetTextures[4]; + ID3D11DepthStencilView *depthStencilView; + ID3D11VertexShader **vertexShaderTable; + ID3D11VertexShader *screenSpaceVertexShader; + ID3D11VertexShader *screenClearVertexShader; + ID3D11PixelShader **pixelShaderTable; + ID3D11PixelShader *screenSpacePixelShader; + ID3D11PixelShader *screenClearPixelShader; + unsigned int *vertexStrideTable; + ID3D11InputLayout **inputLayoutTable; + ID3D11Buffer *quadIndexBuffer; + ID3D11Buffer *fanIndexBuffer; + DWORD defaultTextureIndex; + WORD reservedRendererWord0; + BYTE paddingAfterRendererWord0[2]; + DWORD presentCount; + BYTE rendererFlag0; + BYTE paddingAfterRendererFlag0[3]; + _RTL_CRITICAL_SECTION rtl_critical_section100; + DWORD activeVertexType; + DWORD activePixelType; + C4JRender::eViewportType m_ViewportType; + BYTE reservedRendererByte0; + BYTE paddingAfterViewportType[3]; + Renderer::Texture m_textures[512]; + DWORD backBufferWidth; + DWORD backBufferHeight; + BYTE reservedRendererByte1; + BYTE paddingAfterRendererByte1[3]; + DWORD reservedRendererDword1; + void *reservedRendererPtr2; + void *reservedRendererPtr3; + uint64_t reservedRendererPtr1; + void *reservedRendererPtr4; + void *reservedRendererPtr5; + void *reservedRendererPtr6; + DWORD reservedRendererDword2; + DWORD reservedRendererDword3; + std::unordered_map managedBlendStates; + std::unordered_map managedDepthStencilStates; + std::unordered_map managedSamplerStates; + std::unordered_map managedRasterizerStates; + BYTE shouldScreenGrabNextFrame; + BYTE suspended; + BYTE paddingAfterSuspendState[2]; }; // Singleton diff --git a/Windows_Libs/Dev/Render/RendererCBuff.cpp b/Windows_Libs/Dev/Render/RendererCBuff.cpp index 042125b..54b1659 100644 --- a/Windows_Libs/Dev/Render/RendererCBuff.cpp +++ b/Windows_Libs/Dev/Render/RendererCBuff.cpp @@ -9,864 +9,852 @@ #include Renderer::CommandBuffer::CommandBuffer(bool full) - : m_vertexBuffer(nullptr) - , m_vertexData(nullptr) - , m_vertexDataLength(0) - , m_commands() - , m_allocated(0x1000) - , isActive(full ? 1 : 0) + : m_vertexBuffer(nullptr), m_vertexData(nullptr), m_vertexDataLength(0), m_commands(), m_allocated(0x1000), isActive(full ? 1 : 0) { - std::memset(paddingAfterActive, 0, sizeof(paddingAfterActive)); - m_vertexData = std::malloc(m_allocated); - EnterCriticalSection(&Renderer::totalAllocCS); - Renderer::totalAlloc += static_cast(m_allocated); - LeaveCriticalSection(&Renderer::totalAllocCS); + std::memset(paddingAfterActive, 0, sizeof(paddingAfterActive)); + m_vertexData = std::malloc(m_allocated); + EnterCriticalSection(&Renderer::totalAllocCS); + Renderer::totalAlloc += static_cast(m_allocated); + LeaveCriticalSection(&Renderer::totalAllocCS); } Renderer::CommandBuffer::~CommandBuffer() { - if (m_vertexBuffer) - { - m_vertexBuffer->Release(); - } + if (m_vertexBuffer) + { + m_vertexBuffer->Release(); + } - std::free(m_vertexData); + std::free(m_vertexData); - EnterCriticalSection(&Renderer::totalAllocCS); - Renderer::totalAlloc -= static_cast(m_allocated); - LeaveCriticalSection(&Renderer::totalAllocCS); + EnterCriticalSection(&Renderer::totalAllocCS); + Renderer::totalAlloc -= static_cast(m_allocated); + LeaveCriticalSection(&Renderer::totalAllocCS); } -void Renderer::CommandBuffer::StartRecording() +void Renderer::CommandBuffer::StartRecording() {} + +void Renderer::CommandBuffer::EndRecording(ID3D11Device *device) { -} + if (m_vertexDataLength != 0) + { + D3D11_BUFFER_DESC desc = {}; + desc.ByteWidth = m_vertexDataLength; + desc.Usage = D3D11_USAGE_IMMUTABLE; + desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; -void Renderer::CommandBuffer::EndRecording(ID3D11Device* device) -{ - if (m_vertexDataLength != 0) - { - D3D11_BUFFER_DESC desc = {}; - desc.ByteWidth = m_vertexDataLength; - desc.Usage = D3D11_USAGE_IMMUTABLE; - desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + D3D11_SUBRESOURCE_DATA data = {}; + data.pSysMem = m_vertexData; + device->CreateBuffer(&desc, &data, &m_vertexBuffer); + } - D3D11_SUBRESOURCE_DATA data = {}; - data.pSysMem = m_vertexData; - device->CreateBuffer(&desc, &data, &m_vertexBuffer); - } - - std::free(m_vertexData); - m_vertexData = nullptr; + std::free(m_vertexData); + m_vertexData = nullptr; } std::uint64_t Renderer::CommandBuffer::GetAllocated() { - return m_allocated; + return m_allocated; } bool Renderer::CommandBuffer::IsBusy() { - return false; + return false; } -void Renderer::CommandBuffer::AddMatrix(const float* matrix) +void Renderer::CommandBuffer::AddMatrix(const float *matrix) { - Command command = {}; - command.m_command_type = COMMAND_ADD_MATRIX; - std::memcpy(command.add_matrix.m_matrix, matrix, sizeof(command.add_matrix.m_matrix)); - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_ADD_MATRIX; + std::memcpy(command.add_matrix.m_matrix, matrix, sizeof(command.add_matrix.m_matrix)); + m_commands.push_back(command); } -void Renderer::CommandBuffer::AddVertices(unsigned int stride, unsigned int count, void* dataIn, Renderer::Context& context) +void Renderer::CommandBuffer::AddVertices(unsigned int stride, unsigned int count, void *dataIn, Renderer::Context &context) { - if (context.matrixDirty[3]) - { - this->AddMatrix(InternalRenderManager.MatrixGet(3)); - context.matrixDirty[3] = false; - } + if (context.matrixDirty[3]) + { + this->AddMatrix(InternalRenderManager.MatrixGet(3)); + context.matrixDirty[3] = false; + } - const std::uint64_t vertexOffset = m_vertexDataLength; - const std::uint64_t copySize = std::uint64_t(stride) * std::uint64_t(count); + const std::uint64_t vertexOffset = m_vertexDataLength; + const std::uint64_t copySize = std::uint64_t(stride) * std::uint64_t(count); - Command command = {}; - command.m_command_type = COMMAND_ADD_VERTICES; - command.add_vertices.m_vertex_index_start = (unsigned int)vertexOffset; - command.add_vertices.m_vertex_count = count; + Command command = {}; + command.m_command_type = COMMAND_ADD_VERTICES; + command.add_vertices.m_vertex_index_start = (unsigned int)vertexOffset; + command.add_vertices.m_vertex_count = count; - m_vertexDataLength = vertexOffset + copySize; - if (m_vertexDataLength > m_allocated) - { - EnterCriticalSection(&Renderer::totalAllocCS); - Renderer::totalAlloc -= static_cast(m_allocated); - LeaveCriticalSection(&Renderer::totalAllocCS); + m_vertexDataLength = vertexOffset + copySize; + if (m_vertexDataLength > m_allocated) + { + EnterCriticalSection(&Renderer::totalAllocCS); + Renderer::totalAlloc -= static_cast(m_allocated); + LeaveCriticalSection(&Renderer::totalAllocCS); - m_allocated = ((m_vertexDataLength + (0x1000u - 1u)) & ~(0x1000u - 1u)); - m_vertexData = std::realloc(m_vertexData, m_allocated); + m_allocated = ((m_vertexDataLength + (0x1000u - 1u)) & ~(0x1000u - 1u)); + m_vertexData = std::realloc(m_vertexData, m_allocated); - EnterCriticalSection(&Renderer::totalAllocCS); - Renderer::totalAlloc += static_cast(m_allocated); - LeaveCriticalSection(&Renderer::totalAllocCS); - } + EnterCriticalSection(&Renderer::totalAllocCS); + Renderer::totalAlloc += static_cast(m_allocated); + LeaveCriticalSection(&Renderer::totalAllocCS); + } - const std::size_t byteCount = std::size_t(stride) * std::size_t(count); - std::memcpy(static_cast(m_vertexData) + vertexOffset, dataIn, byteCount); - m_commands.push_back(command); + const std::size_t byteCount = std::size_t(stride) * std::size_t(count); + std::memcpy(static_cast(m_vertexData) + vertexOffset, dataIn, byteCount); + m_commands.push_back(command); } void Renderer::CommandBuffer::BindTexture(int idx) { - Command command = {}; - command.m_command_type = COMMAND_BIND_TEXTURE; - command.bind_texture.m_texture_index = idx; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_BIND_TEXTURE; + command.bind_texture.m_texture_index = idx; + m_commands.push_back(command); } void Renderer::CommandBuffer::SetColor(float r, float g, float b, float a) { - Command command = {}; - command.m_command_type = COMMAND_SET_COLOR; - command.set_color.m_color[0] = r; - command.set_color.m_color[1] = g; - command.set_color.m_color[2] = b; - command.set_color.m_color[3] = a; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_SET_COLOR; + command.set_color.m_color[0] = r; + command.set_color.m_color[1] = g; + command.set_color.m_color[2] = b; + command.set_color.m_color[3] = a; + m_commands.push_back(command); } void Renderer::CommandBuffer::SetDepthFunc(int func) { - Command command = {}; - command.m_command_type = COMMAND_SET_DEPTH_FUNC; - command.set_depth_func.m_depth_func = func; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_SET_DEPTH_FUNC; + command.set_depth_func.m_depth_func = func; + m_commands.push_back(command); } void Renderer::CommandBuffer::SetDepthMask(bool enable) { - Command command = {}; - command.m_command_type = COMMAND_SET_DEPTH_MASK; - command.set_depth_mask.m_enable = enable; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_SET_DEPTH_MASK; + command.set_depth_mask.m_enable = enable; + m_commands.push_back(command); } void Renderer::CommandBuffer::SetDepthTestEnable(bool enable) { - Command command = {}; - command.m_command_type = COMMAND_SET_DEPTH_TEST; - command.set_depth_test.m_enable = enable; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_SET_DEPTH_TEST; + command.set_depth_test.m_enable = enable; + m_commands.push_back(command); } void Renderer::CommandBuffer::SetLightingEnable(bool enable) { - Command command = {}; - command.m_command_type = COMMAND_SET_LIGHTING_ENABLE; - command.set_lighting_enable.m_enable = enable; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_SET_LIGHTING_ENABLE; + command.set_lighting_enable.m_enable = enable; + m_commands.push_back(command); } void Renderer::CommandBuffer::SetLightEnable(int light, bool enable) { - Command command = {}; - command.m_command_type = COMMAND_SET_LIGHT_ENABLE; - command.set_light_enable.m_light_index = light; - command.set_light_enable.m_enable = enable; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_SET_LIGHT_ENABLE; + command.set_light_enable.m_light_index = light; + command.set_light_enable.m_enable = enable; + m_commands.push_back(command); } void Renderer::CommandBuffer::SetLightDirection(int light, float x, float y, float z) { - Renderer::Context& context = InternalRenderManager.getContext(); - const std::uint32_t depth = context.matrixStackDepth[3]; - const DirectX::XMMATRIX& matrix = context.matrixStacks[3][depth]; + Renderer::Context &context = InternalRenderManager.getContext(); + const std::uint32_t depth = context.matrixStackDepth[3]; + const DirectX::XMMATRIX &matrix = context.matrixStacks[3][depth]; - DirectX::XMVECTOR direction = DirectX::XMVectorSet(x, y, z, 0.0f); - direction = DirectX::XMVector3TransformNormal(direction, matrix); - direction = DirectX::XMVector3Normalize(direction); + DirectX::XMVECTOR direction = DirectX::XMVectorSet(x, y, z, 0.0f); + direction = DirectX::XMVector3TransformNormal(direction, matrix); + direction = DirectX::XMVector3Normalize(direction); - Command command = {}; - command.m_command_type = COMMAND_SET_LIGHT_DIRECTION; - command.set_light_direction.m_light_index = light; - DirectX::XMFLOAT4 outDirection; - DirectX::XMStoreFloat4(&outDirection, direction); - command.set_light_direction.m_direction[0] = outDirection.x; - command.set_light_direction.m_direction[1] = outDirection.y; - command.set_light_direction.m_direction[2] = outDirection.z; - command.set_light_direction.m_direction[3] = outDirection.w; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_SET_LIGHT_DIRECTION; + command.set_light_direction.m_light_index = light; + DirectX::XMFLOAT4 outDirection; + DirectX::XMStoreFloat4(&outDirection, direction); + command.set_light_direction.m_direction[0] = outDirection.x; + command.set_light_direction.m_direction[1] = outDirection.y; + command.set_light_direction.m_direction[2] = outDirection.z; + command.set_light_direction.m_direction[3] = outDirection.w; + m_commands.push_back(command); } void Renderer::CommandBuffer::SetLightColour(int light, float r, float g, float b) { - Command command = {}; - command.m_command_type = COMMAND_SET_LIGHT_COLOUR; - command.set_light_colour.m_light_index = light; - command.set_light_colour.m_color[0] = r; - command.set_light_colour.m_color[1] = g; - command.set_light_colour.m_color[2] = b; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_SET_LIGHT_COLOUR; + command.set_light_colour.m_light_index = light; + command.set_light_colour.m_color[0] = r; + command.set_light_colour.m_color[1] = g; + command.set_light_colour.m_color[2] = b; + m_commands.push_back(command); } void Renderer::CommandBuffer::SetLightAmbientColour(float r, float g, float b) { - Command command = {}; - command.m_command_type = COMMAND_SET_LIGHT_AMBIENT_COLOUR; - command.set_light_ambient_colour.m_color[0] = r; - command.set_light_ambient_colour.m_color[1] = g; - command.set_light_ambient_colour.m_color[2] = b; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_SET_LIGHT_AMBIENT_COLOUR; + command.set_light_ambient_colour.m_color[0] = r; + command.set_light_ambient_colour.m_color[1] = g; + command.set_light_ambient_colour.m_color[2] = b; + m_commands.push_back(command); } void Renderer::CommandBuffer::SetBlendEnable(bool enable) { - Command command = {}; - command.m_command_type = COMMAND_SET_BLEND_ENABLE; - command.set_blend_enable.m_enable = enable; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_SET_BLEND_ENABLE; + command.set_blend_enable.m_enable = enable; + m_commands.push_back(command); } void Renderer::CommandBuffer::SetBlendFunc(int src, int dst) { - Command command = {}; - command.m_command_type = COMMAND_SET_BLEND_FUNC; - command.set_blend_func.m_src = src; - command.set_blend_func.m_dst = dst; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_SET_BLEND_FUNC; + command.set_blend_func.m_src = src; + command.set_blend_func.m_dst = dst; + m_commands.push_back(command); } void Renderer::CommandBuffer::SetBlendFactor(unsigned int factor) { - Command command = {}; - command.m_command_type = COMMAND_SET_BLEND_FACTOR; - command.set_blend_factor.m_blend_factor = factor; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_SET_BLEND_FACTOR; + command.set_blend_factor.m_blend_factor = factor; + m_commands.push_back(command); } void Renderer::CommandBuffer::SetFaceCull(bool enable) { - Command command = {}; - command.m_command_type = COMMAND_SET_FACE_CULL; - command.set_face_cull.m_enable = enable; - m_commands.push_back(command); + Command command = {}; + command.m_command_type = COMMAND_SET_FACE_CULL; + command.set_face_cull.m_enable = enable; + m_commands.push_back(command); } -void Renderer::CommandBuffer::Render(C4JRender::eVertexType vertexType, Renderer::Context& context, int primitiveType) +void Renderer::CommandBuffer::Render(C4JRender::eVertexType vertexType, Renderer::Context &context, int primitiveType) { - if (!m_vertexBuffer) - { - return; - } + if (!m_vertexBuffer) + { + return; + } - int drawVertexType = vertexType; - int shaderVertexType = drawVertexType; - bool matrixOverride = false; + int drawVertexType = vertexType; + int shaderVertexType = drawVertexType; + bool matrixOverride = false; - for (std::vector::const_iterator it = m_commands.begin(); it != m_commands.end(); ++it) - { - const Command& command = *it; - switch (command.m_command_type) - { - case COMMAND_ADD_MATRIX: - { - if (drawVertexType == C4JRender::VERTEX_TYPE_COMPRESSED) - { - const float row[4] = - { - command.add_matrix.m_matrix[12], - command.add_matrix.m_matrix[13], - command.add_matrix.m_matrix[14], - command.add_matrix.m_matrix[15] - }; - D3D11_MAPPED_SUBRESOURCE mappedAux0 = {}; - context.m_pDeviceContext->Map(context.cbAux0, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedAux0); - std::memcpy(mappedAux0.pData, row, sizeof(row)); - context.m_pDeviceContext->Unmap(context.cbAux0, 0); - } - else - { - D3D11_MAPPED_SUBRESOURCE mappedMatrix1 = {}; - context.m_pDeviceContext->Map(context.cbMatrix1, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedMatrix1); - std::memcpy(mappedMatrix1.pData, command.add_matrix.m_matrix, sizeof(command.add_matrix.m_matrix)); - context.m_pDeviceContext->Unmap(context.cbMatrix1, 0); - matrixOverride = true; - } - break; - } - case COMMAND_ADD_VERTICES: - { - if (isActive) - { - InternalRenderManager.UpdateLightingState(); + for (std::vector::const_iterator it = m_commands.begin(); it != m_commands.end(); ++it) + { + const Command &command = *it; + switch (command.m_command_type) + { + case COMMAND_ADD_MATRIX: + { + if (drawVertexType == C4JRender::VERTEX_TYPE_COMPRESSED) + { + const float row[4] = {command.add_matrix.m_matrix[12], command.add_matrix.m_matrix[13], command.add_matrix.m_matrix[14], + command.add_matrix.m_matrix[15]}; + D3D11_MAPPED_SUBRESOURCE mappedAux0 = {}; + context.m_pDeviceContext->Map(context.cbAux0, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedAux0); + std::memcpy(mappedAux0.pData, row, sizeof(row)); + context.m_pDeviceContext->Unmap(context.cbAux0, 0); + } + else + { + D3D11_MAPPED_SUBRESOURCE mappedMatrix1 = {}; + context.m_pDeviceContext->Map(context.cbMatrix1, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedMatrix1); + std::memcpy(mappedMatrix1.pData, command.add_matrix.m_matrix, sizeof(command.add_matrix.m_matrix)); + context.m_pDeviceContext->Unmap(context.cbMatrix1, 0); + matrixOverride = true; + } + break; + } + case COMMAND_ADD_VERTICES: + { + if (isActive) + { + InternalRenderManager.UpdateLightingState(); - if (drawVertexType == C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1) - { - if (context.lightingEnabled) - { - drawVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT; - shaderVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT; - } - } - else if (drawVertexType == C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT && !context.lightingEnabled) - { - drawVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1; - shaderVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1; - } + if (drawVertexType == C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1) + { + if (context.lightingEnabled) + { + drawVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT; + shaderVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT; + } + } + else if (drawVertexType == C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT && !context.lightingEnabled) + { + drawVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1; + shaderVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1; + } - if (static_cast(drawVertexType) != InternalRenderManager.activeVertexType) - { - context.m_pDeviceContext->VSSetShader(InternalRenderManager.vertexShaderTable[shaderVertexType], nullptr, 0); - context.m_pDeviceContext->IASetInputLayout(InternalRenderManager.inputLayoutTable[shaderVertexType]); - InternalRenderManager.activeVertexType = drawVertexType; - } - } + if (static_cast(drawVertexType) != InternalRenderManager.activeVertexType) + { + context.m_pDeviceContext->VSSetShader(InternalRenderManager.vertexShaderTable[shaderVertexType], nullptr, 0); + context.m_pDeviceContext->IASetInputLayout(InternalRenderManager.inputLayoutTable[shaderVertexType]); + InternalRenderManager.activeVertexType = drawVertexType; + } + } - unsigned int drawCount = command.add_vertices.m_vertex_count; - bool drawIndexed = false; - if (primitiveType == C4JRender::PRIMITIVE_TYPE_QUAD_LIST) - { - drawCount = (drawCount * 6u) / 4u; - drawIndexed = true; - } - else if (primitiveType == C4JRender::PRIMITIVE_TYPE_TRIANGLE_FAN) - { - drawCount = (drawCount - 2u) * 3u; - drawIndexed = true; - } + unsigned int drawCount = command.add_vertices.m_vertex_count; + bool drawIndexed = false; + if (primitiveType == C4JRender::PRIMITIVE_TYPE_QUAD_LIST) + { + drawCount = (drawCount * 6u) / 4u; + drawIndexed = true; + } + else if (primitiveType == C4JRender::PRIMITIVE_TYPE_TRIANGLE_FAN) + { + drawCount = (drawCount - 2u) * 3u; + drawIndexed = true; + } - ID3D11Buffer* buffer = m_vertexBuffer; - const UINT stride = InternalRenderManager.vertexStrideTable[drawVertexType]; - const UINT offset = command.add_vertices.m_vertex_index_start; - context.m_pDeviceContext->IASetVertexBuffers(0, 1, &buffer, &stride, &offset); + ID3D11Buffer *buffer = m_vertexBuffer; + const UINT stride = InternalRenderManager.vertexStrideTable[drawVertexType]; + const UINT offset = command.add_vertices.m_vertex_index_start; + context.m_pDeviceContext->IASetVertexBuffers(0, 1, &buffer, &stride, &offset); - if (drawIndexed) - { - context.m_pDeviceContext->DrawIndexed(drawCount, 0, 0); - } - else - { - context.m_pDeviceContext->Draw(drawCount, 0); - } - break; - } - case COMMAND_BIND_TEXTURE: - { - context.boundTextureIndex = command.bind_texture.m_texture_index; - ID3D11ShaderResourceView* view = InternalRenderManager.m_textures[context.boundTextureIndex].view; - context.m_pDeviceContext->PSSetShaderResources(0, 1, &view); - InternalRenderManager.UpdateTextureState(false); - break; - } - case COMMAND_SET_COLOR: - { - D3D11_MAPPED_SUBRESOURCE mappedColour = {}; - context.m_pDeviceContext->Map(context.cbColour, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedColour); - std::memcpy(mappedColour.pData, command.set_color.m_color, sizeof(command.set_color.m_color)); - context.m_pDeviceContext->Unmap(context.cbColour, 0); - break; - } - case COMMAND_SET_DEPTH_FUNC: - { - context.depthStencilDesc.DepthFunc = static_cast(command.set_depth_func.m_depth_func); - context.m_pDeviceContext->OMSetDepthStencilState(InternalRenderManager.GetManagedDepthStencilState(), 0); - break; - } - case COMMAND_SET_DEPTH_MASK: - { - context.depthWriteEnabled = command.set_depth_mask.m_enable ? 1 : 0; - context.depthStencilDesc.DepthWriteMask = command.set_depth_mask.m_enable ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO; - context.m_pDeviceContext->OMSetDepthStencilState(InternalRenderManager.GetManagedDepthStencilState(), 0); - break; - } - case COMMAND_SET_DEPTH_TEST: - { - context.depthTestEnabled = command.set_depth_test.m_enable ? 1 : 0; - context.depthStencilDesc.DepthEnable = command.set_depth_test.m_enable ? TRUE : FALSE; - context.m_pDeviceContext->OMSetDepthStencilState(InternalRenderManager.GetManagedDepthStencilState(), 0); - break; - } - case COMMAND_SET_LIGHTING_ENABLE: - { - context.lightingEnabled = command.set_lighting_enable.m_enable ? 1 : 0; - break; - } - case COMMAND_SET_LIGHT_ENABLE: - { - const int light = command.set_light_enable.m_light_index; - if (light >= 0 && light < 2) - { - context.lightEnabled[light] = command.set_light_enable.m_enable ? 1 : 0; - context.lightingDirty = 1; - } - break; - } - case COMMAND_SET_LIGHT_DIRECTION: - { - const int light = command.set_light_direction.m_light_index; - if (light >= 0 && light < 2) - { - context.lightDirection[light].x = command.set_light_direction.m_direction[0]; - context.lightDirection[light].y = command.set_light_direction.m_direction[1]; - context.lightDirection[light].z = command.set_light_direction.m_direction[2]; - context.lightDirection[light].w = command.set_light_direction.m_direction[3]; - context.lightingDirty = 1; - } - break; - } - case COMMAND_SET_LIGHT_COLOUR: - { - const int light = command.set_light_colour.m_light_index; - if (light >= 0 && light < 2) - { - context.lightColour[light].x = command.set_light_colour.m_color[0]; - context.lightColour[light].y = command.set_light_colour.m_color[1]; - context.lightColour[light].z = command.set_light_colour.m_color[2]; - context.lightColour[light].w = 1.0f; - context.lightingDirty = 1; - } - break; - } - case COMMAND_SET_LIGHT_AMBIENT_COLOUR: - { - context.lightAmbientColour.x = command.set_light_ambient_colour.m_color[0]; - context.lightAmbientColour.y = command.set_light_ambient_colour.m_color[1]; - context.lightAmbientColour.z = command.set_light_ambient_colour.m_color[2]; - context.lightAmbientColour.w = 1.0f; - context.lightingDirty = 1; - break; - } - case COMMAND_SET_BLEND_ENABLE: - { - context.blendDesc.RenderTarget[0].BlendEnable = command.set_blend_enable.m_enable ? TRUE : FALSE; - break; - } - case COMMAND_SET_BLEND_FUNC: - { - context.blendDesc.RenderTarget[0].SrcBlend = static_cast(command.set_blend_func.m_src); - context.blendDesc.RenderTarget[0].DestBlend = static_cast(command.set_blend_func.m_dst); - context.m_pDeviceContext->OMSetBlendState(InternalRenderManager.GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); - break; - } - case COMMAND_SET_BLEND_FACTOR: - { - const unsigned int factor = command.set_blend_factor.m_blend_factor; - context.blendFactor[0] = float((factor >> 0) & 0xFFu) / 255.0f; - context.blendFactor[1] = float((factor >> 8) & 0xFFu) / 255.0f; - context.blendFactor[2] = float((factor >> 16) & 0xFFu) / 255.0f; - context.blendFactor[3] = float((factor >> 24) & 0xFFu) / 255.0f; - context.m_pDeviceContext->OMSetBlendState(InternalRenderManager.GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); - break; - } - case COMMAND_SET_FACE_CULL: - { - context.rasterizerDesc.CullMode = command.set_face_cull.m_enable ? D3D11_CULL_BACK : D3D11_CULL_NONE; - context.m_pDeviceContext->RSSetState(InternalRenderManager.GetManagedRasterizerState()); - context.faceCullEnabled = command.set_face_cull.m_enable ? 1 : 0; - break; - } - default: - break; - } - } + if (drawIndexed) + { + context.m_pDeviceContext->DrawIndexed(drawCount, 0, 0); + } + else + { + context.m_pDeviceContext->Draw(drawCount, 0); + } + break; + } + case COMMAND_BIND_TEXTURE: + { + context.boundTextureIndex = command.bind_texture.m_texture_index; + ID3D11ShaderResourceView *view = InternalRenderManager.m_textures[context.boundTextureIndex].view; + context.m_pDeviceContext->PSSetShaderResources(0, 1, &view); + InternalRenderManager.UpdateTextureState(false); + break; + } + case COMMAND_SET_COLOR: + { + D3D11_MAPPED_SUBRESOURCE mappedColour = {}; + context.m_pDeviceContext->Map(context.cbColour, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedColour); + std::memcpy(mappedColour.pData, command.set_color.m_color, sizeof(command.set_color.m_color)); + context.m_pDeviceContext->Unmap(context.cbColour, 0); + break; + } + case COMMAND_SET_DEPTH_FUNC: + { + context.depthStencilDesc.DepthFunc = static_cast(command.set_depth_func.m_depth_func); + context.m_pDeviceContext->OMSetDepthStencilState(InternalRenderManager.GetManagedDepthStencilState(), 0); + break; + } + case COMMAND_SET_DEPTH_MASK: + { + context.depthWriteEnabled = command.set_depth_mask.m_enable ? 1 : 0; + context.depthStencilDesc.DepthWriteMask = command.set_depth_mask.m_enable ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO; + context.m_pDeviceContext->OMSetDepthStencilState(InternalRenderManager.GetManagedDepthStencilState(), 0); + break; + } + case COMMAND_SET_DEPTH_TEST: + { + context.depthTestEnabled = command.set_depth_test.m_enable ? 1 : 0; + context.depthStencilDesc.DepthEnable = command.set_depth_test.m_enable ? TRUE : FALSE; + context.m_pDeviceContext->OMSetDepthStencilState(InternalRenderManager.GetManagedDepthStencilState(), 0); + break; + } + case COMMAND_SET_LIGHTING_ENABLE: + { + context.lightingEnabled = command.set_lighting_enable.m_enable ? 1 : 0; + break; + } + case COMMAND_SET_LIGHT_ENABLE: + { + const int light = command.set_light_enable.m_light_index; + if (light >= 0 && light < 2) + { + context.lightEnabled[light] = command.set_light_enable.m_enable ? 1 : 0; + context.lightingDirty = 1; + } + break; + } + case COMMAND_SET_LIGHT_DIRECTION: + { + const int light = command.set_light_direction.m_light_index; + if (light >= 0 && light < 2) + { + context.lightDirection[light].x = command.set_light_direction.m_direction[0]; + context.lightDirection[light].y = command.set_light_direction.m_direction[1]; + context.lightDirection[light].z = command.set_light_direction.m_direction[2]; + context.lightDirection[light].w = command.set_light_direction.m_direction[3]; + context.lightingDirty = 1; + } + break; + } + case COMMAND_SET_LIGHT_COLOUR: + { + const int light = command.set_light_colour.m_light_index; + if (light >= 0 && light < 2) + { + context.lightColour[light].x = command.set_light_colour.m_color[0]; + context.lightColour[light].y = command.set_light_colour.m_color[1]; + context.lightColour[light].z = command.set_light_colour.m_color[2]; + context.lightColour[light].w = 1.0f; + context.lightingDirty = 1; + } + break; + } + case COMMAND_SET_LIGHT_AMBIENT_COLOUR: + { + context.lightAmbientColour.x = command.set_light_ambient_colour.m_color[0]; + context.lightAmbientColour.y = command.set_light_ambient_colour.m_color[1]; + context.lightAmbientColour.z = command.set_light_ambient_colour.m_color[2]; + context.lightAmbientColour.w = 1.0f; + context.lightingDirty = 1; + break; + } + case COMMAND_SET_BLEND_ENABLE: + { + context.blendDesc.RenderTarget[0].BlendEnable = command.set_blend_enable.m_enable ? TRUE : FALSE; + break; + } + case COMMAND_SET_BLEND_FUNC: + { + context.blendDesc.RenderTarget[0].SrcBlend = static_cast(command.set_blend_func.m_src); + context.blendDesc.RenderTarget[0].DestBlend = static_cast(command.set_blend_func.m_dst); + context.m_pDeviceContext->OMSetBlendState(InternalRenderManager.GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); + break; + } + case COMMAND_SET_BLEND_FACTOR: + { + const unsigned int factor = command.set_blend_factor.m_blend_factor; + context.blendFactor[0] = float((factor >> 0) & 0xFFu) / 255.0f; + context.blendFactor[1] = float((factor >> 8) & 0xFFu) / 255.0f; + context.blendFactor[2] = float((factor >> 16) & 0xFFu) / 255.0f; + context.blendFactor[3] = float((factor >> 24) & 0xFFu) / 255.0f; + context.m_pDeviceContext->OMSetBlendState(InternalRenderManager.GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); + break; + } + case COMMAND_SET_FACE_CULL: + { + context.rasterizerDesc.CullMode = command.set_face_cull.m_enable ? D3D11_CULL_BACK : D3D11_CULL_NONE; + context.m_pDeviceContext->RSSetState(InternalRenderManager.GetManagedRasterizerState()); + context.faceCullEnabled = command.set_face_cull.m_enable ? 1 : 0; + break; + } + default: + break; + } + } - if (matrixOverride) - { - const DirectX::XMMATRIX identity = DirectX::XMMatrixIdentity(); - D3D11_MAPPED_SUBRESOURCE mappedIdentity = {}; - context.m_pDeviceContext->Map(context.cbMatrix1, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedIdentity); - std::memcpy(mappedIdentity.pData, &identity, sizeof(identity)); - context.m_pDeviceContext->Unmap(context.cbMatrix1, 0); - } + if (matrixOverride) + { + const DirectX::XMMATRIX identity = DirectX::XMMatrixIdentity(); + D3D11_MAPPED_SUBRESOURCE mappedIdentity = {}; + context.m_pDeviceContext->Map(context.cbMatrix1, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedIdentity); + std::memcpy(mappedIdentity.pData, &identity, sizeof(identity)); + context.m_pDeviceContext->Unmap(context.cbMatrix1, 0); + } } bool Renderer::CBuffCall(int index, bool full) { - EnterCriticalSection(&rtl_critical_section100); + EnterCriticalSection(&rtl_critical_section100); - bool result = false; - std::int16_t* externalToInternal = static_cast(reservedRendererPtr2); - const int internalIndex = externalToInternal[index]; - if (internalIndex >= 0) - { - Renderer::Context& context = this->getContext(); - const std::uint8_t vertexType = static_cast(reservedRendererPtr6)[internalIndex]; - const std::uint8_t primitiveType = reinterpret_cast(reservedRendererPtr1)[internalIndex]; + bool result = false; + std::int16_t *externalToInternal = static_cast(reservedRendererPtr2); + const int internalIndex = externalToInternal[index]; + if (internalIndex >= 0) + { + Renderer::Context &context = this->getContext(); + const std::uint8_t vertexType = static_cast(reservedRendererPtr6)[internalIndex]; + const std::uint8_t primitiveType = reinterpret_cast(reservedRendererPtr1)[internalIndex]; - if (full) - { - if (context.matrixDirty[0]) - { - D3D11_MAPPED_SUBRESOURCE mappedMatrix0 = {}; - context.m_pDeviceContext->Map(context.cbMatrix0, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedMatrix0); - std::memcpy(mappedMatrix0.pData, this->MatrixGet(0), sizeof(DirectX::XMMATRIX)); - context.m_pDeviceContext->Unmap(context.cbMatrix0, 0); - context.matrixDirty[0] = false; - } + if (full) + { + if (context.matrixDirty[0]) + { + D3D11_MAPPED_SUBRESOURCE mappedMatrix0 = {}; + context.m_pDeviceContext->Map(context.cbMatrix0, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedMatrix0); + std::memcpy(mappedMatrix0.pData, this->MatrixGet(0), sizeof(DirectX::XMMATRIX)); + context.m_pDeviceContext->Unmap(context.cbMatrix0, 0); + context.matrixDirty[0] = false; + } - if (context.matrixDirty[1]) - { - D3D11_MAPPED_SUBRESOURCE mappedMatrix2 = {}; - context.m_pDeviceContext->Map(context.cbMatrix2, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedMatrix2); - std::memcpy(mappedMatrix2.pData, this->MatrixGet(1), sizeof(DirectX::XMMATRIX)); - context.m_pDeviceContext->Unmap(context.cbMatrix2, 0); - context.matrixDirty[1] = false; - } + if (context.matrixDirty[1]) + { + D3D11_MAPPED_SUBRESOURCE mappedMatrix2 = {}; + context.m_pDeviceContext->Map(context.cbMatrix2, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedMatrix2); + std::memcpy(mappedMatrix2.pData, this->MatrixGet(1), sizeof(DirectX::XMMATRIX)); + context.m_pDeviceContext->Unmap(context.cbMatrix2, 0); + context.matrixDirty[1] = false; + } - if (context.matrixDirty[2]) - { - D3D11_MAPPED_SUBRESOURCE mappedMatrix3 = {}; - context.m_pDeviceContext->Map(context.cbMatrix3, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedMatrix3); - std::memcpy(mappedMatrix3.pData, this->MatrixGet(2), sizeof(DirectX::XMMATRIX)); - context.m_pDeviceContext->Unmap(context.cbMatrix3, 0); - context.matrixDirty[2] = false; - } + if (context.matrixDirty[2]) + { + D3D11_MAPPED_SUBRESOURCE mappedMatrix3 = {}; + context.m_pDeviceContext->Map(context.cbMatrix3, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedMatrix3); + std::memcpy(mappedMatrix3.pData, this->MatrixGet(2), sizeof(DirectX::XMMATRIX)); + context.m_pDeviceContext->Unmap(context.cbMatrix3, 0); + context.matrixDirty[2] = false; + } - this->UpdateFogState(); - this->UpdateLightingState(); - this->UpdateViewportState(); - this->UpdateTexGenState(); + this->UpdateFogState(); + this->UpdateLightingState(); + this->UpdateViewportState(); + this->UpdateTexGenState(); - if (vertexType != activeVertexType) - { - context.m_pDeviceContext->VSSetShader(vertexShaderTable[vertexType], nullptr, 0); - context.m_pDeviceContext->IASetInputLayout(inputLayoutTable[vertexType]); - activeVertexType = vertexType; - } + if (vertexType != activeVertexType) + { + context.m_pDeviceContext->VSSetShader(vertexShaderTable[vertexType], nullptr, 0); + context.m_pDeviceContext->IASetInputLayout(inputLayoutTable[vertexType]); + activeVertexType = vertexType; + } - int pixelType = 0; - if (static_cast(context.forcedLOD) > -1) - { - const float forcedLod[4] = { static_cast(static_cast(context.forcedLOD)), 0.0f, 0.0f, 0.0f }; - D3D11_MAPPED_SUBRESOURCE mappedAux4 = {}; - context.m_pDeviceContext->Map(context.cbAux4, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedAux4); - std::memcpy(mappedAux4.pData, forcedLod, sizeof(forcedLod)); - context.m_pDeviceContext->Unmap(context.cbAux4, 0); - pixelType = C4JRender::PIXEL_SHADER_TYPE_FORCELOD; - } + int pixelType = 0; + if (static_cast(context.forcedLOD) > -1) + { + const float forcedLod[4] = {static_cast(static_cast(context.forcedLOD)), 0.0f, 0.0f, 0.0f}; + D3D11_MAPPED_SUBRESOURCE mappedAux4 = {}; + context.m_pDeviceContext->Map(context.cbAux4, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedAux4); + std::memcpy(mappedAux4.pData, forcedLod, sizeof(forcedLod)); + context.m_pDeviceContext->Unmap(context.cbAux4, 0); + pixelType = C4JRender::PIXEL_SHADER_TYPE_FORCELOD; + } - if (static_cast(pixelType) != activePixelType) - { - context.m_pDeviceContext->PSSetShader(pixelShaderTable[pixelType], nullptr, 0); - activePixelType = pixelType; - } + if (static_cast(pixelType) != activePixelType) + { + context.m_pDeviceContext->PSSetShader(pixelShaderTable[pixelType], nullptr, 0); + activePixelType = pixelType; + } - context.m_pDeviceContext->IASetPrimitiveTopology(m_Topologies[primitiveType]); + context.m_pDeviceContext->IASetPrimitiveTopology(m_Topologies[primitiveType]); - ID3D11Buffer* indexBuffer = nullptr; - if (primitiveType == C4JRender::PRIMITIVE_TYPE_QUAD_LIST) - { - indexBuffer = quadIndexBuffer; - } - else if (primitiveType == C4JRender::PRIMITIVE_TYPE_TRIANGLE_FAN) - { - indexBuffer = fanIndexBuffer; - } + ID3D11Buffer *indexBuffer = nullptr; + if (primitiveType == C4JRender::PRIMITIVE_TYPE_QUAD_LIST) + { + indexBuffer = quadIndexBuffer; + } + else if (primitiveType == C4JRender::PRIMITIVE_TYPE_TRIANGLE_FAN) + { + indexBuffer = fanIndexBuffer; + } - context.m_pDeviceContext->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R16_UINT, 0); - } + context.m_pDeviceContext->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R16_UINT, 0); + } - Renderer::CommandBuffer* commandBuffer = static_cast(reservedRendererPtr3)[internalIndex]; - commandBuffer->Render(static_cast(vertexType), context, primitiveType); + Renderer::CommandBuffer *commandBuffer = static_cast(reservedRendererPtr3)[internalIndex]; + commandBuffer->Render(static_cast(vertexType), context, primitiveType); - if (full) - { - this->MultWithStack(static_cast(reservedRendererPtr4)[internalIndex]); - context.matrixStacks[3][0] = DirectX::XMMatrixIdentity(); - context.matrixDirty[3] = true; - } + if (full) + { + this->MultWithStack(static_cast(reservedRendererPtr4)[internalIndex]); + context.matrixStacks[3][0] = DirectX::XMMatrixIdentity(); + context.matrixDirty[3] = true; + } - result = true; - } + result = true; + } - LeaveCriticalSection(&rtl_critical_section100); - return result; + LeaveCriticalSection(&rtl_critical_section100); + return result; } void Renderer::CBuffClear(int index) { - EnterCriticalSection(&rtl_critical_section100); + EnterCriticalSection(&rtl_critical_section100); - std::int16_t* externalToInternal = static_cast(reservedRendererPtr2); - const int internalIndex = externalToInternal[index]; - if (internalIndex >= 0) - { - this->DeleteInternalBuffer(internalIndex); - externalToInternal[index] = static_cast(-2); - } + std::int16_t *externalToInternal = static_cast(reservedRendererPtr2); + const int internalIndex = externalToInternal[index]; + if (internalIndex >= 0) + { + this->DeleteInternalBuffer(internalIndex); + externalToInternal[index] = static_cast(-2); + } - LeaveCriticalSection(&rtl_critical_section100); + LeaveCriticalSection(&rtl_critical_section100); } int Renderer::CBuffCreate(int count) { - const int kMaxExternalCBuffers = 0x800000; + const int kMaxExternalCBuffers = 0x800000; - EnterCriticalSection(&rtl_critical_section100); + EnterCriticalSection(&rtl_critical_section100); - int first = reservedRendererDword1; - std::int16_t* externalToInternal = static_cast(reservedRendererPtr2); + int first = reservedRendererDword1; + std::int16_t *externalToInternal = static_cast(reservedRendererPtr2); - if (first < kMaxExternalCBuffers) - { - int probe = first; - int end = first + count; - while (true) - { - int cursor = probe; - while (cursor < end && cursor < kMaxExternalCBuffers && externalToInternal[cursor] == static_cast(-1)) - { - ++cursor; - } + if (first < kMaxExternalCBuffers) + { + int probe = first; + int end = first + count; + while (true) + { + int cursor = probe; + while (cursor < end && cursor < kMaxExternalCBuffers && externalToInternal[cursor] == static_cast(-1)) + { + ++cursor; + } - if (cursor >= end) - { - break; - } + if (cursor >= end) + { + break; + } - ++first; - ++probe; - ++end; - if (first >= kMaxExternalCBuffers || end > kMaxExternalCBuffers) - { - first = -1; - break; - } - } + ++first; + ++probe; + ++end; + if (first >= kMaxExternalCBuffers || end > kMaxExternalCBuffers) + { + first = -1; + break; + } + } - if (first >= 0) - { - const int allocationEnd = first + count; - for (int i = first; i < allocationEnd; ++i) - { - externalToInternal[i] = static_cast(-2); - } + if (first >= 0) + { + const int allocationEnd = first + count; + for (int i = first; i < allocationEnd; ++i) + { + externalToInternal[i] = static_cast(-2); + } - if (reservedRendererByte1) - { - reservedRendererDword1 = allocationEnd; - } - } - } - else - { - first = -1; - } + if (reservedRendererByte1) + { + reservedRendererDword1 = allocationEnd; + } + } + } + else + { + first = -1; + } - LeaveCriticalSection(&rtl_critical_section100); - return first; + LeaveCriticalSection(&rtl_critical_section100); + return first; } void Renderer::CBuffDeferredModeEnd() { - Renderer::Context& context = this->getContext(); - if (!context.deferredModeEnabled) - { - return; - } + Renderer::Context &context = this->getContext(); + if (!context.deferredModeEnabled) + { + return; + } - EnterCriticalSection(&rtl_critical_section100); - context.deferredModeEnabled = 0; + EnterCriticalSection(&rtl_critical_section100); + context.deferredModeEnabled = 0; - std::int16_t* externalToInternal = static_cast(reservedRendererPtr2); - int* internalToExternal = static_cast(reservedRendererPtr5); - std::uint8_t* internalVertexTypes = static_cast(reservedRendererPtr6); - std::uint8_t* internalPrimitiveTypes = reinterpret_cast(reservedRendererPtr1); - Renderer::CommandBuffer** internalBuffers = static_cast(reservedRendererPtr3); - DirectX::XMMATRIX* internalMatrices = static_cast(reservedRendererPtr4); + std::int16_t *externalToInternal = static_cast(reservedRendererPtr2); + int *internalToExternal = static_cast(reservedRendererPtr5); + std::uint8_t *internalVertexTypes = static_cast(reservedRendererPtr6); + std::uint8_t *internalPrimitiveTypes = reinterpret_cast(reservedRendererPtr1); + Renderer::CommandBuffer **internalBuffers = static_cast(reservedRendererPtr3); + DirectX::XMMATRIX *internalMatrices = static_cast(reservedRendererPtr4); - for (std::vector::const_iterator it = context.deferredBuffers.begin(); it != context.deferredBuffers.end(); ++it) - { - const Renderer::DeferredCBuff& deferred = *it; - const int existingIndex = externalToInternal[deferred.m_vertex_index]; - if (existingIndex >= 0) - { - this->DeleteInternalBuffer(existingIndex); - } + for (std::vector::const_iterator it = context.deferredBuffers.begin(); it != context.deferredBuffers.end(); ++it) + { + const Renderer::DeferredCBuff &deferred = *it; + const int existingIndex = externalToInternal[deferred.m_vertex_index]; + if (existingIndex >= 0) + { + this->DeleteInternalBuffer(existingIndex); + } - if (static_cast(reservedRendererDword2 + reservedRendererDword3 + 10u) > 16000) - { - DebugBreak(); - } + if (static_cast(reservedRendererDword2 + reservedRendererDword3 + 10u) > 16000) + { + DebugBreak(); + } - const int internalSlot = reservedRendererDword2; - ++reservedRendererDword2; + const int internalSlot = reservedRendererDword2; + ++reservedRendererDword2; - externalToInternal[deferred.m_vertex_index] = static_cast(internalSlot); - internalToExternal[internalSlot] = deferred.m_vertex_index; - internalVertexTypes[internalSlot] = static_cast(deferred.m_vertex_type); - internalPrimitiveTypes[internalSlot] = static_cast(deferred.m_primitive_type); - internalBuffers[internalSlot] = deferred.m_command_buf; - internalMatrices[internalSlot] = deferred.m_matrix; - } + externalToInternal[deferred.m_vertex_index] = static_cast(internalSlot); + internalToExternal[internalSlot] = deferred.m_vertex_index; + internalVertexTypes[internalSlot] = static_cast(deferred.m_vertex_type); + internalPrimitiveTypes[internalSlot] = static_cast(deferred.m_primitive_type); + internalBuffers[internalSlot] = deferred.m_command_buf; + internalMatrices[internalSlot] = deferred.m_matrix; + } - context.deferredBuffers.clear(); - LeaveCriticalSection(&rtl_critical_section100); + context.deferredBuffers.clear(); + LeaveCriticalSection(&rtl_critical_section100); } void Renderer::CBuffDeferredModeStart() { - this->getContext().deferredModeEnabled = 1; + this->getContext().deferredModeEnabled = 1; } void Renderer::CBuffDelete(int first, int count) { - EnterCriticalSection(&rtl_critical_section100); + EnterCriticalSection(&rtl_critical_section100); - std::int16_t* externalToInternal = static_cast(reservedRendererPtr2); - const int end = first + count; - for (int i = first; i < end; ++i) - { - const int internalIndex = externalToInternal[i]; - if (internalIndex >= 0) - { - this->DeleteInternalBuffer(internalIndex); - } + std::int16_t *externalToInternal = static_cast(reservedRendererPtr2); + const int end = first + count; + for (int i = first; i < end; ++i) + { + const int internalIndex = externalToInternal[i]; + if (internalIndex >= 0) + { + this->DeleteInternalBuffer(internalIndex); + } - externalToInternal[i] = static_cast(-1); - } + externalToInternal[i] = static_cast(-1); + } - LeaveCriticalSection(&rtl_critical_section100); + LeaveCriticalSection(&rtl_critical_section100); } void Renderer::CBuffEnd() { - Renderer::Context& context = this->getContext(); - EnterCriticalSection(&rtl_critical_section100); + Renderer::Context &context = this->getContext(); + EnterCriticalSection(&rtl_critical_section100); - if (context.deferredModeEnabled) - { - Renderer::DeferredCBuff deferred; - deferred.m_command_buf = context.commandBuffer; - deferred.m_vertex_index = context.recordingBufferIndex; - deferred.m_vertex_type = context.recordingVertexType; - deferred.m_primitive_type = context.recordingPrimitiveType; - deferred.m_matrix = context.matrixStacks[3][0]; - context.deferredBuffers.push_back(deferred); - } - else - { - std::int16_t* externalToInternal = static_cast(reservedRendererPtr2); - int* internalToExternal = static_cast(reservedRendererPtr5); - std::uint8_t* internalVertexTypes = static_cast(reservedRendererPtr6); - std::uint8_t* internalPrimitiveTypes = reinterpret_cast(reservedRendererPtr1); - Renderer::CommandBuffer** internalBuffers = static_cast(reservedRendererPtr3); - DirectX::XMMATRIX* internalMatrices = static_cast(reservedRendererPtr4); + if (context.deferredModeEnabled) + { + Renderer::DeferredCBuff deferred; + deferred.m_command_buf = context.commandBuffer; + deferred.m_vertex_index = context.recordingBufferIndex; + deferred.m_vertex_type = context.recordingVertexType; + deferred.m_primitive_type = context.recordingPrimitiveType; + deferred.m_matrix = context.matrixStacks[3][0]; + context.deferredBuffers.push_back(deferred); + } + else + { + std::int16_t *externalToInternal = static_cast(reservedRendererPtr2); + int *internalToExternal = static_cast(reservedRendererPtr5); + std::uint8_t *internalVertexTypes = static_cast(reservedRendererPtr6); + std::uint8_t *internalPrimitiveTypes = reinterpret_cast(reservedRendererPtr1); + Renderer::CommandBuffer **internalBuffers = static_cast(reservedRendererPtr3); + DirectX::XMMATRIX *internalMatrices = static_cast(reservedRendererPtr4); - const int existingIndex = externalToInternal[context.recordingBufferIndex]; - if (existingIndex >= 0) - { - this->DeleteInternalBuffer(existingIndex); - } + const int existingIndex = externalToInternal[context.recordingBufferIndex]; + if (existingIndex >= 0) + { + this->DeleteInternalBuffer(existingIndex); + } - if (static_cast(reservedRendererDword2 + reservedRendererDword3 + 10u) > 16000) - { - DebugBreak(); - } + if (static_cast(reservedRendererDword2 + reservedRendererDword3 + 10u) > 16000) + { + DebugBreak(); + } - const int internalSlot = reservedRendererDword2; - ++reservedRendererDword2; + const int internalSlot = reservedRendererDword2; + ++reservedRendererDword2; - externalToInternal[context.recordingBufferIndex] = static_cast(internalSlot); - internalToExternal[internalSlot] = context.recordingBufferIndex; - internalVertexTypes[internalSlot] = static_cast(context.recordingVertexType); - internalPrimitiveTypes[internalSlot] = static_cast(context.recordingPrimitiveType); - internalBuffers[internalSlot] = context.commandBuffer; - internalMatrices[internalSlot] = context.matrixStacks[3][0]; - } + externalToInternal[context.recordingBufferIndex] = static_cast(internalSlot); + internalToExternal[internalSlot] = context.recordingBufferIndex; + internalVertexTypes[internalSlot] = static_cast(context.recordingVertexType); + internalPrimitiveTypes[internalSlot] = static_cast(context.recordingPrimitiveType); + internalBuffers[internalSlot] = context.commandBuffer; + internalMatrices[internalSlot] = context.matrixStacks[3][0]; + } - context.matrixModeType = 0; - context.commandBuffer->EndRecording(m_pDevice); - context.commandBuffer = nullptr; + context.matrixModeType = 0; + context.commandBuffer->EndRecording(m_pDevice); + context.commandBuffer = nullptr; - LeaveCriticalSection(&rtl_critical_section100); + LeaveCriticalSection(&rtl_critical_section100); } void Renderer::CBuffLockStaticCreations() { - reservedRendererByte1 = 0; + reservedRendererByte1 = 0; } int Renderer::CBuffSize(int index) { - if (index == -1) - { - return totalAlloc < 0 ? 0 : totalAlloc; - } + if (index == -1) + { + return totalAlloc < 0 ? 0 : totalAlloc; + } - unsigned int size = 0; - EnterCriticalSection(&rtl_critical_section100); - const int internalIndex = static_cast(reservedRendererPtr2)[index]; - if (internalIndex >= 0) - { - size = static_cast(reservedRendererPtr3)[internalIndex]->GetAllocated(); - } - LeaveCriticalSection(&rtl_critical_section100); - return size; + unsigned int size = 0; + EnterCriticalSection(&rtl_critical_section100); + const int internalIndex = static_cast(reservedRendererPtr2)[index]; + if (internalIndex >= 0) + { + size = static_cast(reservedRendererPtr3)[internalIndex]->GetAllocated(); + } + LeaveCriticalSection(&rtl_critical_section100); + return size; } void Renderer::CBuffStart(int index, bool full) { - Renderer::Context& context = this->getContext(); - context.commandBuffer = new (std::nothrow) Renderer::CommandBuffer(full); - context.recordingBufferIndex = index; - context.matrixModeType = 3; - context.matrixStackDepth[3] = 0; - context.matrixStacks[3][0] = DirectX::XMMatrixIdentity(); - context.matrixDirty[3] = true; + Renderer::Context &context = this->getContext(); + context.commandBuffer = new (std::nothrow) Renderer::CommandBuffer(full); + context.recordingBufferIndex = index; + context.matrixModeType = 3; + context.matrixStackDepth[3] = 0; + context.matrixStacks[3][0] = DirectX::XMMatrixIdentity(); + context.matrixDirty[3] = true; } void Renderer::CBuffTick() { - const int kMaxInternalCBuffers = 16000; + const int kMaxInternalCBuffers = 16000; - EnterCriticalSection(&rtl_critical_section100); + EnterCriticalSection(&rtl_critical_section100); - Renderer::CommandBuffer** buffers = static_cast(reservedRendererPtr3); - const int firstPending = kMaxInternalCBuffers - static_cast(reservedRendererDword3); - for (int i = firstPending; i < kMaxInternalCBuffers; ++i) - { - Renderer::CommandBuffer* buffer = buffers[i]; - if (buffer) - { - delete buffer; - } - buffers[i] = nullptr; - } + Renderer::CommandBuffer **buffers = static_cast(reservedRendererPtr3); + const int firstPending = kMaxInternalCBuffers - static_cast(reservedRendererDword3); + for (int i = firstPending; i < kMaxInternalCBuffers; ++i) + { + Renderer::CommandBuffer *buffer = buffers[i]; + if (buffer) + { + delete buffer; + } + buffers[i] = nullptr; + } - reservedRendererDword3 = 0; - LeaveCriticalSection(&rtl_critical_section100); + reservedRendererDword3 = 0; + LeaveCriticalSection(&rtl_critical_section100); } void Renderer::DeleteInternalBuffer(int index) { - const int kMaxInternalCBuffers = 16000; + const int kMaxInternalCBuffers = 16000; - EnterCriticalSection(&rtl_critical_section100); + EnterCriticalSection(&rtl_critical_section100); - Renderer::CommandBuffer** internalBuffers = static_cast(reservedRendererPtr3); - DirectX::XMMATRIX* internalMatrices = static_cast(reservedRendererPtr4); - std::uint8_t* internalVertexTypes = static_cast(reservedRendererPtr6); - std::uint8_t* internalPrimitiveTypes = reinterpret_cast(reservedRendererPtr1); - std::int16_t* externalToInternal = static_cast(reservedRendererPtr2); - int* internalToExternal = static_cast(reservedRendererPtr5); + Renderer::CommandBuffer **internalBuffers = static_cast(reservedRendererPtr3); + DirectX::XMMATRIX *internalMatrices = static_cast(reservedRendererPtr4); + std::uint8_t *internalVertexTypes = static_cast(reservedRendererPtr6); + std::uint8_t *internalPrimitiveTypes = reinterpret_cast(reservedRendererPtr1); + std::int16_t *externalToInternal = static_cast(reservedRendererPtr2); + int *internalToExternal = static_cast(reservedRendererPtr5); - ++reservedRendererDword3; - const int recycledSlot = kMaxInternalCBuffers - static_cast(reservedRendererDword3); + ++reservedRendererDword3; + const int recycledSlot = kMaxInternalCBuffers - static_cast(reservedRendererDword3); - internalBuffers[recycledSlot] = internalBuffers[index]; - internalMatrices[recycledSlot] = internalMatrices[index]; + internalBuffers[recycledSlot] = internalBuffers[index]; + internalMatrices[recycledSlot] = internalMatrices[index]; - if (reservedRendererDword2-- != 1) - { - const int lastActive = reservedRendererDword2; + if (reservedRendererDword2-- != 1) + { + const int lastActive = reservedRendererDword2; - internalBuffers[index] = internalBuffers[lastActive]; - internalMatrices[index] = internalMatrices[lastActive]; - internalVertexTypes[index] = internalVertexTypes[lastActive]; - internalPrimitiveTypes[index] = internalPrimitiveTypes[lastActive]; + internalBuffers[index] = internalBuffers[lastActive]; + internalMatrices[index] = internalMatrices[lastActive]; + internalVertexTypes[index] = internalVertexTypes[lastActive]; + internalPrimitiveTypes[index] = internalPrimitiveTypes[lastActive]; - const int externalIndex = internalToExternal[lastActive]; - externalToInternal[externalIndex] = static_cast(index); - internalToExternal[index] = externalIndex; - } + const int externalIndex = internalToExternal[lastActive]; + externalToInternal[externalIndex] = static_cast(index); + internalToExternal[index] = externalIndex; + } - LeaveCriticalSection(&rtl_critical_section100); + LeaveCriticalSection(&rtl_critical_section100); } diff --git a/Windows_Libs/Dev/Render/RendererCore.cpp b/Windows_Libs/Dev/Render/RendererCore.cpp index 9d2f666..8ae5cec 100644 --- a/Windows_Libs/Dev/Render/RendererCore.cpp +++ b/Windows_Libs/Dev/Render/RendererCore.cpp @@ -9,203 +9,184 @@ #include #include +Renderer InternalRenderManager; + +DWORD Renderer::tlsIdx = TlsAlloc(); +int Renderer::totalAlloc = 0; +_RTL_CRITICAL_SECTION Renderer::totalAllocCS = {0}; + static const unsigned int kVertexBufferSize = 0x100000u; static const float kAspectRatio = 1.7777778f; static const unsigned int kScreenGrabWidth = 1920u; static const unsigned int kScreenGrabHeight = 1080u; static const unsigned int kThumbnailSize = 64u; -static const unsigned int g_vertexStrides[C4JRender::VERTEX_TYPE_COUNT] = { - 32u, 16u, 32u, 32u}; +static const unsigned int g_vertexStrides[C4JRender::VERTEX_TYPE_COUNT] = {32u, 16u, 32u, 32u}; -Renderer InternalRenderManager; +unsigned int Renderer::s_auiWidths[MAX_MIP_LEVELS + 1] = {1920u, 512u, 256u, 128u, 64u, 0u}; + +unsigned int Renderer::s_auiHeights[MAX_MIP_LEVELS + 1] = {1080u, 512u, 256u, 128u, 64u, 0xFFFFFFFFu}; -DWORD Renderer::tlsIdx = TlsAlloc(); -unsigned int Renderer::s_auiWidths[MAX_MIP_LEVELS + 1] = {1920u, 512u, 256u, - 128u, 64u, 0u}; -unsigned int Renderer::s_auiHeights[MAX_MIP_LEVELS + 1] = { - 1080u, 512u, 256u, 128u, 64u, 0xFFFFFFFFu}; D3D11_INPUT_ELEMENT_DESC Renderer::g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1[5] = { - {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, - D3D11_INPUT_PER_VERTEX_DATA, 0}, - {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, - D3D11_INPUT_PER_VERTEX_DATA, 0}, - {"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, - 0}, - {"NORMAL", 0, DXGI_FORMAT_R8G8B8A8_SNORM, 0, 24, - D3D11_INPUT_PER_VERTEX_DATA, 0}, - {"TEXCOORD", 1, DXGI_FORMAT_R16G16_SINT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, - 0}, + {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, + {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}, + {"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0}, + {"NORMAL", 0, DXGI_FORMAT_R8G8B8A8_SNORM, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0}, + {"TEXCOORD", 1, DXGI_FORMAT_R16G16_SINT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0}, }; + D3D11_INPUT_ELEMENT_DESC Renderer::g_vertex_PTN_Elements_Compressed[2] = { - {"POSITION", 0, DXGI_FORMAT_R16G16B16A16_SINT, 0, 0, - D3D11_INPUT_PER_VERTEX_DATA, 0}, - {"TEXCOORD", 0, DXGI_FORMAT_R16G16B16A16_SINT, 0, 8, - D3D11_INPUT_PER_VERTEX_DATA, 0}, + {"POSITION", 0, DXGI_FORMAT_R16G16B16A16_SINT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, + {"TEXCOORD", 0, DXGI_FORMAT_R16G16B16A16_SINT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0}, }; + D3D11_PRIMITIVE_TOPOLOGY Renderer::g_topologies[C4JRender::PRIMITIVE_TYPE_COUNT] = { - D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, - D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, - D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, - D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, - D3D11_PRIMITIVE_TOPOLOGY_LINELIST, - D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP, + D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, + D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, D3D11_PRIMITIVE_TOPOLOGY_LINELIST, D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP, }; -int Renderer::totalAlloc = 0; -_RTL_CRITICAL_SECTION Renderer::totalAllocCS = {0}; -Renderer::Context::Context(ID3D11Device *device, - ID3D11DeviceContext *deviceContext) - : m_pDeviceContext(deviceContext), userAnnotation(nullptr), - contextStateFlags(0), matrixModeType(0), boundTextureIndex(0), - faceCullEnabled(1), depthTestEnabled(1), alphaTestEnabled(0), - alphaReference(1.0f), depthWriteEnabled(1), fogEnabled(0), - fogNearDistance(0.0f), fogFarDistance(0.0f), fogDensity(0.0f), - fogColourRed(0.0f), fogColourBlue(0.0f), fogColourGreen(0.0f), fogMode(0), - lightingEnabled(0), lightingDirty(0), forcedLOD(0xFFFFFFFFu), - cbMatrix0(nullptr), cbMatrix1(nullptr), cbMatrix2(nullptr), - cbMatrix3(nullptr), cbVertexTexcoord(nullptr), cbFogParams(nullptr), - cbLighting(nullptr), cbTexGen(nullptr), cbAux0(nullptr), cbAux1(nullptr), - cbColour(nullptr), cbFogColour(nullptr), cbAux2(nullptr), - cbAlphaTest(nullptr), cbAux3(nullptr), cbAux4(nullptr), - dynamicVertexBase(0), dynamicVertexOffset(0), - dynamicVertexBuffer(nullptr), commandBuffer(nullptr), - recordingBufferIndex(0), recordingVertexType(0), - recordingPrimitiveType(0), deferredModeEnabled(0), deferredBuffers(), - reservedContext0(0), reservedContext1(0) { - deviceContext->QueryInterface(IID_PPV_ARGS(&userAnnotation)); - std::memset(matrixStacks, 0, sizeof(matrixStacks)); - std::memset(matrixDirty, 0, sizeof(matrixDirty)); - std::memset(matrixStackDepth, 0, sizeof(matrixStackDepth)); - std::memset(lightEnabled, 0, sizeof(lightEnabled)); - std::memset(lightDirection, 0, sizeof(lightDirection)); - std::memset(lightColour, 0, sizeof(lightColour)); - std::memset(&lightAmbientColour, 0, sizeof(lightAmbientColour)); - std::memset(texGenMatrices, 0, sizeof(texGenMatrices)); - std::memset(&blendDesc, 0, sizeof(blendDesc)); - std::memset(&depthStencilDesc, 0, sizeof(depthStencilDesc)); - std::memset(&rasterizerDesc, 0, sizeof(rasterizerDesc)); - blendFactor[0] = 0.0f; - blendFactor[1] = 0.0f; - blendFactor[2] = 0.0f; - blendFactor[3] = 0.0f; +Renderer::Context::Context(ID3D11Device *device, ID3D11DeviceContext *deviceContext) + : m_pDeviceContext(deviceContext), userAnnotation(nullptr), contextStateFlags(0), matrixModeType(0), boundTextureIndex(0), faceCullEnabled(1), + depthTestEnabled(1), alphaTestEnabled(0), alphaReference(1.0f), depthWriteEnabled(1), fogEnabled(0), fogNearDistance(0.0f), + fogFarDistance(0.0f), fogDensity(0.0f), fogColourRed(0.0f), fogColourBlue(0.0f), fogColourGreen(0.0f), fogMode(0), lightingEnabled(0), + lightingDirty(0), forcedLOD(0xFFFFFFFFu), cbMatrix0(nullptr), cbMatrix1(nullptr), cbMatrix2(nullptr), cbMatrix3(nullptr), + cbVertexTexcoord(nullptr), cbFogParams(nullptr), cbLighting(nullptr), cbTexGen(nullptr), cbAux0(nullptr), cbAux1(nullptr), cbColour(nullptr), + cbFogColour(nullptr), cbAux2(nullptr), cbAlphaTest(nullptr), cbAux3(nullptr), cbAux4(nullptr), dynamicVertexBase(0), dynamicVertexOffset(0), + dynamicVertexBuffer(nullptr), commandBuffer(nullptr), recordingBufferIndex(0), recordingVertexType(0), recordingPrimitiveType(0), + deferredModeEnabled(0), deferredBuffers(), reservedContext0(0), reservedContext1(0) +{ + deviceContext->QueryInterface(IID_PPV_ARGS(&userAnnotation)); + std::memset(matrixStacks, 0, sizeof(matrixStacks)); + std::memset(matrixDirty, 0, sizeof(matrixDirty)); + std::memset(matrixStackDepth, 0, sizeof(matrixStackDepth)); + std::memset(lightEnabled, 0, sizeof(lightEnabled)); + std::memset(lightDirection, 0, sizeof(lightDirection)); + std::memset(lightColour, 0, sizeof(lightColour)); + std::memset(&lightAmbientColour, 0, sizeof(lightAmbientColour)); + std::memset(texGenMatrices, 0, sizeof(texGenMatrices)); + std::memset(&blendDesc, 0, sizeof(blendDesc)); + std::memset(&depthStencilDesc, 0, sizeof(depthStencilDesc)); + std::memset(&rasterizerDesc, 0, sizeof(rasterizerDesc)); + blendFactor[0] = 0.0f; + blendFactor[1] = 0.0f; + blendFactor[2] = 0.0f; + blendFactor[3] = 0.0f; - const DirectX::XMMATRIX identity = DirectX::XMMatrixIdentity(); - for (unsigned int i = 0; i < 4; ++i) { - matrixStacks[i][0] = identity; - matrixStackDepth[i] = 0; - } + const DirectX::XMMATRIX identity = DirectX::XMMatrixIdentity(); + for (unsigned int i = 0; i < 4; ++i) + { + matrixStacks[i][0] = identity; + matrixStackDepth[i] = 0; + } - blendDesc.AlphaToCoverageEnable = FALSE; - blendDesc.IndependentBlendEnable = FALSE; - blendDesc.RenderTarget[0].BlendEnable = FALSE; - blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; - blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO; - blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; - blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; - blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; - blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; - blendDesc.RenderTarget[0].RenderTargetWriteMask = - D3D11_COLOR_WRITE_ENABLE_ALL; + blendDesc.AlphaToCoverageEnable = FALSE; + blendDesc.IndependentBlendEnable = FALSE; + blendDesc.RenderTarget[0].BlendEnable = FALSE; + blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; + blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO; + blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; + blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; + blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; - depthStencilDesc.DepthEnable = TRUE; - depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; - depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; - depthStencilDesc.StencilEnable = FALSE; - depthStencilDesc.StencilReadMask = 0xFFu; - depthStencilDesc.StencilWriteMask = 0xFFu; - depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; - depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; - depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; - depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; - depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; - depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; - depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; - depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + depthStencilDesc.DepthEnable = TRUE; + depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; + depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; + depthStencilDesc.StencilEnable = FALSE; + depthStencilDesc.StencilReadMask = 0xFFu; + depthStencilDesc.StencilWriteMask = 0xFFu; + depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; + depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; + depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; - rasterizerDesc.FillMode = D3D11_FILL_SOLID; - rasterizerDesc.CullMode = D3D11_CULL_BACK; - rasterizerDesc.FrontCounterClockwise = TRUE; - rasterizerDesc.DepthBias = 0; - rasterizerDesc.DepthBiasClamp = 0.0f; - rasterizerDesc.SlopeScaledDepthBias = 0.0f; - rasterizerDesc.DepthClipEnable = TRUE; - rasterizerDesc.ScissorEnable = FALSE; - rasterizerDesc.MultisampleEnable = TRUE; - rasterizerDesc.AntialiasedLineEnable = FALSE; + rasterizerDesc.FillMode = D3D11_FILL_SOLID; + rasterizerDesc.CullMode = D3D11_CULL_BACK; + rasterizerDesc.FrontCounterClockwise = TRUE; + rasterizerDesc.DepthBias = 0; + rasterizerDesc.DepthBiasClamp = 0.0f; + rasterizerDesc.SlopeScaledDepthBias = 0.0f; + rasterizerDesc.DepthClipEnable = TRUE; + rasterizerDesc.ScissorEnable = FALSE; + rasterizerDesc.MultisampleEnable = TRUE; + rasterizerDesc.AntialiasedLineEnable = FALSE; - std::memset(lightDirection, 0, sizeof(lightDirection)); - std::memset(lightColour, 0, sizeof(lightColour)); - std::memset(&lightAmbientColour, 0, sizeof(lightAmbientColour)); - std::memset(texGenMatrices, 0, sizeof(texGenMatrices)); + std::memset(lightDirection, 0, sizeof(lightDirection)); + std::memset(lightColour, 0, sizeof(lightColour)); + std::memset(&lightAmbientColour, 0, sizeof(lightAmbientColour)); + std::memset(texGenMatrices, 0, sizeof(texGenMatrices)); - const float zero4[4] = {0.0f, 0.0f, 0.0f, 0.0f}; - const float one4[4] = {1.0f, 1.0f, 1.0f, 1.0f}; - const float alpha4[4] = {0.0f, 0.0f, 0.0f, 1.0f}; + const float zero4[4] = {0.0f, 0.0f, 0.0f, 0.0f}; + const float one4[4] = {1.0f, 1.0f, 1.0f, 1.0f}; + const float alpha4[4] = {0.0f, 0.0f, 0.0f, 1.0f}; - D3D11_BUFFER_DESC cbDesc = {}; - cbDesc.Usage = D3D11_USAGE_DYNAMIC; - cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; - cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + D3D11_BUFFER_DESC cbDesc = {}; + cbDesc.Usage = D3D11_USAGE_DYNAMIC; + cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; + cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; - D3D11_SUBRESOURCE_DATA cbData = {}; - cbDesc.ByteWidth = sizeof(DirectX::XMMATRIX); - cbData.pSysMem = &identity; - device->CreateBuffer(&cbDesc, &cbData, &cbMatrix0); - device->CreateBuffer(&cbDesc, &cbData, &cbMatrix1); - device->CreateBuffer(&cbDesc, &cbData, &cbMatrix2); - device->CreateBuffer(&cbDesc, &cbData, &cbMatrix3); + D3D11_SUBRESOURCE_DATA cbData = {}; + cbDesc.ByteWidth = sizeof(DirectX::XMMATRIX); + cbData.pSysMem = &identity; + device->CreateBuffer(&cbDesc, &cbData, &cbMatrix0); + device->CreateBuffer(&cbDesc, &cbData, &cbMatrix1); + device->CreateBuffer(&cbDesc, &cbData, &cbMatrix2); + device->CreateBuffer(&cbDesc, &cbData, &cbMatrix3); - cbDesc.ByteWidth = sizeof(zero4); - cbData.pSysMem = zero4; - device->CreateBuffer(&cbDesc, &cbData, &cbVertexTexcoord); - device->CreateBuffer(&cbDesc, &cbData, &cbFogParams); + cbDesc.ByteWidth = sizeof(zero4); + cbData.pSysMem = zero4; + device->CreateBuffer(&cbDesc, &cbData, &cbVertexTexcoord); + device->CreateBuffer(&cbDesc, &cbData, &cbFogParams); - const UINT lightingBytes = - sizeof(lightDirection) + sizeof(lightColour) + sizeof(lightAmbientColour); - cbDesc.ByteWidth = lightingBytes; - cbData.pSysMem = lightDirection; - device->CreateBuffer(&cbDesc, &cbData, &cbLighting); + const UINT lightingBytes = sizeof(lightDirection) + sizeof(lightColour) + sizeof(lightAmbientColour); + cbDesc.ByteWidth = lightingBytes; + cbData.pSysMem = lightDirection; + device->CreateBuffer(&cbDesc, &cbData, &cbLighting); - cbDesc.ByteWidth = sizeof(texGenMatrices); - cbData.pSysMem = texGenMatrices; - device->CreateBuffer(&cbDesc, &cbData, &cbTexGen); + cbDesc.ByteWidth = sizeof(texGenMatrices); + cbData.pSysMem = texGenMatrices; + device->CreateBuffer(&cbDesc, &cbData, &cbTexGen); - cbDesc.ByteWidth = sizeof(zero4); - cbData.pSysMem = zero4; - device->CreateBuffer(&cbDesc, &cbData, &cbAux0); - device->CreateBuffer(&cbDesc, &cbData, &cbAux1); + cbDesc.ByteWidth = sizeof(zero4); + cbData.pSysMem = zero4; + device->CreateBuffer(&cbDesc, &cbData, &cbAux0); + device->CreateBuffer(&cbDesc, &cbData, &cbAux1); - cbDesc.ByteWidth = sizeof(one4); - cbData.pSysMem = one4; - device->CreateBuffer(&cbDesc, &cbData, &cbColour); - device->CreateBuffer(&cbDesc, &cbData, &cbFogColour); - device->CreateBuffer(&cbDesc, &cbData, &cbAux2); + cbDesc.ByteWidth = sizeof(one4); + cbData.pSysMem = one4; + device->CreateBuffer(&cbDesc, &cbData, &cbColour); + device->CreateBuffer(&cbDesc, &cbData, &cbFogColour); + device->CreateBuffer(&cbDesc, &cbData, &cbAux2); - cbDesc.ByteWidth = sizeof(alpha4); - cbData.pSysMem = alpha4; - device->CreateBuffer(&cbDesc, &cbData, &cbAlphaTest); + cbDesc.ByteWidth = sizeof(alpha4); + cbData.pSysMem = alpha4; + device->CreateBuffer(&cbDesc, &cbData, &cbAlphaTest); - cbDesc.ByteWidth = sizeof(zero4); - cbData.pSysMem = zero4; - device->CreateBuffer(&cbDesc, &cbData, &cbAux3); - device->CreateBuffer(&cbDesc, &cbData, &cbAux4); + cbDesc.ByteWidth = sizeof(zero4); + cbData.pSysMem = zero4; + device->CreateBuffer(&cbDesc, &cbData, &cbAux3); + device->CreateBuffer(&cbDesc, &cbData, &cbAux4); - deviceContext->VSSetConstantBuffers(0, 10, &cbMatrix0); - deviceContext->PSSetConstantBuffers(0, 6, &cbColour); + deviceContext->VSSetConstantBuffers(0, 10, &cbMatrix0); + deviceContext->PSSetConstantBuffers(0, 6, &cbColour); - { - void *dynamicVertexPtr = operator new[](kVertexBufferSize); - dynamicVertexBase = reinterpret_cast(dynamicVertexPtr); - } - dynamicVertexOffset = 0; + { + void *dynamicVertexPtr = operator new[](kVertexBufferSize); + dynamicVertexBase = reinterpret_cast(dynamicVertexPtr); + } + dynamicVertexOffset = 0; - D3D11_BUFFER_DESC vbDesc = {}; - vbDesc.ByteWidth = kVertexBufferSize; - vbDesc.Usage = D3D11_USAGE_DYNAMIC; - vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; - vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; - device->CreateBuffer(&vbDesc, nullptr, &dynamicVertexBuffer); + D3D11_BUFFER_DESC vbDesc = {}; + vbDesc.ByteWidth = kVertexBufferSize; + vbDesc.Usage = D3D11_USAGE_DYNAMIC; + vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + device->CreateBuffer(&vbDesc, nullptr, &dynamicVertexBuffer); } void Renderer::BeginConditionalRendering(int) {} @@ -214,746 +195,749 @@ void Renderer::BeginConditionalSurvey(int) {} void Renderer::CaptureScreen(ImageFileBuffer *, XSOCIAL_PREVIEWIMAGE *) {} -void Renderer::Clear(int flags, D3D11_RECT *) { - Renderer::Context &context = this->getContext(); +void Renderer::Clear(int flags, D3D11_RECT *) +{ + Renderer::Context &context = this->getContext(); - ID3D11BlendState *blendState = nullptr; - ID3D11DepthStencilState *depthState = nullptr; - ID3D11RasterizerState *rasterizerState = nullptr; + ID3D11BlendState *blendState = nullptr; + ID3D11DepthStencilState *depthState = nullptr; + ID3D11RasterizerState *rasterizerState = nullptr; - D3D11_BLEND_DESC blendDesc = {}; - blendDesc.AlphaToCoverageEnable = FALSE; - blendDesc.IndependentBlendEnable = FALSE; - blendDesc.RenderTarget[0].BlendEnable = FALSE; - blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; - blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO; - blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; - blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; - blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; - blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; - blendDesc.RenderTarget[0].RenderTargetWriteMask = - (flags & CLEAR_COLOUR_FLAG) ? D3D11_COLOR_WRITE_ENABLE_ALL : 0; - m_pDevice->CreateBlendState(&blendDesc, &blendState); + D3D11_BLEND_DESC blendDesc = {}; + blendDesc.AlphaToCoverageEnable = FALSE; + blendDesc.IndependentBlendEnable = FALSE; + blendDesc.RenderTarget[0].BlendEnable = FALSE; + blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; + blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO; + blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; + blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; + blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[0].RenderTargetWriteMask = (flags & CLEAR_COLOUR_FLAG) ? D3D11_COLOR_WRITE_ENABLE_ALL : 0; + m_pDevice->CreateBlendState(&blendDesc, &blendState); - D3D11_DEPTH_STENCIL_DESC depthDesc = {}; - depthDesc.DepthEnable = (flags & CLEAR_DEPTH_FLAG) ? TRUE : FALSE; - depthDesc.DepthWriteMask = depthDesc.DepthEnable - ? D3D11_DEPTH_WRITE_MASK_ALL - : D3D11_DEPTH_WRITE_MASK_ZERO; - depthDesc.DepthFunc = D3D11_COMPARISON_ALWAYS; - depthDesc.StencilEnable = FALSE; - depthDesc.StencilReadMask = 0xFFu; - depthDesc.StencilWriteMask = 0xFFu; - depthDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; - depthDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; - depthDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; - depthDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; - depthDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; - depthDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; - depthDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; - depthDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; - m_pDevice->CreateDepthStencilState(&depthDesc, &depthState); + D3D11_DEPTH_STENCIL_DESC depthDesc = {}; + depthDesc.DepthEnable = (flags & CLEAR_DEPTH_FLAG) ? TRUE : FALSE; + depthDesc.DepthWriteMask = depthDesc.DepthEnable ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO; + depthDesc.DepthFunc = D3D11_COMPARISON_ALWAYS; + depthDesc.StencilEnable = FALSE; + depthDesc.StencilReadMask = 0xFFu; + depthDesc.StencilWriteMask = 0xFFu; + depthDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + depthDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + depthDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; + depthDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + depthDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + depthDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + depthDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; + depthDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + m_pDevice->CreateDepthStencilState(&depthDesc, &depthState); - D3D11_RASTERIZER_DESC rasterDesc = {}; - rasterDesc.FillMode = D3D11_FILL_SOLID; - rasterDesc.CullMode = D3D11_CULL_NONE; - rasterDesc.DepthClipEnable = TRUE; - rasterDesc.MultisampleEnable = TRUE; - m_pDevice->CreateRasterizerState(&rasterDesc, &rasterizerState); + D3D11_RASTERIZER_DESC rasterDesc = {}; + rasterDesc.FillMode = D3D11_FILL_SOLID; + rasterDesc.CullMode = D3D11_CULL_NONE; + rasterDesc.DepthClipEnable = TRUE; + rasterDesc.MultisampleEnable = TRUE; + m_pDevice->CreateRasterizerState(&rasterDesc, &rasterizerState); - context.m_pDeviceContext->VSSetShader(screenClearVertexShader, nullptr, 0); - context.m_pDeviceContext->IASetInputLayout(nullptr); - context.m_pDeviceContext->PSSetShader(screenClearPixelShader, nullptr, 0); - context.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, - depthStencilView); - context.m_pDeviceContext->OMSetBlendState(blendState, nullptr, 0xFFFFFFFFu); - context.m_pDeviceContext->OMSetDepthStencilState(depthState, 0); - context.m_pDeviceContext->RSSetState(rasterizerState); - context.m_pDeviceContext->PSSetShaderResources(0, 0, nullptr); - context.m_pDeviceContext->IASetPrimitiveTopology( - D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); - context.m_pDeviceContext->Draw(4, 0); + context.m_pDeviceContext->VSSetShader(screenClearVertexShader, nullptr, 0); + context.m_pDeviceContext->IASetInputLayout(nullptr); + context.m_pDeviceContext->PSSetShader(screenClearPixelShader, nullptr, 0); + context.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, depthStencilView); + context.m_pDeviceContext->OMSetBlendState(blendState, nullptr, 0xFFFFFFFFu); + context.m_pDeviceContext->OMSetDepthStencilState(depthState, 0); + context.m_pDeviceContext->RSSetState(rasterizerState); + context.m_pDeviceContext->PSSetShaderResources(0, 0, nullptr); + context.m_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); + context.m_pDeviceContext->Draw(4, 0); - if (blendState) { - blendState->Release(); - blendState = nullptr; - } - if (depthState) { - depthState->Release(); - depthState = nullptr; - } - if (rasterizerState) { - rasterizerState->Release(); - rasterizerState = nullptr; - } + if (blendState) + { + blendState->Release(); + blendState = nullptr; + } + if (depthState) + { + depthState->Release(); + depthState = nullptr; + } + if (rasterizerState) + { + rasterizerState->Release(); + rasterizerState = nullptr; + } - context.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), - context.blendFactor, 0xFFFFFFFFu); - context.m_pDeviceContext->OMSetDepthStencilState( - this->GetManagedDepthStencilState(), 0); - context.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); - context.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, - depthStencilView); - activeVertexType = 0xFFFFFFFFu; - activePixelType = 0xFFFFFFFFu; + context.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); + context.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0); + context.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); + context.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, depthStencilView); + activeVertexType = 0xFFFFFFFFu; + activePixelType = 0xFFFFFFFFu; } -void Renderer::ConvertLinearToPng(ImageFileBuffer *pngOut, - unsigned char *linearData, unsigned int width, - unsigned int height) { - const size_t dataSize = size_t(width) * size_t(height) * 4u; - const size_t outputCapacity = ((dataSize * 24u) / 20u) + 256u; - const int outputCapacityInt = int(outputCapacity); - const int widthInt = int(width); - const int heightInt = int(height); - void *outputBuffer = std::malloc(outputCapacity); - int outputLength = 0; - this->SaveTextureDataToMemory(outputBuffer, outputCapacityInt, &outputLength, - widthInt, heightInt, - reinterpret_cast(linearData)); - pngOut->m_type = ImageFileBuffer::e_typePNG; - pngOut->m_pBuffer = outputBuffer; - pngOut->m_bufferSize = outputLength; +void Renderer::ConvertLinearToPng(ImageFileBuffer *pngOut, unsigned char *linearData, unsigned int width, unsigned int height) +{ + const size_t dataSize = size_t(width) * size_t(height) * 4u; + const size_t outputCapacity = ((dataSize * 24u) / 20u) + 256u; + const int outputCapacityInt = int(outputCapacity); + const int widthInt = int(width); + const int heightInt = int(height); + void *outputBuffer = std::malloc(outputCapacity); + int outputLength = 0; + this->SaveTextureDataToMemory(outputBuffer, outputCapacityInt, &outputLength, widthInt, heightInt, reinterpret_cast(linearData)); + pngOut->m_type = ImageFileBuffer::e_typePNG; + pngOut->m_pBuffer = outputBuffer; + pngOut->m_bufferSize = outputLength; } -void Renderer::DoScreenGrabOnNextPresent() { shouldScreenGrabNextFrame = 1; } +void Renderer::DoScreenGrabOnNextPresent() +{ + shouldScreenGrabNextFrame = 1; +} void Renderer::EndConditionalRendering() {} void Renderer::EndConditionalSurvey() {} -void Renderer::BeginEvent(LPCWSTR eventName) { - Renderer::Context *context = - static_cast(TlsGetValue(Renderer::tlsIdx)); - if (context && - context->m_pDeviceContext->GetType() != D3D11_DEVICE_CONTEXT_DEFERRED && - context->userAnnotation) { - context->userAnnotation->BeginEvent(eventName); - ++context->contextStateFlags; - } -} - -void Renderer::EndEvent() { - Renderer::Context *context = - static_cast(TlsGetValue(Renderer::tlsIdx)); - if (context && - context->m_pDeviceContext->GetType() != D3D11_DEVICE_CONTEXT_DEFERRED && - context->userAnnotation) { - context->userAnnotation->EndEvent(); - if (context->contextStateFlags > 0) { - --context->contextStateFlags; +void Renderer::BeginEvent(LPCWSTR eventName) +{ + Renderer::Context *context = static_cast(TlsGetValue(Renderer::tlsIdx)); + if (context && context->m_pDeviceContext->GetType() != D3D11_DEVICE_CONTEXT_DEFERRED && context->userAnnotation) + { + context->userAnnotation->BeginEvent(eventName); + ++context->contextStateFlags; } - } } -void Renderer::Initialise(ID3D11Device *pDevice, IDXGISwapChain *pSwapChain) { - m_pDevice = pDevice; - m_pDeviceContext = this->InitialiseContext(true); - m_pSwapChain = pSwapChain; - - reservedRendererPtr2 = operator new[](0x1000000u); - reservedRendererPtr3 = operator new[](0x1F400u); - reservedRendererPtr4 = operator new[](0xFA000u); - reservedRendererPtr5 = operator new[](0xFA00u); - reservedRendererPtr6 = operator new[](0x3E80u); - { - void *reservedPtr = operator new[](0x3E80u); - reservedRendererPtr1 = reinterpret_cast(reservedPtr); - } - - std::memset(reservedRendererPtr2, 0xFF, 0x1000000u); - std::memset(reservedRendererPtr3, 0, 0xFA00u); - std::memset(reservedRendererPtr5, 0, 0xFA00u); - std::memset(reservedRendererPtr6, 0, 0x3E80u); - { - void *reservedPtr = reinterpret_cast(reservedRendererPtr1); - std::memset(reservedPtr, 0, 0x3E80u); - } - reservedRendererDword3 = 0; - - shouldScreenGrabNextFrame = 0; - suspended = 0; - - this->SetupShaders(); - const float clearColour[4] = {0.0f, 0.0f, 0.0f, 0.0f}; - this->SetClearColour(clearColour); - - UINT backBufferSampleCount = 1; - UINT backBufferSampleQuality = 0; - ID3D11Texture2D *backBuffer = nullptr; - pSwapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer)); - if (backBuffer) { - D3D11_TEXTURE2D_DESC backDesc = {}; - backBuffer->GetDesc(&backDesc); - backBufferWidth = backDesc.Width; - backBufferHeight = backDesc.Height; - backBufferSampleCount = backDesc.SampleDesc.Count; - backBufferSampleQuality = backDesc.SampleDesc.Quality; - m_pDevice->CreateRenderTargetView(backBuffer, nullptr, &renderTargetView); - m_pDevice->CreateShaderResourceView(backBuffer, nullptr, - &renderTargetShaderResourceView); - backBuffer->Release(); - } - - ID3D11RenderTargetView *boundRTV = nullptr; - m_pDeviceContext->OMGetRenderTargets(1, &boundRTV, &depthStencilView); - if (boundRTV) { - boundRTV->Release(); - } - - if (!depthStencilView && backBufferWidth != 0 && backBufferHeight != 0) { - D3D11_TEXTURE2D_DESC depthDesc = {}; - depthDesc.Width = backBufferWidth; - depthDesc.Height = backBufferHeight; - depthDesc.MipLevels = 1; - depthDesc.ArraySize = 1; - depthDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; - depthDesc.SampleDesc.Count = backBufferSampleCount; - depthDesc.SampleDesc.Quality = backBufferSampleQuality; - depthDesc.Usage = D3D11_USAGE_DEFAULT; - depthDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; - - ID3D11Texture2D *depthTexture = nullptr; - if (SUCCEEDED( - m_pDevice->CreateTexture2D(&depthDesc, nullptr, &depthTexture))) { - m_pDevice->CreateDepthStencilView(depthTexture, nullptr, - &depthStencilView); - depthTexture->Release(); - } - } - - for (unsigned int i = 0; i < MAX_MIP_LEVELS - 1; ++i) { - D3D11_TEXTURE2D_DESC desc = {}; - desc.Width = s_auiWidths[i + 1]; - desc.Height = s_auiHeights[i + 1]; - desc.MipLevels = 1; - desc.ArraySize = 1; - desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - desc.SampleDesc.Count = 1; - desc.Usage = D3D11_USAGE_DEFAULT; - desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; - - m_pDevice->CreateTexture2D(&desc, nullptr, &renderTargetTextures[i]); - m_pDevice->CreateRenderTargetView(renderTargetTextures[i], nullptr, - &renderTargetViews[i]); - m_pDevice->CreateShaderResourceView(renderTargetTextures[i], nullptr, - &renderTargetShaderResourceViews[i]); - } - - std::memset(m_textures, 0, sizeof(m_textures)); - defaultTextureIndex = this->TextureCreate(); - this->TextureBind(defaultTextureIndex); - - unsigned char *defaultTextureData = new unsigned char[0x400u]; - std::memset(defaultTextureData, 0xFF, 0x400u); - this->TextureData(16, 16, defaultTextureData, 0, - C4JRender::TEXTURE_FORMAT_RxGyBzAw); - delete[] defaultTextureData; - - presentCount = 0; - rendererFlag0 = 0; - reservedRendererWord0 = 10922; - this->StateSetViewport(C4JRender::VIEWPORT_TYPE_FULLSCREEN); - this->StateSetVertexTextureUV(0.0f, 0.0f); - this->TextureBindVertex(-1); - InitializeCriticalSection(&rtl_critical_section100); - reservedRendererDword1 = 0; - activeVertexType = 0xFFFFFFFFu; - activePixelType = 0xFFFFFFFFu; - reservedRendererByte1 = 1; - reservedRendererByte0 = 0; - - unsigned short *quadIndices = new unsigned short[0x18000u]; - for (unsigned int i = 0; i < 0x4000u; ++i) { - const unsigned short base = (unsigned short)(i * 4u); - const unsigned int offset = i * 6u; - quadIndices[offset + 0] = base; - quadIndices[offset + 1] = (unsigned short)(base + 1u); - quadIndices[offset + 2] = (unsigned short)(base + 3u); - quadIndices[offset + 3] = (unsigned short)(base + 1u); - quadIndices[offset + 4] = (unsigned short)(base + 2u); - quadIndices[offset + 5] = (unsigned short)(base + 3u); - } - D3D11_BUFFER_DESC quadIndexDesc = {}; - quadIndexDesc.ByteWidth = 0x30000u; - quadIndexDesc.Usage = D3D11_USAGE_IMMUTABLE; - quadIndexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; - D3D11_SUBRESOURCE_DATA quadIndexData = {}; - quadIndexData.pSysMem = quadIndices; - m_pDevice->CreateBuffer(&quadIndexDesc, &quadIndexData, &quadIndexBuffer); - delete[] quadIndices; - - unsigned short *fanIndices = new unsigned short[0x2FFFAu]; - for (unsigned int i = 0; i < 65534u; ++i) { - const unsigned int offset = i * 3u; - fanIndices[offset + 0] = 0u; - fanIndices[offset + 1] = (unsigned short)(i + 1u); - fanIndices[offset + 2] = (unsigned short)(i + 2u); - } - D3D11_BUFFER_DESC fanIndexDesc = {}; - fanIndexDesc.ByteWidth = 0x5FFF4u; - fanIndexDesc.Usage = D3D11_USAGE_IMMUTABLE; - fanIndexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; - D3D11_SUBRESOURCE_DATA fanIndexData = {}; - fanIndexData.pSysMem = fanIndices; - m_pDevice->CreateBuffer(&fanIndexDesc, &fanIndexData, &fanIndexBuffer); - delete[] fanIndices; - - InitializeCriticalSection(&Renderer::totalAllocCS); - m_Topologies = g_topologies; -} - -ID3D11DeviceContext *Renderer::InitialiseContext(bool fromPresent) { - ID3D11DeviceContext *deviceContext = nullptr; - if (fromPresent) { - m_pDevice->GetImmediateContext(&deviceContext); - } else { - m_pDevice->CreateDeferredContext(0, &deviceContext); - } - - Renderer::Context *context = - new (std::nothrow) Renderer::Context(m_pDevice, deviceContext); - TlsSetValue(Renderer::tlsIdx, context); - return deviceContext; -} - -bool Renderer::IsHiDef() { return true; } - -bool Renderer::IsWidescreen() { return true; } - -void Renderer::Present() { - if (shouldScreenGrabNextFrame) { - int *linearData = new int[kScreenGrabWidth * kScreenGrabHeight]; - ID3D11Texture2D *backBuffer = nullptr; - ID3D11Texture2D *stagingTexture = nullptr; - - m_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer)); - if (backBuffer) { - D3D11_TEXTURE2D_DESC desc = {}; - backBuffer->GetDesc(&desc); - desc.Usage = D3D11_USAGE_STAGING; - desc.BindFlags = 0; - desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; - desc.MiscFlags = 0; - m_pDevice->CreateTexture2D(&desc, nullptr, &stagingTexture); - } - - if (stagingTexture && backBuffer) { - m_pDeviceContext->CopyResource(stagingTexture, backBuffer); - D3D11_MAPPED_SUBRESOURCE mapped = {}; - if (SUCCEEDED(m_pDeviceContext->Map(stagingTexture, 0, D3D11_MAP_READ, 0, - &mapped))) { - unsigned char *dst = reinterpret_cast(linearData); - const unsigned char *src = - reinterpret_cast(mapped.pData); - for (unsigned int y = 0; y < kScreenGrabHeight; ++y) { - unsigned char *dstRow = dst + y * (kScreenGrabWidth * 4u); - const unsigned char *srcRow = src + y * mapped.RowPitch; - std::memcpy(dstRow, srcRow, kScreenGrabWidth * 4u); - for (unsigned int x = 0; x < kScreenGrabWidth; ++x) { - dstRow[x * 4u + 3u] = 0xFF; - } +void Renderer::EndEvent() +{ + Renderer::Context *context = static_cast(TlsGetValue(Renderer::tlsIdx)); + if (context && context->m_pDeviceContext->GetType() != D3D11_DEVICE_CONTEXT_DEFERRED && context->userAnnotation) + { + context->userAnnotation->EndEvent(); + if (context->contextStateFlags > 0) + { + --context->contextStateFlags; } - m_pDeviceContext->Unmap(stagingTexture, 0); - } + } +} + +void Renderer::Initialise(ID3D11Device *pDevice, IDXGISwapChain *pSwapChain) +{ + m_pDevice = pDevice; + m_pDeviceContext = this->InitialiseContext(true); + m_pSwapChain = pSwapChain; + + reservedRendererPtr2 = operator new[](0x1000000u); + reservedRendererPtr3 = operator new[](0x1F400u); + reservedRendererPtr4 = operator new[](0xFA000u); + reservedRendererPtr5 = operator new[](0xFA00u); + reservedRendererPtr6 = operator new[](0x3E80u); + { + void *reservedPtr = operator new[](0x3E80u); + reservedRendererPtr1 = reinterpret_cast(reservedPtr); } - static int count = 0; - char fileName[304]; - sprintf_s(fileName, "d:\\screen%d.png", count++); - D3DXIMAGE_INFO info; - info.Width = kScreenGrabWidth; - info.Height = kScreenGrabHeight; - this->SaveTextureData(fileName, &info, linearData); - delete[] linearData; + std::memset(reservedRendererPtr2, 0xFF, 0x1000000u); + std::memset(reservedRendererPtr3, 0, 0xFA00u); + std::memset(reservedRendererPtr5, 0, 0xFA00u); + std::memset(reservedRendererPtr6, 0, 0x3E80u); + { + void *reservedPtr = reinterpret_cast(reservedRendererPtr1); + std::memset(reservedPtr, 0, 0x3E80u); + } + reservedRendererDword3 = 0; - if (stagingTexture) { - stagingTexture->Release(); - stagingTexture = nullptr; - } - if (backBuffer) { - backBuffer->Release(); - backBuffer = nullptr; - } shouldScreenGrabNextFrame = 0; - } + suspended = 0; - m_pSwapChain->Present(1, 0); - ++presentCount; + this->SetupShaders(); + const float clearColour[4] = {0.0f, 0.0f, 0.0f, 0.0f}; + this->SetClearColour(clearColour); + + UINT backBufferSampleCount = 1; + UINT backBufferSampleQuality = 0; + ID3D11Texture2D *backBuffer = nullptr; + pSwapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer)); + if (backBuffer) + { + D3D11_TEXTURE2D_DESC backDesc = {}; + backBuffer->GetDesc(&backDesc); + backBufferWidth = backDesc.Width; + backBufferHeight = backDesc.Height; + backBufferSampleCount = backDesc.SampleDesc.Count; + backBufferSampleQuality = backDesc.SampleDesc.Quality; + m_pDevice->CreateRenderTargetView(backBuffer, nullptr, &renderTargetView); + m_pDevice->CreateShaderResourceView(backBuffer, nullptr, &renderTargetShaderResourceView); + backBuffer->Release(); + } + + ID3D11RenderTargetView *boundRTV = nullptr; + m_pDeviceContext->OMGetRenderTargets(1, &boundRTV, &depthStencilView); + if (boundRTV) + { + boundRTV->Release(); + } + + if (!depthStencilView && backBufferWidth != 0 && backBufferHeight != 0) + { + D3D11_TEXTURE2D_DESC depthDesc = {}; + depthDesc.Width = backBufferWidth; + depthDesc.Height = backBufferHeight; + depthDesc.MipLevels = 1; + depthDesc.ArraySize = 1; + depthDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; + depthDesc.SampleDesc.Count = backBufferSampleCount; + depthDesc.SampleDesc.Quality = backBufferSampleQuality; + depthDesc.Usage = D3D11_USAGE_DEFAULT; + depthDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; + + ID3D11Texture2D *depthTexture = nullptr; + if (SUCCEEDED(m_pDevice->CreateTexture2D(&depthDesc, nullptr, &depthTexture))) + { + m_pDevice->CreateDepthStencilView(depthTexture, nullptr, &depthStencilView); + depthTexture->Release(); + } + } + + for (unsigned int i = 0; i < MAX_MIP_LEVELS - 1; ++i) + { + D3D11_TEXTURE2D_DESC desc = {}; + desc.Width = s_auiWidths[i + 1]; + desc.Height = s_auiHeights[i + 1]; + desc.MipLevels = 1; + desc.ArraySize = 1; + desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + desc.SampleDesc.Count = 1; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; + + m_pDevice->CreateTexture2D(&desc, nullptr, &renderTargetTextures[i]); + m_pDevice->CreateRenderTargetView(renderTargetTextures[i], nullptr, &renderTargetViews[i]); + m_pDevice->CreateShaderResourceView(renderTargetTextures[i], nullptr, &renderTargetShaderResourceViews[i]); + } + + std::memset(m_textures, 0, sizeof(m_textures)); + defaultTextureIndex = this->TextureCreate(); + this->TextureBind(defaultTextureIndex); + + unsigned char *defaultTextureData = new unsigned char[0x400u]; + std::memset(defaultTextureData, 0xFF, 0x400u); + this->TextureData(16, 16, defaultTextureData, 0, C4JRender::TEXTURE_FORMAT_RxGyBzAw); + delete[] defaultTextureData; + + presentCount = 0; + rendererFlag0 = 0; + reservedRendererWord0 = 10922; + this->StateSetViewport(C4JRender::VIEWPORT_TYPE_FULLSCREEN); + this->StateSetVertexTextureUV(0.0f, 0.0f); + this->TextureBindVertex(-1); + InitializeCriticalSection(&rtl_critical_section100); + reservedRendererDword1 = 0; + activeVertexType = 0xFFFFFFFFu; + activePixelType = 0xFFFFFFFFu; + reservedRendererByte1 = 1; + reservedRendererByte0 = 0; + + unsigned short *quadIndices = new unsigned short[0x18000u]; + for (unsigned int i = 0; i < 0x4000u; ++i) + { + const unsigned short base = (unsigned short)(i * 4u); + const unsigned int offset = i * 6u; + quadIndices[offset + 0] = base; + quadIndices[offset + 1] = (unsigned short)(base + 1u); + quadIndices[offset + 2] = (unsigned short)(base + 3u); + quadIndices[offset + 3] = (unsigned short)(base + 1u); + quadIndices[offset + 4] = (unsigned short)(base + 2u); + quadIndices[offset + 5] = (unsigned short)(base + 3u); + } + D3D11_BUFFER_DESC quadIndexDesc = {}; + quadIndexDesc.ByteWidth = 0x30000u; + quadIndexDesc.Usage = D3D11_USAGE_IMMUTABLE; + quadIndexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; + D3D11_SUBRESOURCE_DATA quadIndexData = {}; + quadIndexData.pSysMem = quadIndices; + m_pDevice->CreateBuffer(&quadIndexDesc, &quadIndexData, &quadIndexBuffer); + delete[] quadIndices; + + unsigned short *fanIndices = new unsigned short[0x2FFFAu]; + for (unsigned int i = 0; i < 65534u; ++i) + { + const unsigned int offset = i * 3u; + fanIndices[offset + 0] = 0u; + fanIndices[offset + 1] = (unsigned short)(i + 1u); + fanIndices[offset + 2] = (unsigned short)(i + 2u); + } + D3D11_BUFFER_DESC fanIndexDesc = {}; + fanIndexDesc.ByteWidth = 0x5FFF4u; + fanIndexDesc.Usage = D3D11_USAGE_IMMUTABLE; + fanIndexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; + D3D11_SUBRESOURCE_DATA fanIndexData = {}; + fanIndexData.pSysMem = fanIndices; + m_pDevice->CreateBuffer(&fanIndexDesc, &fanIndexData, &fanIndexBuffer); + delete[] fanIndices; + + InitializeCriticalSection(&Renderer::totalAllocCS); + m_Topologies = g_topologies; } -void Renderer::Resume() { suspended = 0; } +ID3D11DeviceContext *Renderer::InitialiseContext(bool fromPresent) +{ + ID3D11DeviceContext *deviceContext = nullptr; + if (fromPresent) + { + m_pDevice->GetImmediateContext(&deviceContext); + } + else + { + m_pDevice->CreateDeferredContext(0, &deviceContext); + } -void Renderer::SetClearColour(const float colourRGBA[4]) { - std::memcpy(m_fClearColor, colourRGBA, sizeof(m_fClearColor)); - - Renderer::Context *context = - static_cast(TlsGetValue(Renderer::tlsIdx)); - if (context) { - D3D11_MAPPED_SUBRESOURCE mapped = {}; - context->m_pDeviceContext->Map(context->cbAux3, 0, D3D11_MAP_WRITE_DISCARD, - 0, &mapped); - std::memcpy(mapped.pData, colourRGBA, sizeof(float) * 4); - context->m_pDeviceContext->Unmap(context->cbAux3, 0); - } + Renderer::Context *context = new (std::nothrow) Renderer::Context(m_pDevice, deviceContext); + TlsSetValue(Renderer::tlsIdx, context); + return deviceContext; } -void Renderer::SetupShaders() { - vertexShaderTable = new ID3D11VertexShader *[C4JRender::VERTEX_TYPE_COUNT]; - pixelShaderTable = new ID3D11PixelShader *[C4JRender::PIXEL_SHADER_COUNT]; - vertexStrideTable = new unsigned int[C4JRender::VERTEX_TYPE_COUNT]; - inputLayoutTable = new ID3D11InputLayout *[C4JRender::VERTEX_TYPE_COUNT]; - - for (unsigned int i = 0; i < C4JRender::VERTEX_TYPE_COUNT; ++i) { - vertexShaderTable[i] = nullptr; - inputLayoutTable[i] = nullptr; - vertexStrideTable[i] = g_vertexStrides[i]; - } - - for (unsigned int i = 0; i < C4JRender::PIXEL_SHADER_COUNT; ++i) { - pixelShaderTable[i] = nullptr; - } - - screenSpaceVertexShader = nullptr; - screenClearVertexShader = nullptr; - screenSpacePixelShader = nullptr; - screenClearPixelShader = nullptr; - - m_pDevice->CreateVertexShader( - VS_PF3_TF2_CB4_NB4_XW1_Data, sizeof(VS_PF3_TF2_CB4_NB4_XW1_Data), nullptr, - &vertexShaderTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1]); - m_pDevice->CreateVertexShader( - VS_Compressed_Data, sizeof(VS_Compressed_Data), nullptr, - &vertexShaderTable[C4JRender::VERTEX_TYPE_COMPRESSED]); - m_pDevice->CreateVertexShader( - VS_PF3_TF2_CB4_NB4_XW1_Lighting_Data, - sizeof(VS_PF3_TF2_CB4_NB4_XW1_Lighting_Data), nullptr, - &vertexShaderTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT]); - m_pDevice->CreateVertexShader( - VS_PF3_TF2_CB4_NB4_XW1_Texgen_Data, - sizeof(VS_PF3_TF2_CB4_NB4_XW1_Texgen_Data), nullptr, - &vertexShaderTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_TEXGEN]); - m_pDevice->CreateVertexShader(VS_ScreenSpace_Data, - sizeof(VS_ScreenSpace_Data), nullptr, - &screenSpaceVertexShader); - m_pDevice->CreateVertexShader(VS_ScreenClear_Data, - sizeof(VS_ScreenClear_Data), nullptr, - &screenClearVertexShader); - - m_pDevice->CreatePixelShader( - PS_Standard_Data, sizeof(PS_Standard_Data), nullptr, - &pixelShaderTable[C4JRender::PIXEL_SHADER_TYPE_STANDARD]); - m_pDevice->CreatePixelShader( - PS_TextureProjection_Data, sizeof(PS_TextureProjection_Data), nullptr, - &pixelShaderTable[C4JRender::PIXEL_SHADER_TYPE_PROJECTION]); - m_pDevice->CreatePixelShader( - PS_ForceLOD_Data, sizeof(PS_ForceLOD_Data), nullptr, - &pixelShaderTable[C4JRender::PIXEL_SHADER_TYPE_FORCELOD]); - m_pDevice->CreatePixelShader(PS_ScreenSpace_Data, sizeof(PS_ScreenSpace_Data), - nullptr, &screenSpacePixelShader); - m_pDevice->CreatePixelShader(PS_ScreenClear_Data, sizeof(PS_ScreenClear_Data), - nullptr, &screenClearPixelShader); - - m_pDevice->CreateInputLayout( - g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1, 5, VS_PF3_TF2_CB4_NB4_XW1_Data, - sizeof(VS_PF3_TF2_CB4_NB4_XW1_Data), - &inputLayoutTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1]); - m_pDevice->CreateInputLayout( - g_vertex_PTN_Elements_Compressed, 2, VS_Compressed_Data, - sizeof(VS_Compressed_Data), - &inputLayoutTable[C4JRender::VERTEX_TYPE_COMPRESSED]); - m_pDevice->CreateInputLayout( - g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1, 5, - VS_PF3_TF2_CB4_NB4_XW1_Lighting_Data, - sizeof(VS_PF3_TF2_CB4_NB4_XW1_Lighting_Data), - &inputLayoutTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT]); - m_pDevice->CreateInputLayout( - g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1, 5, - VS_PF3_TF2_CB4_NB4_XW1_Texgen_Data, - sizeof(VS_PF3_TF2_CB4_NB4_XW1_Texgen_Data), - &inputLayoutTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_TEXGEN]); +bool Renderer::IsHiDef() +{ + return true; } -void Renderer::StartFrame() { - Renderer::Context &context = this->getContext(); - - activeVertexType = 0xFFFFFFFFu; - activePixelType = 0xFFFFFFFFu; - - this->TextureBindVertex(-1); - this->TextureBind(-1); - this->StateSetColour(1.0f, 1.0f, 1.0f, 1.0f); - this->StateSetDepthMask(true); - this->StateSetBlendEnable(true); - this->StateSetBlendFunc(D3D11_BLEND_SRC_ALPHA, D3D11_BLEND_INV_SRC_ALPHA); - this->StateSetBlendFactor(0xFFFFFFFFu); - this->StateSetAlphaFunc(D3D11_COMPARISON_GREATER, 0.1f); - this->StateSetDepthFunc(D3D11_COMPARISON_LESS_EQUAL); - this->StateSetFaceCull(true); - this->StateSetLineWidth(1.0f); - this->StateSetWriteEnable(true, true, true, true); - this->StateSetDepthTestEnable(false); - this->StateSetAlphaTestEnable(true); - - context.m_pDeviceContext->VSSetConstantBuffers(0, 10, &context.cbMatrix0); - context.m_pDeviceContext->PSSetConstantBuffers(0, 6, &context.cbColour); - - D3D11_VIEWPORT viewport = {}; - viewport.TopLeftX = 0.0f; - viewport.TopLeftY = 0.0f; - viewport.Width = (float)backBufferWidth; - viewport.Height = (float)backBufferHeight; - viewport.MinDepth = 0.0f; - viewport.MaxDepth = 1.0f; - context.m_pDeviceContext->RSSetViewports(1, &viewport); - context.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, - depthStencilView); +bool Renderer::IsWidescreen() +{ + return true; } -void Renderer::Suspend() { suspended = 1; } +void Renderer::Present() +{ + if (shouldScreenGrabNextFrame) + { + int *linearData = new int[kScreenGrabWidth * kScreenGrabHeight]; + ID3D11Texture2D *backBuffer = nullptr; + ID3D11Texture2D *stagingTexture = nullptr; -bool Renderer::Suspended() { return suspended != 0; } + m_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer)); + if (backBuffer) + { + D3D11_TEXTURE2D_DESC desc = {}; + backBuffer->GetDesc(&desc); + desc.Usage = D3D11_USAGE_STAGING; + desc.BindFlags = 0; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + desc.MiscFlags = 0; + m_pDevice->CreateTexture2D(&desc, nullptr, &stagingTexture); + } -void Renderer::UpdateGamma(unsigned short) {} + if (stagingTexture && backBuffer) + { + m_pDeviceContext->CopyResource(stagingTexture, backBuffer); + D3D11_MAPPED_SUBRESOURCE mapped = {}; + if (SUCCEEDED(m_pDeviceContext->Map(stagingTexture, 0, D3D11_MAP_READ, 0, &mapped))) + { + unsigned char *dst = reinterpret_cast(linearData); + const unsigned char *src = reinterpret_cast(mapped.pData); + for (unsigned int y = 0; y < kScreenGrabHeight; ++y) + { + unsigned char *dstRow = dst + y * (kScreenGrabWidth * 4u); + const unsigned char *srcRow = src + y * mapped.RowPitch; + std::memcpy(dstRow, srcRow, kScreenGrabWidth * 4u); + for (unsigned int x = 0; x < kScreenGrabWidth; ++x) + { + dstRow[x * 4u + 3u] = 0xFF; + } + } + m_pDeviceContext->Unmap(stagingTexture, 0); + } + } -Renderer::Context &Renderer::getContext() { - return *static_cast(TlsGetValue(Renderer::tlsIdx)); + static int count = 0; + char fileName[304]; + sprintf_s(fileName, "d:\\screen%d.png", count++); + D3DXIMAGE_INFO info; + info.Width = kScreenGrabWidth; + info.Height = kScreenGrabHeight; + this->SaveTextureData(fileName, &info, linearData); + delete[] linearData; + + if (stagingTexture) + { + stagingTexture->Release(); + stagingTexture = nullptr; + } + if (backBuffer) + { + backBuffer->Release(); + backBuffer = nullptr; + } + shouldScreenGrabNextFrame = 0; + } + + m_pSwapChain->Present(1, 0); + ++presentCount; } -void Renderer::CaptureThumbnail(ImageFileBuffer *pngOut) { - Renderer::Context &context = this->getContext(); +void Renderer::Resume() +{ + suspended = 0; +} - float left; - float bottom; - float right; - float top; - left = 0.0f; - bottom = 0.0f; - right = 1.0f; - top = 1.0f; - switch (m_ViewportType) { - case C4JRender::VIEWPORT_TYPE_SPLIT_TOP: - bottom = 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_SPLIT_BOTTOM: - top = 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_SPLIT_LEFT: - right = 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_SPLIT_RIGHT: - left = 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_QUADRANT_TOP_LEFT: - right = 0.5f; - bottom = 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_QUADRANT_TOP_RIGHT: - left = 0.5f; - bottom = 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_QUADRANT_BOTTOM_LEFT: - right = 0.5f; - top = 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_QUADRANT_BOTTOM_RIGHT: - left = 0.5f; - top = 0.5f; - break; - default: - break; - } +void Renderer::SetClearColour(const float colourRGBA[4]) +{ + std::memcpy(m_fClearColor, colourRGBA, sizeof(m_fClearColor)); - float leftScaled = left * kAspectRatio; - float rightScaled = right * kAspectRatio; - const float viewportHeight = top - bottom; - const float viewportScaledWidth = rightScaled - leftScaled; + Renderer::Context *context = static_cast(TlsGetValue(Renderer::tlsIdx)); + if (context) + { + D3D11_MAPPED_SUBRESOURCE mapped = {}; + context->m_pDeviceContext->Map(context->cbAux3, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + std::memcpy(mapped.pData, colourRGBA, sizeof(float) * 4); + context->m_pDeviceContext->Unmap(context->cbAux3, 0); + } +} - if (viewportHeight <= viewportScaledWidth) { - const float pad = (viewportScaledWidth - viewportHeight) * 0.5f; - leftScaled += pad; - rightScaled -= pad; - } else { - const float pad = (viewportHeight - viewportScaledWidth) * 0.5f; - bottom += pad; - top -= pad; - } +void Renderer::SetupShaders() +{ + vertexShaderTable = new ID3D11VertexShader *[C4JRender::VERTEX_TYPE_COUNT]; + pixelShaderTable = new ID3D11PixelShader *[C4JRender::PIXEL_SHADER_COUNT]; + vertexStrideTable = new unsigned int[C4JRender::VERTEX_TYPE_COUNT]; + inputLayoutTable = new ID3D11InputLayout *[C4JRender::VERTEX_TYPE_COUNT]; - const float sampleLeft = leftScaled / kAspectRatio; - const float sampleWidth = (rightScaled - leftScaled) / kAspectRatio; - const float sampleHeight = top - bottom; + for (unsigned int i = 0; i < C4JRender::VERTEX_TYPE_COUNT; ++i) + { + vertexShaderTable[i] = nullptr; + inputLayoutTable[i] = nullptr; + vertexStrideTable[i] = g_vertexStrides[i]; + } - ID3D11BlendState *blendState = nullptr; - ID3D11DepthStencilState *depthState = nullptr; - ID3D11RasterizerState *rasterizerState = nullptr; - ID3D11SamplerState *samplerState = nullptr; - ID3D11Texture2D *stagingTexture = nullptr; + for (unsigned int i = 0; i < C4JRender::PIXEL_SHADER_COUNT; ++i) + { + pixelShaderTable[i] = nullptr; + } - D3D11_BLEND_DESC blendDesc = {}; - blendDesc.RenderTarget[0].BlendEnable = FALSE; - blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; - blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO; - blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; - blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; - blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; - blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; - blendDesc.RenderTarget[0].RenderTargetWriteMask = - D3D11_COLOR_WRITE_ENABLE_ALL; - m_pDevice->CreateBlendState(&blendDesc, &blendState); + screenSpaceVertexShader = nullptr; + screenClearVertexShader = nullptr; + screenSpacePixelShader = nullptr; + screenClearPixelShader = nullptr; - D3D11_DEPTH_STENCIL_DESC depthDesc = {}; - depthDesc.DepthEnable = FALSE; - depthDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; - depthDesc.DepthFunc = D3D11_COMPARISON_ALWAYS; - depthDesc.StencilEnable = FALSE; - depthDesc.StencilReadMask = 0xFFu; - depthDesc.StencilWriteMask = 0xFFu; - depthDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; - depthDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; - depthDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; - depthDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; - depthDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; - depthDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; - depthDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; - depthDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; - m_pDevice->CreateDepthStencilState(&depthDesc, &depthState); + m_pDevice->CreateVertexShader(VS_PF3_TF2_CB4_NB4_XW1_Data, sizeof(VS_PF3_TF2_CB4_NB4_XW1_Data), nullptr, + &vertexShaderTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1]); + m_pDevice->CreateVertexShader(VS_Compressed_Data, sizeof(VS_Compressed_Data), nullptr, &vertexShaderTable[C4JRender::VERTEX_TYPE_COMPRESSED]); + m_pDevice->CreateVertexShader(VS_PF3_TF2_CB4_NB4_XW1_Lighting_Data, sizeof(VS_PF3_TF2_CB4_NB4_XW1_Lighting_Data), nullptr, + &vertexShaderTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT]); + m_pDevice->CreateVertexShader(VS_PF3_TF2_CB4_NB4_XW1_Texgen_Data, sizeof(VS_PF3_TF2_CB4_NB4_XW1_Texgen_Data), nullptr, + &vertexShaderTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_TEXGEN]); + m_pDevice->CreateVertexShader(VS_ScreenSpace_Data, sizeof(VS_ScreenSpace_Data), nullptr, &screenSpaceVertexShader); + m_pDevice->CreateVertexShader(VS_ScreenClear_Data, sizeof(VS_ScreenClear_Data), nullptr, &screenClearVertexShader); - D3D11_RASTERIZER_DESC rasterDesc = {}; - rasterDesc.FillMode = D3D11_FILL_SOLID; - rasterDesc.CullMode = D3D11_CULL_NONE; - rasterDesc.DepthClipEnable = TRUE; - rasterDesc.MultisampleEnable = TRUE; - m_pDevice->CreateRasterizerState(&rasterDesc, &rasterizerState); + m_pDevice->CreatePixelShader(PS_Standard_Data, sizeof(PS_Standard_Data), nullptr, &pixelShaderTable[C4JRender::PIXEL_SHADER_TYPE_STANDARD]); + m_pDevice->CreatePixelShader(PS_TextureProjection_Data, sizeof(PS_TextureProjection_Data), nullptr, + &pixelShaderTable[C4JRender::PIXEL_SHADER_TYPE_PROJECTION]); + m_pDevice->CreatePixelShader(PS_ForceLOD_Data, sizeof(PS_ForceLOD_Data), nullptr, &pixelShaderTable[C4JRender::PIXEL_SHADER_TYPE_FORCELOD]); + m_pDevice->CreatePixelShader(PS_ScreenSpace_Data, sizeof(PS_ScreenSpace_Data), nullptr, &screenSpacePixelShader); + m_pDevice->CreatePixelShader(PS_ScreenClear_Data, sizeof(PS_ScreenClear_Data), nullptr, &screenClearPixelShader); - D3D11_SAMPLER_DESC samplerDesc = {}; - samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; - samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; - samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; - samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; - samplerDesc.MaxAnisotropy = 1; - samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; - samplerDesc.MinLOD = 0.0f; - samplerDesc.MaxLOD = (std::numeric_limits::max)(); - m_pDevice->CreateSamplerState(&samplerDesc, &samplerState); + m_pDevice->CreateInputLayout(g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1, 5, VS_PF3_TF2_CB4_NB4_XW1_Data, sizeof(VS_PF3_TF2_CB4_NB4_XW1_Data), + &inputLayoutTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1]); + m_pDevice->CreateInputLayout(g_vertex_PTN_Elements_Compressed, 2, VS_Compressed_Data, sizeof(VS_Compressed_Data), + &inputLayoutTable[C4JRender::VERTEX_TYPE_COMPRESSED]); + m_pDevice->CreateInputLayout(g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1, 5, VS_PF3_TF2_CB4_NB4_XW1_Lighting_Data, + sizeof(VS_PF3_TF2_CB4_NB4_XW1_Lighting_Data), &inputLayoutTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT]); + m_pDevice->CreateInputLayout(g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1, 5, VS_PF3_TF2_CB4_NB4_XW1_Texgen_Data, + sizeof(VS_PF3_TF2_CB4_NB4_XW1_Texgen_Data), &inputLayoutTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_TEXGEN]); +} - context.m_pDeviceContext->VSSetShader(screenSpaceVertexShader, nullptr, 0); - context.m_pDeviceContext->IASetInputLayout(nullptr); - context.m_pDeviceContext->PSSetShader(screenSpacePixelShader, nullptr, 0); - context.m_pDeviceContext->OMSetBlendState(blendState, nullptr, 0xFFFFFFFFu); - context.m_pDeviceContext->OMSetDepthStencilState(depthState, 0); - context.m_pDeviceContext->RSSetState(rasterizerState); +void Renderer::StartFrame() +{ + Renderer::Context &context = this->getContext(); + + activeVertexType = 0xFFFFFFFFu; + activePixelType = 0xFFFFFFFFu; + + this->TextureBindVertex(-1); + this->TextureBind(-1); + this->StateSetColour(1.0f, 1.0f, 1.0f, 1.0f); + this->StateSetDepthMask(true); + this->StateSetBlendEnable(true); + this->StateSetBlendFunc(D3D11_BLEND_SRC_ALPHA, D3D11_BLEND_INV_SRC_ALPHA); + this->StateSetBlendFactor(0xFFFFFFFFu); + this->StateSetAlphaFunc(D3D11_COMPARISON_GREATER, 0.1f); + this->StateSetDepthFunc(D3D11_COMPARISON_LESS_EQUAL); + this->StateSetFaceCull(true); + this->StateSetLineWidth(1.0f); + this->StateSetWriteEnable(true, true, true, true); + this->StateSetDepthTestEnable(false); + this->StateSetAlphaTestEnable(true); + + context.m_pDeviceContext->VSSetConstantBuffers(0, 10, &context.cbMatrix0); + context.m_pDeviceContext->PSSetConstantBuffers(0, 6, &context.cbColour); - for (unsigned int i = 0; i < MAX_MIP_LEVELS - 1; ++i) { D3D11_VIEWPORT viewport = {}; viewport.TopLeftX = 0.0f; viewport.TopLeftY = 0.0f; - viewport.Width = (float)s_auiWidths[i + 1]; - viewport.Height = (float)s_auiHeights[i + 1]; + viewport.Width = (float)backBufferWidth; + viewport.Height = (float)backBufferHeight; viewport.MinDepth = 0.0f; viewport.MaxDepth = 1.0f; - - context.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetViews[i], - nullptr); context.m_pDeviceContext->RSSetViewports(1, &viewport); - - ID3D11ShaderResourceView *inputTexture = - (i == 0) ? renderTargetShaderResourceView - : renderTargetShaderResourceViews[i - 1]; - context.m_pDeviceContext->PSSetShaderResources(0, 1, &inputTexture); - context.m_pDeviceContext->PSSetSamplers(0, 1, &samplerState); - context.m_pDeviceContext->IASetPrimitiveTopology( - D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); - - D3D11_MAPPED_SUBRESOURCE mapped = {}; - context.m_pDeviceContext->Map(context.cbAux1, 0, D3D11_MAP_WRITE_DISCARD, 0, - &mapped); - float *constants = static_cast(mapped.pData); - if (i == 0) { - constants[0] = sampleLeft; - constants[1] = bottom; - constants[2] = sampleWidth; - constants[3] = sampleHeight; - } else { - constants[0] = 0.0f; - constants[1] = 0.0f; - constants[2] = 1.0f; - constants[3] = 1.0f; - } - context.m_pDeviceContext->Unmap(context.cbAux1, 0); - context.m_pDeviceContext->Draw(4, 0); - } - - D3D11_TEXTURE2D_DESC desc = {}; - renderTargetTextures[MAX_MIP_LEVELS - 2]->GetDesc(&desc); - desc.Usage = D3D11_USAGE_STAGING; - desc.BindFlags = 0; - desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; - desc.MiscFlags = 0; - m_pDevice->CreateTexture2D(&desc, nullptr, &stagingTexture); - - unsigned char *linearData = - new unsigned char[kThumbnailSize * kThumbnailSize * 4u]; - if (stagingTexture) { - context.m_pDeviceContext->CopyResource( - stagingTexture, renderTargetTextures[MAX_MIP_LEVELS - 2]); - D3D11_MAPPED_SUBRESOURCE mapped = {}; - if (SUCCEEDED(context.m_pDeviceContext->Map(stagingTexture, 0, - D3D11_MAP_READ, 0, &mapped))) { - const unsigned char *src = - static_cast(mapped.pData); - unsigned char *dst = linearData; - for (unsigned int y = 0; y < kThumbnailSize; ++y) { - std::memcpy(dst, src, kThumbnailSize * 4u); - for (unsigned int x = 0; x < kThumbnailSize; ++x) { - dst[x * 4u + 3u] = 0xFF; - } - src += mapped.RowPitch; - dst += kThumbnailSize * 4u; - } - context.m_pDeviceContext->Unmap(stagingTexture, 0); - } - } - - this->ConvertLinearToPng(pngOut, linearData, kThumbnailSize, kThumbnailSize); - delete[] linearData; - - if (stagingTexture) { - stagingTexture->Release(); - stagingTexture = nullptr; - } - if (samplerState) { - samplerState->Release(); - samplerState = nullptr; - } - if (rasterizerState) { - rasterizerState->Release(); - rasterizerState = nullptr; - } - if (depthState) { - depthState->Release(); - depthState = nullptr; - } - if (blendState) { - blendState->Release(); - blendState = nullptr; - } - - context.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), - context.blendFactor, 0xFFFFFFFFu); - context.m_pDeviceContext->OMSetDepthStencilState( - this->GetManagedDepthStencilState(), 0); - context.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); - - D3D11_VIEWPORT viewport = {}; - viewport.TopLeftX = 0.0f; - viewport.TopLeftY = 0.0f; - viewport.Width = (float)backBufferWidth; - viewport.Height = (float)backBufferHeight; - viewport.MinDepth = 0.0f; - viewport.MaxDepth = 1.0f; - context.m_pDeviceContext->RSSetViewports(1, &viewport); - context.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, - depthStencilView); - activeVertexType = 0xFFFFFFFFu; - activePixelType = 0xFFFFFFFFu; + context.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, depthStencilView); +} + +void Renderer::Suspend() +{ + suspended = 1; +} + +bool Renderer::Suspended() +{ + return suspended != 0; +} + +void Renderer::UpdateGamma(unsigned short) {} + +Renderer::Context &Renderer::getContext() +{ + return *static_cast(TlsGetValue(Renderer::tlsIdx)); +} + +void Renderer::CaptureThumbnail(ImageFileBuffer *pngOut) +{ + Renderer::Context &context = this->getContext(); + + float left; + float bottom; + float right; + float top; + left = 0.0f; + bottom = 0.0f; + right = 1.0f; + top = 1.0f; + switch (m_ViewportType) + { + case C4JRender::VIEWPORT_TYPE_SPLIT_TOP: + bottom = 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_SPLIT_BOTTOM: + top = 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_SPLIT_LEFT: + right = 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_SPLIT_RIGHT: + left = 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_QUADRANT_TOP_LEFT: + right = 0.5f; + bottom = 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_QUADRANT_TOP_RIGHT: + left = 0.5f; + bottom = 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_QUADRANT_BOTTOM_LEFT: + right = 0.5f; + top = 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_QUADRANT_BOTTOM_RIGHT: + left = 0.5f; + top = 0.5f; + break; + default: + break; + } + + float leftScaled = left * kAspectRatio; + float rightScaled = right * kAspectRatio; + const float viewportHeight = top - bottom; + const float viewportScaledWidth = rightScaled - leftScaled; + + if (viewportHeight <= viewportScaledWidth) + { + const float pad = (viewportScaledWidth - viewportHeight) * 0.5f; + leftScaled += pad; + rightScaled -= pad; + } + else + { + const float pad = (viewportHeight - viewportScaledWidth) * 0.5f; + bottom += pad; + top -= pad; + } + + const float sampleLeft = leftScaled / kAspectRatio; + const float sampleWidth = (rightScaled - leftScaled) / kAspectRatio; + const float sampleHeight = top - bottom; + + ID3D11BlendState *blendState = nullptr; + ID3D11DepthStencilState *depthState = nullptr; + ID3D11RasterizerState *rasterizerState = nullptr; + ID3D11SamplerState *samplerState = nullptr; + ID3D11Texture2D *stagingTexture = nullptr; + + D3D11_BLEND_DESC blendDesc = {}; + blendDesc.RenderTarget[0].BlendEnable = FALSE; + blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; + blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO; + blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; + blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; + blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; + m_pDevice->CreateBlendState(&blendDesc, &blendState); + + D3D11_DEPTH_STENCIL_DESC depthDesc = {}; + depthDesc.DepthEnable = FALSE; + depthDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; + depthDesc.DepthFunc = D3D11_COMPARISON_ALWAYS; + depthDesc.StencilEnable = FALSE; + depthDesc.StencilReadMask = 0xFFu; + depthDesc.StencilWriteMask = 0xFFu; + depthDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + depthDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + depthDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; + depthDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + depthDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + depthDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + depthDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; + depthDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + m_pDevice->CreateDepthStencilState(&depthDesc, &depthState); + + D3D11_RASTERIZER_DESC rasterDesc = {}; + rasterDesc.FillMode = D3D11_FILL_SOLID; + rasterDesc.CullMode = D3D11_CULL_NONE; + rasterDesc.DepthClipEnable = TRUE; + rasterDesc.MultisampleEnable = TRUE; + m_pDevice->CreateRasterizerState(&rasterDesc, &rasterizerState); + + D3D11_SAMPLER_DESC samplerDesc = {}; + samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; + samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; + samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; + samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; + samplerDesc.MaxAnisotropy = 1; + samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; + samplerDesc.MinLOD = 0.0f; + samplerDesc.MaxLOD = (std::numeric_limits::max)(); + m_pDevice->CreateSamplerState(&samplerDesc, &samplerState); + + context.m_pDeviceContext->VSSetShader(screenSpaceVertexShader, nullptr, 0); + context.m_pDeviceContext->IASetInputLayout(nullptr); + context.m_pDeviceContext->PSSetShader(screenSpacePixelShader, nullptr, 0); + context.m_pDeviceContext->OMSetBlendState(blendState, nullptr, 0xFFFFFFFFu); + context.m_pDeviceContext->OMSetDepthStencilState(depthState, 0); + context.m_pDeviceContext->RSSetState(rasterizerState); + + for (unsigned int i = 0; i < MAX_MIP_LEVELS - 1; ++i) + { + D3D11_VIEWPORT viewport = {}; + viewport.TopLeftX = 0.0f; + viewport.TopLeftY = 0.0f; + viewport.Width = (float)s_auiWidths[i + 1]; + viewport.Height = (float)s_auiHeights[i + 1]; + viewport.MinDepth = 0.0f; + viewport.MaxDepth = 1.0f; + + context.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetViews[i], nullptr); + context.m_pDeviceContext->RSSetViewports(1, &viewport); + + ID3D11ShaderResourceView *inputTexture = (i == 0) ? renderTargetShaderResourceView : renderTargetShaderResourceViews[i - 1]; + context.m_pDeviceContext->PSSetShaderResources(0, 1, &inputTexture); + context.m_pDeviceContext->PSSetSamplers(0, 1, &samplerState); + context.m_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); + + D3D11_MAPPED_SUBRESOURCE mapped = {}; + context.m_pDeviceContext->Map(context.cbAux1, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + float *constants = static_cast(mapped.pData); + if (i == 0) + { + constants[0] = sampleLeft; + constants[1] = bottom; + constants[2] = sampleWidth; + constants[3] = sampleHeight; + } + else + { + constants[0] = 0.0f; + constants[1] = 0.0f; + constants[2] = 1.0f; + constants[3] = 1.0f; + } + context.m_pDeviceContext->Unmap(context.cbAux1, 0); + context.m_pDeviceContext->Draw(4, 0); + } + + D3D11_TEXTURE2D_DESC desc = {}; + renderTargetTextures[MAX_MIP_LEVELS - 2]->GetDesc(&desc); + desc.Usage = D3D11_USAGE_STAGING; + desc.BindFlags = 0; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + desc.MiscFlags = 0; + m_pDevice->CreateTexture2D(&desc, nullptr, &stagingTexture); + + unsigned char *linearData = new unsigned char[kThumbnailSize * kThumbnailSize * 4u]; + if (stagingTexture) + { + context.m_pDeviceContext->CopyResource(stagingTexture, renderTargetTextures[MAX_MIP_LEVELS - 2]); + D3D11_MAPPED_SUBRESOURCE mapped = {}; + if (SUCCEEDED(context.m_pDeviceContext->Map(stagingTexture, 0, D3D11_MAP_READ, 0, &mapped))) + { + const unsigned char *src = static_cast(mapped.pData); + unsigned char *dst = linearData; + for (unsigned int y = 0; y < kThumbnailSize; ++y) + { + std::memcpy(dst, src, kThumbnailSize * 4u); + for (unsigned int x = 0; x < kThumbnailSize; ++x) + { + dst[x * 4u + 3u] = 0xFF; + } + src += mapped.RowPitch; + dst += kThumbnailSize * 4u; + } + context.m_pDeviceContext->Unmap(stagingTexture, 0); + } + } + + this->ConvertLinearToPng(pngOut, linearData, kThumbnailSize, kThumbnailSize); + delete[] linearData; + + if (stagingTexture) + { + stagingTexture->Release(); + stagingTexture = nullptr; + } + if (samplerState) + { + samplerState->Release(); + samplerState = nullptr; + } + if (rasterizerState) + { + rasterizerState->Release(); + rasterizerState = nullptr; + } + if (depthState) + { + depthState->Release(); + depthState = nullptr; + } + if (blendState) + { + blendState->Release(); + blendState = nullptr; + } + + context.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); + context.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0); + context.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); + + D3D11_VIEWPORT viewport = {}; + viewport.TopLeftX = 0.0f; + viewport.TopLeftY = 0.0f; + viewport.Width = (float)backBufferWidth; + viewport.Height = (float)backBufferHeight; + viewport.MinDepth = 0.0f; + viewport.MaxDepth = 1.0f; + context.m_pDeviceContext->RSSetViewports(1, &viewport); + context.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, depthStencilView); + activeVertexType = 0xFFFFFFFFu; + activePixelType = 0xFFFFFFFFu; } diff --git a/Windows_Libs/Dev/Render/RendererMatrix.cpp b/Windows_Libs/Dev/Render/RendererMatrix.cpp index 32959c4..19641dc 100644 --- a/Windows_Libs/Dev/Render/RendererMatrix.cpp +++ b/Windows_Libs/Dev/Render/RendererMatrix.cpp @@ -3,109 +3,109 @@ #include -const float* Renderer::MatrixGet(int type) +const float *Renderer::MatrixGet(int type) { - Context& context = this->getContext(); - const int depth = context.matrixStackDepth[type]; - return reinterpret_cast(&context.matrixStacks[type][depth]); + Context &context = this->getContext(); + const int depth = context.matrixStackDepth[type]; + return reinterpret_cast(&context.matrixStacks[type][depth]); } void Renderer::MatrixMode(int type) { - Context& context = this->getContext(); - context.matrixModeType = type; + Context &context = this->getContext(); + context.matrixModeType = type; } -void Renderer::MatrixMult(float* mat) +void Renderer::MatrixMult(float *mat) { - DirectX::XMMATRIX matrix; - std::memcpy(&matrix, mat, sizeof(matrix)); - this->MultWithStack(matrix); + DirectX::XMMATRIX matrix; + std::memcpy(&matrix, mat, sizeof(matrix)); + this->MultWithStack(matrix); } void Renderer::MatrixOrthogonal(float left, float right, float bottom, float top, float zNear, float zFar) { - const DirectX::XMMATRIX matrix = DirectX::XMMatrixOrthographicOffCenterRH(left, right, bottom, top, zNear, zFar); - this->MultWithStack(matrix); + const DirectX::XMMATRIX matrix = DirectX::XMMatrixOrthographicOffCenterRH(left, right, bottom, top, zNear, zFar); + this->MultWithStack(matrix); } void Renderer::MatrixPerspective(float fovy, float aspect, float zNear, float zFar) { - const float fovRadians = fovy * (3.14159274f / 180.0f); - const DirectX::XMMATRIX matrix = DirectX::XMMatrixPerspectiveFovRH(fovRadians, aspect, zNear, zFar); - this->MultWithStack(matrix); + const float fovRadians = fovy * (3.14159274f / 180.0f); + const DirectX::XMMATRIX matrix = DirectX::XMMatrixPerspectiveFovRH(fovRadians, aspect, zNear, zFar); + this->MultWithStack(matrix); } void Renderer::MatrixPop() { - Context& context = this->getContext(); - const int mode = context.matrixModeType; - --context.matrixStackDepth[mode]; - context.matrixDirty[mode] = true; + Context &context = this->getContext(); + const int mode = context.matrixModeType; + --context.matrixStackDepth[mode]; + context.matrixDirty[mode] = true; } void Renderer::MatrixPush() { - Context& context = this->getContext(); - const int mode = context.matrixModeType; - const int depth = context.matrixStackDepth[mode]; - context.matrixStacks[mode][depth + 1] = context.matrixStacks[mode][depth]; - ++context.matrixStackDepth[mode]; + Context &context = this->getContext(); + const int mode = context.matrixModeType; + const int depth = context.matrixStackDepth[mode]; + context.matrixStacks[mode][depth + 1] = context.matrixStacks[mode][depth]; + ++context.matrixStackDepth[mode]; } void Renderer::MatrixRotate(float angle, float x, float y, float z) { - const DirectX::XMVECTOR axis = DirectX::XMVectorSet(x, y, z, 0.0f); - const DirectX::XMMATRIX matrix = DirectX::XMMatrixRotationAxis(axis, angle); - this->MultWithStack(matrix); + const DirectX::XMVECTOR axis = DirectX::XMVectorSet(x, y, z, 0.0f); + const DirectX::XMMATRIX matrix = DirectX::XMMatrixRotationAxis(axis, angle); + this->MultWithStack(matrix); } void Renderer::MatrixScale(float x, float y, float z) { - const DirectX::XMMATRIX matrix = DirectX::XMMatrixScaling(x, y, z); - this->MultWithStack(matrix); + const DirectX::XMMATRIX matrix = DirectX::XMMatrixScaling(x, y, z); + this->MultWithStack(matrix); } void Renderer::MatrixSetIdentity() { - Context& context = this->getContext(); - const int mode = context.matrixModeType; - const int depth = context.matrixStackDepth[mode]; - context.matrixStacks[mode][depth] = DirectX::XMMatrixIdentity(); - context.matrixDirty[mode] = true; + Context &context = this->getContext(); + const int mode = context.matrixModeType; + const int depth = context.matrixStackDepth[mode]; + context.matrixStacks[mode][depth] = DirectX::XMMatrixIdentity(); + context.matrixDirty[mode] = true; } void Renderer::MatrixTranslate(float x, float y, float z) { - const DirectX::XMMATRIX matrix = DirectX::XMMatrixTranslation(x, y, z); - this->MultWithStack(matrix); + const DirectX::XMMATRIX matrix = DirectX::XMMatrixTranslation(x, y, z); + this->MultWithStack(matrix); } void Renderer::MultWithStack(DirectX::XMMATRIX matrix) { - Context& context = this->getContext(); - const int mode = context.matrixModeType; - const int depth = context.matrixStackDepth[mode]; - DirectX::XMMATRIX& current = context.matrixStacks[mode][depth]; - current = DirectX::XMMatrixMultiply(matrix, current); - context.matrixDirty[mode] = true; + Context &context = this->getContext(); + const int mode = context.matrixModeType; + const int depth = context.matrixStackDepth[mode]; + DirectX::XMMATRIX ¤t = context.matrixStacks[mode][depth]; + current = DirectX::XMMatrixMultiply(matrix, current); + context.matrixDirty[mode] = true; } void Renderer::Set_matrixDirty() { - Context& context = this->getContext(); - const DirectX::XMMATRIX identity = DirectX::XMMatrixIdentity(); + Context &context = this->getContext(); + const DirectX::XMMATRIX identity = DirectX::XMMatrixIdentity(); - context.matrixStacks[0][0] = identity; - context.matrixStacks[1][0] = identity; - context.matrixStacks[2][0] = identity; - context.matrixStacks[3][0] = identity; + context.matrixStacks[0][0] = identity; + context.matrixStacks[1][0] = identity; + context.matrixStacks[2][0] = identity; + context.matrixStacks[3][0] = identity; - context.matrixDirty[0] = true; - context.matrixDirty[1] = true; - context.matrixDirty[2] = true; - context.matrixDirty[3] = true; + context.matrixDirty[0] = true; + context.matrixDirty[1] = true; + context.matrixDirty[2] = true; + context.matrixDirty[3] = true; - activeVertexType = 0xFFFFFFFFu; - activePixelType = 0xFFFFFFFFu; + activeVertexType = 0xFFFFFFFFu; + activePixelType = 0xFFFFFFFFu; } diff --git a/Windows_Libs/Dev/Render/RendererState.cpp b/Windows_Libs/Dev/Render/RendererState.cpp index 127ff75..0f99238 100644 --- a/Windows_Libs/Dev/Render/RendererState.cpp +++ b/Windows_Libs/Dev/Render/RendererState.cpp @@ -5,652 +5,634 @@ #include #include -ID3D11BlendState* Renderer::GetManagedBlendState() +ID3D11BlendState *Renderer::GetManagedBlendState() { - Context& context = this->getContext(); - const D3D11_RENDER_TARGET_BLEND_DESC& rtBlend = context.blendDesc.RenderTarget[0]; + Context &context = this->getContext(); + const D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = context.blendDesc.RenderTarget[0]; - const int key = - (rtBlend.BlendEnable ? 1 : 0) | - ((static_cast(rtBlend.SrcBlend) & 0x1F) << 1) | - ((static_cast(rtBlend.DestBlend) & 0x1F) << 6) | - ((static_cast(rtBlend.RenderTargetWriteMask) & 0x0F) << 11); + const int key = (rtBlend.BlendEnable ? 1 : 0) | ((static_cast(rtBlend.SrcBlend) & 0x1F) << 1) | + ((static_cast(rtBlend.DestBlend) & 0x1F) << 6) | ((static_cast(rtBlend.RenderTargetWriteMask) & 0x0F) << 11); - auto it = managedBlendStates.find(key); - if (it != managedBlendStates.end()) - { - return it->second; - } + auto it = managedBlendStates.find(key); + if (it != managedBlendStates.end()) + { + return it->second; + } - ID3D11BlendState* state = nullptr; - m_pDevice->CreateBlendState(&context.blendDesc, &state); - managedBlendStates.emplace(key, state); - return state; + ID3D11BlendState *state = nullptr; + m_pDevice->CreateBlendState(&context.blendDesc, &state); + managedBlendStates.emplace(key, state); + return state; } -ID3D11DepthStencilState* Renderer::GetManagedDepthStencilState() +ID3D11DepthStencilState *Renderer::GetManagedDepthStencilState() { - Context& context = this->getContext(); + Context &context = this->getContext(); - const int key = - (context.depthStencilDesc.DepthEnable ? 2 : 0) | - ((static_cast(context.depthStencilDesc.DepthFunc) & 0x0F) << 2) | - (context.depthStencilDesc.DepthWriteMask == D3D11_DEPTH_WRITE_MASK_ALL ? 1 : 0); + const int key = (context.depthStencilDesc.DepthEnable ? 2 : 0) | ((static_cast(context.depthStencilDesc.DepthFunc) & 0x0F) << 2) | + (context.depthStencilDesc.DepthWriteMask == D3D11_DEPTH_WRITE_MASK_ALL ? 1 : 0); - auto it = managedDepthStencilStates.find(key); - if (it != managedDepthStencilStates.end()) - { - return it->second; - } + auto it = managedDepthStencilStates.find(key); + if (it != managedDepthStencilStates.end()) + { + return it->second; + } - ID3D11DepthStencilState* state = nullptr; - m_pDevice->CreateDepthStencilState(&context.depthStencilDesc, &state); - managedDepthStencilStates.emplace(key, state); - return state; + ID3D11DepthStencilState *state = nullptr; + m_pDevice->CreateDepthStencilState(&context.depthStencilDesc, &state); + managedDepthStencilStates.emplace(key, state); + return state; } -ID3D11RasterizerState* Renderer::GetManagedRasterizerState() +ID3D11RasterizerState *Renderer::GetManagedRasterizerState() { - Context& context = this->getContext(); + Context &context = this->getContext(); - const int key = - (static_cast(context.rasterizerDesc.DepthBias)) | - (static_cast(static_cast(context.rasterizerDesc.SlopeScaledDepthBias)) << 8) | - ((static_cast(context.rasterizerDesc.CullMode) & 0x03) << 16); + const int key = (static_cast(context.rasterizerDesc.DepthBias)) | + (static_cast(static_cast(context.rasterizerDesc.SlopeScaledDepthBias)) << 8) | + ((static_cast(context.rasterizerDesc.CullMode) & 0x03) << 16); - auto it = managedRasterizerStates.find(key); - if (it != managedRasterizerStates.end()) - { - return it->second; - } + auto it = managedRasterizerStates.find(key); + if (it != managedRasterizerStates.end()) + { + return it->second; + } - ID3D11RasterizerState* state = nullptr; - m_pDevice->CreateRasterizerState(&context.rasterizerDesc, &state); - managedRasterizerStates.emplace(key, state); - return state; + ID3D11RasterizerState *state = nullptr; + m_pDevice->CreateRasterizerState(&context.rasterizerDesc, &state); + managedRasterizerStates.emplace(key, state); + return state; } -ID3D11SamplerState* Renderer::GetManagedSamplerState() +ID3D11SamplerState *Renderer::GetManagedSamplerState() { - Context& context = this->getContext(); - const int key = m_textures[context.boundTextureIndex].samplerParams; + Context &context = this->getContext(); + const int key = m_textures[context.boundTextureIndex].samplerParams; - auto it = managedSamplerStates.find(key); - if (it != managedSamplerStates.end()) - { - return it->second; - } + auto it = managedSamplerStates.find(key); + if (it != managedSamplerStates.end()) + { + return it->second; + } - const bool clampU = (key & 0x01) != 0; - const bool clampV = (key & 0x02) != 0; - const bool linearFilter = (key & 0x04) != 0; - const bool mipLinear = (key & 0x08) != 0; - const int filterBits = (mipLinear ? 0x08 : 0x00) | (linearFilter ? 0x22 : 0x02); + const bool clampU = (key & 0x01) != 0; + const bool clampV = (key & 0x02) != 0; + const bool linearFilter = (key & 0x04) != 0; + const bool mipLinear = (key & 0x08) != 0; + const int filterBits = (mipLinear ? 0x08 : 0x00) | (linearFilter ? 0x22 : 0x02); - D3D11_SAMPLER_DESC desc = {}; - desc.Filter = static_cast(filterBits >> 1); - desc.AddressU = clampU ? D3D11_TEXTURE_ADDRESS_CLAMP : D3D11_TEXTURE_ADDRESS_WRAP; - desc.AddressV = clampV ? D3D11_TEXTURE_ADDRESS_CLAMP : D3D11_TEXTURE_ADDRESS_WRAP; - desc.AddressW = static_cast(3); - desc.MipLODBias = 0.0f; - desc.MaxAnisotropy = 16; - desc.ComparisonFunc = static_cast(1); - desc.BorderColor[0] = 0.0f; - desc.BorderColor[1] = 0.0f; - desc.BorderColor[2] = 0.0f; - desc.BorderColor[3] = 0.0f; - desc.MinLOD = -(std::numeric_limits::max)(); - desc.MaxLOD = (std::numeric_limits::max)(); + D3D11_SAMPLER_DESC desc = {}; + desc.Filter = static_cast(filterBits >> 1); + desc.AddressU = clampU ? D3D11_TEXTURE_ADDRESS_CLAMP : D3D11_TEXTURE_ADDRESS_WRAP; + desc.AddressV = clampV ? D3D11_TEXTURE_ADDRESS_CLAMP : D3D11_TEXTURE_ADDRESS_WRAP; + desc.AddressW = static_cast(3); + desc.MipLODBias = 0.0f; + desc.MaxAnisotropy = 16; + desc.ComparisonFunc = static_cast(1); + desc.BorderColor[0] = 0.0f; + desc.BorderColor[1] = 0.0f; + desc.BorderColor[2] = 0.0f; + desc.BorderColor[3] = 0.0f; + desc.MinLOD = -(std::numeric_limits::max)(); + desc.MaxLOD = (std::numeric_limits::max)(); - ID3D11SamplerState* state = nullptr; - m_pDevice->CreateSamplerState(&desc, &state); - managedSamplerStates.emplace(key, state); - return state; + ID3D11SamplerState *state = nullptr; + m_pDevice->CreateSamplerState(&desc, &state); + managedSamplerStates.emplace(key, state); + return state; } void Renderer::StateSetFogEnable(bool enable) { - Context& context = this->getContext(); - context.fogEnabled = enable ? 1 : 0; + Context &context = this->getContext(); + context.fogEnabled = enable ? 1 : 0; } void Renderer::StateSetFogMode(int mode) { - Context& context = this->getContext(); - context.fogMode = mode; + Context &context = this->getContext(); + context.fogMode = mode; } void Renderer::StateSetFogNearDistance(float dist) { - Context& context = this->getContext(); - context.fogNearDistance = dist; + Context &context = this->getContext(); + context.fogNearDistance = dist; } void Renderer::StateSetFogFarDistance(float dist) { - Context& context = this->getContext(); - context.fogFarDistance = dist; + Context &context = this->getContext(); + context.fogFarDistance = dist; } void Renderer::StateSetFogDensity(float density) { - Context& context = this->getContext(); - context.fogDensity = density; + Context &context = this->getContext(); + context.fogDensity = density; } void Renderer::StateSetFogColour(float red, float green, float blue) { - Context& context = this->getContext(); - context.fogColourRed = red; - context.fogColourBlue = blue; - context.fogColourGreen = green; + Context &context = this->getContext(); + context.fogColourRed = red; + context.fogColourBlue = blue; + context.fogColourGreen = green; } -void Renderer::UpdateViewportState() -{ -} +void Renderer::UpdateViewportState() {} void Renderer::StateSetLightingEnable(bool enable) { - Context& context = this->getContext(); - if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) - { - context.commandBuffer->SetLightingEnable(enable); - return; - } + Context &context = this->getContext(); + if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) + { + context.commandBuffer->SetLightingEnable(enable); + return; + } - context.lightingEnabled = enable ? 1 : 0; + context.lightingEnabled = enable ? 1 : 0; } void Renderer::StateSetLightColour(int light, float red, float green, float blue) { - if (light >= 2) - { - return; - } + if (light >= 2) + { + return; + } - Context& context = this->getContext(); - if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) - { - context.commandBuffer->SetLightColour(light, red, green, blue); - return; - } + Context &context = this->getContext(); + if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) + { + context.commandBuffer->SetLightColour(light, red, green, blue); + return; + } - context.lightColour[light].x = red; - context.lightColour[light].y = green; - context.lightColour[light].z = blue; - context.lightColour[light].w = 1.0f; - context.lightingDirty = 1; + context.lightColour[light].x = red; + context.lightColour[light].y = green; + context.lightColour[light].z = blue; + context.lightColour[light].w = 1.0f; + context.lightingDirty = 1; } void Renderer::StateSetLightAmbientColour(float red, float green, float blue) { - Context& context = this->getContext(); - if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) - { - context.commandBuffer->SetLightAmbientColour(red, green, blue); - return; - } + Context &context = this->getContext(); + if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) + { + context.commandBuffer->SetLightAmbientColour(red, green, blue); + return; + } - context.lightAmbientColour.x = red; - context.lightAmbientColour.y = green; - context.lightAmbientColour.z = blue; - context.lightAmbientColour.w = 1.0f; - context.lightingDirty = 1; + context.lightAmbientColour.x = red; + context.lightAmbientColour.y = green; + context.lightAmbientColour.z = blue; + context.lightAmbientColour.w = 1.0f; + context.lightingDirty = 1; } void Renderer::StateSetLightEnable(int light, bool enable) { - if (light >= 2) - { - return; - } + if (light >= 2) + { + return; + } - Context& context = this->getContext(); - if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) - { - context.commandBuffer->SetLightEnable(light, enable); - return; - } + Context &context = this->getContext(); + if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) + { + context.commandBuffer->SetLightEnable(light, enable); + return; + } - context.lightEnabled[light] = enable ? 1 : 0; - context.lightingDirty = 1; + context.lightEnabled[light] = enable ? 1 : 0; + context.lightingDirty = 1; } void Renderer::StateSetColour(float r, float g, float b, float a) { - Context& context = this->getContext(); - if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) - { - context.commandBuffer->SetColor(r, g, b, a); - return; - } + Context &context = this->getContext(); + if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) + { + context.commandBuffer->SetColor(r, g, b, a); + return; + } - ID3D11DeviceContext* d3d11 = context.m_pDeviceContext; - const float colour[4] = { r, g, b, a }; + ID3D11DeviceContext *d3d11 = context.m_pDeviceContext; + const float colour[4] = {r, g, b, a}; - D3D11_MAPPED_SUBRESOURCE mapped = {}; - d3d11->Map(context.cbColour, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); - std::memcpy(mapped.pData, colour, sizeof(colour)); - d3d11->Unmap(context.cbColour, 0); + D3D11_MAPPED_SUBRESOURCE mapped = {}; + d3d11->Map(context.cbColour, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + std::memcpy(mapped.pData, colour, sizeof(colour)); + d3d11->Unmap(context.cbColour, 0); } void Renderer::StateSetDepthMask(bool enable) { - Context& context = this->getContext(); - if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) - { - context.commandBuffer->SetDepthMask(enable); - return; - } + Context &context = this->getContext(); + if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) + { + context.commandBuffer->SetDepthMask(enable); + return; + } - context.depthStencilDesc.DepthWriteMask = enable ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO; - context.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0); - context.depthWriteEnabled = enable ? 1 : 0; + context.depthStencilDesc.DepthWriteMask = enable ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO; + context.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0); + context.depthWriteEnabled = enable ? 1 : 0; } void Renderer::StateSetBlendEnable(bool enable) { - Context& context = this->getContext(); - if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) - { - context.commandBuffer->SetBlendEnable(enable); - return; - } + Context &context = this->getContext(); + if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) + { + context.commandBuffer->SetBlendEnable(enable); + return; + } - context.blendDesc.RenderTarget[0].BlendEnable = enable ? TRUE : FALSE; - context.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); + context.blendDesc.RenderTarget[0].BlendEnable = enable ? TRUE : FALSE; + context.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); } void Renderer::StateSetBlendFunc(int src, int dst) { - Context& context = this->getContext(); - if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) - { - context.commandBuffer->SetBlendFunc(src, dst); - return; - } + Context &context = this->getContext(); + if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) + { + context.commandBuffer->SetBlendFunc(src, dst); + return; + } - context.blendDesc.RenderTarget[0].SrcBlend = static_cast(src); - context.blendDesc.RenderTarget[0].DestBlend = static_cast(dst); - context.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); + context.blendDesc.RenderTarget[0].SrcBlend = static_cast(src); + context.blendDesc.RenderTarget[0].DestBlend = static_cast(dst); + context.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); } void Renderer::StateSetBlendFactor(unsigned int colour) { - Context& context = this->getContext(); - if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) - { - context.commandBuffer->SetBlendFactor(colour); - return; - } + Context &context = this->getContext(); + if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) + { + context.commandBuffer->SetBlendFactor(colour); + return; + } - const float scale = 255.0f; - context.blendFactor[0] = static_cast((colour >> 0) & 0xFFu) / scale; - context.blendFactor[1] = static_cast((colour >> 8) & 0xFFu) / scale; - context.blendFactor[2] = static_cast((colour >> 16) & 0xFFu) / scale; - context.blendFactor[3] = static_cast((colour >> 24) & 0xFFu) / scale; - context.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); + const float scale = 255.0f; + context.blendFactor[0] = static_cast((colour >> 0) & 0xFFu) / scale; + context.blendFactor[1] = static_cast((colour >> 8) & 0xFFu) / scale; + context.blendFactor[2] = static_cast((colour >> 16) & 0xFFu) / scale; + context.blendFactor[3] = static_cast((colour >> 24) & 0xFFu) / scale; + context.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); } void Renderer::StateSetAlphaFunc(int, float param) { - Context& context = this->getContext(); - context.alphaReference = param; + Context &context = this->getContext(); + context.alphaReference = param; - const float alpha[4] = { 0.0f, 0.0f, 0.0f, context.alphaTestEnabled ? context.alphaReference : 0.0f }; - D3D11_MAPPED_SUBRESOURCE mapped = {}; - context.m_pDeviceContext->Map(context.cbAlphaTest, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); - std::memcpy(mapped.pData, alpha, sizeof(alpha)); - context.m_pDeviceContext->Unmap(context.cbAlphaTest, 0); + const float alpha[4] = {0.0f, 0.0f, 0.0f, context.alphaTestEnabled ? context.alphaReference : 0.0f}; + D3D11_MAPPED_SUBRESOURCE mapped = {}; + context.m_pDeviceContext->Map(context.cbAlphaTest, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + std::memcpy(mapped.pData, alpha, sizeof(alpha)); + context.m_pDeviceContext->Unmap(context.cbAlphaTest, 0); } void Renderer::StateSetDepthFunc(int func) { - Context& context = this->getContext(); - if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) - { - context.commandBuffer->SetDepthFunc(func); - return; - } + Context &context = this->getContext(); + if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) + { + context.commandBuffer->SetDepthFunc(func); + return; + } - context.depthStencilDesc.DepthFunc = static_cast(func); - context.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0); + context.depthStencilDesc.DepthFunc = static_cast(func); + context.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0); } void Renderer::StateSetFaceCull(bool enable) { - Context& context = this->getContext(); - if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) - { - context.commandBuffer->SetFaceCull(enable); - return; - } + Context &context = this->getContext(); + if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) + { + context.commandBuffer->SetFaceCull(enable); + return; + } - context.rasterizerDesc.CullMode = enable ? D3D11_CULL_BACK : D3D11_CULL_NONE; - context.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); - context.faceCullEnabled = enable ? 1 : 0; + context.rasterizerDesc.CullMode = enable ? D3D11_CULL_BACK : D3D11_CULL_NONE; + context.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); + context.faceCullEnabled = enable ? 1 : 0; } void Renderer::StateSetFaceCullCW(bool enable) { - Context& context = this->getContext(); - if (context.faceCullEnabled) - { - context.rasterizerDesc.CullMode = enable ? D3D11_CULL_BACK : D3D11_CULL_FRONT; - } - else - { - context.rasterizerDesc.CullMode = D3D11_CULL_NONE; - } + Context &context = this->getContext(); + if (context.faceCullEnabled) + { + context.rasterizerDesc.CullMode = enable ? D3D11_CULL_BACK : D3D11_CULL_FRONT; + } + else + { + context.rasterizerDesc.CullMode = D3D11_CULL_NONE; + } - context.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); + context.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); } -void Renderer::StateSetLineWidth(float) -{ -} +void Renderer::StateSetLineWidth(float) {} void Renderer::StateSetWriteEnable(bool red, bool green, bool blue, bool alpha) { - Context& context = this->getContext(); + Context &context = this->getContext(); - std::uint8_t mask = 0; - mask |= red ? 0x1 : 0; - mask |= green ? 0x2 : 0; - mask |= blue ? 0x4 : 0; - mask |= alpha ? 0x8 : 0; + std::uint8_t mask = 0; + mask |= red ? 0x1 : 0; + mask |= green ? 0x2 : 0; + mask |= blue ? 0x4 : 0; + mask |= alpha ? 0x8 : 0; - context.blendDesc.RenderTarget[0].RenderTargetWriteMask = mask; - context.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); + context.blendDesc.RenderTarget[0].RenderTargetWriteMask = mask; + context.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), context.blendFactor, 0xFFFFFFFFu); } void Renderer::StateSetDepthTestEnable(bool enable) { - Context& context = this->getContext(); - if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) - { - context.commandBuffer->SetDepthTestEnable(enable); - return; - } + Context &context = this->getContext(); + if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) + { + context.commandBuffer->SetDepthTestEnable(enable); + return; + } - context.depthStencilDesc.DepthEnable = enable ? TRUE : FALSE; - context.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0); - context.depthTestEnabled = enable ? 1 : 0; + context.depthStencilDesc.DepthEnable = enable ? TRUE : FALSE; + context.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0); + context.depthTestEnabled = enable ? 1 : 0; } void Renderer::StateSetAlphaTestEnable(bool enable) { - Context& context = this->getContext(); - context.alphaTestEnabled = enable ? 1 : 0; + Context &context = this->getContext(); + context.alphaTestEnabled = enable ? 1 : 0; - const float alpha[4] = { 0.0f, 0.0f, 0.0f, enable ? context.alphaReference : 0.0f }; - D3D11_MAPPED_SUBRESOURCE mapped = {}; - context.m_pDeviceContext->Map(context.cbAlphaTest, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); - std::memcpy(mapped.pData, alpha, sizeof(alpha)); - context.m_pDeviceContext->Unmap(context.cbAlphaTest, 0); + const float alpha[4] = {0.0f, 0.0f, 0.0f, enable ? context.alphaReference : 0.0f}; + D3D11_MAPPED_SUBRESOURCE mapped = {}; + context.m_pDeviceContext->Map(context.cbAlphaTest, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + std::memcpy(mapped.pData, alpha, sizeof(alpha)); + context.m_pDeviceContext->Unmap(context.cbAlphaTest, 0); } void Renderer::StateSetDepthSlopeAndBias(float slope, float bias) { - Context& context = this->getContext(); + Context &context = this->getContext(); - const float scale = 65536.0f; - context.rasterizerDesc.DepthBias = static_cast(bias * scale); - context.rasterizerDesc.SlopeScaledDepthBias = slope * scale; - context.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); + const float scale = 65536.0f; + context.rasterizerDesc.DepthBias = static_cast(bias * scale); + context.rasterizerDesc.SlopeScaledDepthBias = slope * scale; + context.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); } void Renderer::UpdateFogState() { - Context& context = this->getContext(); - ID3D11DeviceContext* d3d11 = context.m_pDeviceContext; + Context &context = this->getContext(); + ID3D11DeviceContext *d3d11 = context.m_pDeviceContext; - float fogParams[4] = {}; - if (context.fogEnabled) - { - if (context.fogMode == 1) - { - fogParams[0] = context.fogFarDistance; - fogParams[1] = 1.0f / (context.fogFarDistance - context.fogNearDistance); - fogParams[2] = 1.0f; - } - else - { - fogParams[0] = context.fogDensity; - fogParams[2] = 2.0f; - } - } + float fogParams[4] = {}; + if (context.fogEnabled) + { + if (context.fogMode == 1) + { + fogParams[0] = context.fogFarDistance; + fogParams[1] = 1.0f / (context.fogFarDistance - context.fogNearDistance); + fogParams[2] = 1.0f; + } + else + { + fogParams[0] = context.fogDensity; + fogParams[2] = 2.0f; + } + } - const float fogColour[4] = - { - context.fogColourRed, - context.fogColourGreen, - context.fogColourBlue, - 1.0f - }; + const float fogColour[4] = {context.fogColourRed, context.fogColourGreen, context.fogColourBlue, 1.0f}; - D3D11_MAPPED_SUBRESOURCE mapped = {}; - d3d11->Map(context.cbFogParams, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); - std::memcpy(mapped.pData, fogParams, sizeof(fogParams)); - d3d11->Unmap(context.cbFogParams, 0); + D3D11_MAPPED_SUBRESOURCE mapped = {}; + d3d11->Map(context.cbFogParams, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + std::memcpy(mapped.pData, fogParams, sizeof(fogParams)); + d3d11->Unmap(context.cbFogParams, 0); - d3d11->Map(context.cbFogColour, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); - std::memcpy(mapped.pData, fogColour, sizeof(fogColour)); - d3d11->Unmap(context.cbFogColour, 0); + d3d11->Map(context.cbFogColour, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + std::memcpy(mapped.pData, fogColour, sizeof(fogColour)); + d3d11->Unmap(context.cbFogColour, 0); } void Renderer::StateSetVertexTextureUV(float u, float v) { - Context& context = this->getContext(); - const float texgen[4] = { u - 1.0f, v - 1.0f, 0.0f, 0.0f }; + Context &context = this->getContext(); + const float texgen[4] = {u - 1.0f, v - 1.0f, 0.0f, 0.0f}; - D3D11_MAPPED_SUBRESOURCE mapped = {}; - context.m_pDeviceContext->Map(context.cbVertexTexcoord, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); - std::memcpy(mapped.pData, texgen, sizeof(texgen)); - context.m_pDeviceContext->Unmap(context.cbVertexTexcoord, 0); + D3D11_MAPPED_SUBRESOURCE mapped = {}; + context.m_pDeviceContext->Map(context.cbVertexTexcoord, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + std::memcpy(mapped.pData, texgen, sizeof(texgen)); + context.m_pDeviceContext->Unmap(context.cbVertexTexcoord, 0); } void Renderer::UpdateTexGenState() { - Context& context = this->getContext(); + Context &context = this->getContext(); - D3D11_MAPPED_SUBRESOURCE mapped = {}; - context.m_pDeviceContext->Map(context.cbTexGen, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); - std::memcpy(mapped.pData, context.texGenMatrices, sizeof(context.texGenMatrices)); - context.m_pDeviceContext->Unmap(context.cbTexGen, 0); + D3D11_MAPPED_SUBRESOURCE mapped = {}; + context.m_pDeviceContext->Map(context.cbTexGen, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + std::memcpy(mapped.pData, context.texGenMatrices, sizeof(context.texGenMatrices)); + context.m_pDeviceContext->Unmap(context.cbTexGen, 0); } void Renderer::UpdateLightingState() { - Context& context = this->getContext(); - if (!context.lightingDirty || !context.lightingEnabled) - { - return; - } + Context &context = this->getContext(); + if (!context.lightingDirty || !context.lightingEnabled) + { + return; + } - if (!context.lightEnabled[0]) - { - std::memset(&context.lightDirection[0], 0, sizeof(context.lightDirection[0])); - std::memset(&context.lightColour[0], 0, sizeof(context.lightColour[0])); - } + if (!context.lightEnabled[0]) + { + std::memset(&context.lightDirection[0], 0, sizeof(context.lightDirection[0])); + std::memset(&context.lightColour[0], 0, sizeof(context.lightColour[0])); + } - if (!context.lightEnabled[1]) - { - std::memset(&context.lightDirection[1], 0, sizeof(context.lightDirection[1])); - std::memset(&context.lightColour[1], 0, sizeof(context.lightColour[1])); - } + if (!context.lightEnabled[1]) + { + std::memset(&context.lightDirection[1], 0, sizeof(context.lightDirection[1])); + std::memset(&context.lightColour[1], 0, sizeof(context.lightColour[1])); + } - const std::size_t lightingBytes = sizeof(context.lightDirection) + sizeof(context.lightColour) + sizeof(context.lightAmbientColour); - D3D11_MAPPED_SUBRESOURCE mapped = {}; - context.m_pDeviceContext->Map(context.cbLighting, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); - std::memcpy(mapped.pData, context.lightDirection, lightingBytes); - context.m_pDeviceContext->Unmap(context.cbLighting, 0); + const std::size_t lightingBytes = sizeof(context.lightDirection) + sizeof(context.lightColour) + sizeof(context.lightAmbientColour); + D3D11_MAPPED_SUBRESOURCE mapped = {}; + context.m_pDeviceContext->Map(context.cbLighting, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + std::memcpy(mapped.pData, context.lightDirection, lightingBytes); + context.m_pDeviceContext->Unmap(context.cbLighting, 0); - context.lightingDirty = 0; + context.lightingDirty = 0; } void Renderer::StateSetLightDirection(int light, float x, float y, float z) { - if (light >= 2) - { - return; - } + if (light >= 2) + { + return; + } - Context& context = this->getContext(); - if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) - { - context.commandBuffer->SetLightDirection(light, x, y, z); - return; - } + Context &context = this->getContext(); + if (context.commandBuffer != nullptr && context.commandBuffer->isActive != 0) + { + context.commandBuffer->SetLightDirection(light, x, y, z); + return; + } - const std::uint32_t stackIndex = context.matrixStackDepth[0]; - const DirectX::XMMATRIX& modelView = context.matrixStacks[0][stackIndex]; - const DirectX::XMVECTOR direction = DirectX::XMVectorSet(x, y, z, 0.0f); - const DirectX::XMVECTOR transformed = DirectX::XMVector3TransformNormal(direction, modelView); - const DirectX::XMVECTOR normalized = DirectX::XMVector3Normalize(transformed); + const std::uint32_t stackIndex = context.matrixStackDepth[0]; + const DirectX::XMMATRIX &modelView = context.matrixStacks[0][stackIndex]; + const DirectX::XMVECTOR direction = DirectX::XMVectorSet(x, y, z, 0.0f); + const DirectX::XMVECTOR transformed = DirectX::XMVector3TransformNormal(direction, modelView); + const DirectX::XMVECTOR normalized = DirectX::XMVector3Normalize(transformed); - DirectX::XMStoreFloat4(&context.lightDirection[light], normalized); - context.lightingDirty = 1; + DirectX::XMStoreFloat4(&context.lightDirection[light], normalized); + context.lightingDirty = 1; } void Renderer::StateSetViewport(C4JRender::eViewportType viewportType) { - this->getContext(); - m_ViewportType = viewportType; + this->getContext(); + m_ViewportType = viewportType; - const float fullWidth = static_cast(backBufferWidth); - const float fullHeight = static_cast(backBufferHeight); + const float fullWidth = static_cast(backBufferWidth); + const float fullHeight = static_cast(backBufferHeight); - float x = 0.0f; - float y = 0.0f; - float width = fullWidth; - float height = fullHeight; + float x = 0.0f; + float y = 0.0f; + float width = fullWidth; + float height = fullHeight; - switch (viewportType) - { - case C4JRender::VIEWPORT_TYPE_FULLSCREEN: - break; - case C4JRender::VIEWPORT_TYPE_SPLIT_TOP: - y = fullHeight * 0.5f; - height = fullHeight * 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_SPLIT_BOTTOM: - height = fullHeight * 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_SPLIT_LEFT: - width = fullWidth * 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_SPLIT_RIGHT: - x = fullWidth * 0.5f; - width = fullWidth * 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_QUADRANT_TOP_LEFT: - width = fullWidth * 0.5f; - height = fullHeight * 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_QUADRANT_TOP_RIGHT: - x = fullWidth * 0.5f; - width = fullWidth * 0.5f; - height = fullHeight * 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_QUADRANT_BOTTOM_LEFT: - y = fullHeight * 0.5f; - width = fullWidth * 0.5f; - height = fullHeight * 0.5f; - break; - case C4JRender::VIEWPORT_TYPE_QUADRANT_BOTTOM_RIGHT: - x = fullWidth * 0.5f; - y = fullHeight * 0.5f; - width = fullWidth * 0.5f; - height = fullHeight * 0.5f; - break; - default: - break; - } + switch (viewportType) + { + case C4JRender::VIEWPORT_TYPE_FULLSCREEN: + break; + case C4JRender::VIEWPORT_TYPE_SPLIT_TOP: + y = fullHeight * 0.5f; + height = fullHeight * 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_SPLIT_BOTTOM: + height = fullHeight * 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_SPLIT_LEFT: + width = fullWidth * 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_SPLIT_RIGHT: + x = fullWidth * 0.5f; + width = fullWidth * 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_QUADRANT_TOP_LEFT: + width = fullWidth * 0.5f; + height = fullHeight * 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_QUADRANT_TOP_RIGHT: + x = fullWidth * 0.5f; + width = fullWidth * 0.5f; + height = fullHeight * 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_QUADRANT_BOTTOM_LEFT: + y = fullHeight * 0.5f; + width = fullWidth * 0.5f; + height = fullHeight * 0.5f; + break; + case C4JRender::VIEWPORT_TYPE_QUADRANT_BOTTOM_RIGHT: + x = fullWidth * 0.5f; + y = fullHeight * 0.5f; + width = fullWidth * 0.5f; + height = fullHeight * 0.5f; + break; + default: + break; + } - D3D11_VIEWPORT viewport = {}; - viewport.TopLeftX = x; - viewport.TopLeftY = y; - viewport.Width = width; - viewport.Height = height; - viewport.MinDepth = 0.0f; - viewport.MaxDepth = 1.0f; + D3D11_VIEWPORT viewport = {}; + viewport.TopLeftX = x; + viewport.TopLeftY = y; + viewport.Width = width; + viewport.Height = height; + viewport.MinDepth = 0.0f; + viewport.MaxDepth = 1.0f; - m_pDeviceContext->RSSetViewports(1, &viewport); - m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, depthStencilView); + m_pDeviceContext->RSSetViewports(1, &viewport); + m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, depthStencilView); } -void Renderer::StateSetEnableViewportClipPlanes(bool) -{ -} +void Renderer::StateSetEnableViewportClipPlanes(bool) {} void Renderer::StateSetTexGenCol(int col, float x, float y, float z, float w, bool eyeSpace) { - Context& context = this->getContext(); + Context &context = this->getContext(); - DirectX::XMVECTOR plane = DirectX::XMVectorSet(x, y, z, w); - if (eyeSpace) - { - DirectX::XMFLOAT4X4 modelView; - std::memset(&modelView, 0, sizeof(modelView)); - std::memcpy(&modelView, this->MatrixGet(0), sizeof(modelView)); + DirectX::XMVECTOR plane = DirectX::XMVectorSet(x, y, z, w); + if (eyeSpace) + { + DirectX::XMFLOAT4X4 modelView; + std::memset(&modelView, 0, sizeof(modelView)); + std::memcpy(&modelView, this->MatrixGet(0), sizeof(modelView)); - DirectX::XMVECTOR determinant = DirectX::XMVectorZero(); - const DirectX::XMMATRIX inverse = DirectX::XMMatrixInverse(&determinant, DirectX::XMLoadFloat4x4(&modelView)); - plane = DirectX::XMVector4Transform(plane, inverse); - } + DirectX::XMVECTOR determinant = DirectX::XMVectorZero(); + const DirectX::XMMATRIX inverse = DirectX::XMMatrixInverse(&determinant, DirectX::XMLoadFloat4x4(&modelView)); + plane = DirectX::XMVector4Transform(plane, inverse); + } - DirectX::XMFLOAT4 transformed; - DirectX::XMStoreFloat4(&transformed, plane); + DirectX::XMFLOAT4 transformed; + DirectX::XMStoreFloat4(&transformed, plane); - const int activeSet = eyeSpace ? 0 : 1; - const int inactiveSet = eyeSpace ? 1 : 0; + const int activeSet = eyeSpace ? 0 : 1; + const int inactiveSet = eyeSpace ? 1 : 0; - float* active = reinterpret_cast(&context.texGenMatrices[activeSet]); - active[col + 0] = transformed.x; - active[col + 4] = transformed.y; - active[col + 8] = transformed.z; - active[col + 12] = transformed.w; + float *active = reinterpret_cast(&context.texGenMatrices[activeSet]); + active[col + 0] = transformed.x; + active[col + 4] = transformed.y; + active[col + 8] = transformed.z; + active[col + 12] = transformed.w; - float* inactive = reinterpret_cast(&context.texGenMatrices[inactiveSet]); - inactive[col + 0] = 0.0f; - inactive[col + 4] = 0.0f; - inactive[col + 8] = 0.0f; - inactive[col + 12] = 0.0f; + float *inactive = reinterpret_cast(&context.texGenMatrices[inactiveSet]); + inactive[col + 0] = 0.0f; + inactive[col + 4] = 0.0f; + inactive[col + 8] = 0.0f; + inactive[col + 12] = 0.0f; } void Renderer::StateSetStencil(D3D11_COMPARISON_FUNC function, uint8_t stencil_ref, uint8_t stencil_func_mask, uint8_t stencil_write_mask) { - Context& context = this->getContext(); + Context &context = this->getContext(); - D3D11_DEPTH_STENCIL_DESC desc = context.depthStencilDesc; - desc.StencilEnable = TRUE; - desc.StencilReadMask = stencil_func_mask; - desc.StencilWriteMask = stencil_write_mask; - desc.FrontFace.StencilFunc = function; - desc.BackFace.StencilFunc = function; + D3D11_DEPTH_STENCIL_DESC desc = context.depthStencilDesc; + desc.StencilEnable = TRUE; + desc.StencilReadMask = stencil_func_mask; + desc.StencilWriteMask = stencil_write_mask; + desc.FrontFace.StencilFunc = function; + desc.BackFace.StencilFunc = function; - ID3D11DepthStencilState* state = nullptr; - m_pDevice->CreateDepthStencilState(&desc, &state); - m_pDeviceContext->OMSetDepthStencilState(state, stencil_ref); - if (state != nullptr) - { - state->Release(); - } + ID3D11DepthStencilState *state = nullptr; + m_pDevice->CreateDepthStencilState(&desc, &state); + m_pDeviceContext->OMSetDepthStencilState(state, stencil_ref); + if (state != nullptr) + { + state->Release(); + } } void Renderer::StateSetForceLOD(int LOD) { - Context& context = this->getContext(); - context.forcedLOD = LOD; + Context &context = this->getContext(); + context.forcedLOD = LOD; } void Renderer::StateUpdate() { - Context& context = this->getContext(); - this->StateSetFaceCull(context.faceCullEnabled != 0); - this->StateSetDepthMask(context.depthWriteEnabled != 0); - this->StateSetDepthTestEnable(context.depthTestEnabled != 0); - this->StateSetAlphaTestEnable(context.alphaTestEnabled != 0); + Context &context = this->getContext(); + this->StateSetFaceCull(context.faceCullEnabled != 0); + this->StateSetDepthMask(context.depthWriteEnabled != 0); + this->StateSetDepthTestEnable(context.depthTestEnabled != 0); + this->StateSetAlphaTestEnable(context.alphaTestEnabled != 0); } diff --git a/Windows_Libs/Dev/Render/RendererTexture.cpp b/Windows_Libs/Dev/Render/RendererTexture.cpp index ce5ea4e..6709032 100644 --- a/Windows_Libs/Dev/Render/RendererTexture.cpp +++ b/Windows_Libs/Dev/Render/RendererTexture.cpp @@ -11,319 +11,348 @@ DXGI_FORMAT Renderer::textureFormats[C4JRender::MAX_TEXTURE_FORMATS] = { DXGI_FORMAT_B8G8R8A8_UNORM, }; -int Renderer::TextureCreate() { - for (int i = 0; i < 512; ++i) { - Texture &texture = m_textures[i]; - if (!texture.allocated) { - texture.texture = nullptr; - texture.allocated = true; - texture.mipLevels = 1; - texture.samplerParams = 0; - return i; +int Renderer::TextureCreate() +{ + for (int i = 0; i < 512; ++i) + { + Texture &texture = m_textures[i]; + if (!texture.allocated) + { + texture.texture = nullptr; + texture.allocated = true; + texture.mipLevels = 1; + texture.samplerParams = 0; + return i; + } } - } - return -1; + return -1; } -void Renderer::TextureSetTextureLevels(int levels) { - const int boundTextureIndex = this->getContext().boundTextureIndex; - m_textures[boundTextureIndex].mipLevels = levels; +void Renderer::TextureSetTextureLevels(int levels) +{ + const int boundTextureIndex = this->getContext().boundTextureIndex; + m_textures[boundTextureIndex].mipLevels = levels; } -int Renderer::TextureGetTextureLevels() { - const int boundTextureIndex = this->getContext().boundTextureIndex; - return m_textures[boundTextureIndex].mipLevels; +int Renderer::TextureGetTextureLevels() +{ + const int boundTextureIndex = this->getContext().boundTextureIndex; + return m_textures[boundTextureIndex].mipLevels; } -void Renderer::TextureSetParam(int param, int value) { - Texture &texture = m_textures[this->getContext().boundTextureIndex]; +void Renderer::TextureSetParam(int param, int value) +{ + Texture &texture = m_textures[this->getContext().boundTextureIndex]; - switch (param) { - case GL_TEXTURE_MIN_FILTER: - texture.samplerParams &= ~4u; - if (value == 1) { - texture.samplerParams |= 4u; + switch (param) + { + case GL_TEXTURE_MIN_FILTER: + texture.samplerParams &= ~4u; + if (value == 1) + { + texture.samplerParams |= 4u; + } + break; + case GL_TEXTURE_MAG_FILTER: + texture.samplerParams &= ~8u; + if (value == 1) + { + texture.samplerParams |= 8u; + } + break; + case GL_TEXTURE_WRAP_S: + texture.samplerParams &= ~1u; + if (value == 0) + { + texture.samplerParams |= 1u; + } + break; + case GL_TEXTURE_WRAP_T: + case 5: + texture.samplerParams &= ~2u; + if (value == 0) + { + texture.samplerParams |= 2u; + } + break; + default: + break; } - break; - case GL_TEXTURE_MAG_FILTER: - texture.samplerParams &= ~8u; - if (value == 1) { - texture.samplerParams |= 8u; - } - break; - case GL_TEXTURE_WRAP_S: - texture.samplerParams &= ~1u; - if (value == 0) { - texture.samplerParams |= 1u; - } - break; - case GL_TEXTURE_WRAP_T: - case 5: - texture.samplerParams &= ~2u; - if (value == 0) { - texture.samplerParams |= 2u; - } - break; - default: - break; - } } void Renderer::TextureDynamicUpdateStart() {} void Renderer::TextureDynamicUpdateEnd() {} -void Renderer::TextureData(int width, int height, void *data, int level, - C4JRender::eTextureFormat format) { - Renderer::Context &context = this->getContext(); - Texture &texture = m_textures[context.boundTextureIndex]; - texture.textureFormat = format; +void Renderer::TextureData(int width, int height, void *data, int level, C4JRender::eTextureFormat format) +{ + Renderer::Context &context = this->getContext(); + Texture &texture = m_textures[context.boundTextureIndex]; + texture.textureFormat = format; + + if (level == 0) + { + D3D11_TEXTURE2D_DESC desc = {}; + desc.Width = width; + desc.Height = height; + desc.MipLevels = texture.mipLevels; + desc.ArraySize = 1; + desc.Format = textureFormats[format]; + desc.SampleDesc.Count = 1; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + m_pDevice->CreateTexture2D(&desc, nullptr, &texture.texture); + m_pDevice->CreateShaderResourceView(texture.texture, nullptr, &texture.view); + } + + const UINT rowPitch = width * 4u; + const UINT depthPitch = width * height * 4u; + context.m_pDeviceContext->UpdateSubresource(texture.texture, level, nullptr, data, rowPitch, depthPitch); +} + +void Renderer::TextureDataUpdate(int xoffset, int yoffset, int width, int height, void *data, int level) +{ + Renderer::Context &context = this->getContext(); + Texture &texture = m_textures[context.boundTextureIndex]; + + D3D11_BOX box = {}; + box.left = xoffset; + box.right = xoffset + width; + box.top = yoffset; + box.bottom = yoffset + height; + box.front = 0; + box.back = 1; - if (level == 0) { D3D11_TEXTURE2D_DESC desc = {}; - desc.Width = width; - desc.Height = height; - desc.MipLevels = texture.mipLevels; - desc.ArraySize = 1; - desc.Format = textureFormats[format]; - desc.SampleDesc.Count = 1; - desc.Usage = D3D11_USAGE_DEFAULT; - desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; - m_pDevice->CreateTexture2D(&desc, nullptr, &texture.texture); - m_pDevice->CreateShaderResourceView(texture.texture, nullptr, - &texture.view); - } + texture.texture->GetDesc(&desc); - const UINT rowPitch = width * 4u; - const UINT depthPitch = width * height * 4u; - context.m_pDeviceContext->UpdateSubresource(texture.texture, level, nullptr, - data, rowPitch, depthPitch); + const UINT rowPitch = width * 4u; + const UINT depthPitch = width * height * 4u; + context.m_pDeviceContext->UpdateSubresource(texture.texture, level, &box, data, rowPitch, depthPitch); } -void Renderer::TextureDataUpdate(int xoffset, int yoffset, int width, - int height, void *data, int level) { - Renderer::Context &context = this->getContext(); - Texture &texture = m_textures[context.boundTextureIndex]; - - D3D11_BOX box = {}; - box.left = xoffset; - box.right = xoffset + width; - box.top = yoffset; - box.bottom = yoffset + height; - box.front = 0; - box.back = 1; - - D3D11_TEXTURE2D_DESC desc = {}; - texture.texture->GetDesc(&desc); - - const UINT rowPitch = width * 4u; - const UINT depthPitch = width * height * 4u; - context.m_pDeviceContext->UpdateSubresource(texture.texture, level, &box, - data, rowPitch, depthPitch); -} - -void Renderer::TextureFree(int idx) { - Texture &texture = m_textures[idx]; - texture.texture->Release(); - texture.view->Release(); - texture.view = nullptr; - texture.allocated = false; - texture.texture = nullptr; +void Renderer::TextureFree(int idx) +{ + Texture &texture = m_textures[idx]; + texture.texture->Release(); + texture.view->Release(); + texture.view = nullptr; + texture.allocated = false; + texture.texture = nullptr; } void Renderer::TextureGetStats() {} -ID3D11ShaderResourceView *Renderer::TextureGetTexture(int idx) { - if (idx < 0 || idx > 0x1FF) { - return nullptr; - } +ID3D11ShaderResourceView *Renderer::TextureGetTexture(int idx) +{ + if (idx < 0 || idx > 0x1FF) + { + return nullptr; + } - const Texture &texture = m_textures[idx]; - if (!texture.allocated) { - return nullptr; - } + const Texture &texture = m_textures[idx]; + if (!texture.allocated) + { + return nullptr; + } - return texture.view; + return texture.view; } -void Renderer::TextureBind(int idx) { - int textureIndex = idx; - if (textureIndex == -1) { - textureIndex = defaultTextureIndex; - } +void Renderer::TextureBind(int idx) +{ + int textureIndex = idx; + if (textureIndex == -1) + { + textureIndex = defaultTextureIndex; + } - Renderer::Context &context = this->getContext(); + Renderer::Context &context = this->getContext(); - if (context.commandBuffer && context.commandBuffer->isActive) { - context.commandBuffer->BindTexture(textureIndex); - } + if (context.commandBuffer && context.commandBuffer->isActive) + { + context.commandBuffer->BindTexture(textureIndex); + } - context.boundTextureIndex = textureIndex; - ID3D11ShaderResourceView *const view = m_textures[textureIndex].view; - context.m_pDeviceContext->PSSetShaderResources(0, 1, &view); - this->UpdateTextureState(false); + context.boundTextureIndex = textureIndex; + ID3D11ShaderResourceView *const view = m_textures[textureIndex].view; + context.m_pDeviceContext->PSSetShaderResources(0, 1, &view); + this->UpdateTextureState(false); } -void Renderer::TextureBindVertex(int idx) { - int textureIndex = idx; - if (textureIndex == -1) { - textureIndex = defaultTextureIndex; - } +void Renderer::TextureBindVertex(int idx) +{ + int textureIndex = idx; + if (textureIndex == -1) + { + textureIndex = defaultTextureIndex; + } - Renderer::Context &context = this->getContext(); - context.boundTextureIndex = textureIndex; + Renderer::Context &context = this->getContext(); + context.boundTextureIndex = textureIndex; - ID3D11ShaderResourceView *const view = m_textures[textureIndex].view; - context.m_pDeviceContext->VSSetShaderResources(0, 1, &view); - this->UpdateTextureState(true); + ID3D11ShaderResourceView *const view = m_textures[textureIndex].view; + context.m_pDeviceContext->VSSetShaderResources(0, 1, &view); + this->UpdateTextureState(true); } -void Renderer::UpdateTextureState(bool vertexSampler) { - Renderer::Context &context = this->getContext(); - ID3D11SamplerState *sampler = this->GetManagedSamplerState(); +void Renderer::UpdateTextureState(bool vertexSampler) +{ + Renderer::Context &context = this->getContext(); + ID3D11SamplerState *sampler = this->GetManagedSamplerState(); - if (vertexSampler) { - context.m_pDeviceContext->VSSetSamplers(0, 1, &sampler); - } else { - context.m_pDeviceContext->PSSetSamplers(0, 1, &sampler); - } + if (vertexSampler) + { + context.m_pDeviceContext->VSSetSamplers(0, 1, &sampler); + } + else + { + context.m_pDeviceContext->PSSetSamplers(0, 1, &sampler); + } } -HRESULT Renderer::LoadTextureData(const char *szFilename, - D3DXIMAGE_INFO *pSrcInfo, int **ppDataOut) { - if (!szFilename || !pSrcInfo || !ppDataOut) { - return -1; - } +HRESULT Renderer::LoadTextureData(const char *szFilename, D3DXIMAGE_INFO *pSrcInfo, int **ppDataOut) +{ + if (!szFilename || !pSrcInfo || !ppDataOut) + { + return -1; + } - *ppDataOut = nullptr; + *ppDataOut = nullptr; - png_image image; - std::memset(&image, 0, sizeof(image)); - image.version = PNG_IMAGE_VERSION; - if (!png_image_begin_read_from_file(&image, szFilename)) { - return -1; - } + png_image image; + std::memset(&image, 0, sizeof(image)); + image.version = PNG_IMAGE_VERSION; + if (!png_image_begin_read_from_file(&image, szFilename)) + { + return -1; + } - if (image.width == 0 || image.height == 0) { + if (image.width == 0 || image.height == 0) + { + png_image_free(&image); + return -1; + } + + image.format = PNG_FORMAT_RGBA; + const png_alloc_size_t pixelCount = png_alloc_size_t(image.width) * png_alloc_size_t(image.height); + if (pixelCount == 0 || pixelCount > (std::numeric_limits::max)() / sizeof(int)) + { + png_image_free(&image); + return -1; + } + + int *output = new (std::nothrow) int[size_t(pixelCount)]; + + if (!output) + { + png_image_free(&image); + return -1; + } + + if (!png_image_finish_read(&image, nullptr, output, 0, nullptr)) + { + delete[] output; + png_image_free(&image); + return -1; + } + + *ppDataOut = output; + pSrcInfo->Width = image.width; + pSrcInfo->Height = image.height; png_image_free(&image); - return -1; - } - - image.format = PNG_FORMAT_RGBA; - const png_alloc_size_t pixelCount = - png_alloc_size_t(image.width) * png_alloc_size_t(image.height); - if (pixelCount == 0 || - pixelCount > (std::numeric_limits::max)() / sizeof(int)) { - png_image_free(&image); - return -1; - } - - int *output = new (std::nothrow) int[size_t(pixelCount)]; - - if (!output) { - png_image_free(&image); - return -1; - } - - if (!png_image_finish_read(&image, nullptr, output, 0, nullptr)) { - delete[] output; - png_image_free(&image); - return -1; - } - - *ppDataOut = output; - pSrcInfo->Width = image.width; - pSrcInfo->Height = image.height; - png_image_free(&image); - return 0; + return 0; } -HRESULT Renderer::LoadTextureData(BYTE *pbData, DWORD dwBytes, - D3DXIMAGE_INFO *pSrcInfo, int **ppDataOut) { - if (!pbData || dwBytes == 0 || !pSrcInfo || !ppDataOut) { - return -1; - } +HRESULT Renderer::LoadTextureData(BYTE *pbData, DWORD dwBytes, D3DXIMAGE_INFO *pSrcInfo, int **ppDataOut) +{ + if (!pbData || dwBytes == 0 || !pSrcInfo || !ppDataOut) + { + return -1; + } - *ppDataOut = nullptr; + *ppDataOut = nullptr; - png_image image; - std::memset(&image, 0, sizeof(image)); - image.version = PNG_IMAGE_VERSION; - if (!png_image_begin_read_from_memory(&image, pbData, dwBytes)) { - return -1; - } + png_image image; + std::memset(&image, 0, sizeof(image)); + image.version = PNG_IMAGE_VERSION; + if (!png_image_begin_read_from_memory(&image, pbData, dwBytes)) + { + return -1; + } - if (image.width == 0 || image.height == 0) { + if (image.width == 0 || image.height == 0) + { + png_image_free(&image); + return -1; + } + + image.format = PNG_FORMAT_RGBA; + const png_alloc_size_t pixelCount = png_alloc_size_t(image.width) * png_alloc_size_t(image.height); + if (pixelCount == 0 || pixelCount > (std::numeric_limits::max)() / sizeof(int)) + { + png_image_free(&image); + return -1; + } + + int *output = new (std::nothrow) int[size_t(pixelCount)]; + + if (!output) + { + png_image_free(&image); + return -1; + } + + if (!png_image_finish_read(&image, nullptr, output, 0, nullptr)) + { + delete[] output; + png_image_free(&image); + return -1; + } + + *ppDataOut = output; + pSrcInfo->Width = image.width; + pSrcInfo->Height = image.height; png_image_free(&image); - return -1; - } - - image.format = PNG_FORMAT_RGBA; - const png_alloc_size_t pixelCount = - png_alloc_size_t(image.width) * png_alloc_size_t(image.height); - if (pixelCount == 0 || - pixelCount > (std::numeric_limits::max)() / sizeof(int)) { - png_image_free(&image); - return -1; - } - - int *output = new (std::nothrow) int[size_t(pixelCount)]; - - if (!output) { - png_image_free(&image); - return -1; - } - - if (!png_image_finish_read(&image, nullptr, output, 0, nullptr)) { - delete[] output; - png_image_free(&image); - return -1; - } - - *ppDataOut = output; - pSrcInfo->Width = image.width; - pSrcInfo->Height = image.height; - png_image_free(&image); - return 0; + return 0; } -HRESULT Renderer::SaveTextureData(const char *szFilename, - D3DXIMAGE_INFO *pSrcInfo, int *ppDataOut) { - png_image image = {}; - image.version = PNG_IMAGE_VERSION; - image.width = pSrcInfo->Width; - image.height = pSrcInfo->Height; - image.format = PNG_FORMAT_RGBA; +HRESULT Renderer::SaveTextureData(const char *szFilename, D3DXIMAGE_INFO *pSrcInfo, int *ppDataOut) +{ + png_image image = {}; + image.version = PNG_IMAGE_VERSION; + image.width = pSrcInfo->Width; + image.height = pSrcInfo->Height; + image.format = PNG_FORMAT_RGBA; - png_image_write_to_file(&image, szFilename, 0, ppDataOut, 0, nullptr); - return 0; + png_image_write_to_file(&image, szFilename, 0, ppDataOut, 0, nullptr); + return 0; } -HRESULT Renderer::SaveTextureDataToMemory(void *pOutput, int outputCapacity, - int *outputLength, int width, - int height, int *ppDataIn) { - if (!pOutput || outputCapacity <= 0 || !outputLength || width <= 0 || - height <= 0 || !ppDataIn) { - return -1; - } +HRESULT Renderer::SaveTextureDataToMemory(void *pOutput, int outputCapacity, int *outputLength, int width, int height, int *ppDataIn) +{ + if (!pOutput || outputCapacity <= 0 || !outputLength || width <= 0 || height <= 0 || !ppDataIn) + { + return -1; + } - png_image image; - std::memset(&image, 0, sizeof(image)); - image.version = PNG_IMAGE_VERSION; - image.width = width; - image.height = height; - image.format = PNG_FORMAT_RGBA; + png_image image; + std::memset(&image, 0, sizeof(image)); + image.version = PNG_IMAGE_VERSION; + image.width = width; + image.height = height; + image.format = PNG_FORMAT_RGBA; - png_alloc_size_t memoryBytes = static_cast(outputCapacity); - if (!png_image_write_to_memory(&image, pOutput, &memoryBytes, 0, ppDataIn, 0, - nullptr)) { - *outputLength = 0; + png_alloc_size_t memoryBytes = static_cast(outputCapacity); + if (!png_image_write_to_memory(&image, pOutput, &memoryBytes, 0, ppDataIn, 0, nullptr)) + { + *outputLength = 0; + png_image_free(&image); + return -1; + } + + *outputLength = static_cast(memoryBytes); png_image_free(&image); - return -1; - } - - *outputLength = static_cast(memoryBytes); - png_image_free(&image); - return 0; + return 0; } diff --git a/Windows_Libs/Dev/Render/RendererVertex.cpp b/Windows_Libs/Dev/Render/RendererVertex.cpp index 27c79c8..9dba5c9 100644 --- a/Windows_Libs/Dev/Render/RendererVertex.cpp +++ b/Windows_Libs/Dev/Render/RendererVertex.cpp @@ -5,181 +5,169 @@ #include #include -D3D11_PRIMITIVE_TOPOLOGY* Renderer::m_Topologies = nullptr; +D3D11_PRIMITIVE_TOPOLOGY *Renderer::m_Topologies = nullptr; -void Renderer::DrawVertexBuffer( - C4JRender::ePrimitiveType PrimitiveType, - int count, - ID3D11Buffer* buffer, - C4JRender::eVertexType vType, - C4JRender::ePixelShaderType psType) +void Renderer::DrawVertexBuffer(C4JRender::ePrimitiveType PrimitiveType, int count, ID3D11Buffer *buffer, C4JRender::eVertexType vType, + C4JRender::ePixelShaderType psType) { - Renderer::Context& context = this->getContext(); - ID3D11DeviceContext* d3d11 = context.m_pDeviceContext; + Renderer::Context &context = this->getContext(); + ID3D11DeviceContext *d3d11 = context.m_pDeviceContext; - int drawCount = count; - bool indexed = false; - this->DrawVertexSetup(vType, psType, PrimitiveType, &drawCount, &indexed); - this->StateUpdate(); + int drawCount = count; + bool indexed = false; + this->DrawVertexSetup(vType, psType, PrimitiveType, &drawCount, &indexed); + this->StateUpdate(); - const UINT stride = vertexStrideTable[vType]; - const UINT offset = 0; - d3d11->IASetVertexBuffers(0, 1, &buffer, &stride, &offset); + const UINT stride = vertexStrideTable[vType]; + const UINT offset = 0; + d3d11->IASetVertexBuffers(0, 1, &buffer, &stride, &offset); - if (indexed) - { - d3d11->DrawIndexed(drawCount, 0, 0); - } - else - { - d3d11->Draw(count, 0); - } + if (indexed) + { + d3d11->DrawIndexed(drawCount, 0, 0); + } + else + { + d3d11->Draw(count, 0); + } } -void Renderer::DrawVertexSetup( - C4JRender::eVertexType vType, - C4JRender::ePixelShaderType psType, - C4JRender::ePrimitiveType PrimitiveType, - int* count, - bool* indexed) +void Renderer::DrawVertexSetup(C4JRender::eVertexType vType, C4JRender::ePixelShaderType psType, C4JRender::ePrimitiveType PrimitiveType, int *count, + bool *indexed) { - Renderer::Context& context = this->getContext(); - ID3D11DeviceContext* d3d11 = context.m_pDeviceContext; + Renderer::Context &context = this->getContext(); + ID3D11DeviceContext *d3d11 = context.m_pDeviceContext; - C4JRender::eVertexType effectiveVertexType = vType; - if (effectiveVertexType == C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1 && context.lightingEnabled) - { - effectiveVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT; - } + C4JRender::eVertexType effectiveVertexType = vType; + if (effectiveVertexType == C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1 && context.lightingEnabled) + { + effectiveVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT; + } - if (effectiveVertexType != activeVertexType) - { - d3d11->VSSetShader(vertexShaderTable[effectiveVertexType], nullptr, 0); - d3d11->IASetInputLayout(inputLayoutTable[effectiveVertexType]); - activeVertexType = effectiveVertexType; - } + if (effectiveVertexType != activeVertexType) + { + d3d11->VSSetShader(vertexShaderTable[effectiveVertexType], nullptr, 0); + d3d11->IASetInputLayout(inputLayoutTable[effectiveVertexType]); + activeVertexType = effectiveVertexType; + } - if (psType != activePixelType) - { - d3d11->PSSetShader(pixelShaderTable[psType], nullptr, 0); - activePixelType = psType; - } + if (psType != activePixelType) + { + d3d11->PSSetShader(pixelShaderTable[psType], nullptr, 0); + activePixelType = psType; + } - D3D11_MAPPED_SUBRESOURCE mapped = {}; + D3D11_MAPPED_SUBRESOURCE mapped = {}; - if (context.matrixDirty[0]) - { - d3d11->Map(context.cbMatrix0, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); - std::memcpy(mapped.pData, this->MatrixGet(0), sizeof(DirectX::XMMATRIX)); - d3d11->Unmap(context.cbMatrix0, 0); - context.matrixDirty[0] = false; - } + if (context.matrixDirty[0]) + { + d3d11->Map(context.cbMatrix0, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + std::memcpy(mapped.pData, this->MatrixGet(0), sizeof(DirectX::XMMATRIX)); + d3d11->Unmap(context.cbMatrix0, 0); + context.matrixDirty[0] = false; + } - if (context.matrixDirty[1]) - { - d3d11->Map(context.cbMatrix2, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); - std::memcpy(mapped.pData, this->MatrixGet(1), sizeof(DirectX::XMMATRIX)); - d3d11->Unmap(context.cbMatrix2, 0); - context.matrixDirty[1] = false; - } + if (context.matrixDirty[1]) + { + d3d11->Map(context.cbMatrix2, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + std::memcpy(mapped.pData, this->MatrixGet(1), sizeof(DirectX::XMMATRIX)); + d3d11->Unmap(context.cbMatrix2, 0); + context.matrixDirty[1] = false; + } - if (context.matrixDirty[2]) - { - d3d11->Map(context.cbMatrix3, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); - std::memcpy(mapped.pData, this->MatrixGet(2), sizeof(DirectX::XMMATRIX)); - d3d11->Unmap(context.cbMatrix3, 0); - context.matrixDirty[2] = false; - } + if (context.matrixDirty[2]) + { + d3d11->Map(context.cbMatrix3, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + std::memcpy(mapped.pData, this->MatrixGet(2), sizeof(DirectX::XMMATRIX)); + d3d11->Unmap(context.cbMatrix3, 0); + context.matrixDirty[2] = false; + } - this->UpdateFogState(); - this->UpdateViewportState(); - this->UpdateLightingState(); - this->UpdateTexGenState(); + this->UpdateFogState(); + this->UpdateViewportState(); + this->UpdateLightingState(); + this->UpdateTexGenState(); - d3d11->IASetPrimitiveTopology(m_Topologies[PrimitiveType]); + d3d11->IASetPrimitiveTopology(m_Topologies[PrimitiveType]); - if (PrimitiveType == C4JRender::PRIMITIVE_TYPE_QUAD_LIST) - { - d3d11->IASetIndexBuffer(quadIndexBuffer, DXGI_FORMAT_R16_UINT, 0); - *count = (*count * 6) / 4; - *indexed = true; - return; - } + if (PrimitiveType == C4JRender::PRIMITIVE_TYPE_QUAD_LIST) + { + d3d11->IASetIndexBuffer(quadIndexBuffer, DXGI_FORMAT_R16_UINT, 0); + *count = (*count * 6) / 4; + *indexed = true; + return; + } - if (PrimitiveType == C4JRender::PRIMITIVE_TYPE_TRIANGLE_FAN) - { - d3d11->IASetIndexBuffer(fanIndexBuffer, DXGI_FORMAT_R16_UINT, 0); - *count = (*count - 2) * 3; - *indexed = true; - return; - } + if (PrimitiveType == C4JRender::PRIMITIVE_TYPE_TRIANGLE_FAN) + { + d3d11->IASetIndexBuffer(fanIndexBuffer, DXGI_FORMAT_R16_UINT, 0); + *count = (*count - 2) * 3; + *indexed = true; + return; + } - d3d11->IASetIndexBuffer(nullptr, DXGI_FORMAT_R16_UINT, 0); - *indexed = false; + d3d11->IASetIndexBuffer(nullptr, DXGI_FORMAT_R16_UINT, 0); + *indexed = false; } -void Renderer::DrawVertices( - C4JRender::ePrimitiveType PrimitiveType, - int count, - void* vertices, - C4JRender::eVertexType vType, - C4JRender::ePixelShaderType psType) +void Renderer::DrawVertices(C4JRender::ePrimitiveType PrimitiveType, int count, void *vertices, C4JRender::eVertexType vType, + C4JRender::ePixelShaderType psType) { - Renderer::Context& context = this->getContext(); - ID3D11DeviceContext* d3d11 = context.m_pDeviceContext; - Renderer::CommandBuffer* commandBuffer = context.commandBuffer; + Renderer::Context &context = this->getContext(); + ID3D11DeviceContext *d3d11 = context.m_pDeviceContext; + Renderer::CommandBuffer *commandBuffer = context.commandBuffer; - if (commandBuffer != nullptr) - { - C4JRender::eVertexType effectiveVertexType = vType; - if (effectiveVertexType == C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1 && context.lightingEnabled) - { - effectiveVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT; - } + if (commandBuffer != nullptr) + { + C4JRender::eVertexType effectiveVertexType = vType; + if (effectiveVertexType == C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1 && context.lightingEnabled) + { + effectiveVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT; + } - context.recordingPrimitiveType = PrimitiveType; - context.recordingVertexType = effectiveVertexType; - const UINT stride = vertexStrideTable[effectiveVertexType]; - commandBuffer->AddVertices(stride, static_cast(count), vertices, context); - return; - } + context.recordingPrimitiveType = PrimitiveType; + context.recordingVertexType = effectiveVertexType; + const UINT stride = vertexStrideTable[effectiveVertexType]; + commandBuffer->AddVertices(stride, static_cast(count), vertices, context); + return; + } - int drawCount = count; - bool indexed = false; - this->DrawVertexSetup(vType, psType, PrimitiveType, &drawCount, &indexed); + int drawCount = count; + bool indexed = false; + this->DrawVertexSetup(vType, psType, PrimitiveType, &drawCount, &indexed); - const UINT stride = vertexStrideTable[vType]; - const UINT copySize = stride * static_cast(count); - if (context.dynamicVertexOffset + copySize > 0x100000u) - { - context.dynamicVertexOffset = 0; - } + const UINT stride = vertexStrideTable[vType]; + const UINT copySize = stride * static_cast(count); + if (context.dynamicVertexOffset + copySize > 0x100000u) + { + context.dynamicVertexOffset = 0; + } - D3D11_MAPPED_SUBRESOURCE mapped = {}; - const D3D11_MAP mapType = context.dynamicVertexOffset == 0 ? D3D11_MAP_WRITE_DISCARD : D3D11_MAP_WRITE_NO_OVERWRITE; - const HRESULT hr = d3d11->Map(context.dynamicVertexBuffer, 0, mapType, 0, &mapped); - if (hr != 0) - { - std::printf("ERROR: 0x%x\n", static_cast(hr)); - } + D3D11_MAPPED_SUBRESOURCE mapped = {}; + const D3D11_MAP mapType = context.dynamicVertexOffset == 0 ? D3D11_MAP_WRITE_DISCARD : D3D11_MAP_WRITE_NO_OVERWRITE; + const HRESULT hr = d3d11->Map(context.dynamicVertexBuffer, 0, mapType, 0, &mapped); + if (hr != 0) + { + std::printf("ERROR: 0x%x\n", static_cast(hr)); + } - std::memcpy(static_cast(mapped.pData) + context.dynamicVertexOffset, vertices, copySize); - d3d11->Unmap(context.dynamicVertexBuffer, 0); + std::memcpy(static_cast(mapped.pData) + context.dynamicVertexOffset, vertices, copySize); + d3d11->Unmap(context.dynamicVertexBuffer, 0); - this->StateUpdate(); + this->StateUpdate(); - ID3D11Buffer* dynamicBuffer = context.dynamicVertexBuffer; - const UINT vertexOffset = context.dynamicVertexOffset; - d3d11->IASetVertexBuffers(0, 1, &dynamicBuffer, &stride, &vertexOffset); + ID3D11Buffer *dynamicBuffer = context.dynamicVertexBuffer; + const UINT vertexOffset = context.dynamicVertexOffset; + d3d11->IASetVertexBuffers(0, 1, &dynamicBuffer, &stride, &vertexOffset); - if (indexed) - { - d3d11->DrawIndexed(drawCount, 0, 0); - } - else - { - d3d11->Draw(count, 0); - } + if (indexed) + { + d3d11->DrawIndexed(drawCount, 0, 0); + } + else + { + d3d11->Draw(count, 0); + } - context.dynamicVertexOffset += copySize; + context.dynamicVertexOffset += copySize; } diff --git a/Windows_Libs/Dev/Storage/4J_Storage.cpp b/Windows_Libs/Dev/Storage/4J_Storage.cpp index 16cab7d..04d330a 100644 --- a/Windows_Libs/Dev/Storage/4J_Storage.cpp +++ b/Windows_Libs/Dev/Storage/4J_Storage.cpp @@ -5,280 +5,284 @@ C4JStorage StorageManager; XMARKETPLACE_CONTENTOFFER_INFO InternalContentOfferInfo; -C4JStorage::C4JStorage() -{ - -} +C4JStorage::C4JStorage() {} void C4JStorage::Tick(void) { - InternalStorageManager.Tick(); + InternalStorageManager.Tick(); } -C4JStorage::EMessageResult C4JStorage::RequestMessageBox(UINT uiTitle, UINT uiText, UINT* uiOptionA, UINT uiOptionC, DWORD dwPad, - int(*Func)(LPVOID, int, const C4JStorage::EMessageResult), LPVOID lpParam, C4JStringTable* pStringTable, WCHAR* pwchFormatString, DWORD dwFocusButton) +C4JStorage::EMessageResult C4JStorage::RequestMessageBox(UINT uiTitle, UINT uiText, UINT *uiOptionA, UINT uiOptionC, DWORD dwPad, + int (*Func)(LPVOID, int, const C4JStorage::EMessageResult), LPVOID lpParam, + C4JStringTable *pStringTable, WCHAR *pwchFormatString, DWORD dwFocusButton) { - return EMessage_Undefined; + return EMessage_Undefined; } C4JStorage::EMessageResult C4JStorage::GetMessageBoxResult() { - return EMessage_Undefined; + return EMessage_Undefined; } -bool C4JStorage::SetSaveDevice(int(*Func)(LPVOID, const bool), LPVOID lpParam, bool bForceResetOfSaveDevice) +bool C4JStorage::SetSaveDevice(int (*Func)(LPVOID, const bool), LPVOID lpParam, bool bForceResetOfSaveDevice) { - return true; + return true; } -void C4JStorage::Init(unsigned int uiSaveVersion, LPCWSTR pwchDefaultSaveName, char* pszSavePackName, int iMinimumSaveSize, int(*Func)(LPVOID, const ESavingMessage, int), LPVOID lpParam, LPCSTR szGroupID) +void C4JStorage::Init(unsigned int uiSaveVersion, LPCWSTR pwchDefaultSaveName, char *pszSavePackName, int iMinimumSaveSize, + int (*Func)(LPVOID, const ESavingMessage, int), LPVOID lpParam, LPCSTR szGroupID) { - InternalStorageManager.Init(Func, lpParam, szGroupID); + InternalStorageManager.Init(Func, lpParam, szGroupID); } void C4JStorage::ResetSaveData() { - InternalStorageManager.m_SaveGame.ResetSaveData(); + InternalStorageManager.m_SaveGame.ResetSaveData(); } void C4JStorage::SetDefaultSaveNameForKeyboardDisplay(LPCWSTR pwchDefaultSaveName) { - ; + ; } void C4JStorage::SetSaveTitle(LPCWSTR pwchDefaultSaveName) { - InternalStorageManager.m_SaveGame.SetSaveTitle(pwchDefaultSaveName); + InternalStorageManager.m_SaveGame.SetSaveTitle(pwchDefaultSaveName); } -bool C4JStorage::GetSaveUniqueNumber(INT* piVal) +bool C4JStorage::GetSaveUniqueNumber(INT *piVal) { - return InternalStorageManager.m_SaveGame.GetSaveUniqueNumber(piVal); + return InternalStorageManager.m_SaveGame.GetSaveUniqueNumber(piVal); } -bool C4JStorage::GetSaveUniqueFilename(char* pszName) +bool C4JStorage::GetSaveUniqueFilename(char *pszName) { - return InternalStorageManager.m_SaveGame.GetSaveUniqueFilename(pszName); + return InternalStorageManager.m_SaveGame.GetSaveUniqueFilename(pszName); } -void C4JStorage::SetSaveUniqueFilename(char* szFilename) +void C4JStorage::SetSaveUniqueFilename(char *szFilename) { - InternalStorageManager.m_SaveGame.SetSaveUniqueFilename(szFilename); + InternalStorageManager.m_SaveGame.SetSaveUniqueFilename(szFilename); } -void C4JStorage::SetState(ESaveGameControlState eControlState, int(*Func)(LPVOID, const bool), LPVOID lpParam) +void C4JStorage::SetState(ESaveGameControlState eControlState, int (*Func)(LPVOID, const bool), LPVOID lpParam) { - ; + ; } void C4JStorage::SetSaveDisabled(bool bDisable) { - InternalStorageManager.m_SaveGame.SetSaveDisabled(bDisable); + InternalStorageManager.m_SaveGame.SetSaveDisabled(bDisable); } bool C4JStorage::GetSaveDisabled(void) { - return InternalStorageManager.m_SaveGame.GetSaveDisabled(); + return InternalStorageManager.m_SaveGame.GetSaveDisabled(); } unsigned int C4JStorage::GetSaveSize() { - return InternalStorageManager.m_SaveGame.GetSaveSize(); + return InternalStorageManager.m_SaveGame.GetSaveSize(); } -void C4JStorage::GetSaveData(void* pvData, unsigned int* puiBytes) +void C4JStorage::GetSaveData(void *pvData, unsigned int *puiBytes) { - InternalStorageManager.m_SaveGame.GetSaveData(pvData, puiBytes); + InternalStorageManager.m_SaveGame.GetSaveData(pvData, puiBytes); } PVOID C4JStorage::AllocateSaveData(unsigned int uiBytes) { - return InternalStorageManager.m_SaveGame.AllocateSaveData(uiBytes); + return InternalStorageManager.m_SaveGame.AllocateSaveData(uiBytes); } void C4JStorage::SetSaveImages(PBYTE pbThumbnail, DWORD dwThumbnailBytes, PBYTE pbImage, DWORD dwImageBytes, PBYTE pbTextData, DWORD dwTextDataBytes) { - InternalStorageManager.m_SaveGame.SetSaveImages(pbThumbnail, dwThumbnailBytes, pbImage, dwImageBytes, pbTextData, dwTextDataBytes); + InternalStorageManager.m_SaveGame.SetSaveImages(pbThumbnail, dwThumbnailBytes, pbImage, dwImageBytes, pbTextData, dwTextDataBytes); } -C4JStorage::ESaveGameState C4JStorage::SaveSaveData(int(*Func)(LPVOID, const bool), LPVOID lpParam) +C4JStorage::ESaveGameState C4JStorage::SaveSaveData(int (*Func)(LPVOID, const bool), LPVOID lpParam) { - return InternalStorageManager.m_SaveGame.SaveSaveData(Func, lpParam); + return InternalStorageManager.m_SaveGame.SaveSaveData(Func, lpParam); } -void C4JStorage::CopySaveDataToNewSave(PBYTE pbThumbnail, DWORD cbThumbnail, WCHAR* wchNewName, int (*Func)(LPVOID lpParam, bool), LPVOID lpParam) +void C4JStorage::CopySaveDataToNewSave(PBYTE pbThumbnail, DWORD cbThumbnail, WCHAR *wchNewName, int (*Func)(LPVOID lpParam, bool), LPVOID lpParam) { - ; + ; } void C4JStorage::SetSaveDeviceSelected(unsigned int uiPad, bool bSelected) { - ; + ; } bool C4JStorage::GetSaveDeviceSelected(unsigned int iPad) { - return true; + return true; } -C4JStorage::ESaveGameState C4JStorage::DoesSaveExist(bool* pbExists) +C4JStorage::ESaveGameState C4JStorage::DoesSaveExist(bool *pbExists) { - *pbExists = true; - return ESaveGame_Idle; + *pbExists = true; + return ESaveGame_Idle; } bool C4JStorage::EnoughSpaceForAMinSaveGame() { - return true; + return true; } void C4JStorage::SetSaveMessageVPosition(float fY) { - ; + ; } -C4JStorage::ESaveGameState C4JStorage::GetSavesInfo(int iPad, int (*Func)(LPVOID lpParam, SAVE_DETAILS* pSaveDetails, const bool), LPVOID lpParam, char* pszSavePackName) +C4JStorage::ESaveGameState C4JStorage::GetSavesInfo(int iPad, int (*Func)(LPVOID lpParam, SAVE_DETAILS *pSaveDetails, const bool), LPVOID lpParam, + char *pszSavePackName) { - return InternalStorageManager.m_SaveGame.GetSavesInfo(iPad, Func, lpParam, pszSavePackName); + return InternalStorageManager.m_SaveGame.GetSavesInfo(iPad, Func, lpParam, pszSavePackName); } PSAVE_DETAILS C4JStorage::ReturnSavesInfo() { - return InternalStorageManager.m_SaveGame.ReturnSavesInfo(); + return InternalStorageManager.m_SaveGame.ReturnSavesInfo(); } void C4JStorage::ClearSavesInfo() { - InternalStorageManager.m_SaveGame.ClearSavesInfo(); + InternalStorageManager.m_SaveGame.ClearSavesInfo(); } -C4JStorage::ESaveGameState C4JStorage::LoadSaveDataThumbnail(PSAVE_INFO pSaveInfo, int(*Func)(LPVOID lpParam, PBYTE pbThumbnail, DWORD dwThumbnailBytes), LPVOID lpParam) +C4JStorage::ESaveGameState C4JStorage::LoadSaveDataThumbnail(PSAVE_INFO pSaveInfo, + int (*Func)(LPVOID lpParam, PBYTE pbThumbnail, DWORD dwThumbnailBytes), LPVOID lpParam) { - return InternalStorageManager.m_SaveGame.LoadSaveDataThumbnail(pSaveInfo, Func, lpParam); + return InternalStorageManager.m_SaveGame.LoadSaveDataThumbnail(pSaveInfo, Func, lpParam); } -void C4JStorage::GetSaveCacheFileInfo(DWORD dwFile, XCONTENT_DATA& xContentData) +void C4JStorage::GetSaveCacheFileInfo(DWORD dwFile, XCONTENT_DATA &xContentData) { - ; + ; } -void C4JStorage::GetSaveCacheFileInfo(DWORD dwFile, PBYTE* ppbImageData, DWORD* pdwImageBytes) +void C4JStorage::GetSaveCacheFileInfo(DWORD dwFile, PBYTE *ppbImageData, DWORD *pdwImageBytes) { - ; + ; } -C4JStorage::ESaveGameState C4JStorage::LoadSaveData(PSAVE_INFO pSaveInfo, int(*Func)(LPVOID lpParam, const bool, const bool), LPVOID lpParam) +C4JStorage::ESaveGameState C4JStorage::LoadSaveData(PSAVE_INFO pSaveInfo, int (*Func)(LPVOID lpParam, const bool, const bool), LPVOID lpParam) { - return InternalStorageManager.m_SaveGame.LoadSaveData(pSaveInfo, Func, lpParam); + return InternalStorageManager.m_SaveGame.LoadSaveData(pSaveInfo, Func, lpParam); } -C4JStorage::ESaveGameState C4JStorage::DeleteSaveData(PSAVE_INFO pSaveInfo, int(*Func)(LPVOID lpParam, const bool), LPVOID lpParam) +C4JStorage::ESaveGameState C4JStorage::DeleteSaveData(PSAVE_INFO pSaveInfo, int (*Func)(LPVOID lpParam, const bool), LPVOID lpParam) { - return InternalStorageManager.m_SaveGame.DeleteSaveData(pSaveInfo, Func, lpParam); + return InternalStorageManager.m_SaveGame.DeleteSaveData(pSaveInfo, Func, lpParam); } -void C4JStorage::RegisterMarketplaceCountsCallback(int (*Func)(LPVOID lpParam, C4JStorage::DLC_TMS_DETAILS*, int), LPVOID lpParam) +void C4JStorage::RegisterMarketplaceCountsCallback(int (*Func)(LPVOID lpParam, C4JStorage::DLC_TMS_DETAILS *, int), LPVOID lpParam) { - ; + ; } -void C4JStorage::SetDLCPackageRoot(char* pszDLCRoot) +void C4JStorage::SetDLCPackageRoot(char *pszDLCRoot) { - InternalStorageManager.m_DLC.SetPackageRoot(pszDLCRoot); + InternalStorageManager.m_DLC.SetPackageRoot(pszDLCRoot); } -C4JStorage::EDLCStatus C4JStorage::GetDLCOffers(int iPad, int(*Func)(LPVOID, int, DWORD, int), LPVOID lpParam, DWORD dwOfferTypesBitmask) +C4JStorage::EDLCStatus C4JStorage::GetDLCOffers(int iPad, int (*Func)(LPVOID, int, DWORD, int), LPVOID lpParam, DWORD dwOfferTypesBitmask) { - return EDLC_Idle; + return EDLC_Idle; } DWORD C4JStorage::CancelGetDLCOffers() { - return 0; + return 0; } void C4JStorage::ClearDLCOffers() { - ; + ; } -XMARKETPLACE_CONTENTOFFER_INFO& C4JStorage::GetOffer(DWORD dw) +XMARKETPLACE_CONTENTOFFER_INFO &C4JStorage::GetOffer(DWORD dw) { - return InternalContentOfferInfo; + return InternalContentOfferInfo; } int C4JStorage::GetOfferCount() { - return 0; + return 0; } -DWORD C4JStorage::InstallOffer(int iOfferIDC, __uint64* ullOfferIDA, int(*Func)(LPVOID, int, int), LPVOID lpParam, bool bTrial) +DWORD C4JStorage::InstallOffer(int iOfferIDC, __uint64 *ullOfferIDA, int (*Func)(LPVOID, int, int), LPVOID lpParam, bool bTrial) { - return 0; + return 0; } DWORD C4JStorage::GetAvailableDLCCount(int iPad) { - return InternalStorageManager.m_DLC.GetAvailableDLCCount(iPad); + return InternalStorageManager.m_DLC.GetAvailableDLCCount(iPad); } -C4JStorage::EDLCStatus C4JStorage::GetInstalledDLC(int iPad, int(*Func)(LPVOID, int, int), LPVOID lpParam) +C4JStorage::EDLCStatus C4JStorage::GetInstalledDLC(int iPad, int (*Func)(LPVOID, int, int), LPVOID lpParam) { - return InternalStorageManager.m_DLC.GetInstalledDLC(iPad, Func, lpParam); + return InternalStorageManager.m_DLC.GetInstalledDLC(iPad, Func, lpParam); } -XCONTENT_DATA& C4JStorage::GetDLC(DWORD dw) +XCONTENT_DATA &C4JStorage::GetDLC(DWORD dw) { - return InternalStorageManager.m_DLC.GetDLC(dw); + return InternalStorageManager.m_DLC.GetDLC(dw); } -DWORD C4JStorage::MountInstalledDLC(int iPad, DWORD dwDLC, int(*Func)(LPVOID, int, DWORD, DWORD), LPVOID lpParam, LPCSTR szMountDrive) +DWORD C4JStorage::MountInstalledDLC(int iPad, DWORD dwDLC, int (*Func)(LPVOID, int, DWORD, DWORD), LPVOID lpParam, LPCSTR szMountDrive) { - return InternalStorageManager.m_DLC.MountInstalledDLC(iPad, dwDLC, Func, lpParam, szMountDrive); + return InternalStorageManager.m_DLC.MountInstalledDLC(iPad, dwDLC, Func, lpParam, szMountDrive); } DWORD C4JStorage::UnmountInstalledDLC(LPCSTR szMountDrive) { - return InternalStorageManager.m_DLC.UnmountInstalledDLC(szMountDrive); + return InternalStorageManager.m_DLC.UnmountInstalledDLC(szMountDrive); } -void C4JStorage::GetMountedDLCFileList(const char* szMountDrive, std::vector& fileList) +void C4JStorage::GetMountedDLCFileList(const char *szMountDrive, std::vector &fileList) { - InternalStorageManager.m_DLC.GetMountedDLCFileList(szMountDrive, fileList); + InternalStorageManager.m_DLC.GetMountedDLCFileList(szMountDrive, fileList); } std::string C4JStorage::GetMountedPath(std::string szMount) { - return InternalStorageManager.m_DLC.GetMountedPath(szMount); + return InternalStorageManager.m_DLC.GetMountedPath(szMount); } C4JStorage::ETMSStatus C4JStorage::ReadTMSFile(int iQuadrant, eGlobalStorage eStorageFacility, C4JStorage::eTMS_FileType eFileType, - WCHAR* pwchFilename, BYTE** ppBuffer, DWORD* pdwBufferSize, int(*Func)(LPVOID, WCHAR*, int, bool, int), LPVOID lpParam, int iAction) + WCHAR *pwchFilename, BYTE **ppBuffer, DWORD *pdwBufferSize, + int (*Func)(LPVOID, WCHAR *, int, bool, int), LPVOID lpParam, int iAction) { - return ETMSStatus_Idle; + return ETMSStatus_Idle; } -bool C4JStorage::WriteTMSFile(int iQuadrant, eGlobalStorage eStorageFacility, WCHAR* pwchFilename, BYTE* pBuffer, DWORD dwBufferSize) +bool C4JStorage::WriteTMSFile(int iQuadrant, eGlobalStorage eStorageFacility, WCHAR *pwchFilename, BYTE *pBuffer, DWORD dwBufferSize) { - return true; + return true; } -bool C4JStorage::DeleteTMSFile(int iQuadrant, eGlobalStorage eStorageFacility, WCHAR* pwchFilename) +bool C4JStorage::DeleteTMSFile(int iQuadrant, eGlobalStorage eStorageFacility, WCHAR *pwchFilename) { - return true; + return true; } -void C4JStorage::StoreTMSPathName(WCHAR* pwchName) +void C4JStorage::StoreTMSPathName(WCHAR *pwchName) { - ; + ; } -C4JStorage::ETMSStatus C4JStorage::TMSPP_ReadFile(int iPad, C4JStorage::eGlobalStorage eStorageFacility, C4JStorage::eTMS_FILETYPEVAL eFileTypeVal, LPCSTR szFilename, int(*Func)(LPVOID, int, int, PTMSPP_FILEDATA, LPCSTR), LPVOID lpParam, int iUserData) +C4JStorage::ETMSStatus C4JStorage::TMSPP_ReadFile(int iPad, C4JStorage::eGlobalStorage eStorageFacility, C4JStorage::eTMS_FILETYPEVAL eFileTypeVal, + LPCSTR szFilename, int (*Func)(LPVOID, int, int, PTMSPP_FILEDATA, LPCSTR), LPVOID lpParam, + int iUserData) { - return ETMSStatus_Idle; + return ETMSStatus_Idle; } -unsigned int C4JStorage::CRC(unsigned char* buf, int len) +unsigned int C4JStorage::CRC(unsigned char *buf, int len) { - return 0; + return 0; } diff --git a/Windows_Libs/Dev/Storage/STO_DLC.cpp b/Windows_Libs/Dev/Storage/STO_DLC.cpp index da5f5bc..a26b2d3 100644 --- a/Windows_Libs/Dev/Storage/STO_DLC.cpp +++ b/Windows_Libs/Dev/Storage/STO_DLC.cpp @@ -2,33 +2,33 @@ #include "STO_DLC.h" #include "STO_Main.h" -XCONTENT_DATA& CDLC::GetDLC(DWORD dw) +XCONTENT_DATA &CDLC::GetDLC(DWORD dw) { - return m_vInstalledDLCs[dw]; + return m_vInstalledDLCs[dw]; } CDLC::CDLC(void) : m_vInstalledDLCs(), m_szMountPath(), m_vDLCDriveMappings() { - m_iHasNewInstalledDLCs = false; - dword0 = 0; - dwordC0 = 0; - m_iHasNewMountedDLCs = false; // @Patoke fix + m_iHasNewInstalledDLCs = false; + dword0 = 0; + dwordC0 = 0; + m_iHasNewMountedDLCs = false; // @Patoke fix - ZeroMemory(m_szDLCProductCode, sizeof(m_szDLCProductCode)); - ZeroMemory(m_szProductUpgradeKey, sizeof(m_szProductUpgradeKey)); + ZeroMemory(m_szDLCProductCode, sizeof(m_szDLCProductCode)); + ZeroMemory(m_szProductUpgradeKey, sizeof(m_szProductUpgradeKey)); } -C4JStorage::EDLCStatus CDLC::GetOffers(int iPad, int(*Func)(LPVOID, int, DWORD, int), LPVOID lpParam, DWORD dwOfferTypesBitmask) +C4JStorage::EDLCStatus CDLC::GetOffers(int iPad, int (*Func)(LPVOID, int, DWORD, int), LPVOID lpParam, DWORD dwOfferTypesBitmask) { - return C4JStorage::EDLC_NoOffers; + return C4JStorage::EDLC_NoOffers; } void CDLC::ClearOffers() { - ; + ; } -C4JStorage::EDLCStatus CDLC::GetInstalledDLC(int iPad, int(*Func)(LPVOID, int, int), LPVOID lpParam) +C4JStorage::EDLCStatus CDLC::GetInstalledDLC(int iPad, int (*Func)(LPVOID, int, int), LPVOID lpParam) { if (m_iHasNewInstalledDLCs) { @@ -37,7 +37,7 @@ C4JStorage::EDLCStatus CDLC::GetInstalledDLC(int iPad, int(*Func)(LPVOID, int, i m_pInstalledDLCFunc = Func; m_pInstalledDLCParam = lpParam; - m_iHasNewInstalledDLCs = true; + m_iHasNewInstalledDLCs = true; bool ret = false; DWORD atts = GetFileAttributesA("Windows64Media/DLC"); @@ -54,196 +54,195 @@ C4JStorage::EDLCStatus CDLC::GetInstalledDLC(int iPad, int(*Func)(LPVOID, int, i return C4JStorage::EDLC_Error; } - _WIN32_FIND_DATAA hFind; - HANDLE hFindFile; - if (ret) - { - hFindFile = FindFirstFileA("Windows64/DLC/*", &hFind); - } - else - { - hFindFile = FindFirstFileA("Windows64Media/DLC/*", &hFind); - } + _WIN32_FIND_DATAA hFind; + HANDLE hFindFile; + if (ret) + { + hFindFile = FindFirstFileA("Windows64/DLC/*", &hFind); + } + else + { + hFindFile = FindFirstFileA("Windows64Media/DLC/*", &hFind); + } - if (hFindFile != (HANDLE)-1LL) - { - do - { - atts = hFind.dwFileAttributes; + if (hFindFile != (HANDLE)-1LL) + { + do + { + atts = hFind.dwFileAttributes; - bool isArt = hFind.dwFileAttributes != -1 && (hFind.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); - if (isArt && hFind.cFileName[0] != '.') - { - XCONTENT_DATA data; + bool isArt = hFind.dwFileAttributes != -1 && (hFind.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); + if (isArt && hFind.cFileName[0] != '.') + { + XCONTENT_DATA data; - if (ret) - { - sprintf(data.szFileName, "Windows64/DLC/%s", hFind.cFileName); - } - else - { - sprintf(data.szFileName, "Windows64Media/DLC/%s", hFind.cFileName); - } + if (ret) + { + sprintf(data.szFileName, "Windows64/DLC/%s", hFind.cFileName); + } + else + { + sprintf(data.szFileName, "Windows64Media/DLC/%s", hFind.cFileName); + } - swprintf(data.szDisplayName, 256, L"%s", hFind.cFileName); - int displayNameLen = wcslen(data.szDisplayName); + swprintf(data.szDisplayName, 256, L"%s", hFind.cFileName); + int displayNameLen = wcslen(data.szDisplayName); - data.DeviceID = 0; - data.dwContentType = 0; + data.DeviceID = 0; + data.dwContentType = 0; - AddInstalled(&data); - } - } while (FindNextFileA(hFindFile, &hFind)); - FindClose(hFindFile); - } + AddInstalled(&data); + } + } while (FindNextFileA(hFindFile, &hFind)); + FindClose(hFindFile); + } - return C4JStorage::EDLC_Idle; + return C4JStorage::EDLC_Idle; } -DWORD CDLC::MountInstalledDLC(int iPad, DWORD dwDLC, int(*Func)(LPVOID, int, DWORD, DWORD), LPVOID lpParam, LPCSTR szMountDrive) +DWORD CDLC::MountInstalledDLC(int iPad, DWORD dwDLC, int (*Func)(LPVOID, int, DWORD, DWORD), LPVOID lpParam, LPCSTR szMountDrive) { - this->m_pMountedDLCFunc = Func; - this->m_pMountedDLCParam = lpParam; + this->m_pMountedDLCFunc = Func; + this->m_pMountedDLCParam = lpParam; - if (szMountDrive) - { - m_szMountPath = szMountDrive; - } - else - { - m_szMountPath = this->m_szPackageRoot; - } + if (szMountDrive) + { + m_szMountPath = szMountDrive; + } + else + { + m_szMountPath = this->m_szPackageRoot; + } - this->m_uiCurrentMappedDLC = dwDLC; + this->m_uiCurrentMappedDLC = dwDLC; - char* dlcdirPath = m_vInstalledDLCs[m_uiCurrentMappedDLC].szFileName; - m_vDLCDriveMappings.push_back(DriveMapping(dlcdirPath, m_szMountPath)); + char *dlcdirPath = m_vInstalledDLCs[m_uiCurrentMappedDLC].szFileName; + m_vDLCDriveMappings.push_back(DriveMapping(dlcdirPath, m_szMountPath)); - m_iHasNewMountedDLCs = true; + m_iHasNewMountedDLCs = true; - return 997; + return 997; } DWORD CDLC::UnmountInstalledDLC(LPCSTR szMountDrive) { - LPCSTR szDrive = nullptr; + LPCSTR szDrive = nullptr; - if (szMountDrive) - { - szDrive = szMountDrive; - } - else - { - szDrive = this->m_szPackageRoot; - } + if (szMountDrive) + { + szDrive = szMountDrive; + } + else + { + szDrive = this->m_szPackageRoot; + } - for (int i = 0; i < this->m_vDLCDriveMappings.size(); i++) - { - if (m_vDLCDriveMappings[i].m_szDirectoryPath == szDrive) - { - m_vDLCDriveMappings.erase(m_vDLCDriveMappings.begin() + i); + for (int i = 0; i < this->m_vDLCDriveMappings.size(); i++) + { + if (m_vDLCDriveMappings[i].m_szDirectoryPath == szDrive) + { + m_vDLCDriveMappings.erase(m_vDLCDriveMappings.begin() + i); - return 0; - } - } - return 0; + return 0; + } + } + return 0; } -void CDLC::GetMountedDLCFileList(const char* szMountDrive, std::vector& fileList) +void CDLC::GetMountedDLCFileList(const char *szMountDrive, std::vector &fileList) { - char* dlcdirPath = new char[256]; - sprintf(dlcdirPath, "%s/*", m_vInstalledDLCs[m_uiCurrentMappedDLC].szFileName); + char *dlcdirPath = new char[256]; + sprintf(dlcdirPath, "%s/*", m_vInstalledDLCs[m_uiCurrentMappedDLC].szFileName); - _WIN32_FIND_DATAA atts; - HANDLE hFind = FindFirstFileA(dlcdirPath, &atts); - if (hFind != (HANDLE)-1LL) - { - do - { - if (atts.dwFileAttributes == -1 || (atts.dwFileAttributes & 0x10) != 0x10) - { - char dir[256]; - sprintf(dir, "%s/%s", m_vInstalledDLCs[m_uiCurrentMappedDLC].szFileName, atts.cFileName); + _WIN32_FIND_DATAA atts; + HANDLE hFind = FindFirstFileA(dlcdirPath, &atts); + if (hFind != (HANDLE)-1LL) + { + do + { + if (atts.dwFileAttributes == -1 || (atts.dwFileAttributes & 0x10) != 0x10) + { + char dir[256]; + sprintf(dir, "%s/%s", m_vInstalledDLCs[m_uiCurrentMappedDLC].szFileName, atts.cFileName); - fileList.push_back(dir); - } - } while (FindNextFileA(hFind, &atts)); - FindClose(hFind); - } - delete[] dlcdirPath; + fileList.push_back(dir); + } + } while (FindNextFileA(hFind, &atts)); + FindClose(hFind); + } + delete[] dlcdirPath; } std::string CDLC::GetMountedPath(std::string szMount) { - for (int ch = 0; ch < szMount.size(); ++ch) - { - if (szMount[ch] == '/' || szMount[ch] == '\\') - { - return ""; - } + for (int ch = 0; ch < szMount.size(); ++ch) + { + if (szMount[ch] == '/' || szMount[ch] == '\\') + { + return ""; + } - if (szMount[ch] == ':') - { - std::string driveName = szMount.substr(0, ch); - for (int i = 0; i < m_vDLCDriveMappings.size(); ++i) - { - if (m_vDLCDriveMappings[i].m_szDirectoryPath == driveName) - { - std::string newPath = m_vDLCDriveMappings[i].m_szMountPath; + if (szMount[ch] == ':') + { + std::string driveName = szMount.substr(0, ch); + for (int i = 0; i < m_vDLCDriveMappings.size(); ++i) + { + if (m_vDLCDriveMappings[i].m_szDirectoryPath == driveName) + { + std::string newPath = m_vDLCDriveMappings[i].m_szMountPath; - newPath.append(szMount.substr(ch + 1, -1)); + newPath.append(szMount.substr(ch + 1, -1)); - return newPath; - } - } - break; - } - } + return newPath; + } + } + break; + } + } - return ""; + return ""; } - -void CDLC::SetDLCProductCode(const char* szProductCode) +void CDLC::SetDLCProductCode(const char *szProductCode) { - strcpy(m_szDLCProductCode, szProductCode); + strcpy(m_szDLCProductCode, szProductCode); } -void CDLC::SetProductUpgradeKey(const char* szProductCode) +void CDLC::SetProductUpgradeKey(const char *szProductCode) { - strcpy(m_szProductUpgradeKey, szProductCode); + strcpy(m_szProductUpgradeKey, szProductCode); } int CDLC::GetAvailableDLCCount(int iPad) { - return 0; + return 0; } -void CDLC::SetPackageRoot(char* pszDLCRoot) +void CDLC::SetPackageRoot(char *pszDLCRoot) { - strcpy(this->m_szPackageRoot, pszDLCRoot); + strcpy(this->m_szPackageRoot, pszDLCRoot); } void CDLC::Tick(void) { - if (m_iHasNewInstalledDLCs) - { - m_iHasNewInstalledDLCs = false; - m_pInstalledDLCFunc(m_pInstalledDLCParam, m_vInstalledDLCs.size(), 0); - } - if (m_iHasNewMountedDLCs) - { - m_iHasNewMountedDLCs = false; - m_pMountedDLCFunc(m_pMountedDLCParam, 0, 0, dword94); - } + if (m_iHasNewInstalledDLCs) + { + m_iHasNewInstalledDLCs = false; + m_pInstalledDLCFunc(m_pInstalledDLCParam, m_vInstalledDLCs.size(), 0); + } + if (m_iHasNewMountedDLCs) + { + m_iHasNewMountedDLCs = false; + m_pMountedDLCFunc(m_pMountedDLCParam, 0, 0, dword94); + } } -void CDLC::AddInstalled(XCONTENT_DATA* data) +void CDLC::AddInstalled(XCONTENT_DATA *data) { - m_vInstalledDLCs.push_back(*data); + m_vInstalledDLCs.push_back(*data); } DWORD CDLC::CancelOffers(void) { - return 0; + return 0; } diff --git a/Windows_Libs/Dev/Storage/STO_DLC.h b/Windows_Libs/Dev/Storage/STO_DLC.h index bf3286c..e565a11 100644 --- a/Windows_Libs/Dev/Storage/STO_DLC.h +++ b/Windows_Libs/Dev/Storage/STO_DLC.h @@ -4,54 +4,54 @@ class CDLC { public: + struct DriveMapping + { + DriveMapping(std::string szDirectoryPath, std::string szMountPath) : m_szDirectoryPath(szDirectoryPath), m_szMountPath(szMountPath) + { + ; + } - struct DriveMapping - { - DriveMapping(std::string szDirectoryPath, std::string szMountPath) : m_szDirectoryPath(szDirectoryPath), m_szMountPath(szMountPath) - { - ; - } + std::string m_szDirectoryPath; + std::string m_szMountPath; + }; - std::string m_szDirectoryPath; - std::string m_szMountPath; - }; + XCONTENT_DATA &GetDLC(DWORD dw); + CDLC(void); - XCONTENT_DATA& GetDLC(DWORD dw); - CDLC(void); + C4JStorage::EDLCStatus GetOffers(int iPad, int (*Func)(LPVOID, int, DWORD, int), LPVOID lpParam, + DWORD dwOfferTypesBitmask = XMARKETPLACE_OFFERING_TYPE_CONTENT); + void ClearOffers(); + C4JStorage::EDLCStatus GetInstalledDLC(int iPad, int (*Func)(LPVOID, int, int), LPVOID lpParam); + DWORD MountInstalledDLC(int iPad, DWORD dwDLC, int (*Func)(LPVOID, int, DWORD, DWORD), LPVOID lpParam, LPCSTR szMountDrive = NULL); + DWORD UnmountInstalledDLC(LPCSTR szMountDrive = NULL); + void GetMountedDLCFileList(const char *szMountDrive, std::vector &fileList); + std::string GetMountedPath(std::string szMount); - C4JStorage::EDLCStatus GetOffers(int iPad, int(*Func)(LPVOID, int, DWORD, int), LPVOID lpParam, DWORD dwOfferTypesBitmask = XMARKETPLACE_OFFERING_TYPE_CONTENT); - void ClearOffers(); - C4JStorage::EDLCStatus GetInstalledDLC(int iPad, int(*Func)(LPVOID, int, int), LPVOID lpParam); - DWORD MountInstalledDLC(int iPad, DWORD dwDLC, int(*Func)(LPVOID, int, DWORD, DWORD), LPVOID lpParam, LPCSTR szMountDrive = NULL); - DWORD UnmountInstalledDLC(LPCSTR szMountDrive = NULL); - void GetMountedDLCFileList(const char* szMountDrive, std::vector& fileList); - std::string GetMountedPath(std::string szMount); + void SetDLCProductCode(const char *szProductCode); + void SetProductUpgradeKey(const char *szProductCode); + int GetAvailableDLCCount(int iPad); + void SetPackageRoot(char *pszDLCRoot); - void SetDLCProductCode(const char* szProductCode); - void SetProductUpgradeKey(const char* szProductCode); - int GetAvailableDLCCount(int iPad); - void SetPackageRoot(char* pszDLCRoot); + void Tick(void); + void AddInstalled(XCONTENT_DATA *data); + DWORD CancelOffers(void); - void Tick(void); - void AddInstalled(XCONTENT_DATA* data); - DWORD CancelOffers(void); - - DWORD dword0; - int (*m_pInstalledDLCFunc)(LPVOID, int, int); - LPVOID m_pInstalledDLCParam; - BYTE gap18[16]; - int m_iHasNewInstalledDLCs; - std::vector m_vInstalledDLCs; - BYTE gap48[4]; - DWORD m_iHasNewMountedDLCs; - int (*m_pMountedDLCFunc)(LPVOID, int, DWORD, DWORD); - LPVOID m_pMountedDLCParam; - std::string m_szMountPath; - DWORD m_uiCurrentMappedDLC; - DWORD dword94; - char m_szPackageRoot[40]; - DWORD dwordC0; - std::vector m_vDLCDriveMappings; - char m_szDLCProductCode[16]; - char m_szProductUpgradeKey[60]; + DWORD dword0; + int (*m_pInstalledDLCFunc)(LPVOID, int, int); + LPVOID m_pInstalledDLCParam; + BYTE gap18[16]; + int m_iHasNewInstalledDLCs; + std::vector m_vInstalledDLCs; + BYTE gap48[4]; + DWORD m_iHasNewMountedDLCs; + int (*m_pMountedDLCFunc)(LPVOID, int, DWORD, DWORD); + LPVOID m_pMountedDLCParam; + std::string m_szMountPath; + DWORD m_uiCurrentMappedDLC; + DWORD dword94; + char m_szPackageRoot[40]; + DWORD dwordC0; + std::vector m_vDLCDriveMappings; + char m_szDLCProductCode[16]; + char m_szProductUpgradeKey[60]; }; \ No newline at end of file diff --git a/Windows_Libs/Dev/Storage/STO_Main.cpp b/Windows_Libs/Dev/Storage/STO_Main.cpp index 9943527..d1ab634 100644 --- a/Windows_Libs/Dev/Storage/STO_Main.cpp +++ b/Windows_Libs/Dev/Storage/STO_Main.cpp @@ -10,17 +10,14 @@ CStorage::CStorage(void) m_DLC = CDLC(); } -void CStorage::Init(int(*Func)(LPVOID, const C4JStorage::ESavingMessage, int), LPVOID lpParam, LPCSTR szGroupID) -{ +void CStorage::Init(int (*Func)(LPVOID, const C4JStorage::ESavingMessage, int), LPVOID lpParam, LPCSTR szGroupID) {} -} - -void CStorage::Tick(void) +void CStorage::Tick(void) { m_DLC.Tick(); } -unsigned int CStorage::CRC(unsigned char* buf, int len) +unsigned int CStorage::CRC(unsigned char *buf, int len) { return ~UpdateCRC(0xFFFFFFFF, buf, len); } @@ -46,7 +43,7 @@ void CStorage::MakeCRCTable(void) m_bHasCRCTable = true; } -unsigned int CStorage::UpdateCRC(unsigned int crc, unsigned __int8* buf, int len) +unsigned int CStorage::UpdateCRC(unsigned int crc, unsigned __int8 *buf, int len) { if (!m_bHasCRCTable) { @@ -55,13 +52,13 @@ unsigned int CStorage::UpdateCRC(unsigned int crc, unsigned __int8* buf, int len for (int c = 0; c < len; ++c) { - crc = (crc >> 8) ^ m_CRCTable[(unsigned __int8)(buf[c] ^ crc)]; + crc = (crc >> 8) ^ m_CRCTable[(unsigned __int8)(buf[c] ^ crc)]; } return crc; } -void CStorage::DebugPrintf(const char* szFormat, ...) +void CStorage::DebugPrintf(const char *szFormat, ...) { char buf[1024]; diff --git a/Windows_Libs/Dev/Storage/STO_Main.h b/Windows_Libs/Dev/Storage/STO_Main.h index dbf5714..080dcc3 100644 --- a/Windows_Libs/Dev/Storage/STO_Main.h +++ b/Windows_Libs/Dev/Storage/STO_Main.h @@ -1,26 +1,25 @@ #pragma once -#include "STO_SaveGame.h" #include "STO_DLC.h" +#include "STO_SaveGame.h" class CStorage { public: + CStorage(void); - CStorage(void); + void Init(int (*Func)(LPVOID, const C4JStorage::ESavingMessage, int), LPVOID lpParam, LPCSTR szGroupID); + void Tick(void); + unsigned int CRC(unsigned char *buf, int len); + void MakeCRCTable(void); + unsigned int UpdateCRC(unsigned int crc, unsigned __int8 *buf, int len); + void DebugPrintf(const char *szFormat, ...); - void Init(int(*Func)(LPVOID, const C4JStorage::ESavingMessage, int), LPVOID lpParam, LPCSTR szGroupID); - void Tick(void); - unsigned int CRC(unsigned char* buf, int len); - void MakeCRCTable(void); - unsigned int UpdateCRC(unsigned int crc, unsigned __int8* buf, int len); - void DebugPrintf(const char* szFormat, ...); - - BYTE gap0[8]; - CSaveGame m_SaveGame; - CDLC m_DLC; - BYTE gap278[0x10]; - DWORD m_CRCTable[256]; - bool m_bHasCRCTable; + BYTE gap0[8]; + CSaveGame m_SaveGame; + CDLC m_DLC; + BYTE gap278[0x10]; + DWORD m_CRCTable[256]; + bool m_bHasCRCTable; }; // Singleton diff --git a/Windows_Libs/Dev/Storage/STO_SaveGame.cpp b/Windows_Libs/Dev/Storage/STO_SaveGame.cpp index 65eacc5..f1c830a 100644 --- a/Windows_Libs/Dev/Storage/STO_SaveGame.cpp +++ b/Windows_Libs/Dev/Storage/STO_SaveGame.cpp @@ -3,43 +3,44 @@ CSaveGame::CSaveGame() { - m_pSaveData = nullptr; - m_uiSaveSize = 0; - m_bIsSafeDisabled = false; + m_pSaveData = nullptr; + m_uiSaveSize = 0; + m_bIsSafeDisabled = false; - ZeroMemory(m_szSaveUniqueName, sizeof(m_szSaveUniqueName)); - ZeroMemory(m_szSaveTitle, sizeof(m_szSaveTitle)); + ZeroMemory(m_szSaveUniqueName, sizeof(m_szSaveUniqueName)); + ZeroMemory(m_szSaveTitle, sizeof(m_szSaveTitle)); - m_pSaveDetails = nullptr; - m_bHasSaveDetails = false; + m_pSaveDetails = nullptr; + m_bHasSaveDetails = false; - GetCurrentDirectoryA(sizeof(m_szSaveUniqueName), m_szSaveUniqueName); + GetCurrentDirectoryA(sizeof(m_szSaveUniqueName), m_szSaveUniqueName); - char dirName[256]; - char curDir[256]; - GetCurrentDirectoryA(sizeof(dirName), dirName); - sprintf(curDir, "%s/Windows64/GameHDD/", dirName); - CreateDirectoryA(curDir, 0); + char dirName[256]; + char curDir[256]; + GetCurrentDirectoryA(sizeof(dirName), dirName); + sprintf(curDir, "%s/Windows64/GameHDD/", dirName); + CreateDirectoryA(curDir, 0); } void CSaveGame::SetSaveDisabled(bool bDisable) { - m_bIsSafeDisabled = bDisable; + m_bIsSafeDisabled = bDisable; } bool CSaveGame::GetSaveDisabled(void) { - return m_bIsSafeDisabled; + return m_bIsSafeDisabled; } void CSaveGame::ResetSaveData() { - free(m_pSaveData); - m_pSaveData = nullptr; - m_uiSaveSize = 0; + free(m_pSaveData); + m_pSaveData = nullptr; + m_uiSaveSize = 0; } -C4JStorage::ESaveGameState CSaveGame::GetSavesInfo(int iPad, int (*Func)(LPVOID lpParam, SAVE_DETAILS* pSaveDetails, const bool), LPVOID lpParam, char* pszSavePackName) +C4JStorage::ESaveGameState CSaveGame::GetSavesInfo(int iPad, int (*Func)(LPVOID lpParam, SAVE_DETAILS *pSaveDetails, const bool), LPVOID lpParam, + char *pszSavePackName) { WIN32_FIND_DATAA findFileData; WIN32_FILE_ATTRIBUTE_DATA fileInfoBuffer; @@ -90,16 +91,15 @@ C4JStorage::ESaveGameState CSaveGame::GetSavesInfo(int iPad, int (*Func)(LPVOID { do { - if ((findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 - && strcmp(findFileData.cFileName, ".") - && strcmp(findFileData.cFileName, "..")) + if ((findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 && strcmp(findFileData.cFileName, ".") && + strcmp(findFileData.cFileName, "..")) { strcpy_s(m_pSaveDetails->SaveInfoA[i].UTF8SaveFilename, findFileData.cFileName); strcpy_s(m_pSaveDetails->SaveInfoA[i].UTF8SaveTitle, findFileData.cFileName); - - char fileName[280]; + + char fileName[280]; sprintf(fileName, "%s\\Windows64\\GameHDD\\%s\\saveData.ms", dirName, findFileData.cFileName); - + GetFileAttributesExA(fileName, GetFileExInfoStandard, &fileInfoBuffer); m_pSaveDetails->SaveInfoA[i++].metaData.dataSize = fileInfoBuffer.nFileSizeLow; m_pSaveDetails->iSaveC++; @@ -142,24 +142,25 @@ void CSaveGame::ClearSavesInfo() } } -C4JStorage::ESaveGameState CSaveGame::LoadSaveDataThumbnail(PSAVE_INFO pSaveInfo, int(*Func)(LPVOID lpParam, PBYTE pbThumbnail, DWORD dwThumbnailBytes), LPVOID lpParam) +C4JStorage::ESaveGameState CSaveGame::LoadSaveDataThumbnail(PSAVE_INFO pSaveInfo, + int (*Func)(LPVOID lpParam, PBYTE pbThumbnail, DWORD dwThumbnailBytes), LPVOID lpParam) { - Func(lpParam, pSaveInfo->thumbnailData, pSaveInfo->metaData.thumbnailSize); + Func(lpParam, pSaveInfo->thumbnailData, pSaveInfo->metaData.thumbnailSize); return C4JStorage::ESaveGame_GetSaveThumbnail; } -C4JStorage::ESaveGameState CSaveGame::LoadSaveData(PSAVE_INFO pSaveInfo, int(*Func)(LPVOID lpParam, const bool, const bool), LPVOID lpParam) +C4JStorage::ESaveGameState CSaveGame::LoadSaveData(PSAVE_INFO pSaveInfo, int (*Func)(LPVOID lpParam, const bool, const bool), LPVOID lpParam) { SetSaveUniqueFilename(pSaveInfo->UTF8SaveFilename); - + if (m_pSaveData) { free(m_pSaveData); } - + m_pSaveData = malloc(pSaveInfo->metaData.dataSize); m_uiSaveSize = pSaveInfo->metaData.dataSize; - + char dirName[256]; char curDir[256]; char fileName[280]; @@ -167,7 +168,7 @@ C4JStorage::ESaveGameState CSaveGame::LoadSaveData(PSAVE_INFO pSaveInfo, int(*Fu sprintf(dirName, "%s/Windows64/GameHDD/%s", curDir, m_szSaveUniqueName); CreateDirectoryA(dirName, 0); sprintf(fileName, "%s/saveData.ms", dirName); - + HANDLE h = CreateFileA(fileName, GENERIC_READ, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); bool success = false; @@ -193,7 +194,7 @@ unsigned int CSaveGame::GetSaveSize() return m_uiSaveSize; } -void CSaveGame::GetSaveData(void* pvData, unsigned int* puiBytes) +void CSaveGame::GetSaveData(void *pvData, unsigned int *puiBytes) { if (pvData) { @@ -206,12 +207,12 @@ void CSaveGame::GetSaveData(void* pvData, unsigned int* puiBytes) } } -bool CSaveGame::GetSaveUniqueNumber(INT* piVal) +bool CSaveGame::GetSaveUniqueNumber(INT *piVal) { return false; } -bool CSaveGame::GetSaveUniqueFilename(char* pszName) +bool CSaveGame::GetSaveUniqueFilename(char *pszName) { return false; } @@ -225,7 +226,7 @@ void CSaveGame::SetSaveTitle(LPCWSTR pwchDefaultSaveName) PVOID CSaveGame::AllocateSaveData(unsigned int uiBytes) { free(m_pSaveData); - + m_pSaveData = malloc(uiBytes); if (m_pSaveData) { @@ -240,7 +241,7 @@ void CSaveGame::SetSaveImages(PBYTE pbThumbnail, DWORD dwThumbnailBytes, PBYTE p ; } -C4JStorage::ESaveGameState CSaveGame::SaveSaveData(int(*Func)(LPVOID, const bool), LPVOID lpParam) +C4JStorage::ESaveGameState CSaveGame::SaveSaveData(int (*Func)(LPVOID, const bool), LPVOID lpParam) { char dirName[256]; char curDir[256]; @@ -255,7 +256,7 @@ C4JStorage::ESaveGameState CSaveGame::SaveSaveData(int(*Func)(LPVOID, const bool DWORD bytesWritten = 0; BOOL res = WriteFile(h, m_pSaveData, m_uiSaveSize, &bytesWritten, 0); _ASSERT(res && bytesWritten == m_uiSaveSize); - + CloseHandle(h); Func(lpParam, true); @@ -263,28 +264,21 @@ C4JStorage::ESaveGameState CSaveGame::SaveSaveData(int(*Func)(LPVOID, const bool return C4JStorage::ESaveGame_Idle; } -C4JStorage::ESaveGameState CSaveGame::DeleteSaveData(PSAVE_INFO pSaveInfo, int(*Func)(LPVOID lpParam, const bool), LPVOID lpParam) +C4JStorage::ESaveGameState CSaveGame::DeleteSaveData(PSAVE_INFO pSaveInfo, int (*Func)(LPVOID lpParam, const bool), LPVOID lpParam) { return C4JStorage::ESaveGame_Idle; } -void CSaveGame::SetSaveUniqueFilename(char* szFilename) +void CSaveGame::SetSaveUniqueFilename(char *szFilename) { strcpy_s(m_szSaveUniqueName, szFilename); } - void CSaveGame::CreateSaveUniqueName(void) { _SYSTEMTIME UTCSysTime; GetSystemTime(&UTCSysTime); - sprintf_s(m_szSaveUniqueName, sizeof(m_szSaveUniqueName), - "%4d%02d%02d%02d%02d%02d", - UTCSysTime.wYear, - UTCSysTime.wMonth, - UTCSysTime.wDay, - UTCSysTime.wHour, - UTCSysTime.wMinute, - UTCSysTime.wSecond); + sprintf_s(m_szSaveUniqueName, sizeof(m_szSaveUniqueName), "%4d%02d%02d%02d%02d%02d", UTCSysTime.wYear, UTCSysTime.wMonth, UTCSysTime.wDay, + UTCSysTime.wHour, UTCSysTime.wMinute, UTCSysTime.wSecond); } diff --git a/Windows_Libs/Dev/Storage/STO_SaveGame.h b/Windows_Libs/Dev/Storage/STO_SaveGame.h index 5b337ac..6aff70c 100644 --- a/Windows_Libs/Dev/Storage/STO_SaveGame.h +++ b/Windows_Libs/Dev/Storage/STO_SaveGame.h @@ -4,35 +4,36 @@ class CSaveGame { public: + CSaveGame(); + void SetSaveDisabled(bool bDisable); + bool GetSaveDisabled(void); - CSaveGame(); - void SetSaveDisabled(bool bDisable); - bool GetSaveDisabled(void); + void ResetSaveData(); + C4JStorage::ESaveGameState GetSavesInfo(int iPad, int (*Func)(LPVOID lpParam, SAVE_DETAILS *pSaveDetails, const bool), LPVOID lpParam, + char *pszSavePackName); + PSAVE_DETAILS ReturnSavesInfo(); + void ClearSavesInfo(); + C4JStorage::ESaveGameState LoadSaveDataThumbnail(PSAVE_INFO pSaveInfo, int (*Func)(LPVOID lpParam, PBYTE pbThumbnail, DWORD dwThumbnailBytes), + LPVOID lpParam); + C4JStorage::ESaveGameState LoadSaveData(PSAVE_INFO pSaveInfo, int (*Func)(LPVOID lpParam, const bool, const bool), LPVOID lpParam); + unsigned int GetSaveSize(); + void GetSaveData(void *pvData, unsigned int *puiBytes); + bool GetSaveUniqueNumber(INT *piVal); + bool GetSaveUniqueFilename(char *pszName); + void SetSaveTitle(LPCWSTR pwchDefaultSaveName); + PVOID AllocateSaveData(unsigned int uiBytes); + void SetSaveImages(PBYTE pbThumbnail, DWORD dwThumbnailBytes, PBYTE pbImage, DWORD dwImageBytes, PBYTE pbTextData, DWORD dwTextDataBytes); + C4JStorage::ESaveGameState SaveSaveData(int (*Func)(LPVOID, const bool), LPVOID lpParam); + C4JStorage::ESaveGameState DeleteSaveData(PSAVE_INFO pSaveInfo, int (*Func)(LPVOID lpParam, const bool), LPVOID lpParam); + void SetSaveUniqueFilename(char *szFilename); - void ResetSaveData(); - C4JStorage::ESaveGameState GetSavesInfo(int iPad, int (*Func)(LPVOID lpParam, SAVE_DETAILS* pSaveDetails, const bool), LPVOID lpParam, char* pszSavePackName); - PSAVE_DETAILS ReturnSavesInfo(); - void ClearSavesInfo(); - C4JStorage::ESaveGameState LoadSaveDataThumbnail(PSAVE_INFO pSaveInfo, int(*Func)(LPVOID lpParam, PBYTE pbThumbnail, DWORD dwThumbnailBytes), LPVOID lpParam); - C4JStorage::ESaveGameState LoadSaveData(PSAVE_INFO pSaveInfo, int(*Func)(LPVOID lpParam, const bool, const bool), LPVOID lpParam); - unsigned int GetSaveSize(); - void GetSaveData(void* pvData, unsigned int* puiBytes); - bool GetSaveUniqueNumber(INT* piVal); - bool GetSaveUniqueFilename(char* pszName); - void SetSaveTitle(LPCWSTR pwchDefaultSaveName); - PVOID AllocateSaveData(unsigned int uiBytes); - void SetSaveImages(PBYTE pbThumbnail, DWORD dwThumbnailBytes, PBYTE pbImage, DWORD dwImageBytes, PBYTE pbTextData, DWORD dwTextDataBytes); - C4JStorage::ESaveGameState SaveSaveData(int(*Func)(LPVOID, const bool), LPVOID lpParam); - C4JStorage::ESaveGameState DeleteSaveData(PSAVE_INFO pSaveInfo, int(*Func)(LPVOID lpParam, const bool), LPVOID lpParam); - void SetSaveUniqueFilename(char* szFilename); + void CreateSaveUniqueName(void); - void CreateSaveUniqueName(void); - - void* m_pSaveData; - unsigned int m_uiSaveSize; - char m_szSaveUniqueName[32]; - char m_szSaveTitle[256]; - bool m_bIsSafeDisabled; - bool m_bHasSaveDetails; - SAVE_DETAILS* m_pSaveDetails; + void *m_pSaveData; + unsigned int m_uiSaveSize; + char m_szSaveUniqueName[32]; + char m_szSaveTitle[256]; + bool m_bIsSafeDisabled; + bool m_bHasSaveDetails; + SAVE_DETAILS *m_pSaveDetails; }; \ No newline at end of file