home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 030.lha / Ogre / termcap.c < prev   
C/C++ Source or Header  |  1986-11-10  |  24KB  |  1,022 lines

  1. /*
  2.  * This file contains all the console I/O stuff, most of which is cribbed
  3.  * from the ROM Kernel Manual. Some of the routines here are not used, like
  4.  * clear_screen() and the cursor movement defines.  The idea behind the
  5.  * apparently redundant routines Amiga_putchar(), Amiga_getchar(), etc, is
  6.  * to avoid having external pointers to windows and message ports in all the
  7.  * other files.
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/io.h>
  12. #include <exec/exec.h>
  13. #include <exec/memory.h>
  14. #include <graphics/gfx.h>
  15. #include <hardware/dmabits.h>
  16. #include <hardware/custom.h>
  17. #include <hardware/blit.h>
  18. #include <graphics/gfxmacros.h>
  19. #include <graphics/copper.h>
  20. #include <graphics/view.h>
  21. #include <graphics/gels.h>
  22. #include <graphics/regions.h>
  23. #include <graphics/clip.h>
  24. #include <graphics/text.h>
  25. #include <graphics/gfxbase.h>
  26. #include <libraries/dos.h>
  27. #include <devices/console.h>
  28. #include <devices/keymap.h>
  29. #include <intuition/intuition.h>
  30.  
  31. #define HELPKEY   '?'
  32.  
  33. struct TextAttr MyFont =
  34. {
  35.             "topaz.font", TOPAZ_EIGHTY,
  36.              FS_NORMAL,FPF_ROMFONT
  37. };
  38.  
  39. struct NewWindow nw =
  40. {
  41.             0, 0, 640, 200, 3, 1, CLOSEWINDOW|MENUPICK|GADGETUP|GADGETDOWN,
  42.             WINDOWCLOSE|SMART_REFRESH|ACTIVATE|GIMMEZEROZERO|BORDERLESS,
  43.             0, NULL,"Ogre 2.0 by Hobie Orris", NULL, NULL, 100, 45,
  44.             640, 200, CUSTOMSCREEN
  45. };
  46.  
  47. struct NewScreen ns =
  48. {
  49.             0, 0, 640, 200, 2, 0, 1, HIRES, CUSTOMSCREEN,
  50.             &MyFont, NULL, 0, 0
  51. };
  52.  
  53.  
  54.  
  55. extern struct MsgPort *CreatePort();
  56. extern struct IOStdReq *CreateStdIO();
  57.  
  58. struct Screen *scrn;
  59. struct Window *w;
  60. struct RastPort *rp;
  61. struct IOStdReq *WriteMsg, *ReadMsg;
  62. struct MsgPort *WritePort, *ReadPort;
  63. struct IntuiMessage *Imsg;
  64.  
  65. ULONG DosBase, GfxBase, IntuitionBase;
  66.  
  67. /************************************************************************/
  68. /*                                                                      */
  69. /*                            Gadget Stuff                              */
  70. /*                                                                      */
  71. /************************************************************************/
  72.  
  73. /* The gadgets look like this:
  74.  *
  75.  *                            Gadget0     Gadget1
  76.  *                      Gadget2     Gadget6     Gadget3
  77.  *                            Gadget4     Gadget5
  78.  *
  79.  * All have plane 0 as below, and plane 1 as zeroes.
  80.  */
  81.  
  82. /* offsets of gadget 0 from the window top left */
  83.  
  84. #define BASEPTX   450      /* x offset */
  85. #define BASEPTY   123      /* y offset */
  86.  
  87. /* the pictures themselves */
  88.  
  89. USHORT Imgadget0[36] =
  90. {
  91.    0xffff, 0xf000,
  92.    0x8000, 0x1000,
  93.    0xbff0, 0x1000,
  94.    0xbfe0, 0x1000,
  95.    0xbfc0, 0x1000,
  96.    0xbf80, 0x1000,
  97.    0xbfc0, 0x1000,
  98.    0xbfe0, 0x1000,
  99.    0xbbf0, 0x1000,
  100.    0xb1f8, 0x1000,
  101.    0xa0fc, 0x1000,
  102.    0x807e, 0x1000,
  103.    0x803f, 0x1000,
  104.    0x801f, 0x9000,
  105.    0x800f, 0x1000,
  106.    0x8006, 0x1000,
  107.    0x8000, 0x1000,
  108.    0xffff, 0xf000
  109. };
  110.  
  111. USHORT Imgadget1[36] =
  112. {
  113.    0xffff, 0xf000,
  114.    0x8000, 0x1000,
  115.    0x80ff, 0xd000,
  116.    0x807f, 0xd000,
  117.    0x803f, 0xd000,
  118.    0x801f, 0xd000,
  119.    0x803f, 0xd000,
  120.    0x807f, 0xd000,
  121.    0x80fd, 0xd000,
  122.    0x81f8, 0xd000,
  123.    0x83f0, 0x5000,
  124.    0x87e0, 0x1000,
  125.    0x8fc0, 0x1000,
  126.    0x9f80, 0x1000,
  127.    0x8f00, 0x1000,
  128.    0x8600, 0x1000,
  129.    0x8000, 0x1000,
  130.    0xffff, 0xf000
  131. };
  132.  
  133. USHORT Imgadget2[36] =
  134. {
  135.    0xffff, 0xf000,
  136.    0x8000, 0x1000,
  137.    0x8080, 0x1000,
  138.    0x8180, 0x1000,
  139.    0x8380, 0x1000,
  140.    0x8700, 0x1000,
  141.    0x8fff, 0xd000,
  142.    0x9fff, 0xd000,
  143.    0xbfff, 0xd000,
  144.    0x9fff, 0xd000,
  145.    0x8fff, 0xd000,
  146.    0x8700, 0x1000,
  147.    0x8380, 0x1000,
  148.    0x8180, 0x1000,
  149.    0x8080, 0x1000,
  150.    0x8000, 0x1000,
  151.    0x8000, 0x1000,
  152.    0xffff, 0xf000
  153. };
  154.  
  155. USHORT Imgadget3[36] =
  156. {
  157.    0xffff, 0xf000,
  158.    0x8000, 0x1000,
  159.    0x8010, 0x1000,
  160.    0x8018, 0x1000,
  161.    0x801c, 0x1000,
  162.    0x801e, 0x1000,
  163.    0xbfff, 0x1000,
  164.    0xbfff, 0x9000,
  165.    0xbfff, 0xd000,
  166.    0xbfff, 0x9000,
  167.    0xbfff, 0x1000,
  168.    0x801e, 0x1000,
  169.    0x801c, 0x1000,
  170.    0x8018, 0x1000,
  171.    0x8010, 0x1000,
  172.    0x8000, 0x1000,
  173.    0x8000, 0x1000,
  174.    0xffff, 0xf000,
  175. };
  176.  
  177. USHORT Imgadget4[36] =
  178. {
  179.    0xffff, 0xf000,
  180.    0x8000, 0x1000,
  181.    0x8006, 0x1000,
  182.    0x800f, 0x1000,
  183.    0x801f, 0x9000,
  184.    0x803f, 0x1000,
  185.    0x807e, 0x1000,
  186.    0xa0fc, 0x1000,
  187.    0xb1f8, 0x1000,
  188.    0xbbf0, 0x1000,
  189.    0xbfe0, 0x1000,
  190.    0xbfc0, 0x1000,
  191.    0xbf80, 0x1000,
  192.    0xbfc0, 0x1000,
  193.    0xbfe0, 0x1000,
  194.    0xbff0, 0x1000,
  195.    0x8000, 0x1000,
  196.    0xffff, 0xf000
  197. };
  198.  
  199. USHORT Imgadget5[36] =
  200. {
  201.    0xffff, 0xf000,
  202.    0x8000, 0x1000,
  203.    0x8600, 0x1000,
  204.    0x8f00, 0x1000,
  205.    0x9f80, 0x1000,
  206.    0x8fc0, 0x1000,
  207.    0x87e0, 0x1000,
  208.    0x83f0, 0x5000,
  209.    0x81f8, 0xd000,
  210.    0x80fd, 0xd000,
  211.    0x807f, 0xd000,
  212.    0x803f, 0xd000,
  213.    0x801f, 0xd000,
  214.    0x803f, 0xd000,
  215.    0x807f, 0xd000,
  216.    0x80ff, 0xd000,
  217.    0x8000, 0x1000,
  218.    0xffff, 0xf000
  219. };
  220.  
  221. USHORT Imgadget6[36] =
  222. {
  223.    0xffff, 0xf000,
  224.    0x8000, 0x1000,
  225.    0x81fc, 0x1000,
  226.    0x83fe, 0x1000,
  227.    0x8706, 0x1000,
  228.    0x8e00, 0x1000,
  229.    0x8c00, 0x1000,
  230.    0x8e00, 0x1000,
  231.    0x87fc, 0x1000,
  232.    0x83fe, 0x1000,
  233.    0x8007, 0x1000,
  234.    0x8003, 0x1000,
  235.    0x8007, 0x1000,
  236.    0x860e, 0x1000,
  237.    0x87fc, 0x1000,
  238.    0x83f8, 0x1000,
  239.    0x8000, 0x1000,
  240.    0xffff, 0xf000
  241. };
  242.  
  243. #define PLNUM  0x01  /* number of planes in image */
  244. #define PLPICK 0x02  /* display in plane 1 */
  245. #define PLOFF  0x01  /* plane 0 turned on */
  246.  
  247. struct Image Gadgpic[7] =
  248. {
  249.    /* image 0 */
  250.    0, 0, 20, 18, PLNUM, &Imgadget0[0], PLPICK, PLOFF, NULL,
  251.  
  252.    /* image 1 */
  253.    0, 0, 20, 18, PLNUM, &Imgadget1[0], PLPICK, PLOFF, NULL,
  254.  
  255.    /* image 2 */
  256.    0, 0, 20, 18, PLNUM, &Imgadget2[0], PLPICK, PLOFF, NULL,
  257.  
  258.    /* image 3 */
  259.    0, 0 , 20, 18, PLNUM, &Imgadget3[0], PLPICK, PLOFF, NULL,
  260.  
  261.    /* image 4 */
  262.    0, 0, 20, 18, PLNUM, &Imgadget4[0], PLPICK, PLOFF, NULL,
  263.  
  264.    /* image 5 */
  265.    0, 0, 20, 18, PLNUM, &Imgadget5[0], PLPICK, PLOFF, NULL,
  266.  
  267.    /* image 6 */
  268.    0, 0, 20, 18, PLNUM, &Imgadget6[0], PLPICK, PLOFF, NULL
  269. };
  270.  
  271.  
  272. struct Gadget Arrow[7] =
  273. {
  274.    /* Up left arrow 0 */
  275.    NULL, BASEPTX, BASEPTY, 20, 18,
  276.    GADGHCOMP|GADGIMAGE, GADGIMMEDIATE|RELVERIFY,
  277.    BOOLGADGET, &Gadgpic[0], NULL, NULL, NULL, NULL, NULL, NULL,
  278.  
  279.    /* Up right arrow 1 */
  280.    NULL, BASEPTX + 20, BASEPTY, 20, 18,
  281.    GADGHCOMP|GADGIMAGE, RELVERIFY|GADGIMMEDIATE,
  282.    BOOLGADGET, &Gadgpic[1], NULL, NULL, NULL, NULL, NULL, NULL,
  283.  
  284.  
  285.    /* left arrow 2 */
  286.    NULL, BASEPTX - 10, BASEPTY + 18, 20, 18,
  287.    GADGHCOMP|GADGIMAGE, RELVERIFY|GADGIMMEDIATE,
  288.    BOOLGADGET, &Gadgpic[2], NULL, NULL, NULL, NULL, NULL, NULL,
  289.  
  290.    /* right arrow 3 */
  291.    NULL, BASEPTX + 30, BASEPTY + 18, 20, 18,
  292.    GADGHCOMP|GADGIMAGE, RELVERIFY|GADGIMMEDIATE,
  293.    BOOLGADGET, &Gadgpic[3], NULL, NULL, NULL, NULL, NULL, NULL,
  294.  
  295.    /* down left arrow 4 */
  296.    NULL, BASEPTX, BASEPTY + 36, 20, 18,
  297.    GADGHCOMP|GADGIMAGE, RELVERIFY|GADGIMMEDIATE,
  298.    BOOLGADGET, &Gadgpic[4], NULL, NULL, NULL, NULL, NULL, NULL,
  299.  
  300.    /* down right arrow 5 */
  301.    NULL, BASEPTX + 20, BASEPTY + 36, 20, 18,
  302.    GADGHCOMP|GADGIMAGE, RELVERIFY|GADGIMMEDIATE,
  303.    BOOLGADGET, &Gadgpic[5], NULL, NULL, NULL, NULL, NULL, NULL,
  304.  
  305.    /* sit 6 */
  306.    NULL, BASEPTX + 10, BASEPTY + 18, 20, 18,
  307.    GADGHCOMP|GADGIMAGE, RELVERIFY|GADGIMMEDIATE,
  308.    BOOLGADGET, &Gadgpic[6], NULL, NULL, NULL, NULL, NULL, NULL,
  309. };
  310.  
  311. /*  These are the command characters returned to Amiga_getchar()
  312.  *  when a gadget is selected.
  313.  */
  314.  
  315. char Gadtbl[] =
  316. {
  317.                'w', 'e', 'a', 'd', 'z', 'x', 's', '\0'
  318. };
  319.  
  320. /*************************************************************************/
  321. /*                                                                       */
  322. /*                           Menu Stuff                                  */
  323. /*                                                                       */
  324. /*************************************************************************/
  325.  
  326. /* Main menu structure which has three entries: Deploy, Target and Help */
  327.  
  328. #define WID  112
  329. #define WID2 130
  330.  
  331. struct Menu mymenu[3] = { NULL, 1, 0, WID, 0, MENUENABLED, NULL, NULL,
  332.                           0, 0, 0, 0,
  333.                           NULL, WID + 1, 0, WID, 0, MENUENABLED, NULL, NULL,
  334.                           0, 0, 0, 0,
  335.                           NULL, WID * 2 + 2, 0, WID, 0, MENUENABLED, NULL,
  336.                           NULL, 0, 0, 0, 0
  337. };
  338.  
  339. /* Menu entries for the Deploy menu */
  340.  
  341. struct MenuItem Menu1[6] = { /* 1st item */
  342.                              NULL, 0, 0, WID, 8,
  343.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0, 0, 0,
  344.  
  345.                              /* 2nd item */
  346.                              NULL, 0, 10, WID, 8,
  347.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0, 0, 0,
  348.  
  349.                              /* 3rd item */
  350.                              NULL, 0, 20, WID, 8,
  351.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0, 0,0,
  352.  
  353.                              /* 4th item */
  354.                              NULL, 0, 30, WID, 8,
  355.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0, 0, 0,
  356.  
  357.                              /* 5th item */
  358.                              NULL, 0, 40, WID, 8,
  359.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0, 0,0,
  360.  
  361.                              /* 6th item */
  362.                              NULL, 0, 50, WID, 8,
  363.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0, 0, 0
  364.                         };
  365.  
  366.  
  367. struct MenuItem Menu2[7] = { NULL, 0, 0, WID2, 8,
  368.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0, 0, 0,
  369.  
  370.                              /* 2nd item */
  371.                              NULL, 0, 10, WID2, 8,
  372.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0,0,0,
  373.  
  374.                              /* 3rd item */
  375.                              NULL, 0, 20, WID2, 8,
  376.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0, 0, 0,
  377.  
  378.                              /* 4th item */
  379.                              NULL, 0, 30, WID2, 8,
  380.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0, 0,0,
  381.  
  382.                              /* 5th item */
  383.                              NULL, 0, 40, WID2, 8,
  384.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0, 0,0,
  385.  
  386.                              /* 6th item */
  387.                              NULL, 0, 50, WID2, 8,
  388.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0, 0,0,
  389.  
  390.                              /* 7th item */
  391.                              NULL, 0, 60, WID2, 8,
  392.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0, 0, 0
  393. };
  394.  
  395. struct MenuItem Menu3[1] = {
  396.                              /* 1st item */
  397.                              NULL, 0, 0, WID2, 8,
  398.                              ITEMENABLED|HIGHCOMP|ITEMTEXT, 0, 0, 0, 0, 0, 0
  399.                            };
  400.  
  401. /* deploy menu titles */
  402.  
  403. UBYTE *Depstr[6] = { "C Command Post",
  404.                      "H Howitzer",
  405.                      "T Heavy Tank",
  406.                      "M Missile Tank",
  407.                      "G GEV",
  408.                      "I Infantry"
  409.                    };
  410.  
  411. /* target menu titles */
  412.  
  413. UBYTE *Tarstr[7] = { "t Treads",
  414.                      "b Main Battery",
  415.                      "s Secondary Batt",
  416.                      "m Missile",
  417.                      "a Anti-Personnel",
  418.                      "p Pass now",
  419.                      "r Resolve now"
  420.                     };
  421.  
  422. /* help menu title */
  423.  
  424. UBYTE *Helpstr = "? Instructions";
  425.  
  426. /* Menu strip titles */
  427.  
  428. UBYTE *Titles[] = { "Deploy", "Target", "Help" };
  429.  
  430. /* Menu item text message descriptors */
  431.  
  432. struct IntuiText MenuText[14] = {3, 0, JAM1, 0, 0, &MyFont, 0, 0,
  433.                                  3, 0, JAM1, 0, 0, &MyFont, 0, 0,
  434.                                  3, 0, JAM1, 0, 0, &MyFont, 0, 0,
  435.                                  3, 0, JAM1, 0, 0, &MyFont, 0, 0,
  436.                                  3, 0, JAM1, 0, 0, &MyFont, 0, 0,
  437.                                  3, 0, JAM1, 0, 0, &MyFont, 0, 0,
  438.                                  3, 0, JAM1, 0, 0, &MyFont, 0, 0,
  439.                                  3, 0, JAM1, 0, 0, &MyFont, 0, 0,
  440.                                  3, 0, JAM1, 0, 0, &MyFont, 0, 0,
  441.                                  3, 0, JAM1, 0, 0, &MyFont, 0, 0,
  442.                                  3, 0, JAM1, 0, 0, &MyFont, 0, 0,
  443.                                  3, 0, JAM1, 0, 0, &MyFont, 0, 0,
  444.                                  3, 0, JAM1, 0, 0, &MyFont, 0, 0,
  445.                                  3, 0, JAM1, 0, 0, &MyFont, 0, 0 };
  446.  
  447. /* These are the command characters associated with menu selections */
  448.  
  449. char Mentbl[3][7] =
  450. {               {'C', 'H', 'T', 'M', 'G', 'I', '\0'},  /* deploy cmds */
  451.                 {'t', 'b', 's', 'm', 'a', 'p', 'r'},   /* target cmds */
  452.                 {'?', '\0', '\0', '\0', '\0', '\0', '\0'} /* help */
  453. };
  454.  
  455. /***********************************************************************/
  456. /*                                                                     */
  457. /*                         Requester Stuff                             */
  458. /*                                                                     */
  459. /***********************************************************************/
  460.  
  461.  
  462. char BodyText[] = "Choose an Ogre type";
  463. char PosText[] = "Mark III";
  464. char NegText[] = "Mark V";
  465.  
  466. struct IntuiText Body =
  467. {
  468.    3, 1, JAM1, 60, 10, NULL, &BodyText[0], NULL
  469. };
  470.  
  471. struct IntuiText Pos =
  472. {
  473.    3, 1, JAM1, 7, 4, NULL, &PosText[0], NULL
  474. };
  475.  
  476. struct IntuiText Neg =
  477. {
  478.    3, 1, JAM1, 7, 4, NULL, &NegText[0], NULL
  479. };
  480.  
  481.  
  482. /***********************************************************************/
  483. /*                                                                     */
  484. /*                            Toys Code                                */
  485. /*                                                                     */
  486. /***********************************************************************/
  487.  
  488.  
  489. /* getevent pulls a message off the program's message queue and checks if
  490.  * it's a menu-related or gadget-related message.  If it is, the appropriate
  491.  * handler is called.
  492.  */
  493.  
  494. char getevent()
  495. {
  496.    char GadgetServe(), MenuServe(), result;
  497.    struct IntuiMessage  *GetMsg(), Tmpmsg;
  498.  
  499.    /* check queue for incoming messages */
  500.  
  501.    Imsg = GetMsg(w->UserPort);
  502.    if (Imsg == NULL)
  503.       return('\0');
  504.    else
  505.       Tmpmsg = *Imsg;
  506.    ReplyMsg(Imsg);
  507.  
  508.    /* if message available, decode it */
  509.  
  510.    if (Tmpmsg.Class != 0)
  511.    {
  512.       switch (Tmpmsg.Class)
  513.       {
  514.          /* could be a close window event */
  515.  
  516.          case CLOSEWINDOW:
  517.             endit();
  518.             break;
  519.  
  520.       /* or a menu selection */
  521.  
  522.          case MENUPICK:
  523.             result = MenuServe(&Tmpmsg);
  524.             return(result);
  525.             break;
  526.  
  527.          case GADGETUP:
  528.             result = GadgetServe(&Tmpmsg);
  529.             return (result);
  530.             break;
  531.  
  532.       /* if no selection was made, return a null */
  533.  
  534.          default:
  535.             return('\0');
  536.             break;
  537.       }
  538.    }
  539. }     /* checkmenu */
  540.  
  541. /* This routine invokes the requester that asks for Ogre type
  542.  */
  543.  
  544. int get_mark()
  545. {
  546.    int x;
  547.  
  548.    x = AutoRequest(w, &Body, &Pos, &Neg, 0L, 0L, 300L, 60L);
  549.    if (x)
  550.       x = 3;
  551.    else
  552.       x = 5;
  553.    return(x);
  554. }
  555.  
  556. /* Initmenu sets up the menus at the start of the program.
  557.  * Nobody said this would be easy.
  558.  */
  559.  
  560. InitMenu()
  561. {
  562.    int i;
  563.  
  564.    /* link the three menus */
  565.  
  566.    mymenu[0].NextMenu = &mymenu[1];
  567.    mymenu[1].NextMenu = &mymenu[2];
  568.    mymenu[2].NextMenu = NULL;
  569.  
  570.    /* point to first items on the menus */
  571.  
  572.    mymenu[0].FirstItem = &Menu1[0];
  573.    mymenu[1].FirstItem = &Menu2[0];
  574.    mymenu[2].FirstItem = &Menu3[0];
  575.  
  576.    /* initialize titles */
  577.  
  578.    mymenu[0].MenuName = Titles[0];
  579.    mymenu[1].MenuName = Titles[1];
  580.    mymenu[2].MenuName = Titles[2];
  581.  
  582.    /* link menu items */
  583.  
  584.    for (i = 0; i < 6; ++i)
  585.       Menu1[i].NextItem = &Menu1[i + 1];
  586.    Menu1[5].NextItem = NULL;
  587.  
  588.    for (i = 0; i < 7; ++i)
  589.       Menu2[i].NextItem = &Menu2[i + 1];
  590.    Menu2[6].NextItem = NULL;
  591.  
  592.    Menu3[0].NextItem = NULL;
  593.  
  594.    /* link item names to text descriptors */
  595.  
  596.    for (i = 0; i < 6; ++i)
  597.       MenuText[i].IText = Depstr[i];
  598.    for (i = 0; i < 7; ++i)
  599.       MenuText[6 + i].IText = Tarstr[i];
  600.    MenuText[13].IText = Helpstr;
  601.  
  602.    /* link text descriptors to item structures */
  603.  
  604.    for (i = 0; i < 6; ++i)
  605.       Menu1[i].ItemFill = &MenuText[i];
  606.    for (i = 0; i < 7; ++i)
  607.       Menu2[i].ItemFill = &MenuText[6 + i];
  608.    Menu3[0].ItemFill = &MenuText[13];
  609.  
  610.    SetMenuStrip(w, &mymenu[0]);
  611. }
  612.  
  613. /*  InitGad sets up the gadgets before the game starts
  614.  */
  615.  
  616. InitGad()
  617. {
  618.    int i;
  619.  
  620.    /* link Image structures to Gadget structures */
  621.  
  622.    for (i = 0; i < 6; ++i)
  623.       Arrow[i].GadgetRender = (APTR) &Gadgpic[i];
  624.  
  625.    /* link image data to Image structures */
  626.  
  627.    Gadgpic[0].ImageData = &Imgadget0[0];
  628.    Gadgpic[1].ImageData = &Imgadget1[0];
  629.    Gadgpic[2].ImageData = &Imgadget2[0];
  630.    Gadgpic[3].ImageData = &Imgadget3[0];
  631.    Gadgpic[4].ImageData = &Imgadget4[0];
  632.    Gadgpic[5].ImageData = &Imgadget5[0];
  633.    Gadgpic[6].ImageData = &Imgadget6[0];
  634.  
  635.    Arrow[6].NextGadget = NULL;
  636.  
  637. }
  638.  
  639.  
  640. refresh()
  641. {
  642.    int i;
  643.  
  644.    RefreshGadgets(&Arrow[0], w, NULL);
  645.  
  646.    /* Add the gadgets to the window's gadget list and activate them */
  647.    for (i = 0; i < 7; ++i)
  648.    {
  649.       AddGadget(w, &Arrow[i], -1);
  650.       OnGadget(&Arrow[i], w, NULL);
  651.    }
  652. }
  653.  
  654. /* MenuServe decodes menu request messages and translates them into the
  655.  * proper character to hand to main program.
  656.  */
  657.  
  658. char MenuServe(Menmsg)
  659. struct IntuiMessage *Menmsg;
  660. {
  661.    USHORT menusel, itemsel;      /* menu and item selected */
  662.    char selection;
  663.  
  664.    /* extract the menu and item numbers */
  665.  
  666.    menusel = MENUNUM(Menmsg->Code);
  667.    itemsel = ITEMNUM(Menmsg->Code);
  668.    if (menusel == MENUNULL)
  669.       return(NULL);
  670.  
  671.    /* get the command character from the table */
  672.  
  673.    selection = Mentbl[menusel][itemsel];
  674.    return(selection);
  675. }
  676.  
  677. /* GadgetServe translates a gadget selection into a command character
  678.  * and returns that character.
  679.  */
  680.  
  681. char GadgetServe(GadMsg)
  682. struct IntuiMessage *GadMsg;
  683. {
  684.    int i;
  685.  
  686.    for (i = 0; i < 7; ++i)
  687.       if (GadMsg->IAddress == (APTR) &Arrow[i])
  688.          break;
  689.    return (Gadtbl[i]);
  690. }
  691.  
  692. /*
  693.  * These are the calls known to the rest of the Ogre program
  694.  */
  695.  
  696. tc_setup()
  697. {
  698.    make_console();
  699. }
  700.  
  701. /* This is not a true erase to end of line routine, since we want
  702.  * to preserve the gadget display.  It erases to column 55.
  703.  */
  704.  
  705. eeol(row)
  706. int row;
  707. {
  708.    static char s[55] =
  709.    {"                                                      "};
  710.  
  711.    movecur(row,0);
  712.    Amiga_puts(s);
  713.    movecur(row,0);
  714. }
  715.  
  716. clear_screen()
  717. {
  718.    static char ff = 0x0c;
  719.  
  720.    ConPutChar(WriteMsg,&ff);
  721. }
  722.  
  723. /*
  724.  *    Move the cursor to row, col by building a CSI sequence
  725.  */
  726.  
  727. movecur(row, col)
  728. int row, col;
  729. {
  730.    char seq[8];
  731.    int n,i;
  732.  
  733.    row += 1;  /* Kludge city! The Amiga display is shorter than a terminal */
  734.  
  735.    /* CSI character */
  736.  
  737.    seq[0] = 0x9b;
  738.  
  739.    /* convert row to ASCII */
  740.  
  741.    n = stci_d(&seq[1], row, 4);
  742.    i = n + 1;
  743.  
  744.    /* field separator */
  745.  
  746.    seq[i] = 0x3b;
  747.    i++;
  748.  
  749.    /* convert column to ASCII */
  750.  
  751.    n = stci_d(&seq[i], col, 4);
  752.    i += n;
  753.  
  754.    /* terminator */
  755.  
  756.    seq[i] = 0x48;
  757.    seq[i+1] = '\0';
  758.    ConPutStr(WriteMsg, &seq[0]);
  759. }
  760.  
  761. set_term()
  762. {
  763.    cursoff(WriteMsg);
  764. }
  765.  
  766. reset_term()
  767. {
  768.    endit();
  769. }
  770.  
  771. /*
  772.  * These are mine. They insulate the rest of the files from knowing anything
  773.  * about message ports and other messy stuff.
  774.  */
  775.  
  776. Amiga_puts(s)
  777. char *s;
  778. {
  779.    ConPutStr(WriteMsg, s);
  780. }
  781.  
  782. /* Put a single char out.  This is weird: take away the static, and it no
  783.  * longer works!
  784.  */
  785.  
  786. Amiga_putchar(c)
  787. char c;
  788. {
  789.    static char x;
  790.  
  791.    x = c;
  792.    ConPutChar(WriteMsg, &x);
  793. }
  794.  
  795. /*    Get a character from the keyboard or a close window event from the
  796.  *    mouse. A close window event ends the program.
  797.  *    If you want to add menus then this is the place to do it.
  798.  */
  799.  
  800. char Amiga_getchar()
  801. {
  802.    static char c;
  803.    BYTE MayGetChar();
  804.    struct IntuiMessage *GetMsg();
  805.    char checkmenu(), event;
  806.    BYTE ret;
  807.  
  808.    FOREVER
  809.    {
  810.       ret = 0;
  811.  
  812.       /* first, check for keyboard input */
  813.  
  814.       ret = MayGetChar(ReadPort, ReadMsg, &c);
  815.       if (ret != -1)
  816.       {
  817.          /* Trap help requests here, so that help can be obtained
  818.           * from anywhere in the program.
  819.           */
  820.  
  821.          if (ret == HELPKEY)
  822.             ret = get_help();
  823.          else
  824.             return((char) ret);
  825.       }
  826.  
  827.       /* if no keyboard chars are ready, check our message port for
  828.        * menus and gadgets.
  829.        */
  830.  
  831.       if ((event = getevent()) != '\0')
  832.       {
  833.          if (event == HELPKEY)
  834.             ret = get_help();
  835.          else
  836.             return(event);
  837.       }
  838.    }
  839. }
  840.  
  841. endit()
  842. {
  843.    err(7);
  844.    exit(0);
  845. }
  846.  
  847. /*
  848.  * This stuff is from the ROM Kernel Manual
  849.  */
  850. make_console()
  851. {
  852.    int error;
  853.    UBYTE c;
  854.  
  855.    if ((DosBase = OpenLibrary("dos.library", 0)) == NULL)
  856.       err(1);
  857.    if ((IntuitionBase = OpenLibrary("intuition.library", 0)) == NULL)
  858.       err(2);
  859.    if ((GfxBase = OpenLibrary("graphics.library", 0)) == NULL)
  860.       err(3);
  861.  
  862.    WritePort = CreatePort("con1.write", 0);
  863.    if (WritePort == 0)
  864.       err(4);
  865.    WriteMsg = CreateStdIO(WritePort);
  866.    if (WriteMsg == 0)
  867.       err(19);
  868.  
  869.    ReadPort = CreatePort("con1.read", 0);
  870.    if (ReadPort == 0)
  871.       err(5);
  872.  
  873.    ReadMsg = CreateStdIO(ReadPort);
  874.    if (ReadMsg == 0)
  875.       err(12);
  876.  
  877.    scrn = (struct Screen *) OpenScreen(&ns);
  878.    if (scrn == NULL)
  879.    {
  880.       printf("Can't open window");
  881.       exit(20);
  882.    }
  883.  
  884.    nw.Screen = scrn;
  885.  
  886.    w = (struct Window *) OpenWindow(&nw);
  887.    if (w == NULL)
  888.       err(6);
  889.  
  890.    /* link gadgets into window */
  891.  
  892.    InitGad();
  893.  
  894.    /* link menus into window */
  895.  
  896.    InitMenu();
  897.  
  898.    error = OpenConsole(WriteMsg, ReadMsg, w);
  899.    if (error != 0)
  900.       err(7);
  901.  
  902.    QueueRead(ReadMsg, &c);
  903.  
  904.    c = CheckIO(ReadMsg);
  905.    if (c == FALSE) AbortIO(ReadMsg);
  906.    else GetMsg(ReadPort);
  907.  
  908. }
  909.  
  910. /*
  911.  * This routine is ugly. Please fix it.
  912.  */
  913.  
  914. static err(n)
  915. int n;
  916. {
  917.    switch (n)
  918.    {
  919.       case 7: CloseDevice(WriteMsg);
  920.       case 6: CloseWindow(w);
  921.       case 10: CloseScreen(scrn);
  922.       case 5: DeletePort(ReadPort);
  923.       case 12: DeleteStdIO(ReadMsg);
  924.       case 4: DeletePort(WritePort);
  925.       case 19: DeleteStdIO(WriteMsg);
  926.       case 3: CloseLibrary(GfxBase);
  927.       case 2: CloseLibrary(IntuitionBase);
  928.       case 1: CloseLibrary(DosBase);
  929.    }
  930.    exit(n);                                  /* return error code to CLI */
  931. }
  932.  
  933.  
  934. OpenConsole(wreq, rreq, win)
  935. struct IOStdReq *wreq, *rreq;
  936. struct Window *win;
  937. {
  938.    int error;
  939.    wreq->io_Data = (APTR) win;
  940.    wreq->io_Length = sizeof(*win);
  941.    error = OpenDevice("console.device", 0, wreq, 0);
  942.    rreq->io_Device = wreq->io_Device;
  943.    rreq->io_Unit = wreq->io_Unit;
  944.    return(error);
  945. }
  946.  
  947.  
  948. ConPutChar(request, ch)
  949. struct IOStdReq *request;
  950. char *ch;
  951. {
  952.    request->io_Command = CMD_WRITE;
  953.    request->io_Data = ch;
  954.    request->io_Length = 1;
  955.    DoIO(request);
  956.    return(0);
  957. }
  958.  
  959.  
  960. ConWrite(request, string, len)
  961. struct IOStdReq *request;
  962. char *string;
  963. int len;
  964. {
  965.    request->io_Command = CMD_WRITE;
  966.    request->io_Data = (APTR) string;
  967.    request->io_Length = len;
  968.    DoIO(request);
  969.    return(0);
  970. }
  971.  
  972.  
  973. ConPutStr(request, string)
  974. struct IOStdReq *request;
  975. char *string;
  976. {
  977.    request->io_Command = CMD_WRITE;
  978.    request->io_Data = (APTR) string;
  979.    request->io_Length = -1;
  980.    DoIO(request);
  981.    return(0);
  982. }
  983.  
  984.  
  985. QueueRead(request, buf)
  986. struct IOStdReq *request;
  987. UBYTE *buf;
  988. {
  989.    request->io_Command = CMD_READ;
  990.    request->io_Data = (APTR)buf;
  991.    request->io_Length = 1;
  992.    SendIO(request);
  993.    return(0);
  994. }
  995.  
  996.  
  997. char ConGetChar(console, request, buf)
  998. struct IOStdReq *request;
  999. struct MsgPort *console;
  1000. char *buf;
  1001. {
  1002.    char temp;
  1003.  
  1004.    while ((GetMsg(console) == NULL)) WaitPort(console);
  1005.    QueueRead(request, buf);
  1006.    temp = *buf;
  1007.    return(temp);
  1008. }
  1009.  
  1010. BYTE MayGetChar(console,rreq, buf)
  1011. struct IOStdReq *rreq;
  1012. struct MsgPort *console;
  1013. char *buf;
  1014. {
  1015.    register BYTE *temp;
  1016.  
  1017.    if (GetMsg(console) == NULL) return(-1);
  1018.    temp = (char *) rreq->io_Data;
  1019.    QueueRead(rreq, buf);
  1020.    return(*temp);
  1021. }
  1022.