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

Author Topic: C++: Drawing through iteration of objects in vector of objects  (Read 4506 times)

0 Members and 1 Guest are viewing this topic.

juiceeYay

  • Newbie
  • *
  • Posts: 16
    • View Profile
C++: Drawing through iteration of objects in vector of objects
« on: December 01, 2014, 10:30:14 pm »
So this is a function I call in my main function in my 'main.cpp' which returns a vector of object of the 'Star' class:

std::vector<Star> generateStars(int numOfStars,int thresh)
{
    std::vector<Star> stars;
    //does some stuff with the vector 'stars'

    return stars;
};

My 'Star' class inherits from 'sf::Sprite' as such:

class Star : public sf::Sprite
{
private:
    void Init(int xCoord,int yCoord)
    {
        setPosition(xCoord,yCoord);
    }
public:
    int xPos;
    int yPos;
   
    Star(int inX, int inY)
    {
        xPos = inX;
        yPos = inY;
        Init(xPos,yPos);
    }
};

I obviously then want to assign a texture to each 'Star' object in my vector of 'Star' objects and then draw each, so I thought I would implement it as such:

int main(int, char const**)
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

    // Set the Icon
    sf::Image icon;
    if (!icon.loadFromFile(resourcePath() + "icon.png")) {
        return EXIT_FAILURE;
    }
    window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());

    // Load a sprite to display
    sf::Texture texture;
    if (!texture.loadFromFile(resourcePath() + "Untitled-1.png")) {
        return EXIT_FAILURE;    //was "cute_image.jpg"
    }

    std::vector<Star> starsVec = generateStars(20,20);
   
    for(Star iter : starsVec)
    {
        iter.setTexture(texture);
    }

    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed) {
                window.close();
            }

            // Espace pressed : exit
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
                window.close();
            }
        }

        // Clear screen
        window.clear();

        // Draw the sprite
        for(Star it : starsVec)
        {
            window.draw(it);
        }

        // Update the window
        window.display();
    }
   
    return EXIT_SUCCESS;
}

This returns a blank window, so I'm assuming that my calls to 'setTexture' and 'draw' are being destroyed after the loops which iterate through my vector ends. If this is a correct assumption, how would I rewrite my code so that this does not happen?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: C++: Drawing through iteration of objects in vector of objects
« Reply #1 on: December 01, 2014, 11:10:05 pm »
Quote
This returns a blank window, so I'm assuming that my calls to 'setTexture' and 'draw' are being destroyed after the loops which iterate through my vector ends. If this is a correct assumption, how would I rewrite my code so that this does not happen?

Technically, the question doesn't make sense because a call to a function is not an object that can get created or destroyed. What I assume you're trying to ask is if each object you're calling those functions on is getting destroyed at the end of its loop iteration, which is in fact correct.

The for-each loops you've written are making copies of the Stars.  You are calling setTexture and draw on copies of the Stars in your stars vector, not on the actual Stars in that vector.  And these copies are temporary objects that die at the end of their respective loop iterations. To "iterate by reference" instead, so that you assign to the "correct" Stars instead of their copies, simply add a & in front of it/iter.

https://stackoverflow.com/questions/15176104/c11-range-based-loop-get-item-by-value-or-reference-to-const might help a bit.

juiceeYay

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: C++: Drawing through iteration of objects in vector of objects
« Reply #2 on: December 01, 2014, 11:16:57 pm »
Yes what you said was what I meant. Thank you for the clarification, I figured it involved those reference operators, but I have been having trouble understanding there exact meaning. The link you provided had some great answers which clarified greatly, so thank you for that and apologies for asking a question that I realize now might've been more appropriate in a general C++ forum.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: C++: Drawing through iteration of objects in vector of objects
« Reply #3 on: December 01, 2014, 11:29:20 pm »
Yeah, you should probably spend some time reading a quality C++ book until you fully understand what this stuff means, since it's pretty fundamental to doing C++ properly, but...

The main reason the "&" is tricky to understand is that it has two completely different meanings.  Here, the & is not an operator, but a part of a variable declaration that indicates the declared variable is a reference to some other variable.  In an expression, the & is an operator that takes the address of its operand (though now we have std::addressof to help fight this ambiguity).

 

anything