DirectX programming technology-want to learn game programming? Mandatory

Source: Internet
Author: User
Tags terminates

DirectX Programming Technology

Everyone must be familiar with DirectX, because when Microsoft launched the Windows window operating system, the display interface adopts a unified GDI, prohibiting programmers from directly operating the hardware, which makes Windows 3. game programs on X systems are so slow that they cannot be promoted. To solve this problem, Microsoft has launched wing graphics acceleration. However, due to the lack of support from a wide range of game manufacturers, wing is not widely used. Therefore, most of the games we were playing were running in the DOS environment.
Until 1995, with the birth of Windows 95, Microsoft officially announced its next-generation game development system DirectX. DirectX, with its efficient performance and unified program interface, made its launch popular among various game manufacturers and expressed support for it in succession, so far, the game development era in Windows has really started.
DirectX programming involves some basic knowledge of Win 95 and C ++, so before that, you 'd better have the basics in this regard. It doesn't matter if you are not familiar with them, you can learn it with the introduction in this article.

Now, let's talk less about the subject:
(1) DirectX consists of the following parts:
1. DirectDraw: quick and direct access through direct access to the Display memory and software and hardware acceleration technologies.
2. directsound: it provides software and hardware sound mixing and recording regeneration functions.
3. directplay: Provides interaction functions for multiplayer games, allowing you to easily interconnect online.
4. direct3d: interactive 3D graphics technology.
5. directinput: Enables your program to control input devices such as the mouse, keyboard, and game pole.
6. directsetup: Install the DirectX driver.
7. autoplay: as long as you put the disc into the optical drive, it will automatically run.
Finally, we will talk about and use DirectX 5, so we need a set of DirectX 5 sdks. You can find them on some CDs for free.

(2) preparations before formal programming
To allow VC ++ 5 to properly compile and link your program, you must first make the following settings in Microsoft developer studio, so that the compiler can find the desired Link Library and include files.
First, open a new project workspace. In the File menu, select new to create a new Win32 application named 'mydirectx1 ', A new folder is displayed in the workspace window. After the project is created, select Add to project/files from the project menu to add a program to the new project (this step is described later ).
Then, set the path of the file to be included during compilation. In the Tools menu, select options. In the displayed Options dialog box, select directories, select include files in the show directories for list box, double-click the blank row at the bottom of the list box, and enter C: /dx5sdk/SDK/INC and C:/dx5sdk/SDK/samples/MISC; then select library files in the show directories for list box, double-click the blank line at the bottom, and enter C: /dx5sdk/SDK/lib (we assume that the DirectX 5 SDK is installed in C:/dx5sdk /).
Finally, set the library files required for the link. Open settings/link in the project menu, select general in the category drop-down box, and add ddraw. lib and winmm. lib to the object/Library module list box.

(3) Our first DirectX Program
We plan to use the DirectX SDK's example program 'ddex1' as the basis for the explanation, because doing so has at least the following advantages: (1) Everyone has the correct source program, when you have worked hard to lose a program, but unfortunately there is an error during compilation, you will not be overwhelmed, you can compare with the source program. (2) I no longer need to move all the code to the paper, so that we can introduce the key content of DirectX in more detail. (3) It can cultivate your ability to read other people's programs and make it easier to learn later.
In this case, what are you waiting? Remember that we have created a new project 'mydirectx1? Open Project/Add to project/files, browse the dxsdk/SDK/samples/ddex1 directory, and select all the files in the directory, click 'OK' and add them to 'mydirectx1. Now let's open 'ddex1. cpp.

1. Before using dirextdraw, you must first create an object DirectDraw, which represents the display adapter of the microcomputer. Then, you can use the methods provided by the interface to operate the object to complete related commands and tasks.
Therefore, a DirectDraw object is first declared at the beginning of the program:
Lpdirectdraw lpdd;
Create the object as follows:
Hresult ddrval;
Ddrval = directdrawcreate (null, & lpdd, null );
If (ddrval = dd_ OK)
{
// DirectDraw object lpdd created successfully
}
Else
{
// The DirectDraw object cannot be created.
}

2. Set the running mode of the Collaboration layer. That is, the running mode of the DirectDraw program, including the full screen mode, window mode, and modex mode.
Hresult ddrval;
Ddrval = lpdd-> setcooperativelevel (hwnd, ddscl_exclusive | ddscl_fullscreen );
If (ddrval = dd_ OK ){
// The full-screen exclusive mode is set successfully.
}
Else {
// Setting failed
}

3. Set the display mode, that is, the screen resolution and number of colors.
Hresult ddrval;
Ddrval = lpdd-> setdisplaymode (640,480, 8 );
If (ddrval = dd_ OK ){
// Successfully set the screen to 640x480x256.
}
Else {
// The display mode cannot be changed.
}

4. Create a surface. We can regard the surface as a memory buffer. All operations are performed on the Buffer Zone. After the operation, we only need to flip the entire buffer zone into the primary surface (Display memory) this completes screen writing. Using this technology, you can achieve smooth and non-blinking animation effects. The following describes how to create a surface.
The first step to creating a surface that can be switched is to set the surface parameters in the ddsurfacedesc structure. See the following program:

// 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 preceding rows, the size of the ddsurfacedesc structure is assigned to the dwsize member. This prevents you from returning an invalid value when calling the DirectDraw method, and facilitates the expansion of the ddsurfacedesc structure in the future.
The member dwflags is used to indicate which areas in the ddsurfacedesc structure are valid and which areas are invalid. In the program, we use dwflags to indicate that you want to use the structure ddscaps (ddsc_caps), and you want to create a background buffer (ddsd_backbuffercount ). In the routine, the member dwcaps contains some flags used in the ddscaps structure. As a result, member dwcaps defines a surface (ddscaps_primarysurface), a pop-up surface (DDS-CPAS_PRIMARYSURFACE), and a surface (ddscaps_complex ). A surface is composed of several subsurfaces. Finally, the preceding routine defines a background buffer. This background buffer is the memory zone for our real operations. After the operation is complete, you can play them from the background buffer to the main surface. Here, the number of backend buffers is set to 1. However, you can set any number of backend buffers as long as your memory permits.
After entering the ddsurfacedesc structure, you can use this structure and lpdd to create the surface pointer:
Ddrval = lpdd-> createsurface (& ddsd, & lpddsprimary, null );
If (ddrval = dd_ OK ){
// Lpddsprimary points to the main surface
}
Else {
Return false;
}
If the call is successful, the idirectdraw: createsurface function returns the pointer lpddsprimary pointing to the main surface. Then call idirectdrawsurface: getattachedsurface to obtain the pointer of the background Buffer:
Ddscaps. dwcaps = ddscaps_backbuffer;
Ddrval = lpddsprimary-> getattachedsurface (& ddcaps, & lpddsback );
If (ddrval = dd_ OK ){
// Lpddsback points to the background Buffer
}
Else {
Return false;
}
If the call to idirectdrawsurface: getattachedsurface is successful, lpddsback points to the background buffer. Now we can perform various operations on the background buffer and write the content to be displayed. For example, for a game, we need to first import the background image, then call the image currently displayed by various roles, and finally 'White 'and 'text. After all, flip the background buffer and the main surface to display what you want.

5. Flip the surface
While (1 ){
Hresult ddrval;
Ddrval = lpddsprimary-> flip (null, 0 );
If (ddrval = dd_ OK)
{
Break; file: // The Flip is complete and the loop is exited.
}
File: // if the surface is lost
If (ddrval = dderr_surfacelost)
{
Ddral = lpddsprimary-> restore (); file: // restore the surface
If (ddral! = Dd_ OK)
{
Break; // restored successfully, exit the loop
}
}
File: // if the previous surface flip has not occurred
If (ddrval! = Dderr_wasstilldrawing)
{
Break;
}
}
In this example, lpddsprimary specifies the main surface and its background buffer. After calling idirectdrawsurface: Flip, the main surface is switched with the background buffer. If the call is successful, dd_ OK is returned, and the program terminates the while loop. If dderr_surfacelost is returned, the surface may be lost. Use idirectdrawsurface: Restore to restore the surface. If the restoration is successful, call the idirectdrawsurface: Flip method again. If it fails, the program terminates the while loop and returns an error value. In addition, even if the call to idirectdrawsurface: Flip is successful, the exchange is not completed immediately, and it will not be completed until the previous surface swap in the system is completed. If the previous surface flip has not occurred, idirectdrawsurface: Flip returns dderr_wasstilldrawing. Idirectdrawsurface: Flip continues the loop until dd_ OK is returned.

6. Do not forget to use release () to release your object before you end the program.
If (lpdd! = NULL)
{
If (lpddsprimary! = NULL ){
Lpddsprimary-> release ();
Lpddsprimary = NULL;
}
Lpdd-> release ();
Lpdd = NULL;

OK! Success. Using the above methods, we can create a basic DirectX program, but don't be too happy. DirectX is much bigger than you think! What we are dealing with is nothing more than a bit (only including DirectDraw). To fully master the DirectX programming skills, you still have to make painstaking efforts. My advice is to learn more about computers, read more books, and read other people's programs. Only in this way can I quickly improve my skills.
Now, let's talk about it today. I hope this will help you. If you have a chance, I think we will meet again. Of course, you can also write here wj77@990.net, you make friends! See you next time!

Chengdu Electronic Science and Technology University 95080-5 Wang Jiang (610054)

 

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.