home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff256.lzh / BlitDemons / bdintui.c < prev    next >
C/C++ Source or Header  |  1989-10-19  |  13KB  |  425 lines

  1. /*******************************************************************************
  2.  *   BlitDemons by Walter Strickler
  3.  *   This program and all its source code are in the public domain and are
  4.  * freely distributable and usable for any purpose, private or commercial.
  5.  ******************************************************************************/
  6.  
  7. #include "BDemon.h"
  8.  
  9.  
  10. /*******************************************************************************
  11.  *     Intuition stuff is global:
  12.  ******************************************************************************/
  13. struct Window *BDWindow;
  14. struct Screen *BDScreen;
  15.  
  16. /*******************************************************************************
  17.  *    Defines to make life with Power Windows easier...
  18.  *****************************************************************************/
  19. #define BDNewWindow BDWindowNewWindowStructure3
  20. #define ABD1NewWindow ABDWin1NewWindowStructure4
  21. #define ABD2NewWindow ABDWin2NewWindowStructure1
  22. #define ABD3NewWindow ABDWin3NewWindowStructure2
  23. #define ABD1IText     ABDWin1IText28
  24. #define ABD2IText     ABDWin2IText1
  25. #define ABD3IText     ABDWin3IText16
  26.  
  27. /*******************************************************************************
  28.  *    This stuff came from PowerWindows, but we gotta know what it is here.
  29.  *****************************************************************************/
  30. extern struct NewScreen NewScreenStructure;
  31. extern struct NewWindow BDNewWindow;
  32. extern struct Menu BDWindowMenu1;
  33. extern struct NewWindow ABD1NewWindow;
  34. extern struct NewWindow ABD2NewWindow;
  35. extern struct NewWindow ABD3NewWindow;
  36. extern struct IntuiText ABD1IText;
  37. extern struct IntuiText ABD2IText;
  38. extern struct IntuiText ABD3IText;
  39.  
  40.  
  41. /*******************************************************************************
  42.  *    Here is a useful #define for turning on/off Menu items.
  43.  *****************************************************************************/
  44. #define GET_MENU_NUMBER(Menu, Item) (SHIFTMENU((Menu)) + SHIFTITEM((Item)))
  45.  
  46.  
  47. /*******************************************************************************
  48.    InitIntui() opens various system things.
  49. *******************************************************************************/
  50. int InitIntui()
  51.     { 
  52.     int RetVal;
  53.  
  54.     IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",0);
  55.     if (IntuitionBase == 0) 
  56.         {
  57.         RetVal = NO_INTUI;
  58.         }
  59.     else
  60.         {
  61.         GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",0);
  62.         if (GfxBase == 0) 
  63.             {
  64.             RetVal = NO_GFX;
  65.             }
  66.         else
  67.             {
  68.             BDScreen = OpenScreen(&NewScreenStructure);
  69.             if (BDScreen == NULL)
  70.                 {
  71.                 RetVal = NO_SCREEN;
  72.                 }
  73.              else
  74.                 {
  75.                 /* Set the Palette */
  76.                 SetPalette();
  77.                 /* Power Windows doesn't init this schtick 'cause it can't */
  78.                 BDNewWindow.Screen = BDScreen;
  79.                 ABD1NewWindow.Screen = BDScreen;
  80.                 ABD2NewWindow.Screen = BDScreen;
  81.                 ABD3NewWindow.Screen = BDScreen;
  82.                 BDWindow = OpenWindow(&BDNewWindow);
  83.                 if (BDWindow == NULL)
  84.                     {
  85.                     RetVal = NO_WIN;
  86.                     }
  87.                 else
  88.                     {
  89.                     SetMenuStrip(BDWindow, &BDWindowMenu1);
  90.                     RetVal = INTUI_OK;
  91.                     }   /* END window open */
  92.                 }   /* END screen open */
  93.             }   /* END Gfx open */
  94.         }  /* END intuition open */
  95.     return RetVal;
  96.     }  /* END IntuiInit() */
  97.  
  98. void SetPalette()
  99.     {
  100. /*
  101.     struct ViewPort *ThisVP;
  102.     USHORT           RVal,
  103.                      GVal,
  104.                      BVal;
  105.     int              i;
  106.  
  107.     static int Red[32] =   {1,1, 0, 0, 0,0,0, 3, 9,11,13,15,15,14,15,0};
  108.     static int Green[32] = {0,0, 0, 0, 7,9,9,10,11,12,10, 6, 5, 1, 0,0};
  109.     static int Blue[32] =  {4,9,12,14,11,5,0, 0, 0, 0, 0, 0, 0, 0, 0,0};
  110. */
  111.  
  112. /*   Neither of these algorithms produced satisfying results.  There was
  113.  * Video beam hash with both that didn't exist with the default colors.
  114.  */
  115.  
  116.     /* Set Palette as in MFPU */
  117. /*    ThisVP = &(BDScreen -> ViewPort);
  118.     for (i = 2; i <= 15; i++)
  119.         {
  120.         SetRGB4(ThisVP, i, Red[i], Green[i], Blue[i]);        
  121.         }
  122. */
  123.  
  124.     /* Set Palette according to algorithm by Loren Blaney: */
  125. /*  
  126.     RVal = 14;
  127.     GVal = 2;
  128.     BVal = 0; 
  129.     for (i = 2; i <= 8; i++)
  130.         {
  131.         SetRGB4(ThisVP, i, RVal, GVal, BVal);
  132.         RVal = RVal - 2;
  133.         GVal = GVal + 2;
  134.         }
  135.     RVal = 0;
  136.     GVal = 14;
  137.     BVal = 2;
  138.     for (i = 9; i <= 15; i++)
  139.         {
  140.         SetRGB4(ThisVP, i, RVal, GVal, BVal);
  141.         GVal = GVal - 2;
  142.         BVal = BVal + 2;
  143.         }
  144. */
  145.     }  /* End SetPalette() */
  146.     
  147. /*******************************************************************************
  148.  *  CloseIntui closes everything opened by IntiIntui().
  149.  ******************************************************************************/
  150. void CloseIntui()
  151.     {
  152.     if (IntuitionBase != NULL)
  153.         {
  154.         CloseLibrary((struct Library *) IntuitionBase);
  155.         }
  156.     if (GfxBase != NULL)
  157.         {
  158.         CloseLibrary((struct Library *) GfxBase);
  159.         }
  160.     ClearMenuStrip(BDWindow);
  161.     if (BDWindow != NULL)
  162.         {
  163.         CloseWindow(BDWindow);
  164.         }
  165.     if (BDScreen != NULL)
  166.         {
  167.         CloseScreen (BDScreen);
  168.         }
  169.     }
  170.  
  171.  
  172. /*******************************************************************************
  173.  *  BDClearScreen() clears the blitfield.
  174. *******************************************************************************/
  175. /*
  176. BDClearScreen()
  177.     {
  178.     SetRast(BDWindow -> RPort, 0);
  179.     }
  180. */
  181.  
  182. /*******************************************************************************
  183.  *  This function checks the message port BDWindow -> UserPort, waiting if
  184.  * the parameter Control has a value of WAIT, polling otherwise.  Returns
  185.  * constants relative to the type of message it found.
  186. *******************************************************************************/
  187. int CheckMsg(Control)
  188.     int Control;
  189.     {
  190.     struct IntuiMessage *ThisMessage,
  191.                          MsgCopy;
  192.     ULONG                ThisClass;
  193.     int                  RetVal,
  194.                          MenuVal,
  195.                          RecurseReturn;
  196.  
  197.     if (Control == WAIT)
  198.         {
  199.         WaitPort(BDWindow -> UserPort);
  200.         }
  201.     ThisMessage = (struct IntuiMessage *) GetMsg(BDWindow->UserPort);
  202.     if (ThisMessage != NULL)
  203.         {
  204.         MsgCopy = *ThisMessage;
  205.         ReplyMsg((struct Message *) ThisMessage);   /* Away with thee! */
  206.         ThisClass = MsgCopy.Class;
  207.         switch (ThisClass)
  208.             {
  209.             case MENUVERIFY:
  210.                 /* Recursion time! */
  211.                 RecurseReturn = CheckMsg(WAIT);  /* Get the menu message */
  212.                 RetVal = RecurseReturn;  /* Return the menu message value */
  213.                 break;
  214.             case MENUPICK:
  215.                 MenuVal = DoMenus(&MsgCopy);
  216.                 if (MenuVal != NO_MENU)
  217.                     {
  218.                     RetVal = MenuVal;
  219.                     }
  220.                 else 
  221.                     {
  222.                     RetVal = NO_MSG;
  223.                     }
  224.                 break;
  225.             case CLOSEWINDOW:
  226.                 RetVal = CLOSE_WIN;
  227.                 break;
  228.             default:   
  229.                 assert (FALSE);  /* Shouldn't be here */        
  230.                 break;
  231.             }      /* End switch(Class) */
  232.         }     /* End Have a message */
  233.     else   /* Tell caller no message */
  234.         {
  235.         RetVal = NO_MSG;
  236.         }     /* End have no message */
  237.     return RetVal;
  238.     }    /* End CheckMsg() */
  239.  
  240.  
  241.  
  242. /*******************************************************************************
  243.  *   DoMenus returns a constant depending on what kind of menu item is specified
  244.  * by the Message pointed at by Msg.
  245. *******************************************************************************/
  246. #define PROJECT_MENU 0
  247. #define ABOUT_ITEM 0
  248. #define NEW_ITEM   1
  249. #define START_ITEM 2
  250. #define STOP_ITEM  3
  251. #define QUIT_ITEM  4
  252.  
  253.  
  254. int DoMenus(Msg)
  255.     struct IntuiMessage *Msg;
  256.  
  257.     {
  258.     USHORT  MenuNumber;
  259.     int  Item,
  260.          RetVal;
  261.  
  262.     MenuNumber = Msg -> Code;
  263.     if (MenuNumber != MENUNULL)  /* Note: assuming only one menu */
  264.         {
  265.         Item = ITEMNUM(MenuNumber);
  266.         switch (Item)
  267.             {
  268.             /* This is so obvious it must be good code... */
  269.             case ABOUT_ITEM:
  270.                 RetVal = ABOUT;
  271.                 break;
  272.             case STOP_ITEM:
  273.                 RetVal = STOP;
  274.                 break;
  275.             case START_ITEM:
  276.                 RetVal = START;
  277.                 break;
  278.             case NEW_ITEM:
  279.                 RetVal = NEW;
  280.                 break;
  281.             case QUIT_ITEM:
  282.                 RetVal = QUIT;
  283.                 break;
  284.             default:
  285.                 RetVal = NO_MENU;
  286.                 break;
  287.             }   /* End switch (Item) */
  288.         }  /* End if not MENUNULL */
  289.     else
  290.         {
  291.         RetVal = NO_MENU;
  292.         }  /* End if MENUNULL */
  293.     return RetVal;
  294.     }      
  295.  
  296.  
  297. /*******************************************************************************
  298.  *   DisplayAbout() displays all three "About" windows.
  299.  ******************************************************************************/
  300. int DisplayAbout()
  301.     {
  302.     int RetVal;
  303.     struct Window *Window1,
  304.                   *Window2,
  305.                   *Window3;
  306.  
  307.     ClearBDMenu();
  308.     Window1 = Window2 = Window3 = NULL;
  309.  
  310.     Window1 = DisplayOne(&ABD1NewWindow, &ABD1IText);
  311.     if (Window1 != NULL)
  312.         {
  313.         Window2 = DisplayOne(&ABD2NewWindow, &ABD2IText);
  314.         CloseWindow(Window1);   /* Window2 is now covering blitfield */
  315.         if (Window2 != NULL)
  316.             {
  317.             Window3 = DisplayOne(&ABD3NewWindow, &ABD3IText);
  318.             CloseWindow(Window2);  /* Window3 is now covering blitfield */
  319.             if (Window3 != NULL)
  320.                 {                
  321.                 /* Window3 opened and close gadget selected, it */
  322.                 CloseWindow(Window3);
  323.                 RetVal = DA_OK;
  324.                 }  /* End Window3 OK */
  325.             else
  326.                 {
  327.                 RetVal = DA_CHOKE;
  328.                 }  /* End Window3 choked */
  329.             }  /* End Window2 OK */
  330.         else
  331.             {
  332.             RetVal = DA_CHOKE;
  333.             }  /* End Window2 choked */
  334.         }  /* End Window1 OK */
  335.     else
  336.         {
  337.         RetVal = DA_CHOKE;
  338.         }   /* End Window1 choked */
  339.     SetBDMenu();
  340.     return RetVal;
  341.     }
  342.  
  343.  
  344. /*******************************************************************************
  345.  *   DisplayOne() displays one "About" window.
  346.  ******************************************************************************/
  347. struct Window *DisplayOne(AboutNewWindow, IText)
  348.     struct NewWindow *AboutNewWindow;
  349.     struct IntuiText *IText;
  350.  
  351.     {
  352.     struct Window       *AboutWindow;
  353.     struct IntuiMessage *ThisMessage; 
  354.     int                  AllDone;
  355.  
  356.     AboutWindow = OpenWindow (AboutNewWindow);
  357.     if (AboutWindow != 0)
  358.         {
  359.         PrintIText(AboutWindow -> RPort, IText, 0,0);
  360.         AllDone = FALSE;
  361.         while (!AllDone)
  362.             {
  363.             WaitPort(AboutWindow -> UserPort);
  364.             ThisMessage = 
  365.              (struct IntuiMessage *) GetMsg(AboutWindow -> UserPort);
  366.             if (ThisMessage != NULL)
  367.                 {
  368.                 if (ThisMessage -> Class == CLOSEWINDOW)
  369.                     {
  370.                     AllDone = TRUE;
  371.                     }
  372.                 ReplyMsg((struct Message *) ThisMessage);
  373.                 }    /* End if Message != NULL */
  374.             }    /* End while !AllDone */
  375.         }
  376.     return AboutWindow;
  377.     }
  378.     
  379.  
  380. /*******************************************************************************
  381.  *   These functions support information hiding by providing functions to 
  382.  * operate on relatively hidden objects (in this case, menu items).
  383.  ******************************************************************************/
  384. void OnStart()
  385.     {
  386.     OnMenu(BDWindow, GET_MENU_NUMBER(PROJECT_MENU, START_ITEM));
  387.     }   /* End OnStart */
  388.  
  389. void OffStart()
  390.     {
  391.     OffMenu(BDWindow, GET_MENU_NUMBER(PROJECT_MENU, START_ITEM));
  392.     }   /* End OffStart */
  393.  
  394. void OnStop()
  395.     {
  396.     OnMenu(BDWindow, GET_MENU_NUMBER(PROJECT_MENU, STOP_ITEM));
  397.     }   /* End OnStop */
  398.  
  399. void OffStop()
  400.     {
  401.     OffMenu(BDWindow, GET_MENU_NUMBER(PROJECT_MENU, STOP_ITEM));
  402.     }   /* End OffStop */
  403.  
  404.  
  405. void SetBDMenu()
  406.     {
  407.     ULONG Flags;
  408.  
  409.     Flags = (BDWindow -> IDCMPFlags) | MENUVERIFY; /* Set MENUVERIFY */
  410.     ModifyIDCMP(BDWindow, Flags); 
  411.     SetMenuStrip(BDWindow, &BDWindowMenu1);
  412.     }
  413.  
  414. void ClearBDMenu()
  415.     {
  416.     ULONG Flags;
  417.  
  418.     Flags = (BDWindow -> IDCMPFlags) & ~MENUVERIFY; /* Clear that nasty */
  419.                                                     /* MENUVERIFY bit */
  420.     ModifyIDCMP(BDWindow, Flags);    
  421.     ClearMenuStrip(BDWindow);
  422.     }
  423.     
  424.  
  425.