Graphic map of Visual C + + game programming foundation

I. Basic Ideas

1. The map is composed of 3 * 4 blocks, 4 blocks in column direction and 3 blocks in row direction. There are 3 blocks of different colors here;

2. Three different blocks are represented by 0, 1 and 2 respectively, and map array is established, with 8 rows and 8 columns, and initialized with 0, 1 and 2

3. Create a blank bitmap and save it to mdc. Cycle the whole map array. From the first one, take out the corresponding map according to the array value (0 or 1 or 2) and save it to memory bufdc

4. Calculate the row and column number according to the index value, calculate the mapping coordinates, and paste them into mdc one by one, mdc finally paste them into hdc;

Formula: column number = index value / number of blocks in each column

Row number = index value% number of blocks per column

II. The code is as follows

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

HINSTANCE hInst;
HBITMAP fullmap;//bitmap handle to store the completed map in the initialization function
HDC		mdc;   //Memory DC handle 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", "Drawing window" , 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,  //Material 1
								0,2,2,0,0,0,1,1,  //Material 2
								0,0,0,0,0,0,0,1,  //Material 3
								2,0,0,0,0,0,2,2,  //Material 4
								2,0,0,0,0,2,2,2,  //Material 5
								2,0,0,0,2,2,0,0,  //Material 6
								0,0,2,2,2,0,0,1,  //Material 7
								0,0,2,0,0,0,1,1 }; //Material 8
	hdc = GetDC(hWnd);
	mdc = CreateCompatibleDC(hdc);
	bufdc = CreateCompatibleDC(hdc);
	fullmap = CreateCompatibleBitmap(hdc,cols*50,rows*50);//Number of rows * width of block; number of columns * height of block, create a blank bitmap fullmap

	SelectObject(mdc,fullmap);//Save fullmap to 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);//Save the filename of the picture to filename by using the loop
		map[i] = (HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
	}

	for (i=0;i<rows*cols;i++)
	{
		SelectObject(bufdc,map[mapIndex[i]]);//Save block to bufdc

		rowNum = i / cols;   //Calculated column number
		colNum = i % cols;   //Calculation line number
		x = colNum * 50;     //Upper left X coordinate: line number * block width
		y = rowNum * 50;     //Y coordinate of upper left corner: column number * block height

		BitBlt(mdc,x,y,50,50,bufdc,0,0,SRCCOPY);//Paste the map on 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;
}

Three, effect

Posted on Sat, 02 Nov 2019 00:53:45 -0700 by PHP_PhREEEk