home *** CD-ROM | disk | FTP | other *** search
/ Game Developers Magazine 4 / GDM004.ZIP / SOURCE / FCDEMO / FCDEMO.C
C/C++ Source or Header  |  1994-06-06  |  4KB  |  178 lines

  1. /*
  2.  
  3.  FCDEMO.C - Filled Circle Drawing, Using horizontal, then vertical, lines
  4.  
  5.  Written by Phil Inch for Game Developers Magazine (issue 4).
  6.  Contributed to the public domain.
  7.  
  8.  This program written and compiled with Borland C++ v3.1
  9.  Compatibility with other compilers is not guaranteed.
  10.  
  11.  Usage of this program is subject to the disclaimer printed
  12.  in the magazine.  You assume all risks associated with the use
  13.  of this program.
  14.  
  15. */
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <conio.h>
  20. #include <time.h>
  21. #include <dos.h>
  22. #include <mem.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <math.h>
  26.  
  27. const float PI = 3.1415927;
  28. char far *screen=MK_FP(0xA000,0);
  29.  
  30. int   INTCosArray[360], INTSinArray[360];
  31.  
  32. void    SetGraphicsMode( void ) {
  33.     asm {
  34.         mov    ax,0x13
  35.     int    0x10
  36.       }
  37. }
  38.  
  39. void    SetTextMode( void ) {
  40.     asm {
  41.       mov    ax,0x03
  42.     int    0x10;
  43.     }
  44. }
  45.  
  46. /* Fast horizontal line drawing program - from issue #2! */
  47. void fast_horizontal_line( int x1, int x2, int y, unsigned char colour ) {
  48.     char far *screen_location;
  49.     int xcount;
  50.  
  51.   /* "Clip" the co-ordinates */
  52.   if (x1<0) x1=0;
  53.   if (x1>319) x1=319;
  54.   if (x2<0) x2=0;
  55.   if (x2>319) x2=319;
  56.  
  57.     screen_location = MK_FP( 0xA000, ((int)y*320) + (int)x1 );
  58.     for ( xcount = x1; xcount <= x2; xcount++ ) {
  59.         *screen_location = colour;
  60.         screen_location++;
  61.         }
  62. }
  63.  
  64. /* Fast vertical line drawing program - from issue #2! */
  65. void fast_vertical_line( int x, int y1, int y2, unsigned char colour ) {
  66.     char far *screen_location;
  67.     int ycount;
  68.  
  69.   /* "Clip" the co-ordinates */
  70.   if (y1<0) y1=0;
  71.   if (y1>199) y1=199;
  72.   if (y2<0) y2=0;
  73.   if (y2>199) y2=199;
  74.  
  75.     screen_location = MK_FP( 0xA000, (int)x + ((int)y1*320) );
  76.     for ( ycount = y1; ycount <= y2; ycount++ ) {
  77.         *screen_location = colour;
  78.         screen_location += 320;
  79.         }
  80. }
  81.  
  82. /* Set up the integer cos and sin arrays */
  83. void LoadINTCosSinArrays(void) {
  84.   int a;
  85.   float radians;
  86.  
  87.   for ( a = 0; a < 360; a++ ) {
  88.       radians = (float)a * PI / 180;
  89.       INTCosArray[a] = (int)(cos(radians) * 100);
  90.       INTSinArray[a] = (int)(sin(radians) * 100);
  91.       }
  92. }
  93.  
  94. /* Filled circle drawing routine using HORIZONTAL lines */
  95. void FillHorizCircDraw(int xc, int yc, int r, int color) {
  96.   int x1, x2, y, s;
  97.  
  98.   for ( s = 90; s < 270; s++ ) {
  99.       x1 = xc + ( ( r * INTCosArray[s] ) / 100 );
  100.       x2 = xc - ( ( r * INTCosArray[s] ) / 100 );
  101.       y = yc + ( ( r * INTSinArray[s] ) / 100 );
  102.       fast_horizontal_line( x1, x2, y, color );
  103.       }
  104. }
  105.  
  106. /* Filled circle drawing routine using VERTICAL lines */
  107. void FillVertCircDraw(int xc, int yc, int r, int color) {
  108.   int x, y1, y2, s;
  109.  
  110.   for ( s = 0; s < 180; s++ ) {
  111.       x = xc + ( ( r * INTCosArray[s] ) / 100 );
  112.       y1 = yc - ( ( r * INTSinArray[s] ) / 100 );
  113.       y2 = yc + ( ( r * INTSinArray[s] ) / 100 );
  114.       fast_vertical_line( x, y1, y2, color );
  115.       }
  116. }
  117.  
  118. void main( void ) {
  119.   char input[64];
  120.   int xc,yc,r,c,d,number_of_circles;
  121.  
  122.   /* Set up the integer cos and sin arrays */
  123.   LoadINTCosSinArrays();
  124.  
  125.     clrscr();
  126.  
  127.     printf( "*** FILLED CIRCLE DRAWING DEMO ***\n\n" );
  128.     printf( "Suggestions for number of circles:\n\n" );
  129.     printf( "486/50:  2000\n" );
  130.     printf( "486/33:  1000\n" );
  131.     printf( "486/25:   750\n" );
  132.     printf( "386/40:   100\n" );
  133.     printf( "386/33:   100\n" );
  134.     printf( "lower :    50\n\n" );
  135.  
  136.     printf( "Enter number of circles to draw: " );
  137.     gets( input );
  138.  
  139.     number_of_circles = atol(input);
  140.     if ( number_of_circles <= 0 ) {
  141.         printf( "That's not a valid number!" );
  142.         exit(1);
  143.         }
  144.  
  145.   SetGraphicsMode();
  146.  
  147.   gotoxy( 12, 12 ); printf( "Horizontal Lines" ); delay(3000);
  148.   for ( d = 0; d < number_of_circles; d++ ) {
  149.       xc = rand()%320;
  150.       yc = rand()%200;
  151.       r = rand()%50;
  152.       c = rand()%255;
  153.       FillHorizCircDraw(xc,yc,r,c);
  154.       }
  155.  
  156.   delay(1000);
  157.  
  158.   SetGraphicsMode(); /* Quick way of clearing the graphics screen */
  159.  
  160.   gotoxy( 13, 12 ); printf( "Vertical Lines" ); delay(3000);
  161.   for ( d = 0; d < number_of_circles; d++ ) {
  162.       xc = rand()%320;
  163.       yc = rand()%200;
  164.       r = rand()%50;
  165.       c = rand()%255;
  166.       FillVertCircDraw(xc,yc,r,c);
  167.       }
  168.  
  169.   delay(1000);
  170.  
  171.   SetTextMode();
  172. }
  173.  
  174.  
  175.  
  176.  
  177.  
  178.