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
- #include <iostream>
- #include <SDL/SDL.h>
- #include <cmath>
- #include <fstream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- const int BPP=32;
- const int NUM=6;
- const int ANGLE=80;
- 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++)
- {
- Uint32 rgb=(Uint32)((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)b);//b
- out.put((char)g);//g
- out.put((char)r);//r
- out.put((char)255);
- }
- }
- 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 line2(int x0,int y0,int x1,int y1,Uint32 color,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,color,size);
- else
- putpixel(x,y,color,size);
- error = error + deltaerr;
- if(error>= 0.5)
- {
- y = y + ystep;
- error = error - 1.0;
- }
- }
- }
- void drawTree(int startx,int starty,float len,int vast,float dir)
- {
- if(vast>=1)
- {
- if(len<1)
- len=1;
- float endx=startx+sin((float)dir*M_PI/180.0)*len;
- float endy=starty+cos((float)dir*M_PI/180.0)*len;
- float vecx=abs(endx-startx);
- float vecy=abs(endy-starty);
- float dist=sqrt(vecx*vecx+vecy*vecy);
- vecx/=dist;
- vecy/=dist;
- Uint32 color;
- if(vast>=4)
- color=SDL_MapRGB(SDL_GetVideoSurface()->format,0x7d+rand()%20-10,0x50+rand()%20-10,0x00);
- else if(vast==3)
- color=SDL_MapRGB(SDL_GetVideoSurface()->format,0x4c+rand()%20-10,0x57+rand()%20-10,0x00);
- else if(vast==2)
- color=SDL_MapRGB(SDL_GetVideoSurface()->format,rand()%20,0x5f+rand()%50-25,rand()%20);
- else if(vast==1)
- color=SDL_MapRGB(SDL_GetVideoSurface()->format,rand()%50,0xff-rand()%120,rand()%50);
- else
- color=SDL_MapRGB(SDL_GetVideoSurface()->format,0x00,0xff,0x00);
- for(int i=-vast/2;i<=vast/1;i++)
- {
- if(endx+i<0)
- endx=-i;
- if(endx+i>=WIDTH)
- endx=WIDTH-i;
- if(endy<0)
- endy=0;
- if(endy>=HEIGHT)
- endy=HEIGHT-1;
- if(startx+i<0)
- startx=-i;
- if(startx+i>WIDTH)
- startx=WIDTH-i;
- if(starty<0)
- starty=0;
- if(starty>=HEIGHT)
- starty=HEIGHT;
- line2(i+startx,starty,endx+i,endy,color,vast);
- }
- for(int j=-ANGLE;j<=ANGLE;j+=(ANGLE*2)/(NUM-1))
- {
- if(rand()%30<27)
- drawTree(endx,endy,len/(len>100 ? rand()/RAND_MAX+3.0 : rand()/RAND_MAX+1.5),vast-1,dir+j+rand()%40-20);
- }
- }
- }
- void pauseSDL()
- {
- SDL_Event event;
- bool running=true;
- while(1)
- {
- SDL_WaitEvent(&event);
- if(event.type==SDL_KEYDOWN || event.type==SDL_QUIT)
- break;
- }
- }
- 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;
- //for(int i=1;i<15;i++)
- // line2(10,(i+1)*30,300,(i+20)*30,SDL_MapRGB(screen->format,255,255,255),i);
- SDL_Flip(screen);
- saveBMP(screen);
- pauseSDL();
- // 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