home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_03 / 2n03006b < prev    next >
Text File  |  1991-01-14  |  2KB  |  82 lines

  1. /*
  2.  * Listing 2: Print the screen to an Epson-compatible
  3.  * graphics printer.  (Link this with Listing 1.)
  4.  */
  5. #include <io.h>
  6. #include <graphics.h>
  7.  
  8. #define ESC 0x1b    /* escape */
  9. #define CR  0x0d    /* carriage return */
  10. #define LF  0x0a    /* line feed */
  11.  
  12. /* global variables
  13.  */
  14. extern int x_max, y_max;
  15. extern int print_fh;
  16.  
  17. /* prototypes
  18.  */
  19. void print_stripe( int y_start );
  20.  
  21.  
  22. void print_screen( void )
  23.     {
  24.     static char line_sp[3] = {ESC, 'A', 0x08};
  25.     static char reset_prn[2] = {ESC, '@'};
  26.     int y;
  27.  
  28.     /* Send ESC A 08 to set line spacing to 8 dot rows.
  29.      */
  30.     write(print_fh, line_sp, 3);
  31.  
  32.     /* Send the data in groups of 8 dots rows at a time,
  33.      * starting with row 0, then 8, 16...
  34.      */
  35.     for (y = 0; y < y_max; y += 8)
  36.         print_stripe(y);
  37.  
  38.     /* Send ESC @ to return the printer to defaults.
  39.      */
  40.     write(print_fh, reset_prn, 2);
  41.     }
  42.  
  43.  
  44. /*
  45.  * Send one "stripe" of pixels to the printer, beginning
  46.  * with the given Y-coordinate.  A stripe is 8 dots high,
  47.  * and as wide as the screen.
  48.  */
  49. void print_stripe( int y_start )
  50.     {
  51.     int num_pix, x, y, bit_num;
  52.     unsigned char out_pixel;
  53.     static char graph_cmd[4] = {ESC, 'K', 0, 0};
  54.     static char cr_lf[2] = {CR, LF};
  55.  
  56.     /* Send ESC K n1 n2, where (256 * n2) + n1 is the
  57.      * number of pixels across a line.
  58.      */
  59.     num_pix = x_max + 1;
  60.     graph_cmd[2] = num_pix & 0xff;
  61.     graph_cmd[3] = num_pix >> 8;
  62.     write(print_fh, graph_cmd, 4);
  63.  
  64.     for (x = 0; x < num_pix; ++x)
  65.         {
  66.         /* Accumulate 8 pixels, vertically, into out_pixel.
  67.          */
  68.         out_pixel = 0;
  69.         y = y_start;
  70.         for (bit_num = 7; bit_num >= 0; --bit_num, ++y)
  71.             {
  72.             if (getpixel(x, y) > 0)
  73.                 out_pixel |= (1 << bit_num);
  74.             }
  75.         write(print_fh, &out_pixel, 1);
  76.         }
  77.  
  78.     /* Send carriage return, line feed.
  79.      */
  80.     write(print_fh, cr_lf, 2);
  81.     }
  82.