I'm stopping by for a quick question regarding how to calculate the sub-rectangle position for a tileset?
Say I have a tileset of 512x512 ( 256 worth of 32x32 tiles ) and that in my "map class" I have an array of integers that defines the array index of each tile in each cell.
Having a 1D Tileset is quite easy to work with, it's basically: tile_index * tile_width to get what the part of the whole tileset I should display. Once moving into a tileset of 2D I'm not sure how to go on about it.
I have found this snippet of code that works ( I have integrated it )
( Pseudo-Code )
int frame_col = tile_index % ( texture_width / tile_width ); int frame_row = tile_index / ( texture_width / tile_width ); subRect.setPos( frame_col * tile_width, frame_row * tile_height);
Could someone explain this to me a bit better, why does it work? and possible ( mostly because I'm very interested ) present another way to calculate it?