Browse Source

Container access should happen through CContext
Textures now have a cache system that prevents loading them more than once

Signed-off-by: Alexis Maiquez <almamu@almamu.com>

Alexis Maiquez 3 năm trước cách đây
mục cha
commit
c13d743022
41 tập tin đã thay đổi với 211 bổ sung và 122 xóa
  1. 2 0
      CMakeLists.txt
  2. 2 2
      main.cpp
  3. 1 1
      src/WallpaperEngine/Assets/CCombinedContainer.cpp
  4. 1 1
      src/WallpaperEngine/Assets/CCombinedContainer.h
  5. 7 7
      src/WallpaperEngine/Assets/CContainer.cpp
  6. 6 6
      src/WallpaperEngine/Assets/CContainer.h
  7. 1 7
      src/WallpaperEngine/Assets/CDirectory.cpp
  8. 1 1
      src/WallpaperEngine/Assets/CDirectory.h
  9. 2 2
      src/WallpaperEngine/Assets/CFileEntry.h
  10. 1 1
      src/WallpaperEngine/Assets/CPackage.cpp
  11. 1 1
      src/WallpaperEngine/Assets/CPackage.h
  12. 19 19
      src/WallpaperEngine/Assets/CTexture.cpp
  13. 4 4
      src/WallpaperEngine/Assets/CTexture.h
  14. 2 2
      src/WallpaperEngine/Audio/CAudioStream.cpp
  15. 3 3
      src/WallpaperEngine/Audio/CAudioStream.h
  16. 2 2
      src/WallpaperEngine/Core/Objects/Images/CMaterial.cpp
  17. 2 2
      src/WallpaperEngine/Core/Objects/Images/CMaterial.h
  18. 2 2
      src/WallpaperEngine/FileSystem/FileSystem.cpp
  19. 1 1
      src/WallpaperEngine/FileSystem/FileSystem.h
  20. 14 2
      src/WallpaperEngine/Render/CContext.cpp
  21. 7 1
      src/WallpaperEngine/Render/CContext.h
  22. 1 1
      src/WallpaperEngine/Render/CObject.cpp
  23. 1 1
      src/WallpaperEngine/Render/CObject.h
  24. 2 2
      src/WallpaperEngine/Render/CScene.cpp
  25. 1 1
      src/WallpaperEngine/Render/CScene.h
  26. 35 0
      src/WallpaperEngine/Render/CTextureCache.cpp
  27. 42 0
      src/WallpaperEngine/Render/CTextureCache.h
  28. 2 2
      src/WallpaperEngine/Render/CVideo.cpp
  29. 1 1
      src/WallpaperEngine/Render/CVideo.h
  30. 6 7
      src/WallpaperEngine/Render/CWallpaper.cpp
  31. 8 9
      src/WallpaperEngine/Render/CWallpaper.h
  32. 1 1
      src/WallpaperEngine/Render/Objects/CEffect.cpp
  33. 1 1
      src/WallpaperEngine/Render/Objects/CEffect.h
  34. 6 6
      src/WallpaperEngine/Render/Objects/CImage.cpp
  35. 3 3
      src/WallpaperEngine/Render/Objects/CImage.h
  36. 1 1
      src/WallpaperEngine/Render/Objects/CSound.cpp
  37. 1 1
      src/WallpaperEngine/Render/Objects/CSound.h
  38. 10 10
      src/WallpaperEngine/Render/Objects/Effects/CPass.cpp
  39. 5 5
      src/WallpaperEngine/Render/Objects/Effects/CPass.h
  40. 1 1
      src/WallpaperEngine/Render/Shaders/Compiler.cpp
  41. 2 2
      src/WallpaperEngine/Render/Shaders/Compiler.h

+ 2 - 0
CMakeLists.txt

@@ -79,6 +79,8 @@ add_executable(
 
         src/WallpaperEngine/Render/CContext.h
         src/WallpaperEngine/Render/CContext.cpp
+        src/WallpaperEngine/Render/CTextureCache.h
+        src/WallpaperEngine/Render/CTextureCache.cpp
 
         src/WallpaperEngine/Render/CWallpaper.h
         src/WallpaperEngine/Render/CWallpaper.cpp

+ 2 - 2
main.cpp

@@ -380,14 +380,14 @@ int main (int argc, char* argv[])
         chdir (path.c_str ());
 
     // initialize custom context class
-    WallpaperEngine::Render::CContext* context = new WallpaperEngine::Render::CContext (screens, window);
+    WallpaperEngine::Render::CContext* context = new WallpaperEngine::Render::CContext (screens, window, containers);
     // initialize mouse support
     context->setMouse (new CMouseInput (window));
     // set the default viewport
     context->setDefaultViewport ({0, 0, windowWidth, windowHeight});
     // ensure the context knows what wallpaper to render
     context->setWallpaper (
-        WallpaperEngine::Render::CWallpaper::fromWallpaper (project->getWallpaper (), containers, context)
+        WallpaperEngine::Render::CWallpaper::fromWallpaper (project->getWallpaper (), context)
     );
 
     // update maximum FPS if it's a video

+ 1 - 1
src/WallpaperEngine/Assets/CCombinedContainer.cpp

@@ -8,7 +8,7 @@ void CCombinedContainer::add (CContainer* container)
     this->m_containers.emplace_back (container);
 }
 
