home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 1.ddi / EXAMPLE.EXE / CSCAPE / EXAMPLE / DEMOGRAF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-12  |  14.2 KB  |  663 lines

  1. /*
  2.     demograf.c
  3.  
  4.     Copyright (c) 1988, by Oakland Group, Inc.
  5.     ALL RIGHTS RESERVED.
  6.  
  7.     This program demonstrates how C-scape screens can be placed
  8.     on top of graphics images.
  9.  
  10.     This program relies on the compiler supplied graphics library
  11.     to draw the graphic images.
  12.  
  13.     The following graphics libraries are supported:
  14.  
  15.     Microsoft C5:        you must define M5 on the compile line.
  16.     Turbo C2:            Turbo C defines the Symbol __TURBOC__
  17.  
  18.     Note:    You must run the DOS utility GRAPTABL.COM
  19.             before running this program on a CGA.
  20.             GRAPTABL.COM loads the extended ASCII character
  21.             set for use in graphics mode.
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26.  
  27. #include <cscape.h>
  28. #include <popdecl.h>
  29. #include <framer.h>
  30.  
  31. #include <pcmode.h>
  32.  
  33. /*** Graphics Lib headers ***/
  34.  
  35. #ifdef M5                    /* Microsoft 5.x */
  36. #include <graph.h>
  37. #endif
  38.  
  39. #ifdef __TURBOC__            /* Turbo C 2.0 */
  40. #include <graphics.h>
  41.  
  42. /*     BGI_PATH is directory in which initgraph looks for the 
  43.     Borland .BGI files.  You may have to change this path 
  44.     depending on how you have installed your Turbo C compiler.
  45.     Consult the Turbo C manual under initgraph for more information.
  46.     Note that the backslashes in the path name must quoted
  47.     with a preceding backslash.
  48. */
  49.  
  50. #define BGI_PATH     "\\TC\\"
  51.  
  52. #endif
  53.  
  54. /*** data structure to hold information about the demo ***/
  55. typedef struct {
  56.  
  57.     int        shape;                /* current shape type    */
  58.     int     size;                /* current shape size    */
  59.     int     color;                /* current painting color */
  60.  
  61.     int        hgt;                /* display height in pixels */
  62.     int        wid;                /* display width in pixels     */
  63.     int        ncolors;            /* number of available colors */
  64.  
  65.     byte     reg;                /* colors used by menus and popups */
  66.     byte     sel;
  67.  
  68. } dgraf_struct;
  69.  
  70.  
  71. /*** Macros and definitions ***/
  72.  
  73. #define nrand(n)    (rand() % n)
  74. #define BLANK    998     /* positive for compat w/ cs31 */
  75. #define QUIT    999
  76. #define MIXED    999
  77.  
  78. /*** Function prototypes ***/
  79.  
  80. int     draw(dgraf_struct *);
  81.  
  82. int     u_erase(VOID *, int);        /* User functions connected to pulldown menu */
  83. int     u_shape(VOID *, int);
  84. int     u_size(VOID *, int);
  85. int     u_color(VOID *, int);
  86. int     u_horoscope(VOID *, int);
  87.  
  88. void     dgraf_Clear(void);
  89. void     dgraf_Ellipse(opbox *, int);
  90. void     dgraf_Rectangle(opbox *, int);
  91. void     dgraf_Line(opbox *, int);
  92. int        dgraf_Init(dgraf_struct *);
  93.  
  94. /*** The pulldown menu definition ***/
  95.  
  96. struct frame_def main_menu[] = {
  97.  
  98.     {    "Erase", u_erase,    BLANK},
  99.     { FRAME_END     },
  100.     {    "Figure", NULL,     BLANK},
  101.     {    " Mixed  ",    u_shape,     MIXED},
  102.     {    " Line   ", u_shape,     1},
  103.     {    " Circle ", u_shape,    2},
  104.     {    " Square ", u_shape,     3},
  105.     { FRAME_END     },
  106.     {    "Size", NULL,     BLANK},
  107.     {    " Mixed  ", u_size,     MIXED},
  108.     {    " Small  ", u_size,     1},
  109.     {    " Medium ", u_size,     2},
  110.     {    " Large  ", u_size,    3},
  111.     { FRAME_END     },
  112.     {    "Color", NULL,     BLANK},
  113.     {    " Mixed  ", u_color,     MIXED},
  114.     {    " Single ", u_color,     1},
  115.     { FRAME_END     },
  116.     {    "Horoscope",     NULL,    BLANK},
  117.     {    " Fast   ", u_horoscope,     5},
  118.     {    " Medium ", u_horoscope,     3},
  119.     {    " Slow   ", u_horoscope,     1},
  120.     { FRAME_END     },
  121.     {    "Quit",     NULL,    QUIT},
  122.     { FRAME_END     },
  123.     { FRAME_END     },
  124. };
  125.  
  126.  
  127. void main()
  128. {
  129.     dgraf_struct     dgraf;
  130.     sed_type         frame;
  131.     int             ret, key, x;
  132.  
  133.     /*  Initialize the C-scape device interface:
  134.         def_ModeGraphics selects best available graphics mode
  135.         grwin_Class saves graphic in the background window
  136.     */
  137.  
  138.     if (!disp_Init(def_ModeGraphics, grwin_Class)) {
  139.         printf("demograf: No graphics hardware found\n");
  140.         return;
  141.     }
  142.  
  143.     /* Initialize Graphics Library */
  144.     if ((x = dgraf_Init(&dgraf)) != 0) {
  145.         disp_Close();
  146. #ifdef __TURBOC__            /* Turbo C 2.0 */
  147.         printf("demograf: Turbo C initgraph error %d\n", x);
  148. #else
  149.         printf("demograf: unable to initialize graphics library.\n");
  150. #endif
  151.     }
  152.  
  153.     /* Create the pulldown menu */
  154.     frame = frame_Open(main_menu, bd_1, dgraf.reg, dgraf.sel, dgraf.reg);
  155.     frame_Repaint(frame);
  156.  
  157.     while (TRUE) {
  158.  
  159.         while (!kb_Check()) {
  160.             draw(&dgraf);
  161.         }
  162.  
  163.         /* key pressed, read it and call pulldown menu */
  164.         key = kb_Read();
  165.         if ((ret = frame_Go(frame, key, (VOID *) &dgraf )) == QUIT) {
  166.             /* Quit selected, break out of while loop */
  167.             break;
  168.         }
  169.         /* if first letter search of menu failed, try again */
  170.         else if (ret == 0) {
  171.             if (frame_Go(frame, ' ', (VOID *) &dgraf ) == QUIT) {
  172.                 /* Quit selected, break out of while loop */
  173.                 break;
  174.             }
  175.         }
  176.     }
  177.  
  178.     /* close down device interface */
  179.     disp_Close();
  180. }
  181.  
  182. int draw(dgrafp)
  183.     dgraf_struct *dgrafp;
  184. /*
  185.     Draws a shape using the current information
  186.     in the dgraf structure.
  187. */
  188. {
  189.     int shape, size, color;
  190.     opbox    box;
  191.  
  192.     shape = (dgrafp->shape == MIXED) ? nrand(3) + 1 : dgrafp->shape;
  193.     size  = (dgrafp->size  == MIXED) ? nrand(3) + 1: dgrafp->size;
  194.     color = (dgrafp->color == MIXED) ? nrand(dgrafp->ncolors) : dgrafp->color;
  195.  
  196.     box.ymin = nrand(dgrafp->hgt);
  197.     box.xmin = nrand(dgrafp->wid);
  198.     box.ymax = box.ymin + (dgrafp->hgt)/10 * size;
  199.     box.xmax = box.xmin + (dgrafp->wid)/10 * size;
  200.  
  201.     switch(shape) {
  202.     case 1:
  203.         dgraf_Line(&box, color);
  204.         break;
  205.     case 2:
  206.         dgraf_Ellipse(&box, color);
  207.         break;
  208.     case 3:
  209.         dgraf_Rectangle(&box, color);
  210.         break;
  211.     }
  212.  
  213.     return(TRUE);
  214. }
  215.  
  216. /*---------------------------------------------------------------------------*/
  217. /*    User functions: functions called from the pull down menu */
  218. /*  pdata is a pointer to the dgraf struct and is passed via frame_Go */
  219.  
  220. int u_erase(pdata, idata)
  221.     VOID *pdata;
  222.     int  idata;
  223. /*
  224.     Erase the display
  225. */
  226. {
  227.     dgraf_Clear();
  228.  
  229.     return(1);
  230. }
  231.  
  232. int u_shape(pdata, idata)
  233.     VOID *pdata;
  234.     int  idata;
  235. /*
  236.     Change the current shape to idata's value
  237.     idata is defined in the menu definition structure.
  238. */
  239. {
  240.     dgraf_struct *dgrafp;
  241.  
  242.     dgrafp = (dgraf_struct *) pdata;
  243.  
  244.     dgrafp->shape = idata;
  245.  
  246.     return(1);
  247. }
  248.  
  249. int u_size(pdata, idata)
  250.     VOID *pdata;
  251.     int  idata;
  252. /*
  253.     Change the current size to idata's value
  254.     idata is defined in the menu definition structure.
  255. */
  256. {
  257.     dgraf_struct *dgrafp;
  258.  
  259.     dgrafp = (dgraf_struct *)pdata;
  260.  
  261.     dgrafp->size = idata;
  262.  
  263.     return(1);
  264. }
  265.  
  266. int u_color(pdata, idata)
  267.     VOID *pdata;
  268.     int  idata;
  269. /*
  270.     Change the current color to idata's value
  271.     idata is defined in the menu definition structure.
  272. */
  273. {
  274.     dgraf_struct *dgrafp;
  275.  
  276.     dgrafp = (dgraf_struct *)pdata;
  277.  
  278.     dgrafp->color = idata;
  279.  
  280.     return(1);
  281. }
  282.  
  283. char *horoscope_msg[] = {
  284.   "AQUARIUS\nYou will find a substanstial amount of cash in your toothpaste.",
  285.   "PISCES\nYou will be the Elvis of the nineties.",
  286.   "ARIES\nA surprise gift of a lawn ornament is not out of the question.",
  287.   "TAURUS\nA dream about cows carries a clue to building a better life for yourself.",
  288.   "GEMINI\nAll your hard work will pay off; aluminum siding is yours for the asking.",
  289.   "CANCER\nA ringing pay phone holds your key to a world of fun.",
  290.   "LEO\nYou are too busy to waste time on horoscopes.",
  291.   "VIRGO\nA warped bowling alley will lead you to a major scientific discovery.",
  292.   "LIBRA\nYou will star in a major motion picture with Garrett Morris.",
  293.   "SCORPIO\nYou need a good change of pace.  Purchase some imported cheeses.",
  294.   "SAGITTARIUS\nGood times ahead, despite a boring horoscope.",
  295.   "CAPRICORN\nUnexpected company will be dropping in.  Be sure you bathroom is tidy."
  296. };
  297.  
  298. #define COUNT    (sizeof(horoscope_msg) / sizeof(char *))
  299.  
  300. int u_horoscope(pdata, idata)
  301.     VOID *pdata;
  302.     int  idata;
  303. /*
  304.     Slides a window across the display.
  305.     idata determines the speed of the horoscope.
  306.     idata comes from the framer definition structure.
  307. */
  308. {
  309.     sed_type     sed;
  310.     int             col;
  311.     dgraf_struct *dgrafp;
  312.  
  313.     dgrafp = (dgraf_struct *)pdata;
  314.  
  315.     /* Create a message window */
  316.     sed = pop_Text(horoscope_msg[nrand(COUNT)], 10, disp_GetWidth(), -1, 22, dgrafp->reg, bd_1);
  317.  
  318.     /* Slide the window across the display */
  319.     for (col = disp_GetWidth(); col > -20; col -= idata) {
  320.         sed_SetPosition(sed, 10, col);
  321.     }
  322.  
  323.     /* Destroy the window */
  324.     sed_Close(sed);
  325.  
  326.     return(TRUE);
  327. }
  328.  
  329.  
  330. /*---------------------------------------------------------------------------*/
  331. /*     Portable graphics routines:
  332.     The following routines are defined for the various graphics libraries
  333.     supported.
  334. */
  335.  
  336. #ifdef M5            /* Microsoft */
  337.  
  338. int dgraf_Init(dgrafp)
  339.     dgraf_struct *dgrafp;
  340. /*
  341.     Initialize the graphics library.
  342.     Initialize demo data.
  343.  
  344.     Returns 0 if successful.
  345.  
  346.     Microsoft version.
  347. */
  348. {
  349.     struct    videoconfig config;        /* Microsoft defined structure */
  350.     int     char_height;            /* height of characters in pixels */
  351.  
  352.     switch(pc_GetMode()) {
  353.        case 0x13:
  354.         _setvideomode(_MRES256COLOR);    /* initialize MicroSoft video mode */
  355.         char_height = 8;
  356.         break;
  357.  
  358.        case 0x12:
  359.         _setvideomode(_VRES16COLOR);    /* initialize MicroSoft video mode */
  360.         char_height = 16;
  361.         break;
  362.  
  363.        case 0x11:
  364.         _setvideomode(_VRES2COLOR);    /* initialize MicroSoft video mode */
  365.         char_height = 16;
  366.         break;
  367.  
  368.        case 0x10:
  369.         _setvideomode(_ERESCOLOR);    /* initialize MicroSoft video mode */
  370.         char_height = 16;
  371.         break;
  372.  
  373.        case 0x0f:
  374.         _setvideomode(_ERESNOCOLOR);    /* initialize MicroSoft video mode */
  375.         char_height = 14;
  376.         break;
  377.  
  378.        case 0x0e:
  379.         _setvideomode(_HRES16COLOR);    /* initialize MicroSoft video mode */
  380.         char_height = 8;
  381.         break;
  382.  
  383.        case 0x0d:
  384.         _setvideomode(_MRES16COLOR);    /* initialize MicroSoft video mode */
  385.         char_height = 8;
  386.         break;
  387.  
  388.        case 0x06:
  389.         _setvideomode(_HRESBW);    /* initialize MicroSoft video mode */
  390.         char_height = 8;
  391.         break;
  392.  
  393.        case 0x05:
  394.         _setvideomode(_MRESNOCOLOR);    /* initialize MicroSoft video mode */
  395.         char_height = 8;
  396.         break;
  397.     default:
  398.         return(1);
  399.     }
  400.  
  401.     /* initialize config structure */
  402.     _getvideoconfig(&config);
  403.  
  404.     dgraf_Clear();
  405.  
  406.     /* Set clip region to avoid drawing over menu bar */
  407.     _setcliprgn(0, char_height, config.numxpixels - 1, config.numypixels - 1);
  408.  
  409.     /* set up dgraf data */
  410.  
  411.     dgrafp->shape = MIXED;
  412.     dgrafp->size  = MIXED;
  413.     dgrafp->color = MIXED;
  414.  
  415.     dgrafp->hgt = config.numypixels;
  416.     dgrafp->wid = config.numxpixels;
  417.     dgrafp->ncolors = config.numcolors;
  418.  
  419.     /* Set up the menu colors */
  420.     if (disp_GetColors() > 2L) {
  421.         dgrafp->reg  = 0x34;
  422.         dgrafp->sel = 0x43;
  423.     }
  424.     else {
  425.         dgrafp->reg  = 0x10;
  426.         dgrafp->sel = 0x01;
  427.     }
  428.  
  429.     return(0);
  430. }
  431.  
  432. void dgraf_Line(opboxp, color)
  433.     opbox *opboxp;                /* defined by C-scape */
  434.     int color;
  435. /*
  436.     Draw a line from
  437.     (opboxp->xmin, opboxp->ymax) to (opboxp->xmax, opboxp->ymax)
  438.     with color color.
  439.  
  440.     Microsoft version.
  441. */
  442. {
  443.     _setcolor(color);
  444.     _moveto(opboxp->xmin, opboxp->ymin);
  445.     _lineto(opboxp->xmax, opboxp->ymax);
  446. }
  447.  
  448. void dgraf_Rectangle(opboxp, color)
  449.     opbox *opboxp;
  450.     int color;
  451. /*
  452.     Draw a rectangle with corners
  453.     (opboxp->xmin, opboxp->ymax) (opboxp->xmax, opboxp->ymax)
  454.     with color color.
  455.  
  456.     Microsoft version.
  457. */
  458. {
  459.     _setcolor(color);
  460.     _rectangle(_GBORDER, opboxp->xmin, opboxp->ymin, opboxp->xmax, opboxp->ymax);
  461. }
  462.  
  463. void dgraf_Ellipse(opboxp, color)
  464.     opbox *opboxp;
  465.     int color;
  466. /*
  467.     Draw an ellipse bounded by the rectangle with corners
  468.     (opboxp->xmin, opboxp->ymax) (opboxp->xmax, opboxp->ymax)
  469.     with color color.
  470.  
  471.     Microsoft version.
  472. */
  473. {
  474.     _setcolor(color);
  475.     _ellipse(_GBORDER, opboxp->xmin, opboxp->ymin, opboxp->xmax, opboxp->ymax);
  476. }
  477.  
  478. void dgraf_Clear()
  479. /*
  480.     Clear the display.
  481.  
  482.     Microsoft version.
  483. */
  484. {
  485.     _clearscreen(_GVIEWPORT);
  486. }
  487.  
  488. #endif
  489. /*---------------------------------------------------------------------------*/
  490.  
  491. #ifdef __TURBOC__            /* Turbo C 2.0 */
  492.  
  493. int dgraf_Init(dgrafp)
  494.     dgraf_struct *dgrafp;
  495. /*
  496.     Initialize the graphics library.
  497.     Initialize demo data.
  498.  
  499.     Returns 0 if successful.
  500.  
  501.     Turbo C version.
  502. */
  503. {
  504.     int        mode, driver;
  505.     int     char_height;            /* height of characters in pixels */
  506.  
  507.     switch(pc_GetMode()) {
  508.        case 0x13:
  509.         /* Not supported by Turbo C */
  510.         return(FALSE);
  511.  
  512.        case 0x12:
  513.         driver = VGA;
  514.         mode = VGAHI;
  515.         char_height = 16;
  516.         break;
  517.  
  518.        case 0x11:
  519.         driver = MCGA;
  520.         mode = MCGAHI;
  521.         char_height = 16;
  522.         break;
  523.  
  524.        case 0x10:
  525.         driver = EGA;
  526.         mode = EGAHI;
  527.         char_height = 16;
  528.         break;
  529.  
  530.        case 0x0f:
  531.         driver = EGAMONO;
  532.         mode = EGAMONOHI;
  533.         char_height = 14;
  534.         break;
  535.  
  536.        case 0x0e:
  537.         driver = EGA;
  538.         mode = EGALO;
  539.         char_height = 8;
  540.         break;
  541.  
  542.        case 0x0d:
  543.         /* Not supported by Turbo C */
  544.         return(FALSE);
  545.  
  546.        case 0x06:
  547.         driver = CGA;
  548.         mode = CGAHI;
  549.         char_height = 8;
  550.         break;
  551.  
  552.        case 0x05:
  553.         driver = CGA;
  554.         mode = CGAC0;
  555.         char_height = 8;
  556.         break;
  557.  
  558.     default:
  559.         return(FALSE);
  560.     }
  561.  
  562.     initgraph(&driver, &mode, BGI_PATH);
  563.  
  564.     if (driver < 0) {
  565.         /* initgraph failed */
  566.         return(driver);
  567.     }
  568.  
  569.     dgraf_Clear();
  570.  
  571.     /* Set clip region to avoid drawing over menu bar */
  572.     setviewport(0, char_height, getmaxx(), getmaxy(), TRUE);
  573.  
  574.     /* set up dgraf data */
  575.  
  576.     dgrafp->shape = MIXED;
  577.     dgrafp->size  = MIXED;
  578.     dgrafp->color = MIXED;
  579.  
  580.     dgrafp->hgt = getmaxy();
  581.     dgrafp->wid = getmaxx();
  582.     dgrafp->ncolors = getmaxcolor() + 1;
  583.  
  584.     /* Set up the menu colors */
  585.     if (disp_GetColors() > 2L) {
  586.         dgrafp->reg  = 0x34;
  587.         dgrafp->sel = 0x43;
  588.     }
  589.     else {
  590.         dgrafp->reg  = 0x10;
  591.         dgrafp->sel = 0x01;
  592.     }
  593.  
  594.     return(0);
  595. }
  596.  
  597. void dgraf_Line(opboxp, color)
  598.     opbox *opboxp;                /* defined by C-scape */
  599.     int color;
  600. /*
  601.     Draw a line from
  602.     (opboxp->xmin, opboxp->ymax) to (opboxp->xmax, opboxp->ymax)
  603.     with color color.
  604.  
  605.     Turbo C version.
  606. */
  607. {
  608.     setcolor(color);
  609.     line(opboxp->xmin, opboxp->ymin, opboxp->xmax, opboxp->ymax);
  610. }
  611.  
  612. void dgraf_Rectangle(opboxp, color)
  613.     opbox *opboxp;
  614.     int color;
  615. /*
  616.     Draw a rectangle with corners
  617.     (opboxp->xmin, opboxp->ymax) (opboxp->xmax, opboxp->ymax)
  618.     with color color.
  619.  
  620.     Turbo C version.
  621. */
  622. {
  623.     setcolor(color);
  624.     rectangle(opboxp->xmin, opboxp->ymin, opboxp->xmax, opboxp->ymax);
  625. }
  626.  
  627. void dgraf_Ellipse(opboxp, color)
  628.     opbox *opboxp;
  629.     int color;
  630. /*
  631.     Draw an ellipse bounded by the rectangle with corners
  632.     (opboxp->xmin, opboxp->ymax) (opboxp->xmax, opboxp->ymax)
  633.     with color color.
  634.  
  635.     Turbo C version.
  636.  
  637.     Note:    This really draws circles not ellipses...
  638. */
  639. {
  640.     int rad, x, y;
  641.  
  642.     x = opboxp->xmin + ((opboxp->xmax - opboxp->xmin) / 2);
  643.     y = opboxp->ymin + ((opboxp->ymax - opboxp->ymin) / 2);
  644.     rad = (opboxp->xmax - opboxp->xmin) / 2;
  645.  
  646.     setcolor(color);
  647.     circle(x, y, rad);
  648. }
  649.  
  650. void dgraf_Clear()
  651. /*
  652.     Clear the display.
  653.  
  654.     Turbo C version.
  655. */
  656. {
  657.     clearviewport();
  658. }
  659.  
  660. #endif
  661. /*---------------------------------------------------------------------------*/
  662.  
  663.