Jump to content

  • Log In with Google      Sign In   
  • Create Account


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.

  • You cannot reply to this topic
6 replies to this topic

#1 MrPhoenix   Members   -  Reputation: 134

Like
0Likes
Like

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.


Sponsor:

#2 Aercom   Members   -  Reputation: 138

Like
2Likes
Like

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.
 



#3 MrPhoenix   Members   -  Reputation: 134

Like
0Likes
Like

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



#4 Brother Bob   Moderators   -  Reputation: 7779

Like
1Likes
Like

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.



#5 Servant of the Lord   Crossbones+   -  Reputation: 17092

Like
0Likes
Like

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' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

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.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#6 BornToCode   Members   -  Reputation: 901

Like
0Likes
Like

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.


#7 MrPhoenix   Members   -  Reputation: 134

Like
0Likes
Like

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.



PARTNERS