16
Graphics / Re: Weird lines in particle test
« on: December 01, 2013, 09:36:39 pm »
Interesting...
Why's this?
Why's this?
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.
#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
using namespace std;
sf::Vector2f Randomize(int Boundary1, int Boundary2)
{
return sf::Vector2f((0 +((rand() % Boundary1))), (0 +((rand() % Boundary2))));
}
int RandomizeInt(int Boundary1, int Boundary2)
{
return (Boundary1 +((rand() % Boundary2)));
}
struct PI
{
sf::Color Colour = sf::Color::Yellow;
int X = 0;
int Y = 0;
int LifeTime = 20000;
};
int main()
{
int FPS = 0; //Setting up variables for measuring the FPS
sf::Clock FPSClock;
sf::Time FPSTime;
sf::Music music;
if (!music.openFromFile("sound.ogg"))
return -1; // error
music.play();
bool MousePressed = false;
sf::Clock EffectClock;
sf::Time EffectTime;
int ActiveParticles = 0; //How many particles are active
int ParticleCap = 0; //The limit of how many particles can be placed
cout << "\nParticle Cap: ";
cin >> ParticleCap;
PI ParticleInfo[ParticleCap]; //Our particle information such as colour and lifetime.
sf::VertexArray Pixel(sf::Points, ParticleCap);
sf::RenderWindow window(sf::VideoMode(800, 600), "Partical System");
window.setVerticalSyncEnabled(true);
while (window.isOpen())
{
sf::Vector2i MousePosition = sf::Mouse::getPosition(window); //Storing mouse coords for later use
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && MousePressed == false) //Checking if mouse is pressed
{
for(int a = 0; a < ParticleCap; a++) //Updating particle information
{
Pixel[a].position = sf::Vector2f(MousePosition.x,MousePosition.y);
ParticleInfo[a].X = MousePosition.x;
ParticleInfo[a].Y = MousePosition.y;
}
MousePressed = true;
EffectClock.restart();
}
else if(!sf::Mouse::isButtonPressed(sf::Mouse::Left) && MousePressed == true)
{
MousePressed = false;
}
sf::Event event; //Polling window events to check for user input
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
EffectTime = EffectClock.getElapsedTime();
for(int a = 0; a < ParticleCap; a++) //Updating particle information
{
ParticleInfo[a].LifeTime = RandomizeInt(0,40);
ParticleInfo[a].X = ParticleInfo[a].X + RandomizeInt(0,6) - 3;
ParticleInfo[a].Y = ParticleInfo[a].Y + RandomizeInt(0,6) - 3;
Pixel[a].color = ParticleInfo[a].Colour;
if(EffectTime.asMilliseconds() > ParticleInfo[a].LifeTime)
{
//Pixel[a].position = sf::Vector2f(-1,-1);
ParticleInfo[a].X = MousePosition.x;
ParticleInfo[a].Y = MousePosition.y;
Pixel[a].position = sf::Vector2f(ParticleInfo[a].X, ParticleInfo[a].Y);
EffectClock.restart();
}
else
{
Pixel[a].position = sf::Vector2f(ParticleInfo[a].X, ParticleInfo[a].Y - 6);
ParticleInfo[a].Y = ParticleInfo[a].Y - 6;
}
}
window.clear(sf::Color::Black);
window.draw(Pixel);
window.display();
FPS++; //Calculating FPS and displaying it
FPSTime = FPSClock.getElapsedTime();
if(FPSTime.asSeconds() >= 1)
{
cout << "\nFPS: " << FPS << endl;
FPSClock.restart();
FPS = 0;
}
}
return 0;
}