aboutsummaryrefslogtreecommitdiff
path: root/Plugins/gol.c
diff options
context:
space:
mode:
Diffstat (limited to 'Plugins/gol.c')
-rw-r--r--Plugins/gol.c84
1 files changed, 50 insertions, 34 deletions
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 <stdlib.h>
#include <stdio.h>
#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; y<f.height-3; y++)
@@ -51,17 +71,17 @@ void perform_effect(struct frame f, struct args a)
p = scanline16(f, y);
for(x=2; x<f.width-3; x++)
{
- if(r16(p[x])>0)
+ 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);
}