初始
DDraw_BOB bob;
BOB textures;
tool mytool;
char buffer[80];
BITMAP_FILE bitmap8bit;
int world_x = 0, // current position of viewing window
world_y = 0;
// use an array of string pointers, could have used an
// array of chars or int, but harder to initialize
// the characters '0' - '9' represent bitmaps 0-9 in some texture memory
char *world[21] =
{
"111111111111111111111111111111",
"100000000000000000000000000001",
"100002222220000000000000077701",
"100002222223333333333000077701",
"100002222227777777773000070001",
"100002222227777777773000070001",
"100000000377777777773000070001",
"107777700377777777773000070001",
"177777770377777777773000770001",
"107777700377777777773007700001",
"100777770377777777773777000001",
"100000707377777777773000000001",
"100007777377777777773000000001",
"100000000302222777773000000001",
"100000000332222777773000000001",
"100000000002222333333000000001",
"100000666666666666666666600001",
"100000800000000000000000800001",
"100000800000000000000000800001",
"100000000000000000000000000001",
"111111111111111111111111111111",
};
在初始化中
int Game_Init(void *parms = NULL, int num_parms = 0)
{
// this is called once after the initial window is created and
// before the main event loop is entered, do all your initialization
// here
srand( GetTickCount());
//初始化DDRAW
char filename[80]; // used to build up files names
ddraw->DDraw_Init( main_window_handle, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP );
bob.Load_Bitmap_File( & bitmap8bit, "SCROLLTEXTURES.BMP");
ddraw->Set_Palette( bitmap8bit.palette );
if( ! bob.Create_BOB( ddraw->getlpdd(), & textures, 0, 0, 64, 64, 10, BOB_ATTR_VISIBLE | BOB_ATTR_MULTI_FRAME, DDSCAPS_SYSTEMMEMORY ) )
return ( 0 );
for (int index = 0; index < NUM_TEXTURES; index++)
bob.Load_Frame_BOB(&textures,&bitmap8bit,index,index%4,index/4,BITMAP_EXTRACT_MODE_CELL);
// unload the texture map bitmap
bob.Unload_Bitmap_File(&bitmap8bit);
RECT screen_rect = {0,0,SCREEN_WIDTH,SCREEN_HEIGHT-32}; // 32 pixels at the bottom for controls
ddraw->DDraw_Attach_Clipper(ddraw->getbackSurface(),1,&screen_rect);
ShowCursor( FALSE );
// return success or failure or your own return code here
return(1);
}
在帧循环中,
int Game_Main(void *parms = NULL, int num_parms = 0)
{
int index, index_x, index_y; // looping vars
int start_map_x,start_map_y, // map positions
end_map_x,end_map_y;
int offset_x, offset_y; // pixel offsets within cell
// check of user is trying to exit
if (KEYDOWN(VK_ESCAPE) )
PostMessage(main_window_handle, WM_DESTROY,0,0);
mytool.Start_Clock();
ddraw->DDraw_Fill_Surface( ddraw->getbackSurface(), 0 );
// check for movement (scrolling)
if (KEYDOWN(VK_RIGHT))
{
if ((world_x+=4) >= 1280)
world_x = 1279;
} // end if
else
if (KEYDOWN(VK_LEFT))
{
if ((world_x-=4) < 0)
world_x = 0;
} // end if
if (KEYDOWN(VK_UP))
{
if ((world_y-=4) < 0)
world_y = 0;
} // end if
else
if (KEYDOWN(VK_DOWN))
{
if ((world_y+=4) >= 896)
world_y = 895;
} // end if
// compute starting map indices by dividing position by size of cell
start_map_x = world_x/64; // use >> 6 for speed, but this is clearer
start_map_y = world_y/64;
// compute end of map rectangle for best cast i.e. aligned on 64x64 boundary
end_map_x = start_map_x + 10 - 1;
end_map_y = start_map_y + 7 - 1;
// now compute number of pixels in x,y we are within the tile, i.e
// how much is scrolled off the edge?
offset_x = -(world_x % 64);
offset_y = -(world_y % 64);
// adjust end_map_x,y for offsets
if (offset_x)
end_map_x++;
if (offset_y)
end_map_y++;
// set starting position of first upper lh texture
int texture_x = offset_x;
int texture_y = offset_y;
// draw the current window
for (index_y = start_map_y; index_y <= end_map_y; index_y++)
{
for (index_x = start_map_x; index_x <= end_map_x; index_x++)
{
// set position to blit
textures.x = texture_x;
textures.y = texture_y;
// set frame
textures.curr_frame = world[index_y][index_x] - '0';
// draw the texture
bob.Draw_BOB(&textures,ddraw->getbackSurface());
// update texture position
texture_x+=64;
} // end for map_x
// reset x postion, update y
texture_x = offset_x;
texture_y += 64;
} // end for map_y
ddraw->DDraw_Flip();
//while( FAILED( ddraw->DDraw_Flip() ) );
mytool.Wait_Clock( 30 );
// do nothing -- look at pretty picture
// return success or failure or your own return code here
return(1);
} // end Game_Main
如下图所示