Jump to content
  • Advertisement
Sign in to follow this  
kratz

Simple tile selection

This topic is 3236 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!