[ Pobierz całość w formacie PDF ]
.dwBackBufferCount.The dwBackBufferCount member of our surface description has been set to 1, toindicate that one back buffer should be created in addition to our primary surface.We have added DDSCAPS_FLIP and DDSCAPS_COMPLEX to the surface capabilities,indicating that the surface will be complex (consist of multiple surfaces), and thatthe surfaces can be exchanged, or flipped.The additional surfaces are created at the same time as your primary surface, but they arenot returned by your call to CreateSurface().They are instead created as attachedsurfaces.You can retrieve a pointer to the back buffer by using theGetAttachedSurface() command, as shown in Listing 3.2.LISTING 3.2 Retrieving the Back Buffer1: // Fetch back buffer interface2:3: ddscaps.dwCaps=DDSCAPS_BACKBUFFER;4: ddrval=lpDDSPrimary->GetAttachedSurface(&ddscaps,&lpDDSBack);5: if (ddrval!=DD_OK) {6: ErrStr=Err_CreateSurf;7: return FALSE;8: }Using Page FlippingWhen you have created your surfaces, double buffering is easy to implement.The pri-mary difference is that you blit to the back buffer rather than the primary surface.Whena frame is completed, you use the Flip() function from the primary surface, as shown inthe following:lpDDSPrimary->Flip(NULL,DDFLIP_WAIT);06 1634xCH03 11/13/99 11:00 AM Page 45Moving On Grabbing Control of the System 45The Flip() command causes the primary surface to be exchanged with the back buffer.The flip might not occur immediately; instead, waiting for the next blanking intervalbefore actually performing the exchange.The syntax for Flip() is as follows.The Syntax for Flip()HRESULT IDirectDrawSurface7::Flip(LPDIRECTDRAWSURFACE7 lpDDSurfaceTargetOverride,DWORD dwFlags);The Flip() function causes DirectDraw to exchange surfaces, flipping the first backbuffer to the screen and moving the screen surface to the end of the chain.If this func-tion is successful, DD_OK is returned.3Parameters:lpDDSurfaceTargetOverride Surface to be exchanged with the primary sur-face.Normally this is set to NULL, which causesthe function to set the first back buffer as the vis-ible surface.dwFlags Flags that govern the behavior of this function.There are a variety of optional flags, which canbe found in the SDK documentation.Normally,this is set to DDFLIP_WAIT, which causes thefunction to wait until the flip is properly set up ifthe hardware is waiting for another operation tocomplete.Slide Show A Simple Surface-FlippingApplicationTo apply what you have learned, you will begin by creating a slide-show application.Theprogram will allow the user to step through a series of screen shots from a 3D actiongame, using the arrow keys for navigation.The images for this example can be found with the sample code for this hour on the CD-ROM.There are seven images, labeled SLIDE001.BMP through SLIDE007.BMP.,,SYNTAX06 1634xCH03 11/13/99 11:00 AM Page 4646 Hour 3Setting Up the ApplicationTo begin with, you will need to create a new Win32 project, just as you did in the sectionSetting Up the Project in Hour 2.Include the code from the bitmap_surface() functionthat you created in the last hour, as well as the error string assignments.After the project is created, begin by loading the appropriate headers and establishing theglobal variables that are needed for this application (see Listing 3.3).LISTING 3.3 Setting Up the Application1: /*-------------------------------------------------------------*/2: // Sample Application3: //4: // Chapter 35: //6: // Learn DirectX in 24 Hours7: // by Robert Dunlop8: //9: // Copyright (C) 199910: /*-------------------------------------------------------------*/11:12: //------ Include Files ------//13:14: #include stdafx.h15: #define INITGUID16: #include17: #include18: #include19:20: //------ Window Class Information ------//21:22: static char szClass[] = XmplHr3Class ;23: static char szCaption[] = Example - Hour 3 ;24:25: //------ Global Interface Pointers ------//26:27: LPDIRECTDRAW7 lpDD=NULL;28: LPDIRECTDRAWSURFACE7 lpDDSPrimary=NULL;29: LPDIRECTDRAWSURFACE7 lpDDSBack=NULL;30:31: //------Define number of images and set up list of file names ------//32:33: #define IMAGE_COUNT 734:35: char file_names[IMAGE_COUNT][256] = { slide001.bmp ,36: slide002.bmp ,37: slide003.bmp ,38: slide004.bmp ,39: slide005.bmp ,40: slide006.bmp ,06 1634xCH03 11/13/99 11:00 AM Page 47Moving On Grabbing Control of the System 4741: slide007.bmp ,42: };43:44: //------ DirectDraw Surfaces for Image Storage ------//45:46: LPDIRECTDRAWSURFACE7 lpSlides[IMAGE_COUNT];47:48: //------ current image displayed------//49:50: int cur_image=0;There are a few additions to the global code of this application, compared to the previousexample.Here is an overview of the changes:A new surface pointer, lpDDSBack, is provided for the back buffer.3The constant IMAGE_COUNT indicates the number of images in the slide sequence.The array file_names[] contains the names of the files to be loaded.lpSlides[] will contain an array of pointers to surfaces containing the slideimages.The index of the currently displayed image is stored in cur_image.Initializing the ApplicationYour initialization of the application window and the creation of a DirectDraw7 willremain the same as in the last hour.When that has been achieved, you are ready toswitch to full-screen graphics, setting the cooperation level, and then the display modeas in Listing 3.4.LISTING 3.4 Establishing Full-Screen Display1: // Set our cooperative level2:3: ddrval = lpDD->SetCooperativeLevel( hWnd,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );4: if (ddrval != DD_OK) {5: ErrStr=Err_Coop;6: return FALSE;7: }8:9: // Set the display mode10:11: ddrval = lpDD->SetDisplayMode( 640, 480, 16, 0, 0);12: if (ddrval !=DD_OK) {13: ErrStr=Err_DispMode;14: return FALSE;15: }06 1634xCH03 11/13/99 11:00 AM Page 4848 Hour 3Next, you will create your primary surface and back buffer (see Listing 3.5).This willform a flipping chain that you can use to preload the next slide onto the back buffer,avoiding any tearing that would otherwise occur.LISTING 3.5 Creating the Flipping Chain1: // Create the primary surface with 1 back buffer2:3: DDSURFACEDESC2 ddsd;4: DDSCAPS2 ddscaps;5: ZeroMemory(&ddsd,sizeof(ddsd));6: ddsd.dwSize = sizeof( ddsd );7: ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;8: ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |9: DDSCAPS_FLIP |10: DDSCAPS_COMPLEX;11: ddsd
[ Pobierz całość w formacie PDF ]