<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GuShH&#039;s DevBlog &#187; PureBasic</title>
	<atom:link href="http://gushh.net/blog/?cat=139&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://gushh.net/blog</link>
	<description>This blog is about software, electronics engineering and game development.</description>
	<lastBuildDate>Tue, 24 Jan 2012 00:31:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>(PB) Loading Assets, the simple way.</title>
		<link>http://gushh.net/blog/loading-assets-simple-way/</link>
		<comments>http://gushh.net/blog/loading-assets-simple-way/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 00:29:27 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PureBasic]]></category>
		<category><![CDATA[asset]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[loading files]]></category>
		<category><![CDATA[purebasic]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/?p=1284</guid>
		<description><![CDATA[Intro Assets are an essential part of most games, how you manage them determines whether you spend more time working on them than dealing with them. For small to medium games, loading an entire directory and having it referenced to a Map is the ideal solution. It&#8217;s both flexible and simple. The following code allows [...]]]></description>
			<content:encoded><![CDATA[<h2>Intro</h2>
<p>Assets are an essential part of most games, how you manage them determines whether you spend more time working on them than dealing with them.</p>
<p>For small to medium games, loading an entire directory and having it referenced to a Map is the ideal solution. It&#8217;s both flexible and simple.</p>
<h4>The following code allows you to do just this:</h4>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Prototype</span>.i LOAD_DIRECTORY_CALLBACK<span style="color: #000066;">&#40;</span> Map Assets.i<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>, directory.s, extension.s, name.s, userDefined.i <span style="color: #000066;">=</span> #Null <span style="color: #000066;">&#41;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i LoadDirectory<span style="color: #000066;">&#40;</span> Map assets.i<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>, directory.s, extension.s, <span style="color: #000066;">*</span>callback.LOAD_DIRECTORY_CALLBACK, userDefined.i <span style="color: #000066;">=</span> #Null <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Define</span>.i dir <span style="color: #000066;">=</span> <span style="color: #0000ff;">ExamineDirectory</span><span style="color: #000066;">&#40;</span> #PB_Any, directory, <span style="color: #009900;">&quot;*&quot;</span> <span style="color: #000066;">+</span> extension <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">IsDirectory</span><span style="color: #000066;">&#40;</span> dir <span style="color: #000066;">&#41;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">Define</span>.i count <span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span>
		<span style="color: #000066; font-weight: bold;">Define</span>.s name <span style="color: #000066;">=</span> <span style="color: #009900;">&quot;&quot;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">While</span> <span style="color: #0000ff;">NextDirectoryEntry</span><span style="color: #000066;">&#40;</span> dir <span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">DirectoryEntryType</span><span style="color: #000066;">&#40;</span> dir <span style="color: #000066;">&#41;</span> <span style="color: #000066;">=</span> #PB_DirectoryEntry_File
				name <span style="color: #000066;">=</span> <span style="color: #0000ff;">DirectoryEntryName</span><span style="color: #000066;">&#40;</span> dir <span style="color: #000066;">&#41;</span><span style="color: #ff0000; font-style: italic;">;</span>
				assets<span style="color: #000066;">&#40;</span> <span style="color: #0000ff;">ReplaceString</span><span style="color: #000066;">&#40;</span> name, extension, <span style="color: #009900;">&quot;&quot;</span> <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span> <span style="color: #000066;">=</span> <span style="color: #000066;">*</span>callback<span style="color: #000066;">&#40;</span> assets<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>, directory, extension, name, userDefined <span style="color: #000066;">&#41;</span><span style="color: #ff0000; font-style: italic;">;LoadSound( #PB_Any, directory + &quot;\&quot; + name )</span>
				count <span style="color: #000066;">+</span> <span style="color: #CC0000;">1</span>
			<span style="color: #000066; font-weight: bold;">EndIf</span>
		<span style="color: #000066; font-weight: bold;">Wend</span>
&nbsp;
		<span style="color: #0000ff;">FinishDirectory</span><span style="color: #000066;">&#40;</span> dir <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">EndIf</span>
	<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> count
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>To use this code you must define a procedure of your own, this procedure is going to be called on each file to be loaded; you&#8217;ll have to load and process the file in this function.</p>
<h4>Example use:</h4>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Global</span> NewMap sprite.i<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Procedure</span>.i callback_loadsprites<span style="color: #000066;">&#40;</span> Map Assets.i<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>, directory.s, extension.s, name.s, userDefined.i <span style="color: #000066;">=</span> #Null <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #0000ff;">LoadSprite</span><span style="color: #000066;">&#40;</span> #PB_Any, directory <span style="color: #000066;">+</span> <span style="color: #009900;">&quot;\&quot;</span> <span style="color: #000066;">+</span> name, userDefined <span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
LoadDirectory<span style="color: #000066;">&#40;</span> sprite<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>, <span style="color: #009900;">&quot;sprites&quot;</span>, <span style="color: #009900;">&quot;.png&quot;</span>, @callback_loadsprites<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span></pre></div></div>

<p>The example will attempt to scan through the directory &#8220;sprites&#8221;, it will execute your callback on each png file it finds inside the aforementioned directory, furthermore it&#8217;ll reference the filename (without extension) to the sprite() Map, so when you need a handle for the sprite called &#8220;fire.png&#8221; you&#8217;d just use sprite(&#8220;fire&#8221;) to obtain it.</p>
<p>Since the files are referenced to a Map by their actual name, you could easily implement a scripting system or any other dynamic management solution for your assets without much hassle.</p>
<blockquote><p>An interesting part of the code is the return value, it&#8217;s actually the number of assets found (not the ones loaded, since your callback could choose not to load a certain file, for instance).</p></blockquote>
<p>There are several limitations to this simple implementation, which is why I mentioned &#8220;small games&#8221; &#8211; There is no directory recursion, no advanced filtering options, no way to parallel the process on a separate thread and there&#8217;s no error handling at the moment.</p>
<p>However, it&#8217;s still very useful and I highly recommend you try it out, while it&#8217;s not a novel aproach, it&#8217;s always been the method I used to load my assets and it just works.</p>
<p>Have fun!</p>
<g:plusone href='http://gushh.net/blog/loading-assets-simple-way/'></g:plusone>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/loading-assets-simple-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>(PB) Nearest Power of Two in Assembly</title>
		<link>http://gushh.net/blog/pb-nearest-power-assembly/</link>
		<comments>http://gushh.net/blog/pb-nearest-power-assembly/#comments</comments>
		<pubDate>Thu, 04 Aug 2011 11:25:19 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PureBasic]]></category>
		<category><![CDATA[asm]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[find nearest power of two]]></category>
		<category><![CDATA[nearest power of 2]]></category>
		<category><![CDATA[Nearest power of two]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[pow2]]></category>
		<category><![CDATA[premature optimization]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/?p=1088</guid>
		<description><![CDATA[On today&#8217;s masochism section&#8230; we find the nearest power of two in Assembly! Macro PowerOfTwo&#40; _num_ &#41; !MOV Eax, &#91;v_#_num_&#93; !SUB Eax, 1 !PowerOfTwo_Loop_#_num_: !MOV Ecx, Eax !ADD Eax, 1 !AND Ecx, Eax !JNZ PowerOfTwo_Loop_#_num_ !MOV &#91;v_#_num_&#93;, Eax EndMacro Truth be told I was looking for some old libraries I wrote a while back and [...]]]></description>
			<content:encoded><![CDATA[<p>On today&#8217;s masochism section&#8230; we find the nearest power of two in Assembly!</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Macro</span> PowerOfTwo<span style="color: #000066;">&#40;</span> _num_ <span style="color: #000066;">&#41;</span>
	<span style="color: #000066;">!</span><span style="color: #000fff;">MOV</span>     Eax, <span style="color: #000066;">&#91;</span>v_#_num_<span style="color: #000066;">&#93;</span>
	<span style="color: #000066;">!</span><span style="color: #000fff;">SUB</span>     Eax, <span style="color: #CC0000;">1</span>
	<span style="color: #000066;">!</span>PowerOfTwo_Loop_#_num_:
	<span style="color: #000066;">!</span><span style="color: #000fff;">MOV</span>     Ecx, Eax
	<span style="color: #000066;">!</span><span style="color: #000fff;">ADD</span>     Eax, <span style="color: #CC0000;">1</span>
	<span style="color: #000066;">!</span><span style="color: #000066; font-weight: bold;">AND</span>     Ecx, Eax
	<span style="color: #000066;">!</span><span style="color: #000fff;">JNZ</span>     PowerOfTwo_Loop_#_num_ 
	<span style="color: #000066;">!</span><span style="color: #000fff;">MOV</span> <span style="color: #000066;">&#91;</span>v_#_num_<span style="color: #000066;">&#93;</span>, Eax
<span style="color: #000066; font-weight: bold;">EndMacro</span></pre></div></div>

<p>Truth be told I was looking for some old libraries I wrote a while back and I stumbled upon that code. It made me chuckle so of course I had to share it. The reason I laughed was because, well&#8230; Who would really need to optimize such a routine?; in those days I was suffering from a severe case of premature-optimizationitis.</p>
<p>However, compared to the normal method which follows:</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Procedure</span>.i NearestPow2<span style="color: #000066;">&#40;</span> Value.i <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #0000ff;">Pow</span><span style="color: #000066;">&#40;</span> <span style="color: #CC0000;">2</span>, <span style="color: #0000ff;">Int</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#40;</span><span style="color: #0000ff;">Log</span><span style="color: #000066;">&#40;</span> Value <span style="color: #000066;">&#41;</span> <span style="color: #000066;">/</span> <span style="color: #0000ff;">Log</span><span style="color: #000066;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">+</span> <span style="color: #CC0000;">0.5</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>The ASM routine is about 25 times faster (I benchmarked the code itself without the procedure to be fair, otherwise it would&#8217;ve been 55 times faster!)</p>
<p>So, while unnecessary it still proves a point &#8212; You can always optimize your code for speed. However, the cost is clear&#8230; readability!</p>
<p>I guess it&#8217;s worth stating the obvious in this case: Always benchmark your code and find a balance when it comes to optimization, only optimize your bottlenecks. Don&#8217;t waste time in small details unless the reward is worth the time.</p>
<p>Cheers!</p>
<p>PS: Since the PB pre-processor is case sensitive (who would&#8217;ve known!) Remember that you must pass the macro the same label your variable has, also because we define a label this means we can only call the routine in one place for one particular variable. That however was not an issue with my code, so it never bothered me.</p>
<g:plusone href='http://gushh.net/blog/pb-nearest-power-assembly/'></g:plusone>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/pb-nearest-power-assembly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>(PB) String Between Characters</title>
		<link>http://gushh.net/blog/pb-string-characters/</link>
		<comments>http://gushh.net/blog/pb-string-characters/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 12:30:07 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PureBasic]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[find string]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[pb]]></category>
		<category><![CDATA[purebasic]]></category>
		<category><![CDATA[string between chars]]></category>
		<category><![CDATA[str_between_char]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/?p=849</guid>
		<description><![CDATA[This is a small purebasic function used to retrieve a string between two known characters, it also allows you to provide a starting position. Procedure.s str_between_char&#40; *Source.CHARACTER, First.C, Last.C, StartAt.i = 0 &#41; Define.i dwLength &#160; *Source + &#40;StartAt * SizeOf&#40;CHARACTER&#41;&#41; Repeat If *Source\c = First ; If this character matches the First character *Source [...]]]></description>
			<content:encoded><![CDATA[<p>This is a small purebasic function used to retrieve a string between two known characters, it also allows you to provide a starting position.</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Procedure</span>.s str_between_char<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>Source.CHARACTER, First.C, Last.C, StartAt.i <span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span> <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Define</span>.i dwLength
&nbsp;
	<span style="color: #000066;">*</span>Source <span style="color: #000066;">+</span> <span style="color: #000066;">&#40;</span>StartAt <span style="color: #000066;">*</span> <span style="color: #0000ff;">SizeOf</span><span style="color: #000066;">&#40;</span>CHARACTER<span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Repeat</span>
		<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>Source<span style="color: #000066;">\</span>c <span style="color: #000066;">=</span> First <span style="color: #ff0000; font-style: italic;">; If this character matches the First character</span>
			<span style="color: #000066;">*</span>Source <span style="color: #000066;">+</span> <span style="color: #0000ff;">SizeOf</span><span style="color: #000066;">&#40;</span>CHARACTER<span style="color: #000066;">&#41;</span>
			dwLength <span style="color: #000066;">=</span> <span style="color: #000066;">*</span>Source
			<span style="color: #000066; font-weight: bold;">Repeat</span> <span style="color: #ff0000; font-style: italic;">; Repeat until we find an occurrance with the Last character</span>
				<span style="color: #000066;">*</span>Source <span style="color: #000066;">+</span> <span style="color: #0000ff;">SizeOf</span><span style="color: #000066;">&#40;</span>CHARACTER<span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">Until</span> <span style="color: #000066;">*</span>Source<span style="color: #000066;">\</span>c <span style="color: #000066;">=</span> Last
			dwLength <span style="color: #000066;">=</span> <span style="color: #000066;">&#40;</span><span style="color: #000066;">*</span>Source <span style="color: #000066;">-</span> dwLength<span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #0000ff;">PeekS</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>Source <span style="color: #000066;">-</span> dwLength, dwLength <span style="color: #000066;">&#41;</span> <span style="color: #ff0000; font-style: italic;">; Peek the output string</span>
			<span style="color: #000066; font-weight: bold;">Break</span>
		<span style="color: #000066; font-weight: bold;">EndIf</span>
		<span style="color: #000066;">*</span>Source <span style="color: #000066;">+</span> <span style="color: #0000ff;">SizeOf</span><span style="color: #000066;">&#40;</span>CHARACTER<span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Until</span> <span style="color: #000066;">*</span>Source<span style="color: #000066;">\</span>c <span style="color: #000066;">=</span> #Null
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>Example use:</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Define</span>.s <span style="color: #0000ff;">str</span> 		<span style="color: #000066;">=</span> <span style="color: #009900;">&quot;dd(hallo)xyz(a)&quot;</span>
<span style="color: #000066; font-weight: bold;">Define</span>.s result 	<span style="color: #000066;">=</span> str_between_char<span style="color: #000066;">&#40;</span>@<span style="color: #0000ff;">str</span>, '<span style="color: #000066;">&#40;</span>', '<span style="color: #000066;">&#41;</span>', <span style="color: #CC0000;">2</span><span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Debug</span> result</pre></div></div>

<p>If you&#8217;re working on a parser or similar project this will sure come in handy.<br />
<strong>Enjoy!</strong></p>
<g:plusone href='http://gushh.net/blog/pb-string-characters/'></g:plusone>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/pb-string-characters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PB Parallel Port Library</title>
		<link>http://gushh.net/blog/parallel-port-library/</link>
		<comments>http://gushh.net/blog/parallel-port-library/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 04:17:42 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[PureBasic]]></category>
		<category><![CDATA[LPT]]></category>
		<category><![CDATA[Parallel Port]]></category>
		<category><![CDATA[Paraport]]></category>
		<category><![CDATA[PB Parallel Library]]></category>
		<category><![CDATA[purebasic]]></category>
		<category><![CDATA[Stepper Motor]]></category>
		<category><![CDATA[Unipolar Stepper]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/?p=743</guid>
		<description><![CDATA[What is it: This is a minimalist  library based on the inpout32 DLL Basically I wrapped the original library and added a whole bunch of useful constants to easily access the Control and Status registers while keeping things clean and simple. This library is currently being used on a side project I&#8217;ve been working on with a friend, [...]]]></description>
			<content:encoded><![CDATA[<h2>What is it:</h2>
<p>This is a minimalist  library based on the <a title="http://logix4u.net/Legacy_Ports/Parallel_Port/Inpout32.dll_for_Windows_98/2000/NT/XP.html" href="http://" target="_blank">inpout32 DLL</a></p>
<p>Basically I wrapped the original library and added a whole bunch of useful constants to easily access the Control and Status registers while keeping things clean and simple.</p>
<p>This library is currently being used on a side project I&#8217;ve been working on with a friend, since he doesn&#8217;t have any μController experience we decided to use LPT for the time being.</p>
<h3></h3>
<h2>The code:</h2>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;">&nbsp;
<span style="color: #ff0000; font-style: italic;">; 	Minimalist Library for Parallel port access based on inpout32.dll </span>
<span style="color: #ff0000; font-style: italic;">;		by Gustavo J. Fiorenza AKA GuShH (info@gushh.net - info@gushh.com.ar)</span>
<span style="color: #ff0000; font-style: italic;">;		Version 1.0 - 11/01/2011</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EnableExplicit</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">; Default LPT Addresses.</span>
#PARALLEL_PORT_LPT1						<span style="color: #000066;">=</span> $3BC
#PARALLEL_PORT_LPT2						<span style="color: #000066;">=</span> $378
#PARALLEL_PORT_LPT3						<span style="color: #000066;">=</span> $278
&nbsp;
<span style="color: #ff0000; font-style: italic;">; Register offsets.</span>
#PARALLEL_PORT_STATUS						<span style="color: #000066;">=</span> $01
#PARALLEL_PORT_CONTROL						<span style="color: #000066;">=</span> $02
&nbsp;
<span style="color: #ff0000; font-style: italic;">; Shared with Data Register.</span>
#PARALLEL_PORT_OFF						<span style="color: #000066;">=</span> $00
#PARALLEL_PORT_BIT0						<span style="color: #000066;">=</span> $01
#PARALLEL_PORT_BIT1						<span style="color: #000066;">=</span> $02
#PARALLEL_PORT_BIT2						<span style="color: #000066;">=</span> $04
#PARALLEL_PORT_BIT3						<span style="color: #000066;">=</span> $08
#PARALLEL_PORT_BIT4						<span style="color: #000066;">=</span> $10
#PARALLEL_PORT_BIT5						<span style="color: #000066;">=</span> $20
#PARALLEL_PORT_BIT6						<span style="color: #000066;">=</span> $40
#PARALLEL_PORT_BIT7						<span style="color: #000066;">=</span> $80
&nbsp;
<span style="color: #000066; font-weight: bold;">XIncludeFile</span> <span style="color: #009900;">&quot;ParallelPort_Constants.pbi&quot;</span> 	<span style="color: #ff0000; font-style: italic;">; All of the helper, non-essential constants are defined here.</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">Structure</span> PARALLEL_PORT
	Handle.i
	Port.i
	LastData.i
<span style="color: #000066; font-weight: bold;">EndStructure</span>
&nbsp;
&nbsp;
<span style="color: #ff0000; font-style: italic;">;- Instance construction and destruction</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i ParallelPort_Create<span style="color: #000066;">&#40;</span> Port.i <span style="color: #000066;">=</span> #PARALLEL_PORT_LPT2 <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Define</span>.PARALLEL_PORT <span style="color: #000066;">*</span>this <span style="color: #000066;">=</span> <span style="color: #0000ff;">AllocateMemory</span><span style="color: #000066;">&#40;</span> <span style="color: #0000ff;">SizeOf</span><span style="color: #000066;">&#40;</span>PARALLEL_PORT<span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>Port 	<span style="color: #000066;">=</span> port
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>Handle 	<span style="color: #000066;">=</span> <span style="color: #0000ff;">OpenLibrary</span><span style="color: #000066;">&#40;</span> #PB_Any, <span style="color: #009900;">&quot;inpout32.dll&quot;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">IsLibrary</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>Handle <span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this
		<span style="color: #000066; font-weight: bold;">Else</span>
			<span style="color: #000066; font-weight: bold;">Debug</span> <span style="color: #009900;">&quot;Couldn't load inpout32.dll&quot;</span>
			<span style="color: #0000ff;">FreeMemory</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">*</span>this<span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> #Null
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i ParallelPort_Destroy<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.PARALLEL_PORT <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">IsLibrary</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>Handle <span style="color: #000066;">&#41;</span>
			<span style="color: #0000ff;">CloseLibrary</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>Handle <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
		<span style="color: #0000ff;">FreeMemory</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this <span style="color: #000066;">&#41;</span>
		<span style="color: #000066;">*</span>this <span style="color: #000066;">=</span> #Null
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">;- Communication Functions</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i ParallelPort_Out<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.PARALLEL_PORT, Bits.w <span style="color: #000066;">=</span> $00 <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>LastData <span style="color: #000066;">=</span> Bits <span style="color: #000066;">&amp;</span> $FF
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #0000ff;">CallFunction</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>handle, <span style="color: #009900;">&quot;Out32&quot;</span>, <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>Port, Bits <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">EndIf</span>
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i ParallelPort_In<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.PARALLEL_PORT, Type.i <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #0000ff;">CallFunction</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>handle, <span style="color: #009900;">&quot;Inp32&quot;</span>, <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>Port <span style="color: #000066;">+</span> Type <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">EndIf</span>
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">;- Getter Functions</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i ParallelPort_GetHandle<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.PARALLEL_PORT <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>Handle
	<span style="color: #000066; font-weight: bold;">EndIf</span>
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i ParallelPort_GetPort<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.PARALLEL_PORT <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>Port
	<span style="color: #000066; font-weight: bold;">EndIf</span>
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i ParallelPort_GetLastData<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.PARALLEL_PORT <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>LastData
	<span style="color: #000066; font-weight: bold;">EndIf</span>
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">;- Helper Functions</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i ParallelPort_Clear<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.PARALLEL_PORT <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
		ParallelPort_Out<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this <span style="color: #000066;">&#41;</span> <span style="color: #ff0000; font-style: italic;">; The default data parameter is $00</span>
	<span style="color: #000066; font-weight: bold;">EndIf</span>
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>And the constants include:</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;">&nbsp;
<span style="color: #ff0000; font-style: italic;">; Read Only Status Register.</span>
#PARALLEL_PORT_STATUS_IRQ		<span style="color: #000066;">=</span> #PARALLEL_PORT_BIT2
&nbsp;
#PARALLEL_PORT_STATUS_ERROR		<span style="color: #000066;">=</span> #PARALLEL_PORT_BIT3
#PARALLEL_PORT_STATUS_SELECT		<span style="color: #000066;">=</span> #PARALLEL_PORT_BIT4
#PARALLEL_PORT_STATUS_PAPEROUT		<span style="color: #000066;">=</span> #PARALLEL_PORT_BIT5
#PARALLEL_PORT_STATUS_ACK		<span style="color: #000066;">=</span> #PARALLEL_PORT_BIT6
#PARALLEL_PORT_STATUS_BUSY		<span style="color: #000066;">=</span> #PARALLEL_PORT_BIT7
&nbsp;
<span style="color: #ff0000; font-style: italic;">; Read / Write Control Register.</span>
#PARALLEL_PORT_CONTROL_STROBE		<span style="color: #000066;">=</span> #PARALLEL_PORT_BIT0
#PARALLEL_PORT_CONTROL_LINEFEED		<span style="color: #000066;">=</span> #PARALLEL_PORT_BIT1
#PARALLEL_PORT_CONTROL_RESET		<span style="color: #000066;">=</span> #PARALLEL_PORT_BIT2 <span style="color: #ff0000; font-style: italic;">; AKA Initialize Printer OR Init.</span>
#PARALLEL_PORT_CONTROL_SELECT		<span style="color: #000066;">=</span> #PARALLEL_PORT_BIT3
&nbsp;
<span style="color: #ff0000; font-style: italic;">; These constants are icluded for completeness. All pin numbers are relative to D-Type 25, Centronics pins are between parentheses.</span>
&nbsp;
#PARALLEL_PORT_D0	<span style="color: #000066;">=</span>	#PARALLEL_PORT_BIT0			<span style="color: #ff0000; font-style: italic;">; PIN2</span>
#PARALLEL_PORT_D1	<span style="color: #000066;">=</span>	#PARALLEL_PORT_BIT1			<span style="color: #ff0000; font-style: italic;">; PIN3</span>
#PARALLEL_PORT_D2	<span style="color: #000066;">=</span>	#PARALLEL_PORT_BIT2			<span style="color: #ff0000; font-style: italic;">; PIN4</span>
#PARALLEL_PORT_D3	<span style="color: #000066;">=</span>	#PARALLEL_PORT_BIT3			<span style="color: #ff0000; font-style: italic;">; PIN5</span>
#PARALLEL_PORT_D4	<span style="color: #000066;">=</span>	#PARALLEL_PORT_BIT4			<span style="color: #ff0000; font-style: italic;">; PIN6</span>
#PARALLEL_PORT_D5	<span style="color: #000066;">=</span>	#PARALLEL_PORT_BIT5			<span style="color: #ff0000; font-style: italic;">; PIN7</span>
#PARALLEL_PORT_D6	<span style="color: #000066;">=</span>	#PARALLEL_PORT_BIT6			<span style="color: #ff0000; font-style: italic;">; PIN8</span>
#PARALLEL_PORT_D7	<span style="color: #000066;">=</span>	#PARALLEL_PORT_BIT7			<span style="color: #ff0000; font-style: italic;">; PIN9</span>
&nbsp;
#PARALLEL_PORT_C0	<span style="color: #000066;">=</span>	#PARALLEL_PORT_CONTROL_STROBE		<span style="color: #ff0000; font-style: italic;">; PIN 1</span>
#PARALLEL_PORT_C1	<span style="color: #000066;">=</span>	#PARALLEL_PORT_CONTROL_LINEFEED		<span style="color: #ff0000; font-style: italic;">; PIN 14</span>
#PARALLEL_PORT_C2	<span style="color: #000066;">=</span>	#PARALLEL_PORT_CONTROL_RESET		<span style="color: #ff0000; font-style: italic;">; PIN 16	(31)</span>
#PARALLEL_PORT_C3	<span style="color: #000066;">=</span>	#PARALLEL_PORT_CONTROL_SELECT		<span style="color: #ff0000; font-style: italic;">; PIN 17	(36)</span>
&nbsp;
#PARALLEL_PORT_S3	<span style="color: #000066;">=</span>	#PARALLEL_PORT_STATUS_ERROR		<span style="color: #ff0000; font-style: italic;">; PIN 15	(32)</span>
#PARALLEL_PORT_S4	<span style="color: #000066;">=</span>	#PARALLEL_PORT_STATUS_SELECT		<span style="color: #ff0000; font-style: italic;">; PIN 13</span>
#PARALLEL_PORT_S5	<span style="color: #000066;">=</span>	#PARALLEL_PORT_STATUS_PAPEROUT		<span style="color: #ff0000; font-style: italic;">; PIN 12</span>
#PARALLEL_PORT_S6	<span style="color: #000066;">=</span>	#PARALLEL_PORT_STATUS_ACK		<span style="color: #ff0000; font-style: italic;">; PIN 10</span>
#PARALLEL_PORT_S7	<span style="color: #000066;">=</span>	#PARALLEL_PORT_STATUS_BUSY		<span style="color: #ff0000; font-style: italic;">; PIN 11</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">; PINS 18-25 (19-30) Are all tied to GND.</span></pre></div></div>

<h3>The hardware:</h3>
<p>You don&#8217;t need any special hardware other than a Centronics or similar cable and a PC with a Parallel port (I&#8217;m sure some of you keep older PCs around for a good reason!) &#8212; Aside from this if you&#8217;re planning on running the examples you might want to get some LEDs and Switches.</p>
<h3>Something to download:</h3>
<p>The entire sources and support files including the examples can be found <a href='http://gushh.net/blog/wp-content/uploads/2011/01/ParallelPort.zip'>Here</a>. Remember to exit the demo programs with the ESC key so the program gets a chance to reset the output bits.</p>
<h3>Useful companion:</h3>
<p>During development you may want to keep an eye on the status of each pin, however not everyone has a breakout board for this particular interface so you may want to use a software version of this concept instead, one of my favourite ones is <a href="http://neil.fraser.name/software/lpt/" target="_blank">LPT.exe</a> &#8212; It works fairly well and there are several ports to different languages in case you&#8217;re interested in modifying it to suit your own needs.</p>
<h3>Closing up:</h3>
<p>That&#8217;s all for now, hopefully I&#8217;ll be able to finish the project and post some of the code here. Hint: it involves controlling unipolar steppers, computer vision and webcams!</p>
<p>Can you guess what it is?</p>
<p>Cheers.</p>
<g:plusone href='http://gushh.net/blog/parallel-port-library/'></g:plusone>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/parallel-port-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AutoComplete Library</title>
		<link>http://gushh.net/blog/autocomplete-library/</link>
		<comments>http://gushh.net/blog/autocomplete-library/#comments</comments>
		<pubDate>Sun, 15 Aug 2010 02:24:32 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PureBasic]]></category>
		<category><![CDATA[ajax autocomplete]]></category>
		<category><![CDATA[auto complete]]></category>
		<category><![CDATA[autocomplete]]></category>
		<category><![CDATA[autocomplete library]]></category>
		<category><![CDATA[autocompletion]]></category>
		<category><![CDATA[lib autocomplete]]></category>
		<category><![CDATA[pb autocomplete]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/?p=452</guid>
		<description><![CDATA[This nifty library will allow you to incorporate auto-completion in your latest game/software project without much hassle. It supports the use of a dictionary with both load and save routines, it keeps track of hits and allows to filter the suggestion using a &#8220;headroom&#8221; parameter. The &#8220;algorithm&#8221; used to populate the suggestion list is down [...]]]></description>
			<content:encoded><![CDATA[<p>This nifty library will allow you to incorporate auto-completion in your latest game/software project without much hassle. It supports the use of a dictionary with both load and save routines, it keeps track of hits and allows to filter the suggestion using a &#8220;headroom&#8221; parameter.</p>
<p>The &#8220;algorithm&#8221; used to populate the suggestion list is down to the bare basics, reason being I wanted to keep the library small and simple. In fact some &#8220;methods&#8221; were not implemented for this very same reason. But please feel free to write a helper lib and I&#8217;ll happily add a link towards it.</p>
<p>The 4.50 source:</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Structure</span> AUTOCOMPLETE_ENTRY	<span style="color: #ff0000; font-style: italic;">; This represents each word in the dictionary</span>
	word.s			<span style="color: #ff0000; font-style: italic;">; The word</span>
	length.i		<span style="color: #ff0000; font-style: italic;">; The length of the word (cached for speed)</span>
	hits.i			<span style="color: #ff0000; font-style: italic;">; Amount of hits this word received (see GetSuggestions for more information on this)</span>
<span style="color: #000066; font-weight: bold;">EndStructure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Structure</span> AUTOCOMPLETE
	DictionaryFileName.s			<span style="color: #ff0000; font-style: italic;">; Filename for the dictionary, optional.</span>
	last_input.s				<span style="color: #ff0000; font-style: italic;">; Handy last input helper</span>
	last_suggestion.s			<span style="color: #ff0000; font-style: italic;">; Likewise but it holds the last &quot;best&quot; suggestion, in case you don't want to go through the list.</span>
	List dictionary.AUTOCOMPLETE_ENTRY<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>	<span style="color: #ff0000; font-style: italic;">; Holds complete words or sentences, it is our database.</span>
	List suggestion.AUTOCOMPLETE_ENTRY<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>	<span style="color: #ff0000; font-style: italic;">; This list will be populated with results once GetSuggestion is called.</span>
<span style="color: #000066; font-weight: bold;">EndStructure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Declare</span>.i AutoComplete_LoadDictionary<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AUTOCOMPLETE, DictionaryFile.s <span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Declare</span>.i AutoComplete_SaveDictionary<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AUTOCOMPLETE, DictionaryFile.s <span style="color: #000066;">=</span> <span style="color: #009900;">&quot;&quot;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i AutoComplete_Create<span style="color: #000066;">&#40;</span> DictionaryFile.s <span style="color: #000066;">=</span> <span style="color: #009900;">&quot;&quot;</span> <span style="color: #000066;">&#41;</span>
	<span style="color: #ff0000; font-style: italic;">; Creates an instance of the AutoComplete library.</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Define</span>.AUTOCOMPLETE <span style="color: #000066;">*</span>this <span style="color: #000066;">=</span> <span style="color: #0000ff;">AllocateMemory</span><span style="color: #000066;">&#40;</span> <span style="color: #0000ff;">SizeOf</span><span style="color: #000066;">&#40;</span> AUTOCOMPLETE <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		InitializeStructure<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this, AUTOCOMPLETE <span style="color: #000066;">&#41;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> DictionaryFile
			AutoComplete_LoadDictionary<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this, DictionaryFile <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this	
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i AutoComplete_Destroy<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AUTOCOMPLETE, AutoSave.i <span style="color: #000066;">=</span> #False <span style="color: #000066;">&#41;</span>
	<span style="color: #ff0000; font-style: italic;">; Destroys an instance of the AutoComplete library.</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> AutoSave
			AutoComplete_SaveDictionary<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this <span style="color: #000066;">&#41;</span>	
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
		ClearStructure<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this, AUTOCOMPLETE <span style="color: #000066;">&#41;</span>
		<span style="color: #0000ff;">FreeMemory</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.s AutoComplete_GetSuggestion<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AUTOCOMPLETE, PartialInput.s, Headroom.i <span style="color: #000066;">=</span> <span style="color: #CC0000;">4</span> <span style="color: #000066;">&#41;</span>
	<span style="color: #ff0000; font-style: italic;">; Generates a suggestion list based on input string.</span>
	<span style="color: #ff0000; font-style: italic;">; The Headroom variable limits how bigger a suggestion can be, compared to the input length.</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066; font-weight: bold;">Define</span>.i PartialFindLength	<span style="color: #000066;">=</span> <span style="color: #CC0000;">99999</span>
		<span style="color: #000066; font-weight: bold;">Define</span>.i PartialInputLength 	<span style="color: #000066;">=</span> <span style="color: #0000ff;">Len</span><span style="color: #000066;">&#40;</span> PartialInput <span style="color: #000066;">&#41;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> PartialInputLength <span style="color: #000066;">&gt;</span> <span style="color: #CC0000;">0</span>
&nbsp;
			<span style="color: #0000ff;">ClearList</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>suggestion<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">ForEach</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
&nbsp;
				<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">Left</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>word, PartialInputLength <span style="color: #000066;">&#41;</span> <span style="color: #000066;">=</span> PartialInput
&nbsp;
					<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>word <span style="color: #000066;">=</span> PartialInput
						<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>hits <span style="color: #000066;">+</span> <span style="color: #CC0000;">1</span>
					<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
					<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>length <span style="color: #000066;">&lt;</span> PartialFindLength <span style="color: #000066;">+</span> Headroom
&nbsp;
						PartialFindLength <span style="color: #000066;">=</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>length
&nbsp;
						<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">AddElement</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>suggestion<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span>
							<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>suggestion<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> 	<span style="color: #000066;">=</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
							<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>last_suggestion 	<span style="color: #000066;">=</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>suggestion<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>word
						<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
					<span style="color: #000066; font-weight: bold;">EndIf</span>
				<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">Next</span>
&nbsp;
			<span style="color: #0000ff;">SortStructuredList</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>suggestion<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>,  #PB_Sort_Integer, <span style="color: #0000ff;">OffsetOf</span><span style="color: #000066;">&#40;</span> AUTOCOMPLETE_ENTRY<span style="color: #000066;">\</span>hits <span style="color: #000066;">&#41;</span>, #PB_Sort_Descending <span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>last_suggestion
&nbsp;
		<span style="color: #000066; font-weight: bold;">EndIf</span>
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i AutoComplete_AddWord<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AUTOCOMPLETE, Word.s, Hits.i <span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span> <span style="color: #000066;">&#41;</span>
	<span style="color: #ff0000; font-style: italic;">; Adds a word to the dictionary.</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">AddElement</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
			<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>word 	<span style="color: #000066;">=</span> Word
			<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>length 	<span style="color: #000066;">=</span> <span style="color: #0000ff;">Len</span><span style="color: #000066;">&#40;</span>Word<span style="color: #000066;">&#41;</span>
			<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>hits 	<span style="color: #000066;">=</span> Hits
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> #True
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i AutoComplete_ClearHits<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AUTOCOMPLETE <span style="color: #000066;">&#41;</span>
	<span style="color: #ff0000; font-style: italic;">; Clears all word hits from the dictionary.</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066; font-weight: bold;">ForEach</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
			<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>hits <span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span>
		<span style="color: #000066; font-weight: bold;">Next</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> #True
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i AutoComplete_LoadDictionary<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AUTOCOMPLETE, DictionaryFile.s <span style="color: #000066;">&#41;</span>
	<span style="color: #ff0000; font-style: italic;">; Loads a dictionary from a file.</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
		<span style="color: #000066; font-weight: bold;">If</span> DictionaryFile
&nbsp;
			<span style="color: #000066; font-weight: bold;">Define</span>.s sInput, sOutput
			<span style="color: #000066; font-weight: bold;">Define</span>.i Hits <span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span>
			<span style="color: #000066; font-weight: bold;">Define</span>.i fp <span style="color: #000066;">=</span> <span style="color: #0000ff;">ReadFile</span><span style="color: #000066;">&#40;</span> #PB_Any, DictionaryFile <span style="color: #000066;">&#41;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">IsFile</span><span style="color: #000066;">&#40;</span> fp <span style="color: #000066;">&#41;</span>
&nbsp;
				<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>DictionaryFileName <span style="color: #000066;">=</span> DictionaryFile
&nbsp;
				<span style="color: #000066; font-weight: bold;">While</span> <span style="color: #000066; font-weight: bold;">Not</span> <span style="color: #0000ff;">Eof</span><span style="color: #000066;">&#40;</span> fp <span style="color: #000066;">&#41;</span>
&nbsp;
					sInput <span style="color: #000066;">=</span> <span style="color: #0000ff;">ReadString</span><span style="color: #000066;">&#40;</span> fp <span style="color: #000066;">&#41;</span>
&nbsp;
					<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">FindString</span><span style="color: #000066;">&#40;</span> sInput, <span style="color: #009900;">&quot;,&quot;</span>, <span style="color: #CC0000;">1</span> <span style="color: #000066;">&#41;</span> <span style="color: #ff0000; font-style: italic;">; If there's a comma in the line, then it means we have to load the hits.</span>
&nbsp;
						sOutput <span style="color: #000066;">=</span> <span style="color: #0000ff;">StringField</span><span style="color: #000066;">&#40;</span> sInput, <span style="color: #CC0000;">1</span>, <span style="color: #009900;">&quot;,&quot;</span> <span style="color: #000066;">&#41;</span>
						Hits 	<span style="color: #000066;">=</span> <span style="color: #0000ff;">Val</span><span style="color: #000066;">&#40;</span> <span style="color: #0000ff;">StringField</span><span style="color: #000066;">&#40;</span> sInput, <span style="color: #CC0000;">2</span>, <span style="color: #009900;">&quot;,&quot;</span> <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
					<span style="color: #000066; font-weight: bold;">Else</span> <span style="color: #ff0000; font-style: italic;">; Otherwise no hits are present in the dictionary file, assume 0.</span>
&nbsp;
						sOutput <span style="color: #000066;">=</span> sInput
						Hits	<span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span>
&nbsp;
					<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
					AutoComplete_AddWord<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this, sOutput, Hits <span style="color: #000066;">&#41;</span>
				<span style="color: #000066; font-weight: bold;">Wend</span>
&nbsp;
				<span style="color: #0000ff;">CloseFile</span><span style="color: #000066;">&#40;</span> fp <span style="color: #000066;">&#41;</span>	
			<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">EndIf</span>
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i AutoComplete_SaveDictionary<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AUTOCOMPLETE, DictionaryFile.s <span style="color: #000066;">=</span> <span style="color: #009900;">&quot;&quot;</span> <span style="color: #000066;">&#41;</span>
	<span style="color: #ff0000; font-style: italic;">; Saves the dictionary to a file.</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> DictionaryFile <span style="color: #000066;">=</span> <span style="color: #009900;">&quot;&quot;</span>
			DictionaryFile <span style="color: #000066;">=</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>DictionaryFileName	
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> DictionaryFile
&nbsp;
			<span style="color: #000066; font-weight: bold;">Define</span>.s sInput, sOutput
			<span style="color: #000066; font-weight: bold;">Define</span>.i fp <span style="color: #000066;">=</span> <span style="color: #0000ff;">CreateFile</span><span style="color: #000066;">&#40;</span> #PB_Any, DictionaryFile <span style="color: #000066;">&#41;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">IsFile</span><span style="color: #000066;">&#40;</span> fp <span style="color: #000066;">&#41;</span>
&nbsp;
				<span style="color: #000066; font-weight: bold;">ForEach</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
&nbsp;
					sOutput <span style="color: #000066;">=</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>word
					sOutput <span style="color: #000066;">+</span> <span style="color: #009900;">&quot;,&quot;</span> <span style="color: #000066;">+</span> <span style="color: #0000ff;">Str</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>dictionary<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>hits <span style="color: #000066;">&#41;</span>
					<span style="color: #0000ff;">WriteStringN</span><span style="color: #000066;">&#40;</span> fp, sOutput <span style="color: #000066;">&#41;</span>
&nbsp;
				<span style="color: #000066; font-weight: bold;">Next</span>
&nbsp;
				<span style="color: #0000ff;">CloseFile</span><span style="color: #000066;">&#40;</span>fp<span style="color: #000066;">&#41;</span>	
			<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">EndIf</span>
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>And a quick example:</p>
<p>Notice: The example requires a dictionary, you may download a sample one based on english words by clicking <a href="http://gushh.net/dev/?file=pb/english.txt" target="_blank">here</a>.</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Define</span>.AUTOCOMPLETE <span style="color: #000066;">*</span>auto <span style="color: #000066;">=</span> AutoComplete_Create<span style="color: #000066;">&#40;</span> <span style="color: #009900;">&quot;english.txt&quot;</span> <span style="color: #000066;">&#41;</span> <span style="color: #ff0000; font-style: italic;">; load this dictionary for future auto completion assistance.</span>
<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>auto
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">OpenConsole</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
		<span style="color: #0000ff;">PrintN</span><span style="color: #000066;">&#40;</span><span style="color: #009900;">&quot; Type part of a word and press enter to view the suggestions&quot;</span> <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Repeat</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">Define</span>.s <span style="color: #000fff;">in</span> <span style="color: #000066;">=</span> <span style="color: #0000ff;">Input</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000fff;">in</span>
&nbsp;
				<span style="color: #000066; font-weight: bold;">If</span> AutoComplete_GetSuggestion<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>auto, <span style="color: #000fff;">in</span>, <span style="color: #CC0000;">2</span> <span style="color: #000066;">&#41;</span>
					<span style="color: #000066; font-weight: bold;">ForEach</span> <span style="color: #000066;">*</span>auto<span style="color: #000066;">\</span>suggestion<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
						<span style="color: #0000ff;">PrintN</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>auto<span style="color: #000066;">\</span>suggestion<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>word <span style="color: #000066;">+</span> <span style="color: #009900;">&quot; - &quot;</span> <span style="color: #000066;">+</span> <span style="color: #0000ff;">Str</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>auto<span style="color: #000066;">\</span>suggestion<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">\</span>hits <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
					<span style="color: #000066; font-weight: bold;">Next</span>
					<span style="color: #0000ff;">PrintN</span><span style="color: #000066;">&#40;</span><span style="color: #009900;">&quot;&quot;</span><span style="color: #000066;">&#41;</span>
				<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
			<span style="color: #0000ff;">Delay</span><span style="color: #000066;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Until</span> <span style="color: #0000ff;">Inkey</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">=</span> <span style="color: #0000ff;">Chr</span><span style="color: #000066;">&#40;</span><span style="color: #CC0000;">27</span><span style="color: #000066;">&#41;</span> <span style="color: #000066; font-weight: bold;">Or</span> <span style="color: #000fff;">in</span> <span style="color: #000066;">=</span> <span style="color: #009900;">&quot;exit&quot;</span>
&nbsp;
		<span style="color: #0000ff;">CloseConsole</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
	AutoComplete_Destroy<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>auto, #True <span style="color: #000066;">&#41;</span> <span style="color: #ff0000; font-style: italic;">; Because we loaded a dictionary, if we pass True as the second parameter, the dictionary will be saved before the instance is destroyed.</span>
<span style="color: #000066; font-weight: bold;">EndIf</span></pre></div></div>

<p>Another good point about this library is that it's 100% cross-platform, so you're not depending on the OS to auto-complete your fields and like I said before, you could easily implement it in your game, etc. A bad point is that it includes no error handling at the moment.</p>
<p>That's it for now, you can download the entire source <a href="http://gushh.net/dev/?file=pb/AutoComplete.pb" target="_blank">here</a>. And a sample dictionary <a href="http://gushh.net/dev/?raw=1&#038;file=pb/english.txt" target="_blank">here</a>.</p>
<p>Cheers!</p>
<g:plusone href='http://gushh.net/blog/autocomplete-library/'></g:plusone>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/autocomplete-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: gushh.net @ 2012-02-09 09:24:22 by W3 Total Cache -->
