8 /* Algorithmic variables */
13 double xmin = -2, ymin = -2, xmax = 2, ymax = 2;
15 /* Display variables */
17 SDL_Color palette[256],origpalette[256];
22 int iterate(double x, double y)
29 while ((i < maxiters) && (hypot(cplxr, cplxi) < BAILOUT))
31 double a=cplxr, b=cplxi, c=cplxr, d=cplxi;
42 SDL_SetColors(screen, palette, 0, 256);
51 palette[i] = palette[i+1];
56 Uint32 cycle_colors_cbk(Uint32 interval)
67 palette[i].r = sin(((double)i)/256.0*2.0*M_PI*5.0f)*256.0;
68 palette[i].g = sin(((double)(i+64))/256.0*2.0*M_PI*4.0f)*256.0;
69 palette[i].b = sin(((double)(i+128))/256.0*2.0*M_PI*3.0f)*256.0;
70 origpalette[i] = palette[i];
74 int videoflags = SDL_SWSURFACE | SDL_RESIZABLE;
78 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
80 printf("SDL init failed: %s\n", SDL_GetError());
85 screen = SDL_SetVideoMode(xres, yres, 8, videoflags);
88 printf("SDL video init failed: %s\n", SDL_GetError());
92 SDL_WM_SetCaption("Mandelbrot Set", "Mandelbrot");
95 double pxltofltx(int x)
97 return ((double)x)*((double)(xmax-xmin))/((double)xres)+xmin;
99 double pxltoflty(int y)
101 return ((double)y)*((double)(ymax-ymin))/((double)yres)+ymin;
109 printf("P6\n%d %d\n255\n", xres, yres);
113 SDL_WM_SetCaption("Mandelbrot Set [working]", "Mandelbrot");
114 SDL_SetTimer(0, NULL);
115 SDL_LockSurface(screen);
116 buffer = (Uint8*)screen->pixels;
122 int val = iterate(pxltofltx(x), pxltoflty(y));
124 printf("%c%c%c", palette[val].r, palette[val].g, palette[val].b);
129 buffer += screen->pitch;
136 SDL_UpdateRect(screen,0,0,0,0);
138 fprintf(stderr, "%d\n", y);
143 SDL_UpdateRect(screen, 0, 0, 0, 0);
144 SDL_UnlockSurface(screen);
145 SDL_WM_SetCaption("Mandelbrot Set", "Mandelbrot");
149 int load_palette(char* name)
154 file = gzopen(name, "rb");
160 for (idx = 0; idx < 256; idx++)
165 gzgets(file, c, 1024);
167 if ((c[0] == '\r') || (c[0] == '\n'))
172 if (c[0] == 0x1A /* EOF */)
177 for (element = 0; element < 3; element++)
180 while (isblank(*cidx))
184 fprintf(stderr, "%s: palette file format fault on color %d -- expected digit, got `%c'\n", name, idx, *cidx);
188 while (isdigit(*cidx))
191 accum += *cidx - '0';
196 case 0: origpalette[idx].r = palette[idx].r = accum; break;
197 case 1: origpalette[idx].g = palette[idx].g = accum; break;
198 case 2: origpalette[idx].b = palette[idx].b = accum; break;
211 "Usage: %s [OPTION]...\n"
212 "Renders the Mandelbrot set.\n"
214 "Mandatory arguments to long options are mandatory for short options too.\n"
215 " -x, --width=PIXELS sets the output width\n"
216 " -y, --height=PIXELS sets the output height\n"
217 " -m, --maxiters=ITERATIONS sets the maximum number of iterations\n"
218 " -s, --sdl enables SDL as an output device (default)\n"
219 " -S, --no-sdl disables SDL as an output device (implies -q)\n"
220 " -p, --ppm enables .ppm to stdout as an output device (implies -q)\n"
221 " -P, --no-ppm disables .ppm as an output device (default)\n"
222 " -q, --quit-immediately exits immediately after rendering\n"
223 " -l, --location=XMIN,XMAX,YMIN,YMAX\n"
224 " sets the starting location\n"
225 " -t, --palette=FILENAME sets the palette\n"
226 " --help shows this help\n"
228 "Written by Joshua Wise <joshua@joshuawise.com>.\n"
234 struct option longopts[] = {
235 {"x", required_argument, NULL, 'x'},
236 {"width", required_argument, NULL, 'x'},
237 {"y", required_argument, NULL, 'y'},
238 {"height", required_argument, NULL, 'y'},
239 {"maxiters", required_argument, NULL, 'm'},
240 {"sdl", no_argument, NULL, 's'},
241 {"ppm", no_argument, NULL, 'p'},
242 {"no-sdl", no_argument, NULL, 'S'},
243 {"no-ppm", no_argument, NULL, 'P'},
244 {"help", no_argument, NULL, 'h'},
245 {"location", required_argument, NULL, 'l'},
246 {"palette", required_argument, NULL, 't'},
247 {"quit-immediately", no_argument, NULL, 'q'},
251 int main(int argc, char** argv)
258 int custompalette = 0;
259 int quitimmediately = 0;
262 while ((arg = getopt_long(argc, argv, "x:y:m:l:t:sSpPq", longopts, NULL)) != -1)
270 printf("%s: option `%c' requires an integer argument\n", argv[0], arg);
271 printf("Try `%s --help' for more information.\n", argv[0]);
279 printf("%s: option `%c' requires an integer argument\n", argv[0], arg);
280 printf("Try `%s --help' for more information.\n", argv[0]);
285 maxiters = atoi(optarg);
288 printf("%s: option `%c' requires an integer argument\n", argv[0], arg);
289 printf("Try `%s --help' for more information.\n", argv[0]);
312 if (sscanf(optarg, "%f,%f,%f,%f", &f1, &f2, &f3, &f4) != 4)
314 printf("%s: incorrect syntax for option `%c'\n", argv[0], arg);
315 printf("Try %s --help' for more information.\n", argv[0]);
318 xmin = f1; xmax = f2; ymin = f3; ymax = f4;
322 custompalette = load_palette(optarg);
329 printf("Try `%s --help' for more information.\n", argv[0]);
333 printf("Oh God I am not good with computers how did this get here?\n");
340 yctr = ymin + (ymax - ymin) / 2.0;
341 // get y in proportion
342 ysz = (xmax-xmin)*((double)yres)/((double)xres);
343 ymax = yctr+ysz/2; ymin = yctr-ysz/2;
351 while ( SDL_WaitEvent(&event) ) {
352 switch (event.type) {
354 if (event.key.keysym.sym == SDLK_c)
358 SDL_WM_SetCaption("Mandelbrot Set [color cycling]", "Mandelbrot");
359 SDL_SetTimer(50, cycle_colors_cbk);
361 SDL_WM_SetCaption("Mandelbrot Set", "Mandelbrot");
362 SDL_SetTimer(0, NULL);
365 } else if (event.key.keysym.sym == SDLK_r) {
369 palette[i].r = origpalette[i].r;
370 palette[i].g = origpalette[i].g;
371 palette[i].b = origpalette[i].b;
374 } else if (event.key.keysym.sym == SDLK_ESCAPE ||
375 event.key.keysym.sym == SDLK_q) {
377 } else if (event.key.keysym.sym == SDLK_p) {
381 } else if (event.key.keysym.sym == SDLK_RETURN) {
383 } else if (event.key.keysym.sym == SDLK_i) {
384 printf("mandelbrot by Joshua Wise\n");
385 printf("2005-06-19, Little Cayman\n");
387 printf("Current settings:\n");
388 printf(" xmin: %f, xmax: %f\n", xmin, xmax);
389 printf(" ymin: %f, ymax: %f\n", ymin, ymax);
390 printf(" maxiters: %d, bailout: %f\n", maxiters, BAILOUT);
391 printf(" xres: %d, yres: %d\n", xres, yres);
392 printf("Recreate with:\n");
393 printf(" %s -x%d -y%d -l%f,%f,%f,%f -m%d\n", argv[0], xres,yres,xmin,xmax,ymin,ymax,maxiters);
396 case SDL_MOUSEBUTTONDOWN:
397 if (event.button.button == 1)
399 startx = event.button.x;
400 starty = event.button.y;
402 case SDL_MOUSEMOTION:
406 case SDL_MOUSEBUTTONUP:
407 if (event.button.button == 1)
411 if (event.button.button == 1 || event.button.button == 3)
413 double xsz = xmax-xmin, ysz = ymax-ymin;
416 if (event.button.button == 1)
422 x = pxltofltx(event.button.x);
423 y = pxltoflty(event.button.y);
429 } else if (event.button.button == 2) {
437 double newmaxx, newminx, newmaxy, newminy;
438 if (event.button.x > startx)
440 newmaxx = pxltofltx(event.button.x);
441 newminx = pxltofltx(startx);
443 newminx = pxltofltx(event.button.x);
444 newmaxx = pxltofltx(startx);
446 if (event.button.y > starty)
448 newmaxy = pxltoflty(event.button.y);
449 newminy = pxltoflty(starty);
451 newminy = pxltoflty(event.button.y);
452 newmaxy = pxltoflty(starty);
454 yctr = newminy + (newmaxy - newminy) / 2.0;
455 // get y in proportion
456 ysz = (newmaxx-newminx)*((double)yres)/((double)xres);
457 ymax = yctr+ysz/2; ymin = yctr-ysz/2;
458 xmax = newmaxx; xmin = newminx;
463 case SDL_VIDEORESIZE:
465 screen = SDL_SetVideoMode(event.resize.w, event.resize.h,
466 screen->format->BitsPerPixel,
470 fprintf(stderr, "Couldn't resize screen\n");
473 SDL_SetColors(screen, palette, 0, 256);
474 xres = event.resize.w;
475 yres = event.resize.h;
476 yctr = ymin + (ymax - ymin) / 2.0;
477 // get y in proportion
478 ysz = (xmax-xmin)*((double)yres)/((double)xres);
479 ymax = yctr+ysz/2; ymin = yctr-ysz/2;