diff --git a/Sources/Draw/GLProgram.cpp b/Sources/Draw/GLProgram.cpp index 33460bdb..7ff8d51f 100644 --- a/Sources/Draw/GLProgram.cpp +++ b/Sources/Draw/GLProgram.cpp @@ -37,9 +37,9 @@ namespace spades { device->DeleteProgram(handle); } - void GLProgram::Attach(spades::draw::GLShader *shader) { + void GLProgram::Attach(GLShader &shader) { SPADES_MARK_FUNCTION(); - device->AttachShader(handle, shader->GetHandle()); + this->Attach(shader.GetHandle()); } void GLProgram::Attach(IGLDevice::UInteger shader) { diff --git a/Sources/Draw/GLProgram.h b/Sources/Draw/GLProgram.h index bfcb047a..ae666064 100644 --- a/Sources/Draw/GLProgram.h +++ b/Sources/Draw/GLProgram.h @@ -36,7 +36,7 @@ namespace spades { GLProgram(IGLDevice *, std::string name = "(unnamed)"); ~GLProgram(); - void Attach(GLShader *); + void Attach(GLShader &); void Attach(IGLDevice::UInteger shader); void Link(); diff --git a/Sources/Draw/GLProgramManager.cpp b/Sources/Draw/GLProgramManager.cpp index 046f6fe6..5c40f703 100644 --- a/Sources/Draw/GLProgramManager.cpp +++ b/Sources/Draw/GLProgramManager.cpp @@ -31,6 +31,7 @@ #include #include #include +#include namespace spades { namespace draw { @@ -40,97 +41,90 @@ namespace spades { SPADES_MARK_FUNCTION(); } - GLProgramManager::~GLProgramManager() { - SPADES_MARK_FUNCTION(); - - for (std::map::iterator it = programs.begin(); - it != programs.end(); it++) { - delete it->second; - } - for (std::map::iterator it = shaders.begin(); - it != shaders.end(); it++) { - delete it->second; - } - } + GLProgramManager::~GLProgramManager() { SPADES_MARK_FUNCTION(); } GLProgram *GLProgramManager::RegisterProgram(const std::string &name) { SPADES_MARK_FUNCTION(); - std::map::iterator it; - it = programs.find(name); + auto it = programs.find(name); if (it == programs.end()) { - GLProgram *prog = CreateProgram(name); - programs[name] = prog; - return prog; + auto program = CreateProgram(name); + GLProgram *programPtr = program.get(); + + programs[name] = std::move(program); + + return programPtr; } else { - return it->second; + return it->second.get(); } } GLShader *GLProgramManager::RegisterShader(const std::string &name) { SPADES_MARK_FUNCTION(); - std::map::iterator it; - it = shaders.find(name); + auto it = shaders.find(name); if (it == shaders.end()) { - GLShader *prog = CreateShader(name); - shaders[name] = prog; - return prog; + auto shader = CreateShader(name); + GLShader *shaderPtr = shader.get(); + + shaders[name] = std::move(shader); + + return shaderPtr; } else { - return it->second; + return it->second.get(); } } - GLProgram *GLProgramManager::CreateProgram(const std::string &name) { + std::unique_ptr GLProgramManager::CreateProgram(const std::string &name) { SPADES_MARK_FUNCTION(); SPLog("Loading GLSL program '%s'", name.c_str()); std::string text = FileManager::ReadAllBytes(name.c_str()); std::vector lines = SplitIntoLines(text); - GLProgram *p = new GLProgram(&device, name); + auto p = stmp::make_unique(&device, name); for (size_t i = 0; i < lines.size(); i++) { std::string text = TrimSpaces(lines[i]); if (text.empty()) - break; + break; // TODO: Probably wrong, but unconfirmed if (text == "*shadow*") { std::vector shaders = GLShadowShader::RegisterShader(this, settings, false); - for (size_t i = 0; i < shaders.size(); i++) - p->Attach(shaders[i]); - continue; + for (GLShader *shader : shaders) { + p->Attach(*shader); + } } else if (text == "*shadow-lite*") { std::vector shaders = GLShadowShader::RegisterShader(this, settings, false, true); - for (size_t i = 0; i < shaders.size(); i++) - p->Attach(shaders[i]); - continue; + for (GLShader *shader : shaders) { + p->Attach(*shader); + } } else if (text == "*shadow-variance*") { std::vector shaders = GLShadowShader::RegisterShader(this, settings, true); - for (size_t i = 0; i < shaders.size(); i++) - p->Attach(shaders[i]); - continue; + for (GLShader *shader : shaders) { + p->Attach(*shader); + } } else if (text == "*dlight*") { std::vector shaders = GLDynamicLightShader::RegisterShader(this); - for (size_t i = 0; i < shaders.size(); i++) - p->Attach(shaders[i]); - continue; + for (GLShader *shader : shaders) { + p->Attach(*shader); + } } else if (text == "*shadowmap*") { std::vector shaders = GLShadowMapShader::RegisterShader(this); - for (size_t i = 0; i < shaders.size(); i++) - p->Attach(shaders[i]); - continue; + for (GLShader *shader : shaders) { + p->Attach(*shader); + } } else if (text[0] == '*') { SPRaise("Unknown special shader: %s", text.c_str()); } else if (text[0] == '#') { - continue; + // Comment line + } else { + auto shader = CreateShader(text); + p->Attach(*shader); } - GLShader *s = CreateShader(text); - - p->Attach(s); } Stopwatch sw; @@ -141,7 +135,7 @@ namespace spades { return p; } - GLShader *GLProgramManager::CreateShader(const std::string &name) { + std::unique_ptr GLProgramManager::CreateShader(const std::string &name) { SPADES_MARK_FUNCTION(); SPLog("Loading GLSL shader '%s'", name.c_str()); @@ -157,7 +151,7 @@ namespace spades { else SPRaise("Failed to determine the type of a shader: %s", name.c_str()); - GLShader *s = new GLShader(device, type); + auto s = stmp::make_unique(device, type); std::string finalSource; diff --git a/Sources/Draw/GLProgramManager.h b/Sources/Draw/GLProgramManager.h index d0501c84..0a28a6cc 100644 --- a/Sources/Draw/GLProgramManager.h +++ b/Sources/Draw/GLProgramManager.h @@ -20,6 +20,7 @@ #pragma once #include +#include #include namespace spades { @@ -34,11 +35,11 @@ namespace spades { GLSettings &settings; IGLShadowMapRenderer *shadowMapRenderer; - std::map programs; - std::map shaders; + std::map> programs; + std::map> shaders; - GLProgram *CreateProgram(const std::string &name); - GLShader *CreateShader(const std::string &name); + std::unique_ptr CreateProgram(const std::string &name); + std::unique_ptr CreateShader(const std::string &name); public: GLProgramManager(IGLDevice &, IGLShadowMapRenderer *shadowMapRenderer,