Game development basis (3)
Chapter III DirectX SDK Introduction The first section About DirectX SDKMicrosoft DirectX provides a very good application interface that contains a high-performance, real-time application source code. DirectX technology will help you construct the next generation of computer games and multimedia applications. Its content includes DirectDraw, DirectSound, DirectPlet, Direct3D, and DirectInput, etc., which are primarily applied to graphics, sound programs, etc. Due to DirectX, the performance of the application running in Windows can be comparable to the application performance running under the DOS or gaming platform, even more than they. It will provide a robust, standardized operating environment for your Windows game development. DirectX includes two parts: runtime section (Runtime) and SDK. When DirectX is developed, these two parts are used, but only the runtime sections are used when the DirectX application is run. In Windows NT 4.0 and above, the DirectX runtime section is included, and Win95 is not. But Win95 can easily get the DirectX runtime section. Windows NT 4.0 previous versions cannot run the DirectX program. Many DirectX-based applications and games contain DirectX runtime sections. It currently has five versions: 1, 2, 3, 5, and 6 (no version 4). Different versions have different runtime sections, but the new version of the runtime section can cooperate with the old version of the application, ie upward compatibility. Most popular games are based on version 5. The second section of DirectX5 SDK has a DirectX SDK including the development of all examples and help files needed to develop DirectX applications, but these are optional resources, and the required files are header files (.h files) and library files (.lib file) ). Getting DirectX SDK is difficult than getting a section of the running period. Windows NT 4.0 and WIN95 do not take DirectX SDK, you have to get the SDK can pass the following three ways: * Buy Visual C 5.0 (including DirectX SDK) * Access the DirectX Download page of the Microsoft Web site * Become MSDN (Microsoft Development Network) User SDK can also be obtained from the Microsoft Web site, the download amount is large, especially when dialing, it is possible to need a night. Become an MSDN user is a good way to get SDK unless you have access to upgrading and its operating system for program development privileges. SDK is provided by MSDN Level 2 and above. The third element object model (COM) DirectX is implemented according to Microsoft's COM (Component Object Model, the component object model) specification. COM is designed to provide fully portable, safe, upgraded software structure. COM uses an object-oriented model, which is more stringent than the model used in languages such as C . For example, COM objects are often accessed through member functions, and there is no public data member. COM supports the support of inheritance and C is also limited. A lot of DirectX is created as an instance of a COM object. You can see this: Object is like a black box, which represents hardware, which needs to be liaped with the application via an interface. Commands transmitted and received by COM interface are called "Method". For example, way iDirectdraw2 :: getDisplayMode is sent via interface IDirectDraw2, which is capable of getting a display mode of the current display adapter from the DirectDRAW object. The COM object provides actual functionality, and the COM interface provides a method of accessing a functionality. The COM object cannot be accessed directly. In contrast, all accessors are done through the interface.
This rule is so powerful, so that we cannot give any COM objects in name. We can only give the interface name for accessing objects. All COM interfaces are derived from the iUnknown interface. The "i" logo is often used to name the COM interface (which represents Interface is interface). The IunkNown interface provides 3 member functions, and all COM interfaces are thus inherit of these functions: ● addRef (): Make the internal reference value of the object plus one. For example, when you create a new interface, the constructor will automatically call AddRef (). ● queryinterface (): Let COM objects to query specific interfaces. For example, an upgraded COM object provides an additional interface, not a modified version of the interface. The queryinterface () function can be used to determine the old interface to determine if the new interface is supported. If the object being queried does not support a problematic interface, the pointer to the interface is returned. ● Release (): Make the internal reference value of the object. You should use this function when the interface's pointer will be exceeded or by using the interface pointer to end with the interface pointer. Each DirectX interface of an object represents a device, such as iDirectDraw2, iDirectSound, and iDirectplay. The DirectX object model symbolically provides a primary object for each device, and other supported function objects are derived from the primary object. For example, the DirectDRAW object represents a display adapter, which you can create a DirectDrawSurface object to represent the memory. Similarly, DirectSound objects represent sound cards and create DirectSoundBuffer objects represent sound information on the sound card. Any COM interface mode can be called in the C program. The following example creates a plane, using the DirectDraw object IDIRECTDRAW2 :: CreateSurface mode: Ret = LPDD-> LPVTBL-> CreateSurface (LPDD, & DDSD, & LPDDS, NULL); here LPDD represents the DirectDRAW object related to the new plane. This mode fills a flat type structure (& DDSD) and returns a pointer to the plane (& lpdds). The same functions are implemented with C : RET = LPDD-> CreateSurface (& DDSD, & LPDDS, NULL) Section 4 DirectDrawDirectDraw is a component in DirectX SDK, which allows users to operate directly, support hardware coverage, memory and memory reversal . DirectDraw is provided while providing these features and supports Microsoft Windows-based applications and device drivers. DirectDraw serves as a software interface that supports Windows GDI (Image Device Interface) and provides a function of direct display devices. It provides software development methods such as games, Windows subsystems. It also supports many display hardware, including from simple SVGA displays to advanced hardware performance (supported image shear, extension, and non-RGB color format). The interface design allows your application to know the feature of the hardware used, so that they can support their hardware acceleration. About DirectDraw, there will be more detailed explanations in the future. Section 5 DirectSoundMicrosoft DirectSound API is a voice component of the DirectX Platform Software Development Tool (SDK) programmer guide. DirectSound provides hardware acceleration, direct operation of sound devices. While providing these functions, it maintains compatibility with current devices. The new version of DirectSound can query hardware performance in operation to determine the optimal operation mode, and also have functional characteristics such as sound, low delay mixing.
Its new feature also includes a Porperty Sets that can be utilized even if DirectSound does not directly support new hardware features. DirectSound is a sound hardware by "Hardware Abstract Layer (HAL)", which is an interface implemented by the sound device. HAL provides the following features: requests and release of sound hardware control; describe the characteristics of sound hardware; implement specific operations under the conditions allowed by hardware; the report operation failed when the hardware is not allowed. DirectSound can automatically use hardware acceleration, including hardware mixing and hardware buffering. Your application does not have to query hardware and program characteristics, and you can query DirectSound in the runtime section to obtain a full description of the sound device characteristics, and then optimize using different methods according to the presence or absence of each characteristic. You can also specify a sound buffer that accepts hardware acceleration. The figure below shows the relationship between DirectSound and other sound components in the system: About DirectSound, there will be more detailed explanations in the future. The main role of DirectPlayDirectPlay is to simplify the application between applications. DirectPlay technology not only provides a program, but also has unrelated communication and transfer protocols and online services, but also realizes independence with the transmission server and gaming servers. In case of a network-based network does not support a certain way, DirectPlay contains the necessary code to imitate it. DirectPlay's Service Provider structure enables applications to be isolated from the network. The application obtains the network functionality (such as delay, bandwidth, etc.) by querying DirectPlay, thereby adjusting the communication mode accordingly. The following figure shows the structure of the DirectPlay Service Provider: The first step using DirectPlay is to choose which Service Provider used to use it, which determines the network or protocol that will be used for communication. The protocol can be a TCP / IP, an IPX or two-computer connection cable between the TCP / IP, LAN or two computers. DirectPlay provides two very useful connection management methods: iDirectplay3 :: EnumConnections lists all connections available to the application; idirectplay3 :: InitializeConnection initializes a specific connection. After determining the connection of the network, in order to facilitate understanding the following, let's take a look at the concepts: First, from the session. DirectPlay Session is a communication channel between several computers. An application can only be communicated with other computers only after they become part of a session. There are two ways to implement: list all the existing session in a network and then add one of them; or create a session and wait for other computers to join. When the application becomes part of the session, it can create a Player and deliver information with other Players in the Session. There is a computer as a host in each session. The host is the owner of the session and has the only power to change the session attribute. The figure below shows a session model: SESSION has two types: peers and client / server types. Player is an object on the logic concept in the session that can send and receive messages. There is no representation of the entity computer in the DirectPlay Session. Player can be used as a local Player (on your own computer), or as a remote Player (on other computers). Only after having at least one Player, the computer can send and receive messages. Every computer can have more than one local Player. Group is a collection of Players on logical concepts.
By creating a group, an application can send a message to it makes all of the Player received. DirectPlay provides ways to manage Group and membership. The figure below shows a logical structure of a session content: If a computer can call IDirectplay3 :: EnumSession to list all the session you can get after completing the connection. Then, iDirectplay3 :: Open adds a session according to the contents of the session cache, or create a new session. Next, you can create Player and Group. In the game, there are more frequent, large amounts of messaging throughout the Message ManaGment. In different types of session, the flow of messages is different. In the peer type, the transmission of the message is done directly between the various Players. When sending a message to Group, you want to specify a MultiCast Server in Group, transfer the message to the Group in Group. In the session of the client / server type, all Players are only directly communicating with the server, and then transmitted to other clients. The following figure shows such a relationship: Seventh section DirectInputDirectInput is used to support an input device including mouse, keyboard, and joystick, etc., and even have an advanced input / output device. It is also based on COM. DirectInput objects are provided by the input device. Each corresponding device has a so-called "object instance", that is, individual controls, such as buttons, pressing the axial direction of the Board and the joystick. Each button on the keyboard can be used as an object instance of the keyboard object. Even if the program runs in the background, DirectInput can also respond to the input device. In general, first create a DirectInput object (via DirectInputCreat), representing a DirectInput subsystem. Then, you can list the input devices you can get in your system, and then create a DirectInputDevice object for these individual devices (via iDirectInput :: CreateDevice). This object is used to obtain information about the device, set their attributes and get data from them. An example of using a mouse is given below. You can find these codes in the Scrawl.c file under the CD Example. First create a mouse device DirectInput // LPDIRECTINPUT g_pdi; // has been initialized LPDIRECTINPUTDEVICE g_pMouse; HRESULT hr; hr = g_pdi-> CreateDevice (GUID_SysMouse, & g_pMouse, NULL); if (FAILED (hr)) {Complain (hwnd, hr, " CreateDevice (sysmouse) ");} CreateDevice method has three parameters: The first is the global unique flag Guid_SYSMOUSE, here is the mouse; the second is the idirectinputDevice interface pointer type, if this call is successful, it points to Available mice; generally do not use a COM polymer, the third parameter is NULL.
Then, set the format of the mouse information: hr = g_pmouse-> setDataFormat; if (Failed (HR)) {Complain (HWND, HR, "SETDATAFORMAT (SYSMOUSE, DFDIMOUSE)"; Return False;} Setting mouse behavior: HR = g_pmouse-> setCooperativeEveLevel (hwnd, discl_exclusive | discl_Foreground); // The following two markers implements exclusive IF (Failed (HR)) {Complain (HR)) {Complain (hr)) {Complain (hr)) {Complain (hr)) {Complain (hr, hr, "setcooperadiveelevel ")") "); Return False;} To use event notification (Event Notification) to install the mouse action and enter the buffered mouse to read, this requires some settings. First, create an event and link it with the mouse. When a hardware interruption occurs, you can tell the mouse device object to have new data. // Handle g_hevtmouse; // Global variable g_hevtmouse = creteEvent (0, 0, 0, 0); if (g_hevtmouse == null) {Complain (hwnd, getLastError (), "createEvent"); return false;} hr = g_pmouse -> setEventNotification; if (failed (hr)) {Complain (hwnd, hr, "setEventNotification (sysmouse)); Return False;} Now you need to set buffers size. To initialize a DipropdWord structure, many of this is irrelevant, the key is the last, DWDATA, which determines the number of can accommodate the project in the buffer. #define DINPUT_BUFFERSIZE 16DIPROPDWORD dipdw = {// the header {sizeof (DIPROPDWORD), // diph.dwSize sizeof (DIPROPHEADER), // diph.dwHeaderSize 0, // diph.dwObj DIPH_DEVICE, // diph.dwHow}, // The data dinput_buffersize, // dwdata}; then follow the flag of the attribute you want to change, transfer the header address to the idirectinputDevice :: setProperty mode: hr = g_pmouse-> setProperty (Diprop_buffersize, & dipdw.diph); if (Failed (HR) )) {Complain (HWnd, HR, "SET BUFFER SIZE (SYSMOUSE)"); Return False;} to this, the setting ends. Then start the mouse based on the requirements of the program. Section 8 Direct3DDirect3d provides 3D video hardware operations that are unrelated to hardware. You can choose one of two modes (immediate mode and retention mode). The immediate mode is a low-level 3D API than the reserved mode, which is constructed on the former. Immediate mode is suitable for developing high-quality multimedia applications that communicate with acceleration hardware under the underlying. The retention mode is applied to the 3D scene for real-time manipulation.