我最近在 Cinder 中遇到了一种范围技术——一个图形库:
{
gl::ScopedModelMatrix scpModelMatrix;
//.... Anything in this area will be executed in ModelMatrix-mode.
// (if applicable)
}
//.... In this area, there is no ModelMatrix-mode effect.
让我们假设这种技术是通过以下方式实现的:
- 构造函数(在作用域的开头);
- 析构函数(在作用域的末尾)。
(谢谢,Klitos Kyriacou!)
问题
这种技术叫什么名字?
...我试图用谷歌搜索它,但没有找到提到这一点的文章。这种技术有什么缺点?
我计划做类似的事情:
{
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.
}
换句话说,有什么我应该特别注意的吗?
Edit2:非常感谢许多有用的评论。
根据要求,这就是我将如何实现它。大致说来,
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 ...
每次创建新的图形/物理对象时,它都会被标记为游戏对象所有。
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.
请注意,我不使用任何静态变量/函数。
参考:https ://libcinder.org/docs/structcinder_1_1gl_1_1_scoped_depth_test.html