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

Author Topic: C++/SFML: display several shapes simultaneously on screen  (Read 377 times)

0 Members and 1 Guest are viewing this topic.

JLesage

  • Newbie
  • *
  • Posts: 7
    • View Profile
C++/SFML: display several shapes simultaneously on screen
« on: February 22, 2024, 11:05:51 am »
Hello. I'm a beginner in c++ and I'm learning SFML. I have coded a small program to simultaneously display several shapes of different size, color and position on the screen, but I don't understand why there is just one shape that is displayed when the code is executed. Please help me.

#include <SFML/Graphics.hpp>

#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

#include <iostream>

using std::vector;

using std::cout;
using std::cin;
using std::endl;



int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");

    vector<sf::RectangleShape> vecRec(25);

    srand(time(NULL));

    int r, g, b = rand()%255+1;

    sf::Vector2f recSize(rand()%40+20, rand()%40+20);
    sf::Vector2f recPos(rand()%760+1, rand()%560+1);

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();


        sf::RectangleShape rec;
        rec.setFillColor(sf::Color(r,g,b));
        rec.setSize(recSize);
        rec.setPosition(recPos);
        vecRec.push_back(rec);

        /*for(auto& r: vecRec){
            window.draw(r);
        }*/


        for(int i=0; i<vecRec.size();i++){
            window.draw(vecRec[i]);
        }


        window.display();
    }

    return 0;
}
 
« Last Edit: February 29, 2024, 10:15:26 pm by eXpl0it3r »

kojack

  • Sr. Member
  • ****
  • Posts: 327
  • C++/C# game dev teacher.
    • View Profile
Re: C++/SFML: display several shapes simultaneously on screen
« Reply #1 on: February 22, 2024, 08:14:03 pm »
window.draw(vecRec); should be window.draw(vecRec);

But the main issue here is you are making a new rectangle every frame and pushing them into the vector, but every one of them has the same position, size and colour so you will only ever see what appears to be one. The random position, size and colour is chosen once outside of the loop and reused. You'll need to move all of the randoms into the loop so they are recreated for every new rectangle.

JLesage

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: C++/SFML: display several shapes simultaneously on screen
« Reply #2 on: February 22, 2024, 08:47:08 pm »
Thank you for the reply @kojack. Can you give an example with code, please?

JLesage

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: C++/SFML: display several shapes simultaneously on screen
« Reply #3 on: February 22, 2024, 09:06:49 pm »
I made some change in the code, but the shapes are moving...

#include <SFML/Graphics.hpp>

#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

#include <iostream>

using std::vector;

using std::cout;
using std::cin;
using std::endl;



int main()
{



    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");

    vector<sf::RectangleShape> vecRec(25);

    srand(time(NULL));




    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }


        window.clear();

        for(int i=0; i<vecRec.size();i++){

            int r= rand()%255+1; int g= rand()%255+1; int b= rand()%255+1;
            sf::Vector2f recSize(rand()%40+20, rand()%40+20);
            sf::Vector2f recPos(rand()%760+1, rand()%560+1);

            vecRec[i].setSize(recSize);
            vecRec[i].setPosition(recPos);
            vecRec[i].setFillColor(sf::Color(r,g,b));
            window.draw(vecRec[i]);
        }

        window.display();
    }

    return 0;
}


 

« Last Edit: February 29, 2024, 10:14:45 pm by eXpl0it3r »

JLesage

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: C++/SFML: display several shapes simultaneously on screen
« Reply #4 on: February 22, 2024, 09:14:16 pm »
I also don't know why certain expressions like vecRec aren't displayed in the code on the forum.


« Last Edit: February 29, 2024, 10:14:59 pm by eXpl0it3r »

Hapax

  • Hero Member
  • *****
  • Posts: 3360
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: C++/SFML: display several shapes simultaneously on screen
« Reply #5 on: March 06, 2024, 06:09:36 pm »
You are randomly changing their positions, sizes and shapes on every frame so they will continue to move.

Loop through them all and set all of their positions before the main window loop: while (window.isOpen())

Then, loop through them all and draw (and only draw) them inside the main window loop. (between window.clear and window.display as you have already been doing)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything