<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Lua + Keyboard input</title>
		<link>http://www.allegro.cc/forums/view/591708</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 03 Jun 2007 02:05:36 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello. I am trying to write a game engine with Lua+Allegro. I&#39;ve got a lot of it done, with the exception of keyboard input. I am clueless as to how to get the keyboard presses in Allegro and C into a Lua script, so that the Lua script can just call something like</p><div class="source-code snippet"><div class="inner"><pre>isKeyDown<span class="k2">(</span>KEY_ESC<span class="k2">)</span>
</pre></div></div><p>

(obviously, isKeyDown would be a C function that lua can call).</p><p>Could someone help me with this?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (edfredcoder)</author>
		<pubDate>Sun, 03 Jun 2007 01:01:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think what you want is a callback for Lua.</p><p>ie:<br />OnKeyDown(int key)</p><p>That you implement in Lua. But I don&#39;t know Lua, so I&#39;m just throwing stuff out there.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kibiz0r)</author>
		<pubDate>Sun, 03 Jun 2007 01:06:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Read the manual and Programming In Lua. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> Especially the parts on registering C functions for Lua.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Sun, 03 Jun 2007 01:09:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What I was thinking of is more of a function that returns a boolean and accepts the key to check, like the key[] array in Allegro does, only from within Lua. I&#39;d like to avoid callbacks, and I don&#39;t know how to do callbacks in Lua.</p><p>edit:</p><p>I have read the manual, and can register functions and call them from Lua. That works. What I want to know is how to get the constants KEY_* into Lua so that when I call a isKeyDown or similar function it can accept these and return the boolean.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (edfredcoder)</author>
		<pubDate>Sun, 03 Jun 2007 01:10:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The same way you would register any other global value.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Sun, 03 Jun 2007 01:18:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So, if I understand this correctly, I should set the KEY_* variables after initializing the Lua VM, as such:</p><div class="source-code snippet"><div class="inner"><pre>lua_pushinteger<span class="k2">(</span>lvm,KEY_A<span class="k2">)</span><span class="k2">;</span>
lua_setglobal<span class="k2">(</span>lvm,<span class="s">"KEY_A"</span><span class="k2">)</span><span class="k2">;</span>
lua_pushinteger<span class="k2">(</span>lvm,KEY_B<span class="k2">)</span><span class="k2">;</span>
lua_setglobal<span class="k2">(</span>lvm,<span class="s">"KEY_B"</span><span class="k2">)</span><span class="k2">;</span>
lua_pushinteger<span class="k2">(</span>lvm,KEY_B<span class="k2">)</span><span class="k2">;</span>
lua_setglobal<span class="k2">(</span>lvm,<span class="s">"KEY_B"</span><span class="k2">)</span><span class="k2">;</span>
<span class="c">//And so on</span>
</pre></div></div><p>

Then define and register the isKeyDown function:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">static</span> <span class="k1">int</span> isKeyDown<span class="k2">(</span>lua_State <span class="k3">*</span>l<span class="k2">)</span>
<span class="k2">{</span>
     <span class="k1">int</span> <a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k3">=</span>luaL_checkint<span class="k2">(</span>l,<span class="n">1</span><span class="k2">)</span><span class="k2">;</span>

     lua_pushboolean<span class="k2">(</span>lvm,KEY<span class="k2">[</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">]</span><span class="k2">)</span><span class="k2">;</span>

     <span class="k1">return</span> <span class="n">1</span><span class="k2">;</span>
<span class="k2">}</span>

<span class="c">//...VM is initialized, etc.</span>

<span class="c">//Now we register the functions</span>
lua_pushcfunction<span class="k2">(</span>lvm,isKeyDown<span class="k2">)</span><span class="k2">;</span>
lua_setglobal<span class="k2">(</span>lvm,<span class="s">"isKeyDown"</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

That part is fairly straightforward, thank you X-G. However, the setting of global KEY_* variables in the Lua VM is very tedious. Is there an elegant way of doing that?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (edfredcoder)</author>
		<pubDate>Sun, 03 Jun 2007 01:28:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I imagine you could loop through them all with some fancy for loop.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Sun, 03 Jun 2007 01:31:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I found the defines in allegro/keyboard.h:</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#define KEY_A                 __allegro_KEY_A</span>
<span class="p">#define KEY_B                 __allegro_KEY_B</span>
<span class="p">#define KEY_C                 __allegro_KEY_C</span>
<span class="p">#define KEY_D                 __allegro_KEY_D</span>
<span class="p">#define KEY_E                 __allegro_KEY_E</span>
</pre></div></div><p>

I wrote the following program to take this text and turn it into the lines of code I need:</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="p">#include &lt;stdio.h&gt;</span></td></tr><tr><td class="number">2</td><td>&#160;</td></tr><tr><td class="number">3</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">4</td><td><span class="k2">{</span></td></tr><tr><td class="number">5</td><td>        <span class="k1">char</span> buffer<span class="k2">[</span><span class="n">100</span><span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td>        <span class="k1">int</span> i<span class="k2">;</span></td></tr><tr><td class="number">8</td><td>&#160;</td></tr><tr><td class="number">9</td><td>        <span class="k1">for</span><span class="k2">(</span>i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>i<span class="k3">&lt;</span><span class="n">127</span><span class="k2">;</span>i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span> <span class="c">//127 different keys, see allegro/keyboard comments</span></td></tr><tr><td class="number">10</td><td>                <a href="http://www.delorie.com/djgpp/doc/libc/libc_670.html" target="_blank">scanf</a><span class="k2">(</span><span class="s">"%s"</span>,buffer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>                <a href="http://www.delorie.com/djgpp/doc/libc/libc_670.html" target="_blank">scanf</a><span class="k2">(</span><span class="s">"%s"</span>,buffer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>                  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"lua_pushinteger(lvm,%s);\nlua_setglobal(lvm,\"%s\");\n"</span>,buffer,buffer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>                <a href="http://www.delorie.com/djgpp/doc/libc/libc_670.html" target="_blank">scanf</a><span class="k2">(</span><span class="s">"%s"</span>,buffer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>        <span class="k2">}</span></td></tr><tr><td class="number">15</td><td>        <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

Which, when I copy+paste the lines from the include file into stdin and redirect stdout to a file, gives me a file with the code neatly written for me.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (edfredcoder)</author>
		<pubDate>Sun, 03 Jun 2007 02:05:36 +0000</pubDate>
	</item>
</rss>
