以后所有图片都放在根目录下:
如有另放,会特别注明
//全局变量
HBITMAP bg,girl;
HDC mdc;
//起始坐标
const int xstart = 50;
const int ystart = 20;
//--------------------------------------------InitInstance() 函数----------------------------------------------
//加载资源
//-------------------------------------------------------------------------------------------------------------
BOOL InitInstance(HINSTANCE hInstance, int nShowCmd)
{
HWND hwnd;
HDC hdc;
//创建窗口第三步:正式创建窗口
//创建窗口函数
hwnd = CreateWindow("GameClass", WINDOW_TITLE, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hwnd)
{
return FALSE;
}
//创建窗口第四步:窗口的移动,显示和更新
MoveWindow(hwnd, 250, 80, WINDOW_WIDTH, WINDOW_HEIGHT, true);//调整窗口显示时的位置及窗口的大小
ShowWindow(hwnd, nShowCmd);//设定显示窗口时的状态
UpdateWindow(hwnd);//将窗口绘制于显示设备上
hdc = GetDC(hwnd);
mdc = CreateCompatibleDC(hdc);
BITMAP bm1, bm2;
unsigned char *px1, *px2;
//处理背景图
bg = (HBITMAP)LoadImage(NULL, "bg.bmp", IMAGE_BITMAP, 800, 600, LR_LOADFROMFILE);
GetObject(bg, sizeof(BITMAP), &bm1);
if (bm1.bmBitsPixel != 32 && bm1.bmBitsPixel != 24)
{
MessageBox(NULL, "此程序只能在 32 bit或 24 bit显示模式中运行", "警告", 0);
return FALSE;
}
px1 = new unsigned char[bm1.bmHeight * bm1.bmWidthBytes];
GetBitmapBits(bg, bm1.bmHeight * bm1.bmWidthBytes, px1);
//处理前景图
girl = (HBITMAP)LoadImage(NULL, "girl.bmp", IMAGE_BITMAP, 300, 350, LR_LOADFROMFILE);
GetObject(girl, sizeof(BITMAP), &bm2);
px2 = new unsigned char[bm2.bmHeight * bm2.bmWidthBytes];
GetBitmapBits(girl, bm2.bmHeight * bm2.bmWidthBytes, px2);
int xend, yend;
int x, y, i; //循环变量
int rgb_b;
int PxBytes = bm1.bmBitsPixel / 8;
xend = xstart + 300;
yend = ystart + 350;
//处理背景图像像素颜色
for (y = ystart; y<yend; y++)
{
for (x = xstart; x<xend; x++)
{
rgb_b = y * bm1.bmWidthBytes + x * PxBytes;
//RGB 以背景色乘不透明色70%
px1[rgb_b] = px1[rgb_b] * 0.7;
px1[rgb_b + 1] = px1[rgb_b + 1] * 0.7;
px1[rgb_b + 2] = px1[rgb_b + 2] * 0.7;
}
}
//处理前景图像像素颜色
for (y = 0; y<(bm2.bmHeight); y++)
{
for (x = 0; x<bm2.bmWidth; x++)
{
rgb_b = y * bm2.bmWidthBytes + x * PxBytes;
i = (ystart + y) * bm1.bmWidthBytes + (xstart + x) * PxBytes;
//依次将像素的RGB色彩值乘前景图的不透明度 30%,并与处理过的背景色彩值相加,便完成了半透明色彩的合成
px2[rgb_b] = px2[rgb_b] * 0.3 + px1[i];
px2[rgb_b + 1] = px2[rgb_b + 1] * 0.3 + px1[i + 1];
px2[rgb_b + 2] = px2[rgb_b + 2] * 0.3 + px1[i + 2];
}
}
//重设前景图的颜色(完成半透明)
SetBitmapBits(girl, bm2.bmHeight*bm2.bmWidthBytes, px2);
MyPaint(hdc);
//释放DC及内存DC
ReleaseDC(hwnd, hdc);
delete[] px1;
delete[] px2;
return TRUE;
}
//--------------------------------------------MyPaint() 函数---------------------------------------------------
//绘制图形
//-------------------------------------------------------------------------------------------------------------
void MyPaint(HDC hdc)
{
//贴上背景图
SelectObject(mdc, bg);
BitBlt(hdc, 0, 0, 800, 600, mdc, 0, 0, SRCCOPY);
//贴上处理过的半透明图
SelectObject(mdc, girl);
BitBlt(hdc, xstart, ystart, 300, 350, mdc, 0, 0, SRCCOPY);
}
//--------------------------------------------Game_CleanUp() 函数----------------------------------------------
//资源清理函数,在此函数中进行退出前的资源的清理工作
//-------------------------------------------------------------------------------------------------------------
BOOL Game_CleanUp(HWND hwnd)
{
DeleteDC(mdc);
DeleteObject(bg);
DeleteObject(girl);
return TRUE;
}
如需整体运行框架,请点击: 游戏框架
游戏开发的梦想
梦开始的地方