From fc3170e2445725f719fcbca72023cbc133b68df6 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 01:58:44 +0200 Subject: [PATCH 01/41] refactor render item manager --- Client/core/Graphics/CRenderItemManager.cpp | 105 ++++++++++---------- 1 file changed, 54 insertions(+), 51 deletions(-) diff --git a/Client/core/Graphics/CRenderItemManager.cpp b/Client/core/Graphics/CRenderItemManager.cpp index ad532a3be33..a7397d127f5 100644 --- a/Client/core/Graphics/CRenderItemManager.cpp +++ b/Client/core/Graphics/CRenderItemManager.cpp @@ -312,8 +312,7 @@ CGuiFontItem* CRenderItemManager::CreateGuiFont(const SString& strFullFilePath, //////////////////////////////////////////////////////////////// void CRenderItemManager::NotifyContructRenderItem(CRenderItem* pItem) { - assert(!MapContains(m_CreatedItemList, pItem)); - MapInsert(m_CreatedItemList, pItem); + assert(!m_CreatedItemList.insert(pItem).second); // assert if item was already in the collection. if (CScreenSourceItem* pScreenSourceItem = DynamicCast(pItem)) m_bBackBufferCopyMaybeNeedsResize = true; @@ -328,8 +327,7 @@ void CRenderItemManager::NotifyContructRenderItem(CRenderItem* pItem) //////////////////////////////////////////////////////////////// void CRenderItemManager::NotifyDestructRenderItem(CRenderItem* pItem) { - assert(MapContains(m_CreatedItemList, pItem)); - MapRemove(m_CreatedItemList, pItem); + assert(!m_CreatedItemList.insert(pItem).second); // assert if item was already in the collection. if (CScreenSourceItem* pScreenSourceItem = DynamicCast(pItem)) m_bBackBufferCopyMaybeNeedsResize = true; @@ -451,11 +449,10 @@ void CRenderItemManager::UpdateBackBufferCopySize() m_bBackBufferCopyMaybeNeedsResize = false; // Set what the max size requirement is for the back buffer copy - uint uiSizeX = 0; - uint uiSizeY = 0; - for (std::set::iterator iter = m_CreatedItemList.begin(); iter != m_CreatedItemList.end(); iter++) + uint uiSizeX = 0, uiSizeY = 0; + for (const auto item : m_CreatedItemList) { - if (CScreenSourceItem* pScreenSourceItem = DynamicCast(*iter)) + if (CScreenSourceItem* pScreenSourceItem = DynamicCast(item)) { uiSizeX = std::max(uiSizeX, pScreenSourceItem->m_uiSizeX); uiSizeY = std::max(uiSizeY, pScreenSourceItem->m_uiSizeY); @@ -631,7 +628,9 @@ void CRenderItemManager::ChangeRenderTarget(uint uiSizeX, uint uiSizeY, IDirect3 // // CRenderItemManager::CanCreateRenderItem // -// +// Returns whenever a render item can be created +// depending on the current set m_TestMode +// Also checks if theres actually enough free video memory // //////////////////////////////////////////////////////////////// bool CRenderItemManager::CanCreateRenderItem(ClassId classId) @@ -646,21 +645,22 @@ bool CRenderItemManager::CanCreateRenderItem(ClassId classId) if (m_iMemoryKBFreeForMTA <= 0) return false; - if (m_TestMode == DX_TEST_MODE_NO_MEM) + switch (m_TestMode) + { + case DX_TEST_MODE_NO_MEM: return false; - if (m_TestMode == DX_TEST_MODE_LOW_MEM) - { - if ((rand() % 1000) > 750) - return false; + case DX_TEST_MODE_LOW_MEM: + return (rand() % 1000) <= 750; + + default: + return true; } } else if (classId == CShaderItem::GetClassId()) { - if (m_TestMode == DX_TEST_MODE_NO_SHADER) - return false; + return m_TestMode != DX_TEST_MODE_NO_SHADER; } - return true; } //////////////////////////////////////////////////////////////// @@ -687,17 +687,20 @@ void CRenderItemManager::UpdateMemoryUsage() m_iTextureMemoryKBUsed = 0; m_iRenderTargetMemoryKBUsed = 0; m_iFontMemoryKBUsed = 0; - for (std::set::iterator iter = m_CreatedItemList.begin(); iter != m_CreatedItemList.end(); iter++) + + for (const auto pRenderItem : m_CreatedItemList) { - CRenderItem* pRenderItem = *iter; if (!pRenderItem->GetIncludeInMemoryStats()) continue; - int iMemoryKBUsed = pRenderItem->GetVideoMemoryKBUsed(); + + const int iMemoryKBUsed = pRenderItem->GetVideoMemoryKBUsed(); if (pRenderItem->IsA(CFileTextureItem::GetClassId())) m_iTextureMemoryKBUsed += iMemoryKBUsed; + else if (pRenderItem->IsA(CRenderTargetItem::GetClassId()) || pRenderItem->IsA(CScreenSourceItem::GetClassId())) m_iRenderTargetMemoryKBUsed += iMemoryKBUsed; + else if (pRenderItem->IsA(CGuiFontItem::GetClassId()) || pRenderItem->IsA(CDxFontItem::GetClassId())) m_iFontMemoryKBUsed += iMemoryKBUsed; } @@ -776,21 +779,20 @@ void CRenderItemManager::GetDxStatus(SDxStatus& outStatus) } // Display color depth - D3DFORMAT BackBufferFormat = g_pDeviceState->CreationState.PresentationParameters.BackBufferFormat; - if (BackBufferFormat >= D3DFMT_R5G6B5 && BackBufferFormat < D3DFMT_A8R3G3B2) - outStatus.settings.b32BitColor = 0; - else - outStatus.settings.b32BitColor = 1; + outStatus.settings.b32BitColor = GetBitsPerPixel(g_pDeviceState->CreationState.PresentationParameters.BackBufferFormat) == 32; // Modify if using test mode - if (m_TestMode == DX_TEST_MODE_NO_MEM) + switch (m_TestMode) + { + case DX_TEST_MODE_NO_MEM: outStatus.videoMemoryKB.iFreeForMTA = 0; - if (m_TestMode == DX_TEST_MODE_LOW_MEM) + case DX_TEST_MODE_LOW_MEM: outStatus.videoMemoryKB.iFreeForMTA = 1; - if (m_TestMode == DX_TEST_MODE_NO_SHADER) + case DX_TEST_MODE_NO_SHADER: outStatus.videoCard.strPSVersion = "0"; + } } //////////////////////////////////////////////////////////////// @@ -886,21 +888,23 @@ int CRenderItemManager::GetPitchDivisor(D3DFORMAT Format) //////////////////////////////////////////////////////////////// int CRenderItemManager::CalcD3DResourceMemoryKBUsage(IDirect3DResource9* pD3DResource) { - D3DRESOURCETYPE type = pD3DResource->GetType(); - - if (type == D3DRTYPE_SURFACE) + switch (pD3DResource->GetType()) + { + case D3DRTYPE_SURFACE: return CalcD3DSurfaceMemoryKBUsage((IDirect3DSurface9*)pD3DResource); - if (type == D3DRTYPE_TEXTURE) + case D3DRTYPE_TEXTURE: return CalcD3DTextureMemoryKBUsage((IDirect3DTexture9*)pD3DResource); - if (type == D3DRTYPE_VOLUMETEXTURE) + case D3DRTYPE_VOLUMETEXTURE: return CalcD3DVolumeTextureMemoryKBUsage((IDirect3DVolumeTexture9*)pD3DResource); - if (type == D3DRTYPE_CUBETEXTURE) + case D3DRTYPE_CUBETEXTURE: return CalcD3DCubeTextureMemoryKBUsage((IDirect3DCubeTexture9*)pD3DResource); - return 0; + default: + return 0; + } } //////////////////////////////////////////////////////////////// @@ -915,10 +919,10 @@ int CRenderItemManager::CalcD3DSurfaceMemoryKBUsage(IDirect3DSurface9* pD3DSurfa D3DSURFACE_DESC surfaceDesc; pD3DSurface->GetDesc(&surfaceDesc); - int iBitsPerPixel = GetBitsPerPixel(surfaceDesc.Format); - int iMemoryUsed = surfaceDesc.Width * surfaceDesc.Height / 8 * iBitsPerPixel; + const int iBitsPerPixel = GetBitsPerPixel(surfaceDesc.Format); + const int iMemoryUsedBytes = (surfaceDesc.Width * surfaceDesc.Height * iBitsPerPixel) / 8; - return iMemoryUsed / 1024; + return iMemoryUsedBytes / 1024; } //////////////////////////////////////////////////////////////// @@ -933,7 +937,7 @@ int CRenderItemManager::CalcD3DTextureMemoryKBUsage(IDirect3DTexture9* pD3DTextu int iMemoryUsed = 0; // Calc memory usage - int iLevelCount = pD3DTexture->GetLevelCount(); + const int iLevelCount = pD3DTexture->GetLevelCount(); for (int i = 0; i < iLevelCount; i++) { D3DSURFACE_DESC surfaceDesc; @@ -955,20 +959,20 @@ int CRenderItemManager::CalcD3DTextureMemoryKBUsage(IDirect3DTexture9* pD3DTextu //////////////////////////////////////////////////////////////// int CRenderItemManager::CalcD3DVolumeTextureMemoryKBUsage(IDirect3DVolumeTexture9* pD3DVolumeTexture) { - int iMemoryUsed = 0; + int iMemoryUsedBytes = 0; // Calc memory usage - int iLevelCount = pD3DVolumeTexture->GetLevelCount(); + const int iLevelCount = pD3DVolumeTexture->GetLevelCount(); for (int i = 0; i < iLevelCount; i++) { D3DVOLUME_DESC volumeDesc; pD3DVolumeTexture->GetLevelDesc(i, &volumeDesc); int iBitsPerPixel = GetBitsPerPixel(volumeDesc.Format); - iMemoryUsed += volumeDesc.Width * volumeDesc.Height * volumeDesc.Depth / 8 * iBitsPerPixel; + iMemoryUsedBytes += (volumeDesc.Width * volumeDesc.Height * volumeDesc.Depth * iBitsPerPixel) / 8; } - return iMemoryUsed / 1024; + return iMemoryUsedBytes / 1024; } //////////////////////////////////////////////////////////////// @@ -980,20 +984,20 @@ int CRenderItemManager::CalcD3DVolumeTextureMemoryKBUsage(IDirect3DVolumeTexture //////////////////////////////////////////////////////////////// int CRenderItemManager::CalcD3DCubeTextureMemoryKBUsage(IDirect3DCubeTexture9* pD3DCubeTexture) { - int iMemoryUsed = 0; + int iMemoryUsedBytes = 0; // Calc memory usage - int iLevelCount = pD3DCubeTexture->GetLevelCount(); + const int iLevelCount = pD3DCubeTexture->GetLevelCount(); for (int i = 0; i < iLevelCount; i++) { D3DSURFACE_DESC surfaceDesc; pD3DCubeTexture->GetLevelDesc(i, &surfaceDesc); - int iBitsPerPixel = GetBitsPerPixel(surfaceDesc.Format); - iMemoryUsed += surfaceDesc.Width * surfaceDesc.Height / 8 * iBitsPerPixel; + const int iBitsPerPixel = GetBitsPerPixel(surfaceDesc.Format); + iMemoryUsedBytes += (surfaceDesc.Width * surfaceDesc.Height * iBitsPerPixel) / 8; } - return iMemoryUsed * 6 / 1024; + return iMemoryUsedBytes * 6 / 1024; } //////////////////////////////////////////////////////////////// @@ -1040,9 +1044,7 @@ void CRenderItemManager::PreDrawWorld() IDirect3DTexture9*& pReadableDepthBuffer = g_pDeviceState->MainSceneState.DepthBuffer; // Determine what is needed - bool bRequireDepthBuffer = false; - if (!m_ShadersUsingDepthBuffer.empty() && m_depthBufferFormat != RFORMAT_UNKNOWN) - bRequireDepthBuffer = true; + const bool bRequireDepthBuffer = !m_ShadersUsingDepthBuffer.empty() && m_depthBufferFormat != RFORMAT_UNKNOWN; bool bRequireNonAADisplay = false; if (g_pDeviceState->CreationState.PresentationParameters.MultiSampleType != D3DMULTISAMPLE_NONE) @@ -1070,6 +1072,7 @@ void CRenderItemManager::PreDrawWorld() m_pDevice->CreateTexture(m_uiDefaultViewportSizeX, m_uiDefaultViewportSizeY, 1, D3DUSAGE_DEPTHSTENCIL, (D3DFORMAT)m_depthBufferFormat, D3DPOOL_DEFAULT, &pReadableDepthBuffer, NULL); } + if (bRequireNonAADisplay && !m_pNonAARenderTarget) { // Create a non-AA render target and depth buffer From 7b6c52359c0b737b054cab6de064603c8e8af8d8 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 01:59:59 +0200 Subject: [PATCH 02/41] Add constexpr to SharedUtil.ClaslIdent.h --- Shared/sdk/SharedUtil.ClassIdent.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Shared/sdk/SharedUtil.ClassIdent.h b/Shared/sdk/SharedUtil.ClassIdent.h index 04745ad53b6..780e2177e11 100644 --- a/Shared/sdk/SharedUtil.ClassIdent.h +++ b/Shared/sdk/SharedUtil.ClassIdent.h @@ -19,24 +19,24 @@ namespace SharedUtil #define DECLARE_BASE_CLASS(cls) \ public: \ - static ClassId GetClassId ( void ) \ + constexpr static ClassId GetClassId ( void ) \ { \ return CLASS_##cls; \ } \ - bool IsA( ClassId classId ) const \ + constexpr bool IsA( ClassId classId ) const \ { \ - return ( ClassHierarchyBits & ( 1ULL << classId ) ) ? true : false; \ + return ( bool )( ClassHierarchyBits & ( 1ULL << classId ) ); \ } \ - const char* GetClassName ( void ) \ + constexpr const char* GetClassName ( void ) \ { \ return ClassName; \ } \ protected: \ - static const char* StaticGetClassName ( void ) \ + constexpr static const char* StaticGetClassName ( void ) \ { \ return #cls; \ } \ - static ClassBits CalcClassHierarchyBits ( void ) \ + constexpr static ClassBits CalcClassHierarchyBits ( void ) \ { \ return ( 1ULL << GetClassId () ); \ } \ @@ -50,7 +50,7 @@ namespace SharedUtil #define DECLARE_CLASS(cls,super) \ public: \ - static ClassId GetClassId ( void ) \ + constexpr static ClassId GetClassId ( void ) \ { \ return CLASS_##cls; \ } \ From 15be6925793cb8881c3cf49230f19a9d990720f7 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 05:54:35 +0200 Subject: [PATCH 03/41] add static keyword to g_rectEdgePixelsData --- Client/core/Graphics/CGraphics.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 4b1cc308de9..c2f890145ec 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -27,7 +27,7 @@ using namespace std; template <> CGraphics* CSingleton::m_pSingleton = NULL; -const SColor g_rectEdgePixelsData[] = { +static const SColor g_rectEdgePixelsData[] = { 0x00FFFF00, 0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, // 0x00FFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00FFFFFF, // 0x00FFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00FFFFFF, // From 84fed9babe912a1371e6566882e468dc8f8e7b1e Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 05:58:12 +0200 Subject: [PATCH 04/41] refactor CGraphics::DrawStringOutline --- Client/core/Graphics/CGraphics.cpp | 31 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index c2f890145ec..4bb8143fc16 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -158,46 +158,47 @@ void CGraphics::DrawString(int iX, int iY, unsigned long dwColor, float fScale, // Slow void CGraphics::DrawStringOutline(const RECT& rect, unsigned long ulColor, const wchar_t* szText, unsigned long ulFormat, LPD3DXFONT pDXFont) { - const uint uiKernelSizeX = 5; - const uint uiKernelSizeY = 5; - const float* pKernel; + constexpr int iKernelSizeX = 5; + constexpr int iKernelSizeY = 5; + const float* pKernel; // Select outline style - int iRed = (ulColor & 0x00FF0000) >> 16; - int iGreen = (ulColor & 0x0000FF00) >> 8; - int iBlue = (ulColor & 0x000000FF) >> 0; - float fBrightness = (iRed * 0.299f + iGreen * 0.587f + iBlue * 0.114f); + const int iRed = (ulColor & 0x00FF0000) >> 16; + const int iGreen = (ulColor & 0x0000FF00) >> 8; + const int iBlue = (ulColor & 0x000000FF) >> 0; + const float fBrightness = (iRed * 0.299f + iGreen * 0.587f + iBlue * 0.114f); if (fBrightness > 64) { // Use black outline with thicker border ulColor = ulColor & 0xFF000000; - const float F = 0, E = 0.16f, D = 0.33f, C = 0.66f, B = 1, A = 0; - static const float kernelData[] = {F, E, D, E, F, E, C, B, C, E, D, B, A, B, D, E, C, B, C, E, F, E, D, E, F}; + constexpr const float F = 0, E = 0.16f, D = 0.33f, C = 0.66f, B = 1, A = 0; + constexpr static const float kernelData[] = {F, E, D, E, F, E, C, B, C, E, D, B, A, B, D, E, C, B, C, E, F, E, D, E, F}; pKernel = kernelData; } else { // Use white outline with thinner border ulColor = ulColor | 0x00FFFFFF; - const float F = 0, E = 0, D = 0.25f, C = 0.5f, B = 1, A = 0; - static const float kernelData[] = {F, E, D, E, F, E, C, B, C, E, D, B, A, B, D, E, C, B, C, E, F, E, D, E, F}; + constexpr const float F = 0, E = 0, D = 0.25f, C = 0.5f, B = 1, A = 0; + constexpr static const float kernelData[] = {F, E, D, E, F, E, C, B, C, E, D, B, A, B, D, E, C, B, C, E, F, E, D, E, F}; pKernel = kernelData; } // Apply definition int iInputAlpha = (ulColor & 0xFF000000) >> 24; iInputAlpha = iInputAlpha * iInputAlpha / 256; - for (uint y = 0; y < uiKernelSizeY; y++) + for (int y = 0; y < iKernelSizeY; y++) { - for (uint x = 0; x < uiKernelSizeX; x++) + for (int x = 0; x < iKernelSizeX; x++) { float fAlpha = *pKernel++; if (fAlpha == 0) continue; + uint uiUseAlpha = (uint)(iInputAlpha * fAlpha); uint uiUseColor = (uiUseAlpha << 24) | (ulColor & 0x00FFFFFF); - int iOffsetX = x - (uiKernelSizeX - 1) / 2; - int iOffsetY = y - (uiKernelSizeY - 1) / 2; + int iOffsetX = x - (iKernelSizeX - 1) / 2; + int iOffsetY = y - (iKernelSizeY - 1) / 2; RECT useRect = {rect.left + iOffsetX, rect.top + iOffsetY, rect.right + iOffsetX, rect.bottom + iOffsetY}; pDXFont->DrawTextW(m_pDXSprite, szText, -1, &useRect, ulFormat, uiUseColor); } From 3d5cd49c3ea9e57c58bd343c7c84715332a995f1 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 05:59:00 +0200 Subject: [PATCH 05/41] refactor CGraphics::CheckModes --- Client/core/Graphics/CGraphics.cpp | 43 +++++++++++++++--------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 4bb8143fc16..31b69e55669 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -484,43 +484,42 @@ void CGraphics::EndDrawBatch() // void CGraphics::CheckModes(EDrawModeType newDrawMode, EBlendModeType newBlendMode) { - bool bDrawModeChanging = (m_CurDrawMode != newDrawMode); - bool bBlendModeChanging = (m_CurBlendMode != newBlendMode); - // Draw mode changing? - if (bDrawModeChanging || bBlendModeChanging) + if (m_CurDrawMode != newDrawMode || m_CurBlendMode != newBlendMode) { - // Flush old - if (m_CurDrawMode == EDrawMode::DX_SPRITE) + switch (m_CurDrawMode) + { + case EDrawMode::DX_SPRITE: { m_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); m_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); m_pDXSprite->End(); + break; } - else if (m_CurDrawMode == EDrawMode::DX_LINE) - { + case EDrawMode::DX_LINE: m_pLineInterface->End(); - } - else if (m_CurDrawMode == EDrawMode::TILE_BATCHER) - { + break; + + case EDrawMode::TILE_BATCHER: m_pTileBatcher->Flush(); - } - else if (m_CurDrawMode == EDrawMode::PRIMITIVE) - { + break; + + case EDrawMode::PRIMITIVE: m_pPrimitiveBatcher->Flush(); - } - else if (m_CurDrawMode == EDrawMode::PRIMITIVE_MATERIAL) - { + break; + + case EDrawMode::PRIMITIVE_MATERIAL: m_pPrimitiveMaterialBatcher->Flush(); + break; } - // Start new - if (newDrawMode == EDrawMode::DX_SPRITE) + switch (newDrawMode) { + case EDrawMode::DX_SPRITE: m_pDXSprite->Begin(D3DXSPRITE_DONOTSAVESTATE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE); - } - else if (newDrawMode == EDrawMode::DX_LINE) - { + break; + + case EDrawMode::DX_LINE: m_pLineInterface->Begin(); } From c7177e2955cbddc4f97f4d9466d433841406f366 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:00:57 +0200 Subject: [PATCH 06/41] refactor CGraphics::GetDXTextExtentW --- Client/core/Graphics/CGraphics.cpp | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 31b69e55669..7e1585cdf1a 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -668,18 +668,10 @@ float CGraphics::GetDXTextExtentW(const wchar_t* wszText, float fScale, LPD3DXFO { // DT_CALCRECT may not take space characters at the end of a line into consideration for the rect size. // Count the amount of space characters at the end - size_t spaceCount = 0; const size_t textLength = wcslen(wszText); - for (int i = textLength - 1; i >= 0; --i) - { - const wchar_t c = wszText[i]; - - if (c == L' ') - ++spaceCount; - else - break; - } + size_t spaceCount = 0; + for (int i = textLength - 1; i >= 0 && wszText[i] == L' '; --i) { spaceCount++; } // count spaces starting from the end // Compute the size of a single space and use that to get the width of the ignored space characters size_t trailingSpacePixels = 0; From 54bfd13b5d706a6882039ac71ece3636fbf4e974 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:01:17 +0200 Subject: [PATCH 07/41] CGraphics::GetTrailingSpacesWidth --- Client/core/Graphics/CGraphics.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 7e1585cdf1a..80b6038c0b8 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -731,13 +731,7 @@ int CGraphics::GetTrailingSpacesWidth(ID3DXFont* pDXFont, WString& strText) { // Count the amount of space characters at the end int iSpaceCount = 0; - for (auto c = strText.rbegin(); c != strText.rend(); ++c) - { - if (*c == ' ') - ++iSpaceCount; - else - break; - } + for (auto c = strText.rbegin(); c != strText.rend() && *c == ' '; ++c) { iSpaceCount++; } // count from the end // Compute the size of a single space and use that // to get the width of the ignored space characters From dfab03db3a63eddc69471477834a30b8ea108efd Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:03:38 +0200 Subject: [PATCH 08/41] refactor CGraphics::IsValidPrimitiveSize --- Client/core/Graphics/CGraphics.cpp | 34 +++++++++++------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 80b6038c0b8..d2595e2226c 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -1000,39 +1000,29 @@ void CGraphics::DrawMaterialPrimitiveQueued(std::vector= 2; + case D3DPT_LINELIST: - if (iNumVertives % 2 != 0) - { - return false; - } - break; + return iNumVertives % 2 == 0; + case D3DPT_TRIANGLELIST: - if (iNumVertives % 3 != 0) - { - return false; - } + return iNumVertives % 3 == 0; + case D3DPT_TRIANGLEFAN: case D3DPT_TRIANGLESTRIP: - if (iNumVertives < 3) - { - return false; - } - break; - } + return iNumVertives >= 3; + case D3DPT_POINTLIST: return true; + + default: + dassert(0); // shoudnt be reached +} } void CGraphics::DrawTextureQueued(float fX, float fY, float fWidth, float fHeight, float fU, float fV, float fSizeU, float fSizeV, bool bRelativeUV, From 2a2c54eeb204ed19c29bfd2024fc66819d2bf3e5 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:04:30 +0200 Subject: [PATCH 09/41] refactor CGraphics::LoadStandardDXFonts --- Client/core/Graphics/CGraphics.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index d2595e2226c..4acf7e87c6a 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -1320,6 +1320,8 @@ bool CGraphics::LoadStandardDXFonts() // Add our custom font resources if (m_FontResourceNames.empty()) { + m_FontResourceNames.reserve(5); + m_FontResourceNames.push_back("pricedown.ttf"); m_FontResourceNames.push_back("sabankgothic.ttf"); m_FontResourceNames.push_back("saheader.ttf"); @@ -1327,17 +1329,17 @@ bool CGraphics::LoadStandardDXFonts() m_FontResourceNames.push_back("unifont-5.1.20080907.ttf"); } - for (uint i = 0; i < m_FontResourceNames.size(); i++) + for (const auto& fontName : m_FontResourceNames) { - SString strPathFilename = CalcMTASAPath("MTA\\cgui\\" + m_FontResourceNames[i]); + SString strPathFilename = CalcMTASAPath("MTA\\cgui\\" + fontName); if (!AddFontResourceEx(strPathFilename, FR_PRIVATE, 0)) { - SString strFileMd5 = GenerateHashHexStringFromFile(EHashFunctionType::MD5, strPathFilename); - uint uiFileSize = (uint)FileSize(strPathFilename); + const SString strFileMd5 = GenerateHashHexStringFromFile(EHashFunctionType::MD5, strPathFilename); + const uint uiFileSize = (uint)FileSize(strPathFilename); AddReportLog(9442, SString("Problem with AddFontResourceEx [MD5:%s Size:%d] %s ", *strFileMd5, uiFileSize, *strPathFilename)); if (!FileExists(strPathFilename)) { - BrowseToSolution("mta-datafiles-missing", EXIT_GAME_FIRST | ASK_GO_ONLINE, "Error loading MTA font " + m_FontResourceNames[i]); + BrowseToSolution("mta-datafiles-missing", EXIT_GAME_FIRST | ASK_GO_ONLINE, "Error loading MTA font " + fontName); } } } From 995b236aa4691ddbe5ce2ebb1c75a16c1671fe69 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:05:21 +0200 Subject: [PATCH 10/41] refactor CGraphics::DrawQueue --- Client/core/Graphics/CGraphics.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 4acf7e87c6a..3cb7cc31011 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -1606,20 +1606,15 @@ bool CGraphics::HasPrimitive3DPreGUIQueueItems(void) void CGraphics::DrawQueue(std::vector& Queue) { BeginDrawBatch(); - // Items to draw? + if (Queue.size() > 0) { - // Loop through it - std::vector::iterator iter = Queue.begin(); - for (; iter != Queue.end(); iter++) - { - // Draw the item - DrawQueueItem(*iter); - } + for (const auto queueItem : Queue) + DrawQueueItem(queueItem); - // Clear the list Queue.clear(); } + EndDrawBatch(); } From e5c31e3a17506ce39c7e4fcc9a7683c3b195dade Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:06:08 +0200 Subject: [PATCH 11/41] refactor CGraphics::ClearDrawQueue --- Client/core/Graphics/CGraphics.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 3cb7cc31011..62bf7018ccd 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -1795,13 +1795,19 @@ ID3DXFont* CGraphics::MaybeGetBigFont(ID3DXFont* pDXFont, float& fScaleX, float& // void CGraphics::ClearDrawQueue(std::vector& Queue) { - for (std::vector::const_iterator iter = Queue.begin(); iter != Queue.end(); iter++) + for (auto& item : Queue) { - const sDrawQueueItem& item = *iter; - if (item.eType == QUEUE_TEXTURE || item.eType == QUEUE_SHADER) + switch (item.eType) + { + case QUEUE_TEXTURE: + case QUEUE_SHADER: RemoveQueueRef(item.Texture.pMaterial); - else if (item.eType == QUEUE_TEXT) + break; + + case QUEUE_TEXT: RemoveQueueRef(item.Text.pDXFont); + break; + } } Queue.clear(); } From 93010b1ab0eae35ba26aa584364348553e00cc36 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:06:43 +0200 Subject: [PATCH 12/41] refactor CGraphics::ResizeTextureData --- Client/core/Graphics/CGraphics.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 62bf7018ccd..b7cabcd28cc 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -2244,36 +2244,40 @@ void CGraphics::DrawProgressMessage(bool bPreserveBackbuffer) bool CGraphics::ResizeTextureData(const void* pData, uint uiDataPitch, uint uiWidth, uint uiHeight, uint d3dFormat, uint uiNewWidth, uint uiNewHeight, CBuffer& outBuffer) { - bool bResult = false; IDirect3DSurface9* pCurrentSurface = NULL; IDirect3DSurface9* pNewSurface = NULL; - do - { + + + bool bResult = false; + // Create surfaces if (FAILED(g_pGraphics->GetDevice()->CreateOffscreenPlainSurface(uiWidth, uiHeight, (D3DFORMAT)d3dFormat, D3DPOOL_SCRATCH, &pCurrentSurface, NULL))) - break; + goto cleanup_and_return; + if (FAILED(g_pGraphics->GetDevice()->CreateOffscreenPlainSurface(uiNewWidth, uiNewHeight, (D3DFORMAT)d3dFormat, D3DPOOL_SCRATCH, &pNewSurface, NULL))) - break; + goto cleanup_and_return; // Data in if (!CopyDataToSurface(pCurrentSurface, (const BYTE*)pData, uiDataPitch)) - break; + goto cleanup_and_return; // Resize if (FAILED(D3DXLoadSurfaceFromSurface(pNewSurface, NULL, NULL, pCurrentSurface, NULL, NULL, D3DX_FILTER_TRIANGLE, 0))) - break; + goto cleanup_and_return; // Data out if (!CopyDataFromSurface(pNewSurface, outBuffer)) - break; + goto cleanup_and_return; bResult = true; - } while (false); + +cleanup_and_return: // Clean up SAFE_RELEASE(pCurrentSurface); SAFE_RELEASE(pNewSurface); + return bResult; } From a27c4fc861b90205b24e3dfcc92c49781ea2fabd Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:07:29 +0200 Subject: [PATCH 13/41] refactor CGaphics::CreateSphereFaces --- Client/core/Graphics/CGraphics.cpp | 70 +++++++++++++++++------------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index b7cabcd28cc..e9d4def2e0d 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -2392,49 +2392,59 @@ namespace // void CreateSphereFaces(std::vector& faceList, int iIterations) { - int numFaces = (int)(pow(4.0, iIterations) * 8); faceList.clear(); - faceList.reserve(numFaces); + faceList.reserve(pow(4.0, iIterations) * (size_t)8); // Initial octahedron - static SFixedArray vecPoints = {CVector(0, 0, 1), CVector(0, 0, -1), CVector(-1, -1, 0), - CVector(1, -1, 0), CVector(1, 1, 0), CVector(-1, 1, 0)}; - static const SFixedArray indices = {0, 3, 4, 0, 4, 5, 0, 5, 2, 0, 2, 3, 1, 4, 3, 1, 5, 4, 1, 2, 5, 1, 3, 2}; - for (uint i = 0; i < NUMELMS(vecPoints); i++) - vecPoints[i].Normalize(); - - for (uint i = 0; i < NUMELMS(indices); i += 3) - faceList.push_back(SFace(vecPoints[indices[i]], vecPoints[indices[i + 1]], vecPoints[indices[i + 2]])); - - // For each iteration - while (iIterations--) + // Trick: so we can make this whole array constexpr + // basically, because these vectors were 1, 0, 1, etc.. + // after normalization we'd get a ector with v, 0, v for example. + constexpr auto v = 1.414213562373095f / 2.0f; + constexpr static CVector vecPoints[] = { + {0, 0, 1}, + {0, 0, -1}, + {-v, -v, 0}, + {v, -v, 0}, + {v, v, 0}, + {-v, v, 0} + }; + + // 24 random ass indecies in a table + for (const auto i : { 0, 3, 4, 0, 4, 5, 0, 5, 2, 0, 2, 3, 1, 4, 3, 1, 5, 4, 1, 2, 5, 1, 3, 2 }) + faceList.emplace_back(vecPoints[i], vecPoints[i + 1], vecPoints[i + 2]); + + while (iIterations-- != 0) { // Divide each face into 4 - for (int i = faceList.size() - 1; i >= 0; i--) + for (auto iter = faceList.rbegin(); iter != faceList.rend(); iter++) { + auto& face = *iter; + // Get the three face points - CVector a = faceList[i].a; - CVector b = faceList[i].b; - CVector c = faceList[i].c; + const CVector& a = face.a; + const CVector& b = face.b; + const CVector& c = face.c; - // Make three inner points - CVector a2 = (a + b) * 0.5f; - CVector b2 = (b + c) * 0.5f; - CVector c2 = (c + a) * 0.5f; + // Create the new face from the first sub-face + SFace newSubFace = SFace( + (a + b) * 0.5f, + (b + c) * 0.5f, + (c + a) * 0.5f + ); // Keep points to the edge of the unit sphere - a2.Normalize(); - b2.Normalize(); - c2.Normalize(); - - // Replace the original face with the first sub-face - faceList[i] = SFace(a2, b2, c2); + newSubFace.a.Normalize(); + newSubFace.b.Normalize(); + newSubFace.c.Normalize(); // Add the three other sub-faces to the end of the list - faceList.push_back(SFace(a, a2, c2)); - faceList.push_back(SFace(b, a2, b2)); - faceList.push_back(SFace(c, b2, c2)); + faceList.emplace_back(b, newSubFace.a, newSubFace.b); + faceList.emplace_back(a, newSubFace.a, newSubFace.c); + faceList.emplace_back(c, newSubFace.b, newSubFace.c); + + // And only now replace old face with new, because otherwise a, b, c gets replaced to the new values + face = newSubFace; } } } From cbeac8683651f9ccb0fa39f7c0537e141434a061 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:07:46 +0200 Subject: [PATCH 14/41] refactor Cgraphics::GetSphereWireModel --- Client/core/Graphics/CGraphics.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index e9d4def2e0d..0f25efb8134 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -2480,24 +2480,24 @@ namespace static std::map wireModelMap; // Find existing - SWireModel* pWireModel = MapFind(wireModelMap, iterations); + SWireModel* const pWireModel = MapFind(wireModelMap, iterations); if (pWireModel) return *pWireModel; // Add new - MapSet(wireModelMap, iterations, SWireModel()); - SWireModel& wireModel = *MapFind(wireModelMap, iterations); + SWireModel& wireModel = wireModelMap.emplace(iterations, SWireModel()).first->second; std::vector faceList; CreateSphereFaces(faceList, iterations); // Create big vertex/line list - for (uint i = 0; i < faceList.size(); i++) + for (const auto& face : faceList) { - wireModel.AddLine(faceList[i].a, faceList[i].b); - wireModel.AddLine(faceList[i].b, faceList[i].c); - wireModel.AddLine(faceList[i].c, faceList[i].a); + wireModel.AddLine(face.a, face.b); + wireModel.AddLine(face.b, face.c); + wireModel.AddLine(face.c, face.a); } + return wireModel; } } // namespace WireShpere From bfdeb28db884dd146f2a0757c3b190a0a30baf26 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:08:21 +0200 Subject: [PATCH 15/41] other small refactors in CGraphics --- Client/core/Graphics/CGraphics.cpp | 117 +++++++++++++++++------------ 1 file changed, 68 insertions(+), 49 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 0f25efb8134..bd2ae1a9650 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -232,7 +232,9 @@ void CGraphics::DrawRectangleInternal(float fX, float fY, float fWidth, float fH int posOffsetY; int overrideWidth; int overrideHeight; - } static sectionList[] = { + }; + + constexpr static SSection sectionList[] = { {{3, 3, 5, 5}, 0, 0, 0, 0}, // Center {{3, 0, 5, 2}, 0, -2, 0, 2}, // Top {{0, 0, 2, 2}, -2, -2, 2, 2}, // Top left @@ -244,8 +246,7 @@ void CGraphics::DrawRectangleInternal(float fX, float fY, float fWidth, float fH {{6, 0, 8, 2}, 2, -2, 2, 2}, // Top right }; - D3DXMATRIX matrix; - const D3DXVECTOR2 scalingCentre(0.5f, 0.5f); + D3DXMATRIX matrix; for (uint i = 0; i < NUMELMS(sectionList); i++) { const SSection& section = sectionList[i]; @@ -743,7 +744,6 @@ int CGraphics::GetTrailingSpacesWidth(ID3DXFont* pDXFont, WString& strText) GetTextExtentPoint32W(dc, L" ", 1, &size); iSpacesWidth = iSpaceCount * size.cx; } - return iSpacesWidth; } @@ -806,6 +806,7 @@ void CGraphics::DrawLineQueued(float fX1, float fY1, float fX2, float fY2, float sDrawQueueItem Item; Item.eType = QUEUE_LINE; Item.blendMode = m_ActiveBlendMode; + Item.Line.fX1 = fX1; Item.Line.fY1 = fY1; Item.Line.fX2 = fX2; @@ -859,6 +860,7 @@ void CGraphics::DrawRectQueued(float fX, float fY, float fWidth, float fHeight, sDrawQueueItem Item; Item.eType = QUEUE_RECT; Item.blendMode = m_ActiveBlendMode; + Item.Rect.fX = fX; Item.Rect.fY = fY; Item.Rect.fWidth = fWidth; @@ -878,10 +880,10 @@ void CGraphics::DrawCircleQueued(float fX, float fY, float fRadius, float fStart return; auto pVecVertices = new std::vector(); - fStartAngle = D3DXToRadian(fStartAngle); - fStopAngle = D3DXToRadian(fStopAngle); + pVecVertices->reserve(siSegments + 1); + // Calculate each segment angle - const float kfSegmentAngle = (fStopAngle - fStartAngle) / siSegments; + const float kfSegmentAngle = (D3DXToRadian(fStopAngle) - D3DXToRadian(fStartAngle)) / siSegments; // Add center point pVecVertices->push_back({fX, fY, 0.0f, ulColorCenter}); @@ -987,11 +989,14 @@ void CGraphics::DrawMaterialPrimitiveQueued(std::vector= 2; - case D3DPT_LINELIST: + case D3DPT_LINELIST: return iNumVertives % 2 == 0; - case D3DPT_TRIANGLELIST: + case D3DPT_TRIANGLELIST: return iNumVertives % 3 == 0; - case D3DPT_TRIANGLEFAN: - case D3DPT_TRIANGLESTRIP: + case D3DPT_TRIANGLEFAN: + case D3DPT_TRIANGLESTRIP: return iNumVertives >= 3; case D3DPT_POINTLIST: - return true; + return true; default: dassert(0); // shoudnt be reached -} + } } void CGraphics::DrawTextureQueued(float fX, float fY, float fWidth, float fHeight, float fU, float fV, float fSizeU, float fSizeV, bool bRelativeUV, @@ -1033,6 +1038,7 @@ void CGraphics::DrawTextureQueued(float fX, float fY, float fWidth, float fHeigh // Set up a queue item sDrawQueueItem Item; Item.blendMode = m_ActiveBlendMode; + Item.Texture.fX = fX; Item.Texture.fY = fY; Item.Texture.fWidth = fWidth; @@ -1059,10 +1065,7 @@ void CGraphics::DrawTextureQueued(float fX, float fY, float fWidth, float fHeigh Item.Texture.pMaterial = pMaterial; // Use tilebatcher if non-default texture addessing is needed - if (pMaterial->m_TextureAddress == TADDRESS_WRAP) - Item.eType = QUEUE_TEXTURE; - else - Item.eType = QUEUE_SHADER; + Item.eType = (pMaterial->m_TextureAddress == TADDRESS_WRAP) ? QUEUE_TEXTURE : QUEUE_SHADER; } // Keep material valid while in the queue @@ -1099,8 +1102,10 @@ void CGraphics::DrawStringQueued(float fLeft, float fTop, float fRight, float fB if (fScaleX != 1.0f || fScaleY != 1.0f) { + // Optimization hack: multiplying is faster than diving const float fRcpScaleX = 1 / fScaleX; const float fRcpScaleY = 1 / fScaleY; + fLeft *= fRcpScaleX; fTop *= fRcpScaleY; fRight *= fRcpScaleX; @@ -1153,10 +1158,10 @@ void CGraphics::DrawStringQueued(float fLeft, float fTop, float fRight, float fB // Break into lines CSplitStringW splitLines(wstrText, L"\n"); - int iNumLines = splitLines.size(); + const int iNumLines = splitLines.size(); - float fLineHeight = GetDXFontHeight(fScaleY, pDXFont); - float fTotalHeight = iNumLines * fLineHeight; + const float fLineHeight = GetDXFontHeight(fScaleY, pDXFont); + const float fTotalHeight = iNumLines * fLineHeight; // Y position of text float fY; @@ -1169,9 +1174,10 @@ void CGraphics::DrawStringQueued(float fLeft, float fTop, float fRight, float fB // Process each line SColor currentColor = dwColor; - for (uint i = 0; i < splitLines.size(); i++) + + for (const auto& lineStr : splitLines) { - DrawColorCodedTextLine(fLeft, fRight, fY, currentColor, splitLines[i], fScaleX, fScaleY, ulFormat, pDXFont, bPostGUI, bSubPixelPositioning, + DrawColorCodedTextLine(fLeft, fRight, fY, currentColor, lineStr, fScaleX, fScaleY, ulFormat, pDXFont, bPostGUI, bSubPixelPositioning, fRotation, fRotationCenterX, fRotationCenterY); fY += fLineHeight; } @@ -1189,6 +1195,8 @@ void CGraphics::DrawColorCodedTextLine(float fLeft, float fRight, float fY, SCol SColor color; }; + // TODO: Make an std::map with wszText, so we can cache this section list, because most texts are constant, not randomly changing + // also make section list a vector please... who uses lists these days? std::list sectionList; // Break line into color sections @@ -1256,10 +1264,8 @@ void CGraphics::DrawColorCodedTextLine(float fLeft, float fRight, float fY, SCol fX = fLeft; // DT_LEFT // Draw all the color sections - for (std::list::const_iterator iter = sectionList.begin(); iter != sectionList.end(); ++iter) + for (const auto& section : sectionList) { - const STextSection& section = *iter; - float fLeft = fX; float fTop = fY; @@ -1304,7 +1310,8 @@ void CGraphics::DrawColorCodedTextLine(float fLeft, float fRight, float fY, SCol } } -static const sFontInfo fontInfos[] = {{"tahoma", 15, FW_NORMAL}, +constexpr static sFontInfo fontInfos[] = { + {"tahoma", 15, FW_NORMAL}, {"tahomabd", 15, FW_BOLD}, {"verdana", 15, FW_NORMAL}, {"arial", 15, FW_NORMAL}, @@ -1313,7 +1320,8 @@ static const sFontInfo fontInfos[] = {{"tahoma", 15, FW_NORMAL}, {"bankgothic md bt", 30, FW_NORMAL}, {"diploma", 30, FW_NORMAL}, {"beckett", 30, FW_NORMAL}, - {"unifont", 14, FW_NORMAL}}; + {"unifont", 14, FW_NORMAL} +}; bool CGraphics::LoadStandardDXFonts() { @@ -1425,10 +1433,8 @@ bool CGraphics::DestroyAdditionalDXFont(std::string strFontPath, ID3DXFont* pD3D bool CGraphics::DestroyStandardDXFonts() { // Remove our custom font resources (needs to be identical to LoadFonts) - for (uint i = 0; i < m_FontResourceNames.size(); i++) - { - RemoveFontResourceEx(CalcMTASAPath("MTA\\cgui\\" + m_FontResourceNames[i]), FR_PRIVATE, 0); - } + for (const auto fontName : m_FontResourceNames) + RemoveFontResourceEx(CalcMTASAPath("MTA\\cgui\\" + fontName), FR_PRIVATE, 0); // Release for (int i = 0; i < NUM_FONTS; i++) @@ -1438,8 +1444,8 @@ bool CGraphics::DestroyStandardDXFonts() } // Release custom scale versions of standard fonts as well - for (std::map::iterator iter = m_CustomScaleFontMap.begin(); iter != m_CustomScaleFontMap.end(); ++iter) - SAFE_RELEASE(iter->second.pFont); + for (auto& [name, info] : m_CustomScaleFontMap) + SAFE_RELEASE(info.pFont); return true; } @@ -1459,13 +1465,15 @@ void CGraphics::DrawTexture(CTextureItem* pTexture, float fX, float fY, float fS const float fFileHeight = pTexture->m_uiSizeY; BeginDrawBatch(); - RECT cutImagePos; const float fMultU = (bRelativeUV ? fSurfaceWidth : fSurfaceWidth / fFileWidth); const float fMultV = (bRelativeUV ? fSurfaceHeight : fSurfaceHeight / fFileHeight); + + RECT cutImagePos; cutImagePos.left = (fU)*fMultU; cutImagePos.right = (fU + fSizeU) * fMultU; cutImagePos.top = (fV)*fMultV; cutImagePos.bottom = (fV + fSizeV) * fMultV; + const float fCutWidth = cutImagePos.right - cutImagePos.left; const float fCutHeight = cutImagePos.bottom - cutImagePos.top; @@ -1515,13 +1523,16 @@ void CGraphics::OnDeviceInvalidate(IDirect3DDevice9* pDevice) { if (m_pDXFonts[i]) m_pDXFonts[i]->OnLostDevice(); + if (m_pBigDXFonts[i]) m_pBigDXFonts[i]->OnLostDevice(); } - for (std::map::iterator iter = m_CustomScaleFontMap.begin(); iter != m_CustomScaleFontMap.end(); ++iter) - if (iter->second.pFont) - iter->second.pFont->OnLostDevice(); + for (auto& [name, info] : m_CustomScaleFontMap) + { + if (info.pFont) + info.pFont->OnLostDevice(); + } if (m_pDXSprite) m_pDXSprite->OnLostDevice(); @@ -1531,6 +1542,7 @@ void CGraphics::OnDeviceInvalidate(IDirect3DDevice9* pDevice) m_pRenderItemManager->OnLostDevice(); m_pScreenGrabber->OnLostDevice(); + SAFE_RELEASE(m_pSavedFrontBufferData); SAFE_RELEASE(m_pTempBackBufferData); } @@ -1545,9 +1557,11 @@ void CGraphics::OnDeviceRestore(IDirect3DDevice9* pDevice) m_pBigDXFonts[i]->OnResetDevice(); } - for (std::map::iterator iter = m_CustomScaleFontMap.begin(); iter != m_CustomScaleFontMap.end(); ++iter) - if (iter->second.pFont) - iter->second.pFont->OnResetDevice(); + for (auto& [name, info] : m_CustomScaleFontMap) + { + if (info.pFont) + info.pFont->OnResetDevice(); + } if (m_pDXSprite) m_pDXSprite->OnResetDevice(); @@ -1587,18 +1601,18 @@ void CGraphics::DrawLine3DPreGUIQueue() m_pMaterialLine3DBatcherPreGUI->Flush(); } -void CGraphics::DrawPrimitive3DPreGUIQueue(void) +void CGraphics::DrawPrimitive3DPreGUIQueue() { m_pPrimitive3DBatcherPreGUI->Flush(); m_pMaterialPrimitive3DBatcherPreGUI->Flush(); } -bool CGraphics::HasLine3DPreGUIQueueItems(void) +bool CGraphics::HasLine3DPreGUIQueueItems() { return m_pLine3DBatcherPreGUI->HasItems() || m_pMaterialLine3DBatcherPreGUI->HasItems(); } -bool CGraphics::HasPrimitive3DPreGUIQueueItems(void) +bool CGraphics::HasPrimitive3DPreGUIQueueItems() { return m_pMaterialPrimitive3DBatcherPreGUI->HasItems() || m_pPrimitive3DBatcherPreGUI->HasItems(); } @@ -1780,9 +1794,11 @@ ID3DXFont* CGraphics::MaybeGetBigFont(ID3DXFont* pDXFont, float& fScaleX, float& if (m_pDXFonts[i] == pDXFont) { // Adjust scale to compensate for higher res font - fScaleX *= 0.25f; + // devide by 4 because the big font is 4x bigger + fScaleX /= 4.0f; if (&fScaleX != &fScaleY) // Check fScaleY is not the same variable - fScaleY *= 0.25f; + fScaleX /= 4.0f; + return m_pBigDXFonts[i]; } } @@ -1887,12 +1903,14 @@ void CGraphics::LeavingMTARenderZone() //////////////////////////////////////////////////////////////// void CGraphics::MaybeEnteringMTARenderZone() { - if (m_MTARenderZone == MTA_RZONE_OUTSIDE) + switch (m_MTARenderZone) { + case MTA_RZONE_OUTSIDE: // Handle stacking if already outside m_iOutsideZoneCount++; - } - else if (m_MTARenderZone == MTA_RZONE_NONE) + break; + + case MTA_RZONE_NONE: { assert(!m_pSavedStateBlock); assert(m_iOutsideZoneCount == 0); @@ -1903,6 +1921,7 @@ void CGraphics::MaybeEnteringMTARenderZone() m_MTARenderZone = MTA_RZONE_OUTSIDE; } } +} //////////////////////////////////////////////////////////////// // From 0bb8f0966ab19f78f8158cb25e9ee487024b0197 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:09:05 +0200 Subject: [PATCH 16/41] refactor CVector.h to use constexpr --- Shared/sdk/CVector.h | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/Shared/sdk/CVector.h b/Shared/sdk/CVector.h index d4e938ae926..bb5d18fc4b4 100644 --- a/Shared/sdk/CVector.h +++ b/Shared/sdk/CVector.h @@ -28,40 +28,44 @@ class CVector public: float fX, fY, fZ; - CVector() + constexpr CVector() : + fX(0), + fY(0), + fZ(0) { - this->fX = 0; - this->fY = 0; - this->fZ = 0; }; - CVector(float fX, float fY, float fZ) + constexpr CVector(float x, float y, float z) : + fX(x), + fY(y), + fZ(z) { - this->fX = fX; - this->fY = fY; - this->fZ = fZ; } - CVector(const CVector4D& vec) + constexpr CVector(const CVector4D& vec) : + fX(vec.fX), + fY(vec.fY), + fZ(vec.fZ) { - this->fX = vec.fX; - this->fY = vec.fY; - this->fZ = vec.fZ; } + // returns the length of the vector as well float Normalize() { - float t = sqrt(fX * fX + fY * fY + fZ * fZ); + const float t = sqrt(fX * fX + fY * fY + fZ * fZ); if (t > FLOAT_EPSILON) { - float fRcpt = 1 / t; + // stupid hack: mul. is faster than div............. + const float fRcpt = 1 / t; + fX *= fRcpt; fY *= fRcpt; fZ *= fRcpt; + + return t; } else - t = 0; - return t; + return 0; } float Length() const { return sqrt((fX * fX) + (fY * fY) + (fZ * fZ)); } From 67289ecce518796e3c0a63604e387b11964ead8d Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:09:24 +0200 Subject: [PATCH 17/41] refactor CVector2D to use constexpr --- Shared/sdk/CVector2D.h | 46 ++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/Shared/sdk/CVector2D.h b/Shared/sdk/CVector2D.h index ec5573593b0..759f1f098fb 100644 --- a/Shared/sdk/CVector2D.h +++ b/Shared/sdk/CVector2D.h @@ -20,30 +20,32 @@ class CVector2D { public: - CVector2D() + + constexpr CVector2D() : + fX(0), + fY(0) { - fX = 0; - fY = 0; - } + }; - CVector2D(float _fX, float _fY) + constexpr CVector2D(float x, float y) : + fX(x), + fY(y) { - fX = _fX; - fY = _fY; } - CVector2D(const CVector& vec) + constexpr CVector2D(const CVector& vec) : + fX(vec.fX), + fY(vec.fY) { - fX = vec.fX; - fY = vec.fY; } - CVector2D(const CVector4D& vec) + constexpr CVector2D(const CVector4D& vec) : + fX(vec.fX), + fY(vec.fY) { - fX = vec.fX; - fY = vec.fY; } + CVector2D& operator=(const CVector& vec) { fX = vec.fX; @@ -74,21 +76,17 @@ class CVector2D } } - CVector2D operator*(float fRight) const { return CVector2D(fX * fRight, fY * fRight); } + constexpr CVector2D operator*(float fRight) const { return CVector2D(fX * fRight, fY * fRight); } - CVector2D operator/(float fRight) const - { - float fRcpValue = 1 / fRight; - return CVector2D(fX * fRcpValue, fY * fRcpValue); - } + constexpr CVector2D operator/(float fRight) const { return CVector2D(fX / fRight, fY / fRight); } - CVector2D operator+(const CVector2D& vecRight) const { return CVector2D(fX + vecRight.fX, fY + vecRight.fY); } + constexpr CVector2D operator+(const CVector2D& vecRight) const { return CVector2D(fX + vecRight.fX, fY + vecRight.fY); } - CVector2D operator-(const CVector2D& vecRight) const { return CVector2D(fX - vecRight.fX, fY - vecRight.fY); } + constexpr CVector2D operator-(const CVector2D& vecRight) const { return CVector2D(fX - vecRight.fX, fY - vecRight.fY); } - CVector2D operator*(const CVector2D& vecRight) const { return CVector2D(fX * vecRight.fX, fY * vecRight.fY); } + constexpr CVector2D operator*(const CVector2D& vecRight) const { return CVector2D(fX * vecRight.fX, fY * vecRight.fY); } - CVector2D operator/(const CVector2D& vecRight) const { return CVector2D(fX / vecRight.fX, fY / vecRight.fY); } + constexpr CVector2D operator/(const CVector2D& vecRight) const { return CVector2D(fX / vecRight.fX, fY / vecRight.fY); } void operator+=(float fRight) { @@ -140,7 +138,7 @@ class CVector2D bool operator==(const CVector2D& param) const { return ((fabs(fX - param.fX) < FLOAT_EPSILON) && (fabs(fY - param.fY) < FLOAT_EPSILON)); } - bool operator!=(const CVector2D& param) const { return ((fabs(fX - param.fX) >= FLOAT_EPSILON) || (fabs(fY - param.fY) >= FLOAT_EPSILON)); } + bool operator!=(const CVector2D& param) const { return !(*this == param); } float fX; float fY; From 1171387f0b328dc16faa6e48679fc548806be914 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:09:36 +0200 Subject: [PATCH 18/41] refactor CVector2D to use constexpr --- Shared/sdk/CVector4D.h | 44 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/Shared/sdk/CVector4D.h b/Shared/sdk/CVector4D.h index 798d6bc3a41..0f18c73af4b 100644 --- a/Shared/sdk/CVector4D.h +++ b/Shared/sdk/CVector4D.h @@ -20,28 +20,28 @@ class CVector4D { public: - CVector4D() + constexpr CVector4D() : + fX(0), + fY(0), + fZ(0), + fW(0) { - fX = 0; - fY = 0; - fZ = 0; - fW = 0; - } + }; - CVector4D(float _fX, float _fY, float _fZ, float _fW) + constexpr CVector4D(float x, float y, float z, float w) : + fX(x), + fY(y), + fZ(z), + fW(w) { - fX = _fX; - fY = _fY; - fZ = _fZ; - fW = _fW; } - CVector4D(const CVector4D& vec) + constexpr CVector4D(const CVector4D& vec) : + fX(vec.fX), + fY(vec.fY), + fZ(vec.fZ), + fW(vec.fW) { - fX = vec.fX; - fY = vec.fY; - fZ = vec.fZ; - fW = vec.fW; } CVector4D& operator=(const CVector4D& vec) @@ -71,21 +71,21 @@ class CVector4D } } - CVector4D operator*(float fRight) const { return CVector4D(fX * fRight, fY * fRight, fZ * fRight, fW * fRight); } + constexpr CVector4D operator*(float fRight) const { return CVector4D(fX * fRight, fY * fRight, fZ * fRight, fW * fRight); } - CVector4D operator/(float fRight) const + constexpr CVector4D operator/(float fRight) const { float fRcpValue = 1 / fRight; return CVector4D(fX * fRcpValue, fY * fRcpValue, fZ * fRcpValue, fW * fRcpValue); } - CVector4D operator+(const CVector4D& vecRight) const { return CVector4D(fX + vecRight.fX, fY + vecRight.fY, fZ + vecRight.fZ, fW + vecRight.fW); } + constexpr CVector4D operator+(const CVector4D& vecRight) const { return CVector4D(fX + vecRight.fX, fY + vecRight.fY, fZ + vecRight.fZ, fW + vecRight.fW); } - CVector4D operator-(const CVector4D& vecRight) const { return CVector4D(fX - vecRight.fX, fY - vecRight.fY, fZ - vecRight.fZ, fW - vecRight.fW); } + constexpr CVector4D operator-(const CVector4D& vecRight) const { return CVector4D(fX - vecRight.fX, fY - vecRight.fY, fZ - vecRight.fZ, fW - vecRight.fW); } - CVector4D operator*(const CVector4D& vecRight) const { return CVector4D(fX * vecRight.fX, fY * vecRight.fY, fZ * vecRight.fZ, fW * vecRight.fW); } + constexpr CVector4D operator*(const CVector4D& vecRight) const { return CVector4D(fX * vecRight.fX, fY * vecRight.fY, fZ * vecRight.fZ, fW * vecRight.fW); } - CVector4D operator/(const CVector4D& vecRight) const { return CVector4D(fX / vecRight.fX, fY / vecRight.fY, fZ / vecRight.fZ, fW / vecRight.fW); } + constexpr CVector4D operator/(const CVector4D& vecRight) const { return CVector4D(fX / vecRight.fX, fY / vecRight.fY, fZ / vecRight.fZ, fW / vecRight.fW); } void operator+=(float fRight) { From ee8fc8e1de3bc642e93defc8f9ef3424eac21891 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:10:05 +0200 Subject: [PATCH 19/41] change SColor constructor to use member init list --- Shared/sdk/SharedUtil.Misc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Shared/sdk/SharedUtil.Misc.h b/Shared/sdk/SharedUtil.Misc.h index 260a63b55ca..c3ed4af1a74 100644 --- a/Shared/sdk/SharedUtil.Misc.h +++ b/Shared/sdk/SharedUtil.Misc.h @@ -468,7 +468,7 @@ namespace SharedUtil }; SColor() {} - SColor(unsigned long ulValue) { ulARGB = ulValue; } + SColor(unsigned long ulValue) : ulARGB(ulValue) {} operator unsigned long() const { return ulARGB; } }; From e5eeca2531d0525d8764622638a394772e630794 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:13:06 +0200 Subject: [PATCH 20/41] Revert "refactor render item manager" This reverts commit fc3170e2445725f719fcbca72023cbc133b68df6. --- Client/core/Graphics/CRenderItemManager.cpp | 105 ++++++++++---------- 1 file changed, 51 insertions(+), 54 deletions(-) diff --git a/Client/core/Graphics/CRenderItemManager.cpp b/Client/core/Graphics/CRenderItemManager.cpp index a7397d127f5..ad532a3be33 100644 --- a/Client/core/Graphics/CRenderItemManager.cpp +++ b/Client/core/Graphics/CRenderItemManager.cpp @@ -312,7 +312,8 @@ CGuiFontItem* CRenderItemManager::CreateGuiFont(const SString& strFullFilePath, //////////////////////////////////////////////////////////////// void CRenderItemManager::NotifyContructRenderItem(CRenderItem* pItem) { - assert(!m_CreatedItemList.insert(pItem).second); // assert if item was already in the collection. + assert(!MapContains(m_CreatedItemList, pItem)); + MapInsert(m_CreatedItemList, pItem); if (CScreenSourceItem* pScreenSourceItem = DynamicCast(pItem)) m_bBackBufferCopyMaybeNeedsResize = true; @@ -327,7 +328,8 @@ void CRenderItemManager::NotifyContructRenderItem(CRenderItem* pItem) //////////////////////////////////////////////////////////////// void CRenderItemManager::NotifyDestructRenderItem(CRenderItem* pItem) { - assert(!m_CreatedItemList.insert(pItem).second); // assert if item was already in the collection. + assert(MapContains(m_CreatedItemList, pItem)); + MapRemove(m_CreatedItemList, pItem); if (CScreenSourceItem* pScreenSourceItem = DynamicCast(pItem)) m_bBackBufferCopyMaybeNeedsResize = true; @@ -449,10 +451,11 @@ void CRenderItemManager::UpdateBackBufferCopySize() m_bBackBufferCopyMaybeNeedsResize = false; // Set what the max size requirement is for the back buffer copy - uint uiSizeX = 0, uiSizeY = 0; - for (const auto item : m_CreatedItemList) + uint uiSizeX = 0; + uint uiSizeY = 0; + for (std::set::iterator iter = m_CreatedItemList.begin(); iter != m_CreatedItemList.end(); iter++) { - if (CScreenSourceItem* pScreenSourceItem = DynamicCast(item)) + if (CScreenSourceItem* pScreenSourceItem = DynamicCast(*iter)) { uiSizeX = std::max(uiSizeX, pScreenSourceItem->m_uiSizeX); uiSizeY = std::max(uiSizeY, pScreenSourceItem->m_uiSizeY); @@ -628,9 +631,7 @@ void CRenderItemManager::ChangeRenderTarget(uint uiSizeX, uint uiSizeY, IDirect3 // // CRenderItemManager::CanCreateRenderItem // -// Returns whenever a render item can be created -// depending on the current set m_TestMode -// Also checks if theres actually enough free video memory +// // //////////////////////////////////////////////////////////////// bool CRenderItemManager::CanCreateRenderItem(ClassId classId) @@ -645,22 +646,21 @@ bool CRenderItemManager::CanCreateRenderItem(ClassId classId) if (m_iMemoryKBFreeForMTA <= 0) return false; - switch (m_TestMode) - { - case DX_TEST_MODE_NO_MEM: + if (m_TestMode == DX_TEST_MODE_NO_MEM) return false; - case DX_TEST_MODE_LOW_MEM: - return (rand() % 1000) <= 750; - - default: - return true; + if (m_TestMode == DX_TEST_MODE_LOW_MEM) + { + if ((rand() % 1000) > 750) + return false; } } else if (classId == CShaderItem::GetClassId()) { - return m_TestMode != DX_TEST_MODE_NO_SHADER; + if (m_TestMode == DX_TEST_MODE_NO_SHADER) + return false; } + return true; } //////////////////////////////////////////////////////////////// @@ -687,20 +687,17 @@ void CRenderItemManager::UpdateMemoryUsage() m_iTextureMemoryKBUsed = 0; m_iRenderTargetMemoryKBUsed = 0; m_iFontMemoryKBUsed = 0; - - for (const auto pRenderItem : m_CreatedItemList) + for (std::set::iterator iter = m_CreatedItemList.begin(); iter != m_CreatedItemList.end(); iter++) { + CRenderItem* pRenderItem = *iter; if (!pRenderItem->GetIncludeInMemoryStats()) continue; - - const int iMemoryKBUsed = pRenderItem->GetVideoMemoryKBUsed(); + int iMemoryKBUsed = pRenderItem->GetVideoMemoryKBUsed(); if (pRenderItem->IsA(CFileTextureItem::GetClassId())) m_iTextureMemoryKBUsed += iMemoryKBUsed; - else if (pRenderItem->IsA(CRenderTargetItem::GetClassId()) || pRenderItem->IsA(CScreenSourceItem::GetClassId())) m_iRenderTargetMemoryKBUsed += iMemoryKBUsed; - else if (pRenderItem->IsA(CGuiFontItem::GetClassId()) || pRenderItem->IsA(CDxFontItem::GetClassId())) m_iFontMemoryKBUsed += iMemoryKBUsed; } @@ -779,20 +776,21 @@ void CRenderItemManager::GetDxStatus(SDxStatus& outStatus) } // Display color depth - outStatus.settings.b32BitColor = GetBitsPerPixel(g_pDeviceState->CreationState.PresentationParameters.BackBufferFormat) == 32; + D3DFORMAT BackBufferFormat = g_pDeviceState->CreationState.PresentationParameters.BackBufferFormat; + if (BackBufferFormat >= D3DFMT_R5G6B5 && BackBufferFormat < D3DFMT_A8R3G3B2) + outStatus.settings.b32BitColor = 0; + else + outStatus.settings.b32BitColor = 1; // Modify if using test mode - switch (m_TestMode) - { - case DX_TEST_MODE_NO_MEM: + if (m_TestMode == DX_TEST_MODE_NO_MEM) outStatus.videoMemoryKB.iFreeForMTA = 0; - case DX_TEST_MODE_LOW_MEM: + if (m_TestMode == DX_TEST_MODE_LOW_MEM) outStatus.videoMemoryKB.iFreeForMTA = 1; - case DX_TEST_MODE_NO_SHADER: + if (m_TestMode == DX_TEST_MODE_NO_SHADER) outStatus.videoCard.strPSVersion = "0"; - } } //////////////////////////////////////////////////////////////// @@ -888,23 +886,21 @@ int CRenderItemManager::GetPitchDivisor(D3DFORMAT Format) //////////////////////////////////////////////////////////////// int CRenderItemManager::CalcD3DResourceMemoryKBUsage(IDirect3DResource9* pD3DResource) { - switch (pD3DResource->GetType()) - { - case D3DRTYPE_SURFACE: + D3DRESOURCETYPE type = pD3DResource->GetType(); + + if (type == D3DRTYPE_SURFACE) return CalcD3DSurfaceMemoryKBUsage((IDirect3DSurface9*)pD3DResource); - case D3DRTYPE_TEXTURE: + if (type == D3DRTYPE_TEXTURE) return CalcD3DTextureMemoryKBUsage((IDirect3DTexture9*)pD3DResource); - case D3DRTYPE_VOLUMETEXTURE: + if (type == D3DRTYPE_VOLUMETEXTURE) return CalcD3DVolumeTextureMemoryKBUsage((IDirect3DVolumeTexture9*)pD3DResource); - case D3DRTYPE_CUBETEXTURE: + if (type == D3DRTYPE_CUBETEXTURE) return CalcD3DCubeTextureMemoryKBUsage((IDirect3DCubeTexture9*)pD3DResource); - default: - return 0; - } + return 0; } //////////////////////////////////////////////////////////////// @@ -919,10 +915,10 @@ int CRenderItemManager::CalcD3DSurfaceMemoryKBUsage(IDirect3DSurface9* pD3DSurfa D3DSURFACE_DESC surfaceDesc; pD3DSurface->GetDesc(&surfaceDesc); - const int iBitsPerPixel = GetBitsPerPixel(surfaceDesc.Format); - const int iMemoryUsedBytes = (surfaceDesc.Width * surfaceDesc.Height * iBitsPerPixel) / 8; + int iBitsPerPixel = GetBitsPerPixel(surfaceDesc.Format); + int iMemoryUsed = surfaceDesc.Width * surfaceDesc.Height / 8 * iBitsPerPixel; - return iMemoryUsedBytes / 1024; + return iMemoryUsed / 1024; } //////////////////////////////////////////////////////////////// @@ -937,7 +933,7 @@ int CRenderItemManager::CalcD3DTextureMemoryKBUsage(IDirect3DTexture9* pD3DTextu int iMemoryUsed = 0; // Calc memory usage - const int iLevelCount = pD3DTexture->GetLevelCount(); + int iLevelCount = pD3DTexture->GetLevelCount(); for (int i = 0; i < iLevelCount; i++) { D3DSURFACE_DESC surfaceDesc; @@ -959,20 +955,20 @@ int CRenderItemManager::CalcD3DTextureMemoryKBUsage(IDirect3DTexture9* pD3DTextu //////////////////////////////////////////////////////////////// int CRenderItemManager::CalcD3DVolumeTextureMemoryKBUsage(IDirect3DVolumeTexture9* pD3DVolumeTexture) { - int iMemoryUsedBytes = 0; + int iMemoryUsed = 0; // Calc memory usage - const int iLevelCount = pD3DVolumeTexture->GetLevelCount(); + int iLevelCount = pD3DVolumeTexture->GetLevelCount(); for (int i = 0; i < iLevelCount; i++) { D3DVOLUME_DESC volumeDesc; pD3DVolumeTexture->GetLevelDesc(i, &volumeDesc); int iBitsPerPixel = GetBitsPerPixel(volumeDesc.Format); - iMemoryUsedBytes += (volumeDesc.Width * volumeDesc.Height * volumeDesc.Depth * iBitsPerPixel) / 8; + iMemoryUsed += volumeDesc.Width * volumeDesc.Height * volumeDesc.Depth / 8 * iBitsPerPixel; } - return iMemoryUsedBytes / 1024; + return iMemoryUsed / 1024; } //////////////////////////////////////////////////////////////// @@ -984,20 +980,20 @@ int CRenderItemManager::CalcD3DVolumeTextureMemoryKBUsage(IDirect3DVolumeTexture //////////////////////////////////////////////////////////////// int CRenderItemManager::CalcD3DCubeTextureMemoryKBUsage(IDirect3DCubeTexture9* pD3DCubeTexture) { - int iMemoryUsedBytes = 0; + int iMemoryUsed = 0; // Calc memory usage - const int iLevelCount = pD3DCubeTexture->GetLevelCount(); + int iLevelCount = pD3DCubeTexture->GetLevelCount(); for (int i = 0; i < iLevelCount; i++) { D3DSURFACE_DESC surfaceDesc; pD3DCubeTexture->GetLevelDesc(i, &surfaceDesc); - const int iBitsPerPixel = GetBitsPerPixel(surfaceDesc.Format); - iMemoryUsedBytes += (surfaceDesc.Width * surfaceDesc.Height * iBitsPerPixel) / 8; + int iBitsPerPixel = GetBitsPerPixel(surfaceDesc.Format); + iMemoryUsed += surfaceDesc.Width * surfaceDesc.Height / 8 * iBitsPerPixel; } - return iMemoryUsedBytes * 6 / 1024; + return iMemoryUsed * 6 / 1024; } //////////////////////////////////////////////////////////////// @@ -1044,7 +1040,9 @@ void CRenderItemManager::PreDrawWorld() IDirect3DTexture9*& pReadableDepthBuffer = g_pDeviceState->MainSceneState.DepthBuffer; // Determine what is needed - const bool bRequireDepthBuffer = !m_ShadersUsingDepthBuffer.empty() && m_depthBufferFormat != RFORMAT_UNKNOWN; + bool bRequireDepthBuffer = false; + if (!m_ShadersUsingDepthBuffer.empty() && m_depthBufferFormat != RFORMAT_UNKNOWN) + bRequireDepthBuffer = true; bool bRequireNonAADisplay = false; if (g_pDeviceState->CreationState.PresentationParameters.MultiSampleType != D3DMULTISAMPLE_NONE) @@ -1072,7 +1070,6 @@ void CRenderItemManager::PreDrawWorld() m_pDevice->CreateTexture(m_uiDefaultViewportSizeX, m_uiDefaultViewportSizeY, 1, D3DUSAGE_DEPTHSTENCIL, (D3DFORMAT)m_depthBufferFormat, D3DPOOL_DEFAULT, &pReadableDepthBuffer, NULL); } - if (bRequireNonAADisplay && !m_pNonAARenderTarget) { // Create a non-AA render target and depth buffer From a86646baac0596ba9f4a3b79f3a5729012da6f94 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 06:15:36 +0200 Subject: [PATCH 21/41] Revert "Revert "refactor render item manager"" This reverts commit e5eeca2531d0525d8764622638a394772e630794. --- Client/core/Graphics/CRenderItemManager.cpp | 105 ++++++++++---------- 1 file changed, 54 insertions(+), 51 deletions(-) diff --git a/Client/core/Graphics/CRenderItemManager.cpp b/Client/core/Graphics/CRenderItemManager.cpp index ad532a3be33..a7397d127f5 100644 --- a/Client/core/Graphics/CRenderItemManager.cpp +++ b/Client/core/Graphics/CRenderItemManager.cpp @@ -312,8 +312,7 @@ CGuiFontItem* CRenderItemManager::CreateGuiFont(const SString& strFullFilePath, //////////////////////////////////////////////////////////////// void CRenderItemManager::NotifyContructRenderItem(CRenderItem* pItem) { - assert(!MapContains(m_CreatedItemList, pItem)); - MapInsert(m_CreatedItemList, pItem); + assert(!m_CreatedItemList.insert(pItem).second); // assert if item was already in the collection. if (CScreenSourceItem* pScreenSourceItem = DynamicCast(pItem)) m_bBackBufferCopyMaybeNeedsResize = true; @@ -328,8 +327,7 @@ void CRenderItemManager::NotifyContructRenderItem(CRenderItem* pItem) //////////////////////////////////////////////////////////////// void CRenderItemManager::NotifyDestructRenderItem(CRenderItem* pItem) { - assert(MapContains(m_CreatedItemList, pItem)); - MapRemove(m_CreatedItemList, pItem); + assert(!m_CreatedItemList.insert(pItem).second); // assert if item was already in the collection. if (CScreenSourceItem* pScreenSourceItem = DynamicCast(pItem)) m_bBackBufferCopyMaybeNeedsResize = true; @@ -451,11 +449,10 @@ void CRenderItemManager::UpdateBackBufferCopySize() m_bBackBufferCopyMaybeNeedsResize = false; // Set what the max size requirement is for the back buffer copy - uint uiSizeX = 0; - uint uiSizeY = 0; - for (std::set::iterator iter = m_CreatedItemList.begin(); iter != m_CreatedItemList.end(); iter++) + uint uiSizeX = 0, uiSizeY = 0; + for (const auto item : m_CreatedItemList) { - if (CScreenSourceItem* pScreenSourceItem = DynamicCast(*iter)) + if (CScreenSourceItem* pScreenSourceItem = DynamicCast(item)) { uiSizeX = std::max(uiSizeX, pScreenSourceItem->m_uiSizeX); uiSizeY = std::max(uiSizeY, pScreenSourceItem->m_uiSizeY); @@ -631,7 +628,9 @@ void CRenderItemManager::ChangeRenderTarget(uint uiSizeX, uint uiSizeY, IDirect3 // // CRenderItemManager::CanCreateRenderItem // -// +// Returns whenever a render item can be created +// depending on the current set m_TestMode +// Also checks if theres actually enough free video memory // //////////////////////////////////////////////////////////////// bool CRenderItemManager::CanCreateRenderItem(ClassId classId) @@ -646,21 +645,22 @@ bool CRenderItemManager::CanCreateRenderItem(ClassId classId) if (m_iMemoryKBFreeForMTA <= 0) return false; - if (m_TestMode == DX_TEST_MODE_NO_MEM) + switch (m_TestMode) + { + case DX_TEST_MODE_NO_MEM: return false; - if (m_TestMode == DX_TEST_MODE_LOW_MEM) - { - if ((rand() % 1000) > 750) - return false; + case DX_TEST_MODE_LOW_MEM: + return (rand() % 1000) <= 750; + + default: + return true; } } else if (classId == CShaderItem::GetClassId()) { - if (m_TestMode == DX_TEST_MODE_NO_SHADER) - return false; + return m_TestMode != DX_TEST_MODE_NO_SHADER; } - return true; } //////////////////////////////////////////////////////////////// @@ -687,17 +687,20 @@ void CRenderItemManager::UpdateMemoryUsage() m_iTextureMemoryKBUsed = 0; m_iRenderTargetMemoryKBUsed = 0; m_iFontMemoryKBUsed = 0; - for (std::set::iterator iter = m_CreatedItemList.begin(); iter != m_CreatedItemList.end(); iter++) + + for (const auto pRenderItem : m_CreatedItemList) { - CRenderItem* pRenderItem = *iter; if (!pRenderItem->GetIncludeInMemoryStats()) continue; - int iMemoryKBUsed = pRenderItem->GetVideoMemoryKBUsed(); + + const int iMemoryKBUsed = pRenderItem->GetVideoMemoryKBUsed(); if (pRenderItem->IsA(CFileTextureItem::GetClassId())) m_iTextureMemoryKBUsed += iMemoryKBUsed; + else if (pRenderItem->IsA(CRenderTargetItem::GetClassId()) || pRenderItem->IsA(CScreenSourceItem::GetClassId())) m_iRenderTargetMemoryKBUsed += iMemoryKBUsed; + else if (pRenderItem->IsA(CGuiFontItem::GetClassId()) || pRenderItem->IsA(CDxFontItem::GetClassId())) m_iFontMemoryKBUsed += iMemoryKBUsed; } @@ -776,21 +779,20 @@ void CRenderItemManager::GetDxStatus(SDxStatus& outStatus) } // Display color depth - D3DFORMAT BackBufferFormat = g_pDeviceState->CreationState.PresentationParameters.BackBufferFormat; - if (BackBufferFormat >= D3DFMT_R5G6B5 && BackBufferFormat < D3DFMT_A8R3G3B2) - outStatus.settings.b32BitColor = 0; - else - outStatus.settings.b32BitColor = 1; + outStatus.settings.b32BitColor = GetBitsPerPixel(g_pDeviceState->CreationState.PresentationParameters.BackBufferFormat) == 32; // Modify if using test mode - if (m_TestMode == DX_TEST_MODE_NO_MEM) + switch (m_TestMode) + { + case DX_TEST_MODE_NO_MEM: outStatus.videoMemoryKB.iFreeForMTA = 0; - if (m_TestMode == DX_TEST_MODE_LOW_MEM) + case DX_TEST_MODE_LOW_MEM: outStatus.videoMemoryKB.iFreeForMTA = 1; - if (m_TestMode == DX_TEST_MODE_NO_SHADER) + case DX_TEST_MODE_NO_SHADER: outStatus.videoCard.strPSVersion = "0"; + } } //////////////////////////////////////////////////////////////// @@ -886,21 +888,23 @@ int CRenderItemManager::GetPitchDivisor(D3DFORMAT Format) //////////////////////////////////////////////////////////////// int CRenderItemManager::CalcD3DResourceMemoryKBUsage(IDirect3DResource9* pD3DResource) { - D3DRESOURCETYPE type = pD3DResource->GetType(); - - if (type == D3DRTYPE_SURFACE) + switch (pD3DResource->GetType()) + { + case D3DRTYPE_SURFACE: return CalcD3DSurfaceMemoryKBUsage((IDirect3DSurface9*)pD3DResource); - if (type == D3DRTYPE_TEXTURE) + case D3DRTYPE_TEXTURE: return CalcD3DTextureMemoryKBUsage((IDirect3DTexture9*)pD3DResource); - if (type == D3DRTYPE_VOLUMETEXTURE) + case D3DRTYPE_VOLUMETEXTURE: return CalcD3DVolumeTextureMemoryKBUsage((IDirect3DVolumeTexture9*)pD3DResource); - if (type == D3DRTYPE_CUBETEXTURE) + case D3DRTYPE_CUBETEXTURE: return CalcD3DCubeTextureMemoryKBUsage((IDirect3DCubeTexture9*)pD3DResource); - return 0; + default: + return 0; + } } //////////////////////////////////////////////////////////////// @@ -915,10 +919,10 @@ int CRenderItemManager::CalcD3DSurfaceMemoryKBUsage(IDirect3DSurface9* pD3DSurfa D3DSURFACE_DESC surfaceDesc; pD3DSurface->GetDesc(&surfaceDesc); - int iBitsPerPixel = GetBitsPerPixel(surfaceDesc.Format); - int iMemoryUsed = surfaceDesc.Width * surfaceDesc.Height / 8 * iBitsPerPixel; + const int iBitsPerPixel = GetBitsPerPixel(surfaceDesc.Format); + const int iMemoryUsedBytes = (surfaceDesc.Width * surfaceDesc.Height * iBitsPerPixel) / 8; - return iMemoryUsed / 1024; + return iMemoryUsedBytes / 1024; } //////////////////////////////////////////////////////////////// @@ -933,7 +937,7 @@ int CRenderItemManager::CalcD3DTextureMemoryKBUsage(IDirect3DTexture9* pD3DTextu int iMemoryUsed = 0; // Calc memory usage - int iLevelCount = pD3DTexture->GetLevelCount(); + const int iLevelCount = pD3DTexture->GetLevelCount(); for (int i = 0; i < iLevelCount; i++) { D3DSURFACE_DESC surfaceDesc; @@ -955,20 +959,20 @@ int CRenderItemManager::CalcD3DTextureMemoryKBUsage(IDirect3DTexture9* pD3DTextu //////////////////////////////////////////////////////////////// int CRenderItemManager::CalcD3DVolumeTextureMemoryKBUsage(IDirect3DVolumeTexture9* pD3DVolumeTexture) { - int iMemoryUsed = 0; + int iMemoryUsedBytes = 0; // Calc memory usage - int iLevelCount = pD3DVolumeTexture->GetLevelCount(); + const int iLevelCount = pD3DVolumeTexture->GetLevelCount(); for (int i = 0; i < iLevelCount; i++) { D3DVOLUME_DESC volumeDesc; pD3DVolumeTexture->GetLevelDesc(i, &volumeDesc); int iBitsPerPixel = GetBitsPerPixel(volumeDesc.Format); - iMemoryUsed += volumeDesc.Width * volumeDesc.Height * volumeDesc.Depth / 8 * iBitsPerPixel; + iMemoryUsedBytes += (volumeDesc.Width * volumeDesc.Height * volumeDesc.Depth * iBitsPerPixel) / 8; } - return iMemoryUsed / 1024; + return iMemoryUsedBytes / 1024; } //////////////////////////////////////////////////////////////// @@ -980,20 +984,20 @@ int CRenderItemManager::CalcD3DVolumeTextureMemoryKBUsage(IDirect3DVolumeTexture //////////////////////////////////////////////////////////////// int CRenderItemManager::CalcD3DCubeTextureMemoryKBUsage(IDirect3DCubeTexture9* pD3DCubeTexture) { - int iMemoryUsed = 0; + int iMemoryUsedBytes = 0; // Calc memory usage - int iLevelCount = pD3DCubeTexture->GetLevelCount(); + const int iLevelCount = pD3DCubeTexture->GetLevelCount(); for (int i = 0; i < iLevelCount; i++) { D3DSURFACE_DESC surfaceDesc; pD3DCubeTexture->GetLevelDesc(i, &surfaceDesc); - int iBitsPerPixel = GetBitsPerPixel(surfaceDesc.Format); - iMemoryUsed += surfaceDesc.Width * surfaceDesc.Height / 8 * iBitsPerPixel; + const int iBitsPerPixel = GetBitsPerPixel(surfaceDesc.Format); + iMemoryUsedBytes += (surfaceDesc.Width * surfaceDesc.Height * iBitsPerPixel) / 8; } - return iMemoryUsed * 6 / 1024; + return iMemoryUsedBytes * 6 / 1024; } //////////////////////////////////////////////////////////////// @@ -1040,9 +1044,7 @@ void CRenderItemManager::PreDrawWorld() IDirect3DTexture9*& pReadableDepthBuffer = g_pDeviceState->MainSceneState.DepthBuffer; // Determine what is needed - bool bRequireDepthBuffer = false; - if (!m_ShadersUsingDepthBuffer.empty() && m_depthBufferFormat != RFORMAT_UNKNOWN) - bRequireDepthBuffer = true; + const bool bRequireDepthBuffer = !m_ShadersUsingDepthBuffer.empty() && m_depthBufferFormat != RFORMAT_UNKNOWN; bool bRequireNonAADisplay = false; if (g_pDeviceState->CreationState.PresentationParameters.MultiSampleType != D3DMULTISAMPLE_NONE) @@ -1070,6 +1072,7 @@ void CRenderItemManager::PreDrawWorld() m_pDevice->CreateTexture(m_uiDefaultViewportSizeX, m_uiDefaultViewportSizeY, 1, D3DUSAGE_DEPTHSTENCIL, (D3DFORMAT)m_depthBufferFormat, D3DPOOL_DEFAULT, &pReadableDepthBuffer, NULL); } + if (bRequireNonAADisplay && !m_pNonAARenderTarget) { // Create a non-AA render target and depth buffer From 21cf6006309922848973960eded880b6133b801d Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 10:44:16 +0200 Subject: [PATCH 22/41] Fix typo in NoifyDestructRenderItem --- Client/core/Graphics/CRenderItemManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client/core/Graphics/CRenderItemManager.cpp b/Client/core/Graphics/CRenderItemManager.cpp index a7397d127f5..79886e830de 100644 --- a/Client/core/Graphics/CRenderItemManager.cpp +++ b/Client/core/Graphics/CRenderItemManager.cpp @@ -327,7 +327,7 @@ void CRenderItemManager::NotifyContructRenderItem(CRenderItem* pItem) //////////////////////////////////////////////////////////////// void CRenderItemManager::NotifyDestructRenderItem(CRenderItem* pItem) { - assert(!m_CreatedItemList.insert(pItem).second); // assert if item was already in the collection. + assert(!m_CreatedItemList.erase(pItem) == 1); // assert if item wasnt in the collection exactly once if (CScreenSourceItem* pScreenSourceItem = DynamicCast(pItem)) m_bBackBufferCopyMaybeNeedsResize = true; From 33e551dfbad198c1941d27cbeb3e35c06f320263 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Tue, 26 May 2020 18:40:25 +0200 Subject: [PATCH 23/41] fix formatting for for loop --- Client/core/Graphics/CGraphics.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index bd2ae1a9650..d3c25b3a9af 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -672,7 +672,8 @@ float CGraphics::GetDXTextExtentW(const wchar_t* wszText, float fScale, LPD3DXFO const size_t textLength = wcslen(wszText); size_t spaceCount = 0; - for (int i = textLength - 1; i >= 0 && wszText[i] == L' '; --i) { spaceCount++; } // count spaces starting from the end + for (int i = textLength - 1; i >= 0 && wszText[i] == L' '; --i) // count spaces starting from the end + spaceCount++; // Compute the size of a single space and use that to get the width of the ignored space characters size_t trailingSpacePixels = 0; @@ -685,7 +686,6 @@ float CGraphics::GetDXTextExtentW(const wchar_t* wszText, float fScale, LPD3DXFO } RECT rect = {}; - if ((textLength - spaceCount) > 0) { pDXFont->DrawTextW(nullptr, wszText, textLength - spaceCount, &rect, DT_CALCRECT | DT_SINGLELINE, D3DCOLOR_XRGB(0, 0, 0)); @@ -732,7 +732,8 @@ int CGraphics::GetTrailingSpacesWidth(ID3DXFont* pDXFont, WString& strText) { // Count the amount of space characters at the end int iSpaceCount = 0; - for (auto c = strText.rbegin(); c != strText.rend() && *c == ' '; ++c) { iSpaceCount++; } // count from the end + for (auto c = strText.rbegin(); c != strText.rend() && *c == ' '; ++c) // count from the end + iSpaceCount++; // Compute the size of a single space and use that // to get the width of the ignored space characters From 8b88eacab375163b8ccbc49c4bcfffcdb7122cf0 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Wed, 27 May 2020 00:04:35 +0200 Subject: [PATCH 24/41] Fix indentation in ResizeTextureData --- Client/core/Graphics/CGraphics.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index d3c25b3a9af..09f9829ac47 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -2267,30 +2267,28 @@ bool CGraphics::ResizeTextureData(const void* pData, uint uiDataPitch, uint uiWi IDirect3DSurface9* pCurrentSurface = NULL; IDirect3DSurface9* pNewSurface = NULL; - - bool bResult = false; - // Create surfaces - if (FAILED(g_pGraphics->GetDevice()->CreateOffscreenPlainSurface(uiWidth, uiHeight, (D3DFORMAT)d3dFormat, D3DPOOL_SCRATCH, &pCurrentSurface, NULL))) + // Create surfaces + if (FAILED(g_pGraphics->GetDevice()->CreateOffscreenPlainSurface(uiWidth, uiHeight, (D3DFORMAT)d3dFormat, D3DPOOL_SCRATCH, &pCurrentSurface, NULL))) goto cleanup_and_return; - if (FAILED(g_pGraphics->GetDevice()->CreateOffscreenPlainSurface(uiNewWidth, uiNewHeight, (D3DFORMAT)d3dFormat, D3DPOOL_SCRATCH, &pNewSurface, NULL))) + if (FAILED(g_pGraphics->GetDevice()->CreateOffscreenPlainSurface(uiNewWidth, uiNewHeight, (D3DFORMAT)d3dFormat, D3DPOOL_SCRATCH, &pNewSurface, NULL))) goto cleanup_and_return; - // Data in - if (!CopyDataToSurface(pCurrentSurface, (const BYTE*)pData, uiDataPitch)) + // Data in + if (!CopyDataToSurface(pCurrentSurface, (const BYTE*)pData, uiDataPitch)) goto cleanup_and_return; - // Resize - if (FAILED(D3DXLoadSurfaceFromSurface(pNewSurface, NULL, NULL, pCurrentSurface, NULL, NULL, D3DX_FILTER_TRIANGLE, 0))) + // Resize + if (FAILED(D3DXLoadSurfaceFromSurface(pNewSurface, NULL, NULL, pCurrentSurface, NULL, NULL, D3DX_FILTER_TRIANGLE, 0))) goto cleanup_and_return; - // Data out - if (!CopyDataFromSurface(pNewSurface, outBuffer)) + // Data out + if (!CopyDataFromSurface(pNewSurface, outBuffer)) goto cleanup_and_return; - bResult = true; + bResult = true; cleanup_and_return: From bbf5a38be04ba949a3ef9831bf8709af04d14818 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Wed, 27 May 2020 01:34:35 +0200 Subject: [PATCH 25/41] refactor CVector4D --- Shared/sdk/CVector4D.h | 97 ++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 65 deletions(-) diff --git a/Shared/sdk/CVector4D.h b/Shared/sdk/CVector4D.h index 0f18c73af4b..346681f5fa2 100644 --- a/Shared/sdk/CVector4D.h +++ b/Shared/sdk/CVector4D.h @@ -20,15 +20,15 @@ class CVector4D { public: - constexpr CVector4D() : - fX(0), - fY(0), - fZ(0), - fW(0) - { - }; + float fX = 0.0f, fY = 0.0f, fZ = 0.0f, fW = 0.0f; + + constexpr CVector4D() noexcept = default; + + constexpr CVector4D(const CVector4D&) noexcept = default; - constexpr CVector4D(float x, float y, float z, float w) : + constexpr CVector4D& operator=(const CVector4D&) noexcept = default; + + constexpr CVector4D(float x, float y, float z, float w) noexcept : fX(x), fY(y), fZ(z), @@ -36,32 +36,16 @@ class CVector4D { } - constexpr CVector4D(const CVector4D& vec) : - fX(vec.fX), - fY(vec.fY), - fZ(vec.fZ), - fW(vec.fW) - { - } + constexpr float DotProduct(const CVector4D& other) const noexcept { return fX * other.fX + fY * other.fY + fZ * other.fZ; } - CVector4D& operator=(const CVector4D& vec) - { - fX = vec.fX; - fY = vec.fY; - fZ = vec.fZ; - fW = vec.fW; - return *this; - } - - float DotProduct(CVector4D& other) const { return fX * other.fX + fY * other.fY + fZ * other.fZ; } + float Length() const noexcept { return sqrt(fX * fX + fY * fY + fZ * fZ + fW * fW); } - float Length() const { return sqrt(fX * fX + fY * fY + fZ * fZ + fW * fW); } + // returns just the squared length(eg.: x*x* + y*y + z*z + w*w) + constexpr float LengthSquared() const noexcept { return (fX * fX) + (fY * fY) + (fZ * fZ) + (fW * fW); } - float LengthSquared() const { return (fX * fX) + (fY * fY) + (fZ * fZ) + (fW * fW); } - - void Normalize() + void Normalize() noexcept { - float fLength = Length(); + const float fLength = Length(); if (fLength > 0.0f) { fX /= fLength; @@ -71,23 +55,22 @@ class CVector4D } } - constexpr CVector4D operator*(float fRight) const { return CVector4D(fX * fRight, fY * fRight, fZ * fRight, fW * fRight); } + constexpr CVector4D operator*(const CVector4D& vecRight) const { return CVector4D(fX * vecRight.fX, fY * vecRight.fY, fZ * vecRight.fZ, fW * vecRight.fW); } + + constexpr CVector4D operator*(const float fRight) const noexcept { return CVector4D(fX * fRight, fY * fRight, fZ * fRight, fW * fRight); } - constexpr CVector4D operator/(float fRight) const - { - float fRcpValue = 1 / fRight; - return CVector4D(fX * fRcpValue, fY * fRcpValue, fZ * fRcpValue, fW * fRcpValue); - } - constexpr CVector4D operator+(const CVector4D& vecRight) const { return CVector4D(fX + vecRight.fX, fY + vecRight.fY, fZ + vecRight.fZ, fW + vecRight.fW); } + constexpr CVector4D operator+(const CVector4D& vecRight) const noexcept { return CVector4D(fX + vecRight.fX, fY + vecRight.fY, fZ + vecRight.fZ, fW + vecRight.fW); } - constexpr CVector4D operator-(const CVector4D& vecRight) const { return CVector4D(fX - vecRight.fX, fY - vecRight.fY, fZ - vecRight.fZ, fW - vecRight.fW); } + constexpr CVector4D operator-(const CVector4D& vecRight) const noexcept { return CVector4D(fX - vecRight.fX, fY - vecRight.fY, fZ - vecRight.fZ, fW - vecRight.fW); } - constexpr CVector4D operator*(const CVector4D& vecRight) const { return CVector4D(fX * vecRight.fX, fY * vecRight.fY, fZ * vecRight.fZ, fW * vecRight.fW); } - constexpr CVector4D operator/(const CVector4D& vecRight) const { return CVector4D(fX / vecRight.fX, fY / vecRight.fY, fZ / vecRight.fZ, fW / vecRight.fW); } + constexpr CVector4D operator/(const CVector4D& vecRight) const noexcept { return CVector4D(fX / vecRight.fX, fY / vecRight.fY, fZ / vecRight.fZ, fW / vecRight.fW); } + + constexpr CVector4D operator/(const float fRight) const noexcept { return CVector4D(fX / fRight, fY / fRight, fZ / fRight, fW / fRight); } + - void operator+=(float fRight) + constexpr void operator+=(const float fRight) noexcept { fX += fRight; fY += fRight; @@ -95,7 +78,7 @@ class CVector4D fW += fRight; } - void operator+=(const CVector4D& vecRight) + constexpr void operator+=(const CVector4D& vecRight) noexcept { fX += vecRight.fX; fY += vecRight.fY; @@ -103,7 +86,7 @@ class CVector4D fW += vecRight.fW; } - void operator-=(float fRight) + constexpr void operator-=(const float fRight) noexcept { fX -= fRight; fY -= fRight; @@ -111,7 +94,7 @@ class CVector4D fW -= fRight; } - void operator-=(const CVector4D& vecRight) + constexpr void operator-=(const CVector4D& vecRight) noexcept { fX -= vecRight.fX; fY -= vecRight.fY; @@ -119,7 +102,7 @@ class CVector4D fW -= vecRight.fW; } - void operator*=(float fRight) + constexpr void operator*=(const float fRight) noexcept { fX *= fRight; fY *= fRight; @@ -127,15 +110,8 @@ class CVector4D fW *= fRight; } - void operator*=(const CVector4D& vecRight) - { - fX *= vecRight.fX; - fY *= vecRight.fY; - fZ *= vecRight.fZ; - fW *= vecRight.fW; - } - void operator/=(float fRight) + constexpr void operator/=(const float fRight) noexcept { fX /= fRight; fY /= fRight; @@ -143,7 +119,7 @@ class CVector4D fW /= fRight; } - void operator/=(const CVector4D& vecRight) + constexpr void operator/=(const CVector4D& vecRight) noexcept { fX /= vecRight.fX; fY /= vecRight.fY; @@ -151,20 +127,11 @@ class CVector4D fW /= vecRight.fW; } - bool operator==(const CVector4D& param) const + constexpr bool operator==(const CVector4D& param) const noexcept { return ((fabs(fX - param.fX) < FLOAT_EPSILON) && (fabs(fY - param.fY) < FLOAT_EPSILON) && (fabs(fZ - param.fZ) < FLOAT_EPSILON) && (fabs(fW - param.fW) < FLOAT_EPSILON)); } - bool operator!=(const CVector4D& param) const - { - return ((fabs(fX - param.fX) >= FLOAT_EPSILON) || (fabs(fY - param.fY) >= FLOAT_EPSILON) || (fabs(fZ - param.fZ) >= FLOAT_EPSILON) || - (fabs(fW - param.fW) >= FLOAT_EPSILON)); - } - - float fX; - float fY; - float fZ; - float fW; + constexpr bool operator!=(const CVector4D& param) const noexcept { return !(*this == param); } }; From 9045fe03cb8217ec99386ac100bbc05971768f65 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Wed, 27 May 2020 01:34:38 +0200 Subject: [PATCH 26/41] refactor CVector2D --- Shared/sdk/CVector2D.h | 63 +++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 34 deletions(-) diff --git a/Shared/sdk/CVector2D.h b/Shared/sdk/CVector2D.h index 759f1f098fb..e29fa474595 100644 --- a/Shared/sdk/CVector2D.h +++ b/Shared/sdk/CVector2D.h @@ -20,55 +20,53 @@ class CVector2D { public: + float fX = 0.0f, fY = 0.0f; - constexpr CVector2D() : - fX(0), - fY(0) - { - }; + constexpr CVector2D() noexcept = default; - constexpr CVector2D(float x, float y) : + constexpr CVector2D(float x, float y) noexcept : fX(x), fY(y) { } - constexpr CVector2D(const CVector& vec) : + constexpr CVector2D(const CVector& vec) noexcept : fX(vec.fX), fY(vec.fY) { } - constexpr CVector2D(const CVector4D& vec) : + constexpr CVector2D(const CVector4D& vec) noexcept : fX(vec.fX), fY(vec.fY) { } - CVector2D& operator=(const CVector& vec) + constexpr CVector2D& operator=(const CVector& vec) noexcept { fX = vec.fX; fY = vec.fY; return *this; } - CVector2D& operator=(const CVector4D& vec) + constexpr CVector2D& operator=(const CVector4D& vec) noexcept { fX = vec.fX; fY = vec.fY; return *this; } - float DotProduct(CVector2D& other) const { return fX * other.fX + fY * other.fY; } + constexpr float DotProduct(CVector2D& other) const { return fX * other.fX + fY * other.fY; } - float Length() const { return sqrt(fX * fX + fY * fY); } + float Length() const { return std::hypotf(fX, fY); } - float LengthSquared() const { return (fX * fX) + (fY * fY); } + // returns just the squared length(eg.: x*x* + y*y) + constexpr float LengthSquared() const noexcept { return (fX * fX) + (fY * fY); } - void Normalize() + inline void Normalize() noexcept { - float fLength = Length(); + const float fLength = Length(); if (fLength > 0.0f) { fX /= fLength; @@ -76,70 +74,67 @@ class CVector2D } } - constexpr CVector2D operator*(float fRight) const { return CVector2D(fX * fRight, fY * fRight); } + constexpr CVector2D operator*(float fRight) const noexcept { return CVector2D(fX * fRight, fY * fRight); } - constexpr CVector2D operator/(float fRight) const { return CVector2D(fX / fRight, fY / fRight); } + constexpr CVector2D operator/(float fRight) const noexcept { return CVector2D(fX / fRight, fY / fRight); } - constexpr CVector2D operator+(const CVector2D& vecRight) const { return CVector2D(fX + vecRight.fX, fY + vecRight.fY); } + constexpr CVector2D operator+(const CVector2D& vecRight) const noexcept { return CVector2D(fX + vecRight.fX, fY + vecRight.fY); } - constexpr CVector2D operator-(const CVector2D& vecRight) const { return CVector2D(fX - vecRight.fX, fY - vecRight.fY); } + constexpr CVector2D operator-(const CVector2D& vecRight) const noexcept { return CVector2D(fX - vecRight.fX, fY - vecRight.fY); } - constexpr CVector2D operator*(const CVector2D& vecRight) const { return CVector2D(fX * vecRight.fX, fY * vecRight.fY); } + constexpr CVector2D operator*(const CVector2D& vecRight) const noexcept { return CVector2D(fX * vecRight.fX, fY * vecRight.fY); } - constexpr CVector2D operator/(const CVector2D& vecRight) const { return CVector2D(fX / vecRight.fX, fY / vecRight.fY); } + constexpr CVector2D operator/(const CVector2D& vecRight) const noexcept { return CVector2D(fX / vecRight.fX, fY / vecRight.fY); } - void operator+=(float fRight) + constexpr void operator+=(float fRight) noexcept { fX += fRight; fY += fRight; } - void operator+=(const CVector2D& vecRight) + constexpr void operator+=(const CVector2D& vecRight) noexcept { fX += vecRight.fX; fY += vecRight.fY; } - void operator-=(float fRight) + constexpr void operator-=(float fRight) noexcept { fX -= fRight; fY -= fRight; } - void operator-=(const CVector2D& vecRight) + constexpr void operator-=(const CVector2D& vecRight) noexcept { fX -= vecRight.fX; fY -= vecRight.fY; } - void operator*=(float fRight) + constexpr void operator*=(float fRight) noexcept { fX *= fRight; fY *= fRight; } - void operator*=(const CVector2D& vecRight) + constexpr void operator*=(const CVector2D& vecRight) noexcept { fX *= vecRight.fX; fY *= vecRight.fY; } - void operator/=(float fRight) + constexpr void operator/=(float fRight) noexcept { fX /= fRight; fY /= fRight; } - void operator/=(const CVector2D& vecRight) + constexpr void operator/=(const CVector2D& vecRight) noexcept { fX /= vecRight.fX; fY /= vecRight.fY; } - bool operator==(const CVector2D& param) const { return ((fabs(fX - param.fX) < FLOAT_EPSILON) && (fabs(fY - param.fY) < FLOAT_EPSILON)); } - - bool operator!=(const CVector2D& param) const { return !(*this == param); } + bool operator==(const CVector2D& param) const noexcept { return ((fabs(fX - param.fX) < FLOAT_EPSILON) && (fabs(fY - param.fY) < FLOAT_EPSILON)); } - float fX; - float fY; + bool operator!=(const CVector2D& param) const noexcept { return !(*this == param); } }; From 1084b4d019b9eef5ad26469b9b9f59120ebb6072 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Wed, 27 May 2020 01:34:40 +0200 Subject: [PATCH 27/41] refactor CVector --- Shared/sdk/CVector.h | 132 ++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 72 deletions(-) diff --git a/Shared/sdk/CVector.h b/Shared/sdk/CVector.h index bb5d18fc4b4..0e600ff9872 100644 --- a/Shared/sdk/CVector.h +++ b/Shared/sdk/CVector.h @@ -26,14 +26,9 @@ class CVector { public: - float fX, fY, fZ; + float fX = 0.0f, fY = 0.0f, fZ = 0.0f; - constexpr CVector() : - fX(0), - fY(0), - fZ(0) - { - }; + constexpr CVector() = default; constexpr CVector(float x, float y, float z) : fX(x), @@ -42,25 +37,33 @@ class CVector { } - constexpr CVector(const CVector4D& vec) : + constexpr CVector(const CVector4D& vec) noexcept : fX(vec.fX), fY(vec.fY), fZ(vec.fZ) { } - // returns the length of the vector as well + constexpr CVector& operator=(const CVector4D& vec) noexcept + { + fX = vec.fX; + fY = vec.fY; + fZ = vec.fZ; + return *this; + } + + + constexpr CVector Clone() const { return *this; } + + // returns the length of the vector and normalizes it float Normalize() { - const float t = sqrt(fX * fX + fY * fY + fZ * fZ); + const float t = Length(); if (t > FLOAT_EPSILON) { - // stupid hack: mul. is faster than div............. - const float fRcpt = 1 / t; - - fX *= fRcpt; - fY *= fRcpt; - fZ *= fRcpt; + fX /= t; + fY /= t; + fZ /= t; return t; } @@ -68,15 +71,16 @@ class CVector return 0; } - float Length() const { return sqrt((fX * fX) + (fY * fY) + (fZ * fZ)); } + inline float Length() const { return sqrt((fX * fX) + (fY * fY) + (fZ * fZ)); } - float LengthSquared() const { return (fX * fX) + (fY * fY) + (fZ * fZ); } + // returns just the squared length(eg.: x*x* + y*y + z*z) + inline float LengthSquared() const { return (fX * fX) + (fY * fY) + (fZ * fZ); } - float DotProduct(const CVector* param) const { return fX * param->fX + fY * param->fY + fZ * param->fZ; } + inline float DotProduct(const CVector* param) const { return fX * param->fX + fY * param->fY + fZ * param->fZ; } void CrossProduct(const CVector* param) { - float _fX = fX, _fY = fY, _fZ = fZ; + const float _fX = fX, _fY = fY, _fZ = fZ; fX = _fY * param->fZ - param->fY * _fZ; fY = _fZ * param->fX - param->fZ * _fX; fZ = _fX * param->fY - param->fX * _fY; @@ -87,7 +91,7 @@ class CVector { CVector vecRotation; vecRotation.fZ = atan2(fY, fX); - CVector vecTemp(sqrt(fX * fX + fY * fY), fZ, 0); + CVector vecTemp(std::hypotf(fX, fY), fZ, 0); vecTemp.Normalize(); vecRotation.fY = atan2(vecTemp.fX, vecTemp.fY) - PI / 2; return vecRotation; @@ -101,25 +105,16 @@ class CVector vecResult = CVector(fZ, 0, -fX); else vecResult = CVector(0, -fZ, fY); - vecResult.Normalize(); - return vecResult; - } - CVector Clone() const - { - CVector vecResult; - vecResult.fX = fX; - vecResult.fY = fY; - vecResult.fZ = fZ; + vecResult.Normalize(); return vecResult; } // Intersections code based on https://github.com/juj/MathGeoLib/blob/master/src/Geometry/Plane.h - bool IntesectsLinePlane(const CVector& vecRay, const CVector& vecNormal, const CVector& vecPosition, float* fOutDist) const + bool IntesectsLinePlane(const CVector& vecRay, const CVector& vecNormal, const CVector& vecPosition, float* fOutDist) const noexcept { - bool bIntersects = false; + const float fDenom = vecNormal.DotProduct(&vecRay); - float fDenom = vecNormal.DotProduct(&vecRay); if (fabs(fDenom) > 1e-4f) { *fOutDist = (vecPosition.Length() - vecNormal.DotProduct(this)) / fDenom; @@ -136,12 +131,13 @@ class CVector return fabs(vecNormal.DotProduct(this) - vecPosition.Length()) < 1e-3f;; } - bool IntersectsSegmentPlane(const CVector& vecSegment, const CVector& vecNormal, const CVector& vecPosition, CVector* outVec) const + bool IntersectsSegmentPlane(const CVector& vecSegment, const CVector& vecNormal, const CVector& vecPosition, CVector* outVec) const noexcept { float fDist; CVector vecRay = vecSegment; vecRay.Normalize(); bool bIntersects = IntesectsLinePlane(vecRay, vecNormal, vecPosition, &fDist); + const float fSegLength = vecSegment.Length(); *outVec = *this + vecRay * fDist; @@ -149,9 +145,9 @@ class CVector } // https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm - bool IntersectsSegmentTriangle(const CVector& vecSegment, const CVector& vecVert1, const CVector& vecVert2, const CVector& vecVert3, CVector* outVec) const + bool IntersectsSegmentTriangle(const CVector& vecSegment, const CVector& vecVert1, const CVector& vecVert2, const CVector& vecVert3, CVector* outVec) const noexcept { - const float fEpsilon = 1e-6f; + constexpr float fEpsilon = 1e-6f; CVector vecEdge1, vecEdge2, h, s; float a, f, u, v; @@ -196,96 +192,88 @@ class CVector return false; } - CVector operator+(const CVector& vecRight) const { return CVector(fX + vecRight.fX, fY + vecRight.fY, fZ + vecRight.fZ); } + constexpr CVector operator+(const CVector& vecRight) const noexcept { return CVector(fX + vecRight.fX, fY + vecRight.fY, fZ + vecRight.fZ); } - CVector operator-(const CVector& vecRight) const { return CVector(fX - vecRight.fX, fY - vecRight.fY, fZ - vecRight.fZ); } - CVector operator*(const CVector& vecRight) const { return CVector(fX * vecRight.fX, fY * vecRight.fY, fZ * vecRight.fZ); } + constexpr CVector operator-(const CVector& vecRight) const noexcept { return CVector(fX - vecRight.fX, fY - vecRight.fY, fZ - vecRight.fZ); } - CVector operator*(float fRight) const { return CVector(fX * fRight, fY * fRight, fZ * fRight); } + constexpr CVector operator-() const noexcept { return CVector(-fX, -fY, -fZ); } - CVector operator/(const CVector& vecRight) const { return CVector(fX / vecRight.fX, fY / vecRight.fY, fZ / vecRight.fZ); } - CVector operator/(float fRight) const - { - float fRcpValue = 1 / fRight; - return CVector(fX * fRcpValue, fY * fRcpValue, fZ * fRcpValue); - } + constexpr CVector operator*(const CVector& vecRight) const noexcept { return CVector(fX * vecRight.fX, fY * vecRight.fY, fZ * vecRight.fZ); } + + constexpr CVector operator*(const float fRight) const noexcept { return CVector(fX * fRight, fY * fRight, fZ * fRight); } + + + constexpr CVector operator/(const CVector& vecRight) const noexcept { return CVector(fX / vecRight.fX, fY / vecRight.fY, fZ / vecRight.fZ); } + + constexpr CVector operator/(const float fRight) const noexcept { return CVector(fX / fRight, fY / fRight, fZ / fRight); } - CVector operator-() const { return CVector(-fX, -fY, -fZ); } - void operator+=(float fRight) + constexpr void operator+=(const float fRight) noexcept { fX += fRight; fY += fRight; fZ += fRight; } - void operator+=(const CVector& vecRight) + constexpr void operator+=(const CVector& vecRight) noexcept { fX += vecRight.fX; fY += vecRight.fY; fZ += vecRight.fZ; } - void operator-=(float fRight) + + constexpr void operator-=(const float fRight) noexcept { fX -= fRight; fY -= fRight; fZ -= fRight; } - void operator-=(const CVector& vecRight) + constexpr void operator-=(const CVector& vecRight) noexcept { fX -= vecRight.fX; fY -= vecRight.fY; fZ -= vecRight.fZ; } - void operator*=(float fRight) + + constexpr void operator*=(const float fRight) noexcept { fX *= fRight; fY *= fRight; fZ *= fRight; } - void operator*=(const CVector& vecRight) + constexpr void operator*=(const CVector& vecRight) noexcept { fX *= vecRight.fX; fY *= vecRight.fY; fZ *= vecRight.fZ; } - void operator/=(float fRight) + + constexpr void operator/=(const float fRight) noexcept { - float fRcpValue = 1 / fRight; - fX *= fRcpValue; - fY *= fRcpValue; - fZ *= fRcpValue; + fX /= fRight; + fY /= fRight; + fZ /= fRight; } - void operator/=(const CVector& vecRight) + constexpr void operator/=(const CVector& vecRight) noexcept { fX /= vecRight.fX; fY /= vecRight.fY; fZ /= vecRight.fZ; } - bool operator==(const CVector& param) const - { - return ((fabs(fX - param.fX) < FLOAT_EPSILON) && (fabs(fY - param.fY) < FLOAT_EPSILON) && (fabs(fZ - param.fZ) < FLOAT_EPSILON)); - } - bool operator!=(const CVector& param) const + inline bool operator==(const CVector& param) const noexcept { - return ((fabs(fX - param.fX) >= FLOAT_EPSILON) || (fabs(fY - param.fY) >= FLOAT_EPSILON) || (fabs(fZ - param.fZ) >= FLOAT_EPSILON)); + return ((fabs(fX - param.fX) < FLOAT_EPSILON) && (fabs(fY - param.fY) < FLOAT_EPSILON) && (fabs(fZ - param.fZ) < FLOAT_EPSILON)); } - CVector& operator=(const CVector4D& vec) - { - fX = vec.fX; - fY = vec.fY; - fZ = vec.fZ; - return *this; - } + inline bool operator!=(const CVector& param) const noexcept { return !(*this == param); } }; From a434d395f38c06541b53d8a2f35e58c8196e1b66 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Wed, 27 May 2020 01:38:32 +0200 Subject: [PATCH 28/41] Restore removed if check in, and do some cleanup in ::CheckModes --- Client/core/Graphics/CGraphics.cpp | 80 +++++++++++++++++------------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 09f9829ac47..55b1f0d8168 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -485,49 +485,59 @@ void CGraphics::EndDrawBatch() // void CGraphics::CheckModes(EDrawModeType newDrawMode, EBlendModeType newBlendMode) { - if (m_CurDrawMode != newDrawMode || m_CurBlendMode != newBlendMode) { - // Flush old - switch (m_CurDrawMode) - { - case EDrawMode::DX_SPRITE: - { - m_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); - m_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); - m_pDXSprite->End(); - break; - } - case EDrawMode::DX_LINE: - m_pLineInterface->End(); - break; + const bool bDrawModeChanging = (m_CurDrawMode != newDrawMode); + const bool bBlendModeChanging = (m_CurBlendMode != newBlendMode); - case EDrawMode::TILE_BATCHER: - m_pTileBatcher->Flush(); - break; + // Draw mode changing? + if (!bDrawModeChanging && !bBlendModeChanging) + return; + } - case EDrawMode::PRIMITIVE: - m_pPrimitiveBatcher->Flush(); - break; - case EDrawMode::PRIMITIVE_MATERIAL: - m_pPrimitiveMaterialBatcher->Flush(); - break; - } + // Flush old + switch (m_CurDrawMode) + { + case EDrawMode::DX_SPRITE: + { + m_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); + m_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); + m_pDXSprite->End(); + break; + } + case EDrawMode::DX_LINE: + m_pLineInterface->End(); + break; - switch (newDrawMode) - { - case EDrawMode::DX_SPRITE: - m_pDXSprite->Begin(D3DXSPRITE_DONOTSAVESTATE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE); - break; + case EDrawMode::TILE_BATCHER: + m_pTileBatcher->Flush(); + break; - case EDrawMode::DX_LINE: - m_pLineInterface->Begin(); - } + case EDrawMode::PRIMITIVE: + m_pPrimitiveBatcher->Flush(); + break; - SetBlendModeRenderStates(newBlendMode); - m_CurBlendMode = newBlendMode; - m_CurDrawMode = newDrawMode; + case EDrawMode::PRIMITIVE_MATERIAL: + m_pPrimitiveMaterialBatcher->Flush(); + break; } + + switch (newDrawMode) + { + case EDrawMode::DX_SPRITE: + m_pDXSprite->Begin(D3DXSPRITE_DONOTSAVESTATE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE); + break; + + case EDrawMode::DX_LINE: + m_pLineInterface->Begin(); + break; + + } + + SetBlendModeRenderStates(newBlendMode); + m_CurBlendMode = newBlendMode; + m_CurDrawMode = newDrawMode; + } void CGraphics::CalcWorldCoors(CVector* vecScreen, CVector* vecWorld) From df84b07d569498e9c909fc194205c543c8ced35a Mon Sep 17 00:00:00 2001 From: Pirulax Date: Wed, 27 May 2020 01:48:47 +0200 Subject: [PATCH 29/41] Fix indentation in for loop in GetTrailingSpacesWidth --- Client/core/Graphics/CGraphics.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 55b1f0d8168..43971222538 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -743,7 +743,7 @@ int CGraphics::GetTrailingSpacesWidth(ID3DXFont* pDXFont, WString& strText) // Count the amount of space characters at the end int iSpaceCount = 0; for (auto c = strText.rbegin(); c != strText.rend() && *c == ' '; ++c) // count from the end - iSpaceCount++; + iSpaceCount++; // Compute the size of a single space and use that // to get the width of the ignored space characters From 79412f6d7042716d11f81ccb6e55da8ef9493bc8 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Fri, 29 May 2020 04:58:18 +0200 Subject: [PATCH 30/41] Change space counting loop to be more readable --- Client/core/Graphics/CGraphics.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 43971222538..982bcd42a90 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -531,13 +531,11 @@ void CGraphics::CheckModes(EDrawModeType newDrawMode, EBlendModeType newBlendMod case EDrawMode::DX_LINE: m_pLineInterface->Begin(); break; - } SetBlendModeRenderStates(newBlendMode); m_CurBlendMode = newBlendMode; - m_CurDrawMode = newDrawMode; - + m_CurDrawMode = newDrawMode; } void CGraphics::CalcWorldCoors(CVector* vecScreen, CVector* vecWorld) @@ -681,9 +679,15 @@ float CGraphics::GetDXTextExtentW(const wchar_t* wszText, float fScale, LPD3DXFO // Count the amount of space characters at the end const size_t textLength = wcslen(wszText); + // count spaces starting from the end until the first non-whitespace char size_t spaceCount = 0; - for (int i = textLength - 1; i >= 0 && wszText[i] == L' '; --i) // count spaces starting from the end + for (int i = textLength - 1; i >= 0; --i) + { + if (wszText[i] != L' ') + break; + spaceCount++; + } // Compute the size of a single space and use that to get the width of the ignored space characters size_t trailingSpacePixels = 0; @@ -740,10 +744,15 @@ void CGraphics::GetDXTextSize(CVector2D& vecSize, const char* szText, float fWid int CGraphics::GetTrailingSpacesWidth(ID3DXFont* pDXFont, WString& strText) { - // Count the amount of space characters at the end + // count spaces starting from the end until the first non-whitespace char int iSpaceCount = 0; - for (auto c = strText.rbegin(); c != strText.rend() && *c == ' '; ++c) // count from the end + for (auto iter = strText.rbegin(); iter != strText.rend(); iter++) + { + if (*iter != L' ') + break; + iSpaceCount++; + } // Compute the size of a single space and use that // to get the width of the ignored space characters From 88322b59b43be1f95a6c6869afd122182540a4ca Mon Sep 17 00:00:00 2001 From: Pirulax Date: Fri, 29 May 2020 05:03:28 +0200 Subject: [PATCH 31/41] CrenderItemManager apply ccw's assert sugg, also revert changes at 1044 --- Client/core/Graphics/CRenderItemManager.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Client/core/Graphics/CRenderItemManager.cpp b/Client/core/Graphics/CRenderItemManager.cpp index 79886e830de..b8adae692c6 100644 --- a/Client/core/Graphics/CRenderItemManager.cpp +++ b/Client/core/Graphics/CRenderItemManager.cpp @@ -312,7 +312,8 @@ CGuiFontItem* CRenderItemManager::CreateGuiFont(const SString& strFullFilePath, //////////////////////////////////////////////////////////////// void CRenderItemManager::NotifyContructRenderItem(CRenderItem* pItem) { - assert(!m_CreatedItemList.insert(pItem).second); // assert if item was already in the collection. + const bool wasInserted = m_CreatedItemList.insert(pItem).second; + assert(!wasInserted); // assert if item was already in the collection. if (CScreenSourceItem* pScreenSourceItem = DynamicCast(pItem)) m_bBackBufferCopyMaybeNeedsResize = true; @@ -449,7 +450,8 @@ void CRenderItemManager::UpdateBackBufferCopySize() m_bBackBufferCopyMaybeNeedsResize = false; // Set what the max size requirement is for the back buffer copy - uint uiSizeX = 0, uiSizeY = 0; + uint uiSizeX = 0; + uint uiSizeY = 0; for (const auto item : m_CreatedItemList) { if (CScreenSourceItem* pScreenSourceItem = DynamicCast(item)) @@ -1044,7 +1046,9 @@ void CRenderItemManager::PreDrawWorld() IDirect3DTexture9*& pReadableDepthBuffer = g_pDeviceState->MainSceneState.DepthBuffer; // Determine what is needed - const bool bRequireDepthBuffer = !m_ShadersUsingDepthBuffer.empty() && m_depthBufferFormat != RFORMAT_UNKNOWN; + bool bRequireDepthBuffer = false; + if (!m_ShadersUsingDepthBuffer.empty() && m_depthBufferFormat != RFORMAT_UNKNOWN) + bRequireDepthBuffer = true; bool bRequireNonAADisplay = false; if (g_pDeviceState->CreationState.PresentationParameters.MultiSampleType != D3DMULTISAMPLE_NONE) From caa193fd8b645dc9d46bd6bfcb747ed0f8c2839d Mon Sep 17 00:00:00 2001 From: Pirulax Date: Fri, 29 May 2020 05:05:45 +0200 Subject: [PATCH 32/41] apply changes to CVector4D, also add comment that DotProduct is returning the wrong value --- Shared/sdk/CVector4D.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Shared/sdk/CVector4D.h b/Shared/sdk/CVector4D.h index 346681f5fa2..02226ae5085 100644 --- a/Shared/sdk/CVector4D.h +++ b/Shared/sdk/CVector4D.h @@ -20,7 +20,10 @@ class CVector4D { public: - float fX = 0.0f, fY = 0.0f, fZ = 0.0f, fW = 0.0f; + float fX = 0.0f; + float fY = 0.0f; + float fZ = 0.0f; + float fW = 0.0f; constexpr CVector4D() noexcept = default; @@ -36,6 +39,8 @@ class CVector4D { } + // Warning, this function is returning the wrong value(fW is missing), its kept because nothing uses it, only + // CLuaVector4DDefs. constexpr float DotProduct(const CVector4D& other) const noexcept { return fX * other.fX + fY * other.fY + fZ * other.fZ; } float Length() const noexcept { return sqrt(fX * fX + fY * fY + fZ * fZ + fW * fW); } From b060c22686689a23ec43724b4c051ee61e455431 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Fri, 29 May 2020 05:06:33 +0200 Subject: [PATCH 33/41] Apply changes to CVector: declare x, y, z on separate lines --- Shared/sdk/CVector.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Shared/sdk/CVector.h b/Shared/sdk/CVector.h index 0e600ff9872..3fdf5accaac 100644 --- a/Shared/sdk/CVector.h +++ b/Shared/sdk/CVector.h @@ -26,7 +26,9 @@ class CVector { public: - float fX = 0.0f, fY = 0.0f, fZ = 0.0f; + float fX = 0.0f; + float fY = 0.0f; + float fZ = 0.0f; constexpr CVector() = default; From 0ff08133042810b378953983f402e0ddca3a929e Mon Sep 17 00:00:00 2001 From: Pirulax Date: Fri, 29 May 2020 05:06:51 +0200 Subject: [PATCH 34/41] Apply changes to CVector2D: declare x, y on separate lines --- Shared/sdk/CVector2D.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Shared/sdk/CVector2D.h b/Shared/sdk/CVector2D.h index e29fa474595..64929eb127e 100644 --- a/Shared/sdk/CVector2D.h +++ b/Shared/sdk/CVector2D.h @@ -20,7 +20,8 @@ class CVector2D { public: - float fX = 0.0f, fY = 0.0f; + float fX = 0.0f; + float fY = 0.0f; constexpr CVector2D() noexcept = default; From 0831dd9ee2716fe6586ecf44a301b3ca9d5c12c4 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Fri, 29 May 2020 05:22:04 +0200 Subject: [PATCH 35/41] Modify DrawCircleQueued as suggested(logical error), also make it use emplace_back, not push_back --- Client/core/Graphics/CGraphics.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 982bcd42a90..8085fbbce52 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -903,21 +903,24 @@ void CGraphics::DrawCircleQueued(float fX, float fY, float fRadius, float fStart pVecVertices->reserve(siSegments + 1); // Calculate each segment angle - const float kfSegmentAngle = (D3DXToRadian(fStopAngle) - D3DXToRadian(fStartAngle)) / siSegments; + const float fStartAngleRad = D3DXToRadian(fStartAngle); + const float fStopAngleRad = D3DXToRadian(fStopAngle); + + const float kfSegmentAngle = (fStopAngleRad - fStartAngleRad) / siSegments; // Add center point - pVecVertices->push_back({fX, fY, 0.0f, ulColorCenter}); + pVecVertices->emplace_back(fX, fY, 0.0f, ulColorCenter); // And calculate all other vertices - for (short siSeg = 0; siSeg <= siSegments; siSeg++) + for (int segIndex = 0; segIndex <= siSegments; segIndex++) { - PrimitiveVertice vert; - float curAngle = fStartAngle + siSeg * kfSegmentAngle; + PrimitiveVertice& vert = pVecVertices->emplace_back(); // emplace it in the vector, and modify the inserted value afterwards + + const float curAngle = fStartAngleRad + segIndex * kfSegmentAngle; vert.fX = fX + fRadius * cos(curAngle) * fRatio; vert.fY = fY + fRadius * sin(curAngle); vert.fZ = 0.0f; vert.Color = ulColor; - pVecVertices->push_back(vert); } DrawPrimitiveQueued(pVecVertices, D3DPT_TRIANGLEFAN, bPostGUI); From f9e59289c2f4c4d214b0718ebcd3b8ee66a85775 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Fri, 29 May 2020 05:40:12 +0200 Subject: [PATCH 36/41] Refactor AddQueueItem to CreateQueueItem, and make it use emplace_back instead of inserting copying the QueueItem --- Client/core/Graphics/CGraphics.cpp | 98 ++++++++++++++++-------------- Client/core/Graphics/CGraphics.h | 1 + 2 files changed, 52 insertions(+), 47 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 8085fbbce52..f954cf328a3 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -819,11 +819,15 @@ void CGraphics::SetCursorPosition(int iX, int iY, DWORD Flags) void CGraphics::DrawLineQueued(float fX1, float fY1, float fX2, float fY2, float fWidth, unsigned long ulColor, bool bPostGUI) { + if (g_pCore->IsWindowMinimized()) + return; + fY1 = m_pAspectRatioConverter->ConvertPositionForAspectRatio(fY1); fY2 = m_pAspectRatioConverter->ConvertPositionForAspectRatio(fY2); // Set up a queue item - sDrawQueueItem Item; + sDrawQueueItem& Item = CreateQueueItem(bPostGUI); + Item.eType = QUEUE_LINE; Item.blendMode = m_ActiveBlendMode; @@ -833,9 +837,6 @@ void CGraphics::DrawLineQueued(float fX1, float fY1, float fX2, float fY2, float Item.Line.fY2 = fY2; Item.Line.fWidth = fWidth; Item.Line.ulColor = ulColor; - - // Add it to the queue - AddQueueItem(Item, bPostGUI); } void CGraphics::DrawLine3DQueued(const CVector& vecBegin, const CVector& vecEnd, float fWidth, unsigned long ulColor, bool bPostGUI) @@ -874,10 +875,13 @@ void CGraphics::DrawMaterialLine3DQueued(const CVector& vecBegin, const CVector& void CGraphics::DrawRectQueued(float fX, float fY, float fWidth, float fHeight, unsigned long ulColor, bool bPostGUI, bool bSubPixelPositioning) { + if (g_pCore->IsWindowMinimized()) + return; + m_pAspectRatioConverter->ConvertSideForAspectRatio(&fY, &fHeight); // Set up a queue item - sDrawQueueItem Item; + sDrawQueueItem& Item = CreateQueueItem(bPostGUI); Item.eType = QUEUE_RECT; Item.blendMode = m_ActiveBlendMode; @@ -887,9 +891,6 @@ void CGraphics::DrawRectQueued(float fX, float fY, float fWidth, float fHeight, Item.Rect.fHeight = fHeight; Item.Rect.ulColor = ulColor; Item.Rect.bSubPixelPositioning = bSubPixelPositioning; - - // Add it to the queue - AddQueueItem(Item, bPostGUI); } void CGraphics::DrawCircleQueued(float fX, float fY, float fRadius, float fStartAngle, float fStopAngle, unsigned long ulColor, unsigned long ulColorCenter, @@ -940,14 +941,13 @@ void CGraphics::DrawPrimitiveQueued(std::vector* pVecVertices, { vert.fY = m_pAspectRatioConverter->ConvertPositionForAspectRatio(vert.fY); } - // Set up a queue item - sDrawQueueItem Item; + sDrawQueueItem& Item = CreateQueueItem(bPostGUI); + Item.eType = QUEUE_PRIMITIVE; Item.blendMode = m_ActiveBlendMode; Item.Primitive.eType = eType; Item.Primitive.pVecVertices = pVecVertices; - AddQueueItem(Item, bPostGUI); } void CGraphics::DrawPrimitive3DQueued(std::vector* pVecVertices, D3DPRIMITIVETYPE eType, bool bPostGUI) @@ -1011,7 +1011,7 @@ void CGraphics::DrawMaterialPrimitiveQueued(std::vectorIsWindowMinimized()) + return; + m_pAspectRatioConverter->ConvertSideForAspectRatio(&fY, &fHeight); // Set up a queue item - sDrawQueueItem Item; + sDrawQueueItem& Item = CreateQueueItem(bPostGUI); + Item.blendMode = m_ActiveBlendMode; Item.Texture.fX = fX; @@ -1093,15 +1095,16 @@ void CGraphics::DrawTextureQueued(float fX, float fY, float fWidth, float fHeigh // Keep material valid while in the queue AddQueueRef(Item.Texture.pMaterial); - - // Add it to the queue - AddQueueItem(Item, bPostGUI); } void CGraphics::DrawStringQueued(float fLeft, float fTop, float fRight, float fBottom, unsigned long dwColor, const char* szText, float fScaleX, float fScaleY, unsigned long ulFormat, ID3DXFont* pDXFont, bool bPostGUI, bool bColorCoded, bool bSubPixelPositioning, float fRotation, float fRotationCenterX, float fRotationCenterY) { + + if (g_pCore->IsWindowMinimized()) + return; + if (!szText || !m_pDXSprite) return; @@ -1143,7 +1146,9 @@ void CGraphics::DrawStringQueued(float fLeft, float fTop, float fRight, float fB fBottom = floor(fBottom); } - sDrawQueueItem Item; + // Set up a queue item + sDrawQueueItem& Item = CreateQueueItem(bPostGUI); + Item.eType = QUEUE_TEXT; Item.blendMode = m_ActiveBlendMode; @@ -1165,9 +1170,6 @@ void CGraphics::DrawStringQueued(float fLeft, float fTop, float fRight, float fB // Keep font valid while in the queue incase it's a custom font AddQueueRef(Item.Text.pDXFont); - - // Add it to the queue - AddQueueItem(Item, bPostGUI); return; } else @@ -1211,6 +1213,9 @@ void CGraphics::DrawColorCodedTextLine(float fLeft, float fRight, float fY, SCol unsigned long ulFormat, ID3DXFont* pDXFont, bool bPostGUI, bool bSubPixelPositioning, float fRotation, float fRotationCenterX, float fRotationCenterY) { + if (g_pCore->IsWindowMinimized()) + return; + struct STextSection { std::wstring wstrText; @@ -1304,7 +1309,9 @@ void CGraphics::DrawColorCodedTextLine(float fLeft, float fRight, float fY, SCol fTop = floor(fTop); } - sDrawQueueItem Item; + // Set up a queue item + sDrawQueueItem& Item = CreateQueueItem(bPostGUI); + Item.eType = QUEUE_TEXT; Item.blendMode = m_ActiveBlendMode; @@ -1326,9 +1333,6 @@ void CGraphics::DrawColorCodedTextLine(float fLeft, float fRight, float fY, SCol // Keep font valid while in the queue incase it's a custom font AddQueueRef(Item.Text.pDXFont); - // Add it to the queue - AddQueueItem(Item, bPostGUI); - fX += section.fWidth; } } @@ -1603,16 +1607,26 @@ void CGraphics::OnZBufferModified() void CGraphics::DrawPreGUIQueue() { - DrawQueue(m_PreGUIQueue); + if (g_pCore->IsWindowMinimized()) + ClearDrawQueue(m_PreGUIQueue); + else + DrawQueue(m_PreGUIQueue); } void CGraphics::DrawPostGUIQueue() { - DrawQueue(m_PostGUIQueue); - m_pLine3DBatcherPostGUI->Flush(); - m_pMaterialLine3DBatcherPostGUI->Flush(); - m_pPrimitive3DBatcherPostGUI->Flush(); - m_pMaterialPrimitive3DBatcherPostGUI->Flush(); + if (g_pCore->IsWindowMinimized()) + { + ClearDrawQueue(m_PostGUIQueue); + } + else + { + DrawQueue(m_PostGUIQueue); + m_pLine3DBatcherPostGUI->Flush(); + m_pMaterialLine3DBatcherPostGUI->Flush(); + m_pPrimitive3DBatcherPostGUI->Flush(); + m_pMaterialPrimitive3DBatcherPostGUI->Flush(); + } // Both queues should be empty now, and there should be no outstanding refs assert(m_PreGUIQueue.empty() && m_iDebugQueueRefs == 0); @@ -1655,22 +1669,12 @@ void CGraphics::DrawQueue(std::vector& Queue) EndDrawBatch(); } -void CGraphics::AddQueueItem(const sDrawQueueItem& Item, bool bPostGUI) +// the order of adding refs(to textures, fonts and whatnot) doesnt matter +// just add them before / after crating the item +CGraphics::sDrawQueueItem& CGraphics::CreateQueueItem(const bool bPostGUI) { - // Add it to the correct queue - if (bPostGUI && !CCore::GetSingleton().IsMenuVisible()) // Don't draw over the main menu. Ever. - m_PostGUIQueue.push_back(Item); - else - m_PreGUIQueue.push_back(Item); - - // Prevent queuing when minimized - if (g_pCore->IsWindowMinimized()) - { - ClearDrawQueue(m_PreGUIQueue); - ClearDrawQueue(m_PostGUIQueue); - assert(m_iDebugQueueRefs == 0); - return; - } + auto& emplaceInto = bPostGUI ? m_PostGUIQueue : m_PreGUIQueue; + return emplaceInto.emplace_back(); } void CGraphics::DrawQueueItem(const sDrawQueueItem& Item) diff --git a/Client/core/Graphics/CGraphics.h b/Client/core/Graphics/CGraphics.h index 3e381057aef..b710cb9f034 100644 --- a/Client/core/Graphics/CGraphics.h +++ b/Client/core/Graphics/CGraphics.h @@ -348,6 +348,7 @@ class CGraphics : public CGraphicsInterface, public CSingleton std::vector m_PostGUIQueue; void AddQueueItem(const sDrawQueueItem& Item, bool bPostGUI); + sDrawQueueItem& CreateQueueItem(const bool bPostGUI); void DrawQueueItem(const sDrawQueueItem& Item); void DrawQueue(std::vector& Queue); void ClearDrawQueue(std::vector& Queue); From db4d80b3c570850d8b1fa5e8813e428377dffb6e Mon Sep 17 00:00:00 2001 From: Pirulax Date: Fri, 29 May 2020 05:42:24 +0200 Subject: [PATCH 37/41] remove static from g rectEdgePixelsData --- Client/core/Graphics/CGraphics.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index f954cf328a3..f05af272c6a 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -27,7 +27,7 @@ using namespace std; template <> CGraphics* CSingleton::m_pSingleton = NULL; -static const SColor g_rectEdgePixelsData[] = { +const SColor g_rectEdgePixelsData[] = { 0x00FFFF00, 0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, // 0x00FFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00FFFFFF, // 0x00FFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00FFFFFF, // From 63a270bf99c37f5bef2b92f2f0d82aca7aaea94b Mon Sep 17 00:00:00 2001 From: Pirulax Date: Fri, 29 May 2020 23:53:00 +0200 Subject: [PATCH 38/41] Add PrimitiveVertice constructor, emplace_back using arguments in CGraphics::DrawCircleQueued --- Client/core/Graphics/CGraphics.cpp | 10 ++++------ Client/sdk/core/CGraphicsInterface.h | 7 +++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index f05af272c6a..52ba1634f9f 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -915,13 +915,11 @@ void CGraphics::DrawCircleQueued(float fX, float fY, float fRadius, float fStart // And calculate all other vertices for (int segIndex = 0; segIndex <= siSegments; segIndex++) { - PrimitiveVertice& vert = pVecVertices->emplace_back(); // emplace it in the vector, and modify the inserted value afterwards - const float curAngle = fStartAngleRad + segIndex * kfSegmentAngle; - vert.fX = fX + fRadius * cos(curAngle) * fRatio; - vert.fY = fY + fRadius * sin(curAngle); - vert.fZ = 0.0f; - vert.Color = ulColor; + const float verticefX = fX + fRadius * cos(curAngle) * fRatio; + const float verticefY = fY + fRadius * sin(curAngle); + + pVecVertices->emplace_back(verticefX, verticefY, 0.0f, ulColor); } DrawPrimitiveQueued(pVecVertices, D3DPT_TRIANGLEFAN, bPostGUI); diff --git a/Client/sdk/core/CGraphicsInterface.h b/Client/sdk/core/CGraphicsInterface.h index a4f83c1d222..d9b1c1d7ebd 100644 --- a/Client/sdk/core/CGraphicsInterface.h +++ b/Client/sdk/core/CGraphicsInterface.h @@ -20,6 +20,13 @@ struct PrimitiveVertice static const uint FNV = D3DFVF_XYZ | D3DFVF_DIFFUSE; float fX, fY, fZ; D3DCOLOR Color; + + PrimitiveVertice(float x, float y, float z, D3DCOLOR color) : + fX(x), + fY(y), + fZ(z), + Color(color) + {} }; struct PrimitiveMaterialVertice { From defbe697477133d1093e6101eb73fd85449a6b4d Mon Sep 17 00:00:00 2001 From: Pirulax Date: Fri, 29 May 2020 23:53:36 +0200 Subject: [PATCH 39/41] fix MaybeGetBigFont incorrectly modifying scaleX twice --- Client/core/Graphics/CGraphics.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 52ba1634f9f..da4452c9a51 100644 --- a/Client/core/Graphics/CGraphics.cpp +++ b/Client/core/Graphics/CGraphics.cpp @@ -1822,7 +1822,7 @@ ID3DXFont* CGraphics::MaybeGetBigFont(ID3DXFont* pDXFont, float& fScaleX, float& // devide by 4 because the big font is 4x bigger fScaleX /= 4.0f; if (&fScaleX != &fScaleY) // Check fScaleY is not the same variable - fScaleX /= 4.0f; + fScaleY /= 4.0f; return m_pBigDXFonts[i]; } From 4df60199c0a2b89a961d0d7b05a98f4ac23c746b Mon Sep 17 00:00:00 2001 From: Pirulax Date: Fri, 29 May 2020 23:56:05 +0200 Subject: [PATCH 40/41] Fix NotifyDe/ContructRenderItem incorrect assertion expressions --- Client/core/Graphics/CRenderItemManager.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Client/core/Graphics/CRenderItemManager.cpp b/Client/core/Graphics/CRenderItemManager.cpp index b8adae692c6..7e78c48aff9 100644 --- a/Client/core/Graphics/CRenderItemManager.cpp +++ b/Client/core/Graphics/CRenderItemManager.cpp @@ -313,7 +313,7 @@ CGuiFontItem* CRenderItemManager::CreateGuiFont(const SString& strFullFilePath, void CRenderItemManager::NotifyContructRenderItem(CRenderItem* pItem) { const bool wasInserted = m_CreatedItemList.insert(pItem).second; - assert(!wasInserted); // assert if item was already in the collection. + assert(wasInserted); // assert if item was already in the collection(and thus we didnt insert this) if (CScreenSourceItem* pScreenSourceItem = DynamicCast(pItem)) m_bBackBufferCopyMaybeNeedsResize = true; @@ -328,7 +328,8 @@ void CRenderItemManager::NotifyContructRenderItem(CRenderItem* pItem) //////////////////////////////////////////////////////////////// void CRenderItemManager::NotifyDestructRenderItem(CRenderItem* pItem) { - assert(!m_CreatedItemList.erase(pItem) == 1); // assert if item wasnt in the collection exactly once + const auto deletedCount = m_CreatedItemList.erase(pItem); + assert(deletedCount == 1); // it must be in the collection exactly once if (CScreenSourceItem* pScreenSourceItem = DynamicCast(pItem)) m_bBackBufferCopyMaybeNeedsResize = true; From f4594332466e61222f0025c992600001245aa09f Mon Sep 17 00:00:00 2001 From: Pirulax Date: Fri, 29 May 2020 23:56:43 +0200 Subject: [PATCH 41/41] Fix incorrect usage of constexpr for Cvector4d:operator== --- Shared/sdk/CVector4D.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Shared/sdk/CVector4D.h b/Shared/sdk/CVector4D.h index 02226ae5085..5635a5cef94 100644 --- a/Shared/sdk/CVector4D.h +++ b/Shared/sdk/CVector4D.h @@ -132,11 +132,11 @@ class CVector4D fW /= vecRight.fW; } - constexpr bool operator==(const CVector4D& param) const noexcept + bool operator==(const CVector4D& param) const noexcept { return ((fabs(fX - param.fX) < FLOAT_EPSILON) && (fabs(fY - param.fY) < FLOAT_EPSILON) && (fabs(fZ - param.fZ) < FLOAT_EPSILON) && (fabs(fW - param.fW) < FLOAT_EPSILON)); } - constexpr bool operator!=(const CVector4D& param) const noexcept { return !(*this == param); } + bool operator!=(const CVector4D& param) const noexcept { return !(*this == param); } };