From 4d34589816c44f4e7597820b2675de33bc0c21b8 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 16 Jun 2020 18:04:07 -0400 Subject: [PATCH] CDrawUtil: Make use of std::array While we're at it, we can make several arrays constexpr. --- src/Core/OpenGL/CIndexBuffer.cpp | 2 +- src/Core/OpenGL/CIndexBuffer.h | 2 +- src/Core/Render/CDrawUtil.cpp | 60 +++++++------------------------- src/Core/Render/CDrawUtil.h | 47 +++++++++++++------------ 4 files changed, 38 insertions(+), 73 deletions(-) diff --git a/src/Core/OpenGL/CIndexBuffer.cpp b/src/Core/OpenGL/CIndexBuffer.cpp index e3e8792f..f2b9c328 100644 --- a/src/Core/OpenGL/CIndexBuffer.cpp +++ b/src/Core/OpenGL/CIndexBuffer.cpp @@ -18,7 +18,7 @@ void CIndexBuffer::AddIndex(uint16 index) mIndices.push_back(index); } -void CIndexBuffer::AddIndices(uint16 *indices, size_t count) +void CIndexBuffer::AddIndices(const uint16 *indices, size_t count) { Reserve(count); for (size_t i = 0; i < count; i++) diff --git a/src/Core/OpenGL/CIndexBuffer.h b/src/Core/OpenGL/CIndexBuffer.h index dc88937e..62cef554 100644 --- a/src/Core/OpenGL/CIndexBuffer.h +++ b/src/Core/OpenGL/CIndexBuffer.h @@ -17,7 +17,7 @@ public: explicit CIndexBuffer(GLenum type); ~CIndexBuffer(); void AddIndex(uint16 index); - void AddIndices(uint16 *indices, size_t count); + void AddIndices(const uint16 *indices, size_t count); void Reserve(size_t size); void Clear(); void Buffer(); diff --git a/src/Core/Render/CDrawUtil.cpp b/src/Core/Render/CDrawUtil.cpp index 5ad20a33..6c8f2ada 100644 --- a/src/Core/Render/CDrawUtil.cpp +++ b/src/Core/Render/CDrawUtil.cpp @@ -3,42 +3,6 @@ #include "Core/GameProject/CResourceStore.h" #include #include -#include - -// ************ MEMBER INITIALIZATION ************ -std::optional CDrawUtil::mGridVertices; -CIndexBuffer CDrawUtil::mGridIndices; - -std::optional CDrawUtil::mSquareVertices; -CIndexBuffer CDrawUtil::mSquareIndices; - -std::optional CDrawUtil::mLineVertices; -CIndexBuffer CDrawUtil::mLineIndices; - -TResPtr CDrawUtil::mpCubeModel; - -std::optional CDrawUtil::mWireCubeVertices; -CIndexBuffer CDrawUtil::mWireCubeIndices; - -TResPtr CDrawUtil::mpSphereModel; -TResPtr CDrawUtil::mpDoubleSidedSphereModel; - -TResPtr CDrawUtil::mpWireSphereModel; - -CShader *CDrawUtil::mpColorShader; -CShader *CDrawUtil::mpColorShaderLighting; -CShader *CDrawUtil::mpBillboardShader; -CShader *CDrawUtil::mpLightBillboardShader; -CShader *CDrawUtil::mpTextureShader; -CShader *CDrawUtil::mpCollisionShader; -CShader *CDrawUtil::mpTextShader; - -TResPtr CDrawUtil::mpCheckerTexture; - -TResPtr CDrawUtil::mpLightTextures[4]; -TResPtr CDrawUtil::mpLightMasks[4]; - -bool CDrawUtil::mDrawUtilInitialized = false; // ************ PUBLIC ************ void CDrawUtil::DrawGrid(CColor LineColor, CColor BoldLineColor) @@ -366,13 +330,13 @@ void CDrawUtil::LoadCheckerboardTexture(uint32 GLTextureUnit) CTexture* CDrawUtil::GetLightTexture(ELightType Type) { Init(); - return mpLightTextures[(int) Type]; + return mpLightTextures[static_cast(Type)]; } CTexture* CDrawUtil::GetLightMask(ELightType Type) { Init(); - return mpLightMasks[(int) Type]; + return mpLightMasks[static_cast(Type)]; } CModel* CDrawUtil::GetCubeModel() @@ -453,37 +417,37 @@ void CDrawUtil::InitSquare() EVertexAttribute::Tex4 | EVertexAttribute::Tex5 | EVertexAttribute::Tex6 | - EVertexAttribute::Tex7 ); + EVertexAttribute::Tex7); mSquareVertices->SetVertexCount(4); - CVector3f SquareVertices[] = { + static constexpr std::array SquareVertices{ CVector3f(-1.f, 1.f, 0.f), CVector3f( 1.f, 1.f, 0.f), CVector3f( 1.f, -1.f, 0.f), CVector3f(-1.f, -1.f, 0.f) }; - CVector3f SquareNormals[] = { + static constexpr std::array SquareNormals{ CVector3f(0.f, 0.f, 1.f), CVector3f(0.f, 0.f, 1.f), CVector3f(0.f, 0.f, 1.f), CVector3f(0.f, 0.f, 1.f) }; - CVector2f SquareTexCoords[] = { + static constexpr std::array SquareTexCoords{ CVector2f(0.f, 1.f), CVector2f(1.f, 1.f), CVector2f(1.f, 0.f), CVector2f(0.f, 0.f) }; - mSquareVertices->BufferAttrib(EVertexAttribute::Position, SquareVertices); - mSquareVertices->BufferAttrib(EVertexAttribute::Normal, SquareNormals); + mSquareVertices->BufferAttrib(EVertexAttribute::Position, SquareVertices.data()); + mSquareVertices->BufferAttrib(EVertexAttribute::Normal, SquareNormals.data()); for (uint32 iTex = 0; iTex < 8; iTex++) { - EVertexAttribute Attrib = (EVertexAttribute) (EVertexAttribute::Tex0 << iTex); - mSquareVertices->BufferAttrib(Attrib, SquareTexCoords); + const auto Attrib = static_cast(EVertexAttribute::Tex0 << iTex); + mSquareVertices->BufferAttrib(Attrib, SquareTexCoords.data()); } mSquareIndices.Reserve(4); @@ -528,7 +492,7 @@ void CDrawUtil::InitWireCube() mWireCubeVertices->AddVertex(CVector3f( 0.5f, 0.5f, 0.5f)); mWireCubeVertices->AddVertex(CVector3f(-0.5f, 0.5f, 0.5f)); - uint16 Indices[] = { + static constexpr std::array Indices{ 0, 1, 1, 2, 2, 3, @@ -542,7 +506,7 @@ void CDrawUtil::InitWireCube() 2, 6, 3, 5 }; - mWireCubeIndices.AddIndices(Indices, sizeof(Indices) / sizeof(uint16)); + mWireCubeIndices.AddIndices(Indices.data(), Indices.size()); mWireCubeIndices.SetPrimitiveType(GL_LINES); } diff --git a/src/Core/Render/CDrawUtil.h b/src/Core/Render/CDrawUtil.h index a8550480..c90e33c4 100644 --- a/src/Core/Render/CDrawUtil.h +++ b/src/Core/Render/CDrawUtil.h @@ -7,6 +7,7 @@ #include "Core/Resource/Model/CModel.h" #include "Core/Resource/CLight.h" +#include #include /** @@ -19,48 +20,48 @@ class CDrawUtil { // 7x7 Grid - static std::optional mGridVertices; - static CIndexBuffer mGridIndices; + static inline std::optional mGridVertices; + static inline CIndexBuffer mGridIndices; // Square - static std::optional mSquareVertices; - static CIndexBuffer mSquareIndices; + static inline std::optional mSquareVertices; + static inline CIndexBuffer mSquareIndices; // Line - static std::optional mLineVertices; - static CIndexBuffer mLineIndices; + static inline std::optional mLineVertices; + static inline CIndexBuffer mLineIndices; // Cube - static TResPtr mpCubeModel; + static inline TResPtr mpCubeModel; // Wire Cube - static std::optional mWireCubeVertices; - static CIndexBuffer mWireCubeIndices; + static inline std::optional mWireCubeVertices; + static inline CIndexBuffer mWireCubeIndices; // Sphere - static TResPtr mpSphereModel; - static TResPtr mpDoubleSidedSphereModel; + static inline TResPtr mpSphereModel; + static inline TResPtr mpDoubleSidedSphereModel; // Wire Sphere - static TResPtr mpWireSphereModel; + static inline TResPtr mpWireSphereModel; // Shaders - static CShader *mpColorShader; - static CShader *mpColorShaderLighting; - static CShader *mpBillboardShader; - static CShader *mpLightBillboardShader; - static CShader *mpTextureShader; - static CShader *mpCollisionShader; - static CShader *mpTextShader; + static inline CShader *mpColorShader = nullptr; + static inline CShader *mpColorShaderLighting = nullptr; + static inline CShader *mpBillboardShader = nullptr; + static inline CShader *mpLightBillboardShader = nullptr; + static inline CShader *mpTextureShader = nullptr; + static inline CShader *mpCollisionShader = nullptr; + static inline CShader *mpTextShader = nullptr; // Textures - static TResPtr mpCheckerTexture; + static inline TResPtr mpCheckerTexture; - static TResPtr mpLightTextures[4]; - static TResPtr mpLightMasks[4]; + static inline std::array, 4> mpLightTextures; + static inline std::array, 4> mpLightMasks; // Have all the above members been initialized? - static bool mDrawUtilInitialized; + static inline bool mDrawUtilInitialized = false; public: static void DrawGrid(CColor LineColor, CColor BoldLineColor);