Commit 177f61b8 authored by Lars Krecklau's avatar Lars Krecklau

Started to constify everything. The FileManager now returns a...

Started to constify everything. The FileManager now returns a ConstSharedResource to prevent changes.
parent 21585e94
...@@ -13,7 +13,8 @@ private:\ ...@@ -13,7 +13,8 @@ private:\
void operator=(Class& other){ } void operator=(Class& other){ }
#define ACGL_SHARED_TYPEDEF(Class) \ #define ACGL_SHARED_TYPEDEF(Class) \
typedef std::tr1::shared_ptr<Class> Shared ## Class; typedef std::tr1::shared_ptr<Class> Shared ## Class; \
typedef std::tr1::shared_ptr<const Class> ConstShared ## Class;
#endif // MACROS_HH #endif // MACROS_HH
...@@ -72,7 +72,7 @@ public: ...@@ -72,7 +72,7 @@ public:
// ============================================================================================ GETTERS \/ // ============================================================================================ GETTERS \/
// ==================================================================================================== \/ // ==================================================================================================== \/
public: public:
inline GLuint getContext (void) { return mContext; } inline GLuint getContext (void) const { return mContext; }
inline GLenum getUsage (void) const { return mUsage; } inline GLenum getUsage (void) const { return mUsage; }
inline GLenum getMode (void) const { return mMode; } inline GLenum getMode (void) const { return mMode; }
inline GLsizei getElements (void) const { return mElements; } inline GLsizei getElements (void) const { return mElements; }
......
...@@ -53,7 +53,7 @@ public: ...@@ -53,7 +53,7 @@ public:
// ============================================================================================ GETTERS \/ // ============================================================================================ GETTERS \/
// ==================================================================================================== \/ // ==================================================================================================== \/
public: public:
inline GLuint getContext (void) { return mContext; } inline GLuint getContext (void) const { return mContext; }
inline GLenum getUsage (void) const { return mUsage; } inline GLenum getUsage (void) const { return mUsage; }
inline GLenum getMode (void) const { return mMode; } inline GLenum getMode (void) const { return mMode; }
inline GLsizei getElements (void) const { return mElements; } inline GLsizei getElements (void) const { return mElements; }
......
...@@ -28,7 +28,7 @@ class ShaderProgram ...@@ -28,7 +28,7 @@ class ShaderProgram
// ============================================================================================ TYPEDEFS \/ // ============================================================================================ TYPEDEFS \/
// ===================================================================================================== \/ // ===================================================================================================== \/
public: public:
typedef std::vector< SharedShader > SharedShaderVec; typedef std::vector< ConstSharedShader > ConstSharedShaderVec;
// ========================================================================================================= \/ // ========================================================================================================= \/
// ============================================================================================ CONSTRUCTORS \/ // ============================================================================================ CONSTRUCTORS \/
...@@ -43,15 +43,6 @@ public: ...@@ -43,15 +43,6 @@ public:
virtual ~ShaderProgram(void) virtual ~ShaderProgram(void)
{ {
for(SharedShaderVec::size_type i = 0; i < mShaders.size(); ++i) {
if(mShaders[i]) {
// we have to detach all shaders, otherwise the attached shaders will get deleted by
// OpenGL: "When a program object is deleted, all shader objects attached to it are detached"
// - GL Spec
glDetachShader(mContext, mShaders[i]->getContext());
}
}
// "DeleteProgram will silently ignore the value zero." - GL Spec // "DeleteProgram will silently ignore the value zero." - GL Spec
glDeleteProgram(mContext); glDeleteProgram(mContext);
} }
...@@ -60,8 +51,8 @@ public: ...@@ -60,8 +51,8 @@ public:
// ============================================================================================ GETTERS \/ // ============================================================================================ GETTERS \/
// ==================================================================================================== \/ // ==================================================================================================== \/
public: public:
inline GLuint getContext (void) const { return mContext; } inline GLuint getContext (void) const { return mContext; }
inline const SharedShaderVec& getShaders (void) const { return mShaders; } inline const ConstSharedShaderVec& getShaders (void) const { return mShaders; }
// ===================================================================================================== \/ // ===================================================================================================== \/
// ============================================================================================ WRAPPERS \/ // ============================================================================================ WRAPPERS \/
...@@ -70,7 +61,7 @@ public: ...@@ -70,7 +61,7 @@ public:
inline GLint getUniformLocation (const std::string& _nameInShader) const { return glGetUniformLocation(mContext, _nameInShader.c_str()); } inline GLint getUniformLocation (const std::string& _nameInShader) const { return glGetUniformLocation(mContext, _nameInShader.c_str()); }
inline GLint getAttribLocation (const std::string& _nameInShader) const { return glGetAttribLocation (mContext, _nameInShader.c_str()); } inline GLint getAttribLocation (const std::string& _nameInShader) const { return glGetAttribLocation (mContext, _nameInShader.c_str()); }
inline void attachShader(const SharedShader& _shader) inline void attachShader(const ConstSharedShader& _shader)
{ {
mShaders.push_back(_shader); mShaders.push_back(_shader);
glAttachShader(mContext, _shader->getContext()); glAttachShader(mContext, _shader->getContext());
...@@ -89,7 +80,7 @@ public: ...@@ -89,7 +80,7 @@ public:
inline void setUniform (GLint _location, const glm::mat3& _v, GLboolean _transpose = GL_FALSE) const { glUniformMatrix3fv(_location, 1, _transpose, glm::value_ptr(_v)); } inline void setUniform (GLint _location, const glm::mat3& _v, GLboolean _transpose = GL_FALSE) const { glUniformMatrix3fv(_location, 1, _transpose, glm::value_ptr(_v)); }
inline void setUniform (GLint _location, const glm::mat4& _v, GLboolean _transpose = GL_FALSE) const { glUniformMatrix4fv(_location, 1, _transpose, glm::value_ptr(_v)); } inline void setUniform (GLint _location, const glm::mat4& _v, GLboolean _transpose = GL_FALSE) const { glUniformMatrix4fv(_location, 1, _transpose, glm::value_ptr(_v)); }
inline void setTexture (GLint _location, const SharedTexture& _texture, GLenum _unit = 0) const { glUniform1i(_location, _unit); _texture->bind(_unit); } inline void setTexture (GLint _location, const ConstSharedTexture& _texture, GLenum _unit = 0) const { glUniform1i(_location, _unit); _texture->bind(_unit); }
inline void use(void) const inline void use(void) const
{ {
...@@ -113,7 +104,7 @@ public: ...@@ -113,7 +104,7 @@ public:
inline void setUniform (const std::string& _nameInShader, const glm::mat3& _v, GLboolean _transpose = GL_FALSE) const { glUniformMatrix3fv(getUniformLocation(_nameInShader), 1, _transpose, glm::value_ptr(_v)); } inline void setUniform (const std::string& _nameInShader, const glm::mat3& _v, GLboolean _transpose = GL_FALSE) const { glUniformMatrix3fv(getUniformLocation(_nameInShader), 1, _transpose, glm::value_ptr(_v)); }
inline void setUniform (const std::string& _nameInShader, const glm::mat4& _v, GLboolean _transpose = GL_FALSE) const { glUniformMatrix4fv(getUniformLocation(_nameInShader), 1, _transpose, glm::value_ptr(_v)); } inline void setUniform (const std::string& _nameInShader, const glm::mat4& _v, GLboolean _transpose = GL_FALSE) const { glUniformMatrix4fv(getUniformLocation(_nameInShader), 1, _transpose, glm::value_ptr(_v)); }
inline void setTexture (const std::string& _nameInShader, const SharedTexture& _texture, GLenum _unit = 0) const { glUniform1i(getUniformLocation(_nameInShader), _unit); _texture->bind(_unit); } inline void setTexture (const std::string& _nameInShader, const ConstSharedTexture& _texture, GLenum _unit = 0) const { glUniform1i(getUniformLocation(_nameInShader), _unit); _texture->bind(_unit); }
// =================================================================================================== \/ // =================================================================================================== \/
// ============================================================================================ FIELDS \/ // ============================================================================================ FIELDS \/
...@@ -121,8 +112,8 @@ public: ...@@ -121,8 +112,8 @@ public:
protected: protected:
void bindAttributeLocations() const; void bindAttributeLocations() const;
GLuint mContext; GLuint mContext;
SharedShaderVec mShaders; ConstSharedShaderVec mShaders;
}; };
ACGL_SHARED_TYPEDEF(ShaderProgram) ACGL_SHARED_TYPEDEF(ShaderProgram)
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <ACGL/ACGL.hh> #include <ACGL/ACGL.hh>
#include <ACGL/Base/Singleton.hh> #include <ACGL/Base/Singleton.hh>
#include <ACGL/Base/Macros.hh>
#include <ACGL/Resource/FileController.hh> #include <ACGL/Resource/FileController.hh>
#include <tr1/memory> #include <tr1/memory>
...@@ -24,11 +25,11 @@ class FileManager : public Base::Singleton< FileManager<RESOURCE> > ...@@ -24,11 +25,11 @@ class FileManager : public Base::Singleton< FileManager<RESOURCE> >
public: public:
typedef std::tr1::shared_ptr< FileController<RESOURCE> > SharedController; typedef std::tr1::shared_ptr< FileController<RESOURCE> > SharedController;
typedef std::tr1::shared_ptr<RESOURCE> SharedResource; ACGL_SHARED_TYPEDEF(RESOURCE)
struct Resource struct Resource
{ {
SharedController controller; SharedController controller;
SharedResource resource; SharedRESOURCE resource;
}; };
typedef std::map<std::string, Resource> ResourceMap; typedef std::map<std::string, Resource> ResourceMap;
...@@ -37,8 +38,8 @@ public: ...@@ -37,8 +38,8 @@ public:
virtual ~FileManager(void) {} virtual ~FileManager(void) {}
template<typename CONTROLLER> template<typename CONTROLLER>
SharedResource get(const CONTROLLER& _controller); ConstSharedRESOURCE get(const CONTROLLER& _controller);
SharedResource query(const std::string& _filename); ConstSharedRESOURCE query(const std::string& _filename);
bool exists(const std::string& _key); bool exists(const std::string& _key);
bool erase(const std::string& key); bool erase(const std::string& key);
void eraseAll(void); void eraseAll(void);
...@@ -60,14 +61,14 @@ private: ...@@ -60,14 +61,14 @@ private:
}; };
template<typename RESOURCE> template<typename CONTROLLER> template<typename RESOURCE> template<typename CONTROLLER>
typename FileManager<RESOURCE>::SharedResource FileManager<RESOURCE>::get(const CONTROLLER& _controller) typename FileManager<RESOURCE>::ConstSharedRESOURCE FileManager<RESOURCE>::get(const CONTROLLER& _controller)
{ {
typename ResourceMap::iterator existingResource = mResourceMap.find(_controller.getFilename()); typename ResourceMap::iterator existingResource = mResourceMap.find(_controller.getFilename());
if(existingResource != mResourceMap.end()) if(existingResource != mResourceMap.end())
return existingResource->second.resource; return existingResource->second.resource;
SharedController pController(new CONTROLLER(_controller)); SharedController pController(new CONTROLLER(_controller));
SharedResource pResource = pController->create(); SharedRESOURCE pResource = pController->create();
if(pResource) if(pResource)
{ {
Resource resource = { pController, pResource }; Resource resource = { pController, pResource };
...@@ -79,16 +80,16 @@ typename FileManager<RESOURCE>::SharedResource FileManager<RESOURCE>::get(const ...@@ -79,16 +80,16 @@ typename FileManager<RESOURCE>::SharedResource FileManager<RESOURCE>::get(const
{ {
Utils::error() << "FileManager::getResource: Resource could not be loaded: " << std::endl; Utils::error() << "FileManager::getResource: Resource could not be loaded: " << std::endl;
} }
return SharedResource(); return SharedRESOURCE();
} }
template<typename RESOURCE> template<typename RESOURCE>
typename FileManager<RESOURCE>::SharedResource FileManager<RESOURCE>::query(const std::string& _key) typename FileManager<RESOURCE>::ConstSharedRESOURCE FileManager<RESOURCE>::query(const std::string& _key)
{ {
typename ResourceMap::iterator existingResource = mResourceMap.find(_key); typename ResourceMap::iterator existingResource = mResourceMap.find(_key);
if(existingResource != mResourceMap.end()) if(existingResource != mResourceMap.end())
return existingResource->second.resource; return existingResource->second.resource;
return SharedResource(); return ConstSharedRESOURCE();
} }
template<typename RESOURCE> template<typename RESOURCE>
......
...@@ -16,9 +16,9 @@ using namespace ACGL::OpenGL; ...@@ -16,9 +16,9 @@ using namespace ACGL::OpenGL;
SharedShaderProgram ShaderProgramControlAutoFiles::create(void) SharedShaderProgram ShaderProgramControlAutoFiles::create(void)
{ {
SharedShaderProgram shaderProgram(new ShaderProgram()); SharedShaderProgram shaderProgram(new ShaderProgram());
if(SharedShader shader = ShaderFileManager::the()->get(ShaderControlFile(mFilename + ".vsh").type(GL_VERTEX_SHADER))) if(ConstSharedShader shader = ShaderFileManager::the()->get(ShaderControlFile(mFilename + ".vsh").type(GL_VERTEX_SHADER)))
shaderProgram->attachShader(shader); shaderProgram->attachShader(shader);
if(SharedShader shader = ShaderFileManager::the()->get(ShaderControlFile(mFilename + ".fsh").type(GL_FRAGMENT_SHADER))) if(ConstSharedShader shader = ShaderFileManager::the()->get(ShaderControlFile(mFilename + ".fsh").type(GL_FRAGMENT_SHADER)))
shaderProgram->attachShader(shader); shaderProgram->attachShader(shader);
if(shaderProgram->link()) if(shaderProgram->link())
return shaderProgram; return shaderProgram;
......
...@@ -12,7 +12,7 @@ using namespace ACGL::Utils; ...@@ -12,7 +12,7 @@ using namespace ACGL::Utils;
void ShaderProgram::bindAttributeLocations() const void ShaderProgram::bindAttributeLocations() const
{ {
for (SharedShaderVec::size_type i = 0; i < mShaders.size(); ++i) for (ConstSharedShaderVec::size_type i = 0; i < mShaders.size(); ++i)
{ {
const Shader::AttributeVec& attribs = mShaders[i]->getAttributes(); const Shader::AttributeVec& attribs = mShaders[i]->getAttributes();
for (Shader::AttributeVec::size_type k = 0; k < attribs.size(); ++k) for (Shader::AttributeVec::size_type k = 0; k < attribs.size(); ++k)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment