MAG - CGAZV5N1.ZIP - JOYAPPL.C

 
Output of file : JOYAPPL.C contained in archive : CGAZV5N1.ZIP

/******* Listing 5 *************** JOYAPPL.C *******
* A sample application using joysticks and sound
* Copyright as per Listing 1
******************************************************/

#include
#include
#include
#include
#include
#include

#ifndef MAKEFD
#include "joyappl.fd"
#endif

#include "joy.fd"
#include "sound.fd"
#include "game.h"

#define BASE_FREQ 6000 /* In Hertz */
#define DELTA_FREQ 500
#define MIN_FREQ 0
#define MAX_FREQ 15000

void ctrl_c_handler(void); /* turns sound off */

/* A musical joystick application. To link, use the following:

LINK joyappl joy sound joytime;

To run, use the following:
JOYAPPL to play musical joystick or
JOYAPPL BENCH to benchmark against your BIOS */

main(argc,argv)
int argc;
char **argv;
{

signal(SIGINT,ctrl_c_handler);
if (argc == 2)
{
if (stricmp(argv[1],"BENCH") == 0)
joy_bench();
}
else
joy_sound();

return 0;
}


void joy_sound(void) /* Musical joystick on Port "A" */
{
short joya_x; /* Game Port "A", X-Axis */
short joya_y; /* Game Port "A", Y-Axis */
short joyb_x; /* Game Port "B", X-Axis */
short joyb_y; /* Game Port "B", Y-Axis */
short fire;
short old_fire;
unsigned new_freq;
unsigned old_freq;
unsigned base_freq;

base_freq = BASE_FREQ;
printf
("Use game port \"A\": Buttons increase/decrease frequency\n");
printf
("Move joystick handle to vary frequency, press any key when done.\n");
sound_init();
sound_on();
while ( !kbhit() )
{
c_joystick(&joya_x, &joya_y, &joyb_x, &joyb_y, &fire);
if (! (joya_x && joya_y) )
{
printf("Joystick not found!\n");
break;
}

if (old_fire != fire) /* de-bounce */
{
old_fire = fire;
if ((base_freq > MIN_FREQ) &&
((fire & GAME_A_FIRE_1) == GAME_A_FIRE_1))
base_freq -= DELTA_FREQ;

if ((base_freq < MAX_FREQ) &&
((fire & GAME_A_FIRE_2) == GAME_A_FIRE_2))
base_freq += DELTA_FREQ;
}

new_freq =
(unsigned) (((float) ++joya_x / (float) ++joya_y) * base_freq);
if (new_freq != old_freq)
{
sound_freq(new_freq);
old_freq = new_freq;
}
}
sound_off();
}


void ctrl_c_handler(void)
{
printf("leaving via handler...\n");
sound_off();
exit(-1);
}


void joy_bench(void)
{
short joya_x; /* Game Port "A", X-Axis */
short joya_y; /* Game Port "A", Y-Axis */
short joyb_x; /* Game Port "B", X-Axis */
short joyb_y; /* Game Port "B", Y-Axis */
short fire;
int i;
time_t start_time;
time_t end_time;

printf("benchmarking BIOS...\n");
time(&start_time);
for (i=1; i<2000; i++)
_bios_joystick(&joya_x, &joya_y, &joyb_x, &joyb_y, &fire);
time(&end_time);
printf("elapsed time: %ld seconds\n",(long)(end_time-start_time));

printf("benchmarking custom \"C\" code...\n");
time(&start_time);
for (i=1; i<2000; i++)
c_joystick(&joya_x, &joya_y, &joyb_x, &joyb_y, &fire);
time(&end_time);
printf("elapsed time: %ld seconds\n",(long)(end_time-start_time));
}