home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / mgl11 / examples / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-10  |  10.0 KB  |  351 lines

  1. /****************************************************************************
  2. *
  3. *                        MegaGraph Graphics Library
  4. *
  5. *                   Copyright (C) 1994 SciTech Software.
  6. *                            All rights reserved.
  7. *
  8. * Filename:        $RCSfile: main.c $
  9. * Version:        $Revision: 1.2 $
  10. *
  11. * Language:        ANSI C
  12. * Environment:    IBM PC (MS DOS)
  13. *
  14. * Description:    Main program for the MegaGraph graphics library test
  15. *                programs. This contains a set of quick and dirty routines
  16. *                to set up the graphics environment in a number of different
  17. *                ways to test the library routines.
  18. *
  19. *                It depends upon the 'getopt.c' and 'getopt.h' files, from
  20. *                my ctools10.zip archive. They should be included in the
  21. *                distribution. It also requires the zen timer library which
  22. *                should also be included in the distribution.
  23. *
  24. * $Id: main.c 1.2 1994/03/10 09:25:52 kjb release $
  25. *
  26. ****************************************************************************/
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <time.h>
  31. #include <conio.h>
  32. #include <dos.h>
  33. #include <string.h>
  34. #include "getopt.h"
  35. #include "mgraph.h"
  36. #include "ztimer.h"
  37.  
  38. /*--------------------------- Global Variables ----------------------------*/
  39.  
  40. char    modename[80];                /* Name of active video mode        */
  41. bool    smallclip;                    /* Small clipping mode                */
  42.  
  43. /*------------------------- Implementation --------------------------------*/
  44.  
  45. void four_dots(void)
  46. /****************************************************************************
  47. *
  48. * Function:        four_dots
  49. *
  50. * Description:    Very simple routine to draw 4 dots, one at each corner
  51. *                of the display in the current drawing color.
  52. *
  53. ****************************************************************************/
  54. {
  55.     MGL_beginPixel();
  56.     MGL_pixelCoord(0,0);
  57.     MGL_pixelCoord(MGL_maxx(),0);
  58.     MGL_pixelCoord(MGL_maxx(),MGL_maxy());
  59.     MGL_pixelCoord(0,MGL_maxy());
  60.     MGL_endPixel();
  61. }
  62.  
  63. void ReportTime(ulong count)
  64. /****************************************************************************
  65. *
  66. * Function:        ReportTime
  67. * Parameters:    count    - Number of microseconds to count with.
  68. *
  69. * Description:    Simple routine to convert a count of microseconds into
  70. *                seconds and display it on the standard output device.
  71. *
  72. ****************************************************************************/
  73. {
  74.     ulong    secs;
  75.  
  76.     secs = count / 1000000L;
  77.     count = count - secs * 1000000L;
  78.     printf("Time taken: %lu.%06lu seconds\n",secs,count);
  79. }
  80.  
  81. void help(int driver,int *modeList)
  82. /****************************************************************************
  83. *
  84. * Function:        help
  85. * Parameters:    driver        - Device driver detected.
  86. *                chipID        - Device driver chip ID.
  87. *                dac            - Video DAC installed
  88. *                memory        - Amount of memory installed
  89. *                modeList    - List of valid video mode numbers.
  90. *
  91. * Description:    Provide command line usage information about the program,
  92. *                and how to modify the options.
  93. *
  94. ****************************************************************************/
  95. {
  96.     int        i;
  97.  
  98.     printf("Options are:\n");
  99.     printf("  -v        Map all output to a small viewport\n");
  100.     printf("  -f<style> Set the fill style (TRANS,OPAQUE or PIX)\n");
  101.     printf("  -h<size>  Set the pen height\n");
  102.     printf("  -w<size>  Set the pen width\n");
  103.     printf("  -x        Scan convert primitives in XOR mode\n");
  104.     printf("  -a        Scan convert primitives in AND mode\n");
  105.     printf("  -o        Scan convert primitives in OR mode\n");
  106.     printf("  -c        Turn clipping off\n");
  107.     printf("  -C        Clip to a small clip rectangle\n");
  108.     printf("  -p        Use page 1 if available\n");
  109.     printf("  -V        Use the non-acclerated VESA VBE drivers\n");
  110.     printf("  -E        Force EGA operation\n");
  111.     printf("  -P<value> Set the number of palette entries set at a time (default 100)\n");
  112.     printf("  -m\"mode\"  Set the video mode. Valid modes are:\n\n");
  113.  
  114.     i = 0;
  115.     while(modeList[i] != -1) {
  116.         printf("            \"%s\" - %d page\n",
  117.             MGL_modeName(modeList[i]),
  118.             MGL_availablePages(driver,modeList[i]));
  119.         i++;
  120.         }
  121.  
  122.     printf("\nVideo Card: %s\n",MGL_driverName(driver));
  123.     exit(1);
  124. }
  125.  
  126. void init_graphics(char *testname,int argc,char *argv[])
  127. /****************************************************************************
  128. *
  129. * Function:        init_graphics
  130. * Parameters:    testname    - Name of the test being run
  131. *                argc,argv    - Parameters to the main() routine.
  132. *
  133. * Description:    Parses the command line and starts the graphics environment
  134. *                using the specified options.
  135. *
  136. ****************************************************************************/
  137. {
  138.     int        i,driver,mode,err;
  139.     int        *modeList;
  140.     int        option;
  141.     int        writemode;
  142.     int        fillstyle;
  143.     bool    smallview;
  144.     bool    sep_page;
  145.     int        clipping;
  146.     int        height,width,snowlevel;
  147.     char    *argument;
  148.     rect    r;
  149.     pattern        pat = {0xF8, 0x74, 0x22, 0x47, 0x8F, 0x17, 0x22, 0x71};
  150.     pixpattern    pix = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08},
  151.                        {0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01},
  152.                        {0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02},
  153.                        {0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03},
  154.                        {0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03},
  155.                        {0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02},
  156.                        {0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01},
  157.                        {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}};
  158.  
  159.     printf("%s\n\n",testname);
  160.     driver = grDETECT;
  161.     writemode = REPLACE_MODE;
  162.     fillstyle = SOLID_PATTERN;
  163.     clipping = CLIPON;
  164.     smallclip = false;
  165.     smallview = false;
  166.     sep_page = false;
  167.     height = width = 1;
  168.     snowlevel = -1;
  169.  
  170. #ifdef    USE_LINKED
  171.     MGL_registerDriver(EGANAME,EGA_driver);
  172.     MGL_registerDriver(VGANAME,VGA_driver);
  173.     MGL_registerDriver(VGA256NAME,VGA256_driver);
  174.     MGL_registerDriver(VGAXNAME,VGAX_driver);
  175.     MGL_registerDriver(SVGA256NAME,SVGA256_driver);
  176.     MGL_registerDriver(SVGA32KNAME,SVGA32K_driver);
  177.     MGL_registerDriver(SVGA64KNAME,SVGA64K_driver);
  178.     MGL_registerDriver(SVGA16MNAME,SVGA16M_driver);
  179.     MGL_registerDriver(SVGAS3NAME,SVGAS3_driver);
  180. #endif
  181.  
  182.     MGL_detectGraph(&driver,&mode);
  183.     modeList = MGL_availableModes(driver);
  184.  
  185.     do {
  186.         option = getopt(argc,argv,"EIVvf:xaocCm:h:w:pP:",&argument);
  187.         switch(option) {
  188.             case 'V':
  189.                 MGL_detectGraph(&driver,&mode);
  190.                 if (driver > grSVGA)
  191.                     driver = grSVGA;
  192.                 modeList = MGL_availableModes(driver);
  193.                 break;
  194.             case 'E':
  195.                 driver = grEGA;
  196.                 mode = grEGA_640x350x16;
  197.                 break;
  198.             case 'v':
  199.                 smallview = true;
  200.                 printf("Mapping to small viewport ...\n");
  201.                 break;
  202.             case 'p':
  203.                 sep_page = true;
  204.                 printf("Using page 1 for all output if available ...\n");
  205.                 break;
  206.             case 'P':
  207.                 snowlevel = atoi(argument);
  208.                 break;
  209.             case 'f':
  210.                 strlwr(argument);
  211.                 if (strcmp(argument,"trans") == 0) {
  212.                     fillstyle = BITMAP_PATTERN_TRANSPARENT;
  213.                     printf("Using TRANSPARENT bitmap style ...\n");
  214.                     }
  215.                 else if (strcmp(argument,"opaque") == 0) {
  216.                     fillstyle = BITMAP_PATTERN_OPAQUE;
  217.                     printf("Using OPAQUE bitmap style ...\n");
  218.                     }
  219.                 else if (strcmp(argument,"pix") == 0) {
  220.                     fillstyle = PIXMAP_PATTERN;
  221.                     printf("Using PIXMAP bitmap style ...\n");
  222.                     }
  223.                 else {
  224.                     printf("invalid argument\a\n");
  225.                     exit(1);
  226.                     }
  227.                 break;
  228.             case 'x':
  229.                 writemode = XOR_MODE;
  230.                 printf("Using XOR write mode ...\n");
  231.                 break;
  232.             case 'a':
  233.                 writemode = AND_MODE;
  234.                 printf("Using AND write mode ...\n");
  235.                 break;
  236.             case 'o':
  237.                 writemode = OR_MODE;
  238.                 printf("Using OR write mode ...\n");
  239.                 break;
  240.             case 'c':
  241.                 clipping = CLIPOFF;
  242.                 printf("Turning clipping off ...\n");
  243.                 break;
  244.             case 'C':
  245.                 smallclip = true;
  246.                 printf("Clipping to a small rectangle ...\n");
  247.                 break;
  248.             case 'h':
  249.                 height = atoi(argument);
  250.                 printf("Setting pen height to %d ...\n",height);
  251.                 break;
  252.             case 'w':
  253.                 width = atoi(argument);
  254.                 printf("Setting pen width to %d ...\n",width);
  255.                 break;
  256.             case 'm':
  257.                 /* Look up the mode number given it's name */
  258.  
  259.                 i = 0;
  260.                 while (true) {
  261.                     if ((mode = modeList[i++]) == -1)
  262.                         break;
  263.                     if (strcmp(MGL_modeName(mode),argument) == 0)
  264.                         break;
  265.                     }
  266.                 if (mode == -1) {
  267.                     printf("Invalid video mode selected\n");
  268.                     exit(1);
  269.                     }
  270.                 break;
  271.             case PARAMETER:
  272.             case INVALID:
  273.                 help(driver,modeList);
  274.             }
  275.         } while (option != ALLDONE);
  276.  
  277.     printf("\nPress a key to start ...");
  278.     getch();
  279.  
  280. #ifdef    MGL_TEST
  281.     MGL_init(&driver,&mode,"");
  282. #else
  283.     MGL_init(&driver,&mode,"..\\");
  284. #endif
  285.     err = MGL_result();
  286.     if (err != grOK) {
  287.         printf("Graphics error: %s\n",MGL_errorMsg(err));
  288.         printf("Driver: %d, Mode: %d\n",driver,mode);
  289.         exit(1);
  290.         }
  291.     MGL_setWriteMode(writemode);
  292.     MGL_setPenStyle(fillstyle);
  293.     if (fillstyle == BITMAP_PATTERN_OPAQUE)
  294.         MGL_setBackColor(5);
  295.     MGL_setPenBitmapPattern(&pat);
  296.     MGL_setPenPixmapPattern(&pix);
  297.     MGL_setClipMode(clipping);
  298.     MGL_setPenSize(height,width);
  299.     if (sep_page) {
  300.         MGL_setActivePage(1);
  301.         MGL_setVisualPage(1);
  302.         MGL_clearDevice();
  303.         }
  304.     if (smallview) {
  305.         r.left = (MGL_maxx()+1)/4;
  306.         r.right = r.left * 3;
  307.         r.top = (MGL_maxy()+1)/4;
  308.         r.bottom = r.top * 3;
  309.  
  310.         r.left -= 2;
  311.         r.top -= 2;
  312.  
  313.         MGL_setColor(1);
  314.         MGL_insetRect(r,-1,-1);
  315.         MGL_rect(r);
  316.         MGL_insetRect(r,1,1);
  317.         MGL_setColor(MGL_maxColor());
  318.         MGL_setViewport(r);
  319.         }
  320.     if (smallclip) {
  321.         r.left = (MGL_maxx()+1)/4;
  322.         r.right = r.left * 3;
  323.         r.top = (MGL_maxy()+1)/4;
  324.         r.bottom = r.top * 3;
  325.         MGL_setColor(1);
  326.         MGL_insetRect(r,-1,-1);
  327.         MGL_rect(r);
  328.         MGL_insetRect(r,1,1);
  329.         MGL_setColor(MGL_maxColor());
  330.         MGL_setClipRect(r);
  331.         }
  332.     if (snowlevel != -1)
  333.         MGL_setPaletteSnowLevel(snowlevel);
  334.     strcpy(modename,MGL_modeName(mode));
  335. }
  336.  
  337. void exit_graphics(void)
  338. /****************************************************************************
  339. *
  340. * Function:        void exit_graphics(void)
  341. *
  342. * Description:    Pause for a key press before killing graphics modes, and
  343. *                then output the name of the video mode used.
  344. *
  345. ****************************************************************************/
  346. {
  347.     getch();
  348.     MGL_exit();
  349.     printf("Name of video mode used: \"%s\"\n",modename);
  350. }
  351.