diff --git a/Client/core/Graphics/CGraphics.cpp b/Client/core/Graphics/CGraphics.cpp index 4b1cc308de9..da4452c9a51 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); } @@ -231,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 @@ -243,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]; @@ -483,50 +485,57 @@ 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) { + const bool bDrawModeChanging = (m_CurDrawMode != newDrawMode); + const bool bBlendModeChanging = (m_CurBlendMode != newBlendMode); - // Flush old - if (m_CurDrawMode == EDrawMode::DX_SPRITE) - { - m_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); - m_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); - m_pDXSprite->End(); - } - else if (m_CurDrawMode == EDrawMode::DX_LINE) - { - m_pLineInterface->End(); - } - else if (m_CurDrawMode == EDrawMode::TILE_BATCHER) - { - m_pTileBatcher->Flush(); - } - else if (m_CurDrawMode == EDrawMode::PRIMITIVE) - { - m_pPrimitiveBatcher->Flush(); - } - else if (m_CurDrawMode == EDrawMode::PRIMITIVE_MATERIAL) - { - m_pPrimitiveMaterialBatcher->Flush(); - } + // Draw mode changing? + if (!bDrawModeChanging && !bBlendModeChanging) + return; + } - // Start new - if (newDrawMode == EDrawMode::DX_SPRITE) - { - m_pDXSprite->Begin(D3DXSPRITE_DONOTSAVESTATE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE); - } - else if (newDrawMode == EDrawMode::DX_LINE) - { - m_pLineInterface->Begin(); - } - SetBlendModeRenderStates(newBlendMode); - m_CurBlendMode = newBlendMode; - m_CurDrawMode = newDrawMode; + // 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; + + case EDrawMode::TILE_BATCHER: + m_pTileBatcher->Flush(); + break; + + case EDrawMode::PRIMITIVE: + m_pPrimitiveBatcher->Flush(); + break; + + 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) @@ -668,17 +677,16 @@ 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) + // count spaces starting from the end until the first non-whitespace char + size_t spaceCount = 0; + for (int i = textLength - 1; i >= 0; --i) { - const wchar_t c = wszText[i]; - - if (c == L' ') - ++spaceCount; - else + 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 @@ -692,7 +700,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)); @@ -737,14 +744,14 @@ 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) + for (auto iter = strText.rbegin(); iter != strText.rend(); iter++) { - if (*c == ' ') - ++iSpaceCount; - else + if (*iter != L' ') break; + + iSpaceCount++; } // Compute the size of a single space and use that @@ -757,7 +764,6 @@ int CGraphics::GetTrailingSpacesWidth(ID3DXFont* pDXFont, WString& strText) GetTextExtentPoint32W(dc, L" ", 1, &size); iSpacesWidth = iSpaceCount * size.cx; } - return iSpacesWidth; } @@ -813,22 +819,24 @@ 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; + Item.Line.fX1 = fX1; Item.Line.fY1 = fY1; Item.Line.fX2 = fX2; 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) @@ -867,21 +875,22 @@ 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; + Item.Rect.fX = fX; Item.Rect.fY = fY; Item.Rect.fWidth = fWidth; 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, @@ -892,24 +901,25 @@ 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 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; - vert.fX = fX + fRadius * cos(curAngle) * fRatio; - vert.fY = fY + fRadius * sin(curAngle); - vert.fZ = 0.0f; - vert.Color = ulColor; - pVecVertices->push_back(vert); + const float curAngle = fStartAngleRad + segIndex * kfSegmentAngle; + 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); @@ -929,14 +939,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) @@ -1000,13 +1009,14 @@ void CGraphics::DrawMaterialPrimitiveQueued(std::vector= 2; - return true; + case D3DPT_LINELIST: + return iNumVertives % 2 == 0; + + case D3DPT_TRIANGLELIST: + return iNumVertives % 3 == 0; + + case D3DPT_TRIANGLEFAN: + case D3DPT_TRIANGLESTRIP: + 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, CMaterialItem* pMaterial, float fRotation, float fRotCenOffX, float fRotCenOffY, unsigned long ulColor, bool bPostGUI) { + if (g_pCore->IsWindowMinimized()) + 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; Item.Texture.fY = fY; Item.Texture.fWidth = fWidth; @@ -1083,23 +1088,21 @@ 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 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; @@ -1123,8 +1126,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; @@ -1139,7 +1144,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; @@ -1161,9 +1168,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 @@ -1177,10 +1181,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; @@ -1193,9 +1197,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; } @@ -1206,6 +1211,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; @@ -1213,6 +1221,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 @@ -1280,10 +1290,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; @@ -1299,7 +1307,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; @@ -1321,14 +1331,12 @@ 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; } } -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}, @@ -1337,13 +1345,16 @@ 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() { // 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"); @@ -1351,17 +1362,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); } } } @@ -1447,10 +1458,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++) @@ -1460,8 +1469,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; } @@ -1481,13 +1490,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; @@ -1537,13 +1548,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(); @@ -1553,6 +1567,7 @@ void CGraphics::OnDeviceInvalidate(IDirect3DDevice9* pDevice) m_pRenderItemManager->OnLostDevice(); m_pScreenGrabber->OnLostDevice(); + SAFE_RELEASE(m_pSavedFrontBufferData); SAFE_RELEASE(m_pTempBackBufferData); } @@ -1567,9 +1582,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(); @@ -1588,16 +1605,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); @@ -1609,18 +1636,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(); } @@ -1628,39 +1655,24 @@ 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(); } -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) @@ -1807,9 +1819,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; + fScaleY /= 4.0f; + return m_pBigDXFonts[i]; } } @@ -1822,13 +1836,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) + { + switch (item.eType) { - const sDrawQueueItem& item = *iter; - if (item.eType == QUEUE_TEXTURE || item.eType == QUEUE_SHADER) + 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(); } @@ -1908,12 +1928,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); @@ -1924,6 +1946,7 @@ void CGraphics::MaybeEnteringMTARenderZone() m_MTARenderZone = MTA_RZONE_OUTSIDE; } } +} //////////////////////////////////////////////////////////////// // @@ -2265,36 +2288,38 @@ 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 - { - // Create surfaces - if (FAILED(g_pGraphics->GetDevice()->CreateOffscreenPlainSurface(uiWidth, uiHeight, (D3DFORMAT)d3dFormat, D3DPOOL_SCRATCH, &pCurrentSurface, NULL))) - break; - if (FAILED(g_pGraphics->GetDevice()->CreateOffscreenPlainSurface(uiNewWidth, uiNewHeight, (D3DFORMAT)d3dFormat, D3DPOOL_SCRATCH, &pNewSurface, NULL))) - break; + bool bResult = false; - // Data in - if (!CopyDataToSurface(pCurrentSurface, (const BYTE*)pData, uiDataPitch)) - break; + // Create surfaces + if (FAILED(g_pGraphics->GetDevice()->CreateOffscreenPlainSurface(uiWidth, uiHeight, (D3DFORMAT)d3dFormat, D3DPOOL_SCRATCH, &pCurrentSurface, NULL))) + goto cleanup_and_return; - // Resize - if (FAILED(D3DXLoadSurfaceFromSurface(pNewSurface, NULL, NULL, pCurrentSurface, NULL, NULL, D3DX_FILTER_TRIANGLE, 0))) - break; + if (FAILED(g_pGraphics->GetDevice()->CreateOffscreenPlainSurface(uiNewWidth, uiNewHeight, (D3DFORMAT)d3dFormat, D3DPOOL_SCRATCH, &pNewSurface, NULL))) + goto cleanup_and_return; - // Data out - if (!CopyDataFromSurface(pNewSurface, outBuffer)) - break; + // Data in + if (!CopyDataToSurface(pCurrentSurface, (const BYTE*)pData, uiDataPitch)) + goto cleanup_and_return; - bResult = true; - } while (false); + // Resize + if (FAILED(D3DXLoadSurfaceFromSurface(pNewSurface, NULL, NULL, pCurrentSurface, NULL, NULL, D3DX_FILTER_TRIANGLE, 0))) + goto cleanup_and_return; + + // Data out + if (!CopyDataFromSurface(pNewSurface, outBuffer)) + goto cleanup_and_return; + + bResult = true; + +cleanup_and_return: // Clean up SAFE_RELEASE(pCurrentSurface); SAFE_RELEASE(pNewSurface); + return bResult; } @@ -2409,49 +2434,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; } } } @@ -2487,24 +2522,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 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); diff --git a/Client/core/Graphics/CRenderItemManager.cpp b/Client/core/Graphics/CRenderItemManager.cpp index ad532a3be33..7e78c48aff9 100644 --- a/Client/core/Graphics/CRenderItemManager.cpp +++ b/Client/core/Graphics/CRenderItemManager.cpp @@ -312,8 +312,8 @@ CGuiFontItem* CRenderItemManager::CreateGuiFont(const SString& strFullFilePath, //////////////////////////////////////////////////////////////// void CRenderItemManager::NotifyContructRenderItem(CRenderItem* pItem) { - assert(!MapContains(m_CreatedItemList, pItem)); - MapInsert(m_CreatedItemList, pItem); + const bool wasInserted = m_CreatedItemList.insert(pItem).second; + 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,8 +328,8 @@ void CRenderItemManager::NotifyContructRenderItem(CRenderItem* pItem) //////////////////////////////////////////////////////////////// void CRenderItemManager::NotifyDestructRenderItem(CRenderItem* pItem) { - assert(MapContains(m_CreatedItemList, pItem)); - MapRemove(m_CreatedItemList, pItem); + 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; @@ -453,9 +453,9 @@ void CRenderItemManager::UpdateBackBufferCopySize() // 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++) + 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 +631,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 +648,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 +690,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 +782,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 +891,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 +922,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 +940,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 +962,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 +987,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; } //////////////////////////////////////////////////////////////// @@ -1070,6 +1077,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 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 { diff --git a/Shared/sdk/CVector.h b/Shared/sdk/CVector.h index d4e938ae926..3fdf5accaac 100644 --- a/Shared/sdk/CVector.h +++ b/Shared/sdk/CVector.h @@ -26,53 +26,63 @@ class CVector { public: - float fX, fY, fZ; + float fX = 0.0f; + float fY = 0.0f; + float fZ = 0.0f; - CVector() + constexpr CVector() = default; + + constexpr CVector(float x, float y, float z) : + fX(x), + fY(y), + fZ(z) { - this->fX = 0; - this->fY = 0; - this->fZ = 0; - }; + } - CVector(float fX, float fY, float fZ) + constexpr CVector(const CVector4D& vec) noexcept : + fX(vec.fX), + fY(vec.fY), + fZ(vec.fZ) { - this->fX = fX; - this->fY = fY; - this->fZ = fZ; } - CVector(const CVector4D& vec) + constexpr CVector& operator=(const CVector4D& vec) noexcept { - this->fX = vec.fX; - this->fY = vec.fY; - this->fZ = vec.fZ; + 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() { - float t = sqrt(fX * fX + fY * fY + fZ * fZ); + const float t = Length(); if (t > FLOAT_EPSILON) { - float fRcpt = 1 / t; - fX *= fRcpt; - fY *= fRcpt; - fZ *= fRcpt; + fX /= t; + fY /= t; + fZ /= t; + + return t; } else - t = 0; - return t; + 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; @@ -83,7 +93,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; @@ -97,25 +107,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; @@ -132,12 +133,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; @@ -145,9 +147,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; @@ -192,96 +194,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); } - CVector operator-() const { return CVector(-fX, -fY, -fZ); } + constexpr CVector operator*(const float fRight) const noexcept { return CVector(fX * fRight, fY * fRight, fZ * fRight); } - void operator+=(float 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); } + + + 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); } }; diff --git a/Shared/sdk/CVector2D.h b/Shared/sdk/CVector2D.h index ec5573593b0..64929eb127e 100644 --- a/Shared/sdk/CVector2D.h +++ b/Shared/sdk/CVector2D.h @@ -20,53 +20,54 @@ class CVector2D { public: - CVector2D() - { - fX = 0; - fY = 0; - } + float fX = 0.0f; + float fY = 0.0f; + + constexpr CVector2D() noexcept = default; - CVector2D(float _fX, float _fY) + constexpr CVector2D(float x, float y) noexcept : + fX(x), + fY(y) { - fX = _fX; - fY = _fY; } - CVector2D(const CVector& vec) + constexpr CVector2D(const CVector& vec) noexcept : + fX(vec.fX), + fY(vec.fY) { - fX = vec.fX; - fY = vec.fY; } - CVector2D(const CVector4D& vec) + constexpr CVector2D(const CVector4D& vec) noexcept : + fX(vec.fX), + fY(vec.fY) { - 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; @@ -74,74 +75,67 @@ class CVector2D } } - CVector2D operator*(float fRight) const { return CVector2D(fX * fRight, fY * fRight); } + constexpr CVector2D operator*(float fRight) const noexcept { 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 noexcept { 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 noexcept { 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 noexcept { 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 noexcept { 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 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 ((fabs(fX - param.fX) >= FLOAT_EPSILON) || (fabs(fY - param.fY) >= FLOAT_EPSILON)); } + 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); } }; diff --git a/Shared/sdk/CVector4D.h b/Shared/sdk/CVector4D.h index 798d6bc3a41..5635a5cef94 100644 --- a/Shared/sdk/CVector4D.h +++ b/Shared/sdk/CVector4D.h @@ -20,48 +20,37 @@ class CVector4D { public: - CVector4D() - { - fX = 0; - fY = 0; - fZ = 0; - fW = 0; - } + float fX = 0.0f; + float fY = 0.0f; + float fZ = 0.0f; + float fW = 0.0f; - CVector4D(float _fX, float _fY, float _fZ, float _fW) - { - fX = _fX; - fY = _fY; - fZ = _fZ; - fW = _fW; - } + constexpr CVector4D() noexcept = default; - CVector4D(const CVector4D& vec) - { - fX = vec.fX; - fY = vec.fY; - fZ = vec.fZ; - fW = vec.fW; - } + constexpr CVector4D(const CVector4D&) noexcept = default; + + constexpr CVector4D& operator=(const CVector4D&) noexcept = default; - CVector4D& operator=(const CVector4D& vec) + constexpr CVector4D(float x, float y, float z, float w) noexcept : + fX(x), + fY(y), + fZ(z), + fW(w) { - 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; } + // 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 { return sqrt(fX * fX + fY * fY + fZ * fZ + fW * fW); } + float Length() const noexcept { return sqrt(fX * fX + fY * fY + fZ * fZ + fW * fW); } - float LengthSquared() const { return (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); } - void Normalize() + void Normalize() noexcept { - float fLength = Length(); + const float fLength = Length(); if (fLength > 0.0f) { fX /= fLength; @@ -71,23 +60,22 @@ class CVector4D } } - 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); } - CVector4D operator/(float fRight) const - { - float fRcpValue = 1 / fRight; - return CVector4D(fX * fRcpValue, fY * fRcpValue, fZ * fRcpValue, fW * fRcpValue); - } + constexpr CVector4D operator*(const float fRight) const noexcept { return CVector4D(fX * fRight, fY * fRight, fZ * fRight, fW * fRight); } - 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 noexcept { 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 noexcept { 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); } - void operator+=(float fRight) + 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); } + + + constexpr void operator+=(const float fRight) noexcept { fX += fRight; fY += fRight; @@ -95,7 +83,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 +91,7 @@ class CVector4D fW += vecRight.fW; } - void operator-=(float fRight) + constexpr void operator-=(const float fRight) noexcept { fX -= fRight; fY -= fRight; @@ -111,7 +99,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 +107,7 @@ class CVector4D fW -= vecRight.fW; } - void operator*=(float fRight) + constexpr void operator*=(const float fRight) noexcept { fX *= fRight; fY *= fRight; @@ -127,15 +115,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 +124,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 +132,11 @@ class CVector4D fW /= vecRight.fW; } - bool operator==(const CVector4D& param) const + 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; + bool operator!=(const CVector4D& param) const noexcept { return !(*this == param); } }; 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; \ } \ 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; } };