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

📄 dxdw.cpp

📁 本程序表现了一个草地在天空飞行的情景本程序是系列程序的第四步。
💻 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];
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;


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;
}

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<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->ClearBuf();  
    this->BltMan(); 
	this->Flip(); 
}

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::ClearBuf()
{
	HDC tmpDC;
	DDBuf->GetDC(&tmpDC);
	::BitBlt(tmpDC,0,0,640,480,hdc,0,0,BLACKNESS); 
	DDBuf->ReleaseDC(tmpDC);
}


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;
	if (keydown) 
	{
		rowindex++;
		if (rowindex>2) rowindex=0;
		switch (manindex)
		{
		case 1: 
			manY--;
			break;
		case 2:
			manX++;
			break;
		case 3:
			manY++;
			break;
		case 4:
			manX--;
			break;
		}
	}

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

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()
{
    result = CreateBMP(72,136,"man.bmp",DDman);
	if(result !=DD_OK)
	{
		MessageBox(NULL,"建立 man.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 (int 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 + -