i know that Jim Adams has an article about this on the light befallen site.
www.lightbefallen.com
fortunately, and unfortunately at the same time, i'm currently working on a program that demonstrates EXACTLY what you are asking about. (the unfortunate part is that i wont be done with it for a few days, or i could show you right now)
however, i will give a quick answer as to how you can solve your problem.
the issue here is in translation of screen coordinates(ScreenSpace) to world coordinates(TileSpace), and vice versa.
ScreenSpace is the viewable area of the screen. it is usually a rectangle.
so, for example, lets say you are doing a fullscreen application that runs in 640x480, but at the left hand side of the screen, you have a 64 pixel wide toolbar, at the top of the screen you have a 16 pixels menu bar, thus leaving you with the following rectangle:
code:
const int TOOLBARWIDTH=64;const int MENUHEIGHT=8;const int SCREENWIDTH=640;const int SCREENHEIGHT=480;RECT rcScreenSpace;SetRect(&rcScreenSpace,TOOLBARWIDTH,MENUHEIGHT,SCREENWIDTH,SCREENHEIGHT);
and now lets say you want to also put an 8x8 pixel border inside this area.
code:
const int BORDERWIDTH=8;cosnt int BORDERHEIGHT=8;rcScreenSpace.left+=BORDERWIDTH;rcScreenSpace.right-=BORDERWIDTH;rcScreenSpace.top+=BORDERHEIGHT;rcScreenSpace.bottom-=BORDERHEIGHT;
now i've got the bounds of my screenspace (please note that since i've used constants,i have no idea what the actual coordinates of the screenspace are, and nor do i care, and nor SHOULD i care).
next, i'm going to determine by PlotSpace.
PlotSpace is exactly the same size as ScreenSpace, but with left and top at (0,0)
code:
RECT rcPlotSpace;CopyRect(&rcPlotSpace,&rcScreenSpace);OffsetRect(&rcPlotSpace,-rcScreenSpace.left,-rcScreenSpace.top);
converting between them is a simple matter
code:
//screenspace->plotspaceptPlot.x=ptScreen.x-rcScreenSpace.left;ptPlot.y=ptScreen.y-rcScreenSpace.top;//plotspace->screenspaceptScreen.x=ptPlot.x+rcScreenSpace.left;ptScreen.y=ptPlot.y+rcScreenSpace.top;
now, we are going to make plotspace talk to tilespace, through a tilespacewindow
to do this, we merely need to keep track of a single point, that is the corresponding point in tilespace to (0,0) in plotspace. this is an arbitrary point, and it is the key to scrolling your map. i.e. this is the value you change by whatever factor you like to scroll your map in any direction.
so, let us call this point ptTileSpaceWindow.
now, to calculate the position of the tilespace window:
code:
RECT rcTileSpaceWindow;CopyRect(&rcTileSpaceWindow,&rcPlotSpace);OffsetRect(&rcTileSpaceWindow,ptTileSpaceWindow.x,ptTileSpaceWindow.y);
now we have a method by which we can translate tilespace coordinates into plotspace coordinates
code:
//plotspace->tilespaceptWorld.x=ptPlot.x+ptTileSpaceWindow.x;ptWorld.y=ptPlot.y+ptTileSpaceWindow.y;//tilespace->plotspaceptPlot.x=ptWorld.x-ptTileSpaceWindow.x;ptPlot.y=ptWorld.y-ptTileSpaceWindow.y;
its important to note, at this point, that i'm breaking this down into more steps than it needs to be. you can translate from screen space to tilespace without utilizing plotspace, which is only included here for illustration purposed.
now, we want to determine if a given tile is within the viewable area. in order to do this, we need to know the world coordinates of the rectangle that will contain an individual tile.
the following code provides the equations for plotting staggered (offset) tilemaps as well as diagonal (diamond) tilemaps. most likely, your own equations will be similar to one of these.
code:
//staggered mapXPos=TileX*TILEWIDTH+(TileY&1)*(TILEWIDTH>>1);YPos=TileY*(TILEHEIGHT>>1);//diagonal mapXPos=(TileX-TileY)*(TILEWIDTH>>1);YPos=(TileX+TileY)*(TILEHEIGHT>>1);
next we find the bounding rectangle of the tile.
LeftPad, etc are padding values, and may well be 0 depending on whether or not images stick out of the tile.
code:
RECT rcTile;SetRect(&rcTile,TileX-LeftPad,TileY-TopPad,TileX+TILEWIDTH+RightPad,TileY+TILEHEIGHT+BottomPad);
now, we check to see if any of the tile bounding rect is within the tilespace window:
code:
RECT rcIntersect;BOOL bIntersects;bIntersects=IntersectRect(&rcIntersect,&rcTile,&rcTileSpaceWindow);if(bIntersects){//the tile is viewable}
if it is viewable, you can easily translate rcTile from tilespace to screenspace coordinates, also, you can use rcIntersect to clip the tile without too much trouble.
did i say this post would be short? i lied.
[This message has been edited by TANSTAAFL (edited August 30, 1999).]