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)
42 for (ys = 0, y0 = -1; ys < screen->h;
43 y0 += 2 / (double)screen->h, ys++) {
44 for (xs = 0, x0 = -2.5; xs < screen->w;
45 x0 += 3 / (double)screen->w, xs++) {
47 iteration = get_mandelbrot_iterations(x0, y0);
49 if (iteration == MAX_ITERATION)
50 putpixel(screen, xs, ys, 255, 255, 255);
52 putpixel(screen, xs, ys,
61 int main(int argc, char *argv[])
64 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
65 int xres = 800, yres = 600;
67 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
68 fprintf(stderr, "Unable to initialize SDL: %s\n",
75 screen = SDL_SetVideoMode(xres, yres, 32, flags);
77 fprintf(stderr, "Unable to set video mode: %s\n",
82 SDL_WM_SetCaption(argv[0], NULL);
84 draw_mandelbrot(screen);
86 yres = read(0, &xres, 1);