![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The SampleApp::InitInstance() function terminates by calling the RMApp::InitInstance() function, which looks like this: BOOL RMApp::InitInstance() { ASSERT(m_pMainWnd); m_pMainWnd->ShowWindow( SW_SHOWNORMAL ); m_pMainWnd->UpdateWindow(); return TRUE; } The first thing that the function does is check to make sure that the m_pMainWnd data member has been assigned. The ASSERT macro will terminate the program with a message box if the data member is NULL. Next, the ShowWindow() and UpdateWindow() member functions are called. These functions are inherited from the CWnd class and are required for window initialization. Finally, TRUE is returned. Should an error occur in the InitInstance() function, the function should return FALSE to notify MFC that the application was unable to perform its initialization. The OnCreate() Member FunctionThe OnCreate() function is inherited by RMWin from the CWnd class. The function is called during window creation (after the window has been created but before the window is displayed) and provides a good place to initialize Direct3D. The code looks like this: int RMWin::OnCreate(LPCREATESTRUCT lpCreateStruct) { HRESULT r; r = Direct3DRMCreate( &d3drm ); if (r!=D3DRM_OK) { TRACE("failed to create D3DRM object\n"); return -1; } return 0; } The function uses the Direct3DRMCreate() function to initialize a pointer to the Direct3DRM interface. The d3drm data member is used throughout the program to invoke various Direct3DRM interface functions. The value returned by Direct3DRMCreate() is then checked. Any value other than D3DRM_OK indicates an error. If an error is detected, a diagnostic message is displayed, and the function returns with a return value of 1. This signals MFC to abort the window creation. If all goes well, then 0 is returned.
The CreateDevice() FunctionThe CreateDevice() member function is responsible for creating the standard Direct3D interfaces, the most important of which is the Direct3DRMDevice interface. CreateDevice() is a private member function, so it performs its task outside of the knowledge of classes that are derived from RMWin. The function is shown in Listing 4.1. Listing 4.1 The RMWin::CreateDevice() function. BOOL RMWin::CreateDevice() { HRESULT r; r = DirectDrawCreateClipper( 0, &clipper, NULL ); if (r!=D3DRM_OK) { TRACE("failed to create D3D clipper\n"); return FALSE; } r = clipper->SetHWnd( NULL, m_hWnd ); if (r!=DD_OK) { TRACE("failed in SetHWnd call\n"); return FALSE; } RECT rect; ::GetClientRect( m_hWnd, &rect ); r = d3drm->CreateDeviceFromClipper( clipper, GetGUID(), rect.right, rect.bottom, &device ); if (r!=D3DRM_OK) { TRACE("CreateDeviceFromClipper failed\n"); return FALSE; } device->SetQuality( D3DRMRENDER_GOURAUD ); HDC hdc = ::GetDC( m_hWnd ); int bpp = ::GetDeviceCaps( hdc, BITSPIXEL ); ::ReleaseDC( m_hWnd, hdc ); switch ( bpp ) { case 8: device->SetDither( FALSE ); break; case 16: device->SetShades( 32 ); d3drm->SetDefaultTextureColors( 64 ); d3drm->SetDefaultTextureShades( 32 ); device->SetDither( FALSE ); break; case 24: case 32: device->SetShades( 256 ); d3drm->SetDefaultTextureColors( 64 ); d3drm->SetDefaultTextureShades( 256 ); device->SetDither( FALSE ); break; } r = d3drm->CreateFrame( NULL, &scene ); if (r!=D3DRM_OK) { TRACE("CreateFrame(&scene) failed\n"); return FALSE; } if (CreateScene()==FALSE) { AfxMessageBox("CreateScene() failed"); return FALSE; } ASSERT( camera ); ASSERT( viewport ); return TRUE; } The first thing that you should know about the CreateDevice() function is that it assigns values to three RMWin data members: clipper, device, and scene. These values are protected members of RMWin, so they can be accessed by the SampleWin class members. This allows SampleWin to use the interfaces that RMWin creates. The first data member to be assigned is the clipper member. The DirectDrawCreateClipper() function is used to retrieve a pointer to a DirectDrawClipper interface. A clipper is a DirectDraw construct that manages window updates. Clippers allow DirectDraw and Direct3D applications to behave properly in windowed environments. The clipper object gets its name from the fact that overlapping windows must be drawn according to which portions of the window are visible. This rectangular clipping is handled by Windows, and the clipper object represents this functionality. After the clipper has been created, the DirectDrawClipper SetHWnd() member function is called. This introduces the clipper to the window that it will be managing. The m_hWnd data member that is used as an argument is a window handle that is initialized by MFC. Next, the GetClientRect() and CreateDeviceFromClipper() functions are called. GetClientRect() is a Win32 function that retrieves the dimensions of the client area of the window (the client area is the inner portion of the window and does not include the window frame and menu). The CreateDeviceFromClipper() member function is a Direct3DRM function that is used to create a pointer to the Direct3DRMDevice interface. The CreateDeviceFromClipper() function takes several arguments and deserves a closer look. The function call appears as it does in the CreateDevice() function: r = d3drm->CreateDeviceFromClipper( clipper, GetGUID(), rect.right, rect.bottom, &device );
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |