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

📄 canvasframe.cpp

📁 《Visual C++游戏设计入门》的配套代码
💻 CPP
字号:
// canvasFrame.cpp : implementation file
//

#include "stdafx.h"
#include "canvasr.h"
#include "canvasFrame.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// canvasFrame

IMPLEMENT_DYNCREATE(canvasFrame, CFrameWnd)

LPDIRECTDRAW7              pDD7;  //声明DirectDraw对象
LPDIRECTDRAWSURFACE7       pPSur; //声明主绘图页
LPDIRECTDRAWSURFACE7       pBBuf; //声明后缓冲区
LPDIRECTDRAWSURFACE7	   pOPla[8];
DDSCAPS2  caps;                   //声明DDSCAPS结构
DDSURFACEDESC2             desc;  //声明描述结构
HRESULT result;                   //声明HRESULT类型变量
DDCOLORKEY key;					  //声明颜色键
int i;
LPDIRECTSOUND pDS;          //声明 DirectSound 对象指针
LPDIRECTSOUNDBUFFER pPBuf;  //声明主缓冲区指标
LPDIRECTSOUNDBUFFER pSBuf[2];  //声明次缓冲区
WAVEFORMATEX pwfmt;         //声明声音结构
WAVEFORMATEX swfmt;         //声明声音结构
DSBUFFERDESC dsdesc;          //声明描述结构
MMCKINFO      ckRiff;       //RIFF 区块的信息  
MMCKINFO      ckInfo;       //子区块的信息
MMRESULT	  mmresult;     //传回结果
HMMIO hmmio;                //开启的多媒体文件
DWORD size;                 //实际数据大小
LPVOID pAudio;
DWORD bytesAudio;
canvasFrame::canvasFrame()
{
	hdc = ::CreateCompatibleDC(NULL);
	Create(NULL,"绘图窗口",WS_POPUP);
	pSBuf[0] = createbuffer("bground.wav"); //加载背景音乐
	pSBuf[1] = createbuffer("foot.wav"); //载入脚步声
	pSBuf[0]->Play(0,0,1); //播放背景音乐
}

void canvasFrame::ColorKey(int i)
{
	key.dwColorSpaceHighValue = 0x001f;
	key.dwColorSpaceLowValue = 0x001f;
	pOPla[i]->SetColorKey(DDCKEY_SRCBLT,&key);
	return;
}
canvasFrame::~canvasFrame()
{
	delete hdc;
	delete hdc1;
	delete bitmap;
	pDD7->Release();
	pPSur->Release();
	for(i=0;i<8;i++)
		pOPla[i]->Release();
	pDS->Release();
	pPBuf->Release();
	for(i=0;i<1;i++)
		pSBuf[i]->Release();
}


BEGIN_MESSAGE_MAP(canvasFrame, CFrameWnd)
	//{{AFX_MSG_MAP(canvasFrame)
	ON_WM_CREATE()
	ON_WM_CHAR()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// canvasFrame message handlers

int canvasFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
    result = DirectDrawCreateEx(NULL, (VOID**)&pDD7, IID_IDirectDraw7, NULL);
	//建立DirectDraw对象
    if (result != DD_OK)
		MessageBox("建立DirectDraw对象失败!");
	result = pDD7->SetCooperativeLevel(m_hWnd,DDSCL_EXCLUSIVE |
			         DDSCL_FULLSCREEN|DDSCL_ALLOWREBOOT );
	//设定协调层级
	if(result !=DD_OK)
		MessageBox("设定程序协调层级失败!");
	result = pDD7->SetDisplayMode(640,480,16,0,DDSDM_STANDARDVGAMODE);  //设定屏幕显示模式
	//设定显示模式
	if(result !=DD_OK)
		MessageBox("设定屏幕显示模式失败!");
	memset(&desc,0,sizeof(desc));       
	desc.dwSize = sizeof(desc);           
	desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
	desc.dwBackBufferCount = 1;        
	desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
	result = pDD7->CreateSurface(&desc,&pPSur,NULL);
	//建立主绘图页
	if(result !=DD_OK)
	 	MessageBox("建立主绘图页失败!");
	caps.dwCaps = DDSCAPS_BACKBUFFER;  
	result = pPSur->GetAttachedSurface(&caps,&pBBuf); 
	//连结后缓冲区
	if(result !=DD_OK)
		MessageBox("连接后缓冲区失败!");
	memset(&desc,0,sizeof(desc));        
	desc.dwSize = sizeof(desc);
	desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; 
	desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN ;
	desc.dwWidth = 640; 
	desc.dwHeight = 480; 
	char file[10];
	char num[5];
	for(i=0;i<=7;i++)
	{
		result = pDD7->CreateSurface(&desc, &pOPla[i], NULL); 
		//建立幕后内存区
		if(result !=DD_OK)
  			MessageBox("建立幕后内存区失败!");
		ColorKey(i);  //设定颜色键
		sprintf( file , "b%d.bmp" , i ); //取得图文件文件名字符串
		bitmap = (HBITMAP)::LoadImage(NULL,file,IMAGE_BITMAP,640,480,LR_LOADFROMFILE); 
		if(bitmap==NULL)
			MessageBox("无法加载位图!");
		::SelectObject(hdc,bitmap);       //设定hdc中的位图为bitmap
		pOPla[i]->GetDC( &hdc1 );            //取得幕后内存区的DC
		::BitBlt( hdc1 , 0 , 0 , 640 , 480 , hdc , 0 , 0 , SRCCOPY );
   		sprintf( num , "第 %d 张图" , i );  //设定讯息字符串
		::TextOut(hdc1, 0, 0, num, lstrlen(num));  //绘制讯息字符串
		pOPla[i]->ReleaseDC( hdc1 );      //释放DC
	}
	SetTimer(1,300,NULL);  //设定定时器
	i=0;                   //重设 i 值
    result = DirectSoundCreate( NULL, &pDS, NULL ); //建立 DirectSound 对象
	if(result != DS_OK)
		MessageBox("建立 DirectSound 对象失败!");
    result = pDS->SetCooperativeLevel( m_hWnd, DSSCL_PRIORITY );
	if(result != DS_OK)
		MessageBox("设定协调层级失败!");
    memset( &dsdesc,0, sizeof(dsdesc) );          //清空结构内容
    dsdesc.dwSize        = sizeof(dsdesc);        //配置描述结构大小
    dsdesc.dwFlags       = DSBCAPS_PRIMARYBUFFER;
    dsdesc.dwBufferBytes = 0;
    dsdesc.lpwfxFormat   = NULL;
    result = pDS->CreateSoundBuffer( &dsdesc, &pPBuf, NULL );
	if(result != DS_OK)
		MessageBox("建立主缓冲区失败!");
    memset( &pwfmt,0, sizeof(pwfmt) );
    pwfmt.wFormatTag      = WAVE_FORMAT_PCM;
    pwfmt.nChannels       = 2;               //播放声道
    pwfmt.nSamplesPerSec  = 44100;           //播放频率
    pwfmt.wBitsPerSample  = 16;              //位
    pwfmt.nBlockAlign     = pwfmt.wBitsPerSample / 8 * pwfmt.nChannels;
    pwfmt.nAvgBytesPerSec = pwfmt.nSamplesPerSec * pwfmt.nBlockAlign;
    result = pPBuf->SetFormat(&pwfmt);       //设定播放格式
	if(result != DS_OK)
		MessageBox("设定播放格式失败!");
	return 0;
}

