Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This program is made by thecplusplusguy from YouTube for demonstartion purposes.
- //Feel free to read, learn, or modifie the source as you wish, LGPL licence, I guess.
- //http://www.youtube.com/user/thecplusplusguy
- //keep this header comment for advertisment :)
- //compile in case of Windows normally, in case of Linux normally as well, so:
- //g++ particle.cpp -lSDL
- //Code::blocks: build and run
- #include <SDL/SDL.h>
- #include <iostream>
- #include <vector> //to store the particles (I like std::vector :D)
- #include <cstdlib> //for the random numbers
- #include <ctime>
- class particle{ //represent 1 particle
- float x,y,xvel,yvel; //position && speed
- Uint32 endTime; //death time
- Uint8 color; //color
- public:
- particle(float X,float Y,float Xvel,float Yvel,int life,Uint8 Color);
- void move();
- void show();
- bool isDead(); //true if the particle dead else false
- };
- particle::particle(float X,float Y,float Xvel,float Yvel,int life,Uint8 Color)
- {
- //initialize the variables, nothing new
- x=X;
- y=Y;
- xvel=Xvel;
- yvel=Yvel;
- endTime=SDL_GetTicks()+life; //the end time, is the current time + the time, while the particle lives
- color=Color;
- }
- void particle::move()
- {
- //move
- x+=xvel;
- y+=yvel;
- //if it is outside of the screen, set it back to the edge of the screen
- if(x<0)
- x=0;
- if(y<0)
- y=0;
- if(x>SDL_GetVideoSurface()->w)
- x=SDL_GetVideoSurface()->w-1;
- if(y>SDL_GetVideoSurface()->h)
- y=SDL_GetVideoSurface()->h-1;
- }
- void particle::show()
- {
- //just set the pixel to the current color at the current position (pixel manipulation tutorial)
- Uint8* pixels=(Uint8*)SDL_GetVideoSurface()->pixels;
- Uint8* pixel=pixels+(int)y*SDL_GetVideoSurface()->pitch+(int)x;
- *pixel=color;
- }
- bool particle::isDead()
- {
- //read above, what it does
- return (SDL_GetTicks()>=endTime || x==0 || y==0 || x==SDL_GetVideoSurface()->w-1 || y==SDL_GetVideoSurface()->h-1);
- }
- //the particle engine will store all of the particle, and their initialize position
- class particleEngine{
- std::vector<particle*> particles; //this vector represent all of the particle
- int x,y,maxparticle;
- public:
- particleEngine(int maxpart,int X, int Y);
- ~particleEngine(); //need for deallocate allocated space
- void refresh(); //move && show the particles
- };
- particleEngine::particleEngine(int maxpart,int X, int Y)
- {
- x=X;
- y=Y;
- maxparticle=maxpart;
- for(int i=0;i<maxparticle;i++) //create maxparticle number of particles with random values both for position, velocity, life, color
- 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));
- }
- particleEngine::~particleEngine()
- {
- //deallocate all reserved memory
- for(int i=0;i<maxparticle;i++)
- delete particles[i];
- }
- void particleEngine::refresh()
- {
- for(int i=0;i<maxparticle;i++)
- {
- if(particles[i]->isDead()) //if dead
- {
- delete particles[i]; //delete the earlier one and create a new
- 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);
- }else{ //else
- particles[i]->move(); //move && show
- particles[i]->show();
- }
- }
- }
- int main(int argc,char** argv)
- {
- //this should be familiar to you
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_Surface* screen;
- screen=SDL_SetVideoMode(640,480,8,SDL_SWSURFACE);
- bool running=true;
- const int FPS=30; //make the FPS bigger, for quicker animation
- Uint32 start;
- srand(time(0)); //seed the random number generator
- particleEngine ps(5000,screen->w/2,screen->h/2); //create the particles
- while(running)
- {
- start=SDL_GetTicks();
- SDL_Event event;
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_QUIT:
- running=false;
- break;
- }
- }
- SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0x00,0x00,0x00)); //clear the screen
- //logic
- ps.refresh(); //refresh the particles
- SDL_Flip(screen);
- if(1000/FPS>SDL_GetTicks()-start)
- SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
- }
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment