Scene 支持添加/插入/移除 Shape 和子 Scene,灵活控制绘制顺序和层级结构。
| 函数 | 说明 |
|---|---|
int xgeShapeExSceneAdd(xge_shape_ex_scene pScene, xge_shape_ex pShape) | 追加 Shape |
int xgeShapeExSceneAddScene(xge_shape_ex_scene pScene, xge_shape_ex_scene pChild) | 追加子 Scene |
int xgeShapeExSceneInsert(xge_shape_ex_scene pScene, xge_shape_ex pShape, xge_shape_ex pBefore) | 在指定 Shape 前插入 |
int xgeShapeExSceneInsertShapeAt(xge_shape_ex_scene pScene, xge_shape_ex pShape, int iIndex) | 按索引插入 Shape |
int xgeShapeExSceneInsertSceneAt(xge_shape_ex_scene pScene, xge_shape_ex_scene pChild, int iIndex) | 按索引插入 Scene |
int xgeShapeExSceneRemove(xge_shape_ex_scene pScene, xge_shape_ex pShape) | 移除 Shape |
int xgeShapeExSceneRemoveScene(xge_shape_ex_scene pScene, xge_shape_ex_scene pChild) | 移除子 Scene |
gcc -O2 -I. -o ch64.exe ch64_main.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch64.exe
xge_shape_ex_scene root = NULL;
xgeShapeExSceneCreate(&root);
// 背景层
xge_shape_ex bg = NULL;
xgeShapeExCreate(&bg);
xgeShapeExAppendRect(bg, 0,0, 800,600, 0,0, 1);
xgeShapeExFillColor(bg, 0x1A1A2EFF);
xgeShapeExSceneAdd(root, bg);
xgeShapeExDestroy(bg);
// 子 Scene:角色组
xge_shape_ex_scene charGroup = NULL;
xgeShapeExSceneCreate(&charGroup);
xgeShapeExSceneAddScene(root, charGroup);
xgeShapeExSceneDestroy(charGroup);
// 在索引 0 处插入(最底层)
xgeShapeExSceneInsertShapeAt(root, bgShape, 0);
// 移除
xgeShapeExSceneRemove(root, bgShape);将以下代码保存为 ch64_main.c,编译后运行即可看到场景层级构建与绘制顺序效果:
#include "xge.h"
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0x081114FF);
xge_shape_ex_scene root = NULL;
xgeShapeExSceneCreate(&root);
// 背景层(最先添加 = 最底层)
xge_shape_ex bg = NULL;
xgeShapeExCreate(&bg);
xgeShapeExAppendRect(bg, 50,50, 700,500, 16,16, 1);
xgeShapeExFillColor(bg, 0x1A1A2EFF);
xgeShapeExSceneAdd(root, bg);
xgeShapeExDestroy(bg);
// 子 Scene:角色组
xge_shape_ex_scene charGroup = NULL;
xgeShapeExSceneCreate(&charGroup);
xge_shape_ex c1 = NULL;
xgeShapeExCreate(&c1);
xgeShapeExAppendCircle(c1, 300, 300, 70, 70, 1);
xgeShapeExFillColor(c1, 0x4FD8C2FF);
xgeShapeExSceneAdd(charGroup, c1);
xgeShapeExDestroy(c1);
xge_shape_ex c2 = NULL;
xgeShapeExCreate(&c2);
xgeShapeExAppendRect(c2, 400,230, 140,140, 12,12, 1);
xgeShapeExFillColor(c2, 0xFFB454FF);
xgeShapeExSceneAdd(charGroup, c2);
xgeShapeExDestroy(c2);
xgeShapeExSceneAddScene(root, charGroup);
xgeShapeExSceneDestroy(charGroup);
// 绘制整棵树
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 = "ch64 - Scene Children";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
xgeRun(onFrame, NULL);
xgeUnit();
return 0;
}