home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff214.lzh / MandelVroom / src / main.c < prev    next >
C/C++ Source or Header  |  1989-05-30  |  12KB  |  589 lines

  1. /*
  2.  * MandelVroom 2.0
  3.  *
  4.  * (c) Copyright 1987,1989  Kevin L. Clague, San Jose, CA
  5.  *
  6.  * All rights reserved.
  7.  *
  8.  * Permission is hereby granted to distribute this program's source
  9.  * executable, and documentation for non-comercial purposes, so long as the
  10.  * copyright notices are not removed from the sources, executable or
  11.  * documentation.  This program may not be distributed for a profit without
  12.  * the express written consent of the author Kevin L. Clague.
  13.  *
  14.  * This program is not in the public domain.
  15.  *
  16.  * Fred Fish is expressly granted permission to distribute this program's
  17.  * source and executable as part of the "Fred Fish freely redistributable
  18.  * Amiga software library."
  19.  *
  20.  * Permission is expressly granted for this program and it's source to be
  21.  * distributed as part of the Amicus Amiga software disks, and the
  22.  * First Amiga User Group's Hot Mix disks.
  23.  *
  24.  * contents: this file contains main(), Workbench support code, and
  25.  * miscellaneous funtions.
  26.  */
  27.  
  28. #define COLORCYCLE
  29.  
  30. #include "mandp.h"
  31.  
  32. extern struct NewScreen NewScreen;
  33. extern struct Picture *ZoomedPict;
  34.  
  35. extern UBYTE ContOpen, PaletteOpen, CycleOpen, HistOpen;
  36.  
  37. struct Window  *CurWind;
  38. struct Picture *CurPict;
  39.  
  40. struct Window *ClosedWind;
  41.  
  42. UBYTE  QuitScreen;
  43.  
  44. SHORT MouseX,MouseY;
  45.  
  46. LONG mSigBit, mSigMask; /* Finished Signal */
  47. struct Task *mTask;
  48.  
  49. extern int Num_vp_Colors;
  50.  
  51. extern LONG MainPri, TaskPri;
  52.  
  53. extern struct WBStartup *WBenchMsg;
  54. struct IntuiMessage *ParseMsg;
  55.  
  56. BYTE FromWB;
  57.  
  58. /*
  59.  * Do it all
  60.  */
  61. main (argc, argv)
  62.   int   argc;
  63.   char *argv[];
  64.  {
  65.   struct WBArg     *arg;
  66.   char  *LoadFile;
  67.   char  *NameFromLock();
  68.  
  69.   struct Process *Process;
  70.  
  71.   int i, rc;
  72.  
  73.   AllocArrows();
  74.  
  75.   ToggleEnableds();
  76.  
  77.   Menu[CALCULATEMENU].Flags ^= MENUENABLED;
  78.  
  79.   MainPri = FindTask(0)->tc_Node.ln_Pri;
  80.   TaskPri = MainPri - 1;
  81.  
  82.   FromWB = argc == 0;
  83.  
  84.   Process = (struct Process *) FindTask(0);
  85.  
  86.   /* Set up Mandelbrot Pict */
  87.  
  88.   if (OpenLibs() == 0 && OpenTasks() == 0 && OpenDisp() == 0) {
  89.  
  90.     DefaultColors();
  91.  
  92.     if (FromWB) {
  93.       argc = WBenchMsg->sm_NumArgs;
  94.       arg = WBenchMsg->sm_ArgList;
  95.     }
  96.  
  97.     if (argc == 1) {
  98.  
  99.       if (SetPreset( 0 ) == UNSUCCESSFUL)
  100.         AndDie( "Can't open preset 0. No RAM\n" );
  101.  
  102.     } else {
  103.  
  104.       rc = SUCCESSFUL;
  105.  
  106.       /* get all the projects indicated */
  107.  
  108.       for (i = 1; i < argc && rc == SUCCESSFUL; i++) {
  109.  
  110.         if (FromWB) {
  111.  
  112.           arg++;
  113.           rc = LoadPicture( NameFromLock( (struct Lock *) arg->wa_Lock,
  114.                                            arg->wa_Name ) );
  115.         } else {
  116.  
  117.           rc = LoadPicture( NameFromLock( (struct Lock *)
  118.                                            Process->pr_CurrentDir), argv[i]);
  119.  
  120.         }
  121.  
  122.         if (rc == UNSUCCESSFUL) {
  123.  
  124.           DispErrMsg("Failure opening project",0);
  125.         } else {
  126.  
  127.           if (CurPict->Flags & NO_RAM_GENERATE)
  128.             rc = UNSUCCESSFUL;
  129.         }
  130.       }
  131.     }
  132.  
  133.     DoBrot();
  134.   }
  135.  
  136. #ifdef CHECK_TASK_STACK
  137.   PrintStack();
  138. #endif
  139.  
  140.   ThrowPicts();
  141.  
  142.   CloseTasks();
  143.   CloseDisp();
  144.   CloseLibs();
  145.  
  146.   FreeArrows();
  147.  
  148. #ifdef MEM_DEBUG
  149.   TERMINATE();
  150. #endif
  151. }
  152.  
  153. char *
  154. NameFromLock(Lock,WBName)
  155.   struct Lock *Lock;
  156.   char  *WBName;
  157. {
  158.   struct FileInfoBlock *fib;
  159.   struct Lock *lock,*newlock;
  160.  
  161.   static char cwd[255];
  162.  
  163.   int i,len;
  164.   char *name;
  165.  
  166.   i = 255;
  167.   cwd[255] = '\0';
  168.  
  169.   len = strlen( WBName );
  170.   i -= len;
  171.   movmem(WBName, cwd + i, (unsigned) len);
  172.  
  173.   fib = (struct FileInfoBlock *)
  174.         safeAllocMem( (long) sizeof(struct FileInfoBlock), MEMF_CLEAR );
  175.  
  176.   if (fib == NULL) {
  177.     DispErrMsg("Can't Allocate FileInfoBlock",0);
  178.     return(NULL);
  179.   }
  180.  
  181.   lock = Lock;
  182.  
  183.   while (lock) {                    /* while the lock is good */
  184.  
  185.     newlock = (struct Lock *) ParentDir(lock);
  186.     (void) Examine(lock, fib);
  187.  
  188.     name = fib->fib_FileName;
  189.  
  190.     if (*name == '\0')
  191.        name = "ram";
  192.     len = strlen(name);
  193.  
  194.     if (newlock) {
  195.  
  196.       i -= len + 1;
  197.       movmem(name, cwd + i, (unsigned) len);
  198.       cwd[i+len] = '/';
  199.  
  200.     } else {
  201.  
  202.       i -= len + 1;
  203.       movmem(name, cwd + i, (unsigned) len);
  204.       cwd[i + len] = ':';
  205.     }
  206.  
  207.     if (lock != Lock)
  208.       UnLock(lock);
  209.     lock = newlock;
  210.   }
  211.   movmem(cwd + i, cwd, (unsigned) 256 - i);
  212.  
  213.   FreeMem( (char *) fib, (long) sizeof(struct FileInfoBlock) );
  214.  
  215.   return( cwd );
  216. }
  217.  
  218. /*
  219.  * Wait and process a message
  220.  */
  221. DoBrot()
  222. {
  223.   LONG bSigMask = 1 << BackWind->UserPort->mp_SigBit;
  224.   LONG rc;
  225.  
  226.   while (QuitScreen == FALSE) {
  227.  
  228.     rc = Wait( bSigMask | mSigMask );
  229.  
  230.     if (rc & bSigMask) DoMsg();
  231.     if (rc & mSigMask) ServiceTasks();
  232.   }
  233.  
  234.   if (CurPict == NULL) {
  235.     GetCurPict();
  236.   }
  237. }
  238.  
  239. /*
  240.  * if there is a message, process it
  241.  */
  242. DoMsg()
  243. {
  244.   register SHORT  MouseMoved, Activate;
  245.   register struct IntuiMessage *message;
  246.   register struct IntuiMessage *CopyMsg;
  247.   struct IntuiMessage LocalMsg;
  248.   struct IntuiMessage MouseMsg;
  249.   struct IntuiMessage ActiveMsg;
  250.  
  251.   MouseMoved = FALSE;
  252.   Activate   = FALSE;
  253.  
  254.   while (message = (struct IntuiMessage *) GetMsg( BackWind->UserPort)) {
  255.  
  256.     if (message->Class == MOUSEMOVE) {
  257.       CopyMsg = &MouseMsg;
  258.       MouseMoved = TRUE;
  259.     } else
  260.     if (message->Class == ACTIVEWINDOW) {
  261.       CopyMsg  = &ActiveMsg;
  262.       Activate = TRUE;
  263.     } else {
  264.       CopyMsg = &LocalMsg;
  265.     }
  266.  
  267.     CopyMsg->Class       = message->Class;
  268.     CopyMsg->Code        = message->Code;
  269.     CopyMsg->IAddress    = message->IAddress;
  270.     CopyMsg->MouseX      = message->MouseX;
  271.     CopyMsg->MouseY      = message->MouseY;
  272.              MouseX      = message->MouseX;
  273.              MouseY      = message->MouseY;
  274.     CopyMsg->IDCMPWindow = message->IDCMPWindow;
  275.  
  276.     /* got a message */
  277.  
  278.     ReplyMsg(message);
  279.  
  280.     if (CopyMsg->Class != MOUSEMOVE && CopyMsg->Class != ACTIVEWINDOW) {
  281.       if (CopyMsg->Class == MOUSEBUTTONS && MouseMoved) {
  282.         ProcessCmd( &MouseMsg );
  283.         MouseMoved = FALSE;
  284.       }
  285.       ProcessCmd( CopyMsg );
  286.     }
  287.   }
  288.  
  289.   /* If the Mouse moved during the loop, respond to it now */
  290.   if ( MouseMoved ) {
  291.     ProcessCmd( &MouseMsg );
  292.   }
  293.  
  294.   /* If there was one or more WindowActive then service the last one */
  295.   if ( Activate ) {
  296.     ProcessCmd( &ActiveMsg );
  297.   }
  298. } /* DoMsg */
  299.  
  300. UBYTE ScreenSizeChanged;
  301.  
  302. MaybeNewScreen()
  303. {
  304.   int rc;
  305.  
  306.   extern USHORT NewViewModes;
  307.   extern UBYTE  NewDepth;
  308.  
  309. #define VIEW_MODE_MASK (HIRES|INTERLACE|EXTRA_HALFBRITE)
  310.  
  311.   if (screen->BitMap.Depth  != NewDepth ||
  312.       (screen->ViewPort.Modes & VIEW_MODE_MASK) != NewViewModes ||
  313.        ScreenSizeChanged ) {
  314.  
  315.     if (CheckNewScreen( NewViewModes ) == SUCCESSFUL) {
  316.  
  317.       NewScreen.ViewModes = NewViewModes;
  318.       NewScreen.Depth = NewDepth;
  319.  
  320.       CloseDisp();
  321.       (void) OpenDisp();
  322.       rc = 1;
  323.     } else {
  324.       DispErrMsg("Can't change screen. Projects too big",0);
  325.       rc = 0;
  326.     }
  327.     ScreenSizeChanged = 0;
  328.   } else {
  329.     rc = 0;
  330.   }
  331.   NewViewModes = NewScreen.ViewModes;
  332.   NewDepth = NewScreen.Depth;
  333.   return( rc );
  334. }
  335.  
  336. ActivatePict( Window )
  337.   register struct Window *Window;
  338. {
  339.   SetMouse( Window );
  340.  
  341.   CurWind = Window;
  342.  
  343.   DisplayMsg();
  344. }
  345.  
  346. CloseWinds( Window )
  347.   register struct Window *Window;
  348. {
  349.   register struct Picture *NewPict;
  350.   extern UBYTE ContOpen, OrbitOpen, StatsOpen;
  351.  
  352.   ForceNormPointer();
  353.  
  354.   if ( Window == ContWind) {
  355.  
  356.     CloseContWind();
  357.     ContOpen = 0;
  358.  
  359.   } else
  360.   if ( Window == HistWind) {
  361.  
  362.     CloseHistWind();
  363.     HistOpen = 0;
  364.  
  365.   } else
  366.   if ( Window == PalWind) {
  367.  
  368.     ClosePalWind();
  369.     PaletteOpen = 0;
  370.  
  371.   } else
  372.   if ( Window == CycWind) {
  373.  
  374.     CloseCycWind();
  375.     CycleOpen = 0;
  376.  
  377.   } else
  378.   if ( Window == HelpWind) {
  379.  
  380.     CloseHelpWind(0,NULL);
  381.  
  382.   } else
  383.   if ( Window == StatsWind) {
  384.  
  385.     CloseStatsWind();
  386.     StatsOpen = 0;
  387.  
  388.   } else
  389.   if ( Window == OrbitWind) {
  390.  
  391.     CloseOrbitWind();
  392.     OrbitOpen = 0;
  393.  
  394.   } else {
  395.  
  396.     if ( NewPict = (struct Picture *) Window->UserData  ) {
  397.  
  398.       /* Erase the Zoom box if it is open */
  399.  
  400.       if (NewPict->DrawPict)
  401.         ZoomOnOff( NewPict );
  402.  
  403.       /* Undo any pictures that have zoom boxes open in this window */
  404.  
  405.       CloseZoomedPicts( NewPict );
  406.  
  407.       ThrowPict( NewPict );
  408.  
  409.       /* Figure out who is next */
  410.  
  411.       GetCurPict();
  412.  
  413.       if (CurPict == NULL) {
  414.  
  415.         /* Close the contour window */
  416.  
  417.         CloseContWind();  ContOpen    = 0;
  418.         ClosePalWind();   PaletteOpen = 0;
  419.         CloseCycWind();   CycleOpen   = 0;
  420.         CloseOrbitWind(); OrbitOpen   = 0;
  421.         CloseHistWind();  HistOpen    = 0;
  422.         CloseStatsWind(); StatsOpen   = 0;
  423.       } else {
  424.  
  425.         LoadRGB4(vp, CurPict->RGBs, (long) Num_vp_Colors);
  426.         RefreshContours();
  427.       }
  428.     }
  429.   }
  430. }
  431.  
  432. ToggleEnableds( )
  433. {
  434.   extern struct MenuItem ProjectItems[];
  435.   extern struct MenuItem DisplayItems[];
  436.   extern struct MenuItem SpecialItems[];
  437.   extern struct Menu     Menu[];
  438.  
  439.   ProjectItems[CURITEM].Flags   ^= ITEMENABLED;
  440.   ProjectItems[SAVEPROJ].Flags  ^= ITEMENABLED;
  441.   ProjectItems[CLOSEPROJ].Flags ^= ITEMENABLED;
  442.   ProjectItems[SAVEILBM].Flags  ^= ITEMENABLED;
  443.  
  444.   DisplayItems[COLORITEM].Flags     ^= ITEMENABLED;
  445.   DisplayItems[CYCLEITEM].Flags     ^= ITEMENABLED;
  446.   DisplayItems[CONTOURITEM].Flags   ^= ITEMENABLED;
  447.   DisplayItems[AUTOCNTRITEM].Flags  ^= ITEMENABLED;
  448.   DisplayItems[HISTOGRAMITEM].Flags ^= ITEMENABLED;
  449.   DisplayItems[BORDERITEM].Flags    ^= ITEMENABLED;
  450.  
  451.   Menu[CALCULATEMENU].Flags ^= MENUENABLED;
  452.  
  453.   SpecialItems[ORBITITEM].Flags     ^= ITEMENABLED;
  454.   SpecialItems[ORBITMATHITEM].Flags ^= ITEMENABLED;
  455.   SpecialItems[MAXORBITEM].Flags    ^= ITEMENABLED;
  456. }
  457.  
  458. BorderWindow( Window )
  459.   register struct Window *Window;
  460. {
  461.   register struct RastPort *Rp;
  462.   register LONG   Bottom, Right;
  463.   register struct Picture *Pict;
  464.   register LONG   Max;
  465.   struct Gadget *Sizing;
  466.   int      Top;
  467.  
  468.   extern struct Gadget *OrbitResize;
  469.  
  470.   Pict = (struct Picture *) Window->UserData;
  471.  
  472.   if (Pict && (Pict->Flags & BORDERLESS_PROJ))
  473.     return;
  474.  
  475.   Bottom = Window->Height - 1;
  476.   Right  = Window->Width  - 1;
  477.  
  478.   Rp = Window->RPort;
  479.  
  480.   if (Pict || Window == OrbitWind) {
  481.  
  482.     if (Pict) {
  483.       ObtainSemaphore( &Pict->WindowSemi );
  484.       Sizing = Pict->SizingGadget;
  485.     } else {
  486.       Sizing = OrbitResize;
  487.     }
  488.  
  489.     SetAPen( Rp, NORMALPEN );
  490.  
  491.     Move( Rp, LEFTMARG,                Bottom - 1);
  492.     Draw( Rp, Right - RIGHTMARG/2 - 1, Bottom - 1);
  493.  
  494.     if (Pict) {
  495.       if (Pict->pNode.ln_Type == MANDPICT) {
  496.         Max = 10+4*14;
  497.       } else {
  498.         Max = 10+5*14;
  499.       }
  500.     } else {
  501.       Max = 10+1*14;
  502.     }
  503.  
  504.     if ( Bottom > Max ) {
  505.  
  506.       RectFill( Rp, Right - RIGHTMARG + 1, Max, Right - 1, Bottom - 1);
  507.       RefreshGadgets( Window->FirstGadget, Window, NULL);
  508.     }
  509.  
  510.     /* Bevel the resize gadget edges */
  511.  
  512.     Max = Right + Sizing->LeftEdge;
  513.     Top = Bottom+ Sizing->TopEdge-1+YScale;
  514.  
  515.     SetAPen( Rp, HIGHLIGHTPEN );
  516.     Move(Rp, Max,            Bottom-2+YScale);
  517.     Draw(Rp, Max,            Top);
  518.     Draw(Rp, Right-2+XScale, Top);
  519.     SetAPen( Rp, SHADOWPEN );
  520.     Move(Rp, Right-2+XScale, Top+1);
  521.     Draw(Rp, Right-2+XScale, Bottom-2+YScale);
  522.     Draw(Rp, Max,            Bottom-2+YScale);
  523.   }
  524.  
  525.   SetAPen( Rp, SHADOWPEN );
  526.   WritePixel( Rp, (long) 0, TOPMARG );
  527.  
  528.   Move( Rp, (long) 0,  Bottom );
  529.   Draw( Rp, Right,     Bottom );
  530.   Draw( Rp, Right,     TOPMARG);
  531.  
  532.   SetAPen( Rp, HIGHLIGHTPEN );
  533.  
  534.   Move( Rp, (long) 0,     TOPMARG + 1);
  535.   Draw( Rp, (long) 0,     Bottom - 1 );
  536.  
  537.   if (Pict)
  538.     ReleaseSemaphore( &Pict->WindowSemi );
  539. }
  540.  
  541. MakeCurProj( NewPict )
  542.   register struct Picture *NewPict;
  543. {
  544.   extern USHORT NewViewModes;
  545.   extern UBYTE  NewDepth;
  546.  
  547.   if ( NewPict ) {
  548.  
  549.     if ( NewPict != CurPict ) {
  550.  
  551.       SetWindowActive( NewPict->Window , '*' );
  552.  
  553.       if (CurPict) {
  554.         SetWindowActive( CurPict->Window, ' ' );
  555.       }
  556.       SetOnString( NewPict->CycleOn );
  557.  
  558.       KillCycle();
  559.  
  560.       CurPict = NewPict;
  561.  
  562.       NewViewModes = NewPict->ViewModes;
  563.       NewDepth     = NewPict->Depth;
  564.  
  565.       InitGenSubs();
  566.       InitBorderSubs();
  567.       InitOrbitSubs();
  568.  
  569.       if (MaybeNewScreen() == 0) {
  570.  
  571.         LoadRGB4(vp, NewPict->RGBs, (long) Num_vp_Colors);
  572.         SetColorProps(CurPen);
  573.  
  574.         ReDoHist(CurPict);
  575.         ShowStats(CurPict);
  576.         RefreshContours();
  577.       }
  578.  
  579.       ModifySpeedPot();
  580.       SetDirGadget(CurPict);
  581.  
  582.       if (CurPict->CycleOn) {
  583.         CreateCycle();
  584.       }
  585.     }
  586.   }
  587. }
  588.  
  589.