From 8efc78ebe7f083f1335a84fe6f7e79c33c9081c8 Mon Sep 17 00:00:00 2001 From: mathusummut Date: Wed, 3 Oct 2018 23:08:44 +0200 Subject: [PATCH] Enforced G3D enum type safety --- source/shared_lib/include/graphics/model.h | 2 +- .../include/graphics/model_header.h | 75 +++++++++++++++++-- source/shared_lib/sources/graphics/model.cpp | 16 ++-- 3 files changed, 75 insertions(+), 18 deletions(-) diff --git a/source/shared_lib/include/graphics/model.h b/source/shared_lib/include/graphics/model.h index 56dc4f898..aa29dfb0e 100644 --- a/source/shared_lib/include/graphics/model.h +++ b/source/shared_lib/include/graphics/model.h @@ -76,7 +76,7 @@ namespace Shared { bool glow; uint8 factionColorOpacity; - uint32 textureFlags; + MeshTexture textureFlags; InterpolationData *interpolationData; TextureManager *textureManager; diff --git a/source/shared_lib/include/graphics/model_header.h b/source/shared_lib/include/graphics/model_header.h index 3c00d44fa..0f91ef79e 100644 --- a/source/shared_lib/include/graphics/model_header.h +++ b/source/shared_lib/include/graphics/model_header.h @@ -40,22 +40,64 @@ namespace Shared { mtMorphMesh }; - enum MeshPropertyFlag { + enum MeshPropertyFlag : uint32 { mpfNone = 0, mpfCustomColor = 1, mpfTwoSided = 2, mpfNoSelect = 4, mpfGlow = 8 }; + inline MeshPropertyFlag operator~ (MeshPropertyFlag a) { + return (MeshPropertyFlag) ~(uint32) a; + } + inline MeshPropertyFlag operator| (MeshPropertyFlag a, MeshPropertyFlag b) { + return (MeshPropertyFlag) ((uint32) a | (uint32) b); + } + inline MeshPropertyFlag operator& (MeshPropertyFlag a, MeshPropertyFlag b) { + return (MeshPropertyFlag) ((uint32) a & (uint32) b); + } + inline MeshPropertyFlag operator^ (MeshPropertyFlag a, MeshPropertyFlag b) { + return (MeshPropertyFlag) ((uint32) a ^ (uint32) b); + } + inline MeshPropertyFlag& operator|= (MeshPropertyFlag& a, MeshPropertyFlag b) { + return (MeshPropertyFlag&) ((uint32&) a |= (uint32) b); + } + inline MeshPropertyFlag& operator&= (MeshPropertyFlag& a, MeshPropertyFlag b) { + return (MeshPropertyFlag&) ((uint32&) a &= (uint32) b); + } + inline MeshPropertyFlag& operator^= (MeshPropertyFlag& a, MeshPropertyFlag b) { + return (MeshPropertyFlag&) ((uint32&) a ^= (uint32) b); + } static const int MESH_TEXTURE_COUNT = 3; - enum MeshTexture { + enum MeshTexture : uint32 { mtNone = 0, mtDiffuse = 1, mtSpecular = 2, mtNormal = 4 }; + inline MeshTexture operator~ (MeshTexture a) { + return (MeshTexture) ~(uint32) a; + } + inline MeshTexture operator| (MeshTexture a, MeshTexture b) { + return (MeshTexture) ((uint32) a | (uint32) b); + } + inline MeshTexture operator& (MeshTexture a, MeshTexture b) { + return (MeshTexture) ((uint32) a & (uint32) b); + } + inline MeshTexture operator^ (MeshTexture a, MeshTexture b) { + return (MeshTexture) ((uint32) a ^ (uint32) b); + } + inline MeshTexture& operator|= (MeshTexture& a, MeshTexture b) { + return (MeshTexture&) ((uint32&) a |= (uint32) b); + } + inline MeshTexture& operator&= (MeshTexture& a, MeshTexture b) { + return (MeshTexture&) ((uint32&) a &= (uint32) b); + } + inline MeshTexture& operator^= (MeshTexture& a, MeshTexture b) { + return (MeshTexture&) ((uint32&) a ^= (uint32) b); + } const int meshTextureChannelCount[] = { 4, 1, 3 }; @@ -71,8 +113,8 @@ namespace Shared { float32 specularColor[3]; float32 specularPower; float32 opacity; - uint32 properties; - uint32 textures; + MeshPropertyFlag properties; + MeshTexture textures; }; #pragma pack(pop) @@ -84,12 +126,33 @@ namespace Shared { uint32 meshCount; }; - enum MeshPropertyV3 { + enum MeshPropertyV3 : uint32 { mp3None = 0, mp3NoTexture = 1, mp3TwoSided = 2, mp3CustomColor = 4 }; + inline MeshPropertyV3 operator~ (MeshPropertyV3 a) { + return (MeshPropertyV3) ~(uint32) a; + } + inline MeshPropertyV3 operator| (MeshPropertyV3 a, MeshPropertyV3 b) { + return (MeshPropertyV3) ((uint32) a | (uint32) b); + } + inline MeshPropertyV3 operator& (MeshPropertyV3 a, MeshPropertyV3 b) { + return (MeshPropertyV3) ((uint32) a & (uint32) b); + } + inline MeshPropertyV3 operator^ (MeshPropertyV3 a, MeshPropertyV3 b) { + return (MeshPropertyV3) ((uint32) a ^ (uint32) b); + } + inline MeshPropertyV3& operator|= (MeshPropertyV3& a, MeshPropertyV3 b) { + return (MeshPropertyV3&) ((uint32&) a |= (uint32) b); + } + inline MeshPropertyV3& operator&= (MeshPropertyV3& a, MeshPropertyV3 b) { + return (MeshPropertyV3&) ((uint32&) a &= (uint32) b); + } + inline MeshPropertyV3& operator^= (MeshPropertyV3& a, MeshPropertyV3 b) { + return (MeshPropertyV3&) ((uint32&) a ^= (uint32) b); + } struct MeshHeaderV3 { uint32 vertexFrameCount; @@ -98,7 +161,7 @@ namespace Shared { uint32 colorFrameCount; uint32 pointCount; uint32 indexCount; - uint32 properties; + MeshPropertyV3 properties; uint8 texName[64]; }; diff --git a/source/shared_lib/sources/graphics/model.cpp b/source/shared_lib/sources/graphics/model.cpp index 62cdba71e..3b62d1318 100644 --- a/source/shared_lib/sources/graphics/model.cpp +++ b/source/shared_lib/sources/graphics/model.cpp @@ -228,7 +228,7 @@ namespace Shared { glow = false; factionColorOpacity = 255; - textureFlags = 0; + textureFlags = mtNone; hasBuiltVBOs = false; // Vertex Buffer Object Names @@ -447,10 +447,7 @@ namespace Shared { if (SystemFlags::VERBOSE_MODE_ENABLED) printf("Load v2, this = %p Found meshHeader.hasTexture = %d, texName [%s] mtDiffuse = %d meshIndex = %d modelFile [%s]\n", this, meshHeader.hasTexture, toLower(reinterpret_cast(meshHeader.texName)).c_str(), mtDiffuse, meshIndex, modelFile.c_str()); - textureFlags = 0; - if (meshHeader.hasTexture) { - textureFlags = 1; - } + textureFlags = meshHeader.hasTexture ? mtDiffuse : mtNone; //texture if (meshHeader.hasTexture && textureManager != NULL) { @@ -586,10 +583,7 @@ namespace Shared { noSelect = false; glow = false; - textureFlags = 0; - if ((meshHeader.properties & mp3NoTexture) != mp3NoTexture) { - textureFlags = 1; - } + textureFlags = ((meshHeader.properties & mp3NoTexture) == mp3NoTexture) ? mtNone : mtDiffuse; if (SystemFlags::VERBOSE_MODE_ENABLED) printf("Load v3, this = %p Found meshHeader.properties = %d, textureFlags = %d, texName [%s] mtDiffuse = %d meshIndex = %d modelFile [%s]\n", this, meshHeader.properties, textureFlags, toLower(reinterpret_cast(meshHeader.texName)).c_str(), mtDiffuse, meshIndex, modelFile.c_str()); @@ -888,9 +882,9 @@ namespace Shared { meshHeader.opacity = opacity; //properties - meshHeader.properties = 0; + meshHeader.properties = mpfNone; if (customColor) { - meshHeader.properties |= (255 - (factionColorOpacity << 24)) | mpfCustomColor; + meshHeader.properties |= static_cast(255 - (factionColorOpacity << 24)) | mpfCustomColor; } if (twoSided) { meshHeader.properties |= mpfTwoSided;