Advertisement
mgostih

Purple Rain Drops in C++ SFML

Jun 28th, 2017
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <vector>
  3. #include <random>
  4. #include <cmath>
  5.  
  6. const int Width = 1080;
  7. const int Heigth = 720;
  8.  
  9. class RainDrop {
  10. private:
  11.     double _x, _y, _z;
  12.     int ceiling=Heigth;
  13.     double yspeed=0, gravity=0;
  14.    
  15. public:
  16.    
  17.     RainDrop() : _x(0), _y(0), _z(1),gravity(0) {};
  18.     RainDrop(double x, double y, double z, double G = 0) : _x(x), _y(y), _z(z) { SetGravity(G); };
  19.     void Show(sf::RenderWindow & window) {
  20.         using namespace sf;
  21.         RectangleShape Drop;
  22.         Drop.setFillColor(Color::Magenta);
  23.         Drop.setSize(Vector2f(_z,2*_z));
  24.         Drop.setPosition(_x, _y);
  25.         window.draw(Drop);
  26.     }
  27.     bool Fall() {
  28.         if (_y > ceiling) {
  29.             yspeed = 0;
  30.             return 1;
  31.         }
  32.         yspeed -= gravity;
  33.         _y -= yspeed;
  34.         return 0;
  35.        
  36.     }
  37.     void SetGravity(double Gravity){
  38.         gravity = Gravity * (std::exp(_z/2)) / 100;
  39.     }
  40.     void SetCoordX(double x) {
  41.         _x = x;
  42.     }
  43.     void SetCoordY(double y) {
  44.         _y = y;
  45.     }
  46.  
  47. };
  48.  
  49. int main()
  50. {
  51.    
  52.  
  53.     sf::RenderWindow window(sf::VideoMode(Width, Heigth), "SFML works!");
  54.     window.setFramerateLimit(60);
  55.  
  56.     std::random_device rd;
  57.     std::mt19937 mt(rd());
  58.     std::uniform_real_distribution<> RandX(0, Width);
  59.     std::uniform_real_distribution<> RandY(0, -100);
  60.     std::uniform_real_distribution<> RandZ(1,7);
  61.  
  62.     const int RainDrops = 2000;
  63.     std::vector<RainDrop> Rain(RainDrops);
  64.     for (auto & Drop : Rain) {
  65.         Drop = RainDrop(RandX(mt),RandY(mt),RandZ(mt),0.1);
  66.     }
  67.    
  68.     while (window.isOpen())
  69.     {
  70.         sf::Event event;
  71.         while (window.pollEvent(event))
  72.         {
  73.             if (event.type == sf::Event::Closed)
  74.                 window.close();
  75.         }
  76.  
  77.         window.clear();
  78.        
  79.         for (auto & Drop : Rain) {
  80.             if (Drop.Fall()) {
  81.                 Drop.SetCoordX(RandX(mt));
  82.                 Drop.SetCoordY(RandY(mt));
  83.             }
  84.             Drop.Show(window);
  85.         }
  86.         window.display();
  87.     }
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement