File: infinity.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 (244 lines) | stat: -rw-r--r-- 5,764 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 *  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"
#include "infinity.h"


uint32_t version = 0;
uint32_t options = BO_DISPLACE | BO_WARP;
char dname[] = "Infinity";
char desc[] = "Infinity effect";


/* Infinity plugin port.
 * Original source code has been heavily modified, to take only
 * the vector fields. So modified it's nearly a rewrite.
 * Changes have also been made to reflect style(9).
 * See the original infinity plugin source code for exact details:
 * https://github.com/dprotti/infinity-plugin
 */
#define NB_FCT 6

static Shuffler_t *shuffler = NULL;
static VectorField_t *vf = NULL;
static Timer_t *timer = NULL;

enum Mode { MODE_SELECTED = 0, MODE_RANDOM, MODE_NB } Mode_e;
const char *mode_list[MODE_NB] = { "Selected", "Random" };

/* parameters */
static enum Mode mode = MODE_RANDOM;
static int effect = 0;
static int delay = 5;


json_t *
get_parameters(const uint8_t fetch_all)
{
  json_t *params = json_object();

  plugin_parameters_add_string_list(params, BPP_MODE, MODE_NB, mode_list, mode, MODE_NB-1, "Mode");
  plugin_parameters_add_int(params, BPP_EFFECT, effect, 0, NB_FCT-1, 1, "Effect");
  plugin_parameters_add_int(params, BPP_DELAY, delay, 1, 60, 1, "Delay");

  return params;
}


void
set_parameters(const Context_t *ctx, const json_t *in_parameters)
{
  plugin_parameter_parse_int_range(in_parameters, BPP_EFFECT, &effect);
  if (plugin_parameter_parse_int_range(in_parameters, BPP_DELAY, &delay) & PLUGIN_PARAMETER_CHANGED) {
    Timer_start(timer);
  }

  if (plugin_parameter_parse_string_list_as_int_range(in_parameters, BPP_MODE, MODE_NB, mode_list, (int *)&mode) & PLUGIN_PARAMETER_CHANGED) {
    if (mode == MODE_RANDOM) {
      effect = Shuffler_get(shuffler);
    }
  }
}


json_t *
parameters(const Context_t *ctx, const json_t *in_parameters, const uint8_t fetch_all)
{
  if (NULL != in_parameters) {
    set_parameters(ctx, in_parameters);
  }

  return get_parameters(fetch_all);
}


static t_complex
fct(t_complex a, guint32 n, gint32 p1, gint32 p2)   /* p1 et p2:0-4 */
{
  t_complex b;
  gfloat fact;
  gfloat an;
  gfloat circle_size;
  gfloat speed;
  gfloat co,si;

  a.x -= HWIDTH;
  a.y -= HHEIGHT;

  switch (n) {
    case 0:
      an = 0.025*(p1-2)+0.002;
      co = cosf(an);
      si = sinf(an);
      circle_size = HEIGHT*0.25;
      speed = (gfloat)2000+p2*500;
      b.x = (co*a.x-si*a.y);
      b.y = (si*a.x+co*a.y);
      fact = -(sqrtf(b.x*b.x+b.y*b.y)-circle_size)/speed+1;
      b.x = (b.x*fact);
      b.y = (b.y*fact);
      break;

    case 1:
      an = 0.015*(p1-2)+0.002;
      co = cosf(an);
      si = sinf(an);
      circle_size = HEIGHT*0.45;
      speed = (gfloat)4000+p2*1000;
      b.x = (co*a.x-si*a.y);
      b.y = (si*a.x+co*a.y);
      fact = (sqrtf(b.x*b.x+b.y*b.y)-circle_size)/speed+1;
      b.x = (b.x*fact);
      b.y = (b.y*fact);
      break;

    case 2:
      an = 0.002;
      co = cosf(an);
      si = sinf(an);
      circle_size = HEIGHT*0.25;
      speed = (gfloat)400+p2*100;
      b.x = (co*a.x-si*a.y);
      b.y = (si*a.x+co*a.y);
      fact = -(sqrtf(b.x*b.x+b.y*b.y)-circle_size)/speed+1;
      b.x = (b.x*fact);
      b.y = (b.y*fact);
      break;

    case 3:
      an = (sinf(sqrtf(a.x*a.x+a.y*a.y)/20)/20)+0.002;
      co = cosf(an);
      si = sinf(an);
      circle_size = HEIGHT*0.25;
      speed = (gfloat)4000;
      b.x = (co*a.x-si*a.y);
      b.y = (si*a.x+co*a.y);
      fact = -(sqrtf(b.x*b.x+b.y*b.y)-circle_size)/speed+1;
      b.x = (b.x*fact);
      b.y = (b.y*fact);
      break;

    case 4:
      an = 0.002;
      co = cosf(an);
      si = sinf(an);
      circle_size = HEIGHT*0.25;
      speed = sinf(sqrtf(a.x*a.x+a.y*a.y)/5)*3000+4000;
      b.x = (co*a.x-si*a.y);
      b.y = (si*a.x+co*a.y);
      fact = -(sqrtf(b.x*b.x+b.y*b.y)-circle_size)/speed+1;
      b.x = (b.x*fact);
      b.y = (b.y*fact);
      break;

    case 5:
      an = 0.002;
      co = cosf(an);
      si = sinf(an);
      circle_size = HEIGHT*0.25;
      fact = 1+cosf(atanf(a.x/(a.y+0.00001))*6)*0.02;
      b.x = (co*a.x-si*a.y);
      b.y = (si*a.x+co*a.y);
      b.x = (b.x*fact);
      b.y = (b.y*fact);
      break;

    default:
      b.x = 0.0;
      b.y = 0.0;
  }

  b.x += HWIDTH;
  b.y += HHEIGHT;

  /* because infinity access pixels at (b.x + 1, b.y + 1) */
  b.x = MIN( MAX(b.x, 0), MAXX-1);
  b.y = MIN( MAX(b.y, 0), MAXY-1);

  return b;
}


int8_t
create(Context_t *ctx)
{
  vf = VectorField_new(NB_FCT, &fct);
  timer = Timer_new("infinity");
  shuffler = Shuffler_new(NB_FCT);

  mode = MODE_RANDOM;
  effect = 0;

  return 1;
}


void
destroy(Context_t *ctx)
{
  VectorField_delete(vf);
  Timer_delete(timer);
  Shuffler_delete(shuffler);
}


void
on_switch_on(Context_t *ctx)
{
  delay = b_rand_uint32_range(5, 21);
  effect = Shuffler_get(shuffler);
  if (b_rand_boolean()) {
    mode = MODE_SELECTED;
  } else {
    mode = MODE_RANDOM;
    Timer_start(timer);
  }
}


void
run(Context_t *ctx)
{
  VectorField_run(vf, ctx, effect);

  if ((mode == MODE_RANDOM) && (Timer_elapsed(timer) > delay)) {
    on_switch_on(ctx);
  }
}