From c4574a9fb0281ff3945ecb7eedf101680a7db6ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juha=20Vehvil=C3=A4inen?= Date: Sun, 25 Aug 2002 18:55:02 +0000 Subject: *** empty log message *** svn path=/trunk/Framestein/; revision=88 --- Plugins/gol.c | 84 +++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 34 deletions(-) (limited to 'Plugins/gol.c') diff --git a/Plugins/gol.c b/Plugins/gol.c index 89ff284..3349df2 100644 --- a/Plugins/gol.c +++ b/Plugins/gol.c @@ -1,8 +1,9 @@ - // -// Game of Life .. what a waste of time +// Conway's Game of Life .. also take a look at rowca.cpp // +// Currently only works with 16bit displays + #include #include #include "plugin.h" @@ -10,40 +11,59 @@ #define BORN 1 #define DYING 2 -int dead = 0; - int aroundme(_frame f, int x, int y) { + pixel16 *p; int i=0; - if(r16(scanline16(f, y-1)[x-1])>dead) i++; - if(r16(scanline16(f, y-1)[x])>dead) i++; - if(r16(scanline16(f, y-1)[x+1])>dead) i++; - if(r16(scanline16(f, y)[x-1])>dead) i++; - if(r16(scanline16(f, y)[x+1])>dead) i++; - if(r16(scanline16(f, y+1)[x-1])>dead) i++; - if(r16(scanline16(f, y+1)[x])>dead) i++; - if(r16(scanline16(f, y+1)[x+1])>dead) i++; + + p=scanline16(f, y-1); + if(p[x-1]) i++; + if(p[x]) i++; + if(p[x+1]) i++; + + p=scanline16(f, y); + if(p[x-1]) i++; + if(p[x+1]) i++; + + p=scanline16(f, y+1); + if(p[x-1]) i++; + if(p[x]) i++; + if(p[x+1]) i++; + return i; } -void setstate(_frame f, byte *t, int x, int y) +byte *t=0; +long size=0; + +void setstate(_frame f, int x, int y) { int i = aroundme(f, x, y); if(i<=1 || i>=4) t[y*f.width+x]=DYING; - else - if(i==3) t[y*f.width+x]=BORN; + else if(i==3) t[y*f.width+x]=BORN; } void perform_effect(struct frame f, struct args a) { int x,y,i,r; pixel16 *p, c; - byte *t; - if(f.pixelformat!=16) return; + if(f.pixelformat!=16) + { + printf("Sorry, gol works only on a 16bit display.\n"); + return; + } + + if(t && size != f.width*f.height) + { + free(t); + t=0; + } + + if(!t) + t = malloc(size = f.width*f.height); - t = malloc(f.width*f.height); memset(t, 0, f.width*f.height); for(y=2; y0) + if(p[x]) { - setstate(f, t, x-1, y-1); - setstate(f, t, x, y-1); - setstate(f, t, x+1, y-1); - setstate(f, t, x-1, y); - setstate(f, t, x, y); - setstate(f, t, x+1, y); - setstate(f, t, x-1, y+1); - setstate(f, t, x, y+1); - setstate(f, t, x+1, y+1); + setstate(f, x-1, y-1); + setstate(f, x, y-1); + setstate(f, x+1, y-1); + setstate(f, x-1, y); + setstate(f, x, y); + setstate(f, x+1, y); + setstate(f, x-1, y+1); + setstate(f, x, y+1); + setstate(f, x+1, y+1); } } } @@ -74,16 +94,12 @@ void perform_effect(struct frame f, struct args a) switch(t[y*f.width+x]) { case BORN: - c = p[x]; - r = r16(c); - p[x] = rgbtocolor16(r>0 ? r : 255, g16(c), b16(c)); + p[x] = 5000; break; case DYING: - c = p[+x]; - p[x] = rgbtocolor16(0, g16(c), b16(c)); + p[x] = 0; break; } } } - free(t); } -- cgit v1.2.1