Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include "SDL/SDL.h"
- #include <pthread.h>
- #define WIDTH 900 // divisible by THREADS
- #define HEIGHT 900
- #define DEPTH 32
- #define THREADS 2
- double xoff=-2, yoff=-1.5;
- double scale=(1/3.0);
- int itr=300;
- struct complex {
- double real;
- double img;
- int color[HEIGHT];
- };
- void setpixel(SDL_Surface *screen, int x, int y, int color)
- {
- SDL_Rect rect = {x, y, 1, 1};
- SDL_FillRect(screen, &rect, color);
- }
- void* mandelbrot(void* cP) //
- {
- struct complex z;
- struct complex *c = (struct complex *) cP;
- int i, y;
- double absZ;
- for(y=0; y<HEIGHT; y++){
- z.real=0; z.img=0;
- c->img=(double)(y/(scale*HEIGHT))+yoff;
- for(i=0; i<=itr; i++) //set iterations
- {
- absZ=z.real;
- z.real=(z.real*z.real-z.img*z.img)+(c->real);
- z.img=(absZ*z.img)*2+c->img; // find z^2 + c
- absZ=z.real*z.real+z.img*z.img; // find abs(z)
- if( absZ>4 )
- break; // outside circle with r=2
- }
- if(i>=itr)
- c->color[y]=0;
- else
- c->color[y]=i*log(log10(sqrt(absZ)))/log(2); // kinda nice color
- }
- return NULL;
- }
- int main()
- {
- SDL_Surface *screen;
- SDL_Event event;
- // thread stuff
- pthread_t thread[THREADS];
- struct complex threadC[THREADS];
- int threadReturn[THREADS];
- int it;
- int quit=0;
- if( SDL_Init(SDL_INIT_VIDEO) < 0 )
- return 1;
- if(!(screen = SDL_SetVideoMode( WIDTH, HEIGHT, DEPTH, SDL_HWSURFACE)))
- {
- SDL_Quit();
- return 1;
- }
- int i,j;
- int draw=1;
- int zoomlevel=1;
- while(!quit)
- {
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_LEFT:
- xoff-=.2/scale;
- break;
- case SDLK_RIGHT:
- xoff+=.2/scale;
- break;
- case SDLK_UP:
- yoff-=.2/scale;
- break;
- case SDLK_DOWN:
- yoff+=.2/scale;
- break;
- case SDLK_SPACE:
- draw=1;
- printf("Drawing... ");
- break;
- case SDLK_1:
- scale*=2;
- zoomlevel++;
- printf("zoom: %d, %f percent of original\n",\
- ++zoomlevel, 1.0/zoomlevel);
- break;
- case SDLK_2:
- if(scale>.4){
- scale/=2;
- zoomlevel--;
- printf("zoom: %d, %f percent of original\n",\
- --zoomlevel, 1.0/zoomlevel);
- }
- break;
- case SDLK_9:
- itr+=100;
- printf("iterations: %d\n", itr);
- break;
- case SDLK_0:
- if(itr>100)
- itr-=100;
- printf("iterations: %d\n", itr);
- break;
- default:
- break;
- }
- break;
- case SDL_MOUSEBUTTONDOWN:
- if(event.button.button==SDL_BUTTON_LEFT){
- scale*=2;
- xoff=xoff+(event.button.x)/(scale*WIDTH);
- yoff=yoff+(event.button.y)/(scale*HEIGHT);
- zoomlevel++;
- printf("zoom: %d, %f percent of original\n", zoomlevel,100.0/zoomlevel);
- draw=1;
- }
- break;
- case SDL_QUIT:
- quit=1;
- break;
- default:
- break;
- }
- }
- if(draw){
- for(i=0; i<WIDTH; i+=THREADS)
- {
- for(it=0; it<THREADS; it++)
- threadC[it].real=(double)((i+it)/(scale*WIDTH))+xoff;
- for(it=0; it<THREADS; it++){ // creates threads
- threadC[it].img=(double)(j/(scale*HEIGHT))+yoff;
- pthread_create(&thread[it], NULL, mandelbrot, (void *) &threadC[it]);
- //mandelbrot(&threadC[it]);
- }
- for(it=0; it<THREADS; it++){ // wait for threads to end, draws image
- pthread_join(thread[it], NULL);
- for(j=0; j<HEIGHT; j++)
- setpixel(screen, i+it, j, (threadC[it].color[j])*0xF0FF);
- }
- }
- draw=0;
- printf("Done drawing!\n");
- }
- SDL_Flip(screen);
- SDL_Delay(30); // dont use 100% cpu!!!
- }
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement