home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 1.ddi / MOUSE.ZIP / ESM.C next >
Encoding:
C/C++ Source or Header  |  1990-07-11  |  13.1 KB  |  570 lines

  1. /*
  2.  *    esm.c
  3.  *
  4.  *    This program plots simulated ESM (Electronic Support Measures)
  5.  *    reports on IBM EGA/VGA Graphic Adaptors.
  6.  *
  7.  *    Public Domain (p) July 1990, S. R. Sampson
  8.  */
  9.  
  10. /* Includes */
  11.  
  12. #include <stdio.h>
  13. #include <stdarg.h>
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include <conio.h>
  17. #include <alloc.h>
  18. #include <dos.h>
  19. #include <graphics.h>
  20. #include <mouse.h>
  21.  
  22. /* Defines */
  23.  
  24. typedef unsigned char uchar;
  25. typedef unsigned int uint;
  26.  
  27. #define PI      (3.141593F)
  28. #define RADIAN  (180.0F / PI)           /* One radian                        */
  29. #define TWOPI   (2.0F * PI)             /* Two Pi alias 360 Degrees          */
  30. #define TWO     (2.0F / RADIAN)         /* 2 Degrees in radians              */
  31. #define TEN     (10.0F / RADIAN)        /* 10 degrees in radians             */
  32. #define    ASPECT    (0.775F)        /* Screen aspect ratio             */
  33.  
  34. #define F1        315        /* Hook track with mouse cursor         */
  35. #define F2        316        /* Erase track info             */
  36. #define F7        321        /* Toggle mouse cursor off/on         */
  37. #define F10        324        /* Return to OS                 */
  38.  
  39. /* not used yet */
  40.  
  41. #define ESC        27
  42. #define HOMEKEY        327
  43. #define ENDKEY        335
  44. #define UPKEY        328
  45. #define DOWNKEY     336
  46. #define PGUPKEY     329
  47. #define PGDNKEY     337
  48. #define LEFTKEY     331
  49. #define RIGHTKEY    333
  50. #define INSKEY        338
  51. #define DELKEY        339
  52. #define CTRLLEFTKEY    371
  53. #define CTRLRIGHTKEY    372
  54. #define F3        317
  55. #define F4        318
  56. #define F5        319
  57. #define F6        320
  58. #define F8        322
  59. #define F9        323
  60.  
  61. /*
  62.  *    A Target Detect
  63.  */
  64.  
  65. typedef struct    {
  66.     uchar    amp;
  67.     uchar    bearing;
  68.     uchar    range;
  69.     uchar    leader;
  70.     uint    speed;
  71.     char    line1[9];
  72.     char    line2[9];
  73. } TRACK;
  74.  
  75. #define    EraseTrack(x, y)    PlotTrack(x, y)
  76.  
  77. /* Globals */
  78.  
  79. M_POS    *Mptr;
  80. void    *Image;                /* track symbol            */
  81. int    GraphDriver = DETECT;        /* graphics device driver       */
  82. int    GraphMode;            /* graphics mode value            */
  83. int    MaxX, MaxY;            /* maximum resolution of screen */
  84. int    CenterX, CenterY;        /* center of screen        */
  85. int    Sw1, Sw2, Sw4, Sw5;        /* Five Bit Sliding window    */
  86. uchar    CanTgtFile[256];        /* Detections each pass        */
  87. float    Aspect;                /* Screen aspect ratio        */
  88.  
  89. TRACK    FwdTgts1[256];            /* First pass detects        */
  90. TRACK    FwdTgts2[256];            /* Second pass detects        */
  91. TRACK    *ThisPass = &FwdTgts1[0];
  92. TRACK    *LastPass = &FwdTgts2[0];
  93.  
  94. int    Pos1 = 72;    /* Moving test target window */
  95. int    Pos2 = 73;
  96. int    Pos3 = 74;
  97. int    Pos4 = 75;
  98. int    Pos5 = 76;
  99.  
  100. /*
  101.  *    This is a square mouse cursor with a pointer dot in the center
  102.  */
  103.  
  104. uint Cursor[] = {
  105.     0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xF00F, 0xF7EF, 0xF7EF, 0xF66F,
  106.     0xF7EF, 0xF7EF, 0xF00F, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
  107.     0x0000, 0x0000, 0x0000, 0x0000, 0x0FF0, 0x0810, 0x0810, 0x0990,
  108.     0x0810, 0x0810, 0x0FF0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  109. };
  110.  
  111. /*
  112.  *    This program was designed to work with EGA Graphics.
  113.  *    VGA is supported only in an EGA compatible mode.
  114.  *    EGA = 0.775 Aspect Ratio
  115.  *
  116.  *    8-Bit Sine Degree Table for n = 0 to 255
  117.  *
  118.  *    Sine[n] = sin(x) * 127, x = x + (360 / 256)
  119.  */
  120.  
  121. char    Sine[] = {
  122.     0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45,
  123.     48, 51, 54, 57, 59, 62, 65, 67,    70, 73, 75, 78, 80, 82, 85, 87,
  124.     89, 91, 94, 96, 98, 100, 102, 103, 105, 107, 108, 110, 112, 113,
  125.     114, 116, 117, 118, 119, 120, 121, 122, 123, 123, 124, 125, 125,
  126.     126, 126, 126, 126, 126, 127, 126, 126, 126, 126, 126, 125, 125,
  127.     124, 123, 123, 122, 121, 120, 119, 118, 117, 116, 114, 113, 112,
  128.     110, 108, 107,    105, 103, 102, 100, 98, 96, 94, 91, 89, 87, 85,
  129.     82, 80, 78, 75, 73, 70, 67, 65, 62, 59, 57, 54, 51, 48, 45, 42,
  130.     39, 36, 33, 30, 27, 24, 21, 18, 15, 12, 9, 6, 3, 0, -3, -6, -9,
  131.     -12, -15, -18, -21, -24, -27, -30, -33, -36, -39, -42, -45, -48,
  132.     -51, -54, -57, -59, -62, -65, -67, -70, -73, -75, -78, -80, -82,
  133.     -85, -87, -89, -91, -94, -96, -98, -100, -102, -103, -105, -107,
  134.     -108, -110, -112, -113, -114, -116, -117, -118, -119, -120, -121,
  135.     -122, -123, -123, -124, -125, -125, -126, -126, -126, -126, -126,
  136.     -127, -126, -126, -126, -126, -126, -125, -125,    -124, -123, -123,
  137.     -122, -121, -120, -119, -118, -117, -116, -114, -113, -112, -110,
  138.     -108, -107, -105, -103, -102, -100, -98, -96, -94, -91, -89, -87,
  139.     -85, -82, -80, -78, -75, -73, -70, -67, -65, -62, -59, -57, -54,
  140.     -51, -48, -45, -42, -39, -36, -33, -30, -27, -24, -21, -18, -15,
  141.     -12, -9, -6, -3
  142. };
  143.  
  144. /*
  145.  *    8-Bit Cosine Degree Table for n = 0 to 255
  146.  *
  147.  *    Cosine[n] = cos(x) * Aspect * 127, x = x + (360 / 256)
  148.  */
  149.  
  150. char    Cosine[] = {
  151.     98, 98, 98, 98, 97, 97, 97, 96, 96, 96, 95, 94, 94, 93, 92, 91,
  152.     90, 89, 88, 87, 86, 85, 84, 83, 81, 80, 79, 77, 76, 74, 72, 71,
  153.     69, 67, 66, 64, 62, 60, 58, 56, 54, 52, 50, 48, 46, 44, 42, 39,
  154.     37, 35, 33, 30, 28, 26, 23, 21, 19, 16, 14, 12, 9, 7, 4, 2,
  155.     0, -2, -4, -7, -9, -12, -14, -16, -19, -21, -23, -26, -28, -30,
  156.     -33, -35, -37, -39, -42, -44, -46, -48, -50, -52, -54, -56, -58,
  157.     -60, -62, -64, -66, -67, -69, -71, -72, -74, -76, -77, -79, -80,
  158.     -81, -83, -84, -85, -86, -87, -88, -89, -90, -91, -92, -93, -94,
  159.     -94, -95, -96, -96, -96, -97, -97, -97, -98, -98, -98, -98, -98,
  160.     -98, -98, -97, -97, -97, -96, -96, -96, -95, -94, -94, -93, -92,
  161.     -91, -90, -89, -88, -87, -86, -85, -84, -83, -81, -80, -79, -77,
  162.     -76, -74, -72, -71, -69, -67, -66, -64, -62, -60, -58, -56, -54,
  163.     -52, -50, -48, -46, -44, -42, -39, -37, -35, -33, -30, -28, -26,
  164.     -23, -21, -19, -16, -14, -12, -9, -7, -4, -2, 0, 2, 4, 7, 9, 12,
  165.     14, 16, 19, 21, 23, 26, 28, 30, 33, 35, 37, 39, 42, 44, 46, 48,
  166.     50, 52, 54, 56, 58, 60, 62, 64, 66, 67, 69, 71, 72, 74, 76, 77,
  167.     79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94,
  168.     95, 96, 96, 96, 97, 97, 97, 98, 98, 98
  169. };
  170.  
  171. /* Function Prototypes */
  172.  
  173. void    InitGraphics(void), GetCommand(void), GenTarget(void);
  174. void    ScanForTgt(void), convert(int, int, int *, int *), quit(void);
  175. void    PlotTrack(int, int), LoadCandidate(void);
  176. int    getkey(void);
  177.  
  178. /* Main Program */
  179.  
  180. void main()
  181. {
  182.     int    i;
  183.  
  184.     delay(10);    /* need to prime delay to work properly */
  185.  
  186.     for (i = 0; i < 256; i++)
  187.         FwdTgts1[i].amp = FwdTgts2[i].amp = 0;
  188.  
  189.     if ((i = m_init()) == 0)  {
  190.         fprintf(stderr, "Fatal - Mouse driver not installed\n");
  191.         exit(1);
  192.     }
  193.  
  194.     if (i == 2)  {
  195.         fprintf(stderr, "Fatal - You need DOS 2.0 or greater\n");
  196.         exit(1);
  197.     }
  198.  
  199.     InitGraphics();
  200.     m_graph_cursor(7, 7, (void far *)Cursor);
  201.     m_display(ON);
  202.  
  203.     for (;;)  {
  204.         LoadCandidate();
  205.         ScanForTgt();
  206.         GenTarget();
  207.  
  208.         if (kbhit())
  209.             GetCommand();
  210.     }
  211. }
  212.  
  213. /* Functions */
  214.  
  215. /*
  216.  *    Get the keyboard command entered
  217.  */
  218.  
  219. void GetCommand()
  220. {
  221.     int    x, y;
  222.     static char str1[16], str2[16];
  223.  
  224.     switch (getkey())  {        /* find out which key was pressed */
  225.     case F1:
  226.         Mptr = m_status();
  227.         outtextxy(550, 20, itoa(Mptr->curx, str1, 10));
  228.         outtextxy(600, 20, itoa(Mptr->cury, str2, 10));
  229.         break;
  230.     case F2:
  231.         setcolor(EGA_BLUE);
  232.         outtextxy(550, 20, str1);
  233.         outtextxy(600, 20, str2);
  234.         setcolor(EGA_LIGHTGRAY);
  235.         break;
  236.     case F7:
  237.         Mptr = m_status();
  238.         if (Mptr->display == ON)
  239.             m_display(OFF);
  240.         else
  241.             m_display(ON);
  242.         break;
  243.     case F10:
  244.         quit();
  245.     }
  246. }
  247.  
  248. int getkey()
  249. {
  250.     int    lo, hi, key;
  251.  
  252.     key = bioskey(0);
  253.     lo  = key & 0x00FF;
  254.     hi  = (key & 0xFF00) >> 8;
  255.  
  256.     return ((lo == 0) ? hi + 256 : lo);
  257. }
  258.  
  259. /*
  260.  *    Free all data buffers and return to DOS
  261.  */
  262.  
  263. void quit()
  264. {
  265.     free(Image);
  266.     closegraph();
  267.     m_display(OFF);
  268.  
  269.     sound(500);
  270.     delay(125);
  271.     nosound();
  272.  
  273.     exit(0);
  274. }
  275.  
  276. /*
  277.  *    Input test data into the candidate target array
  278.  */
  279.  
  280. void LoadCandidate()
  281. {
  282.     int    i;
  283.  
  284.     for (i = 0; i < 256; ++i)
  285.         CanTgtFile[i] = 0;
  286.  
  287.     Pos1 = ++Pos1 & 0xFF;
  288.     Pos2 = ++Pos2 & 0xFF;
  289.     Pos3 = ++Pos3 & 0xFF;
  290.     Pos4 = ++Pos4 & 0xFF;
  291.     Pos5 = ++Pos5 & 0xFF;
  292.  
  293.     CanTgtFile[Pos3] = 128;    /*  Moving Test Target */
  294.     CanTgtFile[Pos1] = 63;
  295.     CanTgtFile[Pos2] = 32;
  296.     CanTgtFile[Pos4] = 32;
  297.     CanTgtFile[Pos5] = 63;
  298.  
  299.     CanTgtFile[200] = 3;    /* NW Test Target */
  300.     CanTgtFile[201] = 5;
  301.     CanTgtFile[202] = 9;
  302.     CanTgtFile[203] = 5;
  303.     CanTgtFile[204] = 3;
  304.  
  305.     CanTgtFile[148] = 32;    /* SW Test Target */
  306.     CanTgtFile[149] = 50;
  307.     CanTgtFile[150] = 84;
  308.     CanTgtFile[151] = 50;
  309.     CanTgtFile[152] = 32;
  310.  
  311.     CanTgtFile[254] = 32;    /* N Test Target */
  312.     CanTgtFile[255] = 50;
  313.     CanTgtFile[000] = 192;
  314.     CanTgtFile[001] = 50;
  315.     CanTgtFile[002] = 32;
  316. }
  317.  
  318. /*
  319.  *    Run the Sliding Window (sw) through the data
  320.  *    and produce peak detects
  321.  */
  322.  
  323. void ScanForTgt()
  324. {
  325.     int    i, sw3;
  326.  
  327.     Sw1 = 254,    /* Sliding window */
  328.     Sw2 = 255,
  329.     Sw4 = 001,
  330.     Sw5 = 002;
  331.  
  332.     for (i = 0; i < 256; ++i)  {
  333.  
  334.         /* threshold is twice the average of surrounding signal */
  335.  
  336.         sw3  = CanTgtFile[Sw1];
  337.         sw3 += CanTgtFile[Sw2];
  338.         sw3 += CanTgtFile[Sw4];
  339.         sw3 += CanTgtFile[Sw5];
  340.         sw3 >>= 2;        /* divide by 4 (truncate) */
  341.         sw3 <<= 1;        /* multiply by 2 */
  342.         sw3 &= 0xFF;        /* normalize to range 0 - 255 */
  343.  
  344.         if (CanTgtFile[i] > sw3)
  345.             ThisPass[i].amp = (CanTgtFile[i] * CenterY) >> 8;
  346.         else
  347.             ThisPass[i].amp = 0;
  348.  
  349.         Sw1 = ++Sw1 & 0xFF;
  350.         Sw2 = ++Sw2 & 0xFF;
  351.         Sw4 = ++Sw4 & 0xFF;
  352.         Sw5 = ++Sw5 & 0xFF;
  353.     }
  354. }
  355.  
  356. void GenTarget()
  357. {
  358.     int    t1, t2, i, x, y;
  359.     TRACK    *tmp;
  360.  
  361.     for (i = 0; i < 256; ++i)  {
  362.         t1 = LastPass[i].amp;
  363.         t2 = ThisPass[i].amp;
  364.  
  365.         if (t2)  {
  366.  
  367.             /*
  368.              *    We have an amplitude on this pass
  369.              */
  370.  
  371.             if (t2 == t1)  {
  372.  
  373.                 /*
  374.                  *    Amplitude was already plotted
  375.                  */
  376.  
  377.                 continue;
  378.             } else if (t1)  {
  379.  
  380.                 /*
  381.                  *    Different amplitude was plotted
  382.                  *    on last pass, so erase old amplitude
  383.                  */
  384.  
  385.                 convert(i, CenterY-t1, &x, &y);
  386.                 EraseTrack(x, y);
  387.  
  388.                 /*
  389.                  *    And plot new amplitude
  390.                  */
  391.  
  392.                 convert(i, CenterY-t2, &x, &y);
  393.                 PlotTrack(x, y);
  394.             } else  {
  395.  
  396.                 /*
  397.                  *    No amplitudes plotted on last pass
  398.                  */
  399.  
  400.                 convert(i, CenterY-t2, &x, &y);
  401.                 PlotTrack(x, y);
  402.             }
  403.         }
  404.         else if (t1)  {
  405.  
  406.             /*
  407.              *    No amplitude on this pass but one on last
  408.              */
  409.  
  410.             convert(i, CenterY-t1, &x, &y);
  411.             EraseTrack(x, y);
  412.         }
  413.     }
  414.  
  415.     /*
  416.      *    Swap the data pointers
  417.      */
  418.  
  419.     tmp = ThisPass;
  420.     ThisPass = LastPass;
  421.     LastPass = tmp;
  422. }
  423.  
  424. /*
  425.  *    This is real slow and ugly yet.
  426.  */
  427.  
  428. void PlotTrack(int x, int y)
  429. {
  430.     Mptr = m_status();
  431.  
  432.     if (Mptr->display == ON)  {
  433.         m_display(OFF);
  434.         putimage(x-4, y-4, Image, XOR_PUT);
  435.         m_display(ON);
  436.     } else
  437.         putimage(x-4, y-4, Image, XOR_PUT);
  438. }
  439.  
  440. /*
  441.  *    Initialize the graphics system
  442.  */
  443.  
  444. void InitGraphics()
  445. {
  446.     uint    size;
  447.         float    bearing, t;
  448.         int    x, y;
  449.  
  450.     registerbgidriver(EGAVGA_driver);
  451.     initgraph(&GraphDriver, &GraphMode, "");
  452.  
  453.     if (GraphMode == EGAHI)
  454.         ;
  455.     else if (GraphMode == VGAHI)  {
  456.         GraphMode = VGAMED;
  457.         setgraphmode(VGAMED);
  458.     } else  {
  459.         fprintf(stderr, "Fatal - Program requires EGA or VGA.\n");
  460.         exit(1);
  461.     }
  462.  
  463.     /* Fill in global constants */
  464.  
  465.     MaxX     = getmaxx();        /* size of screen (future) */
  466.     MaxY     = getmaxy();
  467.         CenterX  = (MaxX / 2) - 100;    /* PPI center (moved left) */
  468.         CenterY  = MaxY / 2;
  469.     Aspect     = ASPECT;
  470.  
  471.     /*
  472.      *    Create Target Symbol
  473.      */
  474.  
  475.     setcolor(EGA_GREEN);
  476.     circle(23, 14, 3);
  477.         setfillstyle(SOLID_FILL, EGA_GREEN);
  478.         floodfill(23, 14, EGA_GREEN);
  479.  
  480.     setcolor(EGA_LIGHTGREEN);
  481.     rectangle(22, 13, 24, 15);
  482.  
  483.     size = imagesize(19, 11, 28, 27);
  484.     Image = malloc(size);
  485.  
  486.     getimage(19, 11, 28, 27, Image);
  487.     putimage(19, 11, Image, XOR_PUT);
  488.  
  489.     /*
  490.      *    Draw the border and scope
  491.      */
  492.  
  493.     cleardevice();            /* Clear graphics screen    */
  494.     setcolor(EGA_GREEN);
  495.  
  496.     setactivepage(1);
  497.     outtextxy(175, 175, "Please Wait While Configuring");
  498.     setvisualpage(1);
  499.     setactivepage(0);
  500.  
  501.     setcolor(EGA_LIGHTGRAY);
  502.     circle(CenterX, CenterY, CenterY);        /* inner ring     */
  503.     circle(CenterX, CenterY, CenterY + 10);        /* outer ring     */
  504.  
  505.         /*
  506.          *      Ten Degree Full size ticks
  507.          */
  508.  
  509.     t = (float)(CenterY + 10);
  510.         for (bearing = 0.0F; bearing < TWOPI; bearing += TEN)  {
  511.             moveto(CenterX, CenterY);
  512.             x = CenterX + (int)(t * sin(bearing));
  513.             y = CenterY - (int)(t * cos(bearing) * Aspect);
  514.             lineto(x, y);
  515.         }
  516.  
  517.         /*
  518.          *      Two Degree Half size ticks
  519.          */
  520.  
  521.     t = (float)(CenterY + 5);
  522.         for (bearing = 0.0F; bearing < TWOPI; bearing += TWO)  {
  523.              moveto(CenterX, CenterY);
  524.              x = CenterX + (int)(t * sin(bearing));
  525.              y = CenterY - (int)(t * cos(bearing) * Aspect);
  526.              lineto(x, y);
  527.     }
  528.  
  529.         /*
  530.          *      This routine erases all bearing lines except
  531.          *      those between the two circles.
  532.          */
  533.  
  534.         setcolor(WHITE);
  535.     circle(CenterX, CenterY, CenterY);        /* inner ring     */
  536.  
  537.         setfillstyle(SOLID_FILL, BLACK);
  538.         floodfill(CenterX, CenterY, WHITE);
  539.         setcolor(EGA_LIGHTGRAY);
  540.     circle(CenterX, CenterY, CenterY);        /* inner ring     */
  541.         outtextxy(CenterX, CenterY, "+");
  542.  
  543.     /*
  544.      *    Make a fancy escutcheon
  545.      */
  546.  
  547.     rectangle(0, 0, MaxX, MaxY);
  548.     line(440, 0, 440, 349);
  549.  
  550.     setfillstyle(INTERLEAVE_FILL, EGA_BLUE);
  551.     floodfill(10, 10, EGA_LIGHTGRAY);
  552.  
  553.     setfillstyle(SOLID_FILL, EGA_BLUE);
  554.     floodfill(450, 10, EGA_LIGHTGRAY);
  555.  
  556.     setvisualpage(0);
  557. }
  558.  
  559. void convert(int bearing, int amp, int *x, int *y)
  560. {
  561.     /*
  562.      *    Convert polar to rectangular
  563.      */
  564.  
  565.     *x = CenterX + ((amp *   Sine[bearing]) >> 7);
  566.     *y = CenterY - ((amp * Cosine[bearing]) >> 7);
  567. }
  568.  
  569. /*EOF*/
  570.