00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include "common.h"
00032 #include "fatal.h"
00033
00034 #include "param_types.h"
00035 #include "func_types.h"
00036 #include "expr_types.h"
00037 #include "eval.h"
00038 #include "engine_vars.h"
00039 #include "builtin_funcs.h"
00040 #define EVAL_ERROR -1
00041
00042
00043 infix_op_t * infix_add, * infix_minus, * infix_div, * infix_mult,
00044 * infix_or, * infix_and, * infix_mod, * infix_negative, * infix_positive;
00045 int mesh_i=-1, mesh_j=-1;
00046
00047 static inline double eval_tree_expr(tree_expr_t * tree_expr);
00048 static inline double eval_prefun_expr(prefun_expr_t * prefun_expr);
00049 static inline double eval_val_expr(val_expr_t * val_expr);
00050
00051
00052 inline double eval_gen_expr(gen_expr_t * gen_expr) {
00053 double l;
00054
00055 if (gen_expr == NULL)
00056 return 0;
00057
00058 switch(gen_expr->type) {
00059 case VAL_T:
00060 return eval_val_expr(gen_expr->item);
00061 case PREFUN_T:
00062 l = eval_prefun_expr(gen_expr->item);
00063
00064 return l;
00065 case TREE_T:
00066 return eval_tree_expr(gen_expr->item);
00067 default:
00068 #ifdef EVAL_DEBUG
00069 printf("eval_gen_expr: general expression matched no cases!\n");
00070 #endif
00071 return EVAL_ERROR;
00072 }
00073
00074 }
00075
00076
00077 static inline double eval_prefun_expr(prefun_expr_t * prefun_expr) {
00078 int i;
00079
00080
00081
00082
00083
00084 double arg_list[prefun_expr->num_args];
00085
00086 #ifdef EVAL_DEBUG
00087 printf("fn[");
00088 fflush(stdout);
00089 #endif
00090
00091 for (i = 0; i < prefun_expr->num_args; i++) {
00092 arg_list[i] = eval_gen_expr(prefun_expr->expr_list[i]);
00093 #ifdef EVAL_DEBUG
00094 if (i < (prefun_expr->num_args - 1))
00095 printf(", ");
00096 fflush(stdout);
00097 #endif
00098 }
00099
00100 #ifdef EVAL_DEBUG
00101 printf("]");
00102 fflush(stdout);
00103 #endif
00104
00105
00106
00107
00108
00109
00110 return (prefun_expr->func_ptr)(arg_list);
00111 }
00112
00113
00114 static inline double eval_val_expr(val_expr_t * val_expr) {
00115
00116
00117 if (val_expr == NULL)
00118 return EVAL_ERROR;
00119
00120
00121 if (val_expr->type == CONSTANT_TERM_T) {
00122 #ifdef EVAL_DEBUG
00123 printf("%.4f", val_expr->term.constant);
00124 fflush(stdout);
00125 #endif
00126 return (val_expr->term.constant);
00127 }
00128
00129
00130 if (val_expr->type == PARAM_TERM_T) {
00131 switch (val_expr->term.param->type) {
00132
00133 case P_TYPE_BOOL:
00134 #ifdef EVAL_DEBUG
00135 printf("(%s:%.4f)", val_expr->term.param->name, (double)(*((int*)(val_expr->term.param->engine_val))));
00136 fflush(stdout);
00137 #endif
00138
00139
00140 return (double)(*((int*)(val_expr->term.param->engine_val)));
00141 case P_TYPE_INT:
00142 #ifdef EVAL_DEBUG
00143 printf("(%s:%.4f)", val_expr->term.param->name, (double)(*((int*)(val_expr->term.param->engine_val))));
00144 fflush(stdout);
00145 #endif
00146
00147
00148 return (double)(*((int*)(val_expr->term.param->engine_val)));
00149 case P_TYPE_DOUBLE:
00150 #ifdef EVAL_DEBUG
00151 printf("(%s:%.4f)", val_expr->term.param->name, (*((double*)val_expr->term.param->engine_val)));
00152 fflush(stdout);
00153 #endif
00154
00155 if (val_expr->term.param->matrix_flag | (val_expr->term.param->flags & P_FLAG_ALWAYS_MATRIX)) {
00156 if (mesh_j >= 0) {
00157 return (((double**)val_expr->term.param->matrix)[mesh_i][mesh_j]);
00158 }
00159 else {
00160 return (((double*)val_expr->term.param->matrix)[mesh_i]);
00161 }
00162 }
00163 return *((double*)(val_expr->term.param->engine_val));
00164 default:
00165 return ERROR;
00166 }
00167 }
00168
00169 return FAILURE;
00170 }
00171
00172
00173 static inline double eval_tree_expr(tree_expr_t * tree_expr) {
00174
00175 double left_arg, right_arg;
00176 infix_op_t * infix_op;
00177
00178
00179 if (tree_expr == NULL)
00180 return EVAL_ERROR;
00181
00182
00183 if (tree_expr->infix_op == NULL) {
00184 if (tree_expr->gen_expr == NULL)
00185 return 0;
00186 else
00187 return eval_gen_expr(tree_expr->gen_expr);
00188 }
00189
00190
00191
00192
00193 infix_op = (infix_op_t*)tree_expr->infix_op;
00194 #ifdef EVAL_DEBUG
00195 printf("(");
00196 fflush(stdout);
00197 #endif
00198
00199 left_arg = eval_tree_expr(tree_expr->left);
00200
00201 #ifdef EVAL_DEBUG
00202
00203 switch (infix_op->type) {
00204 case INFIX_ADD:
00205 printf("+");
00206 break;
00207 case INFIX_MINUS:
00208 printf("-");
00209 break;
00210 case INFIX_MULT:
00211 printf("*");
00212 break;
00213 case INFIX_MOD:
00214 printf("%%");
00215 break;
00216 case INFIX_OR:
00217 printf("|");
00218 break;
00219 case INFIX_AND:
00220 printf("&");
00221 break;
00222 case INFIX_DIV:
00223 printf("/");
00224 break;
00225 default:
00226 printf("?");
00227 }
00228
00229 fflush(stdout);
00230 #endif
00231
00232 right_arg = eval_tree_expr(tree_expr->right);
00233
00234 #ifdef EVAL_DEBUG
00235 printf(")");
00236 fflush(stdout);
00237 #endif
00238
00239 switch (infix_op->type) {
00240 case INFIX_ADD:
00241 return (left_arg + right_arg);
00242 case INFIX_MINUS:
00243 return (left_arg - right_arg);
00244 case INFIX_MULT:
00245 return (left_arg * right_arg);
00246 case INFIX_MOD:
00247 if ((int)right_arg == 0) {
00248 #ifdef EVAL_DEBUG
00249 printf("eval_tree_expr: modulo zero!\n");
00250 #endif
00251 return DIV_BY_ZERO;
00252 }
00253 return ((int)left_arg % (int)right_arg);
00254 case INFIX_OR:
00255 return ((int)left_arg | (int)right_arg);
00256 case INFIX_AND:
00257 return ((int)left_arg & (int)right_arg);
00258 case INFIX_DIV:
00259 if (right_arg == 0) {
00260 #ifdef EVAL_DEBUG
00261 printf("eval_tree_expr: division by zero!\n");
00262 #endif
00263 return MAX_DOUBLE_SIZE;
00264 }
00265 return (left_arg / right_arg);
00266 default:
00267 #ifdef EVAL_DEBUG
00268 printf("eval_tree_expr: unknown infix operator!\n");
00269 #endif
00270 return ERROR;
00271 }
00272
00273 return ERROR;
00274 }
00275
00276
00277 gen_expr_t * const_to_expr(double val) {
00278
00279 gen_expr_t * gen_expr;
00280 val_expr_t * val_expr;
00281 term_t term;
00282
00283 term.constant = val;
00284
00285 if ((val_expr = new_val_expr(CONSTANT_TERM_T, term)) == NULL)
00286 return NULL;
00287
00288 gen_expr = new_gen_expr(VAL_T, (void*)val_expr);
00289
00290 if (gen_expr == NULL) {
00291 free_val_expr(val_expr);
00292 }
00293
00294 return gen_expr;
00295 }
00296
00297
00298 gen_expr_t * param_to_expr(param_t * param) {
00299
00300 gen_expr_t * gen_expr = NULL;
00301 val_expr_t * val_expr = NULL;
00302 term_t term;
00303
00304 if (param == NULL)
00305 return NULL;
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 term.param = param;
00322 if ((val_expr = new_val_expr(PARAM_TERM_T, term)) == NULL)
00323 return NULL;
00324
00325 if ((gen_expr = new_gen_expr(VAL_T, (void*)val_expr)) == NULL) {
00326 free_val_expr(val_expr);
00327 return NULL;
00328 }
00329 return gen_expr;
00330 }
00331
00332
00333 gen_expr_t * prefun_to_expr(double (*func_ptr)(), gen_expr_t ** expr_list, int num_args) {
00334
00335 gen_expr_t * gen_expr;
00336 prefun_expr_t * prefun_expr;
00337
00338
00339
00340 prefun_expr = (prefun_expr_t*)malloc(sizeof(prefun_expr_t));
00341
00342 if (prefun_expr == NULL)
00343 return NULL;
00344
00345 prefun_expr->num_args = num_args;
00346 prefun_expr->func_ptr = func_ptr;
00347 prefun_expr->expr_list = expr_list;
00348
00349 gen_expr = new_gen_expr(PREFUN_T, (void*)prefun_expr);
00350
00351 if (gen_expr == NULL)
00352 free_prefun_expr(prefun_expr);
00353
00354 return gen_expr;
00355 }
00356
00357
00358 tree_expr_t * new_tree_expr(infix_op_t * infix_op, gen_expr_t * gen_expr, tree_expr_t * left, tree_expr_t * right) {
00359
00360 tree_expr_t * tree_expr;
00361 tree_expr = (tree_expr_t*)malloc(sizeof(tree_expr_t));
00362
00363 if (tree_expr == NULL)
00364 return NULL;
00365 tree_expr->infix_op = infix_op;
00366 tree_expr->gen_expr = gen_expr;
00367 tree_expr->left = left;
00368 tree_expr->right = right;
00369 return tree_expr;
00370 }
00371
00372
00373
00374 val_expr_t * new_val_expr(int type, term_t term) {
00375
00376 val_expr_t * val_expr;
00377 val_expr = (val_expr_t*)malloc(sizeof(val_expr_t));
00378
00379 if (val_expr == NULL)
00380 return NULL;
00381
00382 val_expr->type = type;
00383 val_expr->term = term;
00384
00385 return val_expr;
00386 }
00387
00388
00389 gen_expr_t * new_gen_expr(int type, void * item) {
00390
00391 gen_expr_t * gen_expr;
00392
00393 gen_expr = (gen_expr_t*)malloc(sizeof(gen_expr_t));
00394 if (gen_expr == NULL)
00395 return NULL;
00396 gen_expr->type = type;
00397 gen_expr->item = item;
00398
00399 return gen_expr;
00400 }
00401
00402
00403 int free_gen_expr(gen_expr_t * gen_expr) {
00404
00405 if (gen_expr == NULL)
00406 return SUCCESS;
00407
00408 switch (gen_expr->type) {
00409 case VAL_T:
00410 free_val_expr(gen_expr->item);
00411 break;
00412 case PREFUN_T:
00413 free_prefun_expr(gen_expr->item);
00414 break;
00415 case TREE_T:
00416 free_tree_expr(gen_expr->item);
00417 break;
00418 default:
00419 return FAILURE;
00420 }
00421
00422 free(gen_expr);
00423 return SUCCESS;
00424
00425 }
00426
00427
00428
00429 int free_prefun_expr(prefun_expr_t * prefun_expr) {
00430
00431 int i;
00432 if (prefun_expr == NULL)
00433 return SUCCESS;
00434
00435
00436 for (i = 0 ; i < prefun_expr->num_args; i++) {
00437 free_gen_expr(prefun_expr->expr_list[i]);
00438 }
00439
00440 free(prefun_expr);
00441 return SUCCESS;
00442 }
00443
00444
00445 int free_val_expr(val_expr_t * val_expr) {
00446
00447 if (val_expr == NULL)
00448 return SUCCESS;
00449
00450 free(val_expr);
00451 return SUCCESS;
00452 }
00453
00454
00455 int free_tree_expr(tree_expr_t * tree_expr) {
00456
00457 if (tree_expr == NULL)
00458 return SUCCESS;
00459
00460
00461 free_tree_expr(tree_expr->left);
00462
00463
00464 free_gen_expr(tree_expr->gen_expr);
00465
00466
00467
00468
00469
00470
00471 free_tree_expr(tree_expr->right);
00472
00473
00474
00475 free(tree_expr);
00476 return SUCCESS;
00477 }
00478
00479
00480
00481
00482 int init_infix_ops() {
00483
00484 infix_add = new_infix_op(INFIX_ADD, 4);
00485 infix_minus = new_infix_op(INFIX_MINUS, 3);
00486 infix_div = new_infix_op(INFIX_DIV, 2);
00487 infix_or = new_infix_op(INFIX_OR, 5);
00488 infix_and = new_infix_op(INFIX_AND,4);
00489 infix_mod = new_infix_op(INFIX_MOD, 1);
00490 infix_mult = new_infix_op(INFIX_MULT, 2);
00491
00492
00493 infix_positive = new_infix_op(INFIX_ADD, 0);
00494 infix_negative = new_infix_op(INFIX_MINUS, 0);
00495
00496 return SUCCESS;
00497 }
00498
00499
00500
00501 int destroy_infix_ops()
00502 {
00503
00504 free(infix_add);
00505 free(infix_minus);
00506 free(infix_div);
00507 free(infix_or);
00508 free(infix_and);
00509 free(infix_mod);
00510 free(infix_mult);
00511 free(infix_positive);
00512 free(infix_negative);
00513
00514 return SUCCESS;
00515 }
00516
00517
00518 infix_op_t * new_infix_op(int type, int precedence) {
00519
00520 infix_op_t * infix_op;
00521
00522 infix_op = (infix_op_t*)malloc(sizeof(infix_op_t));
00523
00524 if (infix_op == NULL)
00525 return NULL;
00526
00527 infix_op->type = type;
00528 infix_op->precedence = precedence;
00529
00530 return infix_op;
00531 }
00532
00533
00534
00535
00536
00537 gen_expr_t * clone_gen_expr(gen_expr_t * gen_expr) {
00538
00539 gen_expr_t * new_gen_expr;
00540 val_expr_t * val_expr;
00541 tree_expr_t * tree_expr;
00542 prefun_expr_t * prefun_expr;
00543
00544
00545 if (gen_expr == NULL)
00546 return NULL;
00547
00548
00549 if ((new_gen_expr = (gen_expr_t*)malloc(sizeof(gen_expr_t))) == NULL)
00550 return NULL;
00551
00552
00553 switch (new_gen_expr->type = gen_expr->type) {
00554
00555 case VAL_T:
00556 if ((val_expr = clone_val_expr((val_expr_t*)gen_expr->item)) == NULL) {
00557 free(new_gen_expr);
00558 return NULL;
00559 }
00560 new_gen_expr->item = (void*)val_expr;
00561 break;
00562
00563 case PREFUN_T:
00564 if ((prefun_expr = clone_prefun_expr((prefun_expr_t*)gen_expr->item)) == NULL) {
00565 free(new_gen_expr);
00566 return NULL;
00567 }
00568 new_gen_expr->item = (void*)prefun_expr;
00569 break;
00570
00571 case TREE_T:
00572 if ((tree_expr = clone_tree_expr((tree_expr_t*)gen_expr->item)) == NULL) {
00573 free(new_gen_expr);
00574 return NULL;
00575 }
00576 new_gen_expr->item = (void*)tree_expr;
00577 break;
00578
00579 default:
00580 free(new_gen_expr);
00581 return NULL;
00582 }
00583
00584 return new_gen_expr;
00585 }
00586
00587
00588
00589 tree_expr_t * clone_tree_expr(tree_expr_t * tree_expr) {
00590
00591 tree_expr_t * new_tree_expr;
00592
00593
00594 if (tree_expr == NULL)
00595 return NULL;
00596
00597
00598 if ((new_tree_expr = (tree_expr_t*)malloc(sizeof(tree_expr_t))) == NULL)
00599 return NULL;
00600
00601
00602 new_tree_expr->infix_op = tree_expr->infix_op;
00603 new_tree_expr->gen_expr = clone_gen_expr(tree_expr->gen_expr);
00604 new_tree_expr->left = clone_tree_expr(tree_expr->left);
00605 new_tree_expr->right = clone_tree_expr(tree_expr->right);
00606
00607 return new_tree_expr;
00608 }
00609
00610
00611
00612 val_expr_t * clone_val_expr(val_expr_t * val_expr) {
00613
00614 val_expr_t * new_val_expr;
00615
00616
00617 if (val_expr == NULL)
00618 return NULL;
00619
00620
00621 if ((new_val_expr = (val_expr_t*)malloc(sizeof(val_expr_t))) == NULL)
00622 return NULL;
00623
00624
00625 new_val_expr->type = val_expr->type;
00626 new_val_expr->term = val_expr->term;
00627
00628
00629 return new_val_expr;
00630 }
00631
00632
00633 prefun_expr_t * clone_prefun_expr(prefun_expr_t * prefun_expr) {
00634
00635 int i;
00636 prefun_expr_t * new_prefun_expr;
00637
00638
00639 if (prefun_expr == NULL)
00640 return NULL;
00641
00642
00643 if ((new_prefun_expr = (prefun_expr_t*)malloc(sizeof(prefun_expr_t))) == NULL)
00644 return NULL;
00645
00646
00647 new_prefun_expr->num_args = prefun_expr->num_args;
00648 new_prefun_expr->func_ptr = prefun_expr->func_ptr;
00649
00650
00651 if ((new_prefun_expr->expr_list = (gen_expr_t**)malloc(sizeof(gen_expr_t*)*new_prefun_expr->num_args)) == NULL) {
00652 free(new_prefun_expr);
00653 return NULL;
00654 }
00655
00656
00657 for (i = 0; i < new_prefun_expr->num_args;i++)
00658 new_prefun_expr->expr_list[i] = clone_gen_expr(prefun_expr->expr_list[i]);
00659
00660
00661 return new_prefun_expr;
00662 }
00663
00664
00665 void reset_engine_vars() {
00666
00667 zoom=1.0;
00668 zoomexp= 1.0;
00669 rot= 0.0;
00670 warp= 0.0;
00671
00672 sx= 1.0;
00673 sy= 1.0;
00674 dx= 0.0;
00675 dy= 0.0;
00676 cx= 0.5;
00677 cy= 0.5;
00678
00679
00680 decay=.98;
00681
00682 wave_r= 1.0;
00683 wave_g= 0.2;
00684 wave_b= 0.0;
00685 wave_x= 0.5;
00686 wave_y= 0.5;
00687 wave_mystery= 0.0;
00688
00689 ob_size= 0.0;
00690 ob_r= 0.0;
00691 ob_g= 0.0;
00692 ob_b= 0.0;
00693 ob_a= 0.0;
00694
00695 ib_size = 0.0;
00696 ib_r = 0.0;
00697 ib_g = 0.0;
00698 ib_b = 0.0;
00699 ib_a = 0.0;
00700
00701 mv_a = 0.0;
00702 mv_r = 0.0;
00703 mv_g = 0.0;
00704 mv_b = 0.0;
00705 mv_l = 1.0;
00706 mv_x = 16.0;
00707 mv_y = 12.0;
00708 mv_dy = 0.02;
00709 mv_dx = 0.02;
00710
00711 meshx = 0;
00712 meshy = 0;
00713
00714 Time = 0;
00715 treb = 0;
00716 mid = 0;
00717 bass = 0;
00718 treb_att = 0;
00719 mid_att = 0;
00720 bass_att = 0;
00721 progress = 0;
00722 frame = 0;
00723
00724
00725
00726
00727 fRating = 0;
00728 fGammaAdj = 1.0;
00729 fVideoEchoZoom = 1.0;
00730 fVideoEchoAlpha = 0;
00731 nVideoEchoOrientation = 0;
00732
00733 nWaveMode = 7;
00734 bAdditiveWaves = 0;
00735 bWaveDots = 0;
00736 bWaveThick = 0;
00737 bModWaveAlphaByVolume = 0;
00738 bMaximizeWaveColor = 0;
00739 bTexWrap = 0;
00740 bDarkenCenter = 0;
00741 bRedBlueStereo = 0;
00742 bBrighten = 0;
00743 bDarken = 0;
00744 bSolarize = 0;
00745 bInvert = 0;
00746 bMotionVectorsOn = 1;
00747
00748 fWaveAlpha =1.0;
00749 fWaveScale = 1.0;
00750 fWaveSmoothing = 0;
00751 fWaveParam = 0;
00752 fModWaveAlphaStart = 0;
00753 fModWaveAlphaEnd = 0;
00754 fWarpAnimSpeed = 0;
00755 fWarpScale = 0;
00756 fShader = 0;
00757
00758
00759
00760 x_per_pixel = 0;
00761 y_per_pixel = 0;
00762 rad_per_pixel = 0;
00763 ang_per_pixel = 0;
00764
00765
00766
00767
00768
00769
00770 q1 = 0;
00771 q2 = 0;
00772 q3 = 0;
00773 q4 = 0;
00774 q5 = 0;
00775 q6 = 0;
00776 q7 = 0;
00777 q8 = 0;
00778
00779
00780
00781
00782
00783 }
00784
00785