home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c021 / 7.img / EXAMPLES.ZIP / PLANETS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-04  |  3.7 KB  |  105 lines

  1. /* PLANETS.C--Example from chapters 4 and 7 of Getting Started */
  2.  
  3. #include <graphics.h>           /* For graphics library functions */
  4. #include <stdlib.h>             /* For exit() */
  5. #include <stdio.h>
  6. #include <conio.h>
  7.  
  8. int set_graph(void);            /* Initialize graphics */
  9. void calc_coords(void);         /* Scale distances onscreen */
  10. void draw_planets(void);        /* Draw and fill planet circles */
  11.  
  12. /* Draw one planet circle */
  13. void draw_planet(float x_pos, float radius,
  14.                  int color, int fill_style);
  15. void get_key(void);         /* Display text on graphics screen, */
  16.                             /* wait for key */
  17.  
  18. /* Global variables -- set by calc_coords() */
  19. int max_x, max_y;           /* Maximum x- and y-coordinates */
  20. int y_org;                  /* Y-coordinate for all drawings */
  21. int au1;                    /* One astronomical unit in pixels
  22.                                (inner planets) */
  23. int au2;                    /* One astronomical unit in pixels
  24.                                (outer planets) */
  25. int erad;                   /* One earth radius in pixels */
  26.  
  27. int main()
  28. {
  29.    /* Exit if not EGA or VGA */
  30.    /* Find out if they have what it takes */
  31.    if (set_graph() != 1) {
  32.       printf("This program requires EGA or VGA graphics\n");
  33.       exit(0);
  34.    }
  35.    calc_coords();       /* Scale to graphics resolution in use */
  36.    draw_planets();      /* Sun through Uranus (no room for others) */
  37.    get_key();           /* Display message and wait for key press */
  38.    closegraph();        /* Close graphics system */
  39.  
  40.    return 0;
  41. }
  42.  
  43. int set_graph(void)
  44. {
  45.    int graphdriver = DETECT, graphmode, error_code;
  46.  
  47.    /* Initialize graphics system; must be EGA or VGA */
  48.    initgraph(&graphdriver, &graphmode, "..\\bgi");
  49.    error_code = graphresult();
  50.    if (error_code != grOk)
  51.       return(-1);               /* No graphics hardware found */
  52.    if ((graphdriver != EGA) && (graphdriver != VGA))
  53.    {
  54.       closegraph();
  55.       return 0;
  56.    }
  57.    return(1);                   /* Graphics OK, so return "true" */
  58. }
  59.  
  60. void calc_coords(void)
  61. {
  62.    /* Set global variables for drawing */
  63.    max_x = getmaxx();           /* Returns maximum x-coordinate */
  64.    max_y = getmaxy();           /* Returns maximum y-coordinate */
  65.    y_org = max_y / 2;           /* Set Y coord for all objects */
  66.    erad = max_x  / 200;         /* One earth radius in pixels */
  67.    au1 = erad * 20;             /* Scale for inner planets */
  68.    au2 = erad * 10;             /* scale for outer planets */
  69. }
  70.  
  71. void draw_planets()
  72. {
  73.    /* Each call specifies x-coordinate in au, radius, and color */
  74.    /* arc of Sun */
  75.    draw_planet(-90, 100, EGA_YELLOW, EMPTY_FILL);
  76.    /* Mercury */
  77.    draw_planet(0.4 * au1, 0.4 * erad, EGA_BROWN, LTBKSLASH_FILL);
  78.    /* Venus */
  79.    draw_planet(0.7 * au1, 1.0 * erad, EGA_WHITE, SOLID_FILL);
  80.    /* Earth */
  81.    draw_planet(1.0 * au1, 1.0 * erad, EGA_LIGHTBLUE, SOLID_FILL);
  82.    /* Mars */
  83.    draw_planet(1.5 * au1, 0.4 * erad, EGA_LIGHTRED, CLOSE_DOT_FILL);
  84.    /* Jupiter */
  85.    draw_planet(5.2 * au2, 11.2 * erad, EGA_WHITE, LINE_FILL);
  86.    /* Saturn */
  87.    draw_planet(9.5 * au2, 9.4 * erad, EGA_LIGHTGREEN, LINE_FILL);
  88.    /* Uranus */
  89.    draw_planet(19.2 * au2, 4.2 * erad, EGA_GREEN, LINE_FILL);
  90. }
  91.  
  92. void draw_planet(float x_pos, float radius, int color, int fill_style)
  93. {
  94.    setcolor (color);                /* This becomes drawing color */
  95.    circle(x_pos, y_org, radius);    /* Draw the circle */
  96.    setfillstyle(fill_style, color); /* Set pattern to fill interior */
  97.    floodfill(x_pos, y_org, color);  /* Fill the circle */
  98. }
  99.  
  100. void get_key(void)
  101. {
  102.    outtextxy(50, max_y - 20, "Press any key to exit");
  103.    getch();
  104. }
  105.