Show exjoy.c syntax highlighted
/*
* Example program for the Allegro library, by Grzegorz Adam Hankiewicz
*
* This program uses the Allegro library to detect and read the value
* of a joystick. The output of the program is a small target sight
* on the al_screen which you can move. At the same time the program will
* tell you what you are doing with the joystick (moving or firing).
*/
#include "allegro.h"
int main()
{
AL_BITMAP *bmp; /* we create a pointer to a virtual al_screen */
int x=160, y=100; /* these will be used to show the target sight */
int analogmode;
AL_CONST char *msg;
int c;
allegro_init(); /* you NEED this man! ;-) */
al_install_keyboard(); /* ahh... read the docs. I will explain only
* joystick specific routines
*/
if (al_set_gfx_mode(AL_GFX_SAFE, 320, 200, 0, 0) != 0) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Unable to set any graphic mode\n%s\n", al_error);
return 1;
}
al_set_palette(al_default_palette);
al_clear_bitmap(al_screen);
al_put_text_centre(al_screen, al_font_8x8, "Please center the joystick", AL_SCREEN_W/2,
AL_SCREEN_H/2 - 36, al_palette_color[255]);
al_put_text_centre(al_screen, al_font_8x8, "and press a al_key.", AL_SCREEN_W/2,
AL_SCREEN_H/2 - 20, al_palette_color[255]);
if ((al_read_key()&0xFF) == 27)
return 0;
/* the first thing is to initialise the joystick driver */
if (al_install_joystick(JOY_TYPE_AUTODETECT) != 0) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Error initialising joystick\n%s\n", al_error);
return 1;
}
/* make sure that we really do have a joystick */
if (!al_num_joysticks) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Error: joystick not found\n");
return 1;
}
/* before using the joystick, we have to calibrate it. This loop only
* calibrates joystick number 0, but you could do the same thing for
* other sticks if they are present (the al_num_joysticks variable will
* tell you how many there are).
*/
while (joy[0].flags & JOYFLAG_CALIBRATE) {
msg = al_calibrate_joystick_text(0);
al_clear_bitmap(al_screen);
al_put_text_centre(al_screen, al_font_8x8, msg, AL_SCREEN_W/2, 64, al_palette_color[255]);
al_put_text_centre(al_screen, al_font_8x8, "and press a al_key.", AL_SCREEN_W/2, 80, al_palette_color[255]);
if ((al_read_key()&0xFF) == 27)
return 0;
if (al_calibrate_joystick(0) != 0) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Error calibrating joystick!\n");
return 1;
}
}
/* if this joystick supports analogue input, ask the user whether to
* use digital or analogue mode. If it is only a digital pad, we don't
* bother with this question.
*/
if (joy[0].stick[0].flags & JOYFLAG_ANALOGUE) {
al_clear_bitmap(al_screen);
al_put_text_centre(al_screen, al_font_8x8, "Now press 'D' to use a digital",
AL_SCREEN_W/2, 64, al_palette_color[255]);
al_put_text_centre(al_screen, al_font_8x8, "joystick or 'A' for analogue mode.",
AL_SCREEN_W/2, 80, al_palette_color[255]);
for (;;) {
c = al_read_key()&0xFF;
if ((c=='d') || (c=='D')) {
analogmode = FALSE;
break;
}
else if ((c=='a') || (c=='A')) {
analogmode = TRUE;
break;
}
else if (c == 27)
return 0;
}
}
else
analogmode = FALSE;
al_drawing_mode(DRAW_MODE_XOR, 0, 0, 0);
al_clear_keybuf();
bmp = al_create_bitmap(320, 200);
al_clear_bitmap(bmp);
do {
al_poll_joystick(); /* we HAVE to do this to read the joystick */
al_clear_bitmap(bmp);
al_put_text_centre(bmp, al_font_8x8, joystick_driver->name, 160, 150, al_palette_color[255]);
if (analogmode)
al_put_text_centre(bmp, al_font_8x8, "Analog mode selected", 160, 160, al_palette_color[255]);
else
al_put_text_centre(bmp, al_font_8x8, "Digital mode selected", 160, 160, al_palette_color[255]);
al_put_text_centre(bmp, al_font_8x8, "Move the joystick all around", 160, 170, al_palette_color[255]);
al_put_text_centre(bmp, al_font_8x8, "Press any al_key to exit", 160, 180, al_palette_color[255]);
al_put_text_centre(bmp, al_font_8x8, "Made by Grzegorz Adam Hankiewicz", 160, 190, al_palette_color[255]);
/* if we detect any buttons, we print a message on the al_screen */
for (c=0; c<joy[0].num_buttons; c++) {
if (joy[0].button[c].b)
al_printf_text_centre(bmp, al_font_8x8, 160, c*10, al_palette_color[15], "%s pressed", joy[0].button[c].name);
}
if (!analogmode) {
/* now we have to check individually every possible movement
* and actualize the coordinates of the target sight.
*/
if (joy[0].stick[0].axis[0].d1) {
if (x > 0)
x--;
al_put_text_centre(bmp, al_font_8x8, "Left", 120, 100, al_palette_color[255]);
}
if (joy[0].stick[0].axis[0].d2) {
if (x < 319)
x++;
al_put_text_centre(bmp, al_font_8x8, "Right", 200, 100, al_palette_color[255]);
}
if (joy[0].stick[0].axis[1].d1) {
if (y > 0)
y--;
al_put_text_centre(bmp, al_font_8x8, "Up", 160, 70, al_palette_color[255]);
}
if (joy[0].stick[0].axis[1].d2) {
if (y < 199)
y++;
al_put_text_centre(bmp, al_font_8x8, "Down", 160, 130, al_palette_color[255]);
}
}
else {
/* yeah! Remember the 'ifs' of the digital part? This looks
* much better, only 2 lines.
*/
x += joy[0].stick[0].axis[0].pos/40;
y += joy[0].stick[0].axis[1].pos/40;
/* for informational purposes, show the input values on al_screen */
al_printf_text(bmp, al_font_8x8, 0, 0, al_palette_color[255], "Axis 0: %d", joy[0].stick[0].axis[0].pos);
al_printf_text(bmp, al_font_8x8, 0, 10, al_palette_color[255], "Axis 1: %d", joy[0].stick[0].axis[1].pos);
/* by checking if the values were positive or negative, we
* can know in which the direction the user pulled the joy.
*/
if (joy[0].stick[0].axis[0].pos/40 < 0)
al_put_text_centre(bmp, al_font_8x8, "Left", 120, 100, al_palette_color[255]);
if (joy[0].stick[0].axis[0].pos/40 > 0)
al_put_text_centre(bmp, al_font_8x8, "Right", 200, 100, al_palette_color[255]);
if (joy[0].stick[0].axis[1].pos/40 < 0)
al_put_text_centre(bmp, al_font_8x8, "Up", 160, 70, al_palette_color[255]);
if (joy[0].stick[0].axis[1].pos/40 > 0)
al_put_text_centre(bmp, al_font_8x8, "Down", 160, 130, al_palette_color[255]);
/* WARNING! An analog joystick can move more than 1 pixel at
* a time and the checks we did with the digital part don't
* work any longer because the steps of the target sight could
* 'jump' over the limits.
* To avoid this, we just check if the target sight has gone
* out of the al_screen. If yes, we put it back at the border.
*/
if (x > 319)
x = 319;
if (x < 0)
x = 0;
if (y < 0)
y = 0;
if (y > 199)
y = 199;
}
/* this draws the target sight. */
al_draw_circle(bmp, x, y, 5, al_palette_color[255]);
al_put_pixel(bmp, x, y, al_palette_color[255]);
al_put_pixel(bmp, x+1, y, al_palette_color[255]);
al_put_pixel(bmp, x, y+1, al_palette_color[255]);
al_put_pixel(bmp, x-1, y, al_palette_color[255]);
al_put_pixel(bmp, x, y-1, al_palette_color[255]);
al_put_pixel(bmp, x+5, y, al_palette_color[255]);
al_put_pixel(bmp, x, y+5, al_palette_color[255]);
al_put_pixel(bmp, x-5, y, al_palette_color[255]);
al_put_pixel(bmp, x, y-5, al_palette_color[255]);
al_blit(bmp, al_screen, 0, 0, AL_SCREEN_W/2 - 160, AL_SCREEN_H/2 - 100, 320, 200);
} while (!al_key_pressed());
al_destroy_bitmap(bmp);
return 0;
}
AL_END_OF_MAIN();
See more files for this project here