namespace Msp {
namespace GL {
-Program::Program():
- del_shaders(false)
+Program::Program()
{
init();
}
-Program::Program(const StandardFeatures &features):
- del_shaders(true)
+Program::Program(const StandardFeatures &features)
{
init();
link();
}
-Program::Program(const string &vert, const string &frag):
- del_shaders(true)
+Program::Program(const string &vert, const string &frag)
{
init();
- attach_shader(*new Shader(VERTEX_SHADER, vert));
- attach_shader(*new Shader(FRAGMENT_SHADER, frag));
+ attach_shader_owned(new Shader(VERTEX_SHADER, vert));
+ attach_shader_owned(new Shader(FRAGMENT_SHADER, frag));
link();
}
Program::~Program()
{
- if(del_shaders)
- {
- for(list<Shader *>::iterator i=shaders.begin(); i!=shaders.end(); ++i)
- delete *i;
- }
+ for(list<Shader *>::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
+ delete *i;
glDeleteObjectARB(id);
}
}
}
+void Program::attach_shader_owned(Shader *shader)
+{
+ attach_shader(*shader);
+ if(find(owned_data.begin(), owned_data.end(), shader)==owned_data.end())
+ owned_data.push_back(shader);
+}
+
void Program::detach_shader(Shader &shader)
{
list<Shader *>::iterator i = remove(shaders.begin(), shaders.end(), &shader);
string flags = features.create_flags();
string vertex_src = process_standard_source(standard_vertex_src, flags);
string fragment_src = process_standard_source(standard_fragment_src, flags);
- attach_shader(*new Shader(VERTEX_SHADER, vertex_src));
- attach_shader(*new Shader(FRAGMENT_SHADER, fragment_src));
+ attach_shader_owned(new Shader(VERTEX_SHADER, vertex_src));
+ attach_shader_owned(new Shader(FRAGMENT_SHADER, fragment_src));
}
string Program::process_standard_source(const char **source, const string &flags)
return result;
}
-void Program::set_del_shaders(bool ds)
-{
- del_shaders = ds;
-}
-
void Program::bind_attribute(unsigned index, const string &name)
{
static RequireExtension _ext("GL_ARB_vertex_shader");
Program::Loader::Loader(Program &p):
DataFile::ObjectLoader<Program>(p)
{
- obj.set_del_shaders(true);
-
add("attribute", &Loader::attribute);
add("fragment_shader", &Loader::fragment_shader);
add("standard", &Loader::standard);
void Program::Loader::fragment_shader(const string &src)
{
- obj.attach_shader(*new Shader(FRAGMENT_SHADER, src));
+ obj.attach_shader_owned(new Shader(FRAGMENT_SHADER, src));
}
void Program::Loader::standard()
void Program::Loader::vertex_shader(const string &src)
{
- obj.attach_shader(*new Shader(VERTEX_SHADER, src));
+ obj.attach_shader_owned(new Shader(VERTEX_SHADER, src));
}
private:
unsigned id;
std::list<Shader *> shaders;
- bool del_shaders;
+ std::list<Shader *> owned_data;
bool linked;
std::map<std::string, UniformInfo> uniforms;
virtual ~Program();
void attach_shader(Shader &shader);
+ void attach_shader_owned(Shader *shader);
void detach_shader(Shader &shader);
void add_standard_shaders(const StandardFeatures &);
private:
static std::string process_standard_source(const char **, const std::string &);
public:
const std::list<Shader *> &get_shaders() const { return shaders; }
- void set_del_shaders(bool);
void bind_attribute(unsigned, const std::string &);
void link();
bool is_linked() const { return linked; }