分享
 
 
 

一个DirectX的例子

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

发信人: winson (阿苦), 信区: Programming

标 题: 一个DirectX的例子

发信站: BBS 水木清华站 (Wed Jan 14 16:49:07 1998)

#include <windows.h>

#include <ddraw.h>

#include <dsound.h>

IDirectDraw *dd;

IDirectDrawSurface *dds0, *dds1, *dds2, *dds3;

IDirectDrawClipper *ddc;

IDirectSound *ds;

IDirectSoundBuffer *dsb1, *dsb2;

int x = 20, y = 20;

int vx = 5, vy = 3;

void MoveBall(HWND hwnd, BOOL bMove)

{

BOOL bBounce = FALSE;

RECT rectSrc, rectDest;

int ox, 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);

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg,

WPARAM wParam, LPARAM lParam)

{

HDC hDC;

PAINTSTRUCT paintStruct;

switch(uMsg)

{

case WM_PAINT:

hDC = BeginPaint(hwnd, &paintStruct);

if (hDC != NULL)

{

MoveBall(hwnd, FALSE);

EndPaint(hwnd, &paintStruct);

}

break;

case WM_TIMER:

MoveBall(hwnd, TRUE);

break;

case WM_KEYDOWN:

switch (wParam)

{

case VK_LEFT: vx--; break;

case VK_UP: vy--; break;

case VK_RIGHT: vx++; break;

case VK_DOWN: vy++; break;

case VK_ESCAPE: PostMessage(hwnd, WM_CLOSE, 0, 0);

}

break;

case WM_DESTROY:

PostQuitMessage(0);

break;

default:

return DefWindowProc(hwnd, uMsg, wParam, lParam);

}

return 0;

}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR d3, int nCmdShow)

{

MSG msg;

HWND hwnd;

WNDCLASS wndClass;

DDSURFACEDESC ddsd;

DSBUFFERDESC dsbd;

HDC hddDC;

RECT rect;

HRSRC hrsrc;

HGLOBAL hRData;

DWORD *pRData;

LPBYTE pMem1, pMem2;

DWORD dwSize1, 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)) return FALSE;

}

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

return msg.wParam;

}

--

※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 166.111.128.111]

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有