Advertisement
thecplusplusguy

Cool fire effect SDL, C++

Jul 6th, 2012
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. //Implemented by http://www.youtube.com/user/thecplusplusguy
  2. //SDL library used
  3. //fire effect
  4. #include <iostream>
  5. #include <SDL/SDL.h>
  6. using namespace std;
  7. const int FPS=30;
  8. const int WIDTH=640;
  9. const int HEIGHT=480;
  10. const float dump=0.5;
  11. Uint32 colors[100];
  12. float fire[HEIGHT][WIDTH];
  13.  
  14. void generateFire()
  15. {
  16.         for(int i=0;i<WIDTH;i++)
  17.         {
  18.             int ran=rand()%50;
  19.             if(ran==0)
  20.                 fire[HEIGHT-1][i]=0;
  21.             if(ran==1)
  22.                 fire[HEIGHT-1][i]=99;
  23.         }
  24.     for(int i=0;i<HEIGHT-1;i++)
  25.         for(int j=1;j<WIDTH-1;j++)
  26.         {
  27.             fire[i][j]=((fire[i][j]+fire[i+1][j+1]+fire[i+1][j]+fire[i+1][j-1])/4)-dump;
  28.             if(fire[i][j]<0)
  29.                 fire[i][j]=0;
  30.             if(fire[i][j]>=100)
  31.                 fire[i][j]=99;
  32.         }
  33. }
  34.  
  35. void renderFire(SDL_Surface* screen)
  36. {
  37.     for(int i=0;i<HEIGHT;i++)
  38.         for(int j=0;j<WIDTH;j++)
  39.         {
  40.             ((Uint32*)screen->pixels)[i*screen->pitch/4+j]=colors[(int)fire[i][j]];
  41.         }
  42. }
  43.  
  44. void interpolate(SDL_Surface* screen,int lindex,int hindex,int str,int stg,int stb,int endr,int endg,int endb)
  45. {
  46.     float stepr,stepg,stepb;
  47.     stepr=(endr-str)/(hindex-lindex);
  48.     stepg=(endg-stg)/(hindex-lindex);
  49.     stepb=(endb-stb)/(hindex-lindex);
  50.     float curr=str,curg=stg,curb=stb;
  51.     for(int i=lindex;i<hindex;i++)
  52.     {
  53.         colors[i]=SDL_MapRGB(screen->format,(int)curr,(int)curg,(int)curb);
  54.         curr+=stepr;
  55.         curg+=stepg;
  56.         curb+=stepb;
  57.     }
  58. }
  59.  
  60.  
  61.  
  62. int main(int argc,char** argv)
  63. {
  64.     SDL_Init(SDL_INIT_EVERYTHING);
  65.     SDL_Surface* screen=SDL_SetVideoMode(WIDTH,HEIGHT,32,SDL_SWSURFACE);
  66.     bool running=true;
  67.     SDL_Event event;
  68.     Uint32 start;
  69.    
  70.     colors[0]=SDL_MapRGB(screen->format,0,0,0);
  71.     interpolate(screen,0,35, 0,0,0,255,0,0);
  72.     interpolate(screen,34,65,255,0,0,255,160,0);
  73.     interpolate(screen,64,100,255,160,0,255,255,255);
  74.     for(int i=0;i<HEIGHT;i++)
  75.         for(int j=0;j<WIDTH;j++)
  76.             fire[i][j]=0;
  77.     while(running)
  78.     {
  79.         start=SDL_GetTicks();
  80.         while(SDL_PollEvent(&event))
  81.         {
  82.             switch(event.type)
  83.             {
  84.                 case SDL_QUIT:
  85.                     running=false;
  86.                     break;
  87.                 case SDL_KEYDOWN:
  88.                     switch(event.key.keysym.sym)
  89.                     {
  90.                         case SDLK_ESCAPE:
  91.                             running=false;
  92.                             break;
  93.                     }
  94.                     break;
  95.             }
  96.         }
  97.         SDL_FillRect(screen,NULL,0);
  98.         generateFire();
  99.         renderFire(screen);
  100.        
  101.         SDL_Flip(screen);
  102.         if(1000.0/FPS>(SDL_GetTicks()-start))
  103.             SDL_Delay(1000.0/FPS-(SDL_GetTicks()-start));
  104.     }
  105.     SDL_Quit();
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement