home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 460.lha / 3DPlot_v2.0 / sources / openclose8.c < prev    next >
C/C++ Source or Header  |  1991-01-04  |  9KB  |  411 lines

  1. /*------------ INCLUDE FILES ----------------*/
  2.  
  3. #include "common.h"
  4.  
  5.  
  6. /*------------    DEFINES  -------------*/
  7.  
  8. #define NO_OUTPUT_FILE 1L
  9. #define NO_ICON_FILE   2L
  10. #define NO_INPUT_FILE  3L
  11.  
  12. #define CkErr(expression)  {if (ifferr == IFF_OKAY) ifferr = (expression);}
  13.  
  14.  
  15. /*---------------  EXTERNAL STRUCTURES    -------------*/
  16.  
  17. extern struct Menu Menu1;
  18. extern struct NewWindow NewWindowStructure1;
  19. extern struct NewScreen NewScreenStructure;
  20.  
  21.  
  22. /*---------------  EXTERNAL DATA  ------------------*/
  23.  
  24. extern USHORT Palette[4];
  25. extern BOOL titleon;
  26. extern AllPlotData *apd;
  27.  
  28.  
  29. /*------------------ STRUCTURES ---------------*/
  30.  
  31. struct IntuitionBase *IntuitionBase;
  32. struct GfxBase *GfxBase;
  33. struct IconBase *IconBase;
  34. struct ArpBase *ArpBase;
  35. struct Library *ColorBase;
  36. struct IFFBase *IFFBase;
  37. struct ILBMBase *ILBMBase;
  38.  
  39. struct Screen *scr;
  40. struct Window *win;
  41. struct RastPort *rastport;
  42. struct ViewPort *viewport;
  43.  
  44. struct Screen *setscr;
  45. struct Window *setwin;
  46. struct RastPort *setrastport;
  47. struct ViewPort *setviewport;
  48.  
  49. static struct NewScreen newscr = {
  50.    0,0,320,200,5,    /* LeftEdge, TopEdge, Width, Height, Depth */
  51.    0,1,          /* DetailPen, BlockPen */
  52.    NULL,         /* ViewModes */
  53.    CUSTOMSCREEN,     /* Type */
  54.    NULL,         /* Font */
  55.    "3DPlot v2.0   1990 Randy Finch  (All Rights Reserved)", /* DefaultTitle */
  56.    NULL,         /* Gadgets */
  57.    NULL          /* CustomBitMap */
  58. };
  59.  
  60. static struct NewWindow newwin = {
  61.     0,0,320,200,        /* LeftEdge, TopEdge, Width, Height */
  62.     0,1,            /* DetailPen, BlockPen */
  63.     MENUPICK | GADGETUP | GADGETDOWN | REQCLEAR,  /* IDCMPFlags */
  64.     BACKDROP | ACTIVATE |
  65.     SMART_REFRESH | BORDERLESS, /* Flags */
  66.     NULL,            /* FirstGadget */
  67.     NULL,            /* CheckMark */
  68.     NULL,            /* Title */
  69.     NULL,            /* Screen */
  70.     NULL,            /* BitMap */
  71.     20,20,            /* MinWidth, MinHeight */
  72.     320,200,            /* MaxWidth, MaxHeight */
  73.     CUSTOMSCREEN        /* Type */
  74. };
  75.  
  76.  
  77. /*----------------- EXTERNAL FUNCTIONS ----------------*/
  78.  
  79. extern VOID FreeAllPlotData(AllPlotData *);
  80.  
  81.  
  82. /*------------------ FUNCTIONS -----------------*/
  83.  
  84. void finishup(code)
  85.    LONG code;
  86. {
  87.    if(apd) FreeAllPlotData(apd);
  88.    if(win) CloseWindow(win);
  89.    if(scr) CloseScreen(scr);
  90.    if(setwin) CloseWindow(setwin);
  91.    if(setscr) CloseScreen(setscr);
  92.    if(IntuitionBase) CloseLibrary(IntuitionBase);
  93.    if(GfxBase) CloseLibrary(GfxBase);
  94.    if(IconBase) CloseLibrary(IconBase);
  95.    if(ArpBase) CloseLibrary(ArpBase);
  96.    if(ColorBase) CloseLibrary(ColorBase);
  97.    if(IFFBase) CloseLibrary(IFFBase);
  98.    if(ILBMBase) CloseLibrary(ILBMBase);
  99.  
  100.    exit(code);
  101.  
  102. }  /* finishup */
  103.  
  104.  
  105. void openlibraries()
  106. {
  107.    IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",33);
  108.    if(!IntuitionBase) {
  109.       puts("Unable to open Intuition Library\n");
  110.       finishup(FALSE);
  111.    }
  112.  
  113.    GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",33);
  114.    if(!GfxBase) {
  115.       puts("Unable to open Graphics Library\n");
  116.       finishup(FALSE);
  117.    }
  118.  
  119.    IconBase = (struct IconBase *) OpenLibrary("icon.library",33);
  120.    if (!IconBase) {
  121.       puts("Can't open icon.library\n");
  122.       finishup(FALSE);
  123.    }
  124.  
  125.    ArpBase = (struct ArpBase *) OpenLibrary("arp.library",0);
  126.    if (!ArpBase) {
  127.       puts("Can't open arp.library\n");
  128.       finishup(FALSE);
  129.    }
  130.  
  131.    ColorBase = (struct Library *) OpenLibrary("color.library",0);
  132.    if (!ColorBase) {
  133.       puts("Can't open color.library\n");
  134.       finishup(FALSE);
  135.    }
  136.  
  137.    IFFBase = (struct IFFBase *) OpenLibrary("iff.library",0);
  138.    if (!IFFBase) {
  139.       puts("Can't open iff.library\n");
  140.       finishup(FALSE);
  141.    }
  142.  
  143.    ILBMBase = (struct ILBMBase *) OpenLibrary("ilbm.library",0);
  144.    if (!ILBMBase) {
  145.       puts("Can't open ilbm.library\n");
  146.       finishup(FALSE);
  147.    }
  148.  
  149. }   /* openlibraries */
  150.  
  151.  
  152. void opendisplay()
  153. {
  154.    scr = (struct Screen *) OpenScreen(&newscr);
  155.  
  156.    if(!scr) {
  157.       puts("Screen failed\n");
  158.       finishup(FALSE);
  159.    }
  160.  
  161.    newwin.Screen = scr;
  162.    win = (struct Window *) OpenWindow(&newwin);
  163.  
  164.    if(!win) {
  165.        puts("Window failed.\n");
  166.        finishup(FALSE);
  167.    }
  168.  
  169.    if(titleon) {
  170.       ShowTitle(scr, TRUE);
  171.    }
  172.    else {
  173.       ShowTitle(scr, FALSE);
  174.    }
  175.    SetMenuStrip(win, &Menu1);
  176.  
  177.    viewport = (struct ViewPort *) ViewPortAddress(win);
  178.    rastport = win->RPort;
  179.  
  180. }    /* opendisplay */
  181.  
  182.  
  183. LONG loaddisplay(STRPTR fn)
  184.    /* fn is filename */
  185. {
  186.    APTR ifffile = NULL;
  187.    long count;
  188.    UWORD colortable[128];
  189.    struct BitMapHeader *bmhd;
  190.  
  191.    /* Close current screen first */
  192.    ClearMenuStrip(win);
  193.    CloseWindow(win);
  194.    CloseScreen(scr);
  195.  
  196.    /* Open new file and get bitmap header */
  197.    if(!(ifffile=OldOpenIFF(fn)))        return IffError();
  198.    if(!(bmhd=GetIFFBMHD(ifffile)))   return IffError();
  199.  
  200.    newscr.Width      = bmhd->w;
  201.    newscr.Height     = bmhd->h;
  202.    newscr.Depth      = bmhd->nPlanes;
  203.    newscr.ViewModes  = GetViewModes(ifffile);
  204.  
  205.    scr = (struct Screen *) OpenScreen(&newscr);
  206.    if(!scr) {
  207.       puts("Screen failed\n");
  208.       finishup(FALSE);
  209.    }
  210.  
  211.    newwin.Screen = scr;
  212.    newwin.Width = newscr.Width;
  213.    newwin.MaxWidth = newscr.Width;
  214.    newwin.Height = newscr.Height;
  215.    newwin.MaxHeight = newscr.Height;
  216.  
  217.    win = (struct Window *) OpenWindow(&newwin);
  218.    if(!win) {
  219.        puts("Window failed.\n");
  220.        finishup(FALSE);
  221.    }
  222.  
  223.    count = GetColorTab(ifffile,colortable);
  224.    if(count>32L) count = 32L; /* Some HAM pictures have 64 colors ?! */
  225.    LoadRGB4(&(scr->ViewPort),colortable,count);
  226.  
  227.    viewport = (struct ViewPort *) ViewPortAddress(win);
  228.    rastport = win->RPort;
  229.    SetMenuStrip(win, &Menu1);
  230.    ShowTitle(scr, FALSE);  /* Turn off for loading pic */
  231.    if(!DecodePic(ifffile,rastport->BitMap)) {
  232.       CloseWindow(win);
  233.       CloseScreen(scr);
  234.       return IffError();
  235.    }
  236.  
  237.    /* Turn on title if necessary */
  238.    if(titleon) {
  239.       ShowTitle(scr, TRUE);
  240.    }
  241.  
  242.    CloseIFF(ifffile);
  243.  
  244.    return 0L;
  245. }    /* loaddisplay */
  246.  
  247.  
  248. LONG loadcolors(STRPTR fn)
  249.    /* fn is filename */
  250. {
  251.    APTR ifffile = NULL;
  252.    long count;
  253.    UWORD colortable[128];
  254.  
  255.    /* Open new file and get colors */
  256.    if(!(ifffile=OldOpenIFF(fn)))   return IffError();
  257.    count = GetColorTab(ifffile,colortable);
  258.    if(count>32L) count = 32L; /* Some HAM pictures have 64 colors ?! */
  259.    LoadRGB4(&(scr->ViewPort),colortable,count);
  260.  
  261.    CloseIFF(ifffile);
  262.  
  263.    return 0L;
  264. }    /* loadcolors */
  265.  
  266.  
  267. IFFP savecolors(STRPTR filename)
  268. {
  269.   LONG         file;
  270.   IFFP         ifferr = IFF_OKAY;
  271.   GroupContext     fileContext, formContext;
  272.  
  273.   if (!(file = Open(filename, MODE_NEWFILE))) {
  274.      printf("Can't open output file\n");
  275.      return (NO_OUTPUT_FILE);
  276.   }
  277.  
  278.   Write(file,"x",1);  /* 1.1 so Seek to beginning works ? */
  279.  
  280. #if DEBUG
  281.   printf("\nSaving...\n");
  282. #endif
  283.  
  284.   CkErr( OpenWIFF(szNotYetKnown, file, &fileContext) );
  285.   CkErr( StartWGroup(FORM,szNotYetKnown,ID_ILBM,&fileContext,&formContext) );
  286.   CkErr( PutCMAP(scr->BitMap.Depth, &formContext, viewport->ColorMap->ColorTable) );
  287.   CkErr( EndWGroup(&formContext) );
  288.   CkErr( CloseWGroup(&fileContext) );
  289.  
  290.   Close(file);
  291.  
  292. #if DEBUG
  293.   if (ifferr == IFF_OKAY) {
  294.      printf("3DPL IFF saved\n");
  295.   }
  296.   else {
  297.      printf("Error on save\n");
  298.   }
  299. #endif
  300.  
  301.   return(ifferr);
  302.  
  303. }  /* savecolors */
  304.  
  305.  
  306. void opendisplayset()
  307. {
  308.    setscr = (struct Screen *) OpenScreen(&NewScreenStructure);
  309.  
  310.    if(!setscr) {
  311.       puts("Screen failed\n");
  312.       finishup(FALSE);
  313.    }
  314.  
  315.    NewWindowStructure1.Screen = setscr;
  316.    setwin = (struct Window *) OpenWindow(&NewWindowStructure1);
  317.  
  318.    if(!setwin) {
  319.        puts("Window failed.\n");
  320.        finishup(FALSE);
  321.    }
  322.  
  323.   SetMenuStrip(setwin, &Menu1);
  324.  
  325.   setviewport = (struct ViewPort *) ViewPortAddress(setwin);
  326.   setrastport = setwin->RPort;
  327.  
  328.   LoadRGB4(setviewport, Palette, 4);
  329.  
  330. }    /* opendisplayset */
  331.  
  332.  
  333. void ChangeResolution(res)
  334.   UBYTE res;
  335. {
  336.   BOOL change = FALSE;
  337.  
  338.   switch(res) {
  339.  
  340.      case SCREENLOWRES:
  341.     if(newscr.Width != 320) {
  342.        newscr.Width = 320;
  343.        newscr.Depth = 5;
  344.        newscr.ViewModes &= ~HIRES;
  345.  
  346.        newwin.Width = 320;
  347.        newwin.MaxWidth = 320;
  348.  
  349.        change = TRUE;
  350.     }
  351.     break;
  352.  
  353.      case SCREENHIGHRES:
  354.     if(newscr.Width != 640) {
  355.        newscr.Width = 640;
  356.        newscr.Depth = 4;
  357.        newscr.ViewModes |= HIRES;
  358.  
  359.        newwin.Width = 640;
  360.        newwin.MaxWidth = 640;
  361.  
  362.        change = TRUE;
  363.     }
  364.     break;
  365.  
  366.      case SCREENINTERLACE:
  367.     if(newscr.Height != 400) {
  368.        newscr.Height = 400;
  369.        newscr.ViewModes |= LACE;
  370.  
  371.        newwin.Height = 400;
  372.        newwin.MaxHeight = 400;
  373.  
  374.        change = TRUE;
  375.     }
  376.     break;
  377.  
  378.      case SCREENNONINTERLACE:
  379.     if(newscr.Height != 200) {
  380.        newscr.Height = 200;
  381.        newscr.ViewModes &= ~LACE;
  382.  
  383.        newwin.Height = 200;
  384.        newwin.MaxHeight = 200;
  385.  
  386.        change = TRUE;
  387.     }
  388.     break;
  389.  
  390.      case SCREENNORMAL:
  391.      case SCREENMEDIUMOVERSCAN:
  392.      case SCREENSEVEREOVERSCAN:
  393.     break;
  394.  
  395.      default:
  396.     break;
  397.  
  398.   } /* switch */
  399.  
  400.   if (change == TRUE) {
  401.      ClearMenuStrip(win);
  402.      CloseWindow(win);
  403.      CloseScreen(scr);
  404.  
  405.      opendisplay();
  406.  
  407.   } /* if */
  408.  
  409. } /* ChangeResolution */
  410.  
  411.