-void* CCombinedContainer::readFile (std::string filename, uint32_t* length)
+const void* CCombinedContainer::readFile (std::string filename, uint32_t* length) const
 {
     auto cur = this->m_containers.begin ();
     auto end = this->m_containers.end ();

+ 1 - 1
src/WallpaperEngine/Assets/CCombinedContainer.h

@@ -24,7 +24,7 @@ namespace WallpaperEngine::Assets
          */
         void add (CContainer* container);
 
-        void* readFile (std::string filename, uint32_t* length) override;
+        const void* readFile (std::string filename, uint32_t* length) const override;
 
     private:
         std::vector<CContainer*> m_containers;

+ 7 - 7
src/WallpaperEngine/Assets/CContainer.cpp

@@ -6,12 +6,12 @@
 
 using namespace WallpaperEngine::Assets;
 
-ITexture* CContainer::readTexture (std::string filename)
+const ITexture* CContainer::readTexture (std::string filename) const
 {
     // get the texture's filename (usually .tex)
     filename = "materials/" + filename + ".tex";
 
-    void* textureContents = this->readFile (filename, nullptr);
+    const void* textureContents = this->readFile (filename, nullptr);
 
     ITexture* result = new CTexture (textureContents);
 
@@ -21,27 +21,27 @@ ITexture* CContainer::readTexture (std::string filename)
     return result;
 }
 
-std::string CContainer::readVertexShader (const std::string& filename)
+std::string CContainer::readVertexShader (const std::string& filename) const
 {
     return this->readFileAsString ("shaders/" + filename + ".vert");
 }
 
-std::string CContainer::readFragmentShader (const std::string& filename)
+std::string CContainer::readFragmentShader (const std::string& filename) const
 {
     return this->readFileAsString ("shaders/" + filename + ".frag");
 }
 
-std::string CContainer::readIncludeShader (const std::string& filename)
+std::string CContainer::readIncludeShader (const std::string& filename) const
 {
     return this->readFileAsString ("shaders/" + filename);
 }
 
-std::string CContainer::readFileAsString (std::string filename)
+std::string CContainer::readFileAsString (std::string filename) const
 {
     uint32_t length = 0;
 
     // read file contents and allocate a buffer for a string
-    void* contents = this->readFile (std::move (filename), &length);
+    const void* contents = this->readFile (std::move (filename), &length);
     char* buffer = new char [length + 1];
 
     // ensure there's a 0 at the end

+ 6 - 6
src/WallpaperEngine/Assets/CContainer.h

@@ -17,7 +17,7 @@ namespace WallpaperEngine::Assets
          *
          * @return
          */
-        virtual void* readFile (std::string filename, uint32_t* length = nullptr) = 0;
+        const virtual void* readFile (std::string filename, uint32_t* length = nullptr) const = 0;
 
         /**
          * Wrapper for readFile, appends the texture extension at the end of the filename
@@ -26,7 +26,7 @@ namespace WallpaperEngine::Assets
          *
          * @return
          */
-        ITexture* readTexture (std::string filename);
+        const ITexture* readTexture (std::string filename) const;
 
         /**
          * Wrapper for readFile, appends the .vert extension at the end and opens the given shader file
@@ -35,7 +35,7 @@ namespace WallpaperEngine::Assets
          *
          * @return The shader code as an string to be used
          */
-        std::string readVertexShader (const std::string& filename);
+        std::string readVertexShader (const std::string& filename) const;
 
         /**
          * Wrapper for readFile, appends the .frag extension at the end and opens the given shader file
@@ -44,7 +44,7 @@ namespace WallpaperEngine::Assets
          *
          * @return The shader code as an string to be used
          */
-        std::string readFragmentShader (const std::string& filename);
+        std::string readFragmentShader (const std::string& filename) const;
 
         /**
          * Wrapper for readFile, appends the .h extension at the end and opens the given shader file
@@ -53,7 +53,7 @@ namespace WallpaperEngine::Assets
          *
          * @return The shader code as an string to be used
          */
-        std::string readIncludeShader (const std::string& filename);
+        std::string readIncludeShader (const std::string& filename) const;
 
     private:
         /**
@@ -63,6 +63,6 @@ namespace WallpaperEngine::Assets
          *
          * @return The file's contents as string
          */
-        std::string readFileAsString (std::string filename);
+        std::string readFileAsString (std::string filename) const;
     };
 }

+ 1 - 7
src/WallpaperEngine/Assets/CDirectory.cpp

@@ -23,7 +23,7 @@ CDirectory::~CDirectory ()
 
 }
 
-void* CDirectory::readFile (std::string filename, uint32_t* length)
+const void* CDirectory::readFile (std::string filename, uint32_t* length) const
 {
     std::string final = this->m_basepath + filename;
 
@@ -57,12 +57,6 @@ void* CDirectory::readFile (std::string filename, uint32_t* length)
         throw CAssetLoadException (filename, "Unexpected error when reading the file");
     }
 
-    // store it in the cache too
-    this->m_cache.insert (std::make_pair <std::string, CFileEntry> (
-        std::move (final),
-        CFileEntry (contents, size)
-    ));
-
     if (length != nullptr)
         *length = size;
 

+ 1 - 1
src/WallpaperEngine/Assets/CDirectory.h

@@ -18,7 +18,7 @@ namespace WallpaperEngine::Assets
         CDirectory (std::string basepath);
         ~CDirectory ();
 
-        void* readFile (std::string filename, uint32_t* length) override;
+        const void* readFile (std::string filename, uint32_t* length) const override;
     private:
         std::string m_basepath;
         std::map <std::string, CFileEntry> m_cache;

+ 2 - 2
src/WallpaperEngine/Assets/CFileEntry.h

@@ -9,11 +9,11 @@ namespace WallpaperEngine::Assets
     class CFileEntry
     {
     public:
-        CFileEntry (void* address, uint32_t length) :
+        CFileEntry (const void* address, uint32_t length) :
             address (address),
             length (length) { }
 
-        void* address;
+        const void* address;
         uint32_t length;
     };
 }

+ 1 - 1
src/WallpaperEngine/Assets/CPackage.cpp

@@ -33,7 +33,7 @@ CPackage::~CPackage()
 }
 
 
