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

  1. /**********************************************************************\
  2. *                                                                      *
  3. *  misc.c -- sound, music, editor, etc. -- things on the MISC menu     *
  4. *                                                                      *
  5. \**********************************************************************/
  6.  
  7. #include "defs.h"
  8. #include "birdmaps.h"
  9.  
  10. /**********************************************************************\
  11. *                                                                      *
  12. *  do_bird -- make a bitmapped bird fly across the screen              *
  13. *                                                                      *
  14. \**********************************************************************/
  15.  
  16. do_bird()
  17. {
  18.    int x;
  19.    int frame;
  20.    int color1,color2,color3;
  21.  
  22.    if (mode06 || mode11)
  23.    {
  24.       color1 = 0;
  25.       color2 = 1;
  26.       color3 = 1;
  27.    }
  28.    else if (mode14)
  29.    {
  30.       color1 = 4;
  31.       color2 = 15;
  32.       color3 = 15;
  33.    }
  34.    else if (mode16)
  35.    {
  36.       color1 = 0;
  37.       color2 = 4;
  38.       color3 = 12;
  39.    }
  40.  
  41.    fg_mousevis(0);
  42.    fg_transfer(0,xlimit,70,100,0,140,visual,hidden);
  43.  
  44.    frame = 0;
  45.    fg_setpage(hidden);
  46.    for (x = 0; x < 592; x+=16)
  47.    {
  48.       fg_transfer(0,xlimit,110,140,0,100,hidden,hidden);
  49.  
  50.       fg_setpage(hidden);
  51.       frame = !frame;
  52.       fg_move(x,100);
  53.       if (frame == 0)
  54.       {
  55.          fg_setcolor(color1);
  56.          fg_drawmap(bird10,6,31);
  57.          fg_setcolor(color2);
  58.          fg_drawmap(bird14,6,31);
  59.          fg_setcolor(color3);
  60.          fg_drawmap(bird112,6,31);
  61.        
  62.       }
  63.       else
  64.       {
  65.          fg_setcolor(color1);
  66.          fg_drawmap(bird20,5,20);
  67.          fg_setcolor(color2);
  68.          fg_drawmap(bird24,5,20);
  69.          fg_setcolor(color3);
  70.          fg_drawmap(bird212,5,20);
  71.       }
  72.       fg_setpage(visual);
  73.       fg_restore(0,xlimit,70,100);
  74.       fg_waitfor(3);
  75.    }
  76.    for (x = 592; x < 640; x+=16)
  77.    {
  78.       fg_transfer(576,639,110,140,0,100,hidden,hidden);
  79.  
  80.       fg_setpage(hidden);
  81.       frame = !frame;
  82.       fg_move(x-576,100);
  83.       if (frame == 0)
  84.       {
  85.          fg_setcolor(color1);
  86.          fg_drawmap(bird10,6,31);
  87.          fg_setcolor(color2);
  88.          fg_drawmap(bird14,6,31);
  89.          fg_setcolor(color3);
  90.          fg_drawmap(bird112,6,31);
  91.        
  92.       }
  93.       else
  94.       {
  95.          fg_setcolor(color1);
  96.          fg_drawmap(bird20,5,20);
  97.          fg_setcolor(color2);
  98.          fg_drawmap(bird24,5,20);
  99.          fg_setcolor(color3);
  100.          fg_drawmap(bird212,5,20);
  101.       }
  102.       fg_setpage(visual);
  103.       fg_transfer(0,63,70,100,576,100,hidden,visual);
  104.       fg_waitfor(3);
  105.    }
  106.  
  107.  
  108.    /* restore the background and foreground to what it was */
  109.  
  110.    fg_transfer(0,xlimit,110,140,0,100,hidden,hidden);
  111.    fg_restore(0,xlimit,70,100);
  112.    fg_transfer(0,xlimit,150,180,0,140,hidden,hidden);
  113.  
  114.    /* restore the screen and return to the menu */
  115.  
  116.    redraw = TRUE;
  117.    return(OK);
  118. }
  119.  
  120. /**********************************************************************\
  121. *                                                                      *
  122. *  do_editor -- call the editor                                        *
  123. *                                                                      *
  124. \**********************************************************************/
  125.  
  126. do_editor()
  127. {
  128.    char *array;
  129.    int array_len;
  130.  
  131.    /* allocate a string to pass to the editor function */
  132.  
  133.    array = calloc(10000,sizeof(char));
  134.    if (array == NULL)
  135.       return(ERR);
  136.  
  137.    /* define a black background */
  138.  
  139.    background = 0;
  140.  
  141.    /* clear the screen */
  142.  
  143.    fg_mousevis(OFF);
  144.    fg_restore(0,xlimit,menu_bottom,ylimit);
  145.  
  146.    /* draw the box */
  147.  
  148.    fg_setcolor(0);
  149.    fg_rect(144,487,50,180);
  150.    fg_setcolor(15);
  151.    fg_rect(144,487,181,195);
  152.    fg_setcolor(0);
  153.    fg_rect(144,487,196,196);
  154.    center_pstring("Press F1 for help, Esc to exit",150,490,195);
  155.  
  156.    /* call the editor function */
  157.  
  158.    fg_setcolor(15);
  159.    editor(array,148,485,53,180,20,&array_len);
  160.  
  161.    /* redraw the screen */
  162.  
  163.    draw_screen();
  164.    fg_restore(0,xlimit,0,ylimit);
  165.    highlight_option(4);
  166.    fg_mousevis(ON);
  167.  
  168.    /*******************************************************************
  169.  
  170.    This editor is included for demonstration purposes only, and does
  171.    not write anything out to a file.  If you were going to write the
  172.    text to a text file, you would probably want to do something like
  173.    this:
  174.  
  175.    index = 0;
  176.    for (i = 0; i < 20; i++)
  177.    {
  178.       nchar = strlen(&array[index]);
  179.       fprintf(stream,"%s\n",&ver_array[index]);
  180.       index+=nchar;
  181.       index+=2;
  182.  
  183.       if (index >= array_len)
  184.          break;
  185.    }
  186.  
  187.    Notice that the array is a collection of null terminated strings.
  188.    Be sure to free the array when you are done.
  189.  
  190.    *******************************************************************/
  191.  
  192.    free(array);
  193.    redraw = TRUE;
  194.  
  195.    return(OK);
  196. }
  197.  
  198. /**********************************************************************\
  199. *                                                                      *
  200. *  do_histogram -- create a histogram                                  *
  201. *                                                                      *
  202. \**********************************************************************/
  203.  
  204. do_histogram()
  205. {
  206.    register int i;
  207.    int x1,x2,y1,y2;
  208.    static char label[] = "Productivity";
  209.    static char title[] = "See profits soar with Fastgraph!";
  210.  
  211.    static int x[] = {130, 180, 230, 280, 330, 380, 430, 480};
  212.    static int y[] = {290, 275, 270, 265, 200, 170, 130, 100};
  213.  
  214.    /* clear the bottom of the screen */
  215.  
  216.    fg_mousevis(OFF);
  217.    fg_restore(0,xlimit,menu_bottom,ylimit);
  218.  
  219.    fg_setpage(visual);
  220.    fg_setcolor(0);
  221.  
  222.    y1 = scale(100);
  223.    y2 = scale(300);
  224.  
  225.    /* axes */
  226.  
  227.    fg_rect(120,520,y2,y2);
  228.    fg_rect(120,120,y1,y2);
  229.  
  230.    for (i = 100; i < 300; i+=20)
  231.    {
  232.       y1 = scale(i);
  233.       fg_rect(120,126,y1,y1);
  234.    }
  235.  
  236.    /* boxes */
  237.  
  238.    for (i = 0; i < 8; i++)
  239.    {
  240.       x1 = x[i];
  241.       x2 = x1 + 30;
  242.       y1 = scale(y[i]);
  243.  
  244.       fg_setcolor(11);
  245.       fg_rect(x1,x2,y1,y2);
  246.  
  247.       fg_setcolor(0);
  248.       fg_box(x1,x2,y1,y2);
  249.    }
  250.  
  251.    /* label the x axis */
  252.  
  253.    x1 = 320 - length_pstring(label)/2 - 2;
  254.    x2 = 320 + length_pstring(label)/2 + 2;
  255.    y2 = scale(320);
  256.    y1 = y2 - ptsize - 1;
  257.    if (mode06 || mode11)
  258.    {
  259.       fg_setcolor(15);
  260.       fg_rect(x1,x2,y1,y2);
  261.    }
  262.    fg_setcolor(0);
  263.    center_pstring(label,x1,x2,y2);
  264.  
  265.    /* Profits soar with Fastgraph */
  266.  
  267.    x1 = 320 - length_pstring(title)/2 - 2;
  268.    x2 = 320 + length_pstring(title)/2 + 2;
  269.    y2 = scale(90);
  270.    y1 = y2 - ptsize - 1;
  271.    if (mode06 || mode11)
  272.    {
  273.       fg_setcolor(15);
  274.       fg_rect(x1,x2,y1,y2);
  275.    }
  276.    fg_setcolor(0);
  277.    center_pstring(title,x1,x2,y2);
  278.  
  279.    /* wait for a keystroke or mouse button */
  280.  
  281.    fg_mousevis(ON);
  282.    wait_for_keystroke();
  283.  
  284.    /* restore the screen and return to the menu */
  285.  
  286.    fg_restore(0,xlimit,menu_bottom,ylimit);
  287.    redraw = TRUE;
  288.  
  289.    return(OK);
  290. }
  291.  
  292. /**********************************************************************\
  293. *                                                                      *
  294. *  do_joystick -- demo some joystick functions                         *
  295. *                                                                      *
  296. \**********************************************************************/
  297.  
  298. do_joystick()
  299. {
  300.    static char *string1[] = {
  301.    "Joystick",
  302.    "Joysticks found on Port 1 and Port 2."
  303.    };
  304.  
  305.    static char *string2[] = {
  306.    "Joystick",
  307.    "Joystick found on Port 1."
  308.    };
  309.  
  310.    static char *string3[] = {
  311.    "Joystick",
  312.    "Joystick found on Port 2."
  313.    };
  314.  
  315.    static char *string4[] = {
  316.    "Joystick",
  317.    "Joystick not found."
  318.    };
  319.  
  320.    static int joy1_x[] = {140,200,260,140,200,260,140,200,260};
  321.    static int joy2_x[] = {360,420,480,360,420,480,360,420,480};
  322.    static int joy_y[] =  {110,110,110,140,140,140,170,170,170};
  323.  
  324.    unsigned char key,aux;
  325.    int x,y;
  326.    int count,mousex,mousey;
  327.    int pos1,pos2,new_pos1,new_pos2;
  328.    int joystick[2];
  329.  
  330.    /* initialize joysticks, if possible */
  331.  
  332.    if (fg_initjoy(1) == 0)
  333.       joystick[0] = TRUE;
  334.    else
  335.       joystick[0] = FALSE;
  336.  
  337.    if (fg_initjoy(2) == 0)
  338.       joystick[1] = TRUE;
  339.    else
  340.       joystick[1] = FALSE;
  341.  
  342.    fg_mousevis(OFF);
  343.    fg_restore(0,xlimit,menu_bottom,ylimit);
  344.  
  345.    /* report status of joysticks */
  346.  
  347.    if (joystick[0] && joystick[1])
  348.       info_window(120,520,60,string1,2);
  349.    else if (joystick[0])
  350.       info_window(120,520,60,string2,2);
  351.    else if (joystick[1])
  352.       info_window(120,520,60,string3,2);
  353.    else
  354.       info_window(120,520,60,string4,2);
  355.  
  356.    /* draw a grid to illustrate joystick positions */
  357.  
  358.    fg_mousevis(OFF);
  359.    if (joystick[0])
  360.    {
  361.       fg_setcolor(15);
  362.       fg_rect(120,300,100,190);
  363.       fg_setcolor(0);
  364.       fg_box(120,300,100,190);
  365.       fg_box(180,240,100,190);
  366.       fg_box(120,300,130,160);
  367.    }
  368.  
  369.    if (joystick[1])
  370.    {
  371.       fg_setcolor(15);
  372.       fg_rect(340,520,100,190);
  373.       fg_setcolor(0);
  374.       fg_box(340,520,100,190);
  375.       fg_box(400,460,100,190);
  376.       fg_box(340,520,130,160);
  377.    }
  378.  
  379.    pos1 = 4; new_pos1 = pos1;
  380.    pos2 = 4; new_pos2 = pos2;
  381.  
  382.    /* draw red boxes to represent joystick straight up position */
  383.  
  384.    fg_setcolor(4);
  385.    if (joystick[0])
  386.    {
  387.       x = joy1_x[pos1];
  388.       y = joy_y[pos1];
  389.       fg_rect(x,x+20,y,y+10);
  390.    }
  391.    if (joystick[1])
  392.    {
  393.       x = joy2_x[pos2];
  394.       y = joy_y[pos2];
  395.       fg_rect(x,x+20,y,y+10);
  396.    }
  397.  
  398.    fg_mousevis(ON);
  399.    while (TRUE)
  400.    {
  401.       fg_waitfor(3);
  402.  
  403.       /* handle first joystick */
  404.  
  405.       if (joystick[0])
  406.       {
  407.          fg_intjoy(1,&key,&aux);
  408.          if (key == CR)
  409.             break;
  410.          if (aux == 0)
  411.             new_pos1 = 4;
  412.          else if (aux >= 71 && aux <= 73)
  413.             new_pos1 = aux - 71;
  414.          else if (aux >= 75 && aux <= 77)
  415.             new_pos1 = aux - 72;
  416.          else if (aux >= 79 && aux <= 81)
  417.             new_pos1 = aux - 73;
  418.  
  419.          if (new_pos1 != pos1)
  420.          {
  421.             fg_setcolor(15);
  422.             x = joy1_x[pos1];
  423.             y = joy_y[pos1];
  424.             fg_mousevis(OFF);
  425.             fg_rect(x,x+20,y,y+10);
  426.  
  427.             pos1 = new_pos1;
  428.             fg_setcolor(4);
  429.             x = joy1_x[pos1];
  430.             y = joy_y[pos1];
  431.             fg_rect(x,x+20,y,y+10);
  432.             fg_mousevis(ON);
  433.          }
  434.       }
  435.  
  436.       /* handle second joystick */
  437.  
  438.       if (joystick[1])
  439.       {
  440.          fg_intjoy(2,&key,&aux);
  441.          if (key == CR)
  442.             break;
  443.          if (aux == 0)
  444.             new_pos2 = 4;
  445.          else if (aux >= 71 && aux <= 73)
  446.             new_pos2 = aux - 71;
  447.          else if (aux >= 75 && aux <= 77)
  448.             new_pos2 = aux - 72;
  449.          else if (aux >= 79 && aux <= 81)
  450.             new_pos2 = aux - 73;
  451.  
  452.          if (new_pos2 != pos2)
  453.          {
  454.             fg_setcolor(15);
  455.             x = joy2_x[pos2];
  456.             y = joy_y[pos2];
  457.             fg_mousevis(OFF);
  458.             fg_rect(x,x+20,y,y+10);
  459.  
  460.             pos2 = new_pos2;
  461.             fg_setcolor(4);
  462.             x = joy2_x[pos2];
  463.             y = joy_y[pos2];
  464.             fg_rect(x,x+20,y,y+10);
  465.             fg_mousevis(ON);
  466.          }
  467.       }
  468.  
  469.       /* exit loop if key or mouse button pressed */
  470.  
  471.       fg_intkey(&key,&aux);
  472.       if (key+aux > 0)
  473.          break;
  474.  
  475.       fg_mousebut(1,&count,&mousex,&mousey);
  476.       if (count > 0)
  477.          break;
  478.    }
  479.  
  480.    /* restore the screen and return to the menu */
  481.  
  482.    fg_mousevis(OFF);
  483.    fg_restore(0,xlimit,menu_bottom,ylimit);
  484.  
  485.    redraw = TRUE;
  486.    return(OK);
  487. }
  488.  
  489. /**********************************************************************\
  490. *                                                                      *
  491. *  do_mouse -- change the mouse cursor                                 *
  492. *                                                                      *
  493. \**********************************************************************/
  494.  
  495. do_mouse()
  496. {
  497.    /* I used the MCD program from Rimrock Software to generate
  498.       the mouse cursor array. */
  499.  
  500.    static int fgcursor[] = {
  501.     0x0083,0x0001,0x0000,0x0018,0x0e18,0x0018,0x0018,0x001f,
  502.     0x0000,0x0e00,0x0e00,0x0e10,0x0e10,0x0e00,0x0e00,0x0f00,
  503.     0x0000,0x7e3c,0x7e66,0x60c2,0x60c2,0x60c2,0x7ec0,0x7ec0,
  504.     0x60c0,0x60ce,0x60ce,0x60c6,0x60c6,0x60e6,0x607e,0x0000};
  505.  
  506.    static char *string[] = {
  507.    "Mouse",
  508.    "Fastgraph has routines to poll the mouse,",
  509.    "move the mouse, turn the cursor off and on,",
  510.    "and even change the shape of the mouse cursor."
  511.    };
  512.  
  513.    static char *string1[] = {
  514.    "Mouse",
  515.    "Mouse Driver not found.",
  516.    };
  517.  
  518.    int lastx, lasty, buttons;
  519.  
  520.    /* clear the screen and display the info window */
  521.  
  522.    fg_mousevis(OFF);
  523.    fg_restore(0,xlimit,menu_bottom,ylimit);
  524.  
  525.    if (mouse)
  526.       info_window(120,520,60,string,4);
  527.    else
  528.       info_window(120,520,60,string1,2);
  529.  
  530.    /* if the mouse is present, change the mouse cursor */
  531.  
  532.    if (mouse)
  533.       fg_mouseptr(fgcursor,0,0);
  534.  
  535.    /* wait for a keystroke or mouse button */
  536.  
  537.    fg_mousevis(ON);
  538.    wait_for_keystroke();
  539.  
  540.    fg_mousevis(OFF);
  541.  
  542.    /* restore the default cursor */
  543.  
  544.    if (mouse)
  545.    {
  546.       fg_mousepos(&lastx,&lasty,&buttons);  /* get current position */
  547.       fg_mouseini();                        /* restore default cursor */
  548.       fg_mousemov(lastx,lasty);             /* move to last position */
  549.    }
  550.  
  551.    /* restore the screen and return to the menu */
  552.  
  553.    fg_restore(0,xlimit,menu_bottom,ylimit);
  554.    redraw = TRUE;
  555.  
  556.    return(OK);
  557. }
  558.  
  559. /**********************************************************************\
  560. *                                                                      *
  561. *  do_music -- play "Stranger in Paradise"                             *
  562. *                                                                      *
  563. \**********************************************************************/
  564.  
  565. do_music()
  566. {
  567.    static char music[] =
  568.    "T140O4L4GGO+L2DL8CDO-L4A#L8AGAA#L2O+C"
  569.    "L4DO-AL8GFL4DDL2GL4AGL8FE"
  570.    "L8FEL2DL4EFAGGO+L2D"
  571.    "L8CDO-L4A#L8AGAA#L2O+CL4DO-AL8GF"
  572.    "L4DDL2GL4AGL8FEL1F$";
  573.  
  574.    static char *string_suspend[] = {
  575.    "Stranger in Paradise",
  576.    "Press any key to suspend the music."
  577.    };
  578.  
  579.    static char *string_resume[] = {
  580.    "Stranger in Paradise",
  581.    "Press any key to resume the music."
  582.    };
  583.  
  584.    static char *string_stop[] = {
  585.    "Stranger in Paradise",
  586.    "Press any key to stop the music."
  587.    };
  588.  
  589.    /* start the music */
  590.  
  591.    fg_musicb(music,-1);
  592.  
  593.    /* clear the screen and display the info window */
  594.  
  595.    fg_mousevis(OFF);
  596.    fg_restore(0,xlimit,menu_bottom,ylimit);
  597.  
  598.    /* play music until key or mouse button is pressed */
  599.  
  600.    info_window(120,520,60,string_suspend,2);
  601.    fg_mousevis(ON);
  602.    wait_for_keystroke();
  603.    fg_suspend();
  604.  
  605.    /* wait for a key or mouse button to restart the music */
  606.  
  607.    info_window(120,520,60,string_resume,2);
  608.    fg_mousevis(ON);
  609.    wait_for_keystroke();
  610.    fg_resume();
  611.  
  612.    /* continue playing music until key or mouse button is pressed */
  613.  
  614.    info_window(120,520,60,string_stop,2);
  615.    fg_mousevis(ON);
  616.    wait_for_keystroke();
  617.    fg_hush();
  618.  
  619.    /* restore the screen and return to the menu */
  620.  
  621.    fg_mousevis(OFF);
  622.    fg_restore(0,xlimit,menu_bottom,ylimit);
  623.  
  624.    redraw = TRUE;
  625.    return(OK);
  626. }
  627.  
  628. /**********************************************************************\
  629. *                                                                      *
  630. *  do_sound -- an increasing pitch sound effect                        *
  631. *                                                                      *
  632. \**********************************************************************/
  633.  
  634. do_sound()
  635. {
  636.    register int i;
  637.  
  638.    static char *string[] = {
  639.    "Sound Effects",
  640.    "Creating sound effects with Fastgraph",
  641.    "is quite simple."
  642.    };
  643.  
  644.    /* clear the screen and display the info window */
  645.  
  646.    fg_mousevis(OFF);
  647.    fg_restore(0,xlimit,menu_bottom,ylimit);
  648.  
  649.    info_window(120,520,60,string,3);
  650.  
  651.    /* make the noise */
  652.  
  653.    for (i = 1000; i < 3000; i+=50)
  654.       fg_sound(i,1);
  655.  
  656.    /* wait for a keystroke or mouse button */
  657.  
  658.    fg_mousevis(ON);
  659.    wait_for_keystroke();
  660.  
  661.    /* restore the screen and return to the menu */
  662.  
  663.    fg_mousevis(OFF);
  664.    fg_restore(0,xlimit,menu_bottom,ylimit);
  665.  
  666.    redraw = TRUE;
  667.    return(OK);
  668. }
  669.