• Create Account

### #ActualServant of the Lord

Posted 10 July 2012 - 10:05 PM

The code is trying to do two things at once, and instead of doing them in order, are mixing the two together in what looks like a poor programmer's attempt at "optimizing code" (and are instead obfuscating code, making it harder to maintain).

1) They are converting from a tile index, to the tile location in tiles.
2) They are converting from the tile location in tiles, to the tile location in pixels.
...But they are intermingling the two at one time, which is what is causes the confusion.

If you do things neatly, it might look like this:
//Step one: Go from index to tile location in tiles.
int tileX = (tileIndex % TilesetWidthInTiles);
int tileY = (tileIndex / TilesetWidthInTiles);

//Step two: Go from location in tiles, to location in pixels.
int tilePixelX = (tileX * TileWidth);
int tilePixelY = (tileY * TileHeight);

If you want to mix them together in one line that's also fine:
int tilePixelX = (tileIndex % TilesetWidthInTiles) * TileWidth;
int tilePixelY = (tileIndex / TilesetWidthInTiles) * TileHeight;

It's still broken into two logical steps (step 1: within the parenthesis, step 2: the multiplication), even if you have it on one line, and is easier to understand.

The other one intermingles the two steps, I'm not sure why, probably to save the cost of using the modulus operator or something.
But (since it's just a tiny snippet of code) it's really not as bad as I'm making it sound - I'm just an idealist when it comes to clean code.
Though even in my own projects, clean code isn't always possible, it's still one of the top goals to shoot for (Much more important than running fast, but I digress).

Here's how it works:
We'll start by assigning some test values, to help us walk through it. It'll work for any sane values you choose, but for figuring out what a piece of code does, it's always good to pick some test values and walk through the code mentally in the same way the computer will walk through it.
int tile_index = 12;
int tileset_width = 5; //The tileset is 5 _tiles_ wide.
int tile_width = 32; //Our tiles are 32 _pixels_ wide and high.

Line one:
int sy = ((tile_index - 1) / tileset_width);
int sy = ((12 - 1) / 5);
int sy = ((11) / 5);
int sy = (2);

Line two:
int sx = ((tile_index - 1) - (sy * tileset_width)) * tile_width;
int sx = ((12 - 1) - (2 * 5)) * 32;
int sx = ((11) - (10)) * 32;
int sx = (1) * 32;
int sx = 32;

Line three:
sy = sy * tile_height;
sy = 2 * 32;
sy = 64;

Result:
sx = 32 pixels
sy = 64 pixels

### #1Servant of the Lord

Posted 10 July 2012 - 10:05 PM

The code is trying to do two things at once, and instead of doing them in order, are mixing the two together in what looks like a poor programmer's attempt at "optimizing code" (and are instead obfuscating code, making it harder to maintain).

1) They are converting from a tile index, to the tile location in tiles.
2) They are converting from the tile location in tiles, to the tile location in pixels.
...But they are intermingling the two at one time, which is what is causes the confusion.

If you do things neatly, it might look like this:
//Step one: Go from index to tile location in tiles.
int tileX = (tileIndex % TilesetWidthInTiles);
int tileY = (tileIndex / TilesetWidthInTiles);

//Step two: Go from location in tiles, to location in pixels.
int tilePixelX = (tileX * TileWidth);
int tilePixelY = (tileY * TileHeight);

If you want to mix them together in one line that's also fine:
int tilePixelX = (tileIndex % TilesetWidthInTiles) * TileWidth;
int tilePixelY = (tileIndex / TilesetWidthInTiles) * TileHeight;

It's still broken into two logical steps (step 1: within the parenthesis, step 2: the multiplication), even if you have it on one line, and is easier to understand.

The other one intermingles the two steps, I'm not sure why, probably to save the cost of using the modulus operator or something.
But (since it's just a tiny snippet of code) it's really not as bad as I'm making it sound - I'm just an idealist when it comes to clean code.
Though even in my own projects, clean code isn't always possible, it's still one of the top goals to shoot for (Much more important than running fast, but I digress).

Here's how it works:

We'll start by assigning some test values, to help us walk through it. It'll work for any sane values you choose, but for figuring out what a piece of code does, it's always good to pick some test values and walk through the code mentally in the same way the computer will walk through it.
int tile_index = 12;
int tileset_width = 5; //The tileset is 5 _tiles_ wide.
int tile_width = 32; //Our tiles are 32 _pixels_ wide and high.

Line one:
int sy = ((tile_index - 1) / tileset_width);
int sy = ((12 - 1) / 5);
int sy = ((11) / 5);
int sy = (2);

Line two:
int sx = ((tile_index - 1) - (sy * tileset_width)) * tile_width;
int sx = ((12 - 1) - (2 * 5)) * 32;
int sx = ((11) - (10)) * 32;
int sx = (1) * 32;
int sx = 32;

Line three:
sy = sy * tile_height;
sy = 2 * 32;
sy = 64;

Result:
sx = 32 pixels
sy = 64 pixels

PARTNERS