home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / 3DGPL.ZIP / 3DGPL / CODE / GRAPHICS / GRP-BASE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-22  |  4.4 KB  |  111 lines

  1. /** 3DGPL *************************************************\
  2.  * (8bit deep bitmap)                                     *
  3.  * 2D graphics and 2D clipping.                           *
  4.  *                                                        *
  5.  *  Defines:                                              *
  6.  *   G_init_graphics         initializing graphics;       *
  7.  *   G_clear                 clearing the bitmap;         *
  8.  *                                                        *
  9.  *   G_dot                   dot into the bitmap;         *
  10.  *   G_line                  line into a bitmap;          *
  11.  *                                                        * 
  12.  *  (6/1995) By Sergei Savhenko. (savs@cs.mcgill.ca).     *
  13.  *  Copyright (c) 1995 Sergei Savchenko.                  *
  14.  *  THIS SOURCE CODE CAN'T BE USED FOR COMERCIAL PURPOSES *
  15.  *  WITHOUT AUTHORISATION                                 *
  16. \**********************************************************/
  17.  
  18. #include <stdlib.h>                         /* malloc */
  19. #include "../hardware/hardware.h"           /* hardware specific stuff */
  20. #include "../clipper/clipper.h"             /* 2D clipping routines */
  21. #include "../graphics/graphics.h"           /* graphics functions */
  22.  
  23. unsigned char *G_buffer;                    /* the bitmap's bits */
  24.  
  25. /**********************************************************\
  26.  *  Allocating space for the colourmap.                   *
  27. \**********************************************************/
  28.  
  29. unsigned char *G_init_graphics(void)
  30. {
  31.  return(G_buffer=(unsigned char*)malloc(HW_COLOURMAP_SIZE_CHAR));
  32. }
  33.    
  34. /**********************************************************\
  35.  *  Clearing the bitmap with the specified colour.        *
  36. \**********************************************************/
  37.  
  38. void G_clear(void)
  39. {
  40.  HW_set_int((int*)G_buffer,HW_COLOURMAP_SIZE_INT,0x0);
  41. }
  42.  
  43. /**********************************************************\
  44.  *  Rendering a dot.                                      *
  45. \**********************************************************/
  46.  
  47. void G_dot(int *vertex,unsigned char colour)
  48. {
  49.  if( (vertex[0]>=0)&&(vertex[0]<HW_SCREEN_X_SIZE) &&
  50.      (vertex[1]>=0)&&(vertex[1]<HW_SCREEN_Y_SIZE) )
  51.  {
  52.   G_buffer[vertex[1]*HW_SCREEN_X_SIZE+vertex[0]]=colour;
  53.  }
  54. }
  55.  
  56. /**********************************************************\
  57.  *  Rendering a line.                                     *
  58. \**********************************************************/
  59.  
  60. void G_line(int *vertex1,int *vertex2,unsigned char colour)
  61. {
  62.  register int inc_ah,inc_al;                
  63.  register int i;                            
  64.  register unsigned char *adr=G_buffer;
  65.  int *v1,*v2;
  66.  int dx,dy,long_d,short_d;
  67.  int d,add_dh,add_dl;
  68.  int inc_xh,inc_yh,inc_xl,inc_yl;
  69.  
  70.  v1=vertex1;
  71.  v2=vertex2;
  72.  
  73.  if(C_line_x_clipping(&v1,&v2,2))           /* horizontal clipping */
  74.  {
  75.   if(C_line_y_clipping(&v1,&v2,2))          /* vertical clipping */
  76.   {
  77.    dx=v2[0]-v1[0]; dy=v2[1]-v1[1];          /* ranges */
  78.  
  79.    if(dx<0){dx=-dx; inc_xh=-1; inc_xl=-1;}  /* making sure dx and dy >0 */
  80.    else    {        inc_xh=1;  inc_xl=1; }  /* adjusting increments */
  81.  
  82.    if(dy<0){dy=-dy;inc_yh=-HW_SCREEN_X_SIZE;
  83.                    inc_yl=-HW_SCREEN_X_SIZE;
  84.            }                                /* to get to the neighboring */
  85.    else    {       inc_yh= HW_SCREEN_X_SIZE;/* point along Y have to add */
  86.                    inc_yl= HW_SCREEN_X_SIZE;/* or subtract this */
  87.            }                               
  88.    if(dx>dy){long_d=dx;short_d=dy;inc_yl=0;}/* long range,&making sure either */
  89.    else     {long_d=dy;short_d=dx;inc_xl=0;}/* x or y is changed in L case */
  90.  
  91.    inc_ah=inc_xh+inc_yh;
  92.    inc_al=inc_xl+inc_yl;                    /* increments for point address */
  93.    adr+=v1[1]*HW_SCREEN_X_SIZE+v1[0];       /* address of first point */
  94.  
  95.    d=2*short_d-long_d;                      /* initial value of d */
  96.    add_dl=2*short_d;                        /* d adjustment for H case */
  97.    add_dh=2*short_d-2*long_d;               /* d adjustment for L case */
  98.  
  99.    for(i=0;i<=long_d;i++)                   /* for all points in longer range */
  100.    {
  101.     *adr=colour;                            /* rendering */
  102.  
  103.     if(d>=0){adr+=inc_ah; d+=add_dh;}       /* previous point was H type */
  104.     else    {adr+=inc_al; d+=add_dl;}       /* previous point was L type */
  105.    }
  106.   }
  107.  }
  108. }
  109.  
  110. /**********************************************************/
  111.