6 static void putpixel(struct SDL_Surface *screen, const int x, const int y,
7 const unsigned char r, const unsigned char g,
10 int offset = y * screen->pitch + x * 4;
11 unsigned char *buf = screen->pixels;
18 #define MAX_ITERATION 1000
20 int get_mandelbrot_iterations(double x0, double y0)
22 double x = 0, y = 0, xtemp;
25 while ((x * x + y * y < 2 * 2) && iteration < MAX_ITERATION) {
27 xtemp = x * x - y * y + x0;
37 int draw_mandelbrot(struct SDL_Surface *screen, double x1, double x2,
40 double x0, y0, xlen, ylen, xstep, ystep;
41 int iteration, xs, ys;
45 xstep = xlen / (double)screen->w;
46 ystep = ylen / (double)screen->h;
49 #pragma omp parallel for private(xs, ys, x0, y0)
50 for (ys = 0; ys < screen->h; ys++) {
53 for (xs = 0; xs < screen->w; xs++) {
54 iteration = get_mandelbrot_iterations(x0, y0);
56 if (iteration == MAX_ITERATION)
57 putpixel(screen, xs, ys, 255, 255, 255);
59 putpixel(screen, xs, ys,
60 iteration * 8, iteration,
70 int main(int argc, char *argv[])
73 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
74 int xres = 800, yres = 600;
76 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
77 fprintf(stderr, "Unable to initialize SDL: %s\n",
84 screen = SDL_SetVideoMode(xres, yres, 32, flags);
86 fprintf(stderr, "Unable to set video mode: %s\n",
91 SDL_WM_SetCaption(argv[0], NULL);
93 draw_mandelbrot(screen, -2.5, 1, -1, 1);
95 yres = read(0, &xres, 1);