CDrawUtil: Make use of std::array

While we're at it, we can make several arrays constexpr.
This commit is contained in:
Lioncash 2020-06-16 18:04:07 -04:00
parent 43596280c5
commit 4d34589816
4 changed files with 38 additions and 73 deletions

View File

@ -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++)

View File

@ -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();

View File

@ -3,42 +3,6 @@
#include "Core/GameProject/CResourceStore.h"
#include <Common/Log.h>
#include <Common/Math/CTransform4f.h>
#include <iostream>
// ************ MEMBER INITIALIZATION ************
std::optional<CVertexBuffer> CDrawUtil::mGridVertices;
CIndexBuffer CDrawUtil::mGridIndices;
std::optional<CDynamicVertexBuffer> CDrawUtil::mSquareVertices;
CIndexBuffer CDrawUtil::mSquareIndices;
std::optional<CDynamicVertexBuffer> CDrawUtil::mLineVertices;
CIndexBuffer CDrawUtil::mLineIndices;
TResPtr<CModel> CDrawUtil::mpCubeModel;
std::optional<CVertexBuffer> CDrawUtil::mWireCubeVertices;
CIndexBuffer CDrawUtil::mWireCubeIndices;
TResPtr<CModel> CDrawUtil::mpSphereModel;
TResPtr<CModel> CDrawUtil::mpDoubleSidedSphereModel;
TResPtr<CModel> CDrawUtil::mpWireSphereModel;
CShader *CDrawUtil::mpColorShader;
CShader *CDrawUtil::mpColorShaderLighting;
CShader *CDrawUtil::mpBillboardShader;
CShader *CDrawUtil::mpLightBillboardShader;
CShader *CDrawUtil::mpTextureShader;
CShader *CDrawUtil::mpCollisionShader;
CShader *CDrawUtil::mpTextShader;
TResPtr<CTexture> CDrawUtil::mpCheckerTexture;
TResPtr<CTexture> CDrawUtil::mpLightTextures[4];
TResPtr<CTexture> 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<size_t>(Type)];
}
CTexture* CDrawUtil::GetLightMask(ELightType Type)
{
Init();
return mpLightMasks[(int) Type];
return mpLightMasks[static_cast<size_t>(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>(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<uint16, 24> 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);
}

View File

@ -7,6 +7,7 @@
#include "Core/Resource/Model/CModel.h"
#include "Core/Resource/CLight.h"
#include <array>
#include <optional>
/**
@ -19,48 +20,48 @@
class CDrawUtil
{
// 7x7 Grid
static std::optional<CVertexBuffer> mGridVertices;
static CIndexBuffer mGridIndices;
static inline std::optional<CVertexBuffer> mGridVertices;
static inline CIndexBuffer mGridIndices;
// Square
static std::optional<CDynamicVertexBuffer> mSquareVertices;
static CIndexBuffer mSquareIndices;
static inline std::optional<CDynamicVertexBuffer> mSquareVertices;
static inline CIndexBuffer mSquareIndices;
// Line
static std::optional<CDynamicVertexBuffer> mLineVertices;
static CIndexBuffer mLineIndices;
static inline std::optional<CDynamicVertexBuffer> mLineVertices;
static inline CIndexBuffer mLineIndices;
// Cube
static TResPtr<CModel> mpCubeModel;
static inline TResPtr<CModel> mpCubeModel;
// Wire Cube
static std::optional<CVertexBuffer> mWireCubeVertices;
static CIndexBuffer mWireCubeIndices;
static inline std::optional<CVertexBuffer> mWireCubeVertices;
static inline CIndexBuffer mWireCubeIndices;
// Sphere
static TResPtr<CModel> mpSphereModel;
static TResPtr<CModel> mpDoubleSidedSphereModel;
static inline TResPtr<CModel> mpSphereModel;
static inline TResPtr<CModel> mpDoubleSidedSphereModel;
// Wire Sphere
static TResPtr<CModel> mpWireSphereModel;
static inline TResPtr<CModel> 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<CTexture> mpCheckerTexture;
static inline TResPtr<CTexture> mpCheckerTexture;
static TResPtr<CTexture> mpLightTextures[4];
static TResPtr<CTexture> mpLightMasks[4];
static inline std::array<TResPtr<CTexture>, 4> mpLightTextures;
static inline std::array<TResPtr<CTexture>, 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);