diff --git a/src/WallpaperEngine/Assets/CCombinedContainer.cpp b/src/WallpaperEngine/Assets/CCombinedContainer.cpp index 50d3995..3f7cb41 100644 --- a/src/WallpaperEngine/Assets/CCombinedContainer.cpp +++ b/src/WallpaperEngine/Assets/CCombinedContainer.cpp @@ -41,7 +41,7 @@ std::filesystem::path CCombinedContainer::resolveRealFile (const std::filesystem throw CAssetLoadException (filename, "Cannot resolve file in any of the containers"); } -const uint8_t* CCombinedContainer::readFile (const std::filesystem::path& filename, uint32_t* length) const { +std::shared_ptr CCombinedContainer::readFile (const std::filesystem::path& filename, uint32_t* length) const { for (const auto cur : this->m_containers) { try { // try to read the file on the current container, if the file doesn't exists diff --git a/src/WallpaperEngine/Assets/CCombinedContainer.h b/src/WallpaperEngine/Assets/CCombinedContainer.h index 338e8a9..1927d52 100644 --- a/src/WallpaperEngine/Assets/CCombinedContainer.h +++ b/src/WallpaperEngine/Assets/CCombinedContainer.h @@ -30,7 +30,7 @@ class CCombinedContainer final : public CContainer { /** @inheritdoc */ [[nodiscard]] std::filesystem::path resolveRealFile (const std::filesystem::path& filename) const override; /** @inheritdoc */ - [[nodiscard]] const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const override; + [[nodiscard]] std::shared_ptr readFile (const std::filesystem::path& filename, uint32_t* length) const override; private: /** The list of containers to search files off from */ diff --git a/src/WallpaperEngine/Assets/CContainer.cpp b/src/WallpaperEngine/Assets/CContainer.cpp index a711981..c30c3c4 100644 --- a/src/WallpaperEngine/Assets/CContainer.cpp +++ b/src/WallpaperEngine/Assets/CContainer.cpp @@ -13,19 +13,17 @@ std::filesystem::path CContainer::resolveRealFile (const std::filesystem::path& throw CAssetLoadException (filename, "Cannot resolve physical file in this container"); } -const ITexture* CContainer::readTexture (const std::filesystem::path& filename) const { +std::shared_ptr CContainer::readTexture (const std::filesystem::path& filename) const { // get the texture's filename (usually .tex) std::filesystem::path texture = "materials" / std::filesystem::path (filename.string ().append (".tex")); - const auto* textureContents = this->readFile (texture, nullptr); - const auto* result = new CTexture (textureContents); + const auto textureContents = this->readFile (texture, nullptr); + const auto result = std::make_shared (textureContents); #if !NDEBUG glObjectLabel (GL_TEXTURE, result->getTextureID (0), -1, texture.c_str ()); #endif /* NDEBUG */ - delete[] textureContents; - return result; } @@ -74,20 +72,7 @@ std::string CContainer::readIncludeShader (const std::filesystem::path& filename std::string CContainer::readFileAsString (const std::filesystem::path& filename) const { uint32_t length = 0; - // read file contents and allocate a buffer for a string - const auto* contents = this->readFile (filename, &length); - auto* buffer = new char [length + 1]; - - // ensure there's a 0 at the end - memset (buffer, 0, length + 1); - // copy over the data - memcpy (buffer, contents, length); - // now build the std::string to use - std::string result = buffer; - - // free the intermediate buffer used to generate the std::string - delete [] buffer; - delete [] contents; - - return result; + return { + reinterpret_cast (this->readFile (filename, &length).get ()), length + }; } \ No newline at end of file diff --git a/src/WallpaperEngine/Assets/CContainer.h b/src/WallpaperEngine/Assets/CContainer.h index 17b47e6..e77228c 100644 --- a/src/WallpaperEngine/Assets/CContainer.h +++ b/src/WallpaperEngine/Assets/CContainer.h @@ -32,7 +32,7 @@ class CContainer { * * @return */ - [[nodiscard]] virtual const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const = 0; + [[nodiscard]] virtual std::shared_ptr readFile (const std::filesystem::path& filename, uint32_t* length) const = 0; /** * Wrapper for readFile, appends the texture extension at the end of the filename @@ -41,7 +41,7 @@ class CContainer { * * @return */ - [[nodiscard]] const ITexture* readTexture (const std::filesystem::path& filename) const; + [[nodiscard]] std::shared_ptr readTexture (const std::filesystem::path& filename) const; /** * Wrapper for readFile, checks for compat versions of the given shader file diff --git a/src/WallpaperEngine/Assets/CDirectory.cpp b/src/WallpaperEngine/Assets/CDirectory.cpp index 9103995..2d32768 100644 --- a/src/WallpaperEngine/Assets/CDirectory.cpp +++ b/src/WallpaperEngine/Assets/CDirectory.cpp @@ -50,12 +50,11 @@ std::filesystem::path CDirectory::resolveRealFile (const std::filesystem::path& } } -const uint8_t* CDirectory::readFile (const std::filesystem::path& filename, uint32_t* length) const { +std::shared_ptr CDirectory::readFile (const std::filesystem::path& filename, uint32_t* length) const { std::filesystem::path final = this->resolveRealFile (filename); FILE* fp = fopen (final.c_str (), "rb"); - if (fp == nullptr) { throw CAssetLoadException (filename, "Cannot open file for reading"); } @@ -67,10 +66,9 @@ const uint8_t* CDirectory::readFile (const std::filesystem::path& filename, uint fseek (fp, 0, SEEK_SET); // now read the whole file - auto* contents = new uint8_t [size]; + std::shared_ptr contents = std::shared_ptr(new uint8_t [size]); - if (fread (contents, size, 1, fp) != 1) { - delete [] contents; + if (fread (contents.get(), size, 1, fp) != 1) { throw CAssetLoadException (filename, "Unexpected error when reading the file"); } diff --git a/src/WallpaperEngine/Assets/CDirectory.h b/src/WallpaperEngine/Assets/CDirectory.h index 0fb9ed7..1100d8f 100644 --- a/src/WallpaperEngine/Assets/CDirectory.h +++ b/src/WallpaperEngine/Assets/CDirectory.h @@ -19,7 +19,7 @@ class CDirectory final : public CContainer { /** @inheritdoc */ [[nodiscard]] std::filesystem::path resolveRealFile (const std::filesystem::path& filename) const override; /** @inheritdoc */ - [[nodiscard]] const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const override; + [[nodiscard]] std::shared_ptr readFile (const std::filesystem::path& filename, uint32_t* length) const override; private: /** The basepath for the directory */ diff --git a/src/WallpaperEngine/Assets/CFileEntry.h b/src/WallpaperEngine/Assets/CFileEntry.h index 3db710f..a5724bd 100644 --- a/src/WallpaperEngine/Assets/CFileEntry.h +++ b/src/WallpaperEngine/Assets/CFileEntry.h @@ -8,14 +8,13 @@ namespace WallpaperEngine::Assets { */ class CFileEntry { public: - CFileEntry (const uint8_t* address, uint32_t length) : address (address), length (length) {} - - ~CFileEntry () { - delete [] address; - } + CFileEntry (std::shared_ptr content, uint32_t length) : + content (content), + length (length) {} + ~CFileEntry() = default; /** File contents */ - const uint8_t* address; + std::shared_ptr content; /** File length */ uint32_t length; }; diff --git a/src/WallpaperEngine/Assets/CPackage.cpp b/src/WallpaperEngine/Assets/CPackage.cpp index 99fbe0b..26ec945 100644 --- a/src/WallpaperEngine/Assets/CPackage.cpp +++ b/src/WallpaperEngine/Assets/CPackage.cpp @@ -24,7 +24,7 @@ CPackage::CPackage (std::filesystem::path path) : m_path (std::move (path)) { this->init (); } -const uint8_t* CPackage::readFile (const std::filesystem::path& filename, uint32_t* length) const { +std::shared_ptr CPackage::readFile (const std::filesystem::path& filename, uint32_t* length) const { const auto it = this->m_contents.find (filename); if (it == this->m_contents.end ()) @@ -34,12 +34,7 @@ const uint8_t* CPackage::readFile (const std::filesystem::path& filename, uint32 if (length != nullptr) *length = it->second->length; - // clone original first - auto* result = new uint8_t [it->second->length]; - - memcpy (result, it->second->address, it->second->length); - - return result; + return it->second->content; } void CPackage::init () { @@ -128,15 +123,13 @@ void CPackage::loadFiles (FILE* fp) { sLog.exception ("Cannot find file ", cur.filename, " from package ", this->m_path); // allocate memory for the file's contents and read it from the file - auto* fileContents = new uint8_t [cur.length]; - - if (fread (fileContents, cur.length, 1, fp) != 1) { - delete [] fileContents; + std::shared_ptr contents = std::shared_ptr(new uint8_t [cur.length]); + if (fread (contents.get(), cur.length, 1, fp) != 1) { sLog.exception ("Cannot read file ", cur.filename, " contents from package ", this->m_path); } // add the file to the map - this->m_contents.insert_or_assign (cur.filename, new CFileEntry (fileContents, cur.length)); + this->m_contents.insert_or_assign (cur.filename, std::make_unique (contents, cur.length)); } } \ No newline at end of file diff --git a/src/WallpaperEngine/Assets/CPackage.h b/src/WallpaperEngine/Assets/CPackage.h index 2e64788..88b42e7 100644 --- a/src/WallpaperEngine/Assets/CPackage.h +++ b/src/WallpaperEngine/Assets/CPackage.h @@ -20,7 +20,7 @@ class CPackage final : public CContainer { public: explicit CPackage (std::filesystem::path path); - [[nodiscard]] const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const override; + [[nodiscard]] std::shared_ptr readFile (const std::filesystem::path& filename, uint32_t* length) const override; protected: /** @@ -63,6 +63,6 @@ class CPackage final : public CContainer { /** The path to the package file */ std::filesystem::path m_path; /** Contents of the package file */ - std::map m_contents; + std::map> m_contents; }; } // namespace WallpaperEngine::Assets diff --git a/src/WallpaperEngine/Assets/CTexture.cpp b/src/WallpaperEngine/Assets/CTexture.cpp index d4ca5ca..77fa428 100644 --- a/src/WallpaperEngine/Assets/CTexture.cpp +++ b/src/WallpaperEngine/Assets/CTexture.cpp @@ -10,8 +10,9 @@ using namespace WallpaperEngine::Assets; -CTexture::CTexture (const void* fileData) : m_resolution () { +CTexture::CTexture (std::shared_ptr buffer) : m_resolution () { // ensure the header is parsed + const void* fileData = buffer.get (); this->m_header = parseHeader (static_cast (fileData)); this->setupResolution (); GLint internalFormat = this->setupInternalFormat(); diff --git a/src/WallpaperEngine/Assets/CTexture.h b/src/WallpaperEngine/Assets/CTexture.h index 4cce5c9..6562bf4 100644 --- a/src/WallpaperEngine/Assets/CTexture.h +++ b/src/WallpaperEngine/Assets/CTexture.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -152,7 +153,7 @@ class CTexture final : public ITexture { }; public: - explicit CTexture (const void* fileData); + explicit CTexture (std::shared_ptr fileData); ~CTexture () override; /** @inheritdoc */ diff --git a/src/WallpaperEngine/Assets/CVirtualContainer.cpp b/src/WallpaperEngine/Assets/CVirtualContainer.cpp index d6b026b..7851326 100644 --- a/src/WallpaperEngine/Assets/CVirtualContainer.cpp +++ b/src/WallpaperEngine/Assets/CVirtualContainer.cpp @@ -5,23 +5,15 @@ using namespace WallpaperEngine::Assets; -CVirtualContainer::~CVirtualContainer() { - for (auto& [_, entry] : this->m_virtualFiles) { - delete entry; - } - - this->m_virtualFiles.clear(); -} - -void CVirtualContainer::add (const std::filesystem::path& filename, const uint8_t* contents, uint32_t length) { - this->m_virtualFiles.insert (std::pair (filename, new CFileEntry (contents, length))); +void CVirtualContainer::add (const std::filesystem::path& filename, const std::shared_ptr& contents, uint32_t length) { + this->m_virtualFiles.insert (std::pair (filename, std::make_unique (contents, length))); } void CVirtualContainer::add (const std::filesystem::path& filename, const std::string& contents) { - auto* copy = new uint8_t [contents.length () + 1]; + std::shared_ptr copy = std::shared_ptr (new uint8_t [contents.length () + 1]); // copy the text AND the \0 - memcpy (copy, contents.c_str (), contents.length () + 1); + memcpy (copy.get(), contents.c_str (), contents.length () + 1); // finally add to the container this->add (filename, copy, contents.length () + 1); @@ -35,7 +27,7 @@ void CVirtualContainer::add (const std::filesystem::path& filename, const json& this->add (filename, contents.dump ()); } -const uint8_t* CVirtualContainer::readFile (const std::filesystem::path& filename, uint32_t* length) const { +std::shared_ptr CVirtualContainer::readFile (const std::filesystem::path& filename, uint32_t* length) const { const auto cur = this->m_virtualFiles.find (filename); if (cur == this->m_virtualFiles.end ()) @@ -45,9 +37,5 @@ const uint8_t* CVirtualContainer::readFile (const std::filesystem::path& filenam *length = cur->second->length; // clone original first - auto* result = new uint8_t [cur->second->length]; - - memcpy (result, cur->second->address, cur->second->length); - - return result; + return cur->second->content; } \ No newline at end of file diff --git a/src/WallpaperEngine/Assets/CVirtualContainer.h b/src/WallpaperEngine/Assets/CVirtualContainer.h index 91e482f..8643f59 100644 --- a/src/WallpaperEngine/Assets/CVirtualContainer.h +++ b/src/WallpaperEngine/Assets/CVirtualContainer.h @@ -16,8 +16,6 @@ using json = nlohmann::json; */ class CVirtualContainer final : public CContainer { public: - ~CVirtualContainer() override; - /** * Adds a new file to the virtual container * @@ -25,7 +23,7 @@ class CVirtualContainer final : public CContainer { * @param contents * @param length */ - void add (const std::filesystem::path& filename, const uint8_t* contents, uint32_t length); + void add (const std::filesystem::path& filename, const std::shared_ptr& contents, uint32_t length); /** * Adds a new file to the virtual container @@ -50,10 +48,10 @@ class CVirtualContainer final : public CContainer { void add (const std::filesystem::path& filename, const json& contents); /** @inheritdoc */ - const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const override; + std::shared_ptr readFile (const std::filesystem::path& filename, uint32_t* length) const override; private: /** The recorded files in this virtual container */ - std::map m_virtualFiles; + std::map> m_virtualFiles; }; } // namespace WallpaperEngine::Assets \ No newline at end of file diff --git a/src/WallpaperEngine/Audio/CAudioStream.cpp b/src/WallpaperEngine/Audio/CAudioStream.cpp index 3f2649e..d33b622 100644 --- a/src/WallpaperEngine/Audio/CAudioStream.cpp +++ b/src/WallpaperEngine/Audio/CAudioStream.cpp @@ -68,7 +68,7 @@ static int audio_read_data_callback (void* streamarg, uint8_t* buffer, int buffe buffer_size = FFMIN (buffer_size, left); - memcpy (buffer, stream->getBuffer () + stream->getPosition (), buffer_size); + memcpy (buffer, stream->getBuffer ().get() + stream->getPosition (), buffer_size); // update position stream->setPosition (stream->getPosition () + buffer_size); @@ -96,7 +96,7 @@ CAudioStream::CAudioStream (CAudioContext& context, const std::string& filename) this->loadCustomContent (filename.c_str ()); } -CAudioStream::CAudioStream (CAudioContext& context, const uint8_t* buffer, uint32_t length) : +CAudioStream::CAudioStream (CAudioContext& context, std::shared_ptr buffer, uint32_t length) : m_swrctx (nullptr), m_audioStream(NO_AUDIO_STREAM), m_audioContext (context) { @@ -339,7 +339,7 @@ bool CAudioStream::isRepeat () const { return this->m_repeat; } -const uint8_t* CAudioStream::getBuffer () { +std::shared_ptr CAudioStream::getBuffer () { return this->m_buffer; } diff --git a/src/WallpaperEngine/Audio/CAudioStream.h b/src/WallpaperEngine/Audio/CAudioStream.h index f3158e5..2dd2b8c 100644 --- a/src/WallpaperEngine/Audio/CAudioStream.h +++ b/src/WallpaperEngine/Audio/CAudioStream.h @@ -33,7 +33,7 @@ class CAudioContext; class CAudioStream { public: CAudioStream (CAudioContext& context, const std::string& filename); - CAudioStream (CAudioContext& context, const uint8_t* buffer, uint32_t length); + CAudioStream (CAudioContext& context, std::shared_ptr buffer, uint32_t length); CAudioStream (CAudioContext& audioContext, AVCodecContext* context); ~CAudioStream (); @@ -84,7 +84,7 @@ class CAudioStream { /** * @return The file data buffer */ - [[nodiscard]] const uint8_t* getBuffer (); + [[nodiscard]] std::shared_ptr getBuffer (); /** * @return The length of the file data buffer */ @@ -181,7 +181,7 @@ class CAudioStream { /** The stream index for the audio being played */ int m_audioStream; /** File data pointer */ - const uint8_t* m_buffer {}; + std::shared_ptr m_buffer; /** The length of the file data pointer */ uint32_t m_length {}; /** The read position on the file data pointer */ diff --git a/src/WallpaperEngine/Render/CRenderContext.cpp b/src/WallpaperEngine/Render/CRenderContext.cpp index 40b4a31..da1fd11 100644 --- a/src/WallpaperEngine/Render/CRenderContext.cpp +++ b/src/WallpaperEngine/Render/CRenderContext.cpp @@ -61,7 +61,7 @@ const Drivers::Output::COutput& CRenderContext::getOutput () const { return this->m_driver.getOutput (); } -const ITexture* CRenderContext::resolveTexture (const std::string& name) { +std::shared_ptr CRenderContext::resolveTexture (const std::string& name) { return this->m_textureCache->resolve (name); } diff --git a/src/WallpaperEngine/Render/CRenderContext.h b/src/WallpaperEngine/Render/CRenderContext.h index e321fdb..26fb607 100644 --- a/src/WallpaperEngine/Render/CRenderContext.h +++ b/src/WallpaperEngine/Render/CRenderContext.h @@ -2,6 +2,7 @@ #include #include +#include #include "CTextureCache.h" #include "WallpaperEngine/Application/CWallpaperApplication.h" @@ -40,7 +41,7 @@ class CRenderContext { [[nodiscard]] const CWallpaperApplication& getApp () const; [[nodiscard]] const Drivers::CVideoDriver& getDriver () const; [[nodiscard]] const Drivers::Output::COutput& getOutput () const; - const ITexture* resolveTexture (const std::string& name); + std::shared_ptr resolveTexture (const std::string& name); [[nodiscard]] const std::map& getWallpapers () const; private: diff --git a/src/WallpaperEngine/Render/CTextureCache.cpp b/src/WallpaperEngine/Render/CTextureCache.cpp index 31a809b..b7e97e0 100644 --- a/src/WallpaperEngine/Render/CTextureCache.cpp +++ b/src/WallpaperEngine/Render/CTextureCache.cpp @@ -8,7 +8,7 @@ using namespace WallpaperEngine::Assets; CTextureCache::CTextureCache (CRenderContext& context) : Helpers::CContextAware (context) {} -const ITexture* CTextureCache::resolve (const std::string& filename) { +std::shared_ptr CTextureCache::resolve (const std::string& filename) { const auto found = this->m_textureCache.find (filename); if (found != this->m_textureCache.end ()) @@ -17,7 +17,7 @@ const ITexture* CTextureCache::resolve (const std::string& filename) { // search for the texture in all the different containers just in case for (const auto& it : this->getContext ().getApp ().getBackgrounds ()) { try { - const ITexture* texture = it.second->getContainer ()->readTexture (filename); + std::shared_ptr texture = it.second->getContainer ()->readTexture (filename); this->store (filename, texture); @@ -30,6 +30,6 @@ const ITexture* CTextureCache::resolve (const std::string& filename) { throw CAssetLoadException (filename, "Cannot find file"); } -void CTextureCache::store (const std::string& name, const ITexture* texture) { +void CTextureCache::store (const std::string& name, std::shared_ptr texture) { this->m_textureCache.insert_or_assign (name, texture); } \ No newline at end of file diff --git a/src/WallpaperEngine/Render/CTextureCache.h b/src/WallpaperEngine/Render/CTextureCache.h index 484539c..8b25c9f 100644 --- a/src/WallpaperEngine/Render/CTextureCache.h +++ b/src/WallpaperEngine/Render/CTextureCache.h @@ -2,6 +2,7 @@ #include #include +#include #include "WallpaperEngine/Assets/ITexture.h" #include "WallpaperEngine/Render/CRenderContext.h" @@ -27,7 +28,7 @@ class CTextureCache final : Helpers::CContextAware { * @param filename * @return */ - const ITexture* resolve (const std::string& filename); + std::shared_ptr resolve (const std::string& filename); /** * Registers a texture in the cache @@ -35,10 +36,10 @@ class CTextureCache final : Helpers::CContextAware { * @param name * @param texture */ - void store (const std::string& name, const ITexture* texture); + void store (const std::string& name, std::shared_ptr texture); private: /** Cached textures */ - std::map m_textureCache; + std::map> m_textureCache; }; } // namespace WallpaperEngine::Render diff --git a/src/WallpaperEngine/Render/CWallpaper.cpp b/src/WallpaperEngine/Render/CWallpaper.cpp index fb2a447..770b2c2 100644 --- a/src/WallpaperEngine/Render/CWallpaper.cpp +++ b/src/WallpaperEngine/Render/CWallpaper.cpp @@ -254,25 +254,26 @@ CAudioContext& CWallpaper::getAudioContext () { return this->m_audioContext; } -CFBO* CWallpaper::createFBO (const std::string& name, ITexture::TextureFormat format, ITexture::TextureFlags flags, - float scale, uint32_t realWidth, uint32_t realHeight, uint32_t textureWidth, - uint32_t textureHeight) { - CFBO* fbo = new CFBO (name, format, flags, scale, realWidth, realHeight, textureWidth, textureHeight); +std::shared_ptr CWallpaper::createFBO ( + const std::string& name, ITexture::TextureFormat format, ITexture::TextureFlags flags, float scale, + uint32_t realWidth, uint32_t realHeight, uint32_t textureWidth, uint32_t textureHeight +) { + std::shared_ptr fbo = std::make_shared (name, format, flags, scale, realWidth, realHeight, textureWidth, textureHeight); this->m_fbos.insert (std::pair (name, fbo)); return fbo; } -void CWallpaper::aliasFBO (const std::string& alias, CFBO* original) { +void CWallpaper::aliasFBO (const std::string& alias, const std::shared_ptr& original) { this->m_fbos.insert (std::pair (alias, original)); } -const std::map& CWallpaper::getFBOs () const { +const std::map>& CWallpaper::getFBOs () const { return this->m_fbos; } -CFBO* CWallpaper::findFBO (const std::string& name) const { +std::shared_ptr CWallpaper::findFBO (const std::string& name) const { const auto it = this->m_fbos.find (name); if (it == this->m_fbos.end ()) @@ -281,7 +282,7 @@ CFBO* CWallpaper::findFBO (const std::string& name) const { return it->second; } -CFBO* CWallpaper::getFBO () const { +std::shared_ptr CWallpaper::getFBO () const { return this->m_sceneFBO; } diff --git a/src/WallpaperEngine/Render/CWallpaper.h b/src/WallpaperEngine/Render/CWallpaper.h index ba3cedc..05185c8 100644 --- a/src/WallpaperEngine/Render/CWallpaper.h +++ b/src/WallpaperEngine/Render/CWallpaper.h @@ -87,7 +87,7 @@ class CWallpaper : public Helpers::CContextAware { * @param textureHeight * @return */ - CFBO* createFBO ( + std::shared_ptr createFBO ( const std::string& name, ITexture::TextureFormat format, ITexture::TextureFlags flags, float scale, uint32_t realWidth, uint32_t realHeight, uint32_t textureWidth, uint32_t textureHeight); @@ -96,24 +96,24 @@ class CWallpaper : public Helpers::CContextAware { * @param alias * @param original */ - void aliasFBO (const std::string& alias, CFBO* original); + void aliasFBO (const std::string& alias, const std::shared_ptr& original); /** * @return The full FBO list to work with */ - [[nodiscard]] const std::map& getFBOs () const; + [[nodiscard]] const std::map>& getFBOs () const; /** * Searches the FBO list for the given FBO * * @param name * @return */ - [[nodiscard]] CFBO* findFBO (const std::string& name) const; + [[nodiscard]] std::shared_ptr findFBO (const std::string& name) const; /** * @return The main FBO of this wallpaper */ - [[nodiscard]] CFBO* getFBO () const; + [[nodiscard]] std::shared_ptr getFBO () const; /** * Updates the UVs coordinates if window/screen/vflip/projection has changed @@ -173,7 +173,7 @@ class CWallpaper : public Helpers::CContextAware { [[nodiscard]] const Core::CWallpaper* getWallpaperData () const; /** The FBO used for scene output */ - CFBO* m_sceneFBO; + std::shared_ptr m_sceneFBO; private: /** The texture used for the scene output */ @@ -192,7 +192,7 @@ class CWallpaper : public Helpers::CContextAware { /** The type of background this wallpaper is */ std::string m_type; /** List of FBOs registered for this wallpaper */ - std::map m_fbos; + std::map> m_fbos; /** Audio context that is using this wallpaper */ CAudioContext& m_audioContext; /** Current Wallpaper state */ diff --git a/src/WallpaperEngine/Render/Objects/CEffect.cpp b/src/WallpaperEngine/Render/Objects/CEffect.cpp index 7a3e388..36fad4e 100644 --- a/src/WallpaperEngine/Render/Objects/CEffect.cpp +++ b/src/WallpaperEngine/Render/Objects/CEffect.cpp @@ -18,7 +18,7 @@ const std::vector& CEffect::getMaterials () const { return this->m_materials; } -const CFBO* CEffect::findFBO (const std::string& name) const { +std::shared_ptr CEffect::findFBO (const std::string& name) const { const auto fbo = this->m_fbos.find (name); if (fbo == this->m_fbos.end ()) { @@ -51,7 +51,7 @@ void CEffect::generateFBOs () { } } -const std::map& CEffect::getFBOs () const { +const std::map>& CEffect::getFBOs () const { return this->m_fbos; } diff --git a/src/WallpaperEngine/Render/Objects/CEffect.h b/src/WallpaperEngine/Render/Objects/CEffect.h index d5d578e..7a55a1e 100644 --- a/src/WallpaperEngine/Render/Objects/CEffect.h +++ b/src/WallpaperEngine/Render/Objects/CEffect.h @@ -26,13 +26,13 @@ class CEffect { [[nodiscard]] const std::vector& getMaterials () const; - [[nodiscard]] const CFBO* findFBO (const std::string& name) const; + [[nodiscard]] std::shared_ptr findFBO (const std::string& name) const; [[nodiscard]] bool isVisible () const; protected: friend class WallpaperEngine::PrettyPrinter::CPrettyPrinter; - [[nodiscard]] const std::map& getFBOs () const; + [[nodiscard]] const std::map>& getFBOs () const; private: void generatePasses (); @@ -41,7 +41,7 @@ class CEffect { CImage* m_image; const Core::Objects::CEffect* m_effect; - std::map m_fbos; + std::map> m_fbos; std::vector m_materials; }; } // namespace WallpaperEngine::Render::Objects diff --git a/src/WallpaperEngine/Render/Objects/CImage.cpp b/src/WallpaperEngine/Render/Objects/CImage.cpp index e7e87fa..ca6e812 100644 --- a/src/WallpaperEngine/Render/Objects/CImage.cpp +++ b/src/WallpaperEngine/Render/Objects/CImage.cpp @@ -96,7 +96,7 @@ CImage::CImage (Wallpapers::CScene* scene, const Core::Objects::CImage* image) : // same applies to effects // TODO: create a dummy texture of correct size, fbo constructors should be enough, but this should be properly // handled - this->m_texture = new CFBO ( + this->m_texture = std::make_shared ( "", ITexture::TextureFormat::ARGB8888, ITexture::TextureFlags::NoFlags, 1, size.x, size.y, size.x, size.y); } @@ -305,8 +305,8 @@ void CImage::setup () { void CImage::setupPasses () { // do a pass on everything and setup proper inputs and values - const CFBO* drawTo = this->m_currentMainFBO; - const ITexture* asInput = this->getTexture (); + std::shared_ptr drawTo = this->m_currentMainFBO; + std::shared_ptr asInput = this->getTexture (); GLuint texcoord = this->getTexCoordCopy (); auto cur = this->m_passes.begin (); @@ -316,7 +316,7 @@ void CImage::setupPasses () { for (; cur != end; ++cur) { // TODO: PROPERLY CHECK EFFECT'S VISIBILITY AND TAKE IT INTO ACCOUNT Effects::CPass* pass = *cur; - const CFBO* prevDrawTo = drawTo; + std::shared_ptr prevDrawTo = drawTo; GLuint spacePosition = (first) ? this->getCopySpacePosition () : this->getPassSpacePosition (); const glm::mat4* projection = (first) ? &this->m_modelViewProjectionCopy : &this->m_modelViewProjectionPass; const glm::mat4* inverseProjection = @@ -362,10 +362,10 @@ void CImage::setupPasses () { } } -void CImage::pinpongFramebuffer (const CFBO** drawTo, const ITexture** asInput) { +void CImage::pinpongFramebuffer (std::shared_ptr* drawTo, std::shared_ptr* asInput) { // temporarily store FBOs used - CFBO* currentMainFBO = this->m_currentMainFBO; - CFBO* currentSubFBO = this->m_currentSubFBO; + std::shared_ptr currentMainFBO = this->m_currentMainFBO; + std::shared_ptr currentSubFBO = this->m_currentSubFBO; if (drawTo != nullptr) *drawTo = currentSubFBO; @@ -435,7 +435,7 @@ void CImage::updateScreenSpacePosition () { {x, y, 0.0f}); } -const ITexture* CImage::getTexture () const { +std::shared_ptr CImage::getTexture () const { return this->m_texture; } diff --git a/src/WallpaperEngine/Render/Objects/CImage.h b/src/WallpaperEngine/Render/Objects/CImage.h index 0e5fb0d..900ae13 100644 --- a/src/WallpaperEngine/Render/Objects/CImage.h +++ b/src/WallpaperEngine/Render/Objects/CImage.h @@ -44,7 +44,7 @@ class CImage final : public CObject { [[nodiscard]] GLuint getPassSpacePosition () const; [[nodiscard]] GLuint getTexCoordCopy () const; [[nodiscard]] GLuint getTexCoordPass () const; - [[nodiscard]] const ITexture* getTexture () const; + [[nodiscard]] std::shared_ptr getTexture () const; [[nodiscard]] double getAnimationTime () const; /** @@ -53,7 +53,7 @@ class CImage final : public CObject { * @param drawTo The framebuffer to use * @param asInput The last texture used as output (if needed) */ - void pinpongFramebuffer (const CFBO** drawTo, const ITexture** asInput); + void pinpongFramebuffer (std::shared_ptr* drawTo, std::shared_ptr* asInput); protected: static const std::string Type; @@ -63,7 +63,7 @@ class CImage final : public CObject { void updateScreenSpacePosition (); private: - const ITexture* m_texture; + std::shared_ptr m_texture; GLuint m_sceneSpacePosition; GLuint m_copySpacePosition; GLuint m_passSpacePosition; @@ -80,10 +80,10 @@ class CImage final : public CObject { glm::mat4 m_modelMatrix; glm::mat4 m_viewProjectionMatrix; - CFBO* m_mainFBO; - CFBO* m_subFBO; - CFBO* m_currentMainFBO; - CFBO* m_currentSubFBO; + std::shared_ptr m_mainFBO; + std::shared_ptr m_subFBO; + std::shared_ptr m_currentMainFBO; + std::shared_ptr m_currentSubFBO; const Core::Objects::CImage* m_image; diff --git a/src/WallpaperEngine/Render/Objects/CSound.cpp b/src/WallpaperEngine/Render/Objects/CSound.cpp index 588e629..05f94b1 100644 --- a/src/WallpaperEngine/Render/Objects/CSound.cpp +++ b/src/WallpaperEngine/Render/Objects/CSound.cpp @@ -17,15 +17,13 @@ CSound::~CSound() { delete stream; } - for (const auto& buffer : this->m_soundBuffer) { - delete buffer; - } + this->m_soundBuffer.clear (); } void CSound::load () { for (const auto& cur : this->m_sound->getSounds ()) { uint32_t filesize = 0; - const uint8_t* filebuffer = this->getContainer ()->readFile (cur, &filesize); + std::shared_ptr filebuffer = this->getContainer ()->readFile (cur, &filesize); auto stream = new Audio::CAudioStream (this->getScene ()->getAudioContext (), filebuffer, filesize); diff --git a/src/WallpaperEngine/Render/Objects/CSound.h b/src/WallpaperEngine/Render/Objects/CSound.h index ad49b1f..4ad1478 100644 --- a/src/WallpaperEngine/Render/Objects/CSound.h +++ b/src/WallpaperEngine/Render/Objects/CSound.h @@ -25,7 +25,7 @@ class CSound final : public CObject { void load (); private: - std::vector m_soundBuffer; + std::vector> m_soundBuffer; std::vector m_audioStreams; const Core::Objects::CSound* m_sound; diff --git a/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp b/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp index d829c4f..4f08389 100644 --- a/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp +++ b/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp @@ -42,7 +42,7 @@ CPass::CPass (CMaterial* material, const Core::Objects::Images::Materials::CPass this->setupShaderVariables (); } -const ITexture* CPass::resolveTexture (const ITexture* expected, int index, const ITexture* previous) { +std::shared_ptr CPass::resolveTexture (std::shared_ptr expected, int index, std::shared_ptr previous) { if (expected == nullptr) { const auto it = this->m_fbos.find (index); @@ -64,7 +64,7 @@ const ITexture* CPass::resolveTexture (const ITexture* expected, int index, cons return this->resolveFBO (it->second->getName ()); } -const CFBO* CPass::resolveFBO (const std::string& name) const { +std::shared_ptr CPass::resolveFBO (const std::string& name) const { auto fbo = this->m_material->getEffect()->findFBO (name); if (fbo == nullptr) { @@ -124,7 +124,7 @@ void CPass::setupRenderTexture () { glUseProgram (this->m_programID); // maybe we can do this when setting the texture? - const ITexture* texture = this->resolveTexture (this->m_input, 0, this->m_input); + std::shared_ptr texture = this->resolveTexture (this->m_input, 0, this->m_input); uint32_t currentTexture = 0; glm::vec2 translation = {0.0f, 0.0f}; @@ -291,12 +291,12 @@ const CMaterial* CPass::getMaterial () const { return this->m_material; } -void CPass::setDestination (const CFBO* drawTo) { - this->m_drawTo = drawTo; +void CPass::setDestination (std::shared_ptr drawTo) { + this->m_drawTo = std::move(drawTo); } -void CPass::setInput (const ITexture* input) { - this->m_input = input; +void CPass::setInput (std::shared_ptr input) { + this->m_input = std::move(input); } void CPass::setModelViewProjectionMatrix (const glm::mat4* projection) { @@ -379,7 +379,7 @@ GLuint CPass::compileShader (const char* shader, GLuint type) { void CPass::setupShaders () { // ensure the constants are defined - const ITexture* texture0 = this->m_material->getImage ()->getTexture (); + std::shared_ptr texture0 = this->m_material->getImage ()->getTexture (); // copy the combos from the pass this->m_combos.insert (this->m_pass->getCombos ().begin (), this->m_pass->getCombos ().end ()); @@ -476,7 +476,7 @@ void CPass::setupTextureUniforms () { for (const auto& [index, textureName] : this->m_shader->getVertex ().getTextures ()) { try { // resolve the texture first - const ITexture* textureRef; + std::shared_ptr textureRef; if (textureName.find ("_rt_") == 0 || textureName.find ("_alias_") == 0) { textureRef = this->resolveFBO (textureName); @@ -493,7 +493,7 @@ void CPass::setupTextureUniforms () { for (const auto& [index, textureName] : this->m_shader->getFragment ().getTextures ()) { try { // resolve the texture first - const ITexture* textureRef; + std::shared_ptr textureRef; if (textureName.find ("_rt_") == 0 || textureName.find ("_alias_") == 0) { textureRef = this->resolveFBO (textureName); @@ -532,7 +532,7 @@ void CPass::setupTextureUniforms () { } // resolve the main texture - const ITexture* texture = this->resolveTexture (this->m_material->getImage ()->getTexture (), 0); + std::shared_ptr texture = this->resolveTexture (this->m_material->getImage ()->getTexture (), 0); // register all the texture uniforms with correct values this->addUniform ("g_Texture0", 0); this->addUniform ("g_Texture1", 1); diff --git a/src/WallpaperEngine/Render/Objects/Effects/CPass.h b/src/WallpaperEngine/Render/Objects/Effects/CPass.h index 084143b..416b5ec 100644 --- a/src/WallpaperEngine/Render/Objects/Effects/CPass.h +++ b/src/WallpaperEngine/Render/Objects/Effects/CPass.h @@ -27,8 +27,8 @@ class CPass final : public Helpers::CContextAware { void render (); - void setDestination (const CFBO* drawTo); - void setInput (const ITexture* input); + void setDestination (std::shared_ptr drawTo); + void setInput (std::shared_ptr input); void setTexCoord (GLuint texcoord); void setPosition (GLuint position); void setModelViewProjectionMatrix (const glm::mat4* projection); @@ -37,7 +37,7 @@ class CPass final : public Helpers::CContextAware { void setViewProjectionMatrix (const glm::mat4* viewProjection); void setBlendingMode (std::string blendingmode); [[nodiscard]] const std::string& getBlendingMode () const; - [[nodiscard]] const CFBO* resolveFBO (const std::string& name) const; + [[nodiscard]] std::shared_ptr resolveFBO (const std::string& name) const; [[nodiscard]] const CMaterial* getMaterial () const; [[nodiscard]] const Core::Objects::Images::Materials::CPass* getPass () const; @@ -146,11 +146,11 @@ class CPass final : public Helpers::CContextAware { void renderGeometry () const; void cleanupRenderSetup (); - const ITexture* resolveTexture (const ITexture* expected, int index, const ITexture* previous = nullptr); + std::shared_ptr resolveTexture (std::shared_ptr expected, int index, std::shared_ptr previous = nullptr); CMaterial* m_material; const Core::Objects::Images::Materials::CPass* m_pass; - std::map m_fbos; + std::map> m_fbos; std::map m_combos; std::vector m_attribs; std::map m_uniforms; @@ -164,12 +164,12 @@ class CPass final : public Helpers::CContextAware { /** * Contains the final map of textures to be used */ - std::map m_textures; + std::map> m_textures; Render::Shaders::CShader* m_shader; - const CFBO* m_drawTo; - const ITexture* m_input; + std::shared_ptr m_drawTo; + std::shared_ptr m_input; GLuint m_programID; diff --git a/src/WallpaperEngine/Render/Wallpapers/CScene.h b/src/WallpaperEngine/Render/Wallpapers/CScene.h index 0f25f72..06daa8e 100644 --- a/src/WallpaperEngine/Render/Wallpapers/CScene.h +++ b/src/WallpaperEngine/Render/Wallpapers/CScene.h @@ -19,18 +19,18 @@ class CScene final : public CWallpaper { const CWallpaperState::TextureUVsScaling& scalingMode, const WallpaperEngine::Assets::ITexture::TextureFlags& clampMode); - CCamera* getCamera () const; + [[nodiscard]] CCamera* getCamera () const; - const Core::Wallpapers::CScene* getScene () const; + [[nodiscard]] const Core::Wallpapers::CScene* getScene () const; - int getWidth () const override; - int getHeight () const override; + [[nodiscard]] int getWidth () const override; + [[nodiscard]] int getHeight () const override; glm::vec2* getMousePosition (); glm::vec2* getMousePositionLast (); glm::vec2* getParallaxDisplacement (); - const std::vector& getObjectsByRenderOrder () const; + [[nodiscard]] const std::vector& getObjectsByRenderOrder () const; protected: void renderFrame (glm::ivec4 viewport) override; @@ -50,8 +50,8 @@ class CScene final : public CWallpaper { glm::vec2 m_mousePosition; glm::vec2 m_mousePositionLast; glm::vec2 m_parallaxDisplacement; - CFBO* _rt_4FrameBuffer; - CFBO* _rt_8FrameBuffer; - CFBO* _rt_Bloom; + std::shared_ptr _rt_4FrameBuffer; + std::shared_ptr _rt_8FrameBuffer; + std::shared_ptr _rt_Bloom; }; } // namespace WallpaperEngine::Render::Wallpaper diff --git a/src/WallpaperEngine/WebBrowser/CEF/CWPSchemeHandler.cpp b/src/WallpaperEngine/WebBrowser/CEF/CWPSchemeHandler.cpp index cf0bb61..076cb54 100644 --- a/src/WallpaperEngine/WebBrowser/CEF/CWPSchemeHandler.cpp +++ b/src/WallpaperEngine/WebBrowser/CEF/CWPSchemeHandler.cpp @@ -22,9 +22,6 @@ bool CWPSchemeHandler::Open(CefRefPtr request, CefRefPtr callback) { DCHECK(!CefCurrentlyOn(TID_UI) && !CefCurrentlyOn(TID_IO)); - // free previous file so we can properly build the right chain of responses - delete this->m_contents; - #if !NDEBUG std::cout << "Processing request for path " << request->GetURL ().c_str () << std::endl; #endif diff --git a/src/WallpaperEngine/WebBrowser/CEF/CWPSchemeHandler.h b/src/WallpaperEngine/WebBrowser/CEF/CWPSchemeHandler.h index f4e8e58..11277ce 100644 --- a/src/WallpaperEngine/WebBrowser/CEF/CWPSchemeHandler.h +++ b/src/WallpaperEngine/WebBrowser/CEF/CWPSchemeHandler.h @@ -35,7 +35,7 @@ class CWPSchemeHandler : public CefResourceHandler { const Core::CProject* m_project; const Assets::CContainer* m_container; - const uint8_t* m_contents; + std::shared_ptr m_contents; uint32_t m_filesize; std::string m_mimeType; uint32_t m_offset;