Scene 是 Shape 的容器节点,支持树形层级、引用计数、深拷贝和批量管理。
| 函数 | 说明 |
|---|---|
int xgeShapeExSceneCreate(xge_shape_ex_scene* ppScene) | 创建场景 |
int xgeShapeExSceneAddRef(xge_shape_ex_scene pScene) | 增加引用 |
int xgeShapeExSceneRefCountGet(xge_shape_ex_scene pScene, int* pRefCount) | 获取引用计数 |
int xgeShapeExSceneClone(xge_shape_ex_scene pScene, xge_shape_ex_scene* ppClone) | 深拷贝 |
void xgeShapeExSceneDestroy(xge_shape_ex_scene pScene) | 释放引用 |
int xgeShapeExSceneClear(xge_shape_ex_scene pScene) | 清除所有子节点 |
int xgeShapeExSceneId(xge_shape_ex_scene pScene, uint32_t iId) | 设置 ID |
int xgeShapeExSceneParentGet(xge_shape_ex_scene pScene, xge_shape_ex_scene* ppParent) | 获取父节点 |
gcc -O2 -I. -o ch63.exe ch63_main.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch63.exe
xge_shape_ex_scene scene = NULL;
xgeShapeExSceneCreate(&scene);
xgeShapeExSceneId(scene, 1001);
// 添加子 Shape
xge_shape_ex shape = NULL;
xgeShapeExCreate(&shape);
xgeShapeExAppendCircle(shape, 0,0, 50,50, 1);
xgeShapeExFillColor(shape, 0x4FD8C2FF);
xgeShapeExSceneAdd(scene, shape);
xgeShapeExDestroy(shape); // Scene 已 AddRef
// 深拷贝
xge_shape_ex_scene clone = NULL;
xgeShapeExSceneClone(scene, &clone);
// 销毁
xgeShapeExSceneDestroy(clone);
xgeShapeExSceneDestroy(scene);将以下代码保存为 ch63_main.c,编译后运行即可看到 Scene 创建、添加、克隆、销毁的完整流程:
#include "xge.h"
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0x081114FF);
// 创建 Scene
xge_shape_ex_scene scene = NULL;
xgeShapeExSceneCreate(&scene);
xgeShapeExSceneId(scene, 1001);
// 添加子 Shape
xge_shape_ex s1 = NULL;
xgeShapeExCreate(&s1);
xgeShapeExAppendCircle(s1, 300, 300, 80, 80, 1);
xgeShapeExFillColor(s1, 0x4FD8C2FF);
xgeShapeExSceneAdd(scene, s1);
xgeShapeExDestroy(s1);
xge_shape_ex s2 = NULL;
xgeShapeExCreate(&s2);
xgeShapeExAppendRect(s2, 420,220, 160,160, 12,12, 1);
xgeShapeExFillColor(s2, 0xFFB454FF);
xgeShapeExSceneAdd(scene, s2);
xgeShapeExDestroy(s2);
// 绘制整棵场景树
xgeShapeExSceneDraw(scene, 0.25f);
xgeShapeExSceneDestroy(scene);
return XGE_OK;
}
int main(void) {
xge_desc_t d = {0};
d.iWidth = 800;
d.iHeight = 600;
d.sTitle = "ch63 - Scene Lifecycle";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
xgeRun(onFrame, NULL);
xgeUnit();
return 0;
}