minor refactor

This commit is contained in:
MihailRis
2024-03-02 13:29:45 +03:00
parent 8001a8bf86
commit 9d9c9f5570
3 changed files with 66 additions and 63 deletions
+23 -25
View File
@@ -7,11 +7,9 @@
#include "../files/files.h"
#include "../files/engine_paths.h"
using std::string;
using std::filesystem::path;
namespace fs = std::filesystem;
void GLSLExtension::setVersion(string version) {
void GLSLExtension::setVersion(std::string version) {
this->version = version;
}
@@ -19,21 +17,21 @@ void GLSLExtension::setPaths(const ResPaths* paths) {
this->paths = paths;
}
void GLSLExtension::loadHeader(string name) {
path file = paths->find("shaders/lib/"+name+".glsl");
string source = files::read_string(file);
void GLSLExtension::loadHeader(std::string name) {
fs::path file = paths->find("shaders/lib/"+name+".glsl");
std::string source = files::read_string(file);
addHeader(name, source);
}
void GLSLExtension::addHeader(string name, string source) {
void GLSLExtension::addHeader(std::string name, std::string source) {
headers[name] = source;
}
void GLSLExtension::define(string name, string value) {
void GLSLExtension::define(std::string name, std::string value) {
defines[name] = value;
}
const string& GLSLExtension::getHeader(const string name) const {
const std::string& GLSLExtension::getHeader(const std::string& name) const {
auto found = headers.find(name);
if (found == headers.end()) {
throw std::runtime_error("no header '"+name+"' loaded");
@@ -41,7 +39,7 @@ const string& GLSLExtension::getHeader(const string name) const {
return found->second;
}
const string GLSLExtension::getDefine(const string name) const {
const std::string GLSLExtension::getDefine(const std::string& name) const {
auto found = defines.find(name);
if (found == defines.end()) {
return "";
@@ -49,41 +47,41 @@ const string GLSLExtension::getDefine(const string name) const {
return found->second;
}
bool GLSLExtension::hasDefine(const string name) const {
bool GLSLExtension::hasDefine(const std::string& name) const {
return defines.find(name) != defines.end();
}
bool GLSLExtension::hasHeader(const string name) const {
bool GLSLExtension::hasHeader(const std::string& name) const {
return headers.find(name) != headers.end();
}
void GLSLExtension::undefine(string name) {
void GLSLExtension::undefine(std::string name) {
if (hasDefine(name)) {
defines.erase(name);
}
}
inline std::runtime_error parsing_error(
const path& file,
const fs::path& file,
uint linenum,
const string message) {
const std::string message) {
return std::runtime_error("file "+file.string()+": "+message+
" at line "+std::to_string(linenum));
}
inline void parsing_warning(
const path& file,
const fs::path& file,
uint linenum, const
string message) {
std::string message) {
std::cerr << "file "+file.string()+": warning: "+message+
" at line "+std::to_string(linenum) << std::endl;
" at line "+std::to_string(linenum) << std::endl;
}
inline void source_line(std::stringstream& ss, uint linenum) {
ss << "#line " << linenum << "\n";
}
const string GLSLExtension::process(const path file, const string& source) {
const std::string GLSLExtension::process(const fs::path file, const std::string& source) {
std::stringstream ss;
size_t pos = 0;
uint linenum = 1;
@@ -94,15 +92,15 @@ const string GLSLExtension::process(const path file, const string& source) {
source_line(ss, linenum);
while (pos < source.length()) {
size_t endline = source.find('\n', pos);
if (endline == string::npos) {
if (endline == std::string::npos) {
endline = source.length();
}
// parsing preprocessor directives
if (source[pos] == '#') {
string line = source.substr(pos+1, endline-pos);
std::string line = source.substr(pos+1, endline-pos);
util::trim(line);
// parsing 'include' directive
if (line.find("include") != string::npos) {
if (line.find("include") != std::string::npos) {
line = line.substr(7);
util::trim(line);
if (line.length() < 3) {
@@ -113,7 +111,7 @@ const string GLSLExtension::process(const path file, const string& source) {
throw parsing_error(file, linenum,
"expected '#include <filename>' syntax");
}
string name = line.substr(1, line.length()-2);
std::string name = line.substr(1, line.length()-2);
if (!hasHeader(name)) {
loadHeader(name);
}
@@ -125,7 +123,7 @@ const string GLSLExtension::process(const path file, const string& source) {
continue;
}
// removing extra 'include' directives
else if (line.find("version") != string::npos) {
else if (line.find("version") != std::string::npos) {
parsing_warning(file, linenum, "removed #version directive");
pos = endline+1;
linenum++;
@@ -138,4 +136,4 @@ const string GLSLExtension::process(const path file, const string& source) {
pos = endline+1;
}
return ss.str();
}
}