Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- //SDL begginning of shadows
- #include <iostream>
- #include <SDL/SDL.h>
- #include <cmath>
- #include <vector>
- using namespace std;
- const int WIDTH=640;
- const int HEIGHT=480;
- const int FPS=30;
- const int LIGHTSPEED=3;
- bool putpixel(SDL_Surface* shadow,int x,int y,int r,int g,int b,int size)
- {
- Uint32* pixels=(Uint32*)shadow->pixels;
- Uint32* pixel;
- if(size<=1)
- {
- if(x>=0 && x<WIDTH && y>=0 && y<HEIGHT)
- {
- pixel=pixels+y*shadow->pitch/4+x;
- *pixel=SDL_MapRGB(shadow->format,r,g,b);
- return true;
- }else
- return false;
- }else{
- for(int i=y-size/2;i<y+size/2;i++)
- for(int j=x-size/2;j<x+size/2;j++)
- {
- if(i<0 || i>=HEIGHT || j<0 || j>=WIDTH)
- continue;
- pixel=pixels+i*shadow->pitch/4+j;
- *pixel=SDL_MapRGB(shadow->format,r,g,b);
- }
- }
- return true;
- }
- void csere(int& a,int &b)
- {
- int tmp=a;
- a=b;
- b=tmp;
- }
- void drawLine(int x0,int y0,int x1,int y1,int size, int r,int g,int b)
- {
- bool steep = abs(y1 - y0) > abs(x1 - x0);
- if(steep)
- {
- csere(x0, y0);
- csere(x1, y1);
- }
- if(x0 > x1)
- {
- csere(x0, x1);
- csere(y0, y1);
- }
- int deltax = x1 - x0;
- int deltay = abs(y1 - y0);
- float error = 0;
- float deltaerr = (float)deltay / deltax;
- int ystep;
- int y = y0;
- if(y0 < y1)
- ystep = 1;
- else
- ystep = -1;
- for(int x=x0;x<x1;x++)
- {
- if(steep)
- {
- putpixel(SDL_GetVideoSurface(),y,x,r,g,b,size);
- }else{
- putpixel(SDL_GetVideoSurface(),x,y,r,g,b,size);
- }
- error = error + deltaerr;
- if(error>= 0.5)
- {
- y = y + ystep;
- error = error - 1.0;
- }
- }
- }
- void drawLinef(SDL_Surface* shadow,int x0,int y0,float dirx,float diry,int size, int r,int g,int b)
- {
- if(dirx==0 && diry==0)
- return;
- float curx=x0;
- float cury=y0;
- // float len=sqrt(dirx*dirx+diry*diry);
- // dirx/=len;
- // diry/=len;
- // cout << dirx << " " << diry << endl;
- // drawLine(x0,y0,x0+(int)(dirx*10),y0+(int)(diry*10),1,0,0,255);
- while(curx<WIDTH && curx>=0 && cury<HEIGHT && cury>=0)
- {
- curx+=dirx;
- cury+=diry;
- putpixel(shadow,(int)curx,(int)cury,r,g,b,size);
- }
- }
- void drawRect(const SDL_Rect& rec,int r,int g,int b)
- {
- drawLine(rec.x,rec.y,rec.x+rec.w,rec.y,1,r,g,b);
- drawLine(rec.x,rec.y,rec.x,rec.y+rec.h,1,r,g,b);
- drawLine(rec.x+rec.w,rec.y,rec.x+rec.w,rec.y+rec.h,1,r,g,b);
- drawLine(rec.x,rec.y+rec.h,rec.x+rec.w,rec.y+rec.h,1,r,g,b);
- }
- void normalize(float& a,float& b)
- {
- if(a==0 && b==0)
- return;
- float len=sqrt(a*a+b*b);
- a/=len;
- b/=len;
- }
- void drawShadows(SDL_Surface* shadow,const vector<SDL_Rect>& rects,const vector<SDL_Rect>& lights)
- {
- for(int i=0;i<lights.size();i++)
- {
- int centerx,centery;
- centerx=lights[i].x+lights[i].w/2;
- centery=lights[i].y+lights[i].h/2;
- float directionx[4];
- float directiony[4];
- float startx[4];
- float starty[4];
- for(int j=0;j<rects.size();j++)
- {
- directionx[0]=rects[j].x-centerx;
- directiony[0]=rects[j].y-centery;
- normalize(directionx[0],directiony[0]);
- startx[0]=rects[j].x;
- starty[0]=rects[j].y;
- drawLinef(shadow,rects[j].x,rects[j].y,directionx[0],directiony[0],1,255,0,0);
- directionx[1]=rects[j].x-centerx;
- directiony[1]=(rects[j].y+rects[j].h)-centery;
- normalize(directionx[1],directiony[1]);
- startx[1]=rects[j].x;
- starty[1]=(rects[j].y+rects[j].h);
- drawLinef(shadow,rects[j].x,(rects[j].y+rects[j].h),directionx[1],directiony[1],1,255,0,0);
- directionx[2]=(rects[j].x+rects[i].w)-centerx;
- directiony[2]=rects[j].y-centery;
- normalize(directionx[2],directiony[2]);
- startx[2]=(rects[j].x+rects[i].w);
- starty[2]=rects[j].y;
- drawLinef(shadow,(rects[j].x+rects[i].w),rects[j].y,directionx[2],directiony[2],1,255,0,0);
- directionx[3]=(rects[j].x+rects[j].w)-centerx;
- directiony[3]=(rects[j].y+rects[j].h)-centery;
- normalize(directionx[3],directiony[3]);
- startx[3]=(rects[j].x+rects[j].w);
- starty[3]=(rects[j].y+rects[j].h);
- drawLinef(shadow,(rects[j].x+rects[j].w),(rects[j].y+rects[j].h),directionx[3],directiony[3],1,255,0,0);
- for(int k=0;k<1;k++)
- {
- signed char dir2=(directiony[k]>0 ? 1 : -1);
- bool b=abs(starty[k]-starty[k+1])>=1.0;
- if(b)
- {
- float tmp=directiony[k];
- directiony[k]=-directionx[k];
- directionx[k]=tmp;
- tmp=directiony[k+1];
- directiony[k+1]=-directionx[k+1];
- directionx[k+1]=tmp;
- startx[k]+=20;
- starty[k]-=20;
- startx[k]+=20;
- starty[k]-=20;
- }
- /* for(float h=0;(dir2==1 && h+starty[k]<HEIGHT) || (dir2==-1 && h+starty[k]>=0);h+=dir2)
- {
- int rayend=startx[k]+h*directionx[k]*(1.0/directiony[k]);
- int raystart=startx[k+1]+h*directionx[k+1]*(1.0/directiony[k+1]);
- signed char dir;
- dir=(raystart>rayend ? -1 : 1);
- putpixel(shadow,raystart,h+starty[k+1],1,255,0,0);
- putpixel(shadow,raystart,h+starty[k+1],1,255,0,0);
- if(raystart>=WIDTH)
- raystart=WIDTH-1;
- if(raystart<0)
- raystart=0;
- if(rayend>=WIDTH)
- rayend=WIDTH-1;
- if(rayend<0)
- rayend=0;
- */ /* while(raystart!=rayend)
- {
- if(!b)
- putpixel(shadow,raystart,h+starty[k],1,255,0,0);
- else
- putpixel(shadow,h+starty[k],raystart,1,255,0,0);
- raystart+=dir;
- }*/
- // }
- }
- //0 - bal felső
- //1 - bal alsó
- //2 - jobb felső
- //3 - jobb alsó
- /* for(int i=0;i<4;i++)
- cout << directionx[i] << " " << directiony[i] << endl;
- cout << endl;
- int min=0;
- int max=0;
- for(int k=1;k<4;k++)
- {
- if(directionx[k]>directionx[max] || directiony[k]>directiony[max])
- max=k;
- if(directionx[k]<directionx[min] || directiony[k]<directiony[min])
- min=k;
- }
- drawLinef(shadow,startx[min],starty[min],directionx[min],directiony[min],1,255,0,0);
- drawLinef(shadow,startx[max],starty[max],directionx[max],directiony[max],1,255,0,0);
- */
- }
- }
- }
- void fill(SDL_Surface* shadow)
- {
- bool volt=false,elozo=false;
- for(int i=0;i<shadow->h;i++)
- for(int j=0;j<shadow->w;j++)
- {
- if(((Uint32*)shadow->pixels)[i*shadow->pitch/4+j])
- {
- volt=!volt;
- if(volt &&elozo)
- volt=false;
- elozo=true;
- }else
- elozo=false;
- if(volt)
- ((Uint32*)shadow->pixels)[i*shadow->pitch/4+j]=SDL_MapRGB(shadow->format,255,0,0);
- }
- }
- int main(int argc,char** argv)
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_Surface* screen=SDL_SetVideoMode(WIDTH,HEIGHT,32,SDL_SWSURFACE);
- Uint32 rmask, gmask, bmask, amask;
- if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
- {
- rmask = 0xff000000;
- gmask = 0x00ff0000;
- bmask = 0x0000ff00;
- amask = 0x000000ff;
- }else{
- rmask = 0x000000ff;
- gmask = 0x0000ff00;
- bmask = 0x00ff0000;
- amask = 0xff000000;
- }
- SDL_Surface* shadow = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT, 32,
- rmask, gmask, bmask, amask);
- SDL_Event event;
- Uint32 start;
- bool running=true;
- bool arrows[4]={0,0,0,0};
- vector<SDL_Rect> recs;
- for(int i=0;i<2;i++)
- for(int j=0;j<4;j++)
- {
- SDL_Rect rec;
- rec.x=30+j*150;
- rec.y=50+i*300;
- rec.w=20;
- rec.h=20;
- recs.push_back(rec);
- }
- vector<SDL_Rect> lightpos;
- SDL_Rect lp={50,200,5,5};
- lightpos.push_back(lp);
- int curlight=0;
- while(running)
- {
- start=SDL_GetTicks();
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_QUIT:
- running=false;
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- running=false;
- break;
- case SDLK_UP:
- arrows[0]=true;
- break;
- case SDLK_RIGHT:
- arrows[1]=true;
- break;
- case SDLK_DOWN:
- arrows[2]=true;
- break;
- case SDLK_LEFT:
- arrows[3]=true;
- break;
- case SDLK_SPACE:
- curlight++;
- if(curlight>=lightpos.size())
- curlight=0;
- break;
- }
- break;
- case SDL_KEYUP:
- switch(event.key.keysym.sym)
- {
- case SDLK_UP:
- arrows[0]=false;
- break;
- case SDLK_RIGHT:
- arrows[1]=false;
- break;
- case SDLK_DOWN:
- arrows[2]=false;
- break;
- case SDLK_LEFT:
- arrows[3]=false;
- break;
- }
- break;
- }
- }
- SDL_FillRect(screen,NULL,0);
- SDL_FillRect(shadow,NULL,0);
- if(arrows[0])
- lightpos[curlight].y-=LIGHTSPEED;
- if(arrows[1])
- lightpos[curlight].x+=LIGHTSPEED;
- if(arrows[2])
- lightpos[curlight].y+=LIGHTSPEED;
- if(arrows[3])
- lightpos[curlight].x-=LIGHTSPEED;
- for(int i=0;i<recs.size();i++)
- drawRect(recs[i],128,128,128);
- for(int i=0;i<lightpos.size();i++)
- drawRect(lightpos[i],255,255,255);
- drawShadows(shadow,recs,lightpos);
- // fill(shadow);
- SDL_BlitSurface(shadow,NULL,screen,NULL);
- SDL_Flip(screen);
- if(1000.0/FPS>(SDL_GetTicks()-start))
- SDL_Delay(1000.0/FPS-(SDL_GetTicks()-start));
- }
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement