SFML community forums

Help => Graphics => Topic started by: DasOhmoff on August 20, 2015, 11:07:49 pm

Title: sf::Texture and sf::Sprite not working with std::tuple
Post by: DasOhmoff on August 20, 2015, 11:07:49 pm
Hi, I have a problem and I can not solve it...

Code: [Select]
#include <SFML/Graphics.hpp>

#include <iostream>
#include <vector>
#include <tuple>

enum class ID
{
Background,
Player
};

std::vector<std::tuple<ID, sf::Texture, sf::Sprite>> drawables;

void add(ID id, const std::string &path)
{
sf::Texture texture;
if(!texture.loadFromFile(path))
throw std::runtime_error("Could not load: " + path);

sf::Sprite sprite(texture);

drawables.push_back(std::make_tuple(id,texture,sprite));
}

int main()
{
add(ID::Background, "Data/Background.png");

sf::RenderWindow window(sf::VideoMode(500, 500), "Test");
window.setFramerateLimit(60);

while(window.isOpen())
{
window.clear();
window.draw(std::get<2>(drawables[0]));
window.display();
}

return 0;
}

I really don't get why this is not working, I think that the texture or sprite is somewhere copied and not used as a reference but i can not change the vector's tuple arguments to references or pointers because this makes errors :/
Title: Re: sf::Texture and sf::Sprite not working with std::tuple
Post by: Jesper Juhl on August 20, 2015, 11:09:57 pm
1) Vectors copy (or move; if noexcept) elements when they resize. Fact of life.
2) You can construct a vector that holds (smart) pointers to objects just fine.
3) You don't want to copy texture objects.