DirectX SDK |
The information in this section pertains only to applications written in C and C++. See Direct3D Immediate Mode Visual Basic Tutorials.
After creating the application window, the first object you will create is the DirectDraw object, which is required to set your application's cooperative level, and to create the surfaces for display and for use as the render target of a rendering device.
The Triangle sample starts performing initialization by creating a DirectDraw object and setting the application's cooperative level, as shown in the following code:
HRESULT Initialize3DEnvironment( HWND hWnd ) { HRESULT hr; hr = DirectDrawCreateEx( NULL, (VOID**)&g_pDD, IID_IDirectDraw7, NULL ); if( FAILED( hr ) ) return hr;
The preceding code creates a DirectDraw object by calling the DirectDrawCreateEx DirectDraw global function. It passes NULL in the first parameter to request that the function create a DirectDraw object for the active display driver. For hardware that doesn't support GDI, such as 3-D only hardware, you should explicitly specify the globally unique identifier (GUID) of the desired driver in the first parameter. These GUIDs are normally obtained through enumeration. The second parameter is the address of a global variable that DirectDrawCreateEx fills with the address of the IDirectDraw7 interface for the DirectDraw object, and the last parameter is set to NULL to indicate that the new object will not be used with COM aggregation features.
The sample continues by setting the application's cooperative level, as follows:
hr = g_pDD->SetCooperativeLevel( hWnd, DDSCL_NORMAL ); if( FAILED( hr ) ) return hr;
The sample sets the cooperative level by calling the IDirectDraw7::SetCooperativeLevel method. Setting the cooperative level effectively tells the system whether or not the application will render in full-screen mode or in a window. (Note that some hardware cannot render into a window. You can detect such hardware by checking for the absence of the DDCAPS2_CANRENDERWINDOWED capability flag when you call IDirectDraw7::GetCaps.) The code requests windowed cooperative level, also called the "normal" cooperative level, by including the DDSCL_NORMAL in the second parameter it passes to SetCooperativeLevel. The SetCooperativeLevel method can fail if another application already controls owns full-screen, exclusive mode.
Note You can include the DDSCL_FPUSETUP cooperative level flag to increase performance. For more information about this cooperative level flag, see DirectDraw Cooperative Levels and FPU Precision. For general information, see Cooperative Levels in the DirectDraw documentation.
Once you create the DirectDraw object and set the cooperative level, you're ready to prepare the surfaces that will be used to contain and display a rendered scene. The Triangle sample does this as discussed in Step 2.2: Set Up DirectDraw Surfaces.