]> git.itanic.dy.fi Git - mandelbrot/blob - mandelbrot.c
624fcf2ebda615e2275021d1d50aa59b91624a30
[mandelbrot] / mandelbrot.c
1 #include <SDL.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <time.h>
5
6 static void putpixel(struct SDL_Surface *screen, const int x, const int y,
7                      const unsigned char r, const unsigned char g,
8                      const unsigned char b)
9 {
10         int offset = y * screen->pitch + x * 4;
11         unsigned char *buf = screen->pixels;
12
13         buf[offset++] = b;
14         buf[offset++] = g;
15         buf[offset]   = r;
16 }
17
18 #define MAX_ITERATION 1000
19
20 int get_mandelbrot_iterations(double x0, double y0)
21 {
22         double x = 0, y = 0, xtemp;
23         int iteration = 0;
24
25         while ((x * x + y * y < 2 * 2) && iteration < MAX_ITERATION) {
26
27                 xtemp = x * x - y * y + x0;
28                 y = 2 * x * y + y0;
29
30                 x = xtemp;
31                 iteration++;
32         }
33
34         return iteration;
35 }
36
37 int draw_mandelbrot(struct SDL_Surface *screen, double x1, double x2,
38                 double y1, double y2)
39 {
40         double x0, y0, xlen, ylen, xstep, ystep;
41         int iteration, xs, ys;
42
43         xlen = x2 - x1;
44         ylen = y2 - y1;
45         xstep = xlen / (double)screen->w;
46         ystep = ylen / (double)screen->h;
47
48         y0 = y1;
49 #pragma omp parallel for private(xs, ys, x0, y0)
50         for (ys = 0; ys < screen->h; ys++) {
51                 y0 = y1 + ystep * ys;
52                 x0 = x1;
53                 for (xs = 0; xs < screen->w; xs++) {
54                         iteration = get_mandelbrot_iterations(x0, y0);
55
56                         if (iteration == MAX_ITERATION)
57                                 putpixel(screen, xs, ys, 255, 255, 255);
58                         else
59                                 putpixel(screen, xs, ys,
60                                         iteration * 8, iteration,
61                                         iteration / 4);
62                         x0 += xstep;
63                 }
64         }
65
66         SDL_Flip(screen);
67         return 0;
68 }
69
70 int main(int argc, char *argv[])
71 {
72         SDL_Surface *screen;
73         int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
74         int xres = 800, yres = 600;
75
76         if (SDL_Init(SDL_INIT_VIDEO) != 0) {
77                 fprintf(stderr, "Unable to initialize SDL: %s\n",
78                         SDL_GetError());
79
80                 return 1;
81         }
82         atexit(SDL_Quit);
83
84         screen = SDL_SetVideoMode(xres, yres, 32, flags);
85         if (screen == NULL) {
86                 fprintf(stderr, "Unable to set video mode: %s\n",
87                         SDL_GetError());
88                 return 2;
89         }
90
91         SDL_WM_SetCaption(argv[0], NULL);
92
93         draw_mandelbrot(screen, -2.5, 1, -1, 1);
94
95         yres = read(0, &xres, 1);
96
97         return 0;
98 }