From da34a2b33e1f600ec11172f599384b9a92835403 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Tue, 19 May 2015 02:24:14 -0400 Subject: Replace instances of std::map with StringMap Also, clean up surrounding code style Replace by-value parameter passing with const refs when possible Fix post-increment of iterators --- src/shader.cpp | 61 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 31 deletions(-) (limited to 'src/shader.cpp') diff --git a/src/shader.cpp b/src/shader.cpp index ea2de3f1e..a467c2ea9 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -103,10 +103,8 @@ std::string getShaderPath(const std::string &name_of_shader, class SourceShaderCache { public: - void insert(const std::string &name_of_shader, - const std::string &filename, - const std::string &program, - bool prefer_local) + void insert(const std::string &name_of_shader, const std::string &filename, + const std::string &program, bool prefer_local) { std::string combined = name_of_shader + DIR_DELIM + filename; // Try to use local shader instead if asked to @@ -122,42 +120,43 @@ public: } m_programs[combined] = program; } + std::string get(const std::string &name_of_shader, - const std::string &filename) + const std::string &filename) { std::string combined = name_of_shader + DIR_DELIM + filename; - std::map::iterator n; - n = m_programs.find(combined); - if(n != m_programs.end()) + StringMap::iterator n = m_programs.find(combined); + if (n != m_programs.end()) return n->second; return ""; } + // Primarily fetches from cache, secondarily tries to read from filesystem std::string getOrLoad(const std::string &name_of_shader, - const std::string &filename) + const std::string &filename) { std::string combined = name_of_shader + DIR_DELIM + filename; - std::map::iterator n; - n = m_programs.find(combined); - if(n != m_programs.end()) + StringMap::iterator n = m_programs.find(combined); + if (n != m_programs.end()) return n->second; std::string path = getShaderPath(name_of_shader, filename); - if(path == ""){ - infostream<<"SourceShaderCache::getOrLoad(): No path found for \"" - < m_programs; + StringMap m_programs; + std::string readFile(const std::string &path) { std::ifstream is(path.c_str(), std::ios::binary); @@ -274,23 +273,23 @@ public: The id 0 points to a null shader. Its material is EMT_SOLID. */ - u32 getShaderIdDirect(const std::string &name, + u32 getShaderIdDirect(const std::string &name, const u8 material_type, const u8 drawtype); /* If shader specified by the name pointed by the id doesn't - exist, create it, then return id. + exist, create it, then return id. Can be called from any thread. If called from some other thread and not found in cache, the call is queued to the main thread for processing. */ - + u32 getShader(const std::string &name, const u8 material_type, const u8 drawtype); - + ShaderInfo getShaderInfo(u32 id); - + // Processes queued shader requests from other threads. // Shall be called from the main thread. void processQueue(); @@ -391,7 +390,7 @@ ShaderSource::~ShaderSource() } } -u32 ShaderSource::getShader(const std::string &name, +u32 ShaderSource::getShader(const std::string &name, const u8 material_type, const u8 drawtype) { /* @@ -435,7 +434,7 @@ u32 ShaderSource::getShader(const std::string &name, /* This method generates all the shaders */ -u32 ShaderSource::getShaderIdDirect(const std::string &name, +u32 ShaderSource::getShaderIdDirect(const std::string &name, const u8 material_type, const u8 drawtype) { //infostream<<"getShaderIdDirect(): name=\""<getBool("enable_shaders"); if(!enable_shaders) return shaderinfo; @@ -648,7 +647,7 @@ ShaderInfo generate_shader(std::string name, u8 material_type, u8 drawtype, "NDT_FIRELIKE", "NDT_GLASSLIKE_FRAMED_OPTIONAL" }; - + for (int i = 0; i < 14; i++){ shaders_header += "#define "; shaders_header += drawTypes[i]; @@ -741,10 +740,10 @@ ShaderInfo generate_shader(std::string name, u8 material_type, u8 drawtype, shaders_header += "#define ENABLE_WAVING_LEAVES "; if (g_settings->getBool("enable_waving_leaves")) shaders_header += "1\n"; - else + else shaders_header += "0\n"; - shaders_header += "#define ENABLE_WAVING_PLANTS "; + shaders_header += "#define ENABLE_WAVING_PLANTS "; if (g_settings->getBool("enable_waving_plants")) shaders_header += "1\n"; else -- cgit v1.2.3