# Scrolling tiles problem directx

This topic is 3900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I changed the scrolling tile code from jharbour's Game Programming 2nd edition to use textures:
//This function updates the scrolling position and speed
{
//update horizontal scrolling position and speed
ScrollX += SpeedX;

if (ScrollX &lt; 0)
{
ScrollX = 0;
SpeedX = 0;
}
else if (ScrollX &gt; GAMEWORLDWIDTH - WINDOWWIDTH)
{
ScrollX = GAMEWORLDWIDTH - WINDOWWIDTH;
SpeedX = 0;
}

//update vertical scrolling position and speed
ScrollY += SpeedY;
if (ScrollY &lt; 0)
{
ScrollY = 0;
SpeedY = 0;
}
else if (ScrollY &gt; GAMEWORLDHEIGHT - WINDOWHEIGHT)
{
ScrollY = GAMEWORLDHEIGHT - WINDOWHEIGHT;
SpeedY = 0;
}
}

//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(LPDIRECT3DTEXTURE9 source,	// source surface image//LPDIRECT3DSURFACE9
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};

D3DXVECTOR3 pos(0,0,0);
pos.x = r2.left;
pos.y = r2.top;
//draw the tile
sprite_handler-&gt;Draw(source,&r1,NULL,&pos,D3DCOLOR_XRGB(255,255,255));

}

//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&lt;=rows; y++)
{
for (x=0; x&lt;=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, SCREENWIDTH-1, SCREENHEIGHT-1};

//draw the "partial tile" scroll window onto the back buffer

//d3ddev-&gt;StretchRect(scrollbuffer, &r1,backbuffer , &r2, D3DTEXF_NONE);
}


I was wondering what the DrawScrollWindow function did, because I know longer really use it as you can see. I suspect it might be related to two of my problems. THe first is that when the character approaches the edge of the screen it scrolls the tiles. Now, with this new tile system using texture the scrolling only scrolls through a whole tile at a time. This is way to much and creates unrealistic motion because the character cannot move 64 pixels (this is the width of one of the tiles) at a time. The second, more important problem is that the character hovers above the ground that it is supposed to be colliding with. Maybe something to do with the partial tiles is causing it to appear as though the character floats above the tiles that it collides with. Any ideas are appreciated.

##### Share on other sites
From what I read there, it looks like DrawTiles will draw all needed tiles onto a "Tile Buffer" and the DrawScrollWindow should draw just the right part of that Tile Buffer onto the actual screen back buffer (you've commented-out the drawing line at the end of the method).

DrawScollWindow is the part that takes the partial tile scroll movement into effect and makes things scroll smoothly instead of a full tile at a time.

So, I would suggest returning to using that DrawScrollWindow method.

-Matt

##### Share on other sites
Alright, so instead of using the sprite_handler and drawing to the screen I should draw to a texture and then use the drawscrollwindow to grab the correct part of the texture to put on the screen?

How would I draw a texture to another texture?

Thanks, for the quick response.

##### Share on other sites
The way I solved my problem is instead of drawing all the tiles onto a huge surface, I drew them directly onto the backbuffer. Just make sure to clip all the textures that are offscreen. I could send you the source code if you want.

##### Share on other sites
Yeah, please do send me the source when you have a chance. Or if someone could just tell me how to copy a texture to a larger texture that would be great, too.

Thanks for all of the help.

1. 1
Rutin
45
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632998
• Total Posts
3009811
• ### Who's Online (See full list)

There are no registered users currently online

×