chore: update file functions to make use of shared_ptr/unique_ptr instead of copying things around

This commit is contained in:
Almamu 2025-04-25 23:26:33 +02:00
parent f3d3cc32e1
commit 4c0c32055a
32 changed files with 122 additions and 161 deletions

View File

@ -41,7 +41,7 @@ std::filesystem::path CCombinedContainer::resolveRealFile (const std::filesystem
throw CAssetLoadException (filename, "Cannot resolve file in any of the containers"); 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<const uint8_t[]> CCombinedContainer::readFile (const std::filesystem::path& filename, uint32_t* length) const {
for (const auto cur : this->m_containers) { for (const auto cur : this->m_containers) {
try { try {
// try to read the file on the current container, if the file doesn't exists // try to read the file on the current container, if the file doesn't exists

View File

@ -30,7 +30,7 @@ class CCombinedContainer final : public CContainer {
/** @inheritdoc */ /** @inheritdoc */
[[nodiscard]] std::filesystem::path resolveRealFile (const std::filesystem::path& filename) const override; [[nodiscard]] std::filesystem::path resolveRealFile (const std::filesystem::path& filename) const override;
/** @inheritdoc */ /** @inheritdoc */
[[nodiscard]] const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const override; [[nodiscard]] std::shared_ptr<const uint8_t[]> readFile (const std::filesystem::path& filename, uint32_t* length) const override;
private: private:
/** The list of containers to search files off from */ /** The list of containers to search files off from */

View File

@ -13,19 +13,17 @@ std::filesystem::path CContainer::resolveRealFile (const std::filesystem::path&
throw CAssetLoadException (filename, "Cannot resolve physical file in this container"); throw CAssetLoadException (filename, "Cannot resolve physical file in this container");
} }
const ITexture* CContainer::readTexture (const std::filesystem::path& filename) const { std::shared_ptr <const ITexture> CContainer::readTexture (const std::filesystem::path& filename) const {
// get the texture's filename (usually .tex) // get the texture's filename (usually .tex)
std::filesystem::path texture = "materials" / std::filesystem::path (filename.string ().append (".tex")); std::filesystem::path texture = "materials" / std::filesystem::path (filename.string ().append (".tex"));
const auto* textureContents = this->readFile (texture, nullptr); const auto textureContents = this->readFile (texture, nullptr);
const auto* result = new CTexture (textureContents); const auto result = std::make_shared<CTexture> (textureContents);
#if !NDEBUG #if !NDEBUG
glObjectLabel (GL_TEXTURE, result->getTextureID (0), -1, texture.c_str ()); glObjectLabel (GL_TEXTURE, result->getTextureID (0), -1, texture.c_str ());
#endif /* NDEBUG */ #endif /* NDEBUG */
delete[] textureContents;
return result; 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 { std::string CContainer::readFileAsString (const std::filesystem::path& filename) const {
uint32_t length = 0; uint32_t length = 0;
// read file contents and allocate a buffer for a string return {
const auto* contents = this->readFile (filename, &length); reinterpret_cast<const char*> (this->readFile (filename, &length).get ()), 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;
} }

View File

@ -32,7 +32,7 @@ class CContainer {
* *
* @return * @return
*/ */
[[nodiscard]] virtual const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const = 0; [[nodiscard]] virtual std::shared_ptr<const uint8_t[]> readFile (const std::filesystem::path& filename, uint32_t* length) const = 0;
/** /**
* Wrapper for readFile, appends the texture extension at the end of the filename * Wrapper for readFile, appends the texture extension at the end of the filename
@ -41,7 +41,7 @@ class CContainer {
* *
* @return * @return
*/ */
[[nodiscard]] const ITexture* readTexture (const std::filesystem::path& filename) const; [[nodiscard]] std::shared_ptr <const ITexture> readTexture (const std::filesystem::path& filename) const;
/** /**
* Wrapper for readFile, checks for compat versions of the given shader file * Wrapper for readFile, checks for compat versions of the given shader file

View File

@ -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<const uint8_t[]> CDirectory::readFile (const std::filesystem::path& filename, uint32_t* length) const {
std::filesystem::path final = this->resolveRealFile (filename); std::filesystem::path final = this->resolveRealFile (filename);
FILE* fp = fopen (final.c_str (), "rb"); FILE* fp = fopen (final.c_str (), "rb");
if (fp == nullptr) { if (fp == nullptr) {
throw CAssetLoadException (filename, "Cannot open file for reading"); 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); fseek (fp, 0, SEEK_SET);
// now read the whole file // now read the whole file
auto* contents = new uint8_t [size]; std::shared_ptr<uint8_t[]> contents = std::shared_ptr<uint8_t[]>(new uint8_t [size]);
if (fread (contents, size, 1, fp) != 1) { if (fread (contents.get(), size, 1, fp) != 1) {
delete [] contents;
throw CAssetLoadException (filename, "Unexpected error when reading the file"); throw CAssetLoadException (filename, "Unexpected error when reading the file");
} }

View File

@ -19,7 +19,7 @@ class CDirectory final : public CContainer {
/** @inheritdoc */ /** @inheritdoc */
[[nodiscard]] std::filesystem::path resolveRealFile (const std::filesystem::path& filename) const override; [[nodiscard]] std::filesystem::path resolveRealFile (const std::filesystem::path& filename) const override;
/** @inheritdoc */ /** @inheritdoc */
[[nodiscard]] const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const override; [[nodiscard]] std::shared_ptr<const uint8_t[]> readFile (const std::filesystem::path& filename, uint32_t* length) const override;
private: private:
/** The basepath for the directory */ /** The basepath for the directory */

View File

@ -8,14 +8,13 @@ namespace WallpaperEngine::Assets {
*/ */
class CFileEntry { class CFileEntry {
public: public:
CFileEntry (const uint8_t* address, uint32_t length) : address (address), length (length) {} CFileEntry (std::shared_ptr<const uint8_t[]> content, uint32_t length) :
content (content),
~CFileEntry () { length (length) {}
delete [] address; ~CFileEntry() = default;
}
/** File contents */ /** File contents */
const uint8_t* address; std::shared_ptr<const uint8_t[]> content;
/** File length */ /** File length */
uint32_t length; uint32_t length;
}; };

View File

@ -24,7 +24,7 @@ CPackage::CPackage (std::filesystem::path path) : m_path (std::move (path)) {
this->init (); this->init ();
} }
const uint8_t* CPackage::readFile (const std::filesystem::path& filename, uint32_t* length) const { std::shared_ptr<const uint8_t[]> CPackage::readFile (const std::filesystem::path& filename, uint32_t* length) const {
const auto it = this->m_contents.find (filename); const auto it = this->m_contents.find (filename);
if (it == this->m_contents.end ()) if (it == this->m_contents.end ())
@ -34,12 +34,7 @@ const uint8_t* CPackage::readFile (const std::filesystem::path& filename, uint32
if (length != nullptr) if (length != nullptr)
*length = it->second->length; *length = it->second->length;
// clone original first return it->second->content;
auto* result = new uint8_t [it->second->length];
memcpy (result, it->second->address, it->second->length);
return result;
} }
void CPackage::init () { void CPackage::init () {
@ -128,15 +123,13 @@ void CPackage::loadFiles (FILE* fp) {
sLog.exception ("Cannot find file ", cur.filename, " from package ", this->m_path); 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 // allocate memory for the file's contents and read it from the file
auto* fileContents = new uint8_t [cur.length]; std::shared_ptr<uint8_t[]> contents = std::shared_ptr<uint8_t[]>(new uint8_t [cur.length]);
if (fread (fileContents, cur.length, 1, fp) != 1) {
delete [] fileContents;
if (fread (contents.get(), cur.length, 1, fp) != 1) {
sLog.exception ("Cannot read file ", cur.filename, " contents from package ", this->m_path); sLog.exception ("Cannot read file ", cur.filename, " contents from package ", this->m_path);
} }
// add the file to the map // 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<CFileEntry> (contents, cur.length));
} }
} }

View File

@ -20,7 +20,7 @@ class CPackage final : public CContainer {
public: public:
explicit CPackage (std::filesystem::path path); 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<const uint8_t[]> readFile (const std::filesystem::path& filename, uint32_t* length) const override;
protected: protected:
/** /**
@ -63,6 +63,6 @@ class CPackage final : public CContainer {
/** The path to the package file */ /** The path to the package file */
std::filesystem::path m_path; std::filesystem::path m_path;
/** Contents of the package file */ /** Contents of the package file */
std::map<std::string, CFileEntry*> m_contents; std::map<std::string, std::unique_ptr<CFileEntry>> m_contents;
}; };
} // namespace WallpaperEngine::Assets } // namespace WallpaperEngine::Assets

View File

@ -10,8 +10,9 @@
using namespace WallpaperEngine::Assets; using namespace WallpaperEngine::Assets;
CTexture::CTexture (const void* fileData) : m_resolution () { CTexture::CTexture (std::shared_ptr<const uint8_t[]> buffer) : m_resolution () {
// ensure the header is parsed // ensure the header is parsed
const void* fileData = buffer.get ();
this->m_header = parseHeader (static_cast<const char*> (fileData)); this->m_header = parseHeader (static_cast<const char*> (fileData));
this->setupResolution (); this->setupResolution ();
GLint internalFormat = this->setupInternalFormat(); GLint internalFormat = this->setupInternalFormat();

View File

@ -5,6 +5,7 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <glm/vec4.hpp> #include <glm/vec4.hpp>
#include <map> #include <map>
#include <memory>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <vector> #include <vector>
@ -152,7 +153,7 @@ class CTexture final : public ITexture {
}; };
public: public:
explicit CTexture (const void* fileData); explicit CTexture (std::shared_ptr<const uint8_t[]> fileData);
~CTexture () override; ~CTexture () override;
/** @inheritdoc */ /** @inheritdoc */

View File

@ -5,23 +5,15 @@
using namespace WallpaperEngine::Assets; using namespace WallpaperEngine::Assets;
CVirtualContainer::~CVirtualContainer() { void CVirtualContainer::add (const std::filesystem::path& filename, const std::shared_ptr<const uint8_t[]>& contents, uint32_t length) {
for (auto& [_, entry] : this->m_virtualFiles) { this->m_virtualFiles.insert (std::pair (filename, std::make_unique<CFileEntry> (contents, length)));
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::string& contents) { void CVirtualContainer::add (const std::filesystem::path& filename, const std::string& contents) {
auto* copy = new uint8_t [contents.length () + 1]; std::shared_ptr<uint8_t[]> copy = std::shared_ptr<uint8_t[]> (new uint8_t [contents.length () + 1]);
// copy the text AND the \0 // 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 // finally add to the container
this->add (filename, copy, contents.length () + 1); 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 ()); this->add (filename, contents.dump ());
} }
const uint8_t* CVirtualContainer::readFile (const std::filesystem::path& filename, uint32_t* length) const { std::shared_ptr<const uint8_t[]> CVirtualContainer::readFile (const std::filesystem::path& filename, uint32_t* length) const {
const auto cur = this->m_virtualFiles.find (filename); const auto cur = this->m_virtualFiles.find (filename);
if (cur == this->m_virtualFiles.end ()) if (cur == this->m_virtualFiles.end ())
@ -45,9 +37,5 @@ const uint8_t* CVirtualContainer::readFile (const std::filesystem::path& filenam
*length = cur->second->length; *length = cur->second->length;
// clone original first // clone original first
auto* result = new uint8_t [cur->second->length]; return cur->second->content;
memcpy (result, cur->second->address, cur->second->length);
return result;
} }

View File

@ -16,8 +16,6 @@ using json = nlohmann::json;
*/ */
class CVirtualContainer final : public CContainer { class CVirtualContainer final : public CContainer {
public: public:
~CVirtualContainer() override;
/** /**
* Adds a new file to the virtual container * Adds a new file to the virtual container
* *
@ -25,7 +23,7 @@ class CVirtualContainer final : public CContainer {
* @param contents * @param contents
* @param length * @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<const uint8_t[]>& contents, uint32_t length);
/** /**
* Adds a new file to the virtual container * 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); void add (const std::filesystem::path& filename, const json& contents);
/** @inheritdoc */ /** @inheritdoc */
const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const override; std::shared_ptr<const uint8_t[]> readFile (const std::filesystem::path& filename, uint32_t* length) const override;
private: private:
/** The recorded files in this virtual container */ /** The recorded files in this virtual container */
std::map<std::string, CFileEntry*> m_virtualFiles; std::map<std::string, std::unique_ptr<CFileEntry>> m_virtualFiles;
}; };
} // namespace WallpaperEngine::Assets } // namespace WallpaperEngine::Assets

View File

@ -68,7 +68,7 @@ static int audio_read_data_callback (void* streamarg, uint8_t* buffer, int buffe
buffer_size = FFMIN (buffer_size, left); 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 // update position
stream->setPosition (stream->getPosition () + buffer_size); stream->setPosition (stream->getPosition () + buffer_size);
@ -96,7 +96,7 @@ CAudioStream::CAudioStream (CAudioContext& context, const std::string& filename)
this->loadCustomContent (filename.c_str ()); this->loadCustomContent (filename.c_str ());
} }
CAudioStream::CAudioStream (CAudioContext& context, const uint8_t* buffer, uint32_t length) : CAudioStream::CAudioStream (CAudioContext& context, std::shared_ptr<const uint8_t[]> buffer, uint32_t length) :
m_swrctx (nullptr), m_swrctx (nullptr),
m_audioStream(NO_AUDIO_STREAM), m_audioStream(NO_AUDIO_STREAM),
m_audioContext (context) { m_audioContext (context) {
@ -339,7 +339,7 @@ bool CAudioStream::isRepeat () const {
return this->m_repeat; return this->m_repeat;
} }
const uint8_t* CAudioStream::getBuffer () { std::shared_ptr<const uint8_t[]> CAudioStream::getBuffer () {
return this->m_buffer; return this->m_buffer;
} }

View File

@ -33,7 +33,7 @@ class CAudioContext;
class CAudioStream { class CAudioStream {
public: public:
CAudioStream (CAudioContext& context, const std::string& filename); CAudioStream (CAudioContext& context, const std::string& filename);
CAudioStream (CAudioContext& context, const uint8_t* buffer, uint32_t length); CAudioStream (CAudioContext& context, std::shared_ptr<const uint8_t[]> buffer, uint32_t length);
CAudioStream (CAudioContext& audioContext, AVCodecContext* context); CAudioStream (CAudioContext& audioContext, AVCodecContext* context);
~CAudioStream (); ~CAudioStream ();
@ -84,7 +84,7 @@ class CAudioStream {
/** /**
* @return The file data buffer * @return The file data buffer
*/ */
[[nodiscard]] const uint8_t* getBuffer (); [[nodiscard]] std::shared_ptr<const uint8_t[]> getBuffer ();
/** /**
* @return The length of the file data buffer * @return The length of the file data buffer
*/ */
@ -181,7 +181,7 @@ class CAudioStream {
/** The stream index for the audio being played */ /** The stream index for the audio being played */
int m_audioStream; int m_audioStream;
/** File data pointer */ /** File data pointer */
const uint8_t* m_buffer {}; std::shared_ptr<const uint8_t[]> m_buffer;
/** The length of the file data pointer */ /** The length of the file data pointer */
uint32_t m_length {}; uint32_t m_length {};
/** The read position on the file data pointer */ /** The read position on the file data pointer */

View File

@ -61,7 +61,7 @@ const Drivers::Output::COutput& CRenderContext::getOutput () const {
return this->m_driver.getOutput (); return this->m_driver.getOutput ();
} }
const ITexture* CRenderContext::resolveTexture (const std::string& name) { std::shared_ptr<const ITexture> CRenderContext::resolveTexture (const std::string& name) {
return this->m_textureCache->resolve (name); return this->m_textureCache->resolve (name);
} }

View File

@ -2,6 +2,7 @@
#include <glm/vec4.hpp> #include <glm/vec4.hpp>
#include <vector> #include <vector>
#include <memory>
#include "CTextureCache.h" #include "CTextureCache.h"
#include "WallpaperEngine/Application/CWallpaperApplication.h" #include "WallpaperEngine/Application/CWallpaperApplication.h"
@ -40,7 +41,7 @@ class CRenderContext {
[[nodiscard]] const CWallpaperApplication& getApp () const; [[nodiscard]] const CWallpaperApplication& getApp () const;
[[nodiscard]] const Drivers::CVideoDriver& getDriver () const; [[nodiscard]] const Drivers::CVideoDriver& getDriver () const;
[[nodiscard]] const Drivers::Output::COutput& getOutput () const; [[nodiscard]] const Drivers::Output::COutput& getOutput () const;
const ITexture* resolveTexture (const std::string& name); std::shared_ptr<const ITexture> resolveTexture (const std::string& name);
[[nodiscard]] const std::map<std::string, CWallpaper*>& getWallpapers () const; [[nodiscard]] const std::map<std::string, CWallpaper*>& getWallpapers () const;
private: private:

View File

@ -8,7 +8,7 @@ using namespace WallpaperEngine::Assets;
CTextureCache::CTextureCache (CRenderContext& context) : Helpers::CContextAware (context) {} CTextureCache::CTextureCache (CRenderContext& context) : Helpers::CContextAware (context) {}
const ITexture* CTextureCache::resolve (const std::string& filename) { std::shared_ptr<const ITexture> CTextureCache::resolve (const std::string& filename) {
const auto found = this->m_textureCache.find (filename); const auto found = this->m_textureCache.find (filename);
if (found != this->m_textureCache.end ()) 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 // search for the texture in all the different containers just in case
for (const auto& it : this->getContext ().getApp ().getBackgrounds ()) { for (const auto& it : this->getContext ().getApp ().getBackgrounds ()) {
try { try {
const ITexture* texture = it.second->getContainer ()->readTexture (filename); std::shared_ptr<const ITexture> texture = it.second->getContainer ()->readTexture (filename);
this->store (filename, texture); this->store (filename, texture);
@ -30,6 +30,6 @@ const ITexture* CTextureCache::resolve (const std::string& filename) {
throw CAssetLoadException (filename, "Cannot find file"); 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<const ITexture> texture) {
this->m_textureCache.insert_or_assign (name, texture); this->m_textureCache.insert_or_assign (name, texture);
} }

View File

@ -2,6 +2,7 @@
#include <map> #include <map>
#include <string> #include <string>
#include <memory>
#include "WallpaperEngine/Assets/ITexture.h" #include "WallpaperEngine/Assets/ITexture.h"
#include "WallpaperEngine/Render/CRenderContext.h" #include "WallpaperEngine/Render/CRenderContext.h"
@ -27,7 +28,7 @@ class CTextureCache final : Helpers::CContextAware {
* @param filename * @param filename
* @return * @return
*/ */
const ITexture* resolve (const std::string& filename); std::shared_ptr<const ITexture> resolve (const std::string& filename);
/** /**
* Registers a texture in the cache * Registers a texture in the cache
@ -35,10 +36,10 @@ class CTextureCache final : Helpers::CContextAware {
* @param name * @param name
* @param texture * @param texture
*/ */
void store (const std::string& name, const ITexture* texture); void store (const std::string& name, std::shared_ptr<const ITexture> texture);
private: private:
/** Cached textures */ /** Cached textures */
std::map<std::string, const ITexture*> m_textureCache; std::map<std::string, std::shared_ptr<const ITexture>> m_textureCache;
}; };
} // namespace WallpaperEngine::Render } // namespace WallpaperEngine::Render

View File

@ -254,25 +254,26 @@ CAudioContext& CWallpaper::getAudioContext () {
return this->m_audioContext; return this->m_audioContext;
} }
CFBO* CWallpaper::createFBO (const std::string& name, ITexture::TextureFormat format, ITexture::TextureFlags flags, std::shared_ptr<const CFBO> CWallpaper::createFBO (
float scale, uint32_t realWidth, uint32_t realHeight, uint32_t textureWidth, const std::string& name, ITexture::TextureFormat format, ITexture::TextureFlags flags, float scale,
uint32_t textureHeight) { 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<const CFBO> fbo = std::make_shared <CFBO> (name, format, flags, scale, realWidth, realHeight, textureWidth, textureHeight);
this->m_fbos.insert (std::pair (name, fbo)); this->m_fbos.insert (std::pair (name, fbo));
return fbo; return fbo;
} }
void CWallpaper::aliasFBO (const std::string& alias, CFBO* original) { void CWallpaper::aliasFBO (const std::string& alias, const std::shared_ptr<const CFBO>& original) {
this->m_fbos.insert (std::pair (alias, original)); this->m_fbos.insert (std::pair (alias, original));
} }
const std::map<std::string, CFBO*>& CWallpaper::getFBOs () const { const std::map<std::string, std::shared_ptr<const CFBO>>& CWallpaper::getFBOs () const {
return this->m_fbos; return this->m_fbos;
} }
CFBO* CWallpaper::findFBO (const std::string& name) const { std::shared_ptr<const CFBO> CWallpaper::findFBO (const std::string& name) const {
const auto it = this->m_fbos.find (name); const auto it = this->m_fbos.find (name);
if (it == this->m_fbos.end ()) if (it == this->m_fbos.end ())
@ -281,7 +282,7 @@ CFBO* CWallpaper::findFBO (const std::string& name) const {
return it->second; return it->second;
} }
CFBO* CWallpaper::getFBO () const { std::shared_ptr<const CFBO> CWallpaper::getFBO () const {
return this->m_sceneFBO; return this->m_sceneFBO;
} }

View File

@ -87,7 +87,7 @@ class CWallpaper : public Helpers::CContextAware {
* @param textureHeight * @param textureHeight
* @return * @return
*/ */
CFBO* createFBO ( std::shared_ptr<const CFBO> createFBO (
const std::string& name, ITexture::TextureFormat format, ITexture::TextureFlags flags, float scale, const std::string& name, ITexture::TextureFormat format, ITexture::TextureFlags flags, float scale,
uint32_t realWidth, uint32_t realHeight, uint32_t textureWidth, uint32_t textureHeight); uint32_t realWidth, uint32_t realHeight, uint32_t textureWidth, uint32_t textureHeight);
@ -96,24 +96,24 @@ class CWallpaper : public Helpers::CContextAware {
* @param alias * @param alias
* @param original * @param original
*/ */
void aliasFBO (const std::string& alias, CFBO* original); void aliasFBO (const std::string& alias, const std::shared_ptr<const CFBO>& original);
/** /**
* @return The full FBO list to work with * @return The full FBO list to work with
*/ */
[[nodiscard]] const std::map<std::string, CFBO*>& getFBOs () const; [[nodiscard]] const std::map<std::string, std::shared_ptr<const CFBO>>& getFBOs () const;
/** /**
* Searches the FBO list for the given FBO * Searches the FBO list for the given FBO
* *
* @param name * @param name
* @return * @return
*/ */
[[nodiscard]] CFBO* findFBO (const std::string& name) const; [[nodiscard]] std::shared_ptr<const CFBO> findFBO (const std::string& name) const;
/** /**
* @return The main FBO of this wallpaper * @return The main FBO of this wallpaper
*/ */
[[nodiscard]] CFBO* getFBO () const; [[nodiscard]] std::shared_ptr<const CFBO> getFBO () const;
/** /**
* Updates the UVs coordinates if window/screen/vflip/projection has changed * 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; [[nodiscard]] const Core::CWallpaper* getWallpaperData () const;
/** The FBO used for scene output */ /** The FBO used for scene output */
CFBO* m_sceneFBO; std::shared_ptr<const CFBO> m_sceneFBO;
private: private:
/** The texture used for the scene output */ /** The texture used for the scene output */
@ -192,7 +192,7 @@ class CWallpaper : public Helpers::CContextAware {
/** The type of background this wallpaper is */ /** The type of background this wallpaper is */
std::string m_type; std::string m_type;
/** List of FBOs registered for this wallpaper */ /** List of FBOs registered for this wallpaper */
std::map<std::string, CFBO*> m_fbos; std::map<std::string, std::shared_ptr<const CFBO>> m_fbos;
/** Audio context that is using this wallpaper */ /** Audio context that is using this wallpaper */
CAudioContext& m_audioContext; CAudioContext& m_audioContext;
/** Current Wallpaper state */ /** Current Wallpaper state */

View File

@ -18,7 +18,7 @@ const std::vector<Effects::CMaterial*>& CEffect::getMaterials () const {
return this->m_materials; return this->m_materials;
} }
const CFBO* CEffect::findFBO (const std::string& name) const { std::shared_ptr<const CFBO> CEffect::findFBO (const std::string& name) const {
const auto fbo = this->m_fbos.find (name); const auto fbo = this->m_fbos.find (name);
if (fbo == this->m_fbos.end ()) { if (fbo == this->m_fbos.end ()) {
@ -51,7 +51,7 @@ void CEffect::generateFBOs () {
} }
} }
const std::map<std::string, CFBO*>& CEffect::getFBOs () const { const std::map<std::string, std::shared_ptr<const CFBO>>& CEffect::getFBOs () const {
return this->m_fbos; return this->m_fbos;
} }

View File

@ -26,13 +26,13 @@ class CEffect {
[[nodiscard]] const std::vector<Effects::CMaterial*>& getMaterials () const; [[nodiscard]] const std::vector<Effects::CMaterial*>& getMaterials () const;
[[nodiscard]] const CFBO* findFBO (const std::string& name) const; [[nodiscard]] std::shared_ptr<const CFBO> findFBO (const std::string& name) const;
[[nodiscard]] bool isVisible () const; [[nodiscard]] bool isVisible () const;
protected: protected:
friend class WallpaperEngine::PrettyPrinter::CPrettyPrinter; friend class WallpaperEngine::PrettyPrinter::CPrettyPrinter;
[[nodiscard]] const std::map<std::string, CFBO*>& getFBOs () const; [[nodiscard]] const std::map<std::string, std::shared_ptr<const CFBO>>& getFBOs () const;
private: private:
void generatePasses (); void generatePasses ();
@ -41,7 +41,7 @@ class CEffect {
CImage* m_image; CImage* m_image;
const Core::Objects::CEffect* m_effect; const Core::Objects::CEffect* m_effect;
std::map<std::string, CFBO*> m_fbos; std::map<std::string, std::shared_ptr<const CFBO>> m_fbos;
std::vector<Effects::CMaterial*> m_materials; std::vector<Effects::CMaterial*> m_materials;
}; };
} // namespace WallpaperEngine::Render::Objects } // namespace WallpaperEngine::Render::Objects

View File

@ -96,7 +96,7 @@ CImage::CImage (Wallpapers::CScene* scene, const Core::Objects::CImage* image) :
// same applies to effects // same applies to effects
// TODO: create a dummy texture of correct size, fbo constructors should be enough, but this should be properly // TODO: create a dummy texture of correct size, fbo constructors should be enough, but this should be properly
// handled // handled
this->m_texture = new CFBO ( this->m_texture = std::make_shared<CFBO> (
"", ITexture::TextureFormat::ARGB8888, ITexture::TextureFlags::NoFlags, 1, size.x, "", ITexture::TextureFormat::ARGB8888, ITexture::TextureFlags::NoFlags, 1, size.x,
size.y, size.x, size.y); size.y, size.x, size.y);
} }
@ -305,8 +305,8 @@ void CImage::setup () {
void CImage::setupPasses () { void CImage::setupPasses () {
// do a pass on everything and setup proper inputs and values // do a pass on everything and setup proper inputs and values
const CFBO* drawTo = this->m_currentMainFBO; std::shared_ptr<const CFBO> drawTo = this->m_currentMainFBO;
const ITexture* asInput = this->getTexture (); std::shared_ptr<const ITexture> asInput = this->getTexture ();
GLuint texcoord = this->getTexCoordCopy (); GLuint texcoord = this->getTexCoordCopy ();
auto cur = this->m_passes.begin (); auto cur = this->m_passes.begin ();
@ -316,7 +316,7 @@ void CImage::setupPasses () {
for (; cur != end; ++cur) { for (; cur != end; ++cur) {
// TODO: PROPERLY CHECK EFFECT'S VISIBILITY AND TAKE IT INTO ACCOUNT // TODO: PROPERLY CHECK EFFECT'S VISIBILITY AND TAKE IT INTO ACCOUNT
Effects::CPass* pass = *cur; Effects::CPass* pass = *cur;
const CFBO* prevDrawTo = drawTo; std::shared_ptr<const CFBO> prevDrawTo = drawTo;
GLuint spacePosition = (first) ? this->getCopySpacePosition () : this->getPassSpacePosition (); GLuint spacePosition = (first) ? this->getCopySpacePosition () : this->getPassSpacePosition ();
const glm::mat4* projection = (first) ? &this->m_modelViewProjectionCopy : &this->m_modelViewProjectionPass; const glm::mat4* projection = (first) ? &this->m_modelViewProjectionCopy : &this->m_modelViewProjectionPass;
const glm::mat4* inverseProjection = 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<const CFBO>* drawTo, std::shared_ptr<const ITexture>* asInput) {
// temporarily store FBOs used // temporarily store FBOs used
CFBO* currentMainFBO = this->m_currentMainFBO; std::shared_ptr<const CFBO> currentMainFBO = this->m_currentMainFBO;
CFBO* currentSubFBO = this->m_currentSubFBO; std::shared_ptr<const CFBO> currentSubFBO = this->m_currentSubFBO;
if (drawTo != nullptr) if (drawTo != nullptr)
*drawTo = currentSubFBO; *drawTo = currentSubFBO;
@ -435,7 +435,7 @@ void CImage::updateScreenSpacePosition () {
{x, y, 0.0f}); {x, y, 0.0f});
} }
const ITexture* CImage::getTexture () const { std::shared_ptr<const ITexture> CImage::getTexture () const {
return this->m_texture; return this->m_texture;
} }

View File

@ -44,7 +44,7 @@ class CImage final : public CObject {
[[nodiscard]] GLuint getPassSpacePosition () const; [[nodiscard]] GLuint getPassSpacePosition () const;
[[nodiscard]] GLuint getTexCoordCopy () const; [[nodiscard]] GLuint getTexCoordCopy () const;
[[nodiscard]] GLuint getTexCoordPass () const; [[nodiscard]] GLuint getTexCoordPass () const;
[[nodiscard]] const ITexture* getTexture () const; [[nodiscard]] std::shared_ptr<const ITexture> getTexture () const;
[[nodiscard]] double getAnimationTime () const; [[nodiscard]] double getAnimationTime () const;
/** /**
@ -53,7 +53,7 @@ class CImage final : public CObject {
* @param drawTo The framebuffer to use * @param drawTo The framebuffer to use
* @param asInput The last texture used as output (if needed) * @param asInput The last texture used as output (if needed)
*/ */
void pinpongFramebuffer (const CFBO** drawTo, const ITexture** asInput); void pinpongFramebuffer (std::shared_ptr<const CFBO>* drawTo, std::shared_ptr<const ITexture>* asInput);
protected: protected:
static const std::string Type; static const std::string Type;
@ -63,7 +63,7 @@ class CImage final : public CObject {
void updateScreenSpacePosition (); void updateScreenSpacePosition ();
private: private:
const ITexture* m_texture; std::shared_ptr<const ITexture> m_texture;
GLuint m_sceneSpacePosition; GLuint m_sceneSpacePosition;
GLuint m_copySpacePosition; GLuint m_copySpacePosition;
GLuint m_passSpacePosition; GLuint m_passSpacePosition;
@ -80,10 +80,10 @@ class CImage final : public CObject {
glm::mat4 m_modelMatrix; glm::mat4 m_modelMatrix;
glm::mat4 m_viewProjectionMatrix; glm::mat4 m_viewProjectionMatrix;
CFBO* m_mainFBO; std::shared_ptr<const CFBO> m_mainFBO;
CFBO* m_subFBO; std::shared_ptr<const CFBO> m_subFBO;
CFBO* m_currentMainFBO; std::shared_ptr<const CFBO> m_currentMainFBO;
CFBO* m_currentSubFBO; std::shared_ptr<const CFBO> m_currentSubFBO;
const Core::Objects::CImage* m_image; const Core::Objects::CImage* m_image;

View File

@ -17,15 +17,13 @@ CSound::~CSound() {
delete stream; delete stream;
} }
for (const auto& buffer : this->m_soundBuffer) { this->m_soundBuffer.clear ();
delete buffer;
}
} }
void CSound::load () { void CSound::load () {
for (const auto& cur : this->m_sound->getSounds ()) { for (const auto& cur : this->m_sound->getSounds ()) {
uint32_t filesize = 0; uint32_t filesize = 0;
const uint8_t* filebuffer = this->getContainer ()->readFile (cur, &filesize); std::shared_ptr<const uint8_t[]> filebuffer = this->getContainer ()->readFile (cur, &filesize);
auto stream = new Audio::CAudioStream (this->getScene ()->getAudioContext (), filebuffer, filesize); auto stream = new Audio::CAudioStream (this->getScene ()->getAudioContext (), filebuffer, filesize);

View File

@ -25,7 +25,7 @@ class CSound final : public CObject {
void load (); void load ();
private: private:
std::vector<const uint8_t*> m_soundBuffer; std::vector<std::shared_ptr<const uint8_t[]>> m_soundBuffer;
std::vector<Audio::CAudioStream*> m_audioStreams; std::vector<Audio::CAudioStream*> m_audioStreams;
const Core::Objects::CSound* m_sound; const Core::Objects::CSound* m_sound;

View File

@ -42,7 +42,7 @@ CPass::CPass (CMaterial* material, const Core::Objects::Images::Materials::CPass
this->setupShaderVariables (); this->setupShaderVariables ();
} }
const ITexture* CPass::resolveTexture (const ITexture* expected, int index, const ITexture* previous) { std::shared_ptr<const ITexture> CPass::resolveTexture (std::shared_ptr<const ITexture> expected, int index, std::shared_ptr<const ITexture> previous) {
if (expected == nullptr) { if (expected == nullptr) {
const auto it = this->m_fbos.find (index); 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 ()); return this->resolveFBO (it->second->getName ());
} }
const CFBO* CPass::resolveFBO (const std::string& name) const { std::shared_ptr<const CFBO> CPass::resolveFBO (const std::string& name) const {
auto fbo = this->m_material->getEffect()->findFBO (name); auto fbo = this->m_material->getEffect()->findFBO (name);
if (fbo == nullptr) { if (fbo == nullptr) {
@ -124,7 +124,7 @@ void CPass::setupRenderTexture () {
glUseProgram (this->m_programID); glUseProgram (this->m_programID);
// maybe we can do this when setting the texture? // maybe we can do this when setting the texture?
const ITexture* texture = this->resolveTexture (this->m_input, 0, this->m_input); std::shared_ptr<const ITexture> texture = this->resolveTexture (this->m_input, 0, this->m_input);
uint32_t currentTexture = 0; uint32_t currentTexture = 0;
glm::vec2 translation = {0.0f, 0.0f}; glm::vec2 translation = {0.0f, 0.0f};
@ -291,12 +291,12 @@ const CMaterial* CPass::getMaterial () const {
return this->m_material; return this->m_material;
} }
void CPass::setDestination (const CFBO* drawTo) { void CPass::setDestination (std::shared_ptr<const CFBO> drawTo) {
this->m_drawTo = drawTo; this->m_drawTo = std::move(drawTo);
} }
void CPass::setInput (const ITexture* input) { void CPass::setInput (std::shared_ptr<const ITexture> input) {
this->m_input = input; this->m_input = std::move(input);
} }
void CPass::setModelViewProjectionMatrix (const glm::mat4* projection) { void CPass::setModelViewProjectionMatrix (const glm::mat4* projection) {
@ -379,7 +379,7 @@ GLuint CPass::compileShader (const char* shader, GLuint type) {
void CPass::setupShaders () { void CPass::setupShaders () {
// ensure the constants are defined // ensure the constants are defined
const ITexture* texture0 = this->m_material->getImage ()->getTexture (); std::shared_ptr<const ITexture> texture0 = this->m_material->getImage ()->getTexture ();
// copy the combos from the pass // copy the combos from the pass
this->m_combos.insert (this->m_pass->getCombos ().begin (), this->m_pass->getCombos ().end ()); 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 ()) { for (const auto& [index, textureName] : this->m_shader->getVertex ().getTextures ()) {
try { try {
// resolve the texture first // resolve the texture first
const ITexture* textureRef; std::shared_ptr<const ITexture> textureRef;
if (textureName.find ("_rt_") == 0 || textureName.find ("_alias_") == 0) { if (textureName.find ("_rt_") == 0 || textureName.find ("_alias_") == 0) {
textureRef = this->resolveFBO (textureName); textureRef = this->resolveFBO (textureName);
@ -493,7 +493,7 @@ void CPass::setupTextureUniforms () {
for (const auto& [index, textureName] : this->m_shader->getFragment ().getTextures ()) { for (const auto& [index, textureName] : this->m_shader->getFragment ().getTextures ()) {
try { try {
// resolve the texture first // resolve the texture first
const ITexture* textureRef; std::shared_ptr<const ITexture> textureRef;
if (textureName.find ("_rt_") == 0 || textureName.find ("_alias_") == 0) { if (textureName.find ("_rt_") == 0 || textureName.find ("_alias_") == 0) {
textureRef = this->resolveFBO (textureName); textureRef = this->resolveFBO (textureName);
@ -532,7 +532,7 @@ void CPass::setupTextureUniforms () {
} }
// resolve the main texture // resolve the main texture
const ITexture* texture = this->resolveTexture (this->m_material->getImage ()->getTexture (), 0); std::shared_ptr<const ITexture> texture = this->resolveTexture (this->m_material->getImage ()->getTexture (), 0);
// register all the texture uniforms with correct values // register all the texture uniforms with correct values
this->addUniform ("g_Texture0", 0); this->addUniform ("g_Texture0", 0);
this->addUniform ("g_Texture1", 1); this->addUniform ("g_Texture1", 1);

View File

@ -27,8 +27,8 @@ class CPass final : public Helpers::CContextAware {
void render (); void render ();
void setDestination (const CFBO* drawTo); void setDestination (std::shared_ptr<const CFBO> drawTo);
void setInput (const ITexture* input); void setInput (std::shared_ptr<const ITexture> input);
void setTexCoord (GLuint texcoord); void setTexCoord (GLuint texcoord);
void setPosition (GLuint position); void setPosition (GLuint position);
void setModelViewProjectionMatrix (const glm::mat4* projection); void setModelViewProjectionMatrix (const glm::mat4* projection);
@ -37,7 +37,7 @@ class CPass final : public Helpers::CContextAware {
void setViewProjectionMatrix (const glm::mat4* viewProjection); void setViewProjectionMatrix (const glm::mat4* viewProjection);
void setBlendingMode (std::string blendingmode); void setBlendingMode (std::string blendingmode);
[[nodiscard]] const std::string& getBlendingMode () const; [[nodiscard]] const std::string& getBlendingMode () const;
[[nodiscard]] const CFBO* resolveFBO (const std::string& name) const; [[nodiscard]] std::shared_ptr<const CFBO> resolveFBO (const std::string& name) const;
[[nodiscard]] const CMaterial* getMaterial () const; [[nodiscard]] const CMaterial* getMaterial () const;
[[nodiscard]] const Core::Objects::Images::Materials::CPass* getPass () const; [[nodiscard]] const Core::Objects::Images::Materials::CPass* getPass () const;
@ -146,11 +146,11 @@ class CPass final : public Helpers::CContextAware {
void renderGeometry () const; void renderGeometry () const;
void cleanupRenderSetup (); void cleanupRenderSetup ();
const ITexture* resolveTexture (const ITexture* expected, int index, const ITexture* previous = nullptr); std::shared_ptr<const ITexture> resolveTexture (std::shared_ptr<const ITexture> expected, int index, std::shared_ptr<const ITexture> previous = nullptr);
CMaterial* m_material; CMaterial* m_material;
const Core::Objects::Images::Materials::CPass* m_pass; const Core::Objects::Images::Materials::CPass* m_pass;
std::map<int, const CFBO*> m_fbos; std::map<int, std::shared_ptr<const CFBO>> m_fbos;
std::map<std::string, int> m_combos; std::map<std::string, int> m_combos;
std::vector<AttribEntry*> m_attribs; std::vector<AttribEntry*> m_attribs;
std::map<std::string, UniformEntry*> m_uniforms; std::map<std::string, UniformEntry*> m_uniforms;
@ -164,12 +164,12 @@ class CPass final : public Helpers::CContextAware {
/** /**
* Contains the final map of textures to be used * Contains the final map of textures to be used
*/ */
std::map<int, const ITexture*> m_textures; std::map<int, std::shared_ptr<const ITexture>> m_textures;
Render::Shaders::CShader* m_shader; Render::Shaders::CShader* m_shader;
const CFBO* m_drawTo; std::shared_ptr<const CFBO> m_drawTo;
const ITexture* m_input; std::shared_ptr<const ITexture> m_input;
GLuint m_programID; GLuint m_programID;

View File

@ -19,18 +19,18 @@ class CScene final : public CWallpaper {
const CWallpaperState::TextureUVsScaling& scalingMode, const CWallpaperState::TextureUVsScaling& scalingMode,
const WallpaperEngine::Assets::ITexture::TextureFlags& clampMode); 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; [[nodiscard]] int getWidth () const override;
int getHeight () const override; [[nodiscard]] int getHeight () const override;
glm::vec2* getMousePosition (); glm::vec2* getMousePosition ();
glm::vec2* getMousePositionLast (); glm::vec2* getMousePositionLast ();
glm::vec2* getParallaxDisplacement (); glm::vec2* getParallaxDisplacement ();
const std::vector<CObject*>& getObjectsByRenderOrder () const; [[nodiscard]] const std::vector<CObject*>& getObjectsByRenderOrder () const;
protected: protected:
void renderFrame (glm::ivec4 viewport) override; void renderFrame (glm::ivec4 viewport) override;
@ -50,8 +50,8 @@ class CScene final : public CWallpaper {
glm::vec2 m_mousePosition; glm::vec2 m_mousePosition;
glm::vec2 m_mousePositionLast; glm::vec2 m_mousePositionLast;
glm::vec2 m_parallaxDisplacement; glm::vec2 m_parallaxDisplacement;
CFBO* _rt_4FrameBuffer; std::shared_ptr<const CFBO> _rt_4FrameBuffer;
CFBO* _rt_8FrameBuffer; std::shared_ptr<const CFBO> _rt_8FrameBuffer;
CFBO* _rt_Bloom; std::shared_ptr<const CFBO> _rt_Bloom;
}; };
} // namespace WallpaperEngine::Render::Wallpaper } // namespace WallpaperEngine::Render::Wallpaper

View File

@ -22,9 +22,6 @@ bool CWPSchemeHandler::Open(CefRefPtr<CefRequest> request,
CefRefPtr<CefCallback> callback) { CefRefPtr<CefCallback> callback) {
DCHECK(!CefCurrentlyOn(TID_UI) && !CefCurrentlyOn(TID_IO)); 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 #if !NDEBUG
std::cout << "Processing request for path " << request->GetURL ().c_str () << std::endl; std::cout << "Processing request for path " << request->GetURL ().c_str () << std::endl;
#endif #endif

View File

@ -35,7 +35,7 @@ class CWPSchemeHandler : public CefResourceHandler {
const Core::CProject* m_project; const Core::CProject* m_project;
const Assets::CContainer* m_container; const Assets::CContainer* m_container;
const uint8_t* m_contents; std::shared_ptr<const uint8_t[]> m_contents;
uint32_t m_filesize; uint32_t m_filesize;
std::string m_mimeType; std::string m_mimeType;
uint32_t m_offset; uint32_t m_offset;