I have recently encountered a scope technique in Cinder -- a graphics library:
{
gl::ScopedModelMatrix scpModelMatrix;
//.... Anything in this area will be executed in ModelMatrix-mode.
// (if applicable)
}
//.... In this area, there is no ModelMatrix-mode effect.
Let's assume that this technique is implemented by:
- The constructor (at the beginning of the scope);
- The destructor (at the end of the scope).
(Thank, Klitos Kyriacou!)
Question
What is the name of this technique?
... I tried to google it, but found no article mentioning this.What are the disadvantages of this technique?
I planned to do something like:
{
Go* gameObject = sysCreator->createGameObject();
ScopeMarker scope=sysCreator->markScope(gameObject);
Graphic_Object* gra = sysGraphic->create( ... );
Physic_Object* phy = sysPhysic->create( .... );
// "gra" & "phy" will be considered as owned by "gameObject".
// That is : both variables will be deleted automatically ...
// ... when "gameObject" is destroyed.
}
In other words, is there anything of which I should be careful specifically?
Edit2: Thank a lot for many useful comments.
As requested, this is how I will implement it. Roughly speaking,
ScopeMarker SysCreator::markScope(GameObject* gameObject){
sysGraphic->notify( gameObject );
sysPhysic->notify( gameObject );
ScopeMarker scope= ScopeMarker(this);
}
int counter=0; // counter for scope constructor (+1), destructor (-1)
// .... other code to manage Constructor / Destructor of ScopeMarker ...
Every time new graphic/physic object created, it will be marked as owned by the game object.
void SysGraphic::notify(GameObject* gameObject){
this->cacheGO=gameObject;
}
Graphic_Object* SysGraphic::create( some parameter ){
Graphic_Object* gra = create_( some parameter);
sysOwnerBinder->markOwner( this->cacheGO, gra ); //another system
}
//Every timestep,
// sysOwnerBinder will check find every "gameObject" that will be deleted.
// It will delete its "gra" & "phy" first.
Note that I don't use any static variables / function.
Reference: https://libcinder.org/docs/structcinder_1_1gl_1_1_scoped_depth_test.html