File: faders.c

package info (click to toggle)
lebiniou 3.18-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,396 kB
  • sloc: ansic: 20,369; sh: 4,169; makefile: 834; awk: 194
file content (129 lines) | stat: -rw-r--r-- 2,627 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
/*
 *  Copyright 1994-2012 Olivier Girondel
 *
 *  This file is part of lebiniou.
 *
 *  lebiniou is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  lebiniou is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with lebiniou. If not, see <http://www.gnu.org/licenses/>.
 */

#include "context.h"


u_long id = 1180546912;
u_long options = BE_GFX|BEQ_FIRST;
u_long mode = OVERLAY;
char desc[] = "Cellular automaton";

/*
 * Adapted from:
 * http://www.fourmilab.ch/cellab/manual/rules.html#Faders
 */

static Buffer8_t *game = NULL, *game2 = NULL;


static void
randomize(Buffer8_t *buff)
{
  Pixel_t *p = buff->buffer;

  for ( ; p < buff->buffer + BUFFSIZE*sizeof(Pixel_t); p++)
    *p = b_rand_boolean() ? 0 : 255;
}


void
create(__attribute__ ((unused)) Context_t *ctx)
{
  game = Buffer8_new();
  game2 = Buffer8_new();
  randomize(game);
}


void
destroy(__attribute__ ((unused)) Context_t *ctx)
{
  Buffer8_delete(game);
  Buffer8_delete(game2);
}


void
on_switch_on(__attribute__ ((unused)) Context_t *ctx)
{
  randomize(game);
  Buffer8_average(game, active_buffer(ctx));
}


#define IS_FIRING(X, Y) (get_pixel_nc(game, X, Y) == 255)

static inline u_char
firing(const int x, const int y)
{
  u_short count = 0;

  count += IS_FIRING(x-1, y-1);
  count += IS_FIRING(x,   y-1);
  count += IS_FIRING(x+1, y-1);
  count += IS_FIRING(x-1, y);
  count += IS_FIRING(x+1, y);
  count += IS_FIRING(x-1, y+1);
  count += IS_FIRING(x,   y+1);
  count += IS_FIRING(x+1, y+1);

  return (count == 2);
}


void
run(Context_t *ctx)
{
  int i, j;
  Buffer8_t *dst = passive_buffer(ctx);
  Buffer8_t *tmp;

  Buffer8_clear(dst);

  for (j = 1; j < MAXY; j++)
    for (i = 1; i < MAXX; i++) {
      const Pixel_t old = get_pixel_nc(game, i, j);
      Pixel_t new;
      
      switch (old) {
      case 0:
	/* dead cell, becomes alive ? */
	new = firing(i, j) ? 255 : 0;
	break;
	
      case 255:
	/* firing cell, keeps firing ? */
	new = firing(i, j) ? 255 : 254;
	break;
	
      default:
	/* cell decays */
	new = old - 2;
	break;
      }
      
      set_pixel_nc(game2, i, j, new);
      set_pixel_nc(dst, i, j, new);
    }

  tmp = game;
  game = game2;
  game2 = tmp;
}