Advertisement
czaffik

sdl particles

Oct 29th, 2017
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.74 KB | None | 0 0
  1. PLIK PARTICLES.H:
  2.  
  3. #ifndef PARTICLES_H
  4. #define PARTICLES_H
  5.  
  6. #include <SDL2/SDL.h>
  7. #include <vector>
  8. #include <memory>
  9. #include "vector2d.h"
  10. #include "random.h"
  11.  
  12. class Particle
  13. {
  14.     public:
  15.         Particle(Vector2D _position, Vector2D _velocity, int _size, float _lifeTime, SDL_Color _color);
  16.         virtual ~Particle();
  17.  
  18.         void draw(SDL_Renderer* renderer);
  19.         void animate(float dt);
  20.  
  21.         void setPosition(Vector2D _position) { position = _position; }
  22.         Vector2D getPosition() const { return position; }
  23.         void setVelocity(Vector2D _velocity) { velocity = _velocity; }
  24.         void changeVelocity(Vector2D _dv) { velocity += _dv; }
  25.         void setSize(int _size) { size = _size; }
  26.         void changeSize(int ds) { size += ds; }
  27.         int getSize() const { return size; }
  28.         float getLifeTime() const { return lifeTime; }
  29.         void setColor(SDL_Color _color) { color = _color; }
  30.         float getCurrentTime() const { return time; }
  31.  
  32.         bool isAlive() { return live; }
  33.         void dead() { live = false; }
  34.         void setState(int i) { state = i; }
  35.         int getState() { return state; }
  36.  
  37.     protected:
  38.         SDL_Rect rect;
  39.  
  40.         Vector2D position;
  41.         Vector2D velocity;
  42.         int size;
  43.         float lifeTime;
  44.         SDL_Color color;
  45.  
  46.         bool live = true;
  47.         int state = 0;
  48.         float time = 0.0f;
  49.  
  50.     private:
  51. };
  52.  
  53. class Emiter
  54. {
  55.     public:
  56.         Emiter(Vector2D _startPoint, float _maxTime, int _width, int _height, int _minParticles, int _numParticles,
  57.                int _maxParticles, int _particleMinSize = 1, int _particleMaxSize = 2, float _particleTime = 1.0f,
  58.                int r = 255, int g = 255, int b = 255, int a = 255, float _velocityDispersion = 0.0f, int _repeat = 0,
  59.                float _frequency = 0.0f, Vector2D _drift = Vector2D(), bool _is_dir = false, float _dir = 0.0f,
  60.                float _speed = 0.0f);
  61.         virtual ~Emiter();
  62.  
  63.         void init();
  64.         void draw(SDL_Renderer *renderer);
  65.         void animate(float dt);
  66.  
  67.         void setDrift(Vector2D _drift) { drift = _drift; }
  68.         void setDirection(float _dir) { dir = _dir; }
  69.         void changeDirection(float _dr) { dir += _dr; }
  70.         void setSpeed(float _speed) { speed = _speed; }
  71.         bool isVisible() { return visible; }
  72.  
  73.     protected:
  74.         virtual void modifyParticles(float dt) = 0;
  75.  
  76.         Vector2D startPoint;
  77.         float maxTime;
  78.         int width;
  79.         int height;
  80.         int minParticles;
  81.         int numParticles;
  82.         int maxParticles;
  83.         int particleMinSize;
  84.         int particleMaxSize;
  85.         float particleTime;
  86.         SDL_Color particleColor;
  87.         float velocityDispersion;
  88.         int repeat;
  89.         float frequency;
  90.         Vector2D drift;
  91.         bool is_dir;
  92.         float dir;
  93.         float speed;
  94.  
  95.         std::vector<std::unique_ptr<Particle> > particle;
  96.         bool visible = true;
  97.         float time = 0.0f;
  98.         Random random;
  99.  
  100.     private:
  101.         void createParticles(float dt);
  102.         void createParticle();
  103.  
  104.         float w, h, t = 0;
  105.         int r = 0;
  106.         Vector2D v;
  107. };
  108.  
  109. class Fire : public Emiter
  110. {
  111.     public:
  112.         Fire(Vector2D _startPoint, float _direction);
  113.         ~Fire();
  114.  
  115.     protected:
  116.         void modifyParticles(float dt);
  117.  
  118.     private:
  119. };
  120.  
  121. class Explosion : public Emiter
  122. {
  123.     public:
  124.         Explosion(Vector2D _startPoint, float _direction);
  125.         ~Explosion();
  126.  
  127.     protected:
  128.         void modifyParticles(float dt);
  129.  
  130.     private:
  131. };
  132.  
  133. #endif // PARTICLES_H
  134.  
  135. PLIK PARTICLES.CPP:
  136.  
  137. #include "particles.h"
  138. #include <iostream>
  139. #include <cmath>
  140. using namespace std;
  141.  
  142. Particle::Particle(Vector2D _position, Vector2D _velocity, int _size, float _lifeTime, SDL_Color _color)
  143.     : position(_position), velocity(_velocity), size(_size), lifeTime(_lifeTime), color(_color)
  144. {
  145.  
  146. }
  147.  
  148. Particle::~Particle()
  149. {
  150.  
  151. }
  152.  
  153. void Particle::draw(SDL_Renderer* renderer)
  154. {
  155.     if (!live) return;
  156.  
  157.     SDL_Rect rect;
  158.     rect.x = position.getX();
  159.     rect.y = position.getY();
  160.     rect.w = size;
  161.     rect.h = size;
  162.  
  163.     SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
  164.     SDL_RenderFillRect(renderer, &rect);
  165. }
  166.  
  167. void Particle::animate(float dt)
  168. {
  169.     if (time >= lifeTime)
  170.     {
  171.         live = false;
  172.         return;
  173.     }
  174.  
  175.     position += velocity*dt;
  176.     time += dt;
  177. }
  178.  
  179. Emiter::Emiter(Vector2D _startPoint, float _maxTime, int _width, int _height, int _minParticles, int _numParticles,
  180.                int _maxParticles, int _particleMinSize, int _particleMaxSize, float _particleTime, int r, int g, int b,
  181.                int a, float _velocityDispersion, int _repeat, float _frequency, Vector2D _drift, bool _is_dir,
  182.                float _dir, float _speed)
  183.     : startPoint(_startPoint), maxTime(_maxTime), width(_width), height(_height), minParticles(_minParticles),
  184.       numParticles(_numParticles), maxParticles(_maxParticles), particleMinSize(_particleMinSize),
  185.       particleMaxSize(_particleMaxSize), particleTime(_particleTime), velocityDispersion(_velocityDispersion),
  186.       repeat(_repeat), frequency(_frequency), drift(_drift), is_dir(_is_dir), dir(_dir), speed(_speed)
  187. {
  188.     particleColor.r = r; particleColor.g = g; particleColor.b = b; particleColor.a = a;
  189.     init();
  190. }
  191.  
  192. Emiter::~Emiter()
  193. {
  194.  
  195. }
  196.  
  197. void Emiter::init()
  198. {
  199.     if (is_dir)
  200.     {
  201.         dir = dir*M_PI/180.0f;
  202.         float len = sqrt(width*width + height*height);
  203.         w = len*cos(dir);
  204.         h = len*sin(dir);
  205.     }
  206.     else
  207.     {
  208.         w = width;
  209.         h = height;
  210.     }
  211.  
  212.     for (int i = 0; i < minParticles; i++) createParticle();
  213. }
  214.  
  215. void Emiter::draw(SDL_Renderer *renderer)
  216. {
  217.     for (const auto &p: particle)
  218.     {
  219.         p->draw(renderer);
  220.     }
  221. }
  222.  
  223. void Emiter::animate(float dt)
  224. {
  225.     if ((particle.empty()) || (maxTime != -1 && time > maxTime))
  226.     {
  227.         visible = false;
  228.         return;
  229.     }
  230.  
  231.     for (unsigned int i = 0; i < particle.size(); i++)
  232.     {
  233.         particle[i]->animate(dt);
  234.         if (!particle[i]->isAlive())
  235.             particle.erase(particle.begin() + i);
  236.     }
  237.  
  238.     createParticles(dt);
  239.     modifyParticles(dt);
  240.  
  241.     while (particle.size() >= static_cast<unsigned int>(maxParticles)) particle.pop_back();
  242.  
  243.     time += dt;
  244. }
  245.  
  246. void Emiter::createParticles(float dt)
  247. {
  248.     if (repeat != -1)
  249.     {
  250.         if (r >= repeat)
  251.             return;
  252.  
  253.         if (t >= frequency)
  254.         {
  255.             for (int i = 0; i < numParticles; i++) createParticle();
  256.             t = 0.0f;
  257.             r += 1;
  258.         }
  259.  
  260.         t += dt;
  261.     }
  262.     else createParticle();
  263. }
  264.  
  265. void Emiter::createParticle()
  266. {
  267.     if (is_dir)
  268.         v = Vector2D(speed*cos(dir) + random.number(-velocityDispersion, velocityDispersion)*sin(dir),
  269.                      speed*sin(dir) + random.number(-velocityDispersion, velocityDispersion)*cos(dir))
  270.             + drift;
  271.     else
  272.         v = Vector2D(random.number(-velocityDispersion, velocityDispersion),
  273.                      random.number(-velocityDispersion, velocityDispersion))
  274.             + drift;
  275.  
  276.     particle.push_back(make_unique<Particle>(Vector2D(startPoint.getX() + random.number(-w, w),
  277.                                                       startPoint.getY() + random.number(-h, h)),
  278.                                              v, random.number(particleMinSize, particleMaxSize),
  279.                                              particleTime, particleColor));
  280. }
  281.  
  282. Fire::Fire(Vector2D _startPoint, float _direction)
  283.     : Emiter(_startPoint, -1, 0, 2, 5, 5, 400, 1, 3, 0.4f, 255, 0, 0, 200, 50, -1, 0.01f, Vector2D(0.0f, 0.0f), true,
  284.              270.0f, 600.0f)
  285. {
  286.  
  287. }
  288.  
  289. Fire::~Fire()
  290. {
  291.  
  292. }
  293.  
  294. void Fire::modifyParticles(float dt)
  295. {
  296.     SDL_Color color; color.r = 25; color.g = 25; color.b = 25; color.a = 100;
  297.  
  298.     for (unsigned int i = 0; i < particle.size(); i++)
  299.     {
  300.         if ((particle[i]->getPosition() - startPoint).length() > 100.0f)
  301.         {
  302.             particle[i]->setState(1);
  303.             particle[i]->setColor(color);
  304.         }
  305.     }
  306. }
  307.  
  308.  
  309. Explosion::Explosion(Vector2D _startPoint, float _direction)
  310.     : Emiter(_startPoint, 100, 10, 10, 100, 5, 400, 1, 3, 0.5f, 255, 0, 0, 155, 200, 10, 0.01f, Vector2D(500.0f, 0.0f),
  311.              false, 0.0f, 0.0f)
  312. {
  313.  
  314. }
  315.  
  316. Explosion::~Explosion()
  317. {
  318.  
  319. }
  320.  
  321. void Explosion::modifyParticles(float dt)
  322. {
  323.     SDL_Color color; color.r = 25; color.g = 25; color.b = 25; color.a = 100;
  324.  
  325.     for (unsigned int i = 0; i < particle.size(); i++)
  326.     {
  327.         if ((particle[i]->getCurrentTime() >= (particle[i]->getLifeTime()*0.5f)) && (particle[i]->getState() == 0))
  328.         {
  329.             particle[i]->setState(1);
  330.             particle[i]->setColor(color);
  331.         }
  332.     }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement