home *** CD-ROM | disk | FTP | other *** search
/ Phoenix CD 2.0 / Phoenix_CD.cdr / 01e / msjall.zip / MSJV2-5.ZIP / MSC5.ALL < prev   
Text File  |  1987-12-27  |  5KB  |  207 lines

  1. Microsoft Systems Journal
  2. Volume 2; Issue 5; November, 1987
  3.  
  4. Code Listings For:
  5.  
  6.     GDEMO
  7.     pp. 67-76
  8.  
  9. Author(s): Augie Hansen
  10. Title:     Microsoft C Optimizing Compiler 5.0 Offers Improved Speed and 
  11.            Code Size
  12.  
  13.  
  14.  
  15.  
  16.  
  17. Figure 5
  18. ========
  19.  
  20.  
  21. /********************************************************************
  22. * NAME: gdemo
  23. *
  24. * DESCRIPTION: This is a simple graphics demonstration program.  Its
  25. *  purpose it to show how to select a graphics mode, do some simple
  26. *  graphics work, and then return to DOS after restoring the user's
  27. *  previous video environment.
  28. ********************************************************************/
  29.  
  30. #include <graph.h>
  31. #include <math.h>
  32.  
  33. #define RHEIGHT         160
  34. #define RWIDTH          300
  35. #define MSG_ROW         25
  36. #define MSG_COL         8
  37. #define M_NOWAIT        0
  38. #define M_WAIT          1
  39.  
  40. /*
  41.  * Macro to plot points with the normal sense of the y axis.  The
  42.  * graphics library default is inverted (y increases in value moving
  43.  * down the screen).
  44.  */
  45.  
  46. #define PLOT(x_, y_)    _setpixel((x_), -(y_))
  47.  
  48. extern void Message(short, short, char *, short);
  49.  
  50. int
  51. main()
  52. {
  53.     struct videoconfig config;  /* video configuration data */
  54.     short x_org, y_org;         /* coordinates of the origin */
  55.     short x_ul, y_ul;           /* upper-left corner */
  56.     short x_lr, y_lr;           /* lower-right corner */
  57.     short x, y;
  58.  
  59.     static char prompt[] = { "Press a key to continue..." };
  60.  
  61.     /*
  62.      * Set up the medium-resloution graphics screen and set
  63.      * the logical origin to the center of the screen.
  64.      */
  65.  
  66.     _setvideomode(_MRES16COLOR);
  67.     _getvideoconfig(&config);
  68.     x_org = config.numxpixels / 2 - 1;
  69.     y_org = config.numypixels / 2 - 1;
  70.     _setlogorg(x_org, y_org);
  71.  
  72.     /*
  73.      * Draw a border rectangle and coordinate system.
  74.      */
  75.  
  76.     x_ul = -RWIDTH / 2;
  77.     y_ul = RHEIGHT / 2;
  78.     x_lr = RWIDTH / 2;
  79.     y_lr = -RHEIGHT / 2;
  80.     _rectangle(_GBORDER, x_ul, y_ul, x_lr, y_lr);
  81.     _moveto(x_ul, 0);
  82.     _lineto(x_lr, 0);
  83.     _moveto(0, y_ul);
  84.     _lineto(0, y_lr);
  85.  
  86.     /*
  87.      * Plot some curves.
  88.      */
  89.  
  90.     for (x = -100; x <= 100; ++x) {
  91.     y = x / 2;
  92.     PLOT(x, y);
  93.     }
  94.     Message(11, 27, "y = x / 2", M_NOWAIT);
  95.     for (x = -20; x <= 20; ++x) {
  96.     y = (x * x / 3) - 75;
  97.     PLOT(x, y);
  98.     }
  99.     Message(22, 3, "y = x**2 / 3 - 75", M_NOWAIT);
  100.  
  101.     /*
  102.      * Wait for the user's command to continue.
  103.      */
  104.     Message(MSG_ROW, MSG_COL, prompt, M_WAIT);
  105.  
  106.     /*
  107.      * Restore the original video mode.
  108.      */
  109.     _setvideomode(_DEFAULTMODE);
  110.  
  111.     return (0);
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118. Figure 6
  119. ========
  120.  
  121.  
  122. /***************************************************************
  123. * Message
  124. *
  125. * DESCRIPTION:  Display the message text at the specified
  126. *  screen location (row, col).  If wait has a non-zero value,
  127. *  wait for the user to press a key.  When the user complies,
  128. *  grab the character from the keyboard buffer so it won't
  129. *  interfere with the calling program following the return.
  130. ***************************************************************/
  131.  
  132. #include <conio.h>
  133. #include <graph.h>
  134.  
  135. void
  136. Message(row, col, text, wait)
  137. short row, col; /* text position */
  138. char *text;     /* text pointer */
  139. short wait;     /* wait flag */
  140.         /* (wait != 0 means wait for a keypress) */
  141. {
  142.     int k;      /* key code */
  143.  
  144.     /*
  145.      * Write the prompt text at the specified location.
  146.      */
  147.     _settextposition(row, col);
  148.     _outtext(text);
  149.  
  150.     /*
  151.      * If the wait flag is set, wait for a key to be pressed,
  152.      * then remove the code from the keyboard buffer.  Handle
  153.      * extended codes by grabbing two bytes if the first is NUL.
  154.      */
  155.     if (wait) {
  156.     while (!kbhit())
  157.         ;
  158.     k = getch();            /* read the character */
  159.     if (k == '\0')
  160.         /* extended code -- get next byte */
  161.         getch();
  162.     }
  163. }
  164.  
  165.  
  166.  
  167. Figure 7
  168. ========
  169.  
  170. #
  171. # Program: Gdemo
  172. #
  173.  
  174. .c.obj:
  175.     qcl -c  -W2 -Ze -AM $*.c
  176.  
  177. gdemo.obj : gdemo.c
  178.  
  179. message.obj : message.c
  180.  
  181. Gdemo.exe : gdemo.obj message.obj 
  182.     del Gdemo.lnk
  183.     echo gdemo.obj+ >>Gdemo.lnk
  184.     echo message.obj  >>Gdemo.lnk
  185.     echo Gdemo.exe >>Gdemo.lnk
  186.     echo Gdemo.map >>Gdemo.lnk
  187.     link @Gdemo.lnk $(LDFLAGS);
  188.  
  189.  
  190.  
  191.  
  192. Figure 8
  193. ========
  194.  
  195.  
  196. # makefile for the GDEMO program
  197.  
  198. .c.obj:
  199.     cl -c -W2 -Ze -AL $*.c
  200.  
  201. gdemo.obj:    gdemo.c
  202.  
  203. message.obj:    message.c
  204.  
  205. gdemo.exe:    gdemo.obj message.obj 
  206.         link gdemo message, gdemo, nul, graphics;
  207.