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

  1. /*--------------------------------------------------------------*/
  2. /*- This file is the sample C program to draw Lines, Squares   -*/
  3. /*- and Ovals randomly in SuperVGA mode. It calls the assembly -*/
  4. /*- language routines contained in the svgalib.asm file.       -*/
  5. /*- Compiler: Microsoft C 6.0                                  -*/
  6. /*--------------------------------------------------------------*/
  7.  
  8. #include <stdio.h>          /** Standard MSC libraries **/
  9. #include <conio.h>
  10. #include <math.h>
  11.  
  12. #include "svgalib.h"  /** Prototypes for svga assembly routines **/  
  13.  
  14. #define XRES         800
  15. #define YRES         600
  16. #define NUM_OF_COLOR 256
  17. #define NUM_OF_RANDOM_RECTS 50
  18. #define NUM_OF_RANDOM_LINES 100
  19. #define NUM_OF_RANDOM_OVALS 100
  20. #define ASPECT_RATIO    5
  21.  
  22. typedef struct POINT_STRUCT
  23. {
  24.     int x;
  25.     int y;
  26. } POINT;
  27. typedef POINT * PPOINT;
  28.  
  29.  
  30. /************* Function Prototypes **************/
  31.  
  32. void Square(PPOINT pPoint1,PPOINT pPoint2,int Color);
  33. void Line(PPOINT pP1,PPOINT pP2,int Color);
  34. void Oval(PPOINT pP1,int Radius,int Color,float Aspect);
  35. void RandomLines(int x);
  36. void RandomSquares(int x);
  37. void RandomOvals(int x);
  38.  
  39.  
  40.  
  41. /*** This is rudimentary square drawing function ***/
  42. /*** It is not intended to be anything put an example ***/
  43. /*** on using the SVGA assembly routines from C ***/
  44.  
  45. void Square(PPOINT pPoint1,PPOINT pPoint2,int Color)
  46. {
  47.     POINT TopRight,BottomLeft;      /** For Rectangle **/
  48.  
  49.     TopRight.x = pPoint2->x;  
  50.     TopRight.y = pPoint1->y;
  51.     BottomLeft.x = pPoint1->x;
  52.     BottomLeft.y = pPoint2->y;
  53.  
  54.     Line(pPoint1,&TopRight,Color);
  55.     Line(&BottomLeft,pPoint2,Color);
  56.     Line(pPoint1,&BottomLeft,Color);
  57.     Line(&TopRight,pPoint2,Color);
  58. }
  59.  
  60.  
  61. /** This function will draw a line using the "Bresenham's Algorithm **/
  62. /** Algorithm taken from the "Graphics Programming in C Book" **/
  63. void Line(PPOINT pP1,PPOINT pP2,int Color)
  64. {
  65.     #define sign(x) ((x) > 0 ? 1 : ((x) == 0 ? 0 : (-1)))
  66.  
  67.     int xSlope,ySlope,xAbs,yAbs,i,px,py,signX,signY,x,y;
  68.  
  69.     xSlope = pP2->x - pP1->x;
  70.     ySlope = pP2->y - pP1->y;
  71.  
  72.     signX = sign(xSlope);
  73.     signY = sign(ySlope);
  74.  
  75.     xAbs = abs(xSlope);
  76.     yAbs = abs(ySlope);
  77.  
  78.     x=0;
  79.     y=0;
  80.  
  81.     px = pP1->x;
  82.     py = pP1->y;
  83.  
  84.     if(xAbs >= yAbs)
  85.     {
  86.         for(i=0;i <= xAbs; i++)
  87.         {
  88.             y += yAbs;
  89.             if( y >= xAbs)
  90.             {
  91.                 y -= xAbs;
  92.                 py += signY;
  93.             }
  94.             SetPixel(px,py,Color);
  95.             px += signX;
  96.         }
  97.  
  98.     }
  99.     else
  100.     {
  101.         for(i=0;i <= yAbs;i++)
  102.         {
  103.             x += xAbs;
  104.             if(x >= yAbs)
  105.             {
  106.                 x-= yAbs;
  107.                 px += signX;
  108.             }
  109.             SetPixel(px,py,Color);
  110.             py += signY;
  111.         }
  112.     }
  113.  
  114. }
  115.  
  116. /** This function will draw a Oval using the "Bresenham's Algorithm **/
  117. /** Algorithm taken from the "Graphics Programming in C Book" **/
  118.  
  119. void Oval(PPOINT pP1,int Radius,int Color,float Aspect)
  120. {                               
  121.     int col,i,row;
  122.     float Aspect_Sqr;
  123.     long d,a_Square,b_Square;
  124.     long Two_a_Square,Two_b_Square,Four_a_Square,Four_b_Square;
  125.  
  126.     int LINEWIDTH = 1;
  127.  
  128.     Aspect_Sqr = Aspect * Aspect;
  129.     Radius -=LINEWIDTH / 2;
  130.  
  131.     for(i=1;i <= LINEWIDTH; i++)
  132.     {
  133.         b_Square = Radius * Radius;
  134.         a_Square = b_Square / Aspect_Sqr;
  135.         row = Radius;
  136.         col = 0;
  137.         Two_a_Square = a_Square << 1;
  138.         Four_a_Square = a_Square << 2;
  139.         Two_b_Square = b_Square << 1;
  140.         Four_b_Square = b_Square << 2;
  141.  
  142.         d = Two_a_Square * ((row - 1) * (row)) + 
  143.             a_Square + Two_b_Square * (1 - a_Square);
  144.  
  145.         while(a_Square * (row) > b_Square * (col))
  146.         {
  147.             SetPixel(col + pP1->x, row + pP1->y, Color);
  148.             SetPixel(col + pP1->x, pP1->y - row, Color);
  149.             SetPixel(pP1->x - col, row + pP1->y, Color);
  150.             SetPixel(pP1->x - col, pP1->y - row, Color);
  151.             if(d >= 0)
  152.             {
  153.                 row--;
  154.                 d -= Four_a_Square * (row);
  155.             }
  156.             d+= Two_b_Square * (3 + (col << 1));
  157.             col++;
  158.         }
  159.  
  160.         d = Two_b_Square * (col + 1) * col +
  161.             Two_a_Square * (row * (row - 2) + 1) +
  162.             (1 - Two_a_Square) * b_Square;
  163.  
  164.         while((row) + 1)
  165.         {
  166.             SetPixel(col + pP1->x, row + pP1->y,Color);
  167.             SetPixel(col + pP1->x, pP1->y - row,Color);
  168.             SetPixel(pP1->x - col, row + pP1->y,Color);
  169.             SetPixel(pP1->x - col, pP1->y - row,Color);
  170.             if(d <= 0)
  171.             {
  172.                 col++;
  173.                 d+= Four_b_Square * col;
  174.             }
  175.             row--;
  176.             d+=Two_a_Square * (3 - (row << 1));
  177.         }
  178.         Radius++;
  179.     }
  180. }
  181.  
  182.  
  183. void RandomSquares(int x)
  184. {
  185.     int i,color;
  186.     POINT TopLeft,BottomRight;      /** For Rectangle **/
  187.  
  188.     for(i=0;i<x;i++)
  189.     {
  190.         TopLeft.x = rand() % XRES;
  191.         TopLeft.y = rand() % YRES;
  192.         BottomRight.x = rand() % XRES;
  193.         BottomRight.y = rand() % YRES;
  194.         color = rand() % NUM_OF_COLOR;
  195.         Square(&TopLeft,&BottomRight,color);
  196.     }
  197. }
  198.  
  199. void RandomLines(int x)
  200. {
  201.     int i,color;
  202.     POINT Pt1,Pt2;      
  203.  
  204.     for(i=0;i<x;i++)
  205.     {
  206.         Pt1.x = rand() % XRES;
  207.         Pt1.y = rand() % YRES;
  208.         Pt2.x = rand() % XRES;
  209.         Pt2.y = rand() % YRES;
  210.         color = rand() % NUM_OF_COLOR;
  211.         Line(&Pt1,&Pt2,color);
  212.     }
  213. }
  214.  
  215. void RandomOvals(int x)
  216. {
  217.     int i,Color,Aspect,Radius;
  218.     POINT Center;      
  219.  
  220.     for(i=0;i<x;i++)
  221.     {
  222.         /** Calculate the Center,Radius,Color and Aspect for Oval **/
  223.         Center.x = rand() % XRES;
  224.         Center.y = rand() % YRES;
  225.         Radius = rand() % (XRES / 4);
  226.         Color = rand() % NUM_OF_COLOR;
  227.         Aspect = (rand() % ASPECT_RATIO) + 1;
  228.  
  229.         Oval(&Center,Radius,Color,(float)Aspect);
  230.     }
  231. }
  232.  
  233. void main()
  234. {
  235.     int c;
  236.  
  237.     SvgaMode();     /** Set Video card to SVGA (800x600x256) mode **/
  238.     RandomLines(NUM_OF_RANDOM_LINES);
  239.     c = getch();
  240.     SvgaMode();
  241.     RandomSquares(NUM_OF_RANDOM_RECTS);
  242.     c = getch();
  243.     SvgaMode();
  244.     RandomOvals(NUM_OF_RANDOM_OVALS);
  245.     c = getch();
  246.     TextMode();     /** Set to Color 80 Col Text **/
  247. }
  248.  
  249.