游戏编程基础004---斜角地图贴图

代码地址:http://download.csdn.net/detail/liu_liu213/4013217

 

/************************************************************************/
/* 
	列编号 = 索引值 / 每一列图块个数
	行编号 = 索引值 % 每一列的图块个数

	菱形的宽和高分别是w和h
	xstart与ystart代表第一张图块左上角贴图坐标
	左上点X坐标 = xstart + 行编号 * w/2 
	左上点Y坐标 = ystart + 列编号 * h/2 

	地图宽 = (列数+行数)* w/2
	地图高 = (列数+行数)* h/2

*/
/************************************************************************//


#include "stdafx.h"

//全局变量声明
HINSTANCE hInst;
HBITMAP fullmap;
HDC		mdc;
const int rows = 10,cols = 10;

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

//****程序入口**************************************
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,600,450,true);
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	int mapIndex[rows*cols] = { 2,2,2,2,2,0,1,0,1,0,  //第1列
		3,3,2,2,0,0,0,1,1,0,  
		3,0,0,0,0,0,0,0,1,2,  
		2,2,0,0,0,0,0,2,2,2,  
		2,2,0,0,0,0,2,2,2,2, 
		2,2,0,0,0,2,2,0,0,2,  
		2,0,0,2,2,2,0,0,1,0,  
		0,0,2,0,0,0,1,1,1,1,  
		0,2,0,3,3,3,3,3,3,1,  
		2,0,3,3,3,3,3,3,3,3 };//第10列

	hdc = GetDC(hWnd);
	mdc = CreateCompatibleDC(hdc);
	bufdc = CreateCompatibleDC(hdc);

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

	xstart = 32 * (rows-1);
	ystart = 0;

	fullmap = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE); 
	SelectObject(mdc,fullmap);

	//加载各图块位图
	for(i=0;i<4;i++)
	{
		sprintf(filename,"map%d.bmp",i);
		map[i] = (HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,128,32,LR_LOADFROMFILE);
	}

	//安装mapIndex数组中定义取出对已图块
	for (i=0;i<rows*cols;i++)
	{
		SelectObject(bufdc,map[mapIndex[i]]);

		rowNum = i / cols;     //求列编号
		colNum = i % cols;     //行编号
		x = xstart + colNum * 32 + rowNum * (-32);  //贴图X坐标
		y = ystart + rowNum * 16 + colNum * 16;     //贴图Y坐标

		BitBlt(mdc,x,y,64,32,bufdc,64,0,SRCAND);
		BitBlt(mdc,x,y,64,32,bufdc,0,0,SRCPAINT);
	}

	MyPaint(hdc);

	return TRUE;
}

void MyPaint(HDC hdc)
{
	//SelectObject(mdc,fullmap);
	BitBlt(hdc,0,0,640,480,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);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:	
			DeleteDC(mdc);
			DeleteObject(fullmap);
			PostQuitMessage(0);
			break;
		default:							
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值