一、半透明效果
半透明效果在游戏世界里是用的很频繁的,比如鬼魂,隐形人物。
简单的说,半透明是前景图案和背景图案像素颜色的混合。可以简单的把半透明处理,整理为一个公式:
半透明图色彩 = 前景图色彩× 不透明度 + 背景图色彩 × (1 -不透明度)
半透明处理的步骤:
1. 获取位图结构。
GetObject()获取位图的结构后,可得到位图信息。
2. 建立暂存数组
获得了位图结构后,接下来需要先建立一个暂时存储位图像素的颜色值。这个暂存数组的大小有前一步获得位图信息的bmHeight, bmWidthBytes决定。
这里有必要提一下BITMAP结构,因为在之后将会用到。
typedef struct tagBITMAP {
LONG bmType; //位图类型
LONG bmWidth; //位图宽度
LONG bmHeight; //长度
LONG bmWidthBytes; //每一列像素所占Byte数
WORD bmPlanes; //颜色平面数
WORD bmBitsPixel; //像素位数
LPVOID bmBits; //位图内存指针
} BITMAP, *PBITMAP;
3. 取得位图位值。
建立数组后,可以用GetBitmapBits()来获得位图位值。
4. 合成像素颜色值
获得位图的所有像素颜色值后,按照公式完成半透明颜色值的合成。
5. 重设位图颜色。
根据数组的内容来重新设位图的颜色。使用SetBitmapBits() 。
完成后,最后就是贴图了。
范例:
首先创建win32项目。让系统生成基本框架代码。
在程序开始处添加全局变量。
HBITMAP bg, girl;
HDC mdc;
//常量
const int xstart= 50;
const int ystart= 20;
然后在InitInstance()函数中添加代码
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
HDC hdc;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFA