Chapter 64 · ShapeEx 场景图

子节点管理

Scene 支持添加/插入/移除 Shape 和子 Scene,灵活控制绘制顺序和层级结构。

64

API 速查

函数说明
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;
}

代码讲解

  • SceneAdd:追加 Shape 到末尾,先添加的先绘制(在底层)。
  • SceneAddScene:添加子 Scene,形成层级结构。
  • InsertShapeAt:按索引插入,可控制绘制顺序。
  • Remove:移除只断开父子关系并 Release,不销毁对象。

要点与陷阱

  • 绘制顺序:先添加的先绘制(在底层),后添加的在上层。
  • Remove 不 Destroy:移除只断开父子关系并 Release,不销毁对象。
  • 一个父节点:同一 Shape/Scene 不能同时属于两个父 Scene。
  • Insert before:pBefore=NULL 时等同于 Add(追加到末尾)。

进阶练习

  1. 实现图层面板:上移/下移调整绘制顺序。
  2. 拖拽重排:Remove + InsertAt 实现图层拖拽排序。
子节点管理效果
图:子节点管理渲染效果