Advertisement
thecplusplusguy

Colored treedrawer in C++ and SDL

Jul 6th, 2012
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.84 KB | None | 0 0
  1. //Implemented by http://www.youtube.com/user/thecplusplusguy
  2. //SDL library used
  3. //draw a tree
  4. #include <iostream>
  5. #include <SDL/SDL.h>
  6. #include <cmath>
  7. #include <fstream>
  8. #include <cstdlib>
  9. #include <ctime>
  10. using namespace std;
  11.  
  12. const int BPP=32;
  13. const int NUM=6;
  14. const int ANGLE=80;
  15. const int WIDTH=1024;
  16. const int HEIGHT=768;
  17.  
  18.  
  19.  
  20. struct bmpfile_magic {
  21.   unsigned char magic[2];
  22. };
  23.  
  24. struct bmpfile_header {
  25.   int filesz;
  26.   short creator1;
  27.   short creator2;
  28.   int bmp_offset;
  29. };
  30.  
  31. typedef struct {
  32.   int header_sz;
  33.   int width;
  34.   int height;
  35.   short nplanes;
  36.   short bitspp;
  37.   int compress_type;
  38.   int bmp_bytesz;
  39.   int hres;
  40.   int vres;
  41.   int ncolors;
  42.   int nimpcolors;
  43. } BITMAPINFOHEADER;
  44.  
  45.  
  46.  
  47. void saveBMP(SDL_Surface* surf)
  48. {
  49.     char filename[30];
  50.     sprintf(filename,"%d.bmp",(int)time(0));
  51.     std::ofstream out(filename,std::ios::binary);
  52.     bmpfile_magic bm={'B','M'};
  53.     bmpfile_header bh={54+((BPP*surf->w)/8)*surf->h,0,0,54};
  54.     BITMAPINFOHEADER bhi={40,surf->w,surf->h,1,BPP,0,((BPP*surf->w)/8)*surf->h,2750,2750,0,0};
  55.     out.write((char*)&bm,sizeof(bm));
  56.     out.write((char*)&bh,sizeof(bh));
  57.     out.write((char*)&bhi,sizeof(bhi));
  58.     for(int i=0;i<surf->h;i++)
  59.         for(int j=0;j<surf->w;j++)
  60.         {
  61.             Uint32 rgb=(Uint32)((Uint32*)surf->pixels)[(surf->h-i-1)*(surf->pitch/4) + j];
  62.             Uint8 r,g,b;
  63.             SDL_GetRGB(rgb,surf->format,&r,&g,&b);
  64.                 out.put((char)b);//b
  65.                 out.put((char)g);//g
  66.                 out.put((char)r);//r
  67.                 out.put((char)255);
  68.         }
  69. }
  70.  
  71.  
  72.  
  73.  
  74. void putpixel(int x,int y,int r,int g,int b,int size)
  75. {
  76.     Uint32* pixels=(Uint32*)SDL_GetVideoSurface()->pixels;
  77.     Uint32* pixel;
  78.     if(size<=1)
  79.     {
  80.         pixel=pixels+y*SDL_GetVideoSurface()->pitch/4+x;
  81.         *pixel=SDL_MapRGB(SDL_GetVideoSurface()->format,r,g,b);
  82.     }else{
  83.         for(int i=y-size/2;i<y+size/2;i++)
  84.             for(int j=x-size/2;j<x+size/2;j++)
  85.             {
  86.                 if(i<0 || i>=HEIGHT || j<0 || j>=WIDTH)
  87.                     continue;
  88.                 pixel=pixels+i*SDL_GetVideoSurface()->pitch/4+j;
  89.                 *pixel=SDL_MapRGB(SDL_GetVideoSurface()->format,r,g,b);
  90.             }  
  91.     }
  92.    
  93. }
  94.  
  95. void putpixel(int x,int y,Uint32 color,int size)
  96. {
  97.     Uint32* pixels=(Uint32*)SDL_GetVideoSurface()->pixels;
  98.     Uint32* pixel;
  99.     if(size<=1)
  100.     {
  101.         pixel=pixels+y*SDL_GetVideoSurface()->pitch/4+x;
  102.         *pixel=color;
  103.     }else{
  104.         for(int i=y-size/2;i<ceil(y+size/2);i++)
  105.             for(int j=x-size/2;j<ceil(x+size/2);j++)
  106.             {
  107.                 if(i<0 || i>=HEIGHT || j<0 || j>=WIDTH)
  108.                     continue;
  109.                 pixel=pixels+i*SDL_GetVideoSurface()->pitch/4+j;
  110.                 *pixel=color;  
  111.             }  
  112.     }
  113. }
  114.  
  115. void csere(int& a,int &b)
  116. {
  117.     int tmp=a;
  118.     a=b;
  119.     b=tmp;
  120. }
  121.  
  122. void line2(int x0,int y0,int x1,int y1,Uint32 color,int size)
  123. {
  124.      bool steep = abs(y1 - y0) > abs(x1 - x0);
  125.      if(steep)
  126.      {
  127.          csere(x0, y0);
  128.          csere(x1, y1);
  129.          }
  130.  
  131.      if(x0 > x1)
  132.      {
  133.          csere(x0, x1);
  134.          csere(y0, y1);
  135.          }
  136.  
  137.      int deltax = x1 - x0;
  138.      int deltay = abs(y1 - y0);
  139.      float error = 0;
  140.      float deltaerr = (float)deltay / deltax;
  141.      int ystep;
  142.      int y = y0;
  143.      if(y0 < y1)
  144.         ystep = 1;
  145.      else
  146.       ystep = -1;
  147.      for(int x=x0;x<x1;x++)
  148.      {
  149.          if(steep)
  150.             putpixel(y,x,color,size);
  151.          else
  152.           putpixel(x,y,color,size);
  153.          error = error + deltaerr;
  154.          if(error>= 0.5)
  155.          {
  156.              y = y + ystep;
  157.              error = error - 1.0;
  158.          }
  159.      }
  160. }
  161.  
  162. void drawTree(int startx,int starty,float len,int vast,float dir)
  163. {
  164.     if(vast>=1)
  165.     {
  166.         if(len<1)
  167.             len=1;
  168.         float endx=startx+sin((float)dir*M_PI/180.0)*len;
  169.         float endy=starty+cos((float)dir*M_PI/180.0)*len;
  170.         float vecx=abs(endx-startx);
  171.         float vecy=abs(endy-starty);
  172.         float dist=sqrt(vecx*vecx+vecy*vecy);
  173.         vecx/=dist;
  174.         vecy/=dist;
  175.         Uint32 color;
  176.         if(vast>=4)
  177.             color=SDL_MapRGB(SDL_GetVideoSurface()->format,0x7d+rand()%20-10,0x50+rand()%20-10,0x00);
  178.         else if(vast==3)
  179.             color=SDL_MapRGB(SDL_GetVideoSurface()->format,0x4c+rand()%20-10,0x57+rand()%20-10,0x00);
  180.         else if(vast==2)
  181.             color=SDL_MapRGB(SDL_GetVideoSurface()->format,rand()%20,0x5f+rand()%50-25,rand()%20);
  182.         else if(vast==1)
  183.             color=SDL_MapRGB(SDL_GetVideoSurface()->format,rand()%50,0xff-rand()%120,rand()%50);
  184.         else
  185.             color=SDL_MapRGB(SDL_GetVideoSurface()->format,0x00,0xff,0x00);
  186.         for(int i=-vast/2;i<=vast/1;i++)
  187.         {
  188.             if(endx+i<0)
  189.                 endx=-i;
  190.             if(endx+i>=WIDTH)
  191.                 endx=WIDTH-i;
  192.             if(endy<0)
  193.                 endy=0;
  194.             if(endy>=HEIGHT)
  195.                 endy=HEIGHT-1;
  196.             if(startx+i<0)
  197.                 startx=-i;
  198.             if(startx+i>WIDTH)
  199.                 startx=WIDTH-i;
  200.             if(starty<0)
  201.                 starty=0;
  202.             if(starty>=HEIGHT)
  203.                 starty=HEIGHT;
  204.             line2(i+startx,starty,endx+i,endy,color,vast);
  205.         }
  206.         for(int j=-ANGLE;j<=ANGLE;j+=(ANGLE*2)/(NUM-1))
  207.         {
  208.             if(rand()%30<27)
  209.                 drawTree(endx,endy,len/(len>100 ? rand()/RAND_MAX+3.0 : rand()/RAND_MAX+1.5),vast-1,dir+j+rand()%40-20);
  210.         }
  211.     }
  212. }
  213.  
  214. void pauseSDL()
  215. {
  216.     SDL_Event event;
  217.     bool running=true;
  218.     while(1)
  219.     {
  220.         SDL_WaitEvent(&event);
  221.         if(event.type==SDL_KEYDOWN || event.type==SDL_QUIT)
  222.             break;
  223.     }  
  224. }
  225.  
  226.  
  227. int main()
  228. {
  229.     srand(time(0));
  230.     SDL_Init(SDL_INIT_EVERYTHING);
  231.     SDL_Surface* screen=SDL_SetVideoMode(WIDTH,HEIGHT,BPP,SDL_SWSURFACE);
  232.     bool running=true;
  233.     SDL_Event event;
  234.     Uint32 start;
  235.     int i=0;
  236.     int j=0;
  237.     bool dir=true;
  238.     while(running)
  239.     {
  240.         start=SDL_GetTicks();
  241.         while(SDL_PollEvent(&event))
  242.         {
  243.             switch(event.type)
  244.             {
  245.                 case SDL_QUIT:
  246.                     running=false;
  247.                     break;
  248.             }
  249.         }
  250.         SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0,0,0));
  251.         drawTree(WIDTH/2,HEIGHT,HEIGHT/2,6,180);
  252.     //  cout << "-----------------------------------" << endl;
  253.         //for(int i=1;i<15;i++)
  254.         //  line2(10,(i+1)*30,300,(i+20)*30,SDL_MapRGB(screen->format,255,255,255),i);
  255.         SDL_Flip(screen);
  256.        
  257.         saveBMP(screen);
  258.         pauseSDL();
  259.        
  260.     //  SDL_Delay(2000);
  261.         break;
  262.         if(1000.0/30>(SDL_GetTicks()-start))
  263.             SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
  264.     }
  265.    
  266.     SDL_Quit();
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement