home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 98 / af098a.adf / archives / HDProgs.lzx / AGA-Morph-V1.3 / Source / AGA-Morph-Render.c < prev    next >
C/C++ Source or Header  |  1997-04-14  |  12KB  |  382 lines

  1. /*----------------------------------------------------------------------------*
  2.  *                                                                            *
  3.  *  AGA-Morph-Render.c V1.3                                                   *
  4.  *                                                                            *
  5.  *----------------------------------------------------------------------------*/
  6.  
  7. /*----------------------------------------------------------------------------*/
  8.  
  9. #include <libraries/diskfont.h>
  10. #include <intuition/intuition.h>
  11. #include <intuition/classes.h>
  12. #include <intuition/pointerclass.h>
  13. #include <datatypes/pictureclass.h>
  14. #include <datatypes/pictureclassext.h>
  15. #include <graphics/gfx.h>
  16. #include <exec/memory.h>
  17. #include <libraries/asl.h>
  18. #include <clib/macros.h>
  19.  
  20. #include "AGA-Morph-Language.h"
  21. #include "AGA-Morph-Struct.h"
  22.  
  23. /*----------------------------------------------------------------------------*/
  24.  
  25. extern struct DPoint Points;
  26. extern struct Gadget SpLineButton;
  27. extern struct Image ProImage;
  28. extern struct IntuiText MemoryError,NoPicError,WindowError;
  29. extern struct MPoint *Point1,*Point2;
  30. extern struct Pic Picture1,Picture2,Picture3;
  31. extern struct Screen *MorphScreen;
  32. extern struct Window *ProWindow;
  33. extern BYTE Direct;
  34. extern LONG x,y;
  35. extern ULONG Height,Width;
  36.  
  37. /*----------------------------------------------------------------------------*/
  38.  
  39. BYTE InitPro();                                          /* Internal use only */
  40. void RemPro();                                           /* Internal use only */
  41. void Pro(UWORD p);                                       /* Internal use only */
  42. void Render(UWORD xd,UWORD yd,UBYTE p1,UBYTE p2);
  43.  
  44. /*----------------------------------------------------------------------------*/
  45.  
  46. BYTE
  47. InitPro()
  48. {
  49.   if (ProWindow=(struct Window *)OpenWindowTags(NULL,
  50.                                                 WA_Left,         (Width-402)/2,
  51.                                                 WA_Top,          (Height-27)/2,
  52.                                                 WA_Width,        402,
  53.                                                 WA_Height,       27,
  54.                                                 WA_CustomScreen, MorphScreen,
  55.                                                 WA_Activate,     TRUE,
  56.                                                 WA_Borderless,   TRUE,
  57.                                                 TAG_END))
  58.   {
  59.     ProImage.Width=0;
  60.     DrawImage(ProWindow->RPort,&ProImage,0,0);
  61.   }
  62.   else
  63.   {
  64.     ShowError(&WindowError);
  65.     return 1;
  66.   }
  67.   return 0;
  68. }
  69.  
  70. /*----------------------------------------------------------------------------*/
  71.  
  72. void
  73. RemPro()
  74. {
  75.   CloseWindow(ProWindow);
  76. };
  77.  
  78. /*----------------------------------------------------------------------------*/
  79.  
  80. void
  81. Pro(UWORD p)
  82. {
  83.   ProImage.Width=p;
  84.   DrawImage(ProWindow->RPort,&ProImage,0,0);
  85. }
  86.  
  87. /*----------------------------------------------------------------------------*/
  88.  
  89. void
  90. Render(UWORD xd,UWORD yd,UBYTE p1,UBYTE p2)
  91. {
  92.   LONG x1,y1,x2,y2;
  93.   double dist,dist2,distx,disty,distx2,disty2,gdist;
  94.   double xp1,yp1,xp2,yp2,pp,pb,pb2,r,g,b,xs1,ys1,xs2,ys2;
  95.   double wx1,wx2,wx3,wx4,wy1,wy2,wy3,wy4,w1,w2,w3,w4,w5,w6,w7,w8;
  96.   LONG xb1,yb1,xb2,yb2;
  97.   LONG po1,po2,po3,po4,po5,po6,po7,po8,pixpos;
  98.   pp=0.01*p1;
  99.   pb=0.01*p2; pb2=1.0-pb;
  100.   x1=Picture1.width;
  101.   y1=Picture1.height;
  102.   x2=Picture2.width;
  103.   y2=Picture2.height;
  104.   if (Picture3.r)
  105.   {
  106.     FreeVec(Picture3.r);
  107.     Picture3.r=0;
  108.   }
  109.   if (Picture3.g)
  110.   {
  111.     FreeVec(Picture3.g);
  112.     Picture3.g=0;
  113.   }
  114.   if (Picture3.b)
  115.   {
  116.     FreeVec(Picture3.b);
  117.     Picture3.b=0;
  118.   }
  119.   if ((x1>0) && (y1>0) && (x2>0) && (y2>0) && (xd>0) && (yd>0))
  120.   {
  121.     Picture3.width=xd;
  122.     Picture3.height=yd;
  123.     xs1=x1; xs1=xs1/xd;
  124.     ys1=y1; ys1=ys1/yd;
  125.     xs2=x2; xs2=xs2/xd;
  126.     ys2=y2; ys2=ys2/yd;
  127.     if (Picture3.r=(UBYTE *)AllocVec(xd*yd,0))
  128.     {
  129.       if (Picture3.g=(UBYTE *)AllocVec(xd*yd,0))
  130.       {
  131.         if (Picture3.b=(UBYTE *)AllocVec(xd*yd,0))
  132.         {
  133.           Point1=Points.First;
  134.           while (Point1)
  135.           {
  136.             if (SpLineButton.Flags & GFLG_SELECTED)
  137.             {
  138.               Point1->xp=(Point1->P.x1*(2.0*pp*pp-3.0*pp+1.0)+
  139.                           Point1->P.x2*(2.0*pp*pp-pp)+
  140.                           Point1->P.xs*(-4.0*pp*pp+4.0*pp))*
  141.                          xd/(x1*(1.0-pp)+x2*pp);
  142.               Point1->yp=(Point1->P.y1*(2.0*pp*pp-3.0*pp+1.0)+
  143.                           Point1->P.y2*(2.0*pp*pp-pp)+
  144.                           Point1->P.ys*(-4.0*pp*pp+4.0*pp))*
  145.                          yd/(y1*(1.0-pp)+y2*pp);
  146.             }
  147.             else
  148.             {
  149.               Point1->xp=(Point1->P.x1*(1.0-pp)+Point1->P.x2*pp)*
  150.                          xd/(x1*(1.0-pp)+x2*pp);
  151.               Point1->yp=(Point1->P.y1*(1.0-pp)+Point1->P.y2*pp)*
  152.                          yd/(y1*(1.0-pp)+y2*pp);
  153.             }
  154.             Point1->xp=MIN(MAX(Point1->xp,0),xd-1);
  155.             Point1->yp=MIN(MAX(Point1->yp,0),yd-1);
  156.             Point1->xp1=Point1->xp*xs1;
  157.             Point1->yp1=Point1->yp*ys1;
  158.             Point1->xp2=Point1->xp*xs2;
  159.             Point1->yp2=Point1->yp*ys2;
  160.             Point1=Point1->Next;
  161.           }
  162.           if (!(InitPro()))
  163.           {
  164.             pixpos=0;
  165.             for (y=0;y<yd;y++)
  166.             {
  167.               if (Points.First)
  168.               {
  169.                 Point1=Points.First;
  170.                 while (Point1)
  171.                 {
  172.                   if (Point1->yp<y)
  173.                   {
  174.                     disty=y-Point1->yp;
  175.                     disty2=(yd+yd-2)-y-Point1->yp;
  176.                   }
  177.                   else
  178.                   {
  179.                     if (Point1->yp>y)
  180.                     {
  181.                       disty=Point1->yp-y;
  182.                       disty2=Point1->yp+y;
  183.                     }
  184.                     else
  185.                     {
  186.                       disty=0;
  187.                       disty2=MIN((yd-1)-y,y);
  188.                       disty2+=disty2;
  189.                     }
  190.                   }
  191.                   if (disty2+disty>0)
  192.                   {
  193.                     Point1->faky=(disty2-disty)/(disty2+disty);
  194.                   }
  195.                   else
  196.                   {
  197.                     Point1->fakx=0;
  198.                   }
  199.                   Point1->disty=disty*disty;
  200.                   Point1->disty2=disty2*disty2;
  201.                   Point1=Point1->Next;
  202.                 }
  203.               }
  204.               for (x=0;x<xd;x++)
  205.               {
  206.                 if (Points.First)
  207.                 {
  208.                   Point1=Points.First;
  209.                   Direct=0;
  210.                   gdist=0.0;
  211.                   while ((Point1) && !(Direct))
  212.                   {
  213.                     if (Point1->xp<x)
  214.                     {
  215.                       distx=x-Point1->xp;
  216.                       distx2=(xd+xd-2)-x-Point1->xp;
  217.                     }
  218.                     else
  219.                     {
  220.                       if (Point1->xp>x)
  221.                       {
  222.                         distx=Point1->xp-x;
  223.                         distx2=Point1->xp+x;
  224.                       }
  225.                       else
  226.                       {
  227.                         if (Point1->yp==y)
  228.                         {
  229.                           Direct=1;
  230.                           Point2=Point1;
  231.                           distx=0;
  232.                           distx2=MIN((xd-1)-x,x);
  233.                           distx2+=distx2;
  234.                         }
  235.                         else
  236.                         {
  237.                           distx=0;
  238.                           distx2=MIN((xd-1)-x,x);
  239.                           distx2+=distx2;
  240.                         }
  241.                       }
  242.                     }
  243.                     if (distx2+distx>0)
  244.                     {
  245.                       Point1->fakx=(distx2-distx)/(distx2+distx);
  246.                     }
  247.                     else
  248.                     {
  249.                       Point1->fakx=0;
  250.                     }
  251.                     dist=distx*distx+Point1->disty;
  252.                     dist2=distx2*distx2+Point1->disty2;
  253.                     Point1->dist=(1/(dist*dist)+1/(dist2*dist2));
  254.                     gdist+=Point1->dist;
  255.                     Point1=Point1->Next;
  256.                   }
  257.                   if (Direct)
  258.                   {
  259.                     xp1=Point2->P.x1;
  260.                     xp2=Point2->P.x2;
  261.                     yp1=Point2->P.y1;
  262.                     yp2=Point2->P.y2;
  263.                   }
  264.                   else
  265.                   {
  266.                     Point1=Points.First;
  267.                     xp1=x*xs1;
  268.                     xp2=x*xs2;
  269.                     yp1=y*ys1;
  270.                     yp2=y*ys2;
  271.                     if (gdist>0.0)
  272.                     {
  273.                       gdist=1.0/gdist;
  274.                       while (Point1)
  275.                       {
  276.                         dist=gdist*Point1->dist;
  277.                         distx=dist*Point1->fakx;
  278.                         disty=dist*Point1->faky;
  279.                         xp1=xp1+distx*(Point1->P.x1-Point1->xp1);
  280.                         xp2=xp2+distx*(Point1->P.x2-Point1->xp2);
  281.                         yp1=yp1+disty*(Point1->P.y1-Point1->yp1);
  282.                         yp2=yp2+disty*(Point1->P.y2-Point1->yp2);
  283.                         Point1=Point1->Next;
  284.                       }
  285.                     }
  286.                   }
  287.                 }
  288.                 else
  289.                 {
  290.                   xp1=x*xs1;
  291.                   xp2=x*xs2;
  292.                   yp1=y*ys1;
  293.                   yp2=y*ys2;
  294.                 }
  295.                 xb1=xp1; xb2=xp2;
  296.                 yb1=yp1; yb2=yp2;
  297.                 wx1=xp1-xb1; wx2=1.0-wx1; wx3=xp2-xb2; wx4=1.0-wx3;
  298.                 wy1=yp1-yb1; wy2=1.0-wy1; wy3=yp2-yb2; wy4=1.0-wy3;
  299.                 w1=pb2*wx2*wy2;
  300.                 w2=pb2*wx2*wy1;
  301.                 w3=pb2*wx1*wy2;
  302.                 w4=pb2*wx1*wy1;
  303.                 w5=pb*wx4*wy4;
  304.                 w6=pb*wx4*wy3;
  305.                 w7=pb*wx3*wy4;
  306.                 w8=pb*wx3*wy3;
  307.                 po1=xb1+yb1*x1;
  308.                 po2=po1+x1;
  309.                 po3=po1+1;
  310.                 po4=po2+1;
  311.                 po5=xb2+yb2*x2;
  312.                 po6=po5+x2;
  313.                 po7=po5+1;
  314.                 po8=po6+1;
  315.                 r=(*(Picture1.r+po1))*w1+
  316.                   (*(Picture1.r+po2))*w2+
  317.                   (*(Picture1.r+po3))*w3+
  318.                   (*(Picture1.r+po4))*w4+
  319.                   (*(Picture2.r+po5))*w5+
  320.                   (*(Picture2.r+po6))*w6+
  321.                   (*(Picture2.r+po7))*w7+
  322.                   (*(Picture2.r+po8))*w8;
  323.                 g=(*(Picture1.g+po1))*w1+
  324.                   (*(Picture1.g+po2))*w2+
  325.                   (*(Picture1.g+po3))*w3+
  326.                   (*(Picture1.g+po4))*w4+
  327.                   (*(Picture2.g+po5))*w5+
  328.                   (*(Picture2.g+po6))*w6+
  329.                   (*(Picture2.g+po7))*w7+
  330.                   (*(Picture2.g+po8))*w8;
  331.                 b=(*(Picture1.b+po1))*w1+
  332.                   (*(Picture1.b+po2))*w2+
  333.                   (*(Picture1.b+po3))*w3+
  334.                   (*(Picture1.b+po4))*w4+
  335.                   (*(Picture2.b+po5))*w5+
  336.                   (*(Picture2.b+po6))*w6+
  337.                   (*(Picture2.b+po7))*w7+
  338.                   (*(Picture2.b+po8))*w8;
  339.                 *(Picture3.r+pixpos)=r;
  340.                 *(Picture3.g+pixpos)=g;
  341.                 *(Picture3.b+pixpos)=b;
  342.                 pixpos++;
  343.               }
  344.               Pro(400*(x+1+y*xd)/(xd*yd));
  345.             }
  346.             RemPro();
  347.           }
  348.         }
  349.         else
  350.         {
  351.           FreeVec(Picture3.g); Picture3.g=0;
  352.           FreeVec(Picture3.r); Picture3.r=0;
  353.           Picture3.width=0;
  354.           Picture3.height=0;
  355.           ShowError(&MemoryError);
  356.         }
  357.       }
  358.       else
  359.       {
  360.         FreeVec(Picture3.r); Picture3.r=0;
  361.         Picture3.width=0;
  362.         Picture3.height=0;
  363.         ShowError(&MemoryError);
  364.       }
  365.     }
  366.     else
  367.     {
  368.       Picture3.width=0;
  369.       Picture3.height=0;
  370.       ShowError(&MemoryError);
  371.     }
  372.   }
  373.   else
  374.   {
  375.     ShowError(&NoPicError);
  376.   }
  377. }
  378.  
  379. /*----------------------------------------------------------------------------*/
  380.  
  381. /* End of Text */
  382.