⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dxdw.cpp

📁 使用VC中的Direct Draw 开发的45度斜角地图
💻 CPP
字号:

#include "DXDW.h"
#include <string.h>
#include <stdio.h>
#include "FPS.h"


LPDIRECTDRAW7              DD;  
LPDIRECTDRAWSURFACE7       DDPri; 
LPDIRECTDRAWSURFACE7       DDBuf; 
int						   mapSum = 5;
LPDIRECTDRAWSURFACE7	   DDtmp[5];
LPDIRECTDRAWSURFACE7	   DDman;
LPDIRECTDRAWSURFACE7	   DDnogo[1];
LPDIRECTDRAWSURFACE7	   c[3];
DDSCAPS2				   DDcaps;
DDSURFACEDESC2             DDde;
DDCOLORKEY                 key;

HRESULT					   result;

HWND tWnd;
HDC hdc,dhdc,winhdc;
HBITMAP bitmap;


//10*15
int locate = 8;
int numMap = locate*8;
int mapIndex[64] = {	1,3,2,2,0,0,1,3, //1
						0,1,2,0,0,0,0,0, //2
						0,0,1,0,1,0,0,0, //3
						0,0,0,1,0,0,0,2, //4
						0,0,0,0,0,0,0,0, //5
						0,0,0,0,0,0,0,0, //6
						0,0,2,2,2,0,1,0, //7
						0,0,2,0,0,0,1,0, //8
					};
int nogoIndex[64] = {
						1,0,0,0,0,0,0,0, //1
						0,0,0,0,0,0,0,0, //2
						0,1,0,0,0,0,0,0, //3
						0,0,1,0,0,0,0,0, //4
						0,0,0,0,0,0,0,0, //5
						0,0,1,0,0,0,0,0, //6
						0,0,0,0,0,0,0,0, //7
						0,0,1,0,0,1,0,0, //8
					}; 

int offset_x = 320, offset_y = 128;

bool fullscreen;
FPS_CLASS fps;

int manX,manY;
int manindex_x=0,manindex_y=0;

tmpXYc cman[3];


DX::DX()
{
	int i;
	DD = NULL;
	DDPri = NULL;
	DDBuf = NULL;
	for(i=0;i<mapSum;i++)
		DDtmp[i] = NULL; 
	for(i=0;i<1;i++)
		DDnogo[i] = NULL; 
	hdc = ::CreateCompatibleDC(NULL);
	manindex = 1;
	rowindex = 0;
	keydown = false;
	manX = (320+32)-12;
	manY = 128-16;

	cman[0].index = 0; cman[0].x = 5; cman[0].y = 5; cman[0].rowindex = 0; cman[0].manindex = 0;
	cman[1].index = 1; cman[1].x = 4; cman[1].y = 6; cman[1].rowindex = 0; cman[1].manindex = 0;
	cman[2].index = 2; cman[2].x = 2; cman[2].y = 3; cman[2].rowindex = 0; cman[2].manindex = 0;
}

DX::~DX()
{
	/*
	delete hdc;
	delete dhdc;
	delete bitmap;
	*/

	int i;

	ReleaseDC(tWnd,winhdc);

	for(i=0;i<mapSum;i++)
	{
		if (DDtmp[i])
		{
			DDtmp[i]->Release(); 
		}
	}
	for(i=0;i<3;i++)
	{
		if (c[i])
		{
			c[i]->Release(); 
		}
	}
	for(i=0;i<1;i++)
	{
		if (DDnogo[i])
		{
			DDnogo[i]->Release(); 
		}
	}
	if (DDBuf) DDBuf->Release();
    if (DDPri) DDPri->Release(); 
	if (DD) DD->Release();
}

void DX::Render()
{ 
	fps.GetFps();
    this->BltBack(); 
	this->BltMap(); 
    //this->BltMan(); 
	this->BltTree(); 
	this->Bltc(); 
	this->Flip(); 
	//TextOut(winhdc,0,0,fps.fpsString,255);
}

void DX::Flip()
{
	if (!fullscreen)
	{
		DDPri->BltFast(0,0,DDBuf,&this->tRect(0,0,640,480),DDBLTFAST_WAIT);
	}else
	{
		DDPri->Flip(NULL,DDFLIP_WAIT);
	}    
}

void DX::BltBack()
{
	static x;
	static DWORD t2;
	if (fps.T2 - t2 > 1) 
	{
		t2 = fps.T2 ;
		x+=2; if (x>640) x=0;
	}
    DDBuf->BltFast(0,0,DDtmp[0],&this->tRect(x,0,640,480),DDBLTFAST_WAIT); 
	DDBuf->BltFast(640-x,0,DDtmp[0],&this->tRect(0,0,x,480),DDBLTFAST_WAIT); 
}

void DX::BltMap()
{
	for (int i=0;i<numMap;i++)
	{
		 int tx=0,ty=0;
		 int tw0,th0,tw1,th1;

		 this->Mapmath(i,tx,ty) ;

		 tw0 = 0; th0 = 0;
		 tw1 = 64; th1 = 32;

		 DDBuf->BltFast(tx,ty,DDtmp[mapIndex[i]+1],
				 &this->tRect(tw0,th0,tw1,th1),DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);  
	} 
}

void DX::Mapmath(int index,int &tx,int &ty)
{
	int x_index,y_index;
	int doux,douy;
	int sc_x,sc_y;

	x_index = index%locate;
	y_index = index/locate;
	if (y_index%2 == 1) doux = 32; else doux = 0;
	if (y_index != 0) douy = -16; else douy = 0;
    sc_x = offset_x - y_index*32;
	sc_y = offset_y + y_index*16;
	tx = sc_x + x_index*32;
	ty = sc_y + x_index*16;
	
}

void DX::BltMan()
{
	int tw0,th0,tw1,th1;
	tw0=0+rowindex*24;th0=0+(manindex-1)*34;
	tw1=24+rowindex*24;th1=34+(manindex-1)*34;
	int tmpindex_x = manindex_x ,tmpindex_y = manindex_y;
	if (keydown) 
	{
		keydown = false;
		rowindex++;
		if (rowindex>2) rowindex=0;
		switch (manindex)
		{
		case 1: 
			manindex_y-=1;
			if (manindex_y<0) manindex_y=0;
			break;
		case 2:
			manindex_x+=1;
			if (manindex_x>locate-1) manindex_x=locate-1;
			break;
		case 3:
			manindex_y+=1;
			if (manindex_y>7) manindex_y=7;
			break;
		case 4:
			manindex_x-=1;
			if (manindex_x<0) manindex_x=0;
			break;
		}
	}

	if (nogoIndex[manindex_x+manindex_y*locate]!=0) 
	{
		manindex_x = tmpindex_x;
		manindex_y = tmpindex_y;
	}

	this->Mapmath(manindex_x+manindex_y*locate,manX,manY);
	manX+=(32-24/2);
	manY-=16;

    DDBuf->BltFast(manX,manY,DDman,
				 &this->tRect(tw0,th0,tw1,th1),DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY); 
}

void DX::Bltc()
{
	int tw0,th0,tw1,th1;
	int tmpindex_x,tmpindex_y;
	for (int i=0;i<3;i++)
	{
		tmpindex_x = cman[i].x ,tmpindex_y = cman[i].y;
		tw0=0+cman[i].rowindex*32;th0=0+(cman[i].manindex)*32;
		tw1=32+cman[i].rowindex*32;th1=32+(cman[i].manindex)*32;

		if (fps.T2 - cman[i].t2 > (DWORD)cman[i].DelayT2)
		{
			cman[i].DelayT2 = timeGetTime() % 100*(timeGetTime()%5+3);
			cman[i].t2 = fps.T2; 
		
			cman[i].rowindex++;
			if (cman[i].rowindex>1) cman[i].rowindex=0;

			cman[i].manindex = timeGetTime() % 5;

			switch (cman[i].manindex)
			{
			case 1: 
				cman[i].y-=1;
				if (cman[i].y<0) cman[i].y=0;
				break;
			case 2:
				cman[i].y+=1;
				if (cman[i].y>7) cman[i].y=7;
				break;
			case 3:
				cman[i].x-=1;
				if (cman[i].x<0) cman[i].x=0;
				break;
			case 4:
				cman[i].x+=1;
				if (cman[i].x>locate-1) cman[i].x=locate-1;
				break;
			}
		}


		if (nogoIndex[cman[i].x+cman[i].y*locate]!=0) 
		{
			cman[i].x = tmpindex_x;
			cman[i].y = tmpindex_y;
		}
		int cmanx=0,cmany=0;

		this->Mapmath(cman[i].x+cman[i].y*locate,cmanx,cmany);
		cmanx+=(32-32/2);
		cmany-=16;

		DDBuf->BltFast(cmanx,cmany,c[i],
					 &this->tRect(tw0,th0,tw1,th1),DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY); 
	}

	
}

