GetCount/GetAt 按索引访问子节点,Traverse 深度优先遍历整棵场景树。
| 函数 | 说明 |
|---|---|
int xgeShapeExSceneGetCount(xge_shape_ex_scene pScene, int* pCount) | 获取子节点数量 |
int xgeShapeExSceneGetAt(xge_shape_ex_scene pScene, int iIndex, xge_shape_ex* ppShape) | 按索引获取 Shape |
int xgeShapeExSceneChildGetAt(xge_shape_ex_scene pScene, int iIndex, xge_shape_ex_scene_child_t* pChild) | 获取子节点(含类型) |
int xgeShapeExSceneTraverse(xge_shape_ex_scene pScene, xge_shape_ex_scene_visit_proc onPaint, void* pUser) | 深度优先遍历 |
gcc -O2 -I. -o ch65.exe ch65_main.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch65.exe
// 遍历回调:统计所有 Shape 数量
static int s_iCount = 0;
static void OnVisit(xge_shape_ex pShape, void* pUser) {
s_iCount++;
}
int iTotal;
xgeShapeExSceneGetCount(root, &iTotal);
s_iCount = 0;
xgeShapeExSceneTraverse(root, OnVisit, NULL);
// s_iCount = 场景中所有 Shape 总数将以下代码保存为 ch65_main.c,编译后运行即可看到场景树遍历效果:
#include "xge.h"
static int s_iCount = 0;
static void OnVisit(xge_shape_ex pShape, void* pUser) {
(void)pUser;
s_iCount++;
// 遍历时修改每个 Shape 的透明度
xgeShapeExOpacity(pShape, 0.5f + 0.5f * (s_iCount % 2));
}
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0x081114FF);
// 构建场景树
xge_shape_ex_scene root = NULL;
xgeShapeExSceneCreate(&root);
for (int i = 0; i < 5; i++) {
xge_shape_ex s = NULL;
xgeShapeExCreate(&s);
xgeShapeExAppendRect(s, 80+i*130,200, 100,200, 8,8, 1);
xgeShapeExFillColor(s, 0x4FD8C2FF);
xgeShapeExSceneAdd(root, s);
xgeShapeExDestroy(s);
}
// 遍历场景树
s_iCount = 0;
xgeShapeExSceneTraverse(root, OnVisit, NULL);
xgeShapeExSceneDraw(root, 0.25f);
xgeShapeExSceneDestroy(root);
return XGE_OK;
}
int main(void) {
xge_desc_t d = {0};
d.iWidth = 800;
d.iHeight = 600;
d.sTitle = "ch65 - Scene Traverse";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
xgeRun(onFrame, NULL);
xgeUnit();
return 0;
}