Scrolling and PanningHow do you get the background to move as viewpoint changes?LaMothe ExamplesBOB AnimationGame_Init( )Slide 6Slide 7Handling Terrain and ScrollingGame_Main( )Slide 10Slide 11Slide 12Slide 13Slide 14BOB FunctionsBOB ProcessSmooth ScrollingSlide 18TilingTiling DemoGlobalsSlide 22Slide 23Slide 24Enormous Sparse WorldsSparse WorldsSlide 27Slide 28Slide 29Slide 301Scrolling and PanningCIS 487/587Bruce R. MaximUM-Dearborn2How 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][max X][max Y]Page 0Page 4Page 5 Page 6 Page 7Page 1Page 2Page 3Display [Page 1]3LaMothe Examples4BOB Animation#include <ddraw.h> // directX includes#include "T3DLIB1.H" // include the library header// demo globalsBOB skelaton; // the player skelaton // animation sequences for bobint 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}, };5Game_Init( )// initialize directdrawDDraw_Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP); // load in each page of the scrolling backgroundfor (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 paletteSet_Palette(bitmap8bit.palette);6Game_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 planefor (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 direction7Game_Init( )// set up stating state of skelatonSet_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 edgesRECT screen_rect = {0,0,screen_width,screen_height};lpddclipper = DDraw_Attach_Clipper(lpddsback,1,&screen_rect); // hide the mouseShowCursor(FALSE);8Handling Terrain and Scrolling// global declarations// default screen size#define SCREEN_WIDTH 640 // size of screen#define SCREEN_HEIGHT 480#define SCREEN_BPP 8 // bits per pixel#define START_GLOW_COLOR 152 // starting color index to glow#define END_GLOW_COLOR 159 // ending color index to glow#define FLOOR_COLOR 1169Game_Main( )// test direction of motionif (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 if10Game_Main( )elseif (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 if11Game_Main( )// apply downward gravity to player, so player follows terrainskelaton.y+=1;// only animate if player is movingif (player_moving){ Animate_BOB(&skelaton);} // end if// lock surface, so we can scan itDDraw_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 floorwhile(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 unlockDDraw_Unlock_Back_Surface();12Game_Main( )// draw the skelatonDraw_BOB(&skelaton, lpddsback); // this performs the animation for the glowing rockstatic int glow_count = 0; // increment action counter and testif (++glow_count > 5) { // rotate the colors Rotate_Colors(152,159); // reset the counter glow_count = 0; } // end if // animate the floorglow.peGreen = rand()%256;Set_Palette_Entry(FLOOR_COLOR, &glow);13Game_Main( )// test for page flip rightif (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 if14Game_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 surfacesDDraw_Flip();// sync to 30 fpsWait_Clock(30);15BOB Functions•Several functions defined to make blitting faster by using DirectDraw surfaces–Create_BOB–
View Full Document