虚线由 Dash 数组定义"画-空"交替长度,Offset 控制起始偏移,可实现蚂蚁线动画。
| 函数 | 说明 |
|---|---|
int xgeShapeExStrokeDash(xge_shape_ex pShape, const float* pDashPattern, int iDashCount, float fDashOffset) | 设置虚线模式 |
int xgeShapeExStrokeDashGet(xge_shape_ex pShape, const float** ppDashPattern, int* pDashCount, float* pDashOffset) | 获取虚线参数 |
gcc -O2 -I. -o ch49.exe ch49_main.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch49.exe
xge_shape_ex shape = NULL;
xgeShapeExCreate(&shape);
xgeShapeExMoveTo(shape, 100, 300);
xgeShapeExLineTo(shape, 700, 300);
xgeShapeExStrokeColor(shape, 0x4FD8C2FF);
xgeShapeExStrokeWidth(shape, 4.0f);
// 虚线:画 20px,空 10px
float dash[] = { 20.0f, 10.0f };
xgeShapeExStrokeDash(shape, dash, 2, 0.0f);
xgeShapeExDraw(shape, 0.25f);static float s_fOffset = 0.0f;
void OnFrame(void) {
s_fOffset += 0.5f;
if (s_fOffset >= 30.0f) s_fOffset -= 30.0f;
float dash[] = { 15.0f, 15.0f };
xgeShapeExStrokeDash(g_shape, dash, 2, s_fOffset);
xgeShapeExDraw(g_shape, 0.25f);
}// 点划线:长画 20, 空 5, 点 2, 空 5
float dashDot[] = { 20.0f, 5.0f, 2.0f, 5.0f };
xgeShapeExStrokeDash(shape, dashDot, 4, 0.0f);
xgeShapeExStrokeCap(shape, XGE_SHAPE_EX_CAP_ROUND);将以下代码保存为 ch49_main.c,编译后运行即可看到虚线模式与蚤蚁线动画效果:
#include "xge.h"
static float s_fOffset = 0.0f;
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0x081114FF);
// 蚤蚁线动画:动态 Offset
s_fOffset += 0.5f;
if (s_fOffset >= 30.0f) s_fOffset -= 30.0f;
// 矩形选区边框
xge_shape_ex sel = NULL;
xgeShapeExCreate(&sel);
xgeShapeExAppendRect(sel, 100,80, 600,200, 8,8, 1);
xgeShapeExStrokeColor(sel, 0x4FD8C2FF);
xgeShapeExStrokeWidth(sel, 3.0f);
float dash[] = { 15.0f, 15.0f };
xgeShapeExStrokeDash(sel, dash, 2, s_fOffset);
xgeShapeExDraw(sel, 0.25f);
xgeShapeExDestroy(sel);
// 点划线:长画 20, 空 5, 点 2, 空 5
xge_shape_ex dl = NULL;
xgeShapeExCreate(&dl);
xgeShapeExMoveTo(dl, 100, 380);
xgeShapeExLineTo(dl, 700, 380);
xgeShapeExStrokeColor(dl, 0xFFB454FF);
xgeShapeExStrokeWidth(dl, 3.0f);
xgeShapeExStrokeCap(dl, XGE_SHAPE_EX_CAP_ROUND);
float dashDot[] = { 20.0f, 5.0f, 2.0f, 5.0f };
xgeShapeExStrokeDash(dl, dashDot, 4, 0.0f);
xgeShapeExDraw(dl, 0.25f);
xgeShapeExDestroy(dl);
// 圆点虚线:Round Cap + 短画线
xge_shape_ex dot = NULL;
xgeShapeExCreate(&dot);
xgeShapeExMoveTo(dot, 100, 480);
xgeShapeExLineTo(dot, 700, 480);
xgeShapeExStrokeColor(dot, 0x4FD8C2FF);
xgeShapeExStrokeWidth(dot, 6.0f);
xgeShapeExStrokeCap(dot, XGE_SHAPE_EX_CAP_ROUND);
float dots[] = { 0.1f, 18.0f };
xgeShapeExStrokeDash(dot, dots, 2, 0.0f);
xgeShapeExDraw(dot, 0.25f);
xgeShapeExDestroy(dot);
return XGE_OK;
}
int main(void) {
xge_desc_t d = {0};
d.iWidth = 800;
d.iHeight = 600;
d.sTitle = "ch49 - Dash Pattern";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
xgeRun(onFrame, NULL);
xgeUnit();
return 0;
}xgeShapeExStrokeDash(shape, NULL, 0, 0) 恢复实线。