First Step: Program Entry functionint WINAPI WinMain (hinstance hinstance,hinstance hprevinstance,lpstr lpcmdline,int ncmdshow)Main functions in the main function: Initialize the Window object, and register//Initialize the Window object Wndclassex WC; Wc.cbclsextra = 0; wc.cbsize = sizeof (wndclassex); Wc.cbwndextra = 0; Wc.hbrbackground = (hbrush) getstockobject (White_brush); Wc.hcursor = LoadCursor (hinstance, Idc_arrow); Wc.hicon = NULL; WC.HICONSM = NULL; Wc.hinstance = hinstance; Wc.lpfnwndproc =(WNDPROC) WinProc; Wc.lpszclassname = "MainClass"; Wc.lpszmenuname = NULL; Wc.style =Cs_hredraw | Cs_vredraw; Initialization parameter is complete, register the object RegisterClassEx (&WC);
After the window object is initialized, create the window handle HWND window = CreateWindow (Wc.lpszclassname, Apptitle.c_str (), Ws_overlappedwindow, Cw_usedefault, Cw_usedefault, screenwidth, screenheight, NULL, NULL, HINSTANCE, NULL);
Function prototypes: CreateWindow (Lpclassname, Lpwindowname, dwstyle, X, Y,nwidth, nheight, hWndParent, HMenu, hinstance, Lpparam);Lpclassname : Type LPCTSTR, specifying the window class name. lpwindowname: Type LPCTSTR, the window caption is displayed in the title bar.
dwstyle: The style created by the window. See "Notes-Window Style type" for specific window styles
x: Position x in the upper left corner of the windowy: Position y in the upper left corner of the windownwidth: Window sizenheight: Window sizehwndparent: Parent window HandlehMenu: Menu handlehinstance: Window instanceLpparam: Parameters that need to be passedThe window handle is created and you need to display the windowDisplay and Update window ShowWindow (window,ncmdshow); UpdateWindow window is finished, the window has been built, the message is received from the game, the message is taken from the message queue, and the message is entered in an infinite loop//Receive MSG; while (!gameover) {if (PeekMessage (&message, window, 0, 0, pm_remove)) {TranslateMessage (&message); DispatchMessage (&message); } game_run (window); } game_end ();
Step Two: Game initializationThe game initialization process is primarily the initialization of the
Direct3D, as well as the peripherals, while initializing the surface Initialize the version of Direct3D//Initialize Direct3D D3D = Direct3dcreate9 (d3d_sdk_version) According to the version number; Set the parameters of the Direct3D structure//Declaration parameter structure Body d3dpresent _parameters D3DPP; //clear parameters This step is critical, need to have this emptying process, otherwise, will be unable to create a device problemZeroMemory (&D3DPP, sizeof (D3DPP)); D3DPP. Windowed = (!fullscreen); D3DPP. SwapEffect = D3dswapeffect_discard; D3DPP. Backbufferformat = D3dfmt_unknown; D3DPP. BackBufferCount = 1; D3DPP. Backbufferwidth = width; D3DPP. Backbufferheight = height; D3dpp.hdevicewindow = hWnd; After the parameter setting is complete, the Direct3D device object needs to be created, and if the return value is D3D_OK, the creation success HRESULT result = D3d->createdevice ( D3dadapter_default, D3ddevtype_hal, HWnd, d3dcreate_software_vertexprocessing, &D3DPP, &d3ddev);
Initialize peripherals, the main use of the project keyboard and mouse two, Direct3D only support Microsoft's Xbox remote control lever, if needed to introduce xinput.h and Xinput.lib library filesInitialize input object//Initialize input object HRESULT result = Directinput8create (GetModuleHandle (NULL), Directinput_version, Iid_idirectinput8, (void**) &dinput, NULL); The GetModuleHandle () function, which is used to get the handle of an instance of the current program hinstance, because the handle usually appears only in WinMain. The second parameter is the version number, the version number exists in the dinput.h, and you need to redefine the macro.#define Directinput_version 0x0800, when defined, place the macro definition at the top of the file to prevent macro redefinition. The third parameter is a reference identifier for the DirectInput version. The current version of this value is IID_IDIRECTINPUT8. The fourth parameter is a pointer to the primary DirectInput object pointer (note that it is a double pointer). The last parameter is always null
After you create an input object, you need to create an input device and set the deviceCreating a keyboard device //creation of equipment dinput->createdevice (Guid_syskeyboard, &dikeyboard, null); //Set data format dikeyboard->setdataformat (&c_ Dfdikeyboard); //set the collaboration level dikeyboard->setcooperativelevel (HWnd, discl_nonexclusive | discl_foreground); //Acquisition Equipment Dikeyboard->acquire (); Create mouse device //Initialize mouse dinput->createdevice (guid_ Sysmouse, &dimouse, null) //Set data format dimouse-> setDataFormat (&c_dfdimouse); //set collaboration level dimouse-> SetCooperativeLevel (Hwnd, discl_nonexclusive | discl_foreground); //Acquiring equipment dimouse->acquire (); d3ddev->showcursor (false);
after the device is initialized, you need to read the Image Wizard from the file. //Declaration of picture class surface LPDIRECT3DSURFACE9 image = NULL; D3DXIMAGE_INFO info; HRESULT result = D3dxgetimageinfofromfile (Filename.c_str (), &info); if (result !=  D3D_OK) { return NULL; } //creating off-screen surfaces result = d3ddev-> Createoffscreenplainsurface (info. Width, info. Height, d3dfmt_x8r8g8b8, d3dpool_default, &image, null); if (RESULT != D3D_OK) { Return null; } result = d3dxloadsurfacefromfile ( Image, null, null, filename.c_str (), null, d3dx_default, d3dcolor_xrgb (0, 0, 255), null); if (RESULT != D3D_OK) return NULL; return image;first of all to introduce d3dx9.h this header file, this file in vs2013 in the SDK Library is not exist, so also need to download direct9.0c SDK (2010.6) This version of the SDK, and set the project properties, Point to the included directories and library reference directories to the installed Direct SDK files.
The code, first create an image of the surface, while declaring an info to get information about the picture in the file, through the image information, creating an image off-screen surface. D3dximage_info INFO; HRESULT result = D3dxgetimageinfofromfile (Filename.c_str (), &info); result = D3ddev->createoffscreenplainsurface (info. Width, Info. Height, D3dfmt_x8r8g8b8, D3dpool_default, &image, NULL);
When the off-screen surface is created, the file picture is read to the off-screen surface and then returned directly to the off-screen surface. result = D3DXLoadSurfaceFromFile (image, NULL, NULL, FILENAME.C_STR (), NULL, D3dx_default, D3DCOLOR_XRGB (0, 0, 255), NULL) ;
Two sprites are created on the off-screen surface, and through the GetBackBuffer function, you can get the pointer address of a real background. D3ddev->getbackbuffer (0, 0, D3dbackbuffer_type_mono, &backbuffer);
Step three: The game starts runningAfter the game begins to run, the first thing you need to get is the state of the keyboard or mouse and the key dimouse->getdevicestate (sizeof (Mouse_state), (LPVOID) &mouse_state); Dikeyboard->getdevicestate (sizeof), (LPVOID) &keys);D ik_a means that the key a,dikeyboard_a represents the address mapping unit
Fourth step: Draw the surface and renderDraw surface void Drawsurface (Lpdirect3dsurface9 dest, float x, float y, lpdirect3dsurface9 src) {//D3dsurface_desc DESC; The height and width of the source bitmap can be obtained by GETDESC, thus a rectangle Src->getdesc (&DESC) can be drawn; Creates a drawn rectangle rect src_rect = {0,0, (long) desc. Width, (Long) desc. Height};
RECT Dest_rect; Dest_rect.left = (long) x; Dest_rect.top = (long) y; Dest_rect.right = (long) x + desc. Width; Dest_rect.bottom = (long) y + desc. Height;
Draw surface to Source surface d3ddev->stretchrect (src, &src_rect, dest, &dest_rect, D3dtexf_none);}
//Use Colorfill to clear the contents of the surface, this is mainly for the real back buffer d3ddev->colorfill (BackBuffer, null, d3dcolor_xrgb (255, 255, 255)) //clear Clear the contents of the screen //d3ddev->clear (0, null, d3dclear_target, d3dcolor_xrgb (255, 255, 255) , 1.0f, 0); //Start Rendering if (D3ddev->beginscene ()) { //copy content from the off-screen surface into the back buffer drawsurface (Backbuffer, bomb.x, bomb.y, bomb_suf); //copy content from the off-screen surface into the back buffer Drawsurface (Backbuffer, bucket.x, bucket.y, bucket_suf); //End Render d3ddev->endscene (); //Display d3ddev->present (null, null, null, null) ; }
Source: http://download.csdn.net/detail/l_shiwei/9509600
Window Direct3D Game Development Primer--bomb Games