Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Using Joystick(s) DAllegro 5.2 code (dlang)

This thread is locked; no one can reply to it. rss feed Print
Using Joystick(s) DAllegro 5.2 code (dlang)
Joel Christensen
Member #5,103
October 2004

I'm trying to get a game pad working (360 game pad compatible):

void process() {
if (al_get_joystick_active(_joy)) {
ALLEGRO_JOYSTICK_STATE* jstate;
al_get_joystick_state(_joy, jstate);
_pos += Point(jstate.STICK.axis[0], jstate.STICK.axis[1]);
}
}

I get this error:
source\ball.d(25,33): Error: need this for axis of type float[3]
source\ball.d(25,55): Error: need this for axis of type float[3]

P.S. I got father with both: Pygame, and DSFML (simple fast media library), though this is my first crack at it using Allegro.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Audric
Member #907
January 2001

I'm not a D programmer, but this part is suspicious:

In the C API, you need to allocate a struct, and pass its address. If you pass an uninitialized pointer, you will (try) write in a random part of memory, with horrific results.

/* C */
ALLEGRO_JOYSTICK_STATE jstate;
al_get_joystick_state(_joy, &jstate);

Chris Katko
Member #1,881
January 2002
avatar

Yep. Pointers are the same in D. If you have a pointer, you have to create something to go in it.

It should probably be:

ALLEGRO_JOYSTICK_STATE jstate; //no pointer
al_get_joystick_state(_joy, &jstate); //give address of non-pointer with &

And that still allocates a new joystick state struct every time, you could have just one and re-use it over and over. Like this

//outside your loop, like in your initialize/setup routine that is called once
ALLEGRO_JOYSTICK_STATE jstate;

//later in your game loop function
al_get_joystick_state(_joy, &jstate); //re-use it without creating a new one every time

But that's a tiny speed issue compared to "not working at all" / leaking memory.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Joel Christensen
Member #5,103
October 2004

ALLEGRO_JOYSTICK* _joy;
ALLEGRO_JOYSTICK_STATE _jstate;
...
_joy = al_get_joystick(_id);
...
void process() {
if (al_get_joystick_active(_joy)) {
al_get_joystick_state(_joy, &_jstate);
_pos += Point(_jstate.stick[_id].axis[0], _jstate.stick[_id].axis[1]);
}
}

Ok, got it working, yay! Thanks everyone.:D

(I actually looked up the source code, from DAllegro5, to help work it out).

Audric
Member #907
January 2001

I suspect you will still have an issue with how you use _id twice for different things.
al_get_joystick(_id) This is the index of a game controller
_jstate.stick[_id] This is the index of an analogic input device on a specific game controller

(The '360 has two analogic sticks)

You should really use the functions al_get_joystick_num_axes(), al_get_joystick_num_sticks(), al_get_joystick_num_buttons() etc., to test what allegro detects. It's a very bad idea to read ".stick[n]" where n is greater than the number of detected sticks on this joystick.

Go to: