DirectX Programming Technology - Do I want to learn game programming? Must read

zhaozj2021-02-08  208

DirectX programming technology

Everyone must be unfamiliar with DirectX because Microsoft has just launched a Windows window operating system, because the display interface adopts unified GDI, the programmer is prohibited directly from operating hardware, which makes the game program on the Win 3.x system is very slow, Can't be promoted at all. Microsoft In order to solve this problem, Microsoft has launched a WING graphics acceleration program, but because Wing lacks the support of the majority of game manufacturers, there is no popularity. So most of the games we played at the time were running under the DOS environment. Until 1995, with the birth of Windows 95, Microsoft officially announced its new generation of game development system DirectX. DirectX has a unified program interface that has been loved by major game manufacturers, and has expressed its support to the game development era of this Windows system. DirectX programming will involve some basic knowledge of WIN 95 and C , so you'd better have this foundation before this, if you are not very familiar with them, don't matter, you can learn together with this article, it can be described as a double carving.

Ok, gossip should start entering the topic: (1) DirectX consists of the following parts: 1. DirectDraw: Fast direct access by direct access to display memory and hardware and hardware acceleration technology. 2, DirectSound: Provide a soft and hardware sound mix and recording and reproduction. 3, Directplay: Provides the interaction between multiplayers, so you easily implement online interconnect. 4, Direct3D: Interactive 3D graphics technology. 5, DirectInput: Make your program to control the input devices such as mice, keyboards, and joysticks. 6, DirectSetup: Complete the installation of the DirectX driver. 7, AutoPlay: As long as you put the disc, you will run automatically. Finally, we talked and used all DirectX 5 things, so you need a DirectX 5 SDK, you can find it in some CDs, it is free.

(2) Preparation before formal programming To make VC 5 can be compiled correctly, link your program, you must first set the following in Microsoft Developer Studio so that the compiler can find the required link library and contain files. First, open a new Project Workspace, in the File menu, select New to create a new Win32 Application named 'MyDirectX1', and a new folder appears in the Workspace window. After the project is created, select Add to Project / Files in the Project menu to join the program to the new project (this step is described later). Then, set the path to the file to contain the file when compiling. In the Tools menu, select Options, pop up the Options dialog, select Directories, select Include Files in the Show Directories for list box, double-click the blank line at the bottom of the list box, 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, enter C: / DX5SDK / SDK / LIB (we assume DirectX 5 SDK installed in C: / DX5SDK /). Finally, set the library files required when link. Open SETTINGS / LINK in the Project menu, select General in the Category drop-down box, then add DDRaw.lib and Winmm.lib in the Object / Library module box. (3) Our first DirectX program we intend to use the example program of DirectX SDK as the foundation of the explanation, because there is such a few benefits: (1) Everyone has a correct source program After you have completed a process after a process, you will not know what it is, you will not know what it is. (2) I don't have to move all the code to the paper so that we can introduce DirectX's key content more detailed. (3) You can cultivate your ability to read others, and learn more easily later. In this case, what are you waiting for? Remember that we have established a new project 'MyDirectX1'? Now open Project / Add to Project / Files, browse directory DXSDK / SDK / Samples / DDEX1, and select all files in this directory, click 'OK' to add them to 'MyDirectX1'. Ok, let's open the 'ddex1.cpp' now.

