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

<channel>
	<title>Dev Hub</title>
	<atom:link href="http://www.sdltutorials.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sdltutorials.com</link>
	<description>SDL Tutorials - Game Tutorials - Programming Tutorials</description>
	<lastBuildDate>Fri, 05 Mar 2010 19:18:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>New Contest</title>
		<link>http://www.sdltutorials.com/new-contest/</link>
		<comments>http://www.sdltutorials.com/new-contest/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 19:18:11 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=292</guid>
		<description><![CDATA[Well, I&#8217;ve been on this overtime marathon again at work, so I haven&#8217;t had any time to do much on here. Well, lets fix that. I can&#8217;t really offer any prizes this time, but hopefully that doesn&#8217;t deter people.
The Goal: Make a Tetris-like game
The Inspiration: Tetris, Pokemon Puzzle League
The Rules: This contest is open to [...]]]></description>
			<content:encoded><![CDATA[<p>Well, I&#8217;ve been on this overtime marathon again at work, so I haven&#8217;t had any time to do much on here. Well, lets fix that. I can&#8217;t really offer any prizes this time, but hopefully that doesn&#8217;t deter people.</p>
<p><strong>The Goal:</strong> Make a Tetris-like game<br />
<strong>The Inspiration:</strong> Tetris, <a href="http://en.wikipedia.org/wiki/Pok%C3%A9mon_Puzzle_League">Pokemon Puzzle League</a><br />
<strong>The Rules:</strong> This contest is open to all game developers, with the following requirements and/or conditions:</p>
<ol>
<li>No more than 3 people per project</li>
<li>You may submit as many projects as you like</li>
<li>I prefer SDL / C/C++ (optionally can use OpenGL <u>with</u> SDL); SDL <u>must be used</u>. Other languages allowed: Delphi, VC++.NET, Java</li>
<li>Project must compile/work on at least one major operating system (Windows, Mac OSX, *nix); extra bonus points for cross-platformy*</li>
<li>You give SDLtutorials.com permission to use any and all submitted resources, code, and/or files for non-commercial use. Submitter retains the rights to his/her project. (SDLtutorials.com will most likely use the winning project as the basis for a tutorial)</li>
<li>All code, resources, and/or files submitted must be the submitters original work, with the exception of use of 3rd party open-source libraries, and/or royalty free resources. No commercial libraries and/or resources are allowed, and will result in immediate disqualification.</li>
<li>No plagiarism is allowed, with the exception of the &#8220;minor&#8221;** use of code from tutorials. Code used from tutorials cannot make up more than 20%** of a submitted project&#8217;s code. You may use the framework provided on this website without limitation.</li>
<li>SDLtutorials.com has full discretion to accept/reject any projects submitted, and can cancel this contest at any time.</li>
</ol>
<p>* No actual points are involved in the judging of entries.<br />
** This, obviously, is hard to determine. Be fair, do your own work; if you use code from online, make it your own.</p>
<p><strong>The Details:</strong></p>
<ul>
<li>The game should play / look like a Tetris clone</li>
<li>You must provide a completely working no time-limit game. (Meaning, there don&#8217;t have to be levels, bosses, etc.)</li>
<li>The game should at least be point based</li>
<li>You must provide a basic menu (Start, Exit at minimum)</li>
<li>The game may be 2D or 3D (using OpenGL), and may use scripting</li>
</ul>
<p><strong>The Prize:</strong></p>
<ul>
<li>None this time &#8211; open to donations</li>
</ul>
<p><strong>The Deadline:</strong><br />
<span style="color: red">May 1, 2009</span></p>
<p>Once all entries are received, I will post 5 of the top projects received. The community will then be able to voice their opinion (which should help weed out any plagiarism). A week after the deadline, I will post who is the winner. If I cannot contact the winner within 30 days, the 2nd place project will become the winner (and so on).</p>
<p>You have plenty of time, make it as cool, and interesting as possible. I could probably make a straight Tetris clone in a day, and all you will have almost 2 months. The only thing I don&#8217;t necessarily want to see is something exactly like Tetris (not unless you put some crazy spin on it).</p>
<p>Please send all completed Projects to: tim [at] metacipher.com</p>
<p>You may send your project multiple times up to the deadline. The last project I receive before <b>midnight</b> is the one that I will use.</p>
<p>Good luck everyone!</p>
<p>Terms are subject to change at any time; I will give notice to such changes.</p>
<p>The following are some guidelines for when you submit projects. A real quick note first: by submitting your project multiple times up to the deadline, I can give you feedback and/or help. Please note, I will not give you any code, or any links to code. All I will simply do is point you in the right direction. This should even the playing field a bit for newbies.</p>
<ol>
<li>Please provide a project file and/or makefile for me to compile your code depending on the target platform.</li>
<li>Please include any and all libraries involved with your project; <b>already compiled</b>. I don&#8217;t want to have to sit here for hours compiling libraries that are used in your project. Exceptions: if the library is <u>easy</u> to grab myself, simply let me know in a ReadMe.txt or something (i.e., being able to apt-get what I need, or simply download).</li>
<li>Please compress your project into a single file, and email it to me with the subject: CONTEST &#8211; [My Project Name] ; remember, email to: tim [at] metacipher.com</li>
<li>Though you can submit projects ahead of time for help, please do not submit your project more than once a week to me. I will provide help, but very limited; remember, this is a contest. I would only ask for help if you really need it; it&#8217;s very likely I&#8217;ll give more kudos to a project that asked for very little to no help, v. the person that asked every week.</li>
<li>Please&#8230;. please! Comment your code. Please.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/new-contest/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A Guide to Graphics with Sprig</title>
		<link>http://www.sdltutorials.com/a-guide-to-graphics-with-sprig/</link>
		<comments>http://www.sdltutorials.com/a-guide-to-graphics-with-sprig/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 16:38:55 +0000</pubDate>
		<dc:creator>Jonny D</dc:creator>
				<category><![CDATA[Blue Dino Code]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=207</guid>
		<description><![CDATA[The following tutorial was created by Blue Dino Code, for the purpose of helping people like you learn SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework [...]]]></description>
			<content:encoded><![CDATA[<p>The following tutorial was created by <a href="http://code.bluedinosaurs.com/">Blue Dino Code</a>, for the purpose of helping people like you learn SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework used by the author. SDLTutorials.com does not endorse or reject any of the coding practices outlined in the tutorial, and is not responsible for any code or files belonging to this tutorial that harms your computer.</p>
<hr/><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">A Guide to Graphics with SPriG</span></div>
<p><span style="font-weight: bold;">Prerequisites</span>:<br />
Some knowledge of SDL (see <a href="http://code.bluedinosaurs.com/installSDL.html">Installing SDL</a>)</p>
<p>The SDL Primitive Generator, <a href="http://code.bluedinosaurs.com/SPriG.html">SPriG</a>, is a quick and simple interface to making graphics on SDL surfaces.  In this guide, I&#8217;ll be discussing the notable features of Sprig with usage examples.  Not all of the functions are described here.  There are plenty more to be found in the documentation once you get the hang of the stuff found here.  I will, however, be describing the entire dirty rect interface.</p>
<p><span id="more-207"></span></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Contents</span></div>
<table style="text-align: left; width: 100%;" border="0" cellspacing="2" cellpadding="2">
<tbody>
<tr>
<td style="width: 302px;"></td>
<td style="width: 316px;"><a href="#Introduction">Introduction</a><br />
<a href="#History">History</a><br />
<a href="#Installation">Installation</a><br />
<a href="#Surface">Surface Commands</a><br />
<a href="#Primitives">Graphics Primitives</a><br />
<a href="#Polygons">Polygons</a><br />
<a href="#Controls">Rendering Controls</a><br />
<a href="#Drawing">Image Drawing</a><br />
<a href="#Angles">Angles</a><br />
<a href="#Transformations">Surface Transformations</a><br />
<a href="#Dirty">Dirty Rects</a></td>
<td style="width: 302px;"></td>
</tr>
</tbody>
</table>
<p><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Introduction<a name="Introduction"></a></span></div>
<p><br/><br />
<img alt="Intro" src="http://code.bluedinosaurs.com/SDL/SPriG/screenshots/intro.png" title="Intro" class="aligncenter" width="600" height="163" /><br />
Sprig does three main things:  It draws graphical primitives, it rotates and scales surfaces, and it simplifies access to SDL surfaces.  Once you&#8217;re off and using Sprig, I suggest that you download the documentation so you have easy reference material at hand.</p>
<p>Along the way, I&#8217;ll be leaving usage examples.  In these, I&#8217;ll be using <a href="http://code.bluedinosaurs.com/SDL.html">SDL_ColorDef</a> to define colors, so if you see something like this:</p>
<div class="dean_ch" style="white-space: wrap;">RGB_BLUE<span class="br0">&#40;</span>mySurface<span class="br0">&#41;</span></div>
<p>This is just a quick replacement for the usual format-independent call in SDL:</p>
<div class="dean_ch" style="white-space: wrap;">SDL_MapRGB<span class="br0">&#40;</span>mySurface-&gt;format, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">255</span><span class="br0">&#41;</span></div>
<p>Isn&#8217;t that convenient?  So, instead of describing a color with red, green, and blue components, I&#8217;ll just use common names.<br/></p>
<div style="text-align: center;"><span style="color: #006600;"> </span><span style="text-decoration: underline;">History and Motivation<a name="History"></a></span></div>
<p><br/><br />
<img alt="history" src="http://code.bluedinosaurs.com/SDL/SPriG/screenshots/history.png" title="history" class="aligncenter" width="400" height="300" /><br />
Sprig was originally an overhaul of SGE.  I called it minSGE back then because I was not satisfied with how many peripheral features are in SGE.  I cut away collision detection, fonts, C++ sprite classes, and text classes.  These features are handled far better by other libraries and would be quite the hassle to maintain and upgrade properly.  I assumed that most people used SGE for primitives and transforms and used other libraries for all the other stuff.  A major annoyance to me were the naming conventions, so I worked on that, too.  Well, that set the stage for designing Sprig.  In fact, I thought that fonts were important enough that I also started NFont as a successor to SFont for drawing bitmap fonts.  Sprig and NFont together are all that a simple game needs to get started.  These decisions also made it easy for Sprig to become a pure C library, so that users of either C or C++ can enjoy it.</p>
<p>I hope you&#8217;ll find that Sprig has a very comfortable feel in its naming scheme and range of convenient functions.  My main goal besides providing primitives and transforms is to supplement the built-in SDL functions to make development faster with less typing.<br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Installation<a name="Installation"></a></span></div>
<p><br/><br />
<img alt="installation" src="http://code.bluedinosaurs.com/SDL/SPriG/screenshots/installation.png" title="installation" class="aligncenter" width="400" height="338" /><br />
Installation for Sprig is just like any other C library.  You can check out the SDL installation guide for generic details (<a href="http://code.bluedinosaurs.com/installSDL.html">Installing SDL</a>).  For Sprig, you can get the pre-built binaries, use &#8216;make&#8217;, or build it by hand.</p>
<p>First of all, <a href="http://code.bluedinosaurs.com/SPriG.html">download</a> the library, of course.  If you have the binaries for your system, unzip them.  Copy &#8217;sprig.h&#8217; and &#8217;sprig_inline.h&#8217; to your compiler&#8217;s /include or /include/SDL folder.  Then copy the appropriate library file (libsprig.a, libsprig.so, sprig.lib) to your compiler&#8217;s /lib folder.  Now you can write Sprig programs by using:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co2">#include &quot;sprig.h&quot;</span></div>
<p>and linking with sprig (-lsprig or sprig.lib).</p>
<p>When you distribute these programs, be sure to include the dynamic library appropriate to your system (sprig.dll, libsprig.so).</p>
<p>Using make (Linux):<br />
Download the Sprig source, open a terminal window, and move into the unzipped Sprig source directory.  Type:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">make</span> <span class="kw2">install</span></div>
<p>That should be it!</p>
<p>Building by hand:<br />
Download the Sprig source and unzip it.  Make a new dynamic library project in your favorite compiler that contains all of the source files (.c).  Add SDLmain and SDL to your linker options.  Compile it and install it as above.</p>
<p>If you&#8217;re willing to comply with the LGPL license on static linking, you can build a static link library or just add all of the Sprig source files to your project.  You will then not need to distribute the library with your executable.</p>
<p>If you have any problems or are not on Windows XP or Debian GNU/Linux, then please send me an email!<br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Lurking Beneath the Surface<a name="Surface"></a></span></div>
<p>Sprig&#8217;s surface functions make creation and control of SDL surfaces quite easy.  Sprig has functions to set up SDL, create surfaces, and copy surfaces.</p>
<table style="text-align: left; text-valign: top; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr>
<td>SDL_Surface*</td>
<td>SPG_InitSDL(Uint16 w, Uint16 h, Uint8 bitsperpixel, Uint32 systemFlags, Uint32 screenFlags)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_CreateSurface32(Uint32 flags, Uint16 width, Uint16 height)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_CopySurface(SDL_Surface* surface)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_Free(SDL_Surface* surface)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_SetColorkey(SDL_Surface* surface, Uint32 color)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_SetClip(SDL_Surface* surface, const SDL_Rect clipRect)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_RestoreClip(SDL_Surface* surface)</td>
</tr>
</tbody>
</table>
<p>SPG_Init is a wrapper for calls to SDL_Init and SDL_SetVideoMode.  It returns the display surface or NULL if there&#8217;s an error.  Just like with SDL_SetVideoMode, passing 0 for the surface depth will give you the best available depth.</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Surface* screen = SPG_InitSDL<span class="br0">&#40;</span><span class="nu0">800</span>, <span class="nu0">600</span>, <span class="nu0">0</span>, SDL_INIT_VIDEO, SDL_SWSURFACE | SDL_FULLSCREEN<span class="br0">&#41;</span>;<br />
<span class="kw1">if</span><span class="br0">&#40;</span>screen == <span class="kw2">NULL</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw3">exit</span><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>;</div>
<p>SPG_CreateSurface32 is how you can create 32-bit RGBA surfaces with minimal work.</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Surface* mySurf = SPG_CreateSurface32<span class="br0">&#40;</span>SDL_SWSURFACE, <span class="nu0">100</span>, <span class="nu0">100</span><span class="br0">&#41;</span>;</div>
<p>SPG_CopySurface returns a duplicate of the given surface.</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Surface* copy = SPG_CopySurface<span class="br0">&#40;</span>mySurf<span class="br0">&#41;</span>;</div>
<p>SPG_Free is a wrapper for SDL_FreeSurface.  It&#8217;s just for less typing, nothing special.  When you&#8217;re done using a surface, you must free the memory using SPG_Free (or SDL_FreeSurface).</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Free<span class="br0">&#40;</span>mySurf<span class="br0">&#41;</span>;</div>
<p>SPG_SetColorkey sets the transparent color for a given surface.  Colorkey transparency can only be used for RGB-&gt;RGB blits, RGB-&gt;RGBA blits, or RGBA-&gt;RGBA blits with SDL_SRCALPHA disabled.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_SetColorkey<span class="br0">&#40;</span>mySurf, RGB_BRIGHTPINK<span class="br0">&#40;</span>mySurf<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
SDL_BlitSurface<span class="br0">&#40;</span>mySurf, <span class="kw2">NULL</span>, screen, <span class="kw2">NULL</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Only non-pink areas will be drawn.</span></div>
<p>SPG_SetClip sets the clipping rectangle for the given surface.  When stuff is drawn to this surface, only the parts inside the clipping rect will actually be drawn.  You can reset the clipping rect by using SPG_RestoreClip.  This is really useful for splitscreen effects.</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Rect clipRect = <span class="br0">&#123;</span><span class="nu0">40</span>, <span class="nu0">40</span>, <span class="nu0">60</span>, <span class="nu0">100</span><span class="br0">&#125;</span>;<br />
SPG_SetClip<span class="br0">&#40;</span>mySurf, clipRect<span class="br0">&#41;</span>;<br />
SPG_RestoreClip<span class="br0">&#40;</span>mySurf<span class="br0">&#41;</span>; &nbsp;<span class="co1">// Reset it</span></div>
<p><br/><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">As Primitive as Can Be<a name="Primitives"></a></span></div>
<p><br/><br />
<img alt="primitives" src="http://code.bluedinosaurs.com/SDL/SPriG/screenshots/primitives.png" title="primitives" class="aligncenter" width="400" height="300" /><br />
Sprig is first and foremost a generator for graphics primitives (of course).  These are pixel shapes like lines, circles, and rectangles.  Sprig has functions to create:</p>
<p>Pixels<br />
Lines<br />
Rectangles<br />
Circles<br />
Ellipses<br />
Arcs<br />
Bezier curves<br />
Trigons (Triangles)<br />
Polygons</p>
<p>Each of these primitives can be drawn with anti-aliasing, different alpha-blending modes, and a specific thickness.  Rects, circles, ellipses, arcs, trigons, and polygons can all be drawn with filled interiors.  Check out the Controls section for more info on changing how primitives are drawn.</p>
<p>The naming convention that Sprig follows is like so:<br />
SPG_(Shape)(Filled)(Blend)</p>
<p>For example:<br />
SPG_RectFilled<br />
SPG_PixelBlend<br />
SPG_EllipseFilledBlend</p>
<p>The different words in each function name follow an intuitive hierarchical naming scheme, based on the magnitude of the change they make to the shape.  This is especially useful when combined with many IDEs&#8217; autocompletion to save you from lots of typing.</p>
<p>Here are some sample drawing commands.  The signature of each function can be found in the documentation.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Rect<span class="br0">&#40;</span>screen, <span class="nu0">40</span>, <span class="nu0">60</span>, <span class="nu0">140</span>, <span class="nu0">100</span>, RGB_RED<span class="br0">&#40;</span>screen<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
SPG_CircleFilledBlend<span class="br0">&#40;</span>screen, <span class="nu0">300</span>, <span class="nu0">100</span>, <span class="nu0">20</span>, RGB_PURPLE<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>, <span class="nu0">200</span><span class="br0">&#41;</span>;<br />
SPG_Line<span class="br0">&#40;</span>screen, <span class="nu0">0</span>, <span class="nu0">0</span>, screen-&gt;w<span class="nu0">-1</span>, screen-&gt;h<span class="nu0">-1</span>, RGB_GRAY<span class="br0">&#40;</span>screen<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
SPG_ArcFilled<span class="br0">&#40;</span>screen, <span class="nu0">200</span>, <span class="nu0">500</span>, <span class="nu0">40</span>, <span class="nu0">-90</span>, <span class="nu0">45</span>, RGB_YELLOW<span class="br0">&#40;</span>screen<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
SPG_PixelBlend<span class="br0">&#40;</span>screen, <span class="nu0">60</span>, <span class="nu0">60</span>, RGB_WHITE<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>, <span class="nu0">100</span><span class="br0">&#41;</span>;</div>
<p><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Two Sides of the Same Polygon<a name="Polygons"></a></span></div>
<p><br/><br />
<img alt="polygons" src="http://code.bluedinosaurs.com/SDL/SPriG/screenshots/polygons.png" title="polygons" class="aligncenter" width="400" height="300" /><br />
Polygons are great for many effects and can represent platforms or form characters in various video games.  With Sprig&#8217;s polygon rotations, you can easily make something like the opening screen for Zelda III&#8230;  Some functions use the SPG_Point coordinate structure to simplify their calls.  SPG_Point contains two floating point values, x and y.  Use SPG_MakePoint to define your own points for these functions.  You can also fill the struct when it&#8217;s created:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Point p = <span class="br0">&#123;</span><span class="nu0">50</span>, <span class="nu0">40</span><span class="br0">&#125;</span>;</div>
<p>The functions:</p>
<table style="text-align: left; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr>
<td>void</td>
<td>SPG_Trigon(SDL_Surface* surface, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_QuadTexPoints(SDL_Surface* dest, SPG_Point* dest_points, SDL_Surface* source, SPG_Point* source_points)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_Polygon(SDL_Surface* surface, Uint16 n, SPG_Point* points, Uint32 color)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_CopyPoints(Uint16 n, SPG_Point* points, SPG_Point* buffer)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_RotatePoints(Uint16 n, SPG_Point* points, float angle)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_ScalePoints(Uint16 n, SPG_Point* points, float xscale, float yscale)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_SkewPoints(Uint16 n, SPG_Point* points, float xskew, float yskew)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_TranslatePoints(Uint16 n, SPG_Point* points, float dx, float dy)</td>
</tr>
</tbody>
</table>
<p>SPG_Trigon draws a triangle from three points.  Just like other primitive shapes, it can be a line drawing or variable thickness, filled, alpha-blended, or anti-aliased.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Trigon<span class="br0">&#40;</span>screen, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">50</span>, <span class="nu0">10</span>, <span class="nu0">20</span>, <span class="nu0">20</span>, RGB_ORANGE<span class="br0">&#40;</span>screen<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
<p>SPG_QuadTexPoints draws a four-sided polygon that is filled with an image.  A set of source points allows you to choose exactly what part of the source image is used.  This can be used for stretching, rotating, and distorting images.  There is another function, SPG_QuadTex, that uses (x,y) coordinates instead of SPG_Points.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_QuadTexPoints<span class="br0">&#40;</span>screen, myQuad, myImage, otherQuad<span class="br0">&#41;</span>;</div>
<p>SPG_Polygon and all of its variants draw a multi-sided shape using SPG_Point values.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Polygon<span class="br0">&#40;</span>screen, <span class="nu0">6</span>, myHexagon, RGB_TEAL<span class="br0">&#40;</span>screen<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
<p>SPG_CopyPoints makes a static copy of your points into the given buffer.  The result can be messed with, drawn using SPG_Polygon, then deleted without ruining your original shape.  It uses a pre-existing buffer, so it&#8217;s nice and fast.  You can also use this to copy part of a polygon into another polygon with some pointer arithmetic.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Point buffer<span class="br0">&#91;</span><span class="nu0">6</span><span class="br0">&#93;</span>;<br />
SPG_CopyPoints<span class="br0">&#40;</span><span class="nu0">6</span>, myHexagon, buffer<span class="br0">&#41;</span>;</p>
<p>SPG_CopyPoints<span class="br0">&#40;</span><span class="nu0">3</span>, myTrigon, <span class="br0">&#40;</span>myHexagon + <span class="nu0">3</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="co1">// Copy points into myHexagon[3], [4], and [5]</span></div>
<p>SPG_RotatePoints rotates each SPG_Point about the origin by the specified angle.  To rotate about a different point, you can use SPG_RotatePointsXY (or translate it back and forth).</p>
<div class="dean_ch" style="white-space: wrap;">SPG_RotatePoints<span class="br0">&#40;</span><span class="nu0">5</span>, myPentagon, <span class="nu0">45</span><span class="br0">&#41;</span>;</div>
<p>SPG_ScalePoints can make a polygon bigger or smaller.  It scales about the origin (i.e. assumes the center of the polygon is at (0,0)), but you can use SPG_ScalePointsXY (or translate it back and forth) to change that.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_ScalePoints<span class="br0">&#40;</span><span class="nu0">5</span>, myPentagon, <span class="nu0">2</span>.0f, <span class="nu0">2</span>.0f<span class="br0">&#41;</span>; &nbsp;<span class="co1">// Double the size</span></div>
<p>SPG_SkewPoints applies a shear transform to the polygon (about the origin, as above &#8211; Try out SPG_SkewPointsXY too).</p>
<div class="dean_ch" style="white-space: wrap;">SPG_SkewPoints<span class="br0">&#40;</span><span class="nu0">4</span>, leaningTower, <span class="nu0">2</span>.0f, <span class="nu0">1</span>.0f<span class="br0">&#41;</span>;</div>
<p>SPG_TranslatePoints moves all the points in a polygon.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_TranslatePoints<span class="br0">&#40;</span><span class="nu0">4</span>, myBox, <span class="nu0">200</span>, <span class="nu0">400</span><span class="br0">&#41;</span>;</div>
<p><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Those Controls are Stacked!<a name="Controls"></a></span></div>
<p>Several options are controlled through a stack system.  When you change an option, you push a new value onto the stack.  When you&#8217;re done, you pop the stack, returning it to the old state.  In this way, you can write a block of code or a function that changes the state to achieve a particular effect, then returns it to the old state for the rest of the program to use.</p>
<p>There are three stacks held by Sprig.  The thickness stack controls the thickness of any line primitives that are drawn (default: 1).  The blending stack determines the special blending mode for alpha-blended primitives and SPG_Blit (default: SPG_DEST_ALPHA).  The anti-alias stack controls whether graphics primitives are anti-aliased (smoothed) or not (default: 0).  The surface alpha stack controls whether or not SPG_Blit combines the per-surface alpha with the per-pixel alpha (default: 0).</p>
<table style="text-align: left; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr>
<td>void</td>
<td>SPG_PushThickness(Uint16 state)</td>
</tr>
<tr>
<td>Uint16</td>
<td>SPG_PopThickness()</td>
</tr>
<tr>
<td>void</td>
<td>SPG_PushBlend(Uint8 state)</td>
</tr>
<tr>
<td>Uint8</td>
<td>SPG_PopBlend()</td>
</tr>
<tr>
<td>void</td>
<td>SPG_PushAA(SPG_bool state)</td>
</tr>
<tr>
<td>SPG_bool</td>
<td>SPG_PopAA()</td>
</tr>
<tr>
<td>void</td>
<td>SPG_PushSurfaceAlpha(SPG_bool state)</td>
</tr>
<tr>
<td>SPG_bool</td>
<td>SPG_PopSurfaceAlpha()</td>
</tr>
</tbody>
</table>
<p>Here&#8217;s a table of the available blending modes:</p>
<table style="text-align: left; margin-right: auto; margin-left: auto; width: auto;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr style="text-align: center">
<td><span>Name</span></td>
<td><span>RGB</span></td>
<td><span>Alpha</span></td>
<td>Description</td>
</tr>
<tr>
<td><span>SPG_DEST_ALPHA</span></td>
<td><span>Blend</span></td>
<td><span>Dest</span></td>
<td>Blends colors normally and keeps the destination per-pixel alpha.  This is the same as SDL_BlitSurface&#8217;s blending.</td>
</tr>
<tr>
<td><span>SPG_SRC_ALPHA</span></td>
<td><span>Blend</span></td>
<td><span>Src</span></td>
<td>Blends colors and copies the source alpha.</td>
</tr>
<tr>
<td><span>SPG_COMBINE_ALPHA</span></td>
<td><span>Blend</span></td>
<td><span>Blend</span></td>
<td>Blends colors and blends the alpha.  This is &#8220;True&#8221; blending and lends well to some neat compositing effects.</td>
</tr>
<tr>
<td><span>SPG_COPY_NO_ALPHA</span></td>
<td><span>Src</span></td>
<td><span>Opaque</span></td>
<td>Copies the source color, sets the dest alpha to opaque.</td>
</tr>
<tr>
<td><span>SPG_COPY_DEST_ALPHA</span></td>
<td><span>Src</span></td>
<td><span>Dest</span></td>
<td>Copies the source color, keeps the dest alpha.</td>
</tr>
<tr>
<td><span>SPG_COPY_SRC_ALPHA</span></td>
<td><span>Src</span></td>
<td><span>Src</span></td>
<td>Copies the source color and alpha.</td>
</tr>
<tr>
<td><span>SPG_COPY_COMBINE_ALPHA</span></td>
<td><span>Src</span></td>
<td><span>Blend</span></td>
<td>Copies the source color and blends the alpha.</td>
</tr>
<tr>
<td><span>SPG_COPY_ALPHA_ONLY</span></td>
<td>Dest</td>
<td>Src</td>
<td>Keeps the dest color, but copies the source alpha.</td>
</tr>
<tr>
<td>SPG_COMBINE_ALPHA_ONLY</td>
<td>Dest</td>
<td>Blend</td>
<td>Keeps the dest color, but blends the alpha.</td>
</tr>
<tr>
<td>SPG_REPLACE_COLORKEY</td>
<td>Src</td>
<td>Src</td>
<td>If the destination surface has the SDL_SRCCOLORKEY flag, this replaces the dest colorkey color in the image with the source color and alpha.  This is similar to palette-swapping, but can be used with gradients and other images.</td>
</tr>
</tbody>
</table>
<p>Some examples:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_PushBlend<span class="br0">&#40;</span>SPG_COMBINE_ALPHA<span class="br0">&#41;</span>;<br />
SPG_PushAA<span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>;<br />
SPG_PushThickness<span class="br0">&#40;</span><span class="nu0">5</span><span class="br0">&#41;</span>;</p>
<p>SPG_PopThickness<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
SPG_PopAA<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
SPG_PopBlend<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
<p><br/><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">All the Pi in the World&#8230;<a name="Angles"></a></span></div>
<p>Sprig can work with both degree angle measure and radians.  By default, all functions use the more common (in everyday life) units of degrees.  You can change this behavior with a call like:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_EnableRadians<span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>;</div>
<p>This avoids one floating-point multiplication in some of Sprig&#8217;s internal conversions (Sprig uses radians internally).  To make radian measure easier to use, there are several #defines involving pi:<br />
PI_8, PI_4, PI_2, PI3_4, PI, PI5_4, PI3_2, PI7_4, PI2</p>
<p>A number after &#8216;PI&#8217; indicates multiplication, and an underscore means division.  So PI3_2 is &#8220;Three halves times pi&#8221; or 1.5*pi.  An amount equal to pi radians is equivalent to 180 degrees.  Every quarter value of pi (45 degree increments) up to 2*pi (360 degrees) is provided.  Sprig also provides conversion factors for converting between degrees and radians:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw4">float</span> radians = degrees * RADPERDEG;</div>
<p>or</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw4">float</span> degrees = radians * DEGPERRAD;</div>
<p>These #defines are named after the unit conversion, which is a fraction.  The measure that you&#8217;re converting to will be in the numerator (top), with the old unit in the denominator (bottom).</p>
<div style="text-align: center;"><span style="text-decoration: underline;">More Than Meets the Eye<a name="Transformations"></a></span></div>
<p>Sprig transformation routines can scale, rotate, and flip SDL surfaces.</p>
<table style="text-align: left; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr>
<td>SDL_Surface*</td>
<td>SPG_Transform(SDL_Surface *src, Uint32 bcol, float angle, float xscale, float yscale, Uint8 flags)</td>
</tr>
<tr>
<td>SDL_Rect</td>
<td>SPG_TransformX(SDL_Surface *src, SDL_Surface *dst, float angle, float xscale, float yscale, Uint16 pivotX, Uint16 pivotY, Uint16 destX, Uint16 destY, Uint8 flags)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_Rotate(SDL_Surface *src, float angle, Uint32 bgColor)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_RotateAA(SDL_Surface *src, float angle, Uint32 bgColor)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_Scale(SDL_Surface* src, float xscale, float yscale)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_ScaleAA(SDL_Surface* src, float xscale, float yscale)</td>
</tr>
</tbody>
</table>
<p>Anti-aliasing is handled a little differently for the transform functions than for primitives.  This is because they operate on a different level: surfaces rather than shapes.  You don&#8217;t usually want your choices for one to affect your results for the other.  For this reason, SPG_Transform and SPG_TransformX use bit flags to control their behavior:<br />
SPG_NONE &#8211; Normal, same as 0.<br />
SPG_TAA &#8211; Anti-aliasing (no more jaggies)<br />
SPG_TSAFE &#8211; Makes incompatible surface depths work right<br />
SPG_TTMAP &#8211; Uses texture mapping (SPG_QuadTex) for a faster, but uglier result<br />
SPG_TSLOW &#8211; Uses a slow transformation, but is very accurate<br />
SPG_TCOLORKEY &#8211; Ignores the colorkey value on the destination surface<br />
SPG_TBLEND &#8211; Alpha-blend on the destination surface<br />
SPG_TSURFACE_ALPHA &#8211; Blend with the per-surface alpha</p>
<p>SPG_Rotate and SPG_Scale are convenience functions that call SPG_TransformX.  So, to simply rotate an image:</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Surface* rotated = SPG_Rotate<span class="br0">&#40;</span>myImage, <span class="nu0">60</span>, RGB_BLACK<span class="br0">&#40;</span>myImage<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
<p>C++ users can omit the background color argument (defaults to 0&#215;000000).</p>
<p>To scale a surface:</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Surface* scaled = SPG_Scale<span class="br0">&#40;</span>myImage, <span class="nu0">0</span>.5f, <span class="nu0">0</span>.5f<span class="br0">&#41;</span>; &nbsp;<span class="co1">// Quarter the size (half each dimension)</span></div>
<p>To flip a surface horizontally:</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Surface* flipped = SPG_Scale<span class="br0">&#40;</span>myImage, <span class="nu0">-1</span>, <span class="nu0">1</span><span class="br0">&#41;</span>;</div>
<p>SPG_TransformX gives you the most control over your surfaces.  Let&#8217;s look at it closely:</p>
<table style="text-align: left; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody>
<tr>
<td>SDL_Rect SPG_TransformX(SDL_Surface *src, SDL_Surface *dst, float angle, float xscale, float yscale, Uint16 pivotX, Uint16 pivotY, Uint16 destX, Uint16 destY, Uint8 flags);</td>
</tr>
</tbody>
</table>
<p>This function takes a surface, transforms it, and draws it onto another surface.  The first two arguments are the source surface and the destination surface.  The third argument is a rotation angle.  The next two are scaling factors.  The sixth and seventh are pivot point coordinates.  These determine the point about which the surface is to be rotated.  The eighth and ninth arguments are the drawing destination coordinates.  The last is a bitwise OR &#8216;|&#8217; combination of transformation flags.</p>
<div style="text-align: center;"><span style="text-decoration: underline;">Draw Me Close&#8230;<a name="Drawing"></a></span></div>
<p>Sprig has plenty of handy drawing functions.  These give you quick control over SDL surfaces.  The ones you should take note of are:</p>
<table style="text-align: left; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr>
<td>Uint32</td>
<td>SPG_GetPixel(SDL_Surface* surface, Sint16 x, Sint16 y)</td>
</tr>
<tr>
<td>Uint32</td>
<td>SPG_ConvertColor(SDL_PixelFormat* srcfmt, Uint32 srccolor, SDL_PixelFormat* destfmt)</td>
</tr>
<tr>
<td>SDL_Rect</td>
<td>SPG_MakeRect(Sint16 x, Sint16 y, Uint16 w, Uint16 h)</td>
</tr>
<tr>
<td>int</td>
<td>SPG_Blit(SDL_Surface* Src, SDL_Rect* srcRect, SDL_Surface* Dest, SDL_Rect* destRect)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_ReplaceColor(SDL_Surface* src, SDL_Rect* srcrect, SDL_Surface* dest, SDL_Rect* destrect, Uint32 color)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_Draw(SDL_Surface* source, SDL_Surface* dest, Sint16 x, Sint16 y)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DrawCenter(SDL_Surface* source, SDL_Surface* dest, Sint16 x, Sint16 y)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_FloodFill(SDL_Surface* dst, Sint16 x, Sint16 y, Uint32 color)</td>
</tr>
</tbody>
</table>
<p>SPG_GetPixel returns the color of a target pixel, like so:</p>
<div class="dean_ch" style="white-space: wrap;">Uint32 color = SPG_GetPixel<span class="br0">&#40;</span>mySurface, x, y<span class="br0">&#41;</span>;</div>
<p>Use SPG_ConvertColor to change a color from one format to another:</p>
<div class="dean_ch" style="white-space: wrap;">Uint32 screenBrown = SPG_ConvertColor<span class="br0">&#40;</span>mySurf-&gt;format, myBrown, screen-&gt;format<span class="br0">&#41;</span>;</div>
<p>In many functions, like SDL_BlitSurface, you use an SDL_Rect to define the source pixels or to indicate the drawing location.  SPG_MakeRect can be used to make rects for such use:</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Rect rect = SPG_MakeRect<span class="br0">&#40;</span><span class="nu0">40</span>, <span class="nu0">30</span>, <span class="nu0">60</span>, <span class="nu0">60</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// A 60&#215;60 rect at the location (40,30)</span></div>
<p>SPG_Blit draws an image using Sprig&#8217;s custom blitter.   It&#8217;s pretty slow, but allows you to use several alpha-blending modes.</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Rect location = <span class="br0">&#123;</span><span class="nu0">50</span>, <span class="nu0">50</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#125;</span>;<br />
SPG_Blit<span class="br0">&#40;</span>mySource, <span class="kw2">NULL</span>, myDest, &amp;location<span class="br0">&#41;</span>;</div>
<p>SPG_ReplaceColor lets you blit an image only to specific colored areas on the destination surface.  This is an effect similar to the palette-swapping that tons of old 8-bit games used to make identical enemies of different colors.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_ReplaceColor<span class="br0">&#40;</span>mySource, <span class="kw2">NULL</span>, myDest, <span class="kw2">NULL</span>, color<span class="br0">&#41;</span>;</div>
<p>SPG_Draw is a quick wrapper call to SDL_BlitSurface.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Draw<span class="br0">&#40;</span>mySurf, screen, x, y<span class="br0">&#41;</span>;</div>
<p>SPG_DrawCenter is just like SPG_Draw except that it uses the width and height of the source surface in order to draw it centered on the given coordinates.</p>
<p>SPG_FloodFill fills in the target area (all pixels of the same color) with the given color.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_FloodFill<span class="br0">&#40;</span>mySurf, x, y, color<span class="br0">&#41;</span>;</div>
<p><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Those Low-down Dirty Rects!<a name="Dirty"></a></span></div>
<p><br/><br />
<img alt="dirty rects" src="http://code.bluedinosaurs.com/SDL/SPriG/screenshots/dirty.png" title="dirty rects" class="aligncenter" width="400" height="300" /><br />
If a game is changing the entire screen every frame, it makes sense to update the entire screen.  But if you&#8217;re only moving a few sprites on a static background, then a huge time savings can be made by updating only the &#8220;dirty&#8221; parts.  Sprig includes a dirty rect system to make this easy to do.  David Olofson&#8217;s code from Fixed Rate Pig (<a href="http://olofson.net/">http://olofson.net/</a>) turned out to be a great and simple implementation, so he deserves all the credit for what ended up in Sprig.</p>
<p>The complete Dirty system consists of these calls:</p>
<table style="text-align: left; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr>
<td>void</td>
<td>SPG_EnableDirty(SPG_bool enable)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyInit(Uint16 maxsize)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyAdd(SDL_Rect* rect)</td>
</tr>
<tr>
<td>SPG_DirtyTable*</td>
<td>SPG_DirtyUpdate(SDL_Surface* screen)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtySwap()</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>SPG_bool</td>
<td>SPG_DirtyEnabled()</td>
</tr>
<tr>
<td>SPG_DirtyTable*</td>
<td>SPG_DirtyMake(Uint16 maxsize)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyAddTo(SDL_Rect* rect, SPG_DirtyTable* table)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyFree(SPG_DirtyTable* table)</td>
</tr>
<tr>
<td>SPG_DirtyTable*</td>
<td>SPG_DirtyGet()</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyClear(SPG_DirtyTable* table)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyLevel(Uint16 optimizationLevel)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyClip(SDL_Surface* screen, SDL_Rect* rect)</td>
</tr>
</tbody>
</table>
<p>The first group up there are the important ones.  To start, call:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_EnableDirty<span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>;</div>
<p>This tells all of the primitives to output dirty rects.  These rects are collected by the &#8216;front&#8217; dirty table.  There are two of these tables, the &#8216;front&#8217; and the &#8216;back&#8217;.  Everything happens on the front table and the back table is used to save old dirty rects for an extra frame.  This is necessary since you must update the new position of a sprite to show where it is, but also the old position in order to show empty space where it once was.  The definition of the tables looks like this:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw4">typedef</span> <span class="kw4">struct</span> SPG_DirtyTable<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Uint16 size; &nbsp;<span class="coMULTI">/* Table size */</span><br />
&nbsp; &nbsp; SDL_Rect *rects; &nbsp;<span class="coMULTI">/* Table of rects */</span><br />
&nbsp; &nbsp; Uint16 count; <span class="coMULTI">/* # of rects currently used */</span><br />
&nbsp; &nbsp; Uint16 best; &nbsp;<span class="coMULTI">/* Merge testing starts here! */</span><br />
<span class="br0">&#125;</span> SPG_DirtyTable;</div>
<p>You will use the count to loop over the rects when redrawing your background.</p>
<p>Next, call:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtyInit<span class="br0">&#40;</span><span class="nu0">64</span><span class="br0">&#41;</span>;</div>
<p>This initializes the front and back tables to contain a maximum of 64 dirty rects.  When it reaches that limit, some rects will be combined as more are added.  Now you&#8217;re ready to enter your main drawing loop.  Whenever you draw something to the screen, you need to collect its dirty rect.  If you use SDL_BlitSurface(), then this is pretty easy:</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Rect dirty;<br />
dirty.<span class="me1">x</span> = myX;<br />
dirty.<span class="me1">y</span> = myY;<br />
SDL_BlitSurface<span class="br0">&#40;</span>myPic, <span class="kw2">NULL</span>, screen, &amp;dirty<span class="br0">&#41;</span>;<br />
SPG_DirtyAdd<span class="br0">&#40;</span>&amp;dirty<span class="br0">&#41;</span>;</div>
<p>SDL_BlitSurface will write the actual dirty rect into &#8216;dirty&#8217;.  SPG_DirtyAdd() puts the given rect into the front table and does any necessary merging.  You must be sure that the rects that you add to the front table are properly clipped to the screen.  If not, the internal call to SDL_UpdateRects() will crash.  If you&#8217;re not sure about it, call:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtyClip<span class="br0">&#40;</span>screen, &amp;dirty<span class="br0">&#41;</span>;</div>
<p>This will make sure that your rect is safe to add.  Any calls to Sprig primitives and blits will automatically do all of this.</p>
<p>When you&#8217;re all done adding dirty rects (you&#8217;re done drawing to the screen), make this call:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtyTable* table = SPG_DirtyUpdate<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>;</div>
<p>Now you&#8217;ve updated the screen, just as if you had called SDL_Flip(screen), and you receive a pointer to the front table.  This has all of the newly updated rects in it for you to redraw the screen&#8217;s background:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">// This is for a full-screen background image</span><br />
<span class="kw4">int</span> i;<br />
SDL_Rect dest;<br />
<span class="kw1">for</span><span class="br0">&#40;</span>i = <span class="nu0">0</span>; i &lt; table-&gt;count; i++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; dest = table-&gt;rects<span class="br0">&#91;</span>i<span class="br0">&#93;</span>; &nbsp;<span class="co1">// SDL_BlitSurface is destructive to your rects</span><br />
&nbsp; &nbsp; SDL_BlitSurface<span class="br0">&#40;</span>myBackground, &amp;<span class="br0">&#40;</span>table-&gt;rects<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span>, screen, &amp;dest<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>The screen is all set for more drawing.  Now we have to swap the tables:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtySwap<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
<p>This clears and switches the tables so that they are ready for the next frame.</p>
<p>The rest of the functions are for if you want to do fancy things with the dirty rects.</p>
<p>You can make your own table with SPG_DirtyMake.  You can add rects to it with SPG_DirtyAddTo and free it with SPG_DirtyFree when you&#8217;re done.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtyTable* table = SPG_DirtyMake<span class="br0">&#40;</span><span class="nu0">128</span><span class="br0">&#41;</span>;<br />
SDL_Rect rect = <span class="br0">&#123;</span><span class="nu0">30</span>, <span class="nu0">40</span>, <span class="nu0">10</span>, <span class="nu0">10</span><span class="br0">&#125;</span>;<br />
SPG_DirtyAddTo<span class="br0">&#40;</span>&amp;rect, table<span class="br0">&#41;</span>;<br />
SPG_DirtyFree<span class="br0">&#40;</span>table<span class="br0">&#41;</span>;</div>
<p>SPG_DirtyEnabled tells you if dirty rects will be automatically generated.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_bool gettingDirty = SPG_DirtyEnabled<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
<p>SPG_DirtyGet returns a pointer to the front table (just be aware that the tables are switched each time SPG_DirtySwap is called).</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtyTable* front = SPG_DirtyGet<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="co1">// Add stuff to it or whatever</span></div>
<p>SPG_DirtyClear will reset a given table, and you can adjust the constants in the dirty algorithm with SPG_DirtyLevel.  These constants determine the thresholds for merging rects.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtyClear<span class="br0">&#40;</span>table<span class="br0">&#41;</span>;<br />
SPG_DirtyLevel<span class="br0">&#40;</span><span class="nu0">200</span><span class="br0">&#41;</span>;</div>
<p><br/><br/><br />
There you go.  By now you should have a feeling of what Sprig can do for you and what kinds of awesome games and applications you can write using it.  If you have any questions, suggestions, or contributions, drop me an email.</p>
<p>Jonny D</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/a-guide-to-graphics-with-sprig/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SDL Per Pixel Collision</title>
		<link>http://www.sdltutorials.com/sdl-per-pixel-collision/</link>
		<comments>http://www.sdltutorials.com/sdl-per-pixel-collision/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 21:52:40 +0000</pubDate>
		<dc:creator>Jason Norris</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=213</guid>
		<description><![CDATA[Are bounding box and circle collisions just not cutting it? Well, now we&#8217;re going to dive right down into pixel precision!

Before we start, I&#8217;d like to say that this is an advanced SDL tutorial. You should have experience with creating SDL applications and some basic knowledge of getting a program with it&#8217;s libraries up and [...]]]></description>
			<content:encoded><![CDATA[<p>Are bounding box and circle collisions just not cutting it? Well, now we&#8217;re going to dive right down into pixel precision!<br />
<span id="more-213"></span></p>
<p>Before we start, I&#8217;d like to say that this is an advanced SDL tutorial. You should have experience with creating SDL applications and some basic knowledge of getting a program with it&#8217;s libraries up and running.</p>
<p>First, we&#8217;ll start off with a very simple Game class to build the foundation of our demo project.</p>
<p><strong>Game.h</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#ifndef GAME_H</span><br />
<span class="co2">#define GAME_H</span></p>
<p><span class="co2">#include &quot;SDL.h&quot;</span></p>
<p><span class="kw2">class</span> Game<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">public</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; Game<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ~Game<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> OnExecute<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> OnThink<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> OnUpdate<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> OnRender<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw2">private</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> ScreenWidth;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> ScreenHeight;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> ScreenBPP;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Uint32 ScreenFlags;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">bool</span> done;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">double</span> thisTime;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">double</span> lastTime;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">double</span> deltaTime;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; SDL_Surface* screen;<br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Event event;<br />
<span class="br0">&#125;</span>;</p>
<p><span class="co2">#endif</span><br />
&nbsp;</div>
<p><strong>Game.cpp</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &quot;Game.h&quot;</span></p>
<p>Game::<span class="me2">Game</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; ScreenWidth = <span class="nu0">800</span>;<br />
&nbsp; &nbsp; ScreenHeight = <span class="nu0">600</span>;<br />
&nbsp; &nbsp; ScreenBPP = <span class="nu0">32</span>;<br />
&nbsp;&nbsp; &nbsp;ScreenFlags = SDL_HWSURFACE | SDL_DOUBLEBUF;</p>
<p>&nbsp; &nbsp; thisTime = <span class="nu0">0.0</span>;<br />
&nbsp; &nbsp; lastTime = <span class="nu0">0.0</span>;<br />
&nbsp; &nbsp; deltaTime = <span class="nu0">0.0</span>;</p>
<p>&nbsp; &nbsp; done = <span class="kw2">false</span>;</p>
<p>&nbsp; &nbsp; SDL_Init<span class="br0">&#40;</span>SDL_INIT_EVERYTHING<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; SDL_WM_SetCaption<span class="br0">&#40;</span><span class="st0">&quot;SDL Per Pixel Collision&quot;</span>, <span class="kw2">NULL</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; SDL_ShowCursor<span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; screen = SDL_SetVideoMode<span class="br0">&#40;</span>ScreenWidth, ScreenHeight, ScreenBPP, ScreenFlags<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>Game::~Game<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_Quit<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Game::<span class="me2">OnExecute</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span>!done<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; OnThink<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; OnUpdate<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; OnRender<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Game::<span class="me2">OnThink</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span>SDL_PollEvent<span class="br0">&#40;</span>&amp;event<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>event.<span class="me1">type</span> == SDL_QUIT<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; done = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>event.<span class="me1">type</span> == SDL_KEYDOWN<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>event.<span class="me1">key</span>.<span class="me1">keysym</span>.<span class="me1">sym</span> == SDLK_ESCAPE<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; done = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Game::<span class="me2">OnUpdate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; lastTime = thisTime;<br />
&nbsp; &nbsp; thisTime = SDL_GetTicks<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; deltaTime = <span class="br0">&#40;</span>thisTime &#8211; lastTime<span class="br0">&#41;</span> / <span class="nu0">1000.0</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Game::<span class="me2">OnRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_FillRect<span class="br0">&#40;</span>screen, <span class="kw2">NULL</span>, <span class="nu0">0</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; SDL_Flip<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="kw4">int</span> argc, <span class="kw4">char</span>* args<span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Game game;<br />
&nbsp; &nbsp; game.<span class="me1">OnExecute</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>You should be able to compile and run at this point. Now we have a nice 800&#215;600 window prepared for our bidding! The three <strong>double</strong> variables are used at keep track of elapsed time each tick. You can simply substitute in your favorite Timer class if you wish. I also turn off cursor visibility since we&#8217;ll be controlling an object with the mouse and we don&#8217;t want it to obscure our view.</p>
<p>Now we&#8217;ll need some sort of objects that we can collide together!</p>
<p><strong>Entity.h</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#ifndef ENTITY_H</span><br />
<span class="co2">#define ENTITY_H</span></p>
<p><span class="co2">#include &quot;SDL.h&quot;</span><br />
<span class="co2">#include &quot;SDL_image.h&quot;</span></p>
<p><span class="kw4">struct</span> Vector<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">float</span> X;<br />
&nbsp; &nbsp; <span class="kw4">float</span> Y;</p>
<p>&nbsp; &nbsp; Vector<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; X = <span class="nu0">0</span>.0f;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Y = <span class="nu0">0</span>.0f;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span>;</p>
<p><span class="kw2">class</span> Entity<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">public</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; Entity<span class="br0">&#40;</span><span class="kw4">char</span>* filename, <span class="kw4">int</span> tilesX, <span class="kw4">int</span> tilesY, <span class="kw4">double</span> frameRate<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ~Entity<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw2">public</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; Vector position;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Vector origin;</p>
<p>&nbsp; &nbsp; <span class="kw2">public</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Surface* surface;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> tilesX;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> tilesY;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> tileWidth;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> tileHeight;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> frame;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> frameCount;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">double</span> frameInterval;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">double</span> frameTimeRemaining;</p>
<p>&nbsp; &nbsp; <span class="kw2">public</span>:&nbsp;&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> Update<span class="br0">&#40;</span><span class="kw4">double</span> deltaTime<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> Render<span class="br0">&#40;</span>SDL_Surface* screen<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span>;</p>
<p><span class="co2">#endif</span><br />
&nbsp;</div>
<p>Simple enough, I hope. For each Entity we create, we&#8217;ll take in a filename for the for the image we&#8217;ll be loading. If you didn&#8217;t notice, SDL_image is included, as I&#8217;ll be working with PNG files with alpha transparency. Next, we take in the tiles across and tiles down in the spritesheet we&#8217;ll be using. Finally, a frame rate to determine how fast the sprite will animate.</p>
<p>Next, the member variables. I&#8217;ve created a simple Vector struct to hold position and origin information. Position is where the Entity will sit on the screen and origin is the center of the tile currently being drawn. This will make things simpler when we want to draw a sprite centered on it&#8217;s position. Of course, an SDL_Surface pointer to hold the image data we&#8217;ll be using. And last, variables to hold information about the spritesheet we&#8217;ll be using and handling animation.</p>
<p>Finally, two methods to update and render the given Entity. Now let&#8217;s implement these!</p>
<p><strong>Entity.cpp</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &quot;Entity.h&quot;</span></p>
<p>Entity::<span class="me2">Entity</span><span class="br0">&#40;</span><span class="kw4">char</span>* filename, <span class="kw4">int</span> tilesX, <span class="kw4">int</span> tilesY, <span class="kw4">double</span> frameRate<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; this-&gt;surface = IMG_Load<span class="br0">&#40;</span>filename<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; this-&gt;tilesX = tilesX;<br />
&nbsp; &nbsp; this-&gt;tilesY = tilesY;<br />
&nbsp; &nbsp; this-&gt;tileWidth = surface-&gt;w / tilesX;<br />
&nbsp; &nbsp; this-&gt;tileHeight = surface-&gt;h / tilesY;<br />
&nbsp; &nbsp; this-&gt;frame = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; this-&gt;frameCount = tilesX * tilesY;<br />
&nbsp; &nbsp; this-&gt;frameInterval = <span class="nu0">1</span> / frameRate;<br />
&nbsp; &nbsp; this-&gt;frameTimeRemaining = frameInterval;</p>
<p>&nbsp; &nbsp; this-&gt;origin.<span class="me1">X</span> = tileWidth / <span class="nu0">2</span>.0f;<br />
&nbsp; &nbsp; this-&gt;origin.<span class="me1">Y</span> = tileHeight / <span class="nu0">2</span>.0f;<br />
<span class="br0">&#125;</span></p>
<p>Entity::~Entity<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_FreeSurface<span class="br0">&#40;</span>surface<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Entity::<span class="me2">Update</span><span class="br0">&#40;</span><span class="kw4">double</span> deltaTime<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; frameTimeRemaining -= deltaTime;<br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>frameTimeRemaining &lt; <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; frame++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; frame %= frameCount;<br />
&nbsp; &nbsp; &nbsp; &nbsp; frameTimeRemaining = frameInterval;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Entity::<span class="me2">Render</span><span class="br0">&#40;</span>SDL_Surface* screen<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_Rect location = GetBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; <br />
&nbsp; &nbsp; SDL_Rect clip = GetFrameBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; SDL_BlitSurface<span class="br0">&#40;</span>surface, &amp;clip, screen, &amp;location<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>The constructor loads in our image using IMG_Load() and stores all our data about the sprite sheet. The destructor ensures we have no memory leaks by freeing our SDL_Surface. Update chips away at our frameTimeRemaining with a given deltaTime. Once we hit or are under zero, we increase the frame, wrap around to the beginning if need be, and reset our frameTimeRemaining with our given frameInterval. Render finds our location and clip rectangles and then blits the surface to the screen. But wait, we haven&#8217;t added GetBounds or GetFrameBounds() yet. I&#8217;m sure those will be useful to have! Let&#8217;s get to that!</p>
<p>Add these method definitions in:</p>
<p><strong>Entity.h</strong></p>
<div class="dean_ch" style="white-space: wrap;">
&nbsp; &nbsp; <span class="kw2">public</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Rect GetBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Rect GetFrameBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>And now we&#8217;ll implement them:</p>
<p><strong>Entity.cpp</strong></p>
<div class="dean_ch" style="white-space: wrap;">
SDL_Rect Entity::<span class="me2">GetBounds</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_Rect bounds;<br />
&nbsp; &nbsp; bounds.<span class="me1">x</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>position.<span class="me1">X</span> &#8211; origin.<span class="me1">X</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; bounds.<span class="me1">y</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>position.<span class="me1">Y</span> &#8211; origin.<span class="me1">Y</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; bounds.<span class="me1">w</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>tileWidth<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; bounds.<span class="me1">h</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>tileHeight<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> bounds;<br />
<span class="br0">&#125;</span></p>
<p>SDL_Rect Entity::<span class="me2">GetFrameBounds</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_Rect frameBounds;<br />
&nbsp; &nbsp; frameBounds.<span class="me1">x</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>frame % tilesX * tileWidth<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; frameBounds.<span class="me1">y</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>frame / tilesX * tileHeight<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; frameBounds.<span class="me1">w</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>tileWidth<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; frameBounds.<span class="me1">h</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>tileHeight<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">return</span> frameBounds;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>GetBounds() takes our position and shifts it over by our origin. This places the position at the center of the sprite, rather than the top left. GetFramesBound() checks what frame we&#8217;re on and finds the matching coordinates. Very important for animation!</p>
<p>Now lets slap a couple of these suckers into our demo!</p>
<p><strong>Game.h</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &quot;Entity.h&quot;</span></p>
<p><span class="kw2">class</span> Game<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &#8230;</p>
<p>&nbsp; &nbsp; <span class="kw2">private</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; Entity* ship;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Entity* asteroid;<br />
<span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p>Make sure you include Entity.h so we can actually use the class and we&#8217;ll add two Entity pointers to our Game class.</p>
<p><strong>Game.cpp</strong></p>
<div class="dean_ch" style="white-space: wrap;">
Game::<span class="me2">Game</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &#8230;</p>
<p>&nbsp; &nbsp; <span class="me1">ship</span> = <span class="kw3">new</span> Entity<span class="br0">&#40;</span><span class="st0">&quot;images/ship.png&quot;</span>, <span class="nu0">4</span>, <span class="nu0">3</span>, <span class="nu0">10</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; asteroid = <span class="kw3">new</span> Entity<span class="br0">&#40;</span><span class="st0">&quot;images/asteroid.png&quot;</span>, <span class="nu0">5</span>, <span class="nu0">6</span>, <span class="nu0">30</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; asteroid-&gt;position.<span class="me1">X</span> = <span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span><span class="br0">&#40;</span>ScreenWidth / <span class="nu0">2</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; asteroid-&gt;position.<span class="me1">Y</span> = <span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span><span class="br0">&#40;</span>ScreenHeight / <span class="nu0">2</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>Game::~Game<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">delete</span> ship;<br />
&nbsp; &nbsp; <span class="kw3">delete</span> asteroid;</p>
<p>&nbsp; &nbsp; &#8230;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Image: <a href="http://i49.tinypic.com/2zq6849.png">Ship</a><br />
Image: <a href="http://i45.tinypic.com/t4zs6q.png">Asteroid</a><br />
(Special thanks to 3DBuzz for these images.)</p>
<p>We&#8217;ll now dynamically allocate memory for our two new Entity objects. I have my images sitting in an &#8220;images&#8221; folder, but you may do whatever you want! I then position the asteroid at the center of the screen. And because we&#8217;re mindful programmers, we&#8217;ll delete the allocated memory once we&#8217;re all done.</p>
<p><strong>Game.cpp</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">void</span> Game::<span class="me2">OnThink</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span>SDL_PollEvent<span class="br0">&#40;</span>&amp;event<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>event.<span class="me1">type</span> == SDL_QUIT<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; done = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>event.<span class="me1">type</span> == SDL_KEYDOWN<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>event.<span class="me1">key</span>.<span class="me1">keysym</span>.<span class="me1">sym</span> == SDLK_ESCAPE<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; done = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>event.<span class="me1">type</span> == SDL_MOUSEMOTION<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship-&gt;position.<span class="me1">X</span> = event.<span class="me1">motion</span>.<span class="me1">x</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship-&gt;position.<span class="me1">Y</span> = event.<span class="me1">motion</span>.<span class="me1">y</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Game::<span class="me2">OnUpdate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; lastTime = thisTime;<br />
&nbsp; &nbsp; thisTime = SDL_GetTicks<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; deltaTime = <span class="br0">&#40;</span>thisTime &#8211; lastTime<span class="br0">&#41;</span> / <span class="nu0">1000.0</span>;</p>
<p>&nbsp; &nbsp; ship-&gt;Update<span class="br0">&#40;</span>deltaTime<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; asteroid-&gt;Update<span class="br0">&#40;</span>deltaTime<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Game::<span class="me2">OnRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_FillRect<span class="br0">&#40;</span>screen, <span class="kw2">NULL</span>, <span class="nu0">0</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; ship-&gt;Render<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; asteroid-&gt;Render<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; SDL_Flip<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>In OnThink, we&#8217;ll mouse movement to our ship by looking for an SDL_MOUSEMOTION event while polling for events. In OnUpdate, we&#8217;ll simply call each Entity&#8217;s Update method with the deltaTime we found. Lastly, in OnRender, we&#8217;ll just call the Render method by passing our screen.</p>
<p>Compile, run, and give it a whirl! Weeee! We now have two animated sprites, one centered in the screen and one controlled by our mouse. But where&#8217;s the collision? Oh yeah, we haven&#8217;t implemented it yet!</p>
<p>We&#8217;ll need a bit more functionally in our Entity class, so let&#8217;s work on that. This one&#8217;s a doozy, so hold on tight! But don&#8217;t worry, we&#8217;ll take it one step at a time&#8230;</p>
<p><strong>Entity.h</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">class</span> Entity<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &#8230;</p>
<p>&nbsp; &nbsp; <span class="kw2">public</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Rect NormalizeBounds<span class="br0">&#40;</span><span class="kw4">const</span> SDL_Rect&amp; rect<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> SDL_Rect Intersection<span class="br0">&#40;</span><span class="kw4">const</span> SDL_Rect&amp; boundsA, <span class="kw4">const</span> SDL_Rect&amp; boundsB<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">bool</span> CheckCollision<span class="br0">&#40;</span>Entity* entityA, Entity* entityB<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">bool</span> GetAlphaXY<span class="br0">&#40;</span>Entity* entity, <span class="kw4">int</span> x, <span class="kw4">int</span> y<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p><strong>Entity.cpp</strong></p>
<div class="dean_ch" style="white-space: wrap;">
SDL_Rect Entity::<span class="me2">NormalizeBounds</span><span class="br0">&#40;</span><span class="kw4">const</span> SDL_Rect&amp; rect<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_Rect normalized;<br />
&nbsp; &nbsp; normalized.<span class="me1">x</span> = rect.<span class="me1">x</span> &#8211; <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span>position.<span class="me1">X</span> + <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span>origin.<span class="me1">X</span> + GetFrameBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">x</span>;<br />
&nbsp; &nbsp; normalized.<span class="me1">y</span> = rect.<span class="me1">y</span> &#8211; <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span>position.<span class="me1">Y</span> + <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span>origin.<span class="me1">Y</span> + GetFrameBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">y</span>;<br />
&nbsp; &nbsp; normalized.<span class="me1">w</span> = rect.<span class="me1">w</span>;<br />
&nbsp; &nbsp; normalized.<span class="me1">h</span> = rect.<span class="me1">h</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">return</span> normalized;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Given the SDL_Rect of a collision rectangle, we want to find what pixels on the sprite sheet correlate to it. A little messy, but it gets the job done.</p>
<p><strong>Entity.cpp</strong></p>
<div class="dean_ch" style="white-space: wrap;">
SDL_Rect Entity::<span class="me2">Intersection</span><span class="br0">&#40;</span><span class="kw4">const</span> SDL_Rect&amp; boundsA, <span class="kw4">const</span> SDL_Rect&amp; boundsB<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> x1 = Maximum<span class="br0">&#40;</span>boundsA.<span class="me1">x</span>, boundsB.<span class="me1">x</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw4">int</span> y1 = Maximum<span class="br0">&#40;</span>boundsA.<span class="me1">y</span>, boundsB.<span class="me1">y</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw4">int</span> x2 = Minimum<span class="br0">&#40;</span>boundsA.<span class="me1">x</span> + boundsA.<span class="me1">w</span>, boundsB.<span class="me1">x</span> + boundsB.<span class="me1">w</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw4">int</span> y2 = Minimum<span class="br0">&#40;</span>boundsA.<span class="me1">y</span> + boundsA.<span class="me1">h</span>, boundsB.<span class="me1">y</span> + boundsB.<span class="me1">h</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw4">int</span> width = x2 &#8211; x1;<br />
&nbsp; &nbsp; <span class="kw4">int</span> height = y2 &#8211; y1;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>width &gt; <span class="nu0">0</span> &amp;&amp; height &gt; <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Rect intersect = <span class="br0">&#123;</span>x1, y1, width, height<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> intersect;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Rect intersect = <span class="br0">&#123;</span><span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> intersect;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Given the two bounds of two entities, we want to find how much they intersect, if at all. If they do, we&#8217;re given their intersection rectangle, if not, we&#8217;re given an empty rectangle. For Max and Min, you can either include &#8220;cmath&#8221; or use a couple macros such as this:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#define Maximum(a, b) ((a &gt; b) ? a : b)</span><br />
<span class="co2">#define Minimum(a, b) ((a &lt; b) ? a : b)</span><br />
&nbsp;</div>
<p><strong>Entity.cpp</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">bool</span> Entity::<span class="me2">CheckCollision</span><span class="br0">&#40;</span>Entity* entityA, Entity* entityB<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_Rect collisionRect = Intersection<span class="br0">&#40;</span>entityA-&gt;GetBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>, entityB-&gt;GetBounds<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>collisionRect.<span class="me1">w</span> == <span class="nu0">0</span> &amp;&amp; collisionRect.<span class="me1">h</span> == <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</p>
<p>&nbsp; &nbsp; SDL_Rect normalA = entityA-&gt;NormalizeBounds<span class="br0">&#40;</span>collisionRect<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; SDL_Rect normalB = entityB-&gt;NormalizeBounds<span class="br0">&#40;</span>collisionRect<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> y = <span class="nu0">0</span>; y &lt; collisionRect.<span class="me1">h</span>; y++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> x = <span class="nu0">0</span>; x &lt; collisionRect.<span class="me1">w</span>; x++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>GetAlphaXY<span class="br0">&#40;</span>entityA, normalA.<span class="me1">x</span> + x, normalA.<span class="me1">y</span> + y<span class="br0">&#41;</span> &amp;&amp; GetAlphaXY<span class="br0">&#40;</span>entityB, normalB.<span class="me1">x</span> + x, normalB.<span class="me1">y</span> + y<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>This is where bulk of our per pixel collision check happens. First, we ensure there&#8217;s even a bounding box collision. No use going through all the trouble when we know nothing will happen. Then we normalize the bounds of each entity with the found collision rectangle. This will give us the region of pixels we&#8217;ll be comparing against each other. You&#8217;ll notice this GetAlphaXY() function. This is where we&#8217;ll get down into the pixels and determine if they&#8217;re solid enough for collision. You&#8217;ll see that it&#8217;s called twice in one if-statement. This is because we&#8217;re searching for a solid enough pixel in each surface at the same position. Once that is found, we&#8217;ll confirm a hit!</p>
<p><strong>Entity.cpp</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">bool</span> Entity::<span class="me2">GetAlphaXY</span><span class="br0">&#40;</span>Entity* entity, <span class="kw4">int</span> x, <span class="kw4">int</span> y<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> bpp = entity-&gt;surface-&gt;format-&gt;BytesPerPixel;<br />
&nbsp; &nbsp; Uint8* p = <span class="br0">&#40;</span>Uint8*<span class="br0">&#41;</span>entity-&gt;surface-&gt;pixels + y * entity-&gt;surface-&gt;pitch + x * bpp;<br />
&nbsp; &nbsp; Uint32 pixelColor;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">switch</span><span class="br0">&#40;</span>bpp<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixelColor = *p;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixelColor = *<span class="br0">&#40;</span>Uint16*<span class="br0">&#41;</span>p;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span><span class="br0">&#40;</span><span class="nu0">3</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>SDL_BYTEORDER == SDL_BIG_ENDIAN<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixelColor = p<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> &lt;&lt; <span class="nu0">16</span> | p<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> &lt;&lt; <span class="nu0">8</span> | p<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixelColor = p<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> | p<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> &lt;&lt; <span class="nu0">8</span> | p<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> &lt;&lt; <span class="nu0">16</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span><span class="br0">&#40;</span><span class="nu0">4</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixelColor = *<span class="br0">&#40;</span>Uint32*<span class="br0">&#41;</span>p;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Uint8 red, green, blue, alpha;<br />
&nbsp; &nbsp; SDL_GetRGBA<span class="br0">&#40;</span>pixelColor, entity-&gt;surface-&gt;format, &amp;red, &amp;green, &amp;blue, &amp;alpha<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> alpha &gt; <span class="nu0">200</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Oh my, some crazy looking stuff here! It&#8217;s not that bad. Let me explain a few things here.</p>
<p>We&#8217;re passing in a pointer to an Entity and an (x,y) coordinate. All we&#8217;re doing in this function is finding the pixel at that coordinate and determining what colors its composed of. The first two lines give us a pointer to the pixel in question. The funky looking switch-statement is finding the color composition depending on the image&#8217;s bytes-per-pixel format, either 8-bit, 16-bit, 24-bit, or 32-bit. In our case, it will always land on case(4), but this should give you an idea on how to expand to other formats.</p>
<p>Finally, we&#8217;re grabbing the RGBA components of the pixel using SDL_GetRGBA. All we care about is alpha, but you can experiment with collision where hits will only happen when a pixel is red or blue enough. Alpha will range between 0 and 255. I wanted collision to happen with not just pixels that were completely opaque, but not too transparent. I found 200 to be a nice number, but you&#8217;re free to tweak as you please.</p>
<p>Well that&#8217;s it! Our Entity class has the functionally to test for per pixel collision&#8230; so lets show it off!</p>
<p> <strong>Game.cpp</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">void</span> Game::<span class="me2">OnRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_FillRect<span class="br0">&#40;</span>screen, <span class="kw2">NULL</span>, <span class="nu0">0</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; SDL_Rect collisionRect = Entity::<span class="me2">Intersection</span><span class="br0">&#40;</span>ship-&gt;GetBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>, asteroid-&gt;GetBounds<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>collisionRect.<span class="me1">w</span> != <span class="nu0">0</span> &amp;&amp; collisionRect.<span class="me1">h</span> != <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>Entity::<span class="me2">CheckCollision</span><span class="br0">&#40;</span>ship, asteroid<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SDL_FillRect<span class="br0">&#40;</span>screen, &amp;collisionRect, SDL_MapRGB<span class="br0">&#40;</span>screen-&gt;format, <span class="nu0">255</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SDL_FillRect<span class="br0">&#40;</span>screen, &amp;collisionRect, SDL_MapRGB<span class="br0">&#40;</span>screen-&gt;format, <span class="nu0">0</span>, <span class="nu0">255</span>, <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; ship-&gt;Render<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; asteroid-&gt;Render<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; SDL_Flip<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Adding this checks for collision between our ship and asteroid. When bounding box collision happens, a green box will be drawn behind them. When per pixel collision happens, the box will turn red!</p>
<p>Well, that&#8217;s about it. Run, compile, and enjoy! You now have per pixel collision checks between your two Entity objects! Feel free to tweak numbers, use different images, or control them in different ways! I&#8217;ve given you the foundation, now go out there and create some interesting implementations!</p>
<p>Here&#8217;s the final code in case you may have some errors and need to check against my own.</p>
<p><strong>Game.h</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#ifndef GAME_H</span><br />
<span class="co2">#define GAME_H</span></p>
<p><span class="co2">#include &quot;Entity.h&quot;</span></p>
<p><span class="kw2">class</span> Game<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">public</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; Game<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ~Game<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> OnExecute<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> OnThink<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> OnUpdate<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> OnRender<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw2">private</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> ScreenWidth;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> ScreenHeight;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> ScreenBPP;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Uint32 ScreenFlags;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">bool</span> done;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">double</span> thisTime;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">double</span> lastTime;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">double</span> deltaTime;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; SDL_Surface* screen;<br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Event event;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; Entity* ship;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Entity* asteroid;<br />
<span class="br0">&#125;</span>;</p>
<p><span class="co2">#endif</span><br />
&nbsp;</div>
<p><strong>Game.cpp</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &quot;Game.h&quot;</span></p>
<p>Game::<span class="me2">Game</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; ScreenWidth = <span class="nu0">800</span>;<br />
&nbsp; &nbsp; ScreenHeight = <span class="nu0">600</span>;<br />
&nbsp; &nbsp; ScreenBPP = <span class="nu0">32</span>;<br />
&nbsp; &nbsp; ScreenFlags = SDL_HWSURFACE | SDL_DOUBLEBUF;</p>
<p>&nbsp; &nbsp; done = <span class="kw2">false</span>;</p>
<p>&nbsp; &nbsp; thisTime = <span class="nu0">0.0</span>;<br />
&nbsp; &nbsp; lastTime = <span class="nu0">0.0</span>;<br />
&nbsp; &nbsp; deltaTime = <span class="nu0">0.0</span>;</p>
<p>&nbsp; &nbsp; SDL_Init<span class="br0">&#40;</span>SDL_INIT_EVERYTHING<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; SDL_WM_SetCaption<span class="br0">&#40;</span><span class="st0">&quot;SDL Per Pixel Collision&quot;</span>, <span class="kw2">NULL</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; SDL_ShowCursor<span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; screen = SDL_SetVideoMode<span class="br0">&#40;</span>ScreenWidth, ScreenHeight, ScreenBPP, ScreenFlags<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; ship = <span class="kw3">new</span> Entity<span class="br0">&#40;</span><span class="st0">&quot;images/ship.png&quot;</span>, <span class="nu0">4</span>, <span class="nu0">3</span>, <span class="nu0">10</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; asteroid = <span class="kw3">new</span> Entity<span class="br0">&#40;</span><span class="st0">&quot;images/asteroid.png&quot;</span>, <span class="nu0">5</span>, <span class="nu0">6</span>, <span class="nu0">30</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; asteroid-&gt;position.<span class="me1">X</span> = <span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span><span class="br0">&#40;</span>ScreenWidth / <span class="nu0">2</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; asteroid-&gt;position.<span class="me1">Y</span> = <span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span><span class="br0">&#40;</span>ScreenHeight / <span class="nu0">2</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>Game::~Game<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">delete</span> ship;<br />
&nbsp; &nbsp; <span class="kw3">delete</span> asteroid;</p>
<p>&nbsp; &nbsp; SDL_Quit<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Game::<span class="me2">OnExecute</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span>!done<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; OnThink<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; OnUpdate<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; OnRender<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Game::<span class="me2">OnThink</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span>SDL_PollEvent<span class="br0">&#40;</span>&amp;event<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>event.<span class="me1">type</span> == SDL_QUIT<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; done = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>event.<span class="me1">type</span> == SDL_KEYDOWN<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>event.<span class="me1">key</span>.<span class="me1">keysym</span>.<span class="me1">sym</span> == SDLK_ESCAPE<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; done = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>event.<span class="me1">type</span> == SDL_MOUSEMOTION<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship-&gt;position.<span class="me1">X</span> = event.<span class="me1">motion</span>.<span class="me1">x</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship-&gt;position.<span class="me1">Y</span> = event.<span class="me1">motion</span>.<span class="me1">y</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Game::<span class="me2">OnUpdate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; lastTime = thisTime;<br />
&nbsp; &nbsp; thisTime = SDL_GetTicks<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; deltaTime = <span class="br0">&#40;</span>thisTime &#8211; lastTime<span class="br0">&#41;</span> / <span class="nu0">1000.0</span>;</p>
<p>&nbsp; &nbsp; ship-&gt;Update<span class="br0">&#40;</span>deltaTime<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; asteroid-&gt;Update<span class="br0">&#40;</span>deltaTime<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Game::<span class="me2">OnRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_FillRect<span class="br0">&#40;</span>screen, <span class="kw2">NULL</span>, <span class="nu0">0</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>collisionRect.<span class="me1">w</span> != <span class="nu0">0</span> &amp;&amp; collisionRect.<span class="me1">h</span> != <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>Entity::<span class="me2">CheckCollision</span><span class="br0">&#40;</span>ship, asteroid<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SDL_FillRect<span class="br0">&#40;</span>screen, &amp;collisionRect, SDL_MapRGB<span class="br0">&#40;</span>screen-&gt;format, <span class="nu0">255</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SDL_FillRect<span class="br0">&#40;</span>screen, &amp;collisionRect, SDL_MapRGB<span class="br0">&#40;</span>screen-&gt;format, <span class="nu0">0</span>, <span class="nu0">255</span>, <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; ship-&gt;Render<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; asteroid-&gt;Render<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; SDL_Flip<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="kw4">int</span> argc, <span class="kw4">char</span>* args<span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Game game;<br />
&nbsp; &nbsp; game.<span class="me1">OnExecute</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span></p>
<p>&nbsp;</p></div>
<p><strong>Entity.h</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#ifndef ENTITY_H</span><br />
<span class="co2">#define ENTITY_H</span></p>
<p><span class="co2">#include &quot;SDL.h&quot;</span><br />
<span class="co2">#include &quot;SDL_image.h&quot;</span></p>
<p><span class="kw4">struct</span> Vector<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">float</span> X;<br />
&nbsp; &nbsp; <span class="kw4">float</span> Y;</p>
<p>&nbsp; &nbsp; Vector<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; X = <span class="nu0">0</span>.0f;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Y = <span class="nu0">0</span>.0f;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span>;</p>
<p><span class="kw2">class</span> Entity<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">public</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; Entity<span class="br0">&#40;</span><span class="kw4">char</span>* filename, <span class="kw4">int</span> tilesX, <span class="kw4">int</span> tilesY, <span class="kw4">double</span> frameRate<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ~Entity<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw2">public</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; Vector position;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Vector origin;</p>
<p>&nbsp; &nbsp; <span class="kw2">public</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Surface* surface;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> tilesX;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> tilesY;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> tileWidth;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> tileHeight;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> frame;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> frameCount;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">double</span> frameInterval;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">double</span> frameTimeRemaining;</p>
<p>&nbsp; &nbsp; <span class="kw2">public</span>:&nbsp;&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> Update<span class="br0">&#40;</span><span class="kw4">double</span> deltaTime<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> Render<span class="br0">&#40;</span>SDL_Surface* screen<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw2">public</span>:&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Rect GetBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Rect GetFrameBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Rect NormalizeBounds<span class="br0">&#40;</span><span class="kw4">const</span> SDL_Rect&amp; rect<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> SDL_Rect Intersection<span class="br0">&#40;</span><span class="kw4">const</span> SDL_Rect&amp; boundsA, <span class="kw4">const</span> SDL_Rect&amp; boundsB<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">bool</span> CheckCollision<span class="br0">&#40;</span>Entity* entityA, Entity* entityB<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">bool</span> GetAlphaXY<span class="br0">&#40;</span>Entity* entity, <span class="kw4">int</span> x, <span class="kw4">int</span> y<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span>;</p>
<p><span class="co2">#endif</span><br />
&nbsp;</div>
<p><strong>Entity.cpp</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &quot;Entity.h&quot;</span></p>
<p><span class="co2">#define Maximum(a, b) ((a &gt; b) ? a : b)</span><br />
<span class="co2">#define Minimum(a, b) ((a &lt; b) ? a : b)</span></p>
<p>Entity::<span class="me2">Entity</span><span class="br0">&#40;</span><span class="kw4">char</span>* filename, <span class="kw4">int</span> tilesX, <span class="kw4">int</span> tilesY, <span class="kw4">double</span> frameRate<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; this-&gt;surface = IMG_Load<span class="br0">&#40;</span>filename<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; this-&gt;tilesX = tilesX;<br />
&nbsp; &nbsp; this-&gt;tilesY = tilesY;<br />
&nbsp; &nbsp; this-&gt;tileWidth = surface-&gt;w / tilesX;<br />
&nbsp; &nbsp; this-&gt;tileHeight = surface-&gt;h / tilesY;<br />
&nbsp; &nbsp; this-&gt;frame = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; this-&gt;frameCount = tilesX * tilesY;<br />
&nbsp; &nbsp; this-&gt;frameInterval = <span class="nu0">1</span> / frameRate;<br />
&nbsp; &nbsp; this-&gt;frameTimeRemaining = frameInterval;</p>
<p>&nbsp; &nbsp; this-&gt;origin.<span class="me1">X</span> = tileWidth / <span class="nu0">2</span>.0f;<br />
&nbsp; &nbsp; this-&gt;origin.<span class="me1">Y</span> = tileHeight / <span class="nu0">2</span>.0f;<br />
<span class="br0">&#125;</span></p>
<p>Entity::~Entity<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_FreeSurface<span class="br0">&#40;</span>surface<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Entity::<span class="me2">Update</span><span class="br0">&#40;</span><span class="kw4">double</span> deltaTime<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; frameTimeRemaining -= deltaTime;<br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>frameTimeRemaining &lt; <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; frame++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; frame %= frameCount;<br />
&nbsp; &nbsp; &nbsp; &nbsp; frameTimeRemaining = frameInterval;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> Entity::<span class="me2">Render</span><span class="br0">&#40;</span>SDL_Surface* screen<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_Rect location = GetBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; <br />
&nbsp; &nbsp; SDL_Rect clip = GetFrameBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; SDL_BlitSurface<span class="br0">&#40;</span>surface, &amp;clip, screen, &amp;location<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>SDL_Rect Entity::<span class="me2">GetBounds</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_Rect bounds;<br />
&nbsp; &nbsp; bounds.<span class="me1">x</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>position.<span class="me1">X</span> &#8211; origin.<span class="me1">X</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; bounds.<span class="me1">y</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>position.<span class="me1">Y</span> &#8211; origin.<span class="me1">Y</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; bounds.<span class="me1">w</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>tileWidth<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; bounds.<span class="me1">h</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>tileHeight<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> bounds;<br />
<span class="br0">&#125;</span></p>
<p>SDL_Rect Entity::<span class="me2">GetFrameBounds</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_Rect frameBounds;<br />
&nbsp; &nbsp; frameBounds.<span class="me1">x</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>frame % tilesX * tileWidth<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; frameBounds.<span class="me1">y</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>frame / tilesX * tileHeight<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; frameBounds.<span class="me1">w</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>tileWidth<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; frameBounds.<span class="me1">h</span> = <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span><span class="br0">&#40;</span>tileHeight<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">return</span> frameBounds;<br />
<span class="br0">&#125;</span></p>
<p>SDL_Rect Entity::<span class="me2">NormalizeBounds</span><span class="br0">&#40;</span><span class="kw4">const</span> SDL_Rect&amp; rect<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_Rect normalized;<br />
&nbsp; &nbsp; normalized.<span class="me1">x</span> = rect.<span class="me1">x</span> &#8211; <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span>position.<span class="me1">X</span> + <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span>origin.<span class="me1">X</span> + GetFrameBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">x</span>;<br />
&nbsp; &nbsp; normalized.<span class="me1">y</span> = rect.<span class="me1">y</span> &#8211; <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span>position.<span class="me1">Y</span> + <span class="br0">&#40;</span>Sint16<span class="br0">&#41;</span>origin.<span class="me1">Y</span> + GetFrameBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">y</span>;<br />
&nbsp; &nbsp; normalized.<span class="me1">w</span> = rect.<span class="me1">w</span>;<br />
&nbsp; &nbsp; normalized.<span class="me1">h</span> = rect.<span class="me1">h</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">return</span> normalized;<br />
<span class="br0">&#125;</span></p>
<p>SDL_Rect Entity::<span class="me2">Intersection</span><span class="br0">&#40;</span><span class="kw4">const</span> SDL_Rect&amp; boundsA, <span class="kw4">const</span> SDL_Rect&amp; boundsB<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> x1 = Maximum<span class="br0">&#40;</span>boundsA.<span class="me1">x</span>, boundsB.<span class="me1">x</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw4">int</span> y1 = Maximum<span class="br0">&#40;</span>boundsA.<span class="me1">y</span>, boundsB.<span class="me1">y</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw4">int</span> x2 = Minimum<span class="br0">&#40;</span>boundsA.<span class="me1">x</span> + boundsA.<span class="me1">w</span>, boundsB.<span class="me1">x</span> + boundsB.<span class="me1">w</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw4">int</span> y2 = Minimum<span class="br0">&#40;</span>boundsA.<span class="me1">y</span> + boundsA.<span class="me1">h</span>, boundsB.<span class="me1">y</span> + boundsB.<span class="me1">h</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw4">int</span> width = x2 &#8211; x1;<br />
&nbsp; &nbsp; <span class="kw4">int</span> height = y2 &#8211; y1;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>width &gt; <span class="nu0">0</span> &amp;&amp; height &gt; <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Rect intersect = <span class="br0">&#123;</span>x1, y1, width, height<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> intersect;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Rect intersect = <span class="br0">&#123;</span><span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> intersect;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">bool</span> Entity::<span class="me2">CheckCollision</span><span class="br0">&#40;</span>Entity* entityA, Entity* entityB<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDL_Rect collisionRect = Intersection<span class="br0">&#40;</span>entityA-&gt;GetBounds<span class="br0">&#40;</span><span class="br0">&#41;</span>, entityB-&gt;GetBounds<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>collisionRect.<span class="me1">w</span> == <span class="nu0">0</span> &amp;&amp; collisionRect.<span class="me1">h</span> == <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</p>
<p>&nbsp; &nbsp; SDL_Rect normalA = entityA-&gt;NormalizeBounds<span class="br0">&#40;</span>collisionRect<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; SDL_Rect normalB = entityB-&gt;NormalizeBounds<span class="br0">&#40;</span>collisionRect<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> y = <span class="nu0">0</span>; y &lt; collisionRect.<span class="me1">h</span>; y++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> x = <span class="nu0">0</span>; x &lt; collisionRect.<span class="me1">w</span>; x++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>GetAlphaXY<span class="br0">&#40;</span>entityA, normalA.<span class="me1">x</span> + x, normalA.<span class="me1">y</span> + y<span class="br0">&#41;</span> &amp;&amp; GetAlphaXY<span class="br0">&#40;</span>entityB, normalB.<span class="me1">x</span> + x, normalB.<span class="me1">y</span> + y<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">bool</span> Entity::<span class="me2">GetAlphaXY</span><span class="br0">&#40;</span>Entity* entity, <span class="kw4">int</span> x, <span class="kw4">int</span> y<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> bpp = entity-&gt;surface-&gt;format-&gt;BytesPerPixel;<br />
&nbsp; &nbsp; Uint8* p = <span class="br0">&#40;</span>Uint8*<span class="br0">&#41;</span>entity-&gt;surface-&gt;pixels + y * entity-&gt;surface-&gt;pitch + x * bpp;<br />
&nbsp; &nbsp; Uint32 pixelColor;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">switch</span><span class="br0">&#40;</span>bpp<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixelColor = *p;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixelColor = *<span class="br0">&#40;</span>Uint16*<span class="br0">&#41;</span>p;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span><span class="br0">&#40;</span><span class="nu0">3</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>SDL_BYTEORDER == SDL_BIG_ENDIAN<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixelColor = p<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> &lt;&lt; <span class="nu0">16</span> | p<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> &lt;&lt; <span class="nu0">8</span> | p<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixelColor = p<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> | p<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> &lt;&lt; <span class="nu0">8</span> | p<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> &lt;&lt; <span class="nu0">16</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span><span class="br0">&#40;</span><span class="nu0">4</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixelColor = *<span class="br0">&#40;</span>Uint32*<span class="br0">&#41;</span>p;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Uint8 red, green, blue, alpha;<br />
&nbsp; &nbsp; SDL_GetRGBA<span class="br0">&#40;</span>pixelColor, entity-&gt;surface-&gt;format, &amp;red, &amp;green, &amp;blue, &amp;alpha<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> alpha &gt; <span class="nu0">200</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>I hope you enjoyed my first tutorial for SDLTutorials.com and learned a lot! I plan to do more in the future!</p>
<p><strong>SDL Per Pixel Collision – Tutorial Files:</strong><br />
<strong>Win32:</strong> <a href="http://www.megaupload.com/?d=DISXV67Q">RAR</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/sdl-per-pixel-collision/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>New Wordpress Theme</title>
		<link>http://www.sdltutorials.com/new-wordpress-theme/</link>
		<comments>http://www.sdltutorials.com/new-wordpress-theme/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 19:51:00 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[C++ tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=210</guid>
		<description><![CDATA[We&#8217;re trying out a new wordpress theme in hopes that more content can fit on a page. In addition, I have turned on nested comments, comment pagination, and I am moving a bunch of things around. Let me know what you like, dislike, or just plain hate. 
Also, I encourage people to register for an [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re trying out a new wordpress theme in hopes that more content can fit on a page. In addition, I have turned on nested comments, comment pagination, and I am moving a bunch of things around. Let me know what you like, dislike, or just plain hate. </p>
<p>Also, I encourage people to register for an account as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/new-wordpress-theme/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MixBox Quick Guide</title>
		<link>http://www.sdltutorials.com/mixbox-quick-guide/</link>
		<comments>http://www.sdltutorials.com/mixbox-quick-guide/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 23:00:41 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[Blue Dino Code]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=205</guid>
		<description><![CDATA[The following tutorial was created by Blue Dino Code, for the purpose of helping people like you learn SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework [...]]]></description>
			<content:encoded><![CDATA[<p>The following tutorial was created by <a href="http://code.bluedinosaurs.com/">Blue Dino Code</a>, for the purpose of helping people like you learn SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework used by the author. SDLTutorials.com does not endorse or reject any of the coding practices outlined in the tutorial, and is not responsible for any code or files belonging to this tutorial that harms your computer.</p>
<hr/>
<p><b>Prerequisites:</b>  C++ classes and a small bit of SDL</p>
<p>MixBox is a set of C++ classes that makes it easy to add sound and music to your games.  The MixBox class is the main one.  It holds all the sound and music data and has functions that access just about everything you&#8217;d want.  The other classes, SoundID, MusicID, and ChannelID, are all subclasses of MixID.  Each of these classes has functions that roughly duplicate what the MixBox class can do for that type of object, but having them lets you choose how best to work MixBox into your game.</p>
<p><span id="more-205"></span></p>
<p>Typical usage of MixBox follows these steps:  Open the mixer, load a sound, close the mixer, then free the sound.<br />
Here&#8217;s a quick example of the simplest usage of MixBox.  You can grab the full example source and sample sounds here.  Link this program with SDLmain, SDL, and SDL_mixer:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &quot;SDL.h&quot;</span><br />
<span class="co2">#include &quot;MixBox.h&quot;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="kw4">int</span> argc, <span class="kw4">char</span>* argv<span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">using</span> <span class="kw2">namespace</span> MB; &nbsp;<span class="co1">// MixBox&#8217;s namespace</span><br />
&nbsp; <br />
&nbsp; &nbsp; <span class="co1">// Get the MixBox for future use</span><br />
&nbsp; &nbsp; MixBox&amp; mixer = MixBox::<span class="me2">instance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// Open the mixer</span><br />
&nbsp; &nbsp; mixer.<span class="me1">openMixer</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; <span class="co1">// Load a sound</span><br />
&nbsp; &nbsp; SoundID hit = mixer.<span class="me1">loadSound</span><span class="br0">&#40;</span><span class="st0">&quot;hit.wav&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; <br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>hit.<span class="me1">isBad</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;Error loading sound file.<span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// Play the sound</span><br />
&nbsp; &nbsp; hit.<span class="me1">play</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span>mixer.<span class="me1">isPlaying</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Delay<span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="co1">// Close the mixer</span><br />
&nbsp; &nbsp; mixer.<span class="me1">closeMixer</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// Delete the sound</span><br />
&nbsp; &nbsp; mixer.<span class="me1">freeSounds</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;</p>
<p>&nbsp; &nbsp; <span class="co1">// All done</span><br />
&nbsp; &nbsp; SDL_Quit<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Really straightforward, right?  Before you can do anything with this example, though, you need to have sound files to play.  There are some in the above link, but you&#8217;ll need more, of course.  If you need somewhere to start, I recommend DrPetter&#8217;s sfxr.  It&#8217;s a great tool for quickly making retro and placeholder sounds.</p>
<p>The examples just have the basics of MixBox.  Here&#8217;s a brief overview of some other functions MixBox has.  Since playing sounds and music is not too complex, I&#8217;ll just explain the odd ones.  There&#8217;s more than this in MixBox.  All the functions can easily be found in MixBox.h, where there are better descriptions of what they do.  Be sure to check it out!</p>
<p>MixBox setup:  Initialize and close MixBox.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">static</span> MixBox&amp; instance<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Returns the singleton MixBox.<br />
<span class="kw4">void</span> setAudioFormat<span class="br0">&#40;</span>Uint16 format<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> setAudioRate<span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">int</span> rate<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> setNumOutputs<span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">int</span> num<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Sets the number of output <span class="br0">&#40;</span>not mixing<span class="br0">&#41;</span> channels <span class="br0">&#40;</span>e.<span class="me1">g</span>. <span class="nu0">1</span> <span class="kw1">for</span> mono, <span class="nu0">2</span> <span class="kw1">for</span> stereo<span class="br0">&#41;</span><br />
<span class="kw4">void</span> setBufferSize<span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">int</span> size<span class="br0">&#41;</span>;<br />
<span class="kw4">unsigned</span> <span class="kw4">int</span> setNumChannels<span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">int</span> numChannels<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Sets the number of mixing channels<br />
<span class="kw4">bool</span> openMixer<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> closeMixer<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>MixBox loading:  Load and delete sounds and music.</p>
<div class="dean_ch" style="white-space: wrap;">
SoundID loadSound<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">char</span>* filename<span class="br0">&#41;</span>;<br />
MusicID loadMusic<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">char</span>* filename<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> freeSounds<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> <span class="kw3">free</span><span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; id<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> freeMusic<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> <span class="kw3">free</span><span class="br0">&#40;</span><span class="kw4">const</span> MusicID&amp; id<span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>MixBox sound controls:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">bool</span> swapStereo<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
ChannelID play<span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; id, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> stop<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
ChannelID playPan2D<span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; id, <span class="kw4">float</span> x, <span class="kw4">float</span> y, <span class="kw4">float</span> maxRange, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Plays a sound with stereo panning and volume that depend on how far <span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> is from <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>.<br />
<span class="me1">ChannelID</span> playPan3D<span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; id, <span class="kw4">float</span> x, <span class="kw4">float</span> y, <span class="kw4">float</span> z, <span class="kw4">float</span> maxRange, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Plays a sound with stereo panning and volume that depend on how far <span class="br0">&#40;</span>x, y, z<span class="br0">&#41;</span> is from <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>.<br />
<span class="kw4">bool</span> isPlaying<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
&nbsp; &nbsp; &#8211; Tells you <span class="kw1">if</span> any channel is playing a sound. &nbsp;<span class="me1">There</span> are other functions that work with specific channels.<br />
&nbsp;</div>
<p>MixBox music controls:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">void</span> play<span class="br0">&#40;</span><span class="kw4">const</span> MusicID&amp; id, <span class="kw4">int</span> loops = <span class="nu0">-1</span><span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> isMusicPlaying<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
<span class="kw4">void</span> stopMusic<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> pauseMusic<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> isMusicPaused<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
<span class="kw4">void</span> rewindMusic<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> resumeMusic<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> skipMusic<span class="br0">&#40;</span><span class="kw4">double</span> position<span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> fadeInMusic<span class="br0">&#40;</span><span class="kw4">const</span> MusicID&amp; id, <span class="kw4">unsigned</span> <span class="kw4">int</span> milliseconds, <span class="kw4">double</span> skipPosition = <span class="nu0">0</span>, <span class="kw4">int</span> loops = <span class="nu0">-1</span><span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> fadeOutMusic<span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">int</span> milliseconds<span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>SoundID:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">bool</span> isBad<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
&nbsp; &nbsp; &#8211; Tells you <span class="kw1">if</span> the sound loaded incorrectly.<br />
<span class="kw4">void</span> setVolume<span class="br0">&#40;</span><span class="kw4">float</span> volume = <span class="nu0">1</span>.0f<span class="br0">&#41;</span>;<br />
<span class="kw4">float</span> getVolume<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
ChannelID play<span class="br0">&#40;</span><span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
ChannelID playPan2D<span class="br0">&#40;</span><span class="kw4">float</span> x, <span class="kw4">float</span> y, <span class="kw4">float</span> maxRange, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Plays a sound with stereo panning and volume that depend on how far <span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> is from <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>.<br />
<span class="me1">ChannelID</span> playPan3D<span class="br0">&#40;</span><span class="kw4">float</span> x, <span class="kw4">float</span> y, <span class="kw4">float</span> z, <span class="kw4">float</span> maxRange, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Plays a sound with stereo panning and volume that depend on how far <span class="br0">&#40;</span>x, y, z<span class="br0">&#41;</span> is from <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>.<br />
&nbsp;</div>
<p>MusicID:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">bool</span> isBad<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
&nbsp; &nbsp; &#8211; Tells you <span class="kw1">if</span> the music loaded incorrectly.<br />
<span class="kw4">void</span> setVolume<span class="br0">&#40;</span><span class="kw4">float</span> volume = <span class="nu0">1</span>.0f<span class="br0">&#41;</span>;<br />
<span class="kw4">float</span> getVolume<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
<span class="kw4">void</span> play<span class="br0">&#40;</span><span class="kw4">int</span> loops = <span class="nu0">-1</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>ChannelID:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">bool</span> isBad<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
&nbsp; &nbsp; &#8211; Tells you <span class="kw1">if</span> the ID represents a viable channel.<br />
<span class="kw4">void</span> setVolume<span class="br0">&#40;</span><span class="kw4">float</span> volume = <span class="nu0">1</span>.0f<span class="br0">&#41;</span>;<br />
<span class="kw4">float</span> getVolume<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
<span class="kw4">bool</span> isPlaying<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
<span class="kw4">void</span> play<span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; sound, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> stop<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> pause<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> isPaused<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
<span class="kw4">void</span> resume<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> playPan2D<span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; id, <span class="kw4">float</span> x, <span class="kw4">float</span> y, <span class="kw4">float</span> maxRange, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Plays a sound with stereo panning and volume that depend on how far <span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> is from <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>.<br />
<span class="kw4">void</span> playPan3D<span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; id, <span class="kw4">float</span> x, <span class="kw4">float</span> y, <span class="kw4">float</span> z, <span class="kw4">float</span> maxRange, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Plays a sound with stereo panning and volume that depend on how far <span class="br0">&#40;</span>x, y, z<span class="br0">&#41;</span> is from <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>.<br />
&nbsp;</div>
<p>And that&#8217;s how easy it is to add sound and music to your games!  Remember that there&#8217;s more in the header file, MixBox.h.  There&#8217;s also a demo program included with the MixBox source.  If you have any questions, suggestions, or contributions, drop me an email.</p>
<p>Files:<br />
<a href="/tutorials/bluedinocode/mixbox-quick-guide.zip">mixbox-quick-guide.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/mixbox-quick-guide/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Jonny D&#8217;s C++ Tutorial</title>
		<link>http://www.sdltutorials.com/jonny-ds-c-tutorial/</link>
		<comments>http://www.sdltutorials.com/jonny-ds-c-tutorial/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 22:54:38 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[Blue Dino Code]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=197</guid>
		<description><![CDATA[The following tutorial was created by Blue Dino Code, for the purpose of helping people like you learn SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework [...]]]></description>
			<content:encoded><![CDATA[<p>The following tutorial was created by <a href="http://code.bluedinosaurs.com/">Blue Dino Code</a>, for the purpose of helping people like you learn SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework used by the author. SDLTutorials.com does not endorse or reject any of the coding practices outlined in the tutorial, and is not responsible for any code or files belonging to this tutorial that harms your computer.</p>
<hr/>
<div class="dean_ch" style="white-space: wrap;">
<span class="coMULTI">/*</p>
<p>//////////////////////// &nbsp;C++ From Start to Classes &nbsp;\\\\\\\\\\\\\\\\\\\\\\\\\\<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
| Hi, this is Jonny D and I&#8217;ll be whirling you through the basics and some &nbsp; &nbsp;|<br />
| intricacies of C++. &nbsp;This tutorial is for ABSOLUTE BEGINNERS that are &nbsp; &nbsp; &nbsp; |<br />
| computer literate. &nbsp;I&#8217;ll try my best to keep it plain, but if anything &nbsp; &nbsp; &nbsp;|<br />
| isn&#8217;t clear, feel free to drop me a line at GrimFang4@hotmail.com . &nbsp; &nbsp; &nbsp; &nbsp; |<br />
| Stick with me and you&#8217;ll be programming in no time. &nbsp;I&#8217;m currently using &nbsp; &nbsp;|<br />
| the Code::Blocks IDE (Integrated Development Environment) to write and &nbsp; &nbsp; &nbsp;|<br />
| compile my code under Windows XP and I use the SDL (Simple DirectMedia &nbsp; &nbsp; &nbsp;|<br />
| Layer) graphics library for my games. &nbsp;I highly recommend SDL for when you &nbsp;|<br />
| feel ready to make video games. &nbsp;I might leave little bits of information &nbsp; |<br />
| about SDL along the way towards our learning goal. &nbsp;To keep it simple, I &nbsp; &nbsp;|<br />
| won&#8217;t be referencing any specific programming language other than C and C++ |<br />
| and I won&#8217;t be giving any history lessons (yay!). &nbsp;From here on out, it&#8217;s &nbsp; |<br />
| pure C++ learning time. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
| Well, let&#8217;s get started. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
*/</span><br />
&nbsp;</div>
<p><span id="more-197"></span></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="coMULTI">/*<br />
First of all, you&#8217;ll need a compiler to convert your code into an executable.<br />
Until Code::Blocks releases a new and bug-free version of their IDE, I <br />
suggest Dev-C++ for beginning C++ programming. &nbsp;It can be found and downloaded<br />
for free at www.bloodshed.net</p>
<p>Strictly speaking, Dev-C++ is not a compiler, it is an IDE that contains a <br />
particular compiler (in this case, g++). &nbsp;Dev-C++ is mostly an editor that makes<br />
it easy for you to write your programs. &nbsp;Whatever you do, don&#8217;t use programs <br />
like Word to write your code. &nbsp;If you don&#8217;t have access to a C++ editor like <br />
Dev-C++, then use something simple like Notepad. &nbsp;Word will leave messy <br />
formatting stuff in your text which will prevent you from compiling your code.</p>
<p>Once you install Dev-C++, you can run it and start coding. &nbsp;You can open this <br />
file in there as well. &nbsp;You might need to make a new project and add these code <br />
files to it.<br />
*/</span></p>
<p><span class="coMULTI">/*<br />
So, what you&#8217;ve just seen is the beginning of a multi-line (or C-style)<br />
comment. &nbsp;This comment starts with a forward slash and an asterisk and is<br />
ended by the reverse, an asterisk and a forward slash. &nbsp;Whatever is <br />
within a comment is totally ignored by the compiler (the program that changes <br />
the code that you&#8217;ve written into a program that the computer can read). &nbsp;<br />
The easy way to make these comments is to use the numpad&#8217;s keys since they are <br />
right next to each other and don&#8217;t require a shift modifier.<br />
*/</span></p>
<p><span class="co1">// This is a single-line (or C++-style or new-style) comment. &nbsp;It starts with </span><br />
<span class="co1">// two forward slashes and ends at the end of a line. &nbsp;It&#8217;s really quick to </span><br />
<span class="co1">// use for leaving notes in your code, but can be annoying for large blocks of</span><br />
<span class="co1">// writing like I&#8217;ll be using for some of my explanations.</span></p>
<p>
<span class="coMULTI">/*<br />
C++ uses preprocessor directives. &nbsp;These are just special keywords that are <br />
checked before the actual code gets compiled. &nbsp;The most common is the #include<br />
(&quot;pound include&quot;) preprocessor directive. &nbsp;It is used to link other source code <br />
files (like this one) to the current source file so that they get compiled <br />
together. &nbsp;This means you can use the code in the other files just as if it <br />
were in your file. &nbsp;It is usually best to write your preprocessor directives<br />
at the beginning of your source code file.<br />
*/</span></p>
<p><span class="co2">#include &lt;cstdlib&gt; &nbsp;//This line includes a standard header file named &quot;cstdlib&quot;,</span><br />
<span class="co1">// the C Standard Library. &nbsp;This contains many features that help with most </span><br />
<span class="co1">// simple tasks. &nbsp;This and the next #include are not really necessary in this </span><br />
<span class="co1">// tutorial, but can be useful in some projects.</span></p>
<p><span class="co2">#include &lt;cstdio&gt; &nbsp;//&quot;cstdio&quot;, the C Standard Input and Output file, gives us </span><br />
<span class="co1">// access to C&#8217;s old way of controlling input and output. &nbsp;Hey, it&#8217;s old but </span><br />
<span class="co1">// can still come in handy!</span></p>
<p><span class="co2">#include &lt;iostream&gt; &nbsp;//This is C++&#8217;s standard input/output file. &nbsp;It makes </span><br />
<span class="co1">// many things easier and uses a different syntax (order of words/symbols)</span><br />
<span class="co1">// than the C way of things.</span></p>
<p><span class="co2">#include &lt;string&gt; //Here&#8217;s C++&#8217;s string class that replaces many uses of C&#8217;s </span><br />
<span class="co1">// outdated character arrays (which we&#8217;ll see later). &nbsp;Strings are just a </span><br />
<span class="co1">// bunch of sequential characters (letters/numbers/symbols). &nbsp;Strings are </span><br />
<span class="co1">// usually seen with double quotes around them, like &quot;This is a string.&quot; or </span><br />
<span class="co1">// &quot;1357&quot;.</span></p>
<p><span class="co2">#include &quot;ClassName.h&quot; &nbsp;//This is a user-defined header file that I made. &nbsp;</span><br />
<span class="co1">// Notice that it uses a string that contains the name of a file in the same </span><br />
<span class="co1">// directory as this source file. &nbsp;If this were in a separate folder, I could </span><br />
<span class="co1">// use relative path names like:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// &quot;folder/ClassName.h&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp; &nbsp; &nbsp; &nbsp;or</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// &quot;../ClassName.h&quot;</span><br />
<span class="co1">// That last one looks for ClassName in the parent folder of the one </span><br />
<span class="co1">// that JDTutorial.cpp is in.</span></p>
<p><span class="co2">#define ONE 1 &nbsp;//&quot;Pound define&quot; is another preprocessor directive. &nbsp;If the first </span><br />
<span class="co1">// term after it, ONE, is found in your code, then it will be converted to the </span><br />
<span class="co1">// second term, 1, before any code is actually compiled.</span></p>
<p><span class="co2">#define MOOCOW &quot;milk&quot; &nbsp;// &quot;milk&quot; and 1 are called &quot;literals&quot; because they don&#8217;t </span><br />
<span class="co1">// evaluate (break down or change) to anything simpler than themselves. &nbsp;They </span><br />
<span class="co1">// are hard-written into the code.</span></p>
<p><span class="co2">#if 0 &nbsp;//The &quot;pound if&quot; is a preprocessor logic control. &nbsp;You should know that </span><br />
<span class="co1">// 0 means false and 1 means true. &nbsp;Remember that. &nbsp;So since 0 means false, </span><br />
<span class="co1">// the next part will not be compiled, thankfully. &nbsp;If I wanted it to compile, </span><br />
<span class="co1">// I could have written either 1 or ONE, since ONE is #defined as 1.</span><br />
<span class="co2">#include &quot;thisFileDoesNotExist.h&quot;</span><br />
<span class="co2">#else &nbsp;//Pound else handles the case that the term after the #if is false.</span><br />
<span class="co2">#define SAMURAI &quot;choppity-chop!&quot;</span><br />
<span class="co2">#endif &nbsp;//Every good if statement must come to an end&#8230;</span></p>
<p><span class="kw2">using</span> <span class="kw2">namespace</span> std; &nbsp;<span class="co1">//This statement is the use of the standard namespace. &nbsp;</span><br />
<span class="co1">// Namespaces are just protection from colliding definitions. &nbsp;C++ won&#8217;t like </span><br />
<span class="co1">// it if you use the same name for two functions (a function is a</span><br />
<span class="co1">// reusable organizational unit of code, similar to math functions), so </span><br />
<span class="co1">// namespaces will free you from possible collisions, say, with #included code.</span><br />
<span class="co1">// The standard namespace is what many important files (like &lt;iostream&gt;) use </span><br />
<span class="co1">// to store their commands in. &nbsp;You&#8217;ll need to be using this namespace in order </span><br />
<span class="co1">// to use these commands. &nbsp;Typically, namespaces other than std are only used </span><br />
<span class="co1">// in gigantic projects like operating systems and such.</span></p>
<p><span class="co1">// Unlike English, where statements are ended with a period, C++ is a language </span><br />
<span class="co1">// where statements are ended with semicolons. &nbsp;All of your statements must end </span><br />
<span class="co1">// with a semicolon because &quot;whitespace&quot; doesn&#8217;t matter to C++. &nbsp;Tabs, spaces, </span><br />
<span class="co1">// and even carriage returns (end of line, beginning of new line) are ignored </span><br />
<span class="co1">// by the compiler (except for when they separate words) and serve only to make </span><br />
<span class="co1">// your code look neat and tidy. &nbsp;It is important to have neat code so that </span><br />
<span class="co1">// you or the next person in line (like a coworker or college professor) can </span><br />
<span class="co1">// read it and tell what it does. &nbsp;Of course you probably won&#8217;t ever have to </span><br />
<span class="co1">// write as many comments as I have in this file.</span></p>
<p><span class="kw4">int</span> counter; <span class="coMULTI">/* This is a variable (just like math) declaration. &nbsp;In this case, <br />
I specified that it is an integer (positive or negative whole number). &nbsp;It&#8217;s<br />
like saying &quot;Let there exist an integer named &#8216;counter&#8217;&quot;. &nbsp;There are a few <br />
options for variable types, such as, but not restricted to:<br />
&nbsp; &nbsp; integer =&gt; int<br />
&nbsp; &nbsp; long integer (for BIG numbers) =&gt; long<br />
&nbsp; &nbsp; character =&gt; char<br />
&nbsp; &nbsp; floating point (number with a decimal) =&gt; float<br />
&nbsp; &nbsp; double-precision floating point (more significant figures) =&gt; double<br />
&nbsp; &nbsp; boolean (either 1, true, or 0, false) =&gt; bool<br />
Every variable must be declared with a type. &nbsp;That&#8217;s why C++ is called a <br />
&quot;strongly typed&quot; language. &nbsp;Also, variable and function names must start with a <br />
letter but can have letters, numbers, or _underscores_ afterwards. &nbsp;C++ is <br />
case-sensitive, so capitalization matters every time. &nbsp;It is a useful <br />
convention to capitalize the beginning of each new word in a name, as below. */</span></p>
<p><span class="kw4">char</span> aLetter = <span class="st0">&#8216;j&#8217;</span>; &nbsp;<span class="coMULTI">/* Variables can be given values when they are declared.<br />
The assignment operator for C++ is the equals sign. &nbsp;The assignment operator <br />
always takes two things, the left part and the right part, and assigns the <br />
value of the right part to the variable on the left (right to left evaluation). <br />
Character values are denoted by having single-quotes around a single <br />
letter, number, or symbol. &nbsp;Characters are different than strings. */</span></p>
<p><span class="coMULTI">/* counter and aLetter are both &quot;global&quot; variables because they were declared <br />
outside of any functions. &nbsp;This means that any part of the program can access <br />
them and change them. &nbsp;Global variables aren&#8217;t always a good idea, though, <br />
because they are hard to recognize in code that refers to them all <br />
of a sudden. &nbsp;Variables declared inside a function are called &quot;local&quot; <br />
variables. &nbsp;These will always be deleted when the function finishes unless<br />
you do something fancy (We&#8217;ll see some of this later).<br />
*/</span></p>
<p><span class="co1">// &nbsp;Here we will declare the functions we will be using in our program.</span><br />
<span class="co1">// These are just the declarations. &nbsp;They tell the compiler that the function </span><br />
<span class="co1">// exists. &nbsp;Later, we must define each function so that we can actually use it.</span></p>
<p><span class="coMULTI">/* I want just1() to always give me back the number 1. &nbsp;Pretty useless, yes, <br />
but it illustrates a few things about functions. &nbsp;The name of a function, <br />
like just1(), should always say something about what the function does. &nbsp;<br />
A function named q1342g() won&#8217;t help me at all. &nbsp;It&#8217;s also good to write a <br />
quick comment next to a function that tells what it does. */</span><br />
<span class="kw4">int</span> just1<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">//Function code goes here</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">1</span>;<br />
<span class="br0">&#125;</span></p>
<p>
<span class="coMULTI">/* Functions are called (used) in this way:<br />
&nbsp; &nbsp; &nbsp;just1();<br />
&nbsp; <br />
&nbsp; But this doesn&#8217;t do much for us, really. &nbsp;The &quot;int&quot; specifies the return <br />
type. &nbsp;This means that when I call just1(), then an integer will pop right <br />
out when the function ends. &nbsp;That integer would just be discarded if we don&#8217;t <br />
assign it to a variable. &nbsp;The integer can be caught with the assignment <br />
operator like so: <br />
&nbsp; &nbsp; &nbsp; &nbsp;int result = just1();<br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; After the return type comes the name followed by parentheses. &nbsp;Inside the <br />
parentheses is a list of whatever you want the function input to be <br />
(called function arguments). &nbsp;There are no arguments for just1() though.<br />
*/</span></p>
<p>
<span class="coMULTI">/* It is quite important (and good coding style) to write <br />
&quot;prototype declarations&quot; of functions. &nbsp;If a declaration isn&#8217;t found before <br />
you try to use a function, C++ won&#8217;t know what you&#8217;re talking about. &nbsp;It&#8217;s <br />
like referring to a joke that you haven&#8217;t told yet.<br />
*/</span></p>
<p>
<span class="co1">// This is a prototype declaration for add1()</span><br />
<span class="kw4">int</span> add1<span class="br0">&#40;</span><span class="kw4">int</span> num<span class="br0">&#41;</span>; <span class="coMULTI">/* I want this function to take a number I give to it, add <br />
one to it, and return the result. &nbsp;num is an integer that I will provide <br />
whenever I use add1(). &nbsp;When I do call add1() and give it an integer, such <br />
as&#8230;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;add1(4);<br />
&nbsp; &#8230;then the value 4 is bound to num which is then used within add1(). &nbsp;I could <br />
even write add1(ONE);, using the preprocessor to convert ONE to 1. */</span></p>
<p><span class="kw4">void</span> countDown<span class="br0">&#40;</span><span class="kw4">int</span> count<span class="br0">&#41;</span>; <span class="coMULTI">/* Here&#8217;s a very fancy function. &nbsp;I want it to <br />
print to the screen a list of numbers starting with one I give it and descending <br />
(decrementing) until it reaches zero. &nbsp;The return type, void, means that the <br />
function will not return a value at all. &nbsp;That means that the function must <br />
perform a useful side-effect like changing a variable or in this case, printing<br />
to the screen.<br />
*/</span></p>
<p><span class="coMULTI">/* &nbsp;You can ignore the following function declarations for now. &nbsp;They&#8217;ll be <br />
used later to explain the topics in this tutorial. &nbsp;You can use this list as <br />
an index if you wish.<br />
*/</span></p>
<p><span class="kw4">void</span> choice<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="kw4">void</span> operators<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// +, -, *, /, =, etc.</span><br />
<span class="kw4">void</span> stdPut<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// cout and cin</span><br />
<span class="kw4">void</span> logic<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// if (conditionals), &amp;&amp;, ||, ==, !, !=</span><br />
<span class="kw4">void</span> controls<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// loops with while, do while, and for</span><br />
<span class="kw4">void</span> arrays<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// arrays[] as lists of variables</span><br />
<span class="kw4">void</span> stringClass<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Explaining the string class</span><br />
<span class="kw4">void</span> randomStuff<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// rand() and random number generation</span><br />
<span class="kw4">void</span> structKeyword<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Structures and struct, the precursor to classes</span><br />
<span class="kw4">void</span> recursion<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Looping without loops, function self-calls</span><br />
<span class="kw4">void</span> pointers<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// -&gt; memory addresses</span><br />
<span class="kw4">void</span> references<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// An easier way to pass by reference</span><br />
<span class="kw4">void</span> functionOverloading<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// functions with nearly the same name</span><br />
<span class="kw4">void</span> scope<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// local vs. global, precedence rules</span><br />
<span class="kw4">void</span> dynamicMemory<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// new and delete memory management</span><br />
<span class="kw4">void</span> classes<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// what makes C++ better than C, classes and inheritance</span></p>
<p><span class="kw2">class</span> Junk<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw4">int</span> j;<br />
<span class="br0">&#125;</span>;</p>
<p><span class="kw4">int</span> fact<span class="br0">&#40;</span><span class="kw4">int</span> num<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> square<span class="br0">&#40;</span><span class="kw4">int</span> a<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> squarePtr<span class="br0">&#40;</span><span class="kw4">int</span>* x<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> squareRef<span class="br0">&#40;</span>int&amp; x<span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">int</span> a<span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">int</span> a, <span class="kw4">int</span> b<span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">int</span> a, <span class="kw4">int</span> b, <span class="kw4">int</span> c<span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">char</span> a, <span class="kw4">char</span> b<span class="br0">&#41;</span>;</p>
<p><span class="coMULTI">/*<br />
&nbsp;Here&#8217;s where the fun begins. &nbsp;The main() function is the entry point of <br />
the program whereas everything up to this is just being held onto for use <br />
in this function. &nbsp;It&#8217;s a good idea to type it <br />
as &quot;int main(int argc, char *argv[])&quot; because then it will return an integer <br />
value to the operating system telling whether or not there was an error and <br />
the arguments afterward give you the ability to catch command-line arguments <br />
(like when you use &quot;Run&#8230; c:\test.exe fps toilet&quot; where fps and toilet are <br />
command-line arguments). &nbsp;You can sometimes put just void main(), but SDL, the <br />
graphics library, requires all this stuff. &nbsp;Like all function definitions, <br />
main() must have wiggly brackets {}, aka braces, after it. &nbsp;These braces <br />
contain the statements to be executed when the function is run.<br />
*/</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="kw4">int</span> argc, <span class="kw4">char</span> *argv<span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="coMULTI">/***********<br />
&nbsp; &nbsp; Let&#8217;s go over exactly what code you need for your program to compile.<br />
&nbsp; &nbsp; First, you need to #include the files that have code that you&#8217;ll be using.<br />
&nbsp; &nbsp; Next, you will usually need the &#8216;using namespace std;&#8217; line. &nbsp;Then, you <br />
&nbsp; &nbsp; need a main() function with a code block ( code surrounded by {} curly <br />
&nbsp; &nbsp; braces ). &nbsp;Lastly, make sure your code is correct with semicolons ; after <br />
&nbsp; &nbsp; every necessary line and you return from main() cleanly.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;************/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// Check out GoodExample.cpp if you want to see an example of a </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// pretty basic program.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// This JDTutorial.cpp is a full program as well, but is very cluttered</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// with tutorial comments.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; My main() is pretty lame. &nbsp;A main() could be the whole program, <br />
&nbsp; &nbsp; but mine just organizes the rest of the program.<br />
&nbsp; &nbsp; */</span></p>
<p>&nbsp; &nbsp; <span class="kw3">srand</span><span class="br0">&#40;</span><span class="kw3">time</span><span class="br0">&#40;</span><span class="kw2">NULL</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// This is explained in randomStuff()</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; choice<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw3">system</span><span class="br0">&#40;</span><span class="st0">&quot;PAUSE&quot;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// This is an inefficient but very simple way to pause </span><br />
&nbsp; &nbsp; <span class="co1">// the program. &nbsp;It&#8217;s a call to the operating system and is a little </span><br />
&nbsp; &nbsp; <span class="co1">// wasteful. &nbsp;It&#8217;s often used in main() to keep the program window open, but </span><br />
&nbsp; &nbsp; <span class="co1">// some compilers do not need this command to do that.</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>; &nbsp;<span class="co1">// No problems running, so return 0 to the operating system.</span><br />
&nbsp; &nbsp; <span class="co1">// This is the end of the program. &nbsp;Once main() returns, it&#8217;s all over.</span><br />
&nbsp; &nbsp; <span class="co1">// The return keyword is how you stop a function from continuing and how </span><br />
&nbsp; &nbsp; <span class="co1">// you can make it give you a value (unless it&#8217;s void). &nbsp;return also </span><br />
&nbsp; &nbsp; <span class="co1">// accepts variables. &nbsp;For void functions, just type &#8216;return;&#8217; to end it.</span><br />
&nbsp; &nbsp; <span class="co1">// Continue on below for the rest of the tutorial.</span><br />
&nbsp; &nbsp; <br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END int main(int argc, char *argv[]) &nbsp;&lt;- Though this may be </span><br />
&nbsp; &nbsp;<span class="co1">// unnecessary since main() is so short, it is a good idea to label the end </span><br />
&nbsp; &nbsp;<span class="co1">// of large blocks of code (like functions).</span></p>
<p>
<span class="kw4">void</span> operators<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Operators in C++ are those symbols that change or eventually give values,<br />
&nbsp; &nbsp; &nbsp;again, just like in math class.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> answer;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> a = <span class="nu0">6</span>; &nbsp;<span class="co1">// The assignment operator, as you&#8217;ve seen.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> b = <span class="nu0">4</span>; &nbsp;<span class="co1">// Never try to use an uninitialized variable. &nbsp;You&#8217;ll get </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// lots of errors.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!) &nbsp;&lt;- Look for this symbol later in the tutorial!</span><br />
&nbsp; &nbsp; &nbsp;answer = a + b; &nbsp; <span class="co1">// This will evaluate and set the value of answer to 10</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;answer = a &#8211; b; &nbsp; <span class="co1">// Answer equals 2</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;answer = a * b; &nbsp; <span class="co1">// Answer equals 24</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;answer = a / b; &nbsp; <span class="co1">// Equals&#8230; 1? &nbsp;Yep, this is integer division!</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;answer = a % b; &nbsp; <span class="co1">// This is &quot;modulo&quot;, which gives the remainder of </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//integer division.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// a % b = 2</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Modulo is a powerful operator. &nbsp;It can do lots of fun stuff.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// You&#8217;ll see more in randomStuff(), but keep in mind that because </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// modulo is based on division, the result is ALWAYS less than the </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// right side (in this case, &quot;b&quot;).</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Just below this comment, see if you can make answer equal 12 by </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// modding two integers. &nbsp;Then write one that equals zero.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// C++ has some handy shorthand operators as well.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// From up top a = 6, b = 4.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;a = a + b; <span class="co1">// This is the same as&#8230;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;a += b; &nbsp;<span class="co1">// This.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Notice now that a = (a + b) + b = 14</span><br />
&nbsp; &nbsp; &nbsp;a -= b; <span class="co1">// Now a = 10</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;a *= b; <span class="co1">// a = 40</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;a /= b; <span class="co1">// a = 10</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// The increment and decrement operators are useful too.</span><br />
&nbsp; &nbsp; &nbsp;a++; <span class="co1">// a = 11</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;a&#8211;; <span class="co1">// a = 10</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// These are in the postfix position, but you can also write them in the </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// prefix position like so:</span><br />
&nbsp; &nbsp; &nbsp;++a;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;&#8211;a;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// The difference is in when they evaluate.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// a++, the postfix notation of the increment operator, will add one to </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the variable &#8216;a&#8217; after the whole line has been performed by the program.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// ++a, the prefix notation of the increment operator, will add one to </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the variable &#8216;a&#8217; immediately and thus before anything else on the line </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// tries to work with it.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Precedence</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Every operator has a preset level of precedence. &nbsp;This tells which operator<br />
&nbsp; &nbsp; &nbsp;&quot;precedes&quot; another particular operator.<br />
&nbsp; &nbsp; &nbsp;For instance, multiplication has a higher precedence than addition.<br />
&nbsp; &nbsp; &nbsp;Thus, a + b * c<br />
&nbsp; &nbsp; &nbsp; &nbsp;evaluates like a + (b * c).<br />
&nbsp; &nbsp; &nbsp; &nbsp;&#8217;b&#8217; and &#8216;c&#8217; get multiplied before &#8216;a&#8217; is added.<br />
&nbsp; &nbsp; &nbsp;Just as I did here, you can make the precedence of your statements clearer<br />
&nbsp; &nbsp; &nbsp;or cause the precedence to work the way you want it to by using parentheses.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;If you look at (a + b) * c,<br />
&nbsp; &nbsp; &nbsp; &nbsp;this evaluates (a + b) first, then multiplies that result by &#8216;c&#8217;.<br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;The precedence of operators is not usually a big deal, but many new <br />
&nbsp; &nbsp; &nbsp;programmers will forget that multiplication and division have higher <br />
&nbsp; &nbsp; &nbsp;precedence than addition and subtraction.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;If you need to know any other precedences, you can just search online for <br />
&nbsp; &nbsp; &nbsp;a C++ precedence table. &nbsp;Otherwise, you can always use parentheses.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Here&#8217;s a little test that you can skip if you want</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp;(or just run the program with the task done from the next section&#8230;)</span><br />
&nbsp; &nbsp; &nbsp;a = <span class="nu0">6</span>;<br />
&nbsp; &nbsp; &nbsp;b = <span class="nu0">4</span>;<br />
&nbsp; &nbsp; &nbsp;answer = <span class="nu0">3</span> * a++ + add1<span class="br0">&#40;</span><span class="nu0">6</span><span class="br0">&#41;</span> &#8211; ++b / <span class="nu0">5</span> + &#8211;b + b++; &nbsp;<span class="co1">// Ooh, nice and complicated.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// It helps to clarify with parentheses sometimes.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// answer = (3 * a++) + add1(6) &#8211; (++b / 5) + &#8211;b + b++;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Now what is the answer? &nbsp;a++ increments &#8216;a&#8217; _after_ the statement is </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// evaluated. &nbsp;++b increments &#8216;b&#8217; _before_ anything else happens.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// What will be the values of answer, a, and b?</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void operators()</span></p>
<p>
<span class="kw4">int</span> add1<span class="br0">&#40;</span><span class="kw4">int</span> num<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><span class="kw1">return</span> num+=<span class="nu0">1</span>;<span class="br0">&#125;</span></p>
<p>
<span class="kw4">void</span> stdPut<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// Stuff in this section requires #include &lt;iostream&gt;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Do you remember way up there when we #included &lt;iostream&gt;? &nbsp;That file <br />
&nbsp; &nbsp; includes two nice objects that we&#8217;ll use a lot, cin and cout, console <br />
&nbsp; &nbsp; input and output objects. &nbsp;We&#8217;ll be getting to the definition of objects <br />
&nbsp; &nbsp; when we start talking about classes later. &nbsp;For now, let&#8217;s truck along. */</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Hello World!&quot;</span> &lt;&lt; endl; &nbsp;<span class="coMULTI">/* The console that cout refers to is that <br />
&nbsp; &nbsp; ugly MSDOS prompt window that Windows still uses. &nbsp;The console will boot <br />
&nbsp; &nbsp; up with the start of the program and then display &quot;Hello World!&quot;. &nbsp;cout <br />
&nbsp; &nbsp; uses the &lt;&lt; left redirect operator. &nbsp;You can think of it as arrows pointing <br />
&nbsp; &nbsp; in the direction the data will move, like it takes all the words and sends <br />
&nbsp; &nbsp; them to the screen. &nbsp;cout accepts strings like &quot;Hello World!&quot; and standard <br />
&nbsp; &nbsp; variables. &nbsp;The keyword &quot;endl&quot; means end line. &nbsp;It&#8217;s like the computer <br />
&nbsp; &nbsp; presses the return key to start a new line. &nbsp; &nbsp;<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">//Here we&#8217;ll output some other stuff. &nbsp;(aLetter is still a global variable)</span><br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; endl &lt;&lt; aLetter &lt;&lt; <span class="st0">&quot;onny d&quot;</span> &lt;&lt; <span class="nu0">3</span> + <span class="nu0">2</span> &lt;&lt; endl; &nbsp;<span class="co1">//jonny d5</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// For some practice (and to make the program more useful), go back up to </span><br />
&nbsp; &nbsp; <span class="co1">// operators() and replace the commented (!) symbol with some uncommented </span><br />
&nbsp; &nbsp; <span class="co1">// code that will display some useful variables to know at each point.</span><br />
&nbsp; &nbsp; <span class="co1">// Your best friends will be named &quot;cut&quot; and &quot;paste&quot;.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; As I said above, &lt;iostream&gt; also includes &quot;cin&quot;, the console input object.<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> number;<br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; endl &lt;&lt; <span class="st0">&quot;Think of a number&#8230; (and enter it)&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;-&gt;&quot;</span>; &nbsp;<span class="co1">// Always prompt for an input. &nbsp;The user won&#8217;t know what </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// to do otherwise.</span><br />
&nbsp; &nbsp; <span class="kw3">cin</span> &gt;&gt; number; &nbsp;<span class="co1">// The data goes from the input to the variable.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; endl &lt;&lt; <span class="st0">&quot;With my mystical abilities, I have divined that&quot;</span>;<br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; endl &lt;&lt; <span class="st0">&quot; you chose the number &quot;</span> &lt;&lt; number &lt;&lt; <span class="st0">&quot;!&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// I personally never use the old C way of collecting input, but I still </span><br />
&nbsp; &nbsp; <span class="co1">// frequently use C&#8217;s output command.</span><br />
&nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;This is how C programmers had to do output.<span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;They also knew that %d is your number&#8230;<span class="es0">\n</span>&quot;</span>, number<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; aLetter = <span class="st0">&#8216;a&#8217;</span>;<br />
&nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;This is about %d times as %cnnoying!<span class="es0">\n</span>&quot;</span>, number, aLetter<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; printf() uses escape characters to represent where info should be inserted. <br />
&nbsp; &nbsp; As you can see above, \n (backslash n) gives a carriage return, %d is the <br />
&nbsp; &nbsp; escape for integers, %f does floats and doubles, and %c escapes for <br />
&nbsp; &nbsp; characters. &nbsp;A bunch of other escapes are out there.<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw4">float</span> pi = <span class="nu0">3.14</span>;<br />
&nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;PI = %.2f<span class="es0">\n</span>&quot;</span>, pi<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;PI = %.4f<span class="es0">\n</span>&quot;</span>, pi<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;PI = %f<span class="es0">\n</span>&quot;</span>, pi<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;End this line<span class="es0">\n</span>&quot;</span>; &nbsp;<span class="co1">// You can still use \n with cout if you prefer</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// it instead of &#8216;endl&#8217;.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// I mostly use printf() with SDL. &nbsp;SDL has it set that printf() and </span><br />
&nbsp; &nbsp; <span class="co1">// cout send text straight to an output text file to be read later. &nbsp;This </span><br />
&nbsp; &nbsp; <span class="co1">// is pretty useful for debugging (fixing) my programs.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; For certain special characters, you&#8217;ll have to use a backslash to show C++<br />
&nbsp; &nbsp; that you want the actual character. &nbsp;Plus, since backslash does this special <br />
&nbsp; &nbsp; trick, you have to use a backslash to escape a backslash.<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Junk coming&#8230; <span class="es0">\&quot;</span>c:<span class="es0">\\</span>cow<span class="es0">\\</span><span class="es0">\&quot;</span>&quot;</span> &lt;&lt; endl; <br />
&nbsp; &nbsp; <span class="co1">// That&#8217;s about all you need to know about input/output until we </span><br />
&nbsp; &nbsp; <span class="co1">// get to classes. &nbsp;It wasn&#8217;t so bad, was it?</span><br />
&nbsp; &nbsp; <br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void stdPut()</span></p>
<p>
<span class="kw4">void</span> logic<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; Logic is the key to having a program that does what you want it to do.<br />
&nbsp; &nbsp; C++ has all you need to build up your ideas into code. &nbsp;Booleans are <br />
&nbsp; &nbsp; commonly used in many languages. &nbsp;A boolean has only two possible <br />
&nbsp; &nbsp; values: 0 or 1.<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw4">bool</span> test = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// &#8216;if&#8217; is C++&#8217;s basic conditional. &nbsp;You use conditionals every time you say </span><br />
&nbsp; &nbsp; <span class="co1">// &quot;If pigs could fly, then I&#8217;d be a pig, or else I&#8217;d fly instead.&quot;.</span><br />
&nbsp; &nbsp; <span class="co1">// &quot;if&quot; must always be followed by a parenthesized statement that evaluates </span><br />
&nbsp; &nbsp; <span class="co1">// to a boolean. &nbsp;&quot;true&quot; is a C++ keyword that evaluates to 1. &nbsp;&quot;false&quot;, </span><br />
&nbsp; &nbsp; <span class="co1">// of course, evaluates to 0. &nbsp;Any integer can be used for the boolean </span><br />
&nbsp; &nbsp; <span class="co1">// test statement because C++ thinks of 0 as meaning false and any other </span><br />
&nbsp; &nbsp; <span class="co1">// number meaning true. &nbsp;Since &quot;true&quot; (or 1) is the test parameter, the </span><br />
&nbsp; &nbsp; <span class="co1">// &quot;then&quot; part will always execute. &nbsp;In C++, the &quot;then&quot; part can be either </span><br />
&nbsp; &nbsp; <span class="co1">// a _line_ of code or a _block_ of code. &nbsp;This one uses a line.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;First <span class="es0">\&quot;</span>if<span class="es0">\&quot;</span> statement&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Here&#8217;s a block. &nbsp;Blocks are multiple code lines enclosed in {}</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>test<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Second <span class="es0">\&quot;</span>if<span class="es0">\&quot;</span> statement&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Test = &quot;</span> &lt;&lt; test &lt;&lt; <span class="st0">&quot; now.&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// The &quot;else&quot; keyword executes it&#8217;s code when a directly preceding if&#8217;s </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// test is false.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;That was false?&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>
&nbsp; &nbsp; &nbsp;<span class="co1">// The operators == &amp;&amp; || != &gt; &lt; &gt;= and &lt;= compare two arguments and </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// return a boolean value. The operator ! negates a boolean value (changes </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// it from true to false or from false to true).</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">4</span> == <span class="nu0">3</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;equals&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">1</span> &amp;&amp; <span class="nu0">0</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;and&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">0</span> || <span class="nu0">1</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;or&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="st0">&quot;table&quot;</span> != <span class="st0">&quot;table&quot;</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;not equal&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">22</span> &gt; <span class="nu0">344</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;greater than&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">1</span> &lt; <span class="br0">&#40;</span><span class="nu0">1</span><span class="nu0">+1</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;less than&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">12</span> &gt;= <span class="nu0">12</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;greater than or equals&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">72</span> &lt;= <span class="nu0">56</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;less than or equals&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>!test<span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;not&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// For fun, try making all of the above statements evaluate to true and </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// print their messages without changing the operators.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> difficulty;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Choose a difficulty level: &quot;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cin</span> &gt;&gt; difficulty;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>difficulty &lt; <span class="nu0">0</span><span class="br0">&#41;</span> || <span class="br0">&#40;</span>difficulty &gt; <span class="nu0">5</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;That was not a choice for difficulty.&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; endl &lt;&lt; <span class="st0">&quot;No switch for you!&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="coMULTI">/* The &quot;switch&quot; statement is a way to check a single variable for lots <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;of values. &nbsp;After each &quot;case&quot; is the value it checks for. &nbsp;Every case <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;set needs a &quot;break&quot; statement. &nbsp;&quot;break&quot; is a way to end a control <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;structure, like this &quot;switch&quot; statement. &nbsp;Once &quot;break&quot; gets executed, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;none of the other cases are checked. &nbsp;The default statement is <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;used when none of the cases match. */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">switch</span><span class="br0">&#40;</span>difficulty<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">case</span> <span class="nu0">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Diff = 1&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">case</span> <span class="nu0">2</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Diff = 2&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">case</span> <span class="nu0">3</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Diff = 3&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">default</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;That&#8217;s too difficult!&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">//END void logic()</span></p>
<p>
<span class="kw4">void</span> controls<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Though &quot;switch&quot; is a control statement, it is hardly as popular as the <br />
&nbsp; &nbsp; &nbsp;loop statements: while, for, and do while. */</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> countDown = <span class="nu0">10</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &quot;while&quot; is the most common loop. &nbsp;It uses a boolean test, just like </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the &quot;if&quot; statement, but it executes it&#8217;s block of code and then performs </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the test again. &nbsp;If it&#8217;s still true, it keeps looping. &nbsp;Try to be </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// careful with loops. &nbsp;A little typo can cause an infinite loop which can </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// crash the program (alerting the operating system) or cause you to ctrl-</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// alt-delete the whole thing. &nbsp;Typically, you don&#8217;t want to use something </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// that always evaluates to &quot;while (1)&quot;. &nbsp;Even so, you can use the &quot;break&quot; </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// command to escape such a loop.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span>countDown&#8211;<span class="br0">&#41;</span> &nbsp;<span class="co1">// Run the program and check this one out.</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; countDown &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// It performs the test before starting the loop, so the full countdown </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// isn&#8217;t shown.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">system</span><span class="br0">&#40;</span><span class="st0">&quot;PAUSE&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;countDown = <span class="nu0">10</span>; &nbsp;<span class="co1">// Reset the count.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &quot;do while&quot; executes the loop at least once before testing it.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">do</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; countDown &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span>countDown&#8211;<span class="br0">&#41;</span>; &nbsp;<span class="co1">// Since &quot;while&quot; doesn&#8217;t have a block after it, &nbsp;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// you&#8217;ll need a semicolon to end the statement.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> letter = <span class="st0">&#8216;a&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> n = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span>n &lt; <span class="nu0">26</span><span class="br0">&#41;</span> &nbsp;<span class="co1">// A counting loop.</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; letter;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;letter++; &nbsp;<span class="co1">// Did you know you can do arithmetic on letters?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;n++;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;letter = <span class="st0">&#8216;A&#8217;</span>; &nbsp;<span class="co1">// Reset letter.</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Most programming languages include a convenience loop statement named <br />
&nbsp; &nbsp; &nbsp;&quot;for&quot;. &nbsp;&quot;for&quot; has three distinct code parts instead of a test. &nbsp;The first <br />
&nbsp; &nbsp; &nbsp;part is the initialization, where you set a variable. &nbsp;The second part <br />
&nbsp; &nbsp; &nbsp;is the test statement. &nbsp;The last part is the iterator, where it changes a <br />
&nbsp; &nbsp; &nbsp;variable. &nbsp;Semicolons are always used to separate these parts. &nbsp;So the <br />
&nbsp; &nbsp; &nbsp;initialization is executed only once, right at the start while both the <br />
&nbsp; &nbsp; &nbsp;test and the iterator are run after each loop.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;init<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; test<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; iterator<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; <span class="nu0">26</span>; i++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; letter;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;letter++;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void controls()</span></p>
<p>
<span class="kw4">void</span> arrays<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;C++ has a good way to store lots of information. &nbsp;It also does this very <br />
&nbsp; &nbsp; &nbsp;efficiently.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// You can declare multiple variables like this:</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> num1, num2, num3, num4, num5;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// And you can initialize them together:</span><br />
&nbsp; &nbsp; &nbsp;num1 = num2 = num3 = num4 = num5 = <span class="nu0">3</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; num1 &lt;&lt; <span class="st0">&quot;, &quot;</span> &lt;&lt; num2 &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// But you wouldn&#8217;t want to clutter your code with tons of variables&#8230;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Why not use an array?</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> someArray<span class="br0">&#91;</span><span class="nu0">40</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Arrays use this notation where the number inside the square brackets <br />
&nbsp; &nbsp; &nbsp;indicates how many variables you&#8217;re storing. <br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Arrays can be initialized right away&#8230;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> otherArray<span class="br0">&#91;</span><span class="nu0">10</span><span class="br0">&#93;</span> = <span class="br0">&#123;</span><span class="nu0">0</span><span class="br0">&#125;</span>; &nbsp;<span class="co1">// Shortcut to set all to 0</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> anArray<span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="br0">&#123;</span> <span class="nu0">7</span>, <span class="nu0">1</span>, <span class="nu0">4</span>, <span class="nu0">16</span>, <span class="nu0">94</span> <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Or each element can be initialized later&#8230;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// To access a particular element of the array, we indicate the index.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Index 3: &quot;</span> &lt;&lt; anArray<span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span> &lt;&lt; endl;</p>
<p>&nbsp; &nbsp; &nbsp;<span class="co1">// The confusing part is that the indices of an array start at 0. &nbsp;That </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// means that the above statement would print 16.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp; &nbsp; &nbsp; &nbsp; indices: &nbsp;0 &nbsp;1 &nbsp;2 &nbsp; 3 &nbsp; 4</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// int anArray[] = { 7, 1, 4, 16, 94 };</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; <span class="nu0">4</span> ; i++<span class="br0">&#41;</span> &nbsp;<span class="co1">// Start at index zero.</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; anArray<span class="br0">&#91;</span>i<span class="br0">&#93;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Be careful not to overrun the array. &nbsp;You&#8217;ll encounter weird results </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// and very likely cause an error if you use anArray[7] when anArray has </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// only five elements.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// C++ can use arrays to form strings of characters. &nbsp;Instead of filling </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// all of the available spaces with data, however, it needs to use the </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// last element as an &quot;end of string&quot; character, &#8216;\0&#8242;. &nbsp;This is also </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// called the &quot;null terminator&quot;, &quot;null zero&quot;, or &quot;zero terminator&quot;. &nbsp;If </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the compiler doesn&#8217;t find this character in the array, it keeps </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// reading (or writing!) into other parts of memory (overruns the array).</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> someString<span class="br0">&#91;</span><span class="nu0">6</span><span class="br0">&#93;</span> = <span class="st0">&quot;Hello&quot;</span>; &nbsp;<span class="co1">// This means to only use 5 letters!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// You can assign literal strings to these arrays.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// C++ will take care of the null terminator</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// That&#8217;s too confusing, so you can make a character array figure out how </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// much memory it needs on it&#8217;s own, but only when you first create </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the array.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> anotherString<span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="st0">&quot;How many elements does this have?&quot;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> count = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> i = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span>anotherString<span class="br0">&#91;</span>i<span class="br0">&#93;</span> != <span class="st0">&#8216;<span class="es0">\0</span>&#8216;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i++;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;anotherString has &quot;</span> &lt;&lt; count &lt;&lt; <span class="st0">&quot; elements.&quot;</span> &lt;&lt; endl;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void arrays()</span></p>
<p>
<span class="kw4">void</span> stringClass<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Stuff in this section requires #include &lt;string&gt;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;C++ Strings are way easier than C strings to understand and use. &nbsp;Since <br />
&nbsp; &nbsp; &nbsp;it is a string class, it is loaded up (down?) with tons of ways to work <br />
&nbsp; &nbsp; &nbsp;with the information. &nbsp;We&#8217;ll focus on the basics that will get you almost <br />
&nbsp; &nbsp; &nbsp;anywhere with C++ strings.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;string word;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;word = <span class="st0">&quot;We shall overcome!&quot;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Particular characters in strings can be accessed just like arrays.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> &lt;&lt; <span class="st0">&quot; &quot;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">5</span><span class="br0">&#93;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">6</span><span class="br0">&#93;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">7</span><span class="br0">&#93;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot; &quot;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">9</span><span class="br0">&#93;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">12</span><span class="br0">&#93;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">13</span><span class="br0">&#93;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">17</span><span class="br0">&#93;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// You can easily concatenate (combine) strings with the + operator.</span><br />
&nbsp; &nbsp; &nbsp;string word2 = <span class="st0">&quot; &nbsp;Unless otherwise noted.&quot;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word + word2 &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;word += word2;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Because string is a class, objects (such as word and word2) each have <br />
&nbsp; &nbsp; &nbsp;access to special string functions. &nbsp;They are accessed with the dot (.) <br />
&nbsp; &nbsp; &nbsp;operator.<br />
&nbsp; &nbsp; &nbsp;The empty() function returns a boolean value telling whether or <br />
&nbsp; &nbsp; &nbsp;not the string is empty (useful for loops).<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> word.<span class="me1">empty</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;EMPTY!&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// The first argument to erase() is the index to start from. &nbsp;The second </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// argument is the number of characters to erase. &nbsp;All characters after </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the erased ones slide right down to replace them.</span><br />
&nbsp; &nbsp; &nbsp;word.<span class="me1">erase</span><span class="br0">&#40;</span><span class="nu0">3</span>, <span class="nu0">6</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// insert() takes an index and a string, squeezing that string into the </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// point the index represents.</span><br />
&nbsp; &nbsp; &nbsp;word.<span class="me1">insert</span><span class="br0">&#40;</span><span class="nu0">7</span>, word2<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// length() takes no arguments and just returns the size of the string.</span><br />
&nbsp; &nbsp; &nbsp;word.<span class="me1">erase</span><span class="br0">&#40;</span> <span class="nu0">0</span>, word2.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Strings can be compared with the ==, &lt;, &gt;, &lt;=, &gt;=, and != operators.<br />
&nbsp; &nbsp; &nbsp;The quirk here is that it&#8217;s alphabetic comparison.<br />
&nbsp; &nbsp; &nbsp;&quot;abc&quot; is greater than &quot;jvc&quot;<br />
&nbsp; &nbsp; &nbsp;&quot;ghi&quot; &lt; &quot;ghj&quot;<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>word != word2<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;word != word2&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p><span class="br0">&#125;</span> &nbsp;<span class="co1">// END void stringClass()</span></p>
<p>
<span class="kw4">void</span> randomStuff<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;The rand() function is C++&#8217;s way of getting a random number. &nbsp;In main(), we <br />
&nbsp; &nbsp; &nbsp;used a function call that looked like this: srand(time(NULL))<br />
&nbsp; &nbsp; &nbsp;&quot;srand&quot; stands for &quot;seed random [number generator]&quot;. &nbsp;Seeding something <br />
&nbsp; &nbsp; &nbsp;means to initialize it to a value. &nbsp;&quot;srand&quot; gives rand() a different <br />
&nbsp; &nbsp; &nbsp;starting place than it has by default. &nbsp;The reason this matters is because <br />
&nbsp; &nbsp; &nbsp;rand() is not actually random. &nbsp;rand() draws from a big list of numbers and <br />
&nbsp; &nbsp; &nbsp;gives them to you in order. &nbsp;Without seeding rand(), it will always start <br />
&nbsp; &nbsp; &nbsp;at the same place. &nbsp;In fact, this won&#8217;t do the job alone, but when we seed <br />
&nbsp; &nbsp; &nbsp;rand() with a constantly changing number (aka time), then we can get fairly<br />
&nbsp; &nbsp; &nbsp;reliable random numbers. &nbsp;Here&#8217;s where one great use of the modulus (%) <br />
&nbsp; &nbsp; &nbsp;operator is. &nbsp;We can use it to restrict the range of possible numbers.<br />
&nbsp; &nbsp; &nbsp;Modulo will return the remainder of integer division. &nbsp;It&#8217;s based on <br />
&nbsp; &nbsp; &nbsp;division, so the result ranges only from 0 to the right side value.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> rando = <span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">99</span> + <span class="nu0">1</span>; &nbsp;<span class="co1">// Gives a number 1-100</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; rando &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;rando = <span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">2</span>; &nbsp;<span class="co1">// 0-1</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; rando &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;rando = <span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">65</span>; &nbsp;<span class="co1">// 0-64</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; rando &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;rando = <span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">4000</span>; &nbsp;<span class="co1">// 0-3999</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; rando &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> strength = <span class="br0">&#40;</span><span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">6</span> + <span class="nu0">1</span><span class="br0">&#41;</span> + <span class="br0">&#40;</span><span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">6</span> + <span class="nu0">1</span><span class="br0">&#41;</span> + <span class="br0">&#40;</span><span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">6</span> + <span class="nu0">1</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// 3-18</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> damage = <span class="nu0">1</span> + <span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%strength;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> low = <span class="nu0">55</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> high = <span class="nu0">231</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> lowToHigh = <span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="br0">&#40;</span>high + <span class="nu0">1</span> &#8211; low<span class="br0">&#41;</span> + low;<br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void randomStuff()</span></p>
<p>
<span class="kw4">void</span> structKeyword<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Structures are handy little things similar to arrays in concept. &nbsp;Arrays <br />
&nbsp; &nbsp; &nbsp;are collections of variables of the same type, whereas structures hold a <br />
&nbsp; &nbsp; &nbsp;bunch of variables of any types (even other structures) that you want to <br />
&nbsp; &nbsp; &nbsp;have grouped together.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">struct</span> ITEM &nbsp;<span class="co1">// How about an item structure?</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> type; &nbsp;<span class="co1">// Which item is it?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> uses; &nbsp;<span class="co1">// How many times can I use it?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">bool</span> ready; &nbsp;<span class="co1">// Can I use it now?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">bool</span> equipped; &nbsp;<span class="co1">// Do I have it at hand?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string itemName;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* &nbsp;Structs are like type definitions. &nbsp;Now that we told the compiler what <br />
&nbsp; &nbsp; &nbsp;this struct is, we can make an instance of one.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;ITEM gravyBoat;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;gravyBoat.<span class="me1">type</span> = <span class="nu0">42</span>;<br />
&nbsp; &nbsp; &nbsp;gravyBoat.<span class="me1">uses</span> = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp;gravyBoat.<span class="me1">ready</span> = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp;gravyBoat.<span class="me1">equipped</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;gravyBoat.<span class="me1">itemName</span> = <span class="st0">&quot;gravyboat&quot;</span>;<br />
&nbsp; &nbsp; &nbsp;</p>
<p>&nbsp; &nbsp; &nbsp;<span class="kw4">struct</span> PLAYER &nbsp;<span class="co1">// But what use is an item without a player to use it?</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">int</span> hp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">int</span> magic;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">bool</span> active;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">int</span> location;<br />
&nbsp; &nbsp; &nbsp; &nbsp;ITEM item;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">struct</span> GRID &nbsp;<span class="co1">// Here&#8217;s a type of grid structure.</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">int</span> x,y; &nbsp; <span class="co1">// We&#8217;ll store some position data</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">bool</span> active; &nbsp;<span class="co1">// Does the grid position exist (say, on this map)?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">bool</span> occupied; &nbsp;<span class="co1">// Is there something already in this grid position?</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;GRID grid<span class="br0">&#91;</span><span class="nu0">15</span><span class="br0">&#93;</span>; &nbsp;<span class="co1">// Now we have a grid with 16 possible positions.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;PLAYER player1; &nbsp;<span class="co1">// I don&#8217;t feel like initializing everything, but remember </span><br />
&nbsp; &nbsp; &nbsp;player1.<span class="me1">hp</span> = <span class="nu0">50</span>; <span class="co1">// &nbsp; to never use something that hasn&#8217;t been initialized.</span><br />
&nbsp; &nbsp; &nbsp;player1.<span class="me1">location</span> = <span class="nu0">10</span>;<br />
&nbsp; &nbsp; &nbsp;player1.<span class="me1">item</span>.<span class="me1">uses</span> = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp;player1.<span class="me1">item</span>.<span class="me1">itemName</span> = <span class="st0">&quot;gravyboat&quot;</span>;<br />
&nbsp; &nbsp; &nbsp;grid<span class="br0">&#91;</span>player1.<span class="me1">location</span><span class="br0">&#93;</span>.<span class="me1">occupied</span> = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;player1.<span class="me1">item</span>.<span class="me1">uses</span>&#8211;;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;You&#8217;ve been &quot;</span> &lt;&lt; player1.<span class="me1">item</span>.<span class="me1">itemName</span> &lt;&lt; <span class="st0">&quot;ed! &nbsp;&#8230;At grid number &quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; player1.<span class="me1">location</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Structures are the precursors to a very important aspect of C++ and most <br />
&nbsp; &nbsp; &nbsp;other programming languages&#8230; &nbsp;Classes. &nbsp;Classes will be covered in just <br />
&nbsp; &nbsp; &nbsp;a few sections, but I&#8217;ll give you a little clue to one of their powerful <br />
&nbsp; &nbsp; &nbsp;features&#8230; &nbsp;Classes are like structures that can contain functions.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void structKeyword()</span></p>
<p>
<span class="kw4">void</span> recursion<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Recursion is a technique that requires a lot of practice to really <br />
&nbsp; &nbsp; &nbsp;figure it out. &nbsp;Recursion is when a function calls itself. &nbsp;There are <br />
&nbsp; &nbsp; &nbsp;literally a limitless number of reasons to do this. &nbsp;We&#8217;ll check out a few. &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Take a look below recursion() for the definition of this function:</span><br />
&nbsp; &nbsp; &nbsp;countDown<span class="br0">&#40;</span><span class="nu0">10</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// As you can see, countDown() takes an integer and prints all the numbers </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// down to zero. &nbsp;This is an example of a recursive function that relies </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// on &quot;side-effects&quot; to produce a result. &nbsp;Side-effects are those that </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// come about by running a function and not just by dropping and catching</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// functions&#8217; return values.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// This one is a recursive function that hints at the power of recursion.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> number = <span class="nu0">5</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> answer = fact<span class="br0">&#40;</span>number<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Factorial of &quot;</span> &lt;&lt; number &lt;&lt; <span class="st0">&quot; equals &quot;</span> &lt;&lt; answer &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void recursion()</span></p>
<p>
<span class="kw4">void</span> countDown<span class="br0">&#40;</span><span class="kw4">int</span> count<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; count &lt;&lt; endl;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>count &gt; <span class="nu0">0</span><span class="br0">&#41;</span> countDown<span class="br0">&#40;</span>&#8211;count<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> fact<span class="br0">&#40;</span><span class="kw4">int</span> num<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>num == <span class="nu0">0</span><span class="br0">&#41;</span> &nbsp;<span class="co1">// Ends the recursion&#8230; &nbsp;We know that 0! = 1</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">1</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">return</span> num * fact<span class="br0">&#40;</span>num &#8211; <span class="nu0">1</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// A recursive definition of factorial</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// n! = n * (n-1) * (n-2) * &#8230; * 1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// The easiest ideas to model are those that </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// have a sequence like factorial does.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// num * fact(num &#8211; 1) could be rewritten as:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// &nbsp;n &nbsp;* &nbsp;(n-1)!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// As you can see (or will hopefully see), every recursive function has two </span><br />
&nbsp; &nbsp; <span class="co1">// main parts. &nbsp;It needs something to recurse around (num*fact(num-1)) and </span><br />
&nbsp; &nbsp; <span class="co1">// it needs something to end the recursion (if (num==0) return 1;). &nbsp;Lots </span><br />
&nbsp; &nbsp; <span class="co1">// of recursive functions have other parts, but they all have those two </span><br />
&nbsp; &nbsp; <span class="co1">// in common.</span><br />
<span class="br0">&#125;</span></p>
<p>
<span class="kw4">void</span> pointers<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Computers store running program data in what is called &quot;Random Access <br />
&nbsp; &nbsp; &nbsp;Memory&quot; or RAM. &nbsp;How does the computer keep track of all the stuff in RAM? &nbsp;<br />
&nbsp; &nbsp; &nbsp;It knows that the memory is set up so that a single &#8216;memory location&#8217; can <br />
&nbsp; &nbsp; &nbsp;be accessed by referring to its &#8216;memory address&#8217;. &nbsp;C++ has a very low-level<br />
&nbsp; &nbsp; &nbsp;(very close to how the computer actually works&#8230; and consequently kind of <br />
&nbsp; &nbsp; &nbsp;hard to deal with, but very efficient) way of working with memory <br />
&nbsp; &nbsp; &nbsp;addresses. &nbsp;We can store the address of some information in a variable <br />
&nbsp; &nbsp; &nbsp;called a &#8216;pointer&#8217; (it points to actual data).<br />
&nbsp; &nbsp; &nbsp;*/</span> <br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span>* intPointer; &nbsp;<span class="co1">// This can point to an integer&#8230; but doesn&#8217;t yet.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span>* charPointer; &nbsp;<span class="co1">// This can point to a character.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Pointers are denoted with a type like: int*</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// This is their actual type now. &nbsp;Instead of being just of type &#8216;int&#8217;, </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// intPointer is of type &#8216;int*&#8217;. &nbsp;It&#8217;s best not to confuse this use of the </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// asterisk &#8216;*&#8217; with what is used next. &nbsp;Think of this as part of the </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// type&#8217;s name.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> integer;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> character;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;intPointer = &amp;integer; &nbsp;<span class="co1">// Here we use the ampersand &#8216;&amp;&#8217; operator. &nbsp;This </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// returns the address of the variable it is put in</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// front of. &nbsp;You can think of this as the &quot;address of&quot; </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// operator. &nbsp;Make sure you initialize your pointers to </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// an actual address like this before you try to use them.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// That could cause a crash of your program. &nbsp;Remember </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// that because it is a pretty common mistake.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;charPointer = &amp;character;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;*intPointer = <span class="nu0">5</span>; &nbsp;<span class="co1">// This assigns the value 5 to the place that is pointed </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// at by intPointer. &nbsp;This place is also named by </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// the variable &quot;integer&quot;.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* &nbsp;The asterisk &#8216;*&#8217; or star is called the dereference operator. &nbsp;This means that <br />
&nbsp; &nbsp; &nbsp;it actually returns the data held in memory. &nbsp;*intPointer is like the <br />
&nbsp; &nbsp; &nbsp;variable name of the data in that location. &nbsp;You can remember what the * <br />
&nbsp; &nbsp; &nbsp;does if you think of it as the &quot;stuff at&quot; operator. &nbsp;Then it means the <br />
&nbsp; &nbsp; &nbsp;&quot;stuff at&quot; the address intPointer. &nbsp;Now we can really use these pointers. &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;*charPointer = <span class="st0">&#8216;g&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>*intPointer / <span class="nu0">5</span> == <span class="nu0">1</span><span class="br0">&#41;</span> *charPointer = <span class="st0">&#8216;p&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Now why don&#8217;t we just use normal variables for this? &nbsp;Well, we would.<br />
&nbsp; &nbsp; &nbsp;But if you&#8217;ve just been following along in this tutorial, there&#8217;s something <br />
&nbsp; &nbsp; &nbsp;you probably haven&#8217;t noticed. &nbsp;Let&#8217;s call a function.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> a = <span class="nu0">3</span>;<br />
&nbsp; &nbsp; &nbsp;square<span class="br0">&#40;</span>a<span class="br0">&#41;</span>; &nbsp;<span class="co1">// This function takes an int and sets it to equal itself </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// times itself. &nbsp;It&#8217;s declared void, so there&#8217;s no </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// returning data.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; a &lt;&lt; endl; &nbsp;<span class="coMULTI">/* This prints &#8216;3&#8242;, the exact value we passed in! &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; This is because we did something called <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8216;pass-by-value&#8217;. &nbsp;We passed the variable &#8216;a&#8217; to the <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function square(). &nbsp;square() made a copy of the <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value stored in &#8216;a&#8217;. &nbsp;As you can see below this <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; section, the function square() names this copied <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; variable &#8216;x&#8217;. &nbsp;It then sets &#8216;x&#8217; to equal x * x.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Obviously, we&#8217;re not working with &#8216;a&#8217; anymore. &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; That&#8217;s why it didn&#8217;t change when we wanted it to.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; So we&#8217;d rather use &#8216;pass-by-reference&#8217;. Take a look<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; at squarePtr(). &nbsp;Here we&#8217;re sending the address of a <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; variable. &nbsp;Let&#8217;s try using it.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span>* aPtr = &amp;a;; &nbsp;<span class="co1">// Here&#8217;s a pointer that we&#8217;ll assign to the address of &#8216;a&#8217;.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;squarePtr<span class="br0">&#40;</span>aPtr<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; a &lt;&lt; endl; &nbsp;<span class="co1">// Now we get our 9!</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;squarePtr<span class="br0">&#40;</span>&amp;a<span class="br0">&#41;</span>; &nbsp;<span class="co1">// Another way to do this is to directly send the address</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// of &#8216;a&#8217;. &nbsp;This doesn&#8217;t require an extra pointer to use.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; a &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// A few uses for pointers will be expanded in the dynamicMemory() section</span><br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void pointers()</span></p>
<p><span class="kw4">void</span> square<span class="br0">&#40;</span><span class="kw4">int</span> x<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; x *= x;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> squarePtr<span class="br0">&#40;</span><span class="kw4">int</span>* x<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; *x *= *x; &nbsp;<span class="co1">// Kinda messy here, but we&#8217;re working with the &quot;stuff at&quot; x.</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> references<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;References are very much like pointers, but they make functions like <br />
&nbsp; &nbsp; &nbsp;squarePtr() much easier to design. &nbsp;A reference is just another name for <br />
&nbsp; &nbsp; &nbsp;the memory address of a variable.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> z = <span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;int&amp; aReference = z; &nbsp;<span class="co1">// Here&#8217;s a reference.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;For the most part, references are used for functions that will take and <br />
&nbsp; &nbsp; &nbsp;change a given variable. &nbsp;Another use is for &#8216;aliases&#8217;, but they aren&#8217;t <br />
&nbsp; &nbsp; &nbsp;that useful, so I won&#8217;t go into them any further.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;Let&#8217;s try our nice, new, and clean squareRef() function.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> var = <span class="nu0">12</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;squareRef<span class="br0">&#40;</span>var<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; var &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// As you can see, I didn&#8217;t have to mess with my variable at all!</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// How convenient!</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void references()</span></p>
<p><span class="kw4">void</span> squareRef<span class="br0">&#40;</span>int&amp; x<span class="br0">&#41;</span> &nbsp;<span class="co1">// Notice the difference?</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;x *= x;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> functionOverloading<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Function overloading is much easier and more useful than it sounds.<br />
&nbsp; &nbsp; &nbsp;Overloading a function is to use the same function name for multiple <br />
&nbsp; &nbsp; &nbsp;functions. &nbsp;This comes in handy when you want to do similar tasks with <br />
&nbsp; &nbsp; &nbsp;different variable types without having to come up with (and remember) a <br />
&nbsp; &nbsp; &nbsp;unique name for each function.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;If you look at the &#8216;equal()&#8217; functions below this section, you can see <br />
&nbsp; &nbsp; &nbsp;some overloaded functions.<br />
&nbsp; &nbsp; &nbsp;We want to know if some integers are equal, but what if we don&#8217;t always <br />
&nbsp; &nbsp; &nbsp;know how many we&#8217;re working with?<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> a = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> b = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> c = <span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> cha = <span class="st0">&#8216;@&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> chb = <span class="st0">&#8216;@&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; equal<span class="br0">&#40;</span>a<span class="br0">&#41;</span> &lt;&lt; endl &nbsp;<span class="co1">// always true (1)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; equal<span class="br0">&#40;</span>a, b<span class="br0">&#41;</span> &lt;&lt; endl &nbsp;<span class="co1">// true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; equal<span class="br0">&#40;</span>a, b, c<span class="br0">&#41;</span> &lt;&lt; endl &nbsp;<span class="co1">// false!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; equal<span class="br0">&#40;</span>cha, chb<span class="br0">&#41;</span> &lt;&lt; endl; &nbsp;<span class="co1">// true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Thankfully, the compiler is smart enough to recognize it when you use <br />
&nbsp; &nbsp; &nbsp;different types and different numbers of arguments to functions. &nbsp;That&#8217;s<br />
&nbsp; &nbsp; &nbsp;what allows for this feature. &nbsp;Different return types will not disambiguate<br />
&nbsp; &nbsp; &nbsp;the call on their own, though, so don&#8217;t try just that.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void functionOverloading()</span></p>
<p><span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">int</span> a<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">1</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">int</span> a, <span class="kw4">int</span> b<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> a == b;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">int</span> a, <span class="kw4">int</span> b, <span class="kw4">int</span> c<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> a == b &amp;&amp; b == c;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">char</span> a, <span class="kw4">char</span> b<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> a == b;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> scope<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Scope is a term for what variables you have access to at a certain <br />
&nbsp; &nbsp; &nbsp;point in a program.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;We&#8217;ve already talked a little about the &#8216;global&#8217; scope.<br />
&nbsp; &nbsp; &nbsp;Global variables can be used anywhere in your program because they were <br />
&nbsp; &nbsp; &nbsp;declared outside of any function.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">//Global variable &#8216;aLetter&#8217;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; aLetter &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> tempLetter = aLetter;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// This just created a variable that is &#8216;local&#8217; to the function scope().</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// When this function ends, this variable is automatically deleted.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// No other function can use &#8216;tempLetter&#8217; unless I pass it as an argument.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Here I&#8217;m making another local variable and calling equal() with two </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// passed variables.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">bool</span> test = equal<span class="br0">&#40;</span>tempLetter, aLetter<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span><span class="br0">&#40;</span>test<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;These are equal!&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// You&#8217;ll need to be aware of the scope you&#8217;re working in when it comes </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// to classes in a couple of sections&#8230;</span><br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void scope()</span></p>
<p>
<span class="kw4">void</span> dynamicMemory<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Dynamic memory is a term for program memory that is not stored at compile <br />
&nbsp; &nbsp; &nbsp;time. &nbsp;With the usual variable declarations that we&#8217;ve been working with, <br />
&nbsp; &nbsp; &nbsp;the variables have been hard-coded into the program and will work exactly <br />
&nbsp; &nbsp; &nbsp;the same for every time the program is run. &nbsp;If we want to make variables <br />
&nbsp; &nbsp; &nbsp;that don&#8217;t automatically take up space or if, say, we don&#8217;t know how many <br />
&nbsp; &nbsp; &nbsp;variables we need (in the case of an array), then we need to use C++&#8217;s <br />
&nbsp; &nbsp; &nbsp;dynamic memory operators, &#8216;new&#8217; and &#8216;delete&#8217;.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;Simply put, &#8216;new&#8217; creates a new variable and &#8216;delete&#8217; frees the memory <br />
&nbsp; &nbsp; &nbsp;that is used by that variable so that the memory can be used again before <br />
&nbsp; &nbsp; &nbsp;the program closes.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;So, let&#8217;s try making an array of an unknown size.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> numElements = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">while</span><span class="br0">&#40;</span>numElements == <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;numElements = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Hey, tell me how many variables we&#8217;ll have.&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cin</span> &gt;&gt; numElements;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Arrays are actually pointers to the first element of the array, so we </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// can write them like pointers:</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span>* anArray = <span class="kw3">new</span> <span class="kw4">int</span><span class="br0">&#91;</span>numElements<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Let&#8217;s initialize the elements</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; numElements; i++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;anArray<span class="br0">&#91;</span>i<span class="br0">&#93;</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Now we can use them for whatever</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; numElements; i++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Element #&quot;</span> &lt;&lt; i &lt;&lt; <span class="st0">&quot; = &quot;</span> &lt;&lt; anArray<span class="br0">&#91;</span>i<span class="br0">&#93;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Then free the memory when we&#8217;re done.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">delete</span><span class="br0">&#91;</span><span class="br0">&#93;</span> anArray;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;To delete an entire array, you have to put those square brackets after<br />
&nbsp; &nbsp; &nbsp;delete. &nbsp;If you don&#8217;t, it will treat it like a pointer instead of an array <br />
&nbsp; &nbsp; &nbsp;and will only delete the first element.<br />
&nbsp; &nbsp; &nbsp;*/</span></p>
<p>&nbsp; &nbsp; &nbsp;<span class="co1">// So, pointers to any other type work like that.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">double</span>* dPtr;<br />
&nbsp; &nbsp; &nbsp;dPtr = <span class="kw3">new</span> <span class="kw4">double</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Use it&#8230;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Then delete it.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">delete</span> dPtr;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Using &#8216;delete&#8217; effectively is the main concern for many programs written <br />
&nbsp; &nbsp; &nbsp;in C++. &nbsp;If an allocated resource is not freed, then the memory is not <br />
&nbsp; &nbsp; &nbsp;usable until the program ends. &nbsp;This is called a &quot;memory leak&quot;. &nbsp;Always <br />
&nbsp; &nbsp; &nbsp;remember to delete every dynamic variable. &nbsp;Some programmers even write <br />
&nbsp; &nbsp; &nbsp;a delete statement immediately whenever they write a new statement so that <br />
&nbsp; &nbsp; &nbsp;they don&#8217;t forget.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">struct</span> Something<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> type;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> number;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;Something* number1 = <span class="kw3">new</span> Something;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;number1-&gt;type = <span class="nu0">4</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; number1-&gt;type &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Here we used a great little operator, the arrow operator.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// This is a shortcut to replace the asterisk-dot combo.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// If we didn&#8217;t use this, we would write:</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(*number).type = 4;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp; Since the dot operator has a higher precedence than the dereference</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp; operator, the arrow is much cleaner (and makes me think of &quot;pointer&quot;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp; anyway). &nbsp;The arrow &quot;points&quot; to a member.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#40;</span>*number1<span class="br0">&#41;</span>.<span class="me1">type</span> = <span class="nu0">20</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="br0">&#40;</span>*number1<span class="br0">&#41;</span>.<span class="me1">type</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">delete</span> number1;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void dynamicMemory()</span></p>
<p>
<span class="kw4">void</span> classes<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">//See ClassName.h, the #included header file for the details</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Let&#8217;s just go through some object handling now.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;ClassName* object = <span class="kw3">new</span> ClassName;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Use that arrow operator to work in the scope of the object.</span><br />
&nbsp; &nbsp; &nbsp;object-&gt;data = <span class="nu0">3</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;7 * 2 = &quot;</span> &lt;&lt; object-&gt;fn<span class="br0">&#40;</span><span class="nu0">7</span><span class="br0">&#41;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Data * 2 = &quot;</span> &lt;&lt; object-&gt;fn<span class="br0">&#40;</span>object-&gt;data<span class="br0">&#41;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;object-&gt;cow<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;InheritsFromClassName* anotherObject = <span class="kw3">new</span> InheritsFromClassName;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// This still has the members of the base class.</span><br />
&nbsp; &nbsp; &nbsp;anotherObject-&gt;cow<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// But it also has it&#8217;s own stuff&#8230;</span><br />
&nbsp; &nbsp; &nbsp;anotherObject-&gt;notCow<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Here&#8217;s a great way to work with inherited classes.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// It&#8217;s part of a topic called &#8216;Polymorphism&#8217;.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;ClassName** array = <span class="kw3">new</span> ClassName*<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>; &nbsp;<span class="co1">// Will hold two pointers to </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// objects of type ClassName.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;array<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> = object; &nbsp;<span class="co1">// This is a ClassName object</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;array<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> = anotherObject; &nbsp;<span class="co1">// Hey! &nbsp;This is too (deep down somewhere)!</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;array<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>-&gt;cow<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Now we can cow() together in an array!</span><br />
&nbsp; &nbsp; &nbsp;array<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>-&gt;cow<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// But only with members of the array&#8217;s class type which </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// in this case is ClassName.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">//array[1]-&gt;notCow(); // ERROR!! &nbsp;The program sees this as just a ClassName</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;<span class="co1">// If you definitely know that array[1] is an InheritsFromClassName object,</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// then you can use &#8216;type-casting&#8217; to change it back into its full </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// abilities. &nbsp;Type-casting is done by writing a type within parentheses </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// and putting that before the variable that you want to convert to </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// that type.</span><br />
&nbsp; &nbsp; &nbsp;InheritsFromClassName* ifcn = <span class="br0">&#40;</span>InheritsFromClassName*<span class="br0">&#41;</span>array<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Now we can use the InheritsFromClassName members!</span><br />
&nbsp; &nbsp; &nbsp;ifcn-&gt;notCow<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// This also works for the primitive types. &nbsp;Try this if you get warnings </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// from your compiler about incompatible types.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">float</span> flo = <span class="nu0">6.0</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">//int q = flo; &nbsp;// Warning</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> z = <span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span>flo;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// C++ offers some other cool type-casting operators, but I&#8217;ll just show </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// you one pretty useful one.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// dynamic_cast returns what you would expect from a cast if it is valid, </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// but returns a NULL value if the cast is not valid.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp;dynamic_cast&lt;TYPE&gt;(VARIABLE)</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;ClassName* cn = <span class="kw3">new</span> ClassName<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Junk is a class I defined that just has an integer member named &#8216;j&#8217;</span><br />
&nbsp; &nbsp; &nbsp;Junk* junky = dynamic_cast&lt;Junk*&gt;<span class="br0">&#40;</span>cn<span class="br0">&#41;</span>; &nbsp;<span class="co1">// Not a valid cast</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span><span class="br0">&#40;</span>junky == <span class="kw2">NULL</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Junky NULL<span class="es0">\n</span>&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Junky not NULL?<span class="es0">\n</span>&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// dynamic_cast does its work on the fly, so don&#8217;t use it too much when </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// you need a program to run fast.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Well, this is the end of the tutorial. &nbsp;I hope you&#8217;ve been able to absorb <br />
&nbsp; &nbsp; &nbsp;all that and come away with some basic, working knowledge of C++ and <br />
&nbsp; &nbsp; &nbsp;programming in general. &nbsp;If you have any questions including any on other <br />
&nbsp; &nbsp; &nbsp;C++ topics (seen below), just catch me at GrimFang4@hotmail.com<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;It&#8217;s been a pleasure.<br />
&nbsp; &nbsp; &nbsp;-Jonny D<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Some Other Topics:<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;templates<br />
&nbsp; &nbsp; &nbsp; &nbsp;extern, file splitting<br />
&nbsp; &nbsp; &nbsp; &nbsp;linked lists, queues, stacks<br />
&nbsp; &nbsp; &nbsp; &nbsp;bubble sort<br />
&nbsp; &nbsp; &nbsp; &nbsp;abstract classes, virtual functions<br />
&nbsp; &nbsp; &nbsp; &nbsp;file streams<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void classes()</span></p>
<p><span class="kw4">void</span> choice<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> choice = <span class="nu0">-1</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span>choice != <span class="nu0">16</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; endl &lt;&lt; <span class="st0">&quot;Type a choice and press enter&#8230;&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; endl &lt;&lt; <span class="st0">&quot;1 = Operators &nbsp;&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;6 = String Class &nbsp; &nbsp;&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;11 = References&quot;</span> &lt;&lt; endl<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;2 = Std &#8216;Put &nbsp; &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;7 = Random Stuff &nbsp; &nbsp;&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;12 = Function Overloading&quot;</span> &lt;&lt; endl<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;3 = Logic &nbsp; &nbsp; &nbsp;&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;8 = Struct Keyword &nbsp;&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;13 = Scope&quot;</span> &lt;&lt; endl<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;4 = Controls &nbsp; &quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;9 = Recursion &nbsp; &nbsp; &nbsp; &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;14 = Dynamic Memory&quot;</span> &lt;&lt; endl<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;5 = Arrays &nbsp; &nbsp; &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;10 = Pointers &nbsp; &nbsp; &nbsp; &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;15 = Classes&quot;</span> &lt;&lt; endl<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 16 = Exit Testorial!&quot;</span> &lt;&lt; endl &lt;&lt; endl &lt;&lt; <span class="st0">&quot;-&gt;&quot;</span>;</p>
<p>&nbsp; &nbsp; &nbsp;<span class="kw3">cin</span> &gt;&gt; choice;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">switch</span><span class="br0">&#40;</span>choice<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;operators<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// +, -, *, /, =, etc.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">2</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stdPut<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// cout and cin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">3</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;logic<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// if (conditionals), &amp;&amp;, ||, ==, !, !=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">4</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;controls<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// switch, loops with while, do while, and for</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">5</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arrays<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// arrays[] as lists of variables</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">6</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stringClass<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Explaining the string class</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">7</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;randomStuff<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// rand() and random number generation</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">8</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;structKeyword<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Structures and struct, the precursor to classes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">9</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;recursion<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Looping without loops, function self-calls</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">10</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pointers<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// -&gt; memory addresses</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">11</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;references<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// An easier way to pass by reference</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">12</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;functionOverloading<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// functions with nearly the same name</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">13</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scope<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// local vs. global, precedence rules</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">14</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dynamicMemory<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// new and delete[] memory management</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">15</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;classes<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// what makes C++ better than C, classes and inheritance</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">16</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>choice != <span class="nu0">16</span><span class="br0">&#41;</span> <span class="kw3">system</span><span class="br0">&#40;</span><span class="st0">&quot;PAUSE&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void choice()</span><br />
&nbsp;</div>
<p><br/><br/><br />
Files:<br />
<a href="/tutorials/bluedinocode/jonny-ds-c-tutorial.zip">jonny-ds-c-tutorial.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/jonny-ds-c-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Pressing On</title>
		<link>http://www.sdltutorials.com/pressing-on/</link>
		<comments>http://www.sdltutorials.com/pressing-on/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 12:12:30 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=192</guid>
		<description><![CDATA[I&#8217;ve decided to not give up, please forgive me everyone for even thinking that. I don&#8217;t want to send the wrong message. When things get harder in life we need to keep pressing on. In this new year hopefully I can push out a lot more tutorials. I&#8217;ve also decided to show on the sidebar [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve decided to not give up, please forgive me everyone for even thinking that. I don&#8217;t want to send the wrong message. When things get harder in life we need to keep pressing on. In this new year hopefully I can push out a lot more tutorials. I&#8217;ve also decided to show on the sidebar upcoming tutorials in the works. Hopefully this will create some excitement. In addition, this year I plan to get a new website out and rolling for SDLTutorials.com, along with a new website for MetaCipher Studios, my upcoming indie game studio. It will not only be a place where I can put my own games to sell, but for all of you as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/pressing-on/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>SDL Net &#8211; Part 2 (User Tutorial)</title>
		<link>http://www.sdltutorials.com/sdl-net-part-2-user-tutorial/</link>
		<comments>http://www.sdltutorials.com/sdl-net-part-2-user-tutorial/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 12:01:30 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[C++ tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=190</guid>
		<description><![CDATA[The following user tutorial was created by Kahshoo-heem, for the purpose of expanding upon the SDLTutorials.com series, and expounding upon the use of SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for [...]]]></description>
			<content:encoded><![CDATA[<p>The following user tutorial was created by Kahshoo-heem, for the purpose of expanding upon the SDLTutorials.com series, and expounding upon the use of SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework used by the author. If you wish to submit your own tutorial to this site, please visit the &#8220;User Tutorials&#8221; page.</p>
<hr/>
<p>Once we have made our common library, it&#8217;s time to think on designing the game. Network games can have many architectures, but, normally, there are always at least two applications: a server and a client.</p>
<p><span id="more-190"></span></p>
<p>A server can be dedicated, like most online multiplayer games over the internet. But, in this case, the server will be playing too. To make things simpler, the server always will be &#8220;X&#8221;, and the player that starts the game, too. It&#8217;ll be responsible for waiting clients to connect, too.</p>
<p>At each player turn, the program sets the grid status, refreshes the screen, and sends a message to the other player. Receiving this message, the other player is now able to make valid inputs, as the first  one can only exit or minimize his application.</p>
<p>At this point, we may have to create a state for the application: connected. Without it,  one can start the game alone, and send messages to a person who is not online.</p>
<p>Now, let&#8217;s take a look at the messages the program has to make. As we said in the first tutorial, we will send only a byte, that corresponds to the ID of the grid cell clicked. So we have to modify our CNetMessage class, as this byte can be zero, and the program can take the message as an empty string. Therefore, we will make the class CNetMessageApp. Here is the header file, CNetApp.h:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &quot;CNet.h&quot;</span></p>
<p>class CNetMessageApp : public CNetMessage <span class="br0">&#123;</span></p>
<p>&nbsp;private:</p>
<p><span class="co1">//Virtual function that indicates how many bytes may have to be loaded onto the object. Overrides the parent class function member to work with only a byte</span></p>
<p>&nbsp; &nbsp;virtual <span class="kw4">int</span> NumToLoad<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//Virtual function that indicates how many bytes may have to be downloaded from the object. Overrides the parent class function member to work with only a byte</span></p>
<p>&nbsp; &nbsp;virtual <span class="kw4">int</span> NumToUnLoad<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>public:</p>
<p><span class="co1">//Function simpler than LoadBytes(), inherited from the parent class, as it only works with one byte</span></p>
<p>&nbsp; &nbsp;<span class="kw4">void</span> LoadByte<span class="br0">&#40;</span><span class="kw4">char</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//Function simpler than UnLoadBytes(), inherited from the parent class, as it only works with one byte</span></p>
<p>&nbsp; &nbsp;<span class="kw4">char</span> UnLoadByte<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p>Here comes the implementation, at CNetApp.cpp:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &quot;CNetApp.h&quot;</span></p>
<p><span class="kw4">int</span> CNetMessageApp::<span class="me2">NumToLoad</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></p>
<p><span class="kw1">if</span> <span class="br0">&#40;</span>state == EMPTY<span class="br0">&#41;</span></p>
<p>&nbsp;<span class="kw1">return</span> <span class="nu0">1</span>;</p>
<p><span class="kw1">else</span></p>
<p>&nbsp;<span class="kw1">return</span> <span class="nu0">0</span>;</p>
<p><span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> CNetMessageApp::<span class="me2">NumToUnLoad</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p><span class="kw1">if</span> <span class="br0">&#40;</span>state == FULL<span class="br0">&#41;</span></p>
<p>&nbsp;<span class="kw1">return</span> <span class="nu0">1</span>;</p>
<p><span class="kw1">else</span></p>
<p>&nbsp;<span class="kw1">return</span> <span class="nu0">0</span>;</p>
<p><span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> CNetMessageApp::<span class="me2">LoadByte</span><span class="br0">&#40;</span><span class="kw4">char</span> ID<span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp;charbuf c;</p>
<p>&nbsp; &nbsp;c<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> = ID;</p>
<p>LoadBytes<span class="br0">&#40;</span>c, <span class="nu0">1</span><span class="br0">&#41;</span>;</p>
<p>finish<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="br0">&#125;</span></p>
<p><span class="kw4">char</span> CNetMessageApp::<span class="me2">UnLoadByte</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp;charbuf c;</p>
<p>&nbsp; &nbsp;UnLoadBytes <span class="br0">&#40;</span>c<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp;<span class="kw1">return</span> c<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>;</p>
<p><span class="br0">&#125;</span><br />
&nbsp;</div>
<p>As we can see, very simple. Finally, we can modify the original files of Tim&#8217;s TIC TAC TOE game.</p>
<p>At the header file CApp.h, in the client side, we will have to add the following lines:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//adding the network library</span></p>
<p><span class="co2">#include &quot;CNetApp.h&quot;</span></p>
<p>&nbsp;.</p>
<p>&nbsp;.</p>
<p>&nbsp;.</p>
<p>&nbsp; &nbsp;<span class="me1">private</span>:</p>
<p><span class="co1">// Network status indicator</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;bool Connected;</p>
<p><span class="co1">// Network objects</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;CClientSocket* tcpclient;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;CIpAddress* remoteip;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;CNetMessageApp msg;<br />
&nbsp;</div>
<p>And, in the server side,  we don&#8217;t need the help of CIpAddress object, so it can be deleted. However, we have to add a tcp socket that looks for &#8211; or, better saying, listen to &#8211; clients:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//network objects</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;CHostSocket* tcplistener;<br />
&nbsp;</div>
<p>And we are able to get into the implementation code. Firstly, let&#8217;s set this members when creating Capp object. At CApp.cpp, in the server side, we&#8217;ll have to add these lines to the constructor:</p>
<div class="dean_ch" style="white-space: wrap;">
&nbsp; &nbsp;tcplistener = <span class="kw2">NULL</span>;</p>
<p>&nbsp; &nbsp;tcpclient = <span class="kw2">NULL</span>;</p>
<p>&nbsp; &nbsp;Connected = <span class="kw2">false</span>;<br />
&nbsp;</div>
<p>And, in the client side,</p>
<div class="dean_ch" style="white-space: wrap;">
&nbsp; &nbsp;Connected = <span class="kw2">false</span>;</p>
<p>&nbsp; &nbsp;tcpclient = <span class="kw2">NULL</span>;</p>
<p>&nbsp; &nbsp;remoteip = <span class="kw2">NULL</span>;<br />
&nbsp;</div>
<p>Now, let&#8217;s see CApp_Oninit.cpp.  We may have only to create the objects above, except CNetMessage, that is created automatically by CApp.  In the server side, the code of CApp::OnInit() will be added by these lines:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//Creating host socket</span></p>
<p>&nbsp; &nbsp;tcplistener = new CHostSocket <span class="br0">&#40;</span><span class="nu0">1234</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>!tcplistener-&gt;Ok<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;exit<span class="br0">&#40;</span>EXIT_FAILURE<span class="br0">&#41;</span>;</p>
<p><span class="co1">//Creating client socket</span></p>
<p>&nbsp; &nbsp;tcpclient = new CClientSocket<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>And in the client side,</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//Creating Ip and Socket objects</span></p>
<p>&nbsp; &nbsp;tcpclient = new CClientSocket <span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp;remoteip = new CIpAddress <span class="br0">&#40;</span><span class="st0">&quot;192.168.0.102&quot;</span>, <span class="nu0">1234</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>Note that the client, in this example, will communicate with a determined machine. In my home network, &#8220;192.168.0.102&#8243; will be the server&#8217;s address, and &#8220;1234&#8243; will be the port we have chosen to use. Otherwise, in  a LAN, there is a manner to broadcast a required connection, but this method uses UDP sockets. Thus, someone can use this kind of protocol to get the server address, and, after that, work with TCP sockets to make reliable and simple communication over the network.</p>
<p>On the other hand, that is the code on CApp_OnCleanup &#8211; it will destroy these objects:</p>
<p>Server side:</p>
<div class="dean_ch" style="white-space: wrap;">
&nbsp; &nbsp;delete tcplistener;</p>
<p>&nbsp; &nbsp;delete tcpclient;<br />
&nbsp;</div>
<p>Client side:</p>
<div class="dean_ch" style="white-space: wrap;">
&nbsp; &nbsp;delete remoteip;</p>
<p>&nbsp; &nbsp;delete tcpclient;<br />
&nbsp;</div>
<p>Now, we can examine what happens when we click the mouse in our application. At CApp::OnLButtonDown() we&#8217;ll change these lines:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">if</span><span class="br0">&#40;</span>CurrentPlayer == <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp;SetCell<span class="br0">&#40;</span>ID, GRID_TYPE_X<span class="br0">&#41;</span>;<br />
CurrentPlayer = <span class="nu0">1</span>;<br />
&nbsp;<span class="br0">&#125;</span><span class="kw1">else</span><span class="br0">&#123;</span><br />
&nbsp; SetCell<span class="br0">&#40;</span>ID, GRID_TYPE_O<span class="br0">&#41;</span>;<br />
&nbsp;CurrentPlayer = <span class="nu0">0</span>;<br />
&nbsp;<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>to these:</p>
<p>CApp_OnEvent &#8211; Server side:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//Only starts to play once connected</span></p>
<p>&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>Connected<span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p><span class="co1">//Player == 0 will always be Server. Always starts to play and always be &quot;X&quot;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">if</span><span class="br0">&#40;</span>CurrentPlayer == <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetCell<span class="br0">&#40;</span>ID, GRID_TYPE_X<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CurrentPlayer = <span class="nu0">1</span>;</p>
<p><span class="co1">//Send a message to the client, telling the ID of the clicked cell</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;msg.<span class="me1">LoadByte</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="kw4">char</span><span class="br0">&#41;</span> ID<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tcpclient-&gt;Send<span class="br0">&#40;</span>msg<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>CApp_OnEvent &#8211; Client side:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//Player == 0 will always be Server. Always starts to play and always be &quot;X&quot;. So if</span></p>
<p><span class="co1">//CurrentPlayer is not zero, the game is necessarily connected</span></p>
<p><span class="kw1">if</span><span class="br0">&#40;</span>CurrentPlayer != <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp;SetCell<span class="br0">&#40;</span>ID, GRID_TYPE_O<span class="br0">&#41;</span>;</p>
<p>&nbsp;CurrentPlayer = <span class="nu0">0</span>;</p>
<p><span class="co1">//Send a message to the server, telling the ID of the clicked cell</span></p>
<p>&nbsp;msg.<span class="me1">LoadByte</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="kw4">char</span><span class="br0">&#41;</span> ID<span class="br0">&#41;</span>;</p>
<p>&nbsp; tcpclient-&gt;Send<span class="br0">&#40;</span>msg<span class="br0">&#41;</span>;</p>
<p><span class="br0">&#125;</span><br />
&nbsp;</div>
<p>As we can see, no much changes have been made. If it&#8217;s not the application&#8217;s turn, it does nothing. If it is, the application sets the grid and player status, like it did priorly, and sends a message to the remote application. There&#8217;s no need to modify the rendering behavior.</p>
<p>Lastly, we have to write CApp OnLoop() member function. We have to do two things: try to connect, or, if yet connected, receive the messages sent over the network. Below, the code of CApp_OnLoop.cpp, server side:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &quot;CApp.h&quot;</span></p>
<p><span class="kw4">void</span> CApp::<span class="me2">OnLoop</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p><span class="co1">//if not connected, listen to the port to detect if there is a client waiting there</span></p>
<p>&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>!Connected<span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>tcplistener-&gt;Accept <span class="br0">&#40;</span>*tcpclient<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Connected = <span class="kw2">true</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp;<span class="kw1">else</span> <span class="br0">&#123;</span></p>
<p><span class="co1">//if connected, checks the socket for messages ready to be read</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>tcpclient-&gt;Ready<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p><span class="co1">// if there is a message, try to receive it. In case of disconnection, the TCP protocol sends</span></p>
<p><span class="co1">// a message with no bytes</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>tcpclient-&gt;Receive <span class="br0">&#40;</span>msg<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//if there is a valid message, we can set the grid and player status</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetCell<span class="br0">&#40;</span><span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span> msg.<span class="me1">UnLoadByte</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, GRID_TYPE_O<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CurrentPlayer = <span class="nu0">0</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">else</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Connected = <span class="kw2">false</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p><span class="br0">&#125;</span><br />
&nbsp;</div>
<p>The commented lines say by themselves the program&#8217;s flow.  On the other side, the client&#8217;s code:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">//if not connected, try to connect to the server</span></p>
<p>&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>!Connected<span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>tcpclient-&gt;Connect<span class="br0">&#40;</span>*remoteip<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>tcpclient-&gt;Ok<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Connected = <span class="kw2">true</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p><span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp;<span class="kw1">else</span> <span class="br0">&#123;</span></p>
<p><span class="co1">//if connected, checks the socket for messages ready to be read</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>tcpclient-&gt;Ready<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></p>
<p><span class="co1">// if there is a message, try to receive it. In case of disconnection, the TCP protocol sends</span></p>
<p><span class="co1">// a message with no bytes</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>tcpclient-&gt;Receive <span class="br0">&#40;</span>msg<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//if there is a valid message, we can set the grid and player status</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetCell<span class="br0">&#40;</span><span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span> msg.<span class="me1">UnLoadByte</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, GRID_TYPE_X<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CurrentPlayer = <span class="nu0">1</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">else</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Connected = <span class="kw2">false</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p><span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Then we have reached the end of this tutorial. I hope you enjoyed it, and feel free to use and modify the library shown here.</p>
<p><b>SDL Net Tutorial Files:</b><br />
<a href="/tutorials/sdl-net-clientfiles.tar">sdl-net-clientfiles.tar</a><br />
<a href="/tutorials/sdl-net-commonfiles.tar">sdl-net-commonfiles.tar</a><br />
<a href="/tutorials/sdl-net-serverfiles.tar">sdl-net-serverfiles.tar</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/sdl-net-part-2-user-tutorial/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>SDL Net &#8211; Part 1 (User Tutorial)</title>
		<link>http://www.sdltutorials.com/sdl-net-part-1-user-tutorial/</link>
		<comments>http://www.sdltutorials.com/sdl-net-part-1-user-tutorial/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 12:01:09 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=188</guid>
		<description><![CDATA[The following user tutorial was created by Kahshoo-heem, for the purpose of expanding upon the SDLTutorials.com series, and expounding upon the use of SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for [...]]]></description>
			<content:encoded><![CDATA[<p>The following user tutorial was created by Kahshoo-heem, for the purpose of expanding upon the SDLTutorials.com series, and expounding upon the use of SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework used by the author. If you wish to submit your own tutorial to this site, please visit the &#8220;User Tutorials&#8221; page.</p>
<hr/>
<p>In this tutorial, I will show you a simple c++ library to add network resources to your game.</p>
<p>Firstly, we have to add the SDL_net headers and libraries to your desktop environment. If you are using a linux distro like Ubuntu, it is enough to download the libsdl-netx.x-dev package from the repositories. If not, the process is analogous of including sdl mixer, image, etc, better explained by Tim&#8217;s Tutorials.<br />
Don&#8217;t forget, as well, to set the linker options in your IDE, adding the SDL_net library.</p>
<p><span id="more-188"></span></p>
<p>So, let&#8217;s create our two files, CNet.h and CNet.cpp, that will build our framework that we&#8217;ll help us at making network games. It&#8217;s necessary to say that this library, for the time being, only works with TCP/IP protocol.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &lt;stdio.h&gt;</span><br />
<span class="co2">#include &lt;stdlib.h&gt;</span><br />
<span class="co2">#include &lt;string.h&gt;</span><br />
<span class="co2">#include &lt;iostream&gt;</span><br />
<span class="co2">#include &lt;string&gt;</span><br />
<span class="co2">#include &lt;cstring&gt;</span><br />
<span class="co2">#include &quot;SDL_net.h&quot;</span></p>
<p><span class="kw4">typedef</span> <span class="kw4">char</span> charbuf <span class="br0">&#91;</span><span class="nu0">256</span><span class="br0">&#93;</span>;</p>
<p>class CNet<span class="br0">&#123;</span><br />
&nbsp; public:<br />
&nbsp; &nbsp; <span class="kw4">static</span> bool Init<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">//Initialize SDL_net</span><br />
&nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">void</span> Quit<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">//Exit SDL_net</span><br />
<span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p>Here is our header file, and, after the includes and the charbuf declaration, our first class. Its aim is initialize and end the network environment, with two static functions. However, they are unnecessary, if we&#8217;re using SDL_Init(SDL_INIT_EVERYTHING).</p>
<div class="dean_ch" style="white-space: wrap;">
class CNetMessage <span class="br0">&#123;</span><br />
&nbsp; protected:<br />
&nbsp; &nbsp; charbuf buffer; <span class="co1">//message stored in buffer, limited to 256 bytes</span><br />
&nbsp; &nbsp; <span class="kw2">enum</span> bufstates <span class="br0">&#123;</span> <span class="co1">//message states</span><br />
&nbsp; &nbsp; EMPTY,<br />
&nbsp; &nbsp; READING,<br />
&nbsp; &nbsp; WRITING,<br />
&nbsp; &nbsp; FULL <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; bufstates state; <span class="co1">//the message state</span><br />
&nbsp; &nbsp; <span class="kw4">void</span> reset<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// reset message: fulfill it with zeroes and change its state to EMPTY</span><br />
&nbsp; public:<br />
&nbsp; &nbsp; CNetMessage<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">//constructor</span><br />
<span class="co1">//Virtual member function that indicates how many bytes may have to be loaded into the instance. By default, it works with strings that have a maximum</span><br />
<span class="co1">//of 255 characters.</span><br />
&nbsp; &nbsp; virtual <span class="kw4">int</span> NumToLoad<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//Virtual member function that indicates how many bytes may have to be downloaded from the instance.</span><br />
&nbsp; &nbsp; virtual <span class="kw4">int</span> NumToUnLoad<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw4">void</span> LoadBytes<span class="br0">&#40;</span>charbuf&amp; inputbuffer, <span class="kw4">int</span> n<span class="br0">&#41;</span>; <span class="co1">//Load a char set into the message buffer</span><br />
&nbsp; &nbsp; <span class="kw4">void</span> UnLoadBytes<span class="br0">&#40;</span>charbuf&amp; destbuffer<span class="br0">&#41;</span>; <span class="co1">//Unload a char set from the buffer</span><br />
&nbsp; &nbsp; <span class="kw4">void</span> finish<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">//set the state object to full. No more data to be loaded</span><br />
<span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p>The class above provides the needed interface for the application to deal with the packets sent through the network. It&#8217;s limited to a maximum of 256 bytes.</p>
<p>Regardless this class uses a buffer with a fixed length, the packets sent through the network can be much more smaller, according to the program&#8217;s needs. In our TIC TAC TOE game, only one byte is sent. To work with this customization, we put some message states, to help the framework user to build a derived class.</p>
<p>The LoadBytes and UnloadBytes member functions load and unload in a buffer the wanted chars. They were determined by the virtual functions NumToLoad and NumToUnload, each of which indicates how many bytes have to be loaded or unloaded. By default, this class is able to work with strings that have a maximum of 255 characters. If you want to work with other data structures, as we&#8217;ll do at the game making, you may override this member function.</p>
<p>Last,  the finish() member function, which sets the class ready to be used. So, we can send through the socket, in the same packet, a message with two different data structures, avoiding unnecessary network pollution. Below, the implementation code of this and the latter classes:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &quot;CNet.h&quot;</span></p>
<p>bool CNet::<span class="me2">Init</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>SDLNet_Init<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">cerr</span> &lt;&lt; <span class="st0">&quot;SDLNet_Init: &quot;</span> &lt;&lt; SDLNet_GetError<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt; std::<span class="me2">endl</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> CNet::<span class="me2">Quit</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; SDLNet_Quit<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>
CNetMessage::<span class="me2">CNetMessage</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; reset<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> CNetMessage::<span class="me2">reset</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">int</span> i =<span class="nu0">0</span>; i &lt; <span class="nu0">256</span>; i++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; buffer <span class="br0">&#91;</span>i<span class="br0">&#93;</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; state = EMPTY;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> CNetMessage::<span class="me2">finish</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>state == READING<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; state = FULL;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> CNetMessage::<span class="me2">NumToLoad</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>state == EMPTY<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">255</span>;<br />
&nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> CNetMessage::<span class="me2">NumToUnLoad</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>state == FULL<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> strlen<span class="br0">&#40;</span>buffer<span class="br0">&#41;</span> + <span class="nu0">1</span>;<br />
&nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> CNetMessage::<span class="me2">LoadBytes</span><span class="br0">&#40;</span>charbuf&amp; inputbuffer, <span class="kw4">int</span> n<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; n; i++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; buffer<span class="br0">&#91;</span>i<span class="br0">&#93;</span> = inputbuffer<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; state = READING;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> CNetMessage::<span class="me2">UnLoadBytes</span><span class="br0">&#40;</span>charbuf&amp; destbuffer<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span>; i &lt; this-&gt;NumToUnLoad<span class="br0">&#40;</span><span class="br0">&#41;</span>; i++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; destbuffer<span class="br0">&#91;</span>i<span class="br0">&#93;</span> = buffer<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; reset<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Now, back to the header file. Let&#8217;s take a look at the CIpAddress class:</p>
<div class="dean_ch" style="white-space: wrap;">
class CIpAddress <span class="br0">&#123;</span><br />
&nbsp; private:<br />
&nbsp; &nbsp; IPaddress m_Ip; <span class="co1">//the IPaddress structure</span><br />
&nbsp; public:<br />
&nbsp; &nbsp; CIpAddress<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">//constructor</span><br />
&nbsp; &nbsp; CIpAddress <span class="br0">&#40;</span>Uint16 port<span class="br0">&#41;</span>; <span class="co1">//create and associate a port to the instance</span><br />
&nbsp; &nbsp; CIpAddress <span class="br0">&#40;</span>std::<span class="kw4">string</span> host, Uint16 port<span class="br0">&#41;</span>; <span class="co1">//create and associate a port and a host to the instance</span><br />
&nbsp; &nbsp; <span class="kw4">void</span> SetIp <span class="br0">&#40;</span>IPaddress sdl_ip<span class="br0">&#41;</span>; <span class="co1">//set a CIpAddress object from an existing SDL IPaddress</span><br />
&nbsp; &nbsp; bool Ok<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>; <span class="co1">//True if the object have a port and a host associated to it</span><br />
&nbsp; &nbsp; IPaddress GetIpAddress<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>; <span class="co1">//return a SDL_net IPaddress structure</span><br />
&nbsp; &nbsp; Uint32 GetHost<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>; <span class="co1">//return the host</span><br />
&nbsp; &nbsp; Uint16 GetPort<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>; <span class="co1">//return the port</span><br />
<span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p>Basically, that is a C++ interface for the SDL NET structure IPaddress. It has informations about the host IP address, and the port which the program is associated to. Here is the definition of the class members, at CNet.cpp:</p>
<div class="dean_ch" style="white-space: wrap;">
CIpAddress::<span class="me2">CIpAddress</span> <span class="br0">&#40;</span>Uint16 port<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>SDLNet_ResolveHost<span class="br0">&#40;</span>&amp;m_Ip, <span class="kw2">NULL</span>, port<span class="br0">&#41;</span> &lt; <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">cerr</span> &lt;&lt; <span class="st0">&quot;SDLNet_ResolveHost: &quot;</span> &lt;&lt; SDLNet_GetError<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt; std::<span class="me2">endl</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; m_Ip.<span class="me1">host</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; m_Ip.<span class="me1">port</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p>
CIpAddress::<span class="me2">CIpAddress</span> <span class="br0">&#40;</span>std::<span class="kw4">string</span> host, Uint16 port<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>SDLNet_ResolveHost<span class="br0">&#40;</span>&amp;m_Ip, host.<span class="me1">c_str</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, port<span class="br0">&#41;</span> &lt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">cerr</span> &lt;&lt; <span class="st0">&quot;SDLNet_ResolveHost: &quot;</span> &lt;&lt; SDLNet_GetError<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt; std::<span class="me2">endl</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; m_Ip.<span class="me1">host</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; m_Ip.<span class="me1">port</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p>CIpAddress::<span class="me2">CIpAddress</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; m_Ip.<span class="me1">host</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; m_Ip.<span class="me1">port</span> = <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span></p>
<p>bool CIpAddress::<span class="me2">Ok</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> !<span class="br0">&#40;</span>m_Ip.<span class="me1">port</span> == <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> CIpAddress::<span class="me2">SetIp</span> <span class="br0">&#40;</span>IPaddress sdl_ip<span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="co1">//sets a CTcpSocket object from a existing SDL socket</span><br />
&nbsp; &nbsp; m_Ip = sdl_ip;<br />
<span class="br0">&#125;</span></p>
<p>IPaddress CIpAddress::<span class="me2">GetIpAddress</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> m_Ip;<br />
<span class="br0">&#125;</span></p>
<p>Uint32 CIpAddress::<span class="me2">GetHost</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> m_Ip.<span class="me1">host</span>;<br />
<span class="br0">&#125;</span></p>
<p>Uint16 CIpAddress::<span class="me2">GetPort</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> m_Ip.<span class="me1">port</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Again, back to header, let&#8217;s analyze CTcpSocket, a framework to the SDL NET TCP socket structure. In a few words, a socket is a connection, a &#8220;pipe&#8221; between two computers, that delivers messages to them, in and out. In a TCP socket, there are necessarily two fixed computers in each pipe&#8217;s end, and messages always are got in the right order that they have sent.</p>
<div class="dean_ch" style="white-space: wrap;">
class CTcpSocket <span class="br0">&#123;</span><br />
&nbsp; protected:<br />
&nbsp; &nbsp; TCPsocket m_Socket; <span class="co1">//the TCP socket structure</span><br />
&nbsp; &nbsp; SDLNet_SocketSet set; <span class="co1">//a set of sockets. Used here only to check existing packets</span><br />
&nbsp; public:<br />
&nbsp; &nbsp; CTcpSocket<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; virtual ~CTcpSocket<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; virtual <span class="kw4">void</span> SetSocket <span class="br0">&#40;</span>TCPsocket &nbsp;the_sdl_socket<span class="br0">&#41;</span>; <span class="co1">//set a CTcpSocket object from a existing SDL socket</span><br />
&nbsp; &nbsp; bool Ok<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>; <span class="co1">//indicate if theres is a TCPsocket associated to the instance</span><br />
&nbsp; &nbsp; bool Ready<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>; <span class="co1">//True if there are bytes ready to be read</span><br />
&nbsp; &nbsp; virtual <span class="kw4">void</span> OnReady<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">//pure virtual</span><br />
<span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p>In the code above, the member function Ready() plays an important role: it checks whether there is or not activity in the socket, freeing the program&#8217;s flow to do what they have to do, without waiting messages from the other side of the pipe.<br />
Below, the code in CNet.cpp:</p>
<div class="dean_ch" style="white-space: wrap;">
CTcpSocket::<span class="me2">CTcpSocket</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; m_Socket = <span class="kw2">NULL</span>;<br />
&nbsp; &nbsp; set = SDLNet_AllocSocketSet<span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>CTcpSocket::~CTcpSocket<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="br0">&#40;</span>m_Socket == <span class="kw2">NULL</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SDLNet_TCP_DelSocket<span class="br0">&#40;</span>set,m_Socket<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; SDLNet_FreeSocketSet<span class="br0">&#40;</span>set<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; SDLNet_TCP_Close <span class="br0">&#40;</span>m_Socket<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> CTcpSocket::<span class="me2">SetSocket</span> <span class="br0">&#40;</span>TCPsocket the_sdl_socket<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="br0">&#40;</span>m_Socket == <span class="kw2">NULL</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SDLNet_TCP_DelSocket<span class="br0">&#40;</span>set,m_Socket<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SDLNet_TCP_Close <span class="br0">&#40;</span>m_Socket<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; m_Socket = the_sdl_socket;<br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>SDLNet_TCP_AddSocket<span class="br0">&#40;</span>set,m_Socket<span class="br0">&#41;</span>==<span class="nu0">-1</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">cerr</span> &lt;&lt; <span class="st0">&quot;SDLNet_TCP_AddSocket: &quot;</span> &lt;&lt; SDLNet_GetError<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt; std::<span class="me2">endl</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; m_Socket = <span class="kw2">NULL</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p>bool CTcpSocket::<span class="me2">Ok</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> !<span class="br0">&#40;</span>m_Socket == <span class="kw2">NULL</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>bool CTcpSocket::<span class="me2">Ready</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; bool rd = <span class="kw2">false</span>;<br />
&nbsp; &nbsp; <span class="kw4">int</span> numready = SDLNet_CheckSockets<span class="br0">&#40;</span>set, <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>numready == <span class="nu0">-1</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">cerr</span> &lt;&lt; <span class="st0">&quot;SDLNet_CheckSockets: &quot;</span> &lt;&lt; SDLNet_GetError<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt; std:: <span class="me2">endl</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>numready<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rd = SDLNet_SocketReady <span class="br0">&#40;</span>m_Socket<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> rd;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> CTcpSocket::<span class="me2">OnReady</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p><span class="br0">&#125;</span><br />
&nbsp;</div>
<p>From this class, we will derive CHostSocket, according to the piece of code of CNet.h:</p>
<div class="dean_ch" style="white-space: wrap;">
class CClientSocket;</p>
<p>
class CHostSocket : public CTcpSocket <span class="br0">&#123;</span><br />
&nbsp; public:<br />
&nbsp; &nbsp; CHostSocket <span class="br0">&#40;</span>CIpAddress&amp; the_ip_address<span class="br0">&#41;</span>; <span class="co1">//create and open a new socket, with an existing CIpAddress object</span></p>
<p>&nbsp; &nbsp; CHostSocket <span class="br0">&#40;</span>Uint16 port<span class="br0">&#41;</span>; <span class="co1">//create and open a new socket with the desired port</span><br />
&nbsp; &nbsp; bool Accept <span class="br0">&#40;</span>CClientSocket&amp;<span class="br0">&#41;</span>; <span class="co1">//set a client CTcpSocket object after listening to the port</span><br />
&nbsp; &nbsp; virtual <span class="kw4">void</span> OnReady<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">//pure virtual</span><br />
<span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p>A host socket is a socket whose utility is listen to a certain port, to check whether there is someone wanting to connect to the service provided by the program, in the machine it runs. If found, we have to use the member Accept to associate another socket to the client. Here is the implementation:</p>
<div class="dean_ch" style="white-space: wrap;">
CHostSocket::<span class="me2">CHostSocket</span> <span class="br0">&#40;</span>CIpAddress&amp; the_ip_address<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; CTcpSocket::<span class="me2">CTcpSocket</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; IPaddress iph = the_ip_address.<span class="me1">GetIpAddress</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="br0">&#40;</span>m_Socket = SDLNet_TCP_Open<span class="br0">&#40;</span>&amp;iph<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SDLNet_FreeSocketSet<span class="br0">&#40;</span>set<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">cerr</span> &lt;&lt; <span class="st0">&quot;SDLNet_TCP_Open: &quot;</span> &lt;&lt; SDLNet_GetError<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt; std::<span class="me2">endl</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p>CHostSocket::<span class="me2">CHostSocket</span> <span class="br0">&#40;</span>Uint16 port<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; CIpAddress iplistener <span class="br0">&#40;</span>port<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!iplistener.<span class="me1">Ok</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m_Socket = <span class="kw2">NULL</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CTcpSocket::<span class="me2">CTcpSocket</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; IPaddress iph = iplistener.<span class="me1">GetIpAddress</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="br0">&#40;</span>m_Socket = SDLNet_TCP_Open<span class="br0">&#40;</span>&amp;iph<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SDLNet_FreeSocketSet<span class="br0">&#40;</span>set<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">cerr</span> &lt;&lt; <span class="st0">&quot;SDLNet_TCP_Open: &quot;</span> &lt;&lt; SDLNet_GetError<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt; std::<span class="me2">endl</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p>
bool CHostSocket::<span class="me2">Accept</span> <span class="br0">&#40;</span>CClientSocket&amp; the_client_socket<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; TCPsocket cs;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>cs = SDLNet_TCP_Accept<span class="br0">&#40;</span>m_Socket<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; the_client_socket.<span class="me1">SetSocket</span><span class="br0">&#40;</span>cs<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> CHostSocket::<span class="me2">OnReady</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p><span class="br0">&#125;</span><br />
&nbsp;</div>
<p>So, a tcp socket client is a socket whose aim is to communicate with a remote host, who, in turn, is using a tcp client to send and receive messages too. Below, the header file for this class:</p>
<div class="dean_ch" style="white-space: wrap;">
class CClientSocket : public CTcpSocket <span class="br0">&#123;</span><br />
&nbsp; private:<br />
&nbsp; &nbsp; CIpAddress m_RemoteIp; <span class="co1">//the CIpAddress object corresponding to the remote host</span><br />
&nbsp; public:<br />
&nbsp; &nbsp; CClientSocket<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">//constructor</span><br />
&nbsp; &nbsp; CClientSocket <span class="br0">&#40;</span>std::<span class="kw4">string</span> host, Uint16 port<span class="br0">&#41;</span>; <span class="co1">//Create the object and connect to a host, in a given port</span><br />
&nbsp; &nbsp; bool Connect <span class="br0">&#40;</span>CIpAddress&amp; remoteip<span class="br0">&#41;</span>; <span class="co1">//make a connection to communicate with a remote host</span><br />
&nbsp; &nbsp; bool Connect <span class="br0">&#40;</span>CHostSocket&amp; the_listener_socket<span class="br0">&#41;</span>; <span class="co1">//make a connection to communicate with a client</span><br />
&nbsp; &nbsp; <span class="kw4">void</span> SetSocket <span class="br0">&#40;</span>TCPsocket &nbsp;the_sdl_socket<span class="br0">&#41;</span>; <span class="co1">//set a CTcpSocket object from an existing SDL_net socket</span><br />
&nbsp; &nbsp; CIpAddress getIpAddress <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>; <span class="co1">//return a CIpAddress object associated to the remote host</span><br />
&nbsp; &nbsp; virtual <span class="kw4">void</span> OnReady<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">//pure virtual</span><br />
&nbsp; &nbsp; bool Receive<span class="br0">&#40;</span>CNetMessage&amp; rData<span class="br0">&#41;</span>; <span class="co1">//receive data and load it into a CNetMessage object</span><br />
&nbsp; &nbsp; bool Send <span class="br0">&#40;</span>CNetMessage&amp; sData<span class="br0">&#41;</span>; <span class="co1">//send data from a CNetMessage object</span><br />
<span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p>The same way CHostSocket is, this class is derived from CTcpSocket, and uses the constructor or Connect() to make a connection to the host. If so, the object uses the Send() and Receive() member functions to throw and get messages through the network. Below, the implementation:</p>
<div class="dean_ch" style="white-space: wrap;">
CClientSocket::<span class="me2">CClientSocket</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; CTcpSocket::<span class="me2">CTcpSocket</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>CClientSocket::<span class="me2">CClientSocket</span> <span class="br0">&#40;</span>std::<span class="kw4">string</span> host, Uint16 port<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; CIpAddress remoteip <span class="br0">&#40;</span>host.<span class="me1">c_str</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, port<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!remoteip.<span class="me1">Ok</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m_Socket = <span class="kw2">NULL</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CTcpSocket::<span class="me2">CTcpSocket</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Connect<span class="br0">&#40;</span>remoteip<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p>bool CClientSocket::<span class="me2">Connect</span> <span class="br0">&#40;</span>CHostSocket&amp; the_listener_socket<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;<br />
<span class="br0">&#125;</span></p>
<p>bool CClientSocket::<span class="me2">Connect</span> <span class="br0">&#40;</span>CIpAddress&amp; remoteip<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; TCPsocket cs;<br />
&nbsp; &nbsp; IPaddress ip = remoteip.<span class="me1">GetIpAddress</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>cs = SDLNet_TCP_Open<span class="br0">&#40;</span>&amp;ip<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SetSocket<span class="br0">&#40;</span>cs<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">cerr</span> &lt;&lt; <span class="st0">&quot;SDLNet_TCP_Open: &quot;</span> &lt;&lt; SDLNet_GetError<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt; std::<span class="me2">endl</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> CClientSocket::<span class="me2">SetSocket</span> <span class="br0">&#40;</span>TCPsocket the_sdl_socket<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; CTcpSocket::<span class="me2">SetSocket</span> <span class="br0">&#40;</span>the_sdl_socket<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; IPaddress* ips;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>ips = SDLNet_TCP_GetPeerAddress<span class="br0">&#40;</span>m_Socket<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/* Print the address, converting it onto the host format */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m_RemoteIp.<span class="me1">SetIp</span><span class="br0">&#40;</span>*ips<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Uint32 hbo = m_RemoteIp.<span class="me1">GetHost</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Uint16 pbo = m_RemoteIp.<span class="me1">GetPort</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; std::<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; <span class="st0">&quot;Client connected: &quot;</span> &lt;&lt; SDLNet_Read32<span class="br0">&#40;</span>&amp;hbo<span class="br0">&#41;</span> &lt;&lt; <span class="st0">&#8216; &#8216;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; SDLNet_Read16 <span class="br0">&#40;</span>&amp;pbo<span class="br0">&#41;</span> &lt;&lt; std::<span class="me2">endl</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">cerr</span> &lt;&lt; <span class="st0">&quot;SDLNet_TCP_GetPeerAddress: &quot;</span> &lt;&lt; SDLNet_GetError<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt; std::<span class="me2">endl</span>;<br />
<span class="br0">&#125;</span></p>
<p>CIpAddress CClientSocket::<span class="me2">getIpAddress</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> m_RemoteIp;<br />
<span class="br0">&#125;</span></p>
<p>bool CClientSocket::<span class="me2">Receive</span><span class="br0">&#40;</span>CNetMessage&amp; rData<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="co1">//Firstly, check if there is a socket</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>m_Socket == <span class="kw2">NULL</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;<br />
&nbsp; &nbsp; charbuf buf;</p>
<p><span class="co1">//Check if the instance can receive bytes, if it can, load the number of bytes specified by NumToLoad() virtual function</span><br />
&nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>rData.<span class="me1">NumToLoad</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>SDLNet_TCP_Recv<span class="br0">&#40;</span>m_Socket, buf, rData.<span class="me1">NumToLoad</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rData.<span class="me1">LoadBytes</span> <span class="br0">&#40;</span>buf, rData.<span class="me1">NumToLoad</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; rData.<span class="me1">finish</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;<br />
<span class="br0">&#125;</span></p>
<p>bool CClientSocket::<span class="me2">Send</span> <span class="br0">&#40;</span>CNetMessage&amp; sData<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="co1">//check if there is a socket</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>m_Socket == <span class="kw2">NULL</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;<br />
&nbsp; &nbsp; charbuf buf;<br />
&nbsp; &nbsp; <span class="kw4">int</span> len;</p>
<p><span class="co1">//Check if the instance can send bytes, if it can, unload the number of bytes specified by NumToLoad() virtual function</span><br />
&nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>len = sData.<span class="me1">NumToUnLoad</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; sData.<span class="me1">UnLoadBytes</span> <span class="br0">&#40;</span>buf<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>SDLNet_TCP_Send<span class="br0">&#40;</span>m_Socket, <span class="br0">&#40;</span><span class="kw4">void</span> *<span class="br0">&#41;</span>buf, len<span class="br0">&#41;</span> &lt; len<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">cerr</span> &lt;&lt; <span class="st0">&quot;SDLNet_TCP_Send: &quot;</span> &lt;&lt; SDLNet_GetError<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt; std::<span class="me2">endl</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> CClientSocket::<span class="me2">OnReady</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p><span class="br0">&#125;</span><br />
&nbsp;</div>
<p>So that, we have finished the first part of our tutorial. Next, we&#8217;ll change the TIC TAC TOE source code to add playability over a network.</p>
<p><b>SDL Net Tutorial Files:</b><br />
<a href="/tutorials/sdl-net-clientfiles.tar">sdl-net-clientfiles.tar</a><br />
<a href="/tutorials/sdl-net-commonfiles.tar">sdl-net-commonfiles.tar</a><br />
<a href="/tutorials/sdl-net-serverfiles.tar">sdl-net-serverfiles.tar</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/sdl-net-part-1-user-tutorial/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A sad day?</title>
		<link>http://www.sdltutorials.com/a-sad-day/</link>
		<comments>http://www.sdltutorials.com/a-sad-day/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 14:26:56 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=181</guid>
		<description><![CDATA[If you all cannot tell, I have been very busy in life lately with two kiddos, full time job, and all sorts of other stuff. As such, I&#8217;m having to cut certain things out to make more time for other things. Unfortunately, that may mean SDLtutorials.com . I hate to say this, but only by [...]]]></description>
			<content:encoded><![CDATA[<p>If you all cannot tell, I have been very busy in life lately with two kiddos, full time job, and all sorts of other stuff. As such, I&#8217;m having to cut certain things out to make more time for other things. Unfortunately, that may mean SDLtutorials.com . I hate to say this, but only by donating will keep SDLtutorials.com going (it&#8217;s actually depressing to have only 1 donation [for the contest] come in since the website started). Otherwise, I&#8217;ll be looking to sell the SDLtutorials.com domain name, and only the domain name, to the highest bidder. Sorry, I won&#8217;t be selling the rights to my tutorials unless you make an extremely good offer.</p>
<p>Update:<br />
It has come to my attention that even if I wanted to sell the domain today, I wouldn&#8217;t be able to do so for at least a year. At any rate, I am simply stressed out and I don&#8217;t think I want to give up on this venture. So, I need your help. 1. Please continue to be patient as more tutorials will eventually come. 2. Lets be an active community helping each other, so that if I&#8217;m idle for a while, people are still getting help. 3. If you&#8217;d like to donate, that&#8217;s up to you guys. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/a-sad-day/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
