ForrestFox

Rainy Droplets

Feb 28th, 2021 (edited)
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <sdl2framework.cc>
  2.  
  3. using namespace std;
  4. SDL2framework* win;
  5.  
  6. struct droplet {
  7.  
  8.     struct vec2 pos;
  9.     struct vec2 spd;
  10.     float color;
  11. };
  12.  
  13. int timer;
  14. const int maxdrop = 8192;
  15.  
  16. struct droplet droplets[16384];
  17.  
  18. void update() {
  19.  
  20.     win->cls(0);
  21.  
  22.     int w = win->w(),
  23.         h = win->h();
  24.  
  25.     struct vec3i destcl;
  26.  
  27.     for (int x = 0; x < w; x++) {
  28.  
  29.         int y = 160 + win->fbm((float)x/75, 0)*64;
  30.         win->line(x, y, x, h, -0x202020);
  31.     }
  32.  
  33.     for (int i = 0; i < maxdrop; i++) {
  34.  
  35.         int cl = droplets[i].color;
  36.         float x = droplets[i].pos.x;
  37.         float y = droplets[i].pos.y;
  38.  
  39.         // Дождь или брызги
  40.         if (cl < 256 && cl >= 0)
  41.              destcl = {0, cl>>2, cl>>1};
  42.         else destcl = {96, 96, 96};
  43.  
  44.         // Фонарный столб
  45.         if (y > h - 100) {
  46.  
  47.             int m = x - 300;
  48.             int n = ((h-120)-y)/2;
  49.  
  50.             if ((m < 0 && m > n) || (m > 0 && m < -n) )
  51.                 destcl = {cl, cl, cl};
  52.         }
  53.  
  54.         // Рисование точки
  55.         win->pset(x, y, -win->rgb(destcl.x, destcl.y, destcl.z));
  56.  
  57.         // Ускорение движения частиц
  58.         if (droplets[i].spd.y > 2)       droplets[i].spd.y = 2;
  59.         else if (droplets[i].spd.y <= 2) droplets[i].spd.y += 0.4;
  60.  
  61.         if (droplets[i].spd.x < -2) droplets[i].spd.x = -2;
  62.         if (droplets[i].spd.x >  2) droplets[i].spd.x =  2;
  63.  
  64.         droplets[i].spd.x = sin((float)timer/360);
  65.  
  66.         // Скорость частиц
  67.         x = x + droplets[i].spd.x;
  68.         y = y + droplets[i].spd.y;
  69.  
  70.         // Циклическое перемещение
  71.         if (x < 0) x += w;
  72.         else if (x >= w) x -= w;
  73.  
  74.         if (y > h) {
  75.  
  76.             // Отскок
  77.             if (droplets[i].color < 256 && droplets[i].color >= 0) {
  78.  
  79.                 x += (rand()%8 - 4);
  80.                 droplets[i].spd.y = -droplets[i].spd.y;
  81.                 droplets[i].color = 256;
  82.  
  83.             } else {
  84.  
  85.                 droplets[i].spd.y = ((rand() % 4) / 2 + 0.5);
  86.                 droplets[i].color = rand() & 255;
  87.                 y = 0;
  88.             }
  89.         }
  90.  
  91.         droplets[i].pos.x = x;
  92.         droplets[i].pos.y = y;
  93.     }
  94.  
  95.     // Фонарный столб
  96.     for (int y = h-110; y < h; y++)
  97.     for (int x = 300-6; x < 300+6; x++) {
  98.  
  99.         if (y > h-106 && (x < 300-2 || x >= 300+2))
  100.             continue;
  101.  
  102.         int addc = (y == h-106 ? 192 : 32);
  103.         struct vec3i m = win->i2rgb( win->point(x, y) );
  104.  
  105.         win->pset(x, y, -win->rgb(m.x + addc, m.y + addc, m.z + addc));
  106.     }
  107.  
  108.     win->locate(1, 1);
  109.     win->color(7, -1);
  110.     win->print("Дождливая ночь");
  111.  
  112.     timer++;
  113. }
  114.  
  115. int main(int argc, char* argv[]) {
  116.  
  117.     win = new SDL2framework(11);
  118.     timer = 0;
  119.  
  120.     // Инициализация капель
  121.     for (int i = 0; i < maxdrop; i++) {
  122.  
  123.         droplets[i].pos = { (float)(rand()%win->w()), (float)(rand()%win->h()) };
  124.         droplets[i].spd = { 0, (float)((rand() % 4) / 2 + 0.5) };
  125.         droplets[i].color = rand() & 255;
  126.     }
  127.  
  128.     while (win->poll()) {
  129.  
  130.         if (win->timer()) {
  131.  
  132.             update();
  133.             win->record(argc, argv);
  134.         }
  135.     }
  136.  
  137.     return 0;
  138. }
Add Comment
Please, Sign In to add comment