home *** CD-ROM | disk | FTP | other *** search
/ PC-SIG Library 8 / PC-SIG Library CD-ROM (8th Edition) (1990-04).iso / 2001_100 / disk2032 / bmdemo.c next >
Encoding:
C/C++ Source or Header  |  1990-01-01  |  13.1 KB  |  505 lines

  1. /*--------------------------------------------------------------
  2.  *  File:           BMDEMO.C
  3.  *  Description:    Demo the MPLUS Bar Menu.
  4.  *
  5.  *  Copyright (c) 1989  Michael Yam
  6.  *-------------------------------------------------------------*/
  7.  
  8. #include <stdio.h>
  9. #include <graph.h>
  10. #include <math.h>
  11.  
  12. #include "gplus.h"
  13. #include "gscreen.h"
  14. #include "mouser.h"
  15. #include "barmenu.h"
  16.  
  17. int dummy(), exitfun();
  18. int memfun();
  19. int mres(), hres(), eres(), vres();
  20. int info(), demo1(), demo2();
  21.  
  22. void sinplot();
  23. void setaxis();
  24.  
  25. char *bmtitle[] =
  26. {
  27.     "File",
  28.     "System",
  29.     "Video Mode",
  30.     "Help",
  31.     "",
  32. };
  33.  
  34. char *bftitle[] = 
  35. {
  36.     "Save", "Load", "DOS", "Exit to system", "",
  37.     "Memory", "",
  38.     "Medium Res 4 Color", "Hi Res Black & White", "EGA", "VGA", "",
  39.     "Info", "Demo 1", "Demo 2", "",
  40.     "",
  41. };
  42.  
  43. int (*funselect[])() =
  44. {
  45.     dummy, dummy, dummy, exitfun, NULL,
  46.     memfun, NULL,
  47.     mres, hres, eres, vres, NULL,
  48.     info, demo1, demo2, NULL,
  49.     NULL,
  50. };
  51.  
  52. extern struct videoconfig _videoconfig;
  53.  
  54. /*--------------------------------------------------------------
  55.  *  Function:       main
  56.  *  Description:    demos the bar menu.
  57.  *  Return value:   0 returned to parent process.
  58.  *--------------------------------------------------------------*/
  59. main()
  60. {
  61.     int ret;
  62.     int (*funptr)();
  63.     char ms_flag;
  64.  
  65.     /*      Try to set video mode to EGA
  66.      */
  67.     if( !setvideomode( _ERESCOLOR ) )
  68.         if( !setvideomode( _HRESBW ) )
  69.         {
  70.             printf("\nUnrecognized video hardware.\n");
  71.             exit(1);
  72.         }
  73.  
  74.     bm_init( 1, 1, bmtitle, bftitle, funselect ); 
  75.     bm_show( BLACK, CYAN, RED );
  76.  
  77.     ms_flag = ms_reset();
  78.     ms_setevent(1);
  79.     ms_showcursor();
  80.  
  81.     if( ms_flag == 0 )
  82.     {
  83.         gdialog( GDINFORM, GDOKAY );
  84.         gdwrite( "No mouse detected but keyboard is\n" );
  85.         gdwrite( "supported.  Press \"O\" to quit dialogue\n");
  86.         gdwrite( "box, then press ALT+first letter of\n");
  87.         gdwrite( "menu title.  See chapter 6.\n");
  88.         gdprompt();
  89.         gdclose();
  90.     }
  91.  
  92.     funptr = NULL;
  93.  
  94.     while( funptr != exitfun || ret != 0)
  95.     {
  96.         funptr = bm_run( BLUE, WHITE );
  97.         if ( funptr != NULL)
  98.             ret = funptr();             /* execute selected function */
  99.     }
  100.     setvideomode( _DEFAULTMODE );
  101.     ms_setevent(0);
  102.     return 0;
  103. }
  104. /*--------------------------------------------------------------
  105.  *  Function:       dummy
  106.  *  Description:    Dummy function to invoke from bar menu
  107.  *  Return value:   0
  108.  *--------------------------------------------------------------*/
  109. int dummy()
  110. {
  111.     gdialog( GDINFORM, GDOKAY );
  112.     gdwrite("Function not available.");
  113.     gdprompt();
  114.     gdclose();
  115.     return 0;
  116. }
  117. /*--------------------------------------------------------------
  118.  *  Function:       exitfun
  119.  *  Description:    quit this program
  120.  *  Return value:   0
  121.  *--------------------------------------------------------------*/
  122. int exitfun()
  123. {
  124.     int i;
  125.  
  126.     gdialog( GDWARN, GDYESNO );
  127.     gdwrite( "Quit bar menu demo?");
  128.     i = gdprompt();
  129.     gdclose();
  130.  
  131.     return i;
  132. }
  133. /*--------------------------------------------------------------
  134.  *  Function:       memfun
  135.  *  Description:    Display memory info to dialog box
  136.  *  Return value:   0
  137.  *--------------------------------------------------------------*/
  138. int memfun()
  139. {
  140.     char buffer[41];
  141.  
  142.     gdialog( GDINFORM, GDOKAY );
  143.  
  144.     sprintf( buffer, "Memory available: %u bytes\n", _memavl() );
  145.     gdwrite( buffer );
  146.     sprintf( buffer, "Max contiguous block: %u bytes\n", _memmax() );
  147.     gdwrite( buffer );
  148.  
  149.     gdprompt();
  150.     gdclose();
  151.  
  152.     return 0;
  153. }
  154. /*--------------------------------------------------------------
  155.  *  Function:       mres
  156.  *  Description:    Set the screen to medium resolution, 4 color
  157.  *  Return value:   0
  158.  *--------------------------------------------------------------*/
  159. int mres()
  160. {
  161.     char buffer[41];
  162.     int i;
  163.  
  164.     gdialog( GDINFORM, GDOKCAN );
  165.     gdwrite( "Reset mode to four color,\nmedium resolution?" );
  166.     i = gdprompt();
  167.     gdclose();
  168.  
  169.     if( i == 0 )
  170.     {
  171.         if( setvideomode( _MRES4COLOR ) == 0 )
  172.         {
  173.             gdialog( GDERROR, GDOKAY );
  174.             gdwrite( "Video mode not supported by hardware." );
  175.             gdprompt();
  176.             gdclose();
  177.         }
  178.         else
  179.         {
  180.             _selectpalette(1);
  181.             bm_init( 1, 1, bmtitle, bftitle, funselect );
  182.             bm_show( 1, 2, 3 );
  183.             ms_showcursor();
  184.         }
  185.     }
  186.     
  187.     return 0;
  188. }
  189. /*--------------------------------------------------------------
  190.  *  Function:       hres
  191.  *  Description:    Set the screen to black and white hi res
  192.  *  Return value:   0
  193.  *--------------------------------------------------------------*/
  194. int hres()
  195. {
  196.     char buffer[41];
  197.     int i;
  198.  
  199.     gdialog( GDINFORM, GDOKCAN );
  200.     gdwrite( "Reset mode to black and white,\nhigh resolution?" );
  201.     i = gdprompt();
  202.     gdclose();
  203.  
  204.     if( i == 0 )
  205.     {
  206.         if( setvideomode( _HRESBW ) == 0 )
  207.         {
  208.             gdialog( GDERROR, GDOKAY );
  209.             gdwrite( "Video mode not supported by hardware." );
  210.             gdprompt();
  211.             gdclose();
  212.         }
  213.         else
  214.         {
  215.             bm_init( 1, 1, bmtitle, bftitle, funselect );
  216.             bm_show( 0x00, 0x07, 0x07 );
  217.             ms_showcursor();
  218.         }
  219.     }
  220.     
  221.     return 0;
  222. }
  223. /*--------------------------------------------------------------
  224.  *  Function:       eres
  225.  *  Description:    Set the screen to ega, 16 color
  226.  *  Return value:   0
  227.  *--------------------------------------------------------------*/
  228. int eres()
  229. {
  230.     char buffer[41];
  231.     int i;
  232.  
  233.     gdialog( GDINFORM, GDOKCAN );
  234.     gdwrite( "Reset mode to ega color?" );
  235.     i = gdprompt();
  236.     gdclose();
  237.  
  238.     if( i == 0 )
  239.     {
  240.         /*      Microsoft is weird.  If we go from VGA to EGA, we get
  241.          *      43 line EGA.  Avoid this by going thru a graphics mode.
  242.          */
  243.         setvideomode( _MRES4COLOR );
  244.         if( setvideomode( _ERESCOLOR ) == 0 )
  245.         {
  246.             gdialog( GDERROR, GDOKAY );
  247.             gdwrite( "Video mode not supported by hardware." );
  248.             gdprompt();
  249.             gdclose();
  250.         }
  251.         else
  252.         {
  253.             bm_init( 1, 1, bmtitle, bftitle, funselect );
  254.             bm_show( BLACK, CYAN, RED );
  255.             ms_showcursor();
  256.         }
  257.     }
  258.     return 0;
  259. }
  260. /*--------------------------------------------------------------
  261.  *  Function:       vres
  262.  *  Description:    Set the screen to vga res 
  263.  *  Return value:   0
  264.  *--------------------------------------------------------------*/
  265. int vres()
  266. {
  267.     char buffer[41];
  268.     int i;
  269.  
  270.     gdialog( GDINFORM, GDOKCAN );
  271.     gdwrite( "Reset mode to vga color?" );
  272.     i = gdprompt();
  273.     gdclose();
  274.  
  275.     if( i == 0 )
  276.     {
  277.         if( setvideomode( _VRES16COLOR ) == 0 )
  278.         {
  279.             gdialog( GDERROR, GDOKAY );
  280.             gdwrite( "Video mode not supported by hardware." );
  281.             gdprompt();
  282.             gdclose();
  283.         }
  284.         else
  285.         {
  286.             bm_init( 1, 1, bmtitle, bftitle, funselect );
  287.             bm_show( BLACK, CYAN, RED );
  288.             ms_showcursor();
  289.         }
  290.     }
  291.     
  292.     return 0;
  293. }
  294. /*--------------------------------------------------------------
  295.  *  Function:       info
  296.  *  Description:    info function invoked from bar menu
  297.  *  Return value:   0
  298.  *--------------------------------------------------------------*/
  299. int info()
  300. {
  301.     int device;
  302.     int ch;
  303.     struct ms_status ms_status;
  304.     GWDW *gwptr;
  305.  
  306.     if( _videoconfig.numtextcols <= 40 )
  307.     {
  308.         gdialog( GDINFORM, GDOKAY );
  309.         gdwrite( "Please change to\n");
  310.         gdwrite( "hi-res mode to\n");
  311.         gdwrite( "view info." );
  312.         gdprompt();
  313.         gdclose();
  314.         return 0;
  315.     }
  316.  
  317.     gwptr = gwdwtopen( 5, 12, 17, 65, _GBORDER, BRIGHTWHITE, GREEN );
  318.     if( (char *)gwptr == NULL )
  319.     {
  320.         gdialog( GDWARN, GDOKAY );
  321.         gdwrite( "Insufficient memory.\n");
  322.         gdwrite( "Set video mode to B/W Hi-res and\n");
  323.         gdwrite( "try again." );
  324.         gdprompt();
  325.         gdclose();
  326.         return 0;
  327.     }
  328.     outtext("         The MPLUS Graphic Interface Library\n", LIGHTYELLOW, -1);
  329.     outtext("         Copyright 1989, 1990 by Michael Yam\n\n", LIGHTYELLOW, -1 );
  330.     outtext("MPLUS is a user supported program.  If you find this\n", BRIGHTWHITE, -1);
  331.     outtext("package useful, please register your copy by sending\n", BRIGHTWHITE, -1);
  332.     outtext("thirty five dollars ($35) to:\n\n", BRIGHTWHITE, -1 );
  333.     outtext("     Michael Yam\n", BRIGHTWHITE, -1 );
  334.     outtext("     230 East 88th St.  #6B\n", BRIGHTWHITE, -1 );
  335.     outtext("     New York, NY  10128\n\n", BRIGHTWHITE, -1 );
  336.     outtext("Thank you for your support!   ", BRIGHTWHITE, -1 );
  337.     outtext("Press a key...", BLACK, -1 );
  338.     while(1)
  339.     {
  340.         device = dev_ready( &ch, &ms_status );
  341.         if( device == _KB )
  342.         {
  343.             if( ch != 0 )
  344.                 break;
  345.         }
  346.         if( device == _MS )
  347.         {
  348.             if( ms_status.rbtn || ms_status.lbtn )
  349.                 break;
  350.         }
  351.     }
  352.     gwdwclose( gwptr );
  353.     return 0;
  354. }
  355. /*--------------------------------------------------------------
  356.  *  Function:       demo1
  357.  *  Description:    plot a sine wave
  358.  *  Return value:   0
  359.  *--------------------------------------------------------------*/
  360. int demo1()
  361. {
  362.     extern struct videoconfig _videoconfig;
  363.  
  364.     short fg, bg, highlite;
  365.     int ch;
  366.     int device;
  367.     double pi;
  368.     struct ms_status ms_status;
  369.     GWDW *gwptr1;
  370.     GWDW *gwptr2;
  371.  
  372.     /*      Although MPLUS is geared for EGA and VGA modes, try to
  373.      *      accomodate low res graphics.
  374.      */
  375.     if( _videoconfig.mode == _MRES4COLOR )
  376.     {
  377.         _selectpalette(1);
  378.         fg = 1;
  379.         bg = 2;
  380.         highlite = 3;
  381.     }
  382.     else if( _videoconfig.mode == _HRESBW )
  383.     {
  384.         fg = 7;
  385.         bg = 0;
  386.         highlite = 7;
  387.     }
  388.     else
  389.     {
  390.         fg = MAGENTA;
  391.         bg = LIGHTYELLOW;
  392.         highlite = BLUE;
  393.     }
  394.  
  395.     pi = 3.141592654;
  396.     gwptr1 = gwdwopen( 20, 20, 300, 150, _GBORDER, fg, bg );
  397.     _setcolor( highlite );
  398.     setaxis( gwptr1 );
  399.     sinplot( -2*pi, 2*pi, 280, 50 );
  400.  
  401.     gwptr2 = gwdwtopen( 22, 1, 24, 40, _GFILLINTERIOR, bg, fg );
  402.     outtext( "Press a key or mouse button...", highlite, -1 );
  403.  
  404.     while (1)
  405.     {
  406.         /*      Wait for keystroke or mouse buttons.
  407.          */
  408.         device = dev_ready( &ch, &ms_status );
  409.         if( device == _MS )
  410.         {
  411.             if( ms_status.lbtn || ms_status.rbtn )
  412.                 break;
  413.         }
  414.         else if( device == _KB )
  415.         {
  416.             if( ch != 0 )
  417.                 break;
  418.         }
  419.     }
  420.     gwdwclose( gwptr2 );
  421.     gwdwclose( gwptr1 );
  422.     return 0;
  423. }
  424. /*--------------------------------------------------------------
  425.  *  Function:       sinplot
  426.  *  Description:    plot a sine wave
  427.  *  Return value:   none
  428.  *--------------------------------------------------------------*/
  429. void sinplot( range1, range2, xpixels, ysf )
  430. double range1;
  431. double range2;
  432. short xpixels;                  /* x pixels available */
  433. int ysf;                        /* y scale factor */
  434. {
  435.     int i;
  436.     int cursor;
  437.     double numperxpix;
  438.     double xpixpernum;
  439.     double x, y;
  440.  
  441.     cursor = ms_cursor();
  442.     ms_hidecursor();
  443.     numperxpix = (fabs(range2-range1))/(double)xpixels;
  444.     xpixpernum = 1/numperxpix;
  445.  
  446.     /*      Calculate first point.  Position cursor with _moveto.
  447.      *      Adjust sign for y axis.
  448.      */
  449.     x = range1;
  450.     y = sin( x );
  451.     _moveto( range1/numperxpix, -( y*ysf ) );
  452.  
  453.     for( i=1; i<xpixels; ++i)
  454.     {
  455.         x += numperxpix;
  456.         y = sin( x );
  457.         _lineto( x * xpixpernum, -(y*ysf) );
  458.     }
  459.     if( cursor )
  460.         ms_showcursor();
  461. }
  462. /*--------------------------------------------------------------
  463.  *  Function:       setaxis
  464.  *  Description:    draw axis and set logical origin to center
  465.  *                  of screen.
  466.  *  Return value:   none
  467.  *--------------------------------------------------------------*/
  468. void setaxis( gwptr )
  469. GWDW *gwptr;
  470. {
  471.     int cursor;
  472.     short xctr, yctr;
  473.  
  474.     cursor = ms_cursor();
  475.     ms_hidecursor();
  476.     xctr = (gwptr->x2-gwptr->x1)/2;
  477.     yctr = (gwptr->y2-gwptr->y1)/2;
  478.  
  479.     _moveto( xctr, 0 );
  480.     _lineto( xctr, gwptr->y2-gwptr->y1 );
  481.     _moveto( 0, yctr );
  482.     _lineto( gwptr->x2-gwptr->x1, yctr );
  483.  
  484.     gwdwsetorg( gwptr, xctr, yctr );
  485.     if( cursor )
  486.         ms_showcursor();
  487. }
  488. /*--------------------------------------------------------------
  489.  *  Function:       demo2
  490.  *  Description:    
  491.  *  Return value:   0
  492.  *--------------------------------------------------------------*/
  493. int demo2()
  494. {
  495.     gdialog( GDINFORM, GDOKAY );
  496.     gdwrite("This is it.");
  497.     gdprompt();
  498.     gdclose();
  499.  
  500.     return 0;
  501. }
  502. /*-------------------------------------------------------------*
  503.  *                      End of BMDEMO.C                        *
  504.  *-------------------------------------------------------------*/
  505.