Sign in to follow this  

Scrolling tiles problem directx

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

If you intended to correct an error in the post then please contact us.

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
void UpdateScrollPosition()
{
    //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)
	{
        ScrollY = 0;
        SpeedY = 0;
	}
	else if (ScrollY > 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->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<=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, SCREENWIDTH-1, SCREENHEIGHT-1};

    //draw the "partial tile" scroll window onto the back buffer
    
    //d3ddev->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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this