home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / EGA.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  7KB  |  214 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* ega.c */
  20. /* EGA graphics hacks */
  21. #define interrupt            /* patch ANSI incompatibility */
  22. #include <dos.h>
  23. #include "math_.h"
  24. #include "stdio_.h"
  25. #include "gstypes.h"
  26. #include "gsmatrix.h"            /* for gxdevice.h */
  27. #include "gxdevice.h"
  28.  
  29. /* Stuff in gdevpcfb.c */
  30. #define pcfb_device gs_vga_device
  31. extern struct gx_device_s pcfb_device;
  32. #define the_device (&pcfb_device)
  33.  
  34. /* Stubs for other GS stuff */
  35. int
  36. gx_device_adjust_resolution(gx_device *dev,
  37.   int actual_width, int actual_height, int fit)
  38. {    return 0;
  39. }
  40.  
  41. float yDx = 35.0/48.0;            /* aspect ratio */
  42. #define black (gx_color_index)0
  43. #define green (gx_color_index)2
  44. #define blue (gx_color_index)9
  45. #define red (gx_color_index)12
  46. #define yellow (gx_color_index)14
  47. #define white (gx_color_index)15
  48.  
  49. main(int argc, char *argv[])
  50. {    return real_main(argc, argv);
  51. }
  52. real_main(int argc, char *argv[])
  53. {    /* Display all kinds of stuff */
  54.     (*the_device->procs->open_device)(the_device);
  55.     if ( !kbhit() ) paint1();
  56.     if ( !kbhit() ) paint2();
  57.     if ( !kbhit() ) paint3();
  58.     if ( !kbhit() ) paint4();
  59.     if ( !kbhit() ) test2();
  60.     while ( !kbhit() ) ;
  61.     (*the_device->procs->close_device)(the_device);
  62. }
  63.  
  64. /* Stubs for gx procedures */
  65. int gx_default_draw_line() { return -1; }
  66. void gx_default_get_initial_matrix() { }
  67. int gx_default_output_page() { return 0; }
  68. int gx_default_sync_output() { return 0; }
  69. int gx_default_tile_rectangle() { return -1; }
  70. int gx_default_get_params() { return 0; }
  71. int gx_default_put_params() { return 0; }
  72.  
  73. /* Display color samples */
  74. paint1()
  75. {    ushort r, g, b;
  76.     ushort m = the_device->color_info.max_rgb;
  77.     for ( r = 0; r <= m; r++ )
  78.      for ( g = 0; g <= m; g++ )
  79.       for ( b = 0; b <= m; b++ )
  80.     {    int x = r * 30, y = (g*(m+1)+b) * 24;
  81.         gx_color_index color =
  82.           (*the_device->procs->map_rgb_color)(the_device,
  83.             (ushort)(r * 0xffffL / m),
  84.             (ushort)(g * 0xffffL / m),
  85.             (ushort)(b * 0xffffL / m));
  86.         static unsigned char map[16] = {
  87.             0xaa, 0xaa, 0xaa, 0, 0x55, 0x55, 0x55, 0,
  88.             0xaa, 0xaa, 0xaa, 0, 0x55, 0x55, 0x55, 0
  89.            };
  90.         (*the_device->procs->fill_rectangle)(the_device, x, y, 21, 8, color);
  91.         (*the_device->procs->copy_mono)(the_device, map, 0, 4, gx_no_bitmap_id, x, y + 8, 21, 4, white, color);
  92.         (*the_device->procs->copy_mono)(the_device, map, 0, 4, gx_no_bitmap_id, x, y + 12, 21, 4, black, color);
  93.     }
  94.        {    int c;
  95.         for ( c = 0; c <= 15; c++ )
  96.            {    (*the_device->procs->fill_rectangle)(the_device, 320+c*20, 0, 14, 11, (gx_color_index)c);
  97.            }
  98.        }
  99. }
  100.  
  101. /* Tile with different color combinations */
  102. paint2()
  103. {    static byte pattern[] = {
  104.       0xaa,0xcc, 0x55,0xcc, 0xaa,0x33, 0x55,0x33,
  105.       0xaa,0xcc, 0x55,0xcc, 0xaa,0x33, 0x55,0x33,
  106.       0xf0,0xcc, 0xf0,0x66, 0xf0,0x33, 0xf0,0x99,
  107.       0x0f,0xcc, 0x0f,0x66, 0x0f,0x33, 0x0f,0x99
  108.     };
  109.     static gx_color_index colors[5] = { 0, 2, 6, 12, 0xf };
  110.     gx_bitmap tile;
  111.     int i, j;
  112.     tile.data = pattern;
  113.     tile.raster = 2;
  114.     tile.size.x = tile.rep_width = 16;
  115.     tile.size.y = tile.rep_height = 16;
  116.     tile.id = gx_no_bitmap_id;
  117.     for ( i = 0; i < 5; i++ )
  118.      for ( j = 0; j < 5; j++ )
  119.         (*the_device->procs->tile_rectangle)(the_device, &tile, 212+i*44, 20+j*44, 32, 32, colors[i], colors[j], 0, 0);
  120. }
  121.  
  122. /* Draw a turkey */
  123. paint3()
  124. {    static byte turkey[] = {
  125.       0x00,0x3b,0x00,0, 0x00,0x27,0x00,0, 0x00,0x24,0x80,0,
  126.       0x0e,0x49,0x40,0, 0x11,0x49,0x20,0, 0x14,0xb2,0x20,0,
  127.       0x3c,0xb6,0x50,0, 0x75,0xfe,0x88,0, 0x17,0xff,0x8c,0,
  128.       0x17,0x5f,0x14,0, 0x1c,0x07,0xe2,0, 0x38,0x03,0xc4,0,
  129.       0x70,0x31,0x82,0, 0xf8,0xed,0xfc,0, 0xb2,0xbb,0xc2,0,
  130.       0xbb,0x6f,0x84,0, 0x31,0xbf,0xc2,0, 0x18,0xea,0x3c,0,
  131.       0x0e,0x3e,0x00,0, 0x07,0xfc,0x00,0, 0x03,0xf8,0x00,0,
  132.       0x1e,0x18,0x00,0, 0x1f,0xf8,0x00,0
  133.     };
  134.     (*the_device->procs->fill_rectangle)(the_device, 0, 250, 30, 30, yellow);
  135.     (*the_device->procs->copy_mono)(the_device, turkey, 0, 4, gx_no_bitmap_id, 0, 250, 24, 23, white, red);
  136.     (*the_device->procs->copy_color)(the_device, turkey, 0, 4, gx_no_bitmap_id, 0, 280, 6, 23);
  137. }
  138.  
  139. /* Copy patterns to the screen */
  140. paint4()
  141. {    static unsigned char box[] =
  142.         { 0xff,0xff,0,0, 0xc0,0x0c,0,0, 0xc0,0x0c,0,0, 0xc0,0x0c,0,0, 0xff,0xff,0,0
  143.         };
  144.     int i, j;
  145.     (*the_device->procs->fill_rectangle)(the_device, 432 - 4, 20 - 4, 4 + 8 * 17 + 8, 4 + 8 * 17, green);
  146.     for ( i = 0; i < 8; i++ )
  147.       for ( j = 0; j < 8; j++ )
  148.         (*the_device->procs->copy_mono)(the_device, box, i, 4,
  149.             gx_no_bitmap_id, 432 + (j * 17) + i, 20 + (i * 17),
  150.             j + 1, 5, black, red);
  151. }
  152.  
  153. /* Plot a circle 2 different ways */
  154. plot_arc(P9(floatp, floatp, floatp, floatp, floatp, floatp, int, gx_color_index, floatp));
  155. test2()
  156. {    float kx = 200.0;
  157.     float ky = -kx * yDx;
  158.     float ox = 320.0, oy = 175.0;
  159.     float x0 = ox - kx, y0 = oy - ky;
  160.     float x1 = ox + kx, y1 = oy + ky;
  161.     /* Plot a real circle */
  162.     float i;
  163.     for ( i = 0; i <= 360; i += 5 )
  164.        {    float theta = i * M_PI / 180.0;
  165.         int x = ox + kx * cos(theta);
  166.         int y = oy + ky * sin(theta);
  167.         ega_write_dot(the_device, x, y, red);
  168.        }
  169.     /* Plot with 90 degree arcs */
  170.        {    static float mar[] = { 0.5, 0.45, 0.4 };
  171.         static gx_color_index car[] = { white, green, yellow };
  172.         int j;
  173.         for ( j = 0; j < 3; j++ )
  174.            {    float magic = mar[j];
  175.             gx_color_index color = car[j];
  176.             plot_arc(x1, oy, x1, y1, ox, y1, 40, color, magic);
  177.             plot_arc(ox, y1, x0, y1, x0, oy, 40, color, magic);
  178.             plot_arc(x0, oy, x0, y0, ox, y0, 40, color, magic);
  179.             plot_arc(ox, y0, x1, y0, x1, oy, 40, color, magic);
  180.            }
  181.        }
  182. }
  183.  
  184. /* Approximate an arc */
  185. plot_curve(P10(floatp, floatp, floatp, floatp, floatp, floatp, floatp, floatp, int, gx_color_index));
  186. plot_arc(floatp x0, floatp y0, floatp xi, floatp yi, floatp x3, floatp y3,
  187.   int nsteps, gx_color_index color, floatp magic)
  188. {    float cmagic = 1 - magic;
  189.     float xic = xi * cmagic, yic = yi * cmagic;
  190.     plot_curve(x0, y0, x0 * magic + xic, y0 * magic + yic,
  191.         x3 * magic + xic, y3 * magic + yic, x3, y3,
  192.         nsteps, color);
  193. }
  194.  
  195. /* Plot a Bezier curve */
  196. plot_curve(floatp x0, floatp y0, floatp x1, floatp y1,
  197.   floatp x2, floatp y2, floatp x3, floatp y3,
  198.   int nsteps, gx_color_index color)
  199. {    float dt = 1.0 / nsteps;
  200.     float cx = 3.0 * (x1 - x0);
  201.     float bx = 3.0 * (x2 - x1) - cx;
  202.     float ax = x3 - (x0 + cx + bx);
  203.     float cy = 3.0 * (y1 - y0);
  204.     float by = 3.0 * (y2 - y1) - cy;
  205.     float ay = y3 - (y0 + cy + by);
  206.     float t = 0.0;
  207.     while ( t <= 1.0 )
  208.        {    int x = ((ax * t + bx) * t + cx) * t + x0;
  209.         int y = ((ay * t + by) * t + cy) * t + y0;
  210.         ega_write_dot(the_device, x, y, color);
  211.         t += dt;
  212.        }
  213. }
  214.