Sign in to follow this  
kratz

Simple tile selection

Recommended Posts

Could some kind soul point me in the right direction for a simple 2D non-isometric tile selection algorithm. I want to be able to select a rectangular area of tiles of arbitrary size from my tile map. I have a function to get a tile index from my 1d tile array based on screen coords. I've come up with this but it only works when you start dragging from up/left and go down/right.
void multiSelect(int r1, int c1, int r2, int c2)
{
//r1 c1 is beginning row/column
//r2 c2 is end row/column
//getIndex() gets an index from a row/column

	for(int i = getIndex(r1, c1); i <= getIndex(r2, c1); i+=MAP_W)
	{
		for(int j = 0; j <= c2-c1; j++)
		{
			addTileToSelection(i+j);
		}
	}
}

Share this post


Link to post
Share on other sites
It only works from top-left to bottom-right because 5 - 2 == 3, and 2 - 5 == -3.


#include <algorithm>
#include <vector>

std::vector<Tile> selection;
unsigned selectionWidth = 0, selectionHeight = 0, selectionArea = 0;

void multiSelect( int r1, int c1, int r2, int c2 )
{
if ( r2 < r1 )
std::swap( r1, r2 );
if ( c2 < c1 )
std::swap( c2, c1 );

selectionWidth = c2 - c1;
selectionHeight = r2 - r1;

selectionArea = selectionWidth * selectionHeight;
selection.resize( selectionArea );

for ( int cy = 0, row = r1; row < r2; ++row, ++cy )
for ( int cx = 0, col = c1; col < c2; ++col, ++cx )
selection[ selectionWidth * cy + cx ] = getTileAtLocation( row, col );
}









Share this post


Link to post
Share on other sites
Well DUH! Man why didn't I think of that. Once again _fastcall you are DA MAN.
Thanks for the direction.
rating++;

Share this post


Link to post
Share on other sites

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