/*
 * MandelVroom 2.0
 *
 * (c) Copyright 1987,1989  Kevin L. Clague, San Jose, CA
 *
 * All rights reserved.
 *
 * Permission is hereby granted to distribute this program's source
 * executable, and documentation for non-comercial purposes, so long as the
 * copyright notices are not removed from the sources, executable or
 * documentation.  This program may not be distributed for a profit without
 * the express written consent of the author Kevin L. Clague.
 *
 * This program is not in the public domain.
 *
 * Fred Fish is expressly granted permission to distribute this program's
 * source and executable as part of the "Fred Fish freely redistributable
 * Amiga software library."
 *
 * Permission is expressly granted for this program and it's source to be
 * distributed as part of the Amicus Amiga software disks, and the
 * First Amiga User Group's Hot Mix disks.
 *
 * contents: this is the include file for MandelVroom standard includes.
 */

#include <exec/types.h>
#include <exec/lists.h>
#include <exec/nodes.h>
#include <exec/memory.h>
#include <exec/execbase.h>
#include <exec/semaphores.h>

#include <graphics/display.h>
#include <graphics/gfxbase.h>
#include <graphics/gfx.h>
#include <graphics/gfxmacros.h>

#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>

#include <workbench/startup.h>
#include <workbench/workbench.h>

#include <libraries/dosextens.h>

#include <iff/ilbm.h>

#include <stdio.h>
#include <functions.h>

/**************************************************************************
 *
 * color cycling stuff
 *
 *************************************************************************/

#define EXDepth 6        /* Maximum depth (6=HAM) */
#define maxColorReg 32
#define maxCycles   4
#define RNG_NORATE  36   /* Dpaint uses this rate to mean non-active */

#define REVERSE 0x2

typedef struct {
   WORD  pad1;   /* future exp - store 0 here */
   WORD  rate;   /* 60/sec=16384, 30/sec=8192, 1/sec=16384/60=273 */
   WORD  active; /* lo bit 0=no cycle, 1=yes; next bit 1=rvs */
   UBYTE low;    /* range lower */
   UBYTE high;   /* range upper */
   } CrngChunk;

/*
 * Mandelvroom uses the gadget ID's to decode the meaning of
 * gadget related messages.  It uses a special encoding to indicate
 * what window, and function this gadget is associated with.
 *
 * I split the gadget id into three fields:
 *
 *    window type - indicates how the window is being used (e.g.
 *      contour or palette)
 *
 *    gadget type - general classification of gadget within the window
 *
 *    gadget number - in the case of an array of gadgets (like the pens
 *      in the color palette) this is the array index.
 */

/*
 * This defines the bit positions of the fields within the gadget ID
 */

#define WINDTYPEBITS 11
#define TYPEBITS  6

/*
 * These masks are used for extracting the fields from gadgetIDs
 * when messages are received.
 */

#define WINDMASK 0xffff
#define TYPEMASK 0xf
#define NUMMASK 0x3f

#define WIND_TYPE(g) ((g) >> WINDTYPEBITS & WINDMASK)
#define GADG_TYPE(g) ((g) >> TYPEBITS & TYPEMASK)
#define GADG_NUM(g)  ((g) & NUMMASK)
#define CNT_NUM(a)   ((a)==CONTLAST ? NUMCONTS-1 : (a)+FirstContour)

/*
 * These are the window types
 */

#define PALTYPE   0 /* gadgetID indicates palette gadget */
#define CONTYPE   1 /* gadgetID indicates contour gadget */
#define PICTTYPE  2 /* gadgetID indicates picture gadget */
#define HELPTYPE  3 /* gadgetID indicates help gadget */
#define CYCTYPE   4 /* gadgetID indicates color cycling gadget */
#define ORBTTYPE  5 /* gadgetID indicates orbit gadget */

/*
 * These are the general classifications for palette gadgets
 */

#define PALPENS  0   /* pen gadget in the palette */
#define PALPOTS  1   /* Red, Green, Blue potentiometer gadgets */
#define PALCNTLS 2   /* Controls (commands like copy, blend, exchange) */

/*
 * Basis for individual ID expansion
 */

#define PALPEN   (PALTYPE << WINDTYPEBITS | PALPENS  << TYPEBITS)
#define PALPOT   (PALTYPE << WINDTYPEBITS | PALPOTS  << TYPEBITS)
#define PALCNTL  (PALTYPE << WINDTYPEBITS | PALCNTLS << TYPEBITS)

/*
 * These are the IDs for the command gadgets
 */

#define PALCOPY  (PALCNTL | 0)
#define PALRANGE (PALCNTL | 1)
#define PALEXCG  (PALCNTL | 2)

/*
 * Now we define the contour gadget IDs
 *
 * Start with the gadget types
 */

#define CONTSELS  0  /* Contour window Pens */
#define CONTUPS   1  /* Contour window increment gadget */
#define CONTDOWNS 2  /* Contour window decrement gadget */
#define CONTPOTS  3  /* Contour window potentiometer gadget */
#define CONTCNTLS 4  /* Contour window controls */

/*
 * Shift everything over so we can or in the specifics
 */

#define CONTSEL  (CONTYPE << WINDTYPEBITS | CONTSELS  << TYPEBITS)
#define CONTUP   (CONTYPE << WINDTYPEBITS | CONTUPS   << TYPEBITS)
#define CONTDOWN (CONTYPE << WINDTYPEBITS | CONTDOWNS << TYPEBITS)
#define CONTPOT  (CONTYPE << WINDTYPEBITS | CONTPOTS  << TYPEBITS)
#define CONTCNTL (CONTYPE << WINDTYPEBITS | CONTCNTLS << TYPEBITS)

#define CONTLAST  (CONTSEL | 32)

/*
 * Make up the command gadgets IDs for the contour window
 */

#define CONTRECOL (CONTCNTL | 0)
#define CONTSET   (CONTCNTL | 1)
#define CONTSMTH  (CONTCNTL | 2)
#define CONTCUT   (CONTCNTL | 3)
#define CONTCOPY  (CONTCNTL | 4)
#define CONTPASTE (CONTCNTL | 5)
#define CONTCEIL  (CONTCNTL | 9)
#define CONTBAR   (CONTCNTL | 10)

/*
 * Shift everything over so we can or in the specifics
 */

#define PICTCUR   (PICTTYPE << WINDTYPEBITS | 0)
#define PICTGEN   (PICTTYPE << WINDTYPEBITS | 1)
#define PICTIN    (PICTTYPE << WINDTYPEBITS | 2)
#define PICTOUT   (PICTTYPE << WINDTYPEBITS | 3)
#define PICTJULIA (PICTTYPE << WINDTYPEBITS | 4)

#define HELPUP     (HELPTYPE << WINDTYPEBITS | 0)
#define HELPDOWN   (HELPTYPE << WINDTYPEBITS | 1)
#define HELPSCROLL (HELPTYPE << WINDTYPEBITS | 2)

/*
 * These are the general classifications for cycle gadgets
 */

#define CYCRNUMS 0   /* cycle range indicators */
#define CYCCNTLS 1   /* Controls (Range changer, direction, and speed) */

/*
 * Basis for individual ID expansion
 */

#define CYCRNUM  (CYCTYPE << WINDTYPEBITS | CYCRNUMS << TYPEBITS)
#define CYCCNTL  (CYCTYPE << WINDTYPEBITS | CYCCNTLS << TYPEBITS)

/*
 * These are the IDs for the command gadgets
 */

#define CYCRANGE (CYCCNTL | 0)
#define CYCDIR   (CYCCNTL | 1)
#define CYCSPEED (CYCCNTL | 2)
#define CYCON    (CYCCNTL | 3)

#define ORBTCNTLS 0 /* controls (go into orbit mode) */
#define ORBTMODE  (ORBTCNTLS | 0)

/*
 * Now for some miscelaneous definitions
 *
 * Define the number of contours the system allows.
 */

#define NUMCONTS    256  /* This is total number of contours */
#define DISPCONTS    32  /* This is how many are displayable */

#define VERSION 200

/*
 * These definitions help us identify menu item selections.
 */

/* Menu Project's, Items */
#define NEWITEM      0
#define CURITEM      1
#define LOADITEM     2
#define SAVEPROJ     3
#define CLOSEPROJ    4
#define SAVEILBM     5
#define HELPITEM     6
#define CANCELITEM   7
#define QUITITEM     8

/* Display Menu's Items */
#define COLORITEM     0
#define CYCLEITEM     1
#define CONTOURITEM   2
#define AUTOCNTRITEM  3
#define HISTOGRAMITEM 4
#define BORDERITEM    5
#define DEPTHITEM     6
#define VIEWMODEITEM  7
#define SCREENITEM    8

/* Menu: Edit, Item: ViewMode's, SubItems */
#define HIRESSUB 0
#define INTERLACESUB 1
#define HALFBRITESUB 2

/* Menu: Edit, Item: Depth's, SubItems */
#define BITPLANE1SUB  0
#define BITPLANES2SUB 1
#define BITPLANES3SUB 2
#define BITPLANES4SUB 3
#define BITPLANES5SUB 4
#define BITPLANES6SUB 5

/* Menu: Edit, Item: Screen's SubItem */

#define WBENCHSUB  0
#define STANDARD   1

/* Calculate Menu's Items */
#define GENERATEITEM   0
#define ZOOMITEM       1
#define SCROLLITEM     2
#define LENSITEM       3
#define GENERATORITEM  4
#define COUNTITEM      5
#define STATSITEM      6

/* Menu: Edit, Item: ZoomIn's, SubItems */
#define ZOOMIN    0
#define ZOOMOUT   1
#define ZOOMOFF   2
#define ZOOMJULIA 3

/* Menu: Project, Item: Generate SubItems */
#define STARTGEN     0
#define STOPGEN      1
#define CONTGEN      2

/* Menu: Project, Item: Scroll SubItems */
#define SETSCROLL    0
#define GENSCROLL    1
#define CANCELSCROLL 2

/* Menu: Edit, Item: Generator, SubItems */
#define INTGENERATOR   0
#define FLOATGENERATOR 1
#define IEEEGENERATOR  2
#define INTIIGENERATOR 3
#define _81GENERATOR   4

/* Special Menu's Items */
#define PRESETITEM     0
#define ORBITITEM      1
#define ORBITMATHITEM  2
#define MAXORBITEM     3

/* Menu: Special, Item: OrbitMode, subitems */
#define INTORBIT  0
#define FFPORBIT  1
#define IEEEORBIT 2

/* Menu Entries */
#define PROJECTMENU    0
#define DISPLAYMENU    1
#define CALCULATEMENU  2
#define SPECIALMENU    3

#define TOGGLED11 0x0200

/* State definitions */

#define COPYRIGHTSTATE    0
#define IDLESTATE         1
#define COPYRGBSTATE      2
#define SPREADRGBSTATE    3
#define XCHGRGBSTATE      4
#define SLIDERGBSTATE     5
#define SETHEIGHTSTATE    6
#define SETCONTSTATE      7
#define SMOOTHCONTSTATE   8
#define CUTCONTSTATE      9
#define COPYCONTSTATE    10
#define PASTECONTSTATE   11
#define CEILINGSTATE     12
#define SLIDEBARSTATE    13
#define ORBITSTATE       14
#define ZOOMINSTATE      15
#define RESIZEZOOMSTATE  16
#define PROPRESIZESTATE  17
#define ZOOMDRAGSTATE    18
#define QUERYHEIGHTSTATE 19
#define SETJULIASTATE    20
#define SLIDESPEEDSTATE  21
#define CYCLERANGESTATE  22
#define HELPSTATE        23
#define SCROLLHELPSTATE  24
#define SCROLLPICTSTATE  25

