From 194a025bc878aab48c157b735df29819b687d685 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 19 Feb 2011 21:09:20 +0000 Subject: [PATCH] Protect the Shader constructor and provide a static creation method that will let us cache shaders. --- src/shader.cpp | 12 +++++++++++- src/shader.h | 10 +++++++--- src/video.cpp | 4 ++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/shader.cpp b/src/shader.cpp index ec385604..93fc88f5 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -44,6 +44,16 @@ private: * Public Shader class */ +Shader *Shader::Create(char const *vert, char const *frag) +{ + return new Shader(vert, frag); +} + +void Shader::Destroy(Shader *shader) +{ + delete shader; +} + Shader::Shader(char const *vert, char const *frag) : data(new ShaderData()) { @@ -76,7 +86,7 @@ Shader::Shader(char const *vert, char const *frag) int Shader::GetAttribLocation(char const *attr) const { - return glGetAttribLocation(data->prog_id, attr); + return glGetAttribLocation(data->prog_id, attr); } int Shader::GetUniformLocation(char const *uni) const diff --git a/src/shader.h b/src/shader.h index 8189226c..670467a5 100644 --- a/src/shader.h +++ b/src/shader.h @@ -10,7 +10,7 @@ // // The Shader class -// -------------- +// ---------------- // #if !defined __DH_SHADER_H__ @@ -21,14 +21,18 @@ class ShaderData; class Shader { public: - Shader(char const *vert, char const *frag); - ~Shader(); + static Shader *Create(char const *vert, char const *frag); + static void Destroy(Shader *shader); int GetAttribLocation(char const *attr) const; int GetUniformLocation(char const *uni) const; void Bind() const; +protected: + Shader(char const *vert, char const *frag); + ~Shader(); + private: ShaderData *data; }; diff --git a/src/video.cpp b/src/video.cpp index 67c0bf48..156d0db6 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -85,7 +85,7 @@ void Video::Setup(int width, int height) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); #if LOL_EXPERIMENTAL - stdshader = new Shader(vertexshader, fragmentshader); + stdshader = Shader::Create(vertexshader, fragmentshader); #endif } @@ -168,7 +168,7 @@ void Video::Clear() void Video::Destroy() { #if LOL_EXPERIMENTAL - delete stdshader; + Shader::Destroy(stdshader); #endif }