SDL2设置透明度

SDL_SetRenderDrawColor 完全支持透明度(Alpha)

一、函数原型

c 复制代码
int SDL_SetRenderDrawColor(
    SDL_Renderer* renderer,
    Uint8 r,   // 红 0~255
    Uint8 g,   // 绿 0~255
    Uint8 b,   // 蓝 0~255
    Uint8 a    // 透明度 0~255(0=全透,255=不透明)
);

二、关键:必须开启混合模式(否则 Alpha 无效)

默认 Alpha 是不生效的,要先设混合模式:

c 复制代码
// 1. 开启混合
SDL_SetRenderDrawBlendMode(render, SDL_BLENDMODE_BLEND);

// 2. 设置半透明红色(A=128,50%透明)
SDL_SetRenderDrawColor(render, 255, 0, 0, 128);

// 3. 画矩形(带透明度)
SDL_Rect rect = {100, 100, 200, 200};
SDL_RenderFillRect(render, &rect);

三、常见坑

  • 忘了 SDL_SetRenderDrawBlendMode() → Alpha 无效,永远不透明。
  • Alpha 值范围 0~255:0 全透,255 不透明。
  • SDL_RenderClear() 的 Alpha:清屏也受 A 影响,但窗口背景是否透要看系统。
相关推荐
clint4562 天前
C++进阶(1)——前景提要
c++
夜悊2 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴2 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0013 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾3 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
один but you3 天前
constexpr函数
c++
凡人叶枫3 天前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++
凡人叶枫3 天前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++
小胖xiaopangss3 天前
BRpc使用
c++·rpc
-森屿安年-3 天前
63. 不同路径 II
c++·算法·动态规划