#define PARSEOK      0
#define PARSEBAD     1

#define ZOOMCLOSEHIT  0
#define ZOOMDRAGHIT   1
#define ZOOMRESIZEHIT 2
#define PROPRESIZEHIT 3
#define NOTHINGHIT    4

/***************************************************************************
 *
 *  Structure for Mandelbrot and Julia Pictures
 *
 **************************************************************************/

struct Rect {
  int Left, Top, Right, Bot;
};

struct Picture {
  struct Node pNode;          /* for a linked list of pictures */

  struct List    zList;       /* Pictures that have zoom     */
                              /* open in this picture        */
  struct Node    zNode;       /* Used to maintain the above  */

  struct Picture *DrawPict;   /* Picture that zoom box is in */

  UBYTE  ZoomType;            /* Zoom In or Out              */
  int    NavTop,  NavBot;     /* Zoom box location and size  */
  int    NavLeft, NavRight;


  struct NewWindow *NewWind;  /* NewWindow structure         */
  struct Window    *Window;   /* Open Intuition window       */

  struct RastPort ScrollRp;   /* rastport for saved image during scroll */
  struct BitMap   ScrollBitMap; /* bitmap for saved image */

                              /* Window origin for scroll*/
  struct Rect ImageLoc;       /* Scroll's image location in Pict->Window*/
  struct Rect ClipImage;      /* location after clipping to Window */
  struct Rect DataClip;       /* clipping also reflected in data */

  struct Gadget    *SizingGadget; /* sizing place            */
                              /* Sempahore for window        */
  struct SignalSemaphore WindowSemi;

  struct Gadget    *Gadgets;

  char   Title[40];           /* Window Title                */

  SHORT *Counts;              /* Pointer to data area. One   */
  LONG   CountsSize;          /* SHORT per pixel.            */
                              /* CountX * CountY * sizeof( SHORT ) */

  ULONG  Flags;               /* Flags regarding Picture specifics */

  int    LeftMarg,  TopMarg;
  int    RightMarg, BotMarg;

  struct Task     *gTask;     /* Task that generates this    */
  LONG   gSigBit, gSigMask;   /* Signal to free up paused tasks */
  UBYTE  GenState;            /* Task state                  */
  UBYTE  GenChildState;       /* Task state                  */
  int    CurLine;             /* Index of Line generator is on */

  struct Task     *cTask;     /* Task for Recolor Slow       */
  UBYTE  ColorState;          /* Task state                  */
  UBYTE  ColorChildState;          /* Task state                  */

  struct Histogram *Hist;     /* Projects Histogram structure */

  SHORT OldMaxIter;           /* used for recalc */
  LONG   TimeStamp[3];        /* Time stamp for calculation */

  /***********************************************************/

  double Real,     Imag;      /* For Julia only (Julia Seed) */
                              /* Set it to 0.0 for Mand      */
  double RealLow,  ImagLow;   /* Screen Boundaries for Julia */
  double RealHigh, ImagHigh;  /* Complex Plane Origin for Mand */
  double RealGap,  ImagGap;   /* Real and Imaginary parts of */

  SHORT  LeftEdge, TopEdge;   /* Window position on screen   */
  SHORT  CountX, CountY;      /* Picture dimensions          */

  SHORT  MaxIteration;
  UBYTE  MathMode;            /* Int32, FFP, IEEE            */

  SHORT  Heights[256];        /* Contours' heights and pens  */
  UBYTE  Pens[256];

  UBYTE  ClrXlate[1024];      /* There are MaxIteration entries */

  SHORT  RGBs[32];            /* Red, Green, Blue values     */
  USHORT ViewModes;
  SHORT  Depth;

  SHORT  CycleOn;             /* Color Cycling stuff */
  CrngChunk Crngs[maxCycles];
};

/* Picture.Types */

