Handle #stk_include in better way.

It allows to use it recursively.
This commit is contained in:
Deve 2017-03-21 18:41:18 +01:00
parent 8265bb14f3
commit 583b858860
2 changed files with 58 additions and 63 deletions

View File

@ -51,6 +51,62 @@ const std::string& ShaderFilesManager::getHeader()
return shader_header;
} // getHeader
// ----------------------------------------------------------------------------
void ShaderFilesManager::readFile(const std::string& file,
std::ostringstream& code)
{
std::ifstream stream(file_manager->getShader(file), std::ios::in);
if (!stream.is_open())
{
Log::error("ShaderFilesManager", "Can not open '%s'.", file.c_str());
return;
}
const std::string stk_include = "#stk_include";
std::string line;
while (std::getline(stream, line))
{
const std::size_t pos = line.find(stk_include);
// load the custom file pointed by the #stk_include directive
if (pos != std::string::npos)
{
// find the start "
std::size_t pos = line.find("\"");
if (pos == std::string::npos)
{
Log::error("ShaderFilesManager", "Invalid #stk_include"
" line: '%s'.", line.c_str());
continue;
}
std::string filename = line.substr(pos + 1);
// find the end "
pos = filename.find("\"");
if (pos == std::string::npos)
{
Log::error("ShaderFilesManager", "Invalid #stk_include"
" line: '%s'.", line.c_str());
continue;
}
filename = filename.substr(0, pos);
// read the whole include file
readFile(filename, code);
}
else
{
code << "\n" << line;
}
}
stream.close();
}
// ----------------------------------------------------------------------------
/** Loads a single shader. This is NOT cached, use addShaderFile for that.
* \param file Filename of the shader to load.
@ -126,69 +182,7 @@ GLuint ShaderFilesManager::loadShader(const std::string &file, unsigned type)
code << getHeader();
std::ifstream stream(file_manager->getShader(file), std::ios::in);
if (stream.is_open())
{
const std::string stk_include = "#stk_include";
std::string line;
while (std::getline(stream, line))
{
const std::size_t pos = line.find(stk_include);
// load the custom file pointed by the #stk_include directive
if (pos != std::string::npos)
{
// find the start "
std::size_t pos = line.find("\"");
if (pos == std::string::npos)
{
Log::error("ShaderFilesManager", "Invalid #stk_include"
" line: '%s'.", line.c_str());
continue;
}
std::string filename = line.substr(pos + 1);
// find the end "
pos = filename.find("\"");
if (pos == std::string::npos)
{
Log::error("ShaderFilesManager", "Invalid #stk_include"
" line: '%s'.", line.c_str());
continue;
}
filename = filename.substr(0, pos);
// read the whole include file
std::ifstream include_stream(file_manager->getShader(filename), std::ios::in);
if (!include_stream.is_open())
{
Log::error("ShaderFilesManager", "Couldn't open included"
" shader: '%s'.", filename.c_str());
continue;
}
std::string include_line = "";
while (std::getline(include_stream, include_line))
{
code << "\n" << include_line;
}
include_stream.close();
}
else
{
code << "\n" << line;
}
}
stream.close();
}
else
{
Log::error("ShaderFilesManager", "Can not open '%s'.", file.c_str());
}
readFile(file, code);
Log::info("ShaderFilesManager", "Compiling shader : %s", file.c_str());
const std::string &source = code.str();

View File

@ -38,6 +38,7 @@ private:
// ------------------------------------------------------------------------
const std::string& getHeader();
void readFile(const std::string& file, std::ostringstream& code);
public:
// ------------------------------------------------------------------------