/* mandel.c - Server fr Mandel.Occ */

#include <stdlib.h>                             /* atof,atoi */
#include <stdio.h>                              /* printf */

int xpoints,ypoints;                            /* Bildschirm */
double left,right,top,bottom;                   /* Parameter */
int maxiter;
int mx,my,mb;                                   /* Mauszustand */

#include "graph256.h"
#include "trproc.h"

void resetmouse(int x,int y) {
  union REGS regs;
  regs.x.ax=0;                                  /* Maus reseten */
  int86(0x33,&regs,&regs);
  regs.x.ax=7;                                  /* Mausbereich horizontal */
  regs.x.cx=0;
  regs.x.dx=x-1;
  int86(0x33,&regs,&regs);
  regs.x.ax=8;                                  /* Mausbereich vertikal */
  regs.x.cx=0;
  regs.x.dx=y-1;
  int86(0x33,&regs,&regs); }

void getmouse() {
  union REGS regs;
  regs.x.ax=3;                                  /* Mauszustand holen */
  int86(0x33,&regs,&regs);
  mx=regs.x.cx; my=regs.x.dx; mb=regs.x.bx & 3; }

void drawpicture() {
  char line[1024];
  int y;
  loadtr("mandel.t8");                          /* Transputer starten */
  puttrint(xpoints); puttrint(ypoints);         /* Bildgr”sse */
  puttrdouble(left); puttrdouble(right);        /* Parameter */
  puttrdouble(top); puttrdouble(bottom);
  puttrint(maxiter);
  for (y=0; y<ypoints; y++) {
    gettr(line, xpoints);                       /* Zeile von Transputer */
    setline(y,line);                            /* Zeile zeichnen */
    getmouse();
    if (mb==2)                                  /* Maustaste 2 gedrckt? */
      break; }                                  /* = nicht mehr zeichnen */
  invcursor(mx,my); }                           /* Cursor setzen */

void newdrawpicture(int x1,int y1,int x2,int y2) {
  double temp;
  temp=left+(right-left)*(double)x1/(double)(xpoints-1);
  right=left+(right-left)*(double)x2/(double)(xpoints-1);
  left=temp;
  temp=top-(top-bottom)*(double)y1/(double)(ypoints-1);
  bottom=top-(top-bottom)*(double)y2/(double)(ypoints-1);
  top=temp;
  maxiter*=2;                                   /* doppelt so tief */
  drawpicture(); }                              /* zeichnen */

void main(int argc,char *argv[]) {
  left=-2.0; right=1.0;                         /* Defaultausschnitt */
  top=1.125; bottom=-top;
  maxiter=32;
  if (argc>1) {                                 /* erster Ausschnitt */
    left=atof(argv[1]);                         /* gewnschte Werte setzen */
    right=atof(argv[2]);
    top=atof(argv[3]);
    bottom=atof(argv[4]);
    maxiter=atoi(argv[5]); }
  setvideo(GRAPH640x480);
  resetmouse(xpoints,ypoints);                  /* Bildgr”sse festlegen */
  drawpicture();                                /* erstes Bild zeichnen */
  for (;;) {
    int lastmx,lastmy,lastmb,x1,y1;
    lastmx=mx; lastmy=my; lastmb=mb;
    getmouse();
    if (lastmb == 0 & mb == 1) {                /* Taste 1 gedrckt? */
      x1=mx; y1=my;                             /* = links oben */
      invcursor(lastmx,lastmy);                 /*   Cursor l”schen */
      invbox(x1,y1,mx,my);                      /*   neu Kasten setzen */
      continue; }
    if (lastmb == 1 & mb == 0) {                /* Taste 1 losgelassen */
      newdrawpicture(x1,y1,mx,my);              /* = naechstes Bild zeichnen */
      continue; }
    if (lastmb == 1 & mb == 3) {                /* Taste 2 zus. gedrckt? */
      invbox(x1,y1,lastmx,lastmy);              /* = Kasten l”schen */
      invcursor(mx,my);                         /*   neu Cursor setzen */
      continue; }
    if (lastmb == 0 & mb == 2)                  /* Taste 2 gedrckt? */
      break;                                    /* = Ende */
    if (mx != lastmx | my != lastmy) {          /* Maus bewegt? */
      if (lastmb == 1)                          /*   Taste 1 war gedrckt? */
	invbox(x1,y1,lastmx,lastmy);            /*   = alten Kasten l”schen */
      else
	invcursor(lastmx,lastmy);               /*     sonst Cursor l”schen */
      if (mb == 1)                              /*   Taste 1 ist gedrckt? */
	invbox(x1,y1,mx,my);                    /*   = neuen Kasten setzen */
      else
	invcursor(mx,my); } }                   /*     sonst Cursor setzen */
  setvideo(TEXT80x25);
  printf("Links=%lf, Rechts=%lf, Oben=%lf, Unten=%lf, Tiefe=%d",
    left,right,top,bottom,maxiter); }

