Just got to playing, and figured other people might want to take advantage of this. It's not a big deal, but something that I've been thinking about.
I do a lot of:
myObject::draw()
{
glPushMatrix();
glPushAttrib(GL_STUFF_I_AM_WORRIED_ABOUT_CLOBERING);
// Draw stuff, do transforms, etc.
glPopAttrib();
glPopMatrix();
}
And got to thinking that I should let C++ help me out with this. Really, it's a couple of simple classes that via C's scoping mechanism will do that for me. It basically reduces that code by 1/2 (big deal eh?), and since it's inlined, should have no run-time speed hit (if compilers do things correctly). However, I forsaw it mostly as a method to prevent forgetting to pop what you push.
myObject::draw()
{
CGLMatrixContext matContext;
CGLAttributeContext theContext(GL_STUFF_I_AM_WORRIED_ABOUT_CLOBERING);
// Draw stuff, do transforms, etc.
}
Hope it makes a few people's lives easier...nothing big, but I like it better.
/*
* GLSaveContext.h
* wxModelView
*
* Created by Casey O'Donnell on Sun Mar 16 2003.
* Copyright (c) 2003 Casey O'Donnell. All rights reserved.
*
*/
// GLSaveContext.h: interface for the GLSaveContext classes.
//
//////////////////////////////////////////////////////////////////////
#ifndef _GLSAVECONTEXT_H__
#define _GLSAVECONTEXT_H__
#include "stdinclude.h"
class CGLMatrixContext
{
public:
CGLMatrixContext();
~CGLMatrixContext();
};
inline CGLMatrixContext::CGLMatrixContext()
{
glPushMatrix();
}
inline CGLMatrixContext::~CGLMatrixContext()
{
glPopMatrix();
}
class CGLAttributeContext
{
public:
CGLAttributeContext(GLbitfield mask);
~CGLAttributeContext();
};
inline CGLAttributeContext::CGLAttributeContext(GLbitfield mask)
{
glPushAttrib(mask);
}
inline CGLAttributeContext::~CGLAttributeContext()
{
glPopAttrib();
}
class CGLTextureContext : public CGLAttributeContext
{
public:
CGLTextureContext();
};
inline CGLTextureContext::CGLTextureContext() : CGLAttributeContext(GL_TEXTURE_BIT)
{
}
class CGLCurrentContext : public CGLAttributeContext
{
public:
CGLCurrentContext();
};
inline CGLCurrentContext::CGLCurrentContext() : CGLAttributeContext(GL_CURRENT_BIT)
{
}
class CGLLightingContext : public CGLAttributeContext
{
public:
CGLLightingContext();
};
inline CGLLightingContext::CGLLightingContext() : CGLAttributeContext(GL_LIGHTING_BIT)
{
}
class CGLEnableContext : public CGLAttributeContext
{
public:
CGLEnableContext();
};
inline CGLEnableContext::CGLEnableContext() : CGLAttributeContext(GL_ENABLE_BIT)
{
}
#endif // _GLSAVECONTEXT_H__
- sighuh?
[edited by - redragon on March 16, 2003 7:50:08 PM]
[edited by - redragon on March 16, 2003 10:25:26 PM]