X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=hacks%2Fsierpinski.c;h=8d5215630417dd4aa6f262e4e9f292b5c7099763;hb=96a411663168b0ba5432b407a83be55f3df0c802;hp=65110247ae66e12e68081acbf0e01af60efc6c9e;hpb=186b0b9f1638444c650c9273df38085e0db71e4a;p=xscreensaver diff --git a/hacks/sierpinski.c b/hacks/sierpinski.c index 65110247..8d521563 100644 --- a/hacks/sierpinski.c +++ b/hacks/sierpinski.c @@ -1,11 +1,12 @@ -/* -*- Mode: C; tab-width: 4 -*- - * sierpinski --- Sierpinski's triangle fractal. - */ -#if !defined( lint ) && !defined( SABER ) -static const char sccsid[] = "@(#)sierpinski.c 4.05 97/09/19 xlockmore"; +/* -*- Mode: C; tab-width: 4 -*- */ +/* sierpinski --- Sierpinski's triangle fractal */ + +#if 0 +static const char sccsid[] = "@(#)sierpinski.c 5.00 2000/11/01 xlockmore"; #endif -/* Copyright (c) 1996 by Desmond Daignault +/*- + * Copyright (c) 1996 by Desmond Daignault * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose and without fee is hereby granted, @@ -19,30 +20,47 @@ static const char sccsid[] = "@(#)sierpinski.c 4.05 97/09/19 xlockmore"; * event will the author be liable for any lost revenue or profits or * other special, indirect and consequential damages. * + * Dots initially appear where they "should not". Later they get + * "focused". This is correct behavior. + * * Revision History: - * 18-Sep-97: 3D version Antti Kuntsi . - * 20-May-97: Changed the name tri to sierpinski for more compatiblity - * 10-May-97: jwz@netscape.com: turned into a standalone program. - * 05-Sep-96: Desmond Daignault Datatimes Incorporated + * 01-Nov-2000: Allocation checks + * 18-Sep-1997: 3D version Antti Kuntsi . + * 20-May-1997: Changed the name tri to sierpinski for more compatiblity + * 10-May-1997: Jamie Zawinski compatible with xscreensaver + * 05-Sep-1996: Desmond Daignault Datatimes Incorporated * . */ #ifdef STANDALONE -# define PROGCLASS "Sierpinski" -# define HACK_INIT init_sierpinski -# define HACK_DRAW draw_sierpinski -# define sierpinski_opts xlockmore_opts -# define DEFAULTS "*count: 2000 \n" \ - "*cycles: 100 \n" \ - "*delay: 400000 \n" \ - "*ncolors: 64 \n" -# include "xlockmore.h" /* from the xscreensaver distribution */ -#else /* !STANDALONE */ -# include "xlock.h" /* from the xlockmore distribution */ -#endif /* !STANDALONE */ +#define MODE_sierpinski +#define PROGCLASS "Sierpinski" +#define HACK_INIT init_sierpinski +#define HACK_DRAW draw_sierpinski +#define sierpinski_opts xlockmore_opts +#define DEFAULTS "*delay: 400000 \n" \ + "*count: 2000 \n" \ + "*cycles: 100 \n" \ + "*ncolors: 64 \n" +#define BRIGHT_COLORS +#include "xlockmore.h" /* in xscreensaver distribution */ +#else /* STANDALONE */ +#include "xlock.h" /* in xlockmore distribution */ +#endif /* STANDALONE */ + +#ifdef MODE_sierpinski ModeSpecOpt sierpinski_opts = -{0, NULL, 0, NULL, NULL}; +{0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL}; + +#ifdef USE_MODULES +ModStruct sierpinski_description = +{"sierpinski", "init_sierpinski", "draw_sierpinski", "release_sierpinski", + "refresh_sierpinski", "init_sierpinski", (char *) NULL, &sierpinski_opts, + 400000, 2000, 100, 1, 64, 1.0, "", + "Shows Sierpinski's triangle", 0, NULL}; + +#endif #define MAXCORNERS 4 @@ -51,14 +69,14 @@ typedef struct { int time; int px, py; int total_npoints; - int corners; + int corners; int npoints[MAXCORNERS]; unsigned long colors[MAXCORNERS]; XPoint *pointBuffer[MAXCORNERS]; XPoint vertex[MAXCORNERS]; } sierpinskistruct; -static sierpinskistruct *tris = NULL; +static sierpinskistruct *tris = (sierpinskistruct *) NULL; static void startover(ModeInfo * mi) @@ -67,23 +85,23 @@ startover(ModeInfo * mi) sierpinskistruct *sp = &tris[MI_SCREEN(mi)]; if (MI_NPIXELS(mi) > 2) { - if (sp->corners == 3) { - sp->colors[0] = (NRAND(MI_NPIXELS(mi))); - sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 + + if (sp->corners == 3) { + sp->colors[0] = (NRAND(MI_NPIXELS(mi))); + sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 + NRAND(2 * MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi); - sp->colors[2] = (sp->colors[0] + 4 * MI_NPIXELS(mi) / 7 + + sp->colors[2] = (sp->colors[0] + 4 * MI_NPIXELS(mi) / 7 + NRAND(2 * MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi); - } else if (sp->corners == 4) { - sp->colors[0] = (NRAND(MI_NPIXELS(mi))); - sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 + - NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi); - sp->colors[2] = (sp->colors[0] + 3 * MI_NPIXELS(mi) / 7 + - NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi); - sp->colors[3] = (sp->colors[0] + 5 * MI_NPIXELS(mi) / 7 + - NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi); - } else { - (void) fprintf(stderr, "colors not set for %d corners\n", sp->corners); - } + } else if (sp->corners == 4) { + sp->colors[0] = (NRAND(MI_NPIXELS(mi))); + sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 + + NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi); + sp->colors[2] = (sp->colors[0] + 3 * MI_NPIXELS(mi) / 7 + + NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi); + sp->colors[3] = (sp->colors[0] + 5 * MI_NPIXELS(mi) / 7 + + NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi); + } else { + (void) fprintf(stderr, "colors not set for %d corners\n", sp->corners); + } } for (j = 0; j < sp->corners; j++) { sp->vertex[j].x = NRAND(sp->width); @@ -92,14 +110,27 @@ startover(ModeInfo * mi) sp->px = NRAND(sp->width); sp->py = NRAND(sp->height); sp->time = 0; - XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi)); + + MI_CLEARWINDOW(mi); +} + +static void +free_sierpinski(sierpinskistruct *sp) +{ + int corner; + + for (corner = 0; corner < MAXCORNERS; corner++) + if (sp->pointBuffer[corner] != NULL) { + (void) free((void *) sp->pointBuffer[corner]); + sp->pointBuffer[corner] = (XPoint *) NULL; + } } void init_sierpinski(ModeInfo * mi) { - sierpinskistruct *sp; int i; + sierpinskistruct *sp; if (tris == NULL) { if ((tris = (sierpinskistruct *) calloc(MI_NUM_SCREENS(mi), @@ -108,17 +139,23 @@ init_sierpinski(ModeInfo * mi) } sp = &tris[MI_SCREEN(mi)]; - sp->width = MI_WIN_WIDTH(mi); - sp->height = MI_WIN_HEIGHT(mi); + sp->width = MI_WIDTH(mi); + sp->height = MI_HEIGHT(mi); - sp->total_npoints = MI_BATCHCOUNT(mi); + sp->total_npoints = MI_COUNT(mi); if (sp->total_npoints < 1) sp->total_npoints = 1; - sp->corners = (LRAND() & 1) + 3; + sp->corners = MI_SIZE(mi); + if (sp->corners < 3 || sp->corners > 4) { + sp->corners = (int) (LRAND() & 1) + 3; + } for (i = 0; i < sp->corners; i++) { if (!sp->pointBuffer[i]) - sp->pointBuffer[i] = (XPoint *) malloc(sp->total_npoints * - sizeof (XPoint)); + if ((sp->pointBuffer[i] = (XPoint *) malloc(sp->total_npoints * + sizeof (XPoint))) == NULL) { + free_sierpinski(sp); + return; + } } startover(mi); } @@ -128,14 +165,19 @@ draw_sierpinski(ModeInfo * mi) { Display *display = MI_DISPLAY(mi); GC gc = MI_GC(mi); - sierpinskistruct *sp = &tris[MI_SCREEN(mi)]; - XPoint **xp; - int i = 0, v; + XPoint *xp[MAXCORNERS]; + int i, v; + sierpinskistruct *sp; + + if (tris == NULL) + return; + sp = &tris[MI_SCREEN(mi)]; + if (sp->pointBuffer[0] == NULL) + return; - xp = (XPoint **) malloc (sp->corners * sizeof (XPoint *)); - + MI_IS_DRAWN(mi) = True; if (MI_NPIXELS(mi) <= 2) - XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi)); + XSetForeground(display, gc, MI_WHITE_PIXEL(mi)); for (i = 0; i < sp->corners; i++) xp[i] = sp->pointBuffer[i]; for (i = 0; i < sp->total_npoints; i++) { @@ -156,29 +198,25 @@ draw_sierpinski(ModeInfo * mi) } if (++sp->time >= MI_CYCLES(mi)) startover(mi); - - free (xp); } void release_sierpinski(ModeInfo * mi) { if (tris != NULL) { - int screen, i; + int screen; - for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) { - for (i = 0; i < MAXCORNERS; i++) - if (tris[screen].pointBuffer[i] != NULL) { - (void) free((void *) tris[screen].pointBuffer[i]); - } - } + for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) + free_sierpinski(&tris[screen]); (void) free((void *) tris); - tris = NULL; + tris = (sierpinskistruct *) NULL; } } void refresh_sierpinski(ModeInfo * mi) { - /* Do nothing, it will refresh by itself */ + MI_CLEARWINDOW(mi); } + +#endif /* MODE_sierpinski */