#include #include #include #include static void putpixel(struct SDL_Surface *screen, const int x, const int y, const unsigned char r, const unsigned char g, const unsigned char b) { int offset = y * screen->pitch + x * 4; unsigned char *buf = screen->pixels; buf[offset++] = b; buf[offset++] = g; buf[offset] = r; } #define MAX_ITERATION 1000 int draw_mandelbrot(struct SDL_Surface *screen) { double x, y, x0, y0, xtemp; int iteration, xs,ys; for (ys = 0, y0 = -1; ys < screen->h; y0 += 2 / (double)screen->h, ys++) { for (xs = 0, x0 = -2.5; xs < screen->w; x0 += 3 / (double)screen->w, xs++) { iteration = 0; x = 0; y = 0; while (x * x + y * y < 2 * 2 && iteration < MAX_ITERATION) { xtemp = x*x - y*y + x0; y = 2*x*y + y0; x = xtemp; iteration++; } if (iteration == MAX_ITERATION) putpixel(screen, xs, ys, 255, 255, 255); else putpixel(screen, xs, ys, iteration, 0, 0); } SDL_Flip(screen); } return 0; } int main(int argc, char *argv[]) { SDL_Surface *screen; int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE; int xres = 800, yres = 600; if (SDL_Init(SDL_INIT_VIDEO) != 0) { fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError()); return 1; } atexit(SDL_Quit); screen = SDL_SetVideoMode(xres, yres, 32, flags); if (screen == NULL) { fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError()); return 2; } SDL_WM_SetCaption(argv[0], NULL); draw_mandelbrot(screen); yres = read(0, &xres, 1); return 0; }