Debian Codegraph @

plugins/actor/oinksie/gfx-palette.c

raw
1 /* Libvisual-plugins - Standard plugins for libvisual
2 *
3 * Copyright (C) 2002, 2003, 2004, 2005, 2006 Dennis Smit <[email protected]>
4 *
5 * Authors: Dennis Smit <[email protected]>
6 *
7 * $Id: gfx-palette.c,v 1.6 2006/01/22 13:25:26 synap Exp $
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include <math.h>
25 #include <string.h>
26
27 #include <libvisual/libvisual.h>
28
29 #include "common.h"
30 #include "misc.h"
31 #include "screen.h"
32 #include "gfx-palette.h"
33
34 void _oink_gfx_palette_init (OinksiePrivate *priv)
35 {
36 priv->pal_startup = 1;
37
38 priv->pal_data.fade_start = 1;
39 priv->pal_data.fade_steps = 100;
40 priv->pal_data.fade_poststeps = priv->pal_data.fade_steps;
41 priv->pal_data.fade_poststop = 1;
42 }
43
44 void _oink_gfx_palette_save_old (OinksiePrivate *priv)
45 {
46 /* If we haven't set up an fade yet, don't save the next old.
47 * this protects us from more event handles per screen draw
48 */
49 if (priv->pal_data.pal_new == 1 && priv->pal_data.fade_stepsdone == priv->pal_data.fade_steps)
50 visual_palette_copy (&priv->pal_old, &priv->pal_cur);
51
52 priv->pal_data.fade_start = 1;
53 }
54
55 void _oink_gfx_palette_transform (OinksiePrivate *priv)
56 {
57 int i;
58
59 if (priv->pal_data.fade_start == 1)
60 {
61 priv->pal_data.fade_stepsdone = 0;
62
63 for (i = 0; i < 256; i++)
64 {
65 priv->pal_fades[i].r = (float) (priv->pal_cur.colors[i].r - priv->pal_old.colors[i].r) / (float) priv->pal_data.fade_steps;
66 priv->pal_fades[i].g = (float) (priv->pal_cur.colors[i].g - priv->pal_old.colors[i].g) / (float) priv->pal_data.fade_steps;
67 priv->pal_fades[i].b = (float) (priv->pal_cur.colors[i].b - priv->pal_old.colors[i].b) / (float) priv->pal_data.fade_steps;
68
69 priv->pal_fades[i].r_cur = priv->pal_old.colors[i].r;
70 priv->pal_fades[i].g_cur = priv->pal_old.colors[i].g;
71 priv->pal_fades[i].b_cur = priv->pal_old.colors[i].b;
72 }
73
74 priv->pal_data.fade_start = 0;
75 }
76
77 for (i = 0; i < 256; i++)
78 {
79 priv->pal_fades[i].r_cur += priv->pal_fades[i].r;
80 priv->pal_fades[i].g_cur += priv->pal_fades[i].g;
81 priv->pal_fades[i].b_cur += priv->pal_fades[i].b;
82
83 priv->pal_old.colors[i].r = priv->pal_fades[i].r_cur;
84 priv->pal_old.colors[i].g = priv->pal_fades[i].g_cur;
85 priv->pal_old.colors[i].b = priv->pal_fades[i].b_cur;
86 }
87
88 priv->pal_data.fade_stepsdone++;
89
90 if (priv->pal_data.fade_stepsdone >= priv->pal_data.fade_poststeps)
91 {
92 visual_palette_copy (&priv->pal_cur, &priv->pal_old);
93
94 priv->pal_data.pal_new = 0;
95 priv->pal_data.fade_start = 1;
96 priv->pal_startup = 0;
97 }
98 }
99
100 void _oink_gfx_palette_build (OinksiePrivate *priv, uint8_t funky)
101 {
102 /* save old palette for transistion */
103 _oink_gfx_palette_save_old (priv);
104 _oink_gfx_palette_build_gradient (priv, funky);
105
106 /* FIXME, changing fadesteps can fuck this up */
107 if (priv->pal_data.fade_poststop == 1 && priv->pal_startup == 0) {
108 switch (visual_random_context_int_range (priv->rcontext, 0, 5)) {
109 case 0:
110 case 1:
111 priv->pal_data.fade_poststeps = visual_random_context_int_range (priv->rcontext, 60, priv->pal_data.fade_steps);
112 break;
113
114 case 2:
115 priv->pal_data.fade_poststeps = visual_random_context_int_range (priv->rcontext, 40, priv->pal_data.fade_steps / 2);
116 break;
117
118 case 3:
119 priv->pal_data.fade_poststeps = visual_random_context_int_range (priv->rcontext, 20, priv->pal_data.fade_steps / 3);
120 break;
121 case 4:
122 priv->pal_data.fade_poststeps = visual_random_context_int_range (priv->rcontext, 80, priv->pal_data.fade_steps);
123 break;
124
125 default:
126 priv->pal_data.fade_poststeps = priv->pal_data.fade_steps;
127 break;
128 }
129
130 }
131
132 priv->pal_data.pal_new = 1;
133 }
134
135 void _oink_gfx_palette_color (OinksiePrivate *priv, int color, int red, int green, int blue)
136 {
137 priv->pal_cur.colors[color].r = red << 2;
138 priv->pal_cur.colors[color].g = green << 2;
139 priv->pal_cur.colors[color].b = blue << 2;
140 }
141
142 uint8_t _oink_gfx_palette_gradient_gen (OinksiePrivate *priv, uint8_t i, int mode)
143 {
144 static const float __magic = 2.0 * PI/256;
145
146 switch (mode)
147 {
148 case 0:
149 return (i * i * i) >> 16;
150 break;
151
152 case 1:
153 return (i * i) >> 8;
154 break;
155
156 case 2:
157 return (uint8_t) i;
158 break;
159
160 case 3:
161 return (uint8_t) (128 * fabs (sin ( __magic * i )));
162 break;
163
164 case 4:
165 return 0;
166 break;
167
168 default:
169 break;
170 }
171
172 return 0;
173 }
174
175 void _oink_gfx_palette_build_gradient (OinksiePrivate *priv, uint8_t funky)
176 {
177 int i = 0;
178 int j = 0;
179 int k = 0;
180 int l = 0;
181
182 if (funky == TRUE)
183 i = 5;
184 else
185 i = 3;
186
187 do
188 {
189 j = visual_random_context_int_range (priv->rcontext, 0, i - 1);
190 k = visual_random_context_int_range (priv->rcontext, 0, i - 1);
191 l = visual_random_context_int_range (priv->rcontext, 0, i - 1);
192
193 } while (j == k || j == l || l == k);
194
195 for( i = 0; i <= 255; i++)
196 {
197 priv->pal_cur.colors[i].r = _oink_gfx_palette_gradient_gen (priv, i, j);
198 priv->pal_cur.colors[i].g = _oink_gfx_palette_gradient_gen (priv, i, k);
199 priv->pal_cur.colors[i].b = _oink_gfx_palette_gradient_gen (priv, i, l);
200 }
201 }
202