<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Using Joystick(s) DAllegro 5.2 code (dlang)</title>
		<link>http://www.allegro.cc/forums/view/617356</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 09 Apr 2018 18:14:07 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m trying to get a game pad working (360 game pad compatible):</p><p>void process() {<br />        if (al_get_joystick_active(_joy)) {<br />            ALLEGRO_JOYSTICK_STATE* jstate;<br />            al_get_joystick_state(_joy, jstate);<br />            _pos += Point(jstate.STICK.axis[0], jstate.STICK.axis[1]);<br />        }<br />    }</p><p>I get this error:<br />source\ball.d(25,33): Error: need this for axis of type float[3]<br />source\ball.d(25,55): Error: need this for axis of type float[3]</p><p>P.S. I got father with both: Pygame, and DSFML (simple fast media library), though this is my first crack at it using Allegro.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Joel Christensen)</author>
		<pubDate>Fri, 06 Apr 2018 13:52:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It needs to be <span class="source-code">jstate<span class="k2">[</span>STICK<span class="k2">]</span><span class="k2">[</span>AXIS<span class="k2">]</span></span>.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 06 Apr 2018 20:28:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m not a D programmer, but this part is suspicious:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_JOYSTICK_STATE"><span class="a">ALLEGRO_JOYSTICK_STATE</span></a><span class="k3">*</span> jstate<span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_get_joystick_state"><span class="a">al_get_joystick_state</span></a><span class="k2">(</span>_joy, jstate<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
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. 
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">/* C */</span>
<a href="http://www.allegro.cc/manual/ALLEGRO_JOYSTICK_STATE"><span class="a">ALLEGRO_JOYSTICK_STATE</span></a> jstate<span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_get_joystick_state"><span class="a">al_get_joystick_state</span></a><span class="k2">(</span>_joy, <span class="k3">&amp;</span>jstate<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Sat, 07 Apr 2018 01:29:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yep. Pointers are the same in D. If you have a pointer, you have to create something to go in it.</p><p>It should probably be:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_JOYSTICK_STATE"><span class="a">ALLEGRO_JOYSTICK_STATE</span></a> jstate<span class="k2">;</span> <span class="c">//no pointer</span>
<a href="http://www.allegro.cc/manual/al_get_joystick_state"><span class="a">al_get_joystick_state</span></a><span class="k2">(</span>_joy, <span class="k3">&amp;</span>jstate<span class="k2">)</span><span class="k2">;</span> <span class="c">//give address of non-pointer with &amp;</span>
</pre></div></div><p>

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</p><div class="source-code snippet"><div class="inner"><pre><span class="c">//outside your loop, like in your initialize/setup routine that is called once</span>
<a href="http://www.allegro.cc/manual/ALLEGRO_JOYSTICK_STATE"><span class="a">ALLEGRO_JOYSTICK_STATE</span></a> jstate<span class="k2">;</span>

<span class="c">//later in your game loop function</span>
<a href="http://www.allegro.cc/manual/al_get_joystick_state"><span class="a">al_get_joystick_state</span></a><span class="k2">(</span>_joy, <span class="k3">&amp;</span>jstate<span class="k2">)</span><span class="k2">;</span> <span class="c">//re-use it without creating a new one every time</span>
</pre></div></div><p>

But that&#39;s a tiny speed issue compared to &quot;not working at all&quot; / leaking memory.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Sat, 07 Apr 2018 01:43:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>    ALLEGRO_JOYSTICK* _joy;<br />    ALLEGRO_JOYSTICK_STATE _jstate;<br />...<br />_joy = al_get_joystick(_id);<br />...<br />    void process() {<br />        if (al_get_joystick_active(_joy)) {<br />            al_get_joystick_state(_joy, &amp;_jstate);<br />            _pos += Point(_jstate.stick[_id].axis[0], _jstate.stick[_id].axis[1]);<br />        }<br />    }</p><p>Ok, got it working, yay! Thanks everyone.:D</p><p>(I actually looked up the source code, from DAllegro5, to help work it out).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Joel Christensen)</author>
		<pubDate>Sun, 08 Apr 2018 05:53:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I suspect you will still have an issue with how you use _id twice for different things.<br /><span class="source-code"><a href="http://www.allegro.cc/manual/al_get_joystick"><span class="a">al_get_joystick</span></a><span class="k2">(</span>_id<span class="k2">)</span></span> This is the index of a game controller<br /><span class="source-code">_jstate.stick<span class="k2">[</span>_id<span class="k2">]</span></span> This is the index of an analogic input device on a specific game controller</p><p>(The &#39;360 has two analogic sticks)</p><p>You should really use the functions <span class="source-code"><a href="http://www.allegro.cc/manual/al_get_joystick_num_axes"><span class="a">al_get_joystick_num_axes</span></a><span class="k2">(</span><span class="k2">)</span>, <a href="http://www.allegro.cc/manual/al_get_joystick_num_sticks"><span class="a">al_get_joystick_num_sticks</span></a><span class="k2">(</span><span class="k2">)</span>, <a href="http://www.allegro.cc/manual/al_get_joystick_num_buttons"><span class="a">al_get_joystick_num_buttons</span></a><span class="k2">(</span><span class="k2">)</span></span> etc., to test what allegro detects. It&#39;s a very bad idea to read &quot;.stick[n]&quot; where n is greater than the number of detected sticks on this joystick.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Mon, 09 Apr 2018 18:14:07 +0000</pubDate>
	</item>
</rss>