void DX::BltTree()
{
	int i;
	int x_index,y_index;
	int doux,douy;
	int tx,ty;
	int tw0,th0,tw1,th1;
	int sc_x,sc_y;

	int sortindex=0;
	tmpXY *a = new tmpXY [numMap];
	for (i=0;i<numMap;i++)
	{
		a[i].index = -1;
		a[i].x = -1;
		a[i].y = -1;
		if (nogoIndex[i] !=0 )
		{
		 x_index = i%locate;
		 y_index = i/locate;
		 if (y_index%2 == 1) doux = 32; else doux = 0;
		 if (y_index != 0) douy = -16; else douy = 0;
         sc_x = offset_x - y_index*32+16;
		 sc_y = offset_y + y_index*16-24;
		 tx = sc_x + x_index*32;
		 ty = sc_y + x_index*16;
		 tw0 = 0; th0 = 0;
		 tw1 = 32; th1 = 40;
		 a[sortindex].index = i;
		 a[sortindex].x = tx;
		 a[sortindex].y = ty+40;
		 sortindex++;
		}
	}

    a[sortindex].index = 99;
	a[sortindex].x = manX;
    a[sortindex].y = manY+34;

	this->sortXY(a); 

	for (i=0;i<numMap;i++)
	{
		 if (a[i].index == -1) break;
         if (a[i].index != 99)
		 {
			DDBuf->BltFast(a[i].x,a[i].y-40,DDnogo[0],
					 &this->tRect(tw0,th0,tw1,th1),DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);   
		 }else
		 {
            this->BltMan(); 
		 }
	}
}

void DX::sortXY(tmpXY *txy) 
{
	tmpXY mxy;

	for (int i=0;i<numMap;i++)
	{
		if (txy[i].index != -1)
		{
			for(int j=i+1;j<numMap;j++)
			{
				if ( (txy[i].y > txy[j].y) && (txy[j].index != -1) )
				{
					mxy = txy[i];
					txy[i] = txy[j];
					txy[j] = mxy;
				}
			}
		}
	}
}

HRESULT DX::InitDX(HWND thWnd,bool tfullscreen)
{
	fullscreen = tfullscreen;
	tWnd = thWnd;
	winhdc = GetDC(tWnd);

	result = DirectDrawCreateEx(NULL, (VOID**)&DD, IID_IDirectDraw7, NULL);
    if (result != DD_OK)
	{
		MessageBox(NULL,"建立DirectDraw对象失败!","失败!",MB_OK);
		return result;
	}

	if (!tfullscreen)
	{
	      result = DD->SetCooperativeLevel(thWnd,DDSCL_NORMAL);
	}else
	{
	      result = DD->SetCooperativeLevel(thWnd,DDSCL_EXCLUSIVE |
		                                 DDSCL_FULLSCREEN|DDSCL_ALLOWREBOOT );
	}
	if(result != DD_OK)
	{
		MessageBox(NULL,"设定程序协调层级失败!","失败!",MB_OK);
		return result;
	}


	if (tfullscreen)
	{
		result = DD->SetDisplayMode(640,480,16,0,DDSDM_STANDARDVGAMODE); 
		if(result !=DD_OK)
		{
			MessageBox(NULL,"设定屏幕显示模式失败!","失败!",MB_OK);
			return result;
		}
	}


	memset(&DDde,0,sizeof(DDde));       
	DDde.dwSize = sizeof(DDde);                   
	if (tfullscreen)
	{
		DDde.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
		DDde.dwBackBufferCount = 1;
		DDde.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
	}else
	{
		DDde.dwFlags = DDSD_CAPS;
		DDde.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
	}

	result = DD->CreateSurface(&DDde,&DDPri,NULL);
	if(result !=DD_OK)
	{
		MessageBox(NULL,"建立主绘图页失败!","失败!",MB_OK);
		return result;
	}


	if (tfullscreen)
	{
		DDcaps.dwCaps = DDSCAPS_BACKBUFFER;  
		result = DDPri->GetAttachedSurface(&DDcaps,&DDBuf); 
	}else
	{
		memset(&DDde,0,sizeof(DDde));        
		DDde.dwSize = sizeof(DDde);
		DDde.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; 
		DDde.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN ; 
		DDde.dwWidth = 640;
		DDde.dwHeight = 480;
		result = DD->CreateSurface(&DDde,&DDBuf,NULL);
	}
	if(result !=DD_OK)
	{
		MessageBox(NULL,"连接后缓冲区失败!","失败!",MB_OK);
		return result;
	}

	return result;

	/*

	HINSTANCE hinst = AfxGetInstanceHandle(); 
	result = DirectInputCreateEx(hinst, DIRECTINPUT_VERSION,IID_IDirectInput7, (void**)&DI, NULL); 
	if(result != DI_OK)
	{
		MessageBox("建立 DirectInput 对象失败!");
		return;
	}
 
	result = DI->CreateDeviceEx(GUID_SysMouse, IID_IDirectInputDevice7,(void**)&DIms, NULL); 
	if(result != DI_OK)
	{
		MessageBox("建立鼠标输入设备失败!");
		return;
	}

	result = DIms->SetDataFormat(&c_dfDIMouse2);
	if(result != DI_OK)
	{
		MessageBox("设定数据格式失败!");
		return;
	}

	result = DIms->SetCooperativeLevel(m_hWnd,DISCL_BACKGROUND | DISCL_NONEXCLUSIVE); 
	if(result != DI_OK)
	{
		MessageBox("设定程序协调层级失败!");
		return;
	}

	result = DIms->Acquire();
	if(result != DI_OK)
	{
		MessageBox("取用输入设备失败!");
		return;
	}
	*/
}

HRESULT DX::InitBmp()
{
	int i;
    result = CreateBMP(72,136,"man.bmp",DDman);
	if(result !=DD_OK)
	{
		MessageBox(NULL,"建立 man.bmp 暂存区失败!","失败!",MB_OK);
		return result;
	}

	result = CreateBMP(64,160,"c0.bmp",c[0]);
	if(result !=DD_OK)
	{
		MessageBox(NULL,"建立 c0.bmp 暂存区失败!","失败!",MB_OK);
		return result;
	}

	result = CreateBMP(64,160,"c1.bmp",c[1]);
	if(result !=DD_OK)
	{
		MessageBox(NULL,"建立 c1.bmp 暂存区失败!","失败!",MB_OK);
		return result;
	}

	result = CreateBMP(64,160,"c2.bmp",c[2]);
	if(result !=DD_OK)
	{
		MessageBox(NULL,"建立 c2.bmp 暂存区失败!","失败!",MB_OK);
		return result;
	}

	result = CreateBMP(32,40,"tree.bmp",DDnogo[0]);
	if(result !=DD_OK)
	{
		MessageBox(NULL,"建立 tree.bmp 暂存区失败!","失败!",MB_OK);
		return result;
	}


	for (i=0;i<mapSum;i++)
	{
		int tw,th;
		char tfilename[255];
		if (i==0)
		{
			strcpy(tfilename,"back.bmp");
			tw = 640; th=480; 
		}else
		{
			tw = 64; th = 32;
			switch (i)
			{
			case 1:
				strcpy(tfilename,"map0.bmp");
				break;
			case 2:
				strcpy(tfilename,"map1.bmp");
				break;
			case 3:
				strcpy(tfilename,"map2.bmp");
				break;
			case 4:
				strcpy(tfilename,"map3.bmp");
				break;
			}
		}

	    result = CreateBMP(tw,th,tfilename,DDtmp[i]);
		if(result !=DD_OK)
		{
			MessageBox(NULL,"建立幕后暂存区失败!","失败!",MB_OK);
			return result;
		}
	}

	return result;
}

HRESULT DX::CreateBMP(int width,int height,char* filename,LPDIRECTDRAWSURFACE7 &tsurf)
{
	DDde.dwWidth = width; 
	DDde.dwHeight = height; 
	result = DD->CreateSurface(&DDde, &tsurf, NULL); 


	DDCOLORKEY dcolor;
	dcolor.dwColorSpaceHighValue = 0x00ff00;
	dcolor.dwColorSpaceLowValue  = 0x00ff00;

	tsurf->SetColorKey(DDCKEY_SRCBLT,&dcolor);

	if(result !=DD_OK)
	{
		return result;
	}

	bitmap = (HBITMAP)::LoadImage(NULL,filename,IMAGE_BITMAP,width,height,LR_LOADFROMFILE); 
	if(bitmap==NULL)
	{
		MessageBox(NULL,"无法载入位图!","失败!",MB_OK);
		return result;
	}

	::SelectObject(hdc,bitmap);
	tsurf->GetDC( &dhdc );
	::BitBlt( dhdc , 0 , 0 ,width,height, hdc , 0 , 0 , SRCCOPY );
	tsurf->ReleaseDC(dhdc);

	return result;
}

RECT DX::tRect(int left,int top,int right,int bottom)
{
	RECT tr;
	tr.left = left;
	tr.top = top;
	tr.right = right;
	tr.bottom = bottom;
    return tr;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -