ShapeEx 是 XGE 的高级矢量路径对象,支持复杂路径、渐变填充、描边样式。本章学习创建、引用计数、克隆、重置与销毁。
| 函数 | 说明 |
|---|---|
int xgeShapeExCreate(xge_shape_ex* ppShape) | 创建 ShapeEx 对象 |
int xgeShapeExAddRef(xge_shape_ex pShape) | 引用计数 +1 |
int xgeShapeExRefCountGet(xge_shape_ex pShape, int* pRefCount) | 获取当前引用计数 |
int xgeShapeExClone(xge_shape_ex pShape, xge_shape_ex* ppClone) | 深拷贝克隆 |
int xgeShapeExReset(xge_shape_ex pShape) | 重置(清除路径和样式) |
void xgeShapeExDestroy(xge_shape_ex pShape) | 引用计数 -1,归零释放 |
gcc -O2 -I. -o ch33.exe ch33_main.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch33.exe
xge_shape_ex shape = NULL;
if (xgeShapeExCreate(&shape) != 0) {
xgeLogWrite(XGE_LOG_ERROR, "创建 ShapeEx 失败");
return;
}
// ... 构建路径、设置样式(后续章节)...
// 不再需要时销毁
xgeShapeExDestroy(shape);xge_shape_ex shared = NULL;
xgeShapeExCreate(&shared); // refCount = 1
// 另一个模块也需要使用
xgeShapeExAddRef(shared); // refCount = 2
// 查询引用计数
int count = 0;
xgeShapeExRefCountGet(shared, &count);
xgeLogWrite(XGE_LOG_INFO, "refCount = %d", count);
// 两个模块各自释放
xgeShapeExDestroy(shared); // refCount = 1
xgeShapeExDestroy(shared); // refCount = 0 → 释放// 克隆一份独立副本
xge_shape_ex copy = NULL;
xgeShapeExClone(shared, ©); // 深拷贝,互不影响
// 修改副本不影响原件
xgeShapeExFillColor(copy, 0xFF6B5EFF);
// 重置:清除所有路径和样式,对象可复用
xgeShapeExReset(copy); // 现在 copy 是"空白"状态
xgeShapeExDestroy(copy);
xgeShapeExDestroy(shared);将以下代码保存为 ch33_main.c,编译后运行即可看到 ShapeEx 对象的创建、克隆、引用计数与销毁流程:
#include "xge.h"
static xge_shape_ex g_shape = NULL;
static xge_shape_ex g_clone = NULL;
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0x081114FF);
// 绘制原始对象(绿色三角形)
xgeShapeExFillColor(g_shape, 0x4FD8C2FF);
xgeShapeExDraw(g_shape, 0.25f);
// 绘制克隆对象(红色,平移后)
xgeShapeExFillColor(g_clone, 0xFF6B5EFF);
xgeShapeExDraw(g_clone, 0.25f);
return XGE_OK;
}
int main(void) {
xge_desc_t d = {0};
d.iWidth = 800;
d.iHeight = 600;
d.sTitle = "ch33 - ShapeEx Lifecycle";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
// 创建 ShapeEx 并构建三角形路径
xgeShapeExCreate(&g_shape);
xgeShapeExMoveTo(g_shape, 200, 100);
xgeShapeExLineTo(g_shape, 100, 350);
xgeShapeExLineTo(g_shape, 300, 350);
xgeShapeExClose(g_shape);
// 克隆并平移
xgeShapeExClone(g_shape, &g_clone);
xge_shape_ex_matrix_t mat;
xgeShapeExMatrixIdentity(&mat);
xgeShapeExMatrixTranslate(&mat, 350, 50);
xgeShapeExTransformSet(g_clone, &mat);
// 查询引用计数
int ref = 0;
xgeShapeExRefCountGet(g_shape, &ref);
xgeRun(onFrame, NULL);
// 销毁
xgeShapeExDestroy(g_clone);
xgeShapeExDestroy(g_shape);
xgeUnit();
return 0;
}xgeShapeExCreate 创建对象,然后用 MoveTo/LineTo/Close 构建三角形路径。xgeShapeExClone 深拷贝,修改克隆的颜色和变换不影响原件。