Advertisement
thecplusplusguy

SDL not finished shadow

Oct 2nd, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.12 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //SDL begginning of shadows
  3. #include <iostream>
  4. #include <SDL/SDL.h>
  5. #include <cmath>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. const int WIDTH=640;
  10. const int HEIGHT=480;
  11. const int FPS=30;
  12. const int LIGHTSPEED=3;
  13.  
  14. bool putpixel(SDL_Surface* shadow,int x,int y,int r,int g,int b,int size)
  15. {
  16.     Uint32* pixels=(Uint32*)shadow->pixels;
  17.     Uint32* pixel;
  18.     if(size<=1)
  19.     {
  20.         if(x>=0 && x<WIDTH && y>=0 && y<HEIGHT)
  21.         {
  22.             pixel=pixels+y*shadow->pitch/4+x;
  23.             *pixel=SDL_MapRGB(shadow->format,r,g,b);
  24.             return true;
  25.         }else
  26.             return false;
  27.     }else{
  28.         for(int i=y-size/2;i<y+size/2;i++)
  29.             for(int j=x-size/2;j<x+size/2;j++)
  30.             {
  31.                 if(i<0 || i>=HEIGHT || j<0 || j>=WIDTH)
  32.                     continue;
  33.                 pixel=pixels+i*shadow->pitch/4+j;
  34.                 *pixel=SDL_MapRGB(shadow->format,r,g,b);
  35.             }  
  36.     }
  37.     return true;
  38.    
  39. }
  40.  
  41. void csere(int& a,int &b)
  42. {
  43.     int tmp=a;
  44.     a=b;
  45.     b=tmp;
  46. }
  47.  
  48. void drawLine(int x0,int y0,int x1,int y1,int size, int r,int g,int b)
  49. {
  50.      bool steep = abs(y1 - y0) > abs(x1 - x0);
  51.      if(steep)
  52.      {
  53.          csere(x0, y0);
  54.          csere(x1, y1);
  55.          }
  56.  
  57.      if(x0 > x1)
  58.      {
  59.          csere(x0, x1);
  60.          csere(y0, y1);
  61.          }
  62.  
  63.      int deltax = x1 - x0;
  64.      int deltay = abs(y1 - y0);
  65.      float error = 0;
  66.      float deltaerr = (float)deltay / deltax;
  67.      int ystep;
  68.      int y = y0;    
  69.      if(y0 < y1)
  70.         ystep = 1;
  71.      else
  72.       ystep = -1;
  73.      for(int x=x0;x<x1;x++)
  74.      {
  75.          if(steep)
  76.          {
  77.             putpixel(SDL_GetVideoSurface(),y,x,r,g,b,size);
  78.          }else{
  79.           putpixel(SDL_GetVideoSurface(),x,y,r,g,b,size);
  80.          }
  81.          error = error + deltaerr;
  82.          if(error>= 0.5)
  83.          {
  84.              y = y + ystep;
  85.              error = error - 1.0;
  86.          }
  87.      }
  88. }
  89.  
  90. void drawLinef(SDL_Surface* shadow,int x0,int y0,float dirx,float diry,int size, int r,int g,int b)
  91. {
  92.     if(dirx==0 && diry==0)
  93.         return;
  94.     float curx=x0;
  95.     float cury=y0;
  96. //  float len=sqrt(dirx*dirx+diry*diry);
  97. //  dirx/=len;
  98. //  diry/=len;
  99. //  cout << dirx << " " << diry << endl;
  100. //  drawLine(x0,y0,x0+(int)(dirx*10),y0+(int)(diry*10),1,0,0,255);
  101.     while(curx<WIDTH && curx>=0 && cury<HEIGHT && cury>=0)
  102.     {
  103.         curx+=dirx;
  104.         cury+=diry;
  105.         putpixel(shadow,(int)curx,(int)cury,r,g,b,size);
  106.     }
  107. }
  108.  
  109.  
  110. void drawRect(const SDL_Rect& rec,int r,int g,int b)
  111. {
  112.     drawLine(rec.x,rec.y,rec.x+rec.w,rec.y,1,r,g,b);
  113.     drawLine(rec.x,rec.y,rec.x,rec.y+rec.h,1,r,g,b);
  114.     drawLine(rec.x+rec.w,rec.y,rec.x+rec.w,rec.y+rec.h,1,r,g,b);
  115.     drawLine(rec.x,rec.y+rec.h,rec.x+rec.w,rec.y+rec.h,1,r,g,b);
  116. }
  117.  
  118. void normalize(float& a,float& b)
  119. {
  120.     if(a==0 && b==0)
  121.         return;
  122.     float len=sqrt(a*a+b*b);
  123.     a/=len;
  124.     b/=len;
  125. }
  126.  
  127. void drawShadows(SDL_Surface* shadow,const vector<SDL_Rect>& rects,const vector<SDL_Rect>& lights)
  128. {
  129.     for(int i=0;i<lights.size();i++)
  130.     {
  131.         int centerx,centery;
  132.         centerx=lights[i].x+lights[i].w/2;
  133.         centery=lights[i].y+lights[i].h/2;
  134.         float directionx[4];
  135.         float directiony[4];
  136.         float startx[4];
  137.         float starty[4];
  138.         for(int j=0;j<rects.size();j++)
  139.         {
  140.             directionx[0]=rects[j].x-centerx;
  141.             directiony[0]=rects[j].y-centery;
  142.             normalize(directionx[0],directiony[0]);
  143.             startx[0]=rects[j].x;
  144.             starty[0]=rects[j].y;
  145.             drawLinef(shadow,rects[j].x,rects[j].y,directionx[0],directiony[0],1,255,0,0);
  146.            
  147.             directionx[1]=rects[j].x-centerx;
  148.             directiony[1]=(rects[j].y+rects[j].h)-centery;
  149.             normalize(directionx[1],directiony[1]);
  150.             startx[1]=rects[j].x;
  151.             starty[1]=(rects[j].y+rects[j].h);         
  152.             drawLinef(shadow,rects[j].x,(rects[j].y+rects[j].h),directionx[1],directiony[1],1,255,0,0);
  153.            
  154.            
  155.             directionx[2]=(rects[j].x+rects[i].w)-centerx;
  156.             directiony[2]=rects[j].y-centery;
  157.             normalize(directionx[2],directiony[2]);
  158.             startx[2]=(rects[j].x+rects[i].w);
  159.             starty[2]=rects[j].y;
  160.             drawLinef(shadow,(rects[j].x+rects[i].w),rects[j].y,directionx[2],directiony[2],1,255,0,0);
  161.            
  162.            
  163.             directionx[3]=(rects[j].x+rects[j].w)-centerx;
  164.             directiony[3]=(rects[j].y+rects[j].h)-centery;
  165.             normalize(directionx[3],directiony[3]);
  166.             startx[3]=(rects[j].x+rects[j].w);
  167.             starty[3]=(rects[j].y+rects[j].h);
  168.             drawLinef(shadow,(rects[j].x+rects[j].w),(rects[j].y+rects[j].h),directionx[3],directiony[3],1,255,0,0);
  169.            
  170.             for(int k=0;k<1;k++)
  171.             {
  172.                 signed char dir2=(directiony[k]>0 ? 1 : -1);
  173.                
  174.                 bool b=abs(starty[k]-starty[k+1])>=1.0;
  175.                 if(b)
  176.                 {
  177.                     float tmp=directiony[k];
  178.                     directiony[k]=-directionx[k];
  179.                     directionx[k]=tmp;
  180.                    
  181.                     tmp=directiony[k+1];
  182.                     directiony[k+1]=-directionx[k+1];
  183.                     directionx[k+1]=tmp;
  184.                    
  185.                     startx[k]+=20;
  186.                     starty[k]-=20;
  187.                    
  188.                     startx[k]+=20;
  189.                     starty[k]-=20;
  190.                    
  191.                    
  192.                 }
  193.                
  194. /*              for(float h=0;(dir2==1 && h+starty[k]<HEIGHT) || (dir2==-1 && h+starty[k]>=0);h+=dir2)
  195.                 {
  196.                     int rayend=startx[k]+h*directionx[k]*(1.0/directiony[k]);
  197.                     int raystart=startx[k+1]+h*directionx[k+1]*(1.0/directiony[k+1]);
  198.                     signed char dir;
  199.                     dir=(raystart>rayend ? -1 : 1);
  200.                     putpixel(shadow,raystart,h+starty[k+1],1,255,0,0);
  201.                     putpixel(shadow,raystart,h+starty[k+1],1,255,0,0);
  202.                     if(raystart>=WIDTH)
  203.                         raystart=WIDTH-1;
  204.                     if(raystart<0)
  205.                         raystart=0;
  206.                     if(rayend>=WIDTH)
  207.                         rayend=WIDTH-1;
  208.                     if(rayend<0)
  209.                         rayend=0;
  210.     */          /*  while(raystart!=rayend)
  211.                     {
  212.                         if(!b)
  213.                             putpixel(shadow,raystart,h+starty[k],1,255,0,0);
  214.                         else
  215.                             putpixel(shadow,h+starty[k],raystart,1,255,0,0);
  216.                         raystart+=dir;
  217.                     }*/
  218.             //  }
  219.                
  220.                
  221.                
  222.             }
  223.            
  224.         //0 - bal felső
  225.         //1 - bal alsó
  226.         //2 - jobb felső
  227.         //3 - jobb alsó
  228.     /*  for(int i=0;i<4;i++)
  229.             cout << directionx[i] << " " << directiony[i] << endl;
  230.         cout << endl;
  231.        
  232.         int min=0;
  233.         int max=0;
  234.         for(int k=1;k<4;k++)
  235.         {
  236.             if(directionx[k]>directionx[max] || directiony[k]>directiony[max])
  237.                 max=k;
  238.             if(directionx[k]<directionx[min] || directiony[k]<directiony[min])
  239.                 min=k;
  240.         }
  241.         drawLinef(shadow,startx[min],starty[min],directionx[min],directiony[min],1,255,0,0);
  242.         drawLinef(shadow,startx[max],starty[max],directionx[max],directiony[max],1,255,0,0);
  243.         */     
  244.         }
  245.     }
  246. }
  247.  
  248. void fill(SDL_Surface* shadow)
  249. {
  250.     bool volt=false,elozo=false;
  251.     for(int i=0;i<shadow->h;i++)
  252.         for(int j=0;j<shadow->w;j++)
  253.         {
  254.             if(((Uint32*)shadow->pixels)[i*shadow->pitch/4+j])
  255.             {
  256.                 volt=!volt;
  257.                 if(volt &&elozo)
  258.                     volt=false;
  259.                 elozo=true;
  260.             }else
  261.                 elozo=false;
  262.             if(volt)
  263.                 ((Uint32*)shadow->pixels)[i*shadow->pitch/4+j]=SDL_MapRGB(shadow->format,255,0,0);
  264.         }
  265. }
  266.  
  267.  
  268. int main(int argc,char** argv)
  269. {
  270.     SDL_Init(SDL_INIT_EVERYTHING);
  271.     SDL_Surface* screen=SDL_SetVideoMode(WIDTH,HEIGHT,32,SDL_SWSURFACE);
  272.     Uint32 rmask, gmask, bmask, amask;
  273.     if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  274.     {
  275.     rmask = 0xff000000;
  276.     gmask = 0x00ff0000;
  277.     bmask = 0x0000ff00;
  278.     amask = 0x000000ff;
  279.     }else{
  280.     rmask = 0x000000ff;
  281.     gmask = 0x0000ff00;
  282.     bmask = 0x00ff0000;
  283.     amask = 0xff000000;
  284.     }
  285.  
  286.     SDL_Surface* shadow = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT, 32,
  287.                                    rmask, gmask, bmask, amask);
  288.     SDL_Event event;
  289.     Uint32 start;
  290.     bool running=true;
  291.     bool arrows[4]={0,0,0,0};
  292.     vector<SDL_Rect> recs;
  293.     for(int i=0;i<2;i++)
  294.         for(int j=0;j<4;j++)
  295.         {
  296.             SDL_Rect rec;
  297.             rec.x=30+j*150;
  298.             rec.y=50+i*300;
  299.             rec.w=20;
  300.             rec.h=20;
  301.             recs.push_back(rec);
  302.         }
  303.     vector<SDL_Rect> lightpos;
  304.     SDL_Rect lp={50,200,5,5};
  305.     lightpos.push_back(lp);
  306.     int curlight=0;
  307.     while(running)
  308.     {
  309.         start=SDL_GetTicks();
  310.         while(SDL_PollEvent(&event))
  311.         {
  312.             switch(event.type)
  313.             {
  314.                 case SDL_QUIT:
  315.                     running=false;
  316.                     break;
  317.                 case SDL_KEYDOWN:
  318.                     switch(event.key.keysym.sym)
  319.                     {
  320.                         case SDLK_ESCAPE:
  321.                             running=false;
  322.                             break;
  323.                         case SDLK_UP:
  324.                             arrows[0]=true;
  325.                             break;
  326.                         case SDLK_RIGHT:
  327.                             arrows[1]=true;
  328.                             break;
  329.                         case SDLK_DOWN:
  330.                             arrows[2]=true;
  331.                             break;
  332.                         case SDLK_LEFT:
  333.                             arrows[3]=true;
  334.                             break;
  335.                         case SDLK_SPACE:
  336.                             curlight++;
  337.                             if(curlight>=lightpos.size())
  338.                                 curlight=0;
  339.                             break;
  340.                     }
  341.                     break;
  342.                 case SDL_KEYUP:
  343.                     switch(event.key.keysym.sym)
  344.                     {
  345.                         case SDLK_UP:
  346.                             arrows[0]=false;
  347.                             break;
  348.                         case SDLK_RIGHT:
  349.                             arrows[1]=false;
  350.                             break;
  351.                         case SDLK_DOWN:
  352.                             arrows[2]=false;
  353.                             break;
  354.                         case SDLK_LEFT:
  355.                             arrows[3]=false;
  356.                             break;
  357.                     }
  358.                     break; 
  359.             }
  360.         }
  361.        
  362.         SDL_FillRect(screen,NULL,0);
  363.         SDL_FillRect(shadow,NULL,0);
  364.        
  365.         if(arrows[0])
  366.             lightpos[curlight].y-=LIGHTSPEED;
  367.         if(arrows[1])
  368.             lightpos[curlight].x+=LIGHTSPEED;
  369.         if(arrows[2])
  370.             lightpos[curlight].y+=LIGHTSPEED;
  371.         if(arrows[3])
  372.             lightpos[curlight].x-=LIGHTSPEED;
  373.        
  374.         for(int i=0;i<recs.size();i++)
  375.             drawRect(recs[i],128,128,128);
  376.        
  377.         for(int i=0;i<lightpos.size();i++)
  378.             drawRect(lightpos[i],255,255,255);
  379.        
  380.        
  381.         drawShadows(shadow,recs,lightpos);
  382.     //  fill(shadow);
  383.         SDL_BlitSurface(shadow,NULL,screen,NULL);
  384.         SDL_Flip(screen);
  385.         if(1000.0/FPS>(SDL_GetTicks()-start))
  386.             SDL_Delay(1000.0/FPS-(SDL_GetTicks()-start));
  387.     }
  388.     SDL_Quit();
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement