<?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; SDL Tutorials</title>
	<atom:link href="http://www.sdltutorials.com/category/sdl-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sdltutorials.com</link>
	<description>SDL Tutorials - Game Tutorials - Programming Tutorials</description>
	<lastBuildDate>Sat, 28 Aug 2010 13:20:06 +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>SDL_ttf 2.0.10 RELEASED!</title>
		<link>http://www.sdltutorials.com/sdl-ttf-2-0-10/</link>
		<comments>http://www.sdltutorials.com/sdl-ttf-2-0-10/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 15:37:20 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=470</guid>
		<description><![CDATA[Official SDL announcment: Announcing the latest release of SDL_ttf! http://www.libsdl.org/projects/SDL_ttf/ CHANGES:  * Find the Unicode or symbol character map if it&#8217;s available in the font  * Set the appropriate font styles for bold and italic fonts  * Added font style TTF_STYLE_STRIKETHROUGH  * Fixed size calculations taking outline and underline into account  * Added API for [...]]]></description>
			<content:encoded><![CDATA[<p>Official SDL announcment:</p>
<p>Announcing the latest release of SDL_ttf!</p>
<p>http://www.libsdl.org/projects/SDL_ttf/</p>
<p>CHANGES:<br />
 * Find the Unicode or symbol character map if it&#8217;s available in the font<br />
 * Set the appropriate font styles for bold and italic fonts<br />
 * Added font style TTF_STYLE_STRIKETHROUGH<br />
 * Fixed size calculations taking outline and underline into account<br />
 * Added API for font outlining: TTF_GetFontOutline()/TTF_SetFontOutline()<br />
 * Added API to disable kerning: TTF_GetFontKerning()/TTF_SetFontKerning()<br />
 * Fixed height calculation for fonts that extend below the font height<br />
 * Added access to font hinting: TTF_GetFontHinting()/TTF_SetFontHinting()<br />
 * Added TTF_GlyphIsProvided() to check whether a glyph is in a given font</p>
<p>Thanks to everybody who contributed feedback and patches for this release!<br />
&#8211;<br />
       -Sam Lantinga, Founder and President, Galaxy Gameworks LLC</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/sdl-ttf-2-0-10/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tetris Contest &#8211; Your Turn</title>
		<link>http://www.sdltutorials.com/tetris-contest-your-turn/</link>
		<comments>http://www.sdltutorials.com/tetris-contest-your-turn/#comments</comments>
		<pubDate>Wed, 12 May 2010 11:25:18 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=420</guid>
		<description><![CDATA[Alright everyone, it&#8217;s your turn to take a look at the contest entries and let me know what you think. Vote for the one you like best, and let us know in the comments what you like / dislike. Bloc Download Blockzorz Download Falling in Pieces Download Tetris with a Twist Download Yage Download Zeetrix [...]]]></description>
			<content:encoded><![CDATA[<p>Alright everyone, it&#8217;s your turn to take a look at the contest entries and let me know what you think. Vote for the one you like best, and let us know in the comments what you like / dislike.</p>
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.
<p><b>Bloc</b><br />
<a href="/Contest-Tetris/Bloc.zip"><img src="/Contest-Tetris/Bloc - SS.jpg"/><br />
Download</a></p>
<p><b>Blockzorz</b><br />
<a href="/Contest-Tetris/Blockzorz.zip"><img src="/Contest-Tetris/Blockzorz - SS.jpg"/><br />
Download</a></p>
<p><b>Falling in Pieces</b><br />
<a href="/Contest-Tetris/Falling in Pieces.zip"><img src="/Contest-Tetris/Falling in Pieces - SS.jpg"/><br />
Download</a></p>
<p><b>Tetris with a Twist</b><br />
<a href="/Contest-Tetris/Tetris with a Twist.zip"><img src="/Contest-Tetris/Tetris with a Twist - SS.jpg"/><br />
Download</a></p>
<p><b>Yage</b><br />
<a href="/Contest-Tetris/Yage.zip"><img src="/Contest-Tetris/Yage - SS.jpg"/><br />
Download</a></p>
<p><b>Zeetrix</b><br />
<a href="/Contest-Tetris/Zeetrix.zip"><img src="/Contest-Tetris/Zeetrix - SS.jpg"/><br />
Download</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/tetris-contest-your-turn/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Welcome to GSoC 2010!</title>
		<link>http://www.sdltutorials.com/welcome-to-gsoc-2010/</link>
		<comments>http://www.sdltutorials.com/welcome-to-gsoc-2010/#comments</comments>
		<pubDate>Sat, 01 May 2010 20:06:42 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=395</guid>
		<description><![CDATA[Official SDL Announcement: We had some amazing applicants for this year&#8217;s Google Summer of Code, and I would like to thank everyone who applied. It was very difficult to narrow down the final list, but in the end we picked 5 excellent students for this year&#8217;s project. I&#8217;d like to introduce them and invite everyone [...]]]></description>
			<content:encoded><![CDATA[<p>Official SDL Announcement:</p>
<p>We had some amazing applicants for this year&#8217;s Google Summer of Code, and I would like to thank everyone who applied.  It was very difficult to narrow down the final list, but in the end we picked 5 excellent students for this year&#8217;s project.</p>
<p>I&#8217;d like to introduce them and invite everyone to welcome them to the SDL community! <img src='http://www.sdltutorials.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Introducing the Students:</p>
<p> * Daniel Wyatt is working on the Windows IME implementation, working with Jiang (a GSoC student from last year) to update the API and build working Japanese input support.  I will be mentoring Danial and coordinating his collaboration with Jiang.  You can find out more<br />
about his project at:<br />
<a href="http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2010/sdl/t127230762719">http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2010/sdl/t127230762719</a></p>
<p> * Eli Gottlieb is working on shaped window support for Linux, Mac OS X, and Windows.  He will be mentored by Andreas Schiffler, who has<br />
actually implemented shaped window support for commercial SDL projects.  You can find out more about his project at:<br />
<a href="http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2010/sdl/t127230762740">http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2010/sdl/t127230762740</a></p>
<p> * Jim Grandpre is working on adding support for multi-touch devices and developing a complete gesture recognition framework.  He will be mentored by Ryan Gordon, who among many other things, is working on porting games to the iPad.  You can find out more about his project at:<br />
<a href="http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2010/sdl/t127230762583">http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2010/sdl/t127230762583</a></p>
<p> * Paul Hunkin is working on an official port of SDL 1.3 to the Android.  I will be mentoring him and testing his code on my development Google Ion.  You can find out more about his project at:<br />
<a href="http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2010/sdl/t127230762779">http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2010/sdl/t127230762779</a></p>
<p>* Sunny Sachanandani is working on integrating X11 XRender support into SDL 1.3, with support for hardware accelerated blending and more.<br />
 I will be mentoring him and testing his code on a variety of Linux distributions.  You can find out more about his project at:<br />
<a href="http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2010/sdl/t127230762857">http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2010/sdl/t127230762857</a></p>
<p>Introducing the Mentors:</p>
<p> * Andreas Schiffler is an architect/dev for a commercial SDL based windows+linux application with shaped-window support.  He is volunteering as a mentor this year to help Eli with his project.</p>
<p> * Ryan Gordon is a long time friend and SDL developer, and is currently rewriting the SDL multi-mouse support.  He will be mentoring Jim on his touch input project this year.</p>
<p> * I am the original author of SDL and founder of Galaxy Gameworks LLC, a small company devoted to commercial development and support of<br />
SDL 1.3 and beyond.  I am looking forward to mentoring Daniel, Paul, and Sunny.</p>
<p>Welcome! <img src='http://www.sdltutorials.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
&#8211;<br />
      -Sam Lantinga, Founder and President, Galaxy Gameworks LLC</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/welcome-to-gsoc-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kizare &#8211; Mercurial</title>
		<link>http://www.sdltutorials.com/kizare-mercurial/</link>
		<comments>http://www.sdltutorials.com/kizare-mercurial/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 12:17:47 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[Kizare]]></category>
		<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=383</guid>
		<description><![CDATA[I decided to switch Kizare over to Mercurial. Just Pushed in the most recent code, with initial client / server framework. It&#8217;s all very basic at the moment, but it works. http://code.google.com/p/kizare/ Again, anyone interested in helping?]]></description>
			<content:encoded><![CDATA[<p>I decided to switch Kizare over to Mercurial. Just Pushed in the most recent code, with initial client / server framework. It&#8217;s all very basic at the moment, but it works.</p>
<p>http://code.google.com/p/kizare/</p>
<p>Again, anyone interested in helping?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/kizare-mercurial/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>SDL now uses Mercurial</title>
		<link>http://www.sdltutorials.com/sdl-now-uses-mercurial/</link>
		<comments>http://www.sdltutorials.com/sdl-now-uses-mercurial/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 10:44:52 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=374</guid>
		<description><![CDATA[SDL source repository has now been switched over to use Mercurial! http://www.libsdl.org/hg.php If you don&#8217;t use Mercurial and never want to, don&#8217;t worry, you can still grab snapshots from the SDL website and send patches the way you always have. There are a number of reasons why this change was made, and here are a [...]]]></description>
			<content:encoded><![CDATA[<p>SDL source repository has now been switched over to use Mercurial!</p>
<p>http://www.libsdl.org/hg.php</p>
<p>If you don&#8217;t use Mercurial and never want to, don&#8217;t worry, you can still grab snapshots from the SDL website and send patches the way you always have.</p>
<p>There are a number of reasons why this change was made, and here are a few:<br />
 * It&#8217;s much easier to iterate locally on changes before pushing them to the world, resulting in better code in the wild.<br />
 * It&#8217;s much easier to share changes with other people using Mercurial, which is great for the Google Summer of Code students.  (e.g. hg export/import)<br />
 * It&#8217;s much faster to update and commit (or push and pull)<br />
 * Mercurial has similar syntax to Subversion for simple operations</p>
<p>Here is Ryan&#8217;s quickstart on Mercurial:</p>
<p>- The best quick-start tutorial I&#8217;ve read is Joel Spolsky&#8217;s new hginit.com. You should read it. It&#8217;s interesting and funny. I&#8217;ve read a lot of quick-start tutorials, and no other does it as well.</p>
<p>- The important thing to know, as a subversion user, is to stop fearing branching. Mercurial works best when you have a bunch of little ad-hoc branches weaving around each other.</p>
<p>- When committing new changes, keep the first sentence of the log comment short and on a line by itself. It&#8217;ll be used for the summary.</p>
<p>- More or less, most commands work like Subversion&#8217;s, and there is a TortoiseHg to match TortoiseSvn, if that&#8217;s you&#8217;re thing. But once you start using things that Subversion can&#8217;t do (bisect, record, shelve, rollback, queues, etc), you&#8217;re going to find yourself annoyed when you have to work with a Subversion repository and can&#8217;t use these extra features.</p>
<p>In case you&#8217;re curious, there&#8217;s lots of discussion on the why&#8217;s and wherefores on the SDL forums:</p>
<p>http://forums.libsdl.org/viewtopic.php?t=6047</p>
<p>See ya!<br />
&#8211;<br />
       -Sam Lantinga, Founder and President, Galaxy Gameworks LLC</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/sdl-now-uses-mercurial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Appcelerator Titanium</title>
		<link>http://www.sdltutorials.com/appcelerator-titanium/</link>
		<comments>http://www.sdltutorials.com/appcelerator-titanium/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 11:29:37 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=359</guid>
		<description><![CDATA[For all you web developers out there (or people that have toyed with JavaScript / HTML / CSS / PHP), this might interest you. In the same type of class as Adobe Air, Appcelerator Titanium is a development environment for developing applications using existing web technologies. It mainly focuses around JavaScript, but you can use [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.appcelerator.com"><img src="http://www.sdltutorials.com/wp-content/uploads/Appcelerator.png" alt="" title="Appcelerator" width="205" height="58" class="alignright size-full wp-image-360" /></a>For all you web developers out there (or people that have toyed with JavaScript / HTML / CSS / PHP), this might interest you. In the same type of class as Adobe Air, Appcelerator Titanium is a development environment for developing applications using existing web technologies. It mainly focuses around JavaScript, but you can use PHP, Python, or Ruby (depending on the target platform). The most interesting part of this is that you can also develop mobile applications (such as the iPhone and Android). Last I heard, they are actively developing an OpenGL module for use on the iPhone &#8211; but you can still make some decent games using CSS3 and HTML5. So while we are waiting for an official release of SDL for the iPhone, this may be a good alternative. It&#8217;s also good for developing Desktop Applications rather quickly, when you don&#8217;t want to waste a whole lot of time.</p>
<p>Check it out here: <a href="http://www.appcelerator.com">http://www.appcelerator.com</a></p>
<p><center><br />
<b>Warning, music is a bit loud</b><br />
<object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8730218&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8730218&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/appcelerator-titanium/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Are you participating in the Tetris Clone Contest</title>
		<link>http://www.sdltutorials.com/are-you-participating-in-the-tetris-clone-contest/</link>
		<comments>http://www.sdltutorials.com/are-you-participating-in-the-tetris-clone-contest/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 17:17:41 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/are-you-participating-in-the-tetris-clone-contest/</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/are-you-participating-in-the-tetris-clone-contest/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Google Summer of Code 2010</title>
		<link>http://www.sdltutorials.com/google-summer-of-code-2010/</link>
		<comments>http://www.sdltutorials.com/google-summer-of-code-2010/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 10:42:50 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=337</guid>
		<description><![CDATA[Official SDL Announcement: That&#8217;s right, we&#8217;re back again for Google Summer of Code 2010! Thanks to Google and the great students we&#8217;ve had, we&#8217;re going around for another year! The Google Summer of Code is a program in which students from all over the world are invited to apply to help out open source projects [...]]]></description>
			<content:encoded><![CDATA[<p>Official SDL Announcement:</p>
<p>That&#8217;s right, we&#8217;re back again for Google Summer of Code 2010!</p>
<p>Thanks to Google and the great students we&#8217;ve had, we&#8217;re going around for another year!</p>
<p>The Google Summer of Code is a program in which students from all over the world are invited to apply to help out open source projects over the summer for a significant stipend. Even if you&#8217;re not interested in helping out SDL, there are lots of great projects to choose from!</p>
<p>Please check it out on the SDL website at:<br />
<a href="http://www.libsdl.org/gsoc.php">http://www.libsdl.org/gsoc.php</a></p>
<p>Because we&#8217;ve had applicants with widely varying skill levels, this year I&#8217;m asking all prospective applicants to submit a sample patch for SDL 1.3. It doesn&#8217;t have to be big or even accepted, but it does have to show familiarity with the development process and good coding practices.</p>
<p>Happy Coding! <img src='http://www.sdltutorials.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
&#8211;<br />
-Sam Lantinga, Founder and President, Galaxy Gameworks LLC</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/google-summer-of-code-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tetris Clone Contest &#8211; Prize Update</title>
		<link>http://www.sdltutorials.com/tetris-clone-contest-prize-updateclone/</link>
		<comments>http://www.sdltutorials.com/tetris-clone-contest-prize-updateclone/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 18:44:20 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=334</guid>
		<description><![CDATA[I&#8217;ve updated the prize to $20, just as a small incentive. I know it&#8217;s not much, but hey, it&#8217;s something right? Check out the Contest here.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve updated the prize to $20, just as a small incentive. I know it&#8217;s not much, but hey, it&#8217;s something right?</p>
<p><a href="http://www.sdltutorials.com/new-contest/">Check out the Contest here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/tetris-clone-contest-prize-updateclone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Attack of the Hover Cat</title>
		<link>http://www.sdltutorials.com/attack-of-the-hover-cat/</link>
		<comments>http://www.sdltutorials.com/attack-of-the-hover-cat/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 15:50:53 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=326</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.sdltutorials.com/wp-content/uploads/2KwfT.jpg"><img src="http://www.sdltutorials.com/wp-content/uploads/2KwfT.jpg" alt="" title="2KwfT" width="500" height="348" class="aligncenter size-full wp-image-327" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/attack-of-the-hover-cat/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tetris Clone Contest Follow Up</title>
		<link>http://www.sdltutorials.com/tetris-clone-contest-follow-up/</link>
		<comments>http://www.sdltutorials.com/tetris-clone-contest-follow-up/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 11:08:36 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=324</guid>
		<description><![CDATA[How&#8217;s everyone doing on the contest. Is anyone participating? I hope so. Here&#8217;s a bit of inspiration to keep you going: I like how they added the ability to swap between two shapes, and how certain shapes group together. The interesting thing about Pokemon Puzzle League is how the entire play area is really a [...]]]></description>
			<content:encoded><![CDATA[<p>How&#8217;s everyone doing on the contest. Is anyone participating? I hope so. Here&#8217;s a bit of inspiration to keep you going:</p>
<p><object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/3PoVKediEM4&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/3PoVKediEM4&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object></p>
<p>I like how they added the ability to swap between two shapes, and how certain shapes group together.</p>
<p><object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/yP4dvK62nVs&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/yP4dvK62nVs&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object></p>
<p>The interesting thing about Pokemon Puzzle League is how the entire play area is really a cylinder that can rotate around.</p>
<p><object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/F8dS_L5DNtE&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/F8dS_L5DNtE&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object></p>
<p>Dr. Robotniks Mean Bean Machine is good in terms of multiplayer play.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/tetris-clone-contest-follow-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More Blog Like?</title>
		<link>http://www.sdltutorials.com/more-blog-like/</link>
		<comments>http://www.sdltutorials.com/more-blog-like/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 12:43:35 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=312</guid>
		<description><![CDATA[Quick question for all you people out there: Edit: Alright, I&#8217;ll give it a shot and see how it goes.]]></description>
			<content:encoded><![CDATA[<p>Quick question for all you people out there:</p>
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.
<p><b>Edit:</b><br />
Alright, I&#8217;ll give it a shot and see how it goes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/more-blog-like/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 [...]]]></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>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>SDL Collision Events</title>
		<link>http://www.sdltutorials.com/sdl-collision-events/</link>
		<comments>http://www.sdltutorials.com/sdl-collision-events/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 11:27:56 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[SDL Tutorials]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=119</guid>
		<description><![CDATA[Finally! The next tutorial. This is the 2nd part to the SDL Collision tutorial. We&#8217;ll be looking at collision events, the part we left unfinished in the last tutorial. To refresh your memory, a collision event is an event that is stored in queue after a collision has taken place. Take our recent shooter contest [...]]]></description>
			<content:encoded><![CDATA[<p>Finally! The next tutorial. This is the 2nd part to the SDL Collision tutorial. We&#8217;ll be looking at collision events, the part we left unfinished in the last tutorial. To refresh your memory, a collision event is an event that is stored in queue after a collision has taken place. Take our recent shooter contest for an example. When the player fires a bullet, the bullet flies through the air, and then hits the enemy. The moment the bullet and the enemy collide, an event is triggered and stored in the queue. After all movement has taken place, the queue is iterated through, and events can appropriately respond.<br />
<span id="more-119"></span></p>
<p>So lets get started. Open up your project from the last tutorial. We&#8217;re first going to make a few prelimary changes. Make a new file called CEntityCol.cpp. Inside that file, put the following:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &quot;CEntity.h&quot;</span></p>
<p>std::<span class="me2">vector</span>&lt;CEntityCol&gt; CEntityCol::<span class="me2">EntityColList</span>;</p>
<p>CEntityCol::<span class="me2">CEntityCol</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; this-&gt;EntityA = <span class="kw2">NULL</span>;<br />
&nbsp; &nbsp; this-&gt;EntityB = <span class="kw2">NULL</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Now, get rid of that code from CEntity.cpp. I am basically moving the code to its own file so that everything is much neater. The second thing. Open up CEntity.h and change:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">virtual</span> <span class="kw4">void</span> OnCollision<span class="br0">&#40;</span>CEntity* Entity<span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>To this:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">virtual</span> <span class="kw4">bool</span> OnCollision<span class="br0">&#40;</span>CEntity* Entity<span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>Also, change the void to bool in CEntity.cpp, CPlayer.h, and CPlayer.cpp. Why the change? Well, you&#8217;ll see in a minute. <img src='http://www.sdltutorials.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>We&#8217;re already storing the collision events in a queue, now we need to do something with them. Open up CApp_OnLoop.cpp. You&#8217;ll notice we already have a loop going for entities and we&#8217;re calling OnLoop. We&#8217;re basically going to do the same thing again, but after our first loop:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>;i &lt; CEntity::<span class="me2">EntityList</span>.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;i++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!CEntity::<span class="me2">EntityList</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="kw1">continue</span>;</p>
<p>&nbsp; &nbsp; CEntity::<span class="me2">EntityList</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span>-&gt;OnLoop<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="co1">//Collision Events</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>;i &lt; CEntityCol::<span class="me2">EntityColList</span>.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;i++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; CEntity* EntityA = CEntityCol::<span class="me2">EntityColList</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">EntityA</span>;<br />
&nbsp; &nbsp; CEntity* EntityB = CEntityCol::<span class="me2">EntityColList</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">EntityB</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>EntityA == <span class="kw2">NULL</span> || EntityB == <span class="kw2">NULL</span><span class="br0">&#41;</span> <span class="kw1">continue</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>EntityA-&gt;OnCollision<span class="br0">&#40;</span>EntityB<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; EntityB-&gt;OnCollision<span class="br0">&#40;</span>EntityA<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p>CEntityCol::<span class="me2">EntityColList</span>.<span class="me1">clear</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>Not much going on here. We&#8217;re first iterating through every event in the queue. We then grab the two entities involved (EntityA, and EntityB), and we make sure they are valid pointers. If so, we first tell EntityA that it has collided with EntityB by calling OnCollision. Now, this is where that void to bool change comes in. EntityA can choose to prevent EntityB from knowing it has collided with him. If you remember, only Entities that are moving check for collision. This would mean EntityA is definitly a moving entity, and EntityB may or may not be moving.</p>
<p>Here&#8217;s an example to explain why you wouldn&#8217;t want to waste time calling EntityB&#8217;s OnCollision. Say you fire a bullet, and it hits a rock entity. Now, lets say the rock has a flag stating it&#8217;s indestructable. Our rock doesn&#8217;t care its been hit. So, when the OnCollision is called for the bullet, the bullet can see the entity it has hit is indestructable. And because of that, it can create a little &#8220;bullet being destroyed&#8221; animation, and then we can ignore telling the rock about it.</p>
<p>This is about the extent of entity collisions. So, now that we have this base, lets do something semi-useful with it.</p>
<p>Open up CEntity.h and add this new function, and variable:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">protected</span>:<br />
&nbsp; &nbsp; <span class="kw4">bool</span>&nbsp; &nbsp; CanJump;</p>
<p><span class="kw2">public</span>:<br />
&nbsp; &nbsp; <span class="kw4">bool</span> &nbsp; &nbsp;Jump<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>Then, open up CEntity.cpp, and add the Jump function. Also make CanJump = false in the constructor:</p>
<div class="dean_ch" style="white-space: wrap;">
CEntity::<span class="me2">CEntity</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; CanJump = <span class="kw2">false</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// &#8230; Other code &#8230; </span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">bool</span> CEntity::<span class="me2">Jump</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>CanJump == <span class="kw2">false</span><span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">false</span>;</p>
<p>&nbsp; &nbsp; SpeedY = -MaxSpeedY;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Finally, go to the OnMove function. We&#8217;re going to figure out when our entity is allowed to jump. An entity is allowed to jump whenever it is touching the ground &#8211; or, in other words, whenever the entity is no longer moving on the positive Y-axis. So, at the top, we are first always going to reset CanJump to false:</p>
<div class="dean_ch" style="white-space: wrap;">
CanJump = <span class="kw2">false</span>;<br />
&nbsp;</div>
<p>Now, go down a bit, and modify the PosValid for the Y-axis to the following:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">if</span><span class="br0">&#40;</span>PosValid<span class="br0">&#40;</span><span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span><span class="br0">&#40;</span>X<span class="br0">&#41;</span>, <span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span><span class="br0">&#40;</span>Y + NewY<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; Y += NewY;<br />
<span class="br0">&#125;</span><span class="kw1">else</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>MoveY &gt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CanJump = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; SpeedY = <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Now, to run through everything we did. First, we have a flag that states if an entity can jump. When set to true, we are allowed to call the Jump function. This CanJump variable is always false, unless our feet are on the ground. Whenever we actually do want to jump, we simply set our SpeedY in the negative Y-axis direction (up). Remember, gravity is always pushing us down, but at a slow speed. So, if we have a SpeedY of -5, and a AccelY of 0.75, we&#8217;ll move up for a time, gravity will kick in, and then we will eventually be pushed back down. Thus, giving the effect of a real jump. The trick here is to get the Speed and Acceleration variables just right.</p>
<p>Also, as a side note, the reason we made Jump return true/false is simply for informational purposes. Meaning, when a key is pressed by the player to jump, they won&#8217;t always be allowed to jump. And, if you wanted, you could do something based upon what Jump() returns.</p>
<p>So a few more tweaks before we try this out. In the constructor of CEntity, change the MaxSpeedX and MaxSpeedY to 10. Then, open up CApp_OnEvent.cpp and add the following to OnKeyDown:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">case</span> SDLK_SPACE: <span class="br0">&#123;</span><br />
&nbsp; &nbsp; Player.<span class="me1">Jump</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw2">break</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>And one last thing. Open up CPlayer.cpp, and change the OnCollision function to this:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">bool</span> CPlayer::<span class="me2">OnCollision</span><span class="br0">&#40;</span>CEntity* Entity<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; Jump<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>That&#8217;s it! Compile, run, and have fun with Yoshi jumping all over the place. You&#8217;ll notice the simple collision event we added is that when Yoshi 1 collides with Yoshi 2, it causes Yoshi 1 to jump. Kind of lame, but you should get the idea.</p>
<p><b>SDL Collision Events &#8211; Tutorial Files:</b><br />
<b>Win32:</b> <a href="../tutorials/sdl-collision-events.zip">Zip</a>, <a href="../tutorials/sdl-collision-events.rar">Rar</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/sdl-collision-events/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>
