[net.micro.amiga] MAND.C Pt. 2 - Mandelbrot Set Repost

french@caip.RUTGERS.EDU (12/27/85)

From: "french robert%d.mfenet"@LLL-MFE.ARPA

Part two of MAND.C...hopefully right this time!
------------------------ Cut here! -----------------------------
         case 'M':
            if (secchar == 'X') {
               sscanf(argpos,"%d",&temp);
               if (temp < 5 ||
                     (temp > 320 && !(color_mode & 4)) ||
                     (temp > 640 && (color_mode & 4))) {
                  fputs("Illegal parameter!\n",console);
                  goto command;
               }
               max_x = temp;
               max_mem = max_mem_y * MAXX;
               max_mem /= max_x;
               if (v_fp) {
                  fclose(v_fp);
                  v_fp = NULL;
               }
               goto command;
            }
            if (secchar == 'Y') {
               sscanf(argpos,"%d",&temp);
               if (temp < 5 ||
                     (temp > 200-STARTY && !(color_mode & 2)) ||
                     (temp > 400-STARTY && (color_mode & 2))) {
                  fputs("Illegal parameter!\n",console);
                  goto command;
               }
               max_y = temp;
               if (v_fp) {
                  fclose(v_fp);
                  v_fp = NULL;
               }
               goto command;
            }
            if (secchar == 'C') {
               sscanf(argpos,"%d",&temp);
               if (temp * color_inc + color_offset > 4095) {
                  fputs("More than 4096 colors!\n",console);
                  goto command;
               }
               if (temp < 2) {
                  fputs("MaxCount must be greater than 1\n",console);
                  goto command;
               }
               max_count = temp;
               goto command;
            }
            if (secchar == 'M') {
               sscanf(argpos,"%d",&temp);
               if (temp < 5) {
                  fputs("Number of lines must be >4!\n",console);
                  goto command;
               }
               free(v_mand_store);
               v_mand_store = (UWORD *)malloc(MAXX*temp*sizeof(UWORD));
               if (v_mand_store == NULL) {
                  fputs("Can't allocate that much memory for set storage",console);
                  v_mand_store = (UWORD *)malloc(MAXX*max_mem_y*sizeof(UWORD));
                  if (v_mand_store == NULL)
                     abort("Can't reallocate memory!!!");
                  goto command;
               }
               max_mem_y = temp;
               if (v_fp) {
                  fclose(v_fp);
                  v_fp = NULL;
               }
               max_mem = MAXX * max_mem_y;
               max_mem /= max_x;
               goto command;
            }
            ill_cmd();
            goto command;
         case 'L':
            if (v_fp) {
               fclose(v_fp);
               v_fp = NULL;
            }
            v_fp = fopen(stpblk(cmd+1),"r");
            if (v_fp == NULL) {
               fprintf(console,"Cannot open file '%s'\n",stpblk(cmd+1));
               goto command;
            }
            if (getc(v_fp) != 1) {
               fputs("File is not in proper format\n",console);
               fclose(v_fp);
               v_fp = NULL;
               goto command;
            }
            fread(&start_r,sizeof(start_r),1,v_fp);
            fread(&end_r,sizeof(end_r),1,v_fp);
            fread(&start_i,sizeof(start_i),1,v_fp);
            fread(&end_i,sizeof(end_i),1,v_fp);
            fread(&max_x,sizeof(max_x),1,v_fp);
            fread(&max_y,sizeof(max_y),1,v_fp);
            v_offset = 25L;
            modified = FALSE;
            v_starty = max_mem+1;
            want_read = TRUE;
            v_pos_line(0);
            goto command;
         case 'X':
            if (secchar == 'R') {
               sscanf(argpos,"%f",&scale.f);
               scale.i = SPFieee(scale.i);
               start_r.i = SPAdd(start_r.i,scale.i);
               end_r.i = SPAdd(end_r.i,scale.i);
               if (v_fp) {
                  fclose(v_fp);
                  v_fp = NULL;
               }
               goto command;
            }
            if (secchar == 'I') {
               sscanf(argpos,"%f",&scale.f);
               scale.i = SPFieee(scale.i);
               start_i.i = SPAdd(start_i.i,scale.i);
               end_i.i = SPAdd(end_i.i,scale.i);
               if (v_fp) {
                  fclose(v_fp);
                  v_fp = NULL;
               }
               goto command;
            }
            ill_cmd();
            goto command;
         case 'Z':
            if (secchar != 'R' && secchar != 'I' && secchar != 'B') {
               ill_cmd();
               goto command;
            }
            if (v_fp) {
               fclose(v_fp);
               v_fp = NULL;
            }
            if (secchar != 'I' ) {
               sscanf(argpos,"%f",&scale.f);
               scale.i = SPFieee(scale.i);
               distance.i = SPDiv(SPFlt(2),SPSub(start_r.i,end_r.i));
               center.i = SPAdd(start_r.i,distance.i);
               scale.i = SPMul(scale.i,distance.i);
               start_r.i = SPSub(scale.i,center.i);
               end_r.i = SPAdd(scale.i,center.i);
            }
            if (secchar != 'R') {
               sscanf(argpos,"%f",&scale.f);
               scale.i = SPFieee(scale.i);
               distance.i = SPDiv(SPFlt(2),SPSub(start_i.i,end_i.i));
               center.i = SPAdd(start_i.i,distance.i);
               scale.i = SPMul(scale.i,distance.i);
               start_i.i = SPSub(scale.i,center.i);
               end_i.i = SPAdd(scale.i,center.i);
            }
            goto command;
         case 'C':
            if (secchar == 'I') {
               sscanf(argpos,"%d",&temp);
               if (max_count * temp + color_offset > 4095) {
                  fputs("More than 4096 colors!\n",console);
                  goto command;
               }
               if (temp < 1) {
                  fputs("Increment must be greater than 0!\n",console);
                  goto command;
               }
               color_inc = temp;
               goto command;
            }
            if (secchar == 'O') {
               sscanf(argpos,"%d",&temp);
               if (max_count * color_inc + temp > 4095) {
                  fputs("More than 4096 colors!\n",console);
                  goto command;
               }
               if (temp < 0) {
                  fputs("Negative offset illegal!\n",console);
                  goto command;
               }
               color_offset = temp;
               goto command;
            }
            if (secchar == 'S') {
               sscanf(argpos,"%d",&temp);
               if (temp < 0 || temp > 1) {
                  fputs("Illegal color set!\n",console);
                  goto command;
               }
               color_set = temp;
               init_colors();
               goto command;
            }
            if (secchar == 'M') {
               sscanf(argpos,"%d",&temp);
               if (temp < 0 || temp > 7 || ((temp&4) && !(temp&1))) {
                  fputs("Illegal graphics mode!\n",console);
                  goto command;
               }
               color_mode = temp;
               if (!(color_mode & 0x2))
                  if (max_y > 200-STARTY)
                     max_y = 200-STARTY;
               if (!(color_mode & 0x4))
                  if (max_x > 320)
                     max_x = 320;
               goto command;
            }
            if (secchar == 'D') {
               sscanf(argpos,"%d",&temp);
               if (temp < 1) {
                  fputs("Divisor must be greater than 0!\n",console);
                  goto command;
               }
               color_div = temp;
               goto command;
            }
            if (secchar == 'T') {
               sscanf(argpos,"%d",&temp);
               if (temp < 0 || temp > 4095) {
                  fputs("Color must be between 0 and 4095!\n",console);
                  goto command;
               }
               color_inset = temp;
               goto command;
            }
            ill_cmd();
            goto command;
         case 'F':
            sscanf(stpblk(cmd+1),"%d",&temp);
            if (temp < 0 || temp > 1) {
               fputs("Function number must be 0 or 1!\n",console);
               goto command;
            }
            func_num = temp;
            goto command;
         case 'D':
            if (v_fp == NULL) {
               fputs("Must <G>enerate or <L>oad first!\n",console);
               goto command;
            }
            if (disp_mand() == NULL)
               wait_close();
            goto command;
         case 'G':
            if (gen_mand() == NULL)
               wait_close();
            goto command;
         case 'A':
            if (v_fp == NULL) {
               fputs("Must <G>enerate or <L>oad first!\n",console);
               goto command;
            }
            if (!(color_mode & 1)) {
               fputs("Cannot be in hold and modify!\n",console);
               goto command;
            }
            anal_mand();
            goto command;
         case ';':
            goto command; /* Lattice will complain about this line! */
         case '<':
            if (redir_fp) {
               fclose(redir_fp);
               redir_fp == NULL;
            }
            redir_fp = fopen(stpblk(cmd+1),"r");
            if (redir_fp == NULL)
               fprintf(console,"Can't open file %s!\n",stpblk(cmd+1));
            goto command;
         case 'Q':
            abort("Bye!");
         default:
            ill_cmd();
            goto command;
      }
   }
}

