Sign in to follow this  

Simple tile selection

This topic is 3043 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

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

This topic is 3043 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