-void* CPackage::readFile (std::string filename, uint32_t* length)
+const void* CPackage::readFile (std::string filename, uint32_t* length) const
 {
     auto it = this->m_contents.find (filename);
 

+ 1 - 1
src/WallpaperEngine/Assets/CPackage.h

@@ -22,7 +22,7 @@ namespace WallpaperEngine::Assets
         CPackage (const std::string& path);
         ~CPackage ();
 
-        void* readFile (std::string filename, uint32_t* length) override;
+        const void* readFile (std::string filename, uint32_t* length) const override;
 
     protected:
         /**

+ 19 - 19
src/WallpaperEngine/Assets/CTexture.cpp

@@ -6,10 +6,10 @@
 
 using namespace WallpaperEngine::Assets;
 
-CTexture::CTexture (void* fileData)
+CTexture::CTexture (const void* fileData)
 {
     // ensure the header is parsed
-    this->m_header = this->parseHeader (static_cast <char*> (fileData));
+    this->m_header = this->parseHeader (static_cast <const char*> (fileData));
 
     GLint internalFormat;
 
@@ -334,7 +334,7 @@ CTexture::TextureHeader::~TextureHeader ()
     }
 }
 
-CTexture::TextureHeader* CTexture::parseHeader (char* fileData)
+CTexture::TextureHeader* CTexture::parseHeader (const char* fileData)
 {
     // check the magic value on the header first
     if (memcmp (fileData, "TEXV0005", 9) != 0)
@@ -349,7 +349,7 @@ CTexture::TextureHeader* CTexture::parseHeader (char* fileData)
 
     TextureHeader* header = new TextureHeader;
 
-    uint32_t* pointer = reinterpret_cast <uint32_t*> (fileData);
+    const uint32_t* pointer = reinterpret_cast <const uint32_t*> (fileData);
 
     header->format = static_cast <TextureFormat>(*pointer ++);
     header->flags = static_cast <TextureFlags> (*pointer ++);
@@ -361,9 +361,9 @@ CTexture::TextureHeader* CTexture::parseHeader (char* fileData)
 
     // now we're going to parse some more data that is string
     // so get the current position back as string
-    fileData = reinterpret_cast <char*> (pointer);
+    fileData = reinterpret_cast <const char*> (pointer);
     // get the position of what comes after the texture data
-    pointer = reinterpret_cast <uint32_t*> (fileData + 9);
+    pointer = reinterpret_cast <const uint32_t*> (fileData + 9);
 
     if (memcmp (fileData, "TEXB0003", 9) == 0)
     {
@@ -393,7 +393,7 @@ CTexture::TextureHeader* CTexture::parseHeader (char* fileData)
         header->mipmapCount = *pointer ++;
         std::vector <TextureMipmap*> mipmaps;
 
-        fileData = reinterpret_cast <char*> (pointer);
+        fileData = reinterpret_cast <const char*> (pointer);
 
         for (uint32_t i = 0; i < header->mipmapCount; i ++)
             mipmaps.emplace_back (parseMipmap (header, &fileData));
@@ -401,7 +401,7 @@ CTexture::TextureHeader* CTexture::parseHeader (char* fileData)
         // add the pixmaps back
         header->images.insert (std::pair <uint32_t, std::vector <TextureMipmap*>> (image, mipmaps));
 
-        pointer = reinterpret_cast <uint32_t*> (fileData);
+        pointer = reinterpret_cast <const uint32_t*> (fileData);
     }
 
     // gifs have extra information after the mipmaps
@@ -421,7 +421,7 @@ CTexture::TextureHeader* CTexture::parseHeader (char* fileData)
         }
 
         // get an integer pointer back to read the frame count
-        pointer = reinterpret_cast <uint32_t*> (fileData + 9);
+        pointer = reinterpret_cast <const uint32_t*> (fileData + 9);
         uint32_t framecount = *pointer++;
 
         if (header->animatedVersion == AnimatedVersion::TEXS0003)
@@ -432,7 +432,7 @@ CTexture::TextureHeader* CTexture::parseHeader (char* fileData)
         }
 
         // get back the pointer into filedata
-        fileData = reinterpret_cast <char*> (pointer);
+        fileData = reinterpret_cast <const char*> (pointer);
 
         while (framecount > 0)
         {
@@ -455,11 +455,11 @@ CTexture::TextureHeader* CTexture::parseHeader (char* fileData)
     return header;
 }
 
-CTexture::TextureFrame* CTexture::parseAnimation (TextureHeader* header, char** originalFileData)
+CTexture::TextureFrame* CTexture::parseAnimation (TextureHeader* header, const char** originalFileData)
 {
-    char* fileData = *originalFileData;
+    const char* fileData = *originalFileData;
     // get back the pointer into integer
-    uint32_t* pointer = reinterpret_cast <uint32_t*> (fileData);
+    const uint32_t* pointer = reinterpret_cast <const uint32_t*> (fileData);
 
     // start reading frame information
     TextureFrame* frame = new TextureFrame();
@@ -467,7 +467,7 @@ CTexture::TextureFrame* CTexture::parseAnimation (TextureHeader* header, char**
     frame->frameNumber = *pointer++;
 
     // reinterpret the pointer into float
-    float* fPointer = reinterpret_cast <float*> (pointer);
+    const float* fPointer = reinterpret_cast <const float*> (pointer);
 
     frame->frametime = *fPointer ++;
     frame->x = *fPointer ++;
@@ -478,20 +478,20 @@ CTexture::TextureFrame* CTexture::parseAnimation (TextureHeader* header, char**
     frame->height1 = *fPointer ++;
 
     // get back the pointer into fileData so it can be reused later
-    *originalFileData = reinterpret_cast <char*> (fPointer);
+    *originalFileData = reinterpret_cast <const char*> (fPointer);
 
     return frame;
 }
 
-CTexture::TextureMipmap* CTexture::parseMipmap (TextureHeader* header, char** originalFileData)
+CTexture::TextureMipmap* CTexture::parseMipmap (TextureHeader* header, const char** originalFileData)
 {
     TextureMipmap* mipmap = new TextureMipmap ();
 
     // get the current position
-    char* fileData = *originalFileData;
+    const char* fileData = *originalFileData;
 
     // get an integer pointer
-    uint32_t* pointer = reinterpret_cast <uint32_t*> (fileData);
+    const uint32_t* pointer = reinterpret_cast <const uint32_t*> (fileData);
 
     mipmap->width = *pointer++;
     mipmap->height = *pointer++;
@@ -506,7 +506,7 @@ CTexture::TextureMipmap* CTexture::parseMipmap (TextureHeader* header, char** or
     mipmap->compressedSize = *pointer++;
 
     // get back a normal char pointer
-    fileData = reinterpret_cast <char*> (pointer);
+    fileData = reinterpret_cast <const char*> (pointer);
 
     if (mipmap->compression == 0)
     {

+ 4 - 4
src/WallpaperEngine/Assets/CTexture.h

@@ -18,7 +18,7 @@ namespace WallpaperEngine::Assets
         struct TextureHeader;
 
     public:
-        CTexture (void* fileData);
+        CTexture (const void* fileData);
         ~CTexture ();
 
         const GLuint getTextureID (uint32_t imageIndex = 0) const override;
@@ -116,9 +116,9 @@ namespace WallpaperEngine::Assets
             std::vector <TextureFrame*> frames;
         };
     private:
-        static TextureHeader* parseHeader (char* fileData);
-        static TextureFrame* parseAnimation (TextureHeader* header, char** originalFileData);
-        static TextureMipmap* parseMipmap (TextureHeader* header, char** fileData);
+        static TextureHeader* parseHeader (const char* fileData);
+        static TextureFrame* parseAnimation (TextureHeader* header, const char** originalFileData);
+        static TextureMipmap* parseMipmap (TextureHeader* header, const char** fileData);
 
         TextureHeader* m_header;
         GLuint* m_textureID;

+ 2 - 2
src/WallpaperEngine/Audio/CAudioStream.cpp

@@ -134,7 +134,7 @@ CAudioStream::CAudioStream (const std::string& filename)
     this->loadCustomContent (filename.c_str ());
 }
 
-CAudioStream::CAudioStream (void* buffer, int length)
+CAudioStream::CAudioStream (const void* buffer, int length)
 {
     // do not do anything if sdl audio was not initialized
     if (SDL_WasInit (SDL_INIT_AUDIO) != SDL_INIT_AUDIO)
@@ -363,7 +363,7 @@ bool CAudioStream::isRepeat ()
     return this->m_repeat;
 }
 
-void* CAudioStream::getBuffer ()
+const void* CAudioStream::getBuffer ()
 {
     return this->m_buffer;
 }

+ 3 - 3
src/WallpaperEngine/Audio/CAudioStream.h

@@ -26,7 +26,7 @@ namespace WallpaperEngine
         {
         public:
             CAudioStream (const std::string& filename);
-            CAudioStream (void* buffer, int length);
+            CAudioStream (const void* buffer, int length);
             CAudioStream (AVCodecContext* context);
 
             void queuePacket (AVPacket* pkt);
@@ -47,7 +47,7 @@ namespace WallpaperEngine
             void setRepeat (bool newRepeat = true);
             bool isRepeat ();
             void stop ();
-            void* getBuffer ();
+            const void* getBuffer ();
             int getLength ();
             int getPosition ();
             void setPosition (int current);
@@ -65,7 +65,7 @@ namespace WallpaperEngine
             AVCodecContext* m_context = nullptr;
             AVFormatContext* m_formatContext = nullptr;
             int m_audioStream = -1;
-            void* m_buffer;
+            const void* m_buffer;
             int m_length;
             int m_position = 0;
 

+ 2 - 2
src/WallpaperEngine/Core/Objects/Images/CMaterial.cpp

@@ -15,13 +15,13 @@ CMaterial::CMaterial (const std::string& name) :
 {
 }
 
-CMaterial* CMaterial::fromFile (const std::string& filename, CContainer* container)
+CMaterial* CMaterial::fromFile (const std::string& filename, const CContainer* container)
 {
     return fromJSON (
         filename, json::parse (WallpaperEngine::FileSystem::loadFullFile (filename, container))
     );
 }
-CMaterial* CMaterial::fromFile (const std::string& filename, const std::string& target, CContainer* container)
+CMaterial* CMaterial::fromFile (const std::string& filename, const std::string& target, const CContainer* container)
 {
     return fromJSON (
         filename, json::parse (WallpaperEngine::FileSystem::loadFullFile (filename, container)), target

+ 2 - 2
src/WallpaperEngine/Core/Objects/Images/CMaterial.h

@@ -14,9 +14,9 @@ namespace WallpaperEngine::Core::Objects::Images
     class CMaterial
     {
     public:
-        static CMaterial* fromFile (const std::string& filename, CContainer* container);
+        static CMaterial* fromFile (const std::string& filename, const CContainer* container);
         static CMaterial* fromJSON (const std::string& name, json data);
-        static CMaterial* fromFile (const std::string& filename, const std::string& target, CContainer* container);
+        static CMaterial* fromFile (const std::string& filename, const std::string& target, const CContainer* container);
         static CMaterial* fromJSON (const std::string& name, json data, const std::string& target);
 
         void insertPass (Materials::CPass* mass);

+ 2 - 2
src/WallpaperEngine/FileSystem/FileSystem.cpp

@@ -5,10 +5,10 @@
 
 using namespace WallpaperEngine;
 
-std::string FileSystem::loadFullFile (const std::string& file, WallpaperEngine::Assets::CContainer* containers)
+std::string FileSystem::loadFullFile (const std::string& file, const WallpaperEngine::Assets::CContainer* containers)
 {
     uint32_t length = 0;
-    void* contents = containers->readFile (file, &length);
+    const void* contents = containers->readFile (file, &length);
 
     // build a new buffer that can fit in the string
     char* filedata = new char [length + 1];

+ 1 - 1
src/WallpaperEngine/FileSystem/FileSystem.h

@@ -17,5 +17,5 @@ namespace WallpaperEngine::FileSystem
      * @param file
      * @return
      */
-    std::string loadFullFile (const std::string& file, WallpaperEngine::Assets::CContainer* containers);
+    std::string loadFullFile (const std::string& file, const WallpaperEngine::Assets::CContainer* containers);
 }

