comparison src/paranormal-ng/misc.c @ 2078:1fa3c8cd366a

paranormal-ng: a GL visualiser. lots to do, most stuff won't work, but hey, this will do cool stuff too soon
author William Pitcock <nenolod@atheme.org>
date Mon, 15 Oct 2007 06:20:13 -0500
parents
children f1b6f1b2cdb3
comparison
equal deleted inserted replaced
2077:e5b639ab62b0 2078:1fa3c8cd366a
1 /*
2 * paranormal: iterated pipeline-driven visualization plugin
3 * Copyright (c) 2006, 2007 William Pitcock <nenolod@dereferenced.org>
4 * Portions copyright (c) 2001 Jamie Gennis <jgennis@mindspring.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23 #include <stdio.h>
24
25 #include <glib.h>
26
27 #include "paranormal.h"
28 #include "actuators.h"
29 #include "pn_utils.h"
30
31 /* ******************** misc_floater ******************** */
32 static struct pn_actuator_option_desc misc_floater_opts[] =
33 {
34 { "value", "The colour value for the floater.",
35 OPT_TYPE_INT, { ival: 255 } },
36 { NULL }
37 };
38
39 typedef enum
40 {
41 float_up = 0x1,
42 float_down = 0x2,
43 float_left = 0x4,
44 float_right = 0x8,
45 } FloaterDirection;
46
47 struct floater_state_data
48 {
49 FloaterDirection dir;
50 gint x;
51 gint y;
52 };
53
54 static void
55 misc_floater_init(gpointer *data)
56 {
57 struct floater_state_data *opaque_data = g_new0(struct floater_state_data, 1);
58 *data = opaque_data;
59 opaque_data->x = rand() % pn_image_data->width;
60 opaque_data->y = rand() % pn_image_data->height;
61 opaque_data->dir = (FloaterDirection) rand() % 15; /* sum of all dir values */
62 }
63
64 static void
65 misc_floater_cleanup(gpointer data)
66 {
67 g_free(data);
68 }
69
70 /*
71 * This implementation isn't very great.
72 * Anyone want to improve it? :(
73 */
74 static void
75 misc_floater_exec(const struct pn_actuator_option *opts, gpointer data)
76 {
77 struct floater_state_data *opaque_data = (struct floater_state_data *) data;
78 guchar value = (opts[0].val.ival < 0 || opts[0].val.ival > 255) ? 255 : opts[0].val.ival;
79
80 /* determine the root coordinate first */
81 if (opaque_data->dir & float_up)
82 opaque_data->y -= 1;
83 if (opaque_data->dir & float_down)
84 opaque_data->y += 1;
85
86 if (opaque_data->dir & float_left)
87 opaque_data->x -= 1;
88 if (opaque_data->dir & float_right)
89 opaque_data->x += 1;
90
91 /* make sure we're within surface boundaries. segfaults suck, afterall. */
92 if (opaque_data->x + 1 <= pn_image_data->width &&
93 opaque_data->x - 1 >= 0 &&
94 opaque_data->y + 1 <= pn_image_data->height &&
95 opaque_data->y - 1 >= 0)
96 {
97 /* draw it. i could use a loop here, but i don't see much reason in it,
98 * so i don't think i will at this time. -nenolod
99 */
100 pn_image_data->surface[0][PN_IMG_INDEX(opaque_data->x, opaque_data->y)] = value;
101 pn_image_data->surface[0][PN_IMG_INDEX(opaque_data->x + 1, opaque_data->y)] = value;
102 pn_image_data->surface[0][PN_IMG_INDEX(opaque_data->x - 1, opaque_data->y)] = value;
103 pn_image_data->surface[0][PN_IMG_INDEX(opaque_data->x, opaque_data->y + 1)] = value;
104 pn_image_data->surface[0][PN_IMG_INDEX(opaque_data->x, opaque_data->y - 1)] = value;
105 }
106
107 /* check if we need to change direction yet, and if so, do so. */
108 if (pn_new_beat == TRUE)
109 opaque_data->dir = (FloaterDirection) rand() % 15; /* sum of all dir values */
110
111 /* now adjust the direction so we stay in boundary */
112 if (opaque_data->x - 1 <= 0 && opaque_data->dir & float_left)
113 {
114 opaque_data->dir &= ~float_left;
115 opaque_data->dir |= float_right;
116 }
117
118 if (opaque_data->x + 1 >= pn_image_data->width && opaque_data->dir & float_right)
119 {
120 opaque_data->dir &= ~float_right;
121 opaque_data->dir |= float_left;
122 }
123
124 if (opaque_data->y - 1 <= 0 && opaque_data->dir & float_up)
125 {
126 opaque_data->dir &= ~float_up;
127 opaque_data->dir |= float_down;
128 }
129
130 if (opaque_data->y + 1 >= pn_image_data->height && opaque_data->dir & float_down)
131 {
132 opaque_data->dir &= ~float_down;
133 opaque_data->dir |= float_up;
134 }
135 }
136
137 struct pn_actuator_desc builtin_misc_floater =
138 {
139 "misc_floater",
140 "Floating Particle",
141 "A floating particle.",
142 0, misc_floater_opts,
143 misc_floater_init, misc_floater_cleanup, misc_floater_exec
144 };
145