// canvasFrame.cpp : implementation file
#include "canvasFrame.h"
IMPLEMENT_DYNCREATE(canvasFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(canvasFrame, CFrameWnd)
//{{AFX_MSG_MAP(canvasFrame)
ON_WM_CREATE()
ON_WM_CHAR()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
LPDIRECTDRAW7 pDD7;
LPDIRECTDRAWSURFACE7 pPSur;
LPDIRECTDRAWSURFACE7 pBBuf;
LPDIRECTDRAWSURFACE7 pOPla[8];
DDSCAPS2 caps;
DDSURFACEDESC2 desc;
HRESULT result;
DDCOLORKEY key;
LPDIRECTSOUND pDS;
LPDIRECTSOUNDBUFFER pPBuf;
LPDIRECTSOUNDBUFFER pSBuf[2];
WAVEFORMATEX pwfmt;
WAVEFORMATEX swfmt;
DSBUFFERDESC dsdesc;
MMCKINFO ckRiff;
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();
int i;
for(i=0; i<8; i++)
pOPla[i]->Release();
pDS->Release();
pPBuf->Release();
for(i=0; i<1; i++)
pSBuf[i]->Release();
}
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 );
//result = pDD7->SetCooperativeLevel(m_hWnd,DSSCL_EXCLUSIVE );
//设定程序协调层级
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];
int i;
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);
pOPla[i]->GetDC( &hdc1 );
::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 );
}
SetTimer(1,300,NULL);
i=0;
result = DirectSoundCreate( NULL, &pDS, NULL );
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 )
PostMessage(WM_CLOSE );
}
void canvasFrame::OnTimer(UINT nIDEvent)
{
CFrameWnd::OnTimer(nIDEvent);
static UINT num = 0;
if(nIDEvent == 1)
{
pSBuf[1]->Play(0,0,0);
pBBuf->BltFast( 0 , 0 , pOPla[7], CRect(0,0,640,480) , DDBLTFAST_WAIT);
pBBuf->BltFast( 0 , 0 , pOPla[num], CRect(0,0,640,480), DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY );
pPSur->Flip( NULL , DDFLIP_WAIT );
num++;
num %= 7;
}
}
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;
}