如何使用DirectX的例子?
发信人:winson(阿苦),信区:Programming
标题:一个DirectX的例子
发信站:BBS水木清华站(WedJan1416:49:071998)
#include<windows.h>
#include<ddraw.h>
#include<dsound.h>
IDirectDraw*dd;
IDirectDrawSurface*dds0,*dds1,*dds2,*dds3;
IDirectDrawClipper*ddc;
IDirectSound*ds;
IDirectSoundBuffer*dsb1,*dsb2;
intx=20,y=20;
intvx=5,vy=3;
voidMoveBall(HWNDhwnd,BOOLbMove)
{
BOOLbBounce=FALSE;
RECTrectSrc,rectDest;
intox,oy,nx,ny;
GetClientRect(hwnd,&rectDest);
ClientToScreen(hwnd,(POINT*)&rectDest.left);
ClientToScreen(hwnd,(POINT*)&rectDest.right);
if(bMove)
{
ox=rectDest.left+
MulDiv(rectDest.right-rectDest.left-32,x,500);
oy=rectDest.top+
MulDiv(rectDest.bottom-rectDest.top-32,y,500);
x+=vx;
y+=vy;
if(x<0){x=0;vx=-vx;bBounce=TRUE;}
if(x>=500){x=1000-x;vx=-vx;bBounce=TRUE;}
if(y<0){y=-y;vy=-vy;bBounce=TRUE;}
if(y>=500){y=1000-y;vy=-vy;bBounce=TRUE;}
if(bBounce)
{
dsb1->SetCurrentPosition(0);
dsb1->Play(0,0,0);
}
}
nx=rectDest.left+
MulDiv(rectDest.right-rectDest.left-32,x,500);
ny=rectDest.top+
MulDiv(rectDest.bottom-rectDest.top-32,y,500);
rectSrc.left=rectSrc.top=0;
rectSrc.right=rectSrc.bottom=32;
if(bMove)
{
rectDest.left=rectDest.top=0;
rectDest.right=rectDest.bottom=32;
dds2->Blt(&rectDest,dds3,&rectSrc,DDBLT_WAIT,NULL);
if(abs(nx-ox)<32&&abs(ny-oy)<32)
{
if(nx<ox)
{
rectSrc.left=ox-nx;
rectSrc.right=32;
rectDest.left=0;
rectDest.right=32-rectSrc.left;
}
else
{
rectDest.left=nx-ox;
rectDest.right=32;
rectSrc.left=0;
rectSrc.right=32-rectDest.left;
}
if(ny<oy)
{
rectSrc.top=oy-ny;
rectSrc.bottom=32;
rectDest.top=0;
rectDest.bottom=32-rectSrc.top;
}
else
{
rectDest.top=ny-oy;
rectDest.bottom=32;
rectSrc.top=0;
rectSrc.bottom=32-rectDest.top;
}
dds2->Blt(&rectDest,dds1,&rectSrc,DDBLT_WAIT,NULL);
}
rectSrc.left=rectSrc.top=0;
rectSrc.right=rectSrc.bottom=32;
rectDest.left=ox;
rectDest.top=oy;
rectDest.right=rectDest.left+32;
rectDest.bottom=rectDest.top+32;
dds0->Blt(&rectDest,dds2,&rectSrc,DDBLT_WAIT,NULL);
}
rectDest.left=nx;
rectDest.top=ny;
rectDest.right=rectDest.left+32;
rectDest.bottom=rectDest.top+32;
dds0->Blt(&rectDest,dds1,&rectSrc,DDBLT_WAIT,NULL);
}
LRESULTCALLBACKWndProc(HWNDhwnd,UINTuMsg,
WPARAMwParam,LPARAMlParam)
{
HDChDC;
PAINTSTRUCTpaintStruct;
switch(uMsg)
{
caseWM_PAINT:
hDC=BeginPaint(hwnd,&paintStruct);
if(hDC!=NULL)
{
MoveBall(hwnd,FALSE);
EndPaint(hwnd,&paintStruct);
}
break;
caseWM_TIMER:
MoveBall(hwnd,TRUE);
break;
caseWM_KEYDOWN:
switch(wParam)
{
caseVK_LEFT:vx--;break;
caseVK_UP:vy--;break;
caseVK_RIGHT:vx++;break;
caseVK_DOWN:vy++;break;
caseVK_ESCAPE:PostMessage(hwnd,WM_CLOSE,0,0);
}
break;
caseWM_DESTROY:
PostQuitMessage(0);
break;
default:
returnDefWindowProc(hwnd,uMsg,wParam,lParam);
}
return0;
}
intWINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,
LPSTRd3,intnCmdShow)
{
MSGmsg;
HWNDhwnd;
WNDCLASSwndClass;
DDSURFACEDESCddsd;
DSBUFFERDESCdsbd;
HDChddDC;
RECTrect;
HRSRChrsrc;
HGLOBALhRData;
DWORD*pRData;
LPBYTEpMem1,pMem2;
DWORDdwSize1,dwSize2;
if(hPrevInstance==NULL)
{
memset(&wndClass,0,sizeof(wndClass));
wndClass.style=CS_HREDRAW|CS_VREDRAW;
wndClass.lpfnWndProc=WndProc;
wndClass.hInstance=hInstance;
wndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndClass.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wndClass.lpszClassName="BOUNCE";
if(!RegisterClass(&wndClass))returnFALSE;
}
hwnd=CreateWindow("BOUNCE","BOUNCE",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,0,CW_USEDEFAULT,0,
NULL,NULL,hInstance,NULL);
DirectDrawCreate(NULL,&dd,NULL);
dd->SetCooperativeLevel(hwnd,
DDSCL_NORMAL|DDSCL_NOWINDOWCHANGES);
memset(&ddsd,0,sizeof(DDSURFACEDESC));
ddsd.dwSize=sizeof(DDSURFACEDESC);
ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE;
ddsd.dwFlags=DDSD_CAPS;
dd->CreateSurface(&ddsd,&dds0,NULL);
dd->CreateClipper(0,&ddc,NULL);
dds0->SetClipper(ddc);
ddc->SetHWnd(0,hwnd);
ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;
ddsd.dwHeight=32;
ddsd.dwWidth=32;
ddsd.dwFlags=DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH;
dd->CreateSurface(&ddsd,&dds1,NULL);
dd->CreateSurface(&ddsd,&dds2,NULL);
dd->CreateSurface(&ddsd,&dds3,NULL);
dds1->GetDC(&hddDC);
SaveDC(hddDC);
rect.left=rect.top=0;
rect.right=rect.bottom=32;
FillRect(hddDC,&rect,(HBRUSH)(COLOR_WINDOW+1));
SelectObject(hddDC,GetStockObject(BLACK_BRUSH));
SelectObject(hddDC,GetStockObject(BLACK_PEN));
Ellipse(hddDC,0,0,32,32);
RestoreDC(hddDC,-1);
dds1->ReleaseDC(hddDC);
dds3->GetDC(&hddDC);
FillRect(hddDC,&rect,(HBRUSH)(COLOR_WINDOW+1));
dds3->ReleaseDC(hddDC);
DirectSoundCreate(NULL,&ds,NULL);
ds->SetCooperativeLevel(hwnd,DSSCL_NORMAL);
memset(&dsbd,0,sizeof(DSBUFFERDESC));
dsbd.dwSize=sizeof(DSBUFFERDESC);
dsbd.dw=(LPWAVEFORMATEX)(pRData+5);
ds->CreateSoundBuffer(&dsbd,&dsb1,NULL);
dsb1->Lock(0,dsbd.dwBufferBytes,&pMem1,&dwSize1,
&pMem2,&dwSize2,0);
memcpy(pMem1,(LPBYTE)(pRData+11),dwSize1);
if(dwSize2!=0)
memcpy(pMem2,(LPBYTE)(pRData+11)+dwSize1,dwSize2);
dsb1->Unlock(pMem1,dwSize1,pMem2,dwSize2);
hrsrc=FindResource(hInstance,"HUM.WAV","WAVE");
hRData=LoadResource(hInstance,hrsrc);
pRData=(DWORD*)LockResource(hRData);
dsbd.dwBufferBytes=*(pRData+10);
dsbd.lpwfxFormat=(LPWAVEFORMATEX)(pRData+5);
ds->CreateSoundBuffer(&dsbd,&dsb2,NULL);
dsb2->Lock(0,dsbd.dwBufferBytes,&pMem1,&dwSize1,
&pMem2,&dwSize2,0);
memcpy(pMem1,(LPBYTE)(pRData+11),dwSize1);
if(dwSize2!=0)
memcpy(pMem2,(LPBYTE)(pRData+11)+dwSize1,dwSize2);
dsb2->Unlock(pMem1,dwSize1,pMem2,dwSize2);
dsb2->Play(0,0,DSBPLAY_LOOPING);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
SetTimer(hwnd,1,100,NULL);
while(GetMessage(&msg,NULL,0,0))
DispatchMessage(&msg);
KillTimer(hwnd,1);
returnmsg.wParam;
}
--
※来源:·BBS水木清华站bbs.net.tsinghua.edu.cn·[FROM:166.111.128.111]
本文地址:http://www.45fan.com/dnjc/67257.html