home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / bcpp / file10 / fgdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  13.6 KB  |  490 lines

  1. /**********************************************************************\
  2. *                                                                      *
  3. *  main.c -- Fastgraph (tm) and Fastgraph/Light (tm) demo program v1.1 *
  4. *                                                                      *
  5. *  This program is provided without warranty of any kind.  It may      *
  6. *  be distributed freely as long as the files are not modified.  You   *
  7. *  may use this source code in your own applications as long as it     *
  8. *  clearly understood that we are not responsible for any bugs in      *
  9. *  this code or in your code.  Use this code freely, use it in good    *
  10. *  health, but use it at your own risk.  Further disclaimers are       *
  11. *  listed at the end of this file.                                     *
  12. *                                                                      *
  13. *  This program requires Fastgraph v2.10 or Fastgraph/Light v1.10 to   *
  14. *  link.  Please call Ted Gruber Software at (702) 735-1980 for more   *
  15. *  information about Fastgraph.                                        *
  16. *                                                                      *
  17. \**********************************************************************/
  18.  
  19. #include "defs.h"
  20.  
  21. int  menu_top;
  22. int  menu_bottom;
  23.  
  24. char matrix1[] = {0xAA,0x55,0xAA,0x55};
  25. char matrix2[] = {0x24,0x92,0x24,0x92};
  26.  
  27. /**********************************************************************\
  28. *                                                                      *
  29. *                                 main                                 *
  30. *                                                                      *
  31. \**********************************************************************/
  32.  
  33. void main()
  34. {
  35.    unsigned char key, aux;
  36.    int mousex, mousey, count;
  37.    register int i;
  38.    int current;
  39.  
  40.    /* initialize the video environment */
  41.  
  42.    initialize();
  43.    fg_mousevis(OFF);
  44.  
  45.    /* read the 8pt and 14pt fonts */
  46.  
  47.    get_font();
  48.  
  49.    /* draw the screen on the hidden page and copy it to the visual page */
  50.  
  51.    draw_screen();
  52.    fg_restore(0,xlimit,0,ylimit);
  53.  
  54.    /* begin the main program loop */
  55.  
  56.    current = 0;
  57.    while (TRUE)
  58.    {
  59.       fg_mousevis(ON);
  60.       fg_waitfor(2);
  61.  
  62.       /* intercept a keystroke */
  63.  
  64.       fg_intkey(&key,&aux);
  65.       if (key == ESC)
  66.       {
  67.          exit_program();
  68.          continue;
  69.       }
  70.       else if (aux == RIGHT_ARROW)
  71.       {
  72.          current++;
  73.          if (current == ITEMS)
  74.             current = 0;
  75.       }
  76.       else if (aux == LEFT_ARROW)
  77.       {
  78.          current--;
  79.          if (current < 0)
  80.             current = ITEMS - 1;
  81.       }
  82.  
  83.       /* display menu according to keystroke pressed */
  84.  
  85.       if (key+aux > 0)
  86.       {
  87.          fg_mousevis(OFF);
  88.          current = horizontal_menu(main_menu,6,current,0,15,15,0);
  89.          if (current == ESC)
  90.          {
  91.             exit_program();
  92.             current = 0;
  93.          }
  94.          fg_mousevis(OFF);
  95.          fg_restore(0,xlimit,menu_top,ylimit);
  96.          fg_mousevis(ON);
  97.       }
  98.  
  99.       /* intercept a mouse click */
  100.  
  101.       fg_mousebut(1,&count,&mousex,&mousey);
  102.       if (count > 0 && BETWEEN(mousey,menu_top,menu_bottom))
  103.       {
  104.          for (i = 0; i <= ITEMS; i++)
  105.          {
  106.             if (BETWEEN(mousex, mouse_limits[i], mouse_limits[i+1]))
  107.             {
  108.                if (horizontal_menu(main_menu,6,i,0,15,15,0) == ESC)
  109.                   exit_program();
  110.                fg_mousevis(OFF);
  111.                fg_restore(0,xlimit,menu_top,ylimit);
  112.                fg_mousevis(ON);
  113.                break;
  114.             }
  115.          }
  116.       }
  117.    }
  118. }
  119.  
  120. /**********************************************************************\
  121. *                                                                      *
  122. *  draw_screen -- draw the main menu screen on the hidden page         *
  123. *                                                                      *
  124. \**********************************************************************/
  125.  
  126. void draw_screen()
  127. {
  128.    int y1, y2, y3;
  129.  
  130.    /* calculate y coordinates based on screen resolution */
  131.  
  132.    if (mode11 || mode16)
  133.    {
  134.       y1 = 3; y2 = 21; y3 = 38;
  135.    }
  136.    else
  137.    {
  138.       y1 = 1; y2 = 12; y3 = 23;
  139.    }
  140.  
  141.    menu_top = y2 + 1;
  142.    menu_bottom  = y3 + 1;
  143.  
  144.    /* erase the hidden page */
  145.  
  146.    background = 15;
  147.    fg_setpage(hidden);
  148.    fg_erase();
  149.  
  150.    /* draw some rectangles */
  151.  
  152.    fg_setcolor(15);
  153.    fg_rect(0,xlimit,0,ylimit);
  154.  
  155.    fg_setcolor(0);
  156.    fg_rect(0,xlimit,0,y2);
  157.  
  158.    /* draw the large dithered rectangles */
  159.  
  160.    if (mode06 || mode11)
  161.       fg_drect(0,xlimit,y3+1,ylimit,matrix1);
  162.    else
  163.    {
  164.       fg_setcolor(9);
  165.       fg_drect(0,xlimit,0,y2-1,matrix1);
  166.       fg_setcolor(14);
  167.       fg_drect(0,xlimit,y3+1,ylimit,matrix2);
  168.    }
  169.  
  170.    /* put the title in the title bar */
  171.  
  172.    fg_setcolor(15);
  173.    center_pstring("Fastgraph Demo",0,xlimit,y2-1);
  174.  
  175.    fg_setcolor(8);
  176.    fg_box(0,xlimit,0,ylimit);
  177.  
  178.    /* draw some black and grey outlines */
  179.  
  180.    fg_setcolor(7);
  181.    fg_boxdepth(scale(2),2);
  182.    fg_box(1,xlimit-1,scale(1),ylimit-scale(1));
  183.    fg_boxdepth(1,1);
  184.  
  185.    fg_setcolor(0);
  186.    fg_box(3,xlimit-3,y1,y3);
  187.    fg_box(3,xlimit-3,y1,ylimit-y1);
  188.    fg_box(4,xlimit-4,y1,ylimit-y1);
  189.  
  190.    /* draw those little boxes in the corners */
  191.  
  192.    if (mode06 || mode11)
  193.    {
  194.       fg_drect(5,25,y1+1,y2-1,matrix1);
  195.       fg_drect(xlimit-25,xlimit-5,y1+1,y2-1,matrix1);
  196.    }
  197.    else
  198.    {
  199.       fg_setcolor(7);
  200.       fg_rect(5,25,y1+1,y2-1);
  201.       fg_rect(xlimit-25,xlimit-5,y1+1,y2-1);
  202.    }
  203.  
  204.    /* display the horizontal menu */
  205.  
  206.    horizontal_menu(main_menu,-6,0,0,15,15,0);
  207.    fg_setpage(visual);
  208. }
  209.  
  210. /**********************************************************************\
  211. *                                                                      *
  212. *  draw_window -- display a pop-up window                              *
  213. *                                                                      *
  214. \**********************************************************************/
  215.  
  216. void draw_window(xmin,xmax,ymin,ymax,string)
  217. int xmin, xmax, ymin, ymax;
  218. char *string;
  219. {
  220.    register int y;
  221.  
  222.    /* display the window and its border */
  223.  
  224.    fg_setcolor(15);
  225.    fg_rect(xmin,xmax,ymin,ymax);
  226.    fg_setcolor(0);
  227.    fg_boxdepth(1,2);
  228.    fg_box(xmin,xmax,ymin,ymax);
  229.    fg_boxdepth(1,1);
  230.  
  231.    /* display the header bar at the top of the window */
  232.  
  233.    y = ymin + ptsize + 1;
  234.    fg_rect(xmin,xmax,ymin,y+1);
  235.  
  236.    /* dither the header bar for 16 color modes */
  237.  
  238.    if (mode14 || mode16)
  239.    {
  240.       fg_setcolor(7);
  241.       fg_rect(xmin+2,xmin+22,ymin+1,y);
  242.       fg_rect(xmax-22,xmax-2,ymin+1,y);
  243.       fg_setcolor(9);
  244.       fg_drect(xmin+23,xmax-23,ymin+1,y,matrix1);
  245.    }
  246.  
  247.    /* dither the header bar for 2 color modes */
  248.  
  249.    else
  250.    {
  251.       fg_drect(xmin+2,xmin+22,ymin+1,y,matrix1);
  252.       fg_drect(xmax-22,xmax-2,ymin+1,y,matrix1);
  253.    }
  254.  
  255.    /* display the text in the window's header bar */
  256.  
  257.    fg_setcolor(15);
  258.    center_pstring(string,xmin,xmax,y+1);
  259.  
  260.    /* display the window's shadow */
  261.  
  262.    if (mode14 || mode16)
  263.       fg_setcolor(8);
  264.    else
  265.       fg_setcolor(0);
  266.  
  267.    fg_rect(xmin+3,xmax+3,ymax+1,ymax+scale(2));
  268.    fg_rect(xmax+1,xmax+3,ymin+scale(2),ymax);
  269. }
  270.  
  271. /**********************************************************************\
  272. *                                                                      *
  273. *  erase_window -- erase the previously displayed pop-up window        *
  274. *                                                                      *
  275. \**********************************************************************/
  276.  
  277. void erase_window(xmin,xmax,ymin,ymax)
  278. int xmin, xmax, ymin, ymax;
  279. {
  280.    register int x1, x2;
  281.  
  282.    /* restore the original area underneath the window */
  283.  
  284.    x1 = xmin & 0xFFF8;
  285.    x2 = ((xmax+3) & 0xFFF8) + 7;
  286.    fg_mousevis(OFF);
  287.    fg_restore(x1,x2,ymin,ymax+2);
  288.  
  289.    /* redraw the dither pattern on the hidden page */
  290.  
  291.    fg_setpage(hidden);
  292.    if (mode06 || mode11)
  293.       fg_drect(x1,x2,ymin,ymax+2,matrix1);
  294.    else
  295.    {
  296.       fg_setcolor(15);
  297.       fg_rect(x1,x2,ymin,ymax+2);
  298.       fg_setcolor(14);
  299.       fg_drect(x1,x2,ymin,ymax+2,matrix2);
  300.    }
  301.    fg_setpage(visual);
  302. }
  303.  
  304. /**********************************************************************\
  305. *                                                                      *
  306. *  get_answer -- accept a YES or NO answer from the keyboard or mouse  *
  307. *                                                                      *
  308. \**********************************************************************/
  309.  
  310. get_answer(xmin,xmax,y)
  311. int xmin, xmax, y;
  312. {
  313.    int color;
  314.    int count, lastx, lasty;
  315.    int return_value;
  316.    int x;
  317.    int x1, x2, y1, y2;
  318.    char yes_no[4];
  319.    unsigned char key, aux;
  320.  
  321.    /* if using a mouse, display the "Yes" and "No" buttons */
  322.  
  323.    if (mouse)
  324.    {
  325.       color = fg_getcolor();
  326.       x1 = ((3 * xmin) + xmax) / 4;
  327.       x2 = ((3 * xmax) + xmin) / 4;
  328.       y1 = y - ptsize/2 - 1;
  329.       y2 = y + ptsize/2 + 1;
  330.       fg_setcolor(0);
  331.       fg_boxdepth(1,2);
  332.       fg_box(x1-25,x1+25,y1,y2);
  333.       fg_box(x2-25,x2+25,y1,y2);
  334.       fg_boxdepth(1,1);
  335.       center_pstring("Yes",x1,x1,y2);
  336.       center_pstring("No",x2,x2,y2);
  337.       fg_setcolor(8);
  338.       fg_rect(x1-23,x1+27,y2+1,y2+scale(2));
  339.       fg_rect(x1+26,x1+27,y1+scale(2),y2);
  340.       fg_rect(x2-23,x2+27,y2+1,y2+scale(2));
  341.       fg_rect(x2+26,x2+27,y1+scale(2),y2);
  342.       fg_mousevis(ON);
  343.    }
  344.  
  345.    /* read keystrokes until Y, y, N, n, or Esc is pressed */
  346.  
  347.    flushkey();
  348.    while (TRUE)
  349.    {
  350.       fg_waitfor(2);
  351.       fg_intkey(&key,&aux);
  352.       key = (unsigned char)tolower(key);
  353.  
  354.       if (mouse)
  355.       {
  356.          fg_mousebut(1,&count,&lastx,&lasty);
  357.          if (BETWEEN(lastx,x1-25,x1+25) && BETWEEN(lasty,y1,y2))
  358.             key = 'y';
  359.          else if (BETWEEN(lastx,x2-25,x2+25) && BETWEEN(lasty,y1,y2))
  360.             key = 'n';
  361.       }
  362.  
  363.       if (key == 'y')
  364.       {
  365.          x = x1;
  366.          strcpy(yes_no,"Yes");
  367.          return_value = TRUE;
  368.          break;
  369.       }
  370.       else if (key == 'n' || key == ESC)
  371.       {
  372.          x = x2;
  373.          strcpy(yes_no,"No");
  374.          return_value = FALSE;
  375.          break;
  376.       }
  377.    }
  378.  
  379.    /* if using the mouse, flash the selected button */
  380.  
  381.    if (mouse)
  382.    {
  383.       fg_mousevis(OFF);
  384.       fg_setcolor(0);
  385.       fg_rect(x-23,x+23,y1+1,y2-1);
  386.       fg_setcolor(15);
  387.       center_pstring(yes_no,x,x,y2);
  388.       fg_waitfor(2);
  389.       fg_setcolor(15);
  390.       fg_rect(x-23,x+23,y1+1,y2-1);
  391.       fg_setcolor(0);
  392.       center_pstring(yes_no,x,x,y2);
  393.       fg_waitfor(2);
  394.       fg_setcolor(color);
  395.    }
  396.  
  397.    return(return_value);
  398. }
  399.  
  400. /**********************************************************************\
  401. *                                                                      *
  402. *  info_window -- display text information in a window                 *
  403. *                                                                      *
  404. \**********************************************************************/
  405.  
  406. void info_window(xmin,xmax,ymin,string,nstrings)
  407. int xmin,xmax,ymin;
  408. char *string[];
  409. int nstrings;
  410. {
  411.    register int i;
  412.    int x, y;
  413.    int miny, maxy;
  414.    int len, maxlen;
  415.    int ptsize1;
  416.  
  417.    /* center text in window according to length of longest string */
  418.  
  419.    ptsize1 = ptsize + 1;
  420.    maxlen = 0;
  421.    for (i = 1; i < nstrings; i++)
  422.    {
  423.       len = length_pstring(string[i]);
  424.       if (len > maxlen) maxlen = len;
  425.    }
  426.  
  427.    /* calculate top and bottom of info window */
  428.  
  429.    miny = scale(ymin);
  430.    maxy = miny + (nstrings+1) * (ptsize1);
  431.  
  432.    /* draw the info window on the hidden page first */
  433.  
  434.    fg_setpage(hidden);
  435.  
  436.    /* draw the window and put the first string in the header bar */
  437.  
  438.    draw_window(xmin,xmax,miny,maxy,string[0]);
  439.  
  440.    /* calculate starting point for text */
  441.  
  442.    x = xmin + ((xmax - xmin) - maxlen) / 2;
  443.    y = miny + ((ptsize+1)*2) + ptsize/2;
  444.  
  445.    /* put all the strings in the rest of the window */
  446.  
  447.    fg_setcolor(0);
  448.    for (i = 1; i < nstrings; i++)
  449.    {
  450.       put_pstring(string[i],x,y);
  451.       y += ptsize1;
  452.    }
  453.  
  454.    /* restore the window to the visual page */
  455.  
  456.    fg_setpage(visual);
  457.    fg_restore(xmin,xmax+3,miny,maxy+2);
  458.  
  459.    /* redraw the dither pattern on the hidden page */
  460.  
  461.    fg_setpage(hidden);
  462.    if (mode06 || mode11)
  463.       fg_drect(xmin,xmax+3,miny,maxy+2,matrix1);
  464.    else
  465.    {
  466.       fg_setcolor(15);
  467.       fg_rect(xmin,xmax+3,miny,maxy+2);
  468.       fg_setcolor(14);
  469.       fg_drect(xmin,xmax+3,miny,maxy+2,matrix2);
  470.    }
  471.    fg_setpage(visual);
  472.  
  473. }
  474.  
  475. /**********************************************************************\
  476. *                                                                      *
  477. *                         Further Disclaimers                          *
  478. *                                                                      *
  479. * Under no circumstances shall Ted Gruber Software be liable for any   *
  480. * loss of profit or any other commercial damage, including but not     *
  481. * limited to special, incidental, consequential, or other damages      *
  482. * resulting from the use of or the inability to use the software,      *
  483. * even if Ted Gruber Software has been notified of the possiblity      *
  484. * of such damages. Ted Gruber Software disclaims all other warranties, *
  485. * either expressed or implied, regarding the merchantability or        *
  486. * fitness of this software and accompanying documentation for any      *
  487. * particular application or purpose.                                   *
  488. *                                                                      *
  489. \**********************************************************************/
  490.