Haven't used sfml in a while, came back to start a new project and am trying to figure stuff out. I used to load something in a sf::Image, and then assign it to a sf::Sprite. Now it seems that sf::Texture has taken its place? Just making sure they're the same thing cause I'm having a problem with getting stuff to draw properly. Looks like corrupted memory somewhere when I render.
And am I doing this right? Loading tiles from a tileset (larger image with all my tiles). I want to make sure the sprite only has that subrect of the image stored in it, or a lot of memory would be taken up.
sprite->SetTexture(*texture);
sprite->SetTextureRect(sf::IntRect(sourceX, sourceY, tileset->GetTileWidth(), tileset->GetTileHeight()));
Might anyone know why this doesn't render properly? If I take out the sprite->SetTextureRect line, the whole tileset renders properly just fine.
bool Map::LoadMap(std::string filename)
{
Tmx::Map *map = new Tmx::Map();
map->ParseFile(filename);
if (map->HasError()) {
printf("error code: %d\n", map->GetErrorCode());
printf("error text: %s\n", map->GetErrorText().c_str());
return map->GetErrorCode();
}
for(int i = 0; i < map->GetNumTilesets(); i++)
{
const Tmx::Tileset* tileset = map->GetTileset(i);
sf::Texture* texture = new sf::Texture();
if(!texture->LoadFromFile(tileset->GetImage()->GetSource().c_str()))
{
return false;
}
for(int y = 0; y < (tileset->GetImage()->GetWidth() / tileset->GetTileWidth()); y++)
{
for(int x = 0; x < (tileset->GetImage()->GetHeight() / tileset->GetTileHeight()); x++)
{
int margin = tileset->GetMargin();
int spacing = tileset->GetSpacing();
int sourceX = (margin + (x * tileset->GetTileWidth()) + (inRange(x, 1, (tileset->GetTileWidth() * (tileset->GetImage()->GetWidth() - tileset->GetTileWidth()))) ? (x * spacing) : 0));
int sourceY = (margin + (y * tileset->GetTileHeight()) + (inRange(y, 1, (tileset->GetTileHeight() * (tileset->GetImage()->GetHeight() - tileset->GetTileHeight()))) ? (y * spacing) : 0));
int sourceWidth = sourceX + tileset->GetTileWidth();
int sourceHeight = sourceY + tileset->GetTileHeight();
sf::Sprite* sprite = new sf::Sprite();
sprite->SetTexture(*texture);
sprite->SetTextureRect(sf::IntRect(sourceX, sourceY, tileset->GetTileWidth(), tileset->GetTileHeight()));
sf::Texture t;
unsigned int gid = tileset->GetFirstGid() + x * y;
_tileSprites[gid] = sprite;
std::cout << gid << " ";
}
}
}
for (int i = 0; i < map->GetNumLayers(); ++i)
{
const Tmx::Layer* layer = map->GetLayer(i);
for (int y = 0; y < layer->GetHeight(); y++)
{
for (int x = 0; x < layer->GetWidth(); x++)
{
// Find a tileset for that id.
unsigned int tileGID = layer->GetTileGid(y,x);
if(tileGID != 0)
{
_tiles.push_back(new Tile(tileGID, sf::Vector2f(x * 32, y * 32)));
//printf("Tile position on layer: x=%d, y=%d\n", x * map->GetTileWidth(), y * map->GetTileHeight());
//printf("Tile rect on tileset: x=%d, y=%d, w=%d, h=%d\n", sourceX, sourceY, sourceWidth, sourceHeight);
//printf("Tile Size: w=%d, h=%d\n", tileset->GetTileWidth(), tileset->GetTileHeight());
//printf("Tile Source: %s\n", tileset->GetImage()->GetSource().c_str());
//printf("\n");
}
else
{
// no tileset found. blank tile.
}
}
}
}
delete map;
}
while(_renderer->GetRenderWindow()->IsOpened())
{
sf::Event Event;
while (_renderer->GetRenderWindow()->PollEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
_renderer->GetRenderWindow()->Close();
}
// Clear screen
_renderer->GetRenderWindow()->Clear();
for(int i = 0; i < m->_tiles.size(); i++)
{
Tile* t = m->_tiles.at(i);
sf::Sprite* s = m->_tileSprites[t->GetGID()];
if(s)
{
std::cout << s << ": " << t->GetPosition().x << "," << t->GetPosition().y << std::endl;
s->SetPosition(t->GetPosition());
_renderer->GetRenderWindow()->Draw(*s);
}
}
// Finally, display the rendered frame on screen
_renderer->GetRenderWindow()->Display();
}
Here's without the subrect line:
http://imgbin.org/index.php?page=image&id=6140Here's with the subrect line:
http://imgbin.org/index.php?page=image&id=6141Thanks.