Construct DirectX application framework with MFC

zhaozj2021-02-08  200

Constructing the DirectX Application Framework Microsoft DirectX SDK is a software development tool for developing Windows platform games. It is mainly included in five components: DirectDraw DirectSound DirectPlay Direct3D and DirectInput, each component is not included in the function: DirectDRAW use Direct write technology speeds up the animation speed of the game; DirectSound controls the synthesis and playback of the game; DirectPlay makes the game have a multiplayer game function; Direct3D makes programmers more convenient to develop 3D games; DirectInput enables games to support more equipment (Now only support the joystick, mouse and keyboard). It can be said that DirectX SDK provides a function and base function that writes a game, so most Windows games use DirectX SDK. MFC (Microsoft Foundation Class) class library is a powerful Windows application development provided in Microsoft Visual C . Class, using these classes We can avoid dealing with cumbersome Windows APIs, and in Visual C we can also use ClassWizard to make a Windows message mapping for the MFC class, so if you can develop DirectX SDK applications, at least The following benefits:

You can use the ClassWizard of VC to make a map of Windows messages; add the readability of the program, and can use the ClassView used by VC to easily manage classes used; increase the reusability of program code, can be developed on the original basis More powerful applications;

Further, if we can develop a project wizard that generates the VC of the DirectX SDK Application Basic Framework, it is convenient for future development DirectX SDK to programs. Below, we will write a basic framework for a DirectX SDK application with Visual C . Second-Book DirectX SDK Application Basic Framework Using the following steps to create a basic framework for a DirectX SDK program: 1 Generate a basic dialog project file with Visual C MFC App Wizard (EXE), named DirectX, in Wizard Second steps Cancel the check box of the About Box and press the Finish button. 2 Delete two files generated in the DirectX engineering directory and remove the above two files in the File View of Visual C , press Ctrl W to start CDIRECTXDLG class, then remove IDD_directx_dialog. 3 Create two in ResSourseView. Document DirectXWnd.h (These two files are in the appendix of this article, please note that there is not the content between "// {" and "//}", otherwise the window information will not be mapped using ClassWizard), and Add them to the project. At this point, a CDIRECTXWND class based on CWND is added, which is the base class of our DirectX application. CDIRECTXWND class Create a window and generates a DirectDraw object LPDD associated with the window, and generates a display plane (lpfrontbuffer) and a display buffer plane (LPBackBuffer), which uses several virtual functions, if necessary, when necessary These functions can be overwritten.

4 Open DirectX.cpp, change the #include "directxdlg.h" to #include "directxwnd.h" and then modify the cdirectxapp :: initInstance () function as follows, in which the black body is to increase: BOOL CDIRECTXAPP :: Initlnstance ) {#ifdef_AFXDLL Enable3dControls (); // Call this when using MFC in a shared DLL #else Enable3dConteolsStatic (); // Call this when linking to MFC Statically #endif CdirectXWnd * pWnd = new CdirectXWnd (); PWnd-> Create ( "DirectXWnd Test"); m-pMainWnd = PWnd; PWnd-> UpdateWindow (); pwnd-> setfocus (); if (pWnd-> InitializeGame (640, 480, 8) = = false) {PWND-> destroyWindow (); return False;} MSG MSG; White (1) {IF (PEEKMESSAGE (& MSG, NULL, 0, 0, PM-Noremove) {if (! GetMessage (& MSG, NULL, 0, 0)) Return Msg.wParam; TranslateMessage & msg); DispatchMessage (& MSG);} else {if (pWnd-> BLSActive) {PWND-> UpdateFrame ();}}} Return Ture;} Compiles the program and runs, you can see a black screen window, press ESC or F12 can exit the program. So our basic framework has been established, although this framework is relatively simple, but we can develop a more powerful application framework on this basis. In order to facilitate the use of this frame, we can write a Custom App Wizard for the framework, of course, may not write, just copy the files in the project directory to another project directory. Three test framework now, we write a program in the following steps to test this framework: 1 Copy the project frame created to a new directory and open. Create a CWND-based class CTestWnd with Class View, and then replace all "CWND" strings in ctestwnd.h and ctestwnd.cpp files to "cdirectxwnd" and add the following strings in the ctestwnd.h file: #include " DirectxWnd.h ". 2 Open the DirectX.cpp file, add the # include" test.h "on the file header and replace all" CDirectXWnd "strings in the file into" ctestwnd "and save.

3 Add a virtual function updateFrame () for the CTestWnd class, which covers its base cdirectXWnd's updateFrame (): void ctestwnd :: updateFrame () {staic int x = 0, DX = 5; Staic Int Y = 0, DY = 5; HDC hdc; DDBL TFX ddbltfx; HRESULT ddrval; UpdateWindow (); Ddbltfx.swSize = sizedof (ddbltfx); Ddbltfx.dwFillColor = 0; Ddrval = lpBackBuffer -> Blt (NULL, // dest rect NULL, // src Surface NULL, / / ​​SRC RECT DDBLT_COLORFILL | DDBLT_WAIT, & DDBLTFX; if (DDRVAL! = DD_OK) {msg ("Fill Failed DDRVAL = 0x% 081X", DDRVAL); Return;} IF (lpBackBuffer -> Getdc (& HDC) = = DD- OK) {IF (x <0) DX = 5; if (x> 590) DX = -5; if (Y <0) DY = 5; if (Y> 430) DY = -5; x = DX; Y = DY; Ellipse (HDC, X, Y, X 50, Y 50); lpBackbuffer -> ReleaseDC (HDC);} while (1) {hResult DDRVAL; DDRVA = LP FrontBuffer-> FLIP NULL, 0); if (DDRVAL = = DD_OK) {Break;} if (DDRAVL = = DDERR_SURFACELOST) {if (! CdirectxWnd :: RESTORESURFCES ()) {Break;}}} (DDRAVL! = DderR_wasstilldrawing) {Break; }}} 4: Compile and run the program, a white ball will appear on the screen in the mobile appendix: Document DirectXWnd.h #if! Defined (DirectXWnd-h) #define directxWnd_H //DirectxWnd.h: H Eader file #include #pragma comment (lib, "DDRAW.

