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;
}
You must call al_init before initializing anything else.
And you can use <code>code goes here...</code> tags for code.
al_init(), of course. Duh. I'll give 'er a shot. Thank you!
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.
Getting the following error message (though it compiles fine):
Unhandled exception at 0x77764c16 in shell.exe: 0xC0000005: Access violation reading
location 0x00000000.
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?
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.
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
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).
Thanks for your help guys! My code is working now and I am happy. :-)