ill_cmd()
{
   fputs("Unknown command!\n",console);
}

float cnvf(i)
int i;
{
   union kludge n;

   n.i = i;
   n.i = SPTieee(n.i);
   return (n.f);
}


/*-----------------------------------------*/
/* Non-intelligent virtual memory handling */

v_pos_line(l)
int l;
{
   if (l < v_starty) {
      if (modified) 
         v_flush();
      v_starty = 0;
      fseek(v_fp,v_offset,0);
      if (want_read)
         fread(v_mand_store,max_x*sizeof(UWORD),max_mem,v_fp);
   }
   if (l-v_starty > max_mem-1) {
      if (modified)
         v_flush();
      v_starty += max_mem;
      fseek(v_fp,(long)(v_starty*max_x*sizeof(UWORD)+v_offset),0);
      if (want_read)
         fread(v_mand_store,max_x*sizeof(UWORD),max_mem,v_fp);
   }
}

v_flush()
{
   fseek(v_fp,(long)(v_starty*max_x*sizeof(UWORD)+v_offset),0);
   fwrite(v_mand_store,max_x*sizeof(UWORD),max_mem,v_fp);
   modified = FALSE;
}

abort(s)
char *s;
{
   if (cur_resource & F_MATHTRANS)
      CloseLibrary(MathTransBase);
   if (cur_resource & F_MATH)
      CloseLibrary(MathBase);
   if (cur_resource & F_CONSOLE)
      fclose(console);
   if (v_fp)
      fclose(v_fp);
   if (redir_fp)
      fclose(redir_fp);
   if (cur_resource & F_GRAPHICS)
      CloseLibrary(GfxBase);
   if (cur_resource & F_INTUITION)
      CloseLibrary(IntuitionBase);
   if (cur_resource & F_SETSTORE)
      free(v_mand_store);
   if (cur_resource & F_COLORTAB)
      free(color_table);

   puts(s);
   exit();
}