Platform SDK: DirectX |
This section pertains only to application development in Visual Basic. See Direct3D Immediate Mode C/C++ Tutorials.
After you create a DirectDraw object and set the cooperative level, you can create the surfaces that your application will use to render and display a scene. Exactly how you create your surface depends largely on whether or not your application will run in a window or in full-screen mode.
Full-screen Application Note Applications that will run in full-screen mode can create surfaces as shown in the preceding code examples. Most often, these applications should take advantage of page flipping, a feature only available in full-screen, exclusive mode. Using full-screen mode is the topic of a subsequent tutorial. In this case, instead of explicitly creating two surfaces, you can create a flipping chain of surfaces with a single call. For more information, see Creating Complex Surfaces and Flipping Chains.
The Triangle tutorial application, designed to run in a window, starts by creating a primary surface that functions as the display:
' Prepare and create the primary surface. g_ddsd.lFlags = DDSD_CAPS g_ddsd.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE Set g_ddsPrimary = g_dd.CreateSurface(g_ddsd)
The description for the primary surface does not contain information about dimensions or pixel format, these traits are assumed to be the same as the display mode. For example, if the current display mode is 800×600, 16-bit color, DirectDraw ensures that the primary surface matches. After creating the primary surface, you can create the render-target surface. In the case of the tutorial application, the render-target surface is a separate off-screen surface created as follows:
' Now create the render-target surface. We are reusing g_ddsd here. g_ddsd.lFlags = DDSD_HEIGHT Or DDSD_WIDTH Or DDSD_CAPS g_ddsd.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_3DDEVICE ' Use the size of the form to determine the size of the render target ' and viewport rectangle. g_dx.GetWindowRect Me.hWnd, g_rcDest ' Set the dimensions of the surface description g_ddsd.lWidth = g_rcDest.Right - g_rcDest.Left g_ddsd.lHeight = g_rcDest.Bottom - g_rcDest.Top ' Create the render-target surface Set g_ddsBackBuffer = g_dd.CreateSurface(g_ddsd) ' Cache the dimensions of the render target. We'll use ' it for blitting operations. With g_rcSrc .Left = 0: .Top = 0 .Bottom = g_ddsd.lHeight .Right = g_ddsd.lWidth End With
The preceding code creates an off-screen surface equal to the dimensions of the application window. The off-screen surface is used to cache the image that will later be blitted to the primary surface. There is no need to create a larger surface, because the dimensions of the window dictate what is visible to the user. (This code also initializes a global variable, g_rcDest, that is used to set up the viewport and to track the application window size and position.) As the preceding code excerpt shows, you must include the DDSCAPS_3DDEVICE capability for any surface that will be used as a render target. This capability instructs the system to allocate additional internal data structures for 3-D rendering. As when creating the primary surface, the pixel format for the off-screen surface is assumed to be the same as the display mode when it is not provided in the surface description.
Note Applications that will use a depth buffer should create one and attach it to the render target surface at this point. For simplicity, this tutorial does not employ a depth buffer, but they are covered in Tutorial 2: Adding a Depth Buffer and in Depth Buffers.
After creating the primary and render-target surface, you can create and attach a DirectDrawClipper object to the display surface. Using a clipper defines your screen boundaries, freeing you from handling cases when the window is partially obscured by other windows, or when the window is partially outside of the display area. Thus, clippers are not needed for applications that run in full-screen mode. The Triangle tutorial application uses the following code to create a clipper and associate it with the display window:
' Create a DirectDrawClipper and attach it to the primary surface. Dim pcClipper As DirectDrawClipper Set pcClipper = g_dd.CreateClipper(0) pcClipper.SetHWnd Me.hWnd g_ddsPrimary.SetClipper pcClipper
Having created the basic DirectDraw objects, you can move on to setting up the essential Direct3D objects that will be needed to render the scene. The Triangle tutorial demonstrates this task in Step 2.3: Initialize Direct3D.