Can anyone help with this joystick code?
agonvs

Hi all,

This is a very simple program I wrote to test joystick responses but for some reason it won't work. The error message I get is: Assertion failed: sysdrv, file allegro-git\src\joynu.c, line 49. Any thoughts? Here's the code: (also attached for your convenience)

#include<allegro5/allegro.h>
#include<allegro5/allegro_primitives.h>
#include<iostream>
using namespace std;
ALLEGRO_EVENT_QUEUE* event_queue;

void render()
{
cout << endl;
}

int main()
{
if(!(al_install_joystick()))
cout << "Couldn't install joystick." << endl;
event_queue = al_create_event_queue();
if(!event_queue)
cout << "al_create_event_queue failed." << endl;
al_register_event_source(event_queue, al_get_joystick_event_source());
ALLEGRO_JOYSTICK* joy = al_get_joystick(0);
ALLEGRO_JOYSTICK_STATE jst;
ALLEGRO_EVENT event;
while(true)
{
if (al_is_event_queue_empty(event_queue))
render();
al_wait_for_event(event_queue, &event);
switch (event.type)
{
case ALLEGRO_EVENT_JOYSTICK_AXIS:
al_get_joystick_state(joy, &jst);
if(jst.stick[0].axis[0])
cout << jst.stick[0].axis[0] << endl;
if(jst.stick[0].axis[1])
cout << jst.stick[0].axis[1] << endl;
break;

case ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN:
al_get_joystick_state(joy, &jst);
if (jst.button[0])
cout << "Button 0 pressed" << endl;
if (jst.button[1])
cout << "Button 1 pressed" << endl;
if (jst.button[9])
cout << "Button 9 pressed" << endl;
break;

case ALLEGRO_EVENT_JOYSTICK_BUTTON_UP:
cout << endl;
break;
}
}
return 0;
}

Edgar Reynaldo

You must call al_init before initializing anything else.

And you can use <code>code goes here...</code> tags for code.

agonvs

al_init(), of course. Duh. I'll give 'er a shot. Thank you!

Edgar Reynaldo

Another helpful tip is that generally you don't want to use the joystick state if you are using events. The event will tell you which axis and which stick and the state of the buttons depending on the event. Otherwise you can assume the state didn't change if you didn't get any events. So it might make sense to get it once when it is at neutral to calibrate the joystick.

https://www.allegro.cc/manual/5/ALLEGRO_EVENT

agonvs

Getting the following error message (though it compiles fine):

Unhandled exception at 0x77764c16 in shell.exe: 0xC0000005: Access violation reading
location 0x00000000.

#SelectExpand
1#include<allegro5/allegro.h> 2#include<allegro5/allegro_primitives.h> 3#include<iostream> 4using namespace std; 5ALLEGRO_EVENT_QUEUE* event_queue; 6 7void render() 8{ 9 cout << endl; 10} 11 12enum directions {NONE, NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST}; 13enum buttons {ZERO,ONE,EIGHT,NINE}; 14 15int main() 16{ 17 ALLEGRO_TIMER *timer; 18 timer = al_create_timer(1.0/60); 19 float gameTime = 0; 20 int frames = 0; 21 int gameFPS = 0; 22 int button; 23 if(!(al_init())) 24 cout << "Couldn't initialize Allegro." << cout; 25 if(!(al_install_joystick())) 26 cout << "Couldn't install joystick." << endl; 27 event_queue = al_create_event_queue(); 28 if(!event_queue) 29 cout << "al_create_event_queue failed." << endl; 30 31 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 32 al_start_timer(timer); 33 gameTime = al_current_time(); 34 al_register_event_source(event_queue, al_get_joystick_event_source()); 35 ALLEGRO_JOYSTICK* joy = al_get_joystick(0); 36 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 37 38 ALLEGRO_JOYSTICK_STATE jst; 39 ALLEGRO_EVENT event; 40 directions dir; 41 bool render = false; 42 while(true) 43 { 44 ALLEGRO_EVENT ev; 45 al_wait_for_event(event_queue, &ev); 46 al_get_joystick_state(joy, &jst); 47 if (ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS) 48 { 49 if ((jst.stick[2].axis[0] == 0) && (jst.stick[2].axis[1] == 0)) 50 dir = NONE; 51 if ((jst.stick[2].axis[0] == 0) && (jst.stick[2].axis[1] == -1)) 52 dir = NORTH; 53 if ((jst.stick[2].axis[0] == 1) && (jst.stick[2].axis[1] == -1)) 54 dir = NORTHEAST; 55 if ((jst.stick[2].axis[0] == 1) && (jst.stick[2].axis[1] == 0)) 56 dir = EAST; 57 if ((jst.stick[2].axis[0] == 1) && (jst.stick[2].axis[1] == 1)) 58 dir = SOUTHEAST; 59 if ((jst.stick[2].axis[0] == 0) && (jst.stick[2].axis[1] == 1)) 60 dir = SOUTH; 61 if ((jst.stick[2].axis[0] == -1) && (jst.stick[2].axis[1] == 1)) 62 dir = SOUTHWEST; 63 if ((jst.stick[2].axis[0] == -1) && (jst.stick[2].axis[1] == 0)) 64 dir = WEST; 65 if ((jst.stick[2].axis[0] == -1) && (jst.stick[2].axis[1] == -1)) 66 dir = NORTHWEST; 67 68 } 69 if (ev.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN) 70 { 71 if (jst.button[0]) 72 button = ZERO; 73 if (jst.button[1]) 74 button = ONE; 75 if (jst.button[8]) 76 button = EIGHT; 77 if (jst.button[9]) 78 button = NINE; 79 } 80 else if (ev.type == ALLEGRO_EVENT_TIMER) 81 { 82 render = true; 83 frames++; 84 if (al_current_time() - gameTime >= 1) 85 { 86 gameTime = al_current_time(); 87 gameFPS = frames; 88 frames = 0; 89 } 90 } 91 if (render && al_is_event_queue_empty(event_queue)) 92 { 93 render = false; 94 cout << "FPS: " << gameFPS << endl; 95 switch (dir) 96 { 97 case NORTH: 98 cout << "NORTH" << endl; 99 case NORTHEAST: 100 cout << "NORTHEAST" << endl; 101 case EAST: 102 cout << "EAST" << endl; 103 case SOUTHEAST: 104 cout << "SOUTHEAST" << endl; 105 case SOUTH: 106 cout << "SOUTH" << endl; 107 case SOUTHWEST: 108 cout << "SOUTHWEST" << endl; 109 case WEST: 110 cout << "WEST" << endl; 111 case NORTHWEST: 112 cout << "NORTHWEST" << endl; 113 } 114 switch (button) 115 { 116 case ZERO: 117 cout << "Button 1 pressed." << endl; 118 case ONE: 119 cout << "Button 2 pressed." << endl; 120 case EIGHT: 121 cout << "Button 9 pressed." << endl; 122 case NINE: 123 cout << "Button 10 pressed." << endl; 124 } 125 } 126 } 127 al_destroy_timer(timer); 128 al_destroy_event_queue(event_queue); 129 return 0; 130}

The compiler is pointing to the line timer = al_create_timer(1.0/60).
Any thoughts?
Could it be possible that I'm getting this error because I'm running Allegro 5.0.10 and VC++2010 on Windows 8.1 64-bit edition?

Edgar Reynaldo

It is null (0x00000000) because you called al_create_timer before you called al_init. You can't create a timer until you initialize allegro.

agonvs

Now I'm getting this:

Assertion failed: queue, file allegro-git\src\events.c, line 333

Debug Error!

Program: C:\MyPrograms\shell\Debug\shell.exe]

R6010
-abort() has been called

Max Savenkov

In line 36 you declare local ALLEGRO_EVENT_QUEUE *event_queue = NULL; which overrides global event_queue, and all further code tries to use that one. Of course, it's not initialized, and everything crashes on the first attempt to access it (in al_wait_for_event(event_queue, &ev), most probably).

agonvs

Thanks for your help guys! My code is working now and I am happy. :-)

Thread #615211. Printed from Allegro.cc