home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / amiga / fractal / mak / source.lzh / Source / LoadIt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-29  |  10.6 KB  |  375 lines

  1. /*   This program was hacked from Display v1.06 by Carolyn
  2.  *   Scheppner for inclusion into the Mandelbrot Adventure
  3.  *   Kit.  I have renamed this program Loadit.  Simple and
  4.  *   sweet.
  5.  * 
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/memory.h>
  10. #include <exec/tasks.h>
  11. #include <libraries/dos.h>
  12. #include <libraries/dosextens.h>
  13. #include <intuition/intuition.h>
  14. #include <graphics/gfxbase.h>
  15.  
  16. #include "iff/ilbm.h"
  17. #include "myreadpict.h"
  18.  
  19. #ifndef MIN
  20. #define MIN(a,b) ((a)<(b)?(a):(b))
  21. #endif  MIN
  22.  
  23. #define MAK_PIC 0
  24. #define MAP_PIC 1
  25. #define TITLE_PIC 2
  26.  
  27. /* Bits we must mask out of CAMG.Viewmodes */
  28. #define BADFLAGS  (SPRITES|VP_HIDE|GENLOCK_AUDIO|GENLOCK_VIDEO)
  29. #define FLAGMASK  (~BADFLAGS)
  30. #define CAMGMASK  (FLAGMASK & 0x0000FFFFL)
  31.  
  32. /* general usage pointers */
  33.  
  34. extern struct GfxBase  *GfxBase;
  35. extern struct IntuitionBase  *IntuitionBase;
  36. extern struct TextAttr  Font;
  37. extern struct Window *file_window, *FirstWindow;
  38. extern struct IntuiText ok_request, prompt;
  39. extern USHORT *Pointer, *Sleepy, *Cross;
  40. extern UWORD POINTER_col[];
  41. extern UWORD CROSS_col[];
  42. extern UWORD SLEEPY_col[];
  43. ULONG  IconBase = 0;
  44.  
  45. /* Globals for displaying an image */
  46.  
  47. struct Screen   *screen1;
  48. struct Window   *window1;
  49. struct ViewPort *vport1;
  50.  
  51. struct BitMap   tBitMap;      /* Temp BitMap struct for small pics  */
  52.  
  53. int titleFlag;
  54.  
  55. /* Structures for new Screen, new Window */
  56.  
  57. struct   NewScreen      ns = {
  58.    0, 0,                                  /* LeftEdge and TopEdge   */
  59.    640, 400,                              /* Width and Height       */
  60.    4,                                     /* Depth                  */
  61.    1, 0,                                  /* DetailPen and BlockPen */
  62.    NULL,                                  /* Special display modes  */
  63.    CUSTOMSCREEN,                          /* Screen Type            */
  64.    &Font,                                 /* Use my font            */
  65.    " <- Click here (after clicking below) when done viewing.", /* Title */
  66.    NULL,                                  /* No gadgets yet         */
  67.    NULL,                                  /* Ptr to CustomBitmap    */
  68.    };
  69.  
  70. struct   NewWindow      nw = {
  71.    0, 0,                                  /* LeftEdge and TopEdge */
  72.    640, 400,                              /* Width and Height */
  73.    0, 1,                                  /* DetailPen and BlockPen */
  74.    MOUSEBUTTONS,                          /* IDCMP Flags */
  75.    BACKDROP|
  76.    BORDERLESS|ACTIVATE,                   /* Flags */
  77.    NULL, NULL,                            /* Gadget and Image pointers */
  78.    NULL,                                  /* Title string */
  79.    NULL,                                  /* Put Screen ptr here */
  80.    NULL,                                  /* SuperBitMap pointer */
  81.    0, 0,                                  /* MinWidth and MinHeight */
  82.    0, 0,                                  /* MaxWidth and MaxHeight */
  83.    CUSTOMSCREEN,                          /* Type of window */
  84.    };
  85.  
  86. USHORT  allBlack[maxColorReg] = {0};
  87.  
  88. ILBMFrame   iFrame;       /* my global frame */
  89.  
  90.  
  91. LoadIt(ilbm_file, showFlag)
  92. char *ilbm_file;
  93. int showFlag;
  94.    {
  95.    IFFP iffp = NO_FILE;
  96.    LONG LoadILBMFile;
  97.    struct IntuiMessage *msg;
  98.    ULONG class, code;
  99.    SHORT mouseX, mouseY;
  100.    int error; 
  101.    char message[40];   
  102.    BOOL TBToggle = FALSE, doneViewing = FALSE;
  103.      
  104.    titleFlag = showFlag;
  105.    
  106.    if(!(LoadILBMFile = Open(ilbm_file, MODE_OLDFILE)))
  107.       {
  108.         strcpy(message, "Picture file not found");
  109.         prompt.IText = message;
  110.         AutoRequest(file_window, &prompt, &ok_request, NULL,
  111.                                   NULL, NULL, 320, 72);
  112.         return(FALSE);
  113.       }                          
  114.    
  115.    iffp = myReadPicture(LoadILBMFile, &iFrame);
  116.    Close(LoadILBMFile);
  117.    if (!(iffp == IFF_DONE))
  118.       {
  119.         strcpy(message, "Not an IFF ILBM");
  120.         prompt.IText = message;
  121.         AutoRequest(file_window, &prompt, &ok_request, NULL,
  122.                                   NULL, NULL, 320, 72);
  123.         return(FALSE);
  124.       } 
  125.  
  126.    error = DisplayPic(&iFrame);
  127.    if(error)  
  128.       {
  129.         strcpy(message, "Can't open screen or window"); 
  130.         prompt.IText = message;
  131.         AutoRequest(file_window, &prompt, &ok_request, NULL,
  132.                                   NULL, NULL, 320, 72);
  133.         return(FALSE);
  134.       }          
  135.  
  136.    if(titleFlag == MAK_PIC || titleFlag == MAP_PIC)
  137.      {
  138.       if(titleFlag == MAK_PIC)
  139.         {
  140.          SetColors( 1 );
  141.          ClearPointer(window1);
  142.          SetPointer(window1,Pointer,16,16,0,0);
  143.         }
  144.         
  145.       if(titleFlag == MAP_PIC)  
  146.         {
  147.          SetColors( 2 );
  148.          ClearPointer(window1);
  149.          SetPointer(window1,Cross,16,16,-8,-8);
  150.         }     
  151.       
  152.       while(!(doneViewing))
  153.         {
  154.          while(msg=(struct IntuiMessage *)GetMsg(window1->UserPort))
  155.             {
  156.              class = msg->Class;
  157.              code  = msg->Code;
  158.              mouseX = msg->MouseX;
  159.              mouseY = msg->MouseY;
  160.          
  161.              ReplyMsg(msg);
  162.              switch(class)
  163.                {
  164.                 case MOUSEBUTTONS:  
  165.                  if((code == SELECTDOWN) && (mouseX < 10)
  166.                      && (mouseY < 10))
  167.                    {
  168.                     doneViewing = TRUE;
  169.                    }
  170.                  else if((code == SELECTDOWN) && (mouseX > 10)
  171.                           && (mouseY > 10) && (TBToggle == FALSE))
  172.                         {
  173.                          TBToggle = TRUE;
  174.                          ShowTitle(screen1, TRUE); 
  175.                         }  
  176.                  else if((code == SELECTDOWN) && (mouseY > 10)
  177.                           && (TBToggle == TRUE))
  178.                         {  
  179.                          TBToggle = FALSE;
  180.                          ShowTitle(screen1, FALSE);
  181.                         }  
  182.                  break;           
  183.                }
  184.             }   
  185.         }
  186.       ClearPointer(window1);  
  187.       CloseWindow(window1);
  188.       CloseScreen(screen1);
  189.       return(TRUE);  
  190.      }    
  191.      
  192.     if(titleFlag == TITLE_PIC)
  193.       {
  194.         Delay(400);
  195.         CloseWindow(window1);
  196.         CloseScreen(screen1);
  197.         return(TRUE);
  198.       }     
  199.    }
  200.  
  201.  
  202. /** getBitMap() *********************************************************
  203.  *
  204.  * Open screen or temp bitmap.
  205.  *   Returns ptr destBitMap  or  0 = error
  206.  *
  207.  *************************************************************************/
  208. struct BitMap *getBitMap(ptilbmFrame)
  209.    ILBMFrame *ptilbmFrame;
  210.    {
  211.    int     i, nPlanes, plsize;
  212.    SHORT  sWidth, sHeight, dWidth, dHeight;
  213.    struct BitMap *destBitMap;
  214.  
  215.    sWidth  = ptilbmFrame->bmHdr.w;
  216.    sHeight = ptilbmFrame->bmHdr.h;
  217.    dWidth  = ptilbmFrame->bmHdr.pageWidth;
  218.    dHeight = ptilbmFrame->bmHdr.pageHeight;
  219.    nPlanes = MIN(ptilbmFrame->bmHdr.nPlanes, EXDepth);
  220.    ns.Depth = nPlanes;
  221.    
  222.    if (ptilbmFrame->foundCAMG)
  223.       {
  224.       ns.ViewModes = ptilbmFrame->camgChunk.ViewModes & CAMGMASK;
  225.       }
  226.    else
  227.       {
  228.       if (ptilbmFrame->bmHdr.pageWidth >= 640)
  229.          ns.ViewModes = HIRES;
  230.       else
  231.          ns.ViewModes = 0;
  232.  
  233.       if (ptilbmFrame->bmHdr.pageHeight >= 400)
  234.          ns.ViewModes |= LACE;
  235.       }
  236.    
  237.  
  238.    if ((screen1 = (struct Screen *)OpenScreen(&ns))==NULL)    return(0);
  239.  
  240.    vport1 = &screen1->ViewPort;
  241.    LoadRGB4(vport1, &allBlack[0], MIN(1<<ns.Depth,maxColorReg)); 
  242.    nw.Screen = screen1;
  243.  
  244.    if ((window1 = (struct Window *)OpenWindow(&nw))==NULL)
  245.       {
  246.       CloseScreen(screen1);
  247.       screen1 = NULL;
  248.       return(0);
  249.       }
  250.  
  251.    if(titleFlag == MAK_PIC || titleFlag == MAP_PIC)
  252.      {
  253.       SetColors( 3 );
  254.       ClearPointer(window1);
  255.       SetPointer(window1,Sleepy,25,16,0,0);
  256.      }  
  257.    
  258.    if ((sWidth == dWidth) && (sHeight == dHeight))
  259.       {
  260.       destBitMap = (struct BitMap *)screen1->RastPort.BitMap;
  261.       }
  262.    else
  263.       {
  264.       InitBitMap( &tBitMap,
  265.                   nPlanes,
  266.                   sWidth,
  267.                   sHeight);
  268.  
  269.       plsize = RowBytes(ptilbmFrame->bmHdr.w) * ptilbmFrame->bmHdr.h;
  270.       if (tBitMap.Planes[0] =
  271.        (PLANEPTR)AllocMem(nPlanes * plsize, MEMF_CHIP))
  272.          {
  273.          for (i = 1; i < nPlanes; i++)
  274.             tBitMap.Planes[i] = (PLANEPTR)tBitMap.Planes[0] + plsize*i;
  275.          destBitMap = &tBitMap;
  276.          }
  277.       else
  278.          {
  279.          CloseWindow(window1);
  280.          window1 = NULL;
  281.          CloseScreen(screen1);
  282.          screen1 = NULL;
  283.          return(0);  /* can't allocate temp BitMap */
  284.          }
  285.       }
  286.    return(destBitMap);          /* destBitMap allocated */
  287.    }
  288.  
  289.  
  290. /** DisplayPic() *********************************************************
  291.  *
  292.  * Display loaded bitmap.  If tBitMap, first transfer to screen.
  293.  *
  294.  *************************************************************************/
  295. DisplayPic(ptilbmFrame)
  296.    ILBMFrame *ptilbmFrame;
  297.    {
  298.    int    i, row, byte, nrows, nbytes;
  299.    struct BitMap  *tbp, *sbp; /* temp and screen BitMap ptrs */
  300.    UBYTE  *tpp, *spp;         /* temp and screen plane ptrs  */
  301.  
  302.    if (tBitMap.Planes[0])     /* transfer from tBitMap if nec. */
  303.       {
  304.       tbp = &tBitMap;
  305.       sbp = screen1->RastPort.BitMap;
  306.       nrows  = MIN(tbp->Rows, sbp->Rows);
  307.       nbytes = MIN(tbp->BytesPerRow, sbp->BytesPerRow);
  308.  
  309.       for (i = 0; i < sbp->Depth; i++)
  310.          {
  311.          tpp = (UBYTE *)tbp->Planes[i];
  312.          spp = (UBYTE *)sbp->Planes[i];
  313.          for (row = 0; row < nrows; row++)
  314.             {
  315.             tpp = tbp->Planes[i] + (row * tbp->BytesPerRow);
  316.             spp = sbp->Planes[i] + (row * sbp->BytesPerRow);
  317.             for (byte = 0; byte < nbytes; byte++)
  318.                {
  319.                *spp++ = *tpp++;
  320.                }
  321.             }
  322.          }
  323.       /*  Can now deallocate the temp BitMap  */
  324.       FreeMem(tBitMap.Planes[0],
  325.                  tBitMap.BytesPerRow * tBitMap.Rows * tBitMap.Depth);
  326.       }
  327.  
  328.    vport1 = &screen1->ViewPort;
  329.    LoadRGB4(vport1, ptilbmFrame->colorMap, ptilbmFrame->nColorRegs);
  330.    return(0);
  331.    }
  332.  
  333. SetColors( Pointer )
  334. int Pointer;
  335.   {
  336.    SHORT i;
  337.    UWORD red, green, blue;
  338.    
  339.    if( Pointer == 1 )
  340.      {
  341.       for (i = 0; i < 3; i++)
  342.         {
  343.          red = (POINTER_col[i] & 0xf00) >> 8;
  344.          green = (POINTER_col[i] & 0x0f0) >> 4;
  345.          blue = (POINTER_col[i] & 0x00f);
  346.          SetRGB4(vport1,i + 17,red,green,blue);
  347.         }
  348.       return(0);
  349.      }
  350.  
  351.    if( Pointer == 2)
  352.      {
  353.       for (i = 0; i < 3; i++)
  354.        {
  355.         red = (CROSS_col[i] & 0xf00) >> 8;
  356.         green = (CROSS_col[i] & 0x0f0) >> 4;
  357.         blue = (CROSS_col[i] & 0x00f);
  358.         SetRGB4(vport1,i + 17,red,green,blue);
  359.        }
  360.       return(0);
  361.      }
  362.  
  363.    if( Pointer == 3)
  364.      {
  365.       for (i = 0; i < 3; i++)
  366.         {
  367.          red = (SLEEPY_col[i] & 0xf00) >> 8;
  368.          green = (SLEEPY_col[i] & 0x0f0) >> 4;
  369.          blue = (SLEEPY_col[i] & 0x00f);
  370.          SetRGB4(vport1,i + 17,red,green,blue);
  371.         }
  372.       return(0);
  373.      }
  374.   }
  375.