Lib ") // adding link ddraw.lib library class CdirectXWnd: public CWnd {// Construction public: CDirectXWnd (); // Attributes public: BOOL blsActive; // whether the application is activated protected: LPDERECTDRAW lpDD; // DirectDraw objects pointer LPDERECTDRAWSURFACE lpFrontBuffer; // DirectDraw primary buffer LPDERECTDRAWSURFACE lpBacdBuffer; // DirectDraw lookaside buffer int nBufferCount; // number lookaside buffer // Operations protected: void Msg (LPSTR fmt, ...); public: BOOL Create ( LPCSTR lpszAppName); // Create a form // Overrides virtual BOOL InitializeGame (UINT Gmodex, UINT GmodeY, UINT GBPP); virtual BOOL CleanSurface (); virtual void UpdateFrame (); virtual BOOL RestoreSurfaces (void); // {{AFX -VIRTUAL (CdirectXWnd) //}} AFX_VIRTUAL // Implementation public: virtual ~ CdirectXWnd (); // Generated message map functions protected: // {{AFX-MSG (CderectXWnd) afx-msg void OnActivateApp (BOOL bActive, hTASK hTask ); //}} AFX-msg declare-message-map ()}; // {{AFX-INSERT-LOCATION}} #ENDIF / / / DEFINED (DerestXWND-H) file DirectXWnd.cpp //directxWnd.cpp: Implementation file #include "stdafx.h" #include "DirectX.H" #include " DirectXWnd.h "# ifdef-deug #define new debug-new #undef this -file static char this -file [] = - file--; #ndif cdirectxWnd :: cDirectwxnd () {lpdd = null; lpfrontbuffer = null; lpBackBuffer = NULL; nBufferCount = 0; blsActive = TRUE;} CDirectXWnd :: ~ CdirectXWnd () {if (lpDD) {CleanSurface (); lpDD-> Release (); lpDD = NULL;}} BOOL CdirectXWnd :: Create (LPCSTR IpszAppName) {CString className = AfxRegisterWndClass (CS-DBLCLKS, :: LoadCursor (NULL, IDC-ARRWINDOW, className, IpszAppName, WS-VISIBLE | WS-SYSMENU | WS-POPUP, 0, 0, GetSystemMetrics (SM-CXSCREEN), GetSystemMetrics (SM-Cyscreen), NULL, NULL);} Bool CDirectxWnd :: InitializeGame (Uint Gmodex, Uint Gmodey, Uint GBPP) {HRESULT DDRVAL; DDRVAL = DirectDrawCreate (Null, & LPDD, NULL);

IF (DDRVAL! = DD-OK) {msg ("DirectDrawCreate Failed Err =% D", DDRVAL); Return False;} Ddral = LPDD-> SetCoopeRATIVELEVEL (M-HWND, DDSCL-EXCLUSIVE | DDSCL-FULLSCREEN); IF ( DDRVAL! = DD-OK) {msg ("setcooPERATIVELEVEL FAILED ERR =% D", DDRVAL); Return False;} DDRVAL = LPDD_> SetDisplayMode (Gmodex, Gmodey, GBPP); if (DDRVAL! -DD-OK) {msg ( "SetDisplayMode failed err =% d", ddrval0; return FALSE;} // check capabilites DDCAPS ddcaps; ddcaps.dwSize = sizeof (ddcaps); ddrval = lpDD-> GetCaps (& ddcaps, NULL);! if (ddrval = DD -Ok) {MSG ("setDisplaymode failed err =% d", ddrval); return false;} if (ddcaps.dwcaps & ddcaps_nohardware) {MSG ("No Hardware Support At All");} // default to double buffered on 1MB, Triple Buffered IF (NBuffercount = 0) {IF (DDCaps.dwvidMEMTOTAL <= 1024L * 1024L * (GBPP / 8) | | GMODEX> 640) {nbuffercount = 2;} else {nbuffercount = 3}} DDSurfaceDesc ddsd;:::::::::::::: ZeroMemory (& ddsd, sizeof (ddsd)); ddsd.dwSize = sizeof (ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAP S_COMPLEX; ddsd.dwBackBufferCount = nBufferCount_1; ddrval = lpDD_> CreateSurface (& ddsd, & lpFrontBuffer, NULL); if (! Ddrval = DD_OK) {Msg ( "CreateSurface failed err =% d", ddrval); return FALSE;} DDSCAPS ddscaps; ddscaps.dwCaps = DDSCAPS_BACKBUFFER; ddrval = lpFrontBuffer_> GetAttachedSurface (& ddscaps, & lpBackBuffer); if (ddrval = DD_OK!) {Msg ( "GetAttachedsurface failed err =% d", ddrval); return FALSE;} return TRUE;} void CdirectXWnd: : MSG (LPSTR FMT, ...) {Char Buff [256]; VA_LIST VA; LSTRCPY (BUFF, "DirectXWnd:"); VA_START (VA, FMT); WVSPrintf (& Buff [LSTRLEN (BUFF)], FMT, VA VA_END (VA);

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

New Post(0)