Add CTextureHandle::IsNullTexture, rename InvalidTexture

Rename `InvalidTexture` to `NullTexture` to avoid confusion because the null texture is not invalid. The function `CTextureHandle::IsNullTexture` is added to make it easier to check the result of using `IGraphics::LoadTexture`, which returns the null texture on error instead of an invalid texture handle. An assertion is added to ensure that `IsNullTexture` works correctly.
This commit is contained in:
Robert Müller 2023-11-04 00:11:58 +01:00
parent fdaa343219
commit 82ba8e0bb8
4 changed files with 20 additions and 18 deletions

View file

@ -289,7 +289,7 @@ void CGraphics_Threaded::FreeTextureIndex(CTextureHandle *pIndex)
int CGraphics_Threaded::UnloadTexture(CTextureHandle *pIndex) int CGraphics_Threaded::UnloadTexture(CTextureHandle *pIndex)
{ {
if(pIndex->Id() == m_InvalidTexture.Id()) if(pIndex->Id() == m_NullTexture.Id())
return 0; return 0;
if(!pIndex->IsValid()) if(!pIndex->IsValid())
@ -492,12 +492,12 @@ IGraphics::CTextureHandle CGraphics_Threaded::LoadTexture(const char *pFilename,
} }
} }
return m_InvalidTexture; return m_NullTexture;
} }
IGraphics::CTextureHandle CGraphics_Threaded::InvalidTexture() const IGraphics::CTextureHandle CGraphics_Threaded::NullTexture() const
{ {
return m_InvalidTexture; return m_NullTexture;
} }
bool CGraphics_Threaded::LoadTextTextures(size_t Width, size_t Height, CTextureHandle &TextTexture, CTextureHandle &TextOutlineTexture, void *pTextData, void *pTextOutlineData) bool CGraphics_Threaded::LoadTextTextures(size_t Width, size_t Height, CTextureHandle &TextTexture, CTextureHandle &TextOutlineTexture, void *pTextData, void *pTextOutlineData)
@ -2612,27 +2612,28 @@ int CGraphics_Threaded::Init()
const unsigned char aGreen[] = {0x00, 0xff, 0x00, 0xff}; const unsigned char aGreen[] = {0x00, 0xff, 0x00, 0xff};
const unsigned char aBlue[] = {0x00, 0x00, 0xff, 0xff}; const unsigned char aBlue[] = {0x00, 0x00, 0xff, 0xff};
const unsigned char aYellow[] = {0xff, 0xff, 0x00, 0xff}; const unsigned char aYellow[] = {0xff, 0xff, 0x00, 0xff};
constexpr size_t InvalidTextureDimension = 16; constexpr size_t NullTextureDimension = 16;
unsigned char aNullTextureData[InvalidTextureDimension * InvalidTextureDimension * PixelSize]; unsigned char aNullTextureData[NullTextureDimension * NullTextureDimension * PixelSize];
for(size_t y = 0; y < InvalidTextureDimension; ++y) for(size_t y = 0; y < NullTextureDimension; ++y)
{ {
for(size_t x = 0; x < InvalidTextureDimension; ++x) for(size_t x = 0; x < NullTextureDimension; ++x)
{ {
const unsigned char *pColor; const unsigned char *pColor;
if(x < InvalidTextureDimension / 2 && y < InvalidTextureDimension / 2) if(x < NullTextureDimension / 2 && y < NullTextureDimension / 2)
pColor = aRed; pColor = aRed;
else if(x >= InvalidTextureDimension / 2 && y < InvalidTextureDimension / 2) else if(x >= NullTextureDimension / 2 && y < NullTextureDimension / 2)
pColor = aGreen; pColor = aGreen;
else if(x < InvalidTextureDimension / 2 && y >= InvalidTextureDimension / 2) else if(x < NullTextureDimension / 2 && y >= NullTextureDimension / 2)
pColor = aBlue; pColor = aBlue;
else else
pColor = aYellow; pColor = aYellow;
mem_copy(&aNullTextureData[(y * InvalidTextureDimension + x) * PixelSize], pColor, PixelSize); mem_copy(&aNullTextureData[(y * NullTextureDimension + x) * PixelSize], pColor, PixelSize);
} }
} }
const int TextureLoadFlags = HasTextureArrays() ? IGraphics::TEXLOAD_TO_2D_ARRAY_TEXTURE : IGraphics::TEXLOAD_TO_3D_TEXTURE; const int TextureLoadFlags = HasTextureArrays() ? IGraphics::TEXLOAD_TO_2D_ARRAY_TEXTURE : IGraphics::TEXLOAD_TO_3D_TEXTURE;
m_InvalidTexture.Invalidate(); m_NullTexture.Invalidate();
m_InvalidTexture = LoadTextureRaw(InvalidTextureDimension, InvalidTextureDimension, CImageInfo::FORMAT_RGBA, aNullTextureData, TextureLoadFlags); m_NullTexture = LoadTextureRaw(NullTextureDimension, NullTextureDimension, CImageInfo::FORMAT_RGBA, aNullTextureData, TextureLoadFlags);
dbg_assert(m_NullTexture.IsNullTexture(), "Null texture invalid");
} }
ColorRGBA GPUInfoPrintColor{0.6f, 0.5f, 1.0f, 1.0f}; ColorRGBA GPUInfoPrintColor{0.6f, 0.5f, 1.0f, 1.0f};

