Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- //SDL very simple lighting model, just click, to put down a new lamp
- #include <iostream>
- #include <vector>
- #include <SDL/SDL.h>
- #include <cmath>
- const int WIDTH=640;
- const int HEIGHT=480;
- const float lightStrength=10.0;
- float light[HEIGHT][WIDTH];
- void putPixel(SDL_Surface* dest,int x,int y,unsigned char r,unsigned char g,unsigned char b)
- {
- // std::cout << x << " " << y << std::endl;
- if(x>=0 && x<dest->w && y>=0 && y<dest->h)
- ((Uint32*)dest->pixels)[y*dest->pitch/4+x]=SDL_MapRGB(dest->format,r,g,b);
- }
- void getPixel(SDL_Surface* img,int x,int y,unsigned char& r,unsigned char& g,unsigned char& b)
- {
- if(x>=0 && x<img->w && y>=0 && y<img->h)
- {
- Uint32 pix=((Uint32*)img->pixels)[y*img->pitch/4+x];
- SDL_GetRGB(pix,img->format,&r,&g,&b);
- }
- }
- void blitImage(SDL_Surface* srcImage,SDL_Rect* srcRect,SDL_Surface* dstScreen,SDL_Rect* dstRect)
- {
- int sx,sy,sw,sh;
- int dx,dy;
- if(srcRect==NULL)
- {
- sx=0;
- sy=0;
- sw=srcImage->w;
- sh=srcImage->h;
- }else{
- sx=srcRect->x;
- sy=srcRect->y;
- sw=srcRect->w;
- sh=srcRect->h;
- }
- if(dstRect==NULL)
- {
- dx=0;
- dy=0;
- }else{
- dx=dstRect->x;
- dy=dstRect->y;
- }
- unsigned char r,g,b;
- for(int i=sy;i<sh;i++)
- for(int j=sx;j<sw;j++)
- {
- getPixel(srcImage,j,i,r,g,b);
- r*=light[i][j];
- g*=light[i][j];
- b*=light[i][j];
- putPixel(dstScreen,dx+j,dy+i,r,g,b);
- }
- }
- int main(int argc,char** argv)
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
- bool running=true;
- SDL_Event event;
- Uint32 start;
- int mx=10,my=10;
- SDL_Surface* image=SDL_LoadBMP("test.bmp");
- SDL_Surface* optImage=SDL_DisplayFormat(image);
- SDL_FreeSurface(image);
- for(int i=0;i<HEIGHT;i++)
- for(int j=0;j<WIDTH;j++)
- light[i][j]=0.05;
- std::vector<SDL_Rect> lightSources;
- 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;
- }
- break;
- case SDL_MOUSEMOTION:
- mx=event.motion.x;
- my=event.motion.y;
- break;
- case SDL_MOUSEBUTTONDOWN:
- {
- SDL_Rect rec;
- rec.x=event.button.x;
- rec.y=event.button.y;
- lightSources.push_back(rec);
- for(int i=0;i<HEIGHT;i++)
- for(int j=0;j<WIDTH;j++)
- {
- light[i][j]=0.05; //ambient
- for(int k=0;k<lightSources.size();k++)
- {
- //(vector length squared)*lightStrength
- float dstSquared=(lightSources[k].x-j)*(lightSources[k].x-j) + (lightSources[k].y-i)*(lightSources[k].y-i);
- if(dstSquared!=0.0)
- {
- light[i][j]+=(lightStrength/(0.01*dstSquared)); //put some nice functions here
- //light[i][j]+=lightStrength/sqrt(dstSquared);
- }else
- light[i][j]=1.0;
- if(light[i][j]>1.0)
- light[i][j]=1.0;
- }
- }
- }
- }
- }
- SDL_FillRect(screen,NULL,0);
- blitImage(optImage,0,screen,0);
- SDL_Flip(screen);
- if(1000.0/30>(SDL_GetTicks()-start))
- SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
- }
- SDL_FreeSurface(optImage);
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement