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;
48 for (ys = 0, y0 = y1; ys < screen->h; y0 += ystep, ys++) {
49 for (xs = 0, x0 = x1; xs < screen->w; x0 += xstep, xs++) {
50 iteration = get_mandelbrot_iterations(x0, y0);
52 if (iteration == MAX_ITERATION)
53 putpixel(screen, xs, ys, 255, 255, 255);
55 putpixel(screen, xs, ys,
56 iteration * 8, iteration,
66 int main(int argc, char *argv[])
69 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
70 int xres = 800, yres = 600;
72 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
73 fprintf(stderr, "Unable to initialize SDL: %s\n",
80 screen = SDL_SetVideoMode(xres, yres, 32, flags);
82 fprintf(stderr, "Unable to set video mode: %s\n",
87 SDL_WM_SetCaption(argv[0], NULL);
89 draw_mandelbrot(screen, -2.5, 1, -1, 1);
91 yres = read(0, &xres, 1);