Opacity 整体调节 Shape 透明度(与颜色 Alpha 相乘),Visible 控制是否参与绘制。
| 函数 | 说明 |
|---|---|
int xgeShapeExOpacity(xge_shape_ex pShape, float fOpacity) | 设置整体透明度 |
int xgeShapeExOpacityGet(xge_shape_ex pShape, float* pOpacity) | 获取透明度 |
int xgeShapeExVisible(xge_shape_ex pShape, int bVisible) | 设置可见性 |
int xgeShapeExVisibleGet(xge_shape_ex pShape, int* pVisible) | 获取可见性 |
gcc -O2 -I. -o ch53.exe ch53_main.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch53.exe
static float s_fAlpha = 0.0f;
void OnFrame(void) {
s_fAlpha += 0.02f;
if (s_fAlpha > 1.0f) s_fAlpha = 1.0f;
xgeShapeExOpacity(g_shape, s_fAlpha);
xgeShapeExDraw(g_shape, 0.25f);
}// 隐藏 Shape(不绘制,零开销)
xgeShapeExVisible(g_shape, 0);
// 需要时重新显示
xgeShapeExVisible(g_shape, 1);
// 查询当前状态
int bVis;
xgeShapeExVisibleGet(g_shape, &bVis);将以下代码保存为 ch53_main.c,编译后运行即可看到透明度淡入与呼吸灯效果:
#include "xge.h"
#include <math.h>
static float s_fTime = 0.0f;
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0x081114FF);
s_fTime += 0.03f;
// 淡入动画:Opacity 从 0 到 1
float fadeIn = s_fTime;
if (fadeIn > 1.0f) fadeIn = 1.0f;
xge_shape_ex s1 = NULL;
xgeShapeExCreate(&s1);
xgeShapeExAppendRect(s1, 80,100, 280,180, 16,16, 1);
xgeShapeExFillColor(s1, 0x4FD8C2FF);
xgeShapeExOpacity(s1, fadeIn);
xgeShapeExDraw(s1, 0.25f);
xgeShapeExDestroy(s1);
// 呼吸灯:Opacity 在 0.3~1.0 正弦波动
float breath = 0.65f + 0.35f * (float)sin(s_fTime * 2.0f);
xge_shape_ex s2 = NULL;
xgeShapeExCreate(&s2);
xgeShapeExAppendCircle(s2, 580, 190, 90, 90, 1);
xgeShapeExFillColor(s2, 0xFFB454FF);
xgeShapeExOpacity(s2, breath);
xgeShapeExDraw(s2, 0.25f);
xgeShapeExDestroy(s2);
// Visible 切换:每 60 帧切换一次
int bShow = ((int)(s_fTime * 60.0f) / 60) % 2;
xge_shape_ex s3 = NULL;
xgeShapeExCreate(&s3);
xgeShapeExAppendRect(s3, 250,380, 300,100, 12,12, 1);
xgeShapeExFillColor(s3, 0x4FD8C2FF);
xgeShapeExVisible(s3, bShow);
xgeShapeExDraw(s3, 0.25f);
xgeShapeExDestroy(s3);
return XGE_OK;
}
int main(void) {
xge_desc_t d = {0};
d.iWidth = 800;
d.iHeight = 600;
d.sTitle = "ch53 - Opacity & Visible";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
xgeRun(onFrame, NULL);
xgeUnit();
return 0;
}