home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 015.lha / tracer_source / graphics.c < prev    next >
C/C++ Source or Header  |  1986-11-10  |  4KB  |  164 lines

  1. /*
  2.     graphics.c
  3.  
  4.     display routines for 16 grey scales dithered to simulate 255.
  5.                         mcr
  6. */
  7.  
  8. #include <graphics/gfxbase.h>
  9. #include <graphics/display.h>
  10. #include <intuition/intuition.h>
  11. #include <stdio.h>
  12. #include "macros.h"
  13.  
  14. struct    GfxBase        *GfxBase;    /* Export the library pointers */
  15. struct    IntuitionBase    *IntuitionBase;
  16. struct    RastPort    *rp;        /* Graphics structures           */
  17. struct    ViewPort    *vp;
  18. struct    Window        *w;        /* Intuition structures        */ 
  19. struct    Screen        *screen;
  20.  
  21. struct    NewScreen    ns = {
  22.     0, 0,                /* start position                */
  23.     640, 400, 4,            /* width, height, depth          */
  24.     15, 0,                /* detail pen, block pen         */
  25.     HIRES|INTERLACE,        /* ViewModes             */
  26.     CUSTOMSCREEN,            /* screen type                   */
  27.     NULL,                /* font to use                   */
  28.     NULL,                /* default title for screen      */
  29.     NULL                /* pointer to additional gadgets */
  30. };
  31.  
  32. struct NewWindow nw = {
  33.     0, 0,                /* start position                */
  34.     640, 400,            /* width, height                 */
  35.     15, 0,                /* detail pen, block pen         */
  36.     NULL,                /* IDCMP flags                   */
  37.     BORDERLESS,            /* window flags                  */
  38.     NULL,                /* pointer to first user gadget  */
  39.     NULL,                /* pointer to user checkmark     */
  40.     NULL,                /* window title                  */
  41.     NULL,                /* pointer to screen (set below) */
  42.     NULL,                /* pointer to superbitmap        */
  43.     0, 0, 640, 386,            /* ignored since not sizeable    */
  44.     CUSTOMSCREEN            /* type of screen desired        */
  45. };
  46.  
  47. int    xoff, yoff;
  48.  
  49. gfx_open()
  50. {
  51.     if( !(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library", 0) ) ) {
  52.         gfx_close();
  53.         return(0);
  54.     }
  55.  
  56.     if ( !( IntuitionBase = (struct IntuitionBase *)
  57.         OpenLibrary("intuition.library", 0) )
  58.      ) {
  59.         gfx_close();
  60.         return(0);
  61.     }
  62.     if ( !( screen = (struct Screen *)
  63.         OpenScreen(&ns) )
  64.     ) { 
  65.         gfx_close();
  66.         return(0);
  67.     }
  68.  
  69.     nw.Screen = screen;                /* Open window in our new screen */
  70.     if ( !( w = (struct Window *)
  71.         OpenWindow(&nw) )
  72.     ) {
  73.         gfx_close();
  74.         return(0);
  75.     }
  76.  
  77.     vp = &screen->ViewPort;             /* Set colors in screen's VP    */
  78.     rp = w->RPort;                      /* Render into the window's RP  */
  79.  
  80.     /* Set the color registers ( cut down on ugly greens )    */
  81.     SetRGB4(vp, 00, 00, 00, 00);
  82.     SetRGB4(vp, 01, 01, 00, 01);    
  83.     SetRGB4(vp, 02, 02, 01, 02);   
  84.     SetRGB4(vp, 03, 03, 02, 03);
  85.     SetRGB4(vp, 04, 04, 03, 04);
  86.     SetRGB4(vp, 05, 05, 04, 05);
  87.     SetRGB4(vp, 06, 06, 05, 06);
  88.     SetRGB4(vp, 07, 07, 06, 07);
  89.     SetRGB4(vp, 08, 08, 07, 08);
  90.     SetRGB4(vp, 09, 09, 08, 09);
  91.     SetRGB4(vp, 10, 10, 09, 10);
  92.     SetRGB4(vp, 11, 11, 10, 11);
  93.     SetRGB4(vp, 12, 12, 11, 12);
  94.     SetRGB4(vp, 13, 13, 12, 13);
  95.     SetRGB4(vp, 14, 14, 13, 14);
  96.     SetRGB4(vp, 15, 15, 14, 15);
  97.  
  98.     return( 1 );
  99. }
  100.  
  101. gfx_close()
  102. {
  103.     if (w)
  104.         CloseWindow(w);
  105.  
  106.     if (screen)
  107.         CloseScreen(screen);
  108.  
  109.     if (IntuitionBase)
  110.         CloseLibrary(IntuitionBase);
  111.  
  112.     if (GfxBase)
  113.         CloseLibrary(GfxBase);
  114. }
  115.  
  116. struct    Screen    *gfx_screen()
  117. {
  118.     return( screen );
  119. }
  120.  
  121. do_pixel(x, y, col)
  122. register    int    x, y, col;
  123. {
  124.     SetAPen(rp, dither(x, y, col) );
  125.     WritePixel(rp, x, y );
  126. }
  127.  
  128.  
  129. #define DITHSIZE    3
  130. /* 4 x 4 dithering matrix */
  131. static    int    matrix[DITHSIZE+1][DITHSIZE+1] = {
  132.     {  0,  8,  2, 10 },
  133.     { 12,  4, 14,  6 },
  134.     {  3, 11,  1,  9 },
  135.     { 15,  7, 13,  5 }
  136. };
  137.  
  138. /*
  139.     dithering by area access to matrix by using
  140.     the MOD function on the x and y coordinates
  141. */
  142. dither(x, y, level) /* dithering function */
  143. register    int    x, y, level;
  144. {
  145.     register    int    base;
  146.  
  147.     base = level >> 4;
  148.  
  149.     if ( (base == 0x0F) || (level < 8) )
  150.         return(base);
  151.  
  152.     /*
  153.      * skew every second line over by half a grid to get a brick-layer's
  154.      * pattern, rather than a straight grid.  The human eye is too good
  155.      * at seeing straight lines when the points in the grid line up.
  156.      */
  157.     x += (y & 8) >> 1;
  158.  
  159.     if ( (level & 0x0f) < matrix [x%DITHSIZE] [y%DITHSIZE] )
  160.         return(base);
  161.  
  162.     return(base+1);
  163. }
  164.