# XNA C# Texture Atlases

Fairly new to XNA and not an expert on C# either. So I was following a tutorial, specifically this one http://rbwhitaker.wikidot.com/texture-atlases-1, and there is a bit of code I am unfamiliar with.

public void Draw(SpriteBatch spriteBatch, Vector2 location)
{
int width = Texture.Width / Columns;
int height = Texture.Height / Rows;
int row = (int)((float)currentFrame / (float)Columns);
int column = currentFrame % Columns;


Mostly all of this I am familiar with. However, the line that says

int row = (int)((float) currentFrame / (float) Columns);


This line is not familiar to me and I would appreciate if someone could explain what this does. Clearly it is designed to find out what row the program is on in the texture atlas but what how does it do that with that code? What is it calculating?

As the variable name suggests, its finding the row where the image for "currentFrame".

int row = (int)((float)currentFrame / (float)Columns);


If you have 4x4 images, so 4 in row and 4 in columns. Then you are animating your image and going through frames. Well, frame 1 is easy to find, since it's the first, so row = 0 and column = 0. But if you are in frame 11, it gets harder. If you need to support different sizes, ie. some animations have 6 frames and some have 9 frames and you are using structure like in the tutorial, then you have to calculate the row. You could of course place images side by side to overcome this.

It's casted to int, because the actual row is pure integer, not floating point ( so not row 1,2424342, but 1), right now i don't understand why there is float casting going on.

Essentially that line is just dividing the current frame with columns and doing some casting at the same time.

Example:

int row = 11 / 4; // row = 2
int column = 11 % 4 // column = 3


So, now we have the position of the image from the atlas. All we have to do now, is to multiply row and column with tile w & h to find the pixel perfect location of the current frame from the atlas.