Skip to main content
GameDev.net gamedev.net
🔒 Locked

Need help with smooth scrolling tiles in direct3d

Started by abcdefg Apr 20, 2005 at 8:55 PM 4 replies 1.1k views
Original Post
abcdefg
abcdefg
Okay this is a simple work in progress. The progress has come to a halt because I cant figure out how to get the tilemap to scroll smoothly. Here is the relevant code. With the scroll plus the x and y its no longer smooth scrolling and when I use just the scroll it works but it no longer looks up my tiles. im not sure whats wrong exactly but help would be good. Thanks for looking.

void DrawTileMap(ID3DXSprite &mapsprite,int tiledimx,int tiledimy,RECT ▭,CGRAPHICS *anobject)
{
	int numtilesx=20,numtilesy=15;


	//are tiles power of 2
	if(tiledimx/2!=0&&tiledimy/2!=0)
	{
		//Print tiles must be power of 2
	}

	//Load Level file using FILE I/O store in structure

	//Draw Screen
	for(int y=0;y<numtilesy+1;y++)
	{
		for(int x=0;x<numtilesx+1;x++)
		{
			//mapspritedraw();

			int scrollx=worldx/tiledimx;
			int scrolly=worldy/tiledimy;

			offsety=scrolly & (tiledimy-1);
			offsetx=scrollx & (tiledimx-1);

			tilecoord.x=(x*tiledimx)-offsetx;
			tilecoord.y=(y*tiledimy)-offsety;

                 //Problem maybe at this line????
			TileLookUp(97,32,32,map[scrollx+x][scrolly+y],rect);

			mapsprite.Draw(anobject->pTexture,▭,NULL, NULL, 0.0f,&tilecoord,0xffffffff);
		}
	}
}
[Edited by - abcdefg on April 20, 2005 10:43:16 PM]
Raduprv
Raduprv
Sorry for being slightly offtopic, but you can replace
if(tiledimx/2!=0&&tiledimy/2!=0)

with
if(tiledimx/2 && tiledimy/2)

Much cleaner.

As for your problem, try to debug your application, and see which part/function doesn't return the desired values (and why).

[edit]
if(tiledimx/2!=0&&tiledimy/2!=0)
will NOT check if the tiles are a power of 2, it will check if they are bigger than 1 :)
dominicoder
dominicoder
Try changing:

offsety=scrolly & (tiledimy-1);
offsetx=scrollx & (tiledimx-1);

To:

offsety=scrolly % tiledimy;
offsetx=scrollx % tiledimx;

In my similar code, that's basically what I did to get how many pixels to shift the tiles by to get smooth scrolling.

BTW, what was the point of using the '&' ?

Hope that helps, good luck.

[Edited wording to clear things up.]
abcdefg
abcdefg
I changed the & to a % and it still works like it did before. Right now the tiles scroll 1 at a time and not smoothly. Not sure what exactly is causing this.
jeroenb
jeroenb
A suggestion would be to pre-calculate the offset of the tiles before the loop. That saves considerable math time and framerate will increase. When you have the offset you can calculate the new tile and texture coordinates.
Crafter 2D: the open source 2D game framework ?Github: https://github.com/crafter2d/crafter2d
Twitter: [twitter]crafter_2d[/twitter]
abcdefg
abcdefg
Im not really thinking speed right now Im just trying to make it work, then I can break it trying for speed. I have no clue why its not smooth scrolling.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.