diff --git a/main.cpp b/main.cpp index 18d18b1..5b44922 100644 --- a/main.cpp +++ b/main.cpp @@ -20,14 +20,12 @@ bool IsRootWindow = false; std::vector Screens; std::vector> 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 (DefaultRootWindow (display)); + irrlichtCreationParameters.WindowId = reinterpret_cast (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; + 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 (); + initialize_viewports (irrlichtCreationParameters); - IrrlichtContext->setDevice (irr::createDeviceEx (_irr_params)); + 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': diff --git a/src/WallpaperEngine/FileSystem/FileSystem.cpp b/src/WallpaperEngine/FileSystem/FileSystem.cpp index 7765c53..0ee94a6 100644 --- a/src/WallpaperEngine/FileSystem/FileSystem.cpp +++ b/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; - } -}; \ No newline at end of file + return content; +} diff --git a/src/WallpaperEngine/Irrlicht/CContext.cpp b/src/WallpaperEngine/Irrlicht/CContext.cpp index d296943..e3038cf 100644 --- a/src/WallpaperEngine/Irrlicht/CContext.cpp +++ b/src/WallpaperEngine/Irrlicht/CContext.cpp @@ -1,3 +1,4 @@ +#include #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 ()); } \ No newline at end of file diff --git a/src/WallpaperEngine/Irrlicht/CContext.h b/src/WallpaperEngine/Irrlicht/CContext.h index 4786d2a..1cb31fe 100644 --- a/src/WallpaperEngine/Irrlicht/CContext.h +++ b/src/WallpaperEngine/Irrlicht/CContext.h @@ -1,5 +1,7 @@ #pragma once +#include + #include 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; }; }; diff --git a/src/WallpaperEngine/Render/Objects/CImage.cpp b/src/WallpaperEngine/Render/Objects/CImage.cpp index 96618ce..4a25828 100644 --- a/src/WallpaperEngine/Render/Objects/CImage.cpp +++ b/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 ( diff --git a/src/WallpaperEngine/Render/Shaders/Compiler.cpp b/src/WallpaperEngine/Render/Shaders/Compiler.cpp index 1f6f3cd..be09043 100644 --- a/src/WallpaperEngine/Render/Shaders/Compiler.cpp +++ b/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& combos, bool recursive) : + Compiler::Compiler (Irrlicht::CContext* context, irr::io::path& file, Type type, const std::map& 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 (); } diff --git a/src/WallpaperEngine/Render/Shaders/Compiler.h b/src/WallpaperEngine/Render/Shaders/Compiler.h index 0c757d8..d9b4299 100644 --- a/src/WallpaperEngine/Render/Shaders/Compiler.h +++ b/src/WallpaperEngine/Render/Shaders/Compiler.h @@ -6,7 +6,10 @@ #include #include -#include +#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& combos, bool recursive = false); + Compiler (Irrlicht::CContext* context, irr::io::path& file, Type type, const std::map& 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; }; }