int MAPDATA[MAPWIDTH*MAPHEIGHT] = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,
70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,
92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,
110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,
142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,
158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,
174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,
190,191,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,
42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,
63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,
84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,
104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,
120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,
136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,
152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,
168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
184,185,186,187,188,189,190,191
};
ScrollY = GAMEWORLDHEIGHT - SCREEN_HEIGHT;
void UpdateScrollPosition()
{
SpeedY = -4;
//update horizontal scrolling position and speed
ScrollX += SpeedX;
if (ScrollX < 0)
{
ScrollX = 0;
SpeedX = 0;
}
else if (ScrollX > GAMEWORLDWIDTH - WINDOWWIDTH)
{
ScrollX = GAMEWORLDWIDTH - WINDOWWIDTH;
SpeedX = 0;
}
//update vertical scrolling position and speed
ScrollY += SpeedY;
if (ScrollY < 0)
{
SpeedY = 0;
}
//if (ScrollY > GAMEWORLDHEIGHT - WINDOWHEIGHT)
//{
// ScrollY = GAMEWORLDHEIGHT - WINDOWHEIGHT;
//}
}
//This function does the real work of drawing a single tile from the
//source image onto the tile scroll buffer. Parameters provide much
//flexibility.
void DrawTile(LPDIRECT3DSURFACE9 source, // source surface image
int tilenum, // tile #
int width, // tile width
int height, // tile height
int columns, // columns of tiles
LPDIRECT3DSURFACE9 dest, // destination surface
int destx, // destination x
int desty) // destination y
{
//create a RECT to describe the source image
RECT r1;
r1.left = (tilenum % columns) * width;
r1.top = (tilenum / columns) * height;
r1.right = r1.left + width;
r1.bottom = r1.top + height;
//set destination rect
RECT r2 = {destx,desty,destx+width,desty+height};
//draw the tile
d3ddev->StretchRect(source, &r1, dest, &r2, D3DTEXF_NONE);
}
//This function fills the tilebuffer with tiles representing
//the current scroll display based on scrollx/scrolly.
void DrawTiles()
{
int tilex, tiley;
int columns, rows;
int x, y;
int tilenum;
//calculate starting tile position
tilex = ScrollX / TILEWIDTH;
tiley = ScrollY / TILEHEIGHT;
//calculate the number of columns and rows
columns = WINDOWWIDTH / TILEWIDTH;
rows = WINDOWHEIGHT / TILEHEIGHT;
//draw tiles onto the scroll buffer surface
for (y = 0; y <= rows; y++)
{
for (x=0; x<=columns; x++)
{
//retrieve the tile number from this position
tilenum = MAPDATA[((tiley + y) * MAPWIDTH + (tilex + x))];
//draw the tile onto the scroll buffer
DrawTile(tiles,tilenum,TILEWIDTH,TILEHEIGHT,16,scrollbuffer,
x*TILEWIDTH,y*TILEHEIGHT);
}
}
}
//This function draws the portion of the scroll buffer onto the back buffer
//according to the current "partial tile" scroll position.
void DrawScrollWindow()
{
//calculate the partial sub-tile lines to draw using modulus
int partialx = ScrollX % TILEWIDTH;
int partialy = ScrollY % TILEHEIGHT;
//set dimensions of the source image as a rectangle
RECT r1 = {partialx,partialy,partialx+WINDOWWIDTH,partialy+WINDOWHEIGHT};
//set the destination rectangle
//This line draws the virtual scroll buffer to the screen exactly as is,
//without scaling the image to fit the screen. If your screen does not
//divide evenly with the tiles, then you may want to scale the scroll
//buffer to fill the entire screen. It's better to use a resolution that
//divides evenly with your tile size.
//use this line for scaled display
//RECT r2 = {0, 0, WINDOWWIDTH-1, WINDOWHEIGHT-1};
//use this line for non-scaled display
RECT r2 = {0, 0, SCREEN_WIDTH-1, SCREEN_HEIGHT-1};
//draw the "partial tile" scroll window onto the back buffer
d3ddev->StretchRect(scrollbuffer, &r1, backbuffer, &r2, D3DTEXF_NONE);
}