1. Before using DirextDraw, you first need to create an entity of an object DirectDraw, which represents the display adapter of the microcomputer. The object entity can then be operated using the methods provided by the interface to complete the commands and tasks. So the beginning of the program first declares a DirectDraw object: LPDIRECTDRAW LPDD; then create this object as follows: HRESULT DDRVAL; DDRVAL = DirectDrawCreate (NULL, & LPDD, NULL); if (DDRVAL == DD_OK) {// DirectDRAW object LPDD creation success} Else {// DirectDRAW objects cannot be created} 2, next to set up the work mode of the collaboration layer. That is, the DirectDraw program is running, refers to the screen mode, window mode, and mod mode, etc. HRESULT DDRVAL; DDRVAL = LPDD-> SetCoopeRATIVELEVEL (HWND, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN); if (DDRVAL == DD_OK) {// full screen exclusive mode set success} ELSE {// setting failed} 3, set display mode, namely screen resolution And the number of colors. HRESULT DDRVAL; DDRVAL = LPDD-> setDisplayMode (640, 480, 8); if (DDRVAL == DD_OK) {// Successfully set the screen to 640x480x256 color} else {// display mode can not change}

4, create a surface. We can regard the surface as a memory buffer, all the operations are performed on the buffer. After the operation is completed, it is only necessary to turn the entire buffer into the main surface (memory) to complete the fast write screen. With this technique, you can achieve a smooth-free animation effect, which explains how to create a surface. The first step in creating a switchable surface is: Set the various parameters of the surface (SURFACE) in the DDSurfaceDesc structure, see the following:

// 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 line, the size of the DDSurfaceDesc structure is assigned to the DWSIZE member. Doing this prevents you from returning an invalid value when calling DirectDraw methods, and it is convenient for the extension of the DDSurfaceDesc structure in the future. Members DWFLAGS is used to indicate which areas fill in the DDSurfaceDesc structure is valid, which areas are invalid. In the program, we use dwflags to indicate you to use structure DDSCAPS (DDSC_CAPS), and you want to create a background buffer (DDSD_BACKBuffercount). The members dwcaps in routines include some flags in the DDSCAPS structure. In this way, members dwcaps define a major surface (DDSCAPS_PRIMARFACE), a pop-up surface (DDS-CPAS_PRIMARFACE), and a backup (DDSCAPS_COMPLEX). The so-called SURFACE means that the surface is composed of a number of sub-surfaces (SURFACE). Finally, the routine above defines a background buffer. This background buffer is our truly operational memory area. Once the operation is completed, you will then pop up from the background buffer to the main surface (Surface). Here, the number of background buffers is set to 1. But you can set any more background buffers, as long as your memory is allowed. The DDSurfaceDesc structure is filled, you can use this structure and LPDD to create a 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 that points to the main surface. IDirectDrawSurface :: GetAttachedSurface then calls made to the back buffer pointers: ddscaps.dwCaps = DDSCAPS_BACKBUFFER; ddrval = lpDDSPrimary-> GetAttachedSurface (& ddcaps, & lpDDSBack); if (ddrval == DD_OK) {// lpDDSBack directed back buffer} else {return If the iDirectdrawsurface :: GetAttachedSurface :: getattachedSurface is successful, LPDDSBACK points to the background buffer. Now we can make a variety of operations on the background buffer, write it in the content you want to display. For example, the game, let's transfer to the background image, then transfer to the image that should be displayed in various characters, and finally 'olve', 'text', etc. After all completed, the background buffer and main surface are turned out, and the things you want are displayed.

5, flip surface while (1) {HRESULT DDRVAL; DDRVAL = LPDDSPRIMARY-> FLIP (NULL, 0); if (ddrval == DD_OK) {BREAK; file: // Finished, exit cycle} file: // If the surface Lost IF (DDRVAL == DDERR_SURFACELOST) {ddral = lpddsprimary-> restore (); file: // Restore surface if (ddral! = DD_OK) {brek; // Restore success, exit cycle}} file: // If The one surface flip has not occurred in IF (DDRVAL! = DDERR_WASSTILDRAWING) {Break;}} example, LPDDSPRIMARY indicates the main surface and its rear buffer. After calling idirectdrawsurface :: flip, the main surface and the background buffer are exchanged. If the call is successful, return DD_OK, the program terminates the While loop; if you return DDERR_SURFACELOST, it may be that the surface is lost, you need to recover this surface with idirectdrawsurface :: RESTORE, if it is successful, then call IDirectDrawsurface :: flip method; if it fails, the program Terminate the While loop and return an error value. In addition, even if the idirectdrawsurface :: flip call is successful, the exchange is not immediately complete, it will wait until the system is completed before it is completed. If the previous surface flips has not occurred, IDirectDrawSurface :: FLIP returns DDERR_WASSTILLDRAWING. IDirectDrawSurface :: FLIP will continue to loop until returns DD_OK. 6. Don't forget to use Release () to release the object you created before the process. IF (LPDD! = Null) {if (LPDDSPRIMARY! = Null) {LPDDSPRIMARY-> Release (); lpddsprimary = null;} lpdd-> release (); lpdd = null;

OK! Come. Using the above means we can make basic DirectX programs, but don't be too early, DirectX is much larger than you imagined! What we have involved is just a peer (only DirectDraw). To fully master DirectX programming skills, you also have to pay hard efforts. My suggestion is Multi-machine, read more books, read more people's procedures, only this can quickly improve your level. Ok, today we will talk about it first, I hope this will help you. If there is a chance, I think we will meet. Of course, you can also write a letter here wj77@990.net, everyone makes a friend! See you next time!

转载请注明原文地址:https://www.9cbs.com/read-3077.html

New Post(0)