Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Implemented by http://www.youtube.com/user/thecplusplusguy
- //SDL library used
- //draw a tree, at treedrawer function there are 2 commented out lines, uncomment those, to see the process
- #include <iostream>
- #include <SDL/SDL.h>
- #include <cmath>
- #include <fstream>
- using namespace std;
- const int BPP=32;
- const int NUM=6;
- const int ANGLE=70;
- const int WIDTH=1024;
- const int HEIGHT=768;
- struct bmpfile_magic {
- unsigned char magic[2];
- };
- struct bmpfile_header {
- int filesz;
- short creator1;
- short creator2;
- int bmp_offset;
- };
- typedef struct {
- int header_sz;
- int width;
- int height;
- short nplanes;
- short bitspp;
- int compress_type;
- int bmp_bytesz;
- int hres;
- int vres;
- int ncolors;
- int nimpcolors;
- } BITMAPINFOHEADER;
- void saveBMP(SDL_Surface* surf)
- {
- char filename[30];
- sprintf(filename,"%d.bmp",(int)time(0));
- std::ofstream out(filename,std::ios::binary);
- bmpfile_magic bm={'B','M'};
- bmpfile_header bh={54+((BPP*surf->w)/8)*surf->h,0,0,54};
- BITMAPINFOHEADER bhi={40,surf->w,surf->h,1,BPP,0,((BPP*surf->w)/8)*surf->h,2750,2750,0,0};
- out.write((char*)&bm,sizeof(bm));
- out.write((char*)&bh,sizeof(bh));
- out.write((char*)&bhi,sizeof(bhi));
- for(int i=0;i<surf->h;i++)
- for(int j=0;j<surf->w;j++)
- {
- // std::cout << i << " " << j << std::endl;
- Uint32 rgb=(char)((Uint32*)surf->pixels)[(surf->h-i-1)*(surf->pitch/4) + j];
- Uint8 r,g,b;
- SDL_GetRGB(rgb,surf->format,&r,&g,&b);
- out.put((char)r);
- out.put((char)g);
- out.put((char)b);
- out.put((char)r);
- }
- }
- void putpixel(int x,int y,int r,int g,int b,int size)
- {
- Uint32* pixels=(Uint32*)SDL_GetVideoSurface()->pixels;
- Uint32* pixel;
- if(size<=1)
- {
- pixel=pixels+y*SDL_GetVideoSurface()->pitch/4+x;
- *pixel=SDL_MapRGB(SDL_GetVideoSurface()->format,r,g,b);
- }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*SDL_GetVideoSurface()->pitch/4+j;
- *pixel=SDL_MapRGB(SDL_GetVideoSurface()->format,r,g,b);
- }
- }
- }
- void putpixel(int x,int y,Uint32 color,int size)
- {
- Uint32* pixels=(Uint32*)SDL_GetVideoSurface()->pixels;
- Uint32* pixel;
- if(size<=1)
- {
- pixel=pixels+y*SDL_GetVideoSurface()->pitch/4+x;
- *pixel=color;
- }else{
- for(int i=y-size/2;i<ceil(y+size/2);i++)
- for(int j=x-size/2;j<ceil(x+size/2);j++)
- {
- if(i<0 || i>=HEIGHT || j<0 || j>=WIDTH)
- continue;
- pixel=pixels+i*SDL_GetVideoSurface()->pitch/4+j;
- *pixel=color;
- }
- }
- }
- void csere(int& a,int &b)
- {
- int tmp=a;
- a=b;
- b=tmp;
- }
- void drawLine(int startx,int starty,int endx,int endy,int size)
- {
- bool b=abs(endy-starty)>abs(endx-startx);
- if(b)
- {
- csere(startx,starty);
- csere(endx,endy);
- }
- if(startx>endx)
- {
- csere(startx,endx);
- csere(starty,endy);
- }
- float step=abs((float)(endy-starty))/(endx-startx);
- int forstep=(endx>startx ? 1 : -1);
- float y=0;
- int ycoord=starty;
- int ystep2=(starty>endy) ? 1 : -1;
- for(int i=startx;i!=endx;i+=forstep)
- {
- if(!b)
- putpixel(i,(int)ycoord,255,255,255,size);
- else
- putpixel((int)ycoord,i,255,255,255,size);
- y+=step;
- if(y>0.5)
- {
- ycoord+=ystep2;
- y-=1.0;
- }
- }
- }
- void line2(int x0,int y0,int x1,int y1,int size)
- {
- 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(y,x,255,255,255,size);
- else
- putpixel(x,y,255,255,255,size);
- error = error + deltaerr;
- if(error>= 0.5)
- {
- y = y + ystep;
- error = error - 1.0;
- }
- }
- }
- void drawTree(int startx,int starty,int len,int vast,float dir)
- {
- if(vast>=1)
- {
- float endx=startx+sin((float)dir*M_PI/180.0)*len;
- float endy=starty+cos((float)dir*M_PI/180.0)*len;
- line2(startx,starty,endx,endy,vast);
- //comment out for immediate result
- //SDL_Flip(SDL_GetVideoSurface());
- //SDL_Delay(1);
- for(int j=-ANGLE;j<=ANGLE;j+=(ANGLE*2)/(NUM-1))
- drawTree(endx,endy,len/2,vast-1,dir+j);
- }
- }
- int main()
- {
- srand(time(0));
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_Surface* screen=SDL_SetVideoMode(WIDTH,HEIGHT,BPP,SDL_SWSURFACE);
- bool running=true;
- SDL_Event event;
- Uint32 start;
- int i=0;
- int j=0;
- bool dir=true;
- while(running)
- {
- start=SDL_GetTicks();
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_QUIT:
- running=false;
- break;
- }
- }
- SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0,0,0));
- drawTree(WIDTH/2,HEIGHT,HEIGHT/2,6,180);
- // cout << "-----------------------------------" << endl;
- SDL_Flip(screen);
- saveBMP(screen);
- // SDL_Delay(2000);
- break;
- if(1000.0/30>(SDL_GetTicks()-start))
- SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
- }
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement