thecplusplusguy

C++/SDL - Particle engine

Jul 16th, 2011
1,994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. //This program is made by thecplusplusguy from YouTube for demonstartion purposes.
  2. //Feel free to read, learn, or modifie the source as you wish, LGPL licence, I guess.
  3. //http://www.youtube.com/user/thecplusplusguy
  4. //keep this header comment for advertisment :)
  5. //compile in case of Windows normally, in case of Linux normally as well, so:
  6. //g++ particle.cpp -lSDL
  7. //Code::blocks: build and run
  8. #include <SDL/SDL.h>
  9. #include <iostream>
  10. #include <vector>   //to store the particles (I like std::vector :D)
  11. #include <cstdlib>  //for the random numbers
  12. #include <ctime>
  13.  
  14. class particle{ //represent 1 particle
  15.     float x,y,xvel,yvel;    //position && speed
  16.     Uint32 endTime; //death time
  17.     Uint8 color;    //color
  18.     public:
  19.     particle(float X,float Y,float Xvel,float Yvel,int life,Uint8 Color);
  20.     void move();
  21.     void show();
  22.     bool isDead();  //true if the particle dead else false
  23. };
  24.  
  25. particle::particle(float X,float Y,float Xvel,float Yvel,int life,Uint8 Color)
  26. {
  27.     //initialize the variables, nothing new
  28.     x=X;
  29.     y=Y;
  30.     xvel=Xvel;
  31.     yvel=Yvel;
  32.     endTime=SDL_GetTicks()+life; //the end time, is the current time + the time, while the particle lives
  33.     color=Color;
  34. }
  35.  
  36. void particle::move()
  37. {
  38.     //move
  39.     x+=xvel;
  40.     y+=yvel;
  41.     //if it is outside of the screen, set it back to the edge of the screen
  42.     if(x<0)
  43.         x=0;
  44.     if(y<0)
  45.         y=0;
  46.     if(x>SDL_GetVideoSurface()->w)
  47.         x=SDL_GetVideoSurface()->w-1;
  48.     if(y>SDL_GetVideoSurface()->h)
  49.         y=SDL_GetVideoSurface()->h-1;
  50. }
  51.  
  52. void particle::show()
  53. {
  54.     //just set the pixel to the current color at the current position (pixel manipulation tutorial)
  55.     Uint8* pixels=(Uint8*)SDL_GetVideoSurface()->pixels;
  56.     Uint8* pixel=pixels+(int)y*SDL_GetVideoSurface()->pitch+(int)x;
  57.     *pixel=color;
  58. }
  59.  
  60. bool particle::isDead()
  61. {
  62.     //read above, what it does
  63.     return (SDL_GetTicks()>=endTime || x==0 || y==0 || x==SDL_GetVideoSurface()->w-1 || y==SDL_GetVideoSurface()->h-1);
  64. }
  65. //the particle engine will store all of the particle, and their initialize position
  66. class particleEngine{
  67.     std::vector<particle*> particles;   //this vector represent all of the particle
  68.     int x,y,maxparticle;
  69.     public:
  70.     particleEngine(int maxpart,int X, int Y);
  71.     ~particleEngine();  //need for deallocate allocated space
  72.     void refresh(); //move && show the particles
  73. };
  74.  
  75. particleEngine::particleEngine(int maxpart,int X, int Y)
  76. {
  77.     x=X;
  78.     y=Y;
  79.     maxparticle=maxpart;
  80.     for(int i=0;i<maxparticle;i++)  //create maxparticle number of particles with random values both for position, velocity, life, color
  81.         particles.push_back(new particle(x+rand()%6-3,y+rand()%6-3,rand()%10+(float)rand()/(float)RAND_MAX-5,rand()%10+(float)rand()/(float)RAND_MAX-5,500+rand()%1000,rand()%255));
  82. }
  83.  
  84. particleEngine::~particleEngine()
  85. {
  86.     //deallocate all reserved memory
  87.     for(int i=0;i<maxparticle;i++)
  88.         delete particles[i];
  89. }
  90.  
  91. void particleEngine::refresh()
  92. {
  93.     for(int i=0;i<maxparticle;i++)
  94.     {
  95.         if(particles[i]->isDead())  //if dead
  96.         {
  97.             delete particles[i];    //delete the earlier one and create a new
  98.             particles[i]=new particle(x+rand()%6-3,y+rand()%6-3,rand()%10+(float)rand()/(float)RAND_MAX-5,rand()%10+(float)rand()/(float)RAND_MAX-5,500+rand()%2000,rand()%255);
  99.         }else{  //else
  100.             particles[i]->move();   //move && show
  101.             particles[i]->show();
  102.         }
  103.     }
  104. }
  105.  
  106. int main(int argc,char** argv)
  107. {
  108.     //this should be familiar to you
  109.     SDL_Init(SDL_INIT_EVERYTHING);
  110.     SDL_Surface* screen;
  111.     screen=SDL_SetVideoMode(640,480,8,SDL_SWSURFACE);  
  112.     bool running=true;
  113.     const int FPS=30;   //make the FPS bigger, for quicker animation
  114.     Uint32 start;
  115.     srand(time(0)); //seed the random number generator
  116.     particleEngine ps(5000,screen->w/2,screen->h/2);    //create the particles
  117.     while(running)
  118.     {
  119.         start=SDL_GetTicks();
  120.         SDL_Event event;
  121.         while(SDL_PollEvent(&event))
  122.         {
  123.             switch(event.type)
  124.             {
  125.                 case SDL_QUIT:
  126.                     running=false;
  127.                     break;
  128.             }
  129.         }
  130.  
  131.     SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0x00,0x00,0x00));  //clear the screen
  132.         //logic
  133.     ps.refresh();   //refresh the particles
  134.  
  135.  
  136.     SDL_Flip(screen);
  137.         if(1000/FPS>SDL_GetTicks()-start)
  138.             SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  139.     }
  140.     SDL_Quit();
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment