Guest User

Proper Example

a guest
Nov 20th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using SFML.System;
  5. using SFML.Graphics;
  6. using SFML.Window;
  7. namespace Example
  8. {
  9. class MainClass
  10. {
  11. private static RenderWindow _window;
  12.  
  13. public static void Main(string[] args)
  14. {
  15. Random R = new Random();
  16. _window = new RenderWindow(new VideoMode(800, 600), "SFML window");
  17. ParticleSystem particles = new ParticleSystem(1000);
  18. Clock clock = new Clock();
  19. _window.SetVisible(true);
  20. _window.SetFramerateLimit(60);
  21. _window.Closed += new EventHandler(OnClosed);
  22. while (_window.IsOpen)
  23. {
  24. Vector2i mouse = Mouse.GetPosition(_window);
  25. particles.SetEmitter(new Vector2f(mouse.X, mouse.Y));
  26. particles.Update(Time.FromMilliseconds(50), R);
  27. _window.DispatchEvents();
  28. _window.Clear(Color.Black);
  29. _window.Draw(particles);
  30. _window.Display();
  31. }
  32. }
  33. private static void OnClosed(object sender, EventArgs e)
  34. {
  35. _window.Close();
  36. }
  37. }
  38. class ParticleSystem : Transformable, Drawable
  39. {
  40. public VertexArray m_vertices;
  41. public Time m_lifetime;
  42. public List<Particle> m_particles;
  43. public Vector2f m_emitter;
  44.  
  45. public ParticleSystem(uint count)
  46. {
  47. m_particles = new List<Particle>((int)count);
  48. m_emitter = new Vector2f(0, 0);
  49. m_vertices = new VertexArray(PrimitiveType.Points);
  50. for (int i = 0; i != count; i++)
  51. {
  52. m_vertices.Append(new Vertex(m_emitter, Color.White));
  53. }
  54. m_lifetime = Time.FromSeconds(3);
  55. for (int i = 0; i < count; i++)
  56. {
  57. m_particles.Add(new Particle());
  58. }
  59. }
  60. public void Draw(RenderTarget target, RenderStates states)
  61. {
  62. states.Texture = null;
  63. states.Transform *= Transform;
  64. target.Draw(m_vertices, states);
  65. }
  66.  
  67. public void Update(Time Elapsed, Random R)
  68. {
  69. for(int i = 0; i != m_particles.Count(); i++)
  70. {
  71. var temp = m_vertices[(uint)i];
  72. m_particles[i].lifetime -= Elapsed / 2;
  73. if (m_particles[i].lifetime <= Time.Zero)
  74. {
  75. float Angle = ((R.Next() % 360) * 3.14f) / 180;
  76. float Speed = ((R.Next() % 50) + 50) / 3f;
  77.  
  78. m_particles[i].velocity = new Vector2f(
  79. (float)Math.Cos(Angle) * Speed,
  80. (float)Math.Sin(Angle) * Speed);
  81. m_particles[i].lifetime = Time.FromMilliseconds((R.Next()%2000)+1000);
  82. temp.Color = Color.White;
  83. temp.Position = m_emitter;
  84. }
  85. temp.Position += m_particles[i].velocity * .125f;
  86. float ratio =
  87. m_particles[i].lifetime.AsSeconds() / m_lifetime.AsSeconds();
  88. temp.Color.A = (byte)(ratio * 255);
  89. m_vertices[(uint)i] = temp;
  90. }
  91. }
  92. public void SetEmitter(Vector2f newEmitter)
  93. {
  94. m_emitter = newEmitter;
  95. }
  96. public class Particle
  97. {
  98. public Vector2f velocity;
  99. public Time lifetime;
  100. }
  101. }
  102. }
Add Comment
Please, Sign In to add comment