#define JULIAPICT 1
#define MANDPICT  2

/* Picture Flags */

#define NO_RAM_GENERATE 0x00000001
#define LENS_DISPLAYED  0x00000002
#define ZOOM_BOX_OPEN   0x00000004
#define SCROLL_HAPPENED 0x00000008
#define PROJECT_CHANGED 0x00000010
#define BORDERLESS_PROJ 0x00000020

#define GENPENDSTATE    1
#define CONTINUESTATE   2
#define GENERATESTATE   3
#define PAUSESTATE      4
#define FINISHEDSTATE   5
#define KILLSTATE       6

#define RECOLORINCOMPLETE 0
#define RECOLORCOMPLETE   1

#define GENINCOMPLETE 0
#define GENCOMPLETE   1
#define NOSIGSTATE    2

#define INT1632MATH 0
#define FFPMATH     1
#define IEEEMATH    2
#define INT3232MATH 3
#define M68881MATH  4

#define INTORBMATH  0
#define FFPORBMATH  1
#define IEEEORBMATH 2

/***************************************************************************
 *
 *  External variables
 *
 **************************************************************************/

/* These are global system variables */

extern struct Picture *CurPict;

extern int CurContour;
extern SHORT  NumContours;

extern LONG   TopMarg, BotMarg, LeftMarg, RightMarg;

extern struct Screen *screen;
extern struct ViewPort *vp;
extern int Num_vp_Colors;

extern struct Window *PalWind, *ContWind, *StatsWind, *HelpWind;
extern struct Window *HistWind,*CycWind, *OrbitWind;
extern struct Window *CurWind, *CurPlot, *ClosedWind, *BackWind;

extern struct Menu Menu[];

extern SHORT  XScale, YScale;
extern SHORT  MouseX, MouseY;

extern LONG CurPen;

extern UBYTE  ClosePlot;
extern UBYTE  QuitScreen;

extern LONG   mSigBit,mSigMask;
extern struct Task *mTask;

extern BYTE FromWB;

extern BYTE State, SubState, Parse_rc;

struct Window *OpenMyWind();

struct Gadget *MakeBool(), *MakePot(), *MakeString();
struct IntuiText *MakeIntui(), *ShadowIntui();
struct Border *ShadowBorder();

#define AddSemaphore myAddSemaphore

/*
 * Picture window ( Julia and Mandlebrot windows ) margins
 */

#define TOPMARG   TopMarg
#define BOTMARG   BotMarg
#define LEFTMARG  LeftMarg
#define RIGHTMARG RightMarg

#define SUCCESSFUL   0
#define UNSUCCESSFUL -1

#define ChildPause(Pict) {if(Pict->GenState==PAUSESTATE)ChildSignal(Pict);}

/**************************************************************************
 *
 * Histogram stuff
 *
 *************************************************************************/

struct Histogram {
  int *Table;
  int  TableSize;
};

#define AllocHistStruct() (struct Histogram *)safeAllocMem(sizeof(struct Histogram), MEMF_CLEAR)
#define FreeHistStruct(p) FreeMem(p,sizeof(struct Histogram))

#define AllocHistTable(s) (int *)safeAllocMem(s*sizeof(int), MEMF_CLEAR)
#define FreeHistTable(p)  FreeMem(p->Table,p->TableSize*sizeof(int))

#define AddHead(List,Node) safeAddHead(List,Node,__FILE__,__LINE__,__FUNC__)
#define Remove(Node)       safeRemove(Node,__FILE__,__LINE__,__FUNC__)

/**************************************************************************
 *
 * standard stuff
 *
 *************************************************************************/

#define BEVELEDUP    0
#define BEVELEDDOWN  1

#define NORMALPEN    1
#define SHADOWPEN    3
#define MEDIUMPEN    2
#define HIGHLIGHTPEN 2
#define POTPEN       3

#ifdef MEM_DEBUG

#include "safeallocmem.h"

#else

APTR safeAllocMem();

#endif

