Flat-screen programming based on the Visual C ++ game map map

First, the basic idea

1. The map consists of 4 * 3 tile composition, four in the column direction, the row direction 3, there are three different colors of tiles;

2. The three different tile 0,1,2 respectively, said the establishment of the map array, 8 rows and 8 columns, initialized with these three numbers 0,1,2

3. Establish blank bitmap stored to MDC, the entire cycle map array, starting from the first, (0 or 1 or 2) extracted from the array map corresponding to the value stored in the memory bufdc

4. Calculate row number according to the index value to calculate mapping coordinates, and attached to a one of the mdc, paste and finally to mdc HDC;

   Formula: column number = index / the number of tiles of each column

              The number of row number = column index value for each tile%

Second, the code is as follows


#include "stdafx.h"
#include <stdio.h>

HINSTANCE hInst;
HBITMAP fullmap;//bitmap handle,存储初始化函数中完成的地图
HDC		mdc;   //内存DC句柄mdc

const int rows = 8,cols = 8;

ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
void				MyPaint(HDC hdc);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	MSG msg;

	MyRegisterClass(hInstance);

	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	
	return msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

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

	return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;
	HDC hdc,bufdc;

	hInst = hInstance;

	hWnd = CreateWindow("canvas", "绘图窗口" , WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{
		return FALSE;
	}

	MoveWindow(hWnd,10,10,430,450,true);
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	
	int mapIndex[rows*cols] = { 2,2,2,2,0,1,0,1,  //材1
								0,2,2,0,0,0,1,1,  //材2
								0,0,0,0,0,0,0,1,  //材3
								2,0,0,0,0,0,2,2,  //材4
								2,0,0,0,0,2,2,2,  //材5
								2,0,0,0,2,2,0,0,  //材6
								0,0,2,2,2,0,0,1,  //材7
								0,0,2,0,0,0,1,1 }; //材8
	hdc = GetDC(hWnd);
	mdc = CreateCompatibleDC(hdc);
	bufdc = CreateCompatibleDC(hdc);
	fullmap = CreateCompatibleBitmap(hdc,cols*50,rows*50);//行数*图块宽;列数*图块高,建立空白位图fullmap

	SelectObject(mdc,fullmap);//将fullmap存入到mdc中

	HBITMAP map[3];
	char filename[20] = "";
	int rowNum,colNum;
	int i,x,y;

	for(i=0;i<3;i++)
	{
		sprintf(filename,"map%d.bmp",i);//利用循环把图片的文件名存到filename
		map[i] = (HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
	}

	for (i=0;i<rows*cols;i++)
	{
		SelectObject(bufdc,map[mapIndex[i]]);//把图块存入到bufdc

		rowNum = i / cols;   //计算列编号
		colNum = i % cols;   //计算行编号
		x = colNum * 50;     //左上角X坐标:行编号*图块宽度
		y = rowNum * 50;     //左上角Y坐标:列编号*图块高度

		BitBlt(mdc,x,y,50,50,bufdc,0,0,SRCCOPY);//把地图贴到mdc上
	}

	MyPaint(hdc);

	ReleaseDC(hWnd,hdc);
	DeleteDC(bufdc);

	return TRUE;
}

void MyPaint(HDC hdc)
{
	SelectObject(mdc,fullmap);
	BitBlt(hdc,10,10,cols*50,rows*50,mdc,0,0,SRCCOPY);
}

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

	switch (message)
	{
		case WM_PAINT:						
			hdc = BeginPaint(hWnd, &ps);
			MyPaint(hdc);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:				
			DeleteDC(mdc);
			DeleteObject(fullmap);
			PostQuitMessage(0);
			break;
		default:						
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

Third, the effect

Guess you like

Origin blog.csdn.net/Sruggle/article/details/90723242
Map
map
map
Map