+ 14 - 2
src/WallpaperEngine/Render/CContext.cpp

@@ -48,12 +48,14 @@ int CustomXIOErrorHandler (Display* dsp)
     return 0;
 }
 
-CContext::CContext (std::vector <std::string> screens, GLFWwindow* window) :
+CContext::CContext (std::vector <std::string> screens, GLFWwindow* window, CContainer* container) :
     m_wallpaper (nullptr),
     m_screens (std::move (screens)),
     m_isRootWindow (m_screens.empty () == false),
     m_defaultViewport ({0, 0, 1920, 1080}),
-    m_window (window)
+    m_window (window),
+    m_container (container),
+    m_textureCache (new CTextureCache (this))
 {
     this->initializeViewports ();
 }
@@ -254,4 +256,14 @@ void CContext::setMouse (CMouseInput* mouse)
 CWallpaper* CContext::getWallpaper () const
 {
     return this->m_wallpaper;
+}
+
+const CContainer* CContext::getContainer () const
+{
+    return this->m_container;
+}
+
+const ITexture* CContext::resolveTexture (const std::string& name)
+{
+    return this->m_textureCache->resolve (name);
 }

+ 7 - 1
src/WallpaperEngine/Render/CContext.h

@@ -4,6 +4,7 @@
 #include <glm/vec4.hpp>
 
 #include "WallpaperEngine/Input/CMouseInput.h"
