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

Author Topic: create several instances  (Read 1118 times)

0 Members and 1 Guest are viewing this topic.

Maximus111

  • Newbie
  • *
  • Posts: 2
    • View Profile
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;
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10924
    • View Profile
    • development blog
    • Email
Re: create several instances
« Reply #1 on: April 20, 2019, 10:39:18 am »
What exactly do you mean with "multiple instances"? You already do have multiple CircleShapes.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Maximus111

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: create several instances
« Reply #2 on: April 20, 2019, 01:06:19 pm »
I want to figure out how I can make a function out of it. In order to draw this animation as often as i want to.