Getting started with DirectX Game Development

Source: Internet
Author: User
Reprinted from guigangsky and finally edited guigangsky

 

[Document Introduction] 

Today we are going to have access to the awesome DirectX. It is several times faster than Windows GDI and can be used in different languages and platforms.

[Body]

  ☆Introduction

Today we are going to have access to the awesome DirectX. It is several times faster than Windows GDI and can be used in different languages and platforms. It supports from pixel rendering to advanced 3D images, from playing sound to digital music, from Keyboard Control to anti-seismic controller ...... It gives you everything you need for game programming (a little exaggerated ). Of course, it is huge and requires several books to cover all of it. Don't worry about the countless things I 've learned here. After all, I pushed you to the starting line.

To read this chapter, you need the knowledge of the previous chapters and the knowledge of C language. As we also need to talk about Component Object Model (COM), it is the basis of object-oriented systems, you 'd better have a little more C ++ knowledge. It doesn't matter. I will take care of you when talking about it. Remember that using DirectX does not require much C ++ knowledge. Start!

  ☆What is DirectX?

DirectX is the game maker's API (application development interface ). It is a set of software that allows you to directly control computer hardware. If your hardware supports DirectX and you use hardware to accelerate your program, it means a word-fast. You don't have to worry about your hardware knowledge, and you won't really get in touch with them. We use hardware abstraction layer (HAL) and hardware simulation layer (EL) to ensure device independence and make your program run normally.

DirectX consists of many components, each of which has a specific purpose. The component DirectDraw is the most important one, because all graphics need to use it. It is a 2D graphics engine, and 3D graphics cannot be separated from it. DirectDraw is what we will talk about today. Other components are:

▲Directsound: provides sound mixing and playback for hardware and software.

▲Directmusic: Process message-based musicData. It supports the Instrument Digital Interface (MIDI) and provides creation tools for creating interactive music.

▲Directplay: enables communication with <strong class = "KGB" onmouseover = "isshowads = true; isshowads2 = true; keygate_ads.move (this ,"", "% u901a % region % u4e00 % u4e2a % u7b80 % u5355 % u7684 % u95ee % u5019 % region % 20 helloserver % 20% u6765 % u4ecb % u7mcm", "-100879 ", "application", "% u5f00 % u53d1 % u9ad8 % u6027 % u80fd % u7f51 % u7edc % u7a0b % u5e8f", "HTTP % 3A // timeout", event) "Style =" border-top-Width: 0px; font-weight: normal; border-left-width: 0px; border-bottom-width: 0px; cursor: hand; color: # 0000ff; border-right-width: 0px; text-Decoration: underline "onclick =" javascript: window. open ("http://s4.17luntan.com/ClickPortal/WebClick.aspx? Id =-100879 & K = % u5e94 % u7528 % u7a0b % u5e8f & siteid = Response & url = http % 3A/Taobao ");" onmouseout = "isshowads = false; isshowads2 = false "> application connection is possible.

▲Direct3d: a 3D graphics package that provides an advanced Retained Mode Interface, allowing you to implement a complete 3D graphics system. It also contains a low-level instant mode interface that gives applications full control over the rendering pipeline.

▲Directinput: provides support for input devices including game rods, mouse, keyboard, and game controllers. It also provides feedback to game devices.

▲Directsetup: provides a simple installation process for DirectX. It simplifies <strong class = "KGB" onmouseover = "isshowads = false; isshowads2 = false; isshowgg = true; intextads_gglayer =" _ u663e_u793a "; keygate_ads.showggads (this, "_ u663e_u793a", event) "style =" border-top-width: 0px; padding-Right: 0px; padding-left: 0px; font-weight: normal; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; cursor: hand; color: # 0000ff; padding-top: 0px; bord Er-right-width: 0px; text-Decoration: underline "onclick =" javascript: window. Open ("http://pagead2.googlesyndication.com/pagead/iclk? Sa = L & AI = BPBau96QmSIGQGIuSvwPh543BCvf6ozDnuMTuA8CNtwHAuAIQAhgCIM-BhwooFDgAUPW507QFYJ2J4YHoBaABwdiG9wOyAQ1uZXdzLmNzZG4ubmV0yAEB2gEqaHR0cDovL25ld3MuY3Nkbi5uZXQvbi8yMDA1MDcxMi8yMzY2MS5odG1sqQJQBo3lHDCDPsgC78OjAqgDAegD5wPoA7ID9QMABAAAiAQBkAQBmAQA & num = 2 & adurl = http://www.ledzd.com & client = ca-pub-3578421812762734 "); ggkwclickstat (" show "," www.ledzd.com "," AFC ", "2000084115"); "onmouseout =" isshowgg = false; intexta Ds_gglayer = "_ u663e_u793a"> display and audio driver processes and ensure that there is no hardware or software conflict.

▲Autoplay: allows you to create a disc that can be automatically installed once the drive is inserted. Autoplay is not unique to DirectX because it is part of Microsoft Win32 API.

The Component Object Model (COM) is the basis of DirectX. There are some skills to create COM objects-Don't ask me how to do it-but you know that it is good to know a little bit. I just want to give a brief description. If you are interested, check the details yourself! You may be confused in the next section, but it doesn't matter. You don't need to be clear about what I said. After all, our goal is to use COM objects, which is much easier to create.

  ☆Component Object Model (COM)

The COM interface is the basis of DirectX technology, and there is no DirectX without COM. (Don't worry, you only need to have a rough understanding of COM technology to use DirectX-as long as you follow certain steps when writing DirectX applications, you can even use DirectX without learning about com.

Most DirectX APIs are based on the com structure. Com provides the most solid foundation for software modularization and software reuse. Its most important concept is interface, which is the most basic method of software reuse. More specifically, interfaces are a series of standardized descriptions of operations, that is, interface specifications.

All com interfaces are inherited from the iunknown interface, and the iunknown interface is the root of all com interfaces. The iunknown interface has three methods:

· QueryInterface (): This method queries a new interface and returns it if the new interface exists.

· Addref (): This method increments the reference count value by 1 when the interface or other applications are connected to this COM object.

· Release (): This method reduces the reference count of the COM object by 1. When the reference count is reduced to 0, the COM object is automatically released.

All COM objects have these three methods. Although DirectX applications generally do not need to consider the reference count issue, the reference count does exist, and it has been automatically completed by DirectX. All we need to do is create a DirectX object and call the release method after use to release the reference.

  ☆Settings

To create a program using DirectX, you need to do three main tasks. The first thing is the COM object itself, which is included in the. dll file. These. DLL files need to be registered in windows, which has been completed when the DirectX software package is installed. These objects are the interfaces we use to create a DirectX application, such as idirectdraw. However, this is not enough because using DirectX directly on the com layer is frustrating and boring. We hope we can solve it more easily. Using a static library (. Lib file) is a good solution. It is part of the DirectX software package and you can get it from Microsoft for free. It has a "package" function to make your work easier. To use different components of DirectX, You need to link different static libraries. For example, if you want to use the DirectDraw component, you need ddraw. Lib.

Finally, you also need the drectx header file, which contains the original function, macros, constants, and various types you need. For DirectDraw, this header file is ddraw. h.
Make sure that you have used the correct file version, and you have to make the compiler contain the software development kit directory. The specific method is:

First, click the tool menu, select options, and then click directories. In the show directories for combo box drop-down menu, select include files to add a new directory. Enter your DirectX path. (For example, C: dxsdkinclude) and move it to the first place in the list, so that the first one looks for it during compilation (to prevent searching for old versions ). Then select the library files in the drop-down menu of the show directories for combo box. The method is the same as that before, just change include to Lib. Now you have set DirectX. You still need to manually add some library files to your project, but don't worry, I will talk about it later. We will use DirectX 7.0.

  ☆Directx version

You may think there is nothing to say about the version number, but let's talk about it. Microsoft has created incredible technologies in DirectX, but it does not mean it is not confusing. For each DirectX version, not all interfaces are upgraded again and again. Therefore, although DirectX has 7 versions (directx8.0 is about to be released when I write an article), DirectDraw does not have 7 versions. When directx6 is the latest version, DirectDraw's latest interface version is idirectdraw4, not idirectdraw6. The latest version is directx7, so we need to use idrectdraw7. It's strange, isn't it? I think you have understood what I mean. Don't be confused about what you will see later.

The last thing. When I write this article, directx7 is the latest available version, but you may already have directx8, and you may have heard that DirectDraw will not be upgraded, it is replaced by DirectX graphics, which is a powerful graphic API. But if DirectDraw is not upgraded, it means that we don't learn about it. After all, we can't do without COM. If you want to use the directx8 interface to write 2D games, you need to use 3D methods to create 2D views. It sounds great. Yes, it does, because using 3D interfaces will give you more hardware support, such as Alpha hybrid. However, this is precisely a problem. If the machine does not have the corresponding hardware device, the program will run at a slower speed.

DirectDraw is easy to learn. Because the 3D graphics in DirectX are based on DirectDraw, 3D applications are executed in the DirectDraw environment, and few applications use 3D. Most programs use 3D functions to model some objects, while other objects, such as backgrounds and Genie, are rendered in 2D images. Therefore, DirectDraw is used in this series. I don't know much about directx8, so I can only give a detailed introduction to direcx7. In general, you can't use DirectX without DirectDraw.

  ☆Directdraw Overview

To use DirectDraw in your program, you must do at least four things:

1. Create a DirectDraw object.

2. Set the collaboration level.

3. Set the display mode and color depth (Full Screen mode ).

4. create at least one DirectDraw surface.

Before proceeding to the above steps, let's take a look at the meaning of each step. First, create a DirectDraw object, which means we need to create a pointer to the idirectdraw7 interface. This is simple, isn't it? There are three ways to implement it. You can directly use com or one of the two DirectDraw functions. The three methods have their own merits. We will introduce them in detail later. Second, set the operation level. This may be fresh to you. Collaboration is a concept created by windows as a multi-task operating system. This means that all running programs should always inform windows of the resources they will or are using, which will ensure that the resources you want will not be allocated to other applications by windows. Don't worry, there is a very simple function to complete it.

Third, are you familiar with it? If you want to write a full screen program, usually a game program, you need to set the display mode and color depth. It is usually not a good idea to do this in a Windows application because it can cause problems in the simultaneous running of other programs. When you end your program, you certainly need to restore to the State before the change. To set full screen mode, you only need to call a separate DirectDraw function. After the program is completed, it must be restored to the original state.

Finally, it is also the most important concept of the DirectDraw surface. The operating surface is the whole of DirectDraw. In short, the DirectDraw surface is a linear memory area used to store image data. The size of the DirectDraw surface is defined in pixels in width and height. So you can think that the surface is a rectangular area used for drawing. It has its own interface called idirectdrawsurface7. There are three main surfaces that we will use in this chapter and the next chapter respectively.

· Main surface: Each DirectDraw application has a main surface. The main surface is equivalent to the user's <strong class = "KGB" onmouseover = "isshowads = false; isshowads2 = false; isshowgg = true; intextads_gglayer =" _ blank "; keygate_ads.showggads (this, "_ u663e_u793a_u5668", event) "style =" border-top-width: 0px; padding-Right: 0px; padding-left: 0px; font-weight: normal; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; cursor: hand; color: # 0000ff; paddin G-top: 0px; border-right-width: 0px; text-Decoration: underline "onclick =" javascript: window. Open ("http://pagead2.googlesyndication.com/pagead/iclk? Sa = L & AI = Bpb0J96QmSIGQGIuSvwPh543BCuTu4k6s9OTEBcCNtwHAuAIQBRgFIM-BhwooFDgAUI7C3YcEYJ2J4YHoBbIBDW5ld3MuY3Nkbi5uZXTIAQHaASpodHRwOi8vbmV3cy5jc2RuLm5ldC9uLzIwMDUwNzEyLzIzNjYxLmh0bWypAlAGjeUcMIM-yALI2KMGqAMB6APnA-gDsgP1AwAEAACIBAGQBAGYBAA & num = 5 & adurl = http://www.sunwaylcd.com/ LCD .asp&client=ca-pub-3578421812762734 "); ggkwclickstat (" display "," www.sunwaylcd.com "," AFC "," 2000084115 "); "onmouseout =" isshowgg = false; Intextads_gglayer = "_ u663e_u793a_u5668" "> display. Its content is visible. Similarly, the main surface is to set the width and height based on the display mode of the monitor.

· Rear buffer zone: the rear buffer zone is closely following the surface of the main surface, but it is invisible. It is the main reason why the animation does not flash. Generally, you draw every frame in the back buffer and copy the content in the back buffer to the main surface to display it. Because it closely follows the main surface, its size is the same as that of the main surface. (You can understand the relationship between the upstairs and the downstairs)

· Off-screen Buffer: it is like the back buffer, but it is not close to the main surface. Although you can use it to do anything, it is often used to store bitmaps. You can set the size of the off-screen buffer. The only limit is the size of your memory.

The DirectDraw surface can be established in the system memory or directly in the display memory. If you are all built on the display memory, the speed will be the best, for example, the system memory will be slower. If you store one surface in the display memory and the other in the system memory, performance may be compromised, especially when there is a disgusting bandwidth between the display card and the motherboard. In short, if you can build the surface in the display memory, you may try your best.

Okay, we have a little bit of recognition. Let's see how to do it! There is a plan, we will build a full screen mode, 16-bit color, 640x480 resolution program, I will tell you all you need to do. But before you start, you need to know a little about Windows programming. You have read the previous chapters and should be familiar with the window creation. Because this is a full screen program, you do not need any window control, so your window style should use ws_poup | ws_visible. Are you ready? All right. Let's go!

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.