<?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>Crows Programming &#187; C/C++</title>
	<atom:link href="http://www.crowsprogramming.com/archives/category/programming/cc/feed" rel="self" type="application/rss+xml" />
	<link>http://www.crowsprogramming.com</link>
	<description>Computer Programming and Random Blurbs</description>
	<lastBuildDate>Mon, 19 Sep 2011 20:53:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>SDL &amp; OpenGL Game with C++ &#8211; Part II</title>
		<link>http://www.crowsprogramming.com/archives/117</link>
		<comments>http://www.crowsprogramming.com/archives/117#comments</comments>
		<pubDate>Tue, 08 Dec 2009 05:39:32 +0000</pubDate>
		<dc:creator>crow</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=117</guid>
		<description><![CDATA[Parts in the Series Part I &#8211; Getting Started Part II &#8211; Drawing a Quad Rendering a Quad with OpenGL and SDL Ok, so if we are going to program a video game then we need to draw something with OpenGL. That being said, last time we left off with a basic framework for creating [...]]]></description>
			<content:encoded><![CDATA[<h3> Parts in the Series </h3>
<ul>
<li><a href="http://www.crowsprogramming.com/archives/104"> Part I  &#8211; Getting Started </a></li>
<li><a href="http://www.crowsprogramming.com/archives/117"> Part II &#8211; Drawing a Quad </a></li>
</ul>
<h3> Rendering a Quad with OpenGL and SDL</h3>
<p>Ok, so if we are going to <b>program a video game</b> then we need to <b>draw something with OpenGL</b>. </p>
<p>That being said, last time we left off with a basic framework for creating a game with SDL. If you haven’t read it <a href="http://www.crowsprogramming.com/archives/104"> then you should check out part 1 here</a> because we’ll be building off of that. This time around the goal is simply to get something rendering on the screen with OpenGL. So with that in mind here is the road map for what I wanted to accomplish:</p>
<ul>
<li> Render a single quad </li>
<li> Render a textured quad </li>
</ul>
<p>As you can see I am going to keep it simple for now. I’ll throw in the ability to swap textures at runtime as well just to keep it interesting.</p>
<p>The game so far will look a little something like this:<br />
<a href="http://www.crowsprogramming.com/wp-content/uploads/2009/12/Figure1.png"><br />
<img src="http://www.crowsprogramming.com/wp-content/uploads/2009/12/Figure1-Thumb.png" /><br />
</a></p>
<h2> Drawing a Square With SDL </h2>
<p>First on the list is to render a single quad. But, before you do anything make sure you add the OpenGL libraries to your project. You can accomplish that by going into the linker section of the project settings and adding <i>opengl32.lib</i> and <i>glu32.lib</i> to the additional dependencies section, just as we did for the SDL libraries in <a href="http://www.crowsprogramming.com/archives/104"> Part I</a>. </p>
<p>I am just going to start out showing you the new main function and then we will go through it step by step. So here ya go!</p>
<p><span id="more-117"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// graphics properties</span>
	<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> width <span style="color: #339933;">=</span> <span style="color: #0000dd;">640</span><span style="color: #339933;">;</span>        <span style="color: #666666; font-style: italic;">// width of the game window </span>
	<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> height <span style="color: #339933;">=</span> <span style="color: #0000dd;">480</span><span style="color: #339933;">;</span>       <span style="color: #666666; font-style: italic;">// height of the game window</span>
	<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> fov <span style="color: #339933;">=</span> <span style="color:#800080;">45.0f</span><span style="color: #339933;">;</span>      <span style="color: #666666; font-style: italic;">// field of view (degrees)</span>
	<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> nearClip <span style="color: #339933;">=</span> <span style="color:#800080;">1.0f</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// near clip plane (don't set to zero)</span>
	<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> farClip <span style="color: #339933;">=</span> <span style="color:#800080;">100.0f</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// far clip plane</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// initialize SDL</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> SDL_Init<span style="color: #009900;">&#40;</span> SDL_INIT_VIDEO <span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
              fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Failed to initialize SDL. [%s]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> SDL_GetError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// set desired OpenGL attributes prior to selecting a video mode</span>
	SDL_GL_SetAttribute<span style="color: #009900;">&#40;</span> SDL_GL_DEPTH_SIZE<span style="color: #339933;">,</span> <span style="color: #0000dd;">16</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// use 16 bits for depth buffer</span>
	SDL_GL_SetAttribute<span style="color: #009900;">&#40;</span> SDL_GL_DOUBLEBUFFER<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// enable double buffering</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// create an OpenGL surface/window</span>
	SDL_Surface <span style="color: #339933;">*</span>screen <span style="color: #339933;">=</span> SDL_SetVideoMode<span style="color: #009900;">&#40;</span>width<span style="color: #339933;">,</span> height<span style="color: #339933;">,</span> <span style="color: #0000dd;">32</span><span style="color: #339933;">,</span> SDL_OPENGL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> screen <span style="color: #339933;">==</span> NULL <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
              fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Failed to set video mode. [%s]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> SDL_GetError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// initialize OpenGL</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> SetupOpenGL<span style="color: #009900;">&#40;</span> width<span style="color: #339933;">,</span> height<span style="color: #339933;">,</span> fov<span style="color: #339933;">,</span> nearClip<span style="color: #339933;">,</span> farClip<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		fprintf<span style="color: #009900;">&#40;</span> stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Failed to setup OpenGL.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// main game loop</span>
	bool done <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>done <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		RenderScene<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> ProcessInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
			done <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	SDL_Quit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The variable declarations on lines 4-8 just control the graphical properties of the application. They’re pretty self-explanatory. You can change the width or height and so on. </p>
<p>Next up you’ll see that the code to initialize the video mode (lines 17-27) has been changed. SDL gives us certain attributes that can modify the behavior of OpenGL, <i>but</i> these changes <i>only</i> take place after a call to SDL_SetVideoMode. In this case we’re telling SDL that we would like OpenGL to use a 16-bit depth buffer and to use double buffering when rendering (actively draws to an off screen surface, not the screen). Note that technically we should check the value of these attributes after the SDL_SetVideoMode call to ensure that they were set successfully. You could (and technically should) do this with the correct calls to SDL_GL_GetAttribute. You’ll also notice that the flags parameter for SDL_SetVideoMode now uses SDL_OPENGL instead of SDL_SWSURFACE since we are specifying that we want to use OpenGL for drawing.</p>
<p>Following the setting of the video mode I make a call to SetupOpenGL which is going to configure OpenGL for the application. I’ll detail this function shortly. The last major change to the code is the addition of RenderScene in the main loop. This is the function that will actually draw the game on the screen. Everthing should seem pretty straight forward.</p>
<h2>Configuring OpenGL</h2>
<p>So now I am going to show you SetupOpenGL. The entire purpose of this function is simply to setup things with respect to OpenGL that we won&#8217;t be changing again or that need a known initial state. It&#8217;s pretty simple stuff.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> SetupOpenGL<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> width<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> height<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> fov<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> nearClip<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> farClip <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	glClearColor<span style="color: #009900;">&#40;</span> <span style="color:#800080;">0.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #993333;">float</span> ar <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>GLfloat<span style="color: #009900;">&#41;</span>width <span style="color: #339933;">/</span> height<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// display aspect ratio</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// setup projection matrix</span>
	glMatrixMode<span style="color: #009900;">&#40;</span>GL_PROJECTION<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>						
	glLoadIdentity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>						
	gluPerspective<span style="color: #009900;">&#40;</span>fov<span style="color: #339933;">,</span> ar<span style="color: #339933;">,</span> nearClip<span style="color: #339933;">,</span> farClip<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// setup model view matrix</span>
	glMatrixMode<span style="color: #009900;">&#40;</span>GL_MODELVIEW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
	glLoadIdentity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// setup depth buffer</span>
	glClearDepth<span style="color: #009900;">&#40;</span><span style="color:#800080;">1.0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>						
	glEnable<span style="color: #009900;">&#40;</span>GL_DEPTH_TEST<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>						
	glDepthFunc<span style="color: #009900;">&#40;</span>GL_LEQUAL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
	<span style="color: #666666; font-style: italic;">// misc. GL settings</span>
	glHint<span style="color: #009900;">&#40;</span>GL_PERSPECTIVE_CORRECTION_HINT<span style="color: #339933;">,</span> GL_NICEST<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	glShadeModel<span style="color: #009900;">&#40;</span>GL_SMOOTH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	glViewport<span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> height <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here I do some basic setup work:</p>
<ul>
<li> Setup the projection matrix with gluPerspective </li>
<li> Set the view matrix to identity</li>
<li> Setup the depth buffer</li>
<li> Setup some various GL settings such as the shade model</li>
<li> And finally setup the viewport</li>
</ul>
<p>If that looks like gably gook to you then you should check out the <a href="http://www.opengl.org/documentation/red_book/">the OpenGL Red Book</a>. The 3D transformation pipeline, matrix math, etc is a vast subject and beyond the scope of this post.</p>
<h2> Render Scene </h2>
<p>Next let’s look at the render scene function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> RenderScene<span style="color: #009900;">&#40;</span> <span style="color: #993333;">void</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// render everything for the current frame</span>
&nbsp;
	glClear<span style="color: #009900;">&#40;</span> GL_COLOR_BUFFER_BIT <span style="color: #339933;">|</span> GL_DEPTH_BUFFER_BIT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	glMatrixMode<span style="color: #009900;">&#40;</span> GL_MODELVIEW <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	glLoadIdentity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// move off center so the quad is visible</span>
	glTranslatef<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">5.0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// draw quad as red</span>
	glColor3f<span style="color: #009900;">&#40;</span> <span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// render the quad</span>
	glBegin<span style="color: #009900;">&#40;</span>GL_QUADS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>						
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
		glVertex3f<span style="color: #009900;">&#40;</span> <span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
		glVertex3f<span style="color: #009900;">&#40;</span> <span style="color:#800080;">1.0f</span><span style="color: #339933;">,-</span><span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">1.0f</span><span style="color: #339933;">,-</span><span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
	glEnd<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// update display</span>
	SDL_GL_SwapBuffers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So this function will be called every frame to draw the entire screen or &#8220;scene&#8221;. In this example, it’s simply drawing a red quad on the screen. What’s happening should be clear. First the framebuffer is cleared to get rid of the last scene. Then the identity matrix is loaded into the view matrix so that we are in the center of our world. After that the &#8220;camera&#8221; is positioned slightly to the left and back a little so the quad is nice and visible. Also, remember OpenGL uses a <a href="http://en.wikipedia.org/wiki/Cartesian_coordinate_system"> right handed system </a> so to move the camera back we move along the negative z-axis. Moving on, the active color is then set to red and the quad is drawn vertex by vertex inside the glBegin/glEnd calls. Note that we are not culling back faces so we don’t worry about vertex winding order. Finally a call to SDL_GL_SwapBuffers is made so that the output can be seen. You should see a nice pretty red square.</p>
<h2> ProcessInput </h2>
<p>The ProcessInput function is responsible for reacting to input events like key presses. Basically it’s the same code from the last example moved into its own little function. It returns true to indicate that the user has indicated he would like to exit the game. Here is the code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">bool ProcessInput<span style="color: #009900;">&#40;</span> <span style="color: #993333;">void</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// runs the input processing loop</span>
	<span style="color: #666666; font-style: italic;">// returns true if any input has requested the application to exit</span>
&nbsp;
	SDL_Event evt<span style="color: #339933;">;</span>
	bool exitFlag <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// check for events generated from SDL</span>
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> SDL_PollEvent<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>evt <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span> evt.<span style="color: #202020;">type</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">case</span> SDL_QUIT<span style="color: #339933;">:</span>
			exitFlag <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">case</span> SDL_KEYUP<span style="color: #339933;">:</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> evt.<span style="color: #202020;">key</span>.<span style="color: #202020;">keysym</span>.<span style="color: #202020;">sym</span> <span style="color: #339933;">==</span> SDLK_ESCAPE <span style="color: #009900;">&#41;</span>
				exitFlag <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> exitFlag<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2> Complete Code Listing </h2>
<p>Ok, that was great fun. Now, behold the entire code listing!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;windows.h&gt;</span>
&nbsp;
<span style="color: #339933;">#include &lt;gl\gl.h&gt;</span>
<span style="color: #339933;">#include &lt;gl\glu.h&gt;</span>
&nbsp;
<span style="color: #339933;">#include &lt;sdl.h&gt;</span>
<span style="color: #339933;">#include &lt;sdl_opengl.h&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// function prototypes</span>
<span style="color: #993333;">int</span> SetupOpenGL<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> width<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> height<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> fov<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> nearClip<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> farClip <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> RenderScene<span style="color: #009900;">&#40;</span> <span style="color: #993333;">void</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
bool ProcessInput<span style="color: #009900;">&#40;</span> <span style="color: #993333;">void</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// graphics properties</span>
	<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> width <span style="color: #339933;">=</span> <span style="color: #0000dd;">640</span><span style="color: #339933;">;</span>        <span style="color: #666666; font-style: italic;">// width of the game window </span>
	<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> height <span style="color: #339933;">=</span> <span style="color: #0000dd;">480</span><span style="color: #339933;">;</span>       <span style="color: #666666; font-style: italic;">// height of the game window</span>
	<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> fov <span style="color: #339933;">=</span> <span style="color:#800080;">45.0f</span><span style="color: #339933;">;</span>      <span style="color: #666666; font-style: italic;">// field of view (degrees)</span>
	<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> nearClip <span style="color: #339933;">=</span> <span style="color:#800080;">1.0f</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// near clip plane (don't set to zero)</span>
	<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> farClip <span style="color: #339933;">=</span> <span style="color:#800080;">100.0f</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// far clip plane</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// initialize SDL</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> SDL_Init<span style="color: #009900;">&#40;</span> SDL_INIT_VIDEO <span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Failed to initialize SDL. [%s]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> SDL_GetError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// set desired OpenGL attributes prior to selecting a video mode</span>
	SDL_GL_SetAttribute<span style="color: #009900;">&#40;</span> SDL_GL_DEPTH_SIZE<span style="color: #339933;">,</span> <span style="color: #0000dd;">16</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// use 16 bits for depth buffer</span>
	SDL_GL_SetAttribute<span style="color: #009900;">&#40;</span> SDL_GL_DOUBLEBUFFER<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// enable double buffering</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// create an OpenGL surface/window</span>
	SDL_Surface <span style="color: #339933;">*</span>screen <span style="color: #339933;">=</span> SDL_SetVideoMode<span style="color: #009900;">&#40;</span>width<span style="color: #339933;">,</span> height<span style="color: #339933;">,</span> <span style="color: #0000dd;">32</span><span style="color: #339933;">,</span> SDL_OPENGL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> screen <span style="color: #339933;">==</span> NULL <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
		fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Failed to set video mode. [%s]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> SDL_GetError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// initialize OpenGL</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> SetupOpenGL<span style="color: #009900;">&#40;</span> width<span style="color: #339933;">,</span> height<span style="color: #339933;">,</span> fov<span style="color: #339933;">,</span> nearClip<span style="color: #339933;">,</span> farClip<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		fprintf<span style="color: #009900;">&#40;</span> stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Failed to setup OpenGL.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// main game loop</span>
	bool done <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>done <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		RenderScene<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> ProcessInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
			done <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	SDL_Quit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> SetupOpenGL<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> width<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> height<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> fov<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> nearClip<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> farClip <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	glClearColor<span style="color: #009900;">&#40;</span> <span style="color:#800080;">0.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #993333;">float</span> ar <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>GLfloat<span style="color: #009900;">&#41;</span>width <span style="color: #339933;">/</span> height<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// display aspect ratio</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// setup projection matrix</span>
	glMatrixMode<span style="color: #009900;">&#40;</span>GL_PROJECTION<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>						
	glLoadIdentity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>						
	gluPerspective<span style="color: #009900;">&#40;</span>fov<span style="color: #339933;">,</span> ar<span style="color: #339933;">,</span> nearClip<span style="color: #339933;">,</span> farClip<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// setup model view matrix</span>
	glMatrixMode<span style="color: #009900;">&#40;</span>GL_MODELVIEW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
	glLoadIdentity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// setup depth buffer</span>
	glClearDepth<span style="color: #009900;">&#40;</span><span style="color:#800080;">1.0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>						
	glEnable<span style="color: #009900;">&#40;</span>GL_DEPTH_TEST<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>						
	glDepthFunc<span style="color: #009900;">&#40;</span>GL_LEQUAL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
	<span style="color: #666666; font-style: italic;">// misc. GL settings</span>
	glHint<span style="color: #009900;">&#40;</span>GL_PERSPECTIVE_CORRECTION_HINT<span style="color: #339933;">,</span> GL_NICEST<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	glShadeModel<span style="color: #009900;">&#40;</span>GL_SMOOTH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	glViewport<span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> height <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> RenderScene<span style="color: #009900;">&#40;</span> <span style="color: #993333;">void</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// render everything for the current frame</span>
&nbsp;
	glClear<span style="color: #009900;">&#40;</span> GL_COLOR_BUFFER_BIT <span style="color: #339933;">|</span> GL_DEPTH_BUFFER_BIT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	glMatrixMode<span style="color: #009900;">&#40;</span> GL_MODELVIEW <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	glLoadIdentity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// move off center so the quad is visible</span>
	glTranslatef<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">5.0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// draw quad as red</span>
	glColor3f<span style="color: #009900;">&#40;</span> <span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// render the quad</span>
	glBegin<span style="color: #009900;">&#40;</span>GL_QUADS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>						
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
		glVertex3f<span style="color: #009900;">&#40;</span> <span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
		glVertex3f<span style="color: #009900;">&#40;</span> <span style="color:#800080;">1.0f</span><span style="color: #339933;">,-</span><span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">1.0f</span><span style="color: #339933;">,-</span><span style="color:#800080;">1.0f</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
	glEnd<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// update display</span>
	SDL_GL_SwapBuffers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
bool ProcessInput<span style="color: #009900;">&#40;</span> <span style="color: #993333;">void</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// runs the input processing loop</span>
	<span style="color: #666666; font-style: italic;">// returns true if any input has requested the application to exit</span>
&nbsp;
	SDL_Event evt<span style="color: #339933;">;</span>
	bool exitFlag <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// check for events generated from SDL</span>
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> SDL_PollEvent<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>evt <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span> evt.<span style="color: #202020;">type</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">case</span> SDL_QUIT<span style="color: #339933;">:</span>
			exitFlag <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">case</span> SDL_KEYUP<span style="color: #339933;">:</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> evt.<span style="color: #202020;">key</span>.<span style="color: #202020;">keysym</span>.<span style="color: #202020;">sym</span> <span style="color: #339933;">==</span> SDLK_ESCAPE <span style="color: #009900;">&#41;</span>
				exitFlag <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> exitFlag<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2> Wrap Up </h2>
<p>Well, now we can at least draw something on the screen. I wanted to do textures on this post but it is already getting pretty lengthy. So, next time we will texture map the quad and work another step closer to having some kind of game programmed.</p>
<p>Have fun!</p>
<fb:like href='http://www.crowsprogramming.com/archives/117' send='false' layout='standard' show_faces='true' width='450' height='65' action='like' colorscheme='light' font='lucida+grande'></fb:like>]]></content:encoded>
			<wfw:commentRss>http://www.crowsprogramming.com/archives/117/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SDL &amp; OpenGL Game with C++ &#8211; Part I</title>
		<link>http://www.crowsprogramming.com/archives/104</link>
		<comments>http://www.crowsprogramming.com/archives/104#comments</comments>
		<pubDate>Mon, 23 Nov 2009 07:22:01 +0000</pubDate>
		<dc:creator>crow</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Game Programming]]></category>

		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=104</guid>
		<description><![CDATA[Parts in the Series Part II &#8211; Drawing a Quad Getting Started I think I&#8217;ve spent too many late nights playing multiplayer Modern Warfare 2 and now I feel the need to build a small game of some kind. Game programming is fun, and although I’ve never done it professionally, I have dabbled with it [...]]]></description>
			<content:encoded><![CDATA[<h3> Parts in the Series </h3>
<ul>
<li><a href="http://www.crowsprogramming.com/archives/117"> Part II &#8211; Drawing a Quad </a></li>
</ul>
<h2> Getting Started </h2>
<p> I think I&#8217;ve spent too many late nights playing multiplayer Modern Warfare 2 and now I feel the need to build a small game of some kind. Game programming is fun, and although I’ve never done it professionally, I have dabbled with it as a hobby for many years. I’ll be documenting my progress here over time via a number of different postings. Maybe some of you will find it useful as a reference or small SDL tutorial or game programming tutorial. That being said, time to get to work!</p>
<p>I have no idea what type of game to build yet so the first thing to do is just get the basic framework setup. I&#8217;ll be using C++ for this game project. I thought about using C# and <a href="http://creators.xna.com">XNA</a> but I need to dust off my C++. For the graphics system I decided to use OpenGL. This is mostly because I am familiar with it and didn&#8217;t want the extra overhead of boilerplate setup stuff associated with DirectX. I just want to get something going quickly. For the rest of the stuff needed (audio, input, etc) I am going to use SDL. SDL is a great tool to get things up and running fast and it easily interfaces with OpenGL. I am also thinking about putting in some UI stuff using <a href="http://www.cegui.org.uk">CEGUI (Crazy Eddie&#8217;s GUI System).</a></p>
<h2>SDL Setup Prerequisites</h2>
<p>Before we get started you’ll want to make sure you have the SDL development libraries (I am using version 1.2.14). You can find them on the SDL website: <a href="http://www.libsdl.org">http://www.libsdl.org.</a> The ones I used specifically are:</p>
<ul>
<li>Runtime &#8211; <a href="http://www.libsdl.org/release/SDL-1.2.14-win32.zip">http://www.libsdl.org/release/SDL-1.2.14-win32.zip</a></li>
<li>Library (Visual Studio 2008) &#8211; <a href="http://www.libsdl.org/release/SDL-devel-1.2.14-VC8.zip">http://www.libsdl.org/release/SDL-devel-1.2.14-VC8.zip</a></li>
</ul>
<p><span id="more-104"></span></p>
<h2> Creating the Project </h2>
<p>Ok enough talk, let&#8217;s get started. You&#8217;ll need to create a new project and correctly configure it to use the SDL library. Fire up Visual Studio and create a new C/C++ console project. In the project wizard, <i>uncheck</i> the &#8220;Using Precompiled Header&#8221; option and <i>check</i> the &#8220;empty project&#8221; option. Now, add a new file and call it <b>main.cpp</b>.</p>
<p>In the Solution Explorer, right click your project and select properties. Open the C/C++ settings and select the general property page. Under the item &#8220;additional include directories&#8221; enter the path to the SDL SDK header files (i.e. C:\YourSDLDir\SDL-1.2.14\include).</p>
<p>Now, drop down to the Code Generation page and under &#8220;Runtime Library&#8221; select &#8220;Multi-threaded DLL&#8221;.  If you try to link with the debug build of the runtime you’ll see a nice error message like this one:</p>
<p><i>warning LNK4098: defaultlib &#8216;msvcrt.lib&#8217; conflicts with use of other libs; use /NODEFAULTLIB:library</i></p>
<p>This is because the SDL runtime is <b>not</b> a debug build. I couldn&#8217;t find a debug build on the SDL website so I guess if you want one you have to build it from source.</p>
<p>That takes care of the C/C++ settings so now let&#8217;s do the linker settings. Under the linker settings select the general page. Under additional library directories enter the path to the lib directory for your SDL SDK (i.e. C:\YourSDLDIR\SDL-1.2.14\lib). On the input page 2 files need to be listed as additional dependencies.</p>
<ul>
<li> SDL.lib</li>
<li> SDLmain.lib</li>
</ul>
<p>Also, make sure you grab SDL.dll from the SDK and put it in your debug folder along side the executable you build.</p>
<p>And that’s it! Go ahead and compile your application to make sure you set everything up correctly.</p>
<h2>Basic SDL Usage</h2>
<p>Next we will fill out main.cpp to get a basic SDL skeleton application running. Start out by including the SDL header file.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;sdl.h&gt;</span></pre></div></div>

<p>Now add the main function:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, an interesting thing to note here is that’s not really your normal main function. It’s not even the main entry point into your application. SDL redefines main using a <i>#define to SDL_main</i>. This also means that you cannot change the function signature as you normally could for a main function. SDL_main just does some basic housekeeping/setup stuff on behalf of SDL.</p>
<h2> SDL Initialization </h2>
<p>Next up we need to initialize the SDL system. This is accomplished via SDL_Init. Go ahead and add the following to your main function.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> SDL_Init<span style="color: #009900;">&#40;</span> SDL_INIT_VIDEO <span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Failed to initialize SDL. [%s]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> SDL_GetError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This will initialize SDL and the video subsystem. If it fails, it will print an error message to the standard error stream. Since we have a console app, this information will be printed directly to the console. Note the usage of SDL_GetError. This is a great tool to help determine why an SDL function call failed. It simply returns a pointer to a null-terminated string containing a description of the <b>last</b> failure.</p>
<p>The next thing to do is setup a video mode. This will give us a window and a drawing surface so we can actually render game content to the screen. Add the following code after the SDL_Init block.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">SDL_Surface <span style="color: #339933;">*</span>screen <span style="color: #339933;">=</span> SDL_SetVideoMode<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">640</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">480</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">32</span><span style="color: #339933;">,</span> SDL_SWSURFACE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> screen <span style="color: #339933;">==</span> NULL <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Failed to set video mode. [%s]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> SDL_GetError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This will get us a window and a surface to draw on that’s 640&#215;480 with 32 bits per pixel. The video surface will be created in system memory as opposed to say video memory thanks to the SDL_SWSURFACE flag. That&#8217;s ok for now. Also note that you could create the application in fullscreen mode by specifying SDL_FULLSCREEN for the last parameter.</p>
<p>Now we&#8217;re going to add the main game loop. Game loops can get pretty complicated but we will keep it simple here. For now we only need to check user input. So let&#8217;s add the following code now.</p>
<h2> Game Loop &#038; Event Processing </h2>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">SDL_Event evt<span style="color: #339933;">;</span>
bool done <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>done <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> SDL_PollEvent<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>evt <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span> evt.<span style="color: #202020;">type</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">case</span> SDL_QUIT<span style="color: #339933;">:</span>
		  done <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">case</span> SDL_KEYUP<span style="color: #339933;">:</span>
		  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> evt.<span style="color: #202020;">key</span>.<span style="color: #202020;">keysym</span>.<span style="color: #202020;">sym</span> <span style="color: #339933;">==</span> SDLK_ESCAPE <span style="color: #009900;">&#41;</span>
		       done <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
                  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ok, so what&#8217;s going on here is that for every trip through the game loop we are emptying the SDL event queue and processing the events. SDL stores all kinds of events for us such as key presses, mouse movement, and joystick input. In this case, we want to know if the user wants to quit the application (SDL_QUIT) or if the user presses the escape key (SDL_KEYUP). In both cases we simply set <i>done</i> to true to signal the main loop to bail. SDL_Event is a union with all kinds of interesting structures in it. You should have a look at it in the SDL documentation.</p>
<p>The last thing that needs to happen is to shutdown the application. We can accomplish that with a simple call to SDL_Quit.</p>
<p>So, without further ado, here is the complete skeletal SDL game code that simply creates a 640&#215;480 window and waits for the user to press escape or close the window.</p>
<h2> Full Code Listing</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;sdl.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	SDL_Event evt<span style="color: #339933;">;</span>
	bool done <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// initialize SDL</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> SDL_Init<span style="color: #009900;">&#40;</span> SDL_INIT_VIDEO <span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
            fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Failed to initialize SDL. [%s]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> SDL_GetError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// create a software surface/window</span>
	SDL_Surface <span style="color: #339933;">*</span>screen <span style="color: #339933;">=</span> SDL_SetVideoMode<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">640</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">480</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">32</span><span style="color: #339933;">,</span> SDL_SWSURFACE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> screen <span style="color: #339933;">==</span> NULL <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Failed to set video mode. [%s]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> SDL_GetError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// main game loop</span>
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>done <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// check for events generated from SDL</span>
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> SDL_PollEvent<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>evt <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span> evt.<span style="color: #202020;">type</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">case</span> SDL_QUIT<span style="color: #339933;">:</span>
				done <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #b1b100;">case</span> SDL_KEYUP<span style="color: #339933;">:</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> evt.<span style="color: #202020;">key</span>.<span style="color: #202020;">keysym</span>.<span style="color: #202020;">sym</span> <span style="color: #339933;">==</span> SDLK_ESCAPE <span style="color: #009900;">&#41;</span>
					done <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	SDL_Quit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Fun Stuff to Try</h2>
<p>So, here&#8217;s some fun stuff to try to understand a little more about making things happen in SDL.<br />
Try adding this snippet of code to see how to fill or clear the screen (SDL surface) with a different color. Before the main game loop add this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">Uint32 color1 <span style="color: #339933;">=</span> SDL_MapRGB<span style="color: #009900;">&#40;</span>screen<span style="color: #339933;">-&gt;</span>format<span style="color: #339933;">,</span> <span style="color: #208080;">0x00</span><span style="color: #339933;">,</span> <span style="color: #208080;">0x00</span><span style="color: #339933;">,</span> <span style="color: #208080;">0xFF</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Uint32 color2 <span style="color: #339933;">=</span> SDL_MapRGB<span style="color: #009900;">&#40;</span>screen<span style="color: #339933;">-&gt;</span>format<span style="color: #339933;">,</span> <span style="color: #208080;">0x00</span><span style="color: #339933;">,</span> <span style="color: #208080;">0xFF</span><span style="color: #339933;">,</span> <span style="color: #208080;">0x00</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Uint32 currentColor <span style="color: #339933;">=</span> color1<span style="color: #339933;">;</span></pre></div></div>

<p>And, your SDL_KEYUP processing code in your main loop should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">case</span> SDL_KEYUP<span style="color: #339933;">:</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> evt.<span style="color: #202020;">key</span>.<span style="color: #202020;">keysym</span>.<span style="color: #202020;">sym</span> <span style="color: #339933;">==</span> SDLK_ESCAPE <span style="color: #009900;">&#41;</span>
      done <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> evt.<span style="color: #202020;">key</span>.<span style="color: #202020;">keysym</span>.<span style="color: #202020;">sym</span> <span style="color: #339933;">==</span> SDLK_SPACE <span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      currentColor <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> currentColor <span style="color: #339933;">==</span> color1 <span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> color2 <span style="color: #339933;">:</span> color1<span style="color: #339933;">;</span>
      SDL_FillRect<span style="color: #009900;">&#40;</span> screen<span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span> currentColor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      SDL_UpdateRect<span style="color: #009900;">&#40;</span>screen<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, every time your press the space key the screen color will alternate between green and blue.</p>
<p>Another thing you can do is when processing the key stroke is display the character pressed in the console window. You can do that like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">case</span> SDL_KEYDOWN<span style="color: #339933;">:</span>
   <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;KEYDOWN: %c<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> evt.<span style="color: #202020;">key</span>.<span style="color: #202020;">keysym</span>.<span style="color: #202020;">unicode</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span></pre></div></div>

<p>But you have to remember to enable Unicode support within SDL. Do that with a call to <i>SDL_EnableUNICODE(1)</i> in the initialization code prior to the main game loop.</p>
<h2> What Next? </h2>
<p>Well that’s it for now. You now have a basic game setup using SDL. It also shows some basic ways to manipulate the screen and process SDL events. Next time I am going to start integrating OpenGL and get something fun on the screen. </p>
<p>If you have any questions or comments let me know. See ya next time!!!</p>
<fb:like href='http://www.crowsprogramming.com/archives/104' send='false' layout='standard' show_faces='true' width='450' height='65' action='like' colorscheme='light' font='lucida+grande'></fb:like>]]></content:encoded>
			<wfw:commentRss>http://www.crowsprogramming.com/archives/104/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

