<?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>Last Stop &#187; os x</title>
	<atom:link href="http://laststop.spaceislimited.org/tag/os-x/feed/" rel="self" type="application/rss+xml" />
	<link>http://laststop.spaceislimited.org</link>
	<description>it&#039;s going to be a long ride</description>
	<lastBuildDate>Wed, 28 Jan 2009 13:21:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Programming Pong in C and OpenGL &#8211; Part II</title>
		<link>http://laststop.spaceislimited.org/2008/06/02/programming-pong-in-c-and-opengl-part-ii/</link>
		<comments>http://laststop.spaceislimited.org/2008/06/02/programming-pong-in-c-and-opengl-part-ii/#comments</comments>
		<pubDate>Tue, 03 Jun 2008 01:50:22 +0000</pubDate>
		<dc:creator>Timothy M. Rodriguez</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[game programming]]></category>
		<category><![CDATA[glut]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[pong]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[This]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://laststop.spaceislimited.org/?p=17</guid>
		<description><![CDATA[This is the second part in what should be a four part series on how to program your very own Pong clone. The first article went over setting up your environment in your favorite OS, now this part gets into some of the mechanics or details on how to actually get started. Before I start [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second part in what should be a four part series on how to program your very own Pong clone.  The first article went over setting up your environment in your favorite OS, now this part gets into some of the mechanics or details on how to actually get started.</p>
<p>Before I start with anything, we have to think about simple animation.  In fact, <a title="Animation" href="http://en.wikipedia.org/wiki/Animation" target="_blank">animation</a> is just simply a series of static images, with each image slightly displaced from the last.</p>
<p>So what does that mean?<span id="more-17"></span></p>
<p>For example, if I&#8217;m going to <span style="text-decoration: line-through;">draw</span> animate a ball falling from mid-air, I would first start with an image of the ball in mid-air, precisely as it would look the moment it began to fall.  The next <em>frame</em>, or image, would be exactly that, but with the ball a little bit lower down.  I would then continue to draw sequential images of the ball, with each image showing it a little bit further than the last.  This is how <strong>every</strong> moving image you see works (well, just about).  A series of images is drawn in front of you, quick enough that you can&#8217;t tell.</p>
<p>So the next question is, what&#8217;s quick enough?  It turns out, this isn&#8217;t such a simple question.  In fact, the answer is different for different animals!  But for humans, it turns out, about 30fps, or 30 frames-per-second, is what it takes to fool us.  The movies you see in the theatre are (at-the-moment anyways) displayed at 24fps, but most computer screens require 60fps to prevent you from seeing the flicker (some, like myself, need an 85Hz (fps) refresh rate, others up to 120!).  The reasons for these differences has a lot to do with the way the image is drawn and the way your eye views things.  It&#8217;s a pretty involved topic, but if you guys would like me to go into it more, let me know in the comments.</p>
<h3>A Simple Animation Loop</h3>
<p>Taking what we just learned, let&#8217;s think about how to draw a box moving across the screen.  Let&#8217;s pretend we have a function that draws a box centered at a point we specify on the screen (in 2-D Space).  It&#8217;s C function prototype would look something like this:</p>
<blockquote><p>void drawbox(int x, int y);</p></blockquote>
<p>So if we were going to draw a box that just infinitely moved to the right (or at least until it was off screen).  We might decide to create a loop that drew it a little bit further each time.  That might look something like this:</p>
<blockquote><p>#define TRUE 1</p>
<p>int x=0;</p>
<p>while(TRUE) {</p>
<p>drawbox(0+x, 0);</p>
<p>x++;</p>
<p>}</p></blockquote>
<p>This little snippet of code, runs an infinite loop and draws a box at a point 0+x, 0 and then increments x.  Let&#8217;s look a little further.  First it draws a box at point(0,0) since x is 0 on first count.  Then x is incremented and the loop goes on again.  Next, it draws a box at (1,0) and increments x.  And so on.  Congratulations, you just made your first animation loop!</p>
<h3>Marching Onward</h3>
<p>In the beginning, video games were hand-coded in assembly directly to a particular hardware.  This had a lot of benefits, primarily speed, which was very important when hardware was relatively expensive.  But as computers progressed and the market expanded, it became necessary to program for a multitude of different hardwares.  And this is when a nasty problem reared it&#8217;s head.  You see, when you decide to have the computer draw something on the screen, it doesn&#8217;t just do it instantaneously.  The computer has to go and draw each individual pixel on the screen (also a topic for another discussion).</p>
<p>This</p>
<p style="padding-left: 30px;">takes</p>
<p style="padding-left: 60px;">time.</p>
<p>Moreover, the time it takes on your brand-spanking new ultra-super processor is not the same time it takes your friend down the street who isn&#8217;t as <em>enthusiastic</em> as you are.  This is a problem, because if you set up all your animations expecting the processor to draw 30 frames per second on your machine.  It&#8217;s going to run at less than that on your friends machine.  (In fact, there&#8217;s some math that shows it will be a fraction/multiple of their refresh rate).  Great, so your fast-action shoot-em-up just got stuck in the mud.</p>
<h3>What&#8217;s a game programmer to do?</h3>
<p>There&#8217;s a really simple answer to all of this&#8211;use our friend (archenemy) time.  You see, rather than making your animation speed depend on the animation loop (which depends on your processor speed), have the your animation be a fixed function of time.</p>
<p>Take another look at our first example.  The movement speed of the box can really be expressed as 1 unit in the x direction per loop iteration.  Really what we want is for the box to move some number of units in the x direction per second (our hour if you like).  To do this we need to express our box movement as a function of time and then know how long its been since the computer last drew the box.  Essentially, we want our animation loop to look like this.</p>
<blockquote><p>#define TRUE 1</p>
<p>int speed = 1;     //1 unit per second</p>
<p>int x = 0;</p>
<p>startTime = 0;</p>
<p>elapsedTime = 0;</p>
<p>while(TRUE) {</p>
<p>startTime = getTime();</p>
<p>x += speed * elapsedTime;</p>
<p>drawbox(0+x, 0)</p>
<p>elapsedTime = getTime() &#8211; startTime;</p>
<p>}</p></blockquote>
<p>As you can see, this simple example becomes a lot more complicated once we add timing to the mix.  First, we defined a universal speed for our box.  We still kept the x value, as this represents the boxes point in space, not it&#8217;s speed.  Then we created variables for keep track when we started our animation loop and how long we were in it.  We&#8217;re also assuming we have a function that gives us the time, called getTime() here, and that it returns us the current time in seconds.</p>
<p>First we enter our loop and fetch our start time from the getTime() function.  Next we set our x position based on how long it&#8217;s been since our last loop.  We multiply our elapsedTime by the speed of our box and add that <em>distance</em> to our x-position.  Then, we draw our box at it&#8217;s new position.  Finally, we figure out how long it took us to draw that box by setting our elapsed time equal to the current time minus the time we started drawing the box.  Now, no matter the computer, our box will move along the screen at the same speed.  Note, this does not mean the animation is fluid!  If your friend&#8217;s computer is really slow (and I mean really if it can&#8217;t draw a box) it may only get to draw a box once every 5 seconds.  By then, our box may already be off the screen!</p>
<p>So I hope that&#8217;s enough for you to digest for a while. Stay tuned for my next post on some of the basic mechanics of GLUT and OpenGL.  Until then, it might be useful to brush up on your C programming on the interwebs!</p>
]]></content:encoded>
			<wfw:commentRss>http://laststop.spaceislimited.org/2008/06/02/programming-pong-in-c-and-opengl-part-ii/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Programming Pong in C and OpenGL &#8211; Part I</title>
		<link>http://laststop.spaceislimited.org/2008/05/17/programming-a-pong-clone-in-c-and-opengl-part-i/</link>
		<comments>http://laststop.spaceislimited.org/2008/05/17/programming-a-pong-clone-in-c-and-opengl-part-i/#comments</comments>
		<pubDate>Sun, 18 May 2008 00:48:35 +0000</pubDate>
		<dc:creator>Timothy M. Rodriguez</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[glut]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[pong]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://laststop.spaceislimited.org/?p=13</guid>
		<description><![CDATA[If you ever wanted to program your own video game, this is a good place to start. I remember when I started learning programming it was surprisingly difficult to find out how to make an honest and simple game. In fact, after reading most programming books, the only type of game you could make would [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://laststop.spaceislimited.org/wp-content/uploads/2008/05/pongactionshot.png"><img class="aligncenter size-full wp-image-14" title="Pong" src="http://laststop.spaceislimited.org/wp-content/uploads/2008/05/pongactionshot.png" alt="Unbelievable Action!" width="499" height="518" /></a></p>
<p>If you ever wanted to program your own video game, this is a good place to start.  I remember when I started learning programming it was surprisingly difficult to find out how to make an honest and simple game.  In fact, after reading most programming books, the only type of game you could make would be a <a href="http://en.wikipedia.org/wiki/Zork">Zork</a> clone.</p>
<blockquote><p>You are in a dank cave and you can see nothing, but you hear a walrus in the distance.</p>
<p>What do you do?</p>
<ol>
<li>Look for the walrus.</li>
<li>Watch <a href="http://www.imdb.com/title/tt0118715/">The Big Lebowski</a></li>
<li>Decide that the walrus is Paul.</li>
</ol>
</blockquote>
<p>This is not to knock Zork in any way!  Zork is a clever, well written, and extremely well thought out game (even if my pseudo-quote isn&#8217;t).  But in a world where we&#8217;re spoiled by moving objects, you want to know how to move and interact with something on screen.  The problem is it takes a lot of different knowledge sets to get the job done.  But in this post, you&#8217;ll get started learning how to make your very own <a title="Pong" href="http://http//en.wikipedia.org/wiki/Pong" target="_blank">Pong</a> clone.<span id="more-13"></span></p>
<h3>A Few Notes..</h3>
<p>First, a few notes about setting up.  I&#8217;m writing this tutorial from the Mac OS X perspective.  However, everything we are about to code is, essentially, <strong>cross-platform</strong>.  This is because, it is written using plaing old C, OpenGL, and GLUT.  All of these things are themselves cross-platform, and therefore, can be compiled anywhere you have a C compiler (OpenGL and GLUT are just C libraries).</p>
<h3>Setting up your environment</h3>
<h4>Mac OS X</h4>
<p>For some reason, this always seems to be the hardest part whenever learning something new in programming.  Fortunately, I found a great blog post that makes setting up XCode for compiling an app using OpenGL and GLUT very easy.  Read all about setting up your <a title="XCode with OpenGL and GLUT" href="http://blog.onesadcookie.com/2007/12/xcodeglut-tutorial.html" target="_blank">XCode</a>.</p>
<h4>Windows</h4>
<p>The process in Windows is actually a bit easier (gasp!) than in Mac OS X and there&#8217;s plenty of resources available on Google for getting setup.  Here&#8217;s one in <a title="Setting up OpenGL and GLUT in Windows" href="http://csf11.acs.uwosh.edu/cs371/visualstudio/" target="_blank">particular</a>.</p>
<h4>Linux</h4>
<p>Here&#8217;s something unusual, you guys get a <a title="OpenGL and GLUT in Linux" href="http://www.youtube.com/watch?v=p4hA-lcKTmE" target="_blank">video</a>.</p>
<p>Congratulations, the hardest part is done!  In my next blog post.  We&#8217;ll start to get knee deep in game programming concepts.</p>
<h2>Update!</h2>
<p><a title="Programming a Pong Clone - Part II" href="http://laststop.spaceislimited.org/2008/06/02/programming-pong-in-c-and-opengl-part-ii/" target="_self">Part II</a> is Up!</p>
]]></content:encoded>
			<wfw:commentRss>http://laststop.spaceislimited.org/2008/05/17/programming-a-pong-clone-in-c-and-opengl-part-i/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

