因为bmp图片上传不了,只能截图啦

 


//全局变量
HDC mdc;
HBITMAP fullmap;//声明位图对象,在初始化函数中完成的斜角地图会保存在这个位图中
const int rows = 10, cols = 10;

 


//--------------------------------------------InitInstance() 函数----------------------------------------------
//加载资源
//-------------------------------------------------------------------------------------------------------------
BOOL InitInstance(HINSTANCE hInstance, int nShowCmd)
{
HWND hwnd;
HDC hdc, tmpdc;

//创建窗口第三步:正式创建窗口
//创建窗口函数
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);//将窗口绘制于显示设备上

int mapIndex[rows * cols] =
{
0, 1, 2, 2, 0, 1, 0, 1, 2, 0,
0, 1, 2, 0, 3, 0, 1, 1, 1, 2,
2, 0, 0, 3, 3, 0, 0, 1, 1, 2,
2, 0, 0, 3, 1, 0, 2, 2, 1, 3,
2, 2, 0, 0, 2, 2, 0, 0, 1, 2,
0, 2, 2, 2, 2, 0, 0, 1, 1, 2,
0, 2, 2, 2, 2, 2, 3, 1, 1, 3,
0, 2, 2, 2, 2, 2, 3, 1, 1, 2,
0, 2, 2, 2, 2, 3, 1, 1, 1, 2
};
int sceneIndex[rows*cols] = {
0, 2, 2, 0, 2, 0, 1, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
0, 0, 1, 0, 1, 0, 0, 0, 2, 0,
2, 2, 0, 0, 1, 0, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

hdc = GetDC(hwnd);
mdc = CreateCompatibleDC(hdc);
tmpdc = CreateCompatibleDC(hdc);

HBITMAP map[4], scene[2];
char mapname[20] = "";
//设定第一个图块的起始坐标,设定x轴上的坐标xstart 是将图块放在整个拼接地图的中间位置
int xstart = 32 * (rows - 1), ystart = 0;
//加载背景图,并选用到mdc中
fullmap = (HBITMAP)LoadImage(NULL, "bg.bmp", IMAGE_BITMAP, 800, 600, LR_LOADFROMFILE);
//将fullmap存入mdc中
SelectObject(mdc, fullmap);

//加载各图块图像
for (int i = 0; i < 4; i++)
{
//取得各个图块并存于"map[i]"中
wsprintf(mapname, "map%d.bmp", i);
map[i] = (HBITMAP)LoadImage(NULL, mapname, IMAGE_BITMAP, 128, 32, LR_LOADFROMFILE);
}
//加载景物的图像
for (int i = 0; i < 2; i++)
{
wsprintf(mapname, "scene%d.bmp", i + 1);
scene[i] = (HBITMAP)LoadImage(NULL, mapname, IMAGE_BITMAP, 100, 60, LR_LOADFROMFILE);
}

//按照mapIndex数组的定义取出对应的图块,进行地图拼接
for (int i = 0; i < rows * cols; i++)
{
int x, y;
//根据 mapIndex[i] 中的代号选取对应的图块到tmpdc中
SelectObject(tmpdc, map[mapIndex[i]]);
//i / cols;//求列编号
//i % cols;//求行编号
x = xstart + i % cols * 32 + i / cols *(-32);//求贴图x坐标
y = ystart + i / cols * 16 + i % cols * 16;//求贴图y坐标
//在mdc进行透明贴图
BitBlt(mdc, x, y, 64, 32, tmpdc, 64, 0, SRCAND);
BitBlt(mdc, x, y, 64, 32, tmpdc, 0, 0, SRCPAINT);

//景物的透明贴图
switch (sceneIndex[i])
{
case 1:
SelectObject(tmpdc, scene[0]);
BitBlt(mdc, x + 7, y - 44, 50, 60, tmpdc, 50, 0, SRCAND);
BitBlt(mdc, x + 7, y - 44, 50, 60, tmpdc, 0, 0, SRCPAINT);
break;
case 2:
SelectObject(tmpdc, scene[1]);
BitBlt(mdc, x + 7, y - 30, 50, 60, tmpdc, 50, 0, SRCAND);
BitBlt(mdc, x + 7, y - 30, 50, 60, tmpdc, 0, 0, SRCPAINT);
break;
}

}

MyPaint(hdc);

ReleaseDC(hwnd, hdc);
DeleteDC(tmpdc);

return TRUE;
}

//--------------------------------------------MyPaint() 函数---------------------------------------------------
//绘制图形
//-------------------------------------------------------------------------------------------------------------
void MyPaint(HDC hdc)
{
//背景图显示
SelectObject(mdc, fullmap);
BitBlt(hdc, 0, 0, 800, 600, mdc, 0, 0, SRCCOPY);
}


//--------------------------------------------Game_CleanUp() 函数----------------------------------------------
//资源清理函数,在此函数中进行退出前的资源的清理工作
//-------------------------------------------------------------------------------------------------------------
BOOL Game_CleanUp(HWND hwnd)
{
DeleteDC(mdc);
DeleteObject(fullmap);
return TRUE;
}

 

如需整体运行框架,请点击: 游戏框架

游戏开发的梦想

梦开始的地方

 

posted on 2015-11-22 18:44  zishen  阅读(520)  评论(1编辑  收藏  举报