From 82bf71e7802365089b396fd3e3e44b10180cc418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sat, 2 Mar 2024 21:56:58 +0100 Subject: [PATCH 1/2] Refactor `IGraphics::CheckImageDivisibility` Put the context name in quotation marks in the warning message. Rename parameter `pFileName` to `pContextName`, as this name does not necessarily describe a file. Rename parameter `Img` to `Image` for consistency. --- src/engine/client/graphics_threaded.cpp | 29 +++++++++++++------------ src/engine/client/graphics_threaded.h | 2 +- src/engine/graphics.h | 2 +- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index 3f624be32..356fa9042 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -651,42 +651,43 @@ bool CGraphics_Threaded::LoadPng(CImageInfo &Image, const char *pFilename, int S return true; } -bool CGraphics_Threaded::CheckImageDivisibility(const char *pFileName, CImageInfo &Img, int DivX, int DivY, bool AllowResize) +bool CGraphics_Threaded::CheckImageDivisibility(const char *pContextName, CImageInfo &Image, int DivX, int DivY, bool AllowResize) { dbg_assert(DivX != 0 && DivY != 0, "Passing 0 to this function is not allowed."); bool ImageIsValid = true; - bool WidthBroken = Img.m_Width == 0 || (Img.m_Width % DivX) != 0; - bool HeightBroken = Img.m_Height == 0 || (Img.m_Height % DivY) != 0; + bool WidthBroken = Image.m_Width == 0 || (Image.m_Width % DivX) != 0; + bool HeightBroken = Image.m_Height == 0 || (Image.m_Height % DivY) != 0; if(WidthBroken || HeightBroken) { SWarning NewWarning; - str_format(NewWarning.m_aWarningMsg, sizeof(NewWarning.m_aWarningMsg), Localize("The width of texture %s is not divisible by %d, or the height is not divisible by %d, which might cause visual bugs."), pFileName, DivX, DivY); - + char aContextNameQuoted[128]; + str_format(aContextNameQuoted, sizeof(aContextNameQuoted), "\"%s\"", pContextName); + str_format(NewWarning.m_aWarningMsg, sizeof(NewWarning.m_aWarningMsg), + Localize("The width of texture %s is not divisible by %d, or the height is not divisible by %d, which might cause visual bugs."), aContextNameQuoted, DivX, DivY); m_vWarnings.emplace_back(NewWarning); - ImageIsValid = false; } - if(AllowResize && !ImageIsValid && Img.m_Width > 0 && Img.m_Height > 0) + if(AllowResize && !ImageIsValid && Image.m_Width > 0 && Image.m_Height > 0) { int NewWidth = DivX; int NewHeight = DivY; if(WidthBroken) { - NewWidth = maximum(HighestBit(Img.m_Width), DivX); + NewWidth = maximum(HighestBit(Image.m_Width), DivX); NewHeight = (NewWidth / DivX) * DivY; } else { - NewHeight = maximum(HighestBit(Img.m_Height), DivY); + NewHeight = maximum(HighestBit(Image.m_Height), DivY); NewWidth = (NewHeight / DivY) * DivX; } - uint8_t *pNewImg = ResizeImage(Img.m_pData, Img.m_Width, Img.m_Height, NewWidth, NewHeight, Img.PixelSize()); - free(Img.m_pData); - Img.m_pData = pNewImg; - Img.m_Width = NewWidth; - Img.m_Height = NewHeight; + uint8_t *pNewImage = ResizeImage(Image.m_pData, Image.m_Width, Image.m_Height, NewWidth, NewHeight, Image.PixelSize()); + free(Image.m_pData); + Image.m_pData = pNewImage; + Image.m_Width = NewWidth; + Image.m_Height = NewHeight; ImageIsValid = true; } diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h index 3255c67aa..06e82b920 100644 --- a/src/engine/client/graphics_threaded.h +++ b/src/engine/client/graphics_threaded.h @@ -983,7 +983,7 @@ public: IGraphics::CTextureHandle LoadTexture(const char *pFilename, int StorageType, int Flags = 0) override; bool LoadPng(CImageInfo &Image, const char *pFilename, int StorageType) override; - bool CheckImageDivisibility(const char *pFileName, CImageInfo &Img, int DivX, int DivY, bool AllowResize) override; + bool CheckImageDivisibility(const char *pContextName, CImageInfo &Image, int DivX, int DivY, bool AllowResize) override; bool IsImageFormatRGBA(const char *pFileName, CImageInfo &Img) override; void CopyTextureBufferSub(uint8_t *pDestBuffer, const CImageInfo &SourceImage, size_t SubOffsetX, size_t SubOffsetY, size_t SubCopyWidth, size_t SubCopyHeight) override; diff --git a/src/engine/graphics.h b/src/engine/graphics.h index 14017affd..8bc5d59b2 100644 --- a/src/engine/graphics.h +++ b/src/engine/graphics.h @@ -331,7 +331,7 @@ public: virtual bool LoadPng(CImageInfo &Image, const char *pFilename, int StorageType) = 0; - virtual bool CheckImageDivisibility(const char *pFileName, CImageInfo &Img, int DivX, int DivY, bool AllowResize) = 0; + virtual bool CheckImageDivisibility(const char *pContextName, CImageInfo &Image, int DivX, int DivY, bool AllowResize) = 0; virtual bool IsImageFormatRGBA(const char *pFileName, CImageInfo &Img) = 0; // destination and source buffer require to have the same width and height From 7f59a159e4c61b4da1f6c79f06a1d7b067732ec3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 3 Jul 2024 22:43:02 +0200 Subject: [PATCH 2/2] Refactor, rename `IGraphics::IsImageFormatRGBA` Rename `IGraphics::IsImageFormatRGBA` function to `IsImageFormatRgba`. Rename parameter `pFileName` to `pContextName`, as this name does not necessarily describe a file. Remove unnecessary check for parameter `pFileName` (now `pContextName`) being unset, which is and should never be the case. Rename parameter `Img` to `Image` for consistency. --- src/engine/client/graphics_threaded.cpp | 14 +++++--------- src/engine/client/graphics_threaded.h | 2 +- src/engine/graphics.h | 2 +- src/game/client/components/skins.cpp | 2 +- src/game/client/gameclient.cpp | 10 +++++----- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index 356fa9042..c4fe1bf8a 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -694,19 +694,15 @@ bool CGraphics_Threaded::CheckImageDivisibility(const char *pContextName, CImage return ImageIsValid; } -bool CGraphics_Threaded::IsImageFormatRGBA(const char *pFileName, CImageInfo &Img) +bool CGraphics_Threaded::IsImageFormatRgba(const char *pContextName, const CImageInfo &Image) { - if(Img.m_Format != CImageInfo::FORMAT_RGBA) + if(Image.m_Format != CImageInfo::FORMAT_RGBA) { SWarning NewWarning; - char aText[128]; - aText[0] = '\0'; - if(pFileName) - { - str_format(aText, sizeof(aText), "\"%s\"", pFileName); - } + char aContextNameQuoted[128]; + str_format(aContextNameQuoted, sizeof(aContextNameQuoted), "\"%s\"", pContextName); str_format(NewWarning.m_aWarningMsg, sizeof(NewWarning.m_aWarningMsg), - Localize("The format of texture %s is not RGBA which will cause visual bugs."), aText); + Localize("The format of texture %s is not RGBA which will cause visual bugs."), aContextNameQuoted); m_vWarnings.emplace_back(NewWarning); return false; } diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h index 06e82b920..156e9d6d5 100644 --- a/src/engine/client/graphics_threaded.h +++ b/src/engine/client/graphics_threaded.h @@ -984,7 +984,7 @@ public: bool LoadPng(CImageInfo &Image, const char *pFilename, int StorageType) override; bool CheckImageDivisibility(const char *pContextName, CImageInfo &Image, int DivX, int DivY, bool AllowResize) override; - bool IsImageFormatRGBA(const char *pFileName, CImageInfo &Img) override; + bool IsImageFormatRgba(const char *pContextName, const CImageInfo &Image) override; void CopyTextureBufferSub(uint8_t *pDestBuffer, const CImageInfo &SourceImage, size_t SubOffsetX, size_t SubOffsetY, size_t SubCopyWidth, size_t SubCopyHeight) override; void CopyTextureFromTextureBufferSub(uint8_t *pDestBuffer, size_t DestWidth, size_t DestHeight, const CImageInfo &SourceImage, size_t SrcSubOffsetX, size_t SrcSubOffsetY, size_t SrcSubCopyWidth, size_t SrcSubCopyHeight) override; diff --git a/src/engine/graphics.h b/src/engine/graphics.h index 8bc5d59b2..da9206349 100644 --- a/src/engine/graphics.h +++ b/src/engine/graphics.h @@ -332,7 +332,7 @@ public: virtual bool LoadPng(CImageInfo &Image, const char *pFilename, int StorageType) = 0; virtual bool CheckImageDivisibility(const char *pContextName, CImageInfo &Image, int DivX, int DivY, bool AllowResize) = 0; - virtual bool IsImageFormatRGBA(const char *pFileName, CImageInfo &Img) = 0; + virtual bool IsImageFormatRgba(const char *pContextName, const CImageInfo &Image) = 0; // destination and source buffer require to have the same width and height virtual void CopyTextureBufferSub(uint8_t *pDestBuffer, const CImageInfo &SourceImage, size_t SubOffsetX, size_t SubOffsetY, size_t SubCopyWidth, size_t SubCopyHeight) = 0; diff --git a/src/game/client/components/skins.cpp b/src/game/client/components/skins.cpp index 29f2ce3af..efb2f7ac1 100644 --- a/src/game/client/components/skins.cpp +++ b/src/game/client/components/skins.cpp @@ -157,7 +157,7 @@ const CSkin *CSkins::LoadSkin(const char *pName, CImageInfo &Info) log_error("skins", "Skin failed image divisibility: %s", pName); return nullptr; } - if(!Graphics()->IsImageFormatRGBA(pName, Info)) + if(!Graphics()->IsImageFormatRgba(pName, Info)) { log_error("skins", "Skin format is not RGBA: %s", pName); return nullptr; diff --git a/src/game/client/gameclient.cpp b/src/game/client/gameclient.cpp index af6ea217d..2b542ed6e 100644 --- a/src/game/client/gameclient.cpp +++ b/src/game/client/gameclient.cpp @@ -3032,7 +3032,7 @@ void CGameClient::LoadGameSkin(const char *pPath, bool AsDir) else LoadGameSkin(pPath, true); } - else if(PngLoaded && Graphics()->CheckImageDivisibility(aPath, ImgInfo, g_pData->m_aSprites[SPRITE_HEALTH_FULL].m_pSet->m_Gridx, g_pData->m_aSprites[SPRITE_HEALTH_FULL].m_pSet->m_Gridy, true) && Graphics()->IsImageFormatRGBA(aPath, ImgInfo)) + else if(PngLoaded && Graphics()->CheckImageDivisibility(aPath, ImgInfo, g_pData->m_aSprites[SPRITE_HEALTH_FULL].m_pSet->m_Gridx, g_pData->m_aSprites[SPRITE_HEALTH_FULL].m_pSet->m_Gridy, true) && Graphics()->IsImageFormatRgba(aPath, ImgInfo)) { m_GameSkin.m_SpriteHealthFull = Graphics()->LoadSpriteTexture(ImgInfo, &g_pData->m_aSprites[SPRITE_HEALTH_FULL]); m_GameSkin.m_SpriteHealthEmpty = Graphics()->LoadSpriteTexture(ImgInfo, &g_pData->m_aSprites[SPRITE_HEALTH_EMPTY]); @@ -3193,7 +3193,7 @@ void CGameClient::LoadEmoticonsSkin(const char *pPath, bool AsDir) else LoadEmoticonsSkin(pPath, true); } - else if(PngLoaded && Graphics()->CheckImageDivisibility(aPath, ImgInfo, g_pData->m_aSprites[SPRITE_OOP].m_pSet->m_Gridx, g_pData->m_aSprites[SPRITE_OOP].m_pSet->m_Gridy, true) && Graphics()->IsImageFormatRGBA(aPath, ImgInfo)) + else if(PngLoaded && Graphics()->CheckImageDivisibility(aPath, ImgInfo, g_pData->m_aSprites[SPRITE_OOP].m_pSet->m_Gridx, g_pData->m_aSprites[SPRITE_OOP].m_pSet->m_Gridy, true) && Graphics()->IsImageFormatRgba(aPath, ImgInfo)) { for(int i = 0; i < 16; ++i) m_EmoticonsSkin.m_aSpriteEmoticons[i] = Graphics()->LoadSpriteTexture(ImgInfo, &g_pData->m_aSprites[SPRITE_OOP + i]); @@ -3247,7 +3247,7 @@ void CGameClient::LoadParticlesSkin(const char *pPath, bool AsDir) else LoadParticlesSkin(pPath, true); } - else if(PngLoaded && Graphics()->CheckImageDivisibility(aPath, ImgInfo, g_pData->m_aSprites[SPRITE_PART_SLICE].m_pSet->m_Gridx, g_pData->m_aSprites[SPRITE_PART_SLICE].m_pSet->m_Gridy, true) && Graphics()->IsImageFormatRGBA(aPath, ImgInfo)) + else if(PngLoaded && Graphics()->CheckImageDivisibility(aPath, ImgInfo, g_pData->m_aSprites[SPRITE_PART_SLICE].m_pSet->m_Gridx, g_pData->m_aSprites[SPRITE_PART_SLICE].m_pSet->m_Gridy, true) && Graphics()->IsImageFormatRgba(aPath, ImgInfo)) { m_ParticlesSkin.m_SpriteParticleSlice = Graphics()->LoadSpriteTexture(ImgInfo, &g_pData->m_aSprites[SPRITE_PART_SLICE]); m_ParticlesSkin.m_SpriteParticleBall = Graphics()->LoadSpriteTexture(ImgInfo, &g_pData->m_aSprites[SPRITE_PART_BALL]); @@ -3336,7 +3336,7 @@ void CGameClient::LoadHudSkin(const char *pPath, bool AsDir) else LoadHudSkin(pPath, true); } - else if(PngLoaded && Graphics()->CheckImageDivisibility(aPath, ImgInfo, g_pData->m_aSprites[SPRITE_HUD_AIRJUMP].m_pSet->m_Gridx, g_pData->m_aSprites[SPRITE_HUD_AIRJUMP].m_pSet->m_Gridy, true) && Graphics()->IsImageFormatRGBA(aPath, ImgInfo)) + else if(PngLoaded && Graphics()->CheckImageDivisibility(aPath, ImgInfo, g_pData->m_aSprites[SPRITE_HUD_AIRJUMP].m_pSet->m_Gridx, g_pData->m_aSprites[SPRITE_HUD_AIRJUMP].m_pSet->m_Gridy, true) && Graphics()->IsImageFormatRgba(aPath, ImgInfo)) { m_HudSkin.m_SpriteHudAirjump = Graphics()->LoadSpriteTexture(ImgInfo, &g_pData->m_aSprites[SPRITE_HUD_AIRJUMP]); m_HudSkin.m_SpriteHudAirjumpEmpty = Graphics()->LoadSpriteTexture(ImgInfo, &g_pData->m_aSprites[SPRITE_HUD_AIRJUMP_EMPTY]); @@ -3411,7 +3411,7 @@ void CGameClient::LoadExtrasSkin(const char *pPath, bool AsDir) else LoadExtrasSkin(pPath, true); } - else if(PngLoaded && Graphics()->CheckImageDivisibility(aPath, ImgInfo, g_pData->m_aSprites[SPRITE_PART_SNOWFLAKE].m_pSet->m_Gridx, g_pData->m_aSprites[SPRITE_PART_SNOWFLAKE].m_pSet->m_Gridy, true) && Graphics()->IsImageFormatRGBA(aPath, ImgInfo)) + else if(PngLoaded && Graphics()->CheckImageDivisibility(aPath, ImgInfo, g_pData->m_aSprites[SPRITE_PART_SNOWFLAKE].m_pSet->m_Gridx, g_pData->m_aSprites[SPRITE_PART_SNOWFLAKE].m_pSet->m_Gridy, true) && Graphics()->IsImageFormatRgba(aPath, ImgInfo)) { m_ExtrasSkin.m_SpriteParticleSnowflake = Graphics()->LoadSpriteTexture(ImgInfo, &g_pData->m_aSprites[SPRITE_PART_SNOWFLAKE]); m_ExtrasSkin.m_aSpriteParticles[0] = m_ExtrasSkin.m_SpriteParticleSnowflake;