diff --git a/src/WallpaperEngine/Assets/CTexture.cpp b/src/WallpaperEngine/Assets/CTexture.cpp index 77fa428..dd3359f 100644 --- a/src/WallpaperEngine/Assets/CTexture.cpp +++ b/src/WallpaperEngine/Assets/CTexture.cpp @@ -10,7 +10,7 @@ using namespace WallpaperEngine::Assets; -CTexture::CTexture (std::shared_ptr buffer) : m_resolution () { +CTexture::CTexture (const std::shared_ptr& buffer) : m_resolution () { // ensure the header is parsed const void* fileData = buffer.get (); this->m_header = parseHeader (static_cast (fileData)); @@ -124,7 +124,7 @@ GLint CTexture::setupInternalFormat () { case TextureFormat::ARGB8888: return GL_RGBA8; break; case TextureFormat::R8: return GL_R8; break; case TextureFormat::RG88: return GL_RG8; break; - default: delete this->m_header; sLog.exception ("Cannot determine texture format"); + default: sLog.exception ("Cannot determine texture format"); } } } @@ -157,14 +157,6 @@ void CTexture::setupOpenGLParameters (uint32_t textureID) { glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, 8.0f); } -CTexture::~CTexture () { - if (this->getHeader () == nullptr) - return; - - // free the header if it was allocated - delete this->getHeader (); -} - GLuint CTexture::getTextureID (uint32_t imageIndex) const { // ensure we do not go out of bounds if (imageIndex >= this->m_header->imageCount) @@ -204,14 +196,14 @@ ITexture::TextureFlags CTexture::getFlags () const { } const CTexture::TextureHeader* CTexture::getHeader () const { - return this->m_header; + return this->m_header.get (); } const glm::vec4* CTexture::getResolution () const { return &this->m_resolution; } -const std::vector& CTexture::getFrames () const { +const std::vector>& CTexture::getFrames () const { return this->getHeader ()->frames; } @@ -222,8 +214,9 @@ bool CTexture::isAnimated () const { CTexture::TextureMipmap::TextureMipmap () = default; CTexture::TextureMipmap::~TextureMipmap () { - if (this->compression == 1) + if (this->compression == 1) { delete this->compressedData; + } delete this->uncompressedData; } @@ -265,16 +258,7 @@ CTexture::TextureHeader::TextureHeader () : mipmapCount (0), isVideoMp4 (false) {} -CTexture::TextureHeader::~TextureHeader () { - for (const auto& [index, mipmaps] : this->images) - for (const auto cur : mipmaps) - delete cur; - - for (const auto& frame : this->frames) - delete frame; -} - -CTexture::TextureHeader* CTexture::parseHeader (const char* fileData) { +std::unique_ptr CTexture::parseHeader (const char* fileData) { // check the magic value on the header first if (strncmp (fileData, "TEXV0005", 9) != 0) sLog.exception ("unexpected texture container type: ", std::string_view (fileData, 9)); @@ -286,8 +270,7 @@ CTexture::TextureHeader* CTexture::parseHeader (const char* fileData) { // jump through the string again fileData += 9; - auto* header = new TextureHeader; - + auto header = std::make_unique (); const auto* pointer = reinterpret_cast (fileData); header->format = static_cast (*pointer++); @@ -327,19 +310,18 @@ CTexture::TextureHeader* CTexture::parseHeader (const char* fileData) { } else if (strncmp (fileData, "TEXB0001", 9) == 0) { header->containerVersion = ContainerVersion::TEXB0001; } else { - delete header; sLog.exception ("unknown texture format type: ", std::string_view (fileData, 9)); } for (uint32_t image = 0; image < header->imageCount; image++) { // read the number of mipmaps available for this image header->mipmapCount = *pointer++; - std::vector mipmaps; + std::vector> mipmaps; fileData = reinterpret_cast (pointer); for (uint32_t i = 0; i < header->mipmapCount; i++) - mipmaps.emplace_back (parseMipmap (header, &fileData)); + mipmaps.emplace_back (parseMipmap (header.get (), &fileData)); // add the pixmaps back header->images.insert (std::pair (image, mipmaps)); @@ -354,7 +336,6 @@ CTexture::TextureHeader* CTexture::parseHeader (const char* fileData) { } else if (strncmp (fileData, "TEXS0003", 9) == 0) { header->animatedVersion = AnimatedVersion::TEXS0003; } else { - delete header; sLog.exception ("found animation information of unknown type: ", std::string_view (fileData, 9)); } @@ -380,7 +361,7 @@ CTexture::TextureHeader* CTexture::parseHeader (const char* fileData) { // ensure gif width and height is right for TEXS0002 if (header->animatedVersion == AnimatedVersion::TEXS0002) { - const TextureFrame* first = *header->frames.begin (); + auto first = *header->frames.begin (); header->gifWidth = first->width1; header->gifHeight = first->height1; @@ -390,13 +371,13 @@ CTexture::TextureHeader* CTexture::parseHeader (const char* fileData) { return header; } -CTexture::TextureFrame* CTexture::parseAnimation (const char** originalFileData) { +std::shared_ptr CTexture::parseAnimation (const char** originalFileData) { const char* fileData = *originalFileData; // get back the pointer into integer const auto* pointer = reinterpret_cast (fileData); // start reading frame information - auto* frame = new TextureFrame (); + auto frame = std::make_shared (); frame->frameNumber = *pointer++; @@ -417,8 +398,8 @@ CTexture::TextureFrame* CTexture::parseAnimation (const char** originalFileData) return frame; } -CTexture::TextureMipmap* CTexture::parseMipmap (const TextureHeader* header, const char** originalFileData) { - auto* mipmap = new TextureMipmap (); +std::shared_ptr CTexture::parseMipmap (const TextureHeader* header, const char** originalFileData) { + auto mipmap = std::make_shared (); // get the current position const char* fileData = *originalFileData; diff --git a/src/WallpaperEngine/Assets/CTexture.h b/src/WallpaperEngine/Assets/CTexture.h index 6562bf4..4b58c49 100644 --- a/src/WallpaperEngine/Assets/CTexture.h +++ b/src/WallpaperEngine/Assets/CTexture.h @@ -114,7 +114,7 @@ class CTexture final : public ITexture { class TextureHeader { public: TextureHeader (); - ~TextureHeader (); + ~TextureHeader () = default; [[nodiscard]] bool isAnimated () const; @@ -147,14 +147,13 @@ class CTexture final : public ITexture { /** Number of mipmap levels on the texture */ uint32_t mipmapCount; /** List of mipmaps */ - std::map> images; + std::map>> images; /** List of animation frames */ - std::vector frames; + std::vector> frames; }; public: - explicit CTexture (std::shared_ptr fileData); - ~CTexture () override; + explicit CTexture (const std::shared_ptr& fileData); /** @inheritdoc */ [[nodiscard]] GLuint getTextureID (uint32_t imageIndex) const override; @@ -173,7 +172,7 @@ class CTexture final : public ITexture { /** @inheritdoc */ [[nodiscard]] const glm::vec4* getResolution () const override; /** @inheritdoc */ - [[nodiscard]] const std::vector& getFrames () const override; + [[nodiscard]] const std::vector>& getFrames () const override; /** @inheritdoc */ [[nodiscard]] bool isAnimated () const override; @@ -189,14 +188,14 @@ class CTexture final : public ITexture { * @param fileData The point at which to start reading data off from * @return */ - static TextureHeader* parseHeader (const char* fileData); + static std::unique_ptr parseHeader (const char* fileData); /** * Tries to parse an animation frame off the given data pointer * * @param originalFileData The point at which to start reading data off from * @return */ - static TextureFrame* parseAnimation (const char** originalFileData); + static std::shared_ptr parseAnimation (const char** originalFileData); /** * Tries to parse mipmap information off the given data pointer * @@ -204,7 +203,7 @@ class CTexture final : public ITexture { * @param fileData The point at which to start reading data off from * @return */ - static TextureMipmap* parseMipmap (const TextureHeader* header, const char** fileData); + static std::shared_ptr parseMipmap (const TextureHeader* header, const char** fileData); /** * Calculate's texture's resolution vec4 @@ -220,7 +219,7 @@ class CTexture final : public ITexture { void setupOpenGLParameters (uint32_t textureID); /** The texture header */ - TextureHeader* m_header; + std::unique_ptr m_header; /** OpenGL's texture ID */ GLuint* m_textureID; /** Resolution vector of the texture */ diff --git a/src/WallpaperEngine/Assets/ITexture.h b/src/WallpaperEngine/Assets/ITexture.h index 4f57b9a..0ac8439 100644 --- a/src/WallpaperEngine/Assets/ITexture.h +++ b/src/WallpaperEngine/Assets/ITexture.h @@ -1,8 +1,10 @@ #pragma once +#include +#include + #include #include -#include namespace WallpaperEngine::Assets { /** @@ -103,7 +105,7 @@ class ITexture { /** * @return The list of frames this texture has */ - [[nodiscard]] virtual const std::vector& getFrames () const = 0; + [[nodiscard]] virtual const std::vector>& getFrames () const = 0; /** * @return The texture's resolution vector */ diff --git a/src/WallpaperEngine/Render/CFBO.cpp b/src/WallpaperEngine/Render/CFBO.cpp index 0e972ca..bac8d05 100644 --- a/src/WallpaperEngine/Render/CFBO.cpp +++ b/src/WallpaperEngine/Render/CFBO.cpp @@ -65,7 +65,7 @@ CFBO::CFBO (std::string name, ITexture::TextureFormat format, ITexture::TextureF this->m_resolution = {textureWidth, textureHeight, realWidth, realHeight}; // create the textureframe entries - auto* frame = new TextureFrame; + auto frame = std::make_shared (); frame->frameNumber = 0; frame->frametime = 0; @@ -80,10 +80,6 @@ CFBO::CFBO (std::string name, ITexture::TextureFormat format, ITexture::TextureF } CFBO::~CFBO () { - // free all the resources - for (const auto* frame : this->m_frames) - delete frame; - // free opengl texture and framebuffer glDeleteTextures (1, &this->m_texture); glDeleteFramebuffers (1, &this->m_framebuffer); @@ -133,7 +129,7 @@ uint32_t CFBO::getRealHeight () const { return this->m_resolution.w; } -const std::vector& CFBO::getFrames () const { +const std::vector>& CFBO::getFrames () const { return this->m_frames; } diff --git a/src/WallpaperEngine/Render/CFBO.h b/src/WallpaperEngine/Render/CFBO.h index 3cd7944..8f703aa 100644 --- a/src/WallpaperEngine/Render/CFBO.h +++ b/src/WallpaperEngine/Render/CFBO.h @@ -26,7 +26,7 @@ class CFBO final : public ITexture { [[nodiscard]] uint32_t getTextureHeight (uint32_t imageIndex) const override; [[nodiscard]] uint32_t getRealWidth () const override; [[nodiscard]] uint32_t getRealHeight () const override; - [[nodiscard]] const std::vector& getFrames () const override; + [[nodiscard]] const std::vector>& getFrames () const override; [[nodiscard]] const glm::vec4* getResolution () const override; [[nodiscard]] bool isAnimated () const override; @@ -40,6 +40,6 @@ class CFBO final : public ITexture { ITexture::TextureFormat m_format; ITexture::TextureFlags m_flags; /** Placeholder for frames, FBOs only have ONE */ - std::vector m_frames; + std::vector> m_frames; }; } // namespace WallpaperEngine::Render