File: critters.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 (137 lines) | stat: -rw-r--r-- 2,896 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
133
134
135
136
137
/*
 *  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 = 1100375479;
u_long options = BE_SFX2D|BEQ_NORANDOM;
u_long mode = OVERLAY;
char desc[] = "Critters demo";


struct Critter {
  Pixel_t color;     /* critter color */
  Point2d_t pos;    /* critter position */
};


static struct Critter *critters = NULL;
static u_short nb_critters = 0;

#define SAFE 50
#define STEP 50


/* da funky function */
static void
randomize(struct Critter *c)
{
  c->color = b_rand_int_range(1, 254);
  /* TODO better random start */
  c->pos.x = b_rand_int_range(MINX+SAFE, MAXX-SAFE);
  c->pos.y = b_rand_int_range(MINY+SAFE, MAXY-SAFE);
}


void
run(Context_t *ctx)
{
  Buffer8_t *dst = passive_buffer(ctx);
  u_short i;

  assert(ctx->input != NULL);
  if (ctx->input == NULL)
    return;

  Buffer8_clear(dst);

  pthread_mutex_lock(&ctx->input->mutex);

  /* foreach input */	
  for (i = 0; i  < nb_critters; i++) {
    /* compute deltas */
    float dx = ctx->input->data[A_LEFT ][i] * STEP;
    float dy = ctx->input->data[A_RIGHT][i] * STEP;
		
    /* compute new target */
    Point2d_t tmp;
    tmp.x = critters[i].pos.x + dx;
    tmp.y = critters[i].pos.y + dy;
		
    /* draw */
    draw_line(dst, critters[i].pos.x, critters[i].pos.y, tmp.x, tmp.y, critters[i].color);

    /* if a critter is out of screen, reset it */
    if ((tmp.x < MINX) || (tmp.y < MINY)
	|| (tmp.x > MAXX) || (tmp.y > MAXY)) {
      randomize(&critters[i]);
    } else {
      /* update position */
      critters[i].pos.x = tmp.x;
      critters[i].pos.y = tmp.y;
    }

  }
	
  pthread_mutex_unlock(&ctx->input->mutex);
}


void
randomize_all()
{
  int i;
  
  for (i = 0; i < nb_critters; i++)
    randomize(&critters[i]);
}


void
on_switch_on(__attribute__ ((unused)) Context_t *ctx)
{
  randomize_all();
}


void
create(Context_t *ctx)
{
  if (ctx->input == NULL) {
    options |= BEQ_DISABLED;
    return;
  }

  nb_critters = ctx->input->size;
	
  critters = xcalloc(nb_critters, sizeof(struct Critter));
  if (critters == NULL)
    xerror("pas de critter, trop dure la vie\n");

  randomize_all();
}


void
destroy(__attribute__ ((unused)) Context_t *ctx)
{
  if (critters != NULL)
    xfree(critters);
}