SFML community forums

Help => General => Topic started by: regibigass on March 28, 2021, 07:15:31 pm

Title: How can i make a static array of shapes and then draw it?
Post by: regibigass on March 28, 2021, 07:15:31 pm
I have been trying to make a static array of circles and then draw it but cannot figure it out at all since my professor never told us how to do it or gave any resources. Can anyone help me figure out how to do this?

#include <SFML/Graphics.hpp>
#include <iostream>

namespace Scene
{
    const int num_shapes=5;
    //LAB: declare a static array of CircleShapes. The size should be num_shapes.

    static sf::CircleShape circle[num_shapes];

    sf::Clock clock;    //keep track of elapsed time
    sf::Shader shader;  //the shader to use when drawing the shapes
    sf::Color clear_color;

    sf::Color GetClearColor()
    {
        return clear_color;
    }

    void Initialize()
    {
        //Initialize the scene
        clear_color = sf::Color::Black;
       

        //LAB: in a for loop, initialize each shape's radius, origin, color, etc.

        for (int i = 200; i <= 1000; i = i + 200)
        {
            sf::CircleShape circle(100.0f);
            circle.setOrigin(100.0f, 100.0f);
            circle.setOutlineThickness(3.0f);
            circle.setFillColor(sf::Color( (i*50), (5 - i) * 50, 255));
            circle.setPosition(i, 400);

        };
        // load both shaders
        if (!shader.loadFromFile("Lab7.vert", "Lab7.frag"))
        {
            //if shader creation fails, alert the user by setting the clear color to magenta
            std::cout << "Could not load shader" << std::endl;
            clear_color = sf::Color(255, 0, 255);
        }
    }

    void Draw(sf::RenderWindow& window)
    {
        //LAB: use a for loop to draw the entire array of shapes
        window.draw(circle);

    };

    void Animate()
    {
        sf::Time elapsed = clock.getElapsedTime();
        float time = elapsed.asSeconds();

        //LAB: send time as a uniform variable to the shader

    }

    void HandleKeyEvent(sf::Event event)
    {
        //Handle key events

        if(event.key.code == sf::Keyboard::R)    
        {
            //reload shader
            bool success = shader.loadFromFile("Lab7.vert", "Lab7.frag");
            if(success == true)
            {
                clear_color = sf::Color::Black;
            }
            else
            {
                clear_color = sf::Color(255, 0, 255);
            }
        }
    }
    void HandleMouseEvent(sf::Event event)
    {
        //Handle mouse events

        //LAB: for MouseMoved events send the mouse coordinates to the shader

    }
};
 
Title: Re: How can i make a static array of shapes and then draw it?
Post by: eXpl0it3r on March 29, 2021, 02:45:37 pm
You should really use a class. Global SFML resource will lead to crashes and other undefined behavior.

Also, don't forget your professor is being paid to help you beyond just holding the lecture, so don't hesitate to reach out to them as well. ;)
Title: Re: How can i make a static array of shapes and then draw it?
Post by: regibigass on March 30, 2021, 03:28:46 am
You should really use a class. Global SFML resource will lead to crashes and other undefined behavior.

Also, don't forget your professor is being paid to help you beyond just holding the lecture, so don't hesitate to reach out to them as well. ;)

The specific assignment says to use a static array, believe me if there was another way to do this I would have done it. And have emailed my professor 2 times and his assistant twice as well and neither have responded.

Sorry for the mildly hostile response, tired and annoyed because of this assignment.
Title: Re: How can i make a static array of shapes and then draw it?
Post by: Nexus on March 30, 2021, 08:54:49 am
"Static array" can also be understood as the counterpart to "dynamic array", i.e. an array with fixed size at compile time, or simply an array -- not a pointer allocated with new[]. I would try this.

Using the keyword static just so it's used really makes no sense. Maybe we're missing some context.