+#include "CTextureCache.h"
 #include "CWallpaper.h"
 #include <X11/Xlib.h>
 
@@ -12,11 +13,12 @@ using namespace WallpaperEngine::Input;
 namespace WallpaperEngine::Render
 {
     class CWallpaper;
+    class CTextureCache;
 
     class CContext
     {
     public:
-        CContext (std::vector <std::string> screens, GLFWwindow* window);
+        CContext (std::vector <std::string> screens, GLFWwindow* window, CContainer* container);
         ~CContext ();
 
         void initializeViewports ();
@@ -26,6 +28,8 @@ namespace WallpaperEngine::Render
         CMouseInput* getMouse () const;
         void setMouse (CMouseInput* mouse);
         CWallpaper* getWallpaper () const;
+        const CContainer* getContainer () const;
+        const ITexture* resolveTexture (const std::string& name);
 
     private:
         Display* m_display;
@@ -41,5 +45,7 @@ namespace WallpaperEngine::Render
         CWallpaper* m_wallpaper;
         CMouseInput* m_mouse;
         bool m_isRootWindow;
+        CContainer* m_container;
+        CTextureCache* m_textureCache;
     };
 }

+ 1 - 1
src/WallpaperEngine/Render/CObject.cpp

@@ -21,7 +21,7 @@ CScene* CObject::getScene () const
     return this->m_scene;
 }
 
-CContainer* CObject::getContainer () const
+const CContainer* CObject::getContainer () const
 {
     return this->getScene ()->getContainer ();
 }

+ 1 - 1
src/WallpaperEngine/Render/CObject.h

@@ -21,7 +21,7 @@ namespace WallpaperEngine::Render
         virtual void render () = 0;
 
         CScene* getScene () const;
-        CContainer* getContainer () const;
+        const CContainer* getContainer () const;
         const int getId () const;
 
     protected:

+ 2 - 2
src/WallpaperEngine/Render/CScene.cpp

@@ -9,8 +9,8 @@
 using namespace WallpaperEngine;
 using namespace WallpaperEngine::Render;
 
