Search code examples
c++c++17sfml

Using vectors of non-copyable objects


I am trying to use sfml to build a app that is functionally running multiple windows at once. In order to do this I am trying to do what would usually be done from the while running loop inside of a function that has a for loop that goes through a vector of RenderWindows. There is another function that adds to that vector. The issue is that RenderWindows are non-copyable. Any help is appreciated.

My Vectors

    vector <MakeKey::NewKey> KeyArray;
    vector <sf::RenderWindow> WindowArray;
    int VectorSize;

May Main

int main()
{
    sf::RenderWindow window(sf::VideoMode(100, 100, 32), "Main Window", sf::Style::Default);
    MakeKey MakeKey;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            //Key Presses
            if (event.type == sf::Event::KeyPressed) {
                if (event.key.code == sf::Keyboard::A)
                    MakeKey.DrawKey("A");
                else if (event.key.code == sf::Keyboard::D)
                    MakeKey.DrawKey("D");
                //Ect
            }
            if (event.type == sf::Event::Closed)
                window.close();
        }
        MakeKey.StepWindows();
    }
    return EXIT_SUCCESS;
}

My StepWindows Function

void MakeKey::StepWindows()
{
    for (int i{ 0 }; i > VectorSize; i++)
    {
        cout << "Inside Step Windows For Loop" << endl;
        WindowArray[i].setActive(true);
        WindowArray[i].clear(sf::Color::Transparent);
        WindowArray[i].draw(KeyArray[i].Sprite);
        WindowArray[i].display();
    }
}

My DrawKey Function

void MakeKey::DrawKey(string input)
{
    MakeKey::NewKey Key;
    if (input == "A")
        Key.Img.loadFromFile("Assets/Images/A.png");
    else if (input == "D")
        Key.Img.loadFromFile("Assets/Images/D.png");
    VectorSize++;
    //WindowArray.reserve(VectorSize);
//attempting to reference deleted function error
    //WindowArray.emplace_back(); 
//same error
    WindowArray[VectorSize].create(sf::VideoMode(Key.Img.getSize().x, Key.Img.getSize().y, 32), "Key", sf::Style::None);
    Key.Tex.loadFromImage(Key.Img);
    Key.Sprite.setTexture(Key.Tex);
    KeyArray.emplace_back(move(Key));
    cout << "KeyArray Has " << KeyArray.size() << " Elements\n" << "WindowArray Has " << WindowArray.size() << " Elements" << endl;
}

And My Key struct

    typedef struct KeyStruct {
        sf::Image Img;
        sf::Texture Tex;
        sf::Sprite Sprite;
    }NewKey;

Solution

  • Store a pointer instead. std::unique_ptr<sf::RenderWindow> should be suitable. Initialize it with new or std::make_unique<sf::RenderWindow> (C++14).

    Note that your main loop will need to be radically different as you would need to poll several windows. pollEvent is unsuitable as it blocks: if you call it on wnd1, you won’t be able to process events arriving to other windows until some event arrives to wnd1 as well.