Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Slicing/extracting tiles from a grid of tiles  (Read 7003 times)

0 Members and 1 Guest are viewing this topic.

AndrewB

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Slicing/extracting tiles from a grid of tiles
« Reply #15 on: August 11, 2012, 06:01:16 pm »
You can have ONE and only ONE sf::Texture and assign it to each of 225 sf::Sprite s and then set each Sprite's rectangle.

By doing this:
  sf::Texture tiles;
  tiles.loadFromFile("Resources/tiles.tga");
  sf::Sprite spr(tiles);
  spr.setTextureRect(sf::IntRect(0,0,25,25));

Each sprite loaded in memory will be storing the entire tiles.tga image.
EDIT: Just to clarify, the spr sprite in the code above will still be storing the same amount of data on the GPU both BEFORE and AFTER setTextureRect is called.

Could @Laurent please clarify if setTextureRect() frees up the memory of the unused portions of the sprites.
« Last Edit: August 11, 2012, 06:03:16 pm by AndrewB »

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Slicing/extracting tiles from a grid of tiles
« Reply #16 on: August 11, 2012, 06:05:32 pm »
This was an example.
Obviously no one does that.
sf::Texture tex;
tex.loadFromFile("whatever.tga");
std::vector<sf::Sprite> sprites;
for (int it=0;i<666;++it)
{
sprites.push_back(sf::Sprite(tex));
sprites[i].setTextureRect(insert rectangle here);
}
 
Back to C++ gamedev with SFML in May 2023

AndrewB

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Slicing/extracting tiles from a grid of tiles
« Reply #17 on: August 11, 2012, 06:08:33 pm »
This was an example.
Obviously no one does that.
sf::Texture tex;
tex.loadFromFile("whatever.tga");
std::vector<sf::Sprite> sprites;
for (int it=0;i<666;++it)
{
sprites.push_back(sf::Sprite(tex));
sprites[i].setTextureRect(insert rectangle here);
}
 

This code would still store the entire texture file with every new sprite created (from what the description of setTextureRect says). See the function that I posted a few messages back, that's what I'm using. It's basically what you just wrote without using the setTextureRect.

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Slicing/extracting tiles from a grid of tiles
« Reply #18 on: August 11, 2012, 06:12:08 pm »
This code is storing texture once. Amount of sprites doesn't affect gpu memory. Sprites don't store textures, your code does. Sprites hold reference to texture and if texture gets out of scope they won't display properly anymore.
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Slicing/extracting tiles from a grid of tiles
« Reply #19 on: August 11, 2012, 08:21:27 pm »
If every sprite had to make a copy of its texture, it wouldn't make sense to have this sprite/texture separation. It allows to share a single texture among many sprites, which saves video memory and improves performances.

I think the sprite tutorial is pretty clear about that, isn't it?
Laurent Gomila - SFML developer

AndrewB

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Slicing/extracting tiles from a grid of tiles
« Reply #20 on: August 12, 2012, 06:20:25 am »
Yes it is haha that's my bad, I apologize.

For some reason it was stuck in my head that sprites and textures were the same thing even though I was arguing differently.