绘制圆弧——进度环、仪表盘、加载动画的核心图元。理解弧度制与角度参数。
XGE 的弧线使用弧度(radians)而非角度。完整圆 = 2π 弧度 = 360°。0 弧度指向右方(3 点钟方向),顺时针增长(Y 轴向下时)。
弧度 = 角度 × π / 180。XGE 提供 3.14159265f 常量。
| 函数 | 说明 |
|---|---|
void xgeShapeArc(float fX, float fY, float fRadius, float fStartRadians, float fEndRadians, float fWidth, uint32_t iColor) | 绘制弧线(世界坐标) |
void xgeShapeArcPx(float fX, float fY, float fRadius, float fStartRadians, float fEndRadians, float fWidth, uint32_t iColor) | 绘制弧线(像素坐标) |
gcc -O2 -I. -o ch24.exe ch24_main.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch24.exe
// 半圆弧(上半圆):从 180° 到 360°
xgeShapeArc(400, 300, 100.0f,
3.14159265f, // 起始:180°(左)
2 * 3.14159265f, // 结束:360°(右)
4.0f, // 线宽
0x4FD8C2FF);
// 四分之一弧(右下角)
xgeShapeArc(400, 300, 60.0f,
0, // 0°(右)
3.14159265f / 2, // 90°(下)
3.0f, 0xFFB454FF);static int onFrame(void* pUser) {
xgeClear(0x081114FF);
double t = xgeTimer();
float start = (float)(t * 3.0); // 旋转速度
float sweep = 3.14159265f * 1.5f; // 270° 弧
xgeShapeArc(400, 300, 50.0f,
start, start + sweep,
6.0f, 0x6DB3F2FF);
return XGE_OK;
}float fProgress = 0.72f; // 72%
float fEnd = fProgress * 2.0f * 3.14159265f;
// 背景环(灰色完整圆)
xgeShapeArc(400, 300, 80.0f, 0, 2*3.14159265f, 10.0f, 0x333333FF);
// 进度弧(从顶部开始,-90° 偏移)
float offset = -3.14159265f / 2.0f;
xgeShapeArc(400, 300, 80.0f, offset, offset + fEnd, 10.0f, 0x4FD8C2FF);将以下代码保存为 ch24_main.c,编译后运行即可看到动态弧线进度环:
#include "xge.h"
#include <math.h>
static int onFrame(void* pUser) {
(void)pUser;
double t = xgeTimer();
xgeClear(0x081114FF);
// 动态进度环(0~360° 循环)
float progress = (float)fmod(t * 0.3, 1.0);
float endAngle = progress * 360.0f;
// 背景环(完整圆)
xgeShapeArc(400, 300, 100, 0.0f, 360.0f, 8.0f, 0x1A2A30FF);
// 进度弧线
xgeShapeArc(400, 300, 100, -90.0f, endAngle, 8.0f, 0x4FD8C2FF);
return XGE_OK;
}
int main(void) {
xge_desc_t d = {0};
d.iWidth = 800;
d.iHeight = 600;
d.sTitle = "ch24 - Arc";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
xgeRun(onFrame, NULL);
xgeUnit();
return 0;
}xgeShapeArc(cx, cy, r, startAngle, sweepAngle, width, color) 绘制圆弧,角度单位为度。fmod(t*0.3, 1.0) 生成 0~1 循环进度,约 3.3 秒一圈。xgeShapePieFill(第 27 章)。