File: faders.c

package info (click to toggle)
lebiniou 3.67.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,500 kB
  • sloc: ansic: 28,670; makefile: 1,276; sh: 602; awk: 432; xml: 261; javascript: 23
file content (133 lines) | stat: -rw-r--r-- 2,698 bytes parent folder | download | duplicates (3)
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
133
/*
 *  Copyright 1994-2022 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"


uint32_t version = 0;
uint32_t options = BO_GFX | BO_FIRST | BO_SCHEMES;
enum LayerMode mode = LM_OVERLAY;
char desc[] = "Cellular automaton";
char dname[] = "Faders";

/*
 * 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;
  }
}


int8_t
create(Context_t *ctx)
{
  game = Buffer8_new();
  game2 = Buffer8_new();
  randomize(game);

  return 1;
}


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


void
on_switch_on(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 uint8_t
firing(const int x, const int y)
{
  uint16_t 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;
}