tag:blogger.com,1999:blog-30570398207164154482024-03-06T20:01:52.463+00:00DifusalComputers, Programming Tutorials and more!Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.comBlogger52125tag:blogger.com,1999:blog-3057039820716415448.post-19915486075824273742018-04-23T23:50:00.000+01:002018-04-23T23:51:25.681+01:00Moving from Blogspot to GitHub PagesHello, people who still take time to read this blog!<br>
<br>
I am done with Blogger/Blogspot... I am switching to <a href="https://pages.github.com/">GitHub Pages</a>.<br>
Worry not, the contents of this blog will stay here (as long as the Blogger platform exists, that is).<br>
<br>
My new blog can be found at: <a href="https://ferrolho.github.io/blog/">https://ferrolho.github.io/blog/</a><br>
<br>
<i>Goodbye, World!</i><br>
- henrique ferrolhoDifusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-74918040327340774842015-06-25T13:19:00.000+01:002015-06-25T13:19:11.534+01:00How to install Eclipse Mars 4.5 on Ubuntu<h2>
<span style="font-family: Trebuchet MS, sans-serif;">Installing Eclipse</span></h2>
<ol>
<li><span style="font-family: Trebuchet MS, sans-serif;">Download Eclipse from <a href="http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/marsr" target="_blank">here</a></span></li>
<li><span style="font-family: Trebuchet MS, sans-serif;">Extract it and you will end up having a folder named eclipse
<br />
<br />
</span><div class="separator" style="clear: both;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKy-EFwbaT2A9k1CFsPjyue6mN8RF3BBaVLM0oO8Gb9-B2XNrg5BJAK5g9jjIm9VR1gIf12ThLpHXS-G0z6AoFbfgcqvrez5QWF0xe36WOKJDcI3V0LErVBTxUkAM6-OexuA3r6HY-6b4/s1600/Screenshot+from+2015-06-25+12%253A44%253A22.png" imageanchor="1"><span style="font-family: Trebuchet MS, sans-serif;">
<img border="0" height="283" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKy-EFwbaT2A9k1CFsPjyue6mN8RF3BBaVLM0oO8Gb9-B2XNrg5BJAK5g9jjIm9VR1gIf12ThLpHXS-G0z6AoFbfgcqvrez5QWF0xe36WOKJDcI3V0LErVBTxUkAM6-OexuA3r6HY-6b4/s400/Screenshot+from+2015-06-25+12%253A44%253A22.png" width="400" /></span></a></div>
<br />
</li>
<li><span style="font-family: Trebuchet MS, sans-serif;">Save it wherever you wish</span></li>
<ul>
<li><span style="font-family: Trebuchet MS, sans-serif;">I saved mine in </span><span style="font-family: Courier New, Courier, monospace;">~/Programs/eclipse
</span></li>
</ul>
<br />
<div class="separator" style="clear: both;">
<span style="font-family: Trebuchet MS, sans-serif;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghdV5lHU4UxgZaJQ8Msc0fezxfYURdhayUSDuvxF_pwvrke4KeSP-XheYCUCcv9fzGuscBQQ5hBOEwi6YvTrMaqstmAWPGeOL2WEYIuMH_mZqLPWLIDsnMh8LOw_9OGpal-wIuMZ2Fg28/s1600/Screenshot+from+2015-06-25+12%253A46%253A16.png" imageanchor="1">
<img border="0" height="283" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghdV5lHU4UxgZaJQ8Msc0fezxfYURdhayUSDuvxF_pwvrke4KeSP-XheYCUCcv9fzGuscBQQ5hBOEwi6YvTrMaqstmAWPGeOL2WEYIuMH_mZqLPWLIDsnMh8LOw_9OGpal-wIuMZ2Fg28/s400/Screenshot+from+2015-06-25+12%253A46%253A16.png" width="400" /></a>
</span></div>
<br />
</ol>
<h2>
<span style="font-family: Trebuchet MS, sans-serif;">
Adding a launcher shortcut</span></h2>
<ol>
<li><span style="font-family: Trebuchet MS, sans-serif;">Press </span><b><span style="font-family: Courier New, Courier, monospace;">CTRL+ALT+T</span></b><span style="font-family: Trebuchet MS, sans-serif;"> to open Terminal</span></li>
<li><span style="font-family: Trebuchet MS, sans-serif;">Run the following commands:</span></li>
<br />
<span style="font-family: Courier New, Courier, monospace;">sudo ln -s <b>*eclipse path*</b> /usr/bin/eclipse</span>
<br />
<ul>
<li><span style="font-family: Trebuchet MS, sans-serif;">make sure you replace </span><span style="font-family: Courier New, Courier, monospace;"><b>*eclipse path*</b> </span><span style="font-family: Trebuchet MS, sans-serif;">with the location where you previously saved eclipse - in my case it is </span><b><span style="font-family: Courier New, Courier, monospace;">~/Programs/eclipse/eclipse</span></b></li>
</ul>
<br />
<span style="font-family: Courier New, Courier, monospace;">sudo gedit /usr/share/applications/eclipse.desktop</span>
<br />
<br />
<li><span style="font-family: Trebuchet MS, sans-serif;">After these commands, gedit - Ubuntu's default text editor - should have opened</span></li>
<li><span style="font-family: Trebuchet MS, sans-serif;">Copy the following text and paste it there:</span></li>
<br />
<span style="font-family: Courier New, Courier, monospace;">
[Desktop Entry]<br />
Name=Eclipse<br />
Type=Application<br />
Exec=/home/henrique/Programs/eclipse/eclipse<br />
Terminal=false<br />
Icon=/home/henrique/Programs/eclipse/icon.xpm<br />
Comment=Integrated Development Environment<br />
NoDisplay=false<br />
Categories=Development;IDE;<br />
Name[en]=Eclipse<br />
X-Desktop-File-Install-Version=0.22</span><br />
<br />
<ul>
<li><span style="font-family: Trebuchet MS, sans-serif;">You need to replace the <b>Exec</b> and <b>Icon</b> paths to the location where you previously saved eclipse</span></li>
<br />
</ul>
<span style="font-family: Trebuchet MS, sans-serif;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifoMx3vwxITF7fSKPHFaKeOrCPW7TsVbCfLU5qPh0HFf-UwBB9HJ-Yoy7GQ9aQZvOlaMuwZPZnJLL24kFzXPrEOLcwErNMRYoMAf6KdhFLlgNOjWDIdt0uxWp-E-yXU48h9YHQ_wb_lMY/s1600/Screenshot+from+2015-06-25+13%253A06%253A42.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifoMx3vwxITF7fSKPHFaKeOrCPW7TsVbCfLU5qPh0HFf-UwBB9HJ-Yoy7GQ9aQZvOlaMuwZPZnJLL24kFzXPrEOLcwErNMRYoMAf6KdhFLlgNOjWDIdt0uxWp-E-yXU48h9YHQ_wb_lMY/s400/Screenshot+from+2015-06-25+13%253A06%253A42.png" /></a>
</span>
<br />
<br />
<li><span style="font-family: Trebuchet MS, sans-serif;"><u>Save</u> the text file and close <u>gedit</u></span></li>
<li><span style="font-family: Trebuchet MS, sans-serif;">A shortcut should have been successfully created
<br />
<br />
</span><div class="separator" style="clear: both;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEhzD3XAwaUYvlGVoHVrC1ybXI4keULSRCQIfNj12tJyeXJz30T9tpx-OmtQQGOv_upotf0PjVHdz338rk9MPwdxHfOdQjArEphiPiAOy9Hg9vTalaf_iOqav42bphfu18TGzyvKtYdnw/s1600/Screenshot+from+2015-06-25+13%253A02%253A32.png" imageanchor="1"><span style="font-family: Trebuchet MS, sans-serif;">
<img border="0" height="81" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEhzD3XAwaUYvlGVoHVrC1ybXI4keULSRCQIfNj12tJyeXJz30T9tpx-OmtQQGOv_upotf0PjVHdz338rk9MPwdxHfOdQjArEphiPiAOy9Hg9vTalaf_iOqav42bphfu18TGzyvKtYdnw/s400/Screenshot+from+2015-06-25+13%253A02%253A32.png" width="400" /></span></a></div>
</li>
<br />
<li><span style="font-family: Trebuchet MS, sans-serif;"><u>You may have to log off</u> and back in for it to show up</span></li>
</ol>Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com34tag:blogger.com,1999:blog-3057039820716415448.post-1572179364982098512014-10-22T18:35:00.000+01:002014-10-30T09:28:40.822+00:00How to connect two virtual machines through a virtual serial port<h1>
Introduction</h1>
After this tutorial you will have set up two linux virtual machines connected through a RS232 virtual serial port.<br />
<br />
<b>VirtualBox</b> will be the program used to run the virtual machines and both of them will be running <a href="http://lubuntu.net/">Lubuntu</a>, which is a very light version of <a href="http://www.ubuntu.com/">Ubuntu</a>.<br />
<br />
I will be using Ubuntu during this setup, but the process is very similar if you are using Windows.<br />
<br />
If you are not interested in creating a virtual serial port using virtual machines, you can emulate a virtual serial port in ubuntu itself using <b>socat</b> with a command similar to the following:<br />
<blockquote>
sudo printf "Opening virtual serial port... Done.\nPress CTRL+C at any time to close it.\n"; sudo socat PTY,link=/dev/ttyS0 PTY,link=/dev/ttyS4; printf "\nClosing virtual serial port... Done.\n"</blockquote>
<br />
<h1>
Install VirtualBox</h1>
First, you will need to install <a href="https://www.virtualbox.org/">VirtualBox</a>. Open a terminal - <b>CTRL+ALT+T</b> - and enter the following command:<br />
<blockquote>
sudo apt-get install virtualbox</blockquote>
After this, you should have successfully installed VirtualBox:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAm_v7Lqpi-PRtLd-M5di5sxKuC35nergJGSg_1CZhicknSB6A3q6xvuMv7MFqRS2b56_qikRkWeQzyPsoF5Pg8RCmWrCYNtp9KLjPa4ur_uIWeDO3Gqd65lZhNVXPYmAgiYzuQ3onLec/s1600/Screenshot+from+2014-10-22+13:19:09.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAm_v7Lqpi-PRtLd-M5di5sxKuC35nergJGSg_1CZhicknSB6A3q6xvuMv7MFqRS2b56_qikRkWeQzyPsoF5Pg8RCmWrCYNtp9KLjPa4ur_uIWeDO3Gqd65lZhNVXPYmAgiYzuQ3onLec/s400/Screenshot+from+2014-10-22+13:19:09.png" /></a><br />
<br />
<h1>
Get Lubuntu</h1>
As I said before, both virtual machines will be running Lubuntu. Download the iso <a href="https://help.ubuntu.com/community/Lubuntu/GetLubuntu">here</a>. Select the <b>PC 64bits - Standard image disc</b> (lower left button):<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwNN0S8p5z7G87mWP8L9qQQB-jercjIu0rILpZd9ZoaGcEbJNfAwhI0FUMEDPBrlyZQ1JollhZxRD3U9uatEJzNL97Xiy_cbqHj108r84Chf2SLHkOIT_Apj6tyLZCOM8Wz4E6xNStWuc/s1600/Screenshot+from+2014-10-22+13:27:28.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwNN0S8p5z7G87mWP8L9qQQB-jercjIu0rILpZd9ZoaGcEbJNfAwhI0FUMEDPBrlyZQ1JollhZxRD3U9uatEJzNL97Xiy_cbqHj108r84Chf2SLHkOIT_Apj6tyLZCOM8Wz4E6xNStWuc/s400/Screenshot+from+2014-10-22+13:27:28.png" /></a><br />
<br />
<h1>
Creating two virtual machines</h1>
Open VirtualBox and create a new virtual machine:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhC7NpCzttKhYjm6qLUXVMwwIe8fNZsnQ2_B7ff-Z55Vmst9JSv-Vsqq1GVFLvXxcgO_Q4hmCQ1gYC44_RDye_RuQzFyVioq8-1JlS6PhkaGHuSwwwjGWJgG79tKaoKecLX3GzVARyQsqA/s1600/Screenshot+from+2014-10-22+17:55:03.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhC7NpCzttKhYjm6qLUXVMwwIe8fNZsnQ2_B7ff-Z55Vmst9JSv-Vsqq1GVFLvXxcgO_Q4hmCQ1gYC44_RDye_RuQzFyVioq8-1JlS6PhkaGHuSwwwjGWJgG79tKaoKecLX3GzVARyQsqA/s400/Screenshot+from+2014-10-22+17:55:03.png" /></a><br />
<br />
Name it <b>lubuntu1</b> and select the type <b>Linux</b> and version <b>Other Linux (64-bit)</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNozBbE0dER49iCHZ9GE6gd3VAlimR-gFO7oaRHxZuL6hRiVtbqbWzUJcGTpYvg5SrlPMU9n2H77KSFUDuO5-LiBrOoT8W8fyRkHkH3EC6NR1F4lyeBky81l6QxS_xU_py4S3yAXIocMc/s1600/Screenshot+from+2014-10-22+17:55:20.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNozBbE0dER49iCHZ9GE6gd3VAlimR-gFO7oaRHxZuL6hRiVtbqbWzUJcGTpYvg5SrlPMU9n2H77KSFUDuO5-LiBrOoT8W8fyRkHkH3EC6NR1F4lyeBky81l6QxS_xU_py4S3yAXIocMc/s400/Screenshot+from+2014-10-22+17:55:20.png" /></a><br />
<br />
Press <b>next</b> without modifying anything else and finally press <b>create</b>.<br />
<br />
<b>Repeat</b> the same process to create a second virtual machine but this time call it <b>lubuntu2</b>.<br />
<br />
<h1>
Installing Lubuntu on the virtual machines</h1>
Click on the first virtual machine <b>settings</b> and go to the <b>storage</b> tab. Press the disk icon with a green plus sign:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqUZdI54OMXl4gJsjl5i7SlTZmfaY97BSLpfPqLRV5ubJyMC9TOccHaY4qE_HhkOdtmyz6rz7PPpoBuRQYGIRM5nMuOXVmSlpnVHv-0qLBC3cNROIkF2TIlCqDWVOqa6sVY-Lu3sMHhAo/s1600/Screenshot+from+2014-10-22+18:05:54.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqUZdI54OMXl4gJsjl5i7SlTZmfaY97BSLpfPqLRV5ubJyMC9TOccHaY4qE_HhkOdtmyz6rz7PPpoBuRQYGIRM5nMuOXVmSlpnVHv-0qLBC3cNROIkF2TIlCqDWVOqa6sVY-Lu3sMHhAo/s400/Screenshot+from+2014-10-22+18:05:54.png" /></a><br />
<br />
Select choose disk:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyl2BWzrN6uDOtB39Iy8SC_xV_0bvE9cJGE9y7sqrZEBTLWRGyhnKZHUxgeconFIzNXBFFd9LdvaN3htmDndxsbtel-LTEI7n75q9RlaVZjqOAiQCB65YLkF3WZVRwfnIT6XRYxSOUgiM/s1600/Screenshot+from+2014-10-22+18:03:41.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyl2BWzrN6uDOtB39Iy8SC_xV_0bvE9cJGE9y7sqrZEBTLWRGyhnKZHUxgeconFIzNXBFFd9LdvaN3htmDndxsbtel-LTEI7n75q9RlaVZjqOAiQCB65YLkF3WZVRwfnIT6XRYxSOUgiM/s400/Screenshot+from+2014-10-22+18:03:41.png" /></a><br />
<br />
Browse to and select the lubuntu image disk (.iso) you downloaded:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjv7HztYlb7ajR4KKjq3rHd-G_MCiMYdD2btzSl96BrqXSP8ckbkx6B1A4boW_KqfB86tm95h-i32G-hL5Zqku4Fx-FcOtitsPp5HO4dL5ePQGCiU_38ezu031lG-kb_Q8XIPzD4zPggXA/s1600/Screenshot+from+2014-10-22+18:03:49.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjv7HztYlb7ajR4KKjq3rHd-G_MCiMYdD2btzSl96BrqXSP8ckbkx6B1A4boW_KqfB86tm95h-i32G-hL5Zqku4Fx-FcOtitsPp5HO4dL5ePQGCiU_38ezu031lG-kb_Q8XIPzD4zPggXA/s400/Screenshot+from+2014-10-22+18:03:49.png" /></a><br />
<br />
There should appear a new entry on the <b>Storage Tree</b> now:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJkMsJDs1JF0Q4xUwcH4VbjdQJN4WWHrEvC4PCFg3Je2Lk99cfdpMpkcSCLa1lyb6cB_ffMXRHF0Vt0i6_lTDg1yzlFLULLOINq8rSNGAGJ_MdNzv6jzYRYUdlkHlopn9B_SLE7dR-5qM/s1600/Screenshot+from+2014-10-22+18:03:33.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJkMsJDs1JF0Q4xUwcH4VbjdQJN4WWHrEvC4PCFg3Je2Lk99cfdpMpkcSCLa1lyb6cB_ffMXRHF0Vt0i6_lTDg1yzlFLULLOINq8rSNGAGJ_MdNzv6jzYRYUdlkHlopn9B_SLE7dR-5qM/s400/Screenshot+from+2014-10-22+18:03:33.png" /></a><br />
<br />
Press OK and repeat for the other virtual machine.<br />
After you are done, go ahead and start both virtual machines: the Lubuntu setup should start. Follow the onscreen instructions to install Lubuntu on the virtual machines. When the installation finishes, shutdown both virtual machines.<br />
<br />
<h1>
Connecting the virtual machines with a virtual serial port</h1>
Open both virtual machine <b>settings</b> and browse to the <b>Serial Ports</b> tab.<br />
<br />
Modify the Port 1 settings as follows - <u>notice that serial port settings differ from lubuntu1 to lubuntu2</u>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwEEQYyF8UIFI8MgRQpBc15wnyI51g_c5wCR3cbSpj_ZWoVHiL2YGjL7Bjrg2ab3m0biBbvrCQFzUooDt2M5DWqz_to4gt4v4yjSmOOC-LRO4QOusY7xjn7C-qdJIlFBkknU3Lxte1HQs/s1600/Screenshot+from+2014-10-22+18:22:07.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwEEQYyF8UIFI8MgRQpBc15wnyI51g_c5wCR3cbSpj_ZWoVHiL2YGjL7Bjrg2ab3m0biBbvrCQFzUooDt2M5DWqz_to4gt4v4yjSmOOC-LRO4QOusY7xjn7C-qdJIlFBkknU3Lxte1HQs/s400/Screenshot+from+2014-10-22+18:22:07.png" /></a><br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6GB6k4ZWJJyZt39SfM2CGNVGPHFgImDWIUtTb1sD60I4EFk56TqiT_RmLdM9jQbcfnmfzGwvfQee-sL6xFQHUIAPwKsFjXO6YsjSL6mys4z13k9bbvsmQbQ6SWynDs6pDbSqMsMs3s9c/s1600/Screenshot+from+2014-10-22+18:22:40.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6GB6k4ZWJJyZt39SfM2CGNVGPHFgImDWIUtTb1sD60I4EFk56TqiT_RmLdM9jQbcfnmfzGwvfQee-sL6xFQHUIAPwKsFjXO6YsjSL6mys4z13k9bbvsmQbQ6SWynDs6pDbSqMsMs3s9c/s400/Screenshot+from+2014-10-22+18:22:40.png" /></a><br />
<br />
<h1>
Testing the virtual serial port</h1>
After having set both serial ports, boot both virtual machines.<br />
<br />
In order to test the connection, install <b>gtkterm</b> on both machines by executing the following command:<br />
<blockquote>sudo apt-get install gtkterm</blockquote>
After installing gtkterm, open a terminal on both machines and type:<br />
<blockquote>sudo gtkterm</blockquote>
If everything was set up correctly, as soon as you type anything on one terminal, the terminal on the other virtual machine should output what you typed.Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com2tag:blogger.com,1999:blog-3057039820716415448.post-54889989097066881082014-09-28T17:54:00.000+01:002014-09-28T17:58:39.723+01:00How to install SICStus on Ubuntu<h1>Installing SICStus</h1>
In order to download and install SICStus, you will need a <b>license</b>. If you are a student at <b>FEUP</b>, you can use the ones provided to the FEUP community. All the required keys are located in the <b>software repository</b>, so go ahead and connect to <b>VPN-FEUP</b>. If you do not know how to do this, check my <a href="http://difusal.blogspot.pt/2014/09/how-to-connect-to-feup-vpn-using-ubuntu.html">previous tutorial</a>.<br />
Afterwards, open following text file:<br />
<blockquote>
<b>//software/Publico/Outros/Departamentos/DEI/Sicstus_Prolog/Sicstus_Prolog_4.3/Sictsus_Prolog_Instrucoes_para_download_e _codigos_estudante.txt</b></blockquote>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBxSPkWnnVVot9pEp1eBpMXMauJ4rkE6yR73Z_HYFnz8hqIDjQJKVsNpQDlMFHx7Z1SgQ-sC1ZcqckSYk-UtgTgmXLo4O_NL0ERSukHyZ4itnWJEpKOwQb_b_m04MCio9UvyavImt0-gE/s1600/Screenshot+from+2014-09-28+15:44:26.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBxSPkWnnVVot9pEp1eBpMXMauJ4rkE6yR73Z_HYFnz8hqIDjQJKVsNpQDlMFHx7Z1SgQ-sC1ZcqckSYk-UtgTgmXLo4O_NL0ERSukHyZ4itnWJEpKOwQb_b_m04MCio9UvyavImt0-gE/s400/Screenshot+from+2014-09-28+15:44:26.png" /></a><br />
<br />
Go to <a href="https://sicstus.sics.se/download4.html">SICStus Prolog download page</a> and download the latest version for your system:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirv3Cd0cuc8-v7hPULbBEWRV2M0wzrAD7sxqmt_PuwU6WDGmMzqi_yeRn-W0zvdnkgiFyZxc35YgY3gftibjehXCzKG4VANc6k5U-KLkkIPu2ETHdDY0xZ-G-QFon5wxWpyZPCuJ_JIqY/s1600/Screenshot+from+2014-09-28+15:27:32.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirv3Cd0cuc8-v7hPULbBEWRV2M0wzrAD7sxqmt_PuwU6WDGmMzqi_yeRn-W0zvdnkgiFyZxc35YgY3gftibjehXCzKG4VANc6k5U-KLkkIPu2ETHdDY0xZ-G-QFon5wxWpyZPCuJ_JIqY/s400/Screenshot+from+2014-09-28+15:27:32.png" /></a><br />
<br />
When prompted for a user and password, use the ones located on line number 7 from the previously opened text file:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBCSwqTjuUso5Ttrk2WS0MrI1OFi4ugDuzjPMFoUk8CnHceAoXqRgj66iKjmjKXPE6MPbpvl7v6nvnHWmI1yVrPuduEayFqg-N4_1n_qGrWhDohc-x2m-PiwtPxzotk9TwkOuLIZ0Qte8/s1600/Screenshot+from+2014-09-28+16:48:09.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBCSwqTjuUso5Ttrk2WS0MrI1OFi4ugDuzjPMFoUk8CnHceAoXqRgj66iKjmjKXPE6MPbpvl7v6nvnHWmI1yVrPuduEayFqg-N4_1n_qGrWhDohc-x2m-PiwtPxzotk9TwkOuLIZ0Qte8/s400/Screenshot+from+2014-09-28+16:48:09.png" /></a><br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjS0hetdxpGP6ZOne5A01EgtT1EzoDsaYgNfjaNX8gzj0Sh_sZ4FKoOa7pUvEz8fFotvPr9j1cg17_nDRnHnK7FW9rKuNaMhFaKWE2dnI32ZzPpiV24AjBGh0QStlKMmfWiBXGC-JvFigI/s1600/Screenshot+from+2014-09-28+16:49:01.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjS0hetdxpGP6ZOne5A01EgtT1EzoDsaYgNfjaNX8gzj0Sh_sZ4FKoOa7pUvEz8fFotvPr9j1cg17_nDRnHnK7FW9rKuNaMhFaKWE2dnI32ZzPpiV24AjBGh0QStlKMmfWiBXGC-JvFigI/s400/Screenshot+from+2014-09-28+16:49:01.png" /></a><br />
<br />
Press <b>CTRL+ALT+T</b> to open the <b>Terminal</b>, navigate to the <b>Downloads</b> folder and <b>extract</b> the downloaded file using the following comand:<br />
<blockquote>
<b>cat sp-4.3.0-x86_64-linux-glibc2.5.tar.gz | gzip -cd |tar xf -</b></blockquote><br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPXvwmYoj8I9AQjW0FYN8n2w052MJBAxkcO_XeawoGP-vaxtVv9k6obCStb-Dj3Qydkgk6aklg59pKLGHwhkcqY8Kd2NqVSNUWIxCY3lHVemBGLutciwdO0Ori-tnANQWyjRSpvkSK73o/s1600/Screenshot+from+2014-09-28+16:55:03.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPXvwmYoj8I9AQjW0FYN8n2w052MJBAxkcO_XeawoGP-vaxtVv9k6obCStb-Dj3Qydkgk6aklg59pKLGHwhkcqY8Kd2NqVSNUWIxCY3lHVemBGLutciwdO0Ori-tnANQWyjRSpvkSK73o/s400/Screenshot+from+2014-09-28+16:55:03.png" /></a><br />
<br />
Navigate to the extracted folder and run SICStus installer using the command:<br />
<blockquote>
<b>sudo ./InstallSICStus</b></blockquote><br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg56VKgY0jiyCUuOWkDsEv_TbmOskmOlgbU5rGGXqZDR6kg6Eb7tOvHN5zbM052RunFx1USgFV3cQnSQOXw0bYlA-WJKBR3oRm44ySTIzkup2g-gPzF5LN8poDscygEdGos40z7Sl_fpFc/s1600/Screenshot+from+2014-09-28+17:00:34.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg56VKgY0jiyCUuOWkDsEv_TbmOskmOlgbU5rGGXqZDR6kg6Eb7tOvHN5zbM052RunFx1USgFV3cQnSQOXw0bYlA-WJKBR3oRm44ySTIzkup2g-gPzF5LN8poDscygEdGos40z7Sl_fpFc/s400/Screenshot+from+2014-09-28+17:00:34.png" /></a><br />
<br />
The installer should start. When prompted with the question <b>below</b>, press <b>Enter</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlNeilNQUjneeWGIdZlgwppHNAyNXrrwCfH57qf3UOgXRz7Zx5tXcY1kzRD5Wn0wV0t_l_Y4oR71SOLHEcG4wuByDtrSJ7QHD-LNsZME5ZqeTUsTm7qXxS7VTE1nn3vd6lKdlm0GZWFII/s1600/Screenshot+from+2014-09-28+17:00:44.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlNeilNQUjneeWGIdZlgwppHNAyNXrrwCfH57qf3UOgXRz7Zx5tXcY1kzRD5Wn0wV0t_l_Y4oR71SOLHEcG4wuByDtrSJ7QHD-LNsZME5ZqeTUsTm7qXxS7VTE1nn3vd6lKdlm0GZWFII/s400/Screenshot+from+2014-09-28+17:00:44.png" /></a><br />
<br />
Press <b>Enter</b> again to install SICStus in /usr/local/sicstus4.3.0.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcd2ysiZo0ipUMGDIsFyRj8iHNMMeQN13DXq7j9Uw6Ajfz1VpWITCOMHlK1rvDzShFZobKjTm5AkQLfQp4gogFjsAIVCq30bCUbFICJn7KTD8erS6eydnukZew0-bLJUL6rSEbEZDKoVA/s1600/Screenshot+from+2014-09-28+17:00:50.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcd2ysiZo0ipUMGDIsFyRj8iHNMMeQN13DXq7j9Uw6Ajfz1VpWITCOMHlK1rvDzShFZobKjTm5AkQLfQp4gogFjsAIVCq30bCUbFICJn7KTD8erS6eydnukZew0-bLJUL6rSEbEZDKoVA/s400/Screenshot+from+2014-09-28+17:00:50.png" /></a><br />
<br />
Press <b>Enter</b> again to confirm:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeupoG7-OUw1nTSVVHOH243iszkOCyPdvVIO9AM83nTDa6y54gQ3UOv9UNuqnpHLJ3thDeQ08caa_ALx1FY4DbzzLdpfjQK0cHqqfY_b3PUicqz8rQAUERzQQwXxCrdd80A6aklxMM9bc/s1600/Screenshot+from+2014-09-28+17:02:00.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeupoG7-OUw1nTSVVHOH243iszkOCyPdvVIO9AM83nTDa6y54gQ3UOv9UNuqnpHLJ3thDeQ08caa_ALx1FY4DbzzLdpfjQK0cHqqfY_b3PUicqz8rQAUERzQQwXxCrdd80A6aklxMM9bc/s400/Screenshot+from+2014-09-28+17:02:00.png" /></a><br />
<br />
Now you will be prompted to enter some keys. Go to the text file we have previously opened: <b>lines 54 to 57</b> have the information you need.<br />
<br />
Afterwards, since we will not need any extra modules, answer <b>no</b> to the next <b>four</b> questions. In the <b>previous</b> questions, we pressed <b>Enter</b> to answer <b>yes</b>, but to answer <b>no</b> you really need to type <b>no</b> and only then press <b>Enter</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirEobEPwC5kpInHW3C0GKq67hK5Slm4Erj1M-MyX5mysvKr5JEFZAgcJtTXeqd4aWzcInOVpEZiEz54eJm7tpILPg5LmMcq0aPP30_ouD7UFbBvjCuKS6UIRyQyBgHCbDJpsJuDMMSZQ4/s1600/Screenshot+from+2014-09-28+17:03:13.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirEobEPwC5kpInHW3C0GKq67hK5Slm4Erj1M-MyX5mysvKr5JEFZAgcJtTXeqd4aWzcInOVpEZiEz54eJm7tpILPg5LmMcq0aPP30_ouD7UFbBvjCuKS6UIRyQyBgHCbDJpsJuDMMSZQ4/s400/Screenshot+from+2014-09-28+17:03:13.png" /></a><br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8hBcsQY2FfJhtsfSUF2hHJBDnU7p8FH1_h3CrDWxBgwQw-VdtY2D2a8xouw0Bsfr3F_QoozrCWo73Xv3Pcka3CYtSiX7atyfoRDgMLshVGgyWfandJqEA0L8bDn8ODBFzDqCpcg8TLqI/s1600/Screenshot+from+2014-09-28+17:03:23.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8hBcsQY2FfJhtsfSUF2hHJBDnU7p8FH1_h3CrDWxBgwQw-VdtY2D2a8xouw0Bsfr3F_QoozrCWo73Xv3Pcka3CYtSiX7atyfoRDgMLshVGgyWfandJqEA0L8bDn8ODBFzDqCpcg8TLqI/s400/Screenshot+from+2014-09-28+17:03:23.png" /></a><br />
<br />
When the installer finishes, it should look something like this:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcUEokRHhCOtvmYr9DgIxLlwZtuWG0EQJckXGGx_ETltIKQEM3EYCloLy8SB8jALgZ8vJgNbTe20X3NY0yViMeVQEuKZnwvJGpPw5BdQS8wfYMjs-lDTp4DNWSwz-HOLsftHzyHnlWv5E/s1600/Screenshot+from+2014-09-28+17:04:51.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcUEokRHhCOtvmYr9DgIxLlwZtuWG0EQJckXGGx_ETltIKQEM3EYCloLy8SB8jALgZ8vJgNbTe20X3NY0yViMeVQEuKZnwvJGpPw5BdQS8wfYMjs-lDTp4DNWSwz-HOLsftHzyHnlWv5E/s400/Screenshot+from+2014-09-28+17:04:51.png" /></a><br />
<br />
<h1>Installing rlwrap</h1>
We are almost done. In order to use the arrow keys and history in SICStus, we will need to install <b>rlwrap</b>: execute the following command on the <b>terminal</b>:<br />
<blockquote><b>sudo apt-get install rlwrap</b></blockquote>
<h1>Creating a link to SICStus</h1>
At this point, in order to run SICStus, we need to open the terminal, navigate to /usr/local/sicstus4.3.0 and only then run sicstus. This is quite exhausting.<br />
At some point, it will be useful to open the terminal and be able to execute SICStus right away just by typing sicstus - this is very easy to achieve.<br />
<br />
Open the terminal and type the following command:<br />
<blockquote><b>sudo ln -s /usr/local/sicstus4.3.0/bin/sicstus /usr/bin/sicstus</b></blockquote>
After that, you can type sicstus from anywhere and SICStus will start:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOODZ8fDPuAkzN4gTHRFxGH8t_53Ye9ff1Wn-j4X8c_vpvzwR2j6DQm1MrCdL26uZZyJPF1e2VyOj9We7-Gbqutpmd_FLV2y4UAb5lUb0L57y67Vxb29nBnvkYGZzFSKd05kp1CNMR7dA/s1600/Screenshot+from+2014-09-28+17:38:31.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOODZ8fDPuAkzN4gTHRFxGH8t_53Ye9ff1Wn-j4X8c_vpvzwR2j6DQm1MrCdL26uZZyJPF1e2VyOj9We7-Gbqutpmd_FLV2y4UAb5lUb0L57y67Vxb29nBnvkYGZzFSKd05kp1CNMR7dA/s400/Screenshot+from+2014-09-28+17:38:31.png" /></a><br />
<br />
To <b>exit</b> SICStus, press <b>CTRL+D</b>.<br />
<br />
<h1>Running SICStus</h1>
You may notice that when you run SICStus, you won't be able to use the arrow keys and weird stuff like <b>^[[A</b> will start to come up instead:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiknD6q-adW2u5QbkipMHMeawgX54QtXzmSDYfX84mvgilSXgIAXKuQkDeDH-FCzg_aAM18tXqoCbIFE0Fx6_qmkcRaGW-WdCfg9sc80lqEjFjbaISeXk-eWt8VzCSll6R3QWWyVeGY2FI/s1600/Screenshot+from+2014-09-28+17:45:20.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiknD6q-adW2u5QbkipMHMeawgX54QtXzmSDYfX84mvgilSXgIAXKuQkDeDH-FCzg_aAM18tXqoCbIFE0Fx6_qmkcRaGW-WdCfg9sc80lqEjFjbaISeXk-eWt8VzCSll6R3QWWyVeGY2FI/s400/Screenshot+from+2014-09-28+17:45:20.png" /></a><br />
<br />
That's really annoying and that's why we installed <b>rlwrap</b>. The correct way to launch SICStus is to type the following on the terminal:<br />
<blockquote><b>rlwrap sicstus</b></blockquote>
That way you will be able to use the arrow keys like expected:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-XLPnJ2v6m9om5Lan3z2zCLLf5wWAL5qkW8OlMwLS3dOjulNaV1VrXlWffCbA7JYWO68w0l_LkvZKEUcA4Zew16WCawz48O2nOvImZEpgGTgmNWtKjuHla0ggYjfJIGt0UP6Jxfw_OYU/s1600/Screenshot+from+2014-09-28+17:46:40.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-XLPnJ2v6m9om5Lan3z2zCLLf5wWAL5qkW8OlMwLS3dOjulNaV1VrXlWffCbA7JYWO68w0l_LkvZKEUcA4Zew16WCawz48O2nOvImZEpgGTgmNWtKjuHla0ggYjfJIGt0UP6Jxfw_OYU/s400/Screenshot+from+2014-09-28+17:46:40.png" /></a>Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com3tag:blogger.com,1999:blog-3057039820716415448.post-36486866236890751282014-09-27T12:17:00.000+01:002014-09-27T21:16:58.328+01:00How to connect to FEUP VPN using UbuntuDownload <a href="http://www.fe.up.pt/~hdesk/scripts/linux/feupservices.tar.gz">this script</a> made by CICA to install all the required tools easily. Right-click it and select <b>Extract Here</b>.<br>
<br>
Press <b>CTRL+ALT+T</b> to open the Terminal. Navigate to the <b>Downloads</b> folder and then enter the command: <b>source upservices.sh</b><br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-Hq4dDyAltB91A98_XZZ10WtRv0mZvIWjUcgfcDhlnTpMPGmXrnIRa8hF5r3PoeGgNF0044ZLpbZaeQf0WJrCDwWFw4QTDLwZvaNxiA0VcoMkMTn8GDTfOvU-mlA97gKRBW8Eh4q3OXU/s1600/Screenshot+from+2014-09-27+12:00:41.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-Hq4dDyAltB91A98_XZZ10WtRv0mZvIWjUcgfcDhlnTpMPGmXrnIRa8hF5r3PoeGgNF0044ZLpbZaeQf0WJrCDwWFw4QTDLwZvaNxiA0VcoMkMTn8GDTfOvU-mlA97gKRBW8Eh4q3OXU/s400/Screenshot+from+2014-09-27+12:00:41.png" /></a><br>
<br>
After entering the command, the script will begin and a couple of windows will appear. Input your email:<br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7ZiGfNcV4A7tenW3XLWBndhUlb4EcZDJeWPkHaia1XdaSZ-wcCowtgwdLVh1_X76HD5PDr-bK7rOXZ3J65uCnEQ0lXtRPYeP9VkwZ10kDMjdoAXxT6exzRMZc_esBUNbRwzN6sgJm5Q0/s1600/Screenshot+from+2014-09-27+12:04:26.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7ZiGfNcV4A7tenW3XLWBndhUlb4EcZDJeWPkHaia1XdaSZ-wcCowtgwdLVh1_X76HD5PDr-bK7rOXZ3J65uCnEQ0lXtRPYeP9VkwZ10kDMjdoAXxT6exzRMZc_esBUNbRwzN6sgJm5Q0/s400/Screenshot+from+2014-09-27+12:04:26.png" /></a><br>
<br>
Input your SIGARRA password:<br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_HjNXs63T5mGxrYfxPPH8kVSmLoXujch3ZJelJhe0nXXiE-OqfxhXLWPx8ZPZZVtmYkTiCBl9WTJxZnGvqse7GdHsjb97pE7fAfCbRYL_K2JczngirMMfr_VHSE9KScVMOR0mRKtW7O4/s1600/Screenshot+from+2014-09-27+12:04:35.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_HjNXs63T5mGxrYfxPPH8kVSmLoXujch3ZJelJhe0nXXiE-OqfxhXLWPx8ZPZZVtmYkTiCBl9WTJxZnGvqse7GdHsjb97pE7fAfCbRYL_K2JczngirMMfr_VHSE9KScVMOR0mRKtW7O4/s400/Screenshot+from+2014-09-27+12:04:35.png" /></a><br>
<br>
Confirm your SIGARRA password:<br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLRazKJED_jODu-ryKzxod_RjfJ1AImsZbBKUM3qzaqieC29Qco49DRxoHEkB9Envj6DfxPO66rafU-7bdmVD2Tb7_eAWgtnW32QTBolaGXti56OO2TT-4UGDoaCjZJ4tFCXQ_JVBk1-c/s1600/Screenshot+from+2014-09-27+12:04:43.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLRazKJED_jODu-ryKzxod_RjfJ1AImsZbBKUM3qzaqieC29Qco49DRxoHEkB9Envj6DfxPO66rafU-7bdmVD2Tb7_eAWgtnW32QTBolaGXti56OO2TT-4UGDoaCjZJ4tFCXQ_JVBk1-c/s400/Screenshot+from+2014-09-27+12:04:43.png" /></a><br>
<br>
Press OK to install all services:<br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhk8pD6lIxFOMmOmqmaNrvtBZ_8tYmol14s82-swjQBad4cmb6vozXZXsUO2iG-Pj09_Sg8KsbMY3e9vrJeYoWgT3WLLmjJ6iwbXT7s1TSWGvgsTky5KGecqJChqrCduzCWeB7Im7S6SOc/s1600/Screenshot+from+2014-09-27+12:04:54.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhk8pD6lIxFOMmOmqmaNrvtBZ_8tYmol14s82-swjQBad4cmb6vozXZXsUO2iG-Pj09_Sg8KsbMY3e9vrJeYoWgT3WLLmjJ6iwbXT7s1TSWGvgsTky5KGecqJChqrCduzCWeB7Im7S6SOc/s400/Screenshot+from+2014-09-27+12:04:54.png" /></a><br>
<br>
Input your Ubuntu account password:<br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYgkg3BjKiKygPcO1JsUvte3ajwEd_4kMGRtUl1D4K7Y7cvaCRnhCs3mBoL0aIFFl36lKQlHCCmNnKJYZ2ZzcOKSs09vVcw6TFZ7MIh-fobflzMeRvl8P-WSGY5gwY0bmxQEOfRF9rYF8/s1600/Screenshot+from+2014-09-27+12:05:04.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYgkg3BjKiKygPcO1JsUvte3ajwEd_4kMGRtUl1D4K7Y7cvaCRnhCs3mBoL0aIFFl36lKQlHCCmNnKJYZ2ZzcOKSs09vVcw6TFZ7MIh-fobflzMeRvl8P-WSGY5gwY0bmxQEOfRF9rYF8/s400/Screenshot+from+2014-09-27+12:05:04.png" /></a><br>
<br>
Press OK:<br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIT0bBEDwvlXomAoXm_NQgBMfwVAUUd-c7sUrH_yxCFh7GFm6ucYyzQqhSD7sBYJpMkIx4Cd17I6duZjoH_61ct_xhCSJnl_lRwBRWPCAZpkZ-dTOwuQh77cnqvbRl1z3S2I5dWTKIJts/s1600/Screenshot+from+2014-09-27+12:05:20.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIT0bBEDwvlXomAoXm_NQgBMfwVAUUd-c7sUrH_yxCFh7GFm6ucYyzQqhSD7sBYJpMkIx4Cd17I6duZjoH_61ct_xhCSJnl_lRwBRWPCAZpkZ-dTOwuQh77cnqvbRl1z3S2I5dWTKIJts/s400/Screenshot+from+2014-09-27+12:05:20.png" /></a><br>
<br>
You may close this window:<br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOGQ_tTLU2QHRVAQGgi-bsGARdPtpdGwe0F4j864MkPkjN0qCOqItkWVocyDzoKa_3YnUGc91ZyJf9a4IlX_tA3x-0o82ZG_CoJrknIqT5eY9_IyxaWRZqC1Hx47otjAwGZcMhn_A5els/s1600/Screenshot+from+2014-09-27+12:05:25.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOGQ_tTLU2QHRVAQGgi-bsGARdPtpdGwe0F4j864MkPkjN0qCOqItkWVocyDzoKa_3YnUGc91ZyJf9a4IlX_tA3x-0o82ZG_CoJrknIqT5eY9_IyxaWRZqC1Hx47otjAwGZcMhn_A5els/s400/Screenshot+from+2014-09-27+12:05:25.png" /></a><br>
<br>
And that's it, the script should be finished:<br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYfWbbHnFS5Ippz5oD5krqe2-t4PluaLRupK6XcjduolXhYrJKqkoK85dYiyylD9hG3R4FBE-yDZYwU9rkqR4GySgVNuXjQstyDv3B6YgVkdkbXHn6-Xb3ubCNCjBUIAd69zZvKrGrrp4/s1600/Screenshot+from+2014-09-27+12:05:36.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYfWbbHnFS5Ippz5oD5krqe2-t4PluaLRupK6XcjduolXhYrJKqkoK85dYiyylD9hG3R4FBE-yDZYwU9rkqR4GySgVNuXjQstyDv3B6YgVkdkbXHn6-Xb3ubCNCjBUIAd69zZvKrGrrp4/s400/Screenshot+from+2014-09-27+12:05:36.png" /></a><br>
<br>
In order to connect to FEUP VPN, press the <b>Wi-Fi</b> status bar icon. Under the list of networks, there should be a submenu named <b>VPN Connections</b> and under that <b>VPN-FEUP</b>. Press <b>VPN-FEUP</b> and a connection will be attempted. When a connection is obtained, the following notification will appear:<br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigeQurw2W1dz-NhNnPraMpvXycMMCaVjJSxBWU421aM0Zu29qdymatdwYL7KbIco3goeQI5D6dnzZvnFmVpBzNvsO4m4H-du4gcw6QPOTD9vJj16ItkguYKZ9DpFcL4N68p5nWq51TCgo/s1600/Screenshot+from+2014-09-27+12:06:46.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigeQurw2W1dz-NhNnPraMpvXycMMCaVjJSxBWU421aM0Zu29qdymatdwYL7KbIco3goeQI5D6dnzZvnFmVpBzNvsO4m4H-du4gcw6QPOTD9vJj16ItkguYKZ9DpFcL4N68p5nWq51TCgo/s400/Screenshot+from+2014-09-27+12:06:46.png" /></a><br>
<br>
You can now access FEUP <b>software repository</b>, <b>samba</b>, and even delegate print jobs to FEUP <b>printers</b>:<br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdNQXdlBF9B1AUF2SeTkx4iZD3QmbjaE0QHPcTwxOv9okbZiA7WTAQnxAuxYQ3XyuL3r0cy-YC0iSoynW2BQEg_5aFd6FWUmEJn62PxueZXryG16wreeaZHrFFBj_QTpfaMX6RebIQTEQ/s1600/Screenshot+from+2014-09-27+21:13:57.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdNQXdlBF9B1AUF2SeTkx4iZD3QmbjaE0QHPcTwxOv9okbZiA7WTAQnxAuxYQ3XyuL3r0cy-YC0iSoynW2BQEg_5aFd6FWUmEJn62PxueZXryG16wreeaZHrFFBj_QTpfaMX6RebIQTEQ/s400/Screenshot+from+2014-09-27+21:13:57.png" /></a><br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRuTplybmTca8ujDi3Z5Vy682XM2HAnyWA_n7ai0H6_prZdYC6fhvT5e9mbnfo1RdFI6MZVL8tf74iKsObkEaN7gUAe50LaG-HOW_AiNk4W1neKyDXX2k1UeR6ygpGs4gy0jl-xw76Kxs/s1600/Screenshot+from+2014-09-27+21:14:09.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRuTplybmTca8ujDi3Z5Vy682XM2HAnyWA_n7ai0H6_prZdYC6fhvT5e9mbnfo1RdFI6MZVL8tf74iKsObkEaN7gUAe50LaG-HOW_AiNk4W1neKyDXX2k1UeR6ygpGs4gy0jl-xw76Kxs/s400/Screenshot+from+2014-09-27+21:14:09.png" /></a><br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRzk4lPyV_D5G1NClNBhcZmRHmpNRwZEUL_-VfDJ25X8VtBBsdk8bgnBnOI2mi0NUVDUr2BLxbta4xTtEJZazzWLlvnqOKcWz0eZDxyhzAnlVyq__jLAuqT5GrOgzHWqw19f27U2szNQQ/s1600/Screenshot+from+2014-09-27+21:14:49.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRzk4lPyV_D5G1NClNBhcZmRHmpNRwZEUL_-VfDJ25X8VtBBsdk8bgnBnOI2mi0NUVDUr2BLxbta4xTtEJZazzWLlvnqOKcWz0eZDxyhzAnlVyq__jLAuqT5GrOgzHWqw19f27U2szNQQ/s400/Screenshot+from+2014-09-27+21:14:49.png" /></a>Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com8tag:blogger.com,1999:blog-3057039820716415448.post-18141659321222332732014-09-07T23:21:00.001+01:002014-09-08T23:21:33.450+01:00[Minix][Tutorial 11] Adding flappy and the mario pipes<h1>
Adding the bird</h1>
Download <a href="http://i.imgur.com/6pH8tNj.png">bird-0x114.bmp</a>, <b>edit</b> it and place it inside the <b>res/images</b> folder.<br />
<br />
Create a new class named <b>Bird</b>, I mean: create two files: <b>Bird.c</b> and <b>Bird.h</b>. Declare it in the <b>Makefile</b>.<br />
<br />
For now, let's just try to make our little bird appear and make it <b>fall</b>.<br />
Our bird will have a <b>x</b> and <b>y</b> coordinates, <b>width</b>, <b>height</b>, a vertical <b>velocity</b> and of course a <b>bitmap</b> image.<br />
To make the little bird fall, I have set a <b>GRAVITY</b>. At each <b>update</b>, the <u>gravity is added to the velocity</u>, which will then be <u>added to the bird's current <b>y</b> location</u>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAuOOrk7gqfGOEMu_XpYXKDXF2x6746jFAWZZ7CA5FaLUS46Q9IDE1XFhAqYBupk5KxAhXzXuM6B_mBj7EbxAAankjkdfcs1GRkKMCFGSmJgZha44zVZUhLeBNoMKr6PlNVPLpQ8PYjss/s1600/Screenshot+from+2014-09-06+23:25:31.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAuOOrk7gqfGOEMu_XpYXKDXF2x6746jFAWZZ7CA5FaLUS46Q9IDE1XFhAqYBupk5KxAhXzXuM6B_mBj7EbxAAankjkdfcs1GRkKMCFGSmJgZha44zVZUhLeBNoMKr6PlNVPLpQ8PYjss/s400/Screenshot+from+2014-09-06+23:25:31.png" /></a><br />
<br />
Now we need to <b>add a bird</b> to our <b>game state</b>, <b>initialize</b> it and add the respective <b>update</b>, <b>draw</b> and <b>delete</b> method calls:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhifSgTWRoKmDULmm1QEG_pY5oloar7rpsvXr4T8wgKOFNzANhTkGfa2Nl1LkMDXCsRJL7z0mKj_ubt5Ptb0f4W458DCZGo8Iv-I1aRo-_ex5thlB7QqEoz6Vkb-amxL171jlQK0Us0sA/s1600/Screenshot+from+2014-09-06+23:28:43.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhifSgTWRoKmDULmm1QEG_pY5oloar7rpsvXr4T8wgKOFNzANhTkGfa2Nl1LkMDXCsRJL7z0mKj_ubt5Ptb0f4W458DCZGo8Iv-I1aRo-_ex5thlB7QqEoz6Vkb-amxL171jlQK0Us0sA/s400/Screenshot+from+2014-09-06+23:28:43.png" /></a><br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEip7aIzd6wPrZO0jxA9GjyQTRbqdIh7baBgYeD2vVN605J10CBHypdgFBJ1KCDCIPpgezCrPOSiEPq_jIsvM_Jlusdw3BtlFKv9L37UqieURsRw7KLKHz61VDnxLPMZa-zJNrCuCTWIx9Y/s1600/Screenshot+from+2014-09-06+23:29:41.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEip7aIzd6wPrZO0jxA9GjyQTRbqdIh7baBgYeD2vVN605J10CBHypdgFBJ1KCDCIPpgezCrPOSiEPq_jIsvM_Jlusdw3BtlFKv9L37UqieURsRw7KLKHz61VDnxLPMZa-zJNrCuCTWIx9Y/s400/Screenshot+from+2014-09-06+23:29:41.png" /></a><br />
<br />
If you <b>compile, install and run</b>, you should now see flappy <b>falling</b>!<br />
<br />
<h2>
Making flappy jump</h2>
Now we need to make little flappy <b>jump</b>. You may have noticed that the bird <b>update</b> method receives an <b>integer</b> named <b>jump</b>. Well, my idea is: when the player presses the <b>space bar</b> on the keyboard, we will call this method with <b>jump = 1</b>, this tells flappy to jump. Otherwise, we just call flappy's update method with <b>jump = 0</b>.<br />
<br />
In order to accomplish this, let's modify flappy's <b>update</b> method - if <b>jump == 1</b>, let's modify it's <b>velocity</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxqbcdtEsVnPQNLuHwHVi6YXfywIUaqEZvpgetLvURReLdA5GPu4QulOilZezJgc1HpDkV3ksN-5Q5fUVbHjAkiD0OtR6nUJI1a4Jqt9HIDMA7NVHrpRxuaQp8dB4xcd4jszwiyni2L38/s1600/Screenshot+from+2014-09-06+23:41:16.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxqbcdtEsVnPQNLuHwHVi6YXfywIUaqEZvpgetLvURReLdA5GPu4QulOilZezJgc1HpDkV3ksN-5Q5fUVbHjAkiD0OtR6nUJI1a4Jqt9HIDMA7NVHrpRxuaQp8dB4xcd4jszwiyni2L38/s400/Screenshot+from+2014-09-06+23:41:16.png" /></a><br />
<br />
Now, let's modify the game state's <b>update</b> method to send <b>jump == 1</b> when the <b>space bar</b> is <b>released</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigUFUKRbemOUl1om4oDpL2iB2o3oqe17O17UbT8Tpo_vFLkW-X20RJlos0-qobEXM-yvGNuDq4uk9LtJjiTJKUl1rscPhxv57kV-HTOb3bhcV2D8JydLzrdw0KDmro2LT6qjM_3N7uQ7M/s1600/Screenshot+from+2014-09-06+23:44:14.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigUFUKRbemOUl1om4oDpL2iB2o3oqe17O17UbT8Tpo_vFLkW-X20RJlos0-qobEXM-yvGNuDq4uk9LtJjiTJKUl1rscPhxv57kV-HTOb3bhcV2D8JydLzrdw0KDmro2LT6qjM_3N7uQ7M/s400/Screenshot+from+2014-09-06+23:44:14.png" /></a><br />
<br />
By the way, let's change flappy's <b>default start position</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgV4pGDv9UWvQTuOeyXHFDeMblCjAC4gNrXuRRd27KrtYSoWVMk9kaMe015WW3NkdWoE9cD1IFVVW7u7oivT8OAmfnJPTlULAzgHyDtlNz9Hni-ujZiZ4-L1-97eVGl-ySV4XraFQHkcOU/s1600/Screenshot+from+2014-09-06+23:47:44.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgV4pGDv9UWvQTuOeyXHFDeMblCjAC4gNrXuRRd27KrtYSoWVMk9kaMe015WW3NkdWoE9cD1IFVVW7u7oivT8OAmfnJPTlULAzgHyDtlNz9Hni-ujZiZ4-L1-97eVGl-ySV4XraFQHkcOU/s400/Screenshot+from+2014-09-06+23:47:44.png" /></a><br />
<br />
<h2>
Quick preview</h2>
<b>Compile and run</b>. Now when you press the <b>space bar</b>, flappy <b>flies</b>! This is how the game looks like now. Cool isn't it?<br />
<br />
<iframe frameborder="0" height="450" scrolling="no" src="http://gfycat.com/ifr/ThinHonestHedgehog" style="-webkit-backface-visibility: hidden; -webkit-transform: scale(1);" width="540"></iframe><br />
<h2>
Increasing FPS</h2>
Our game might be running with a little lag. Let's change that.<br />
<br />
In <b>FlappyNix.c</b>:<br />
<blockquote>
const int FPS = 60;<br />
const int mouseFPSmult = 1;</blockquote>
In <b>Bird.c</b>:<br />
<blockquote>
const int GRAVITY = 1;<br />
const int JUMP_VEL = -12.5;</blockquote>
If you now <b>compile and run</b> the game, it should be much <b>smoother</b>.<br />
<br />
<h2>
Making flappy lose</h2>
Since everything is working so good, let's go further and make flappy die when it <b>touches</b> the <b>ground</b>. In order to do that, after each <b>update</b>, we need to check if the <b>bottom of the bird's sprite</b> is <b>below</b> the <b>top of the ground sprite/image</b> - easy, right?<br />
<br />
To start, I have created an integer <b>groundY</b>, because we were calculating it in the <b>draw</b> method - and <u>draw methods should not contain any calculation what so ever</u>. I initialize <b>groundY</b> in the <b>constructor</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSYhNuV61Tpz_R2EUcr58mlFtX5kUEOA3NIYUXXqfgLxhjV2PLlJW5rA9baPTXUb4ovoioSGlW_PNwwarS8y1CkAaPDRRutyXh3sv0qMqn954TNol8R0m7x6lvhrKTKl3k52xdJE8Yqow/s1600/Screenshot+from+2014-09-07+12:17:33.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSYhNuV61Tpz_R2EUcr58mlFtX5kUEOA3NIYUXXqfgLxhjV2PLlJW5rA9baPTXUb4ovoioSGlW_PNwwarS8y1CkAaPDRRutyXh3sv0qMqn954TNol8R0m7x6lvhrKTKl3k52xdJE8Yqow/s400/Screenshot+from+2014-09-07+12:17:33.png" /></a><br />
<br />
Furthermore, to check flappy's <b>collision</b> with the <b>ground</b>, I have implemented the <b>gameOver</b> function. For now it only checks if flappy <b>hit the ground</b> - I have created another function for that as well - but once we implement the moving <b>pipes</b>, we will also check if flappy collided with any of them in this function.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFTA3ATS-s0kjUrlV3GmiqRoiwOGHPgC6GBqvD5CJ7jZUQVVhhr8rlPD8zc5rp473DSgLlGCo3M1JCZD3TGgNkxoAoZ8FXqNMo6j-wo0Kymi8-inC5ftkYEK1x5qXIRwjzWQmD9PdbZ-Y/s1600/Screenshot+from+2014-09-07+12:22:28.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFTA3ATS-s0kjUrlV3GmiqRoiwOGHPgC6GBqvD5CJ7jZUQVVhhr8rlPD8zc5rp473DSgLlGCo3M1JCZD3TGgNkxoAoZ8FXqNMo6j-wo0Kymi8-inC5ftkYEK1x5qXIRwjzWQmD9PdbZ-Y/s400/Screenshot+from+2014-09-07+12:22:28.png" /></a><br />
<br />
Since we are modifying the <b>update</b> function, and I would like to make flappy <b>fly</b> by pressing either the space bar or the <b>left mouse button</b>, let's add that option:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRzGf-g5K0Z52KlucfIs4AokNpSx20X0h_VyiJP82d9mpalPEuIFOGQuTnHWz_9LhN511UUGi_amgMuarhPuQX_AVxMfgxoycGQMoi0eZEDfp95yi6U_VCzOov87j0GFaYVIh_N8945uw/s1600/Screenshot+from+2014-09-07+12:27:53.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRzGf-g5K0Z52KlucfIs4AokNpSx20X0h_VyiJP82d9mpalPEuIFOGQuTnHWz_9LhN511UUGi_amgMuarhPuQX_AVxMfgxoycGQMoi0eZEDfp95yi6U_VCzOov87j0GFaYVIh_N8945uw/s400/Screenshot+from+2014-09-07+12:27:53.png" /></a><br />
<br />
<b>Compile and run</b> the game. You should now be able to use either the <b>space bar</b> or the <b>left mouse button</b> to make flappy fly. If flappy touches the <b>ground</b>, you should <b>lose</b> and go back to the <b>main menu</b>.<br />
<br />
<h1>
Adding the pipes</h1>
So we are pretty much almost done! We just need to add the <b>pipes</b> now!<br />
<br />
So let's do it! Create <b>Pipe.c</b>, <b>Pipe.h</b> and declare it in the <b>Makefile</b>. This class will represent the pipe with the little <b>gap</b> through which flappy is supposed to fly.<br />
<br />
Download <a href="http://i.imgur.com/edT5oFo.png">top-pipe-0x114.bmp</a> and <a href="http://i.imgur.com/w1pCq91.png">bottom-pipe-0x114.bmp</a>. <b>Edit</b> and place them inside your <b>res/images</b> folder.<br />
<br />
Every pipe will be described by <b>four</b> variables: the <b>x and y coordinates</b> of the <b>top left corner</b> of the <b>gap</b>, and the <b>width</b> and <b>height</b> of each half of the pipe - this is for future convenience. The pipe constructor will have <b>two</b> parameters: the <b>x</b> where the pipe should be created and the <b>ground y</b> coordinate - because the pipe gap location will be <b>randomly</b> generated, we need to know the ground location to set the <b>limit</b> for the random function.<br />
<br />
Since we are going to create <b>a lot</b> of pipes, it is not a good idea for each pipe to have it's own images of the top and bottom parts loaded. A better approach is to load those images only <b>once</b>, and use them to draw every pipe, only at <b>different</b> positions - this is known as <a href="http://sourcemaking.com/design_patterns/flyweight">flyweight</a>, yet another <b>design pattern</b>. We can resolve this using something like a <b>singleton</b>, just like we did for the <b>mouse</b>.<br />
<br />
Here is what everything described above looks like:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyuCNsd3i8uGITkKlDQJp-MUXwhUEwb6_3LtEkB5ZnO8iNIgKPxRCzalGd4yXIzRAG3jP4N2HbDJ1GBM2kTZiJT-QHHncK1gEMu47N7mhd5auG5Adsmmu7ny_ISqypDnN6FPJyg5Qburo/s1600/Screenshot+from+2014-09-07+21:44:54.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyuCNsd3i8uGITkKlDQJp-MUXwhUEwb6_3LtEkB5ZnO8iNIgKPxRCzalGd4yXIzRAG3jP4N2HbDJ1GBM2kTZiJT-QHHncK1gEMu47N7mhd5auG5Adsmmu7ny_ISqypDnN6FPJyg5Qburo/s400/Screenshot+from+2014-09-07+21:44:54.png" /></a><br />
<br />
Ok, now it's time to implement the <b>update</b>, <b>draw</b> and <b>delete</b> methods. I have decided to put some <b>global variables</b> in <b>Utilities.h</b>, so yeah, I have made some changes to the rest of the code - you should be just fine without even having to do them, or if you do have to do them, it will be easy. Here is how the methods I told you to implement look like, as well as the global variables I created:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3VxEzAYOuPgV51C5ChRQ17Pi1S9oYQvNRoC1U2NGr7LyhuFiLodbV8F490Tj1wWSWIjhrS1kb9eEM1Ph3w74kWvQ9eTwdo0764I6JPPRnFTiKj4fb9o6CfK2Df9fUATp7rEgv9kTkn1g/s1600/Screenshot+from+2014-09-07+22:52:00.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3VxEzAYOuPgV51C5ChRQ17Pi1S9oYQvNRoC1U2NGr7LyhuFiLodbV8F490Tj1wWSWIjhrS1kb9eEM1Ph3w74kWvQ9eTwdo0764I6JPPRnFTiKj4fb9o6CfK2Df9fUATp7rEgv9kTkn1g/s400/Screenshot+from+2014-09-07+22:52:00.png" /></a><br />
<br />
Ok, let's try to test our game by adding some pipes! Add an <b>array</b> of <b>three</b> pipe pointers in the <b>GameState</b> struct and <b>initialize</b> it like so:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyk55JXWbnSj8ro5umBKBGQwn7BE_0pDoF3tvldNDd26BK1o6y3VQ-4gk1MVcO1bB5tMtGl4yCuvCuUUZGnlTU_4rpNkxqT9AQT3zGTOZzuSJobRWZbTBV4v_DJsk5U92_kUdwq5zk-lM/s1600/Screenshot+from+2014-09-07+22:55:22.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyk55JXWbnSj8ro5umBKBGQwn7BE_0pDoF3tvldNDd26BK1o6y3VQ-4gk1MVcO1bB5tMtGl4yCuvCuUUZGnlTU_4rpNkxqT9AQT3zGTOZzuSJobRWZbTBV4v_DJsk5U92_kUdwq5zk-lM/s400/Screenshot+from+2014-09-07+22:55:22.png" /></a><br />
<br />
Create a separate function to <b>update</b> and <b>draw</b> the pipes:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0hbDJggZ0LNv7rIib-4yxLMqpf-QtpWzdIlPrFMqoKU5Soj5LF0i6MXCgR3a2U7eptTJoa2liEWLzt2S31_4mKxHb23YA16yZqxd7oIehEpBZspffXXKuz7oTZfOMvmNdt44Idz3gnos/s1600/Screenshot+from+2014-09-07+22:58:23.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0hbDJggZ0LNv7rIib-4yxLMqpf-QtpWzdIlPrFMqoKU5Soj5LF0i6MXCgR3a2U7eptTJoa2liEWLzt2S31_4mKxHb23YA16yZqxd7oIehEpBZspffXXKuz7oTZfOMvmNdt44Idz3gnos/s400/Screenshot+from+2014-09-07+22:58:23.png" /></a><br />
<br />
Do the same to <b>delete</b> them. Do not forget to delete the <b>bitmaps</b> as well, and <u>even more important</u>, to <b>NULL reassign</b> them:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgamF3G_mbP6pxBifobSekoEAbBbJIxfabk94WCSr0sfKYAWzNzLBRQ7AA14-9LDCKRfdayGhRO_Xqb-0YDw8c0ULJQ_FkaMRJGNNP85qi9S9g4vOKyn-Dabqj-H5QW70lqkmDdVYtIBUU/s1600/Screenshot+from+2014-09-07+23:03:49.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgamF3G_mbP6pxBifobSekoEAbBbJIxfabk94WCSr0sfKYAWzNzLBRQ7AA14-9LDCKRfdayGhRO_Xqb-0YDw8c0ULJQ_FkaMRJGNNP85qi9S9g4vOKyn-Dabqj-H5QW70lqkmDdVYtIBUU/s400/Screenshot+from+2014-09-07+23:03:49.png" /></a><br />
<br />
<h2>
Quick preview</h2>
<b>Compile, install and run</b>. This is what we got so far. How awesome is that?<br />
P.S. - I am terrible at playing this game.<br />
<br />
<iframe src="http://gfycat.com/ifr/ImmediateWhoppingCaterpillar" frameborder="0" scrolling="no" width="540" height="450" style="-webkit-backface-visibility: hidden;-webkit-transform: scale(1);" ></iframe><br />
<h1>
Implementing the pipes generator</h1>
We have almost, almost done. We have some <b>pipes</b> moving, but now we have to keep them coming. After that we need to do something about the bird colliding with the pipes, and then our project is finished! So, let's go!<br />
<br />
The first step is to change the <b>size</b> of the pipes array. Since the <b>maximum</b> number of <b>visible</b> pipes on the screen is <b>four</b>, let's make the array have <b>five</b> pipes.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPTblW9H58VObwLgp_Iui1y4Na8zcIkWOxZtZb_fRwfrv4Oqq5ce7MoI0IZqo89Klh4BXYMMMZAfjZDXIJCrU-fesSPdW3t9Wu4NvuGXYzQcZh2KhihbasSz-s9TaBS5HXk9NSCWNhbAk/s1600/Screenshot+from+2014-09-07+23:48:36.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPTblW9H58VObwLgp_Iui1y4Na8zcIkWOxZtZb_fRwfrv4Oqq5ce7MoI0IZqo89Klh4BXYMMMZAfjZDXIJCrU-fesSPdW3t9Wu4NvuGXYzQcZh2KhihbasSz-s9TaBS5HXk9NSCWNhbAk/s400/Screenshot+from+2014-09-07+23:48:36.png" /></a><br />
<br />
Now we have to edit the <b>updatePipes</b> function: when the <b>left most</b> pipe moves <b>off the screen</b>, we have to <b>delete</b> it, <b>shift</b> every pipe on the pipes array <b>one time to the left</b> and finally <b>create</b> a new pipe at the <b>last position</b> of the array. It is that simple!<br />
<br />
<u>Try to code this for yourself and only then compare with the screenshot below</u>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnnBusd5wbf05kOfAdCZe03CY7NgPIofLpCLYtWtKglLCg0U7YbigBVYUzRIiFJ6SYBIqhG9VXsaK81gJodFSMsOgi9ltBjODMSa57v7hLRWTLm6vZ8PkC30mzRQacTOWy8W_7QVzFaGU/s1600/Screenshot+from+2014-09-07+23:51:37.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnnBusd5wbf05kOfAdCZe03CY7NgPIofLpCLYtWtKglLCg0U7YbigBVYUzRIiFJ6SYBIqhG9VXsaK81gJodFSMsOgi9ltBjODMSa57v7hLRWTLm6vZ8PkC30mzRQacTOWy8W_7QVzFaGU/s400/Screenshot+from+2014-09-07+23:51:37.png" /></a><br />
<br />
<h1>
Adding pipe collisions</h1>
Fist things first: we need a simple function to detect <a href="https://www.youtube.com/watch?v=ghqD3e37R7E">AABB collisions</a>. I implemented mine in <b>Rectangle</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmGJNLn6cjoAE2YtSgC6f2pKBFfiStGu39xRddvw0t-roO57J7z022Okg8BecWt64WdEgY2qJtPFxJmDnil-jhUxYS91MUioqDIf20Q38CeB-sg4e2Oh0GSwCdvlLkr2qk8andegNuAMY/s1600/Screenshot+from+2014-09-08+17:34:55.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmGJNLn6cjoAE2YtSgC6f2pKBFfiStGu39xRddvw0t-roO57J7z022Okg8BecWt64WdEgY2qJtPFxJmDnil-jhUxYS91MUioqDIf20Q38CeB-sg4e2Oh0GSwCdvlLkr2qk8andegNuAMY/s400/Screenshot+from+2014-09-08+17:34:55.png" /></a><br />
<br />
After that, we need to check if flappy really hit any pipe. Remember the <b>gameOver</b> function where I have previously told you we were going to do that? Well, let's actually do that there and now.<br />
<br />
I created an <b>integer</b> variable called <b>flappyHitPipe</b> that is initialized with <b>zero</b> (false).<br />
Then I created a <b>rectangle</b> which corresponds to the bird's image limits - <b>bRect</b>.<br />
I am not quite sure about this one, but I guess we need to <b>check collisions</b> on the <b>first two pipes</b>, because although the first one might be a bit off the screen, flappy might be able to hit the second pipe before the first one is deleted. I do this with a simple <b>for</b> cycle.<br />
Inside the <b>for</b> cycle we will need to check if our <b>flappyHitPipe</b> flag is already <b>true</b> - if it is, there is no need to check for any other collisions - therefore the <b>continue;</b>. If the flag is still <b>false</b>, we create <b>two rectangles</b> - pRect1 and pRect2 - corresponding to the <b>top</b> and <b>bottom</b> halves of the <b>pipe</b> being analysed; we then use the <b>colliding</b> function from <b>Rectangle</b> to check if the bird rectangle - <b>bRect</b> - is colliding either with <b>pRect1</b> or <b>pRect2</b> and update the <b>flappyHitPipe</b> flag accordingly. Then, inside the for cycle, I delete <b>pRect1</b> and <b>pRect2</b>; outside the for cycle I delete the <b>bRect</b>. Finally, I return the <b>flappyHitPipe</b> flag. Here is everything explained above translated to code:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdNwbrFT1t0Sbdet2iZXLlx8vwzNllkAVofes3rIrnw1gkRxSwDZQUREMRAD0eb6ztOHVF-rbBenvs2AtxqoOHXtG4NDBlrVdOdRsdVltwVIpcpXn67aO-w5jSSdqlPYtg6Nz1VZLUNHE/s1600/Screenshot+from+2014-09-08+17:50:24.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdNwbrFT1t0Sbdet2iZXLlx8vwzNllkAVofes3rIrnw1gkRxSwDZQUREMRAD0eb6ztOHVF-rbBenvs2AtxqoOHXtG4NDBlrVdOdRsdVltwVIpcpXn67aO-w5jSSdqlPYtg6Nz1VZLUNHE/s400/Screenshot+from+2014-09-08+17:50:24.png" /></a><br />
<br />
And here is a demonstration of flappy going <b>through</b> the pipes, <b>against</b> the pipes and <b>against</b> the floor. The <b>collisions</b> are working marvelously:<br />
<br />
<iframe src="http://gfycat.com/ifr/EducatedInconsequentialIndianhare" frameborder="0" scrolling="no" width="540" height="450" style="-webkit-backface-visibility: hidden;-webkit-transform: scale(1);" ></iframe><br />
Although, you may have noticed a <b>bug</b>! If we press fast enough to make flappy fly really high, it won't hit the pipes because the pipes are not that high, and you will be able to keep flappy going as long as you wish:<br />
<br />
<iframe src="http://gfycat.com/ifr/InfiniteShorttermCollardlizard" frameborder="0" scrolling="no" width="540" height="450" style="-webkit-backface-visibility: hidden;-webkit-transform: scale(1);" ></iframe><br />
There is a really simple way to solve this: make the collision rectangle of the top half of the pipe start for example at <b>y = -500</b> and <u>do not let flappy fly higher than this</u>. <b>Highlighted</b> in the following screenshot are the parts of the code I had to <b>modify</b> to resolve this bug:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRX6ZwIG4iDWBFHiRK_yP0lHsLpE3FuzFd2iZx-u-AuRZ78u7QclQN_9GnNQh3lelqfiq_SokeZEcV2qzI5Z8Y8AkgdqjXHyhuUv7VioAVo2oe0BDg7hPx9P8Erkcw09vBTts0pFqWdS0/s1600/Screenshot+from+2014-09-08+23:10:14.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRX6ZwIG4iDWBFHiRK_yP0lHsLpE3FuzFd2iZx-u-AuRZ78u7QclQN_9GnNQh3lelqfiq_SokeZEcV2qzI5Z8Y8AkgdqjXHyhuUv7VioAVo2oe0BDg7hPx9P8Erkcw09vBTts0pFqWdS0/s400/Screenshot+from+2014-09-08+23:10:14.png" /></a><br />
<br />
<h1>
The end</h1>
Phew, what a long and dangerous journey this has been!<br />
<br />
These tutorials were great to write and I have learned a lot from them. I really like writing these tutorials, they test my patience. I just hope you have learned anything from them, even if it was just a little bit. The time has come for me to say good bye to this tutorial series and ship on to other projects.<br />
<br />
If you would like to get in touch with me directly, go <a href="http://difusal.blogspot.pt/p/blog-page.html">here</a>.<br />
<br />
<h1>
Back to index</h1>
Click <a href="http://difusal.blogspot.pt/2014/07/minix-posts-index.html">here</a> to go back to the index post.Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-5922935993823758732014-09-06T16:21:00.001+01:002014-09-07T23:26:40.312+01:00[Minix][Tutorial 10] Creating the game state, adding a moving background and groundIn this tutorial we are going to focus on the development of the game itself, the game state.<br />
<br />
<h1>
Creating the game state</h1>
Start by creating <b>GameState.h</b> and <b>GameState.c</b> - do not forget to declare it in the <b>Makefile</b>.<br />
<br />
Let's start with <b>small goals</b>, ok? First let's try to put the <b>background</b> scrolling correctly through the screen.<br />
Download <a href="http://i.imgur.com/HwvK2FJ.png">this</a> background, export it as a <b>.bmp</b> using <b>R5 G6 B5</b> with GIMP and place it inside your <b>res/images</b> folder.<br />
<br />
The game state should have <b>four</b> methods that every state should have - <b>new</b>, <b>update</b>, <b>draw</b> and <b>delete</b>. Our game state object, for now, should have an integer telling if the state is <b>done</b>/finished, a bitmap pointer to the <b>background</b> and an integer corresponding to the background horizontal <b>position</b> - because the background is going to be <b>moving</b> to the left. Knowing this, try to code <b>GameState.h</b> <u>for yourself</u>. Afterwards, compare it to the my <b>GameState.h</b> below.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitbGZ6yMD0GjDFf_XpYiYWM30R0Mbr08vaWLpH8y7n73CTGRcUcfUTzeNqW0WYCNU5Ou_-N2e5IoPPJoCLFVCWEByrQnwDsFSaGnOVFBCMEBgncvk5RsYf3iG8LG3yJh38_FdNIfoflFI/s1600/Screenshot+from+2014-09-06+15:57:21.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitbGZ6yMD0GjDFf_XpYiYWM30R0Mbr08vaWLpH8y7n73CTGRcUcfUTzeNqW0WYCNU5Ou_-N2e5IoPPJoCLFVCWEByrQnwDsFSaGnOVFBCMEBgncvk5RsYf3iG8LG3yJh38_FdNIfoflFI/s400/Screenshot+from+2014-09-06+15:57:21.png" /></a><br />
<br />
Now open <b>GameState.c</b> and <u>try to code everything for yourself</u> - come on, you can do it!<br />
<br />
I'll give a little help: in the <b>initialization</b> you have to initialize the background <b>position</b> and actually <b>load</b> the background image; in the <b>update</b> method, you have to update the background <b>position</b>, to make the background <b>slowly move left</b>, and when it has moved enough, <b>reset</b> the position to <b>zero</b>; in the <b>draw</b> method, you just need to draw the background at the right <b>position</b>; and finally in the <b>delete</b> method just delete the <b>background</b> image and free the <b>state</b>.<br />
<br />
Easy, right? You should end up having something like this:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipnIkZc1ylP8r6HzWHn9LMvl3du-jtjmaXJ-g3NR6SUTqnURdwYKSBY6ZSt6CDUFPjqJZLOhiGqC5KKCvUwPaxpa2jGI3kkrLvh78xA3bFdqZyHNo3Q8UyAX7zk-gE8egjvLapUINOtHg/s1600/Screenshot+from+2014-09-06+18:10:44.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipnIkZc1ylP8r6HzWHn9LMvl3du-jtjmaXJ-g3NR6SUTqnURdwYKSBY6ZSt6CDUFPjqJZLOhiGqC5KKCvUwPaxpa2jGI3kkrLvh78xA3bFdqZyHNo3Q8UyAX7zk-gE8egjvLapUINOtHg/s400/Screenshot+from+2014-09-06+18:10:44.png" /></a><br />
<br />
Ok, so now we have the game state <b>ready</b>, we just need to add it to every mechanism (function) of our <b>state machine</b>.<br />
<br />
Add a new state identifier:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgN_A07-JjT8QhDgrm6LG0_QszApzYRoBNamuYbfbz-HalEuYmCSmIG7foWAv9nbQGOH_ZJEeDKaPSh45-3MgPFJsSKBccxrgUzeyFkPBTYMO5g47WhDiETDWHM15PWYvJ9-oXczWVi3Nk/s1600/Screenshot+from+2014-09-06+16:06:36.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgN_A07-JjT8QhDgrm6LG0_QszApzYRoBNamuYbfbz-HalEuYmCSmIG7foWAv9nbQGOH_ZJEeDKaPSh45-3MgPFJsSKBccxrgUzeyFkPBTYMO5g47WhDiETDWHM15PWYvJ9-oXczWVi3Nk/s400/Screenshot+from+2014-09-06+16:06:36.png" /></a><br />
<br />
In <b>FlappyNix.c</b>, include the state header file:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKFs0oVlWCerOilNCtfgnQrkXaaUmxAIWgAxRccwQMNCbewGMGrZueNC1lKVI1MYLD6mGDqqeyer8v1c_EesDnAHMqmhFIwAIl5zW4WGKbOmUQUHaOBnnLxULEaMOOAvKlynxnGgCnC5A/s1600/Screenshot+from+2014-09-06+16:07:27.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKFs0oVlWCerOilNCtfgnQrkXaaUmxAIWgAxRccwQMNCbewGMGrZueNC1lKVI1MYLD6mGDqqeyer8v1c_EesDnAHMqmhFIwAIl5zW4WGKbOmUQUHaOBnnLxULEaMOOAvKlynxnGgCnC5A/s400/Screenshot+from+2014-09-06+16:07:27.png" /></a><br />
<br />
Add the state <b>update</b> and <b>draw</b> method calls to each switch case:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2RkHVmnCMZCR3avE5DBDPvazPyRPmsJ2zVkmKxumFmDYC3XHPHu3_nlCSszB53L3ta_ScyN4LN-eYZx9sA6XlJmbI6vMh2fEwT-GP1qW42y6VY4QpguS1Tg4CGJsrX5vclnAqHGrppZc/s1600/Screenshot+from+2014-09-06+16:08:53.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2RkHVmnCMZCR3avE5DBDPvazPyRPmsJ2zVkmKxumFmDYC3XHPHu3_nlCSszB53L3ta_ScyN4LN-eYZx9sA6XlJmbI6vMh2fEwT-GP1qW42y6VY4QpguS1Tg4CGJsrX5vclnAqHGrppZc/s400/Screenshot+from+2014-09-06+16:08:53.png" /></a><br />
<br />
Update the <b>change state</b> function as well:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigx76mBk-43PpwV7qp1LzyPQyqQwn_cXFju0xn6ZAO4g-oMIEXZ4latm_dwA7BzUpoluH5OEXqjOHRtIWvHbieaXAKFf3QzfpIuZAw6e8Pl6fP8EUIPLJDX1IC09CATCuxDQpRMZAY8WQ/s1600/Screenshot+from+2014-09-06+16:09:55.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigx76mBk-43PpwV7qp1LzyPQyqQwn_cXFju0xn6ZAO4g-oMIEXZ4latm_dwA7BzUpoluH5OEXqjOHRtIWvHbieaXAKFf3QzfpIuZAw6e8Pl6fP8EUIPLJDX1IC09CATCuxDQpRMZAY8WQ/s400/Screenshot+from+2014-09-06+16:09:55.png" /></a><br />
<br />
Also update <b>check if state is done</b> and <b>delete current state</b> methods. Notice that I've slightly modified the switch case for the <b>main menu state</b> as well.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBum8tQkMmX9yx-PZAH8btlZIQxg6-tgM01_WFK1C9beHy2L6vlCqzkI-pMumc-c_Q-nZeJeOXWRLe8kdIj_itI6kqYtLka9t9ieWi5XL7AKCrZMv8XtC88FqK0FOJEG51B79PXf2foJY/s1600/Screenshot+from+2014-09-06+16:11:21.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBum8tQkMmX9yx-PZAH8btlZIQxg6-tgM01_WFK1C9beHy2L6vlCqzkI-pMumc-c_Q-nZeJeOXWRLe8kdIj_itI6kqYtLka9t9ieWi5XL7AKCrZMv8XtC88FqK0FOJEG51B79PXf2foJY/s400/Screenshot+from+2014-09-06+16:11:21.png" /></a><br />
<br />
<h2>
Quick preview</h2>
If we have done everything correctly so far, when we run the program and press <b>play</b>, we should see the background infinitely scrolling to the left. If we press the <b>ESC</b> key on the keyboard, we should be able to <b>return</b> to the main menu as well. So go ahead, <b>compile</b>, <b>install</b> and <b>run</b> your program. It should look something like this:<br />
<br />
<iframe frameborder="0" height="450" scrolling="no" src="http://gfycat.com/ifr/ImprobableSharpBluewhale" style="-webkit-backface-visibility: hidden; -webkit-transform: scale(1);" width="540"></iframe><br />
<h1>
Adding the ground</h1>
Now that we have the background moving, let's add a moving <b>ground</b> as well. The ground will be moving <b>faster</b> than the background to create a sense of <b>perspective</b>.<br />
<br />
Download the <b>ground</b> <a href="http://i.imgur.com/zbytOmh.png">image</a>, <b>edit</b> it like you have done before and place it inside the <b>res/images</b> folder.<br />
Just like what we did to add the <b>background</b> image, do the same to add the <b>ground</b> image: add a <b>bitmap pointer</b> for the actual image as well as an <b>integer</b> to hold it's horizontal <b>position</b>. After that, initialize them just like you did with the background image:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5qxT06i2pWhSxHVMUK55q3cXLad6ov0ld46r0ZtPFrHwzDJ7V3cSGKBbjMic06u_YSeaFLztBAlpAgvjuBel-qkO6ls7CH4L6sHGoHM-U8r5N7OyqgQOHIzj1G5x7js6QyzD5C7Z9KFQ/s1600/Screenshot+from+2014-09-06+22:31:08.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5qxT06i2pWhSxHVMUK55q3cXLad6ov0ld46r0ZtPFrHwzDJ7V3cSGKBbjMic06u_YSeaFLztBAlpAgvjuBel-qkO6ls7CH4L6sHGoHM-U8r5N7OyqgQOHIzj1G5x7js6QyzD5C7Z9KFQ/s400/Screenshot+from+2014-09-06+22:31:08.png" /></a><br />
<br />
After that, you just need to <b>update</b> the ground <b>position</b> - I created a separated function for that; <b>draw</b> the <b>ground</b> - make sure you draw it after the background, otherwise it won't show up; add the <b>delete</b> ground bitmap statement in the state delete method. Here's what all of this should look like:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyrAnyytqFwUjlEJg01wQgHYuwxQLwKJa16Tg8B4bPoh-ayx2vgsef2DDNARhG7aBTfc1CraWmsQ0vcne_4EPZvFmZWOIoQms0W2bzFdkbuK9CldFnipZyMlZUKxTAZXaHgUJAHnK1eaI/s1600/Screenshot+from+2014-09-06+22:38:56.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyrAnyytqFwUjlEJg01wQgHYuwxQLwKJa16Tg8B4bPoh-ayx2vgsef2DDNARhG7aBTfc1CraWmsQ0vcne_4EPZvFmZWOIoQms0W2bzFdkbuK9CldFnipZyMlZUKxTAZXaHgUJAHnK1eaI/s400/Screenshot+from+2014-09-06+22:38:56.png" /></a><br />
<br />
<h2>
Quick preview</h2>
Here is what our game looks like now - the <b>background</b> is moving as well as the <b>ground</b> (but <b>four</b> times faster):<br />
<br />
<iframe frameborder="0" height="450" scrolling="no" src="http://gfycat.com/ifr/GenuineSizzlingDungbeetle" style="-webkit-backface-visibility: hidden; -webkit-transform: scale(1);" width="540"></iframe><br />
<h1>
Back to index</h1>
Click <a href="http://difusal.blogspot.pt/2014/07/minix-posts-index.html">here</a> to go back to the index post.Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-74982118053916508202014-09-06T02:04:00.000+01:002014-09-06T22:25:04.474+01:00[Minix][Tutorial 9] Creating a state machine and a main menuIn this tutorial we are going to create the main menu for our game!<br />
<br />
<h1>
Adding a path builder</h1>
First, get <a href="http://i.imgur.com/5fnUoAB.png">menu-background-0x114.bmp</a>, export it as a <b>.bmp</b> with <b>GIMP</b> like I have explained in the <b>previous</b> tutorial and paste it in your <b>res/images</b> folder. You can <b>delete</b> the <b>test.bmp</b> from the previous tutorial.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLK2IS8QpeaEa6PqRrx4a5deCvGDobpdMlYQd_qe8EEgawKrS0rys308g9W2CHEUaPG86fhsns3tIKDt-51I9-JhnefQ4jB3H2Pu6V-7AjwUb02mRKSgUb2Vk_01LrJDPsePeYOh9qW6E/s1600/Screenshot+from+2014-09-05+22:30:35.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLK2IS8QpeaEa6PqRrx4a5deCvGDobpdMlYQd_qe8EEgawKrS0rys308g9W2CHEUaPG86fhsns3tIKDt-51I9-JhnefQ4jB3H2Pu6V-7AjwUb02mRKSgUb2Vk_01LrJDPsePeYOh9qW6E/s400/Screenshot+from+2014-09-05+22:30:35.png" /></a><br />
<br />
Think of every time we are going to <b>load</b> an image. Isn't it boring to <b>hard code</b> the entire <b>image path</b>? What if in the future we decide to change the images folder location? Man, what a mess to have to change every single hard coded image path! Wouldn't it be better to have a <b>function</b> that given the <b>image name</b>, would <b>build</b> the entire <b>image path</b>? Yes it would! So, let's do it!<br />
<br />
Add the <b>getImagePath</b> function to <b>Utilities</b>, just like shown below.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiEGNMgZ5QASB_p_qmenD0OWHARFBN4YI3Qq4dHut4Vf0JsoSJW2JTYO3WNkYjWfQTInU1BEbiDCJZswgnvfRDYLUh5oYRlZOHQv7jOqqkw3X_Uu8xWhh-AZWQyDitd1hSXsJxLk7uICE/s1600/Screenshot+from+2014-09-05+22:36:09.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiEGNMgZ5QASB_p_qmenD0OWHARFBN4YI3Qq4dHut4Vf0JsoSJW2JTYO3WNkYjWfQTInU1BEbiDCJZswgnvfRDYLUh5oYRlZOHQv7jOqqkw3X_Uu8xWhh-AZWQyDitd1hSXsJxLk7uICE/s400/Screenshot+from+2014-09-05+22:36:09.png" /></a><br />
<br />
Now edit <b>line 24</b> in <b>FlappyNix.c</b>, we should now be able to load images like this. Pretty useful, right?<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6qhdW344U9jT2y9dw8YxTIdvI2qtgii6YU-xGTwqukeL_4qNyQQrzX9MWLRp4nH1DmCWGEaSVzaKtcw9Mp2u0VGI-gKwCkZg7ZfZQeBYuWO7YydBgem8ga9M62gahLxSaem5AMnY675g/s1600/Screenshot+from+2014-09-05+22:38:06.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6qhdW344U9jT2y9dw8YxTIdvI2qtgii6YU-xGTwqukeL_4qNyQQrzX9MWLRp4nH1DmCWGEaSVzaKtcw9Mp2u0VGI-gKwCkZg7ZfZQeBYuWO7YydBgem8ga9M62gahLxSaem5AMnY675g/s400/Screenshot+from+2014-09-05+22:38:06.png" /></a><br />
<br />
<b>Compile</b>, <b>install</b> and <b>run</b> the program. This is how it looks like now:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjG-sI1cZW4G6DW9t8jwWgvrRq9LHaalEx1W984ZhIX2knlYTERZhQEsF2kuHDurN8reGFCs9Dr2vbuBiydqDd0_2dhDDE8lAUw7gNPg5ZKO4_bpJUSYnQ1_eG21hdYTg_n6AzH1EOekBg/s1600/Screenshot+from+2014-09-05+22:40:24.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjG-sI1cZW4G6DW9t8jwWgvrRq9LHaalEx1W984ZhIX2knlYTERZhQEsF2kuHDurN8reGFCs9Dr2vbuBiydqDd0_2dhDDE8lAUw7gNPg5ZKO4_bpJUSYnQ1_eG21hdYTg_n6AzH1EOekBg/s400/Screenshot+from+2014-09-05+22:40:24.png" /></a><br />
<br />
<h1>
Implementing a state machine</h1>
We've got to a point where we will start dealing with <b>states</b>. Every program/game has different states: main menu state, play state, pause state, game over state, etc. In order to manage each state correctly the <b>state machine</b> was invented. It is responsible for state <b>initialization</b>, <b>update</b>, <b>drawing</b> and <b>deletion</b>. So, let's make a few changes and implement our own <b>state machine</b>.<br />
<br />
<h2>
Creating the main menu state</h2>
First, let's create the main menu state structure.<br />
<br />
Create <b>MainMenuState.c</b> and <b>MainMenuState.h</b> and do not forget to declare it in the <b>Makefile</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEix5vmuV9GUwGFz_d0FWHIlpy6j1k9btIY6zF_pNkItZah1uLGUJiT7Y1K7ltytXjlzcoXPYyzaRQSgE47JNhfFt78jzOLbsioCtpInUlxP6q08By525Y2T-6QyIQGLz5Z97vxNwErVSD0/s1600/Screenshot+from+2014-09-05+22:52:40.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEix5vmuV9GUwGFz_d0FWHIlpy6j1k9btIY6zF_pNkItZah1uLGUJiT7Y1K7ltytXjlzcoXPYyzaRQSgE47JNhfFt78jzOLbsioCtpInUlxP6q08By525Y2T-6QyIQGLz5Z97vxNwErVSD0/s400/Screenshot+from+2014-09-05+22:52:40.png" /></a><br />
<br />
To simplify button representation, we will need a <b>Rectangle</b> class. Declare it in the <b>Makefile</b> as well. Here is how it looks, it is pretty simple:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vdFsaVI5BCa58Lj5JRdkHVQPSXKAMLOvfatmEysaIFLSJZbBcosG-jICdtMX8ZAkCtnpQmbyTP6eqtlf537NVY3jzaDhpM3NDFYOdfbQpFYYDhap5hyphenhyphenxGh9KLPi4fekR0ixgYWqJwHI/s1600/Screenshot+from+2014-09-06+00:38:18.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vdFsaVI5BCa58Lj5JRdkHVQPSXKAMLOvfatmEysaIFLSJZbBcosG-jICdtMX8ZAkCtnpQmbyTP6eqtlf537NVY3jzaDhpM3NDFYOdfbQpFYYDhap5hyphenhyphenxGh9KLPi4fekR0ixgYWqJwHI/s400/Screenshot+from+2014-09-06+00:38:18.png" /></a><br />
<br />
Now let's implement the main menu. Here is <b>MainMenuState.h</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghyphenhyphendnYEqQNDF5mUMfgLgcCDPYR2ZqUAiZFnftLq4LfefjdPqKqZdDx3_7KXsc9j1DwNBe6qfK69pOH5lf1lxmxHP6BKfBLZdxaiR9dDh2cWVnBP_o91bT3AKHmY6eRzNklQUa4SmAfOcM/s1600/Screenshot+from+2014-09-06+00:40:57.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghyphenhyphendnYEqQNDF5mUMfgLgcCDPYR2ZqUAiZFnftLq4LfefjdPqKqZdDx3_7KXsc9j1DwNBe6qfK69pOH5lf1lxmxHP6BKfBLZdxaiR9dDh2cWVnBP_o91bT3AKHmY6eRzNklQUa4SmAfOcM/s400/Screenshot+from+2014-09-06+00:40:57.png" /></a><br />
<br />
Long story short: there is an integer to tell if the state is <b>complete/done</b>; a <b>background</b> image; two buttons - <b>play</b> and <b>exit</b> - each with an <b>integer</b> that tells if the mouse is hovering them and a <b>Rectangle</b> representing their boundaries; an integer representing the <b>action performed</b> when the state became done - either play or exit could have been pressed.<br />
<br />
Below is the implementation of the main menu. Paste it to <b>MainMenuState.c</b>:<br />
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;"><span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "MainMenuState.h"</span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "Graphics.h"</span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "Keyboard.h"</span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "Mouse.h"</span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "Utilities.h"</span>
<span class="n" style="box-sizing: border-box;">MainMenuState</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">newMainMenuState</span><span class="p" style="box-sizing: border-box;">()</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">MainMenuState</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">state</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">MainMenuState</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span><span class="p" style="box-sizing: border-box;">)</span> <span class="n" style="box-sizing: border-box;">malloc</span><span class="p" style="box-sizing: border-box;">(</span><span class="k" style="box-sizing: border-box; font-weight: bold;">sizeof</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">MainMenuState</span><span class="p" style="box-sizing: border-box;">));</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">done</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">background</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">loadBitmap</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">getImagePath</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #dd1144;">"menu-background"</span><span class="p" style="box-sizing: border-box;">));</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// these numbers are just meant to create the buttons boundaries</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">double</span> <span class="n" style="box-sizing: border-box;">w</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mf" style="box-sizing: border-box; color: #009999;">.075</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">hi</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mf" style="box-sizing: border-box; color: #009999;">.44</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">hf</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">hi</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="mf" style="box-sizing: border-box; color: #009999;">.12</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">x1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">getHorResolution</span><span class="p" style="box-sizing: border-box;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">/</span> <span class="mi" style="box-sizing: border-box; color: #009999;">2</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="n" style="box-sizing: border-box;">getHorResolution</span><span class="p" style="box-sizing: border-box;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">w</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">x2</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">getHorResolution</span><span class="p" style="box-sizing: border-box;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">/</span> <span class="mi" style="box-sizing: border-box; color: #009999;">2</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="n" style="box-sizing: border-box;">getHorResolution</span><span class="p" style="box-sizing: border-box;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">w</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">y1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">getVerResolution</span><span class="p" style="box-sizing: border-box;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">hi</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">y2</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">getVerResolution</span><span class="p" style="box-sizing: border-box;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">hf</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">playButton</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">newRectangle</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">x1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">y1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">x2</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">y2</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">mouseOnPlay</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">hi</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mf" style="box-sizing: border-box; color: #009999;">.64</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">hf</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">hi</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="mf" style="box-sizing: border-box; color: #009999;">.12</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">y1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">getVerResolution</span><span class="p" style="box-sizing: border-box;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">hi</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">y2</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">getVerResolution</span><span class="p" style="box-sizing: border-box;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">hf</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">exitButton</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">newRectangle</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">x1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">y1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">x2</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">y2</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">mouseOnExit</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="n" style="box-sizing: border-box;">state</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">updateMainMenuState</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">MainMenuState</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">state</span><span class="p" style="box-sizing: border-box;">,</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">long</span> <span class="n" style="box-sizing: border-box;">scancode</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">draw</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// if ESC has been pressed, quit</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">scancode</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">==</span> <span class="n" style="box-sizing: border-box;">KEY_DOWN</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">KEY_ESC</span><span class="p" style="box-sizing: border-box;">))</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">action</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">done</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// if mouse is inside the play button rectangle (boundaries)</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">mouseInsideRect</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">playButton</span><span class="p" style="box-sizing: border-box;">))</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">mouseOnPlay</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">else</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">mouseOnPlay</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// if mouse is inside the exit button rectangle (boundaries)</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">mouseInsideRect</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">exitButton</span><span class="p" style="box-sizing: border-box;">))</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">mouseOnExit</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// and left mouse button has been released</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">getMouse</span><span class="p" style="box-sizing: border-box;">()</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">leftButtonReleased</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">action</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">done</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="p" style="box-sizing: border-box;">}</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">else</span>
<span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">mouseOnExit</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="n" style="box-sizing: border-box;">draw</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">drawMainMenuState</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">MainMenuState</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">state</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">drawBitmap</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">background</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">ALIGN_LEFT</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">mouseOnPlay</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">drawRect</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">playButton</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">YELLOW</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">else</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">mouseOnExit</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">drawRect</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">exitButton</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">YELLOW</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">deleteMainMenuState</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">MainMenuState</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">state</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">deleteBitmap</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">background</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">deleteRectangle</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">playButton</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">deleteRectangle</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">state</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">exitButton</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">free</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">state</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="p" style="box-sizing: border-box;">}</span></pre>
<br />
Contained in the <b>above</b> code snippet are function calls like: <b>mouseInsideRect(state->exitButton)</b> and <b>drawRect(state->playButton, YELLOW)</b><br />
<br />
The first is implemented in <b>Mouse.c</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIDe5TtrFVnU9c05GhqXQMnbF1qMm2Tca3yIGz52zc2YruFW7IfSwQsvxU0Uoqmc2PV4lQTqPRHDHm13ZN5mYefCuVWY-4sBq2lPZeasrv33JcLG47J8HAnqEwk38_MIEszW2xOtClAlU/s1600/Screenshot+from+2014-09-06+00:58:57.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIDe5TtrFVnU9c05GhqXQMnbF1qMm2Tca3yIGz52zc2YruFW7IfSwQsvxU0Uoqmc2PV4lQTqPRHDHm13ZN5mYefCuVWY-4sBq2lPZeasrv33JcLG47J8HAnqEwk38_MIEszW2xOtClAlU/s400/Screenshot+from+2014-09-06+00:58:57.png" /></a><br />
<br />
The second is implemented in <b>Graphics.c</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMOmD6NC0OxwRppFSR98CSO0DuDHt8brYH8VkH0NW-XQwo8lUKDukTw0CNswYzi97ngiuY3RLrjv-inCdpvn5ftOqOuB7Rr0xYo5GiqemFDLe3Tw5Vp9hrR1lM2XInLYN6SvVe5mj5yk8/s1600/Screenshot+from+2014-09-06+01:00:31.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMOmD6NC0OxwRppFSR98CSO0DuDHt8brYH8VkH0NW-XQwo8lUKDukTw0CNswYzi97ngiuY3RLrjv-inCdpvn5ftOqOuB7Rr0xYo5GiqemFDLe3Tw5Vp9hrR1lM2XInLYN6SvVe5mj5yk8/s400/Screenshot+from+2014-09-06+01:00:31.png" /></a><br />
<br />
Ok, now that we have implemented the main menu state, we need to make a lot of changes to FlappyNix and transform it in a state machine.<br />
<br />
<h2>
Modifying FlappyNix to implement a state machine</h2>
<br />
This is how <b>FlappyNix.h</b> looks like now:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhM1zfgMxC-oC-SbbCJEPWqZgEE40GEPZHj_kODL6PQFfXcdMRJEyP49TXcj37BZv-Ew8ycGaeGnhwRFO0VqLXfikJvlHPAJAFFkc8OkF7wmpfwAgmN2MgZpLZxqVOgNqCz3MNnXQFgmzU/s1600/Screenshot+from+2014-09-06+01:06:02.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhM1zfgMxC-oC-SbbCJEPWqZgEE40GEPZHj_kODL6PQFfXcdMRJEyP49TXcj37BZv-Ew8ycGaeGnhwRFO0VqLXfikJvlHPAJAFFkc8OkF7wmpfwAgmN2MgZpLZxqVOgNqCz3MNnXQFgmzU/s400/Screenshot+from+2014-09-06+01:06:02.png" /></a><br />
<br />
We have deleted the test image, as well as our previous yellow rectangle coordinate. Make sure you get rid of anything directly associated to those things from <b>FlappyNix.c</b> as well.<br />
<br />
We now have a <b>void* state;</b> which is a <a href="http://www.c4learn.com/c-programming/c-void-pointers/">generic pointer</a> to the current state of our program. What is a <b>generic pointer</b>? If you have just read the information on that link you might now know that, since there are different states - <b>game over</b>, <b>main menu</b>, etc - but they all fit under the "<b>state</b>" category, we can represent an <b>abstract state</b> with a void pointer/generic pointer. That state can either become the <b>main menu</b> state or the <b>game over</b> state, get it?<br />
<br />
Since there is no way to know the type of that state - because it is just a <b>void pointer</b> - I have added an <a href="http://msdn.microsoft.com/en-us/library/whbyts4t.aspx">enumerator</a> that <u>indicates which state is the current one</u>. Every time the <b>state changes</b>, we will <b>update</b> this <b>currentState</b> indicator - this will enable us to <u>call the right methods on the right state</u> and not make any stupid mistakes like calling <b>drawGameOverState()</b> when the active state is <b>MainMenuState</b>. I hope this was not too confusing and you understood everything.<br />
<br />
Since I have made a lot of changes to <b>FlappyNix.c</b> I am going to paste the whole source file here and try to explain it after.<br />
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;"><span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include <minix/drivers.h></span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "FlappyNix.h"</span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "Graphics.h"</span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "Keyboard.h"</span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "Mouse.h"</span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "MainMenuState.h"</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">const</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">FPS</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">25</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">const</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">mouseFPSmult</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">3</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">checkIfStateIsDone</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">game</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">deleteCurrentState</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">game</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">startFlappyNix</span><span class="p" style="box-sizing: border-box;">()</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">flappy</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span><span class="p" style="box-sizing: border-box;">)</span> <span class="n" style="box-sizing: border-box;">malloc</span><span class="p" style="box-sizing: border-box;">(</span><span class="k" style="box-sizing: border-box; font-weight: bold;">sizeof</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="p" style="box-sizing: border-box;">));</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// subscribing devices</span>
<span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">IRQ_SET_KB</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">kb_subscribe_int</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">IRQ_SET_MOUSE</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">subscribeMouse</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">IRQ_SET_TIMER</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">subscribeTimer</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// resetting timer frequency</span>
<span class="n" style="box-sizing: border-box;">timerSetSquare</span><span class="p" style="box-sizing: border-box;">(</span><span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">mouseFPSmult</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">FPS</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// initializing other variables</span>
<span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">scancode</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">currentState</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">MAIN_MENU_STATE</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">state</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">newMainMenuState</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// finish initialization</span>
<span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">done</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">draw</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">timer</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">newTimer</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="n" style="box-sizing: border-box;">flappy</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">updateFlappyNix</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">flappy</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">ipc_status</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">r</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">message</span> <span class="n" style="box-sizing: border-box;">msg</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">resetTimerTickedFlag</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">timer</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">driver_receive</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">ANY</span><span class="p" style="box-sizing: border-box;">,</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">&</span><span class="n" style="box-sizing: border-box;">msg</span><span class="p" style="box-sizing: border-box;">,</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">&</span><span class="n" style="box-sizing: border-box;">ipc_status</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">!=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">is_ipc_notify</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">ipc_status</span><span class="p" style="box-sizing: border-box;">))</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">switch</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">_ENDPOINT_P</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">msg</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">m_source</span><span class="p" style="box-sizing: border-box;">))</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">case</span> <span class="nl" style="box-sizing: border-box;">HARDWARE</span><span class="p" style="box-sizing: border-box;">:</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// KEYBOARD interruption</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">msg</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">NOTIFY_ARG</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">&</span> <span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">IRQ_SET_KB</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">scancode</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">readKBCState</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// TIMER interruption</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">msg</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">NOTIFY_ARG</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">&</span> <span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">IRQ_SET_TIMER</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">timerHandler</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">timer</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// MOUSE interruption</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">msg</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">NOTIFY_ARG</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">&</span> <span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">IRQ_SET_MOUSE</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">updateMouse</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">default</span><span class="o" style="box-sizing: border-box; font-weight: bold;">:</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">timer</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">ticked</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">getMouse</span><span class="p" style="box-sizing: border-box;">()</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">draw</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">timer</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">counter</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">%</span> <span class="n" style="box-sizing: border-box;">mouseFPSmult</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">==</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// update at 25 FPS</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">switch</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">currentState</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">case</span> <span class="nl" style="box-sizing: border-box;">MAIN_MENU_STATE</span><span class="p" style="box-sizing: border-box;">:</span>
<span class="n" style="box-sizing: border-box;">updateMainMenuState</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">state</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">scancode</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">default</span><span class="o" style="box-sizing: border-box; font-weight: bold;">:</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">scancode</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">draw</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="n" style="box-sizing: border-box;">checkIfStateIsDone</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">drawFlappyNix</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">flappy</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">switch</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">currentState</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">case</span> <span class="nl" style="box-sizing: border-box;">MAIN_MENU_STATE</span><span class="p" style="box-sizing: border-box;">:</span>
<span class="n" style="box-sizing: border-box;">drawMainMenuState</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">state</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">default</span><span class="o" style="box-sizing: border-box; font-weight: bold;">:</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">stopFlappyNix</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">flappy</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">deleteCurrentState</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">deleteMouse</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="n" style="box-sizing: border-box;">deleteTimer</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">timer</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// unsubscribe devices</span>
<span class="n" style="box-sizing: border-box;">kb_unsubscribe_int</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="n" style="box-sizing: border-box;">unsubscribeMouse</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="n" style="box-sizing: border-box;">unsubscribeTimer</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="n" style="box-sizing: border-box;">free</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">changeState</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">game</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">State</span> <span class="n" style="box-sizing: border-box;">newState</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// deleting current state</span>
<span class="n" style="box-sizing: border-box;">deleteCurrentState</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">game</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// changing current state</span>
<span class="n" style="box-sizing: border-box;">game</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">currentState</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">newState</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// creating new state</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">switch</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">game</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">currentState</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">case</span> <span class="nl" style="box-sizing: border-box;">MAIN_MENU_STATE</span><span class="p" style="box-sizing: border-box;">:</span>
<span class="n" style="box-sizing: border-box;">game</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">state</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">newMainMenuState</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="n" style="box-sizing: border-box;">game</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">draw</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">checkIfStateIsDone</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">game</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">switch</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">game</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">currentState</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">case</span> <span class="nl" style="box-sizing: border-box;">MAIN_MENU_STATE</span><span class="p" style="box-sizing: border-box;">:</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(((</span><span class="n" style="box-sizing: border-box;">MainMenuState</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">game</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">state</span><span class="p" style="box-sizing: border-box;">))</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">done</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">action</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="p" style="box-sizing: border-box;">((</span><span class="n" style="box-sizing: border-box;">MainMenuState</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">game</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">state</span><span class="p" style="box-sizing: border-box;">))</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">action</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">switch</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">action</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">case</span> <span class="nl" style="box-sizing: border-box;">PLAY_CHOSEN</span><span class="p" style="box-sizing: border-box;">:</span>
<span class="n" style="box-sizing: border-box;">game</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">done</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">case</span> <span class="nl" style="box-sizing: border-box;">EXIT_CHOSEN</span><span class="p" style="box-sizing: border-box;">:</span>
<span class="n" style="box-sizing: border-box;">game</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">done</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">default</span><span class="o" style="box-sizing: border-box; font-weight: bold;">:</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">deleteCurrentState</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">game</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">switch</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">game</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">currentState</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">case</span> <span class="nl" style="box-sizing: border-box;">MAIN_MENU_STATE</span><span class="p" style="box-sizing: border-box;">:</span>
<span class="n" style="box-sizing: border-box;">deleteMainMenuState</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">game</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">state</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="p" style="box-sizing: border-box;">}</span></pre><br />
So, I will try to explain what is going on...<br />
<br />
The prototypes at <b>lines 13 and 14</b> are there just for <b>visibility purposes</b>. Their actual implementation is near the <b>bottom</b> of the file, but they are called <b>throughout</b> the file inside other functions, so in order to make them <b>visible</b> for the whole file, I have placed their declarations at lines 13 and 14.<br />
<br />
<h2>startFlappyNix</h2>
The previous yellow rectangle related stuff was <b>removed</b>.<br />
The <b>current state</b> and the actual <b>state pointer</b> <u>initialization</u> has been <b>added</b>.<br />
<br />
<h2>updateFlappyNix and drawFlappyNix</h2>
Not much has changed, we now have a <b>switch</b> dependent on the <b>current state</b> enumerator. Again, this enables us to call the <b>right methods</b> for the <b>currently active state</b>. I have explained this just a couple of lines above.<br />
<br />
<h2>
changeState</h2>
This method is called when we want to change from one state to another.<br />
<br />
For example:<br />
In the <b>next</b> tutorial we will be adding the <b>play state</b>. When we implement it and then <b>press the play button</b> on the <b>main menu</b>, this method will be called and what will happen is the following:<br />
the active state (in this case, main menu state) will be <b>deleted</b>, the <b>current state identifier</b> will be <b>updated</b> and then, <b>according</b> to this identifier, the new state will be <b>created</b>. Since we pressed the play button, the <b>play state</b> is expected to be generated.<br />
<br />
<h2>
checkIfStateIsDone</h2>
This method is called <b>right at the end</b> of the <b>update</b> method. It checks if the active state is <b>done/finished</b>.<br />
If it is, depending on the <b>active state</b>, a certain action is performed: the program can be <b>terminated</b> or there can take place a <b>change of state</b>. And yes, if you are wondering if this is the <b>only place</b> where <b>changeState()</b> - the method previously described - can (or should) be called, you are indeed correct.<br />
<br />
<h2>
deleteCurrentState</h2>
This one is easy: according to the <b>current state identifier</b>, the correct <b>delete</b> method for the <b>active state</b> is called.<br />
<br />
<h1>
Main menu preview</h1>
So that's it! Phew, this was a long post! Here is a quick preview of what your program should look like, in particular the main menu and it's buttons being hovered:<br />
<br />
<iframe src="http://gfycat.com/ifr/UnfitSecondEthiopianwolf" frameborder="0" scrolling="no" width="540" height="450" style="-webkit-backface-visibility: hidden;-webkit-transform: scale(1);" ></iframe>
<h1>
Back to index</h1>
Click <a href="http://difusal.blogspot.pt/2014/07/minix-posts-index.html">here</a> to go back to the index post.Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-86601483339590490412014-09-05T20:11:00.002+01:002014-09-07T20:23:32.825+01:00[Minix][Tutorial 8] Loading bmp imagesThis is one of the best tutorials because we are finally going to <b>load some images</b> to our program! This will enable our game to have somewhat cool graphics. So, shall we start?<br />
<br />
<h1>
Implementing the bitmap loader</h1>
Go ahead and create <b>Bitmap.h</b> and <b>Bitmap.c</b> inside your <b>src</b> folder.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1-BfwK00G-1_4gkHXCGioRq8sh-F8jttwS1q_I1lZznceuCqbNE07DJ6WYdryYLyjHwE7NoLKAcV_zuRMJ1EJWrFPTzhyphenhyphen87LUp-kkooDyTTABI_qGNE5dLLueSamClpxg3EI-gjZg-k8/s1600/Screenshot+from+2014-09-05+17:09:57.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1-BfwK00G-1_4gkHXCGioRq8sh-F8jttwS1q_I1lZznceuCqbNE07DJ6WYdryYLyjHwE7NoLKAcV_zuRMJ1EJWrFPTzhyphenhyphen87LUp-kkooDyTTABI_qGNE5dLLueSamClpxg3EI-gjZg-k8/s400/Screenshot+from+2014-09-05+17:09:57.png" /></a><br />
<br />
As usual, do not forget to <b>declare</b> the <b>.c</b> source file in the <b>Makefile</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHAtV-aym37YrYSNffRnhimPR2yzwe89DgG_l9jyGiIFANNUt92LUgl3hwBSwmeRp8VxwWMkKP_xhDPvlaBQhvDs1TPniHu49js5M9ltLMYe-Ln2f1xgAJ-uNqRaIqZPfPCVIL6-ubq6w/s1600/Screenshot+from+2014-09-05+17:10:49.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHAtV-aym37YrYSNffRnhimPR2yzwe89DgG_l9jyGiIFANNUt92LUgl3hwBSwmeRp8VxwWMkKP_xhDPvlaBQhvDs1TPniHu49js5M9ltLMYe-Ln2f1xgAJ-uNqRaIqZPfPCVIL6-ubq6w/s400/Screenshot+from+2014-09-05+17:10:49.png" /></a><br />
<br />
Below are the <b>code snippets</b> of <b>Bitmap.h</b> and <b>Bitmap.c</b>. You should do a little research and make sure to know exactly what is going on, rather than just <b>copy/pasting</b>. <u>LCOM teachers will not tolerate it</u>.<br />
<br />
<h2>
Bitmap.h</h2>
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;"><span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#pragma once</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;">/** @defgroup Bitmap Bitmap</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * @{</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * Functions for manipulating bitmaps</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> */</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">typedef</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">enum</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">ALIGN_LEFT</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">ALIGN_CENTER</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">ALIGN_RIGHT</span>
<span class="p" style="box-sizing: border-box;">}</span> <span class="n" style="box-sizing: border-box;">Alignment</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">typedef</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">struct</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">short</span> <span class="n" style="box-sizing: border-box;">type</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// specifies the file type</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">size</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// specifies the size in bytes of the bitmap file</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">reserved</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// reserved; must be 0</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">offset</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// specifies the offset in bytes from the bitmapfileheader to the bitmap bits</span>
<span class="p" style="box-sizing: border-box;">}</span> <span class="n" style="box-sizing: border-box;">BitmapFileHeader</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">typedef</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">struct</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">size</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// specifies the number of bytes required by the struct</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">width</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// specifies width in pixels</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">height</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// specifies height in pixels</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">short</span> <span class="n" style="box-sizing: border-box;">planes</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// specifies the number of color planes, must be 1</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">short</span> <span class="n" style="box-sizing: border-box;">bits</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// specifies the number of bit per pixel</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">compression</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// specifies the type of compression</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">imageSize</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// size of image in bytes</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">xResolution</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// number of pixels per meter in x axis</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">yResolution</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// number of pixels per meter in y axis</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">nColors</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// number of colors used by the bitmap</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">importantColors</span><span class="p" style="box-sizing: border-box;">;</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// number of colors that are important</span>
<span class="p" style="box-sizing: border-box;">}</span> <span class="n" style="box-sizing: border-box;">BitmapInfoHeader</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">/// Represents a Bitmap</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">typedef</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">struct</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">BitmapInfoHeader</span> <span class="n" style="box-sizing: border-box;">bitmapInfoHeader</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">char</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">bitmapData</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span> <span class="n" style="box-sizing: border-box;">Bitmap</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;">/**</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * @brief Loads a bmp image</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> *</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * @param filename Path of the image to load</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * @return Non NULL pointer to the image buffer</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> */</span>
<span class="n" style="box-sizing: border-box;">Bitmap</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">loadBitmap</span><span class="p" style="box-sizing: border-box;">(</span><span class="k" style="box-sizing: border-box; font-weight: bold;">const</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">char</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">filename</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;">/**</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * @brief Draws an unscaled, unrotated bitmap at the given position</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> *</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * @param bitmap bitmap to be drawn</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * @param x destiny x coord</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * @param y destiny y coord</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * @param alignment image alignment</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> */</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">drawBitmap</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">Bitmap</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">bitmap</span><span class="p" style="box-sizing: border-box;">,</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">x</span><span class="p" style="box-sizing: border-box;">,</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">y</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">Alignment</span> <span class="n" style="box-sizing: border-box;">alignment</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;">/**</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * @brief Destroys the given bitmap, freeing all resources used by it.</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> *</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * @param bitmap bitmap to be destroyed</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> */</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">deleteBitmap</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">Bitmap</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">bmp</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;">/**@}*/</span></pre>
<br />
<h2>
Bitmap.c</h2>
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;"><span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "Bitmap.h"</span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "stdio.h"</span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "Graphics.h"</span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "Utilities.h"</span>
<span class="n" style="box-sizing: border-box;">Bitmap</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">loadBitmap</span><span class="p" style="box-sizing: border-box;">(</span><span class="k" style="box-sizing: border-box; font-weight: bold;">const</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">char</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">filename</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// allocating necessary size</span>
<span class="n" style="box-sizing: border-box;">Bitmap</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">bmp</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">Bitmap</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span><span class="p" style="box-sizing: border-box;">)</span> <span class="n" style="box-sizing: border-box;">malloc</span><span class="p" style="box-sizing: border-box;">(</span><span class="k" style="box-sizing: border-box; font-weight: bold;">sizeof</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">Bitmap</span><span class="p" style="box-sizing: border-box;">));</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// open filename in read binary mode</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">FILE</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span><span class="n" style="box-sizing: border-box;">filePtr</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">filePtr</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">fopen</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">filename</span><span class="p" style="box-sizing: border-box;">,</span> <span class="s" style="box-sizing: border-box; color: #dd1144;">"rb"</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">filePtr</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">==</span> <span class="nb" style="box-sizing: border-box; color: #0086b3;">NULL</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="nb" style="box-sizing: border-box; color: #0086b3;">NULL</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// read the bitmap file header</span>
<span class="n" style="box-sizing: border-box;">BitmapFileHeader</span> <span class="n" style="box-sizing: border-box;">bitmapFileHeader</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">fread</span><span class="p" style="box-sizing: border-box;">(</span><span class="o" style="box-sizing: border-box; font-weight: bold;">&</span><span class="n" style="box-sizing: border-box;">bitmapFileHeader</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #009999;">2</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">filePtr</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// verify that this is a bmp file by check bitmap id</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">bitmapFileHeader</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">type</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">!=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x4D42</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">fclose</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">filePtr</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="nb" style="box-sizing: border-box; color: #0086b3;">NULL</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">rd</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">do</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">((</span><span class="n" style="box-sizing: border-box;">rd</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">fread</span><span class="p" style="box-sizing: border-box;">(</span><span class="o" style="box-sizing: border-box; font-weight: bold;">&</span><span class="n" style="box-sizing: border-box;">bitmapFileHeader</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">size</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #009999;">4</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">filePtr</span><span class="p" style="box-sizing: border-box;">))</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">!=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">((</span><span class="n" style="box-sizing: border-box;">rd</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">fread</span><span class="p" style="box-sizing: border-box;">(</span><span class="o" style="box-sizing: border-box; font-weight: bold;">&</span><span class="n" style="box-sizing: border-box;">bitmapFileHeader</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">reserved</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #009999;">4</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">filePtr</span><span class="p" style="box-sizing: border-box;">))</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">!=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">((</span><span class="n" style="box-sizing: border-box;">rd</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">fread</span><span class="p" style="box-sizing: border-box;">(</span><span class="o" style="box-sizing: border-box; font-weight: bold;">&</span><span class="n" style="box-sizing: border-box;">bitmapFileHeader</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">offset</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #009999;">4</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">filePtr</span><span class="p" style="box-sizing: border-box;">))</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">!=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">break</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">while</span> <span class="p" style="box-sizing: border-box;">(</span><span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">rd</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">!</span><span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">fprintf</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">stderr</span><span class="p" style="box-sizing: border-box;">,</span> <span class="s" style="box-sizing: border-box; color: #dd1144;">"Error reading file</span><span class="se" style="box-sizing: border-box; color: #dd1144;">\n</span><span class="s" style="box-sizing: border-box; color: #dd1144;">"</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">exit</span><span class="p" style="box-sizing: border-box;">(</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-</span><span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// read the bitmap info header</span>
<span class="n" style="box-sizing: border-box;">BitmapInfoHeader</span> <span class="n" style="box-sizing: border-box;">bitmapInfoHeader</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">fread</span><span class="p" style="box-sizing: border-box;">(</span><span class="o" style="box-sizing: border-box; font-weight: bold;">&</span><span class="n" style="box-sizing: border-box;">bitmapInfoHeader</span><span class="p" style="box-sizing: border-box;">,</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">sizeof</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">BitmapInfoHeader</span><span class="p" style="box-sizing: border-box;">),</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">filePtr</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// move file pointer to the begining of bitmap data</span>
<span class="n" style="box-sizing: border-box;">fseek</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">filePtr</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">bitmapFileHeader</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">offset</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">SEEK_SET</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// allocate enough memory for the bitmap image data</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">char</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">bitmapImage</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="p" style="box-sizing: border-box;">(</span><span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">char</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span><span class="p" style="box-sizing: border-box;">)</span> <span class="n" style="box-sizing: border-box;">malloc</span><span class="p" style="box-sizing: border-box;">(</span>
<span class="n" style="box-sizing: border-box;">bitmapInfoHeader</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">imageSize</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// verify memory allocation</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="o" style="box-sizing: border-box; font-weight: bold;">!</span><span class="n" style="box-sizing: border-box;">bitmapImage</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">free</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">bitmapImage</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">fclose</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">filePtr</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="nb" style="box-sizing: border-box; color: #0086b3;">NULL</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// read in the bitmap image data</span>
<span class="n" style="box-sizing: border-box;">fread</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">bitmapImage</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">bitmapInfoHeader</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">imageSize</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">filePtr</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// make sure bitmap image data was read</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">bitmapImage</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">==</span> <span class="nb" style="box-sizing: border-box; color: #0086b3;">NULL</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">fclose</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">filePtr</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="nb" style="box-sizing: border-box; color: #0086b3;">NULL</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// close file and return bitmap image data</span>
<span class="n" style="box-sizing: border-box;">fclose</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">filePtr</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">bmp</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">bitmapData</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">bitmapImage</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">bmp</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">bitmapInfoHeader</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">bitmapInfoHeader</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="n" style="box-sizing: border-box;">bmp</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">drawBitmap</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">Bitmap</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">bmp</span><span class="p" style="box-sizing: border-box;">,</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">x</span><span class="p" style="box-sizing: border-box;">,</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">y</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">Alignment</span> <span class="n" style="box-sizing: border-box;">alignment</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">bmp</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">==</span> <span class="nb" style="box-sizing: border-box; color: #0086b3;">NULL</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">width</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">bmp</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">bitmapInfoHeader</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">width</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">drawWidth</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">width</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">height</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">bmp</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">bitmapInfoHeader</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">height</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">alignment</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">==</span> <span class="n" style="box-sizing: border-box;">ALIGN_CENTER</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">x</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-=</span> <span class="n" style="box-sizing: border-box;">width</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">/</span> <span class="mi" style="box-sizing: border-box; color: #009999;">2</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">else</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">alignment</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">==</span> <span class="n" style="box-sizing: border-box;">ALIGN_RIGHT</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">x</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-=</span> <span class="n" style="box-sizing: border-box;">width</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">x</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="n" style="box-sizing: border-box;">width</span> <span class="o" style="box-sizing: border-box; font-weight: bold;"><</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">||</span> <span class="n" style="box-sizing: border-box;">x</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">></span> <span class="n" style="box-sizing: border-box;">getHorResolution</span><span class="p" style="box-sizing: border-box;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">||</span> <span class="n" style="box-sizing: border-box;">y</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="n" style="box-sizing: border-box;">height</span> <span class="o" style="box-sizing: border-box; font-weight: bold;"><</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span>
<span class="o" style="box-sizing: border-box; font-weight: bold;">||</span> <span class="n" style="box-sizing: border-box;">y</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">></span> <span class="n" style="box-sizing: border-box;">getVerResolution</span><span class="p" style="box-sizing: border-box;">())</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">xCorrection</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">x</span> <span class="o" style="box-sizing: border-box; font-weight: bold;"><</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">xCorrection</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span><span class="n" style="box-sizing: border-box;">x</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">drawWidth</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-=</span> <span class="n" style="box-sizing: border-box;">xCorrection</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">x</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">drawWidth</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">></span> <span class="n" style="box-sizing: border-box;">getHorResolution</span><span class="p" style="box-sizing: border-box;">())</span>
<span class="n" style="box-sizing: border-box;">drawWidth</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">getHorResolution</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="p" style="box-sizing: border-box;">}</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">else</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">x</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="n" style="box-sizing: border-box;">drawWidth</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">>=</span> <span class="n" style="box-sizing: border-box;">getHorResolution</span><span class="p" style="box-sizing: border-box;">())</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">drawWidth</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">getHorResolution</span><span class="p" style="box-sizing: border-box;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="n" style="box-sizing: border-box;">x</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">char</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">bufferStartPos</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">char</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">imgStartPos</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">for</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span> <span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;"><</span> <span class="n" style="box-sizing: border-box;">height</span><span class="p" style="box-sizing: border-box;">;</span> <span class="n" style="box-sizing: border-box;">i</span><span class="o" style="box-sizing: border-box; font-weight: bold;">++</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">pos</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">y</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="n" style="box-sizing: border-box;">height</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">pos</span> <span class="o" style="box-sizing: border-box; font-weight: bold;"><</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">||</span> <span class="n" style="box-sizing: border-box;">pos</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">>=</span> <span class="n" style="box-sizing: border-box;">getVerResolution</span><span class="p" style="box-sizing: border-box;">())</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">continue</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">bufferStartPos</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">getGraphicsBuffer</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="n" style="box-sizing: border-box;">bufferStartPos</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+=</span> <span class="n" style="box-sizing: border-box;">x</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="mi" style="box-sizing: border-box; color: #009999;">2</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="n" style="box-sizing: border-box;">pos</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">getHorResolution</span><span class="p" style="box-sizing: border-box;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="mi" style="box-sizing: border-box; color: #009999;">2</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">imgStartPos</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">bmp</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">bitmapData</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="n" style="box-sizing: border-box;">xCorrection</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="mi" style="box-sizing: border-box; color: #009999;">2</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">width</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="mi" style="box-sizing: border-box; color: #009999;">2</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">memcpy</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">bufferStartPos</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">imgStartPos</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">drawWidth</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="mi" style="box-sizing: border-box; color: #009999;">2</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">deleteBitmap</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">Bitmap</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">bmp</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">bmp</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">==</span> <span class="nb" style="box-sizing: border-box; color: #0086b3;">NULL</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">free</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">bmp</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">bitmapData</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">free</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">bmp</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="p" style="box-sizing: border-box;">}</span></pre>
<br />
You should now <b>compile</b> the program to make sure there are <b>no errors</b> related to what we have just introduced to the project.<br />
Here is a quick explanation about the <b>Bitmap</b> functions: <b>loadBitmap</b>, <b>drawBitmap</b> and <b>deleteBitmap</b>.<br />
<br />
<h2>
loadBitmap</h2>
This function receives a <b>path to an image</b> as a <b>string</b> and <b>loads</b> that image to memory, it then returns a <b>pointer</b> to the memory region where the image was loaded.<br />
<br />
<h2>
drawBitmap</h2>
This function receives a <b>bitmap pointer</b>, a <b>coordinate</b> and an <b>alignment</b>.<br />
<br />
If the <b>alignment</b> is <i>ALIGN_LEFT</i>, the bitmap is drawn with its <b>top left corner</b> at the specified <b>coordinates</b>.<br />
If the <b>alignment</b> is <i>ALIGN_CENTER</i>, the bitmap is drawn with the <b>middle of the top border</b> at the specified <b>coordinates</b>.<br />
If the <b>alignment</b> is <i>ALIGN_RIGHT</i>, the bitmap is drawn with its <b>top right corner</b> at the specified <b>coordinates</b>.<br />
<br />
<h2>
deleteBitmap</h2>
This function receives a <b>pointer</b> to a bitmap and <b>frees</b> the memory used by it, <b>destroying</b> the bitmap.<br />
<br />
<h1>
Preparing an image</h1>
Since we are using a <b>16-bit graphics mode</b>, we can not load every image type. In fact, the load function in <b>Bitmap.c</b> is only able to load <b>.bmp</b> images which are saved in <b>5:6:5</b> mode.<br />
<br />
In order to use images in our project we will need to prepare them by opening them with an editor and save them in this specific format. I am going to use <a href="http://www.gimp.org/">GIMP</a> to edit the images.<br />
<br />
Let's add an image <b>background</b> to our program. Since we are using a graphics mode with a resolution of <b>800x600</b>, I am going to use <a href="http://www.blirk.net/wallpapers/800x600/800x600xuniverse-wallpaper-2.jpg.pagespeed.ic.IqLLbxFKFF.jpg">this</a> image.<br />
<br />
Go ahead and open the image with <b>GIMP</b>. Now select <b>File > Export</b> and name it <b>test.bmp</b>. An export dialog will pop up; expand the <b>Advanced Options</b> and under <b>16 bits</b> select <b>R5 G6 B5</b> and finally press <b>Export</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJD2Pu6NrNvVAC2huoAuxbTEe6T2ipefcG8SqCHOGRIVww-J0EhXXucEn8oA87UHdVmDAd_Obrrdsms1CxMngfIX_wjLqcopUi0nmaMp9OnFDerk18kZBAPg1w7BJgWnqdvL_qkprG6Co/s1600/Screenshot+from+2014-09-05+19:19:26.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJD2Pu6NrNvVAC2huoAuxbTEe6T2ipefcG8SqCHOGRIVww-J0EhXXucEn8oA87UHdVmDAd_Obrrdsms1CxMngfIX_wjLqcopUi0nmaMp9OnFDerk18kZBAPg1w7BJgWnqdvL_qkprG6Co/s400/Screenshot+from+2014-09-05+19:19:26.png" /></a><br />
<br />
This image is now prepared to be correctly loaded by our program, so let's add it. Inside the <b>res</b> folder, create a new folder called <b>images</b>. Paste the exported image - <b>test.bmp</b> - inside the newly created <b>images</b> folder.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGZkg_34lLxV9ya_dAytL_Tde1JCWWp3ZUvyCeLRwa3QynxawhZKfkPrpYOkMLIMyD8DOd22e8INADylYZnpso4ClHyNDjmvE2nVI0pNiui23Vs3egYugZo4le_BBZyAptRVObLbD7aNk/s1600/Screenshot+from+2014-09-05+19:25:57.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGZkg_34lLxV9ya_dAytL_Tde1JCWWp3ZUvyCeLRwa3QynxawhZKfkPrpYOkMLIMyD8DOd22e8INADylYZnpso4ClHyNDjmvE2nVI0pNiui23Vs3egYugZo4le_BBZyAptRVObLbD7aNk/s400/Screenshot+from+2014-09-05+19:25:57.png" /></a><br />
<br />
If you open <b>test.bmp</b> you will notice the quality has <b>decreased</b>. That has to due with the fact we exported it as a <b>16 bit</b> image, since that is the only mode supported by our program. There's nothing to do about it.<br />
<br />
<u>Do not forget to repeat the same exporting process every time you want to add a <b>new</b> image to the project</u>. You first need to <b>export</b> it as a <b>16-bit 5:6:5 .bmp</b> image.<br />
<br />
<h1>
Loading the new background</h1>
Open <b>FlappyNix.h</b> and include <b>Bitmap.h</b>. Also, add a <b>Bitmap*</b> called <b>test</b> to the <b>FlappyNix struct</b>.<br />
Open <b>FlappyNix.c</b> and load the bitmap as shown in <b>line 24</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDb_v37O1OEtTceWt8AmCjqLezE6T9qqozGJbUvJHwkKi5Q01kgDNa94YFpQp0YB-2MHRvc6ztjUe8VMCfCIpVd1zIXdjtSel0tXmXHKQU_GGrQ1qORYmV5kpRtBJIIzMTRD_1PhmTGsg/s1600/Screenshot+from+2014-09-05+19:51:19.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDb_v37O1OEtTceWt8AmCjqLezE6T9qqozGJbUvJHwkKi5Q01kgDNa94YFpQp0YB-2MHRvc6ztjUe8VMCfCIpVd1zIXdjtSel0tXmXHKQU_GGrQ1qORYmV5kpRtBJIIzMTRD_1PhmTGsg/s400/Screenshot+from+2014-09-05+19:51:19.png" /></a><br />
<br />
After that, go to the <b>draw</b> method and instead of filling the screen - remove that line, draw the loaded bitmap - add <b>line 87</b>. Your <b>draw</b> method should now look as follows:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9Cf3lw78S4cuHEm0tm5K44tkWMeFwWtl0IkM764OsyoeFKdXKii9lpHPNc_1jR71pp5V-owUOrmIIYFsqHxbO_j3Mb_M5tpgmD98bgeZgyYKaxifoQoolU5kU8MgokO3-SPZ51E2BnQE/s1600/Screenshot+from+2014-09-05+20:06:16.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9Cf3lw78S4cuHEm0tm5K44tkWMeFwWtl0IkM764OsyoeFKdXKii9lpHPNc_1jR71pp5V-owUOrmIIYFsqHxbO_j3Mb_M5tpgmD98bgeZgyYKaxifoQoolU5kU8MgokO3-SPZ51E2BnQE/s400/Screenshot+from+2014-09-05+20:06:16.png" /></a><br />
<br />
We are now ready to see the final result! But first, do not forget to <b>compile</b> and since we have edited the <b>res</b> folder, you will also need to run the <b>install script</b> again. <u>Always remember</u>, if the <b>res</b> folder is modified, rerun <b>sh install.sh</b>.<br />
<br />
Now you are ready to <b>run</b> the program. It should look something like the image <b>below</b>. Isn't that awesome?<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiXQqTz2V7ioRqiCOpZJANClgMrF-DA6DZiL-LTI899SQGht8LoTUUa30qP6H1oNj8X55sAiASHoE1ltYB-ixsg2PDe8eB5IfLc4-okHbNhju4pINXAHK7ymqTWKZ5F7zwX49Hta9CNRU/s1600/Screenshot+from+2014-09-05+19:46:56.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiXQqTz2V7ioRqiCOpZJANClgMrF-DA6DZiL-LTI899SQGht8LoTUUa30qP6H1oNj8X55sAiASHoE1ltYB-ixsg2PDe8eB5IfLc4-okHbNhju4pINXAHK7ymqTWKZ5F7zwX49Hta9CNRU/s400/Screenshot+from+2014-09-05+19:46:56.png" /></a><br />
<br />
<h1>
Back to index</h1>
Click <a href="http://difusal.blogspot.pt/2014/07/minix-posts-index.html">here</a> to go back to the index post.Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-46353270974680607682014-09-05T16:28:00.000+01:002014-09-05T16:28:40.373+01:00[Minix][Tutorial 7] Adding a mouse to a projectIn this tutorial we will be adding a mouse implementation to our project. We will be able to left-click the rectangle from the previous tutorial to make it go back a bit to the left.<br />
<br />
<h1>
Adding the mouse</h1>
Add the <b>mouse</b> implementation you have developed during class to the <b>src</b> folder. Do not forget to declare it in the <b>Makefile</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD48op1Mo2RAfG-pz0aPDyMYM2t8a2aFfnzZERCkRA1aOv2AlxxPwMSKq0hnpRzkY_XuEkS3nEogtcS0itBI3MAcl8g_r92jTzugvFLB7TUgowbxduMZwQljtfaHLZ_WVVFL0BLSGreH0/s1600/Screenshot+from+2014-09-05+00:47:17.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD48op1Mo2RAfG-pz0aPDyMYM2t8a2aFfnzZERCkRA1aOv2AlxxPwMSKq0hnpRzkY_XuEkS3nEogtcS0itBI3MAcl8g_r92jTzugvFLB7TUgowbxduMZwQljtfaHLZ_WVVFL0BLSGreH0/s400/Screenshot+from+2014-09-05+00:47:17.png" /></a><br />
<br />
I advice you to make a Mouse "class", the one in my <b>Mouse.h</b> looks like this:<br />
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;"><span class="k" style="box-sizing: border-box; font-weight: bold;">typedef</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">struct</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">x</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">y</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">xSign</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">ySign</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">deltaX</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">deltaY</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">double</span> <span class="n" style="box-sizing: border-box;">speedMultiplier</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">byteBeingRead</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">unsigned</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">long</span> <span class="n" style="box-sizing: border-box;">packet</span><span class="p" style="box-sizing: border-box;">[</span><span class="mi" style="box-sizing: border-box; color: #009999;">3</span><span class="p" style="box-sizing: border-box;">];</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">leftButtonDown</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">middleButtonDown</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">rightButtonDown</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">leftButtonReleased</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">rightButtonReleased</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">middleButtonReleased</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">size</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">color1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">color2</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">hasBeenUpdated</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">draw</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span> <span class="n" style="box-sizing: border-box;">Mouse</span><span class="p" style="box-sizing: border-box;">;</span></pre>
You do not need to copy and make exactly like I did. In fact, I encourage you to make it different.<br />
<br />
I want you to think about the mouse now. What two important features should a mouse implementation have in mind?<br />
<ul>
<li>a mouse is global and should be visible to every program;</li>
<li>a mouse is unique, you do not need more than one <b>instance</b>.</li>
</ul>
<br />
<h2>
What is an instance</h2>
Maybe I should have explained this earlier... Oh well, better later than never:<br />
For the beginners, an <a href="http://en.wikipedia.org/wiki/Instance_(computer_science)"><b>instance</b></a> is an occurrence of something. Every <b>object</b> is a <b>class instance</b>.<br />
Think of it like this: a <b>class</b> is like a <b>cake recipe</b>; and an <b>object</b> is the <b>result</b> of that recipe.<br />
Just like you can make multiple cakes, you can create <b>multiple instances</b> of a <b>class</b>: orangeCake, chocolateCake, etc.<br />
Hopefully you already knew this. In case you did not, go study! You should know this by now, and these tutorials are not aimed at explaining these stuff.<br />
<br />
So, let's get back to where I wanted: if you think about it, there should be only <b>one mouse instance</b>, it makes no sense having more than one! This is a common situation where we can implement a <a href="http://en.wikipedia.org/wiki/Software_design_pattern"><b>design pattern</b></a> known as the <a href="http://en.wikipedia.org/wiki/Singleton_pattern"><b>singleton</b></a>.<br />
<br />
We are going to do exactly that in <b>Mouse.c</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKtdzHauiVZfMLngkmrACDDhwvqk9qjM0mAO29I2yU6AbkutwULzCpSbLyQxdxGqNTLo553SDt8byBpTizaHq08fSb49hd3sr8oWPQDioMyLZOfh5Injvs-iYwdGG8UJB7Zi_tvOBrPJQ/s1600/Screenshot+from+2014-09-05+12:55:37.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKtdzHauiVZfMLngkmrACDDhwvqk9qjM0mAO29I2yU6AbkutwULzCpSbLyQxdxGqNTLo553SDt8byBpTizaHq08fSb49hd3sr8oWPQDioMyLZOfh5Injvs-iYwdGG8UJB7Zi_tvOBrPJQ/s400/Screenshot+from+2014-09-05+12:55:37.png" /></a><br />
<br />
Do not forget to add the prototype of the function to <b>Mouse.h</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgNYGM4GB8Y4QOAs7061hL93n_OkxvogHvKs067yEm5FInwChI9eOpeUlFzG5QFrgCc2ic7i4QbqvIYw7jAVmYzFxXuHTBXX-0v_p76hAfuoi9R6gA20S3-6p7IZXUbvO76sgB1p90Cas/s1600/Screenshot+from+2014-09-05+12:55:05.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgNYGM4GB8Y4QOAs7061hL93n_OkxvogHvKs067yEm5FInwChI9eOpeUlFzG5QFrgCc2ic7i4QbqvIYw7jAVmYzFxXuHTBXX-0v_p76hAfuoi9R6gA20S3-6p7IZXUbvO76sgB1p90Cas/s400/Screenshot+from+2014-09-05+12:55:05.png" /></a><br />
<br />
Implement a method to <b>draw</b> the mouse cursor, I chose to have my mouse cursor to look like a crosshair:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDADQmxmf3Axh1oOYf6KLNFmSFPGcnKuzpuRtRAcz33Kfbqcoz0jHhhClOoS8jj4MaEsFUi06qMxaNkpN2Q5Dho-R9HlW9pgctgnOwaHZ1B2CLTxeLZF64t2yL1FbRIYEoGXvK9m6fwTU/s1600/Screenshot+from+2014-09-05+15:07:29.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDADQmxmf3Axh1oOYf6KLNFmSFPGcnKuzpuRtRAcz33Kfbqcoz0jHhhClOoS8jj4MaEsFUi06qMxaNkpN2Q5Dho-R9HlW9pgctgnOwaHZ1B2CLTxeLZF64t2yL1FbRIYEoGXvK9m6fwTU/s400/Screenshot+from+2014-09-05+15:07:29.png" /></a><br />
<br />
Your <b>main.c</b> should now look like this - notice the new <b>drawMouse()</b> function call:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtNdIuuLtzSohp1TITwNDcuxzoNUFnSxHh3HEAbWOaF7ccbeA92F2M2vrzslpfUKgK8zk5ubqrXm-XQHv8DQ92PYRdmvWsiWyHzELKqhR5Pa2XKED9F1uLnltXKltPjmv0YQnEYa-jsxQ/s1600/Screenshot+from+2014-09-05+15:09:51.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtNdIuuLtzSohp1TITwNDcuxzoNUFnSxHh3HEAbWOaF7ccbeA92F2M2vrzslpfUKgK8zk5ubqrXm-XQHv8DQ92PYRdmvWsiWyHzELKqhR5Pa2XKED9F1uLnltXKltPjmv0YQnEYa-jsxQ/s400/Screenshot+from+2014-09-05+15:09:51.png" /></a><br />
<br />
Do not forget to add <b>#include "Mouse.h"</b> everywhere you include mouse function calls.<br />
<br />
<h2>
Updating FlappyNix</h2>
Open <b>FlappyNix.h</b> and add this to the FlappyNix struct:<br />
int IRQ_SET_MOUSE;<br />
<br />
Regarding <b>FlappyNix.c</b>:<br />
Include the mouse header file - <b>line 6</b>.<br />
Add the mouse subscription call - <b>line 16</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-GyWmPWY2gZor7yNa4nnNc0OKDmMRiKWDQLyYxQB8IL9xej_RyEkaTtQo1bMg_6XqyK4T8Ib_2bxcpCaGyg_Rk34OQ9CJt25t42o7EULnSZ5ZpliYENKQZG12bwrekP_Tg7UjR9Zs5k4/s1600/Screenshot+from+2014-09-05+15:20:32.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-GyWmPWY2gZor7yNa4nnNc0OKDmMRiKWDQLyYxQB8IL9xej_RyEkaTtQo1bMg_6XqyK4T8Ib_2bxcpCaGyg_Rk34OQ9CJt25t42o7EULnSZ5ZpliYENKQZG12bwrekP_Tg7UjR9Zs5k4/s400/Screenshot+from+2014-09-05+15:20:32.png" /></a><br />
<br />
Now that we have <b>subscribed</b> mouse interruptions, we have to actually <b>update</b> the mouse when an interruption is issued. We do that in the <b>update</b> method - <b>lines 53 to 55</b>.<br />
<br />
We also need to activate the <b>mouse draw flag</b> to tell the mouse needs to be redrawn. If you look at <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7Gx5j_rUHNQuCrKF4sbu_PhFKNxk7vaUg1gJTbCrVXFsi3Yac8GRQy001IFhIzYDwyg1h20qnZy-UdWrGmBEIi_o09csm-KavOQWVeD3YwB8SfHRW9_nFFnIN6Rcq1yGvbHaP3qPykOo/s1600/Screenshot+from+2014-08-22+22:34:28.png">this</a> picture from the previous tutorial, I left a comment there to remember I had to do it. <u>Use comments, they are useful!</u><br />
<br />
So, do not forget to activate that flag - <b>line 68</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_2ctmBXfDPjZoPXd5pjyziy169nmLrLRIHJ9g0eLfHMFip04sr80oht5VR2eCbjKGg7ihJEmQPpLI3CGCMGrTLDlsVdE8BRgOQQxypVrRPIMcA3eg_rUCbhuNbqD_kkkVYG2fKix4aoE/s1600/Screenshot+from+2014-09-05+15:32:18.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_2ctmBXfDPjZoPXd5pjyziy169nmLrLRIHJ9g0eLfHMFip04sr80oht5VR2eCbjKGg7ihJEmQPpLI3CGCMGrTLDlsVdE8BRgOQQxypVrRPIMcA3eg_rUCbhuNbqD_kkkVYG2fKix4aoE/s400/Screenshot+from+2014-09-05+15:32:18.png" /></a><br />
<br />
We do not need to modify the <b>draw</b> method.<br />
<br />
To finish up, we only need to modify the <b>stop</b> method. Never forget to <b>unsuscribe</b> devices and also never forget to <b>free memory</b> that is no longer used - <b>lines 88 and 91</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRBKfHrH2mhxfEaOYOosHQD0jwZZnlxdZ7H0QJFS-Ls-Put2XCKT90nAsHYhBiRhZMN-RIIu43ylPV6YV005ekZLMqa5PC2jMQ_p1l0Sko0N_6FmR3vHiwPkzYB191jNDm8Gq-ZbAPY5U/s1600/Screenshot+from+2014-09-05+15:40:04.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRBKfHrH2mhxfEaOYOosHQD0jwZZnlxdZ7H0QJFS-Ls-Put2XCKT90nAsHYhBiRhZMN-RIIu43ylPV6YV005ekZLMqa5PC2jMQ_p1l0Sko0N_6FmR3vHiwPkzYB191jNDm8Gq-ZbAPY5U/s400/Screenshot+from+2014-09-05+15:40:04.png" /></a><br />
<br />
And that's it! <b>Compile and run</b> the program. You should see a little crosshair moving around! Isn't that great?<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIa1oOjHK_bIWJszSoX_-fgI4r2EkXu-2FHUOvQcN6gxYlobJp73qfy0Sbj93E9p1sC3mML3iML5sGv1ptSpl6vi3XgdUnIEsx0g86FH-l4O4lwU3a-hoYviX4HXZ4Xir1O54Lea9KNY0/s1600/Screenshot+from+2014-09-05+15:41:05.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIa1oOjHK_bIWJszSoX_-fgI4r2EkXu-2FHUOvQcN6gxYlobJp73qfy0Sbj93E9p1sC3mML3iML5sGv1ptSpl6vi3XgdUnIEsx0g86FH-l4O4lwU3a-hoYviX4HXZ4Xir1O54Lea9KNY0/s400/Screenshot+from+2014-09-05+15:41:05.png" /></a><br />
<br />
If something is not working, please go back to the beginning of this tutorial and make sure you did not miss something. Implementing the mouse can be tricky sometimes.<br />
<br />
<h1>
Using the mouse</h1>
We are now going to use the <b>mouse</b> to interact with the moving rectangle we created previously.<br />
If you recall, the goal is to move the rectangle a bit to the left by pressing on it with the <b>left mouse button</b>.<br />
<br />
<b>Where</b>, <b>when</b> and <b>how</b> should we do this?<br />
It is not that hard: we just need to modify the code in the <b>updateFlappyNix</b> method.<br />
<br />
What we have to check is: if the mouse <b>has been updated</b>, check if the <b>left mouse button was released</b> and also if the mouse was <b>inside the rectangle</b> when that happened - logic, right?<br />
<br />
If <b>all</b> these conditions are verified, we just subtract <b>100</b> to <b>flappy->tempRecX</b> so it shifts <b>100 pixels</b> to the <b>left</b>.<br />
<br />
Here is how <b>updateFlappyNix</b> should look by now:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibMBdvD7ShCQKk4Y_OqaPj-JJB61ccDbeh6J4ZV3GkePxCTn0CQh4WOw3USsrkkRIc6N3gd5wOW0l7fNzg-NMxReus_qGsq1qgkuUVDlyXqJcRvPOzEOq1d5uBXR9ndAL9m1ogHid8ynk/s1600/Screenshot+from+2014-09-05+16:02:04.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibMBdvD7ShCQKk4Y_OqaPj-JJB61ccDbeh6J4ZV3GkePxCTn0CQh4WOw3USsrkkRIc6N3gd5wOW0l7fNzg-NMxReus_qGsq1qgkuUVDlyXqJcRvPOzEOq1d5uBXR9ndAL9m1ogHid8ynk/s400/Screenshot+from+2014-09-05+16:02:04.png" /></a><br />
<br />
Go ahead, <b>compile and run</b> your program. To check how it should look like, see the <b>clip</b> below, where I press the rectangle <b>three times</b>, and watch as the rectangle indeed <b>shifts</b> to the left <b>three times</b>.<br />
<br />
<iframe src="http://gfycat.com/ifr/PertinentIlliterateGrison" frameborder="0" scrolling="no" width="540" height="450" style="-webkit-backface-visibility: hidden;-webkit-transform: scale(1);" ></iframe><br />
<br />
<h1>
Back to index</h1>
Click <a href="http://difusal.blogspot.pt/2014/07/minix-posts-index.html">here</a> to go back to the index post.Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com2tag:blogger.com,1999:blog-3057039820716415448.post-79223290025423546772014-08-22T23:25:00.002+01:002014-08-31T14:05:18.999+01:00[Minix][Tutorial 6] Adding a timer to a projectIn this tutorial we will be adding a timer implementation to our project and making a rectangle move across the screen.<br />
<br />
<h1>
Adding the timer</h1>
Go ahead and add the <b>timer</b> implementation you have developed during class as well as <b>i8254.h</b> to the <b>src</b> folder. Do not forget to declare it in the <b>Makefile</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzNRoagk9KILNolAEwKvHrkqRRWXjEYE1DQtk6WHY7Arulx5t3vF57LE35DwSyQbhSlxpztdzLaMe1C5oQH9-Dn7HohJAUvwIXM_2wn2oP2HAAxJ-n0OXPvgGsmJXi_LSZS6aHzX873w4/s1600/Screenshot+from+2014-08-22+21:50:08.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzNRoagk9KILNolAEwKvHrkqRRWXjEYE1DQtk6WHY7Arulx5t3vF57LE35DwSyQbhSlxpztdzLaMe1C5oQH9-Dn7HohJAUvwIXM_2wn2oP2HAAxJ-n0OXPvgGsmJXi_LSZS6aHzX873w4/s400/Screenshot+from+2014-08-22+21:50:08.png" /></a><br />
<br />
Now we need to declare that FlappyNix has a timer. I have added lines <b>3</b>, <b>7</b> and <b>11</b> to <b>FlappyNix.h</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0-lQhNQofPUU_-VWRaRNEWwQD9Abujnv3mAy_SQTHPTrAucaapCb-OVOcw3GM5rp1w2szC7s8hsPTTo5lrbh9reIkRiVsCF2IcpKh0pe1lW7Xd1pj4dDekXFa6eyNBuDtL6zL7eHryrY/s1600/Screenshot+from+2014-08-22+22:31:23.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0-lQhNQofPUU_-VWRaRNEWwQD9Abujnv3mAy_SQTHPTrAucaapCb-OVOcw3GM5rp1w2szC7s8hsPTTo5lrbh9reIkRiVsCF2IcpKh0pe1lW7Xd1pj4dDekXFa6eyNBuDtL6zL7eHryrY/s400/Screenshot+from+2014-08-22+22:31:23.png" /></a><br />
<br />
We will have to modify the methods of FlappyNix.<br />
<br />
To start, I have added line <b>8</b>: a <b>const int</b> represnting the mouse update FPS multiplier. What is that? It is an integer that multiplied by the FPS will determine the <b>refresh rate</b> of the mouse. Since <b>FPS = 25</b> and <b>mouseFPSmult = 3</b>, the mouse position will be tracked at 25 * 3 = <b>75 FPS</b>.<br />
Regarding the <b>start</b> function, I have added lines <b>15</b>, <b>18</b> and <b>23</b>, which are responsible for <b>subscribing timer interruptions</b>, <b>resetting the timer frequency</b> and <b>creating a new timer</b>, respectively.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiaTEl0FstJBQb2fuRJE3nK5fXvNiIGVfle_PoVAnA3dQkIygJfMnk6J5kNquNVzqHflV6kH8BIBma10IF8SW0rb1HBbM8PxEzFmmBNuMFP14D13UnnxEhLBlfGLizRGLsxhZWmj9Ig2jE/s1600/Screenshot+from+2014-08-22+22:26:51.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiaTEl0FstJBQb2fuRJE3nK5fXvNiIGVfle_PoVAnA3dQkIygJfMnk6J5kNquNVzqHflV6kH8BIBma10IF8SW0rb1HBbM8PxEzFmmBNuMFP14D13UnnxEhLBlfGLizRGLsxhZWmj9Ig2jE/s400/Screenshot+from+2014-08-22+22:26:51.png" /></a><br />
<br />
Regarding the <b>update</b> function, I have added <b>line 32</b>, which at every update, before registering any interruption, <b>resets the timer tick flag</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgx3N6Yn7DI6wd0LqxN31Ou7C6Qk8mKjgbHnMlOq-EctOF0WhJcyrM2wKuID9U8GBD96FmDyke9gSz9O0_EHM2wrsrg1l7D52acJqxAhq-qVvD-wZRfS0SRhtcNzKuN69rWmOzIcGIwfds/s1600/Screenshot+from+2014-08-22+22:34:02.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgx3N6Yn7DI6wd0LqxN31Ou7C6Qk8mKjgbHnMlOq-EctOF0WhJcyrM2wKuID9U8GBD96FmDyke9gSz9O0_EHM2wrsrg1l7D52acJqxAhq-qVvD-wZRfS0SRhtcNzKuN69rWmOzIcGIwfds/s400/Screenshot+from+2014-08-22+22:34:02.png" /></a><br />
<br />
Afterwards, from <b>line 44 to 46</b>, I check for a timer interruption and if there is one, I <b>activate the timer->ticked flag</b> and <b>increment the timer counter</b> - yes, that is exactly what the <b>timerHandler()</b> function does: <br />
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;"><span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">void</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">timerHandler</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">Timer</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">timer</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">timer</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">counter</span><span class="o" style="box-sizing: border-box; font-weight: bold;">++</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">timer</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">ticked</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span></pre>
I have also added <b>lines 58 to 67</b>. <b>Line 60</b> is where the mouse draw function call will be placed when we implement the mouse - <b>75 FPS</b>. Inside this block, there is another block (lines <b>62</b> to <b>66</b>) which is executed at <b>25 FPS</b>. This block is where we are going to update our moving rectangle, but we will get there... Let's focus on planning the <b>program's structure first</b>.<br />
<br />
Moving on, I have not modified the <b>draw</b> function. I did modify the <b>stop</b> function though: I have added a call to <b>unsubscribe</b> the timer and to <b>delete</b> it afterwards, as you can see below.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7Gx5j_rUHNQuCrKF4sbu_PhFKNxk7vaUg1gJTbCrVXFsi3Yac8GRQy001IFhIzYDwyg1h20qnZy-UdWrGmBEIi_o09csm-KavOQWVeD3YwB8SfHRW9_nFFnIN6Rcq1yGvbHaP3qPykOo/s1600/Screenshot+from+2014-08-22+22:34:28.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7Gx5j_rUHNQuCrKF4sbu_PhFKNxk7vaUg1gJTbCrVXFsi3Yac8GRQy001IFhIzYDwyg1h20qnZy-UdWrGmBEIi_o09csm-KavOQWVeD3YwB8SfHRW9_nFFnIN6Rcq1yGvbHaP3qPykOo/s400/Screenshot+from+2014-08-22+22:34:28.png" /></a><br />
<br />
Now is a good time to confirm the program is still able to run without any problems. <b>Compile</b> and <b>run</b> it. So far, although we have added a couple of lines, the program should do exactly the same: show a blue screen and terminate when the <b>Esc</b> key is pressed.<br />
<br />
Is it still working? Good! Let's move on.<br />
<br />
<h1>
Making a rectangle move</h1>
Let's do some interesting stuff: draw a rectangle and make it move horizontally to the right. In order to do this, we will need a variable to save the location of the rectangle - <b>line 13</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKPZXP2apqvHD81QluVrw8gPFO48C_VjPXY5ySk5tYlvAHn1JM04wPRgGJyo9FAk0-2-B9yxcadaffRv0_wHW4ojPTYONe6mRAvCS3xLj0wE2tE-iCeTH7ukUBDRt1yYCiRJSkS9KaGmk/s1600/Screenshot+from+2014-08-22+23:03:43.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKPZXP2apqvHD81QluVrw8gPFO48C_VjPXY5ySk5tYlvAHn1JM04wPRgGJyo9FAk0-2-B9yxcadaffRv0_wHW4ojPTYONe6mRAvCS3xLj0wE2tE-iCeTH7ukUBDRt1yYCiRJSkS9KaGmk/s400/Screenshot+from+2014-08-22+23:03:43.png" /></a><br />
<br />
Now we need to initialize this variable! Let's make the rectangle start at <b>x = 10</b> (line 21).<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSGrNoorLwF7kl9cewT9pRc7Sdklc9lnMLiXe4kje9A-tYk7EMJJJYXWkDfo7KKv9G6Bti28atPXBL6feuzGuxWis6okXNoK-arXZFCDoypScZ8MaVlV-BY0zf78dubcDd2Vt8CwuDmzQ/s1600/Screenshot+from+2014-08-22+23:06:31.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSGrNoorLwF7kl9cewT9pRc7Sdklc9lnMLiXe4kje9A-tYk7EMJJJYXWkDfo7KKv9G6Bti28atPXBL6feuzGuxWis6okXNoK-arXZFCDoypScZ8MaVlV-BY0zf78dubcDd2Vt8CwuDmzQ/s400/Screenshot+from+2014-08-22+23:06:31.png" /></a><br />
<br />
Let's edit the <b>update</b> function and make the <b>x location</b> of the rectangle <b>increment</b> every time - <b>line 67</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJKiavdfTfZsvXacHPBQNRgL5BJH4eiFWwxF8YO3MOA17kx6aZD_myoXR8goDtqWM0OIXNpm4sB7TsdIIQegjVYdeEHOsDQ3ulya2-gsQlR5dEsDFaZ7YNCFE29-W1EAD-EXTiElFpEoo/s1600/Screenshot+from+2014-08-22+23:08:16.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJKiavdfTfZsvXacHPBQNRgL5BJH4eiFWwxF8YO3MOA17kx6aZD_myoXR8goDtqWM0OIXNpm4sB7TsdIIQegjVYdeEHOsDQ3ulya2-gsQlR5dEsDFaZ7YNCFE29-W1EAD-EXTiElFpEoo/s400/Screenshot+from+2014-08-22+23:08:16.png" /></a><br />
<br />
Finally, let's <b>draw</b> a rectangle that starts at <b>flappy->tempRecX</b> with constant width and height of 200 pixels - <b>line 77</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiisMoZcry4oQCADioUdE29gRZ2ASI80ddQC2mRh5FwD61aB73qy98VsML7QKT6U0nro9bkEKOTbqIMo_CU8oghpsK1jTiZf-HUzpRS83UqIYDSU8Pl0ToKNyLSaMThmEh6HuMZt3iYNI4/s1600/Screenshot+from+2014-08-22+23:13:46.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiisMoZcry4oQCADioUdE29gRZ2ASI80ddQC2mRh5FwD61aB73qy98VsML7QKT6U0nro9bkEKOTbqIMo_CU8oghpsK1jTiZf-HUzpRS83UqIYDSU8Pl0ToKNyLSaMThmEh6HuMZt3iYNI4/s400/Screenshot+from+2014-08-22+23:13:46.png" /></a><br />
<br />
<u>Side note</u>: my <b>drawFilledRectangle()</b> function might be different from yours. If you don't understand mine, here is a quick explanation: it receives <b>five arguments</b> - the first two arguments are the <b>x</b> and <b>y</b> coordinates of the <b>top left</b> corner of the rectangle; the second two arguments are the <b>x</b> and <b>y</b> coordinates of the <b>bottom right</b> corner of the rectangle; the last argument is the <b>color</b> of the rectangle.<br />
<br />
And that is it! <b>Compile and run</b> your program and you should see a rectangle moving to the right at constant speed! You should still be able to exit the program by pressing the <b>Esc</b> key on the keyboard. Pretty nice, right?<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZtn5ZHDlP-LPMmepW0460Jq5qy16kZ8Jd6bRtsNrVd_Rf1WuUgU583Eg7LSwyx8MFr0eEgMA9Rg3kNNeOwKAP4NGnSbm0vKxGvsxgcdBmVnGgViPBdHdZl9t06gqFKFKyqNxJfvDQihg/s1600/output_d6LXl0.gif" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZtn5ZHDlP-LPMmepW0460Jq5qy16kZ8Jd6bRtsNrVd_Rf1WuUgU583Eg7LSwyx8MFr0eEgMA9Rg3kNNeOwKAP4NGnSbm0vKxGvsxgcdBmVnGgViPBdHdZl9t06gqFKFKyqNxJfvDQihg/s400/output_d6LXl0.gif" /></a><br />
<br />
<h1>
Back to index</h1>
Click <a href="http://difusal.blogspot.pt/2014/07/minix-posts-index.html">here</a> to go back to the index post.Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-39072821893250479752014-08-22T16:17:00.000+01:002014-10-26T01:56:27.168+00:00[Minix][Tutorial 5] Adding keyboard input to a projectIn the previous tutorial we've added a graphics library to our project. So far, when we run the program, a blue screen is shown for two seconds. In this tutorial we are going to add keyboard features to our program: when we run it a blue screen will appear and the program won't terminate until we press Esc on the keyboard.<br />
<br />
<h1>
Adding keyboard and KBC source code</h1>
During class, you should have developed functions to interact with the <b>keyboard</b> using the <b>kbc</b>. Now it is time to add these to our <b>src</b> folder and add them to the <b>Makefile</b> <b>SRCS</b> tag - I usually declare them by alphabetical order. Below is a screenshot with the changes I have made so far.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwwR0HRzvINWjr0WZYAv-bTrOrkNFjZdoJaNFtV9TjGepwhUm7aCAEvX_YoTWOhzx3SvxQUBaD3dY9r16SB89j7jVMXCkEUC8Rf78ozk__DM8XPVzKf1YxkD3W04wRnx9IMea6hnh81EQ/s1600/Screenshot+from+2014-08-22+13:38:27.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwwR0HRzvINWjr0WZYAv-bTrOrkNFjZdoJaNFtV9TjGepwhUm7aCAEvX_YoTWOhzx3SvxQUBaD3dY9r16SB89j7jVMXCkEUC8Rf78ozk__DM8XPVzKf1YxkD3W04wRnx9IMea6hnh81EQ/s400/Screenshot+from+2014-08-22+13:38:27.png" /></a><br />
<br />
<h1>
Introducing objects in C</h1>
<br />
As you might know, <b>C++ is object-oriented</b>, but <b>C is not</b>. But there is a way to make C programs <b>resemble</b> some object-oriented-like programming language. I think it is time for us to start implementing this in our project.<br />
<br />
Let's create our main object, the engine of our application: flappy-nix. Create both <b>FlappyNix.c</b> and <b>FlappyNix.h</b> inside <b>src</b>.<br />
<br />
After that, what should our "object" FlappyNix contain and represent?<br />
It will have <b>four methods</b> any object should <u>always have</u>:<br />
<ul>
<li>initialize;</li>
<li>update;</li>
<li>draw;</li>
<li>terminate.</li>
</ul>
<br />
It should also have <b>three variables</b>:<br />
<ul>
<li><b>done</b> - tells if the program is done/is going to terminate;</li>
<li><b>draw</b> - a flag that tells the program needs to be redrawn;</li>
<li><b>scancode</b> - whenever a keyboard key is pressed, this will contain the code of that key.</li>
</ul>
<br />
There will also be another variable: <b>IRQ_SET_KB</b>, which will be used to <u>detect keyboard interruptions</u>.<br />
<br />
Here is what my <b>FlappyNix.h</b> looks like so far:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpaXP9P3rPjdlyCLmwmQAjLXHAlLGKQs2HqyFPMiTX0sXT5r_MRpdE1v1N-PVe9wL4Rfc6aEahBUMMvVh1Q8ouDRr3QdQXkC-rM9-t8twQQswoIZG6kHq0Zudv0lJ7V_srMnzVgQTII7s/s1600/Screenshot+from+2014-08-22+14:05:29.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpaXP9P3rPjdlyCLmwmQAjLXHAlLGKQs2HqyFPMiTX0sXT5r_MRpdE1v1N-PVe9wL4Rfc6aEahBUMMvVh1Q8ouDRr3QdQXkC-rM9-t8twQQswoIZG6kHq0Zudv0lJ7V_srMnzVgQTII7s/s400/Screenshot+from+2014-08-22+14:05:29.png" /></a><br />
<br />
Now let's actually implement these methods in <b>FlappyNix.c</b>.<br />
<br />
<h2>
startFlappyNix()</h2>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUOIK8EvJzcffGMckWxusN19SyDavAm9hgpstczFzSXrEN6dqXutfPqywzHsdMAZMCHX5ioMX1Da9X7cdRamxELZINSyMA6EjAhmjiIDzCQjaLAbbPi8QmcSgRYQEsZDwLtvYcK6Thnbg/s1600/Screenshot+from+2014-08-22+14:19:34.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUOIK8EvJzcffGMckWxusN19SyDavAm9hgpstczFzSXrEN6dqXutfPqywzHsdMAZMCHX5ioMX1Da9X7cdRamxELZINSyMA6EjAhmjiIDzCQjaLAbbPi8QmcSgRYQEsZDwLtvYcK6Thnbg/s400/Screenshot+from+2014-08-22+14:19:34.png" /></a><br />
<br />
<b>Side note</b>: notice I have declared a global <b>const int FPS</b>, which is the <b>Frames Per Second</b> at which the application will be redrawn once we implement the timer. For now, don't worry about it.<br />
<br />
Regarding <b>startFlappyNix()</b>, this function returns a <b>pointer</b> to a FlappyNix "object".<br />
First I <b>allocate space</b> for a new FlappyNix.<br />
After that, I <b>subscribe the keyboard</b> to activate keyboard interruptions.<br />
Then, I <b>initialize</b> the object <b>variables</b>.<br />
Finally, I <b>return the pointer</b> to the newly created FlappyNix.<br />
<br />
<h2>
updateFlappyNix(FlappyNix* flappy)</h2>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgwpK20mkkvJ_GGs9OrxCiyXyGiu4RaGF0Zz4t6Jo_VWV1HWZe67bjB2xj-SPqt7Cmn58OleVddDk7XMxpe-qEWnHfOxqNr3131f_rLJmVuDHiLrFbq7H-glHaHI9HJlPRV5ZfA7Jo7oo/s1600/Screenshot+from+2014-08-22+14:38:09.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgwpK20mkkvJ_GGs9OrxCiyXyGiu4RaGF0Zz4t6Jo_VWV1HWZe67bjB2xj-SPqt7Cmn58OleVddDk7XMxpe-qEWnHfOxqNr3131f_rLJmVuDHiLrFbq7H-glHaHI9HJlPRV5ZfA7Jo7oo/s400/Screenshot+from+2014-08-22+14:38:09.png" /></a><br />
<br />
This function should be familiar to you from the LCOM lectures. I'm not going to go into much detail about it: basically, we are checking for <b>hardware interruptions</b>. At the moment we are only registering <b>keyboard interruptions</b> and the code of the pressed key associated to that interruption (lines 32-34). Later we'll be adding code to this function in order to receive <b>timer</b> and <b>mouse</b> interruptions.<br />
Afterwards, we check the <b>scancode</b>, and if it is <u>different than zero</u>, it means a key was pressed. We check if that key was the <b>Esc</b> key. If it was, we tell that the program <b>is done</b>! Easy, right?<br />
<br />
Notice that, just like <b>color names</b>, I also have some <b>key names</b> - line 42 - because that way I don't have to remember every key code or check a table of codes, I just use the <b>key name</b>. As you can see below, I declared these names in <b>Keyboard.h</b>. You can use the <b>snippet</b> below the screenshot in your own projects, it contains every possible <b>key</b> of the keyboard and the respective <b>key code</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2ZmrSz0nxgZW2_XgRO4a4IEO8_KjYTlUtOD2zdhux-Arx1sap2ZrP5XvJK5mBFU7qTYhsIJ7yjPzsbFdKyoprlEU-ZfvMPGpl7MgB7xlBsDy86CGdxMjEm2CVhym0MBEW7dAUqWyc5UU/s1600/Screenshot+from+2014-08-22+15:05:26.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2ZmrSz0nxgZW2_XgRO4a4IEO8_KjYTlUtOD2zdhux-Arx1sap2ZrP5XvJK5mBFU7qTYhsIJ7yjPzsbFdKyoprlEU-ZfvMPGpl7MgB7xlBsDy86CGdxMjEm2CVhym0MBEW7dAUqWyc5UU/s400/Screenshot+from+2014-08-22+15:05:26.png" /></a><br />
<br />
Note: the KEY_UP macro presented in the picture above should make use of '&' instead of '|'.<br />
<br />
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;"><span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">/// Keys</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">typedef</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">enum</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">KEY_NONE</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0000</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_ESC</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0001</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0002</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_2</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0003</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_3</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0004</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_4</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0005</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_5</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0006</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_6</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0007</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_7</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0008</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_8</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0009</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_9</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x000A</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_0</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x000B</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_APOSTROPHE</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x000C</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_ANGLE_QUOTES</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x000D</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_BKSP</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x000E</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_TAB</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x000F</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_Q</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0010</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_W</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0011</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_E</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0012</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_R</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0013</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_T</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0014</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_Y</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0015</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_U</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0016</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_I</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0017</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_O</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0018</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_P</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0019</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_PLUS</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x001A</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_ACCENT</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x001B</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_ENTER</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x001C</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_L_CTRL</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x001D</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_A</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x001E</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_S</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x001F</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_D</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0020</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_F</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0021</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_G</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0022</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_H</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0023</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_J</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0024</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_K</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0025</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_L</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0026</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_C_CEDILLA</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0027</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_ORDINAL</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0028</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_BACKSLASH</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0029</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_L_SHIFT</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x002A</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_TILDE</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x002B</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_Z</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x002C</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_X</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x002D</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_C</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x002E</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_V</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x002F</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_B</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0030</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_N</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0031</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_M</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0032</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_COMMA</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0033</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_DOT</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0034</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_MINUS</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0035</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_R_SHIFT</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0036</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_ALT</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0038</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_SPACE</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0039</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_CAPS</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x003A</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_F1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x003B</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_F2</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x003C</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_F3</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x003D</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_F4</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x003E</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_F5</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x003F</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_F6</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0040</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_F7</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0041</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_F8</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0042</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_F9</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0043</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_F10</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0044</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0045</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_SCRLL</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0046</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_7</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0047</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_8</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0048</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_9</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0049</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_MINUS</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x004A</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_4</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x004B</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_5</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x004C</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_6</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x004D</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_PLUS</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x004E</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x004F</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_2</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0050</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_3</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0051</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_0</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0052</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_DEL</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0053</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_MINOR</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0056</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_F11</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0057</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_F12</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0x0058</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_ENTER</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE01C</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_R_CTRL</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE01D</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_NUM_SLASH</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE035</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_ALT_GR</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE038</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_HOME</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE047</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_ARR_UP</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE048</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_PGUP</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE049</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_ARR_LEFT</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE04B</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_ARR_RIGHT</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE04D</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_ARR_DOWN</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE050</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_PGDN</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE051</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_INS</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE052</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_DEL</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE053</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_WIN</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE05B</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_CNTX</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE05D</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">KEY_END</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mh" style="box-sizing: border-box; color: #009999;">0xE04F</span>
<span class="p" style="box-sizing: border-box;">}</span> <span class="n" style="box-sizing: border-box;">KEY</span><span class="p" style="box-sizing: border-box;">;</span></pre>
<h2>
drawFlappyNix(FlappyNix* flappy)</h2>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNs8_sYZsYRZAG3yv2tGLNVtjdRUC7lgLyXDiLAOqd0m7mDOW5W9A0AAASyPaJCrqBKebRUeajyFm7lHoaUoqoZL-anechLbP42dRZ9gpnZLMtMDnoOyzhsTd1_aPnTH0vWfut8ckIv24/s1600/Screenshot+from+2014-08-22+14:39:44.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNs8_sYZsYRZAG3yv2tGLNVtjdRUC7lgLyXDiLAOqd0m7mDOW5W9A0AAASyPaJCrqBKebRUeajyFm7lHoaUoqoZL-anechLbP42dRZ9gpnZLMtMDnoOyzhsTd1_aPnTH0vWfut8ckIv24/s400/Screenshot+from+2014-08-22+14:39:44.png" /></a><br />
<br />
This one is pretty straight forward: we fill the display with the color blue.<br />
<br />
<h2>
stopFlappyNix(FlappyNix* flappy)</h2>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOfyoWvYx9KILbk0mKfzp36VVKD0FQi9ECtOrYi_IuhPWuu4kiZeUsC8exDx8eFUOM2shUOl-sY8dHwFdheELFBJT13JdPc-cdudqqdDb8DZ8um92fwRBzDBo0gNFw-eDsT4GHkDGEqbo/s1600/Screenshot+from+2014-08-22+14:41:02.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOfyoWvYx9KILbk0mKfzp36VVKD0FQi9ECtOrYi_IuhPWuu4kiZeUsC8exDx8eFUOM2shUOl-sY8dHwFdheELFBJT13JdPc-cdudqqdDb8DZ8um92fwRBzDBo0gNFw-eDsT4GHkDGEqbo/s400/Screenshot+from+2014-08-22+14:41:02.png" /></a><br />
<br />
Here we <b>unsubscribe</b> the keyboard interruptions and <b>free</b> the memory allocated to the FlappyNix "object".<br />
<br />
<h2>
Updating the Makefile</h2>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMwFoIT3P6GXwLvf3ag1-3qKhHmt1Zollc7X_byQXesoqyXY1rQGDij8dNSk3k_Pw37ZofFPVsT0VUuUfcvNsJ9lTE29Yf1TeC_YCvoMYjUagHw0XoMYD9890LUNELiEOIafSNNXswabs/s1600/Screenshot+from+2014-08-22+14:44:50.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMwFoIT3P6GXwLvf3ag1-3qKhHmt1Zollc7X_byQXesoqyXY1rQGDij8dNSk3k_Pw37ZofFPVsT0VUuUfcvNsJ9lTE29Yf1TeC_YCvoMYjUagHw0XoMYD9890LUNELiEOIafSNNXswabs/s400/Screenshot+from+2014-08-22+14:44:50.png" /></a><br />
<br />
Do not forget to update the <b>Makefile</b> each time you add a new source file!<br />
I added a new line - line 8 - where, from now on, I will declare the source code of "objects" we create.<br />
<br />
<h1>
Improving main.c</h1>
Now comes the fun part: let's make use of everything we have been working on and see the results. Go to <b>main.c</b> and edit it according to the screen below.<br />
<br />
First, <b>declare and create</b> a new FlappyNix object.<br />
Then it is really simple: <b>while flappy is not done</b> do the following indefinitely:<br />
<ul>
<li><b>Update</b> flappy;</li>
<li>If flappy is not done and <b>draw flag is active</b> - <b>draw</b> flappy.</li>
</ul>
When flappy is done, the while cycle terminates and so we <b>stop</b> flappy. The rest of the code we already know.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifaDI1UT0frEE-ylW4fJROxoQjNM1OZt9LtYEUCYyKEEOQWQ-6tfyIopJ2CdVKSfQQOoEo0u4noOlBe5SKqTtUXKN-ZJhO296EkoCaI_2RYMWzj4B-ELbCcR6ohv6PBFDxR-P5w78EqpI/s1600/Screenshot+from+2014-08-22+14:55:09.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifaDI1UT0frEE-ylW4fJROxoQjNM1OZt9LtYEUCYyKEEOQWQ-6tfyIopJ2CdVKSfQQOoEo0u4noOlBe5SKqTtUXKN-ZJhO296EkoCaI_2RYMWzj4B-ELbCcR6ohv6PBFDxR-P5w78EqpI/s400/Screenshot+from+2014-08-22+14:55:09.png" /></a><br />
<br />
Below is a <b>snippet</b> with the current content of <b>main.c</b>.<br />
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;"><span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include <minix/sysutil.h></span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include <minix/syslib.h></span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include <minix/drivers.h></span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "FlappyNix.h"</span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include "Graphics.h"</span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">main</span><span class="p" style="box-sizing: border-box;">(</span><span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">argc</span><span class="p" style="box-sizing: border-box;">,</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">char</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">**</span><span class="n" style="box-sizing: border-box;">argv</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">srand</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">time</span><span class="p" style="box-sizing: border-box;">(</span><span class="nb" style="box-sizing: border-box; color: #0086b3;">NULL</span><span class="p" style="box-sizing: border-box;">));</span>
<span class="n" style="box-sizing: border-box;">sef_startup</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;">/*</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * -------------------</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * VESA graphics modes</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * 16-bit (5:6:5)</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * -------------------</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * 320×200 - 0x10E</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * 640×480 - 0x111</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * 800×600 - 0x114</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * 1024×768 - 0x117</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * 1280×1024 - 0x11A</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> * -------------------</span>
<span class="cm" style="box-sizing: border-box; color: #999988; font-style: italic;"> */</span>
<span class="n" style="box-sizing: border-box;">initGraphics</span><span class="p" style="box-sizing: border-box;">(</span><span class="mh" style="box-sizing: border-box; color: #009999;">0x114</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">flappy</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">FlappyNix</span><span class="o" style="box-sizing: border-box; font-weight: bold;">*</span><span class="p" style="box-sizing: border-box;">)</span> <span class="n" style="box-sizing: border-box;">startFlappyNix</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">while</span> <span class="p" style="box-sizing: border-box;">(</span><span class="o" style="box-sizing: border-box; font-weight: bold;">!</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">done</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">updateFlappyNix</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="o" style="box-sizing: border-box; font-weight: bold;">!</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">done</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="o" style="box-sizing: border-box; font-weight: bold;">-></span><span class="n" style="box-sizing: border-box;">draw</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">drawFlappyNix</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">flipMBuffer</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="n" style="box-sizing: border-box;">flipDisplay</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="n" style="box-sizing: border-box;">stopFlappyNix</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">flappy</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">exitGraphics</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span></pre>
<h1>
Testing our project so far</h1>
It's time to test our program! Go to minix, browse to the project folder, <b>compile</b> and <b>run</b> the project.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1EB8ktRajfqH5XV4m3JcSwr3PJii4ZdT90BluEnYZxCIdStufyDBTiwzUFKuW-ALkZA4mSEbCE4z4tt1irUxSuM4oVbciwmSzoQJj078Mzv8BcJJpRxWucEnxDWpbJ3JhlneqdnnkVwY/s1600/Screenshot+from+2014-08-22+14:52:58.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1EB8ktRajfqH5XV4m3JcSwr3PJii4ZdT90BluEnYZxCIdStufyDBTiwzUFKuW-ALkZA4mSEbCE4z4tt1irUxSuM4oVbciwmSzoQJj078Mzv8BcJJpRxWucEnxDWpbJ3JhlneqdnnkVwY/s400/Screenshot+from+2014-08-22+14:52:58.png" /></a><br />
<br />
If everything went well, when you run the program, a blue screen should appear and should only go away when you press the <b>Esc</b> key on the keyboard. And that terminates the program.<br />
<br />
<h1>
Back to index</h1>
Click <a href="http://difusal.blogspot.pt/2014/07/minix-posts-index.html">here</a> to go back to the index post.Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com1tag:blogger.com,1999:blog-3057039820716415448.post-88522113470893498372014-08-21T23:43:00.000+01:002015-01-13T15:56:59.915+00:00[Minix][Tutorial 4] Adding graphics to a projectIn this tutorial we are actually going to start coding flappy-nix.<br />
<br />
<h1>
Important note</h1>
My goal with these tutorials is to <b>help</b> you and <b>guide</b> you through the development of a project and to give you <b>advice</b> and <b>tips</b> for the LCOM course. These guides <b>ARE NOT</b> a way for you to just easily skip the course. Therefore, I will demonstrate how to develop this project from scratch but <u>I will not show you how to code the stuff you are supposed to do in class</u>. Things like developing basic functions to write to the video memory, handle the timer, handle mouse clicks and keyboard presses are expected from you, and I will make use of these abstractions without showing you their source code.<br />
<br />
<h1>
Adding graphics</h1>
The first thing we are going to do is to add some graphics to our program! I will take into account that you already have developed your own graphics library during class.<br />
<br />
You should have <b>two</b> files. I named mine <b>Graphics.c</b> and <b>Graphics.h</b>. As you might know, the <b>.h</b> file is where the functions are declared, where as in the <b>.c</b> the functions are actually defined.<br />
<br />
You should have implemented the VESA BIOS Extensions during class as well. I implemented them in <b>VBE.c</b> and <b>VBE.h</b>.<br />
You should also have a file named <b>LmLib.h</b>, which is provided in the class handout.<br />
<br />
In addition to these, in every project I work, I usually define a <b>Utilities.c</b> and <b>Utilities.h</b> where I define some useful functions. Below is a screenshot of my <b>Utilities</b> current state, where is also visible the <u>current content</u> of the <b>src</b> folder. You should have <b>all</b> these files in there by now.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDW9fWi3aMR0b77Z8JmfwgL8yRF3ql31svAuSO8OvykvXkC0gcc8OPj8ebFsHPrLrRtGNkgo2j5NbpOnQzZsSB3iRgwOOyvwsolL7RErwwGMezIfFrbwnOWo8Or6RVKq4Fvwt_FZvX0Mc/s1600/Screenshot+from+2014-08-21+22:29:18.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDW9fWi3aMR0b77Z8JmfwgL8yRF3ql31svAuSO8OvykvXkC0gcc8OPj8ebFsHPrLrRtGNkgo2j5NbpOnQzZsSB3iRgwOOyvwsolL7RErwwGMezIfFrbwnOWo8Or6RVKq4Fvwt_FZvX0Mc/s400/Screenshot+from+2014-08-21+22:29:18.png" /></a><br />
<br />
In this case, as you can see, I only defined <b>three macros</b> and a function to verify a file's existence. Pay special attention to these macros: <u>you will find them really handy throughout LCOM</u>. If you don't know what a <b>macro</b> is or do not understand what these specific macros are for, I strongly advice you to do a research on google for yourself.<br />
<br />
<h2>
Update Makefile</h2>
Do not forget to constantly update your <b>Makefile</b>: since we've added a lot of files to the <b>src</b> folder, you should declare the <b>.c</b> files after the <b>SRCS</b> label, otherwise the compiler will not know of their existence at compile time.<br />
<br />
Below is my current <b>Makefile</b>. Notice that the new files have been declared at line 7.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUJ79HV45eX8XO7hpRa12PyCmJ21myZEf8taJKbWvRRKRYdtRKSoaEb9rjpHhxRWTgT3ux3aOsdJC2B2nUc0An5qmBb2WkvJJ1s-mFdFHnaedBBbsyQ2rnNxlgdiNHSd3drKgkqJzJUi4/s1600/Screenshot+from+2014-08-21+22:34:54.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUJ79HV45eX8XO7hpRa12PyCmJ21myZEf8taJKbWvRRKRYdtRKSoaEb9rjpHhxRWTgT3ux3aOsdJC2B2nUc0An5qmBb2WkvJJ1s-mFdFHnaedBBbsyQ2rnNxlgdiNHSd3drKgkqJzJUi4/s400/Screenshot+from+2014-08-21+22:34:54.png" /></a><br />
<br />
<h2>
Update main.c</h2>
Now that we have everything set, let's update <b>main.c</b> to make the program fill the display with the color blue for two seconds and then terminate. Here is how my <b>main.c</b> looks like:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvXGdDhrJqkKR-HpisO3fW9y1ynB5RATOQhp-No1UqiYXGBA53vt024sNc0xPHv2T7ehtArSh7Od91VR5xndNWe6UANP3649sqOrKuM9zBcRcr_OCa3gQxyX1o7LhmHPrBCf7JA9Ig4yQ/s1600/Screenshot+from+2014-08-21+22:46:39.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvXGdDhrJqkKR-HpisO3fW9y1ynB5RATOQhp-No1UqiYXGBA53vt024sNc0xPHv2T7ehtArSh7Od91VR5xndNWe6UANP3649sqOrKuM9zBcRcr_OCa3gQxyX1o7LhmHPrBCf7JA9Ig4yQ/s400/Screenshot+from+2014-08-21+22:46:39.png" /></a><br />
<br />
You might be asking: What are all those functions? Where is fillDisplay() defined? Where is BLUE defined?<br />
These are all functions I have developed. You might have developed functions which do almost the same thing but have different names. I will now give you some advice about them.<br />
<br />
As you can see, I have <b>color names</b>! And they are VERY useful. I defined them in <b>Graphics.h</b> as <b>preprocessor directives</b> - look at the screenshot below. This is a nice way to easily access and pass colors to functions! It is a good idea for you to have them. You should notice though that they make use of the <b>rgb()</b> function, which <b>you</b> should implement.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2tE1Myt0HAjx7_sTcmVvi0tRKrec9cZ6SVYbPHiaKgSLZCrNNKvU4JLrs1gTIUSpSd09I72y2c4m5gdKLN3aBslD3Is0WWe9p2m18IvfZV3822AnEISTqAKfCPjyt5OlVm746AfI6nBc/s1600/Screenshot+from+2014-08-21+23:06:41.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2tE1Myt0HAjx7_sTcmVvi0tRKrec9cZ6SVYbPHiaKgSLZCrNNKvU4JLrs1gTIUSpSd09I72y2c4m5gdKLN3aBslD3Is0WWe9p2m18IvfZV3822AnEISTqAKfCPjyt5OlVm746AfI6nBc/s400/Screenshot+from+2014-08-21+23:06:41.png" /></a><br />
<br />
<h2 id="the-mouse-buffer">
The mouse buffer</h2>
I have also implemented two functions called <b>flipMBuffer()</b> and <b>flipDisplay()</b>. What are they used for? Well, it is something similar to a <b>triple-buffer</b>: I have three buffers: <b>videoMem</b>, <b>mBuffer</b> and <b>buffer</b> - <b>flipMBuffer</b> stands for <u>flip mouse buffer</u> and <b>mBuffer</b> stands for <u>mouse buffer</u>.<br />
<br />
Here is how all it works: every graphics function I have defined writes to the <b>buffer</b>. So, say I call <b>drawRectangle()</b>, the function will modify the <b>buffer</b>. Everything <u>except for the mouse</u> is written there: when I call <b>drawMouse()</b>, that function copies the <b>buffer</b> to <b>mBuffer</b> and <u>only then</u> draws the mouse cursor directly in <b>mBuffer</b>. This enables me to <u>update the mouse more frequently</u>, because in order to draw the mouse cursor, I <u>do not need to redraw every polygon of the entire scene</u>, as long as the objects in the scene have not changed! I just copy the <b>buffer</b> to <b>mBuffer</b> again and draw the mouse cursor directly there. Do you get it? This way we can update the mouse more frequently and as a consequence, the mouse cursor movement will look much more fluid. After that, whenever I want to update the display with what has been written to the <b>mBuffer</b>, I just need to call <b>flipDisplay()</b>, which will copy the <b>mBuffer</b> to the <b>video memory</b> - the screen buffer, where we actually see the graphics.<br />
<br />
Below is a draft with the logic behind all this.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjimN-sZmy-W_cvNh7c6cWQquUhD3-ElbUW_DDnCv4Cbq0Bbkaqu-8hunbQT7nbJrIZsBN2U8soiRKDDaj6tzpT4HuH4tofk83jgF8Jj0aNxoqvD46Il_NoqFkPFdDmCXxEm3qcGymWevY/s1600/Untitled.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjimN-sZmy-W_cvNh7c6cWQquUhD3-ElbUW_DDnCv4Cbq0Bbkaqu-8hunbQT7nbJrIZsBN2U8soiRKDDaj6tzpT4HuH4tofk83jgF8Jj0aNxoqvD46Il_NoqFkPFdDmCXxEm3qcGymWevY/s400/Untitled.png" /></a><br />
<br />
<h2>
Testing the program so far</h2>
To test the program, go to minix, browse to the project folder, <b>compile</b> the program and <b>run</b> it. There is <u>no need</u> to run <b>sh install.sh</b> again because we've already run it once; it is only necessary to run it again when we make changes to the <b>res</b> folder.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnKRTbQjF9TS0I1R6apzv-KqhxolG096anVrMGsvAyAHfuF6gCwng2EdRgPdUBPmzLiNWn2tt2K5wSZtTlesMy_F_fu8jvz0dw5UG4LgFoOxNgYd-urazEnA7mxG0V25kCfd_eRw1ugqA/s1600/Screenshot+from+2014-08-21+22:51:08.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnKRTbQjF9TS0I1R6apzv-KqhxolG096anVrMGsvAyAHfuF6gCwng2EdRgPdUBPmzLiNWn2tt2K5wSZtTlesMy_F_fu8jvz0dw5UG4LgFoOxNgYd-urazEnA7mxG0V25kCfd_eRw1ugqA/s400/Screenshot+from+2014-08-21+22:51:08.png" /></a><br />
<br />
And this should be the result: a blue screen lasting two seconds.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvUz9FsLKQ7rLN8DDz4XPmJzUV0k-3boPCNsWFUv9AjbC7RyKIBZuSFFeTQsvrY0Tst2Lw69hD2FrCHfcKyyZ0ot9IZ4pL76w0O7Tyo_OAQNsJ-cfeKqyfw9RMitT9wJMIJGockofK414/s1600/Screenshot+from+2014-08-21+22:51:15.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvUz9FsLKQ7rLN8DDz4XPmJzUV0k-3boPCNsWFUv9AjbC7RyKIBZuSFFeTQsvrY0Tst2Lw69hD2FrCHfcKyyZ0ot9IZ4pL76w0O7Tyo_OAQNsJ-cfeKqyfw9RMitT9wJMIJGockofK414/s400/Screenshot+from+2014-08-21+22:51:15.png" /></a><br />
<br />
<h1>
Back to index</h1>
Click <a href="http://difusal.blogspot.pt/2014/07/minix-posts-index.html">here</a> to go back to the index post.Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-73856305672179344422014-08-21T16:17:00.001+01:002014-09-06T14:52:37.002+01:00[Minix][Tutorial 3] Setting up a project with some useful scriptsIn the previous tutorials we have set everything we need to develop programs for minix:<br />
<ul>
<li>Minix running on a virtual machine through VMware Player;</li>
<li>Eclipse, with Remote System Explorer.</li>
</ul>
From now on, I think people following these tutorials will benefit more if I develop a project from scratch, so that is exactly what I am going to do.<br />
<br />
In this tutorial I will help you to <b>create and set a project</b>. I will also teach you how to create some <b>useful scripts</b> which we will be using a lot during the project development.<br />
<br />
So, let me think of a simple project which will be fun to develop... What about <a href="http://en.wikipedia.org/wiki/Flappy_Bird">Flappy Bird</a>? It makes use of graphics, keyboard, mouse to click the menu buttons, timer, and could easily use more stuff. I think it is a good idea! Let us start?<br />
<br />
<h1>
Create a new project folder</h1>
First of all launch Minix, you know the drill:<br />
<br />
<b>login</b>: lcom<br />
<b>password</b>: lcom<br />
<br />
After logging create a new folder, this will be our project directory. I called mine <b>flappy-nix</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8lrXKCnuDFLo_lPRTrdMsgzmDSWhQ3-6Dme1ULnXjOPHEM4m1PiCjp1Hu5K5ZC9aSviZ1pPyNDBpEahIcSyOCuEnjgYLrRdLVGMPzPZm68jYRV5a9Wc5ey5MM0Zu4Bnvll3yFp_lTb84/s1600/Screenshot+from+2014-08-21+14:11:50.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8lrXKCnuDFLo_lPRTrdMsgzmDSWhQ3-6Dme1ULnXjOPHEM4m1PiCjp1Hu5K5ZC9aSviZ1pPyNDBpEahIcSyOCuEnjgYLrRdLVGMPzPZm68jYRV5a9Wc5ey5MM0Zu4Bnvll3yFp_lTb84/s400/Screenshot+from+2014-08-21+14:11:50.png" /></a><br />
<br />
Now open <b>eclipse</b> and open the <b>RSE</b> perspective. We already have a connection to minix which we created on the previous tutorial. If you open <b>My Home</b> (use the same login and password), the folder we have just created should be listed.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4SGeRXuoZKiIDmLfcD06JktDa2atb0S1ovhwstoXFIabz8PYWMyJO_QKvgJ85TmU3ljm8vk26Yhzx9YF7ov-8NYFxuI8sZwsUc6qKwZ_jkBG9hAFYE-GLnm5wUFW6BYpv23_7lrQokpA/s1600/Screenshot+from+2014-08-21+14:16:15.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4SGeRXuoZKiIDmLfcD06JktDa2atb0S1ovhwstoXFIabz8PYWMyJO_QKvgJ85TmU3ljm8vk26Yhzx9YF7ov-8NYFxuI8sZwsUc6qKwZ_jkBG9hAFYE-GLnm5wUFW6BYpv23_7lrQokpA/s400/Screenshot+from+2014-08-21+14:16:15.png" /></a><br />
<br />
<h1>
Create the project folder structure</h1>
Now is a good time to create our project <b>folder structure</b>. So go ahead and create the following folders through <b>RSE</b>:<br />
<ul>
<li><b>conf</b> - this will contain a configuration file needed to install the program</li>
<li><b>res</b> - this folder will contain all the game resources, such as images</li>
<li><b>src</b> - this is where our source code will be stored</li>
</ul>
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJcsNF08RZouWvGWtN8AxV6nGW_HI6IC28Id0-niAcsMTn9paE1d7AqWtGxN9HCtGmYtiw6RQOLm_qjdTirWbMXJkr4qylui2ZM9rljapvdU1fpj425vro7RORa7_6uNV6dFwWS2_1dJM/s1600/Screenshot+from+2014-08-21+14:24:23.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJcsNF08RZouWvGWtN8AxV6nGW_HI6IC28Id0-niAcsMTn9paE1d7AqWtGxN9HCtGmYtiw6RQOLm_qjdTirWbMXJkr4qylui2ZM9rljapvdU1fpj425vro7RORa7_6uNV6dFwWS2_1dJM/s400/Screenshot+from+2014-08-21+14:24:23.png" /></a><br />
<br />
Next, create <b>main.c</b> inside the <b>src</b> folder.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLF-8qXd3gcPovFr1F70XweMJs51kbpaiZCDfXPOL71Erg_EqkDwL_cM58iSsaE38V1g2LUlkBdoCzCZVI6IOsRrIhILMCM9LpXtcwJKtpGmZkKuYQ2htqpuWRv1f9SZVCJLywTfFNMLI/s1600/Screenshot+from+2014-08-21+14:31:16.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLF-8qXd3gcPovFr1F70XweMJs51kbpaiZCDfXPOL71Erg_EqkDwL_cM58iSsaE38V1g2LUlkBdoCzCZVI6IOsRrIhILMCM9LpXtcwJKtpGmZkKuYQ2htqpuWRv1f9SZVCJLywTfFNMLI/s400/Screenshot+from+2014-08-21+14:31:16.png" /></a><br />
<br />
<h1>
Create the Makefile</h1>
Also create the <b>Makefile</b> inside src.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjTxxdHQdw6vmOnQAZn17kx4TyuFwu_xL2jT91RlYI8at_vnE96j9JG5EPzKjXSz-SMh6LYLP_kKLu6IBWQICpOedDEL6aTZ5LTPHijf2iE7xsgbGt1Ie6Z-gEH6bRP7p3bMD2uZ0zouZ0/s1600/Screenshot+from+2014-08-21+14:35:38.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjTxxdHQdw6vmOnQAZn17kx4TyuFwu_xL2jT91RlYI8at_vnE96j9JG5EPzKjXSz-SMh6LYLP_kKLu6IBWQICpOedDEL6aTZ5LTPHijf2iE7xsgbGt1Ie6Z-gEH6bRP7p3bMD2uZ0zouZ0/s400/Screenshot+from+2014-08-21+14:35:38.png" /></a><br />
<br />
The <b>Makefile</b> is like a set of rules to compile our project. It is a file with <b>no extension</b>, and has to be <b>exactly</b> named <b>Makefile</b> - with the first letter capitalized.<br />
Paste the <b>snippet</b> below to your <b>Makefile</b>.<br />
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;"><span class="nv" style="box-sizing: border-box; color: teal;">COMPILER_TYPE</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> gnu
<span class="nv" style="box-sizing: border-box; color: teal;">CC</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> gcc
<span class="nv" style="box-sizing: border-box; color: teal;">PROG</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> flappy-nix
<span class="nv" style="box-sizing: border-box; color: teal;">SRCS</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> main.c
<span class="nv" style="box-sizing: border-box; color: teal;">CCFLAGS</span><span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> -Wall -O3
<span class="nv" style="box-sizing: border-box; color: teal;">DPADD</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+=</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">${</span><span class="nv" style="box-sizing: border-box; color: teal;">LIBDRIVER</span><span class="k" style="box-sizing: border-box; font-weight: bold;">}</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">${</span><span class="nv" style="box-sizing: border-box; color: teal;">LIBSYS</span><span class="k" style="box-sizing: border-box; font-weight: bold;">}</span>
<span class="nv" style="box-sizing: border-box; color: teal;">LDADD</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+=</span> -llm -ldriver -lsys
<span class="nv" style="box-sizing: border-box; color: teal;">LDFLAGS</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+=</span> -L.
<span class="nv" style="box-sizing: border-box; color: teal;">MAN</span><span class="o" style="box-sizing: border-box; font-weight: bold;">=</span>
<span class="err" style="background-color: #e3d2d2; box-sizing: border-box; color: #a61717;">BINDIR?</span> <span class="err" style="background-color: #e3d2d2; box-sizing: border-box; color: #a61717;">=</span> <span class="err" style="background-color: #e3d2d2; box-sizing: border-box; color: #a61717;">/usr/sbin</span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">.include <bsd.prog.mk></span>
<span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">.include <bsd.gcc.mk></span></pre>
Notice that <b>PROG</b> should have the same name as the project folder. You should also remember that every time you create a source file they should be declared in <b>SRCS</b> (only the <b>.c</b> files, not the <b>.h</b>). That's why <b>SRCS = main.c</b>. We will be adding more source files later.<br />
<br />
We will also need a <b>low memory library</b>. <a href="https://drive.google.com/file/d/0B_TLl7qWyzYHMWd1QUJRbV9aVlE/edit?usp=sharing">Download</a> it and <b>drag it</b> to the <b>src</b> folder.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZG5eBzumT2nvFvEhPYAVShOwKypbEpB93P25XeUzrxNAeSF3g3g7i9Zx2L3bokKyRk98IynXYxwxD0XxNRA8aZmwveoLT-5EepeoQbQ8FpSauL4tk_shn9CUweJlimAfYV5EMltzaO0o/s1600/Screenshot+from+2014-08-21+14:50:57.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZG5eBzumT2nvFvEhPYAVShOwKypbEpB93P25XeUzrxNAeSF3g3g7i9Zx2L3bokKyRk98IynXYxwxD0XxNRA8aZmwveoLT-5EepeoQbQ8FpSauL4tk_shn9CUweJlimAfYV5EMltzaO0o/s400/Screenshot+from+2014-08-21+14:50:57.png" /></a><br />
<br />
<h1>
Create amazing scripts</h1>
Trust me, <b>scripts are great</b>! They save a lot of time and typing. I will show you how to create <b>three</b> scripts you will fall in love with.<br />
<br />
To create a script <b>right-click</b> the project folder, select <b>New > File</b> and then type the name of the script.<br />
<br />
<h2>
install.sh</h2>
This script does the following:
<br />
<ul>
<li>Installs the system configuration file required to execute it with the right permissions;</li>
<li>Copies the <b>res</b> folder to a safe and convenient destination;</li>
<li>Modifies the remaining scripts permissions.</li>
</ul>
<br />
Paste this <b>snippet</b> to <b>install.sh</b>:<br />
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;">cp conf/flappy-nix /etc/system.conf.d
mkdir /home/flappy-nix
cp -vr res/ /home/flappy-nix
chmod <span class="m" style="box-sizing: border-box; color: #009999;">777</span> compile.sh
chmod <span class="m" style="box-sizing: border-box; color: #009999;">777</span> run.sh</pre>
<br />
<h2>
compile.sh</h2>
This script compiles the project. Paste the <b>snippet</b> below to <b>compile.sh</b>:<br />
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;"><span class="nb" style="box-sizing: border-box; color: #0086b3;">cd </span>src
make clean install
mv flappy-nix ../
<span class="nb" style="box-sizing: border-box; color: #0086b3;">cd</span> ..
strip --strip-all flappy-nix</pre>
<br />
<h2>
run.sh</h2>
This script executes the project. Paste the following <b>snippet</b> to <b>run.sh</b>:<br />
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;">service run <span class="sb" style="box-sizing: border-box; color: #dd1144;">`</span><span class="nb" style="box-sizing: border-box; color: #0086b3;">pwd</span><span class="sb" style="box-sizing: border-box; color: #dd1144;">`</span>/flappy-nix</pre>
After all these copy/pasting, you should have <b>exactly</b> what is shown on the picture below.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGOu7TUHLu8n4XXKX0I58bS-QcjCxb28Fu9LrFGAT-CNqW7yZu939TCYCSYCzuWwlwjpTjAfb179gSuluECvOG47DLIit3V3GfND-SuzL-qnQWMc3aYcWwnStvelL3b3Pjm5P1e3-qMAg/s1600/Screenshot+from+2014-08-21+15:22:47.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGOu7TUHLu8n4XXKX0I58bS-QcjCxb28Fu9LrFGAT-CNqW7yZu939TCYCSYCzuWwlwjpTjAfb179gSuluECvOG47DLIit3V3GfND-SuzL-qnQWMc3aYcWwnStvelL3b3Pjm5P1e3-qMAg/s400/Screenshot+from+2014-08-21+15:22:47.png" /></a><br />
<br />
Why did we bother to create these scripts?<br />
Well, because every time we need to compile our project (for example), instead of typing all the stuff we pasted to <b>compile.sh</b> by hand, we will just have to type: <b>sh compile.sh</b>! Isn't that great? You want to run the project? Easy! You just have to type: <b>sh run.sh.</b><br />
<br />
We need to do one more thing: create the system configuration file. <b>This is a must!</b><br />
<b>Right-click</b> the <b>conf</b> folder, select <b>New > File</b> and type <b>flappy-nix</b>. Then paste the <b>snippet</b> below.<br />
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;">service flappy-nix <span class="o" style="box-sizing: border-box; font-weight: bold;">{</span>
system
DEVIO
IRQCTL
UMAP
INT86
PRIVCTL
READBIOS
IOPENABLE
<span class="p" style="box-sizing: border-box;">;</span>
ipc
SYSTEM
rs
vm
pm
vfs
<span class="p" style="box-sizing: border-box;">;</span>
io
40:4 <span class="c" style="box-sizing: border-box; color: #999988; font-style: italic;"># Timer</span>
<span class="m" style="box-sizing: border-box; color: #009999;">60</span> <span class="c" style="box-sizing: border-box; color: #999988; font-style: italic;"># KBC</span>
<span class="m" style="box-sizing: border-box; color: #009999;">64</span> <span class="c" style="box-sizing: border-box; color: #999988; font-style: italic;"># KBC</span>
70:2 <span class="c" style="box-sizing: border-box; color: #999988; font-style: italic;"># RTC</span>
2f8:8 <span class="c" style="box-sizing: border-box; color: #999988; font-style: italic;"># COM2</span>
3f8:8 <span class="c" style="box-sizing: border-box; color: #999988; font-style: italic;"># COM1</span>
<span class="p" style="box-sizing: border-box;">;</span>
irq
<span class="m" style="box-sizing: border-box; color: #009999;">0</span> <span class="c" style="box-sizing: border-box; color: #999988; font-style: italic;"># TIMER 0 IRQ</span>
<span class="m" style="box-sizing: border-box; color: #009999;">1</span> <span class="c" style="box-sizing: border-box; color: #999988; font-style: italic;"># KBD IRQ</span>
<span class="m" style="box-sizing: border-box; color: #009999;">3</span> <span class="c" style="box-sizing: border-box; color: #999988; font-style: italic;"># COM2 IRQ</span>
<span class="m" style="box-sizing: border-box; color: #009999;">4</span> <span class="c" style="box-sizing: border-box; color: #999988; font-style: italic;"># COM1 IRQ</span>
<span class="m" style="box-sizing: border-box; color: #009999;">8</span> <span class="c" style="box-sizing: border-box; color: #999988; font-style: italic;"># RTC</span>
<span class="m" style="box-sizing: border-box; color: #009999;">12</span> <span class="c" style="box-sizing: border-box; color: #999988; font-style: italic;"># AUX/MOUSE IRQ</span>
<span class="p" style="box-sizing: border-box;">;</span>
uid
0
<span class="p" style="box-sizing: border-box;">;</span>
<span class="o" style="box-sizing: border-box; font-weight: bold;">}</span><span class="p" style="box-sizing: border-box;">;</span></pre>
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2Pr_lfutQEFvuC-M1vO56PPZ0RKsWsJd4_j9cl3F94yfy8MiBiltldZA8emqBDEFgHheP7doQ36u3OTxDx9ZEKYrzUAWEsnQL-dmfzeAN462N7lvZCzpYh3y_L2GpC3vwnabuHIe-8kI/s1600/Screenshot+from+2014-08-21+15:49:49.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2Pr_lfutQEFvuC-M1vO56PPZ0RKsWsJd4_j9cl3F94yfy8MiBiltldZA8emqBDEFgHheP7doQ36u3OTxDx9ZEKYrzUAWEsnQL-dmfzeAN462N7lvZCzpYh3y_L2GpC3vwnabuHIe-8kI/s400/Screenshot+from+2014-08-21+15:49:49.png" /></a><br />
<br />
<h1>
Finishing up</h1>
We have already done a lot during this tutorial! I think it is enough for now. So let's just check if everything is working as it should.<br />
<br />
Open <b>main.c</b> and write a simple hello world program, such as the following:<br />
<pre style="background-color: #f7f7f7; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; line-height: 1.45; overflow: auto; padding: 16px; word-break: normal; word-wrap: normal;"><span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include <stdio.h></span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">main</span><span class="p" style="box-sizing: border-box;">(</span><span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="n" style="box-sizing: border-box;">argc</span><span class="p" style="box-sizing: border-box;">,</span> <span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">char</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">**</span><span class="n" style="box-sizing: border-box;">argv</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">printf</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #dd1144;">"Hello, minix!</span><span class="se" style="box-sizing: border-box; color: #dd1144;">\n</span><span class="s" style="box-sizing: border-box; color: #dd1144;">"</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span></pre>
Now go to minix, go to the project folder and enter superuser mode by typing the command: <b>su</b>.<br />
<br />
Then execute the scripts by the following order:<br />
<ul>
<li>install.sh</li>
<li>compile.sh</li>
<li>run.sh</li>
</ul>
<br />
It should give an error, but that's ok for now as long as you see the "Hello, minix!" message.<br />
Here is a screenshot of my output for comparison:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihOZoxwCciWOfsTuwK5l_X5e7OCXooTuZkmsmlKIQ2hCiji7X6xTVkSJ_gLpK80l_XwXwiP3vSr_9BfJ_Zk3Dy1mgWWQwSeQ4MNq-0FxElqBEhrNPC-1_bgsQ8ZtqDiUnA2WTTvYfEtT8/s1600/Screenshot+from+2014-08-21+16:14:16.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihOZoxwCciWOfsTuwK5l_X5e7OCXooTuZkmsmlKIQ2hCiji7X6xTVkSJ_gLpK80l_XwXwiP3vSr_9BfJ_Zk3Dy1mgWWQwSeQ4MNq-0FxElqBEhrNPC-1_bgsQ8ZtqDiUnA2WTTvYfEtT8/s400/Screenshot+from+2014-08-21+16:14:16.png" /></a><br />
<br />
<h2>
Killing a service</h2>
If for some reason you run your program again and you get something like:<br />
<br />
<i>Request to RS failed : Resource busy (error 16)</i><br />
<br />
That means that the program <b>did not</b> finish correctly and it's <b>service</b> is still running in the background. To <b>kill</b>/stop it, just run the command:<br />
<br />
<i>service down name_of_the_service_to_be_killed</i><br />
<br />
In our case, it would be:<br />
<br />
<i>service down flappy-nix</i><br />
<br />
This will <b>kill</b> the service and thus enable you to <b>run</b> it again. But do not get away with it this way, you should look for what is causing the problem and <b>fix it</b>.<br />
This is a very useful command for the <b>entire</b> development of the project.<br />
<br />
<h1>
Back to index</h1>
Click <a href="http://difusal.blogspot.pt/2014/07/minix-posts-index.html">here</a> to go back to the index post.Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com1tag:blogger.com,1999:blog-3057039820716415448.post-37204262498860063972014-08-04T00:51:00.000+01:002014-08-05T11:12:58.924+01:00Manacher's algorithm: longest palindromic substring problemIn this post I'll try to explain Manacher's algorithm in a simple way. Manacher's algorithm is a solution for finding the <a href="http://en.wikipedia.org/wiki/Longest_palindromic_substring">longest palindromic substring</a> of a given string.<br />
<br />
A <a href="http://en.wikipedia.org/wiki/Palindrome">palindrome</a> is a sequence of symbols or elements that reads the same forward or reversed.<br />
Examples of a few palindromes:<br />
<br />
<div style="text-align: center;">
radar    level    rotor    noon</div>
<div style="text-align: center;">
abba    aba    b    xxx1y1xxx</div>
<br />
Manacher's algorithm is a really good/fast solution for this problem since its time and space complexity is <b>O(N)</b>.<br />
<br />
<h1>
Pre-process string S</h1>
The first step of the algorithm is to pre-process a given string <i>S</i>, into string <i>T</i>, by inserting a special character between every character of <i>S</i> and also at both ends. For the algorithm future convenience, a distinct special character should then be placed at both ends. This pre-processing is done just to handle <b>both even and odd</b> sized <i>S</i> strings gracefully.<br />
<br />
If say, '<b>#</b>' is used as the main special character, these are some examples of the original strings and their resulting transformations:<br />
<pre style="background-color: #f8f8f8; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 1px solid rgb(221, 221, 221); box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13px; line-height: 19px; margin-bottom: 15px; margin-top: 15px; overflow: auto; padding: 6px 10px; word-wrap: normal;"><code style="background: transparent; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: none; box-sizing: border-box; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: inherit; margin: 0px; padding: 0px; word-wrap: normal;">S: abba T: ^#a#b#b#a#$</code></pre>
<pre style="background-color: #f8f8f8; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 1px solid rgb(221, 221, 221); box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13px; line-height: 19px; margin-top: 15px; overflow: auto; padding: 6px 10px; word-wrap: normal;"><code style="background: transparent; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: none; box-sizing: border-box; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: inherit; margin: 0px; padding: 0px; word-wrap: normal;">S: a T: ^#a#$</code></pre>
Now would be a good time to code this first step. <b>Try to code it for yourself before seeing the snippet below</b>, it should be pretty straight forward.<br />
When you are done, compare your solution with the <b>pseudo code</b> below.<br />
<pre style="background-color: #f8f8f8; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 1px solid rgb(221, 221, 221); box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13px; line-height: 19px; margin-bottom: 15px; margin-top: 15px; overflow: auto; padding: 6px 10px; word-wrap: normal;"><span class="n" style="box-sizing: border-box;">preprocessManacherAlgorithm</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">s</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">s</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">length</span><span class="p" style="box-sizing: border-box;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">==</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="s" style="box-sizing: border-box; color: #dd1144;">"^$"</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">t</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="s" style="box-sizing: border-box; color: #dd1144;">"^"</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">for</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span> <span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;"><</span> <span class="n" style="box-sizing: border-box;">s</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">length</span><span class="p" style="box-sizing: border-box;">();</span> <span class="n" style="box-sizing: border-box;">i</span><span class="o" style="box-sizing: border-box; font-weight: bold;">++</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">t</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+=</span> <span class="s" style="box-sizing: border-box; color: #dd1144;">"#"</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="n" style="box-sizing: border-box;">s</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">];</span>
<span class="n" style="box-sizing: border-box;">t</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+=</span> <span class="s" style="box-sizing: border-box; color: #dd1144;">"#$"</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="n" style="box-sizing: border-box;">t</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span></pre>
<h1>
Process string T</h1>
After having pre-processed a given string <i>S</i> into string <i>T</i>, the next step is to process string <i>T</i> itself. To do that, an <b>auxiliary array <i>P</i></b> with size equal to the length of <i>T</i> is required.<br />
<br />
The <b>content</b> of the <b><i>i-th</i></b> element of <i>P</i> corresponds to the <b>size of the maximum expansion to both sides</b> of the <b><i>i-th</i></b> element of <i>T</i> in order to form a <b>palindrome</b>.<br />
<br />
Was that too confusing? Look at the example below to better comprehend the purpose of <i>P</i>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVXy44LyhqSWcHIvLkqr-sgoa63WKOU56LlSiMSt9oxqjEps1FevNEBko5SaSxIZyO3yuMwFC0BJrBkwkJZLGZQHFolEdM0UuT5IzSKBEEDRQp2OSe1dtrj_OMuNDP_6DXc2I2PYGKFb4/s1600/Untitled.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVXy44LyhqSWcHIvLkqr-sgoa63WKOU56LlSiMSt9oxqjEps1FevNEBko5SaSxIZyO3yuMwFC0BJrBkwkJZLGZQHFolEdM0UuT5IzSKBEEDRQp2OSe1dtrj_OMuNDP_6DXc2I2PYGKFb4/s400/Untitled.png" /></a><br />
<br />
What is the longest palindromic substring contained in <i>T</i>? The answer is <b># a # b # a #</b>, which has a length of <b>7 characters</b>, right? Also notice that the character '<b>b</b>', <b>i = 4</b>, is the <b>center</b> of the palindrome.<br />
<br />
So, now the question is: <b>how many characters of the palindrome are to the right or to the left of the palindrome's center?</b> Or in other words: <b>what is the size of the expansion to both left or right of the center character which makes up the palindrome?</b><br />
The answer is <b>3</b>. The image below illustrates this.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8G6Xj9lYyBimRPqDbtpC8dw5AAmp16bBATqR2ST-wtNmc2fw0OvdyN3K_ssKXVYB5r1Y1l7EQRc0Y4_pdGJ1Ck0fDAjmJ2jD2NBu3JFQEl69NjFeCCl3eeRwmbgIPuvzb5lqysHEmEl0/s1600/Screenshot+from+2014-08-03+22:51:42.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8G6Xj9lYyBimRPqDbtpC8dw5AAmp16bBATqR2ST-wtNmc2fw0OvdyN3K_ssKXVYB5r1Y1l7EQRc0Y4_pdGJ1Ck0fDAjmJ2jD2NBu3JFQEl69NjFeCCl3eeRwmbgIPuvzb5lqysHEmEl0/s400/Screenshot+from+2014-08-03+22:51:42.png" /></a><br />
<br />
The goal is to populate <i>P</i>, and that task should now be easy.<br />
The result is as follows:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheI4F0jLayLz8AiVVwj65eTahf6TpEx-zV_GJ9EOVRxGnRFQvjgmeLfjLylHhtIRiseAWxJ-xAR-TnysdgGDBvkRM_BOIrdyHXlYkogkEIRlzX3s1mczllaVp6J1_KZFtXlOmrFxKin4A/s1600/Screenshot+from+2014-08-03+22:52:20.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheI4F0jLayLz8AiVVwj65eTahf6TpEx-zV_GJ9EOVRxGnRFQvjgmeLfjLylHhtIRiseAWxJ-xAR-TnysdgGDBvkRM_BOIrdyHXlYkogkEIRlzX3s1mczllaVp6J1_KZFtXlOmrFxKin4A/s400/Screenshot+from+2014-08-03+22:52:20.png" /></a><br />
<br />
<b>Important note</b>:<br />
It is a good idea to have an <b>auxiliary variable maxPalCenterID</b> with the <b>index</b> of the highest value in <i>P</i> and <b>update</b> it while we populate <i>P</i>.<br />
<br />
After having populated <i>P</i>, the longest palindromic substring is very easy to fetch from the original string <i>S</i>.<br />
<br />
Since there is a special character at the start of <i>T</i> due to the pre-processing, and also another special character between every character of <i>S</i>, the palindrome start index is given by the formula below.<br />
<pre style="background-color: #f8f8f8; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 1px solid rgb(221, 221, 221); box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13px; line-height: 19px; margin-bottom: 15px; margin-top: 15px; overflow: auto; padding: 6px 10px; word-wrap: normal;"><span class="n" style="box-sizing: border-box;">palStartID</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">maxPalCenterID</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">maxPalCenterID</span><span class="p" style="box-sizing: border-box;">])</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">/</span> <span class="mi" style="box-sizing: border-box; color: #009999;">2</span><span class="p" style="box-sizing: border-box;">;</span></pre>
At this time, the longest palindromic substring <b>has been found successfully</b>, and though this is already a pretty good solution, <b>it does not have a time and space complexity of O(N)</b>. In order to reach that complexity, there is a <b>trick</b> which Manacher's algorithm makes use of.<br />
<br />
You should try to code <b>by yourself</b> the algorithm so far. <b>Do not peek the snippet below</b>. It is important that you <b>try for yourself</b> and only after compare your solution with the <b>pseudo code</b> below.<br />
<pre style="background-color: #f8f8f8; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 1px solid rgb(221, 221, 221); box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13px; line-height: 19px; margin-bottom: 15px; margin-top: 15px; overflow: auto; padding: 6px 10px; word-wrap: normal;"><span class="n" style="box-sizing: border-box;">almostManacherAlgorithm</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">s</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">t</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">preprocessManacherAlgorithm</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">s</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">n</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">t</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">size</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">n</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">];</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// array of size n filled with 0</span>
<span class="n" style="box-sizing: border-box;">maxPalCenterID</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">for</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span> <span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;"><</span> <span class="n" style="box-sizing: border-box;">n</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span> <span class="n" style="box-sizing: border-box;">i</span><span class="o" style="box-sizing: border-box; font-weight: bold;">++</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">while</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">t</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">]]</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">==</span> <span class="n" style="box-sizing: border-box;">t</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">]])</span>
<span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">]</span><span class="o" style="box-sizing: border-box; font-weight: bold;">++</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">]</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">></span> <span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">maxPalCenterID</span><span class="p" style="box-sizing: border-box;">])</span>
<span class="n" style="box-sizing: border-box;">maxPalCenterID</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="n" style="box-sizing: border-box;">maxPalSize</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">maxPalCenterID</span><span class="p" style="box-sizing: border-box;">];</span>
<span class="n" style="box-sizing: border-box;">palStartID</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">maxPalCenterID</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="n" style="box-sizing: border-box;">maxPalSize</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">/</span> <span class="mi" style="box-sizing: border-box; color: #009999;">2</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="n" style="box-sizing: border-box;">s</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">substr</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">palStartID</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">maxPalSize</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="p" style="box-sizing: border-box;">}</span></pre>
<h1>
Make it O(N)</h1>
Now that we have a functional algorithm, can it run faster? If yes, what can be done to make it run faster?<br />
<br />
The trick resides in the <b>symmetrical property</b> of palindromes. Consider the following example, where <b>C</b> is the center of a palindrome and <b>L</b> and <b>R</b> are the left and right bounds, respectively.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQrDLH26YLKHCbJ5XEwKxa-25nWr0_0FQqUGPHUzAtbSrBveOacq5s_m9eGYhuxc5PENT62QGHANulchzdQdViontVPZk-GCVmlpOde38goXLzwthyphenhyphen5pU4tWcVCmJulKBGf36RIQPn1i8/s1600/a.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQrDLH26YLKHCbJ5XEwKxa-25nWr0_0FQqUGPHUzAtbSrBveOacq5s_m9eGYhuxc5PENT62QGHANulchzdQdViontVPZk-GCVmlpOde38goXLzwthyphenhyphen5pU4tWcVCmJulKBGf36RIQPn1i8/s400/a.png" /></a><br />
<br />
Now populate <i>P</i> until <b>R</b>, the right bound of the palindrome with center in <b>C</b>.<br />
<br />
Notice the symmetry? If there was a mirror at <b>C</b>, the content of <i>P</i> between <b>L</b> and <b>R</b> would be completely symmetric, just like it actually is! Check it for yourself below.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPmhzPpiACL2ywPq5Nno-tn4hQhQEcwC7dro9t-Fh1xAspF9R_8sll5bWqq3wwlX2Db3AlHHjhkTCEpaEIsk3SUlU9jT34Co2AcFF9bT-Wppo0eQljA8uPq4vfEh3kZ8vYHpFBh5phcL8/s1600/b.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPmhzPpiACL2ywPq5Nno-tn4hQhQEcwC7dro9t-Fh1xAspF9R_8sll5bWqq3wwlX2Db3AlHHjhkTCEpaEIsk3SUlU9jT34Co2AcFF9bT-Wppo0eQljA8uPq4vfEh3kZ8vYHpFBh5phcL8/s400/b.png" /></a><br />
<br />
And now you might be thinking: we can save <b>a lot</b> of computations just by copying the half of a palindrome to the left of the center, mirror it, and pasting to the right! And then keep populating <i>P</i> after <b>R</b> with the same principle!<br />
<br />
Go ahead and try that for yourself. Populate <i>P</i> based on this concept for <b>C = 8</b>. You should get something like this, which is wrong if you look carefully:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQZePpz6Xzl263HzFyNIyqUr-Ne8SasmmZbbthOWKymdH4fVkQxl2g_4FQegmrQwCt7Yx5S4-W-hpmsXzVelmwoZCFHpnBmXEAHb1Hajw1ixWgJg3IIYye-_xxzIqLZ7Ojhm2LBh1vKXA/s1600/c.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQZePpz6Xzl263HzFyNIyqUr-Ne8SasmmZbbthOWKymdH4fVkQxl2g_4FQegmrQwCt7Yx5S4-W-hpmsXzVelmwoZCFHpnBmXEAHb1Hajw1ixWgJg3IIYye-_xxzIqLZ7Ojhm2LBh1vKXA/s400/c.png" /></a><br />
<br />
The right answer would be:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhnJbnuwJUDlOn1x4KxbfxV8jP3gvgw_CEsxT3oqKaBAnOgkDAX1FiLTMe4HCNAiQhkbNXoxUv71ylDivsi3KxW0M7mFRuzL7P1yOd_80YSxbPYY_9pRwQLFQr82aRK6R6bvH4sCUrj-1Y/s1600/d.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhnJbnuwJUDlOn1x4KxbfxV8jP3gvgw_CEsxT3oqKaBAnOgkDAX1FiLTMe4HCNAiQhkbNXoxUv71ylDivsi3KxW0M7mFRuzL7P1yOd_80YSxbPYY_9pRwQLFQr82aRK6R6bvH4sCUrj-1Y/s400/d.png" /></a><br />
<br />
Well, that is still a <b>great conclusion</b>! Although it is <b>not completely correct</b>, you are very close to finding the trick!<br />
<br />
As you may already be thinking, this concept works, but it fails at certain point. What is the limit?<br />
<br />
If you examine a couple of more examples, this limit will be very easy to detect:<br />
When <b><i>P</i>[i']</b> is <b>less or equal</b> to <b>R-i</b>, then <b><i>P</i>[i]</b> is always <b>equal</b> to the <b>minimum</b> of these values: <b>R-i</b> or <b><i>P</i>[i']</b>; Otherwise, the only valid information we have is that <b><i>P</i>[i']</b> is <b>greater or equal</b> to <b><i>P</i>[i]</b>, which is already a <b>very valuable</b> information! If this is the case, we just have to try and <b>expand</b> this palindrome to find <b><i>P</i>[i]</b>.<br />
<br />
The final part that completes the trick is answered by the following question: when should <b>C</b> be updated? Again, if a few examples are examined by hand, it is not hard to find that:<br />
<br />
Whenever a palindrome <b>centered</b> at <b>i</b> expands <b>past the right bound</b> of the palindrome centered at <b>C</b> (in other words, if it expands past <b>R</b>), <b>C</b> is assigned the value of <b>i</b> and <b>R</b> is <b>updated</b> according to the content of <b><i>P</i>[i]</b> and <b>i</b> itself. Does that make sense? I really hope so! :)<br />
<br />
And that is it! This <b>concludes</b> the beautiful Manacher's algorithm: a tool for finding the longest palindromic substring in <b>O(N)</b>.<br />
<br />
Now you just have to add these little extras to the previous code. <b>Do not peek the snippet below</b>. Once again, it is important that you <b>try for yourself</b>. Then you may compare your solution with the <b>pseudo code</b> below.<br />
<pre style="background-color: #f8f8f8; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 1px solid rgb(221, 221, 221); box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13px; line-height: 19px; margin-bottom: 15px; margin-top: 15px; overflow: auto; padding: 6px 10px; word-wrap: normal;"><span class="n" style="box-sizing: border-box;">manacherAlgorithm</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">s</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">t</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">preprocessManacherAlgorithm</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">s</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="n" style="box-sizing: border-box;">n</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">t</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">size</span><span class="p" style="box-sizing: border-box;">();</span>
<span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">n</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">];</span> <span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// array of size n filled with 0</span>
<span class="n" style="box-sizing: border-box;">C</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">R</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">maxPalCenterID</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">for</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span> <span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;"><</span> <span class="n" style="box-sizing: border-box;">n</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span><span class="p" style="box-sizing: border-box;">;</span> <span class="n" style="box-sizing: border-box;">i</span><span class="o" style="box-sizing: border-box; font-weight: bold;">++</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// i' = C - (i-C)</span>
<span class="n" style="box-sizing: border-box;">ii</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="mi" style="box-sizing: border-box; color: #009999;">2</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">*</span> <span class="n" style="box-sizing: border-box;">C</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// save several computations</span>
<span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">]</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">R</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">></span> <span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">?</span> <span class="n" style="box-sizing: border-box;">min</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">R</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">ii</span><span class="p" style="box-sizing: border-box;">])</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">:</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// expand palindrome</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">while</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">t</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">]]</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">==</span> <span class="n" style="box-sizing: border-box;">t</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">]])</span>
<span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">]</span><span class="o" style="box-sizing: border-box; font-weight: bold;">++</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// update index of the center of the biggest palindrome</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">]</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">></span> <span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">maxPalCenterID</span><span class="p" style="box-sizing: border-box;">])</span>
<span class="n" style="box-sizing: border-box;">maxPalCenterID</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// adjust center if palindrome</span>
<span class="c1" style="box-sizing: border-box; color: #999988; font-style: italic;">// centered at i expands past R</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">]</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">></span> <span class="n" style="box-sizing: border-box;">R</span><span class="p" style="box-sizing: border-box;">)</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">C</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="n" style="box-sizing: border-box;">R</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">+</span> <span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">];</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="p" style="box-sizing: border-box;">}</span>
<span class="n" style="box-sizing: border-box;">maxPalSize</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box;">P</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">maxPalCenterID</span><span class="p" style="box-sizing: border-box;">];</span>
<span class="n" style="box-sizing: border-box;">palStartID</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">maxPalCenterID</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="mi" style="box-sizing: border-box; color: #009999;">1</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">-</span> <span class="n" style="box-sizing: border-box;">maxPalSize</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">/</span> <span class="mi" style="box-sizing: border-box; color: #009999;">2</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="n" style="box-sizing: border-box;">s</span><span class="p" style="box-sizing: border-box;">.</span><span class="n" style="box-sizing: border-box;">substr</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">palStartID</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">maxPalSize</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="p" style="box-sizing: border-box;">}</span></pre>
You might be interested in checking <a href="http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html">this tutorial</a> if you did not understand my explanation. I learnt this algorithm from there.Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com4tag:blogger.com,1999:blog-3057039820716415448.post-56793093063804979082014-07-23T12:03:00.001+01:002015-03-01T01:03:20.258+00:00Learning BlenderHello everyone!<br />
<br />
Guess what I've been up to: learning how to use <a href="http://www.blender.org/">Blender</a>.<br />
I must confess I am loving it! When I get to see the final result, I get a pretty similar feeling to the one I get when I finish programming some cool feature on a project.<br />
<br />
I haven't done anything of my own just yet. I'm still just watching some tutorials and repeating what I watch but tweaking some details. I'll leave some screenshots here of what I've done so far and the link to the video tutorial.<br />
<br />
By the way, I find these tutorials GREAT. They are simple, concise and assertive making it easy for anyone to follow up. Congratulations to <a href="https://www.youtube.com/user/tutor4u/featured">tutor4u</a> for making such a nice <a href="https://www.youtube.com/playlist?list=PLzmyR17f55-LVbgnzhS4Xl9zJ3dSCdYW3">playlist of tutorials</a>.<br />
<br />
<h2>
Screenshots</h2>
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgw9R9Bqi_JVssZeBO1I-UtDDsRiljmVuqqc8pevwdbdQGoGxNe0mADLBI4XjmKG176_EkOrXQ3h6whRfis9mwgmwT3GP0W5MzQ5xKEnxd0NNdGvW0Y6aVUaP9LEztvWsd0yQBE1QLszs4/s1600/cloth.png" imageanchor="1">
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgw9R9Bqi_JVssZeBO1I-UtDDsRiljmVuqqc8pevwdbdQGoGxNe0mADLBI4XjmKG176_EkOrXQ3h6whRfis9mwgmwT3GP0W5MzQ5xKEnxd0NNdGvW0Y6aVUaP9LEztvWsd0yQBE1QLszs4/s400/cloth.png" /></a><br />
<a href="https://www.youtube.com/watch?v=2zd1AI198I8">Cloth napkin</a><br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcfZrIl1b4c7_0tPm2QsP-IzQZWEnOgbYYryFzud42lR0Vf9HXl08KTM08PoKMWuSsxriOu54X8icJY1h6Hjr_giBiGXkvnoPbFOzKZWe2boMADIng_KOE6twecEd1_jeFGf6UjBerNB0/s1600/icy-text.png" imageanchor="1">
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcfZrIl1b4c7_0tPm2QsP-IzQZWEnOgbYYryFzud42lR0Vf9HXl08KTM08PoKMWuSsxriOu54X8icJY1h6Hjr_giBiGXkvnoPbFOzKZWe2boMADIng_KOE6twecEd1_jeFGf6UjBerNB0/s400/icy-text.png" /></a><br />
<a href="https://www.youtube.com/watch?v=39-CWSn_kms">Icy text</a><br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwUC6tEy67I_s08uGpTTAqn1S1r5dKf96ja1cTSKccMmTUGXaPOnEZ4EfMsrc-ED50QyPYYR4gD0Spn5oV32IeTBUjTCsx-nv32LEfd5jlumulcDnFF83BcfY_EjCg-ixXqZrWyAUE_bs/s1600/chains.png" imageanchor="1">
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwUC6tEy67I_s08uGpTTAqn1S1r5dKf96ja1cTSKccMmTUGXaPOnEZ4EfMsrc-ED50QyPYYR4gD0Spn5oV32IeTBUjTCsx-nv32LEfd5jlumulcDnFF83BcfY_EjCg-ixXqZrWyAUE_bs/s400/chains.png" /></a><br />
<a href="https://www.youtube.com/watch?v=Ci39zXfG-I4">Realistic chain</a><br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhb1EjkbWl56twR-PCX-dCgKhKe0oS4DUJGfwgQ6UEu871ZIowTgelc0XTStslCGdD6nRU1SzCbVeyDcIIwXH6wpUM5ELfXZiHHiY9G0tE9ib2zIP-dMow3d6vMLfD-vpzzlwhfvUvFhQU/s1600/planet-burst.png" imageanchor="1">
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhb1EjkbWl56twR-PCX-dCgKhKe0oS4DUJGfwgQ6UEu871ZIowTgelc0XTStslCGdD6nRU1SzCbVeyDcIIwXH6wpUM5ELfXZiHHiY9G0tE9ib2zIP-dMow3d6vMLfD-vpzzlwhfvUvFhQU/s400/planet-burst.png" /></a><br />
<a href="https://www.youtube.com/watch?v=GTq9sLQOiVs">Planet bursting into pieces</a><br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhG68Yo49CHpJRlhpI44yWruatntHeOU1L9y1frs3QyRcSLWOMCkMktA1fZYVDUADc5O_L6NpQX-aAlnyzmlyai3Tg2gmUCLBqj_TzTSZDIohfHgiPP68Qv84z2wooKsd79vwvnAbVSTnU/s1600/cup0001.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhG68Yo49CHpJRlhpI44yWruatntHeOU1L9y1frs3QyRcSLWOMCkMktA1fZYVDUADc5O_L6NpQX-aAlnyzmlyai3Tg2gmUCLBqj_TzTSZDIohfHgiPP68Qv84z2wooKsd79vwvnAbVSTnU/s400/cup0001.png" /></a><br />
<br />
<a href="https://www.youtube.com/watch?v=9PJL0eAuZ_E">Wooden cup</a><br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDucLhlPCdtURmbxFODY0EqgyNX07gtUrZeoFPIwBe1cJukhLcLK7evjinHSObcDnkiSsNlS1IEbZ_4ETCGwIrirBsmld5nMtgiyhU_jqb_QcsAORgl276acXZ3CU8BHahQ-mfAUs5E-0/s1600/bear0001.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDucLhlPCdtURmbxFODY0EqgyNX07gtUrZeoFPIwBe1cJukhLcLK7evjinHSObcDnkiSsNlS1IEbZ_4ETCGwIrirBsmld5nMtgiyhU_jqb_QcsAORgl276acXZ3CU8BHahQ-mfAUs5E-0/s400/bear0001.png" /></a><br />
<br />
<a href="https://www.youtube.com/watch?v=LCghBIUZyuM">Stuffed bear</a><br />
<br />
<iframe width="400" height="225" src="//www.youtube.com/embed/ePToaTL7Z8A" frameborder="0" allowfullscreen></iframe><br />
<a href="https://www.youtube.com/watch?v=Q93pE5jB1Uw">Dissolve Animation</a>Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-37489425082531187622014-07-20T23:53:00.000+01:002015-01-13T17:32:27.146+00:00[Minix] Posts index<h1>Minix</h1>
<h2>
Final result preview</h2>
I thought it would be appropriate to have a preview of the final result of the project covered in these tutorial series in it's index, so here it is:<br/>
<br/>
<iframe src="http://gfycat.com/ifr/EnergeticNeglectedDuckling" frameborder="0" scrolling="no" width="540" height="450" style="-webkit-backface-visibility: hidden;-webkit-transform: scale(1);" ></iframe><br/>
<h2>
Table of contents</h2>
<a href="http://difusal.blogspot.pt/2013/12/new-tutorial-series-about-minix-318.html">Introduction</a><br/>
You can actually skip this and head to the first tutorial right away.<br/>
<br/>
<a href="http://difusal.blogspot.pt/2013/12/minixtutorial-1-how-to-install-minix.html">Tutorial 1</a><br/>
How to install Minix and setup VMware Player<br/>
<br/>
<a href="http://difusal.blogspot.pt/2014/07/minixtutorial-2-rse-and-minix-commands.html">Tutorial 2</a><br/>
RSE and Minix commands<br/>
<br/>
<a href="http://difusal.blogspot.pt/2014/08/minixtutorial-3-setting-up-project-with.html">Tutorial 3</a><br/>
Setting up a project with some useful scripts<br/>
<br/>
<a href="http://difusal.blogspot.pt/2014/08/minixtutorial-4-setting-up-project-with.html">Tutorial 4</a><br/>
Adding graphics to a project<br/>
<br/>
<a href="http://difusal.blogspot.pt/2014/08/minixtutorial-5-adding-keyboard-input.html">Tutorial 5</a><br/>
Adding keyboard input to a project<br/>
<br/>
<a href="http://difusal.blogspot.pt/2014/08/minixtutorial-6-adding-timer-to-project.html">Tutorial 6</a><br/>
Adding a timer to a project<br/>
<br/>
<a href="http://difusal.blogspot.pt/2014/09/minixtutorial-7-adding-mouse-to-project.html">Tutorial 7</a><br/>
Adding a mouse to a project<br/>
<br/>
<a href="http://difusal.blogspot.pt/2014/09/minixtutorial-8-loading-bmp-images.html">Tutorial 8</a><br/>
Loading bmp images<br/>
<br/>
<a href="http://difusal.blogspot.pt/2014/09/minixtutorial-9-creating-state-machine.html">Tutorial 9</a><br/>
Creating a state machine and a main menu<br/>
<br/>
<a href="http://difusal.blogspot.pt/2014/09/minixtutorial-10-creating-game-state.html">Tutorial 10</a><br/>
Creating the game state, adding a moving background and ground<br/>
<br/>
<a href="http://difusal.blogspot.pt/2014/09/minixtutorial-11-adding-flappy-and.html">Tutorial 11</a><br/>
Adding flappy and the mario pipes<br/>
<br/>
<h2>
Main menu screenshot</h2>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOzN6auIi-aMzN05iDJOMHsAD47Dv-CLV3XGgq40YOTuShP1TYhjh3bufcV-vASJ-R6p7GBiYnwkOmWrqSXdjSq0KsK2M_IsQtbFGjL3S2jkPYrYRc71g5T0krpsFdfISCQsiY4jnUv2Y/s1600/Screenshot+from+2014-09-08+23:37:40.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOzN6auIi-aMzN05iDJOMHsAD47Dv-CLV3XGgq40YOTuShP1TYhjh3bufcV-vASJ-R6p7GBiYnwkOmWrqSXdjSq0KsK2M_IsQtbFGjL3S2jkPYrYRc71g5T0krpsFdfISCQsiY4jnUv2Y/s400/Screenshot+from+2014-09-08+23:37:40.png" /></a>Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com2tag:blogger.com,1999:blog-3057039820716415448.post-52871845209375785632014-07-20T23:39:00.000+01:002015-10-25T19:34:17.953+00:00[Minix][Tutorial 2] RSE and Minix commandsIn this tutorial I will teach you how to <b>link Eclipse with Minix through RSE</b> and the infamous <b>Hello world</b> program.<br />
<br />
RSE, also known as Remote System Explorer, is a plug-in for eclipse which will be essential to develop Minix applications.<br />
<br />
First of all, open Eclipse and boot Minix.<br />
Log in minix. If you did not catch this in the previous tutorial, both the <u>username</u> and <u>password</u> are <b>lcom</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgojFSWrWtUl3iZMhK3jzqNZrqIwcfwBrB19_tadsLMuWnRkWK3JDWTuRE132u3nefXMyt0c0AkHzruOSuo7mO_4skAOUS_dVBUNBP-nsFeBqrpx2k4YtoT4n0_1OzEoX4vsHrTweukuJo/s1600/a1.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgojFSWrWtUl3iZMhK3jzqNZrqIwcfwBrB19_tadsLMuWnRkWK3JDWTuRE132u3nefXMyt0c0AkHzruOSuo7mO_4skAOUS_dVBUNBP-nsFeBqrpx2k4YtoT4n0_1OzEoX4vsHrTweukuJo/s400/a1.png" /></a><br />
<br />
In eclipse, open the Remote System Explorer perspective.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjz-KI2z4MDeHZ3Iwppmv7JLec7g7T_MmId4zEGhh4SHPCHJV2qNfZP9R_EuhpqnDWvooqLqEKumV-zg18XHvWPeXeOej2Aa5FVUQ3AwthI-0d-C8GCpo3_5Z45Mu9hoRk35ysJR52ZetY/s1600/Screenshot+from+2014-07-20+21:10:01.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjz-KI2z4MDeHZ3Iwppmv7JLec7g7T_MmId4zEGhh4SHPCHJV2qNfZP9R_EuhpqnDWvooqLqEKumV-zg18XHvWPeXeOej2Aa5FVUQ3AwthI-0d-C8GCpo3_5Z45Mu9hoRk35ysJR52ZetY/s400/Screenshot+from+2014-07-20+21:10:01.png" /></a>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3NsrCfgMSDAImA4htaKHPfO1FiAEaYRSzd6a3XXPXcSVmjppWe-xp5KE3HCJ3SOyHtf5DbE_wqnm_QbrDl3cVgT6g4kJkXOzA_QuU_h-t_zZ3-7rOVC8jd5160eKWU72xaz6ScRn5AFY/s1600/Screenshot+from+2014-07-20+21:32:39.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3NsrCfgMSDAImA4htaKHPfO1FiAEaYRSzd6a3XXPXcSVmjppWe-xp5KE3HCJ3SOyHtf5DbE_wqnm_QbrDl3cVgT6g4kJkXOzA_QuU_h-t_zZ3-7rOVC8jd5160eKWU72xaz6ScRn5AFY/s400/Screenshot+from+2014-07-20+21:32:39.png" /></a><br />
<br />
If RSE is not on the list, you will need to install it:<br />
<ul>
<li>go to <b>Help > Install New Software</b>;</li>
<li>paste the <b>url</b> of the repository of your eclipse version. In my case it is:
<blockquote>
http://download.eclipse.org/releases/luna/</blockquote>
</li>
<li>type "remote" in the <b>filter</b></li>
<li>select <b>Remote System Explorer End-User Runtime</b></li>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjg5qzOw510RPmD1AOA1YTLimF0V0FNC1GxDk9JU0YighJCXoa3Kb43K_Zk1Sq5D4azWOP1SUnRMRE5iTcJjVaCHDtDSHOltl1rXLtQ2zp5BxZduAOcSueZJz75BjBHtX2uEkDT8ampe40/s1600/Screenshot+from+2014-07-20+21:18:36.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjg5qzOw510RPmD1AOA1YTLimF0V0FNC1GxDk9JU0YighJCXoa3Kb43K_Zk1Sq5D4azWOP1SUnRMRE5iTcJjVaCHDtDSHOltl1rXLtQ2zp5BxZduAOcSueZJz75BjBHtX2uEkDT8ampe40/s400/Screenshot+from+2014-07-20+21:18:36.png" /></a>
<li>click <b>next</b>, <b>I agree</b> and <b>finish</b></li>
</ul>
<br />
We now have the RSE perspective up and running, but we still have to configure the connection to Minix.<br />
<br />
Click the button to <b>add a new connection</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdHqLx9HdsPlQIT1JybMC13RRtBsBbFQLu3OswO1htRAYlImRfRuk_lvVa8o-XD9bns6o7jVjLVJ7Ow4v3gXo_P1bDtNDaUN6ZtUNRqANklT96U7Xas_ehWCu-oo-w_d9GqBWw5TRt34c/s1600/Screenshot+from+2015-10-25+18-52-15.png" imageanchor="1" >
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdHqLx9HdsPlQIT1JybMC13RRtBsBbFQLu3OswO1htRAYlImRfRuk_lvVa8o-XD9bns6o7jVjLVJ7Ow4v3gXo_P1bDtNDaUN6ZtUNRqANklT96U7Xas_ehWCu-oo-w_d9GqBWw5TRt34c/s320/Screenshot+from+2015-10-25+18-52-15.png" /></a>
<br />
<br />
Select <b>SSH Only</b> and press <b>Next</b>.<br />
Name the connection <b>minix</b> and press <b>Finish</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcv42EgILDcag8ukuDO0_K_yvkpNdbxbfrRN0H3yjmvGKB1mTCR7q2G7KkE2nm2zcuPlgxU_YPJ4z9_yUDcA-x1NVQMmydkBIshMhCFHn2pHuR_1Mq1nMJKj1GkbiUlPU1bxFH1g7i5lc/s1600/Screenshot+from+2015-10-25+19-27-23.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcv42EgILDcag8ukuDO0_K_yvkpNdbxbfrRN0H3yjmvGKB1mTCR7q2G7KkE2nm2zcuPlgxU_YPJ4z9_yUDcA-x1NVQMmydkBIshMhCFHn2pHuR_1Mq1nMJKj1GkbiUlPU1bxFH1g7i5lc/s320/Screenshot+from+2015-10-25+19-27-23.png" /></a><br />
<br />
The minix connection should now appear on the list of Remote Systems:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEin4O61c8CcnQTLD3nkVIQcC9HFCrZH7_Ts97HDAjblWFAFnDLebF-Ixv1U5YQgcRikKRdBCIndiNMonCeEAuROBfSqIC4FkIkmCEMBSV96VNInSpdgsZrMi8rgMO03xC4fFQfSfE7i5C8/s1600/Screenshot+from+2014-07-20+21:58:44.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEin4O61c8CcnQTLD3nkVIQcC9HFCrZH7_Ts97HDAjblWFAFnDLebF-Ixv1U5YQgcRikKRdBCIndiNMonCeEAuROBfSqIC4FkIkmCEMBSV96VNInSpdgsZrMi8rgMO03xC4fFQfSfE7i5C8/s400/Screenshot+from+2014-07-20+21:58:44.png" /></a><br />
<br />
<b>Right-click</b> <i>Sftp Files</i> and choose <b>Properties</b>.<br>
Under <b>Subsystem</b>, change the port to <b>2222</b> and save by pressing <b>OK</b>.<br>
<br>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIVwjM54pR7Lm0kHcjSbUtrg7WqS_4uakR_ugUWHKJdp88LrRm1VODqVK9C9YB5k7vsfIY32R2-o91ODNsa3uTIoVrmTcGeHGIVuc_cI8uhSv72Vc5d3HLdxuSjYUnoULm3SI0q0S9S3g/s1600/Screenshot+from+2015-10-25+19-32-32.png" imageanchor="1" >
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIVwjM54pR7Lm0kHcjSbUtrg7WqS_4uakR_ugUWHKJdp88LrRm1VODqVK9C9YB5k7vsfIY32R2-o91ODNsa3uTIoVrmTcGeHGIVuc_cI8uhSv72Vc5d3HLdxuSjYUnoULm3SI0q0S9S3g/s320/Screenshot+from+2015-10-25+19-32-32.png" /></a><br>
<br />
If you try to open <b>My Home</b>, you will be prompted to enter a password. Again, use <u>lcom</u> for both <b>User ID</b> and <b>Password</b> and press <b>OK</b>:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHu6zqwl4iNu1dUH180TpsSRZ3aKBGcL9LnJYRUfC_YDjRGintgRosJOpNfaJ8vVOfjqpmBUWh4c_hPvsoRH1VzdgEOHoAZ0Big83j-OGzy0kgh_Z_ehz5mgO2FdYz0etH6gxZ7ubH6fc/s1600/Screenshot+from+2014-07-20+22:01:40.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHu6zqwl4iNu1dUH180TpsSRZ3aKBGcL9LnJYRUfC_YDjRGintgRosJOpNfaJ8vVOfjqpmBUWh4c_hPvsoRH1VzdgEOHoAZ0Big83j-OGzy0kgh_Z_ehz5mgO2FdYz0etH6gxZ7ubH6fc/s400/Screenshot+from+2014-07-20+22:01:40.png" /></a><br />
<br />
Since it is the first time we are opening the connection, some messages will pop up...<br />
Just press <b>Yes</b>, <b>Yes</b>, <b>Yes</b> and <b>Ok</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiv4sPhYihwFBCd1ZC3k-w77hnETOiZQ7X8MNhyFNVG7-FoclmMvh4LH8rsuZ6MsPYK0I5kMiub7b61deKrn_0qyre8DOjYDgx6ZZvEX30t9fVlsS2zp2OK_ftpdmO08jWxxqBZql1IKA4/s1600/Screenshot+from+2014-07-20+22:02:07.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiv4sPhYihwFBCd1ZC3k-w77hnETOiZQ7X8MNhyFNVG7-FoclmMvh4LH8rsuZ6MsPYK0I5kMiub7b61deKrn_0qyre8DOjYDgx6ZZvEX30t9fVlsS2zp2OK_ftpdmO08jWxxqBZql1IKA4/s400/Screenshot+from+2014-07-20+22:02:07.png" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifv1w1MGrsz7ljBd4oogVUTN9jHbo_H_NtEaMsSofN61SbPoNgD1dAHZ_O1Gylws61_ar7JFsc2wanqdu7_nthha70bZA2KXN5clNFsHlo4f9hHHDfbl59bcX8lLUPNvhAQsx3n0Oov4Q/s1600/Screenshot+from+2014-07-20+22:02:16.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifv1w1MGrsz7ljBd4oogVUTN9jHbo_H_NtEaMsSofN61SbPoNgD1dAHZ_O1Gylws61_ar7JFsc2wanqdu7_nthha70bZA2KXN5clNFsHlo4f9hHHDfbl59bcX8lLUPNvhAQsx3n0Oov4Q/s400/Screenshot+from+2014-07-20+22:02:16.png" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiaFuFvU_3H3uHauZb-SO1VPi2kIW1d7XFWwckh2TTVRVHZ7Baqu37s-K3qJPxQ_K2wmGaDadQ5lBl5Ku8qADbtM9ehBzMLmwQI_FxRj1oh1UGKWt-N8gNwPKAp3znYKqeetv1i5JVdyEU/s1600/Screenshot+from+2014-07-20+22:02:24.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiaFuFvU_3H3uHauZb-SO1VPi2kIW1d7XFWwckh2TTVRVHZ7Baqu37s-K3qJPxQ_K2wmGaDadQ5lBl5Ku8qADbtM9ehBzMLmwQI_FxRj1oh1UGKWt-N8gNwPKAp3znYKqeetv1i5JVdyEU/s400/Screenshot+from+2014-07-20+22:02:24.png" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEJWkt7BMi5O1Su9MXitrOFr_E-5QfkDOefpQZDCsK9VeB4degdNTFiginANemVPfFDYvHAehxsPSrG6uqZOFXske3imiV5VTdj4JL28CGRBgv7GC5Nxn0WQfoq_T3nV_fz_JcH-oi204/s1600/Screenshot+from+2014-07-20+22:02:31.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEJWkt7BMi5O1Su9MXitrOFr_E-5QfkDOefpQZDCsK9VeB4degdNTFiginANemVPfFDYvHAehxsPSrG6uqZOFXske3imiV5VTdj4JL28CGRBgv7GC5Nxn0WQfoq_T3nV_fz_JcH-oi204/s400/Screenshot+from+2014-07-20+22:02:31.png" /></a><br />
<br />
Now that we have everything set, let's create our first program via RSE!<br />
Right-click <b>My Home > New > Folder</b> and name it <b>hello-world</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggVZPkzj5tStZeUNWBLf9YNGB7qynJOVHB4ooHHlc7PGL852EgvaKFgLN7NL6SaxH689tA88TpQxn4Xf3PfX8OYI1ODaeOg6jH87b3shzgwEFvT1g9U3LGOH-ahGYRTV6ICfHuExHdFuE/s1600/Screenshot+from+2014-07-20+22:11:01.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggVZPkzj5tStZeUNWBLf9YNGB7qynJOVHB4ooHHlc7PGL852EgvaKFgLN7NL6SaxH689tA88TpQxn4Xf3PfX8OYI1ODaeOg6jH87b3shzgwEFvT1g9U3LGOH-ahGYRTV6ICfHuExHdFuE/s400/Screenshot+from+2014-07-20+22:11:01.png" /></a><br />
<br />
Right-click <b>hello-world > New > File</b> and name it <b>main.c</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyJ5ZEmchrxcM1qUA6N0z_yDtGGSLy5c2KE27RkOR0G92Dg2ysuy4wzr6OvyAurEAPetBdhTW7uSM8-r8epNoOLB48XfT7woilEu0hHq3S2C7FJnVcPMJtLUe3PQANSQP56XjakGCxdHU/s1600/Screenshot+from+2014-07-20+22:16:47.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyJ5ZEmchrxcM1qUA6N0z_yDtGGSLy5c2KE27RkOR0G92Dg2ysuy4wzr6OvyAurEAPetBdhTW7uSM8-r8epNoOLB48XfT7woilEu0hHq3S2C7FJnVcPMJtLUe3PQANSQP56XjakGCxdHU/s400/Screenshot+from+2014-07-20+22:16:47.png" /></a><br />
<br />
You should end up with:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibeycTR9J0iKMbuPYvarTZhibGpj72tO5I9msd8BfoWwLNXm31751V5TlKVuCdMNd0VfaxietfTqcq4NT4iEkrhDpuxiSdsmhRI1t-9P0n2wf1VGILBGy1zDxDMwUKk9nvjXpR2jJhDM8/s1600/Screenshot+from+2014-07-20+22:17:40.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibeycTR9J0iKMbuPYvarTZhibGpj72tO5I9msd8BfoWwLNXm31751V5TlKVuCdMNd0VfaxietfTqcq4NT4iEkrhDpuxiSdsmhRI1t-9P0n2wf1VGILBGy1zDxDMwUKk9nvjXpR2jJhDM8/s400/Screenshot+from+2014-07-20+22:17:40.png" /></a><br />
<br />
So, what we've done so far was to create a folder and a file inside it using RSE. Let's switch to VMware and see if the folder is really there.<br />
<br />
Right after logging in, type <b>ls</b> and press <b>Enter</b>. This command <u>lists the content of the current folder</u> we're in. If you did everything correctly so far, there should be a folder called <b>hello-world</b>!<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiDXnictu5pFRVec2bShcQ80n2811G3E-wK6DqoYyg6hnu_L9l8POIcV8bFINw5GqRJOF3YuiNB7ZlpLG3z22KNIDEzKuPJLn6hB_suZmV6WLDlqfdDETOqpd-oLgGZMQzj1L996eRWpo/s1600/Screenshot+from+2014-07-20+22:27:06.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiDXnictu5pFRVec2bShcQ80n2811G3E-wK6DqoYyg6hnu_L9l8POIcV8bFINw5GqRJOF3YuiNB7ZlpLG3z22KNIDEzKuPJLn6hB_suZmV6WLDlqfdDETOqpd-oLgGZMQzj1L996eRWpo/s400/Screenshot+from+2014-07-20+22:27:06.png" /></a><br />
<br />
Now, let's navigate to hello-world and check it's content.<br />
To navigate to another folder, use the command <b>cd <i>destiny-folder</i></b>. So in our case, just type: <b>cd hello-world</b>. If you now list the current folder's content using <b>ls</b>, the result should look like this:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdVSNqSB5dg5J6iRjKit-9NTZQF3ccmzkjnGusZj_GjVCtxVpR_W8ztAG29ELmdin8-WY-PKNNPy5rwk4fZYnq0R4l1vJmktMhFTAcAr5Ss0zF8dhlcBVtaBvFVEPsExsAqfOM_f4G-UI/s1600/Screenshot+from+2014-07-20+22:33:08.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdVSNqSB5dg5J6iRjKit-9NTZQF3ccmzkjnGusZj_GjVCtxVpR_W8ztAG29ELmdin8-WY-PKNNPy5rwk4fZYnq0R4l1vJmktMhFTAcAr5Ss0zF8dhlcBVtaBvFVEPsExsAqfOM_f4G-UI/s400/Screenshot+from+2014-07-20+22:33:08.png" /></a><br />
<br />
I really hope you are getting the hang of this and hopefully everything is making sense.<br />
<br />
Let me teach you a little trick that is REALLY useful.<br />
First, navigate to the folder that contains hello-world, in other words, <b>go up</b> in the folders tree using the command "<b>cd ..</b>".<br />
Just to make sure we're where we wanted, type <b>ls</b>. The result should be:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYXwmSTEuaxuGlakF_Yu287sDLUMsf-N9oj2vpt1g_Se-wvDYqHorvwkstP9YEP2K42elCDq0NTts_noujcMpqO0k3egj5jHlP5O1W0CW-rbP1ARFZ6txsnYWFtcuYKFUvSWGRvALf98Y/s1600/Screenshot+from+2014-07-20+22:39:15.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYXwmSTEuaxuGlakF_Yu287sDLUMsf-N9oj2vpt1g_Se-wvDYqHorvwkstP9YEP2K42elCDq0NTts_noujcMpqO0k3egj5jHlP5O1W0CW-rbP1ARFZ6txsnYWFtcuYKFUvSWGRvALf98Y/s400/Screenshot+from+2014-07-20+22:39:15.png" /></a><br />
<br />
The trick I wanted to show you is the <b>auto-completion</b>. Trust me, it is VERY useful.<br />
I want you to navigate to the hello-world folder again, remember the command? Yes, it is <b>cd hello-world</b>. BUT DON'T TYPE IT JUST YET! I want you to type <b>cd h</b> and afterwards to <b>press Tab</b> on your keyboard. Hopefully, after pressing tab, the command <b>auto-completed</b> to <b>ch hello-world/</b>.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9KRXlAmRidHeV4YURBXHSqx8tifuM3Hygw-8kApA2SNJ2cQj5tfj0GeFCG9RODDe9LaXztrrUTYBC4WD_4hbh0YDBON4xLcRL88AbYHCC91B9U-i8NYs-utlIaVdMkgD_NSx4qzZrjA0/s1600/Screenshot+from+2014-07-20+22:51:41.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9KRXlAmRidHeV4YURBXHSqx8tifuM3Hygw-8kApA2SNJ2cQj5tfj0GeFCG9RODDe9LaXztrrUTYBC4WD_4hbh0YDBON4xLcRL88AbYHCC91B9U-i8NYs-utlIaVdMkgD_NSx4qzZrjA0/s400/Screenshot+from+2014-07-20+22:51:41.png" /></a><br />
<br />
And that's it, try not to forget this and use it whenever you can, since it makes browsing through folders <u>blazing fast</u> and you will <u>never misspell a folder</u> again.<br />
The auto-complete also works with files. You must notice though that when you have more than one folder starting with the same letter, the auto-complete will not work and you must provide an additional letter until the prefix on the command only matches to one folder.<br />
<br />
Time to switch to Eclipse.<br />
<br />
Open <b>main.c</b> and write a simple hello world program:<br />
<pre style="background-color: #f8f8f8; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 1px solid rgb(221, 221, 221); box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13px; line-height: 19px; margin-bottom: 15px; margin-top: 15px; overflow: auto; padding: 6px 10px; word-wrap: normal;"><span class="cp" style="box-sizing: border-box; color: #999999; font-weight: bold;">#include <stdio.h></span>
<span class="kt" style="box-sizing: border-box; color: #445588; font-weight: bold;">int</span> <span class="nf" style="box-sizing: border-box; color: #990000; font-weight: bold;">main</span><span class="p" style="box-sizing: border-box;">()</span> <span class="p" style="box-sizing: border-box;">{</span>
<span class="n" style="box-sizing: border-box;">printf</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #dd1144;">"Hello cruel world!</span><span class="se" style="box-sizing: border-box; color: #dd1144;">\n</span><span class="s" style="box-sizing: border-box; color: #dd1144;">"</span><span class="p" style="box-sizing: border-box;">);</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="mi" style="box-sizing: border-box; color: #009999;">0</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="p" style="box-sizing: border-box;">}</span></pre>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwyI1dY8Yl48TxgkJVbX3VUMG4gkjwhpkH7qvCCxNfxnqq4LrHrh8BIIrocCnbvnnQHwlM0tFrA0siybNcnaGJBKQYfpn9qVXVCg9LyQpRsmaukL_k5tK2631lG-v1JalqoXbjd-8A7fE/s1600/Screenshot+from+2014-07-20+23:02:58.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwyI1dY8Yl48TxgkJVbX3VUMG4gkjwhpkH7qvCCxNfxnqq4LrHrh8BIIrocCnbvnnQHwlM0tFrA0siybNcnaGJBKQYfpn9qVXVCg9LyQpRsmaukL_k5tK2631lG-v1JalqoXbjd-8A7fE/s400/Screenshot+from+2014-07-20+23:02:58.png" /></a><br />
<br/>
<b>Save</b> main.c and switch to Minix.<br/>
<br/>
You can type the command <b>clear</b> to clear the minix console.<br/>
You should still be <b>inside hello-world</b> folder, so the <b>output</b> of <b>ls</b> should currently only be <b>main.c</b>.<br/>
<br/>
We now want to <b>compile</b> main.c to create an executable file.<br/>
The command goes like: <b>gcc -Wall main.c -o hello</b><br/>
<br/>
The <b>output</b> of <b>ls</b> should now consist of <b>two files</b>: <i>main.c</i> and <i>hello</i>, the latter being the result of the compilation of <i>main.c</i>.<br/>
Well, what are we waiting for?! Let's run it! Type: <b>./hello</b> and the result should look like this:<br/>
<br/>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgR5iJvAZtzl7SwQMGtCQAZkDtjNAcrDgOq9uyUddxrqFVQWJlgHNgJljqk2gF9ILhRmHookhRZuoof8QSyQEzpaVczz_AKjDVF-kHOArTFLdTjcbsVmzIC1sjZ2GQ27fIcOsdURvQd2No/s1600/Screenshot+from+2014-07-20+23:12:27.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgR5iJvAZtzl7SwQMGtCQAZkDtjNAcrDgOq9uyUddxrqFVQWJlgHNgJljqk2gF9ILhRmHookhRZuoof8QSyQEzpaVczz_AKjDVF-kHOArTFLdTjcbsVmzIC1sjZ2GQ27fIcOsdURvQd2No/s400/Screenshot+from+2014-07-20+23:12:27.png" /></a><br/>
<br/>
And there is the expected output: <b>Hello cruel world!</b><br/>
<br/>
I guess this is enough for one tutorial!<br/>
Let's just turn off the virtual machine and review this tutorial's commands.<br/>
<br/>
To turn off minix, type <b>shutdown</b>. Wait for it to work, and then type <b>off</b>.<br/>
<br/>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDAr6-EXNE4PsoTW5T3UNHa4_c8kH3vjE6JMRnfchIJsVWso0jh3wn3aj8m6b8Kxn3l3ke9gcOeFFxDDuUgbw49T8JF8rB6Ar2VoLsMmaxj2L34-JB_mZNXb3vG_xHG-cygBTVbuNboc4/s1600/Screenshot+from+2014-07-20+23:21:09.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDAr6-EXNE4PsoTW5T3UNHa4_c8kH3vjE6JMRnfchIJsVWso0jh3wn3aj8m6b8Kxn3l3ke9gcOeFFxDDuUgbw49T8JF8rB6Ar2VoLsMmaxj2L34-JB_mZNXb3vG_xHG-cygBTVbuNboc4/s400/Screenshot+from+2014-07-20+23:21:09.png" /></a><br/>
<br/>
Minix should have turned off as well as VMware.<br/>
<br/>
<h2>Commands used on this tutorial:</h2>
<b>ls</b><br/>
lists the content of the current folder<br/>
<br/>
<b>cd <i>destiny-folder</i></b><br/>
navigates to the specified folder<br/>
<br/>
<b>cd ..</b><br/>
navigates up (in other words, to the folder containing the current folder)<br/>
<br/>
<b>clear</b><br/>
clears the console<br/>
<br/>
<b>gcc -Wall file.c -o result</b><br/>
compiles <i>file.c</i> to an executable named <i>result</i><br/>
<i>-Wall</i> is a <b>compile flag</b> that activates the output of all the existing warnings on our program during compilation. For <b>LCOM</b> evaluation purposes, this flag must be active and no errors/warnings are admitted on the compilation output.<br/>
<br/>
<b>./<i>executable-file</i></b><br/>
runs <i>executable-file</i><br/>
<br/>
<b>shutdown</b><br/>
turns off minix<br/>
<br/>
<b>off</b><br/>
completely turns off minix and closes VMware<br/>
<br />
<h1>
Back to index</h1>
Click <a href="http://difusal.blogspot.pt/2014/07/minix-posts-index.html">here</a> to go back to the index post.Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-17808623473267431262014-07-20T18:26:00.000+01:002014-07-20T18:26:18.754+01:00Script to find repositories with unstaged/uncommited changesI found this nice script that I often use to check if I did not forget to push the latest changes to git.<br />
<br />
Here are some instructions on how to use it:<br />
<ol>
<li>Save the snippet below to a file and name it something like <b>multipleStatus.sh</b>;</li>
<li>Place the file on the <b>same folder</b> of your repositories;</li>
<li>To run the script, browse to that folder and type:
<blockquote>
<b>source multipleStatus.sh; find-dirty</b></blockquote>
</li>
</ol>
<br />
And here is the snippet you need to save:<br />
<pre style="background-color: #f8f8f8; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 1px solid rgb(221, 221, 221); box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13px; line-height: 19px; margin-bottom: 15px; margin-top: 15px; overflow: auto; padding: 6px 10px; word-wrap: normal;"><span class="c" style="box-sizing: border-box; color: #999988; font-style: italic;">#!/bin/bash</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">function</span> unstaged_changes<span class="o" style="box-sizing: border-box; font-weight: bold;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">{</span>
<span class="nv" style="box-sizing: border-box; color: teal;">worktree</span><span class="o" style="box-sizing: border-box; font-weight: bold;">=</span><span class="k" style="box-sizing: border-box; font-weight: bold;">${</span><span class="nv" style="box-sizing: border-box; color: teal;">1</span><span class="p" style="box-sizing: border-box;">%/*</span><span class="k" style="box-sizing: border-box; font-weight: bold;">}</span><span class="p" style="box-sizing: border-box;">;</span>
git --git-dir<span class="o" style="box-sizing: border-box; font-weight: bold;">=</span><span class="s2" style="box-sizing: border-box; color: #dd1144;">"$1"</span> --work-tree<span class="o" style="box-sizing: border-box; font-weight: bold;">=</span><span class="s2" style="box-sizing: border-box; color: #dd1144;">"$worktree"</span> diff-files --quiet --ignore-submodules --
<span class="o" style="box-sizing: border-box; font-weight: bold;">}</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">function</span> uncommited_changes<span class="o" style="box-sizing: border-box; font-weight: bold;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">{</span>
<span class="nv" style="box-sizing: border-box; color: teal;">worktree</span><span class="o" style="box-sizing: border-box; font-weight: bold;">=</span><span class="k" style="box-sizing: border-box; font-weight: bold;">${</span><span class="nv" style="box-sizing: border-box; color: teal;">1</span><span class="p" style="box-sizing: border-box;">%/*</span><span class="k" style="box-sizing: border-box; font-weight: bold;">}</span><span class="p" style="box-sizing: border-box;">;</span>
git --git-dir<span class="o" style="box-sizing: border-box; font-weight: bold;">=</span><span class="s2" style="box-sizing: border-box; color: #dd1144;">"$1"</span> --work-tree<span class="o" style="box-sizing: border-box; font-weight: bold;">=</span><span class="s2" style="box-sizing: border-box; color: #dd1144;">"$worktree"</span> diff-index --cached --quiet HEAD --ignore-submodules --
<span class="o" style="box-sizing: border-box; font-weight: bold;">}</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">function</span> find-dirty <span class="o" style="box-sizing: border-box; font-weight: bold;">()</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">{</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">for</span> gitdir in <span class="sb" style="box-sizing: border-box; color: #dd1144;">`</span>find . -name .git<span class="sb" style="box-sizing: border-box; color: #dd1144;">`</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">do</span>
<span class="nv" style="box-sizing: border-box; color: teal;">worktree</span><span class="o" style="box-sizing: border-box; font-weight: bold;">=</span><span class="k" style="box-sizing: border-box; font-weight: bold;">${</span><span class="nv" style="box-sizing: border-box; color: teal;">gitdir</span><span class="p" style="box-sizing: border-box;">%/*</span><span class="k" style="box-sizing: border-box; font-weight: bold;">}</span><span class="p" style="box-sizing: border-box;">;</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> ! unstaged_changes <span class="nv" style="box-sizing: border-box; color: teal;">$gitdir</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">then</span>
<span class="nb" style="box-sizing: border-box; color: #0086b3;">echo</span> <span class="s2" style="box-sizing: border-box; color: #dd1144;">"unstaged $gitdir"</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">fi</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">if</span> ! uncommited_changes <span class="nv" style="box-sizing: border-box; color: teal;">$gitdir</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">then</span>
<span class="nb" style="box-sizing: border-box; color: #0086b3;">echo</span> <span class="s2" style="box-sizing: border-box; color: #dd1144;">"uncommitted $gitdir"</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">fi</span>
<span class="k" style="box-sizing: border-box; font-weight: bold;">done</span>
<span class="o" style="box-sizing: border-box; font-weight: bold;">}</span></pre>
<br />Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-83798280011851238792014-07-20T17:24:00.001+01:002014-08-31T14:07:57.965+01:00How to install Eclipse Luna (4.4) on Ubuntu<h2>Installing Eclipse</h2>
<ol>
<li>Download Eclipse from <a href="https://www.eclipse.org/downloads/packages/eclipse-standard-44/lunar">here</a>;</li>
<li>Extract it. You will end up having a folder named eclipse, <u>copy</u> it;<br/>
<br/>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgL9Ucn8vxQuNiS_YGVmNRdWqgto80rVtuWb8f99GWeKClexGEyrMSTw7O6IGO5rew-PowLNenO9va9MU54qREepk6qf1ZgMZdh4C5bBtZE19647PspH_rmxoiD1QDt1lo0YUU025xrCMg/s1600/a1.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgL9Ucn8vxQuNiS_YGVmNRdWqgto80rVtuWb8f99GWeKClexGEyrMSTw7O6IGO5rew-PowLNenO9va9MU54qREepk6qf1ZgMZdh4C5bBtZE19647PspH_rmxoiD1QDt1lo0YUU025xrCMg/s400/a1.png" /></a><br/>
<br/>
</li>
<li>Press <b>CTRL + ALT + T</b> to open Terminal;</li>
<li>Type the command:
<blockquote>sudo nautilus</blockquote>
and then insert your password;<br/>
<br/>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilJdJOEJUWqRf-0-RSqXQbtyxIETy-7dac83Z3TFWgGt4vZAYw-pT2toEly0CD4pJbWD_f5O5KZcN8OuzUsWktBWiWCix7Nl3zKwpLbo3t0nlMGN9_hiYPbanQ1x13zXDuKHWktKT_OCA/s1600/a2.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilJdJOEJUWqRf-0-RSqXQbtyxIETy-7dac83Z3TFWgGt4vZAYw-pT2toEly0CD4pJbWD_f5O5KZcN8OuzUsWktBWiWCix7Nl3zKwpLbo3t0nlMGN9_hiYPbanQ1x13zXDuKHWktKT_OCA/s400/a2.png" /></a><br/>
<br/>
</li>
<li>
Browse to <b>/opt/</b> and <u>paste</u> the extracted folder there.<br/>
<br/>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_ImYDUYlUD6wWlh31X3K1p5Nt3M0fU_L__ob1MKLR1DgzJgulgntSn5he2t-l_UmGoPOFKqntm4G1K4vaQhtNbiPQQc5HSZzXOpI-xbEdRaKz2xPnmHVCQZ5HZN9BKw5BSzy9JSRTI-I/s1600/a3.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_ImYDUYlUD6wWlh31X3K1p5Nt3M0fU_L__ob1MKLR1DgzJgulgntSn5he2t-l_UmGoPOFKqntm4G1K4vaQhtNbiPQQc5HSZzXOpI-xbEdRaKz2xPnmHVCQZ5HZN9BKw5BSzy9JSRTI-I/s400/a3.png" /></a>
</li>
</ol>
<br/>
<h2>Creating a shortcut</h2>
<ol>
<li>Press <b>CTRL + ALT + T</b> to open Terminal;</li>
<li>Type:
<blockquote>
sudo ln -s /opt/eclipse/eclipse /usr/bin/eclipse
</blockquote>
<blockquote>
sudo gedit /usr/share/applications/eclipse.desktop
</blockquote>
</li>
<li>After these commands, <b>gedit</b> (ubuntu's default text editor) should have opened. Copy this and paste it there:
<blockquote>
[Desktop Entry]<br />
Name=Eclipse<br />
Type=Application<br />
Exec=/opt/eclipse/eclipse<br />
Terminal=false<br />
Icon=/opt/eclipse/icon.xpm<br />
Comment=Integrated Development Environment<br />
NoDisplay=false<br />
Categories=Development;IDE;<br />
Name[en]=Eclipse<br />
X-Desktop-File-Install-Version=0.22
</blockquote>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFHVlU8CMn7Om0S-oOnfm09PeS1ebEH-AfCb-nZoPhAUUEvno9XCoHHk1wnmTvhfrtAabFhYgKukXUFAsSgqmTfKv2dl61eaWuS6z_gxHZc2FCM0gqC1QF5kZwSl1ut3zgb0RiSUFySqE/s1600/a5.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFHVlU8CMn7Om0S-oOnfm09PeS1ebEH-AfCb-nZoPhAUUEvno9XCoHHk1wnmTvhfrtAabFhYgKukXUFAsSgqmTfKv2dl61eaWuS6z_gxHZc2FCM0gqC1QF5kZwSl1ut3zgb0RiSUFySqE/s400/a5.png" /></a><br/>
<br/>
</li>
<li><u>Save</u> the text file and <u>close</u> <b>gedit</b>;</li>
<li>This should have successfully created a shortcut. <u>You may have to log off</u> and back in for it to show up.<br/>
<br/>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjj-IB9xsoo9oHtz4iLK0dR9FXuSbyo8-PYLeznLvR8_uZFSUJFtdI2AgHRu0_Iv15Om0FhWGTJ9bGYQfIXVlfn-CLEaE4wNSD5QOjz_Sr3u2A4zxXBgmWbhb_0xxCTDwV0HLIU-JLUHMo/s1600/a7.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjj-IB9xsoo9oHtz4iLK0dR9FXuSbyo8-PYLeznLvR8_uZFSUJFtdI2AgHRu0_Iv15Om0FhWGTJ9bGYQfIXVlfn-CLEaE4wNSD5QOjz_Sr3u2A4zxXBgmWbhb_0xxCTDwV0HLIU-JLUHMo/s400/a7.png" /></a><br/>
</li>
</ol>Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com13tag:blogger.com,1999:blog-3057039820716415448.post-82931753771379085742014-06-07T23:02:00.004+01:002014-09-29T02:27:16.114+01:00Snake for androidI've just finished another android app: Snake.<br />
<br />
Here are the <a href="https://github.com/difusal/snake-android" target="_blank">github repository</a> and the <a href="http://bin.joaoan.es/Snake-debug-unaligned.apk" target="_blank">.apk download</a> links.<br />
<br />
Screenshots:<br />
<iframe class="imgur-album" frameborder="0" height="550" src="http://imgur.com/a/5YEXk/embed" width="100%"></iframe>Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-62401399069188061722014-06-02T18:31:00.000+01:002014-08-31T14:09:59.575+01:00Remove unwanted file that has been committed several commits ago from git repositoryRecently I have been developing a contacts management program and as usual I've been using git for source code management. This program stores all the contacts info in a file, say <b>contacts.info</b>.<br />
<br />
Today I bumped into a problem I had never had:<br />
I noticed I had committed <b>contacts.info</b> several commits ago. As you can imagine, I had no intention of making those contacts info public. I needed to delete that file from my git repository, from all the commits I've committed since I added <b>contacts.info</b>.<br />
<br />
I ended up finding a very useful tool for the job: <a href="http://rtyley.github.io/bfg-repo-cleaner/" target="_blank">BFG-Repo-Cleaner</a>.<br />
<br />
If you have a similar problem, here is a <b>step-by-step</b> guide on how to completely remove an unwanted file that has been committed several commits ago from your git repository:<br />
<ol>
<li>Download BFG-Repo-Cleaner;</li>
<li>Clone a bare copy of your repository:</li>
<ul>
<li>git clone --mirror https://github.com/difusal/my-repo.git</li>
</ul>
<li>Delete <b>contacts.info</b> from all the commits:</li>
<ul>
<li>java -jar bfg-1.11.6.jar --delete-files contacts.info my-repo.git/</li>
</ul>
<li>Push the repository to save changes:</li>
<ul>
<li>git push</li>
</ul>
</ol>
Yes, it is just that simple! If you browse through your past commits, any trace of <b>contacts.info</b> should now have been completely deleted.Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-58575097298709690782014-05-29T04:11:00.000+01:002014-09-29T02:27:16.111+01:00My first android appI finally started learning android and spent the last few days working on my first app: an illustration of the <a href="http://en.wikipedia.org/wiki/Monty_Hall_problem" target="_blank">Monty Hall problem</a>.<br />
<div>
<br /></div>
<div>
Here are the <a href="https://github.com/difusal/monty-hall-problem" target="_blank">github repository</a> and the <a href="http://bin.joaoan.es/MontyHallProblem-debug-unaligned.apk" target="_blank">.apk download</a> links.</div>
<div>
<br /></div>
<div>
Screenshots:<br />
</div>
<iframe class="imgur-album" frameborder="0" height="550" src="http://imgur.com/a/y1lua/embed" width="100%"></iframe>Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-55326214109148865572014-04-11T12:11:00.001+01:002014-08-31T14:10:51.557+01:00Sudoku Solver<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">With this program you can solve even the <a href="http://www.telegraph.co.uk/science/science-news/9359579/Worlds-hardest-sudoku-can-you-crack-it.html" target="_blank">world's hardest sudoku</a>!</span><br />
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Just insert the puzzle, click "Solve" and the solution will be shown.</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Download link:</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><a href="https://github.com/difusal/Sudoku-Solver/blob/master/Binaries/Sudoku%20Solver.jar?raw=true">https://github.com/difusal/Sudoku-Solver/blob/master/Binaries/Sudoku%20Solver.jar?raw=true</a></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Repository:</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><a href="https://github.com/difusal/Sudoku-Solver">https://github.com/difusal/Sudoku-Solver</a></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Screenshots:</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSB6sKTQxzXY8YmLCGMcMwVoyvHfUc0dJimiNXjXJbj8VElmKkU0rCsP6i1NhyphenhyphendDn-We8JIWV6VBCnnQ_C3-5jHjSt4etDAs_frfLg3NN_aei09SoduMmlTGNQbzvtezg12ukHO3yxaZA/s1600/1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSB6sKTQxzXY8YmLCGMcMwVoyvHfUc0dJimiNXjXJbj8VElmKkU0rCsP6i1NhyphenhyphendDn-We8JIWV6VBCnnQ_C3-5jHjSt4etDAs_frfLg3NN_aei09SoduMmlTGNQbzvtezg12ukHO3yxaZA/s1600/1.png" height="400" width="400" /></span></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdR9tWPr-3HiZzVY_sEkH1jxHJ9Z-Bm_x3gCeh2bGRuqtxUiN3UiSRiafpg5j0ev-q_fLumlaVNx6x1T88NowjQd3IR6J3tRMcZj5Lo8XH3u3dCyB1jAQydMPMn7uvK5e6zZM8YLVwTzQ/s1600/2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdR9tWPr-3HiZzVY_sEkH1jxHJ9Z-Bm_x3gCeh2bGRuqtxUiN3UiSRiafpg5j0ev-q_fLumlaVNx6x1T88NowjQd3IR6J3tRMcZj5Lo8XH3u3dCyB1jAQydMPMn7uvK5e6zZM8YLVwTzQ/s1600/2.png" height="400" width="400" /></span></a></div>
<br /></div>
Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com0tag:blogger.com,1999:blog-3057039820716415448.post-33814062448584835142014-02-22T18:14:00.000+00:002016-03-12T17:34:48.208+00:00Maze Generation Algorithm<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Recently, I had to develop a game that required a random maze to be generated. The logic behind it is not that hard, but some people may have trouble trying to code it, so I decided I would blog about it.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">This post is a how-to generate random labyrinths. Keep in mind there are a lot of algorithms and this is not the best neither the most optimized. In my opinion it is the simplest though.</span><br />
<br />
<h2>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><span style="font-size: x-large;">Introduction</span></span></h2>
<h3>
<br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">What you will need:</span></h3>
<h3>
<ul style="font-weight: normal;">
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: small;">lab: an array to store the labyrinth (I'll be using a 2D array);</span></li>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: small;">visitedCells: a secondary array to keep record of the visited cells;</span></li>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: small;">guideCell: the coordinates of the head of the path;</span></li>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: small;">pathHistory: a stack to keep a record of the path taken.</span></li>
</ul>
<ul style="font-size: medium; font-weight: normal;"></ul>
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">How the algorithm works:</span></h3>
<ul>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The guide cell starts <u>next to the exit</u>;</span></li>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The guide cell advances to a <u>random unvisited cell</u>;</span></li>
<ul>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The guide cell keeps advancing until there are <u>no unvisited cells around</u> it;</span></li>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">When there are no unvisited cells around, the <u>stack</u> gets useful:</span></li>
<ul>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><u>back trace</u> to a point of the path where there is <u>at least one unvisited cell</u> and develop the algorithm from there;</span></li>
</ul>
</ul>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The maze is completely <u>finished</u> when <u>all cells have been visited</u>.</span></li>
<ul>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">An even easier way to know it is finished is when the <u>stack gets empty</u>.</span></li>
</ul>
</ul>
<h3>
</h3>
<h3>
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Restrictions:</span></h3>
<div>
<ul>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The dimension of the maze needs to be an odd number.</span></li>
</ul>
<h2>
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: x-large;">Detailed Algorithm Analysis</span></h2>
</div>
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Let's say we need to generate a 7x7 maze.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The first step is to fill the 2D array with walls (I'll be using 'X' to display walls). After this we need to free the cells which have odd x and y coordinates, just like the picture below:</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbFhwIBkZxzNKEiDOiOe7Jxhrup3t6Njo-TLhJpYBX-WMHnK8p_IPoMgV625nXR72V1mDvK88qu_NMsaCxofiq3FShPyk-epEsvBDV4Cv6wB5UeBwTISNrc41zSCjXi0lgiga24RdKZnY/s1600/Screenshot+from+2014-02-22+16:34:24.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbFhwIBkZxzNKEiDOiOe7Jxhrup3t6Njo-TLhJpYBX-WMHnK8p_IPoMgV625nXR72V1mDvK88qu_NMsaCxofiq3FShPyk-epEsvBDV4Cv6wB5UeBwTISNrc41zSCjXi0lgiga24RdKZnY/s1600/Screenshot+from+2014-02-22+16:34:24.png" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">And this is the base matrix where the algorithm will work. That's why the the maze needs to have an odd dimension.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The next step would be <u>randomly</u> picking one <u>free cell</u> (as long as it is right next to the maze borders) and marking it as the <u>guideCell</u>. After that, place an 'S' on the maze border to mark the <u>exit</u>:</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxOfC4ke8kEj04NT1rI89ne30Rc9H8XWrUHafRu0UTXLhGbQKUBLG5XOw7GkgX2PVvtlckEvzIkOAzE73y5KumEKMUW6cmZBVS33GBKGV0CNR1V3KxQ2DfBmTiB-YBNia5eDeN3pyfjjs/s1600/Screenshot+from+2014-02-22+16:49:35.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxOfC4ke8kEj04NT1rI89ne30Rc9H8XWrUHafRu0UTXLhGbQKUBLG5XOw7GkgX2PVvtlckEvzIkOAzE73y5KumEKMUW6cmZBVS33GBKGV0CNR1V3KxQ2DfBmTiB-YBNia5eDeN3pyfjjs/s1600/Screenshot+from+2014-02-22+16:49:35.png" /></a></div>
<div style="text-align: center;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">S: exit</span></div>
<div style="text-align: center;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">+: guideCell</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The next step is to create the visitedCells array to keep track of the cells which have been visited or not. This array will not be 7x7 but 3x3. The dimension of the visitedCells array is given by</span><br />
<br />
<div style="text-align: center;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">visitedCellsDimension = (labDimension - 1) / 2</span></div>
<div style="text-align: left;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Here is the current state of the visitedCells array:</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIakEDpUKtyA4RvR0C4mQ5M5ri45lFWC6ifVZkOMPoovNu-AAghs7OlSBeZ7KfZKsvZeZfqZQWVvKwxuGxXIqtzBm_fCWXu8gDSV97u4HJX_jRKDrZ5AelQI3nPzRk3TWqjhHqoZR6org/s1600/Screenshot+from+2014-02-22+16:54:19.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIakEDpUKtyA4RvR0C4mQ5M5ri45lFWC6ifVZkOMPoovNu-AAghs7OlSBeZ7KfZKsvZeZfqZQWVvKwxuGxXIqtzBm_fCWXu8gDSV97u4HJX_jRKDrZ5AelQI3nPzRk3TWqjhHqoZR6org/s1600/Screenshot+from+2014-02-22+16:54:19.png" /></a></div>
<div style="text-align: center;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.: unvisited cell</span></div>
<div style="text-align: center;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">+: visited cell</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Ok, by this time we have our lab array and our visitedCell ready. We just need to push the guideCell coords to the stack:</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDseJcR8EQZT-37gRm9-R2SySMp_fT2lfyztwB3sm2kGo0FYCwofhlzD2NihR8USdYey2lfuai9GNH5s48Lp_KGMRY-5_ZpSQdS10GrOoKyrVV8hSGkx1uowEDvAiw3rCTVB38Mn2Fgmw/s1600/Screenshot+from+2014-02-22+17:01:26.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDseJcR8EQZT-37gRm9-R2SySMp_fT2lfyztwB3sm2kGo0FYCwofhlzD2NihR8USdYey2lfuai9GNH5s48Lp_KGMRY-5_ZpSQdS10GrOoKyrVV8hSGkx1uowEDvAiw3rCTVB38Mn2Fgmw/s1600/Screenshot+from+2014-02-22+17:01:26.png" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now that we have everything ready, let the algorithm start working! Yay! :D</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">First we need to generate a <u>random direction</u> and check if that cell has already been visited. If not, the guide cell will advance to that direction.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Assume that the computer randomly says: "guideCell, go down!". The cell below the guideCell current location has not yet been visited according to the visitedCells array, so this is what will happen:</span><br />
<ul>
<li><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">the <u>guideCell</u> coords will be updated and pushed to the stack;</span></li>
<li><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">the <u>visitedCells</u> matrix will be updated:</span></li>
<ul>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">a '+' will be marked at {2, 1};</span></li>
</ul>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">the <u>lab</u> array will also be updated:</span></li>
<ul>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">the 'X' on {5, 2} will be removed.</span></li>
</ul>
</ul>
<ul><ul>
</ul>
</ul>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">This is how the containers look at this point:</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi35tons_X1RaifHs8sMRt6ibgSZ8ViBIMWVtoEzzx2hzy8_XIS2IymkwLLYqrtE8NQIAHwoQ_40_B3V5Xjt8qA2qpmLL-B192E5ndk8Os91kfHMaf-DDjJ7rAomTyFj2nHx5yDDF7oWX4/s1600/Screenshot+from+2014-02-22+17:13:47.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi35tons_X1RaifHs8sMRt6ibgSZ8ViBIMWVtoEzzx2hzy8_XIS2IymkwLLYqrtE8NQIAHwoQ_40_B3V5Xjt8qA2qpmLL-B192E5ndk8Os91kfHMaf-DDjJ7rAomTyFj2nHx5yDDF7oWX4/s1600/Screenshot+from+2014-02-22+17:13:47.png" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Ok let's speed up things a little bit. I hope you are catching up with me.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Let's say the computer generated this directions randomly:</span><br />
<ul>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">right;</span></li>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">up;</span></li>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">left;</span></li>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">down;</span></li>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">right.</span></li>
</ul>
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The 1st direction (right) will be ignored because the guide cell can not go right! That's the border of the maze!</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The 2nd direction generated (up) will also be ignored because according to the visitedCells, the cell above the guideCell has already been visited. Do you get it? :)</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The 3rd direction is a valid one though! The guideCell can indeed go left. So this is what will happen:</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9Xam5OjWN-l6kEeIGc4wjle4PNJ-FE3C9LYTun5TeHNQU8FA9nvShntL-QyIG6x9JGNafLnzMbkYVY8IRWyasOzomnj3FgCJia5YFQqX5-PyiZxYg81GnDMCzwu5NbXoO5d8GIqwm5Hs/s1600/Screenshot+from+2014-02-22+17:22:02.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9Xam5OjWN-l6kEeIGc4wjle4PNJ-FE3C9LYTun5TeHNQU8FA9nvShntL-QyIG6x9JGNafLnzMbkYVY8IRWyasOzomnj3FgCJia5YFQqX5-PyiZxYg81GnDMCzwu5NbXoO5d8GIqwm5Hs/s1600/Screenshot+from+2014-02-22+17:22:02.png" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The 4th direction generated was 'down':</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZkQ98-Cvh_KO4Oi4LShBe6K2E0lRbRzWsWT_wkzQXhcTjs5eVjA3LAZ75sidgCx4AKkBJL3F_BjEiKVbEyWqsyikW46iJIKfK2kSHYuBOcL6SRqAAUejYPZobugfLl759kCrUGTN6fiM/s1600/Screenshot+from+2014-02-22+17:23:38.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZkQ98-Cvh_KO4Oi4LShBe6K2E0lRbRzWsWT_wkzQXhcTjs5eVjA3LAZ75sidgCx4AKkBJL3F_BjEiKVbEyWqsyikW46iJIKfK2kSHYuBOcL6SRqAAUejYPZobugfLl759kCrUGTN6fiM/s1600/Screenshot+from+2014-02-22+17:23:38.png" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">And finally, the 5th direction generated was 'right':</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2k1qtu19SmRzqYIDeNeqy3EkjuiUDdDv-JjBb6jZVOwLBsqoQLyrC44g0TB_lPwnyCgvEMV1spYo6zxQkm_GwHsCL5Bj7WwEogJmkHHPoOuSjFv9q9UI8eWNvT8BKM6zJkfFVipeU-qQ/s1600/Screenshot+from+2014-02-22+17:24:43.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2k1qtu19SmRzqYIDeNeqy3EkjuiUDdDv-JjBb6jZVOwLBsqoQLyrC44g0TB_lPwnyCgvEMV1spYo6zxQkm_GwHsCL5Bj7WwEogJmkHHPoOuSjFv9q9UI8eWNvT8BKM6zJkfFVipeU-qQ/s1600/Screenshot+from+2014-02-22+17:24:43.png" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Ok, by now you should have realized how the algorithm works.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Notice what happens now:</span><br />
<ul>
<li><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">If the computer generates <u>right</u> or <u>down</u>, the guideCell can not go there because that is the <u>maze borders</u>!</span></li>
<li><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">If the computer generates <u>left</u> or <u>up</u>, the guideCell can not go there as well because according to the visitedCells array, both cells in that direction have <u>already been visited</u>!</span></li>
</ul>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">It is a <u>dead end</u>! So, how do we finish generating the maze? It's not finished yet...</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Well, it's not that hard! This is why we have the <u>stack</u> with the history of the path we took!</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">When we run into this problem we just need to <u>back trace</u> the stack and <u>look for a cell</u> that has <u>at least one neighbor</u> that has <u>not yet been visited</u>. When we do find this cell, it becomes our new guideCell.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Let me use the example we have been working on to illustrate what I mean:</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The current guideCell has no unvisited cells as neighbors, so we need to <u>remove</u> it from the stack. And since we removed it, the <u>top</u> of the stack is {1, 2}, and this cell has at least one neighbor unvisited: <b>This is our new guideCell.</b></span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnyAVe8OEmzmLJ49DqlsiEJ3TLeV13lWWKV5YYaOF57ugfgoiuAZ6LRiHjHe1ttUMaAAaYvOkxgnPVPnE8di6_J4vwr9CNjSuTctmGVzfhZBla-Jig6pcvnCoQ4I45X_txUbDp-W9wMbE/s1600/Screenshot+from+2014-02-22+17:53:54.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnyAVe8OEmzmLJ49DqlsiEJ3TLeV13lWWKV5YYaOF57ugfgoiuAZ6LRiHjHe1ttUMaAAaYvOkxgnPVPnE8di6_J4vwr9CNjSuTctmGVzfhZBla-Jig6pcvnCoQ4I45X_txUbDp-W9wMbE/s1600/Screenshot+from+2014-02-22+17:53:54.png" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">And since there is only one valid direction (left), the following algorithm iterations will be:</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-3Lu-Txtc4lLZOG7Jf7hNSeIHNMd6zIsmQAng-srq8w3CfMcRFR7VFbFbyTe4BijWkFQXgPbAOBmIza7zu7-hnx5CMorY-ynwrvrFrsOEiOEZeg8NMIGvML31fMfZovxlQpcdLbG6kYE/s1600/Screenshot+from+2014-02-22+17:57:25.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-3Lu-Txtc4lLZOG7Jf7hNSeIHNMd6zIsmQAng-srq8w3CfMcRFR7VFbFbyTe4BijWkFQXgPbAOBmIza7zu7-hnx5CMorY-ynwrvrFrsOEiOEZeg8NMIGvML31fMfZovxlQpcdLbG6kYE/s1600/Screenshot+from+2014-02-22+17:57:25.png" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkJ3VIjRaSWfDUCA-rg8ieKFQOjFempbwRQ83MzoT4EQ0wZ6pC2JIV9KEXnhnK7ErUOaL2g90a2s5YJ9bKkeXq-ouP8-CNZAtwwGmTQt6SYUceEnpbWojwPfqExPWvvNsEsBhFZuss1uI/s1600/Screenshot+from+2014-02-22+18:06:25.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkJ3VIjRaSWfDUCA-rg8ieKFQOjFempbwRQ83MzoT4EQ0wZ6pC2JIV9KEXnhnK7ErUOaL2g90a2s5YJ9bKkeXq-ouP8-CNZAtwwGmTQt6SYUceEnpbWojwPfqExPWvvNsEsBhFZuss1uI/s1600/Screenshot+from+2014-02-22+18:06:25.png" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhg6435qq1drEsE4cfkMoNxxiGpujRj8wCXoD5Vqg1sIHe6zEUCj4z6ycKquHevzkjgpMjvyiyiXJWDoPTi5bFpR6y-V7f4NeqpXvS8VWM34p7ukTrf3fV7s3WMPOqzaGWTit8YtN7R7TQ/s1600/Screenshot+from+2014-02-22+18:07:02.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhg6435qq1drEsE4cfkMoNxxiGpujRj8wCXoD5Vqg1sIHe6zEUCj4z6ycKquHevzkjgpMjvyiyiXJWDoPTi5bFpR6y-V7f4NeqpXvS8VWM34p7ukTrf3fV7s3WMPOqzaGWTit8YtN7R7TQ/s1600/Screenshot+from+2014-02-22+18:07:02.png" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmcgtjaAWhvjzW7kJ0N_u4P9E1sIhH6m2KLBsRqtrQeaTdcX-tNPrHDGRDM3HUy6xoo2zBO2jiKnV8ufUcW6C-Hcy96uboE9MgYdZRbQsDl2SkDAUeOXFbjmDrtguxnFb9ckB1eBCqxaM/s1600/Screenshot+from+2014-02-22+18:08:32.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmcgtjaAWhvjzW7kJ0N_u4P9E1sIhH6m2KLBsRqtrQeaTdcX-tNPrHDGRDM3HUy6xoo2zBO2jiKnV8ufUcW6C-Hcy96uboE9MgYdZRbQsDl2SkDAUeOXFbjmDrtguxnFb9ckB1eBCqxaM/s1600/Screenshot+from+2014-02-22+18:08:32.png" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">And at this point, once again, we got to a <u>dead end</u>. What did we do when this happened before? That is right! We popped the stack until we found a cell that had at least one unvisited neighbor.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">This time though, we will pop the stack and won't find a cell that matches this condition. Why? Because all cells have been visited! You can check that by looking to the visitedCells array.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The program will therefore be <u>popping</u> the stack and eventually <u>empty</u> it.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">And that concludes the algorithm: when the stack gets empty, all the cells have been visited and the maze generation was successfully completed! :)</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Here is an example of a 31x31 maze generated with this algorithm:</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipcqxPnRFVMbxCmjgxZv4I2qJNi9DOAY0jBIEeHs78XUbkHXL7Ktw2K9SH0-aaGhSLYfF5-4yLpgD8Ggh46S_OCiXNxAY3mllgiTLajPtV30pa2p4wCqAkKQKqufF5NadPoTOIJSOzJR0/s1600/Screenshot+from+2014-02-22+19:54:22.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipcqxPnRFVMbxCmjgxZv4I2qJNi9DOAY0jBIEeHs78XUbkHXL7Ktw2K9SH0-aaGhSLYfF5-4yLpgD8Ggh46S_OCiXNxAY3mllgiTLajPtV30pa2p4wCqAkKQKqufF5NadPoTOIJSOzJR0/s1600/Screenshot+from+2014-02-22+19:54:22.png" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">I hope this helped you and was not too confusing... Cheers!</span>Difusalhttp://www.blogger.com/profile/00209040335941747621noreply@blogger.com1