Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » A5 joystick

This thread is locked; no one can reply to it. rss feed Print
A5 joystick
KeithS
Member #11,950
May 2010

So, how does it work?

#SelectExpand
1 ALLEGRO_JOYSTICK *joy = NULL; 2 3 al_install_joystick(); 4 5 event_queue = al_create_event_queue(); 6 joy = al_get_joystick(0); 7 8 al_register_event_source(event_queue, al_get_joystick_event_source()); 9 10 while(1) 11 { 12 ALLEGRO_EVENT ev; 13 al_wait_for_event(event_queue, &ev); 14 15 if(ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS) 16 { 17 if(ev.joystick.stick == 0) 18 { 19 if(ev.joystick.axis == 0) 20 { 21 col = ev.joystick.pos; 22 } 23 24 } 25 } 26 }

Above just one cut down code example of several attempts to get some response from the joystick. Nothing. What's the trick, please, anyone?

* * * * * * * * * * *

My Noobish Blog

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Use <code>code goes here...</code> tags for posting code :

#SelectExpand
1ALLEGRO_JOYSTICK *joy = NULL; 2 3 al_install_joystick(); 4 5 event_queue = al_create_event_queue(); 6 joy = al_get_joystick(0); 7 8 al_register_event_source(event_queue, al_get_joystick_event_source()); 9 10 while(1) 11 { 12 ALLEGRO_EVENT ev; 13 al_wait_for_event(event_queue, &ev); 14 15 if(ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS) 16 { 17 if(ev.joystick.stick == 0) 18 { 19 if(ev.joystick.axis == 0) 20 { 21 col = ev.joystick.pos; 22 } 23 24 } 25 } 26 }

What did you expect that code to do? It doesn't create a display or do anything with the 'col' variable, so there's no visible output from the program.

What do you want to do with the joystick?

Monitor joystick events and respond accordingly.

KeithS
Member #11,950
May 2010

So I am retrieving the axis data from the joystick correctly?

What's there is only the code pertaining to that end. Displays and such are suppressed. Let's assume that 'col' is a heading modifier variable, for example, like then;

heading += col;

Both col and heading would be float type, BTW.

* * * * * * * * * * *

My Noobish Blog

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Evert
Member #794
November 2000
avatar

Look at the ex_josystick_events example (included in the manual entry for al_get_joystick_event_source()).

KeithS
Member #11,950
May 2010

Thanks guys, it is working as expected now. Rough, functioning mock up here. I am still not sure what I was doing wrong to start with.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_font.h> 3#include <allegro5/allegro_ttf.h> 4#include <stdio.h> 5 6int main(int argc, char *argv[]) 7{ 8 al_init(); 9 al_install_joystick(); 10 al_init_font_addon(); 11 al_init_ttf_addon(); 12 13 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 14 ALLEGRO_DISPLAY *display = NULL; 15 ALLEGRO_TIMER *timer = NULL; 16 ALLEGRO_JOYSTICK *joy = NULL; 17 ALLEGRO_FONT *ttf_font = NULL; 18 ALLEGRO_COLOR clear_color; 19 float col = 0, heading = 0; 20 char output[100]; 21 22 clear_color = al_map_rgb(20,50,80); 23 24 display = al_create_display(800, 600); 25 timer = al_create_timer(1.0 / 60); 26 event_queue = al_create_event_queue(); 27 28 ttf_font = al_load_ttf_font("FreeMono.ttf",24,0); 29 30 joy = al_get_joystick(0); 31 32 al_register_event_source(event_queue, al_get_display_event_source(display)); 33 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 34 al_register_event_source(event_queue, al_get_joystick_event_source()); 35 36 al_start_timer(timer); 37 38 while(1) 39 { 40 ALLEGRO_EVENT ev; 41 al_wait_for_event(event_queue, &ev); 42 43 if(ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS) 44 { 45 if(ev.joystick.stick == 0) 46 { 47 if(ev.joystick.axis == 0) 48 { 49 col = ev.joystick.pos; 50 } 51 52 } 53 } 54 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 55 { 56 break; 57 } 58 59 heading += col; 60 61 sprintf(output, "Heading %f", heading); 62 63 if(ev.type == ALLEGRO_EVENT_TIMER && al_is_event_queue_empty(event_queue)) 64 { 65 al_clear_to_color(clear_color); 66 al_draw_text(ttf_font, al_map_rgb(255,255,255), 400, 300, ALLEGRO_ALIGN_CENTRE, output); 67 al_flip_display(); 68 } 69 70 } 71 72 al_destroy_display(display); 73 al_uninstall_joystick(); 74 al_shutdown_ttf_addon(); 75 al_shutdown_font_addon(); 76 al_uninstall_system(); 77 return 0; 78}

All the best.

* * * * * * * * * * *

My Noobish Blog

Go to: