home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / cbgi111 / drv2 / vga16dem.c < prev   
Encoding:
C/C++ Source or Header  |  1990-02-23  |  7.6 KB  |  284 lines

  1. /*
  2.  +---------------------------------------------------------------------+
  3.  | Demonstration program which illustrates the usage of the VGA16.BGI  |
  4.  | graphics device driver.                                             |
  5.  |                                                                     |
  6.  | Author: John Sieraski                                               |
  7.  | Date:   2/23/90                                                     |
  8.  +---------------------------------------------------------------------+
  9. */
  10.  
  11. #include <graphics.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <conio.h>
  15. #include <string.h>
  16.  
  17. /* function prototypes */
  18.  
  19. int huge detectVGA(void);
  20. void checkerrors(void);
  21. void wait(void);
  22. void box_demo(void);
  23. void cir_demo(void);
  24. void page_demo(void);
  25. void line_demo(void);
  26. void random_lines(void);
  27.  
  28. int gdriver, gmode;
  29.  
  30. main()
  31. {
  32.    gdriver = installuserdriver("VGA16", detectVGA);
  33.    while (1)
  34.    {
  35.       gdriver = DETECT;
  36.       checkerrors();
  37.       initgraph(&gdriver, &gmode, "");
  38.       checkerrors();
  39.       box_demo();
  40.       cir_demo();
  41.       page_demo();
  42.       line_demo();
  43.       random_lines();
  44.       closegraph();
  45.    }
  46.    return 0;
  47. }
  48.  
  49. /* detects EGA or VGA cards */
  50. int huge detectVGA(void)
  51. {
  52.    #define ESC '\x1B'
  53.  
  54.    int driver, mode;
  55.    char numstr[2] = {NULL, NULL};
  56.    char ch;
  57.  
  58.    clrscr();
  59.  
  60.    while (kbhit())
  61.       ch = getch(); /* clear keyboard buffer */
  62.  
  63.    detectgraph(&driver, &mode);
  64.    switch (driver)
  65.    {
  66.       case VGA : cputs("9 : (800x600) SOTA VGA-16 16 color\r\n");
  67.                  cputs("8 : (800x600) Video7 16 color\r\n");
  68.                  cputs("7 : (800x600) Vega 16 color\r\n");
  69.                  cputs("6 : (800x600) Paradise 16 color\r\n");
  70.                  cputs("5 : (800x600) Orchid 16 color\r\n");
  71.                  cputs("4 : (800x600) ATI 16 color\r\n");
  72.                  cputs("3 : (640x480) VGA 16 color\r\n");
  73.       case EGA : cputs("2 : (640x350) EGA/VGA 16 color\r\n");
  74.                  cputs("1 : (640x200) EGA/VGA 16 color\r\n");
  75.                  cputs("0 : (320x200) EGA/VGA 16 color\r\n");
  76.                  cputs("------------------------------\r\n");
  77.                  break;
  78.       default  : return grError;
  79.    }
  80.    cputs("Which mode?, Esc to quit:");
  81.    ch = getch();
  82.    if (ch == ESC)
  83.       exit(0);
  84.    numstr[0] = ch;
  85.    mode = atoi(numstr);
  86.    if ( ((mode == 0) && (numstr[0] != '0')) || ((mode > 9) || (mode < 0)) )
  87.    {
  88.       cputs("\r\n\r\nError: Illegal mode value entered.\r\n");
  89.       cputs("Press any key to halt:");
  90.       getch();
  91.       exit(1);
  92.    }
  93.    else
  94.       return mode;
  95.    return 0;
  96. }
  97.  
  98. /* check for and report any graphics errors */
  99. void checkerrors(void)
  100. {
  101.    int errorcode;
  102.  
  103.    /* read result of last graphics operation */
  104.    errorcode = graphresult();
  105.    if (errorcode != grOk)  /* an error occurred */
  106.    {
  107.       printf("Graphics error: %s\n", grapherrormsg(errorcode));
  108.       printf("Press any key to halt:");
  109.       getch();
  110.       exit(1); /* terminate with an error code */
  111.    }
  112. }
  113.  
  114. /* wait for a keystroke, then clear the screen */
  115. void wait(void)
  116. {
  117.    getch();
  118.    cleardevice();
  119. }
  120.  
  121. /* demonstrate setting the foreground and background fill colors */
  122. void box_demo(void)
  123. {
  124.    #define SETBACK 6
  125.    int startx, starty, width, height, i, j, bcolor;
  126.    char style[8] = { 0x0, 0x10, 0x38, 0x7C, 0xFE, 0x7C, 0x38, 0x10 };
  127.  
  128.    rectangle(0, 0, getmaxx(), getmaxy());
  129.    width = getmaxx() / 48;
  130.    height = getmaxy() / 48;
  131.    starty = height;
  132.    bcolor = 0;
  133.    for (j=0; j<16; j++)
  134.    {
  135.       startx = width;
  136.       for (i=0; i<16; i++)
  137.       {
  138.          setfillpattern(style, i);
  139.          setwritemode(SETBACK+bcolor);
  140.          bar3d(startx, starty, startx+(2*width), starty+(2*height), 0, 0);
  141.          startx += (3*width);
  142.       }
  143.       starty += (3*height);
  144.       bcolor++;
  145.    }
  146.    settextstyle(DEFAULT_FONT, VERT_DIR, 1);
  147.    settextjustify(CENTER_TEXT, CENTER_TEXT);
  148.    outtextxy(getmaxx()-8, getmaxy()/2, "Press a key:");
  149.    wait();
  150.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  151. }
  152.  
  153. /* demonstrate the support for transparent fills */
  154. void cir_demo(void)
  155. {
  156.    #define NO_BACK_FILL 4
  157.    #define BACK_FILL_BLACK 6
  158.  
  159.    int width, height, xradius, yradius, xasp, yasp, i;
  160.  
  161.    width = getmaxx() / 6;
  162.    height = getmaxy() / 6;
  163.    xradius = getmaxx() / 5;
  164.    getaspectratio(&xasp, &yasp);
  165.    yradius = (int)(((long)xradius * xasp) / yasp);
  166.    for (i=0; i<2; i++)
  167.    {
  168.       rectangle(0, 0, getmaxx(), getmaxy());
  169.       if (i==0)
  170.          setwritemode(BACK_FILL_BLACK); /* fill pattern zeros in black */
  171.       else
  172.          setwritemode(NO_BACK_FILL);    /* don't fill pattern zeros */
  173.       setcolor(RED);
  174.       setfillstyle(LTSLASH_FILL, RED);
  175.       fillellipse(3*width, 2*height, xradius, yradius);
  176.       setcolor(YELLOW);
  177.       setfillstyle(LTBKSLASH_FILL, YELLOW);
  178.       fillellipse(2*width, 4*height, xradius, yradius);
  179.       setcolor(LIGHTBLUE);
  180.       setfillstyle(CLOSE_DOT_FILL, LIGHTBLUE);
  181.       fillellipse(4*width, 4*height, xradius, yradius);
  182.       setcolor(WHITE);
  183.       settextjustify(LEFT_TEXT, TOP_TEXT);
  184.       outtextxy(5, 10, "Press a key:");
  185.       wait();
  186.    }
  187. }
  188.  
  189. /* demonstrate the support for multiple display pages */
  190. void page_demo(void)
  191. {
  192.    int max_pages, midx, midy, page;
  193.    char *page_names[8] = { "Page: 0", "Page: 1", "Page: 2", "Page: 3",
  194.                            "Page: 4", "Page: 5", "Page: 6", "Page: 7" };
  195.  
  196.    switch(gmode)
  197.    {
  198.       case 0: max_pages = 8;  /* 320x200 */
  199.               break;
  200.       case 1: max_pages = 4;  /* 640x200 */
  201.               break;
  202.       case 2: max_pages = 2;  /* 640x350 */
  203.               break;
  204.       case 3:
  205.       case 4:
  206.       case 5:
  207.       case 6:
  208.       case 7:
  209.       case 8: max_pages = 1;  /* 640x480 and 800x600 */
  210.               break;
  211.    }
  212.    setcolor(WHITE);
  213.    midx = getmaxx() / 2;
  214.    midy = getmaxy() / 2;
  215.    settextjustify(CENTER_TEXT, CENTER_TEXT);
  216.    for (page=0; page<max_pages; page++)
  217.    {
  218.       setactivepage(page);
  219.       setfillstyle(SOLID_FILL, page+1);
  220.       bar3d(0, 0, getmaxx(), getmaxy(), 0, 0);
  221.       outtextxy(midx, midy, page_names[page]);
  222.       outtextxy(midx, midy+(2*textheight("M")), "Press a key:");
  223.    }
  224.    for (page=0; page<max_pages; page++)
  225.    {
  226.       setvisualpage(page);
  227.       getch();
  228.    }
  229.    cleardevice();
  230. }
  231.  
  232. /* demonstrate setting the foreground and background line colors */
  233. void line_demo(void)
  234. {
  235.    #define LINE_BACK 22
  236.    int x, maxx, maxy, width, fcolor, bcolor;
  237.  
  238.    maxx = getmaxx();
  239.    maxy = getmaxy();
  240.    width = maxx / 16;
  241.    setlinestyle(CENTER_LINE, 0, 1);
  242.    for (bcolor=0; bcolor<=15; bcolor++)
  243.    {
  244.       x = 0;
  245.       setwritemode(LINE_BACK+bcolor);
  246.       for (fcolor=0; fcolor<=15; fcolor++)
  247.       {
  248.          setcolor(fcolor);
  249.          line(x, 0, x, maxy);
  250.          x += width;
  251.       }
  252.       settextstyle(DEFAULT_FONT, VERT_DIR, 1);
  253.       settextjustify(CENTER_TEXT, CENTER_TEXT);
  254.       outtextxy(maxx-8, maxy/2, "Press a key:");
  255.       wait();
  256.    }
  257.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  258. }
  259.  
  260. /* demonstrate the support for transparent lines */
  261. void random_lines(void)
  262. {
  263.    #define NUM_LINES 100
  264.    #define NO_LINE_BACK 5
  265.  
  266.    int x1, y1, x2, y2, i, maxx, maxy;
  267.  
  268.    maxx = getmaxx();
  269.    maxy = getmaxy();
  270.    setwritemode(NO_LINE_BACK); /* toggle off the plotting of the */
  271.                                /* zeros in line patterns.        */
  272.    setlinestyle(DASHED_LINE, 0, 1);
  273.    for (i=0; i<NUM_LINES; i++)
  274.    {
  275.       x1 = random(maxx);
  276.       x2 = random(maxx);
  277.       y1 = random(maxy);
  278.       y2 = random(maxy);
  279.       setcolor(random(15)+1);
  280.       line(x1, y1, x2, y2);
  281.    }
  282.    wait();
  283. }
  284.