函数编写】 棋子位图的原始像素放在全局数组unsigned char *g_pRawChessBmpBits中, 棋盘位图的原始像素放在全局数组unsigned char *g_pRawBoardBmpBits中, 函数的功能是,利用这两个原始像素数组,根据参数传下来的尺寸、坐标和透明度,计算新的半透明效果的玻璃棋子位图的像素值,并放在第一个参数指定的数组中。(不得修改两个全局数组) */ //--------------------------------------------------------------------------------------------------------------------------------------------------------- void ModifyChessBmp( unsigned char *pPixel, //将修改后的像素RGB值放在这个数组中 int iChessBmpWidth, //棋子位图的宽度(以像素为单位) int iChessBmpHeight, //棋子位图的高度(以像素为单位) int iBoardBmpWidth, //棋盘位图的宽度(以像素为单位) int iDrawingX, //棋子位图被贴到棋盘位图的X坐标(水平向右为正向)(以像素为单位) int iDrawingY, //棋子位图被贴到棋盘位图的Y坐标(垂直向下为正向)(以像素为单位) float fTransparency //透明度,值为0.0-1.0 ) { }
时间: 2023-08-15 09:32:28 浏览: 245
下面是一个实现上述功能的函数代码:
```c
void ModifyChessBmp(unsigned char *pPixel, int iChessBmpWidth, int iChessBmpHeight, int iBoardBmpWidth, int iDrawingX, int iDrawingY, float fTransparency)
{
unsigned char *pChessPixel = g_pRawChessBmpBits;
unsigned char *pBoardPixel = g_pRawBoardBmpBits;
int i, j, k, l;
float fAlpha = 1.0f - fTransparency;
for (i = 0; i < iChessBmpHeight; i++) {
for (j = 0; j < iChessBmpWidth; j++) {
int iPixelIndex = 4 * (i * iChessBmpWidth + j);
int iChessX = iDrawingX + j;
int iChessY = iDrawingY + i;
int iBoardX = iChessX - iDrawingX;
int iBoardY = iChessY - iDrawingY;
int iBoardIndex = 4 * (iBoardY * iBoardBmpWidth + iBoardX);
if (iBoardX >= 0 && iBoardX < iBoardBmpWidth && iBoardY >= 0 && iBoardY < iBoardBmpWidth) {
float fRed = fAlpha * pBoardPixel[iBoardIndex] + fTransparency * pChessPixel[iPixelIndex];
float fGreen = fAlpha * pBoardPixel[iBoardIndex + 1] + fTransparency * pChessPixel[iPixelIndex + 1];
float fBlue = fAlpha * pBoardPixel[iBoardIndex + 2] + fTransparency * pChessPixel[iPixelIndex + 2];
float fAlphaChannel = fAlpha * pBoardPixel[iBoardIndex + 3] + fTransparency * pChessPixel[iPixelIndex + 3];
pPixel[iPixelIndex] = (unsigned char)fRed;
pPixel[iPixelIndex + 1] = (unsigned char)fGreen;
pPixel[iPixelIndex + 2] = (unsigned char)fBlue;
pPixel[iPixelIndex + 3] = (unsigned char)fAlphaChannel;
}
else {
pPixel[iPixelIndex] = pChessPixel[iPixelIndex];
pPixel[iPixelIndex + 1] = pChessPixel[iPixelIndex + 1];
pPixel[iPixelIndex + 2] = pChessPixel[iPixelIndex + 2];
pPixel[iPixelIndex + 3] = pChessPixel[iPixelIndex + 3];
}
}
}
}
```
该函数首先将棋子位图和棋盘位图的原始像素分别保存在全局数组 `g_pRawChessBmpBits` 和 `g_pRawBoardBmpBits` 中。然后使用两个嵌套的循环遍历棋子位图的每个像素,并根据棋子在棋盘上的位置和指定的透明度计算新的半透明像素值。最后将计算得到的新像素值保存到指定的数组中。注意,这里使用了浮点数计算透明度和颜色值,因为在计算过程中可能会出现小数。
阅读全文