Browse Source

~ moved file resolution to the Irrlicht Context class

Signed-off-by: Alexis Maiquez <almamu@almamu.com>
Alexis Maiquez 7 năm trước cách đây
mục cha
commit
ce67c0bf64

+ 23 - 24
main.cpp

@@ -20,14 +20,12 @@ bool IsRootWindow = false;
 std::vector<std::string> Screens;
 std::vector<irr::core::rect<irr::s32>> Viewports;
 
-irr::SIrrlichtCreationParameters _irr_params;
-
 irr::f32 g_Time = 0;
 
 WallpaperEngine::Irrlicht::CContext* IrrlichtContext;
-void initialize_viewports ()
+void initialize_viewports (irr::SIrrlichtCreationParameters& irrlichtCreationParameters)
 {
-    if (IsRootWindow == false || Screens.size () == 0)
+    if (IsRootWindow == false || Screens.empty () == true)
         return;
 
     Display* display = XOpenDisplay (NULL);
@@ -82,33 +80,34 @@ void initialize_viewports ()
 
     XRRFreeScreenResources (screenResources);
 
-    _irr_params.WindowId = reinterpret_cast<void*> (DefaultRootWindow (display));
+    irrlichtCreationParameters.WindowId = reinterpret_cast<void*> (DefaultRootWindow (display));
 }
 
 int init_irrlicht()
 {
     IrrlichtContext = new WallpaperEngine::Irrlicht::CContext ();
 
+    irr::SIrrlichtCreationParameters irrlichtCreationParameters;
     // prepare basic configuration for irrlicht
-    _irr_params.AntiAlias = 8;
-    _irr_params.Bits = 16;
+    irrlichtCreationParameters.AntiAlias = 8;
+    irrlichtCreationParameters.Bits = 16;
     // _irr_params.DeviceType = Irrlicht::EIDT_X11;
-    _irr_params.DriverType = irr::video::EDT_OPENGL;
-    _irr_params.Doublebuffer = false;
-    _irr_params.EventReceiver = nullptr;
-    _irr_params.Fullscreen = false;
-    _irr_params.HandleSRGB = false;
-    _irr_params.IgnoreInput = true;
-    _irr_params.Stencilbuffer = true;
-    _irr_params.UsePerformanceTimer = false;
-    _irr_params.Vsync = false;
-    _irr_params.WithAlphaChannel = false;
-    _irr_params.ZBufferBits = 24;
-    _irr_params.LoggingLevel = irr::ELL_DEBUG;
-
-    initialize_viewports ();
-
-    IrrlichtContext->setDevice (irr::createDeviceEx (_irr_params));
+    irrlichtCreationParameters.DriverType = irr::video::EDT_OPENGL;
+    irrlichtCreationParameters.Doublebuffer = false;
+    irrlichtCreationParameters.EventReceiver = nullptr;
+    irrlichtCreationParameters.Fullscreen = false;
+    irrlichtCreationParameters.HandleSRGB = false;
+    irrlichtCreationParameters.IgnoreInput = true;
+    irrlichtCreationParameters.Stencilbuffer = true;
+    irrlichtCreationParameters.UsePerformanceTimer = false;
+    irrlichtCreationParameters.Vsync = false;
+    irrlichtCreationParameters.WithAlphaChannel = false;
+    irrlichtCreationParameters.ZBufferBits = 24;
+    irrlichtCreationParameters.LoggingLevel = irr::ELL_DEBUG;
+
+    initialize_viewports (irrlichtCreationParameters);
+
+    IrrlichtContext->setDevice (irr::createDeviceEx (irrlichtCreationParameters));
 
     if (IrrlichtContext->getDevice () == nullptr)
     {
@@ -210,7 +209,7 @@ int main (int argc, char* argv[])
         {
             case 'r':
                 IsRootWindow = true;
-                Screens.push_back (optarg);
+                Screens.emplace_back (optarg);
                 break;
 
             case 'p':

+ 14 - 15
src/WallpaperEngine/FileSystem/FileSystem.cpp

@@ -6,24 +6,23 @@
 
 extern WallpaperEngine::Irrlicht::CContext* IrrlichtContext;
 
-namespace WallpaperEngine::FileSystem
+using namespace WallpaperEngine;
+
+std::string FileSystem::loadFullFile (const irr::io::path& file)
 {
-    std::string loadFullFile (const irr::io::path& file)
-    {
-        irr::io::IReadFile* reader = IrrlichtContext->getDevice ()->getFileSystem ()->createAndOpenFile (file);
+    irr::io::IReadFile* reader = IrrlichtContext->getDevice ()->getFileSystem ()->createAndOpenFile (file);
 
-        if (reader == NULL)
-            throw std::runtime_error ("Cannot open file " + std::string (file.c_str ()) + " for reading");
+    if (reader == nullptr)
+        throw std::runtime_error ("Cannot open file " + std::string (file.c_str ()) + " for reading");
 
-        char* filedata = new char [reader->getSize () + 1];
-        memset (filedata, 0, reader->getSize () + 1);
+    char* filedata = new char [reader->getSize () + 1];
+    memset (filedata, 0, reader->getSize () + 1);
 
-        reader->read (filedata, reader->getSize ());
-        reader->drop ();
+    reader->read (filedata, reader->getSize ());
+    reader->drop ();
 
-        std::string content = filedata;
-        delete [] filedata;
+    std::string content = filedata;
+    delete [] filedata;
 
-        return content;
-    }
-};
+    return content;
+}

+ 30 - 0
src/WallpaperEngine/Irrlicht/CContext.cpp

@@ -1,3 +1,4 @@
+#include <stdexcept>
 #include "CContext.h"
 
 using namespace WallpaperEngine::Irrlicht;
@@ -10,4 +11,33 @@ void CContext::setDevice (irr::IrrlichtDevice* device)
 irr::IrrlichtDevice* CContext::getDevice ()
 {
     return this->m_device;
+}
+
+irr::io::path CContext::resolveFile (const irr::io::path& file)
+{
+    if (this->getDevice ()->getFileSystem ()->existFile (file) == false)
+    {
+        throw std::runtime_error ("Cannot find file " + std::string (file.c_str ()));
+    }
+
+    return file;
+}
+
+irr::io::path CContext::resolveMaterials (const std::string& materialName)
+{
+    return this->resolveFile (std::string ("materials/" + materialName + ".tex").c_str ());
+}
+
+irr::io::path CContext::resolveVertexShader (const std::string& vertexShader)
+{
+    return this->resolveFile (std::string ("shaders/" + vertexShader + ".vert").c_str ());
+}
+irr::io::path CContext::resolveFragmentShader (const std::string& fragmentShader)
+{
+    return this->resolveFile (std::string ("shaders/" + fragmentShader + ".frag").c_str ());
+}
+
+irr::io::path CContext::resolveIncludeShader (const std::string& includeShader)
+{
+    return this->resolveFile (std::string ("shaders/" + includeShader).c_str ());
 }

+ 8 - 0
src/WallpaperEngine/Irrlicht/CContext.h

@@ -1,5 +1,7 @@
 #pragma once
 
+#include <string>
+
 #include <irrlicht/irrlicht.h>
 
 namespace WallpaperEngine::Irrlicht
@@ -10,7 +12,13 @@ namespace WallpaperEngine::Irrlicht
         void setDevice (irr::IrrlichtDevice* device);
 
         irr::IrrlichtDevice* getDevice ();
+        irr::io::path resolveMaterials (const std::string& materialName);
+        irr::io::path resolveVertexShader (const std::string& vertexShader);
+        irr::io::path resolveFragmentShader (const std::string& fragmentShader);
+        irr::io::path resolveIncludeShader (const std::string& includeShader);
     private:
+        irr::io::path resolveFile (const irr::io::path& file);
+
         irr::IrrlichtDevice* m_device;
     };
 };

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

@@ -127,7 +127,7 @@ void CImage::generatePass (Core::Objects::Images::Materials::CPassess* pass)
     for (int textureNumber = 0; texturesCur != texturesEnd; texturesCur ++, textureNumber ++)
     {
         // TODO: LOOK THIS UP PROPERLY
-        irr::io::path texturepath = std::string ("materials/" + (*texturesCur) + ".tex").c_str ();
+        irr::io::path texturepath = this->getScene ()->getContext ()->resolveMaterials (*texturesCur);
         irr::video::ITexture* texture = nullptr;
 
         if (textureNumber == 0 && this->m_passes > 0)
@@ -152,10 +152,11 @@ void CImage::generatePass (Core::Objects::Images::Materials::CPassess* pass)
     }
 
     // TODO: MOVE SHADER INITIALIZATION ELSEWHERE
-    irr::io::path vertpath = std::string ("shaders/" + pass->getShader () + ".vert").c_str ();
-    irr::io::path fragpath = std::string ("shaders/" + pass->getShader () + ".frag").c_str ();
-    Render::Shaders::Compiler* vertexShader = new Render::Shaders::Compiler (vertpath, Render::Shaders::Compiler::Type::Type_Vertex, pass->getCombos (), false);
-    Render::Shaders::Compiler* pixelShader = new Render::Shaders::Compiler (fragpath, Render::Shaders::Compiler::Type::Type_Pixel, pass->getCombos (), false);
+    irr::io::path vertpath = this->getScene ()->getContext ()->resolveVertexShader (pass->getShader ());
+    irr::io::path fragpath = this->getScene ()->getContext ()->resolveFragmentShader (pass->getShader ());
+
+    auto vertexShader = new Render::Shaders::Compiler (this->getScene ()->getContext (), vertpath, Render::Shaders::Compiler::Type::Type_Vertex, pass->getCombos (), false);
+    auto pixelShader = new Render::Shaders::Compiler (this->getScene ()->getContext (), fragpath, Render::Shaders::Compiler::Type::Type_Pixel, pass->getCombos (), false);
 
     material.MaterialType = (irr::video::E_MATERIAL_TYPE)
         this->getScene ()->getContext ()->getDevice ()->getVideoDriver ()->getGPUProgrammingServices ()->addHighLevelShaderMaterial (

+ 5 - 4
src/WallpaperEngine/Render/Shaders/Compiler.cpp

@@ -19,13 +19,14 @@
 
 namespace WallpaperEngine::Render::Shaders
 {
-    Compiler::Compiler (irr::io::path& file, Type type, const std::map<std::string, int>& combos, bool recursive) :
+    Compiler::Compiler (Irrlicht::CContext* context, irr::io::path& file, Type type, const std::map<std::string, int>& combos, bool recursive) :
         m_combos (combos),
         m_recursive (recursive),
         m_type (type),
         m_file (file),
         m_error (""),
-        m_errorInfo ("")
+        m_errorInfo (""),
+        m_context (context)
     {
         // begin with an space so it gets ignored properly on parse
         if (this->m_recursive == false)
@@ -207,7 +208,7 @@ namespace WallpaperEngine::Render::Shaders
     std::string Compiler::lookupShaderFile (std::string filename)
     {
         // get file information
-        irr::io::path shader = ("shaders/" + filename).c_str ();
+        irr::io::path shader = this->m_context->resolveIncludeShader (filename);
 
         if (shader == "")
         {
@@ -218,7 +219,7 @@ namespace WallpaperEngine::Render::Shaders
 
         // now compile the new shader
         // do not include the default header (as it's already included in the parent)
-        Compiler loader (shader, this->m_type, this->m_combos, true);
+        Compiler loader (this->m_context, shader, this->m_type, this->m_combos, true);
 
         return loader.precompile ();
     }

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

@@ -6,7 +6,10 @@
 #include <map>
 #include <nlohmann/json.hpp>
 
-#include <WallpaperEngine/FileSystem/FileSystem.h>
+#include "WallpaperEngine/FileSystem/FileSystem.h"
+
+#include "WallpaperEngine/Irrlicht/CContext.h"
+
 #include "WallpaperEngine/Render/Shaders/Parameters/CShaderParameter.h"
 
 namespace WallpaperEngine::Render::Shaders
@@ -42,11 +45,13 @@ namespace WallpaperEngine::Render::Shaders
          * the pre-processing and compilation of the shader, adding
          * required definitions if needed
          *
+         * @param context The irrlicht context
          * @param file The file to load
          * @param type The type of shader
+         * @param combos Settings for the shader
          * @param recursive Whether the compiler should add base definitions or not
          */
-        Compiler (irr::io::path& file, Type type, const std::map<std::string, int>& combos, bool recursive = false);
+        Compiler (Irrlicht::CContext* context, irr::io::path& file, Type type, const std::map<std::string, int>& combos, bool recursive = false);
         /**
          * Performs the actual pre-compilation/pre-processing over the shader files
          * This step is kinda big, replaces variables names on sVariableReplacement,
@@ -211,5 +216,9 @@ namespace WallpaperEngine::Render::Shaders
           * Whether this compilation is a recursive one or not
           */
          bool m_recursive;
+         /**
+          * The irrlicht context in use
+          */
+         Irrlicht::CContext* m_context;
     };
 }