An example of directsound

Source: Internet
Author: User
# 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, 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, uint umsg,
Wparam, lparam)
{
HDC;
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 hprevinstance,
Lpstr D3, int ncmdshow)
{
MSG;
Hwnd;
Wndclass;
Ddsurfacedesc ddsd;
Dsbufferdesc dsbd;
HDC hdddc;
Rect;
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-> createid per (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. lpwfxformat = (lpwaveformatex) (prdata + 5 );
DS-> createsoundbuffer (& dsbd, & dsb1, null );
Dsb1-> lock (0, dsbd. dwbufferbytes, (void **) & pmem1, & dwsize1,
(Void **) & 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, (void **) & pmem1, & dwsize1,
(Void **) & 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.