Scrolling and Panning CIS 487 587 Bruce R Maxim UM Dearborn 1 How do you get the background to move as viewpoint changes Page scrolling engines blit each off screen image onto the display as it is needed 0 0 Page 0 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Display Page 1 max X max Y 2 LaMothe Examples 3 BOB Animation include ddraw h include T3DLIB1 H demo globals BOB skelaton directX includes include the library header the player skelaton animation sequences for bob int skelaton anims 8 4 0 1 0 2 0 4 1 4 0 4 2 4 0 8 1 8 0 8 2 8 0 12 1 12 0 12 2 12 0 16 1 16 0 16 2 16 0 20 1 20 0 20 2 20 0 24 1 24 0 24 2 24 0 28 1 28 0 28 2 28 4 Game Init initialize directdraw DDraw Init SCREEN WIDTH SCREEN HEIGHT SCREEN BPP load in each page of the scrolling background for index 0 index 3 index build up file name sprintf filename LANDSCAPE d BMP index 1 load the background Load Bitmap File bitmap8bit filename create and load the reactor bitmap image Create Bitmap landscape index 0 0 640 480 Load Image Bitmap landscape index bitmap8bit 0 0 BITMAP EXTRACT MODE ABS Unload Bitmap File bitmap8bit end for index set the palette to background image palette Set Palette bitmap8bit palette 5 Game Init load in all the frames for the skelaton if Create BOB skelaton 0 0 72 74 32 56x72 BOB ATTR VISIBLE BOB ATTR MULTI ANIM DDSCAPS SYSTEMMEMORY return 0 load the frames in 8 directions 4 frames each each set of frames has a walk and a fire frame sets are loaded in counter clockwise order looking down from a birds eye view or the x z plane for int direction 0 direction 8 direction build up file name sprintf filename QUENSP d BMP direction skelsp load in new bitmap file Load Bitmap File bitmap8bit filename Load Frame BOB skelaton bitmap8bit 0 direction 4 0 0 BITMAP EXTRACT MODE CELL Load Frame BOB skelaton bitmap8bit 1 direction 4 1 0 BITMAP EXTRACT MODE CELL Load Frame BOB skelaton bitmap8bit 2 direction 4 2 0 BITMAP EXTRACT MODE CELL Load Frame BOB skelaton bitmap8bit 3 direction 4 0 1 BITMAP EXTRACT MODE CELL unload the bitmap file Unload Bitmap File bitmap8bit set the animation sequences for skelaton Load Animation BOB skelaton direction 4 skelaton anims direction end for direction 6 Game Init set up stating state of skelaton Set Animation BOB skelaton 0 Set Anim Speed BOB skelaton 4 Set Vel BOB skelaton 0 0 Set Pos BOB skelaton 16 256 right above the floor set clipping rectangle to screen extents so mouse cursor doens t mess up at edges RECT screen rect 0 0 screen width screen height lpddclipper DDraw Attach Clipper lpddsback 1 screen rect hide the mouse ShowCursor FALSE 7 Handling Terrain and Scrolling global declarations default screen size define SCREEN WIDTH define SCREEN HEIGHT define SCREEN BPP define START GLOW COLOR define END GLOW COLOR define FLOOR COLOR 640 480 8 size of screen bits per pixel 152 starting color index to glow 159 ending color index to glow 116 8 Game Main test direction of motion if KEY DOWN VK RIGHT move skelaton skelaton x 2 dx 2 dy 0 set motion flag player moving 1 check animation needs to change if skelaton curr animation SKELATON EAST Set Animation BOB skelaton SKELATON EAST end if 9 Game Main else if KEY DOWN VK LEFT move skelaton skelaton x 2 dx 2 dy 0 set motion flag player moving 1 check animation needs to change if skelaton curr animation SKELATON WEST Set Animation BOB skelaton SKELATON WEST end if 10 Game Main apply downward gravity to player so player follows terrain skelaton y 1 only animate if player is moving if player moving Animate BOB skelaton end if lock surface so we can scan it DDraw Lock Back Surface call the color scanner with FLOOR COLOR the color of the glowing floor try to center the scan on the feet of the player make sure feet stay in contact with floor while Color Scan skelaton x 16 skelaton y 24 skelaton x skelaton width 32 skelaton y skelaton height 12 FLOOR COLOR FLOOR COLOR back buffer back lpitch push the skelaton upward to keep it on the floor skelaton y 1 end while done so unlock DDraw Unlock Back Surface 11 Game Main draw the skelaton Draw BOB skelaton lpddsback this performs the animation for the glowing rock static int glow count 0 increment action counter and test if glow count 5 rotate the colors Rotate Colors 152 159 reset the counter glow count 0 end if animate the floor glow peGreen rand 256 Set Palette Entry FLOOR COLOR glow 12 Game Main test for page flip right if skelaton x SCREEN WIDTH skelaton width 1 bump back regardless skelaton x dx test for page flip if curr page 2 scroll to next page to right curr page reset character to left edge of screen skelaton x skelaton width 1 end if end if 13 Game Main else page flip right if skelaton x skelaton width 1 bump back regardless skelaton x skelaton width 1 test for page flip if curr page 0 scroll to next page to left curr page reset character to right edge of screen skelaton x SCREEN WIDTH skelaton width 1 end if end if flip the surfaces DDraw Flip sync to 30 fps Wait Clock 30 14 BOB Functions Several functions defined to make blitting faster by using DirectDraw surfaces Create BOB Destroy BOB Draw BOB Load BOB Animate BOB Drawing BOB s off screen is more efficient than using secondary memory since they are on flippable pages 15 BOB Process Set up DirectDraw Load 256 color map Create blank BOB Load image into BOB Draw BOB Destroy BOB when finished 16 Smooth Scrolling One way to achieve smooth scrolling would be creating a huge DirectDraw offscreen surface and use the blitter to display a sliding portion of it blitter backbuffer primary 17 Smooth Scrolling Another approach is to use lots of homogeneous and small tiles that can be fit together like building blocks You might also decide to create an index to help you decide which tile to display 18 Tiling world 4 4 0 1 2 3 4 3 1 3 4 3 6 5 6 9 6 9 7 8 0 7 9 pic Viewport contains clipped and rendered bitmaps 19 Tiling Demo Uses 9 bit maps for tiles textures World is 21 by 31 Most of the work is done in Game Init and Game Main 20 Globals BOB textures texture memory int world x 0 world y 0 current position of viewing window use an array of string pointers could have used an characters 0 9 represent bitmaps 0 9 in some texture memory char world 21 111111111111111111111111111111 100000000000000000000000000001 100002222220000000000000077701 100002222223333333333000077701 100002222227777777773000070001 100000666666666666666666600001 100000800000000000000000800001 100000800000000000000000800001 100000000000000000000000000001
View Full Document
Unlocking...