void canvasFrame::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	CFrameWnd::OnChar(nChar, nRepCnt, nFlags);
	if( nChar== VK_ESCAPE )      //判断是否按下 Esc 键
		PostMessage(WM_CLOSE );  //传送WM_CLOSE信息
}

void canvasFrame::OnTimer(UINT nIDEvent) 
{
	CFrameWnd::OnTimer(nIDEvent);
	if(i==7)
		i=0;
	if(i==1 || i==4)  //判断图号
		pSBuf[1]->Play(0,0,0);  //播放脚步声
	pBBuf->BltFast( 0 , 0 , pOPla[7], CRect(0,0,640,480) , DDBLTFAST_WAIT);  //贴上背景图
	pBBuf->BltFast( 0 , 0 , pOPla[i], CRect(0,0,640,480) , DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY );
	//贴上人物图
	pPSur->Flip( NULL , DDFLIP_WAIT ); //翻页
	i++;
}

LPDIRECTSOUNDBUFFER canvasFrame::createbuffer(char* filename)
{	LPDIRECTSOUNDBUFFER buf;
	hmmio = mmioOpen(filename, NULL, MMIO_ALLOCBUF|MMIO_READ );
	//开启文件
	if(hmmio == NULL)                           //判断是否为空
		MessageBox("档案不存在!");
	ckRiff.fccType = mmioFOURCC('W', 'A', 'V', 'E');
	//设定档案类型
	mmresult = mmioDescend(hmmio,&ckRiff,NULL,MMIO_FINDRIFF);
	//搜寻类型
	if(mmresult != MMSYSERR_NOERROR)
		MessageBox("档案格式错误!");
	ckInfo.ckid = mmioFOURCC('f','m','t',' ');  //设定区块类型
	mmresult = mmioDescend(hmmio,&ckInfo,&ckRiff,MMIO_FINDCHUNK);
	//搜寻区块
	if(mmresult != MMSYSERR_NOERROR)
		MessageBox("档案格式错误!");
	if(mmioRead(hmmio,(HPSTR)&swfmt,sizeof(swfmt)) == -1) //读取档案格式
		MessageBox("读取格式失败!");
	mmresult = mmioAscend(hmmio,&ckInfo,0);     //跳出子区块
	ckInfo.ckid = mmioFOURCC('d','a','t','a');  //设定区块类型
	mmresult = mmioDescend(hmmio,&ckInfo,&ckRiff,MMIO_FINDCHUNK);
	//搜寻区块
	if(mmresult != MMSYSERR_NOERROR)
		MessageBox("文件格式错误!");
	size = ckInfo.cksize;                       //取得实际资料大小
	memset( &dsdesc,0,sizeof(dsdesc));        //清空结构内容
    dsdesc.dwSize  = sizeof(dsdesc);          //配置结构大小
    dsdesc.dwFlags = 	 DSBCAPS_STATIC |DSBCAPS_CTRLPAN |DSBCAPS_CTRLVOLUME| DSBCAPS_GLOBALFOCUS;
    dsdesc.dwBufferBytes = size;            //设定缓冲区大小
    dsdesc.lpwfxFormat   = &swfmt;          //设定缓冲区格式
    result = pDS->CreateSoundBuffer( &dsdesc, &buf, NULL );
	if(result != DS_OK)
		MessageBox("建立次缓冲区失败!");

	result = buf->Lock(0,size,&pAudio,&bytesAudio,NULL,NULL,NULL);
	//锁定缓冲区
	if(result != DS_OK)
		MessageBox("锁定缓冲区失败!");
	mmresult = mmioRead(hmmio,(HPSTR)pAudio,bytesAudio);
	//读取音文件数据
	if(mmresult == -1)
		MessageBox("读取音文件数据失败!");
	result = buf->Unlock(pAudio,bytesAudio,NULL,NULL);
	//解除锁定缓冲区
	if(result != DS_OK)
		MessageBox("解除锁定缓冲区失败!");
	mmioClose(hmmio,0);
	return buf;
}



⌨️ 快捷键说明

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