home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / WINDOWS / PROGRAM / WINSRC20.ZIP / MISCRES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-07  |  13.8 KB  |  486 lines

  1. /*
  2.     Resident odds and ends that don't fit anywhere else.
  3. */
  4.  
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <time.h>
  10. #include <stdarg.h>
  11. #ifdef __TURBOC__
  12. #include <dir.h>
  13. #endif
  14. #include "fractint.h"
  15. #include "fractype.h"
  16.  
  17. /* routines in this module    */
  18.  
  19. extern    void restore_active_ovly(void );
  20. extern    void findpath(char *filename,char *fullpathname);
  21. extern    void notdiskmsg(void );
  22. extern    int cvtcentermag(double *Xctr,double *Yctr,double *Magnification);
  23. extern    void updatesavename(char *name);
  24. extern    int check_key(void );
  25. extern    int timer(int timertype,int(*subrtn)(),...);
  26. extern    void showtrig(char *buf);
  27. extern    int set_trig_array(int k,char *name);
  28. extern    void set_trig_pointers(int which);
  29. extern    int tab_display(void );
  30. extern    int endswithslash(char *fl);
  31.  
  32.  
  33. int active_ovly = -1;
  34. long timer_start,timer_interval;    /* timer(...) start & total */
  35.  
  36. extern int  active_ovly;
  37. extern int  xdots, ydots;
  38. extern int  dotmode;
  39. extern int  show_orbit;
  40. extern int  timerflag;
  41. extern int  debugflag;
  42. extern int  maxit;
  43. extern int  fractype;
  44.  
  45.  
  46. /* call next when returning from resident routine and unsure whether
  47.    caller is an overlay which has been displaced */
  48. void restore_active_ovly()
  49. {
  50.    switch (active_ovly) {
  51.       case OVLY_MISCOVL:  miscovl_overlay();  break;
  52.       case OVLY_CMDFILES: cmdfiles_overlay(); break;
  53.       case OVLY_HELP:      help_overlay();     break;
  54.       case OVLY_PROMPTS:  prompts_overlay();  break;
  55.       case OVLY_LOADFILE: loadfile_overlay(); break;
  56.       case OVLY_ROTATE:   rotate_overlay();   break;
  57.       case OVLY_PRINTER:  printer_overlay();  break;
  58.       case OVLY_LINE3D:   line3d_overlay();   break;
  59.       case OVLY_ENCODER:  encoder_overlay();  break;
  60.       case OVLY_CALCFRAC: calcfrac_overlay(); break;
  61.       }
  62. }
  63.  
  64.  
  65. void findpath(char *filename, char *fullpathname) /* return full pathnames */
  66. {
  67.    fullpathname[0] = 0;             /* indicate none found */
  68. #ifdef __TURBOC__                /* look for the file */
  69.    strcpy(fullpathname,searchpath(filename));
  70. #else
  71.    _searchenv(filename,"PATH",fullpathname);
  72. #endif
  73.    if (fullpathname[0] != 0)            /* found it! */
  74.       if (strncmp(&fullpathname[2],"\\\\",2) == 0) /* stupid klooge! */
  75.      strcpy(&fullpathname[3],filename);
  76. }
  77.  
  78.  
  79. void notdiskmsg()
  80. {
  81. static char far sorrymsg[]={"\
  82. I'm sorry, but because of its random-screen-access algorithms, this\n\
  83. type cannot be created using a real-disk based 'video' mode."};
  84.    stopmsg(1,sorrymsg);
  85. }
  86.  
  87.  
  88. /* convert corners to center/mag */
  89. int cvtcentermag(double *Xctr, double *Yctr, double *Magnification)
  90. {
  91.    extern double xxmax,xxmin,yymax,yymin,xx3rd,yy3rd;
  92.    double Width, Height, Radius, Ratio;
  93.    Width  = xxmax - xxmin;
  94.    Height = yymax - yymin;
  95.    if(xx3rd != xxmin || yy3rd != yymin || Width < 0
  96.      || (Ratio = Height / Width) < 0.749 || Ratio > 0.751)
  97.       return(0);
  98.    /* calculate center and magnification */
  99.    Radius = Height / 2.0;
  100.    *Xctr = xxmin + (Width / 2.0);
  101.    *Yctr = yymin + Radius;
  102.    *Magnification = 1.0 / Radius;
  103.    return(1);
  104. }
  105.  
  106.  
  107. void updatesavename(char *filename) /* go to the next file name */
  108. {
  109.    char *save, *hold;
  110.    char name[80],suffix[80];
  111.    char *dotptr;
  112.  
  113.    strcpy(name,filename);
  114.    suffix[0] = 0;
  115.    if ((dotptr = strrchr(name,'.')) != NULL
  116.      && dotptr > strrchr(name,'\\')) {
  117.       strcpy(suffix,dotptr);
  118.       *dotptr = 0;
  119.       }
  120.  
  121.    hold = name + strlen(name) - 1; /* start at the end */
  122.    while(hold >= name && (*hold == ' ' || isdigit(*hold))) /* skip backwards */
  123.       hold--;
  124.    hold++;            /* recover first digit */
  125.    while (*hold == '0')         /* skip leading zeros */
  126.       hold++;
  127.    save = hold;
  128.    while (*save) {        /* check for all nines */
  129.       if (*save != '9')
  130.      break;
  131.       save++;
  132.       }
  133.    if (!*save)            /* if the whole thing is nines then back */
  134.       save = hold - 1;        /* up one place. Note that this will eat */
  135.                 /* your last letter if you go to far.     */
  136.    else
  137.       save = hold;
  138.    itoa(atoi(hold) + 1, save, 10); /* increment the number */
  139.    strcpy(filename,name);
  140.    strcat(filename,suffix);
  141. }
  142.  
  143.  
  144. int check_key()
  145. {
  146.    int key;
  147.    if((key = keypressed()) != 0) {
  148.       if(key != 'o' && key != 'O')
  149.      return(-1);
  150.       getakey();
  151.       if (dotmode != 11)
  152.      show_orbit = 1 - show_orbit;
  153.    }
  154.    return(0);
  155. }
  156.  
  157.  
  158. /* timer function:
  159.      timer(0,(*fractal)())        fractal engine
  160.      timer(1,NULL,int width)        decoder
  161.      timer(2,NULL,char *savename)    encoder
  162.   */
  163. int timer(int timertype,int(*subrtn)(),...)
  164. {
  165.    va_list arg_marker;    /* variable arg list */
  166.    char *savename;
  167.    char *timestring;
  168.    time_t ltime;
  169.    FILE *fp;
  170.    int out;
  171.    int i;
  172.    int do_bench;
  173.  
  174.    va_start(arg_marker,subrtn);
  175.    do_bench = timerflag; /* record time? */
  176.    if (timertype == 2)     /* encoder, record time only if debug=200 */
  177.       do_bench = (debugflag == 200);
  178.    if(do_bench)
  179.       fp=fopen("bench","a");
  180.    timer_start = clock();
  181.    switch(timertype) {
  182.       case 0:
  183.      out = (*subrtn)();
  184.      break;
  185.       case 1:
  186.      i = va_arg(arg_marker,int);
  187.      out = decoder(i);         /* not indirect, safer with overlays */
  188.      break;
  189.       case 2:
  190.      savename = va_arg(arg_marker,char *);
  191.      out = encoder();         /* not indirect, safer with overlays */
  192.      break;
  193.       }
  194.    /* next assumes CLK_TCK is 10^n, n>=2 */
  195.    timer_interval = (clock() - timer_start) / (CLK_TCK/100);
  196.  
  197.    if(do_bench) {
  198.       time(<ime);
  199.       timestring = ctime(<ime);
  200.       timestring[24] = 0; /*clobber newline in time string */
  201.       switch(timertype) {
  202.      case 1:
  203.         fprintf(fp,"decode ");
  204.         break;
  205.      case 2:
  206.         fprintf(fp,"encode ");
  207.         break;
  208.      }
  209.       fprintf(fp,"%s type=%s resolution = %dx%d maxiter=%d",
  210.       timestring,
  211.       fractalspecific[fractype].name,
  212.       xdots,
  213.       ydots,
  214.       maxit);
  215.       fprintf(fp," time= %ld.%02ld secs\n",timer_interval/100,timer_interval%100);
  216.       if(fp != NULL)
  217.      fclose(fp);
  218.       }
  219.    return(out);
  220. }
  221.  
  222.  
  223. extern void lStkSin(void), dStkSin(void), lStkCos(void), dStkCos(void);
  224. extern void lStkSinh(void),dStkSinh(void),lStkCosh(void),dStkCosh(void);
  225. extern void lStkExp(void), dStkExp(void), lStkLog(void), dStkLog(void);
  226. extern void lStkSqr(void), dStkSqr(void);
  227.  
  228. unsigned char trigndx[] = {SIN,SQR,SINH,COSH};
  229. void (*ltrig0)() = lStkSin;
  230. void (*ltrig1)() = lStkSqr;
  231. void (*ltrig2)() = lStkSinh;
  232. void (*ltrig3)() = lStkCosh;
  233. void (*dtrig0)() = dStkSin;
  234. void (*dtrig1)() = dStkSqr;
  235. void (*dtrig2)() = dStkSinh;
  236. void (*dtrig3)() = dStkCosh;
  237.  
  238. struct trig_funct_lst trigfn[] =
  239. /* changing the order of these alters meaning of *.fra file */
  240. {
  241.    {"sin", lStkSin, dStkSin },
  242.    {"cos", lStkCos, dStkCos },
  243.    {"sinh",lStkSinh,dStkSinh},
  244.    {"cosh",lStkCosh,dStkCosh},
  245.    {"exp", lStkExp, dStkExp },
  246.    {"log", lStkLog, dStkLog },
  247.    {"sqr", lStkSqr, dStkSqr }
  248. };
  249.  
  250. void showtrig(char *buf) /* return display form of active trig functions */
  251. {
  252.    int numfn,i;
  253.    char tmpbuf[40];
  254.    numfn = (fractalspecific[fractype].flags >> 6) & 7;
  255.    *buf = 0; /* null string if none */
  256.    if (numfn) {
  257.       sprintf(buf, " function=%s",trigfn[trigndx[0]].name);
  258.       i = 0;
  259.       while(++i < numfn) {
  260.      sprintf(tmpbuf, "/%s",trigfn[trigndx[i]].name);
  261.      strcat(buf,tmpbuf);
  262.      }
  263.       }
  264. }
  265.  
  266. /* set array of trig function indices according to "function=" command */
  267. int set_trig_array(int k, char *name)
  268. {
  269.    char trigname[6];
  270.    int i, lstlen;
  271.    char *slash;
  272.    strncpy(trigname,name,5);
  273.    trigname[5] = 0; /* safety first */
  274.  
  275.    if ((slash = strchr(trigname,'/')))
  276.       *slash = 0;
  277.  
  278.    strlwr(trigname);
  279.    lstlen = sizeof(trigfn)/sizeof(struct trig_funct_lst);
  280.  
  281.    for(i=0;i<lstlen;i++)
  282.    {
  283.       if(strcmp(trigname,trigfn[i].name)==0)
  284.       {
  285.      trigndx[k] = i;
  286.      set_trig_pointers(k);
  287.      break;
  288.       }
  289.    }
  290.    return(0);
  291. }
  292. void set_trig_pointers(int which)
  293. {
  294.   /* set trig variable functions to avoid array lookup time */
  295.    int i;
  296.    switch(which)
  297.    {
  298.    case 0:
  299.       ltrig0 = trigfn[trigndx[0]].lfunct;
  300.       dtrig0 = trigfn[trigndx[0]].dfunct;
  301.       break;
  302.    case 1:
  303.       ltrig1 = trigfn[trigndx[1]].lfunct;
  304.       dtrig1 = trigfn[trigndx[1]].dfunct;
  305.       break;
  306.    case 2:
  307.       ltrig2 = trigfn[trigndx[2]].lfunct;
  308.       dtrig2 = trigfn[trigndx[2]].dfunct;
  309.       break;
  310.    case 3:
  311.       ltrig3 = trigfn[trigndx[3]].lfunct;
  312.       dtrig3 = trigfn[trigndx[3]].dfunct;
  313.       break;
  314.    default: /* do 'em all */
  315.       for(i=0;i<4;i++)
  316.      set_trig_pointers(i);
  317.       break;
  318.    }
  319. }
  320.  
  321.  
  322. int tab_display()    /* display the status of the current image */
  323. {
  324.    extern char usr_floatflag;
  325.    extern double xxmin, xxmax, xx3rd, yymin, yymax, yy3rd;
  326.    extern double param[4];
  327.    extern double rqlim;
  328.    extern long calctime, timer_start;
  329.    extern int  calc_status;
  330.    extern char FormName[];
  331.    extern int  rseed;
  332.    extern int  invert;
  333.    extern char string004[];
  334.    int i;
  335.    double Xctr, Yctr, Magnification;
  336.    char msg[81];
  337.    if (calc_status < 0)     /* no active fractal image */
  338.       return(0);        /* (no TAB on the credits screen) */
  339.    if (calc_status == 1)    /* next assumes CLK_TCK is 10^n, n>=2 */
  340.       calctime += (clock() - timer_start) / (CLK_TCK/100);
  341.    setfortext();
  342.    printf("\n\nCurrent Fractal Type is: %-15s  ",
  343.     fractalspecific[fractype].name[0] == '*' ?
  344.       &fractalspecific[fractype].name[1] :
  345.       fractalspecific[fractype].name
  346.     );
  347.    switch (calc_status) {
  348.       case 0: printf("(parms chgd since generated)\n");
  349.           break;
  350.       case 1: printf("(still being generated)\n");
  351.           break;
  352.       case 2: printf("(interrupted, resumable)\n");
  353.           break;
  354.       case 3: printf("(interrupted, non-resumable)\n");
  355.           break;
  356.       case 4: printf("(image completed)\n");
  357.       }
  358.    showtrig(msg);
  359.    printf(msg);
  360.    printf("\n");
  361.    if (fractype == FORMULA || fractype == FFORMULA)
  362.       printf("Formula name: %s\n",FormName);
  363.    if (calc_status == 1 || calc_status == 2) {
  364.       if (fractalspecific[fractype].flags&INFCALC)
  365.      printf("Note: this type runs forever.\n");
  366.       if (fractalspecific[fractype].flags&NORESUME)
  367.      printf("Note: can't resume this type after interrupts other than <tab> and <F1>\n");
  368.       }
  369.    if (helpmode == HELPCYCLING)
  370.       printf("%s  %s\n","                                        ",
  371.             "(You are in color-cycling mode)");
  372.    printf("\nCalculation time:%3ld:%02ld:%02ld.%02ld", calctime/360000,
  373.       (calctime%360000)/6000, (calctime%6000)/100, calctime%100);
  374.    if (usr_floatflag)
  375.       printf("             Floating-point flag is activated");
  376.    printf("\n\n");
  377.    for(i=0;i<4;i++) {
  378.       printf("   Param%1d = %12.9f ",i+1,param[i]);
  379.       if ((i & 1) != 0)
  380.      printf("\n");
  381.       }
  382.    printf("\nCurrent Corners:        X                     Y\n");
  383.    printf(" top-left      %20.16f  %20.16f\n",xxmin,yymax);
  384.    printf(" bottom-right  %20.16f  %20.16f\n",xxmax,yymin);
  385.    adjust_corner(); /* make bottom left exact if very near exact */
  386.    if (cvtcentermag(&Xctr, &Yctr, &Magnification))
  387.       printf("\nCenter: %20.16f  %20.16f Mag: %20.16f\n",Xctr,Yctr,Magnification);
  388.    else if (xxmin != xx3rd || yymin != yy3rd)
  389.       printf(" bottom-left   %20.16f  %20.16f\n",xx3rd,yy3rd);
  390.    printf("\nCurrent Iteration Maximum = %d  Effective Bailout = %f\n",maxit,rqlim);
  391.    if (fractype == PLASMA)
  392.       printf("\nCurrent 'rseed=' value is %d\n",rseed);
  393.    if(invert) {
  394.       extern double f_radius,f_xcenter,f_ycenter;
  395.       printf("\nCurrent Inversion parameters are: \n");
  396.       printf("   radius = %12.9f \n",f_radius);
  397.       printf("  xcenter = %12.9f \n",f_xcenter);
  398.       printf("  ycenter = %12.9f \n",f_ycenter);
  399.       }
  400.    printf("\n%s",string004);
  401.    getakey();
  402.    setforgraphics();
  403.    timer_start = clock(); /* tab display was "time out" */
  404.    return(0);
  405. }
  406.  
  407.  
  408. int endswithslash(char *fl)
  409. {
  410.    int len;
  411.    len = strlen(fl);
  412.    if(len)
  413.       if(fl[--len]=='\\')
  414.      return(1);
  415.    return(0);
  416. }
  417.  
  418. extern    char    ifsfilename[80];    /* IFS code file */
  419. extern    char    ifs3dfilename[80];  /* IFS 3D code file */
  420.  
  421. /* --------------------------------------------------------------------- */
  422.  
  423. void ifsgetfile()    /* read in IFS parameters */
  424. {
  425.    FILE  *ifsfile;        /* IFS code file pointer */
  426.    float localifs[IFSPARM];
  427.    char temp1[80];
  428.    int i, j;
  429.  
  430.    ENTER_OVLY(OVLY_PROMPTS);
  431.    /* read in IFS codes from file */
  432.    if (!endswithslash(ifsfilename)) {
  433.       findpath(ifsfilename, temp1);
  434.       if ( (ifsfile = fopen( temp1,"r" )) != NULL ) {
  435.      i = -1;
  436.      while (fgets(temp1, 155, ifsfile) != NULL) {
  437.         if (++i >= NUMIFS) break;
  438.         sscanf(temp1," %f %f %f %f %f %f %f",
  439.            &localifs[0], &localifs[1], &localifs[2], &localifs[3],
  440.            &localifs[4], &localifs[5], &localifs[6]  );
  441.         for (j = 0; j < IFSPARM; j++) {
  442.            initifs[i][j]   = localifs[j];
  443.            initifs[i+1][j] = 0.0;
  444.            }
  445.         }
  446.      fclose(ifsfile);
  447.      }
  448.    }
  449.    EXIT_OVLY;
  450. }
  451.  
  452. /* --------------------------------------------------------------------- */
  453.  
  454. void ifs3dgetfile()    /* read in 3D IFS parameters */
  455. {
  456.    FILE  *ifsfile;        /* IFS code file pointer */
  457.    float localifs[IFS3DPARM];
  458.    int i, j;
  459.    char temp1[80];
  460.  
  461.    ENTER_OVLY(OVLY_PROMPTS);
  462.    /* read in IFS codes from file */
  463.    if (!endswithslash(ifs3dfilename)) {
  464.       findpath(ifs3dfilename, temp1);
  465.       if ( (ifsfile = fopen( temp1,"r" )) != NULL ) {
  466.      i = -1;
  467.      while (fgets(temp1, 155, ifsfile) != NULL) {
  468.         if (++i >= NUMIFS) break;
  469.         sscanf(temp1," %f %f %f %f %f %f %f %f %f %f %f %f %f",
  470.            &localifs[ 0], &localifs[ 1], &localifs[ 2],
  471.            &localifs[ 3], &localifs[ 4], &localifs[ 5],
  472.            &localifs[ 6], &localifs[ 7], &localifs[ 8],
  473.            &localifs[ 9], &localifs[10], &localifs[11],
  474.            &localifs[12]
  475.            );
  476.         for (j = 0; j < IFS3DPARM; j++) {
  477.            initifs3d[i][j]     = localifs[j];
  478.            initifs3d[i+1][j] = 0.0;
  479.            }
  480.         }
  481.      fclose(ifsfile);
  482.      }
  483.       }
  484.    EXIT_OVLY;
  485. }
  486.