SFML community forums

Help => Graphics => Topic started by: Aqua_coder on March 03, 2022, 10:13:04 am

Title: How to color the lines of a Vertex Array
Post by: Aqua_coder on March 03, 2022, 10:13:04 am
Recently I was making a Maths Plotter on SFML, and I have done the basic core features like Drawing the animations and input.

But I wanted to know if I can change the color of a vertex array.
Here is the code for reference


void drawFunction(sf::RenderWindow &window)
        {
            while(x < 30 && drawing == true)
            {
                aq::sleep(0.01);
                x += 0.1;
                std::cout << x << std::endl;
                window.clear();
                curve.setPosition(sf::Vector2f(x*20.f + window.getSize().x/2,  
                    -1*(sin(x))*20.f + window.getSize().y/2));
                function.append(
                    sf::Vertex(curve.getPosition()));
               
                window.draw(function);
                window.draw(curve);
                window.draw(sprite);
                window.draw(x_axis);
                window.draw(y_axis);
                window.display();
            }
            if(x > 15 || drawing == false || (x > 15 && drawing == false))
            {
                drawing = false;
                window.clear();
                window.draw(function);
                window.draw(sprite);
                window.draw(x_axis);
                window.draw(y_axis);
                window.display();
            }
        }
Title: Re: How to color the lines of a Vertex Array
Post by: fallahn on March 03, 2022, 10:39:47 am
sf::Vertex has a constructor which takes a colour as a parameter, as well as exposing its colour member so you can set it directly:

https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Vertex.php#a70b0679b4ec531d5bd1a7d0225c7321a
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Vertex.php#a799faa0629442e90f07cd2edb568ff80