<?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>SDLTutorials.com &#187; C++ tutorials</title>
	<atom:link href="http://www.sdltutorials.com/category/c-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sdltutorials.com</link>
	<description>SDL Tutorials - Game Tutorials - Programming Tutorials</description>
	<lastBuildDate>Tue, 20 Jul 2010 19:56:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>C++ Variables</title>
		<link>http://www.sdltutorials.com/cpp-variables/</link>
		<comments>http://www.sdltutorials.com/cpp-variables/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 12:11:51 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[C++ tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=133</guid>
		<description><![CDATA[Congrats. You are one tutorial closer in the wonderful world of programming. Up to now, you should be somewhat comfortable with CodeBlocks, and have a very basic understanding of how a program works. In this tutorial we are going to look at something called variables. A variable in the programming world is a special type [...]]]></description>
			<content:encoded><![CDATA[<p>Congrats. You are one tutorial closer in the wonderful world of programming. Up to now, you should be somewhat comfortable with CodeBlocks, and have a very basic understanding of how a program works. In this tutorial we are going to look at something called variables.</p>
<p><span id="more-133"></span></p>
<p>A variable in the programming world is a special type of container that can hold a value. This container is then stored in memory with its value. In other words, your program can make a request to the operating system to store a value into memory, and then later on, it can request that value back at any time. This is useful for a lot of reasons, especially when you need to do calculations.</p>
<p>There are several types of values you can store into memory. Here&#8217;s a list of some of them:</p>
<table width="100%" border="1">
<tr>
<td width='33%'><b>Keyword</b></td>
<td width='34%'><b>Type</b></td>
<td width='33%'><b>Size*</b></td>
</tr>
<tr>
<td width='33%'>char</td>
<td width='34%'>Character/Small Integer</td>
<td width='33%'>1 Byte</td>
</tr>
<tr>
<td width='33%'>short int</td>
<td width='34%'>Short Integer</td>
<td width='33%'>2 Bytes</td>
</tr>
<tr>
<td width='33%'>int</td>
<td width='34%'>Integer</td>
<td width='33%'>4 Bytes</td>
</tr>
<tr>
<td width='33%'>long int</td>
<td width='34%'>Long Integer</td>
<td width='33%'>4 Bytes</td>
</tr>
<tr>
<td width='33%'>long long int</td>
<td width='34%'>Long Integer (64-bits)</td>
<td width='33%'>8 Bytes</td>
</tr>
<tr>
<td width='33%'>float</td>
<td width='34%'>Single-precision floating point value</td>
<td width='33%'>4 Bytes</td>
</tr>
<tr>
<td width='33%'>double</td>
<td width='34%'>Double-precision floating point value</td>
<td width='33%'>8 Bytes</td>
</tr>
</table>
<p>*Please note that the sizes listed here are simply for reference and based upon commonality. Some machines are different.</p>
<p>So what does this table mean really? Well, each variable keyword can basically hold a certain size, or, in other words, each variable keyword says how much memory we are going to take up. So, for example, if we wanted to store the user&#8217;s age into a variable, a good choice would be short int.  Why? Because short int doesn&#8217;t take up very much memory, and we don&#8217;t expect people to start living to thousands of years of age anytime soon. But lets say we are storing the average age between 10 people, and lets say the average we come up with is 20.7. We would use a float type to store this value because floats can store decimals (that is what &#8220;floating point value&#8221; means). Look at it this way:</p>
<p>Can hold integer values:<br />
char, short int, int, long int, long long int</p>
<p>Can hold floating point values:<br />
float, double</p>
<p>Those that hold integer values have nothing different between them other than the amount of data they can store. Same goes for floating point types. Take a look at the chart again, but this time with limits in place:</p>
<table width="100%" border="1">
<tr>
<td width='33%'><b>Keyword</b></td>
<td width='34%'><b>Type</b></td>
<td width='33%'><b>Size</b></td>
</tr>
<tr>
<td width='33%'>char</td>
<td width='34%'>Character/Small Integer</td>
<td width='33%'>0 &#8211; 255</td>
</tr>
<tr>
<td width='33%'>short int</td>
<td width='34%'>Short Integer</td>
<td width='33%'>0 to 65535</td>
</tr>
<tr>
<td width='33%'>int</td>
<td width='34%'>Integer</td>
<td width='33%'>0 to 4294967295</td>
</tr>
<tr>
<td width='33%'>long int</td>
<td width='34%'>Long Integer</td>
<td width='33%'>0 to 4294967295</td>
</tr>
<tr>
<td width='33%'>long long int</td>
<td width='34%'>Long Integer (64-bits)</td>
<td width='33%'>0 to 18446744073709551615</td>
</tr>
<tr>
<td width='33%'>float</td>
<td width='34%'>Single-precision floating point value</td>
<td width='33%'>+/- 3.4e +/- 38 (~7 digits)</td>
</tr>
<tr>
<td width='33%'>double</td>
<td width='34%'>Double-precision floating point value</td>
<td width='33%'>+/- 1.7e +/- 308 (~15 digits)</td>
</tr>
</table>
<p>(Please note table data was taken from http://www.cplusplus.com/doc/tutorial/variables/ and http://home.att.net/~jackklein/c/inttypes.html#long_long).</p>
<p>You can see, depending on what you need to store into memory, you can choose the appropriate data type. We don&#8217;t want our program to take up all memory on the system, so we must choose the appropriate data types. Notice that int and long int are shown as the same. As I mentioned above, not all systems set these sizes / ranges the same, long int may actually be larger than int on some systems. Also, please note that char, though a rather small integer type, is special in the regard that is can hold characters (letters, and such). We&#8217;ll get into that aspect in later tutorials.</p>
<p>So why is this all important? First, we need to know our limitations when dealing with data. Secondly, we need to know which data types to use when we do need to store data.</p>
<p>If you notice from the table above there doesn&#8217;t seem to be a way to hold negative values. C++ deals with this by introducing two keywords called unsigned / signed. Unsigned means the data type will only hold positive values (including 0). Signed means the data type can hold negative and positive values, but when you make a variable signed, that effectively cuts the positive range in half. For example:</p>
<p>signed char<br />
Range: -128 to 127</p>
<p>unsigned char<br />
Range: 0 to 255</p>
<p>You see what happened there? In order to accommodate negative values we had to give up half of our possible positive values.</p>
<p>Also, to make a further note on this, all data types except for char are signed by default (meaning they can hold negative and positive values). A char can be signed or unsigned depending on the compiler involved, this is because a char is usually used to hold characters (which are all positive numbers) &#8211; most of the time you&#8217;ll find a char is unsigned by default.</p>
<p>So enough fuss, lets get to some coding to show you how to use this new information. Load up CodeBlocks like you did the last tutorial, create a new project, and empty file (if you need help on that please look at the first C++ tutorial). Then, from memory if you can, try to code out the bare bones of your program:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &lt;iostream&gt;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span></div>
<p><br/></p>
<p>We are going to make a simple program that will hold your age, and your age in dog years (boring, yes, I know). So, we&#8217;ll use a short int for the person&#8217;s age, and a float for the average. Why didn&#8217;t I use a char for the person&#8217;s age? Well, this is related to what I briefly said about characters. When cout / printf, or other functions that print text to the screen see a char come across, they will print the corresponding character instead of the value. You can see what I mean by this example:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">char</span> X = <span class="nu0">97</span> ;</p>
<p>std::<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; X;</div>
<p><br/></p>
<p>Instead of printing 97 like you would want, it actually prints &#8216;a&#8217;. Again, char is sort of a special data type that is really meant for characters. Please note there are ways to force the program to print the value instead of a char, but we won&#8217;t get into that.</p>
<p>So, back to the program, we have an int and float. Lets put that into our program:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &lt;iostream&gt;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> Age = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; <span class="kw4">float</span> AgeInDogYears = <span class="nu0">0</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span></div>
<p><br/></p>
<p>Notice what&#8217;s going on here. We first declare our data type, then a name for our variable, and then give it an initial value. You can make the name of the variable whatever you want. For instance, I could have put:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">int</span> TimothysAge = <span class="nu0">0</span>;</div>
<p><br/></p>
<p>Note that a variable name must begin with a character or underscore _, but after the first character it can contain numbers. These variable names are all valid:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">int</span> _Age;<br />
<span class="kw4">int</span> Age;<br />
<span class="kw4">int</span> Age1;<br />
<span class="kw4">int</span> aGE;</div>
<p><br/></p>
<p>These variable names are not valid:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">int</span> 1Age;<br />
<span class="kw4">int</span> !Age;</div>
<p><br/></p>
<p>So, now that we have our variables made, lets make them do something. Set the variable &#8220;Age&#8221; to your actual age. For me, I would do:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &lt;iostream&gt;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> Age = <span class="nu0">23</span>;<br />
&nbsp; &nbsp; <span class="kw4">float</span> AgeInDogYears = <span class="nu0">0</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span></div>
<p><br/></p>
<p>Now, just like in school, we can also create equations and perform math. We know that a person&#8217;s age in Dog years is actually the person&#8217;s age divided by 7.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &lt;iostream&gt;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> Age = <span class="nu0">23</span>;<br />
&nbsp; &nbsp; <span class="kw4">float</span> AgeInDogYears = Age / <span class="nu0">7</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span></div>
<p><br/></p>
<p>You see what I did there? I was able to use the Age variable and treat it like a number. So when our program comes to the AgeInDogYears line, it will look up the value of Age (23), and divide it by 7. Lets actually make this value print out now:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &lt;iostream&gt;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> Age = <span class="nu0">23</span>;<br />
&nbsp; &nbsp; <span class="kw4">float</span> AgeInDogYears = Age / <span class="nu0">7</span>;</p>
<p>&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;My age in dog years: &quot;</span> &lt;&lt; AgeInDogYears;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span></div>
<p><br/></p>
<p>Look at what we did with cout. First, we print out a normal string (like we did in the first tutorial), but then notice we used another double arrow <<  to also print out AgeInDogYears. When we use cout that mixes strings and variables, you have to use multiple << to separate the two. For example, these are valid ways to use cout:</p>
<div class="dean_ch" style="white-space: wrap;">
std::<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; <span class="st0">&quot;Your age &quot;</span> &lt;&lt; Age &lt;&lt; <span class="st0">&quot; in dog years is &quot;</span> &lt;&lt; AgeInDogYears;</div>
<p><br/></p>
<p>These are wrong / invalid ways of using cout:</p>
<div class="dean_ch" style="white-space: wrap;">
std::<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; <span class="st0">&quot;You are Age years old&quot;</span>; <span class="co1">//Will actually say &quot;Age&quot; instead of printing the value of Age</span><br />
std::<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; <span class="st0">&quot;You are &quot;</span> Age <span class="st0">&quot; years old&quot;</span>; <span class="co1">//Syntax error</span><br />
std::<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> &lt;&lt; <span class="st0">&quot;You are &quot;</span> + Age + <span class="st0">&quot; years old&quot;</span>; <span class="co1">//Syntax error</span></div>
<p><br/></p>
<p>Now, take our code above and compile it. You should see it print out the correct number (for me that number is 3). But notice something, even though we used a float to hold decimal numbers, our value is not showing decimal numbers (unless you age is exactly divisible by 7). My age in dog years should really be 3.28571. Look back at this line:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">float</span> AgeInDogYears = Age / <span class="nu0">7</span>;</div>
<p><br/></p>
<p>Look at the number 7. When the system comes across hard-coded numbers in your program it treats them one of two ways, as integers or floating point numbers. The way it distinguinshes between them is if a decimal is used on the number of not:</p>
<p>Treated as integer<br />
7 </p>
<p>Treated as floating point<br />
7.0</p>
<p>So, when our age in dog years is calculated, the decimal number is automatically dropped because 7 is treated like an integer. In any division operations, the denominator always determines the end data type. So, if the numerator is an integer value and the denominator is a floating point value, we&#8217;ll end up with a floating point value. But, if it was the other way around, with a floating point as the numerator and an integer as the denominator, we&#8217;d up with an integer. Example:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">int</span> X = <span class="nu0">7.0</span> / <span class="nu0">3</span>; <span class="co1">// Equals 2</span><br />
<span class="kw4">int</span> X = <span class="nu0">7</span> / <span class="nu0">3.0</span> <span class="co1">// Equals 2.33333</span></div>
<p><br/></p>
<p>So, lets modify our program to fix this floating point issue:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &lt;iostream&gt;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> Age = <span class="nu0">23</span>;<br />
&nbsp; &nbsp; <span class="kw4">float</span> AgeInDogYears = Age / <span class="nu0">7.0</span>;</p>
<p>&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;My age in dog years: &quot;</span> &lt;&lt; AgeInDogYears;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span></div>
<p><br/></p>
<p>I will end this tutorial on this final note. You can perform all mathematical operations on variables and hard-coded values. These operations are:</p>
<p>Multiplication<br />
* </p>
<p>Division<br />
/</p>
<p>Addition<br />
+</p>
<p>Subtraction<br />
-</p>
<p>You can also use parenthesis like in algebra to group operations:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">int</span> X = <span class="nu0">5</span>;<br />
<span class="kw4">int</span> Y = <span class="br0">&#40;</span>X * X<span class="br0">&#41;</span> * <span class="nu0">5</span>;</div>
<p><br/></p>
<p>Play around with some equations and try to get used to them.</p>
<p>Congrats! We&#8217;ve successfully used variables. We&#8217;ll be using them a lot in up coming tutorials so try to get a firm grasp on how they are used, the subtle differences between them.</p>
<p><b>C++ Variables &#8211; Tutorial Files:</b><br />
<b>Win32:</b> <a href="../tutorials/cpp-variables.zip">Zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/cpp-variables/feed/</wfw:commentRss>
		<slash:comments>1</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>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>8</slash:comments>
		</item>
		<item>
		<title>Just to check up&#8230;</title>
		<link>http://www.sdltutorials.com/just-to-check-up/</link>
		<comments>http://www.sdltutorials.com/just-to-check-up/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 11:12:04 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[C++ tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=112</guid>
		<description><![CDATA[So, how is everyone doing so far in the contest? Run into any problems? Some inspiration again:]]></description>
			<content:encoded><![CDATA[<p>So, how is everyone doing so far in the contest? Run into any problems? </p>
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.
<p>Some inspiration again:</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/0t7jeIk-OzQ&#038;hl=en&#038;fs=1&#038;color1=0x006699&#038;color2=0x54abd6"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/0t7jeIk-OzQ&#038;hl=en&#038;fs=1&#038;color1=0x006699&#038;color2=0x54abd6" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/8Rr8K_WScYc&#038;hl=en&#038;fs=1&#038;color1=0x006699&#038;color2=0x54abd6"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/8Rr8K_WScYc&#038;hl=en&#038;fs=1&#038;color1=0x006699&#038;color2=0x54abd6" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/just-to-check-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Contest &#124; Resources</title>
		<link>http://www.sdltutorials.com/contest-resources/</link>
		<comments>http://www.sdltutorials.com/contest-resources/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 11:54:16 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[C++ tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=104</guid>
		<description><![CDATA[As a helpful gesture to all you out there that may need some resources (sounds, music, art), visit the link below: http://forums.sdltutorials.com/viewtopic.php?f=19&#038;t=36]]></description>
			<content:encoded><![CDATA[<p>As a helpful gesture to all you out there that may need some resources (sounds, music, art), visit the link below:</p>
<p><a href="http://forums.sdltutorials.com/viewtopic.php?f=19&#038;t=36">http://forums.sdltutorials.com/viewtopic.php?f=19&#038;t=36</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/contest-resources/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Contest &#124; Prize increase</title>
		<link>http://www.sdltutorials.com/contest-prize-increase/</link>
		<comments>http://www.sdltutorials.com/contest-prize-increase/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 15:42:17 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[C++ tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=101</guid>
		<description><![CDATA[The dollar prize has changed to: $200 (PayPal) A special thanks to Sergey Tikhonov for the donation!]]></description>
			<content:encoded><![CDATA[<p>The dollar prize has changed to: <span style='color: red;'>$200</span> (PayPal)</p>
<p>A special thanks to Sergey Tikhonov for the donation!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/contest-prize-increase/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SDL 1.3</title>
		<link>http://www.sdltutorials.com/sdl-13/</link>
		<comments>http://www.sdltutorials.com/sdl-13/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 13:41:30 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[C++ tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=50</guid>
		<description><![CDATA[It seems there has been a lot of improvement with SDL lately, and that it is nearing its next major release. Here&#8217;s an announcement made by Sam Lantinga recently: SDL 1.3 is ready for a massive bug hunt! http://www.libsdl.org/tmp/SDL-1.3.zip or http://www.libsdl.org/tmp/SDL-1.3.tar.gz The first person to report any particular bug for SDL 1.3 in bugzilla (http://bugzilla.libsdl.org) [...]]]></description>
			<content:encoded><![CDATA[<p>It seems there has been a lot of improvement with SDL lately, and that it is nearing its next major release. Here&#8217;s an announcement made by Sam Lantinga recently:</p>
<blockquote><p>SDL 1.3 is ready for a massive bug hunt!</p>
<p>http://www.libsdl.org/tmp/SDL-1.3.zip</p>
<p>or</p>
<p>http://www.libsdl.org/tmp/SDL-1.3.tar.gz</p>
<p>The first person to report any particular bug for SDL 1.3 in bugzilla (http://bugzilla.libsdl.org) will get their names added to the CREDITS list for the great SDL Bug Hunt of January 2009!</p>
<p>Anyone who contributes an SDL 1.3 bug fix which is accepted, regardless of whether they reported it, will have their names added to the CREDITS list as bug squashers in the great SDL Bug Hunt.  The top three squashers will be featured on the SDL website with a link to their favorite project (if they want.)</p>
<p>When contributing a patch, please include permission for me to release your code with SDL 1.3 and future versions of SDL under both the LGPL and a closed-source commercial license.</p>
<p>Contributors to SDL 1.3 are eligible for a discount on commercial licensing. Please contact me for details if you&#8217;re interested.</p>
<p>See ya!<br />
       -Sam Lantinga, Founder and President, Galaxy Gameworks LLC</p></blockquote>
<p>As such, after the official release all tutorials will get a makeover to reflect the changes in 1.3. Until then, I encourage everyone to continue to use SDL 1.2.x. Also, anyone interested in Commercial Games with SDL, should pay attention to this site now (<a href="http://galaxygameworks.com/">http://galaxygameworks.com/</a>).  You&#8217;re still free to use the library as long as you dynamically link the library, but for static links, you need a commercial license now. One incentive to get a license is official iPhone/iPod support now.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/sdl-13/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C++ Tutorial Basics</title>
		<link>http://www.sdltutorials.com/cpp-tutorial-basics/</link>
		<comments>http://www.sdltutorials.com/cpp-tutorial-basics/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 18:01:43 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[C++ tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/c-tutorial-basics/</guid>
		<description><![CDATA[Okay, so you are here. You&#8217;ve decided, hey, I want to start making games. Good idea! I certainly encourage you in your mission, but lets face facts. Making games is hard. Really hard. And not to throw unscientific percentages at you, but practically 90% of all those wanting to make games never finish anything. There [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, so you are here. You&#8217;ve decided, hey, I want to start making games. Good idea! I certainly encourage you in your mission, but lets face facts. Making games is hard. Really hard. And not to throw unscientific percentages at you, but practically 90% of all those wanting to make games never finish anything. There are always two reasons, lack of discipline, or lack of understanding. You hit a wall, try to find the easiest way around, and then give up for the next project. Or, you get tired of the game you are working on, think of a &#8220;better&#8221; idea, and then start on that. Games are subjective, tastes change, and along with helping you on the way to programming, I also hope I can give you some discipline to finish your work.</p>
<p><span id="more-49"></span><br />
Now, here we are, at the very beginning of it all. Don&#8217;t worry, before you know it you&#8217;ll be making games in no time. Don&#8217;t rush yourself. Believe me, once you know how to program, you&#8217;ll miss all the learning (not that you won&#8217;t have more to learn). The journey is the most important part, and the most fun.</p>
<p>Some very very basics first. That thing in front of you, that&#8217;s a computer (okay, technically a monitor probably, but let&#8217;s not be picky). For all you know, it does stuff. You click, you open windows, you download music (hopefully legally), play games, talk to friends. That&#8217;s about all it has been so far. Then, you got the brilliant idea to make a game. Well, exactly what is a game? More pointedly, how does anything work on a computer? </p>
<p>As you may know, everything in the computer world is represented in binary, 0 and 1. Meaning, on or off, true or false, or whatever other representation you want. This poses a slight problem to human beings, we cannot figure out the right combination of 0s and 1s to do anything. Imagine putting billions upon billions of 0s and 1s to write a game &#8211; it&#8217;s impossible! So, &#8220;in the beginning&#8221;, nice nerd people developed languages. They were crude at first, getting simple things done. These were called low-level languages. They performed basic things, extremely well. They told the computer to store bits into memory, or to get them back out, and so on. Well, that worked for a while, and then things got more complex. Now, we have high-level languages. C++, happens to be one of those languages. So, we use C++ and a bunch of commands and such, and it gets converted into those nice 0s and 1s (please understand, I am being very basic here).</p>
<p>Now, in order to get your C++ code into binary, we use something called a compiler. This is a middle-man program that takes your code, and spits out a program. Click the program just like anything else, and hey, it works! Now, alongside this, is something called a linker. You see, linker and compiler work together, they&#8217;re brothers! Linker is nice, it lets you take Joe&#8217;s C++ code and combine it with your own code &#8211; this then is sent over to compiler. Beyond this, as a beginner, don&#8217;t worry about how it works right now. As you progress, and gain experience, you&#8217;ll learn more about how these two tools (and many other tools) actually work. But for now, understand code go in to compiler, program come out of compiler. <img src='http://www.sdltutorials.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>So what is code exactly? Well, it&#8217;s just text (basically). It&#8217;s like the text on this page, except it has a different meaning. Take, for example, this math equation:</p>
<div class="dean_ch" style="white-space: wrap;">
X = <span class="nu0">1</span> + <span class="nu0">1</span><br />
&nbsp;</div>
<p>That can be seen as code. In itself, it doesn&#8217;t mean anything. But if given to something like a compiler, it can be a program!</p>
<p>Now, one important aspect is to have the right environment. First, we know, you need a compiler and a linker. And for beginners, you&#8217;ll need an IDE (technically, you could code in text editor if you wanted). Now, an IDE, or Integrated Development Environment, is basically a program where you code. But it comes with some nice features, like it comes with a compiler and linker, and sets it up for you! It also gives you a lot of other people&#8217;s code; most of which we&#8217;ll be using in these tutorials. Why use other people&#8217;s code? Because, believe me, if you started with a clean slate, you&#8217;d have to code even the ability to print a single pixel to the screen. So, first task! Go download CodeBlocks:</p>
<p>http://www.codeblocks.org</p>
<p>Important note: Download the most stable, and the one <u>with</u> mingw. I&#8217;ve had quite a few people download the one without (sigh). Once installed, and opened, you&#8217;re ready to continue.</p>
<p>Click File, and then New Project. On the window that opens, click Empty Project and then Go. For the project title, pick what you want. I am putting C++ Basics. For a folder, put whatever. Click Next. Check only the &#8220;Create &#8216;Release&#8217; Configuration&#8221;. Click Finish. Now click, File and then New Empty File. On the window that pops up, be sure to add to your project. For the file name, put main.cpp. Copy this code below, and paste it in:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &lt;iostream&gt;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; std::<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Hello World!<span class="es0">\n</span>&quot;</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>Click Build and then Build and run. If successful, you should see &#8220;Hello World!&#8221; followed by some other text. If so, congratutations! You just made your first program, sort of. <img src='http://www.sdltutorials.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>How lets explain what&#8217;s going here. First off at the very top, we have #include. Now would be a good time to explain the differences between two areas of your code. The first area is code to used by your compiler, the second area is your actual code to be made into a program. Anything that has a # before it, is code to be used by the compiler. Meaning, this code never actually makes it into the program. Why is this useful? Well, in this tutorial we have #include, this tells the compiler to include another file. In this case, we&#8217;re telling the compiler to go out and grab the file iostream and include it in the program. Another example would be #define.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#define MY_NAME &quot;Tim&quot;</span></p>
<p>std::<span class="kw3">cout</span> &lt;&lt; MY_NAME;<br />
&nbsp;</div>
<p>Now, wherever we put MY_NAME, it is replaced with &#8220;Tim.&#8221; This first area is called preprocessor directives. These &#8220;direct&#8221; the compiler before getting to the actual code. So, to explain the first line again, we&#8217;re including the file iostream into our program. The reason why we do that is because the code for std::cout is in there. And we need that.</p>
<p>Next, we have int main() { }. This is called a function, and while we won&#8217;t get into functions until later, I need to try to explain them briefly. First, whenever you first run your program, the operating system needs to know where your code begins. That&#8217;s what main() is for, that&#8217;s where your code begins. So, when you open your program, it starts at the top of main, and goes down line by line. When it reaches the end, it&#8217;s done, and your program stops. Now, the { } simply define ownership. Everything within the curly braces { } belongs to main. Secondly, main is also a function. A function is simply a group of code. So, for example, we might have:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">int</span> Group1<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> Group2<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>We have three functions, each with its own code. But there are some important things about functions. A function may return some data:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">int</span> MyAge<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">22</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>In this case, the function MyAge returns 22. Meaning, when the function is done, it&#8217;ll give back the number 22 from wherever it was first called. So, when the operating system opens our program, and our function main returns 0, it&#8217;s giving the number 0 back to the operating system. Why do we return 0? Well, lets say something wen&#8217;t wrong with your program when loading, in that case we can return 1, and if everything is okay, we can return 0. Again, this is a very very basic explanation of functions, and there&#8217;s a lot more to them. Just understand that when your program first opens that it starts at the top of main. </p>
<p>Next, we have the actual magic, std::cout. Now, this is really hard to explain to beginners, but for all purposes std::cout is simply a way for you to print text. You can use the << to separate text as well. Like:</p>
<div class="dean_ch" style="white-space: wrap;">
std::<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Hi &quot;</span> &lt;&lt; <span class="st0">&quot;my &quot;</span> &lt;&lt; <span class="st0">&quot;name &quot;</span> &lt;&lt; <span class="st0">&quot;is &quot;</span> &lt;&lt; <span class="st0">&quot;Tim.<span class="es0">\n</span>&quot;</span>;<br />
&nbsp;</div>
<p>Here&#8217;s the things I want to get across though. Notice the ; at the end? This is important, this is your period, so to speak. It signals the end of your particular command/code. Notice the code below:</p>
<div class="dean_ch" style="white-space: wrap;">
std::<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Test&quot;</span>;<br />
std::<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Test&quot;</span>;<br />
std::<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Test&quot;</span>;<br />
&nbsp;</div>
<p>Each command is separated not by a newline, but by a semi-colon. For beginners this is important, don&#8217;t forget your semi-colons! (Notice the one on return too). So, what&#8217;s that \n funny thing? That&#8217;s a newline character, which basically says, hey, move down a line. Like pressing Enter in a text editor.</p>
<p>Well, that&#8217;s all for now. This tutorial was very basic, and was intended that way. Future tutorials will tackle harder subjects at an increasing rate. <img src='http://www.sdltutorials.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Note: If your program opens and closes suddenly, run it from the command prompt. Start -> Run -> cmd. Navigate to your folder, and then type in your program. Hit enter!</p>
<p><b>C++ Basics &#8211; Tutorial Files:</b><br />
<b>Win32:</b> <a href="../tutorials/cpp-tutorial-basics.zip">Zip</a>, <a href="../tutorials/cpp-tutorial-basics.rar">Rar</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/cpp-tutorial-basics/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
