home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / LCNOW2.ZIP / EXAMPLES / LCGDEMO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-03  |  5.0 KB  |  202 lines

  1. /*
  2.  * L C G D E M O
  3.  *
  4.  * This is a Learn C graphics demonstration program.  Its purpose
  5.  * is to show how to select a graphics mode, draw some shapes and
  6.  * text, and then return to DOS after restoring the user's
  7.  * previous video environment.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <conio.h>
  13. #include <graph.h>
  14. #include <math.h>
  15.  
  16. #define A_BRIGHTYELLOW  14
  17. #define K_ESC           27
  18. #define MSG_ROW         25
  19. #define MSG_COL         2
  20. #define MSG_WIDTH       38
  21. #define M_NOWAIT        0
  22. #define M_WAIT          1
  23. #define MODE_ERR        0
  24. #define NCOLORS         16
  25. #define R_HORIZ         -140
  26. #define R_VERT          0
  27. #define R_HEIGHT        80
  28. #define R_WIDTH         150
  29. #define R_BORDER        0
  30. #define R_FILL          1
  31.  
  32. /*
  33.  * Color table -- convert color index to universal color number.
  34.  */
  35. Long colortab[] = {
  36.     _BLACK, _BLUE, _GREEN, _CYAN
  37.     _RED, _MAGENTA, _BROWN, _WHITE
  38.     _GRAY, _LIGHTBLUE, _LIGHTGREEN, _LIGHTCYAN
  39.     _LIGHTRED, _LIGHTMAGENTA, _LIGHTYELLOW, _BRIGHTWHITE
  40. };
  41.  
  42. /*
  43.  * Function prototypes
  44.  */
  45. void Message(short, short, char *, short);
  46. void Rectangle(short, short, short, short, short);
  47.  
  48. int
  49. main(void)
  50. {
  51.     struct videoconfig config;  /* video configuration data */
  52.     short x_org, y_org;         /* coordinates of the origin */
  53.     short x_ul, y_ul;           /* upper left corner */
  54.     short x_lr, y_lr;           /* lower right corner */
  55.     short x, y;                 /* current coordinates */
  56.     short x_offset, y_offset;   /* offsets */
  57.     short color;                /* color number */
  58.     short divisor;              /* to keep color number in range */
  59.     short delta;                /* columnar offset */
  60.  
  61.     /*
  62.      * Set up the medium-resloution graphics,
  63.      * set the logical origin to center of screen,
  64.      * and set up a text window.
  65.      */
  66.     divisor = 16;
  67.     if (_setvideomode(_MRES16COLOR) == MODE_ERR) {
  68.         divisor = 4;
  69.         if (_setvideomode(_MRES4COLOR) == MODE_ERR) {
  70.             fprintf(stderr, "Can't set graphics mode\n");
  71.             exit (1);
  72.         }
  73.     }
  74.     _getvideoconfig(&config);
  75.     x_org = config.numxpixels / 2 - 1;
  76.     y_org = config.numypixels / 2 - 1;
  77.     _setlogorg(x_org, y_org);
  78.     _settextwindow(MSG_ROW, MSG_COL, MSG_ROW,
  79.                MSG_COL + MSG_WIDTH - 1);
  80.     _settextposition(MSG_ROW, MSG_COL);
  81.     _settextcolor(A_BRIGHTYELLOW);
  82.  
  83.     /*
  84.      * Draw a stack of rectangles in different colors.
  85.      */
  86.     x = R_HORIZ;
  87.     y = R_VERT;
  88.     x_offset = 0;
  89.     y_offset = 0;
  90.     for (color = 0; color < NCOLORS; ++color) {
  91.         _setcolor(color % divisor);
  92.         Rectangle(R_FILL, y + y_offset, x + x_offset,
  93.               y + R_HEIGHT + y_offset, x + R_WIDTH +
  94.               x_offset);
  95.         _setcolor((NCOLORS - 1 - color) % divisor);
  96.         Rectangle(R_BORDER, y + y_offset, x + x_offset,
  97.               y + R_HEIGHT + y_offset,
  98.               x + R_WIDTH + x_offset);
  99.         x_offset += 8;
  100.         y_offset -= 6;
  101.     }
  102.  
  103.     /*
  104.      * Create a text window and slide the "quit" message into
  105.      * view from right to left and then wait for a keypress.
  106.      */
  107.     _wrapon(_GWRAPOFF);
  108.     for (delta = MSG_WIDTH; delta >= 0; --delta)
  109.         Message(MSG_ROW, MSG_COL + delta,
  110.             "Press a key to quit.", M_NOWAIT);
  111.     Message(MSG_ROW, MSG_COL + delta,
  112.     "Press a key to quit.", M_WAIT);
  113.  
  114.     /*
  115.      * Restore the original video mode and
  116.      * return control to the operating system.
  117.      */
  118.     _setvideomode(_DEFAULTMODE);
  119.  
  120.     return (0);
  121. }
  122.  
  123.  
  124. /*
  125.  * Rectangle()
  126.  *
  127.  * Draw a rectangle in the current color.  The type
  128.  * parameter controls whether the rectangle is drawn
  129.  * as a border only or as a filled (solid) object.
  130.  */
  131.  
  132. void
  133. Rectangle(short type, short top, short left, short bottom,
  134.       short right)
  135. {
  136.     short x, y;
  137.  
  138.     switch (type) {
  139.     case R_BORDER:
  140.         /*
  141.          * Construct a box from four line segments.
  142.          */
  143.         _moveto(left, top);
  144.         _lineto(right, top);
  145.         _lineto(right, bottom);
  146.         _lineto(left, bottom);
  147.         _lineto(left, top);
  148.         break;
  149.     case R_FILL:
  150.         /*
  151.          * Make a solid box from a series of
  152.          * adjacent horizontal lines.
  153.          */
  154.         for (y = top; y <= bottom; ++y) {
  155.             _moveto(left, y);
  156.             _lineto(right, y);
  157.         }
  158.         break;
  159.     default:
  160.         break;
  161.     }
  162. }
  163.  
  164.  
  165. /*
  166.  * Message()
  167.  *
  168.  * Display the message text at the specified screen
  169.  * location (row, col).  If wait has a non-zero value,
  170.  * wait for the user to press a key.  When the user complies,
  171.  * grab the character from the keyboard buffer so it won't
  172.  * interfere with the calling program following the return.
  173.  */
  174.  
  175. void
  176. Message(row, col, text, wait)
  177. short row, col; /* text position */
  178. char *text;     /* text pointer */
  179. short wait;     /* wait flag */
  180.         /* (wait != 0 means wait for a keypress) */
  181. {
  182.     int k;      /* key code */
  183.  
  184.     /*
  185.      * Write the prompt text at the specified location.
  186.      */
  187.     _settextposition(row, col);
  188.     _outtext(text);
  189.  
  190.     /*
  191.      * If the wait flag is set, wait for a key to be pressed,
  192.      * then remove the code from the keyboard buffer.  Handle
  193.      * extended codes by grabbing two bytes if the first is NUL.
  194.      */
  195.     if (wait) {
  196.         k = getch();            /* read the character */
  197.         if (k == '\0')
  198.             /* extended code -- get the scan code */
  199.             getch();
  200.     }
  201. }
  202.