• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Tile sheet spriting math ?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1MrPhoenix  Members

Posted 21 January 2013 - 04:46 PM

ok i got a tilesheet of 1600x1600 in size and each tile is 32px x 32px

i want to assign the tile like 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,etc

but im confused on the math on how to convert the tile number to the sprite_x_start and sprite_y_start

    void draw_tile(int tile_num, int x, int y, SDL_Surface *output_surface) {

draw_sprite(sprite_sheet, output_surface, draw_x_spot, draw_y_spot ,sprite_x_start, sprite_y_start ,32,32);

}

Edited by MrPhoenix, 21 January 2013 - 04:46 PM.

### #2Aercom  Members

Posted 21 January 2013 - 04:55 PM

I recommend starting tile_num at 0, not 1!

Would be similar to this

//50 x 50 tiles
sprite_x_start = 32 * (tile_num % 50);
sprite_y_start = 32 * (tile_num / 50);

assuming sprite_y_start is an integer.

### #3MrPhoenix  Members

Posted 21 January 2013 - 04:58 PM

Aercom i have to have the first tile set at 1 since that the way the map file is generated by tiled

### #4Brother Bob  Moderators

Posted 21 January 2013 - 05:11 PM

Subtract one from the values you read from the map and then work with zero-based indexing. The math turns out so much cleaner with zero-based indexing.

### #5Servant of the Lord  Members

Posted 21 January 2013 - 05:14 PM

Just add subtract one before calculating the index then.

I illustrated the math here.

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #6BornToCode  Members

Posted 21 January 2013 - 06:34 PM

struct Rect

{

int x;

int y;

};

All you need to do is divide the tile image width and height by whatever the tile width and tile height you want. Then from that point you can just create an nxm Rect then assign the x and y value for each one of the tile. Then when you reference an tile from Tiles you can just subtract 1 from it to reference the proper rect index and that should contains your x and y value of where in the image that tile begins at. You do not need the width and height since you already know it is 32x32

Edited by BornToCode, 21 January 2013 - 06:35 PM.

### #7MrPhoenix  Members

Posted 21 January 2013 - 08:17 PM

thanks Servant of the Lord i fixed my program it running great using your method

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.