Advertisement
Guest User

SFML particle system

a guest
Nov 8th, 2017
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SFML;
  7. using SFML.System;
  8. using SFML.Graphics;
  9. using SFML.Window;
  10. namespace ConsoleApp1
  11. {
  12.  
  13.  
  14.     class MainClass
  15.     {
  16.         private static RenderWindow _window;
  17.  
  18.         public static void Main(string[] args)
  19.         {
  20.             _window = new RenderWindow(new VideoMode(800, 600), "SFML window");
  21.             ParticleSystem particles = new ParticleSystem(1000);
  22.             Clock clock = new Clock();
  23.             _window.SetVisible(true);
  24.             _window.SetFramerateLimit(60);
  25.             _window.Closed += new EventHandler(OnClosed);
  26.             while (_window.IsOpen)
  27.             {
  28.                 Vector2i mouse = Mouse.GetPosition(_window);
  29.                 //Console.WriteLine(mouse.X + " " + mouse.Y);
  30.                 particles.SetEmitter(new Vector2f(mouse.X, mouse.Y));
  31.                 particles.update(clock.Restart());
  32.                 _window.DispatchEvents();
  33.                 _window.Clear(Color.Black);
  34.                 _window.Draw(particles);
  35.                 _window.Display();
  36.                
  37.             }
  38.         }
  39.  
  40.         private static void OnClosed(object sender, EventArgs e)
  41.         {
  42.             _window.Close();
  43.         }
  44.     }
  45.     class ParticleSystem : Transformable, Drawable
  46.     {
  47.         public VertexArray m_vertices;
  48.         public Time m_lifetime;
  49.         public List<Particle> m_particles;
  50.         public Vector2f m_emitter;
  51.  
  52.         public ParticleSystem(int count)
  53.         {
  54.             m_particles = new List<Particle>(count);
  55.             m_vertices = new VertexArray(PrimitiveType.Points, (uint)count);
  56.             m_lifetime = Time.FromSeconds(count);
  57.             m_emitter = new Vector2f(0, 0);
  58.             for(int i = 0; i<count; i++)
  59.             {
  60.                 m_particles.Add(new Particle());
  61.                 resetParticle(i);
  62.             }
  63.         }
  64.         public void Draw(RenderTarget target, RenderStates states)
  65.         {
  66.             states.Texture = null;
  67.             states.Transform *= Transform;
  68.             target.Draw(m_vertices, states);
  69.         }
  70.         public void SetEmitter(Vector2f newEmitter)
  71.         {
  72.             m_emitter = newEmitter;
  73.             Console.WriteLine(newEmitter.X + " " + newEmitter.Y);
  74.         }
  75.         public void update(Time elapsed)
  76.         {
  77.             for (int i = 0; i < m_particles.Count; ++i)
  78.             {
  79.                 // update the particle lifetime
  80.                 Particle p = m_particles[i];
  81.                 p.lifetime -= elapsed;
  82.  
  83.                 m_particles[i] = p;
  84.  
  85.                 // if the particle is dead, respawn it
  86.                 if (p.lifetime <= Time.Zero)
  87.                     resetParticle(i);
  88.  
  89.                 // update the position of the corresponding vertex
  90.                 var a = m_vertices[(uint)i];
  91.                 a.Position += p.velocity * elapsed.AsSeconds();
  92.                
  93.  
  94.                 // update the alpha (transparency) of the particle according to its lifetime
  95.                 float ratio = p.lifetime.AsSeconds() / m_lifetime.AsSeconds();
  96.                 a.Color = Color.White;
  97.                 a.Color.A = (byte)(ratio * 255);
  98.                 m_vertices[(uint)i] = a;
  99.             }
  100.         }
  101.         void resetParticle(int index)
  102.         {
  103.             // give a random velocity and lifetime to the particle
  104.             float angle = (new Random().Next() % 360) * 3.14f / 180;
  105.             float speed = (new Random().Next() % 50) + 50;
  106.             var temp = m_particles[index];
  107.             temp.velocity = new SFML.System.Vector2f((float)(Math.Cos(angle) * speed), (float)(Math.Sin(angle) * speed));
  108.             temp.lifetime = Time.FromMilliseconds((new Random().Next() % 2000) + 1000);
  109.             m_particles[index] = temp;
  110.             // reset the position of the corresponding vertex
  111.             var temp2 = m_vertices[(uint)index];
  112.             temp2.Position = m_emitter;
  113.             m_vertices[(uint)index] = temp2;
  114.         }
  115.         public class Particle
  116.         {
  117.             public Vector2f velocity;
  118.             public Time lifetime;
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement