From ece0aec27a607f142975a0ec32e1b2013f9e1671 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 12 Jun 2020 16:32:01 -0400 Subject: [PATCH] CGraphics: Make use of std::array --- src/Core/Render/CGraphics.cpp | 10 +++++----- src/Core/Render/CGraphics.h | 17 +++++++++-------- src/Core/Resource/CLight.cpp | 5 +++-- src/Core/Resource/CMaterial.cpp | 20 ++++++++++---------- src/Core/Resource/CMaterial.h | 4 ++-- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/Core/Render/CGraphics.cpp b/src/Core/Render/CGraphics.cpp index cccccdf8..19e314f1 100644 --- a/src/Core/Render/CGraphics.cpp +++ b/src/Core/Render/CGraphics.cpp @@ -24,11 +24,11 @@ CGraphics::ELightingMode CGraphics::sLightMode; uint32 CGraphics::sNumLights; CColor CGraphics::sAreaAmbientColor = CColor::skTransparentBlack; float CGraphics::sWorldLightMultiplier; -CLight CGraphics::sDefaultDirectionalLights[3] = { - CLight::BuildDirectional(CVector3f(0), CVector3f (0.f, -0.866025f, -0.5f), CColor(0.3f, 0.3f, 0.3f, 0.3f)), - CLight::BuildDirectional(CVector3f(0), CVector3f(-0.75f, 0.433013f, -0.5f), CColor(0.3f, 0.3f, 0.3f, 0.3f)), - CLight::BuildDirectional(CVector3f(0), CVector3f( 0.75f, 0.433013f, -0.5f), CColor(0.3f, 0.3f, 0.3f, 0.3f)) -}; +std::array CGraphics::sDefaultDirectionalLights{{ + CLight::BuildDirectional(CVector3f(0), CVector3f(0.f, -0.866025f, -0.5f), CColor(0.3f, 0.3f, 0.3f, 0.3f)), + CLight::BuildDirectional(CVector3f(0), CVector3f(-0.75f, 0.433013f, -0.5f), CColor(0.3f, 0.3f, 0.3f, 0.3f)), + CLight::BuildDirectional(CVector3f(0), CVector3f(0.75f, 0.433013f, -0.5f), CColor(0.3f, 0.3f, 0.3f, 0.3f)), +}}; // ************ FUNCTIONS ************ void CGraphics::Initialize() diff --git a/src/Core/Render/CGraphics.h b/src/Core/Render/CGraphics.h index 53cf5506..5f837b80 100644 --- a/src/Core/Render/CGraphics.h +++ b/src/Core/Render/CGraphics.h @@ -9,6 +9,7 @@ #include #include #include +#include /** * todo: this entire thing needs to be further abstracted, other classes shouldn't @@ -43,8 +44,8 @@ public: // SVertexBlock struct SVertexBlock { - CMatrix4f TexMatrices[10]; - CMatrix4f PostMatrices[20]; + std::array TexMatrices; + std::array PostMatrices; CColor COLOR0_Amb; CColor COLOR0_Mat; CColor COLOR1_Amb; @@ -55,15 +56,15 @@ public: // SPixelBlock struct SPixelBlock { - CColor Konst[4]; - CColor TevColor[4]; + std::array Konst; + std::array TevColor; CColor TintColor; float LightmapMultiplier; - float Padding[3]; + std::array Padding; void SetAllTevColors(const CColor& color) { - std::fill(std::begin(TevColor), std::end(TevColor), color); + TevColor.fill(color); } }; static SPixelBlock sPixelBlock; @@ -79,7 +80,7 @@ public: CVector4f DistAtten; CVector4f AngleAtten; }; - SGXLight Lights[8]; + std::array Lights; }; static SLightBlock sLightBlock; @@ -90,7 +91,7 @@ public: static constexpr CColor skDefaultAmbientColor{0.5f, 0.5f, 0.5f, 0.0f}; static CColor sAreaAmbientColor; static float sWorldLightMultiplier; - static CLight sDefaultDirectionalLights[3]; + static std::array sDefaultDirectionalLights; // Functions static void Initialize(); diff --git a/src/Core/Resource/CLight.cpp b/src/Core/Resource/CLight.cpp index afc6f831..e41481e4 100644 --- a/src/Core/Resource/CLight.cpp +++ b/src/Core/Resource/CLight.cpp @@ -198,8 +198,9 @@ CStructProperty* CLight::GetProperties() const // ************ OTHER ************ void CLight::Load() const { - uint8 Index = (uint8) CGraphics::sNumLights; - if (Index >= 8) return; + const auto Index = static_cast(CGraphics::sNumLights); + if (Index >= CGraphics::sLightBlock.Lights.size()) + return; CGraphics::SLightBlock::SGXLight *pLight = &CGraphics::sLightBlock.Lights[Index]; diff --git a/src/Core/Resource/CMaterial.cpp b/src/Core/Resource/CMaterial.cpp index 2eee9849..2e1c38cc 100644 --- a/src/Core/Resource/CMaterial.cpp +++ b/src/Core/Resource/CMaterial.cpp @@ -181,13 +181,13 @@ bool CMaterial::SetCurrent(FRenderOptions Options) glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha); // Set konst inputs - for (uint32 iKonst = 0; iKonst < 4; iKonst++) - CGraphics::sPixelBlock.Konst[iKonst] = mKonstColors[iKonst]; + CGraphics::sPixelBlock.Konst = mKonstColors; // Set TEV registers if (mVersion >= EGame::Corruption) - for (uint32 iTev = 0; iTev < 4; iTev++) - CGraphics::sPixelBlock.TevColor[iTev] = mTevColors[iTev]; + { + CGraphics::sPixelBlock.TevColor = mTevColors; + } // Set color channels // COLOR0_Amb,Mat is initialized by the node instead of by the material @@ -242,21 +242,21 @@ uint64 CMaterial::HashParameters() { CFNV1A Hash(CFNV1A::EHashLength::k64Bit); - Hash.HashLong((int) mVersion); + Hash.HashLong(static_cast(mVersion)); Hash.HashLong(mOptions); Hash.HashLong(mVtxDesc); - Hash.HashData(mKonstColors, sizeof(CColor) * 4); - Hash.HashData(mTevColors, sizeof(CColor) * 4); + Hash.HashData(mKonstColors.data(), sizeof(mKonstColors)); + Hash.HashData(mTevColors.data(), sizeof(mTevColors)); Hash.HashLong(mBlendSrcFac); Hash.HashLong(mBlendDstFac); Hash.HashByte(mLightingEnabled); Hash.HashLong(mEchoesUnknownA); Hash.HashLong(mEchoesUnknownB); - for (uint32 iPass = 0; iPass < mPasses.size(); iPass++) - mPasses[iPass]->HashParameters(Hash); + for (auto& pass : mPasses) + pass->HashParameters(Hash); - uint64 NewHash = Hash.GetHash64(); + const uint64 NewHash = Hash.GetHash64(); if (mParametersHash != NewHash) ClearShader(); diff --git a/src/Core/Resource/CMaterial.h b/src/Core/Resource/CMaterial.h index a30161df..d8abf453 100644 --- a/src/Core/Resource/CMaterial.h +++ b/src/Core/Resource/CMaterial.h @@ -83,8 +83,8 @@ private: EGame mVersion; FMaterialOptions mOptions; // See the EMaterialOption enum above FVertexDescription mVtxDesc; // Descriptor of vertex attributes used by this material - CColor mKonstColors[4]; // Konst color values for TEV - CColor mTevColors[4]; // Initial TEV color register values (for MP3 materials only) + std::array mKonstColors; // Konst color values for TEV + std::array mTevColors; // Initial TEV color register values (for MP3 materials only) GLenum mBlendSrcFac; // Source blend factor GLenum mBlendDstFac; // Dest blend factor bool mLightingEnabled; // Color channel control flags; indicate whether lighting is enabled