【游戏程序设计】地图拼接

运行结果:

【游戏程序设计】地图拼接

源代码:

#include <windows.h>
#include <tchar.h>
#pragma comment(lib, "winmm.lib")									//调用PlaySound函数所需库文件t

#define WINDOW_WIDTH 450
#define WINDOW_HEIGHT 450
#define WINDOW_TITLE L"地图拼接"
const int rows = 8;
const int cols = 8;

HINSTANCE hInst;
HDC hdc;		//画布
HDC mdc;
HBITMAP fullmap;
int  MyWindowClass(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyDraw(HDC);

/*****************************************************************************************************************
在不同的应用程序中,在此处添加相关的全局变量
******************************************************************************************************************/
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPreInstace,
									LPSTR lpCmdLine, int nCmdShow) 
{
	MyWindowClass(hInstance);
	PlaySound(L"sound.wav", NULL, SND_FILENAME| SND_ASYNC| SND_LOOP);	//循环播放背景音乐
	if(!InitInstance(hInstance, nCmdShow))
		return FALSE;
	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

int MyWindowClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = NULL;
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = L"gamebase";
	wcex.hIconSm = NULL;

	return RegisterClassEx(&wcex); 
}

/*****************************************************************************************************
声明地图数组,进行图块贴图,完成地图拼接
*****************************************************************************************************/
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	hInst = hInstance;
	HWND hwnd = CreateWindow(L"gamebase", WINDOW_TITLE, WS_OVERLAPPEDWINDOW, 
		CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
	if(!hwnd)
		return FALSE;
	MoveWindow(hwnd, 10, 10, WINDOW_WIDTH, WINDOW_HEIGHT, true);
	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

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

	HDC bufdc;
	hdc = GetDC(hwnd);
	mdc = CreateCompatibleDC(hdc);
	bufdc = CreateCompatibleDC(hdc);													//地图块句柄
	fullmap = CreateCompatibleBitmap(hdc, rows*50, cols*50);							// 形成位图
	SelectObject(mdc, fullmap); 
	
	HBITMAP map[3];
	wchar_t filename[20];
	
	//加载表示小地图的位图
	for(int i = 0; i != 3; ++i)
	{
		swprintf_s(filename, L"map%d.bmp", i); 
		map[i] = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 50, 50, LR_LOADFROMFILE);
	}
	//计算数组中对应的小地图位置
	for(int i = 0; i != rows; ++i)
		for(int j = 0; j != cols; ++j)
		{
			SelectObject(bufdc, map[mapIndex[i][j]]);
			int x = i * 50;																		//求小地图的X坐标
			int y = j * 50;																		//求小地图的Y坐标
			BitBlt(mdc, x, y, 50, 50, bufdc, 0, 0, SRCCOPY);
		}
	SelectObject(mdc, fullmap);
	MyDraw(hdc); 
	ReleaseDC(hwnd, hdc);  
	DeleteDC(bufdc);
	for(int i = 0; i != 3; ++i)
		DeleteObject(map[i]);
	return TRUE;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;

	switch(message)
	{
	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		MyDraw(hdc);
		EndPaint(hwnd, &ps);
		break;
/**************************************************************************************************************
在退出程序前,往往在此处删除创建的相关资源
***************************************************************************************************************/
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
	return 0;
}
/***************************************************************************************************************
在函数MyDraw()中进行相关绘制工作
****************************************************************************************************************/
void MyDraw(HDC hdc)
{
	BitBlt(hdc, 10, 10, rows*50, cols*50, mdc, 0, 0, SRCCOPY);
}