home *** CD-ROM | disk | FTP | other *** search
- /*
- Resident odds and ends that don't fit anywhere else.
- */
-
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <time.h>
- #include <stdarg.h>
- #ifdef __TURBOC__
- #include <dir.h>
- #endif
- #include "fractint.h"
- #include "fractype.h"
-
- /* routines in this module */
-
- extern void restore_active_ovly(void );
- extern void findpath(char *filename,char *fullpathname);
- extern void notdiskmsg(void );
- extern int cvtcentermag(double *Xctr,double *Yctr,double *Magnification);
- extern void updatesavename(char *name);
- extern int check_key(void );
- extern int timer(int timertype,int(*subrtn)(),...);
- extern void showtrig(char *buf);
- extern int set_trig_array(int k,char *name);
- extern void set_trig_pointers(int which);
- extern int tab_display(void );
- extern int endswithslash(char *fl);
-
-
- int active_ovly = -1;
- long timer_start,timer_interval; /* timer(...) start & total */
-
- extern int active_ovly;
- extern int xdots, ydots;
- extern int dotmode;
- extern int show_orbit;
- extern int timerflag;
- extern int debugflag;
- extern int maxit;
- extern int fractype;
-
-
- /* call next when returning from resident routine and unsure whether
- caller is an overlay which has been displaced */
- void restore_active_ovly()
- {
- switch (active_ovly) {
- case OVLY_MISCOVL: miscovl_overlay(); break;
- case OVLY_CMDFILES: cmdfiles_overlay(); break;
- case OVLY_HELP: help_overlay(); break;
- case OVLY_PROMPTS: prompts_overlay(); break;
- case OVLY_LOADFILE: loadfile_overlay(); break;
- case OVLY_ROTATE: rotate_overlay(); break;
- case OVLY_PRINTER: printer_overlay(); break;
- case OVLY_LINE3D: line3d_overlay(); break;
- case OVLY_ENCODER: encoder_overlay(); break;
- case OVLY_CALCFRAC: calcfrac_overlay(); break;
- }
- }
-
-
- void findpath(char *filename, char *fullpathname) /* return full pathnames */
- {
- fullpathname[0] = 0; /* indicate none found */
- #ifdef __TURBOC__ /* look for the file */
- strcpy(fullpathname,searchpath(filename));
- #else
- _searchenv(filename,"PATH",fullpathname);
- #endif
- if (fullpathname[0] != 0) /* found it! */
- if (strncmp(&fullpathname[2],"\\\\",2) == 0) /* stupid klooge! */
- strcpy(&fullpathname[3],filename);
- }
-
-
- void notdiskmsg()
- {
- static char far sorrymsg[]={"\
- I'm sorry, but because of its random-screen-access algorithms, this\n\
- type cannot be created using a real-disk based 'video' mode."};
- stopmsg(1,sorrymsg);
- }
-
-
- /* convert corners to center/mag */
- int cvtcentermag(double *Xctr, double *Yctr, double *Magnification)
- {
- extern double xxmax,xxmin,yymax,yymin,xx3rd,yy3rd;
- double Width, Height, Radius, Ratio;
- Width = xxmax - xxmin;
- Height = yymax - yymin;
- if(xx3rd != xxmin || yy3rd != yymin || Width < 0
- || (Ratio = Height / Width) < 0.749 || Ratio > 0.751)
- return(0);
- /* calculate center and magnification */
- Radius = Height / 2.0;
- *Xctr = xxmin + (Width / 2.0);
- *Yctr = yymin + Radius;
- *Magnification = 1.0 / Radius;
- return(1);
- }
-
-
- void updatesavename(char *filename) /* go to the next file name */
- {
- char *save, *hold;
- char name[80],suffix[80];
- char *dotptr;
-
- strcpy(name,filename);
- suffix[0] = 0;
- if ((dotptr = strrchr(name,'.')) != NULL
- && dotptr > strrchr(name,'\\')) {
- strcpy(suffix,dotptr);
- *dotptr = 0;
- }
-
- hold = name + strlen(name) - 1; /* start at the end */
- while(hold >= name && (*hold == ' ' || isdigit(*hold))) /* skip backwards */
- hold--;
- hold++; /* recover first digit */
- while (*hold == '0') /* skip leading zeros */
- hold++;
- save = hold;
- while (*save) { /* check for all nines */
- if (*save != '9')
- break;
- save++;
- }
- if (!*save) /* if the whole thing is nines then back */
- save = hold - 1; /* up one place. Note that this will eat */
- /* your last letter if you go to far. */
- else
- save = hold;
- itoa(atoi(hold) + 1, save, 10); /* increment the number */
- strcpy(filename,name);
- strcat(filename,suffix);
- }
-
-
- int check_key()
- {
- int key;
- if((key = keypressed()) != 0) {
- if(key != 'o' && key != 'O')
- return(-1);
- getakey();
- if (dotmode != 11)
- show_orbit = 1 - show_orbit;
- }
- return(0);
- }
-
-
- /* timer function:
- timer(0,(*fractal)()) fractal engine
- timer(1,NULL,int width) decoder
- timer(2,NULL,char *savename) encoder
- */
- int timer(int timertype,int(*subrtn)(),...)
- {
- va_list arg_marker; /* variable arg list */
- char *savename;
- char *timestring;
- time_t ltime;
- FILE *fp;
- int out;
- int i;
- int do_bench;
-
- va_start(arg_marker,subrtn);
- do_bench = timerflag; /* record time? */
- if (timertype == 2) /* encoder, record time only if debug=200 */
- do_bench = (debugflag == 200);
- if(do_bench)
- fp=fopen("bench","a");
- timer_start = clock();
- switch(timertype) {
- case 0:
- out = (*subrtn)();
- break;
- case 1:
- i = va_arg(arg_marker,int);
- out = decoder(i); /* not indirect, safer with overlays */
- break;
- case 2:
- savename = va_arg(arg_marker,char *);
- out = encoder(); /* not indirect, safer with overlays */
- break;
- }
- /* next assumes CLK_TCK is 10^n, n>=2 */
- timer_interval = (clock() - timer_start) / (CLK_TCK/100);
-
- if(do_bench) {
- time(<ime);
- timestring = ctime(<ime);
- timestring[24] = 0; /*clobber newline in time string */
- switch(timertype) {
- case 1:
- fprintf(fp,"decode ");
- break;
- case 2:
- fprintf(fp,"encode ");
- break;
- }
- fprintf(fp,"%s type=%s resolution = %dx%d maxiter=%d",
- timestring,
- fractalspecific[fractype].name,
- xdots,
- ydots,
- maxit);
- fprintf(fp," time= %ld.%02ld secs\n",timer_interval/100,timer_interval%100);
- if(fp != NULL)
- fclose(fp);
- }
- return(out);
- }
-
-
- extern void lStkSin(void), dStkSin(void), lStkCos(void), dStkCos(void);
- extern void lStkSinh(void),dStkSinh(void),lStkCosh(void),dStkCosh(void);
- extern void lStkExp(void), dStkExp(void), lStkLog(void), dStkLog(void);
- extern void lStkSqr(void), dStkSqr(void);
-
- unsigned char trigndx[] = {SIN,SQR,SINH,COSH};
- void (*ltrig0)() = lStkSin;
- void (*ltrig1)() = lStkSqr;
- void (*ltrig2)() = lStkSinh;
- void (*ltrig3)() = lStkCosh;
- void (*dtrig0)() = dStkSin;
- void (*dtrig1)() = dStkSqr;
- void (*dtrig2)() = dStkSinh;
- void (*dtrig3)() = dStkCosh;
-
- struct trig_funct_lst trigfn[] =
- /* changing the order of these alters meaning of *.fra file */
- {
- {"sin", lStkSin, dStkSin },
- {"cos", lStkCos, dStkCos },
- {"sinh",lStkSinh,dStkSinh},
- {"cosh",lStkCosh,dStkCosh},
- {"exp", lStkExp, dStkExp },
- {"log", lStkLog, dStkLog },
- {"sqr", lStkSqr, dStkSqr }
- };
-
- void showtrig(char *buf) /* return display form of active trig functions */
- {
- int numfn,i;
- char tmpbuf[40];
- numfn = (fractalspecific[fractype].flags >> 6) & 7;
- *buf = 0; /* null string if none */
- if (numfn) {
- sprintf(buf, " function=%s",trigfn[trigndx[0]].name);
- i = 0;
- while(++i < numfn) {
- sprintf(tmpbuf, "/%s",trigfn[trigndx[i]].name);
- strcat(buf,tmpbuf);
- }
- }
- }
-
- /* set array of trig function indices according to "function=" command */
- int set_trig_array(int k, char *name)
- {
- char trigname[6];
- int i, lstlen;
- char *slash;
- strncpy(trigname,name,5);
- trigname[5] = 0; /* safety first */
-
- if ((slash = strchr(trigname,'/')))
- *slash = 0;
-
- strlwr(trigname);
- lstlen = sizeof(trigfn)/sizeof(struct trig_funct_lst);
-
- for(i=0;i<lstlen;i++)
- {
- if(strcmp(trigname,trigfn[i].name)==0)
- {
- trigndx[k] = i;
- set_trig_pointers(k);
- break;
- }
- }
- return(0);
- }
- void set_trig_pointers(int which)
- {
- /* set trig variable functions to avoid array lookup time */
- int i;
- switch(which)
- {
- case 0:
- ltrig0 = trigfn[trigndx[0]].lfunct;
- dtrig0 = trigfn[trigndx[0]].dfunct;
- break;
- case 1:
- ltrig1 = trigfn[trigndx[1]].lfunct;
- dtrig1 = trigfn[trigndx[1]].dfunct;
- break;
- case 2:
- ltrig2 = trigfn[trigndx[2]].lfunct;
- dtrig2 = trigfn[trigndx[2]].dfunct;
- break;
- case 3:
- ltrig3 = trigfn[trigndx[3]].lfunct;
- dtrig3 = trigfn[trigndx[3]].dfunct;
- break;
- default: /* do 'em all */
- for(i=0;i<4;i++)
- set_trig_pointers(i);
- break;
- }
- }
-
-
- int tab_display() /* display the status of the current image */
- {
- extern char usr_floatflag;
- extern double xxmin, xxmax, xx3rd, yymin, yymax, yy3rd;
- extern double param[4];
- extern double rqlim;
- extern long calctime, timer_start;
- extern int calc_status;
- extern char FormName[];
- extern int rseed;
- extern int invert;
- extern char string004[];
- int i;
- double Xctr, Yctr, Magnification;
- char msg[81];
- if (calc_status < 0) /* no active fractal image */
- return(0); /* (no TAB on the credits screen) */
- if (calc_status == 1) /* next assumes CLK_TCK is 10^n, n>=2 */
- calctime += (clock() - timer_start) / (CLK_TCK/100);
- setfortext();
- printf("\n\nCurrent Fractal Type is: %-15s ",
- fractalspecific[fractype].name[0] == '*' ?
- &fractalspecific[fractype].name[1] :
- fractalspecific[fractype].name
- );
- switch (calc_status) {
- case 0: printf("(parms chgd since generated)\n");
- break;
- case 1: printf("(still being generated)\n");
- break;
- case 2: printf("(interrupted, resumable)\n");
- break;
- case 3: printf("(interrupted, non-resumable)\n");
- break;
- case 4: printf("(image completed)\n");
- }
- showtrig(msg);
- printf(msg);
- printf("\n");
- if (fractype == FORMULA || fractype == FFORMULA)
- printf("Formula name: %s\n",FormName);
- if (calc_status == 1 || calc_status == 2) {
- if (fractalspecific[fractype].flags&INFCALC)
- printf("Note: this type runs forever.\n");
- if (fractalspecific[fractype].flags&NORESUME)
- printf("Note: can't resume this type after interrupts other than <tab> and <F1>\n");
- }
- if (helpmode == HELPCYCLING)
- printf("%s %s\n"," ",
- "(You are in color-cycling mode)");
- printf("\nCalculation time:%3ld:%02ld:%02ld.%02ld", calctime/360000,
- (calctime%360000)/6000, (calctime%6000)/100, calctime%100);
- if (usr_floatflag)
- printf(" Floating-point flag is activated");
- printf("\n\n");
- for(i=0;i<4;i++) {
- printf(" Param%1d = %12.9f ",i+1,param[i]);
- if ((i & 1) != 0)
- printf("\n");
- }
- printf("\nCurrent Corners: X Y\n");
- printf(" top-left %20.16f %20.16f\n",xxmin,yymax);
- printf(" bottom-right %20.16f %20.16f\n",xxmax,yymin);
- adjust_corner(); /* make bottom left exact if very near exact */
- if (cvtcentermag(&Xctr, &Yctr, &Magnification))
- printf("\nCenter: %20.16f %20.16f Mag: %20.16f\n",Xctr,Yctr,Magnification);
- else if (xxmin != xx3rd || yymin != yy3rd)
- printf(" bottom-left %20.16f %20.16f\n",xx3rd,yy3rd);
- printf("\nCurrent Iteration Maximum = %d Effective Bailout = %f\n",maxit,rqlim);
- if (fractype == PLASMA)
- printf("\nCurrent 'rseed=' value is %d\n",rseed);
- if(invert) {
- extern double f_radius,f_xcenter,f_ycenter;
- printf("\nCurrent Inversion parameters are: \n");
- printf(" radius = %12.9f \n",f_radius);
- printf(" xcenter = %12.9f \n",f_xcenter);
- printf(" ycenter = %12.9f \n",f_ycenter);
- }
- printf("\n%s",string004);
- getakey();
- setforgraphics();
- timer_start = clock(); /* tab display was "time out" */
- return(0);
- }
-
-
- int endswithslash(char *fl)
- {
- int len;
- len = strlen(fl);
- if(len)
- if(fl[--len]=='\\')
- return(1);
- return(0);
- }
-
- extern char ifsfilename[80]; /* IFS code file */
- extern char ifs3dfilename[80]; /* IFS 3D code file */
-
- /* --------------------------------------------------------------------- */
-
- void ifsgetfile() /* read in IFS parameters */
- {
- FILE *ifsfile; /* IFS code file pointer */
- float localifs[IFSPARM];
- char temp1[80];
- int i, j;
-
- ENTER_OVLY(OVLY_PROMPTS);
- /* read in IFS codes from file */
- if (!endswithslash(ifsfilename)) {
- findpath(ifsfilename, temp1);
- if ( (ifsfile = fopen( temp1,"r" )) != NULL ) {
- i = -1;
- while (fgets(temp1, 155, ifsfile) != NULL) {
- if (++i >= NUMIFS) break;
- sscanf(temp1," %f %f %f %f %f %f %f",
- &localifs[0], &localifs[1], &localifs[2], &localifs[3],
- &localifs[4], &localifs[5], &localifs[6] );
- for (j = 0; j < IFSPARM; j++) {
- initifs[i][j] = localifs[j];
- initifs[i+1][j] = 0.0;
- }
- }
- fclose(ifsfile);
- }
- }
- EXIT_OVLY;
- }
-
- /* --------------------------------------------------------------------- */
-
- void ifs3dgetfile() /* read in 3D IFS parameters */
- {
- FILE *ifsfile; /* IFS code file pointer */
- float localifs[IFS3DPARM];
- int i, j;
- char temp1[80];
-
- ENTER_OVLY(OVLY_PROMPTS);
- /* read in IFS codes from file */
- if (!endswithslash(ifs3dfilename)) {
- findpath(ifs3dfilename, temp1);
- if ( (ifsfile = fopen( temp1,"r" )) != NULL ) {
- i = -1;
- while (fgets(temp1, 155, ifsfile) != NULL) {
- if (++i >= NUMIFS) break;
- sscanf(temp1," %f %f %f %f %f %f %f %f %f %f %f %f %f",
- &localifs[ 0], &localifs[ 1], &localifs[ 2],
- &localifs[ 3], &localifs[ 4], &localifs[ 5],
- &localifs[ 6], &localifs[ 7], &localifs[ 8],
- &localifs[ 9], &localifs[10], &localifs[11],
- &localifs[12]
- );
- for (j = 0; j < IFS3DPARM; j++) {
- initifs3d[i][j] = localifs[j];
- initifs3d[i+1][j] = 0.0;
- }
- }
- fclose(ifsfile);
- }
- }
- EXIT_OVLY;
- }
-