1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@videolan.org>
8 * code from projectM http://xmms-projectm.sourceforge.net
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
30 #include "param_types.h"
33 #include "expr_types.h"
36 #include "splaytree_types.h"
37 #include "splaytree.h"
38 #include "tree_types.h"
40 #include "per_frame_eqn_types.h"
41 #include "per_frame_eqn.h"
43 #include "init_cond_types.h"
44 #include "init_cond.h"
46 #include "preset_types.h"
48 #include "custom_shape_types.h"
49 #include "custom_shape.h"
51 #include "init_cond_types.h"
52 #include "init_cond.h"
54 custom_shape_t * interface_shape = NULL;
55 int cwave_interface_id = 0;
56 extern preset_t * active_preset;
57 inline void eval_custom_shape_init_conds(custom_shape_t * custom_shape);
58 void load_unspec_init_cond_shape(param_t * param);
60 void destroy_param_db_tree_shape(splaytree_t * tree);
61 void destroy_per_frame_eqn_tree_shape(splaytree_t * tree);
62 void destroy_per_frame_init_eqn_tree_shape(splaytree_t * tree);
63 void destroy_init_cond_tree_shape(splaytree_t * tree);
65 custom_shape_t * new_custom_shape(int id) {
67 custom_shape_t * custom_shape;
70 if ((custom_shape = (custom_shape_t*)malloc(sizeof(custom_shape_t))) == NULL)
73 custom_shape->id = id;
74 custom_shape->per_frame_count = 0;
75 custom_shape->per_frame_eqn_string_index = 0;
76 custom_shape->per_frame_init_eqn_string_index = 0;
78 /* Initialize tree data structures */
80 if ((custom_shape->param_tree =
81 create_splaytree(compare_string, copy_string, free_string)) == NULL) {
82 free_custom_shape(custom_shape);
86 if ((custom_shape->per_frame_eqn_tree =
87 create_splaytree(compare_int, copy_int, free_int)) == NULL) {
88 free_custom_shape(custom_shape);
92 if ((custom_shape->init_cond_tree =
93 create_splaytree(compare_string, copy_string, free_string)) == NULL) {
94 free_custom_shape(custom_shape);
98 if ((custom_shape->per_frame_init_eqn_tree =
99 create_splaytree(compare_string, copy_string, free_string)) == NULL) {
100 free_custom_shape(custom_shape);
104 /* Start: Load custom shape parameters */
106 if ((param = new_param_double("r", P_FLAG_NONE, &custom_shape->r, NULL, 1.0, 0.0, .5)) == NULL) {
107 free_custom_shape(custom_shape);
111 if (insert_param(param, custom_shape->param_tree) < 0) {
112 free_custom_shape(custom_shape);
116 if ((param = new_param_double("g", P_FLAG_NONE, &custom_shape->g, NULL, 1.0, 0.0, .5)) == NULL){
117 free_custom_shape(custom_shape);
121 if (insert_param(param, custom_shape->param_tree) < 0) {
122 free_custom_shape(custom_shape);
126 if ((param = new_param_double("b", P_FLAG_NONE, &custom_shape->b, NULL, 1.0, 0.0, .5)) == NULL){
127 free_custom_shape(custom_shape);
131 if (insert_param(param, custom_shape->param_tree) < 0) {
132 free_custom_shape(custom_shape);
136 if ((param = new_param_double("a", P_FLAG_NONE, &custom_shape->a, NULL, 1.0, 0.0, .5)) == NULL){
137 free_custom_shape(custom_shape);
141 if (insert_param(param, custom_shape->param_tree) < 0) {
142 free_custom_shape(custom_shape);
146 if ((param = new_param_double("border_r", P_FLAG_NONE, &custom_shape->border_r, NULL, 1.0, 0.0, .5)) == NULL) {
147 free_custom_shape(custom_shape);
151 if (insert_param(param, custom_shape->param_tree) < 0) {
152 free_custom_shape(custom_shape);
156 if ((param = new_param_double("border_g", P_FLAG_NONE, &custom_shape->border_g, NULL, 1.0, 0.0, .5)) == NULL){
157 free_custom_shape(custom_shape);
161 if (insert_param(param, custom_shape->param_tree) < 0) {
162 free_custom_shape(custom_shape);
166 if ((param = new_param_double("border_b", P_FLAG_NONE, &custom_shape->border_b, NULL, 1.0, 0.0, .5)) == NULL){
167 free_custom_shape(custom_shape);
171 if (insert_param(param, custom_shape->param_tree) < 0) {
172 free_custom_shape(custom_shape);
176 if ((param = new_param_double("border_a", P_FLAG_NONE, &custom_shape->border_a, NULL, 1.0, 0.0, .5)) == NULL){
177 free_custom_shape(custom_shape);
181 if (insert_param(param, custom_shape->param_tree) < 0) {
182 free_custom_shape(custom_shape);
186 if ((param = new_param_double("r2", P_FLAG_NONE, &custom_shape->r2, NULL, 1.0, 0.0, .5)) == NULL) {
187 free_custom_shape(custom_shape);
191 if (insert_param(param, custom_shape->param_tree) < 0) {
192 free_custom_shape(custom_shape);
196 if ((param = new_param_double("g2", P_FLAG_NONE, &custom_shape->g2, NULL, 1.0, 0.0, .5)) == NULL){
197 free_custom_shape(custom_shape);
201 if (insert_param(param, custom_shape->param_tree) < 0) {
202 free_custom_shape(custom_shape);
206 if ((param = new_param_double("b2", P_FLAG_NONE, &custom_shape->b2, NULL, 1.0, 0.0, .5)) == NULL){
207 free_custom_shape(custom_shape);
211 if (insert_param(param, custom_shape->param_tree) < 0) {
212 free_custom_shape(custom_shape);
216 if ((param = new_param_double("a2", P_FLAG_NONE, &custom_shape->a2, NULL, 1.0, 0.0, .5)) == NULL){
217 free_custom_shape(custom_shape);
221 if (insert_param(param, custom_shape->param_tree) < 0) {
222 free_custom_shape(custom_shape);
226 if ((param = new_param_double("x", P_FLAG_NONE, &custom_shape->x, NULL, 1.0, 0.0, .5)) == NULL) {
227 free_custom_shape(custom_shape);
231 if (insert_param(param, custom_shape->param_tree) < 0) {
232 free_custom_shape(custom_shape);
236 if ((param = new_param_double("y", P_FLAG_NONE, &custom_shape->y, NULL, 1.0, 0.0, .5)) == NULL) {
237 free_custom_shape(custom_shape);
241 if (insert_param(param, custom_shape->param_tree) < 0) {
242 free_custom_shape(custom_shape);
246 if ((param = new_param_bool("thickOutline", P_FLAG_NONE, &custom_shape->thickOutline, 1, 0, 0)) == NULL) {
247 free_custom_shape(custom_shape);
251 if (insert_param(param, custom_shape->param_tree) < 0) {
252 free_custom_shape(custom_shape);
256 if ((param = new_param_bool("enabled", P_FLAG_NONE, &custom_shape->enabled, 1, 0, 0)) == NULL) {
257 free_custom_shape(custom_shape);
261 if (insert_param(param, custom_shape->param_tree) < 0) {
262 free_custom_shape(custom_shape);
266 if ((param = new_param_int("sides", P_FLAG_NONE, &custom_shape->sides, 100, 3, 3)) == NULL) {
267 free_custom_shape(custom_shape);
271 if (insert_param(param, custom_shape->param_tree) < 0) {
272 free_custom_shape(custom_shape);
276 if ((param = new_param_bool("additive", P_FLAG_NONE, &custom_shape->additive, 1, 0, 0)) == NULL) {
277 free_custom_shape(custom_shape);
281 if (insert_param(param, custom_shape->param_tree) < 0) {
282 free_custom_shape(custom_shape);
286 if ((param = new_param_bool("textured", P_FLAG_NONE, &custom_shape->textured, 1, 0, 0)) == NULL) {
287 free_custom_shape(custom_shape);
291 if (insert_param(param, custom_shape->param_tree) < 0) {
292 free_custom_shape(custom_shape);
296 if ((param = new_param_double("rad", P_FLAG_NONE, &custom_shape->rad, NULL, MAX_DOUBLE_SIZE, 0, 0.0)) == NULL) {
297 free_custom_shape(custom_shape);
301 if (insert_param(param, custom_shape->param_tree) < 0) {
302 free_custom_shape(custom_shape);
306 if ((param = new_param_double("ang", P_FLAG_NONE, &custom_shape->ang, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
307 free_custom_shape(custom_shape);
311 if (insert_param(param, custom_shape->param_tree) < 0) {
312 free_custom_shape(custom_shape);
316 if ((param = new_param_double("tex_zoom", P_FLAG_NONE, &custom_shape->tex_zoom, NULL, MAX_DOUBLE_SIZE, .00000000001, 0.0)) == NULL) {
317 free_custom_shape(custom_shape);
321 if (insert_param(param, custom_shape->param_tree) < 0) {
322 free_custom_shape(custom_shape);
326 if ((param = new_param_double("tex_ang", P_FLAG_NONE, &custom_shape->tex_ang, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
327 free_custom_shape(custom_shape);
331 if (insert_param(param, custom_shape->param_tree) < 0) {
332 free_custom_shape(custom_shape);
335 if ((param = new_param_double("t1", P_FLAG_TVAR, &custom_shape->t1, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
336 free_custom_shape(custom_shape);
340 if (insert_param(param, custom_shape->param_tree) < 0) {
341 free_custom_shape(custom_shape);
345 if ((param = new_param_double("t2", P_FLAG_TVAR, &custom_shape->t2, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
346 free_custom_shape(custom_shape);
350 if (insert_param(param, custom_shape->param_tree) < 0) {
351 free_custom_shape(custom_shape);
355 if ((param = new_param_double("t3", P_FLAG_TVAR, &custom_shape->t3, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
356 free_custom_shape(custom_shape);
360 if (insert_param(param, custom_shape->param_tree) < 0) {
361 free_custom_shape(custom_shape);
364 if ((param = new_param_double("t4", P_FLAG_TVAR, &custom_shape->t4, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
365 free_custom_shape(custom_shape);
369 if (insert_param(param, custom_shape->param_tree) < 0) {
370 free_custom_shape(custom_shape);
373 if ((param = new_param_double("t5", P_FLAG_TVAR, &custom_shape->t5, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
374 free_custom_shape(custom_shape);
378 if (insert_param(param, custom_shape->param_tree) < 0) {
379 free_custom_shape(custom_shape);
382 if ((param = new_param_double("t6", P_FLAG_TVAR, &custom_shape->t6, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
383 free_custom_shape(custom_shape);
387 if (insert_param(param, custom_shape->param_tree) < 0) {
388 free_custom_shape(custom_shape);
391 if ((param = new_param_double("t7", P_FLAG_TVAR, &custom_shape->t7, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
392 free_custom_shape(custom_shape);
396 if (insert_param(param, custom_shape->param_tree) < 0) {
397 free_custom_shape(custom_shape);
401 if ((param = new_param_double("t8", P_FLAG_TVAR, &custom_shape->t8, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
402 free_custom_shape(custom_shape);
406 if (insert_param(param, custom_shape->param_tree) < 0) {
407 free_custom_shape(custom_shape);
411 /* End of parameter loading. Note that the read only parameters associated
412 with custom shapes (ie, sample) are global variables, and not specific to
413 the custom shape datastructure. */
421 void destroy_per_frame_init_eqn_tree_shape(splaytree_t * tree) {
426 splay_traverse(free_init_cond, tree);
427 destroy_splaytree(tree);
433 void destroy_init_cond_tree_shape(splaytree_t * tree) {
438 splay_traverse(free_init_cond, tree);
439 destroy_splaytree(tree);
443 void destroy_per_frame_eqn_tree_shape(splaytree_t * tree) {
449 splay_traverse(free_per_frame_eqn, tree);
450 destroy_splaytree(tree);
455 void destroy_param_db_tree_shape(splaytree_t * tree) {
460 splay_traverse(free_param, tree);
461 destroy_splaytree(tree);
465 /* Frees a custom shape form object */
466 void free_custom_shape(custom_shape_t * custom_shape) {
468 if (custom_shape == NULL)
471 if (custom_shape->param_tree == NULL)
474 destroy_per_frame_eqn_tree_shape(custom_shape->per_frame_eqn_tree);
475 destroy_init_cond_tree_shape(custom_shape->init_cond_tree);
476 destroy_param_db_tree_shape(custom_shape->param_tree);
477 destroy_per_frame_init_eqn_tree_shape(custom_shape->per_frame_init_eqn_tree);
486 custom_shape_t * find_custom_shape(int id, preset_t * preset, int create_flag) {
488 custom_shape_t * custom_shape = NULL;
493 if ((custom_shape = splay_find(&id, preset->custom_shape_tree)) == NULL) {
495 if (CUSTOM_SHAPE_DEBUG) { printf("find_custom_shape: creating custom shape (id = %d)...", id);fflush(stdout);}
497 if (create_flag == FALSE) {
498 if (CUSTOM_SHAPE_DEBUG) printf("you specified not to (create flag = false), returning null\n");
502 if ((custom_shape = new_custom_shape(id)) == NULL) {
503 if (CUSTOM_SHAPE_DEBUG) printf("failed...out of memory?\n");
507 if (CUSTOM_SHAPE_DEBUG) { printf("success.Inserting..."); fflush(stdout);}
509 if (splay_insert(custom_shape, &custom_shape->id, preset->custom_shape_tree) < 0) {
510 if (CUSTOM_SHAPE_DEBUG) printf("failed, probably a duplicated!!\n");
511 free_custom_shape(custom_shape);
515 if (CUSTOM_SHAPE_DEBUG) printf("done.\n");
521 inline void evalCustomShapeInitConditions() {
522 splay_traverse(eval_custom_shape_init_conds, active_preset->custom_shape_tree);
526 inline void eval_custom_shape_init_conds(custom_shape_t * custom_shape) {
527 splay_traverse(eval_init_cond, custom_shape->init_cond_tree);
528 splay_traverse(eval_init_cond, custom_shape->per_frame_init_eqn_tree);
532 void load_unspecified_init_conds_shape(custom_shape_t * custom_shape) {
534 interface_shape = custom_shape;
535 splay_traverse(load_unspec_init_cond_shape, interface_shape->param_tree);
536 interface_shape = NULL;
540 void load_unspec_init_cond_shape(param_t * param) {
542 init_cond_t * init_cond;
545 /* Don't count read only parameters as initial conditions */
546 if (param->flags & P_FLAG_READONLY)
548 if (param->flags & P_FLAG_QVAR)
550 if (param->flags & P_FLAG_TVAR)
552 if (param->flags & P_FLAG_USERDEF)
555 /* If initial condition was not defined by the preset file, force a default one
556 with the following code */
557 if ((init_cond = splay_find(param->name, interface_shape->init_cond_tree)) == NULL) {
559 /* Make sure initial condition does not exist in the set of per frame initial equations */
560 if ((init_cond = splay_find(param->name, interface_shape->per_frame_init_eqn_tree)) != NULL)
563 if (param->type == P_TYPE_BOOL)
564 init_val.bool_val = 0;
566 else if (param->type == P_TYPE_INT)
567 init_val.int_val = *(int*)param->engine_val;
569 else if (param->type == P_TYPE_DOUBLE)
570 init_val.double_val = *(double*)param->engine_val;
572 //printf("%s\n", param->name);
573 /* Create new initial condition */
574 if ((init_cond = new_init_cond(param, init_val)) == NULL)
577 /* Insert the initial condition into this presets tree */
578 if (splay_insert(init_cond, init_cond->param->name, interface_shape->init_cond_tree) < 0) {
579 free_init_cond(init_cond);
588 /* Interface function. Makes another custom shape the current
589 concern for per frame / point equations */
590 inline custom_shape_t * nextCustomShape() {
592 if ((interface_shape = splay_find(&cwave_interface_id, active_preset->custom_shape_tree)) == NULL) {
593 cwave_interface_id = 0;
597 cwave_interface_id++;
599 /* Evaluate all per frame equations associated with this shape */
600 splay_traverse(eval_per_frame_eqn, interface_shape->per_frame_eqn_tree);
601 return interface_shape;