Use VB to compile the ddraw Program

Source: Internet
Author: User

Use VB to compile the ddraw Program

Www.applevb.com

Directx7.0 finally came into being. Similar to directx6, version 7 also included a large (129 m) SDK development library, compared with the directx6 SDK library, the directx7 SDK Library provides the following new functions:
L support for Visual Basic. You can use the class library to develop DirectX-based programs in the Visual Basic Environment.
L provides more API functions in directx3d instant mode to support new 3D effects in directx7, including three-dimensional environment ing and vertex mixing.
L directmusic supports the downloadable Sound Level 2 standard.
L directinput supports 8-key game rod devices and Microsoft's force feedback joystick. The SDK Library provides a method to read Force Feedback results files. In addition, the Force editor program is provided to establish the effect.
For VB enthusiasts, the new SDK library finally provides complete support for Vb, and now you can use Visual Basic to compile DirectX programs.
I. Installation of DirectX SDK Library
The sdkdatabase of Microsoft is a self-decompressed dx7sdk.exe file that has reached 129m. you can download it online or obtain it from the related CD. Double-click the file to bring up the WinZip Self-decompression dialog box. In the WinZip Self-extract dk7sdk. EXE window that appears, enter the path of the extracted file and click "Unzip" to decompress the SDK file, as shown in Figure 1-1:

The size of dx7sdk.exe after decompression is 220 MB. before decompression, readers who are nervous about the hard disk should first check whether the size of your hard disk is sufficient.
After Uncompressing the file, go to the Uncompressing directory and double-click the setup.exe file to install the directx7.0 SDK file. The installation is based on the standard InstallShield interface. For Windows users, you should be familiar with the installation interface, as long as you follow the installation instructions step by step. After the installation is complete, the installer adds a Microsoft DirectX 7 SDK menu in the Start Menu, including the DirectX 7 setting tool, VB sample, SDK help, and other menu items.
Now we start to go to VB and start our DirectX vbprogramming. Here we use VB6 Enterprise Edition (English ). Windows 98 Chinese version.
Open VB and click Project | references in the menu. There is a DirectX 7.0 for Visual Basic Type Library list item in the object library list. This is the directx7.0 VB class library. Select this item, select the "OK" button to add the Library to the project file.

Ii. Preliminary DirectX Programming
1 directx7 object
The directx7 object is the service and startup object of all other objects in the DirectX VB object. This object contains methods for establishing objects such as DirectDraw, direct3d, directsound, and directinput. At the same time, this object also contains a series of three-dimensional control vertices, matrix operation functions, and some DirectX system functions. In VB, you can use dim... New to directly define and initialize a directx7 object, for example:
Dim DirectX as new directx7
After a directx7 object is successfully created, you can use the directdrawcreate and direct3drmcreate methods of this object to create DirectDraw and direct3d objects.
Directx7 object Example 1: Obtain the DirectDraw and directsound drivers in the system
Create a new project file and click Project | references in the menu, select the DirectX 7.0 for Visual Basic Type Library item in the object library list and press the OK button (this step is required for the following programs, which will not be described later ). Add a ListBox control and four commandbutton controls to form1, and then add the following code in the form1 code window:

Option explicitdim DirectX as new directx7dim ddenum as directdrawenumdim ddsound as directsoundenumprivate sub command1_click () dim count, I as Integer Set ddenum = DirectX. getddenum COUNT = ddenum. getcount list1.clear for I = 1 to count list1.additem ddenum. getdescription (I) Next I set ddenum = nothingend subprivate sub command2_click () dim count, I as Integer Set ddenum = DirectX. getddenum COUNT = ddenum. getcount list1.clear for I = 1 to count list1.additem ddenum. getname (I) Next I set ddenum = nothingend subprivate sub command3_click () dim count, I as Integer Set ddsound = DirectX. getdsenum COUNT = ddsound. getcount list1.clear for I = 1 to count list1.additem ddsound. getdescription (I) Next iend subprivate sub command4_click () dim count, I as Integer Set ddsound = DirectX. getdsenum COUNT = ddsound. getcount list1.clear for I = 1 to count list1.additem ddsound. getname (I) Next iend subprivate sub form_load () command1.caption = "DirectDraw driver description" command2.caption = "DirectDraw driver name" command3.caption = "directsound driver description" command4.caption = "directsound driver name" End subprivate sub form_unload (cancel as integer) set DirectX = nothingend sub

Run the program and click different buttons. The corresponding device driver name and description are displayed in the list box.
2 directdraw7 object
DirectDraw is a software interface for directly operating display devices compatible with Windows graphics system interfaces (GDI. DirectDraw provides hardware independence while allowing direct operations on the video memory. The program only needs to use some basic standard hardware conventions such as RGB and YUV color formats and resolutions. You do not need to call special procedures to use the blitter and color palette. With DirectDraw, you can simply operate the video memory and completely use various hardware features without worrying about the differences between different hardware.
2.1 create a DirectDraw object
The directdraw7 object is the DirectDraw object in directx7. You must first create a directx7 object and then use the directdrawcreate method of the object to create the directdraw7 object. For example:

Dim DX As New DirectX7Dim DDraw As DirectDraw7Set DDraw = DX.DirectDrawCreate("")

2.2 create a collaboration Layer
After a DirectDraw object is created, you must first set the collaboration layer of DirectDraw. The implementation method is to call the setcooperativelevel function of the DirectDraw object. The function is defined as follows:
Object. setcooperativelevel (HDL as long, flags as const_ddsclflags)
The parameter HDL specifies the window handle of the program. The parameter flag determines the method of running the program. function call
Ddraw. setcooperativelevel me. hwnd, ddscl_normal
The program will run in the normal collaboration layer in both window mode. In this collaboration layer, you cannot change the Main Plane palette or perform page switching, because the program can use multiple windows. Function call
Ddraw. setcooperativelevel mainform. hwnd, ddscl_exclusive or _
Ddscl_fullscreen
The program will run in full screen mode. In full-screen collaboration mode, you can use everything of hardware. In this mode, you can set the definition and dynamic color palette to change the display resolution and switch pages.
2.3 set the Display Mode
The setdispalymode function is used to set the display mode. The function is defined as follows:

object.SetDisplayMode( _     w As Long, _     h As Long, _     bpp As Long, _     ref As Long, _     mode As CONST_DDSDMFLAGS

The W and H parameters respectively specify the screen width and height, BPP specifies the number of colors displayed on the screen, and the ref parameter specifies the screen refresh frequency, which is set to 0 to use the default refresh frequency of the display driver, mode specifies the additional parameter. To obtain the display modes supported by the system, you can use the getdisplaymodesenum function of the DirectDraw object to traverse all supported display modes.
2.4 create a plane object
A plane or directdrawsurface object is a graphic display and Drawing Object in DirectDraw. You can paste a bitmap and draw a graph on directdrawsurface, and directly operate the content in the video memory used by the directdrawsurface object. You can use the createsurface method of the DirectDraw object to create a directdrawsurface7 object. For example:

Public DDSFrontDesc As DDSURFACEDESC2With DDSFrontDesc        .lFlags = DDSD_CAPS        .ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE '    End WithSet DDSFront = DDraw.CreateSurface(DDSFrontDesc)

You can also use the createsurfacefromfile function of the DirectDraw object or the createsurfacefromresource function to create a directdrawsurface7 object, and load the images in the image file or resource file into the directdrawsurface. If the preceding function is successfully called, the function returns a directdrawsurface object. If you set the collaboration layer of the DirectDraw object to full screen mode, you can set a Main Plane and several buffer planes under the screen to improve the image performance, first, the image is generated in the lower plane of the screen, and then the image is flipped to the main plane, which can effectively avoid image flashing.
The following describes DirectDraw through a specific example: this example creates a full screen DirectDraw object, and the display of flame characters on the screen exists in the display of the main display plane, press enter to save the graph in the DirectDraw plane. The specific implementation of the program is as follows:
Create a new project file, click the project | reference option in the menu, open the object library List window, and add DirectX 7.0 for Visual Basic Type Library to the project file. Change the name attribute of form1 to mainform, add a picturebox control to mainform, and set its visible attribute to false. Then add the following code to the mainform code window:

Private sub form_keypress (keyascii as integer) dim srect as rect dim hdcsrc as long if keyascii = 27 then exitloop = true 'end elseif keyascii = vbkeyreturn then ddsfront. blttodc picture1.hdc, srect, srect with picture1 'get the graphic device handle hdcsrc = ddsfront compatible with the primary display plane. getdc 'Save the image set. picture = savetohbmp (hdcsrc, 0, 0,640,480) 'release the graphic handle ddsfront. releasedc hdcsrc savepicture picture1, "C:/a.bmp" end with end ifend subpublic sub form_paint () blitrect. right = ddsbackdesc. lwidth blitrect. bottom = ddsbackdesc. lheight ddsfront. BLT blitrect, ddsback, blitrect, and ddblt_waitend sub Add a module file to the project file, which defines the DirectDraw operation and adds the following code: option explicitpublic DX as new directx7public ddraw as directdraw7public ddsfront as same region as your ddsback as your ddsbackdesc as your Clipper as your PICT () as bytedim alpharect as rectdim X as long, Y as longdim temp as longdim index as longdim index2 as longdim POS as longdim posplus1 as longdim posplus2 as longdim posplus3 as longpublic pal (255) as your palette as your neighbors as rectpublic fullsize as booleanpublic exitloop as your accum as longdim MSG (9) as stringdim counter as longdim msgindex as longdim bdrawtext as your lasttime as longdim xpos as long, ypos as longdim wait as longdim angle as singledim flag as region count as longdim curmodeactivestatus as region brestore as region mode as booleanprivate sub main () initializedx 'initialize picture1 to obtain the DirectDraw interface image with mainform. picture1. width = 640 * screen. twipsperpixelx. height = 480 * screen. twipsperpixely end with ddsback. setforecolor RGB (255,255,255) mainform. font. name = "" ddsback. setfont mainform. font MSG (0) = "a demo showing flame characters" MSG (1) = "Demo" MSG (2) = "using VB array" MSG (3) = "direct access to Display memory" MSG (4) = "MSG (5) = "{ESC} key release" 'sets the 8-bit color palette for Index = 0 to 84 pal (index + 1 ). red = Index * 3 + 3 pal (index + 1 ). green = 0 pal (index + 1 ). blue = 0 pal (index + 86 ). red= 255 pal (index + 86 ). green = Index * 3 + 3 pal (index + 86 ). blue = 0 pal (index + 171 ). red = 255 pal (index + 171 ). green = 255 pal (index + 171 ). blue = Index * 3 + 3 next set palette = ddraw. createpalette (ddpcaps_8bit _ or ddpcaps_allow256, Pal () ddsfront. setpalette palette alpharect. right = ddsbackdesc. lwidth-1 alpharect. bottom = ddsbackdesc. lheight-1 ddsback. lock alpharect, ddsbackdesc, ddlock_wait, 0 ddsback. getlockedarray PICT () for x = 0 to 639 for Y = 0 to 479 PICT (x, y) = 0 next 'corresponding unlock ddsback. unlock alpharect while not exitloop mode = exmodeactive brestore = false do until exmodeactive doevents brestore = true loop doevents if brestore then brestore = false ddraw. restoreallsurfaces end if ddsback. lock alpharect, ddsbackdesc, ddlock_wait, 0 ddsback. getlockedarray PICT () for Y = 0 to 479 PICT (0, y) = 0 PICT (639, y) = 0 next for x = 0 to 639 PICT (x, 477) = RND * 220 + 35 PICT (x, 478) = RND * 220 + 35 PICT (x, 479) = RND * 220 + 35 next accum = 0 for x = 1 to 638 for Y = 0 to 477 accum = (accum + PICT (X, Y + 1) _ + PICT (X, Y + 2) _ + PICT (x + 1, Y + 1) _ + PICT (X-1, Y + 1 )) /5 If accum <0 then accum = 0 elseif accum> 255 then accum = 255 end if PICT (x, y) = accum next for x = 0 to 639 PICT (X, 0) = 0 PICT (x, 1) = 0 next x = RND * 639 for Y = 50 to 439 next 'unlock ddsback. unlock alpharect if dx. tickcount () -lasttime> wait then if counter = 0 then bdrawtext = true counter = 1 xpos = RND * 200 ypos = 300 + RND * 140 wait = 400 elseif counter = 1 then msgindex = msgindex + 1 If msgindex> 5 then msgindex = 0 bdrawtext = false counter = 0 wait = 2000 end if lasttime = DX. tickcount end if 'draw text to the backbuffer if bdrawtext then on error resume next ddsback. drawtext xpos, ypos, MSG (msgindex), false on error goto 0 end if mainform. form_paint Wend terminatedx endend subfunction exmodeactive () as Boolean dim testcoopres as long testcoopres = ddraw. testcooperativelevel select case testcoopres case dderr_noexclusivemode exmodeactive = false case dd_ OK exmodeactive = true end selectend functionpublic sub initializedx () mainform. left = 0 mainform. top = 0 mainform. height = 640 * screen. twipsperpixely mainform. width = 480 * screen. twipsperpixelx mainform. show 'establish DirectDraw object set ddraw = DX. directdrawcreate ("") 'sets the cooperative layer ddraw of the DirectDraw object. setcooperativelevel mainform. hwnd, ddscl_exclusive or ddscl_fullscreen 'ddscl_normal' sets the display mode position 640x480x8-bit color ddraw. setdisplaymode 640,480, 8, 0, ddsdm_default: Set ddsfrontdesc as the Main Plane with ddsfrontdesc. lflags = ddsd_caps. ddscaps. lcaps = ddscaps_primarysurface 'or ddscaps_systemmemory end with' sets ddsbackdesc as the background buffer plane with ddsbackdesc. ddscaps. lcaps = ddscaps_systemmemory. lflags = ddsd_caps or ddsd_width or ddsd_height. lwidth = 640. lheight = 480 end with 'create a plane set ddsfront = ddraw. createsurface (ddsfrontdesc) set ddsback = ddraw. createsurface (ddsbackdesc) set clipper = ddraw. createconper (0) Clipper. sethwnd mainform. hwnd ddsfront. setclipper clipper ddsback. setclipper clipper doevents exit suberrout: If not (ddraw is nothing) Then ddraw. restoredisplaymode ddraw. setcooperativelevel mainform. hwnd, ddscl_normal doevents end if msgbox "DirectDraw initialization fails" + CHR (13) + "maybe your display card does not support the 640x480X8 display mode" endend subpublic sub terminatedx () 'subroutine terminatedx replies to the original display mode and releases all DirectDraw-related objects. restoredisplaymode ddraw. setcooperativelevel mainform. hwnd, ddscl_normal doevents set clipper = nothing set ddsback = nothing set ddsfront = nothing set ddraw = nothing set dx = nothingend sub Add a module to the project file, this module mainly defines operations related to image storage and adds the following code: option explicitoption base 0 private type paletteentry pered as byte pegreen as byte peblue as byte peflags as byteend partition type logpalette palversion as integer partition as integer palpalpalentry (255) as paletteentry 'Enough for 256 colors. end typeprivate type guid data1 as long data2 as integer data3 as integer data4 (7) as byteend typeprivate const rastercaps as long = 38 private const rc_palette as long = & h100private const sizepalette as long = 104 private declare function createcompatibledc lib "GDI32" _ (byval HDC as long) as longprivate declare function createcompatiblebitmap lib "GDI32" _ (byval HDC as long, _ byval nwidth as long, _ byval nheight as long) as longprivate declare function getdevicecaps lib "GDI32" _ (byval HDC as long, _ byval icapabilitiy as long) as longprivate declare function using lib "GDI32" _ (byval HDC as long, _ byval wstartindex as long, _ byval wnumentries as long, _ lppaletteentries as paletteentry) as longprivate declare function createpalette lib "GDI32" _ (lplogpalette as logpalette) as longprivate declare function SelectObject lib "GDI32" _ (byval HDC as long, _ byval hobject as long) as longprivate declare function bitblt lib "GDI32" _ (byval hdcdest as long, _ byval xdest as long, _ byval ydest as long, _ byval nwidth as long, _ byval nheight as long, _ byval hdcsrc as long, _ byval xsrc as long, _ byval ysrc as long, _ byval dwrop as long) as longprivate declare function deletedc lib "GDI32" _ (byval HDC as long) as longprivate declare function getforegroundwindow lib "USER32 "() as longprivate declare function selectpalette lib "GDI32" _ (byval HDC as long, _ byval hpalette as long, _ byval bforcebackground as long) as longprivate declare function realizepalette lib "GDI32" _ (byval HDC as long) as longprivate declare function getwindowdc lib "USER32" _ (byval hwnd as long) as longprivate declare function getdc lib "USER32" _ (byval hwnd as long) as longprivate declare function getwindowrect lib "USER32" _ (byval hwnd as long, _ lprect as rect) as longprivate declare function releasedc lib "USER32" _ (byval hwnd as long, _ byval HDC as long) as longprivate declare function get1_topwindow lib "USER32 "() as longprivate type picbmp size as long type as long hbmp as long hpal as long reserved as longend typeprivate declare function olecreatepictureindirect lib "olepro32.dll" (picdesc as picbmp, refiid AS _ guid, byval loss as long, ipic as ipicture) as longpublic function savetohbmp (byval hdcsrc as long, byval leftsrc as long, _ byval topsrc as long, byval widthsrc as long, byval heightsrc as long) as picture dim hdcmemory as long dim hbmp as long dim hbmpprev as long dim R as long dim hpal as long dim hpalprev as long dim ready as long dim haspalettescrn as long dim ready as long dim logpal as logpalette ''creates a memory Graphics Device handle hdcmemory = createcompatibledc (hdcsrc) 'create a bitmap and save it to hdcmemory. hbmp = createcompatiblebitmap (hdcsrc, widthsrc, heightsrc) hbmpprev = SelectObject (hdcmemory, hbmp) 'Get screen properties. rastercapsscrn = getdevicecaps (hdcsrc, rastercaps) 'raster' capabilities. haspalettescrn = rastercapsscrn and rc_palette 'paster' support. palettesizescrn = getdevicecaps (hdcsrc, sizepalette) 'size of 'palette. if haspalettescrn and (palettesizescrn = 256) then', create a copy logpal for the system palette. palversion = & H300 logpal. palnumentries = 256 r = getsystempaletteentries (hdcsrc, 0,256, logpal. palpalentry (0) hpal = createpalette (logpal) hpalprev = selectpalette (hdcmemory, hpal, 0) r = realizepalette (hdcmemory) end if 'Copy the screen image to the r = bitblt (hdcmemory, 0, 0, widthsrc, heightsrc, hdcsrc, leftsrc, topsrc, vbsrccopy) of the memory image device handle) hbmp = SelectObject (hdcmemory, hbmpprev) If exist and (cost = 256) Then hpal = selectpalette (hdcmemory, hpalprev, 0) end if 'release the graphical device handle r = deletedc (hdcmemory) debug. print R' call the createbitmappicture function to create a picture object set savetohbmp = createbitmappicture (hbmp, hpal) end functionpublic function createbitmappicture (byval hbmp as long, byval hpal as long) as picture dim R as long dim PIC as picbmp dim ipic as ipicture dim iid_idispatch as guid fill the idispatch interface with iid_idispatch. data1 = & h20400. data4 (0) = & hc0. data4 (7) = & h46 end with 'Fill the PIC structure with PIC. size = Len (PIC) 'length of structure .. type = vbpictypebitmap 'Type of picture (Bitmap ).. hbmp = hbmp 'handle to bitmap .. hpal = hpal 'handle to palette (may be null ). end with 'create picture object r = olecreatepictureindirect (PIC, iid_idispatch, 1, ipic) 'Return picture object set createbitmappicture = ipicend Function

Run the program. Some flame effects will appear on the screen. Press enter to save the screen to "C:/a.bmp" and Press ESC to exit the program and return to Windows.
In the above program, the program first establishes a DirectDraw object, then sets the collaboration layer of the object to full-screen collaboration mode, and then sets the display mode to 640x480x8-bit color, create a foreground directdrawsurface object and a background buffer directdrawsurface object, and create and set the directdrawclipper object.
In the main program segment, the program first performs operations on the directdrawpalette object of the foreground drawing plane to change the color of the displayed text, and then performs byte operations on the background buffering drawing plane, in order to produce the effect of text dispersion, and then flip the background buffer drawing plane to the foreground. After the user presses enter, the program obtains the graphical device handle compatible with the front-end drawing plane, and then calls the Windows API function to save the content in the drawing plane memory to the Windows bitmap file.

Www.applevb.com

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.