File: life.c

package info (click to toggle)
effectv 0.3.11-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 748 kB
  • ctags: 1,316
  • sloc: ansic: 12,575; makefile: 216; perl: 38
file content (132 lines) | stat: -rw-r--r-- 2,474 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * EffecTV - Realtime Digital Video Effector
 * Copyright (C) 2001-2006 FUKUCHI Kentaro
 *
 * LifeTV - Play John Horton Conway's `Life' game with video input.
 * Copyright (C) 2001-2002 FUKUCHI Kentaro
 *
 * This idea is stolen from Nobuyuki Matsushita's installation program of
 * ``HoloWall''. (See http://www.csl.sony.co.jp/person/matsu/index.html)
 */

#include <stdlib.h>
#include <string.h>
#include "EffecTV.h"
#include "utils.h"

static int start(void);
static int stop(void);
static int draw(RGB32 *src, RGB32 *dest);
static int event(SDL_Event *event);

static char *effectname = "LifeTV";
static int stat;
static unsigned char *field, *field1, *field2;

static void clear_field(void)
{
	memset(field1, 0, video_area);
}

effect *lifeRegister(void)
{
	effect *entry;
	
	sharedbuffer_reset();
	field = (unsigned char *)sharedbuffer_alloc(video_area*2);
	if(field == NULL) {
		return NULL;
	}

	entry = (effect *)malloc(sizeof(effect));
	if(entry == NULL) {
		return NULL;
	}
	
	entry->name = effectname;
	entry->start = start;
	entry->stop = stop;
	entry->draw = draw;
	entry->event = event;

	return entry;
}

static int start(void)
{
	image_set_threshold_y(40);
	field1 = field;
	field2 = field + video_area;
	clear_field();

	stat = 1;
	return 0;
}

static int stop(void)
{
	stat = 0;
	return 0;
}

static int draw(RGB32 *src, RGB32 *dest)
{
	int x, y;
	unsigned char *p, *q, v;
	unsigned char sum, sum1, sum2, sum3;
	int width;
	RGB32 pix;

	width = video_width;

	p = image_diff_filter(image_bgsubtract_update_y(src));
	for(x=0; x<video_area; x++) {
		field1[x] |= p[x];
	}

	p = field1 + 1;
	q = field2 + width + 1;
	dest += width + 1;
	src += width + 1;
/* each value of cell is 0 or 0xff. 0xff can be treated as -1, so
 * following equations treat each value as negative number. */
	for(y=1; y<video_height-1; y++) {
		sum1 = 0;
		sum2 = p[0] + p[width] + p[width*2];
		for(x=1; x<width-1; x++) {
			sum3 = p[1] + p[width+1] + p[width*2+1];
			sum = sum1 + sum2 + sum3;
			v = 0 - ((sum==0xfd)|((p[width]!=0)&(sum==0xfc)));
			*q++ = v;
			pix = (signed char)v;
//			pix = pix >> 8;
			*dest++ = pix | *src++;
			sum1 = sum2;
			sum2 = sum3;
			p++;
		}
		p += 2;
		q += 2;
		src += 2;
		dest += 2;
	}
	p = field1;
	field1 = field2;
	field2 = p;

	return 0;
}

static int event(SDL_Event *event)
{
	if(event->type == SDL_KEYDOWN) {
		switch(event->key.keysym.sym) {
		case SDLK_SPACE:
			clear_field();
			break;
		default:
			break;
		}
	}
	return 0;
}