-CScene::CScene (Core::CScene* scene, CContainer* container, CContext* context) :
-    CWallpaper (scene, Type, container, context)
+CScene::CScene (Core::CScene* scene, CContext* context) :
+    CWallpaper (scene, Type, context)
 {
     // setup the scene camera
     this->m_camera = new CCamera (this, scene->getCamera ());

+ 1 - 1
src/WallpaperEngine/Render/CScene.h

@@ -15,7 +15,7 @@ namespace WallpaperEngine::Render
     class CScene : public CWallpaper
     {
     public:
-        CScene (Core::CScene* scene, CContainer* container, CContext* context);
+        CScene (Core::CScene* scene, CContext* context);
 
         CCamera* getCamera () const;
 

+ 35 - 0
src/WallpaperEngine/Render/CTextureCache.cpp

@@ -0,0 +1,35 @@
+#include "CTextureCache.h"
+
+using namespace WallpaperEngine::Render;
+
+CTextureCache::CTextureCache (CContext* context) :
+    m_context (context)
+{
+}
+
+CTextureCache::~CTextureCache ()
+{
+}
+
+const ITexture* CTextureCache::resolve (const std::string& filename)
+{
+    auto found = this->m_textureCache.find (filename);
+
+    if (found != this->m_textureCache.end ())
+        return (*found).second;
+
+    const ITexture* texture = this->m_context->getContainer ()->readTexture (filename);
+
+    this->m_textureCache.insert (
+        std::make_pair (filename, texture)
+    );
+
+    return texture;
+}
+
+void CTextureCache::store (std::string name, const ITexture* texture)
+{
+    this->m_textureCache.insert (
+        std::make_pair (name, texture)
+    );
+}

+ 42 - 0
src/WallpaperEngine/Render/CTextureCache.h

@@ -0,0 +1,42 @@
+#pragma once
+
+#include <map>
+#include <string>
+
+#include "WallpaperEngine/Assets/ITexture.h"
+#include "WallpaperEngine/Render/CContext.h"
+
+using namespace WallpaperEngine::Assets;
+
+namespace WallpaperEngine::Render
+{
+    class CContext;
+
+    class CTextureCache
+    {
+    public:
+        CTextureCache (CContext* context);
+        ~CTextureCache ();
+
+        /**
+         * Checks if the given texture was already loaded and returns it
+         * If the texture was not loaded yet, it tries to load it from the container
+         *
+         * @param filename
+         * @return
+         */
+        const ITexture* resolve (const std::string& filename);
+
+        /**
+         * Registers a texture in the cache
+         *
+         * @param name
+         * @param texture
+         */
+        void store (std::string name, const ITexture* texture);
+
+    private:
+        CContext* m_context;
+        std::map<std::string, const ITexture*> m_textureCache;
+    };
+}

+ 2 - 2
src/WallpaperEngine/Render/CVideo.cpp

@@ -3,8 +3,8 @@
 using namespace WallpaperEngine;
 using namespace WallpaperEngine::Render;
 
-CVideo::CVideo (Core::CVideo* video, CContainer* container, CContext* context) :
-    CWallpaper (video, Type, container, context)
+CVideo::CVideo (Core::CVideo* video, CContext* context) :
+    CWallpaper (video, Type, context)
 {
     if (avformat_open_input (&m_formatCtx, video->getFilename ().c_str (), NULL, NULL) < 0)
         throw std::runtime_error ("Failed to open video file");

+ 1 - 1
src/WallpaperEngine/Render/CVideo.h

@@ -18,7 +18,7 @@ namespace WallpaperEngine::Render
     class CVideo : public CWallpaper
     {
     public:
-        CVideo (Core::CVideo* video, CContainer* container, CContext* context);
+        CVideo (Core::CVideo* video, CContext* context);
 
         Core::CVideo* getVideo ();
 

+ 6 - 7
src/WallpaperEngine/Render/CWallpaper.cpp

@@ -8,8 +8,7 @@
 
 using namespace WallpaperEngine::Render;
 
-CWallpaper::CWallpaper (Core::CWallpaper* wallpaperData, std::string type, CContainer* container, CContext* context) :
-    m_container (container),
+CWallpaper::CWallpaper (Core::CWallpaper* wallpaperData, std::string type, CContext* context) :
     m_wallpaperData (wallpaperData),
     m_type (std::move(type)),
     m_context (context),
@@ -49,9 +48,9 @@ CWallpaper::~CWallpaper ()
 {
 }
 
-CContainer* CWallpaper::getContainer () const
+const CContainer* CWallpaper::getContainer () const
 {
-    return this->m_container;
+    return this->m_context->getContainer ();
 }
 
 WallpaperEngine::Core::CWallpaper* CWallpaper::getWallpaperData ()
@@ -364,12 +363,12 @@ CFBO* CWallpaper::getFBO () const
     return this->m_sceneFBO;
 }
 
-CWallpaper* CWallpaper::fromWallpaper (Core::CWallpaper* wallpaper, CContainer* containers, CContext* context)
+CWallpaper* CWallpaper::fromWallpaper (Core::CWallpaper* wallpaper, CContext* context)
 {
     if (wallpaper->is <Core::CScene> () == true)
-        return new WallpaperEngine::Render::CScene (wallpaper->as <Core::CScene> (), containers, context);
+        return new WallpaperEngine::Render::CScene (wallpaper->as <Core::CScene> (), context);
     else if (wallpaper->is <Core::CVideo> () == true)
-        return new WallpaperEngine::Render::CVideo (wallpaper->as <Core::CVideo> (), containers, context);
+        return new WallpaperEngine::Render::CVideo (wallpaper->as <Core::CVideo> (), context);
     else
         throw std::runtime_error ("Unsupported wallpaper type");
 }

+ 8 - 9
src/WallpaperEngine/Render/CWallpaper.h

@@ -25,7 +25,7 @@ namespace WallpaperEngine::Render
 
         template<class T> bool is () { return this->m_type == T::Type; }
 
-        CWallpaper (Core::CWallpaper* wallpaperData, std::string type, CContainer* container, CContext* context);
+        CWallpaper (Core::CWallpaper* wallpaperData, std::string type, CContext* context);
         ~CWallpaper ();
 
         /**
@@ -36,7 +36,12 @@ namespace WallpaperEngine::Render
         /**
          * @return The container to resolve files for this wallpaper
          */
-        CContainer* getContainer () const;
+        const CContainer* getContainer () const;
+
+        /**
+         * @return The current context rendering this wallpaper
+         */
+        CContext* getContext ();
 
         /**
          * @return The scene's framebuffer
@@ -90,7 +95,7 @@ namespace WallpaperEngine::Render
          *
          * @return
          */
-        static CWallpaper* fromWallpaper (Core::CWallpaper* wallpaper, CContainer* containers, CContext* context);
+        static CWallpaper* fromWallpaper (Core::CWallpaper* wallpaper, CContext* context);
 
     protected:
         /**
@@ -103,12 +108,6 @@ namespace WallpaperEngine::Render
          */
         void setupFramebuffers ();
 
-        /**
-         * @return The current context rendering this wallpaper
-         */
-        CContext* getContext ();
-
-        CContainer* m_container;
         Core::CWallpaper* m_wallpaperData;
 
         Core::CWallpaper* getWallpaperData ();

+ 1 - 1
src/WallpaperEngine/Render/Objects/CEffect.cpp

@@ -21,7 +21,7 @@ const std::vector<Effects::CMaterial*>& CEffect::getMaterials () const
     return this->m_materials;
 }
 
-CFBO* CEffect::findFBO (const std::string& name) const
+const CFBO* CEffect::findFBO (const std::string& name) const
 {
     auto cur = this->m_fbos.begin ();
     auto end = this->m_fbos.end ();

+ 1 - 1
src/WallpaperEngine/Render/Objects/CEffect.h

@@ -23,7 +23,7 @@ namespace WallpaperEngine::Render::Objects
 
         const std::vector<Effects::CMaterial*>& getMaterials () const;
 
-        CFBO* findFBO (const std::string& name) const;
+        const CFBO* findFBO (const std::string& name) const;
 
     private:
         void generatePasses ();

+ 6 - 6
src/WallpaperEngine/Render/Objects/CImage.cpp

@@ -53,7 +53,7 @@ CImage::CImage (CScene* scene, Core::Objects::CImage* image) :
         else
         {
             // get the first texture on the first pass (this one represents the image assigned to this object)
-            this->m_texture = this->getScene ()->getContainer ()->readTexture (textureName);
+            this->m_texture = this->getScene ()->getContext ()->resolveTexture (textureName);
         }
     }
     else
@@ -304,7 +304,7 @@ void CImage::setup ()
     this->m_initialized = true;
 }
 
-void CImage::pinpongFramebuffer (CFBO** drawTo, ITexture** asInput)
+void CImage::pinpongFramebuffer (const CFBO** drawTo, const ITexture** asInput)
 {
     // temporarily store FBOs used
     CFBO* currentMainFBO = this->m_currentMainFBO;
@@ -333,8 +333,8 @@ void CImage::render ()
     glColorMask (true, true, true, true);
 
     // start drawing to the main framebuffer
-    CFBO* drawTo = this->m_currentMainFBO;
-    ITexture* asInput = this->getTexture ();
+    const CFBO* drawTo = this->m_currentMainFBO;
+    const ITexture* asInput = this->getTexture ();
     GLuint texcoord = *this->getTexCoordCopy ();
 
     auto cur = this->m_passes.begin ();
@@ -343,7 +343,7 @@ void CImage::render ()
     for (; cur != end; cur ++)
     {
         Effects::CPass* pass = *cur;
-        CFBO* prevDrawTo = drawTo;
+        const CFBO* prevDrawTo = drawTo;
         GLuint spacePosition = *this->getCopySpacePosition ();
         glm::mat4 projection = this->m_modelViewProjectionPass;
 
@@ -381,7 +381,7 @@ void CImage::render ()
     }
 }
 
-ITexture* CImage::getTexture () const
+const ITexture* CImage::getTexture () const
 {
     return this->m_texture;
 }

+ 3 - 3
src/WallpaperEngine/Render/Objects/CImage.h

@@ -46,7 +46,7 @@ namespace WallpaperEngine::Render::Objects
         const GLuint* getPassSpacePosition () const;
         const GLuint* getTexCoordCopy () const;
         const GLuint* getTexCoordPass () const;
-        ITexture* getTexture () const;
+        const ITexture* getTexture () const;
         const double getAnimationTime () const;
 
         /**
@@ -55,12 +55,12 @@ namespace WallpaperEngine::Render::Objects
          * @param drawTo The framebuffer to use
          * @param asInput The last texture used as output (if needed)
          */
-        void pinpongFramebuffer (CFBO** drawTo, ITexture** asInput);
+        void pinpongFramebuffer (const CFBO** drawTo, const ITexture** asInput);
 
     protected:
         static const std::string Type;
     private:
-        ITexture* m_texture;
+        const ITexture* m_texture;
         GLuint m_sceneSpacePosition;
         GLuint m_copySpacePosition;
         GLuint m_passSpacePosition;

+ 1 - 1
src/WallpaperEngine/Render/Objects/CSound.cpp

@@ -19,7 +19,7 @@ void CSound::load ()
     for (; cur != end; cur ++)
     {
         uint32_t filesize = 0;
-        void* filebuffer = this->getContainer ()->readFile ((*cur), &filesize);
+        const void* filebuffer = this->getContainer ()->readFile ((*cur), &filesize);
 
         this->m_audioStreams.push_back (new Audio::CAudioStream (filebuffer, filesize));
         this->m_soundBuffer.push_back (filebuffer);

+ 1 - 1
src/WallpaperEngine/Render/Objects/CSound.h

@@ -22,7 +22,7 @@ namespace WallpaperEngine::Render::Objects
         void load ();
 
     private:
-        std::vector <void*> m_soundBuffer;
+        std::vector <const void*> m_soundBuffer;
         std::vector <Audio::CAudioStream*> m_audioStreams;
 
         Core::Objects::CSound* m_sound;

+ 10 - 10
src/WallpaperEngine/Render/Objects/Effects/CPass.cpp

@@ -31,7 +31,7 @@ CPass::CPass (CMaterial* material, Core::Objects::Images::Materials::CPass* pass
 }
 
 
-ITexture* CPass::resolveTexture (ITexture* expected, int index, ITexture* previous)
+const ITexture* CPass::resolveTexture (const ITexture* expected, int index, const ITexture* previous)
 {
     if (expected == nullptr)
     {
@@ -62,7 +62,7 @@ ITexture* CPass::resolveTexture (ITexture* expected, int index, ITexture* previo
     return fbo;
 }
 
-void CPass::render (CFBO* drawTo, ITexture* input, GLuint position, GLuint texcoord, glm::mat4 projection)
+void CPass::render (const CFBO* drawTo, const ITexture* input, GLuint position, GLuint texcoord, glm::mat4 projection)
 {
     // set the framebuffer we're drawing to
     glBindFramebuffer (GL_FRAMEBUFFER, drawTo->getFramebuffer ());
@@ -131,7 +131,7 @@ void CPass::render (CFBO* drawTo, ITexture* input, GLuint position, GLuint texco
     // use the shader we have registered
     glUseProgram (this->m_programID);
 
-    ITexture* texture = this->resolveTexture (input, 0, input);
+    const ITexture* texture = this->resolveTexture (input, 0, input);
 
     uint32_t currentTexture = 0;
     glm::vec2 translation = {0.0f, 0.0f};
@@ -437,7 +437,7 @@ void CPass::setupAttributes ()
 void CPass::setupUniforms ()
 {
     // resolve the main texture
-    ITexture* texture = this->resolveTexture (this->m_material->getImage ()->getTexture (), 0);
+    const ITexture* 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);
@@ -488,7 +488,7 @@ void CPass::setupUniforms ()
                 try
                 {
                     // resolve the texture first
-                    ITexture* textureRef = nullptr;
+                    const ITexture* textureRef = nullptr;
 
                     if (textureName.find ("_rt_") == 0)
                     {
@@ -498,7 +498,7 @@ void CPass::setupUniforms ()
                             textureRef = this->getMaterial ()->getImage ()->getScene ()->findFBO (textureName);
                     }
                     else
-                        textureRef = this->getMaterial ()->getImage ()->getContainer ()->readTexture (textureName);
+                        textureRef = this->getMaterial ()->getImage ()->getScene ()->getContext ()->resolveTexture (textureName);
 
                     this->m_finalTextures.insert (std::make_pair ((*fragCur).first, textureRef));
                 }
@@ -517,7 +517,7 @@ void CPass::setupUniforms ()
                 try
                 {
                     // resolve the texture first
-                    ITexture* textureRef = nullptr;
+                    const ITexture* textureRef = nullptr;
 
                     if (textureName.find ("_rt_") == 0)
                     {
@@ -527,7 +527,7 @@ void CPass::setupUniforms ()
                             textureRef = this->getMaterial ()->getImage ()->getScene ()->findFBO (textureName);
                     }
                     else
-                        textureRef = this->getMaterial ()->getImage ()->getContainer ()->readTexture (textureName);
+                        textureRef = this->getMaterial ()->getImage ()->getScene ()->getContext ()->resolveTexture (textureName);
 
                     this->m_finalTextures.insert (std::make_pair ((*vertCur).first, textureRef));
                 }
@@ -631,7 +631,7 @@ void CPass::setupTextures ()
 
         if ((*cur).find ("_rt_") == 0)
         {
-            CFBO* fbo = this->m_material->m_effect->findFBO ((*cur));
+            const CFBO* fbo = this->m_material->m_effect->findFBO ((*cur));
 
             if (fbo == nullptr)
                 fbo = this->m_material->getImage ()->getScene ()->findFBO ((*cur));
@@ -654,7 +654,7 @@ void CPass::setupTextures ()
             else
             {
                 this->m_textures.emplace_back (
-                    this->m_material->getImage ()->getContainer ()->readTexture ((*cur))
+                    this->m_material->getImage ()->getScene ()->getContext ()->resolveTexture ((*cur))
                 );
             }
         }

+ 5 - 5
src/WallpaperEngine/Render/Objects/Effects/CPass.h

@@ -21,7 +21,7 @@ namespace WallpaperEngine::Render::Objects::Effects
     public:
         CPass (CMaterial* material, Core::Objects::Images::Materials::CPass* pass);
 
-        void render (CFBO* drawTo, ITexture* input, GLuint position, GLuint texcoord, glm::mat4 projection);
+        void render (const CFBO* drawTo, const ITexture* input, GLuint position, GLuint texcoord, glm::mat4 projection);
 
         const CMaterial* getMaterial () const;
         Core::Objects::Images::Materials::CPass* getPass ();
@@ -87,12 +87,12 @@ namespace WallpaperEngine::Render::Objects::Effects
         template <typename T> void addUniform (const std::string& name, UniformType type, T value);
         template <typename T> void addUniform (const std::string& name, UniformType type, T* value);
 
-        ITexture* resolveTexture (ITexture* expected, int index, ITexture* previous = nullptr);
+        const ITexture* resolveTexture (const ITexture* expected, int index, const ITexture* previous = nullptr);
 
         CMaterial* m_material;
         Core::Objects::Images::Materials::CPass* m_pass;
-        std::vector<ITexture*> m_textures;
-        std::map<int, CFBO*> m_fbos;
+        std::vector<const ITexture*> m_textures;
+        std::map<int, const CFBO*> m_fbos;
         std::map <std::string, bool> m_foundCombos;
         std::vector<AttribEntry*> m_attribs;
         std::map<std::string, UniformEntry*> m_uniforms;
@@ -101,7 +101,7 @@ namespace WallpaperEngine::Render::Objects::Effects
         /**
          * Contains the final map of textures to be used
          */
-        std::map <int, ITexture*> m_finalTextures;
+        std::map <int, const ITexture*> m_finalTextures;
 
         Render::Shaders::Compiler* m_fragShader;
         Render::Shaders::Compiler* m_vertShader;

+ 1 - 1
src/WallpaperEngine/Render/Shaders/Compiler.cpp

@@ -26,7 +26,7 @@ using namespace WallpaperEngine::Assets;
 namespace WallpaperEngine::Render::Shaders
 {
     Compiler::Compiler (
-            CContainer* container,
+            const CContainer* container,
             std::string filename,
             Type type,
             std::map <std::string, int>* combos,

+ 2 - 2
src/WallpaperEngine/Render/Shaders/Compiler.h

@@ -51,7 +51,7 @@ namespace WallpaperEngine::Render::Shaders
          * @param recursive Whether the compiler should add base definitions or not
          */
         Compiler (
-            CContainer* container,
+            const CContainer* container,
             std::string filename,
             Type type,
             std::map<std::string, int>* combos,
@@ -259,7 +259,7 @@ namespace WallpaperEngine::Render::Shaders
          /**
           * The container to load files from
           */
-         CContainer* m_container;
+         const CContainer* m_container;
          /**
           * List of textures that the shader expects (inferred from sampler2D and it's JSON data)
           */