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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - regibigass

Pages: [1]
1
General / How can i make a static array of shapes and then draw it?
« 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

    }
};
 

Pages: [1]