home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD1.bin
/
useful
/
dev
/
c
/
cmanual
/
devices
/
gameportdevice
/
example2.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-12
|
17KB
|
484 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM Devices Amiga C Club */
/* Chapter: Gameport Device Tulevagen 22 */
/* File: Example2.c 181 41 LIDINGO */
/* Author: Anders Bjerin SWEDEN */
/* Date: 92-04-27 */
/* Version: 1.00 */
/* */
/* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
/* */
/* Registered members may use this program freely in their */
/* own commercial/noncommercial programs/articles. */
/* */
/***********************************************************/
/* This program will read 20 joystick events, */
/* using port 2 (the "Joystick Port"), before */
/* it will terminate. This example is "nice" */
/* to the system and does everything according */
/* to the "rules". */
/* */
/* This example does not wait for something to */
/* happen, but is instead constantly trying to */
/* collect messages and can therefore continue */
/* do do something while the user is not using */
/* the Joystick. */
/* */
/* Note how this program terminates! */
#include <exec/types.h> /* UWORD, STRPTR */
#include <devices/gameport.h> /* GPCT_ABSJOYSTICK */
#include <devices/inputevent.h> /* struct InputEvent */
#define RIGHTGAMEPORT 1 /* The "Joystick" port. */
#define LEFTGAMEPORT 0 /* The "Mouse" port. */
/* Pointer to the Graphics library: */
struct GfxBase *GfxBase;
struct IOStdReq *game_io_msg; /* Pointer to our IOStdReq str. */
struct MsgPort *game_msg_port; /* Pointer to our message port. */
BOOL deviceerror; /* Have we opened the device, OK? */
/* Declare some external functions: */
/* (We must otherwise do so much casting.) */
extern struct MsgPort *CreatePort();
extern struct IOStdReq *CreateStdIO();
/* Declare all functions in this module: */
void main();
void PrintJoystickData(); /* Print some information about the joystick. */
BYTE SetControllerType(); /* Set type of controller. */
BYTE SetControllerTrigger(); /* Set trigger. */
void SetControllerRead(); /* Prepare it so we can read. */
BYTE GetControllerType(); /* Get type of controller already connected. */
void clean_up(); /* Cleanup, and quit. */
void main()
{
/* Put all data in this structure: */
struct InputEvent gamedata;
BYTE type;
int counter;
/* Open the Graphics library: */
GfxBase = (struct GfxBase *)
OpenLibrary( "graphics.library", 0 );
if( !GfxBase )
clean_up( "ERROR! Could not open the Graphics library!" );
/* 1. Create a message port so the system can communicate with us: */
game_msg_port = CreatePort( 0, 0 );
if( !game_msg_port )
clean_up( "ERROR! Could not create message port!" );
/* 2. Allocate and initialize a new I/O request block. */
/* It should use our new message port as reply port: */
game_io_msg = CreateStdIO( game_msg_port );
if( !game_io_msg )
clean_up( "ERROR! Could not allocate new I/O request block!" );
/* 3. Open the Game Port Device, use the right port: */
deviceerror = OpenDevice( "gameport.device", RIGHTGAMEPORT, game_io_msg, 0xFFFF );
if( deviceerror )
clean_up( "ERROR! Could not open the Game Port Device!" );
/* 4. Check if some other task is already using the gameport: */
if( type = GetControllerType() )
{
switch( type )
{
case GPCT_MOUSE:
printf( "A mouse is connected to the port!\n" );
break;
case GPCT_RELJOYSTICK:
printf( "A proportional joystick is connected to the port!\n" );
break;
case GPCT_ABSJOYSTICK:
printf( "A normal joystick is connected to the port!\n" );
break;
}
/* Do not close the device! If we do it, the other task will */
/* then not be able to use the gameport device either! */
deviceerror = TRUE;
clean_up( "ERROR! Some other task is already using the Gameport!" );
}
/* 5. Set device type (absolute joystick): */
if( SetControllerType( GPCT_ABSJOYSTICK ) )
clean_up( "ERROR! Could not set device type!" );
/* 6. Set device trigger: */
if( SetControllerTrigger
( /* Report following events: */
GPTF_DOWNKEYS|GPTF_UPKEYS, /* firebutton pressed and release, */
0, /* no timeout messages, */
1, /* every X movements, and */
1 /* every Y movements. */
)
)
clean_up( "ERROR! Could not set device trigger!" );
/* 7. Prepare the device to read: */
SetControllerRead( &gamedata );
/* 8. Do our request, and return without delay: */
SendIO( game_io_msg );
printf( "Gameport Device - Joystick Example - Action\n" );
/* Collect 20 messages, and the leave: */
counter = 0;
while( counter < 20 )
{
printf("Working...\n");
/* Try to collect a message: */
if( GetMsg( game_msg_port ) )
{
/* Print some information abut the joystick: */
PrintJoystickData( &gamedata );
counter++;
/* We have now collected a joystick event, so prepare the */
/* gameport device again: */
SendIO( game_io_msg );
}
}
/* 9. NOTE! In this example we will have done one more SendIO() */
/* than GetMsg(), so before we quit we must try to cancel our */
/* last command: */
AbortIO( game_io_msg );
/* We must also make sure that there are no messages waiting at */
/* our message port, so we try to collect as many messages as */
/* possible, until we can not collect any more: */
while( GetMsg( game_msg_port ) )
printf( "Collecting remaining messages...\n" );
/* 10. Clean up nicely, and quit: */
clean_up( "The End!" );
}
/****************************************************************/
/* */
/* PrintJoystickData() prints current stick and button position */
/* of a Joystick. */
/* */
/* Synopsis: PrintJoystickData( data ); */
/* */
/* data: (struct InputEvent *) Pointer to an InputEvent */
/* structure which has previously been initialized. */
/* */
/****************************************************************/
void PrintJoystickData( data )
struct InputEvent *data;
{
WORD xdirection;
WORD ydirection;
UWORD code;
/* Collect data: */
xdirection = data->ie_X;
ydirection = data->ie_Y;
code = data->ie_Code;
/* Was the button pressed or released? */
if( code == IECODE_LBUTTON )
printf("Button pressed. ");
if( code == IECODE_LBUTTON + IECODE_UP_PREFIX )
printf("Button released. ");
/* What is the position of the stick: */
printf( "Stick position:" );
switch(ydirection)
{
case -1:
printf( " Forward" );
break;
case 1:
printf( " Back" );
break;
}
switch(xdirection)
{
case -1:
printf( " Left" );
break;
case 1:
printf( " Right" );
break;
}
printf( "\n" );