DirectX Programming Technology __ Programming

Source: Internet
Author: User
Tags terminates

DirectX Programming Technology

You must be familiar with DirectX, because in Microsoft just launched Windows window operating system, because the display interface with a unified GDI, prohibit programmers direct operation of hardware, which makes the game on the win 3.x system speed is very slow, simply can not promote. Microsoft in order to solve this problem, once again introduced wing graphics accelerator, but because wing lack of support of the vast number of game manufacturers, also not popular. So most of the games we played at that time were running in a DOS environment.
Until 1995, with the birth of Windows 95, Microsoft officially unveiled its new generation of game development system DirectX. DirectX with its high-performance performance, unified program interface, so that the first launch by the major game manufacturers love, and have expressed support for it, so Windows system under the game development era really began.
DirectX programming, will involve the WIN 95 and C + + some of the basics, so you'd better have the basis of this before, if you are not very familiar with them, it does not matter, can be stone with the introduction of this article to learn.

    Good, gossip less start to get to the point:
(i) DirectX consists of the following parts:
    1, DirectDraw: Rapid direct access is achieved by directly accessing the display memory and hardware and software acceleration technology.
    2, DirectSound: To provide hardware and software sound mixing and recording regeneration function.
    3, DirectPlay: Provides interactive functions for multiplayer games, allowing you to easily realize Internet interconnection.
    4, Direct3D: Interactive three-dimensional graphics technology.
    5, DirectInput: Enables your program to control input devices such as mouse, keyboard, and joystick.
    6, DirectSetup: Completing the installation of the DirectX driver.
    7, AutoPlay: As long as you put the CD-ROM into the optical drive it will automatically run.
  Finally, what we've talked about and used is DirectX 5, so we need a DirectX 5 SDK, which you can look for in a few CDs, and it's free.

(ii) preparation before formal programming
    to allow VC + + 5 to correctly compile and link your program, you must first make the following settings in Microsoft Developer Studio To enable the compiler to find the required link libraries and include files.
    First, open a new project workspace, and in the File menu, select New to create a Win32 application named ' MyDirectX1 ', a new folder appears in the Workspace window. After the project is created, select Add to Project/files in the Project menu to add the program to the new project (This step is described later).
    Then, set the path to the include file that you want at compile time. In the Tools menu, select Options, Pop-up Options dialog box, select Directories, select Include files in the Show Directories for list box, double-click the blank line at the bottom of the list box, and enter c:/dx5sdk/ Sdk/inc and C:/dx5sdk/sdk/samples/misc; then on the show directories the For list box, select Library files, double-click the blank line at the bottom of the show, and enter c:/dx5sdk/sdk/ LIB (We assume that the DirectX 5 SDK is installed in c:/dx5sdk/).
    Finally, set the library file required for the link. Open the Settings/link in the Project menu, select General in the Category Drop-down box, and then add Ddraw.lib and Winmm.lib to the Object/library module list box.

(iii) Our first DirectX program
We're going to take the example program in the DirectX SDK DDEX1 ' As the basis of the explanation, because there are at least several benefits: (1) Everyone has the right source program, when you work hard to lose a program, but unfortunately in the compilation of errors, you will not be overwhelmed, you can control the source program. (2) I don't have to move all the code to the paper, so we can give a more detailed introduction to the focus of DirectX. (3) You can cultivate the ability to read other people's programs, later learning will be easier.
So, what are we waiting for? Remember that we have built a new project ' MyDirectX1 '? Now open Project/add to Project/files, browse the directory dxsdk/sdk/samples/ddex1, and select all the files in the directory and click ' OK ' to add them to ' MyDirectX1 '. OK, now open ' DDEX1. CPP ' Look at it.

1. Before using Dirextdraw, you first need to create an object DirectDraw entity that represents the computer's display adapter. Then, you can use the method provided by the interface to manipulate the object entity so that it completes the commands and tasks.
So the beginning of the program declares a DirectDraw object:
Lpdirectdraw LPDD;
The object is then created as follows:
HRESULT Ddrval;
Ddrval = Directdrawcreate (null, &LPDD, NULL);
if (Ddrval = = DD_OK)
{
DirectDraw Object LPDD created successfully
}
Else
{
DirectDraw object cannot be created
}

2, then set up the collaboration layer of the operation mode. The operation mode of DirectDraw program refers to the whole screen mode, window mode, Modex mode and so on.
HRESULT Ddrval;
Ddrval = Lpdd->setcooperativelevel (hwnd, ddscl_exclusive | Ddscl_fullscreen);
if (Ddrval = = DD_OK) {
Full Screen Exclusive Set success
}
else {
Setup failed
}

3, set the display mode, that is, screen resolution and number of colors.
HRESULT Ddrval;
Ddrval = Lpdd->setdisplaymode (640, 480, 8);
if (Ddrval = = DD_OK) {
Successfully set the screen to a 640x480x256 color
}
else {
Display mode cannot be changed
}

4, create the surface. We can think of the surface as a memory buffer, all operations are to the buffer, the operation is completed only to the entire buffer into the main surface (video memory) that completes the quick write screen. With this technique, you can achieve smooth, flicker-free animation, and here's how to create a surface.
The first step in creating a switchable surface (Surface) is to set the parameters of the surface (Surface) in the DDSURFACEDESC structure, see the following procedure:

Create the primary surface with 1 back buffer.
ddsd.dwsize = sizeof (DDSD);
Ddsd.dwflags = Ddsd_caps | Ddsd_backbuffercount;
Ddsd.ddsCaps.dwCaps = Ddscaps_primarysurface |
Ddscaps_flip | Ddscaps_complex;
Ddsd,dwbackbuffercount = 1;

In the above lines, the size of the DDSURFACEDESC structure is assigned to the dwsize member. Doing so prevents you from returning an invalid value when calling the DirectDraw method, and it is easy to extend the DDSURFACEDESC structure in the future.
The member dwflags is used to indicate which areas in the DDSURFACEDESC structure are filled in and which areas have invalid information. In the program, we use dwflags to indicate that you want to use the structure Ddscaps (ddsc_caps) and that you want to create a back buffer (ddsd_backbuffercount). The member Dwcaps in the routine contains some flags that are used in the DDSCAPS structure. As a result, member Dwcaps defines a main surface (Surface) (Ddscaps_primarysurface), a pop-up surface (Surface) (Dds-cpas_primarysurface), and a complex surface ( Surface) (Ddscaps_complex). The so-called complex surface (Surface) means that the surface (Surface) is composed of several sub surfaces (Surface). Finally, the above routine defines a backend buffer. This backend buffer is the memory area where we actually operate. Once the operation is complete, you can then bounce them from the back buffer to the main surface (Surface). Here, the number of background buffers is set to 1. But you can set any number of back buffers, as long as your memory allows.
After you complete the DDSURFACEDESC structure, you can use the structure and LPDD to create a surface pointer:
Ddrval = Lpdd->createsurface (&DDSD, &lpddsprimary, NULL);
if (Ddrval = = DD_OK) {
Lpddsprimary point to main surface
}
else{
return FALSE;
}
If the call succeeds, the Idirectdraw::createsurface function returns a pointer to the main surface lpddsprimary. Then call Idirectdrawsurface::getattachedsurface to get a pointer to the back buffer:
Ddscaps.dwcaps = Ddscaps_backbuffer;
Ddrval = Lpddsprimary->getattachedsurface (&ddcaps, &lpddsback);
if (Ddrval = = DD_OK) {
Lpddsback point to back buffer
}
else{
return FALSE;
}
If the Idirectdrawsurface::getattachedsurface call succeeds, Lpddsback points to the back buffer. Now we can do various operations on the back buffer, and write what we want to show. For example, the game, we first transferred into the background image, and then into various roles should be displayed in the current image, and finally the ' dialogue ', ' text ' and so on. When all is done, flip the back buffer and the main surface to show what you want.

5. Flip the surface
while (1) {
HRESULT Ddrval;
Ddrval = Lpddsprimary->flip (NULL, 0);
if (Ddrval = = DD_OK)
{
Breakfile://Flip Finish, exit loop
}
file://If the surface is lost
if (Ddrval = = dderr_surfacelost)
{
Ddral = Lpddsprimary->restore ();
file://Recovery surface
if (ddral!= DD_OK)
{
break;//restore success, exit loop
}
}
file://If the previous surface flip has not occurred
            if (ddrval!= dderr_ wasstilldrawing)
            {
                 break;
      }
       } In the
    example, lpddsprimary indicates the main surface and its back buffer. After calling Idirectdrawsurface::flip, the main surface and the back buffer are exchanged. If the call succeeds, the DD_OK is returned, the program terminates the while loop, and if the return dderr_surfacelost indicates that the surface may be missing, the surface needs to be recovered with Idirectdrawsurface::restore, and if the restoration succeeds, The Idirectdrawsurface::flip method is called again, and if it fails, the program terminates the while loop and returns an error value. In addition, even if the idirectdrawsurface::flip call succeeds, the exchange is not done immediately, and it will wait until the previous surface exchange in the system is complete. If the previous surface flip has not occurred, Idirectdrawsurface::flip returns dderr_wasstilldrawing. Idirectdrawsurface::flip will continue looping until it returns DD_OK.

6. Do not forget to release () the object you created before closing the program.
if (LPDD!= NULL)
{
if (lpddsprimary!= NULL) {
Lpddsprimary->release ();
Lpddsprimary = NULL;
}
Lpdd->release ();
LPDD = NULL;

Ok. Done. Using the above means we can make the basic DirectX program, but don't be too happy, DirectX is much bigger than you think. What we're dealing with is just a little bit of fur (DirectDraw only) and you have to work hard to get a full grasp of DirectX programming skills. My advice is more on the machine, read more books, reading other people's procedures, only in order to quickly improve their level.
OK, let's talk about it today, I hope it will help you, and I think we'll meet again if I have the chance. Of course, you can also write to here wj77@990.net, we make a friend. See you next time.

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.