home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / gfx / edit / tsmorph / subroutines.c < prev    next >
C/C++ Source or Header  |  1993-12-21  |  59KB  |  2,335 lines

  1. // TSMorph - Amiga Morphing program
  2. // Copyright (C) © 1993  Topicsave Limited
  3.  
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // any later version.
  8.  
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU General Public License for more details.
  13.  
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. // mpaddock@cix.compulink.co.uk
  19.  
  20. //    $Author: M_J_Paddock $
  21. //    $Date: 1993/09/04 17:43:32 $
  22. //    $Revision: 1.15 $
  23.  
  24. // include precompiled headers if required
  25. #ifndef TSMORPH_H
  26. #include "TSMorph.h"
  27. #endif
  28.  
  29. extern char TempFilename[256]="";    // file name buffer set up and used in various places
  30.  
  31. UBYTE r[256],g[256],b[256];            // Saved rgb colors
  32.  
  33. /* display short help message
  34.  * at top of window
  35.  * helpnum : number of help node
  36.  */
  37. void
  38. ihelp(ULONG helpnum) {
  39.     // only display if not zero, and there is a short message and the window is open
  40.     if (helpnum) {
  41.         if (*(ShortHelp[helpnum])) {
  42.             if (TSMorphWnd) {
  43.                 GT_SetGadgetAttrs(TSMorphGadgets[GDX_Help],TSMorphWnd,NULL,
  44.                                     GTTX_Text,ShortHelp[helpnum], TAG_END );
  45.             }
  46.         }
  47.     }
  48. }
  49.  
  50. /* Display help using amigaguide (if available)
  51.  * based on a number. Also displays short help message
  52.  */
  53. void
  54. help(ULONG helpnum) {
  55.     // display amigaguide if available
  56.     if (handle) {
  57.         SetAmigaGuideContext(handle,helpnum,NULL);
  58.         SendAmigaGuideContext(handle,NULL);
  59.     }
  60.     // display short message
  61.     ihelp(helpnum);
  62. }
  63.  
  64. /* Colour the picture based on it colormap
  65.  * Saving the palette for later use
  66.  * The palette is only set when the window is active and a menu is not displayed
  67.  */
  68. void
  69. ColorWindow(struct Picture *pic) {
  70.     UWORD i;                    // Loop counter
  71.     UWORD color;            // Colour
  72.     if (palette) {
  73.         if (pic) {
  74.             if (pic->ilbm->colortable) {
  75.                 // Store and set up to 256 colours
  76.                 for (i=0; i<min((1 << pic->Win->WScreen->BitMap.Depth),256); i++) {
  77.                     color = GetRGB4(pic->Win->WScreen->ViewPort.ColorMap,i);
  78.                     r[i]=(color&0x0f00)>>8;
  79.                     g[i]=(color&0x00f0)>>4;
  80.                     b[i]=(color&0x000f);
  81.                 }
  82.                 LoadRGB4(&(pic->Win->WScreen->ViewPort),pic->ilbm->colortable,
  83.                     MIN(pic->ilbm->ncolors,pic->Win->WScreen->ViewPort.ColorMap->Count));
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89. /* Reset the palette to the original
  90.  * stored in ColorWindow()
  91.  * Called when window becomes inactive or menu is to be displayed
  92.  *
  93.  * Note: This routine and the one above are not really allowed by CBM
  94.  *       But if no other program is changing the palette whilst its
  95.  *       window is inactive then everything should (amy) be OK.
  96.  */
  97. void
  98. UnColorWindow(struct Picture *pic) {
  99.     UWORD i;        // Loop counter
  100.     if (palette) {
  101.         if (pic) {
  102.             if (pic->ilbm->colortable) {
  103.                 for (i=0; i<min((1 << pic->Win->WScreen->BitMap.Depth),256); i++) {
  104.                     SetRGB4(&(pic->Win->WScreen->ViewPort),i,r[i],g[i],b[i]);
  105.                 }
  106.             }
  107.         }
  108.     }
  109. }
  110.  
  111. /* Standard CloseWindowSafely
  112.  * removing messages on shared ports
  113.  */
  114. void
  115. CloseWindowSafely(struct Window *win) {
  116.     Forbid();
  117.     StripIntuiMessages(win->UserPort,win);
  118.     win->UserPort = NULL;
  119.     ModifyIDCMP(win,0);
  120.     Permit();
  121.     CloseWindow(win);
  122. }
  123.  
  124. /* Standard StripIntuiMessages    */
  125. void
  126. StripIntuiMessages(struct MsgPort *mp,struct Window *win) {
  127.     struct IntuiMessage *msg, *succ;
  128.     msg = (struct IntuiMessage *)mp->mp_MsgList.lh_Head;
  129.     while(succ = (struct IntuiMessage *)msg->ExecMessage.mn_Node.ln_Succ) {
  130.         if(msg->IDCMPWindow == win) {
  131.             Remove((struct Node *)msg);
  132.             ReplyMsg((struct Message *)msg);
  133.         }
  134.         msg = succ;
  135.     }
  136. }
  137.  
  138. /* Display an error message
  139.  * ErrorMessage    : The main text
  140.  * Gadget            : Text for one gadget
  141.  * extra                : more of the text
  142.  * helpnum            : Number of a help node
  143.  *
  144.  * Uses reqtools.library if available
  145.  */
  146. void
  147. Error(char *ErrorMessage,char *Gadget,char *extra,ULONG helpnum) {
  148.     struct EasyStruct EasyStruct = {
  149.         sizeof(struct EasyStruct),
  150.         0,
  151.         NULL,
  152.         NULL,
  153.         NULL
  154.     };
  155.     UBYTE *title = "TSMorph Error";
  156.     struct Window *req;
  157.     UBYTE gad[32] = "Help|";
  158.     UBYTE gad1[32] = "_Help|_";
  159.     ULONG ret = CALL_HANDLER;
  160.     struct AmigaGuideMsg *agm;
  161.     ULONG signals;
  162.     struct rtHandlerInfo *rth;
  163.     // Disable the windows and display the requester
  164.     DisableWindows(helpnum);
  165.     if (ReqToolsBase) {
  166.         // Set up gadgets depending on if amigaguide is available
  167.         if (!handle) {
  168.             strcpy(gad1,"_");
  169.         }
  170.         strcat(gad1,Gadget);
  171.         // This loops until OK is pressed, displaying help if Help is pressed (if present)
  172.         while (ret) {
  173.             ret = CALL_HANDLER;
  174.             if (rtEZRequestTags(ErrorMessage,gad1,NULL,&extra,
  175.                                         RT_ReqHandler,    &rth,
  176.                                         RT_Window,        TSMorphWnd,
  177.                                         RT_Underscore,    '_',
  178.                                         RTEZ_ReqTitle,    title,
  179.                                         TAG_END) == CALL_HANDLER) {
  180.                 while (ret == CALL_HANDLER) {
  181.                     if (!rth->DoNotWait) {
  182.                         signals = Wait(rth->WaitMask | ASig);
  183.                     }
  184.                     ret = rtReqHandlerA(rth,signals,NULL);
  185.                     if (ret == 1) {
  186.                         help(helpnum);
  187.                     }
  188.                     if (signals & ASig) {
  189.                           while (agm = GetAmigaGuideMsg(handle)) {
  190.                              ReplyAmigaGuideMsg(agm);
  191.                          }
  192.                      }
  193.                 }
  194.             }
  195.             else {
  196.                 ret = 0;
  197.             }
  198.         }
  199.     }
  200.     else {
  201.         EasyStruct.es_TextFormat = ErrorMessage;
  202.         EasyStruct.es_Title = title;
  203.         // Set up gadgets depending on if amigaguide is available
  204.         if (handle) {
  205.             strcat(gad,Gadget);
  206.         }
  207.         else {
  208.             strcpy(gad,Gadget);
  209.         }
  210.         EasyStruct.es_GadgetFormat = gad;
  211.         req = BuildEasyRequest(TSMorphWnd,&EasyStruct,NULL,extra);
  212.         // This loops until OK is pressed, displaying help if Help is pressed (if present)
  213.         while (ret) {
  214.             signals  = Wait((1L << req->UserPort->mp_SigBit) | ASig);
  215.             if (signals & (1L << req->UserPort->mp_SigBit)) {
  216.                 ret = SysReqHandler(req,NULL,FALSE);
  217.                 if (ret == 1) {
  218.                     help(helpnum);
  219.                 }
  220.             }
  221.             if (signals & ASig) {
  222.                   while (agm = GetAmigaGuideMsg(handle)) {
  223.                      ReplyAmigaGuideMsg(agm);
  224.                  }
  225.             }
  226.         }
  227.         FreeSysRequest(req);
  228.     }
  229.     EnableWindows();
  230. }
  231.  
  232. /* As Error but
  233.  * pic    : a picture to uncolor if required
  234.  */
  235. void
  236. XError(struct Picture *pic,char *ErrorMessage,char *Gadget,char *extra,ULONG helpnum) {
  237.     UnColorWindow(pic);
  238.     Error(ErrorMessage,Gadget,extra,helpnum);
  239.     ColorWindow(pic);
  240. }
  241.  
  242. /* Draw all points
  243.  * in both windows
  244.  */
  245. void
  246. DrawAllPoints(void) {
  247.     struct MyPoint *MyPoint;
  248.     // For each point
  249.     for (MyPoint = (struct MyPoint *)PointList.lh_Head;
  250.                           MyPoint->MyNode.mln_Succ;
  251.                           MyPoint = (struct MyPoint *)MyPoint->MyNode.mln_Succ) {
  252.         // Draw point in image 1
  253.         DrawPixels(&Pic1,MyPoint->x,MyPoint->y,NULL);
  254.         /* For each linked point draw a line
  255.          * to the other if the other is a "later" point
  256.          */
  257.         if (MyPoint->p1 > MyPoint) {
  258.             MyDraw(Pic1.Win->RPort,MyPoint->p1->x<<Zoom,MyPoint->p1->y<<Zoom,MyPoint->x<<Zoom,MyPoint->y<<Zoom);
  259.         }    
  260.         if (MyPoint->p2 > MyPoint) {
  261.             MyDraw(Pic1.Win->RPort,MyPoint->p2->x<<Zoom,MyPoint->p2->y<<Zoom,MyPoint->x<<Zoom,MyPoint->y<<Zoom);
  262.         }
  263.         if (MyPoint->p3 > MyPoint) {
  264.             MyDraw(Pic1.Win->RPort,MyPoint->p3->x<<Zoom,MyPoint->p3->y<<Zoom,MyPoint->x<<Zoom,MyPoint->y<<Zoom);
  265.         }
  266.         if (MyPoint->p4 > MyPoint) {
  267.             MyDraw(Pic1.Win->RPort,MyPoint->p4->x<<Zoom,MyPoint->p4->y<<Zoom,MyPoint->x<<Zoom,MyPoint->y<<Zoom);
  268.         }
  269.         // Same stuff for image 2
  270.         DrawPixels(&Pic2,MyPoint->x1,MyPoint->y1,NULL);
  271.         if (MyPoint->p1 > MyPoint) {
  272.             MyDraw(Pic2.Win->RPort,MyPoint->p1->x1<<Zoom,MyPoint->p1->y1<<Zoom,MyPoint->x1<<Zoom,MyPoint->y1<<Zoom);
  273.         }    
  274.         if (MyPoint->p2 > MyPoint) {
  275.             MyDraw(Pic2.Win->RPort,MyPoint->p2->x1<<Zoom,MyPoint->p2->y1<<Zoom,MyPoint->x1<<Zoom,MyPoint->y1<<Zoom);
  276.         }
  277.         if (MyPoint->p3 > MyPoint) {
  278.             MyDraw(Pic2.Win->RPort,MyPoint->p3->x1<<Zoom,MyPoint->p3->y1<<Zoom,MyPoint->x1<<Zoom,MyPoint->y1<<Zoom);
  279.         }
  280.         if (MyPoint->p4 > MyPoint) {
  281.             MyDraw(Pic2.Win->RPort,MyPoint->p4->x1<<Zoom,MyPoint->p4->y1<<Zoom,MyPoint->x1<<Zoom,MyPoint->y1<<Zoom);
  282.         }
  283.     }
  284. }
  285.  
  286. /* Handle Raw Key in Information
  287.  * Window - from GadToolsBox
  288.  */
  289. int
  290. TSMorphRawKey(void) {
  291.     UWORD X,Y;        // Mouse position
  292.     ULONG HNum;        // Help node
  293.     X = TSMorphWnd->MouseX;
  294.     Y = TS