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 - Maximus111

Pages: [1]
1
General / create several instances
« on: April 19, 2019, 03:16:52 pm »
Hi guys, i'm stuck in this piece of code. I've made this array of circles, which got a dynamic outline opacity. Its should become a template for blinking lights. My question is: how can i made multiple instances of it, which appear in random places? Also feel free to play around with the code, by modifying:
PULSEPARTS, p_radius and p_outline.
#include "pch.h"
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace sf;

int main()
{
        RenderWindow window(VideoMode(800, 600), "Starfield", Style::Close);
        window.setVerticalSyncEnabled(true);
        window.setFramerateLimit(60);

        // Stars ///////////////////////////////////////////////////
        int s_blink = 5;
        int s_blinkspeed = 1;

        const int PULSEPARTS = 10;
        CircleShape pulse[PULSEPARTS];

        int p_radius = 5;
        int j = 0;
        int p_outline = 1;

        int p_blink[PULSEPARTS];
        int p_blinkspeed = 1;

        // Random generator
        srand((int)time(0) * 10);
        float p_width = (rand() % 800);
        float p_height = (rand() % 600);

        for (int i = 0; i < PULSEPARTS; i++) {
                if (i == 0) {
                        pulse[i].setOutlineThickness(p_outline*=-1);
                }
                pulse[i].setRadius(p_radius + j);
                j += p_outline;
                pulse[i].setPointCount(100);
                pulse[i].setPosition(p_width, p_height);
                pulse[i].setOutlineThickness(p_outline);
                pulse[i].setOrigin(p_radius+j, p_radius+j);
                pulse[i].setFillColor(Color::Transparent);
        }
        ///////////////////////////////////////////////////////////

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

                s_blink -= s_blinkspeed;
                if (s_blink <= 0) {
                        s_blinkspeed *= -1;
                }
                else if (s_blink >= 255) {
                        s_blinkspeed *= -1;
                }
               
                for (int i = 0; i < PULSEPARTS; i++) {
                        pulse[i].setOutlineColor(Color(255, 255, 255, i* s_blink));
                }      

                window.clear();
               
                for (int i = 0; i < PULSEPARTS; i++) {
                        window.draw(pulse[i]);
                }

                window.display();
        }

        return 0;
}
 

Pages: [1]
anything