[ Pobierz całość w formacie PDF ]
.Animationis one of the most exacting, as well as the most rewarding, tasks that can be undertaken in agraphics application.DirectDraw makes possible animation in Windows by furnishing direct accessto the video hardware as well as specialized programming facilities, such as back buffers andflipping operations.In the following chapter we put this knowledge to work in animated applications.Chapter 13: Animation ProgrammingOverviewHere we tackle some of the practical problems related to animation in DirectDraw programming.Animation programming is both exacting and rewarding.DirectDraw makes possible animation inWindows by furnishing direct access to the video hardware as well as specialized programmingfacilities, such as back buffers and flip operations, described in Chapter 12.We now put thesetechniques to work to create effects that simulate movement.The objective is to make thesemovements appear smooth and natural.Animation programming finds use in the development ofcomputer games, simulations, in multimedia, and in many high-end graphics programs.Flipping TechniquesExclusive mode applications that use flipping animation start by initializing DirectDraw, setting adisplay mode, creating the flip chain, obtaining the corresponding pointers to the front and backbuffers, and setting up a timer mechanism that produces the desired beat.After thesehousekeeping chores are finished, the real work can begin, which consists of rendering theimagery to the back buffer, usually by means of blits from other surfaces in video memory oroffscreen.The design and coding challenge in creating an animated application using DirectDrawcan be broken down into two parts: the first one requires the minimum resources that enable theprogram to perform satisfactorily.The second one makes the best use of these resources toproduce the finest and smoothest animation possible.Background animationA typical computer game or real-time simulation often contains two different types of graphicsobjects: backgrounds and sprites.The backgrounds consist of larger bitmaps over which the actiontakes place.For example, in a flight simulator program there can be several background imagesrepresenting views from the cockpit.These may include landscapes, seascapes, and views ofairports and runways used during takeoff and landing.In a computer game that takes place in amedieval castle the backgrounds are the various castle rooms and corridors on which the actiontakes place.Sprites, on the other hand, are rather small, animated objects represented in two orthree dimensions.In the flight simulator program the sprites are other aircraft visible from thecockpit as well as the cabin instruments and controls that are animated during the simulation.Inthe computer game the sprites are medieval knights that do battle in the castle, as well as theobjects animated during the battle.Panning animationThe design and display of background images is relatively straightforward and not complicated.Inthis sense the most difficult processing consists of creating backgrounds that are larger than theviewport and using clipping and blit-time rectangles to generate panning and zoom effects.Figure13-1 shows a bitmapped galaxy, a portion of which is selected by a source rectangle.Figure 13-1: Source rectangle selection in panning animationThe project named DD Panning Demo in the book's CD-ROM demonstrates panning animation ofa background bitmap.In the program the source rectangle has the same vertical dimension as thebackground bitmap, which is 480 pixels.The image bitmap is 1280 pixels wide and the sourcerectangle is one-half that size (640 pixels).This creates a source window that can be moved 639pixels to the right from its original position.The white, dotted rectangle in Figure 13-1 representsthe source rectangle within the background bitmap.The program DDPan.cpp, which is located in the project DD Panning Demo, uses a simpleprocessing logic to demonstrate panning animation.The program action is to pan to the right untilthe right border of the image is reached, and then reverse the panning direction until the left borderis reached.The primary surface and a single back buffer are created and a clipper is installed inboth surfaces.The background bitmap in this case two colliding galaxies imaged by the HubbleSpace Telescope are stored in the panback.bmp file.This bitmap is twice as wide as theviewport; therefore, the source rectangle can be moved horizontally within the bitmap.The panningvariables and the display routine are coded as follows:// Global panning animation controlsRECT thisPan; // Storage for source// rectangleLONG panIteration = 0; // panning iteration counterLONG panDirection = 0; // 1 = left, 0 = right// ConstantsLONG PAN_LIMIT_LEFT = 1;LONG PAN_LIMIT_RIGHT = 639;.//*************************************************// Name: PanImage// Desc: Update back with a source rectangle that// is a portion of the background bitmap// and flip to create a panning animation//*************************************************static void PanImage(){thisTickCount = GetTickCount();if((thisTickCount - lastTickCount) Blt(NULL,lpDDSBackGrnd,&thisPan,DDBLT_WAIT,NULL);if(hRet != DD_OK){DDInitFailed(hWnd, hRet,"Blt() on background failed");return;}// Flip surfaceshRet = lpDDSPrimary->Flip(NULL, DDFLIP_WAIT);if(hRet != DD_OK){DDInitFailed(hWnd, hRet,"Flip() call failed");return;}return;}}The local function named PanImage() performs the panning animation.First it bumps and checksthe ticker counter.If the counter did not expire yet, execution returns immediately.Code thenchecks the panDirection variable.If the direction is 1, then panning is in the left-to-right cycleand the panIteration variable is decremented.If not, then panning is right-to-left and thepanIteration variable is incremented.When either variable reaches the limit, as defined in theconstants PAN_LIMIT_LEFT and PAN_LIMIT_RIGHT, the panning direction is reversed.Astructure variable named thisPan, of type RECT, is used to define the source rectangle for theblit.The panIteration variable is used to define the offset of the source rectangle within theimage bitmap.Because panning takes place on the horizontal axis only, and the display mode isdefined in the code, then the image size can be hard-coded into the thisPan structure members [ Pobierz całość w formacie PDF ]