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 draw_mandelbrot(struct SDL_Surface *screen)
22 double x, y, x0, y0, xtemp;
25 for (ys = 0, y0 = -1; ys < screen->h;
26 y0 += 2 / (double)screen->h, ys++) {
27 for (xs = 0, x0 = -2.5; xs < screen->w;
28 x0 += 3 / (double)screen->w, xs++) {
33 while (x * x + y * y < 2 * 2 &&
34 iteration < MAX_ITERATION) {
36 xtemp = x*x - y*y + x0;
43 if (iteration == MAX_ITERATION)
44 putpixel(screen, xs, ys, 255, 255, 255);
46 putpixel(screen, xs, ys,
55 int main(int argc, char *argv[])
58 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
59 int xres = 800, yres = 600;
61 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
62 fprintf(stderr, "Unable to initialize SDL: %s\n",
69 screen = SDL_SetVideoMode(xres, yres, 32, flags);
71 fprintf(stderr, "Unable to set video mode: %s\n",
76 SDL_WM_SetCaption(argv[0], NULL);
78 draw_mandelbrot(screen);
80 yres = read(0, &xres, 1);