home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / turbo_c / plotc.arc / PLOT.C
Text File  |  1987-05-22  |  4KB  |  129 lines

  1. /****************************************************************
  2. *                                                               *
  3. *     This program is an example of using Turbo C inline        *
  4. *     assembly statements.                                      *
  5. *                                                               *
  6. *     The program should be compiled with the command line      *
  7. *     version of Turbo C with the compile with assembly         *
  8. *     flag on (tcc -B plot).                                    *
  9. *                                                               *
  10. ****************************************************************/
  11.  
  12. #include <stdio.h>
  13.  
  14. /* define some crt modes */
  15.  
  16. #define BW40     0
  17. #define COLOR40  1
  18. #define BW80     2
  19. #define COLOR80  3
  20. #define CGA320   4
  21. #define CGA320CB 5
  22. #define CGA640   6
  23. #define MONO     7
  24. #define EGA320   0x0D
  25. #define EGA640   0x0E
  26. #define EGAMono  0x0F
  27. #define EGAFull  0x10
  28.  
  29. void set_crtmode(int mode) {
  30. /****************************************************************
  31. *                                                               *
  32. *    Set the crt mode with interrupt 16 function 0              *
  33. *                                                               *
  34. ****************************************************************/
  35.  
  36.      asm mov ah, 0       /* function 0, set video mode */
  37.      asm mov al, mode
  38.      asm int 16          /* call interrupt 16 */
  39. }
  40.  
  41.  
  42. void plotpixel(int x, int y, int pixel_val) {
  43. /****************************************************************
  44. *                                                               *
  45. *    Plot a pixel at x,y                                        *
  46. *                                                               *
  47. ****************************************************************/
  48.  
  49.      asm mov ah, 12           /* function 12     */
  50.      asm mov al, pixel_val    /* pixel attribute */
  51.      asm mov cx, x
  52.      asm mov dx, y
  53.      asm int 16               /* call interrupt 16 */
  54.  
  55. } /* plotpixel */
  56.  
  57. void draw(int _x1, int _y1, int _x2, int _y2, int attr){
  58. /****************************************************************
  59. *                                                               *
  60. *    Draw a line from _x1,_y1 to _x2,_y2                        *
  61. *                                                               *
  62. ****************************************************************/
  63.  
  64.      int x, y, deltax, deltay, xstep, ystep, direction;
  65.      int  x1, y1, x2, y2;
  66.  
  67.      x = x1 = _x1;
  68.      y = y1 = _y1;
  69.  
  70.      xstep = (x1 > (x2 = _x2)) ? -1 : 1;
  71.      ystep = (y1 > (y2 = _y2)) ? -1 : 1;
  72.  
  73.      deltax = abs(x2 - x1);
  74.      deltay = abs(y2 - y1);
  75.  
  76.      direction = (deltax == 0) ? -1 : 0;
  77.  
  78.      while ( !( (x == x2) && (y == y2) ) ) {
  79.  
  80.          plotpixel(x, y, attr);
  81.  
  82.          if (direction < 0) {
  83.               y += ystep;
  84.               direction += deltax;
  85.          } else {
  86.               x += xstep;
  87.               direction -= deltay;
  88.          }
  89.      }
  90. } /* draw */
  91.  
  92.  
  93. void box(int x1, int y1, int x2, int y2, int attr){
  94. /****************************************************************
  95. *                                                               *
  96. *     Draw a box with corners x1,y1 & x2,y2                     *
  97. *                                                               *
  98. ****************************************************************/
  99.      int i;
  100.  
  101.      /* draw top and bottom */
  102.      for (i = x1 ; i <= x2 ; i++) {
  103.          plotpixel(i,y1,attr);
  104.          plotpixel(i,y2,attr);
  105.      }
  106.  
  107.      /* draw sides */
  108.      for (i = y1 ; i <= y2 ; i++) {
  109.          plotpixel(x1,i,attr);
  110.          plotpixel(x2,i,attr);
  111.      }
  112.  
  113. }
  114.  
  115. main(){
  116.      /* turn on CGA 640 X 200 */
  117.      set_crtmode(CGA640);
  118.  
  119.      printf("This is an example of using 640 X 200 CGA");
  120.  
  121.      box(20,20,630,190,1);
  122.  
  123.      draw(20,20,630,190,1);
  124.  
  125.      getch();
  126.  
  127.      /* turn on color text mode */
  128.      set_crtmode(COLOR80);
  129. }