Congratulations on the job of creating a nice feature to load files recursively, it's something very useful, because OpenGL doesn't have the feature to include files.
I had a small problem when 2 files included the same file, causing it to generate repeated code, here is a more critical example.
Having these 3 files:
file1.txt
file2.txt
file3.txt
[file1.txt]
//some code
#include file2.txt
// add code
[file2.txt]
//some code
#include file3.txt
// add code
[file3.txt]
// add code
#include file2.txt
The code will load the files:
file1.txt
file2.txt
file3.txt
file2.txt
file3.txt
file2.txt
file3.txt
file2.txt
file3.txt
...
Until the moment it fills up memory or fails to load any of the files, as it is loading recursively and does not have the implementation to check if the file has already been included or not.
But it is possible to make it not add the same file more than once with the following modification:
...
class Shadinclude {
public:
// Return the source code of the complete shader
static std::string load(std::string path, std::string includeIndentifier = "#include") {
std::list<std::string> fileList;
// add the original path to the list to avoid reuse
fileList.push_back(path);
return load2(path, includeIndentifier + ' ', fileList);
}
private:
static std::string load2(std::string path, std::string includeIndentifier, std::list<std::string> &fileList) {
//includeIndentifier += ' ';
static bool isRecursiveCall = false;
...
auto it = std::find(fileList.begin(), fileList.end(), lineBuffer);
// test if the path is not on the list
if (fileList.end() == it) {
isRecursiveCall = true;
//add path to list
fileList.push_back(lineBuffer);
// By using recursion, the new include file can be extracted
// and inserted at this location in the shader source code
// if it has not already been inserted
fullSourceCode += load2(lineBuffer, includeIndentifier, fileList);
}
else {
// file already included, no need to include it again
}
...
Hope I helped improve something :)
Thanks.