Advertisement
thecplusplusguy

SDL simple lighting example

Oct 2nd, 2012
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //SDL very simple lighting model, just click, to put down a new lamp
  3. #include <iostream>
  4. #include <vector>
  5. #include <SDL/SDL.h>
  6. #include <cmath>
  7.  
  8. const int WIDTH=640;
  9. const int HEIGHT=480;
  10. const float lightStrength=10.0;
  11. float light[HEIGHT][WIDTH];
  12.  
  13. void putPixel(SDL_Surface* dest,int x,int y,unsigned char r,unsigned char g,unsigned char b)
  14. {
  15. //  std::cout << x << " " << y << std::endl;
  16.     if(x>=0 && x<dest->w && y>=0 && y<dest->h)
  17.         ((Uint32*)dest->pixels)[y*dest->pitch/4+x]=SDL_MapRGB(dest->format,r,g,b);
  18. }
  19.  
  20. void getPixel(SDL_Surface* img,int x,int y,unsigned char& r,unsigned char& g,unsigned char& b)
  21. {
  22.     if(x>=0 && x<img->w && y>=0 && y<img->h)
  23.     {
  24.         Uint32 pix=((Uint32*)img->pixels)[y*img->pitch/4+x];
  25.         SDL_GetRGB(pix,img->format,&r,&g,&b);
  26.     }
  27. }
  28.  
  29. void blitImage(SDL_Surface* srcImage,SDL_Rect* srcRect,SDL_Surface* dstScreen,SDL_Rect* dstRect)
  30. {
  31.     int sx,sy,sw,sh;
  32.     int dx,dy;
  33.     if(srcRect==NULL)
  34.     {
  35.         sx=0;
  36.         sy=0;
  37.         sw=srcImage->w;
  38.         sh=srcImage->h;
  39.     }else{
  40.         sx=srcRect->x;
  41.         sy=srcRect->y;
  42.         sw=srcRect->w;
  43.         sh=srcRect->h;
  44.     }
  45.     if(dstRect==NULL)
  46.     {
  47.         dx=0;
  48.         dy=0;
  49.     }else{
  50.         dx=dstRect->x;
  51.         dy=dstRect->y;
  52.     }
  53.     unsigned char r,g,b;
  54.     for(int i=sy;i<sh;i++)
  55.         for(int j=sx;j<sw;j++)
  56.         {
  57.             getPixel(srcImage,j,i,r,g,b);
  58.             r*=light[i][j];
  59.             g*=light[i][j];
  60.             b*=light[i][j];
  61.             putPixel(dstScreen,dx+j,dy+i,r,g,b);
  62.         }
  63. }
  64.  
  65. int main(int argc,char** argv)
  66. {
  67.     SDL_Init(SDL_INIT_EVERYTHING);
  68.     SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
  69.     bool running=true;
  70.     SDL_Event event;
  71.     Uint32 start;
  72.     int mx=10,my=10;
  73.     SDL_Surface* image=SDL_LoadBMP("test.bmp");
  74.     SDL_Surface* optImage=SDL_DisplayFormat(image);
  75.     SDL_FreeSurface(image);
  76.     for(int i=0;i<HEIGHT;i++)
  77.         for(int j=0;j<WIDTH;j++)
  78.             light[i][j]=0.05;
  79.     std::vector<SDL_Rect> lightSources;
  80.     while(running)
  81.     {
  82.         start=SDL_GetTicks();
  83.         while(SDL_PollEvent(&event))
  84.         {
  85.             switch(event.type)
  86.             {
  87.                 case SDL_QUIT:
  88.                     running=false;
  89.                     break;
  90.                 case SDL_KEYDOWN:
  91.                     switch(event.key.keysym.sym)
  92.                     {
  93.                         case SDLK_ESCAPE:
  94.                             running=false;
  95.                             break;
  96.                     }
  97.                     break;
  98.                 case SDL_MOUSEMOTION:
  99.                     mx=event.motion.x;
  100.                     my=event.motion.y;
  101.                     break;
  102.                 case SDL_MOUSEBUTTONDOWN:
  103.                 {
  104.                     SDL_Rect rec;
  105.                     rec.x=event.button.x;
  106.                     rec.y=event.button.y;
  107.                     lightSources.push_back(rec);
  108.                     for(int i=0;i<HEIGHT;i++)
  109.                         for(int j=0;j<WIDTH;j++)
  110.                         {
  111.                             light[i][j]=0.05;   //ambient
  112.                             for(int k=0;k<lightSources.size();k++)
  113.                             {
  114.                                 //(vector length squared)*lightStrength
  115.                                 float dstSquared=(lightSources[k].x-j)*(lightSources[k].x-j) + (lightSources[k].y-i)*(lightSources[k].y-i);
  116.                                 if(dstSquared!=0.0)
  117.                                 {
  118.                                     light[i][j]+=(lightStrength/(0.01*dstSquared)); //put some nice functions here
  119.                                     //light[i][j]+=lightStrength/sqrt(dstSquared);
  120.                                 }else
  121.                                     light[i][j]=1.0;
  122.                                 if(light[i][j]>1.0)
  123.                                     light[i][j]=1.0;
  124.                             }
  125.                         }
  126.                    
  127.                 }
  128.             }
  129.         }
  130.         SDL_FillRect(screen,NULL,0);
  131.         blitImage(optImage,0,screen,0);
  132.         SDL_Flip(screen);
  133.         if(1000.0/30>(SDL_GetTicks()-start))
  134.             SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
  135.     }
  136.     SDL_FreeSurface(optImage);
  137.     SDL_Quit();
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement