Advertisement
thecplusplusguy

treedrawer in C++ and SDL

Jul 6th, 2012
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.25 KB | None | 0 0
  1. //Implemented by http://www.youtube.com/user/thecplusplusguy
  2. //SDL library used
  3. //draw a tree, at treedrawer function there are 2 commented out lines, uncomment those, to see the process
  4. #include <iostream>
  5. #include <SDL/SDL.h>
  6. #include <cmath>
  7. #include <fstream>
  8. using namespace std;
  9.  
  10. const int BPP=32;
  11. const int NUM=6;
  12. const int ANGLE=70;
  13. const int WIDTH=1024;
  14. const int HEIGHT=768;
  15.  
  16.  
  17.  
  18. struct bmpfile_magic {
  19.   unsigned char magic[2];
  20. };
  21.  
  22. struct bmpfile_header {
  23.   int filesz;
  24.   short creator1;
  25.   short creator2;
  26.   int bmp_offset;
  27. };
  28.  
  29. typedef struct {
  30.   int header_sz;
  31.   int width;
  32.   int height;
  33.   short nplanes;
  34.   short bitspp;
  35.   int compress_type;
  36.   int bmp_bytesz;
  37.   int hres;
  38.   int vres;
  39.   int ncolors;
  40.   int nimpcolors;
  41. } BITMAPINFOHEADER;
  42.  
  43.  
  44.  
  45. void saveBMP(SDL_Surface* surf)
  46. {
  47.     char filename[30];
  48.     sprintf(filename,"%d.bmp",(int)time(0));
  49.     std::ofstream out(filename,std::ios::binary);
  50.     bmpfile_magic bm={'B','M'};
  51.     bmpfile_header bh={54+((BPP*surf->w)/8)*surf->h,0,0,54};
  52.     BITMAPINFOHEADER bhi={40,surf->w,surf->h,1,BPP,0,((BPP*surf->w)/8)*surf->h,2750,2750,0,0};
  53.     out.write((char*)&bm,sizeof(bm));
  54.     out.write((char*)&bh,sizeof(bh));
  55.     out.write((char*)&bhi,sizeof(bhi));
  56.     for(int i=0;i<surf->h;i++)
  57.         for(int j=0;j<surf->w;j++)
  58.         {
  59.         //  std::cout << i << " " << j << std::endl;
  60.             Uint32 rgb=(char)((Uint32*)surf->pixels)[(surf->h-i-1)*(surf->pitch/4) + j];
  61.             Uint8 r,g,b;
  62.             SDL_GetRGB(rgb,surf->format,&r,&g,&b);
  63.                 out.put((char)r);
  64.                 out.put((char)g);
  65.                 out.put((char)b);
  66.                 out.put((char)r);
  67.         }
  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.  
  116. void csere(int& a,int &b)
  117. {
  118.     int tmp=a;
  119.     a=b;
  120.     b=tmp;
  121. }
  122. void drawLine(int startx,int starty,int endx,int endy,int size)
  123. {
  124.     bool b=abs(endy-starty)>abs(endx-startx);
  125.     if(b)
  126.     {
  127.         csere(startx,starty);
  128.         csere(endx,endy);
  129.     }
  130.     if(startx>endx)
  131.     {
  132.         csere(startx,endx);
  133.         csere(starty,endy);
  134.     }
  135.     float step=abs((float)(endy-starty))/(endx-startx);
  136.     int forstep=(endx>startx ? 1 : -1);
  137.     float y=0;
  138.     int ycoord=starty;
  139.     int ystep2=(starty>endy) ? 1 : -1;
  140.     for(int i=startx;i!=endx;i+=forstep)
  141.     {
  142.         if(!b)
  143.             putpixel(i,(int)ycoord,255,255,255,size);
  144.         else
  145.             putpixel((int)ycoord,i,255,255,255,size);
  146.        
  147.         y+=step;
  148.         if(y>0.5)
  149.         {
  150.             ycoord+=ystep2;
  151.             y-=1.0;
  152.         }
  153.     }
  154. }
  155.  
  156. void line2(int x0,int y0,int x1,int y1,int size)
  157. {
  158.      bool steep = abs(y1 - y0) > abs(x1 - x0);
  159.      if(steep)
  160.      {
  161.          csere(x0, y0);
  162.          csere(x1, y1);
  163.          }
  164.  
  165.      if(x0 > x1)
  166.      {
  167.          csere(x0, x1);
  168.          csere(y0, y1);
  169.          }
  170.  
  171.      int deltax = x1 - x0;
  172.      int deltay = abs(y1 - y0);
  173.      float error = 0;
  174.      float deltaerr = (float)deltay / deltax;
  175.      int ystep;
  176.      int y = y0;
  177.      if(y0 < y1)
  178.         ystep = 1;
  179.      else
  180.       ystep = -1;
  181.      for(int x=x0;x<x1;x++)
  182.      {
  183.          if(steep)
  184.             putpixel(y,x,255,255,255,size);
  185.          else
  186.           putpixel(x,y,255,255,255,size);
  187.          error = error + deltaerr;
  188.          if(error>= 0.5)
  189.          {
  190.              y = y + ystep;
  191.              error = error - 1.0;
  192.          }
  193.      }
  194. }
  195.  
  196. void drawTree(int startx,int starty,int len,int vast,float dir)
  197. {
  198.     if(vast>=1)
  199.     {
  200.         float endx=startx+sin((float)dir*M_PI/180.0)*len;
  201.         float endy=starty+cos((float)dir*M_PI/180.0)*len;
  202.         line2(startx,starty,endx,endy,vast);
  203.         //comment out for immediate result
  204.         //SDL_Flip(SDL_GetVideoSurface());
  205.         //SDL_Delay(1);
  206.         for(int j=-ANGLE;j<=ANGLE;j+=(ANGLE*2)/(NUM-1))
  207.             drawTree(endx,endy,len/2,vast-1,dir+j);
  208.     }
  209. }
  210.  
  211.  
  212.  
  213.  
  214. int main()
  215. {
  216.     srand(time(0));
  217.     SDL_Init(SDL_INIT_EVERYTHING);
  218.     SDL_Surface* screen=SDL_SetVideoMode(WIDTH,HEIGHT,BPP,SDL_SWSURFACE);
  219.     bool running=true;
  220.     SDL_Event event;
  221.     Uint32 start;
  222.     int i=0;
  223.     int j=0;
  224.     bool dir=true;
  225.     while(running)
  226.     {
  227.         start=SDL_GetTicks();
  228.         while(SDL_PollEvent(&event))
  229.         {
  230.             switch(event.type)
  231.             {
  232.                 case SDL_QUIT:
  233.                     running=false;
  234.                     break;
  235.             }
  236.         }
  237.         SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0,0,0));
  238.         drawTree(WIDTH/2,HEIGHT,HEIGHT/2,6,180);
  239.     //  cout << "-----------------------------------" << endl;
  240.         SDL_Flip(screen);
  241.        
  242.        
  243.         saveBMP(screen);
  244.        
  245.        
  246.        
  247.     //  SDL_Delay(2000);
  248.         break;
  249.         if(1000.0/30>(SDL_GetTicks()-start))
  250.             SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
  251.     }
  252.    
  253.     SDL_Quit();
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement