VC.NET Game Development Chapter 3 Creating Direct3D Objects and Equipment Examples

xiaoxiao2021-03-06  49

Although people who see what I wrote, there are not many people, and some people have evaluated. This is a good thing.

This is also the record venue I have learned. Now that the book written by this person can only see it, only you have to touched it.

//*********************************************//Features : create Direct3D objects and devices examples // author: Zhang Xiaobin // time: 2005.3.30 // ************************************************************ *************** / / Prevent link error #Pragma Comment (lib, "d3d9.lib") # Pragma Comment (lib, "d3dx9.lib") # Pragma Comment (libment , "dinput8.lib") # Pragma Comment (lib, "dxguid.lib")

#include // windows program You don't include inevitable #include // is DX9 program, it is definitely necessary to include a header file.

/ / Next define a nameless name of the Windows class #define my_winclass_name "Direct3D" // Develop good habits first declare the HRESULT INITMY3D (hwnd hwnd); void cleanup (); LRESULT WINAPI MSGPROC (HWND HWND, UINT MSG, WPARAM WPARAM , LParam LParam; int apientry Winmain (Hinstance Hinstance, Hinstance Hprecinstance, LPSTR LPCMDLINE, INT NCMDSHOW);

// Recall the program when the program is Direct3D programming, we must first create a Direct3D object, and must finally release the object at the end of the program. / / In order to save the created Direct3D object and device, then we need the following two global variables.

// When this encounters the problem, it is not possible to compile it, then the following two are overwritten, don't be lazy, one word is knocked. // LPDIRECT3D9 PD3D = null; // Defines the pointer of the Direct3D object, initializes the empty // lpdirect3ddevice9 pdev = null; // Defines the pointer to the Direct3D device, the initial empty LPDIRECT3D9 PD3D = NULL; lpdirect3ddevice9 pdev = null;

// This is to start writing a method. Since it is a DX9 program to initialize the D3D environment / / --------------------------- ------------------------------ // method name: initd3d () // function: Initialize the D3D environment, the function creates Direct3D Objects and Direct3D devices. // Note: The function returns E_FAIL to indicate failure, returning E_OK means that it is necessary to mention some additional knowledge // In Direct 3D, two macros are provided to test the function to call success. // 1. Failed () ---- Test the error returned by the function, if the function returns any error message, the test is true. // 2. Chengceeded () - Does the function performs a successful test, such as the function execution, the value is true. / / -------------------------------------------------------------------------------------------- ---------

HRESULT INITMY3D (HWND HWND) {// Create a Direct3D object, which is the first step of using Direct3D (NULL == (PD3D = Direct3DCreate9 (D3D_SDK_VERSION))) Return E_FAIL; // Get the display mode of the current Windows desktop, save D3DDISPLAYMODE D3DDM; // to this to the macro mentioned in the defined D3DDM structure. // Optimistic to write ">" this symbol when writing to PD3D->, no drop-down list is the wrong, hurry to check the error. IF (failed (pd3d-> getadapterdisplaymode (d3dadapter_default, & d3ddm)) Return E_FAIL; / / Define Presend structure D3DPresent_Parameters D3DPP;

// Clear the structure, create a device, the structure value value is 0 means using the default value ZeromeMory (& D3DP, SIZEOF (D3DPP));

/ / Develop the created Direct3D is the device of the support window D3DPP.Windowed = true;

/ / Specify the switching effect is D3DSWAPEFFECT_DISCARD, that is, display the background surface to the front surface, and the information containing the original background surface is automatically lost D3dpp.swapeffect = D3DSWAPEFFECT_DISCARD;

// Point format of the background buffer consistent with the point format of the current display mode D3DPP.BACKBUFFERFORMAT = D3DDM.FORMAT;

// D3D object creates it to create a Direct3D device // 1. Use the primary graphic card to create a device, parameter d3dadapter_default. (If you insert two graphics cards in your computer, you will know clearly, it seems that Microsoft has encountered PCI_E's appearance) // 2. The device type is a hardware abstract device, using hardware acceleration, parameter D3DDEVTYPE_HAL // 3. Use the window created in front for the display of the target surface, the parameter hWnd // 4. Specifies the device processing capability of the vertex process vertex as software, which is to allow all of the graphics system support, parameters // D3DCREATE_SOFTWARE_VERTEXPROCESSING if (FAILED (pD3D-> CreateDevice (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, & d3dpp, & pDev))) { Return E_FAIL;} Return S_OK; // Put the above understands the last three lines, you know that it is dry}

/ / -------------------------------------------------------------------------------------------- -----------------------------------------------//method Name: cleanup () // Features: Release created Direct3D objects and devices // Note: C is not Java garbage collection mechanism, you have to apply the resources you apply after it is used, -------------------------------------------------- ------------------------------------------- void cleanup () {// The order in which the release is released first, release the object if (PDEV! = Null) PDEV-> release (); // Direct3D object first created, but need to release if (PD3D! = Null) PD3D-> Release (); Return; // If you don't understand why you release it, go to review your C } // ------------------------ -------------------------------------------------- ----------------------------------------- // method name: msgproc () / / Features: Windows detailed processing functions // Note: The message is another key points of the game program, which is used to handle the keyboard, mouse, thread response, and triggering events //, such as the protagonist sword chopping monster The collision of swords and monsters need to treat this news is that the monster is not hit or hit, and hurt how to calculate the damage.

/ / -------------------------------------------------------------------------------------------- -------------------------------------------------- ----------------- Lresult WinApi MsgProc (HWND HWND, UINT MSG, WPARAM WPARAM, LPARAM LPARAM) {Switch (MSG) {CASE WM_DESTROY: PostquitMessage (0); Return 0; Case WM_Paint: // Clear the rendered target surface, the color used is RGB (255, 255, 255), ie white PDEV-> Clear (0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB (255, 255), 1.0F, 0); // Rendered Target Surface Displays the window of the customer area PDEV-> Present (NULL, NULL, NULL, NULL); ValidateRect (hwnd, null); Return 0;} Return DefWindowProc (HWND, MSG, WPARAM, LPARAM);} // -------------------------------------------------- -------------------------------------------------- ---------------- // Method: WinMain () // function: Windows main function, the program starts, start // Note: You don't write him , You are over // -------------------------------------------- -------------------------------------------------- --------------------- Int apientry Winmain (Hinstance Hinstance, // Hinstance:) Hinstance Hprecinstance, // Hprevinstance: In order to maintain and 16 Bit Windows Application Compatibility LPSTR LPCMDLINE, // LPCMDLI Ne: Pointer INT ncmdshow in the command line parameter string // ncmdshow: Specifies an integer {// definition of an application window display mode, a class, the processing function of the specified message is MsgProc WNDCLASSEX WC = {Sizeof (WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle (NULL), NULL, NULL, NULL, NULL, MY_WINCLASS_NAME, NULL}; // Register this window REGISTERCLASSEX (& WC);

// Create the application window HWND hWnd = CreateWindow (MY_WINCLASS_NAME, "3D Game Programming - create Direct3D objects and equipment", WS_OVERLAPPEDWINDOW, 100,100,400,300, GetDesktopWindow (), NULL, wc.hInstance, NULL); // call the function 3D InitMy3D Objects and devices initialize, incoming parameters HWND IF (SUCCEEDED)) {// Display window showWindow (hwnd, sw_showdefault); UpdateWindow (hwnd); UpdateWindow (hwnd); UpdateWindow (hwnd); UpdateWindow

// Program main loop, the distribution of window messages, the processing of the message is completed in the MSGProc function to complete the MSG MSG; While (GetMessage (& MSG); DISPATCHMESSAGE (& MSG);}} / / Call the Cleanup function Clear 3D object and device cleanup ();

// Logout window classes unregisterclass (my_winclass_name, wc.hinstance); return 0;}

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

New Post(0)