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

Author Topic: sf::Texture and sf::Sprite not working with std::tuple  (Read 1081 times)

0 Members and 1 Guest are viewing this topic.

DasOhmoff

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
sf::Texture and sf::Sprite not working with std::tuple
« 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 :/

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::Texture and sf::Sprite not working with std::tuple
« Reply #1 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.
« Last Edit: August 20, 2015, 11:14:52 pm by Jesper Juhl »