home *** CD-ROM | disk | FTP | other *** search
/ 3D GFX / 3D GFX.iso / amiutils / i_l / julia / julia.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-31  |  5.0 KB  |  266 lines

  1. /*
  2.     Julia
  3.     Source is for Manx Aztec C 5.0d
  4.     Modifications are necessary to compile this on gnuc (i.e. the bases)
  5.     status: Freeware
  6.     (C) Thomas Radtke 1995
  7. */
  8.  
  9. #include <math.h>
  10. #include <stdlib.h>
  11. #include <intuition/intuitionbase.h>
  12. #include <stdio.h>
  13.  
  14. struct IntuitionBase *IntuitionBase;
  15. struct GfxBase *GfxBase;
  16.  
  17. #define rp CustWindow->RPort
  18.  
  19. struct NewWindow BlankWindow=
  20. {
  21.     50,50,200,100,1,2,
  22.     MOUSEBUTTONS | CLOSEWINDOW | NEWSIZE,
  23.     WINDOWSIZING | WINDOWCLOSE | GIMMEZEROZERO | SMART_REFRESH | WINDOWDEPTH | ACTIVATE | WINDOWDRAG,
  24.     0,0,0,0,0,0,0,0,0,
  25.     PUBLICSCREEN
  26. };
  27.  
  28. #define ran() ((double)rand()/(double)RAND_MAX)
  29.  
  30. main()
  31. {
  32.     double icon,rcon,is,di,rs,dr,iinc,rinc;
  33.     USHORT i,j,n,colors;
  34.     USHORT x,y,xsize,ysize;
  35.     double re2,re,im,restart,imstart;
  36.     char name[256];
  37.     int col[2];
  38.     int paint=1;
  39.     double memi,memr;
  40.     short tmp,startx,starty,xneu,yneu;
  41.     double dx,dy;
  42.     struct Screen *PubScreen;
  43.     struct Window *CustWindow;
  44.     struct IntuiMessage *message;
  45.     struct IntuiMessage *GetMsg();
  46.     ULONG color,coltab[256],pentab[256];
  47.  
  48.     if ((IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0))==NULL) exit(1);
  49.     if ((GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0))==NULL) exit(1);
  50.  
  51. /*    get Number of colors and screensize    */
  52.  
  53.     GetDefaultPubScreen(name);
  54.     PubScreen=(struct Screen *)LockPubScreen(name);
  55.  
  56.     colors=1<<(PubScreen->BitMap.Depth);
  57.  
  58.     xsize=PubScreen->Width;
  59.     ysize=PubScreen->Height;
  60.  
  61.     for (n=0; n<colors; n++)
  62.     {
  63.         coltab[n]=GetRGB4(PubScreen->ViewPort.ColorMap,n);
  64.         pentab[n]=n;
  65.     }
  66.  
  67.     UnlockPubScreen(0L,PubScreen);
  68.  
  69. /*    order pens    */
  70. /*
  71.     for (j=0; j<colors-1; j++)
  72.     {
  73.         for (i=j+1; i<colors; i++)
  74.         {
  75.             if (((coltab[i]&0x00f)+((coltab[i]&0x0f0)>>4)+((coltab[i]&0xf00)>>8))>
  76.                 ((coltab[j]&0x00f) + ((coltab[j]&0x0f0)>>4) + ((coltab[j]&0xf00)>>8)))
  77.                 {
  78.                     color=coltab[i];
  79.                     coltab[i]=coltab[j];
  80.                     coltab[j]=color;
  81.  
  82.                     color=pentab[i];
  83.                     pentab[i]=pentab[j];
  84.                     pentab[j]=color;
  85.                 }
  86.         }
  87.     }
  88. */
  89.     BlankWindow.MinWidth=80;
  90.     BlankWindow.MinHeight=30;
  91.     BlankWindow.MaxWidth=xsize;
  92.     BlankWindow.MaxHeight=ysize;
  93.     BlankWindow.Screen=PubScreen;
  94.  
  95.     if ((CustWindow=(struct Window *)OpenWindow(&BlankWindow))==NULL)
  96.     {
  97.         printf("Kann den Bildschirm %s nicht finden\n",name);
  98.     }
  99.  
  100. /*    Preferences    */
  101.  
  102.     is=-3.0;        /*    Im-range    */
  103.     di=6.0;
  104.  
  105.     rs=-3.0;        /*    Re-range    */
  106.     dr=6.0;
  107.  
  108.     icon=-.2;    /*    complex constant    */
  109.     rcon=-1.3;
  110.  
  111. /*    make love to julia    */
  112.  
  113.     while (1)
  114.     {
  115.         rinc=dr/(double)(CustWindow->GZZWidth);
  116.         iinc=di/(double)(CustWindow->GZZHeight);
  117.  
  118.         SetWindowTitles(CustWindow,"Coming...",-1);
  119.  
  120.         if (!paint) goto next;
  121.  
  122.         memi=is;
  123.  
  124.         for (y=0; y<CustWindow->GZZHeight; y++)    /*    row    */
  125.         {
  126.             memr=rs;
  127.  
  128.             for (x=0; x<CustWindow->GZZWidth; x++)
  129.             {
  130.                 re=restart=(rs+=rinc);
  131.                 im=imstart=is;
  132.  
  133.                 SetAPen(rp,pentab[colors-1]);
  134.  
  135.                 for (n=0; n<colors-1; n++)
  136.                 {
  137.                     re2=re*re-im*im+rcon;
  138.                     im=2*re*im+icon;
  139.                     re=re2;
  140.  
  141.                     if ((re-restart)*(re-restart)+(im-imstart)*(im-imstart)>100.0)
  142.                     {
  143.                         SetAPen(rp,pentab[n]);
  144.                         break;
  145.                     }
  146.                 }
  147.                 WritePixel(rp,x,y);
  148.             }
  149.             rs=memr;
  150.             is+=iinc;
  151.         }
  152.         is=memi;
  153.  
  154. next:        paint=1;
  155.  
  156.         SetWindowTitles(CustWindow,"Fun with Julia",-1);
  157. wait:
  158.         Wait(1L<<CustWindow->UserPort->mp_SigBit);
  159.  
  160.         message=GetMsg(CustWindow->UserPort);
  161.  
  162.         switch (message->Class)
  163.         {
  164.             case CLOSEWINDOW:
  165.                 ReplyMsg(message);
  166.                 CloseWindow(CustWindow);
  167.                 CloseLibrary(IntuitionBase);
  168.                 CloseLibrary(GfxBase);
  169.                 exit(1);
  170.                 break;
  171.  
  172.             case NEWSIZE:
  173.                 ReplyMsg(message);
  174.                 continue;
  175.                 break;
  176.  
  177.             default:
  178.                 break;
  179.         }
  180.  
  181.         switch (message->Code)
  182.         {
  183.             case SELECTDOWN:
  184.                 ReplyMsg(message);
  185.                 SetWindowTitles(CustWindow,"Oh, year...do it to me !",-1);
  186.  
  187.                 startx=x=xneu=CustWindow->GZZMouseX;
  188.                 starty=y=yneu=CustWindow->GZZMouseY;
  189.  
  190.                 SetDrMd(rp,2);
  191.  
  192.                 while (1)
  193.                 {
  194.                     message=GetMsg(CustWindow->UserPort);
  195.  
  196.                     if (message->Code==SELECTUP)
  197.                     {
  198.                         SetDrMd(rp,1);
  199.  
  200.                         if (xneu<startx)
  201.                         {
  202.                             tmp=xneu;
  203.                             xneu=startx;
  204.                             startx=tmp;
  205.                         }
  206.                         if (yneu<starty)
  207.                         {
  208.                             tmp=yneu;
  209.                             yneu=starty;
  210.                             starty=tmp;
  211.                         }
  212.  
  213.                         dx=(double)xneu-(double)startx;
  214.                         dy=(double)yneu-(double)starty;
  215.  
  216.                         if (dx && dy)
  217.                         {
  218.                             rs+=(double)startx*rinc;
  219.                             is+=(double)starty*iinc;
  220.                             dr=dx*rinc;
  221.                             di=dy*iinc;
  222.                         }
  223.                         else
  224.                         {
  225.                             is=-3.0;
  226.                             di=6.0;
  227.                             rs=-3.0;
  228.                             dr=6.0;
  229.                             rcon=2.0*ran()-1.0;
  230.                             icon=2.0*ran()-1.0;
  231.                         }
  232.                         ReplyMsg(message);
  233.                         break;
  234.                     }
  235.  
  236.                     if ((xneu=CustWindow->GZZMouseX)!=x || (yneu=CustWindow->GZZMouseY)!=y)
  237.                     {
  238.                         if (xneu<0) xneu=0;
  239.                         if (yneu<0) yneu=0;
  240.                         if (xneu>=CustWindow->GZZWidth) xneu=CustWindow->GZZWidth-1;
  241.                         if (yneu>=CustWindow->GZZHeight) yneu=CustWindow->GZZHeight-1;
  242.                         Move(rp,startx,starty);
  243.                         Draw(rp,startx,y);
  244.                         Draw(rp,x,y);
  245.                         Draw(rp,x,starty);
  246.                         Draw(rp,startx,starty);
  247.  
  248.                         Move(rp,startx,starty);
  249.                         Draw(rp,startx,yneu);
  250.                         Draw(rp,xneu,yneu);
  251.                         Draw(rp,xneu,starty);
  252.                         Draw(rp,startx,starty);
  253.  
  254.                         x=xneu;
  255.                         y=yneu;
  256.                     }
  257.                 }
  258.                 break;
  259.  
  260.             default:
  261.                 paint=0;
  262.                 break;
  263.         }
  264.     }    
  265. }
  266.