View file

@ -816,7 +816,7 @@ class CGraphics_Threaded : public IEngineGraphics
bool m_DoScreenshot; bool m_DoScreenshot;
char m_aScreenshotName[IO_MAX_PATH_LENGTH]; char m_aScreenshotName[IO_MAX_PATH_LENGTH];
CTextureHandle m_InvalidTexture; CTextureHandle m_NullTexture;
std::vector<int> m_vTextureIndices; std::vector<int> m_vTextureIndices;
size_t m_FirstFreeTexture; size_t m_FirstFreeTexture;
@ -968,7 +968,7 @@ public:
int UnloadTexture(IGraphics::CTextureHandle *pIndex) override; int UnloadTexture(IGraphics::CTextureHandle *pIndex) override;
IGraphics::CTextureHandle LoadTextureRaw(size_t Width, size_t Height, CImageInfo::EImageFormat Format, const void *pData, int Flags, const char *pTexName = nullptr) override; IGraphics::CTextureHandle LoadTextureRaw(size_t Width, size_t Height, CImageInfo::EImageFormat Format, const void *pData, int Flags, const char *pTexName = nullptr) override;
int LoadTextureRawSub(IGraphics::CTextureHandle TextureID, int x, int y, size_t Width, size_t Height, CImageInfo::EImageFormat Format, const void *pData) override; int LoadTextureRawSub(IGraphics::CTextureHandle TextureID, int x, int y, size_t Width, size_t Height, CImageInfo::EImageFormat Format, const void *pData) override;
IGraphics::CTextureHandle InvalidTexture() const override; IGraphics::CTextureHandle NullTexture() const override;
bool LoadTextTextures(size_t Width, size_t Height, CTextureHandle &TextTexture, CTextureHandle &TextOutlineTexture, void *pTextData, void *pTextOutlineData) override; bool LoadTextTextures(size_t Width, size_t Height, CTextureHandle &TextTexture, CTextureHandle &TextOutlineTexture, void *pTextData, void *pTextOutlineData) override;
bool UnloadTextTextures(CTextureHandle &TextTexture, CTextureHandle &TextOutlineTexture) override; bool UnloadTextTextures(CTextureHandle &TextTexture, CTextureHandle &TextOutlineTexture) override;

View file

@ -263,6 +263,7 @@ public:
} }
bool IsValid() const { return Id() >= 0; } bool IsValid() const { return Id() >= 0; }
bool IsNullTexture() const { return Id() == 0; }
int Id() const { return m_Id; } int Id() const { return m_Id; }
void Invalidate() { m_Id = -1; } void Invalidate() { m_Id = -1; }
}; };
@ -337,7 +338,7 @@ public:
virtual CTextureHandle LoadTextureRaw(size_t Width, size_t Height, CImageInfo::EImageFormat Format, const void *pData, int Flags, const char *pTexName = nullptr) = 0; virtual CTextureHandle LoadTextureRaw(size_t Width, size_t Height, CImageInfo::EImageFormat Format, const void *pData, int Flags, const char *pTexName = nullptr) = 0;
virtual int LoadTextureRawSub(CTextureHandle TextureID, int x, int y, size_t Width, size_t Height, CImageInfo::EImageFormat Format, const void *pData) = 0; virtual int LoadTextureRawSub(CTextureHandle TextureID, int x, int y, size_t Width, size_t Height, CImageInfo::EImageFormat Format, const void *pData) = 0;
virtual CTextureHandle LoadTexture(const char *pFilename, int StorageType, int Flags = 0) = 0; virtual CTextureHandle LoadTexture(const char *pFilename, int StorageType, int Flags = 0) = 0;
virtual CTextureHandle InvalidTexture() const = 0; virtual CTextureHandle NullTexture() const = 0;
virtual void TextureSet(CTextureHandle Texture) = 0; virtual void TextureSet(CTextureHandle Texture) = 0;
void TextureClear() { TextureSet(CTextureHandle()); } void TextureClear() { TextureSet(CTextureHandle()); }

View file

@ -138,7 +138,7 @@ void CMapImages::OnMapLoadImpl(class CLayers *pLayers, IMap *pMap)
pMap->UnloadData(pImg->m_ImageData); pMap->UnloadData(pImg->m_ImageData);
} }
pMap->UnloadData(pImg->m_ImageName); pMap->UnloadData(pImg->m_ImageName);
ShowWarning = ShowWarning || m_aTextures[i].Id() == Graphics()->InvalidTexture().Id(); ShowWarning = ShowWarning || m_aTextures[i].IsNullTexture();
} }
if(ShowWarning) if(ShowWarning)
{ {