<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">

<channel>
	<title>Minalien.com &#187; Development</title>
	<atom:link href="http://www.minalien.com/index.php/category/dev/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.minalien.com</link>
	<description>My brain makes things explode.</description>
	<lastBuildDate>Fri, 27 Aug 2010 20:45:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/us/</creativeCommons:license>		<item>
		<title>Work, Stress, and LCAR</title>
		<link>http://www.minalien.com/index.php/2010/08/27/work-stress-and-lcar/</link>
		<comments>http://www.minalien.com/index.php/2010/08/27/work-stress-and-lcar/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 20:45:58 +0000</pubDate>
		<dc:creator>Samantha</dc:creator>
				<category><![CDATA[Life]]></category>
		<category><![CDATA[Lights, Camera, Action/RPG!]]></category>
		<category><![CDATA[Thinking Out Loud]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[Casual Connect]]></category>
		<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[LCAR]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Seattle]]></category>
		<category><![CDATA[Torque]]></category>
		<category><![CDATA[Transgender]]></category>

		<guid isPermaLink="false">http://www.minalien.com/?p=334</guid>
		<description><![CDATA[Aaaand, not surprisingly, here I am again, after paid little to no attention to my blog in gods know how long. Work&#8217;s been long and tiring recently, and it&#8217;s showing no signs of letting up any time soon &#8211; I suspect it&#8217;ll be a good while before I&#8217;m able to get a good amount of]]></description>
			<content:encoded><![CDATA[<p>Aaaand, not surprisingly, here I am again, after paid little to no attention to my blog in gods know how long. Work&#8217;s been long and tiring recently, and it&#8217;s showing no signs of letting up any time soon &#8211; I suspect it&#8217;ll be a good while before I&#8217;m able to get a good amount of work on games done. It&#8217;s rather stressful, but at the same time, calming. Maybe it&#8217;s because I don&#8217;t have time to over-think life at the moment, but who knows.</p>
<p>Alright, so some nice things (and some not-so-nice things) have happened recently. Firstly, I&#8217;ve left KBFail for the umpteenth time, and I have no intention of going back. I&#8217;ve also switched (again) to using my iMac, and I&#8217;m hoping to stick with it this time around &#8211; it runs WoW decently enough, and I&#8217;ve found myself playing few games recently (Before it hits your mind, though I&#8217;m likely too late as it is, it is not because of WoW. I haven&#8217;t been playing that much, either), instead opting to do research, mentally prepare for my transition, trying to be a slightly more cheerful person, and working on development.</p>
<p>On the note of development, I&#8217;m still in a rut. I&#8217;ve come to the conclusion that I have to firmly put my mind to some tasks and force myself to push through and to apply myself to my goals &#8211; which is definitely an improvement. I&#8217;m starting to work out again, trying to get my body into better shape (again in preparation for my transition), eating healthier, and drinking less soda on the health side of things, while I&#8217;m trying to push through my distaste for my awful programmer art and trying to at least create semi-decent placeholders to allow myself to work on projects that I&#8217;ve been wanting to work on, such as LCAR.</p>
<p>And that brings me to the next topic: LCAR. The project is still very much in planning, and I&#8217;m considering taking it to another level. I&#8217;m currently considering the implications of investing in an iPad for development purposes &#8211; not only would it give me a better platform on which I could create programmer art (I may suck with a pen/paper, but I&#8217;m still better with it than with a mouse), but I have a new platform to target. I&#8217;m still working it out to try to determine whether or not I think it would be a worthwhile investment, and I&#8217;ll keep you posted on that. If I do get it, I intend to get the Torque engines targetting iOS (currently only iTGB, though iTGE is being worked on), and I intend to distribute LCAR on the iPad as well as my already-determined Mac and Windows releases. I think it&#8217;ll be pretty interesting, and we&#8217;ll have to see where it goes.</p>
<p>And for my last topic, I am going to be attending Casual Connect in Seattle, WA next year, mostly because I really want to meet the TorquePowered team. This is gonig to be my first time going out in public as.. well, me. I got some great encouragement from Mich Perry on the Torque team, and I&#8217;m confident that I, at the very least, am not going to have rotten fruit thrown at me for being what I am. At least, not from the Torque team &#8211; I can&#8217;t vouch for the rest of Seattle (:P). If anybody else is coming, or has suggestions for places to stay, I&#8217;m all ears. Looks like, on top of my development goals, I have a lot of work to do if I&#8217;m to be comfortable going out in public, and if I want to pass.</p>
<p>Wish me luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.minalien.com/index.php/2010/08/27/work-stress-and-lcar/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Finish Up Friday : LCAR Torque 3D</title>
		<link>http://www.minalien.com/index.php/2010/08/06/finish-up-friday-lcar-t3d/</link>
		<comments>http://www.minalien.com/index.php/2010/08/06/finish-up-friday-lcar-t3d/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 19:59:46 +0000</pubDate>
		<dc:creator>Samantha</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Lights, Camera, Action/RPG!]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.minalien.com/?p=329</guid>
		<description><![CDATA[I think I&#8217;m going to start participating in Finish-up Fridays, partly because I think it&#8217;ll be fun, and partly because I think having weekly goals will help make me a lot more productive when it comes to my development projects. And so with this first FuF, I&#8217;m going to work like hell to get some]]></description>
			<content:encoded><![CDATA[<p>I think I&#8217;m going to start participating in Finish-up Fridays, partly because I think it&#8217;ll be fun, and partly because I think having weekly goals will help make me a lot more productive when it comes to my development projects. And so with this first FuF, I&#8217;m going to work like hell to get some things accomplished with the Torque3D version of my <em>Lights, Camera, Action/RPG!</em> project.</p>
<p>My goals for tonight:<br />
1. Get T3D to build with PhysX in MSVC 2010<br />
2. Implement an SQLite3 Database system (based on the resources available on the TorquePowered website)<br />
3. Re-implement onMouseMove, onMouseDragged, and onRightMouseDragged within the second beta of Torque 3D 1.1<br />
4. Re-implement the isometric camera system for Torque 3D 1.1 Beta 2 (If you can&#8217;t tell, I haven&#8217;t moved anything between betas, and I plan on completely re-starting the project, as far as the Torque engine is concerned, because I was.. messy with B1)<br />
5. Begin implementing a basic Ranged combat system (in other words, I want to shoot things. n.n)</p>
<p>And I hope to do a lot of work with the following over the weekend and next week:<br />
1. Try to get, at the very least, the beginning pieces of a toon shader implemented<br />
2. Start learning to do basic 3D modelling/animaiton with 3D Studio Max and SoftImage, both with relation to the Torque engines and OGRE 3D.</p>
<p>Bye-ni~</p>
]]></content:encoded>
			<wfw:commentRss>http://www.minalien.com/index.php/2010/08/06/finish-up-friday-lcar-t3d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lights, Camera, Action/RPG! Initial Design Plans</title>
		<link>http://www.minalien.com/index.php/2010/07/29/lights-camera-actionrpg-initial-design-plans/</link>
		<comments>http://www.minalien.com/index.php/2010/07/29/lights-camera-actionrpg-initial-design-plans/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 16:13:34 +0000</pubDate>
		<dc:creator>Samantha</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Lights, Camera, Action/RPG!]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Thinking Out Loud]]></category>

		<guid isPermaLink="false">http://www.minalien.com/?p=322</guid>
		<description><![CDATA[This is going to be the first of several &#8220;Thinking Out Loud&#8221; entries. In these, I&#8217;m mostly going to just put my thoughts down in a retrievable area, in a place where people can read them. This first piece will be my initial design plans for my Lights, Camera, Action/RPG project (or rather, projects) &#8211;]]></description>
			<content:encoded><![CDATA[<p>This is going to be the first of several &#8220;Thinking Out Loud&#8221; entries. In these, I&#8217;m mostly going to just put my thoughts down in a retrievable area, in a place where people can read them.</p>
<p>This first piece will be my initial design plans for my <em>Lights, Camera, Action/RPG</em> project (or rather, projects) &#8211; discussing some thoughts, some initial ideas on gameplay and development, etc. Readers will notice that my development style is very iterative &#8211; I don&#8217;t like to plan things out too detailed ahead of time, because it screws me up in the end, when I find a more efficient way of implementing a system. I prefer to focus on certain portions at a time, building on and optimizing what was present before.</p>
<p>Here is the first part of my build process &#8211; determining my initial plans.</p>
<p>As far as the design of the game goes, I intend to make the prototype an isometric, third-person 3D Action/RPG, much in the style of Titan Quest, Diablo, Torchlight, and similar games. I plan to use a cel-shaded graphics style, and will be using bright, stylish colors for characters, items, monsters, and spells along with dark, murky environments and static geometry to give a slightly contrasted mood in the game.</p>
<p><strong>Character Classes</strong><br />
I&#8217;m planning on implementing three classes, each with a specific role in the game. These are <em>Wizard</em>, a ranged caster class that focuses on controlling/restricting enemy movement and area-of-effect damage spells, <em>Warrior</em>, a melee fighter that brings large blades to bear against hordes of enemies, and the <em>Scout</em>, whose focus on stealth and fast attacks allows him to quickly eliminate targets.</p>
<p><strong>Multiplayer</strong><br />
I am planning on implementing a cooperative, multiplayer game mode. It will be a party-based gameplay system, played with two to four players over either remote internet connections or a local LAN. The <em>Torque 3D</em> version of the prototype will utilise the internal networking library, while the in-house engine will likely use the <em>RakNet Multiplayer Game Network Engine</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.minalien.com/index.php/2010/07/29/lights-camera-actionrpg-initial-design-plans/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lights, Camera, Action/RPG!</title>
		<link>http://www.minalien.com/index.php/2010/07/28/lights-camera-actionrpg/</link>
		<comments>http://www.minalien.com/index.php/2010/07/28/lights-camera-actionrpg/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 16:56:29 +0000</pubDate>
		<dc:creator>Samantha</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Life]]></category>
		<category><![CDATA[Lights, Camera, Action/RPG!]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.minalien.com/?p=317</guid>
		<description><![CDATA[Well, I&#8217;m back. With the return of the Internet to my computer, it is time for me to return to my development. Unfortunately, I&#8217;ve been away from writing my tutorial series for too long, and I&#8217;m not going to be continuing it. I intend to release some one-shot tutorials for a while, and I&#8217;m working]]></description>
			<content:encoded><![CDATA[<p>Well, I&#8217;m back. With the return of the Internet to my computer, it is time for me to return to my development. Unfortunately, I&#8217;ve been away from writing my tutorial series for too long, and I&#8217;m not going to be continuing it. I intend to release some one-shot tutorials for a while, and I&#8217;m working on developing two 3D Point-and-Click Action/RPG prototypes &#8211; one with a custom C++ engine using OGRE 3D for rendering, the other using Torque 3D. Both prototypes are pretty much going to be the same thing &#8211; but each of them is intended for me learning/touching up on different systems.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.minalien.com/index.php/2010/07/28/lights-camera-actionrpg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ZuneHD &#8211; Virtual D-Pad Component for XNA 3.1</title>
		<link>http://www.minalien.com/index.php/2010/05/16/zunehd-virtual-dpad-component-for-xna/</link>
		<comments>http://www.minalien.com/index.php/2010/05/16/zunehd-virtual-dpad-component-for-xna/#comments</comments>
		<pubDate>Mon, 17 May 2010 04:11:39 +0000</pubDate>
		<dc:creator>Samantha</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[Zune]]></category>
		<category><![CDATA[Zune HD]]></category>

		<guid isPermaLink="false">http://www.minalien.com/?p=286</guid>
		<description><![CDATA[Just finished working on a pretty basic virtual d-pad for the Zune HD/XNA Framework 3.1. It&#8217;s really rough around the edges, and certainly isn&#8217;t optimized, but it works. Download Source DPadHD.dll (Managed Library) Description A simple, kind of messy virtual d-pad for your ZuneHD games. You can feel free to add it to your games,]]></description>
			<content:encoded><![CDATA[<div id="attachment_287" class="wp-caption alignright" style="width: 490px"><a href="http://www.minalien.com/index.php/2010/05/16/zunehd-virtual-dpad-component-for-xna/minalien-zune-2/" rel="attachment wp-att-287"><img src="http://www.minalien.com/wp-content/uploads/2010/05/Minalien-Zune-2.png" alt="" title="Zune HD Virtual D-Pad" width="480" height="272" class="size-full wp-image-287" /></a><p class="wp-caption-text">Sample use of the Virtual D-Pad Component</p></div>
<p>Just finished working on a pretty basic virtual d-pad for the Zune HD/XNA Framework 3.1. It&#8217;s really rough around the edges, and certainly isn&#8217;t optimized, but it works.</p>
<p><strong>Download</strong><br />
<a href="http://files.minalien.com/DPadHD-Src.7z">Source</a><br />
<a href="http://files.minalien.com/DPadHD-Bin.7z">DPadHD.dll</a> (Managed Library)</p>
<p><strong>Description</strong><br />
A simple, kind of messy virtual d-pad for your ZuneHD games. You can feel free to add it to your games, with or without crediting me, just don&#8217;t distribute the D-Pad or modifications of it without at least giving word that it came from me. It&#8217;ll need a fair bit of optimization and editing before it&#8217;s ready for any game that you wish to distribute, however.</p>
<p><strong>Features</strong><br />
Position can be configured<br />
Works similarly to the default XNA input systems<br />
Built as an XNA DrawableGameComponent</p>
<p><strong>Usage Sample</strong></p>
<pre class="brush: csharp;">			dPadManager = new Segfault.DPadHD.DPadManager(this);
			dPadManager.Landscape = true;
			this.Components.Add(dPadManager);</pre>
<p>Creating the system is easy. First, you create it as you would any other component. You can set whether or not you want to use it in Landscape mode (which merely rotates the inputs counter-clockwise &#8211; I assume a &#8220;back-button-to-the-right&#8221; landscape view) by setting a simple boolean value, and then you add the D-Pad Manager to the components list. It will update automatically, just as the XNA input managers do by default.</p>
<p>Its position can be set on screen with the following code:</p>
<pre class="brush: csharp;">dPadManager.Position = new Vector2(50.0f, 12.0f);</pre>
<p>And finally, the state of the D-Pad&#8217;s four cardinal direction buttons can be checked by retrieving the current game state:</p>
<pre class="brush: csharp;">if (this.dPadManager.GetState().IsButtonDown(Segfault.DPadHD.DPadButton.Up))
				pos.X += (gameTime.ElapsedGameTime.Milliseconds);

			if (this.dPadManager.GetState().IsButtonDown(Segfault.DPadHD.DPadButton.Down))
				pos.X -= (gameTime.ElapsedGameTime.Milliseconds);

			if(this.dPadManager.GetState().IsButtonDown(Segfault.DPadHD.DPadButton.Left))
				pos.Y -= (gameTime.ElapsedGameTime.Milliseconds);

			if (this.dPadManager.GetState().IsButtonDown(Segfault.DPadHD.DPadButton.Right))
				pos.Y += (gameTime.ElapsedGameTime.Milliseconds);</pre>
<p>Enjoy. :D</p>
]]></content:encoded>
			<wfw:commentRss>http://www.minalien.com/index.php/2010/05/16/zunehd-virtual-dpad-component-for-xna/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++/D3D Pong Tutorial 04: Adding Keyboard Input</title>
		<link>http://www.minalien.com/index.php/2010/04/25/cd3d-pong-tutorial-04-adding-keyboard-input/</link>
		<comments>http://www.minalien.com/index.php/2010/04/25/cd3d-pong-tutorial-04-adding-keyboard-input/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 01:09:55 +0000</pubDate>
		<dc:creator>Samantha</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Computers]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Pong (C++/DirectX)]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Series]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[2D]]></category>
		<category><![CDATA[D3DPong]]></category>
		<category><![CDATA[Direct3D]]></category>
		<category><![CDATA[DirectX]]></category>
		<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.minalien.com/?p=240</guid>
		<description><![CDATA[Tutorial Description In the last tutorial, we established a rendering engine and got scene objects working, with basic physics. In this tutorial, we are going to implement the initial Input manager, which will be focusing on the keyboard. I was planning to include mouse input, but it turns out that DirectInput only handles the mouse]]></description>
			<content:encoded><![CDATA[<p><strong>Tutorial Description</strong><br />
In the last tutorial, we established a rendering engine and got scene objects working, with basic physics. In this tutorial, we are going to implement the initial Input manager, which will be focusing on the keyboard. I was planning to include mouse input, but it turns out that DirectInput only handles the mouse in relative positioning &#8211; which is perfect for three-dimensional first-person shooter games that need to track the movement of the mouse &#8211; but it&#8217;s poor for two-dimensional casual games. For those of you that do wish to use mouse input, I will be covering it a later tutorial &#8211; the methods of getting absolute positioning will require a bit of modification to our engine.</p>
<p><strong>Downloads</strong><br />
<em>Please note that I&#8217;ve switched to using 7-zip in place of WinRAR for compression</em><br />
<a href="http://files.minalien.com/D3DPong/04/D3DPong-Bin.7z">Binary Only (20.4KB)</a><br />
<a href="http://files.minalien.com/D3DPong/04/D3DPong-Src.7z">Full VS2010 Project Dir (26.8KB)</a></p>
<p><strong>SVN Repository</strong><br />
As of this tutorial, I have created and added a Subversion repository on Assembla to store the code for the tutorial series. I will continue to upload the source for each tutorial &#8211; the SVN will merely contain the code as I write it, so you can feel free to play around with the code that I&#8217;m working on before I release a tutorial. Mostly, this is to help people play around with things if there&#8217;s a point where I&#8217;m slow to create the tutorial following the code (as was the case with this tutorial).</p>
<p>SVN URL: <a href="http://svn2.assembla.com/svn/d3dpong" title="D3DPong SVN Repository">http://svn2.assembla.com/svn/d3dpong</a></p>
<p><strong>Starting Words</strong><br />
Notice: This tutorial continues from where “<a href="http://www.minalien.com/index.php/2010/03/21/cd3d-pong-tutorial-03-scene-objects-and-sprite-rendering/" title="C++/D3D Pong Tutorial 03: Scene Objects and Sprite Rendering">C++/D3D Pong Tutorial 03</a>” article left off. Please read it if you have not already.</p>
<p><span id="more-240"></span></p>
<p><strong>Libraries</strong><br />
You will need to add a single library to the project to build with the code in this tutorial:<br />
dinput.lib</p>
<p><strong>Code Listing 1: CInputManager.h</strong></p>
<pre class="brush: cpp;">#pragma once

// Global Header Files
#include &lt;Windows.h&gt;
#include &lt;d3d9.h&gt;
#include &lt;d3dx9.h&gt;
#include &lt;dinput.h&gt;

// Local Header Files
#include &quot;CGameEngine.h&quot;
#include &quot;CKeyboardState.h&quot;

namespace D3DPong
{
	class CInputManager
	{
	private:
		/** Constructor &amp; Destructor **/
		CInputManager(void);						// ctor
		CInputManager(CInputManager const&amp;) { }		// copy ctor
		~CInputManager(void);						// dtor

		static CInputManager* _instance;

		// DirectInput Devices
		LPDIRECTINPUT8			_device;	// Core DirectInput Object
		LPDIRECTINPUTDEVICE8	_keyboard;	// Keyboard

		// Keyboard-related Variables
		CKeyboardState* _preKeyboardState;
		CKeyboardState* _curKeyboardState;

		// Device Initialization
		bool initKeyboard(void);

	public:
		/** Singleton **/
		static CInputManager* Instance(void) { return CInputManager::_instance; }

		/** Public Functions **/
		bool Init(void);
		void Update(void);

		/** Keyboard Functions **/
		bool IsKeyDown(int);
		bool IsKeyUp(int);
		bool WasKeyPressed(int);
		bool WasKeyReleased(int);

		/** Accessors &amp; Mutators **/
		LPDIRECTINPUT8 GetDevice(void) { return this-&gt;_device; }
		CKeyboardState* GetCurKeyboardState(void) { return this-&gt;_curKeyboardState; }
		CKeyboardState* GetPreKeyboardState(void) { return this-&gt;_preKeyboardState; }
	};
};</pre>
<p>Here is the framework for our Input Manager. Again, we&#8217;re back down to lower source sizes &#8211; because we&#8217;re not trying to implement the world here. As we did with our Game Engine class, we&#8217;ve turned our Input Manager into a singleton &#8211; we only want one of them to be created throughout the application.</p>
<pre class="brush: cpp;">		// DirectInput Devices
		LPDIRECTINPUT8			_device;	// Core DirectInput Object
		LPDIRECTINPUTDEVICE8	_keyboard;	// Keyboard

		// Keyboard-related Variables
		CKeyboardState* _preKeyboardState;
		CKeyboardState* _curKeyboardState;

		// Device Initialization
		bool initKeyboard(void);</pre>
<p>The first two variables are input devices &#8211; the first, as with our Direct3D object, we have an object that we will use to access the DirectInput system. The second is the actual object that we will use to access the keyboard device &#8211; when adding a Mouse or a Gamepad/Joystick, you will add another LPDIRECTINPUTDEVICE8. If you&#8217;ve noticed the 8, you&#8217;ll notice that this is something from DirectX 8 &#8211; DirectInput hasn&#8217;t changed since then. In all honesty, the use of DirectInput isn&#8217;t recommended by Microsoft, but it will suffice for our purposes. We then add two variables to store the keyboard state &#8211; the current keyboard state, which will contain the state as of the current frame, and the previous, which will (if you haven&#8217;t guessed) store the state of the last frame. We do this so that we can not only check whether a key is <em>currently</em> being pressed, but also whether it was pressed or released between the current and last frame. This makes things easier on us, in the long run, because we can wait for (for example) the escape key to be released before we end the game, and it will make managing our game menus much easier when we want to handle selection of menu options game console style. Finally, we define a private function that will initialize our keyboard. It will help keep our Init() function tidy for the future addition of controllers, mice, and other input devices.</p>
<pre class="brush: cpp;">		/** Public Functions **/
		bool Init(void);
		void Update(void);</pre>
<p>These two functions are pretty obvious in what they do. Init() will initialize all of our input devices, and update will update the current/previous states of our devices. I&#8217;ll explain them more when we actually flesh them out.</p>
<pre class="brush: cpp;">		/** Keyboard Functions **/
		bool IsKeyDown(int);
		bool IsKeyUp(int);
		bool WasKeyPressed(int);
		bool WasKeyReleased(int);</pre>
<p>Last on the list of things to look at is our key-checking functions. The first two will return whether the key is currently pressed or released, which can be useful for continuous motion checking, such as what we will be doing with our pong paddles. The latter two check if they key&#8217;s state has changed between this frame and the last (if the key has been pressed or released).</p>
<p><strong>Code Listing 2: CInputManager.cpp</strong></p>
<pre class="brush: cpp;">#include &quot;CInputManager.h&quot;
#include &quot;CKeyboardState.h&quot;

namespace D3DPong
{
	CInputManager* CInputManager::_instance = new CInputManager();

	CInputManager::CInputManager(void)
		: _device(NULL), _keyboard(NULL),
		_curKeyboardState(NULL), _preKeyboardState(NULL)
	{
	}

	CInputManager::~CInputManager(void)
	{
		if(this-&gt;_keyboard != NULL)
		{
			this-&gt;_keyboard-&gt;Unacquire();
			this-&gt;_keyboard-&gt;Release();
			this-&gt;_keyboard = NULL;
		}

		if(this-&gt;_device != NULL)
		{
			this-&gt;_device-&gt;Release();
			this-&gt;_device = NULL;
		}
	}

	bool CInputManager::Init(void)
	{
		// Initialize the DirectInput Device
		if(FAILED(DirectInput8Create(GetModuleHandle(NULL),	// Instance Handle
			DIRECTINPUT_VERSION,							// DirectInput Version
			IID_IDirectInput8, 								// Device Type
			(LPVOID*)&amp;this-&gt;_device,						// Pointer to device storage
			NULL)))
		{
			MessageBox(NULL, L&quot;Error initializing Input Manager&quot;, L&quot;Error&quot;, MB_OK);
			return false;
		}

		// Initialize the Keyboard
		if(!this-&gt;initKeyboard())
			return false;

		// Call update once to populate the input states
		this-&gt;Update();

		return true;
	}

	bool CInputManager::initKeyboard(void)
	{
		if(FAILED(this-&gt;_device-&gt;CreateDevice(GUID_SysKeyboard,
			&amp;this-&gt;_keyboard,
			NULL)))
		{
			MessageBox(NULL, L&quot;Error initializing Keyboard&quot;, L&quot;Error&quot;, MB_OK);
			return false;
		}

		if(FAILED(this-&gt;_keyboard-&gt;SetDataFormat(&amp;c_dfDIKeyboard)))
		{
			MessageBox(NULL, L&quot;Error setting keyboard data format&quot;, L&quot;Error&quot;, MB_OK);
			return false;
		}

		if(FAILED(this-&gt;_keyboard-&gt;SetCooperativeLevel(CGameEngine::Instance()-&gt;GetWindowHandle(), DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
		{
			MessageBox(NULL, L&quot;Error setting keyboard cooperative level&quot;, L&quot;Error&quot;, MB_OK);
			return false;
		}

		if(FAILED(this-&gt;_keyboard-&gt;Acquire()))
		{
			MessageBox(NULL, L&quot;Error acquiring keyboard device&quot;, L&quot;Error&quot;, MB_OK);
			return false;
		}

		// Create the keyboard states
		this-&gt;_curKeyboardState = new CKeyboardState();
		this-&gt;_preKeyboardState = new CKeyboardState();

		return true;
	}

	void CInputManager::Update(void)
	{
		// Update the keyboard
		this-&gt;_preKeyboardState-&gt;CopyKeyBuffer(this-&gt;_curKeyboardState-&gt;GetKeyStates());

		// Make sure we reacquire the keyboard if the device was lost
		if(this-&gt;_keyboard-&gt;GetDeviceState(this-&gt;_curKeyboardState-&gt;GetBufferSize(), this-&gt;_curKeyboardState-&gt;GetKeyBuffer()) == DIERR_INPUTLOST)
			this-&gt;_keyboard-&gt;Acquire();
	}

	bool CInputManager::IsKeyDown(int keyCode)
	{
		return this-&gt;_curKeyboardState-&gt;IsKeyDown(keyCode);
	}

	bool CInputManager::IsKeyUp(int keyCode)
	{
		return this-&gt;_curKeyboardState-&gt;IsKeyUp(keyCode);
	}

	bool CInputManager::WasKeyPressed(int keyCode)
	{
		if(this-&gt;_curKeyboardState-&gt;IsKeyDown(keyCode) &amp;&amp; this-&gt;_preKeyboardState-&gt;IsKeyUp(keyCode))
			return true;
		else
			return false;
	}

	bool CInputManager::WasKeyReleased(int keyCode)
	{
		if(this-&gt;_curKeyboardState-&gt;IsKeyUp(keyCode) &amp;&amp; this-&gt;_preKeyboardState-&gt;IsKeyDown(keyCode))
			return true;
		else
			return false;
	}
};</pre>
<p>Here are the guts of our system. Not everything will need covered, because it will be pretty obvious based on other parts, but here goes:</p>
<pre class="brush: cpp;">	bool CInputManager::Init(void)
	{
		// Initialize the DirectInput Device
		if(FAILED(DirectInput8Create(GetModuleHandle(NULL),	// Instance Handle
			DIRECTINPUT_VERSION,							// DirectInput Version
			IID_IDirectInput8, 								// Device Type
			(LPVOID*)&amp;this-&gt;_device,						// Pointer to device storage
			NULL)))
		{
			MessageBox(NULL, L&quot;Error initializing Input Manager&quot;, L&quot;Error&quot;, MB_OK);
			return false;
		}

		// Initialize the Keyboard
		if(!this-&gt;initKeyboard())
			return false;

		// Call update once to populate the input states
		this-&gt;Update();

		return true;
	}</pre>
<p>This method initializes our input management system. The first thing that we must do is initialize our DirectInput device, because it will be used to create any DirectInput-based input systems later (keyboard, mouse, and joypad, for example). We then call initKeyboard() to initialize our keyboard (when adding a Mouse or Joystick, you would also add an initMouse/initJoystick function, just to keep things clean), make sure it worked properly, then capture the current state of the input system.</p>
<pre class="brush: cpp;">	bool CInputManager::initKeyboard(void)
	{
		if(FAILED(this-&gt;_device-&gt;CreateDevice(GUID_SysKeyboard,
			&amp;this-&gt;_keyboard,
			NULL)))
		{
			MessageBox(NULL, L&quot;Error initializing Keyboard&quot;, L&quot;Error&quot;, MB_OK);
			return false;
		}

		if(FAILED(this-&gt;_keyboard-&gt;SetDataFormat(&amp;c_dfDIKeyboard)))
		{
			MessageBox(NULL, L&quot;Error setting keyboard data format&quot;, L&quot;Error&quot;, MB_OK);
			return false;
		}

		if(FAILED(this-&gt;_keyboard-&gt;SetCooperativeLevel(CGameEngine::Instance()-&gt;GetWindowHandle(), DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
		{
			MessageBox(NULL, L&quot;Error setting keyboard cooperative level&quot;, L&quot;Error&quot;, MB_OK);
			return false;
		}

		if(FAILED(this-&gt;_keyboard-&gt;Acquire()))
		{
			MessageBox(NULL, L&quot;Error acquiring keyboard device&quot;, L&quot;Error&quot;, MB_OK);
			return false;
		}

		// Create the keyboard states
		this-&gt;_curKeyboardState = new CKeyboardState();
		this-&gt;_preKeyboardState = new CKeyboardState();

		return true;
	}</pre>
<p>Not much to see here, in spite of the length &#8211; most of it&#8217;s error checking. First, we use the DirectInput device to create our Keyboard device, then we follow up by setting the data format and cooperative level. The cooperative level sets how our application works with the operating system and other applications running, when it comes to handling input on the device. Setting a Foreground cooperative level makes it so that our game only responds to input when the game window is our active window. This way, if the player were to open Notepad and press the escape key, it wouldn&#8217;t close our game. Setting this to background, on the other hand, allows this to happen. The second piece of our cooperative level determines whether other applications will be allowed to access the input device when the game is using them. Nonexclusive means that the input device can be shared with others, and that works best to keep our application on friendly terms with any software our players may be using. The only time you really want to use exclusive mode is when using a mouse in a three-dimensional shooter game, to prevent our mouse from slipping out the side of the window when we&#8217;re turning. Finally, we capture (acquire) our input device, and create two empty instances to store the state of the keyboard.</p>
<pre class="brush: cpp;">	void CInputManager::Update(void)
	{
		// Update the keyboard
		this-&gt;_preKeyboardState-&gt;CopyKeyBuffer(this-&gt;_curKeyboardState-&gt;GetKeyStates());

		// Make sure we reacquire the keyboard if the device was lost
		if(this-&gt;_keyboard-&gt;GetDeviceState(this-&gt;_curKeyboardState-&gt;GetBufferSize(), this-&gt;_curKeyboardState-&gt;GetKeyBuffer()) == DIERR_INPUTLOST)
			this-&gt;_keyboard-&gt;Acquire();
	}</pre>
<p>The update method updates our keyboard states. First, we use the CopyKeyBuffer (which, as you will see when we design our Keyboard State class, simply copies the key buffer array from another array). We then check to make sure that the device was not lost (for example, if the window is switched). If it is, we re-acquire it. When we use the GetDeviceState() function within the if() statement, we are also capturing the keyboard state &#8211; first, we pass the size of the buffer, then we pass a pointer to the buffer array. GetDeviceState will copy the state of our keyboard to the key buffer array.</p>
<p>Because the IsKeyUp and IsKeyDown functions directly map to functions of the same name on the current keyboard state, we&#8217;ve only one last thing to cover:</p>
<pre class="brush: cpp;">	bool CInputManager::WasKeyPressed(int keyCode)
	{
		if(this-&gt;_curKeyboardState-&gt;IsKeyDown(keyCode) &amp;&amp; this-&gt;_preKeyboardState-&gt;IsKeyUp(keyCode))
			return true;
		else
			return false;
	}</pre>
<p>This is pretty obvious, but I want to be completely clear on how we&#8217;re working this. If the state has been changed between the current frame and the last, we return true. In the case above, we are checking if the key has been pressed. We check the current state of the key against the previous state &#8211; if it was released (up) on the last frame, but is pressed (down) now, we return true &#8211; the key has been pressed between the current and last frames. Otherwise, it hasn&#8217;t, so we return false.</p>
<p><strong>Code Listing 3: CKeyboardState.h</strong></p>
<pre class="brush: cpp;">#pragma once

#include &lt;Windows.h&gt;

namespace D3DPong
{
	class CKeyboardState
	{
	private:
		char _keyStates[256];

	public:
		/** Constructor &amp; Destructor **/
		CKeyboardState(void);
		~CKeyboardState(void);

		/** Public Methods **/
		bool IsKeyDown(int);
		bool IsKeyUp(int);
		void CopyKeyBuffer(char*);

		/** Accessors &amp; Mutators **/
		char* GetKeyStates(void) { return this-&gt;_keyStates; }
		LPVOID GetKeyBuffer(void) { return (LPVOID)&amp;this-&gt;_keyStates; }
		DWORD GetBufferSize(void) { return sizeof(this-&gt;_keyStates); }
	};
};</pre>
<p>The only member variable here is our key buffer &#8211; which is a simple char array, exactly what will be set by the keyboard device&#8217;s GetDeviceState function. I&#8217;ll cover the functions below:</p>
<p><strong>Code Listing 4: CKeyboardState.cpp</strong></p>
<pre class="brush: cpp;">#include &quot;CKeyboardState.h&quot;

namespace D3DPong
{
	CKeyboardState::CKeyboardState(void)
	{
	}

	CKeyboardState::~CKeyboardState(void)
	{
	}

	bool CKeyboardState::IsKeyDown(int keyCode)
	{
		if(this-&gt;_keyStates[keyCode] &amp; 0x80)
			return true;
		else
			return false;
	}

	bool CKeyboardState::IsKeyUp(int keyCode)
	{
		return !this-&gt;IsKeyDown(keyCode);
	}

	void CKeyboardState::CopyKeyBuffer(char* buffer)
	{
		for(int i = 0; i &lt; 256; i++)
			this-&gt;_keyStates[i] = buffer[i];
	}
};</pre>
<p>Here we go!</p>
<pre class="brush: cpp;">	bool CKeyboardState::IsKeyDown(int keyCode)
	{
		if(this-&gt;_keyStates[keyCode] &amp; 0x80)
			return true;
		else
			return false;
	}</pre>
<p>To check whether or not a key is up, we simply do a BITWISE AND operation against the key state (stored in our buffer) at the keycode in our array against 0&#215;80. The keyCode integer will be a pre-defined DirectInput keycode, such as DIK_ESCAPE or DIK_HOME &#8211; you can find a list of DirectInput keycodes <a href="http://www.gamespp.com/directx/directInputKeyboardScanCodes.html" title="DirectX / DirectInput Keyboard Scan Codes - Games++:">here</a>. To find out if the key is up, we need only check if it is not down &#8211; so our IsKeyUp merely returns !IsKeyDown.</p>
<pre class="brush: cpp;">	void CKeyboardState::CopyKeyBuffer(char* buffer)
	{
		for(int i = 0; i &lt; 256; i++)
			this-&gt;_keyStates[i] = buffer[i];
	}</pre>
<p>Here, we actually copy the key buffer. There are only 256 indexes in the key buffer array, so that&#8217;s all we need to copy. In case you&#8217;re surprised, yes, arrays are simply glorified pointers with fixed sizes &#8211; and you can easily use an array as a pointer.</p>
<p><strong>Wrapping it into our engine</strong><br />
Now, we need to make some ties to bring our singleton into the application. Open up WinMain.cpp, and let&#8217;s get started.</p>
<pre class="brush: cpp;">#define InputManager D3DPong::CInputManager::Instance()</pre>
<p>Just as we did with the instance of the game engine, we are going to create a Macro to access our Input Manager singleton. Now, we can just use InputManager->WasKeyPressed instead of going out of our way to type D3DPong::CInputManager::Instance()->WasKeyPressed.</p>
<pre class="brush: cpp;">	// Initialize the Game Engine
	GEInstance-&gt;SetWindowHandle(hWnd);
	if(!GEInstance-&gt;Init(GEInstance-&gt;GetScreenWidth(), GEInstance-&gt;GetScreenHeight(), GEInstance-&gt;GetColorDepth()))
		return 0;

	if(!InputManager-&gt;Init())
		return 0;

	// Message Loop
	while(GEInstance-&gt;GetGameRunning())
	{
		while(PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&amp;msg);
			DispatchMessage(&amp;msg);
		}

		InputManager-&gt;Update();
		GEInstance-&gt;Update();
	}</pre>
<p>The only changes here are the lines regarding the InputManager &#8211; these parts are all we need to tie the input manager into our engine. They&#8217;re quite obvious in what they do &#8211; initialize and update the input manager &#8211; so I doubt that I need to give much explanation.</p>
<p><strong>Adding input to our test game</strong><br />
I&#8217;m only going to cover checking for the escape key on this, but if you look at the tutorial files, I&#8217;ve added some simple platformer-esque input handling to our earlier rendering and physics tests.</p>
<pre class="brush: cpp;">void GameUpdate(float delta)
{
	if(InputManager-&gt;WasKeyReleased(DIK_ESCAPE))
		GEInstance-&gt;SetGameRunning(false);

	// [...] The rest of our update method
}</pre>
<p>Here, we check if the Escape key has been released &#8211; this will allow us to easily exit our window via a single keystroke. I choose to check for the release of the escape key out of personal preference, because I prefer acting on the release of most keystrokes, but you could achieve similar results by doing WasKeypressed &#8211; it would merely act when the key was pressed, instead of when it was released (&lt;/obvious&gt;).</p>
<p>And there you have it, the (long awaited?) keyboard input tutorial. If I missed something between the downloadable source and what I&#8217;ve posted, please let me know so I can fix it &#8211; I wrote the code for this ages ago, and only finally got around to writing the tutorial portion recently.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.minalien.com/index.php/2010/04/25/cd3d-pong-tutorial-04-adding-keyboard-input/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Qt4</title>
		<link>http://www.minalien.com/index.php/2010/03/29/qt4/</link>
		<comments>http://www.minalien.com/index.php/2010/03/29/qt4/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 22:37:50 +0000</pubDate>
		<dc:creator>Samantha</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Life]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Qt4]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[WebKit]]></category>
		<category><![CDATA[alienIRC]]></category>
		<category><![CDATA[application development]]></category>
		<category><![CDATA[application programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[IRC]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[SDK]]></category>
		<category><![CDATA[Win]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.minalien.com/?p=236</guid>
		<description><![CDATA[As of last night, I started looking into using Qt4 for future Application development projects of mine &#8211; and damned if it doesn&#8217;t look like the best option available to me (the best alternative being writing the UI on each different platform individually). For those who don&#8217;t know, Qt4 is an immensely popular and very]]></description>
			<content:encoded><![CDATA[<p>As of last night, I started looking into using Qt4 for future Application development projects of mine &#8211; and damned if it doesn&#8217;t look like the best option available to me (the best alternative being writing the UI on each different platform individually). For those who don&#8217;t know, <a href="http://qt.nokia.com/" title="Nokia's Qt4 Website">Qt4</a> is an immensely popular and very powerful cross-platform application library that works with C++, and is supported on a large number of machines &#8211; from 64-bit Windows desktops to 32-bit embedded Linux solutions to the wonder that is Mac OS X. All without having to change the code &#8211; all that is required is a recompile (assuming you&#8217;ve set things up properly, of course). It even supports cross-platform project (.pro) files, and has a cross-platform toolset that accompanies its SDK &#8211; including a GUI designer, a set of debugging tools, and even an IDE that ties with a MinGW compiler.</p>
<p>I&#8217;ve started working again on an IRC client &#8211; now under the name alienIRC (formerly SevenIRC, which was being planned solely for Windows). I&#8217;ve also decided to make it completely open-source, and intend to license it under LGPLv3, something it shares with the Qt framework that is going to power it. I&#8217;m going to be setting up a Git repository on <a href="http://www.gitorious.org/" title="Gitorious">Gitorious</a> for the project, and will be setting up some development pages on this blog &#8211; including issue tracking, a wiki, and some other tools. Right now, my plan is to closely emulate the mIRC interface &#8211; mostly because mIRC is my favorite IRC client &#8211; and will be working to make sure it feels native on all supported platforms. I will personally be maintaining builds on Windows (32- and 64-bit), OpenSUSE 11, and Mac OS X Snow Leopard. I will primarily be using the IDE that comes with Qt, though I will work to ensure that it compiles in X-Code 3 and Visual Studio 2008/2010.</p>
<p>As far as alienIRC itself goes, I&#8217;m looking at the possibility of using WebKit for the chat windows&#8217; messages. This would make it incredibly easy to add formatting, because I could easily change messages from the mIRC color character into HTML tags before adding the text to the message box. It all depends on how it runs compared to a custom text box. I also plan to implement a number of features that are popular with mIRC, such as scripting (Lua, specifically), &#8220;address books,&#8221; nick highlighting, file sharing, et cetera. I&#8217;m currently working on some UI concept work &#8211; since it will have to be different from SevenIRC&#8217;s WPF plans and the previous OS X-only alienIRC UI concept that relied on some UI elements only found on Mac (such as Drawers).</p>
<p>If anybody is interested in contributing to this project, send me some messages &#8211; I&#8217;d be happy to hear your input, especially on features that you feel would be good to implement. Make sure you sign up for an account on Gitorious &#8211; and that you upload your SSH keys and such. If you&#8217;ve never used Git, and you&#8217;re a Windows developer, this is a very useful link for seting it up on Windows: <a href="http://www.lostechies.com/blogs/jason_meridth/archive/2009/06/01/git-for-windows-developers-git-series-part-1.aspx" title="Git for Windows Developers">Git for Windows Developers: Part 1</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.minalien.com/index.php/2010/03/29/qt4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++/D3D Pong Maintenance: The Game Loop/Frame Delta</title>
		<link>http://www.minalien.com/index.php/2010/03/25/cd3d-pong-maintenance-game-loop/</link>
		<comments>http://www.minalien.com/index.php/2010/03/25/cd3d-pong-maintenance-game-loop/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 01:23:52 +0000</pubDate>
		<dc:creator>Samantha</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Pong (C++/DirectX)]]></category>
		<category><![CDATA[Series]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[2D]]></category>
		<category><![CDATA[Direct3D]]></category>
		<category><![CDATA[DirectX]]></category>
		<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://www.minalien.com/?p=220</guid>
		<description><![CDATA[Tutorial Description While running the game/engine that we&#8217;ve been developing throughout this tutorial series, you may have noticed that the engine kind of runs&#8230; like crap. So, before moving onto the next tutorial in the series (the code for which is completed), which will cover keyboard and mouse input, I feel that it&#8217;s somewhat important]]></description>
			<content:encoded><![CDATA[<p><strong>Tutorial Description</strong><br />
While running the game/engine that we&#8217;ve been developing throughout this tutorial series, you may have noticed that the engine kind of runs&#8230; like crap. So, before moving onto the next tutorial in the series (the code for which is completed), which will cover keyboard and mouse input, I feel that it&#8217;s somewhat important to fix this. It&#8217;s just going to be some small updates to CGameEngine.cpp, so it won&#8217;t be hard.</p>
<p><strong>Code Listing 1: CGameEngine.cpp -> <em>CGameEngine::CGameEengine(void)</em></strong><br />
First, we&#8217;re going to correct an error with our game timer, which we never actually <em>created</em> an instance of in code &#8211; causing the GetRunTime feature to fail due to a member variable not being initialized (CGameTimer::_start). Replace the constructor for CGameEngine with the following, in CGameEngine.cpp</p>
<pre class="brush: cpp;">	CGameEngine::CGameEngine(void)
		: _windowHandle(NULL), _windowTitle(L&quot;D3DPong&quot;),
		_screenWidth(640), _screenHeight(480), _colorDepth(32),
		_gameRunning(true), _gameTimer(new CGameTimer())
	{
	}</pre>
<p><strong>Code Listing 2: CGameEngine.cpp -> <em>CGameEngine::Update(void)</em></strong><br />
Our new Update method is much smaller than it was before. We&#8217;re still using fast-as-possible rendering and updating, so this isn&#8217;t going to fix the minor graphics tearing that you&#8217;ve no doubt noticed before this, but it is going to make things run a little better.</p>
<pre class="brush: cpp;">	void CGameEngine::Update(void)
	{
		float delta = this-&gt;_gameTimer-&gt;GetRunTime();

		GameUpdate(delta);

		this-&gt;_gameTimer-&gt;Reset();

		this-&gt;BeginRender();

		GameRender(delta);

		this-&gt;EndRender();
	}</pre>
<p>Nicer, cleaner, shorter. This was caused by a bit of an inconsistency and laziness on my part, but things should run a bit smoother now. In the next tutorial, we&#8217;re going to be adding input, and will put the beginning pieces of our game into effect. I&#8217;ll cover enabling VSync at a later point, because I&#8217;m too lazy to deal with it right now. It&#8217;ll probably be in another &#8220;Maintenance&#8221; mini-tutorial.</p>
<p>Expect Tutorial 4 soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.minalien.com/index.php/2010/03/25/cd3d-pong-maintenance-game-loop/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C++/D3D Pong Tutorial 03: Scene Objects and Sprite Rendering</title>
		<link>http://www.minalien.com/index.php/2010/03/21/cd3d-pong-tutorial-03-scene-objects-and-sprite-rendering/</link>
		<comments>http://www.minalien.com/index.php/2010/03/21/cd3d-pong-tutorial-03-scene-objects-and-sprite-rendering/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 20:28:21 +0000</pubDate>
		<dc:creator>Samantha</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Pong (C++/DirectX)]]></category>
		<category><![CDATA[Series]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[2D]]></category>
		<category><![CDATA[Direct3D]]></category>
		<category><![CDATA[DirectX]]></category>
		<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Gaming]]></category>
		<category><![CDATA[Pong]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.minalien.com/?p=197</guid>
		<description><![CDATA[Tutorial Description In the last tutorial, we finished getting Direct3D set up, implemented a timer system to allow us to calculate the time between each frame, and set up the framework that will grant us access to our game without interfering with the core engine. In this tutorial, we are going to implement a reusable]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.minalien.com/index.php/2010/03/21/cd3d-pong-tutorial-03-scene-objects-and-sprite-rendering/d3dpong3/" rel="attachment wp-att-202"><img src="http://www.minalien.com/wp-content/uploads/2010/03/D3DPong3-300x225.png" alt="Where&#039;s the fluffy tail? D:" title="D3DPong Tutorial 3" width="300" height="225" class="alignright size-medium wp-image-202" /></a></p>
<p><strong>Tutorial Description</strong><br />
In the last tutorial, we finished getting Direct3D set up, implemented a timer system to allow us to calculate the time between each frame, and set up the framework that will grant us access to our game without interfering with the core engine. In this tutorial, we are going to implement a reusable Scene Object class, which will be the parent for all future classes that we wish to enable in a scene manager. The scope of this tutorial series makes a full-fledged instance management system overkill, so we will not be building one in here. However, as this tutorial series will act as the base for future tutorials that will implement a scene manager, I am going to implement the scene object class here.</p>
<p><strong>Downloads</strong><br />
<a href="http://files.minalien.com/D3DPong/03/D3DPong-Bin.rar">Binary Only (18.8KB)</a><br />
<a href="http://files.minalien.com/D3DPong/03/D3DPong-Src.rar">Full VS2010 Project Dir (28.4KB)</a></p>
<p><strong>Starting Words</strong><br />
I just wanted to point out that this tutorial series borrows a lot of the structure of Jonathan Harbour&#8217;s Advanced2D game engine, from the book &#8220;<a href="http://www.amazon.com/Advanced-Game-Development-Jonathan-Harbour/dp/1598633422">Advanced 2D Game Development</a>&#8220;. While I certainly believe it to be a wonderful book, and an excellent starting point for beginners (in fact, the reason for me borrowing the format for his engine is <em>because</em> I find the method well-suited for beginners), I find that I have a lot of personal quirks with Jonathan&#8217;s methods and programming habits, and there are a number of inconsistencies in it. Furthermore, there are some topics that are covered in the book that just aren&#8217;t well-suited for beginners, and can cause things to be a bit confusing and convoluted. In this tutorial series, I don&#8217;t plan to implement three-dimensional rendering, which he does cover briefly, and have no intention of including game scripting, though several more advanced tutorials will cover these aspects at a later date (read: when I stop being lazy). Anybody looking at learning to program two-dimensional games with DirectX and C++ should look into the book, but should remember that there will always be better ways to implement certain systems than what is shown to you in a book &#8211; or an online tutorial. Anybody that wants to gain a solid understanding and truly grasp the depth of these programming systems is highly encouraged to look up information on any subjects that they are unfamiliar with. I strongly recommend <a href="http://www.safaribooksonline.com">Safari Books Online</a> &#8211; I read a lot of books there, and it&#8217;s certainly helped me improve myself. Advanced 2D Game Development is available on SBO, as well.</p>
<p><em>Notice: This tutorial continues from where &#8220;<a href="http://www.minalien.com/index.php/2010/02/15/pong-series-02/">C++/D3D Pong Tutorial 02</a>&#8221; article left off. Please read it if you have not already.</em></p>
<p><span id="more-197"></span></p>
<p><strong>Libraries</strong><br />
You will not need to add any more libraries to your project at this time. As of the last tutorial, the following should be in your project:<br />
d3d9.lib<br />
d3dx9.lib<br />
dxguid.lib</p>
<p><strong>Fixing up some Bugs</strong><br />
While writing this tutorial, I&#8217;ve come across a bug in the engine that had remained unnoticed until now. Because of how we&#8217;ve set up the game engine to handle the end of the game (specifically, the destruction of the game window), we end up calling our GameEnd function twice, which will cause issues when deleting the game sprites that we are going to be creating in this tutorial. The following changes will fix the problem:</p>
<p>In <em>CGameEngine.h</em>, add:</p>
<pre class="brush: cpp; toolbar: false;">void SetGameRunning(bool val) { this-&gt;_gameRunning = val; }</pre>
<p>below the rest of the Mutator functions.</p>
<p>In <em>CGameEngine.cpp</em>, change:</p>
<pre class="brush: cpp;">	void CGameEngine::End(void)
	{
		this-&gt;_gameRunning = false;
		GameEnd();
	}</pre>
<p>to</p>
<pre class="brush: cpp;">	void CGameEngine::End(void)
	{
		GameEnd();
	}</pre>
<p>In <em>WinMain.cpp</em>, change</p>
<pre class="brush: cpp;">	case WM_QUIT:
	case WM_CLOSE:
	case WM_DESTROY:{
		GEInstance-&gt;End();
					} break;</pre>
<p>to</p>
<pre class="brush: cpp;">	case WM_QUIT:
	case WM_CLOSE:
	case WM_DESTROY:{
		GEInstance-&gt;SetGameRunning(false);
					} break;</pre>
<p>and add</p>
<pre class="brush: cpp; toolbar: false;">	// End the Game
	GEInstance-&gt;End();</pre>
<p>And our issues should be solved. The problem here is that WM_DESTROY is called a frame after WM_CLOSE/WM_QUIT are called, and the game will still execute a frame. Making these changes will make sure that &#8220;D3DPong::CGameEngine::Instance()->_gameRunning&#8221; is merely set to false, which we are checking in our Main loop, so that we can just stop execution of the frames.</p>
<p>There is one other error that I&#8217;ve uncovered. I&#8217;d forgotten to actually clear the scene before we begin rendering. In <em>CGameEngine.cpp</em>, add the following to the BeginRender() method, right after we check the Direct3D Device (and right before we Begin the scene).</p>
<pre class="brush: cpp; toolbar: false;">		this-&gt;_device-&gt;Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);</pre>
<p>&#8230;Holy hell, I think I wasn&#8217;t paying much attention when I first programmed this, but I guess that&#8217;s how programming goes. There will always be bugs to fix. Here&#8217;s the last one. Our Game Engine&#8217;s Update() method wasn&#8217;t taking the first frame into account. With how we were handling our delta calculation, that meant that our first frame would have an incredibly high delta. So, replace the update method with the following:</p>
<pre class="brush: cpp;">	void CGameEngine::Update(void)
	{
		static float lastFrame = 0.0f;
		static bool firstFrame = true;

		float delta = ((float)((int)this-&gt;_gameTimer-&gt;GetTime())) - lastFrame;
		lastFrame = ((float)((int)this-&gt;_gameTimer-&gt;GetTime()));

		if(firstFrame)
		{
			delta = 0;
			firstFrame = false;
		}

		GameUpdate(delta);

		this-&gt;BeginRender();

		GameRender(delta);

		this-&gt;EndRender();
	}</pre>
<p><strong>What we are implementing</strong><br />
Since we are starting to get into the juicier parts of the system, I am going to start by providing information on <em>why</em> we are doing things as we are doing them &#8211; what the point behind our systems, why certain design decisions are made, etc.</p>
<p>In a game as simple as Pong, we could easily get away with making every scene object a sprite, taking with it all of our rendering capabilities and continuing on with life. However, it is always important to be looking into the future. It&#8217;s much better (and easier) to upgrade an engine that you&#8217;ve already gotten a lot of work done on than it is to build a new one from scratch. Say, for example, that you are working on a two-dimensional adventure game, such as <em>The Legend of Zelda: The Minish Cap</em>. There are several points in such a game that having scene objects that don&#8217;t necessarily have rendering capabilities would be very useful, such as a hidden trap or a trigger area in which the player will constantly lose health. While you could define them and merely not have anything to render for them, they will still have space made for them in the system&#8217;s memory for the texture data and all of the little render flags, and rendering methods will always be called on them, creating extra overhead. Therefore, it would be much easier to have a base Scene Object class, without any of the extra rendering data, that would help us to cut down on the game&#8217;s resource usage.</p>
<p>For our Scene Object class, we are going to hold basic atomic data, such as the scene object&#8217;s position, size, rotation, velocity, acceleration, friction, and a three-dimensional gravity (allowing us to do horizontal &#8220;gravity&#8221; as well as the usual vertical gravity). We are going to use a three-dimensional vector provided to us by D3DX (the D3DXVECTOR3 struct), rather than defining our own to store this information. Position will be a basic value storing the object&#8217;s position, velocity will be a value added to our Position every frame (all of these will be curbed by the frame delta), acceleration will be added to velocity every frame, and friction will then be either added to or subtracted from the velocity before it is finalized. Friction will always go in the direction of a velocity of &#8220;zero,&#8221; and will have a check to ensure that it is a positive value before modifying the velocity. Gravity will be the last to be applied, and will be the very last thing to be accounted for within the scene object&#8217;s velocity.</p>
<p><strong>Code Listing 1: CSceneObject.h</strong></p>
<pre class="brush: cpp;">#pragma once

// Global Header Files
#include &lt;Windows.h&gt;
#include &lt;d3d9.h&gt;
#include &lt;d3dx9.h&gt;

namespace D3DPong
{
	class CSceneObject
	{
	private:
		// Position, Rotation, and Size data
		D3DXVECTOR2 _position;
		D3DXVECTOR2 _size;
		D3DXVECTOR2 _scale;
		float _rotation;

		// Velocity and other basic movement data
		D3DXVECTOR2 _velocity;
		D3DXVECTOR2 _acceleration;
		D3DXVECTOR2 _friction;
		D3DXVECTOR2 _gravity;

	protected:
		bool _canUpdate;
		bool _canRender;

	public:
		/** Constructor &amp; Destructor **/
		CSceneObject();
		~CSceneObject();

		/** Function Declarations **/
		void Update(float);

		/** Accessors &amp; Mutators **/
		bool CanRender(void) { return this-&gt;_canRender; }
		bool CanUpdate(void) { return this-&gt;_canUpdate; }

		float GetLeft(void) { return this-&gt;_position.x; }
		float GetRight(void) { return (this-&gt;_position.x + (this-&gt;_size.x * this-&gt;_scale.x)); }
		float GetTop(void) { return this-&gt;_position.y; }
		float GetBottom(void) { return (this-&gt;_position.y + (this-&gt;_size.y * this-&gt;_scale.y)); }
		float GetWidth(void) { return this-&gt;_size.x; }
		float GetHeight(void) { return this-&gt;_size.y; }
		D3DXVECTOR2 GetCenter(void) { return D3DXVECTOR2(((float)(this-&gt;_size.x * this-&gt;_scale.x)/2), ((float)(this-&gt;_size.y * this-&gt;_scale.y)/2)); }

		D3DXVECTOR2 GetPosition(void) { return this-&gt;_position; }
		void SetPosition(D3DXVECTOR2 val) { this-&gt;_position = val; }
		void SetPositionX(float val) { this-&gt;_position.x = val; }
		void SetPositionY(float val) { this-&gt;_position.y = val; }

		D3DXVECTOR2 GetSize(void) { return this-&gt;_size; }
		void SetSize(D3DXVECTOR2 val) { this-&gt;_size = val; }
		void SetWidth(float val) { this-&gt;_size.x = val; }
		void SetHeight(float val) { this-&gt;_size.y = val; }

		D3DXVECTOR2 GetScale(void) { return this-&gt;_scale; }
		void SetScale(D3DXVECTOR2 val) { this-&gt;_scale = val; }
		void SetScaleX(float val) { this-&gt;_scale.x = val; }
		void SetScaleY(float val) { this-&gt;_scale.y = val; }

		float GetRotation(void) { return this-&gt;_rotation; }
		void SetRotation(float val) { this-&gt;_rotation = val; }

		D3DXVECTOR2 GetVelocity(void) { return this-&gt;_velocity; }
		void SetVelocity(D3DXVECTOR2 val) { this-&gt;_velocity = val; }
		void SetVelocityX(float val) { this-&gt;_velocity.x = val; }
		void SetVelocityY(float val) { this-&gt;_velocity.y = val; }

		D3DXVECTOR2 GetAcceleration(void) { return this-&gt;_acceleration; }
		void SetAcceleration(D3DXVECTOR2 val) { this-&gt;_acceleration = val; }
		void SetAccelerationX(float val) { this-&gt;_acceleration.x = val; }
		void SetAccelerationY(float val) { this-&gt;_acceleration.y = val; }

		D3DXVECTOR2 GetFriction(void) { return this-&gt;_friction; }
		void SetFriction(D3DXVECTOR2 val) { this-&gt;_friction = val; }
		void SetFrictionX(float val) { this-&gt;_friction.x = val; }
		void SetFrictionY(float val) { this-&gt;_friction.y = val; }

		D3DXVECTOR2 GetGravity(void) { return this-&gt;_gravity; }
		void SetGravity(D3DXVECTOR2 val) { this-&gt;_gravity = val; }
		void SetGravityX(float val) { this-&gt;_gravity.x = val; }
		void SetGravityY(float val) { this-&gt;_gravity.y = val; }
	};
};</pre>
<p>It looks big, but it&#8217;s not all that hard. Most of the code here is creating the Mutator and Accessor functions. Let&#8217;s get cracking, then:</p>
<pre class="brush: cpp;">		// Position, Rotation, and Size data
		D3DXVECTOR2 _position;
		D3DXVECTOR2 _size;
		D3DXVECTOR2 _scale;
		float _rotation;</pre>
<p>These are three important variables. The first three are all two-dimensional vectors &#8211; they hold an X and a Y value. Position will store the object&#8217;s position in world space (in this series, world space and screen space are one and the same. In future series, when we implement a sort of two-dimensional camera or viewport system, the two will be different. Size stores the object&#8217;s size, in pixels, and will be used later when we implement our basic collision-detection system. The object&#8217;s scale represents how large it is. Our sprite class (for two-dimensional images) will scale the image on rendering to this value, and our GetBottom and GetRight take the scale into account. Again, this information will be very important for our collision detection system later. Our final variable, rotation, is a floating-point value representing the number of degrees that our object is rotated by &#8211; it can be from zero to three-hundred sixty, though higher (or lower) values will automatically wrap.</p>
<pre class="brush: cpp;">		// Velocity and other basic movement data
		D3DXVECTOR2 _velocity;
		D3DXVECTOR2 _acceleration;
		D3DXVECTOR2 _friction;
		D3DXVECTOR2 _gravity;</pre>
<p>If these physics variables make you cringe in fear,then let me take this moment to ease your mind by saying that they aren&#8217;t even remotely complicated concepts. These are exactly as explained above &#8211; velocity is the object&#8217;s current, linear velocity, acceleration will be added to the velocity, friction will drag our velocity toward zero, and gravity will drag us toward a certain direction in game space.</p>
<pre class="brush: cpp;">	protected:
		bool _canUpdate;
		bool _canRender;</pre>
<p>These protected variables will prove to be very important in future tutorials, when we implement an instance manager. Basically, each sub-class of CSceneObject will set these variables in their constructor. _canUpdate represents whether or not the class implements an Update() method, while _canRender represents whether or not it implements a Render() method.</p>
<pre class="brush: cpp;">		/** Function Declarations **/
		void Update(float);</pre>
<p>Finally, we declare an Update() function. We are going to have a default Update() method defined because it is incredibly rare that we will ever need to have a scene object that doesn&#8217;t implement updating. We will be able to stop Update from being called, if needed, which can later be checked by an instance manager.</p>
<p><strong>Code Listing 2: CSceneObject.cpp</strong></p>
<pre class="brush: cpp;">#include &quot;CSceneObject.h&quot;

namespace D3DPong
{
	CSceneObject::CSceneObject(void)
		: _position(0,0), _size(0,0),
		_scale(1,1), _rotation(0),
		_velocity(0,0), _acceleration(0,0),
		_friction(0,0), _gravity(0,0),
		_canUpdate(true), _canRender(false)
	{
	}

	CSceneObject::~CSceneObject(void)
	{
	}

	void CSceneObject::Update(float delta)
	{
		// First, add our Acceleration to the object's velocity
		this-&gt;_velocity += this-&gt;_acceleration;

		// Now, we need to handle friction. This will be handled based on whether the velocity is positive or negative.
		if(this-&gt;_velocity.x &gt; 0)
		{
			this-&gt;_velocity.x -= abs(this-&gt;_friction.x);
			if(this-&gt;_velocity.x &lt; 0)
				this-&gt;_velocity.x = 0;
		}
		else if(this-&gt;_velocity.x &lt; 0)
		{
			this-&gt;_velocity.x += abs(this-&gt;_friction.x);
			if(this-&gt;_velocity.x &gt; 0)
				this-&gt;_velocity.x = 0;
		}

		if(this-&gt;_velocity.y &gt; 0)
		{
			this-&gt;_velocity.y -= abs(this-&gt;_friction.y);
			if(this-&gt;_velocity.y &lt; 0)
				this-&gt;_velocity.y = 0;
		}
		else if(this-&gt;_velocity.y &lt; 0)
		{
			this-&gt;_velocity.y += abs(this-&gt;_friction.y);
			if(this-&gt;_velocity.y &gt; 0)
				this-&gt;_velocity.y = 0;
		}

		// Finally, it's time to handle gravity
		this-&gt;_velocity -= this-&gt;_gravity;

		// And then we add the velocity (multiplied by the frame delta) to the position
		this-&gt;_position += (this-&gt;_velocity * (delta/100));
	}
};</pre>
<p>This one&#8217;s not very large &#8211; really, the only important method here is the Update() function, so that&#8217;s what I&#8217;ll break down:</p>
<pre class="brush: cpp;">	void CSceneObject::Update(float delta)
	{
		// First, add our Acceleration to the object's velocity
		this-&gt;_velocity += this-&gt;_acceleration;

		// Now, we need to handle friction. This will be handled based on whether the velocity is positive or negative.
		if(this-&gt;_velocity.x &gt; 0)
		{
			this-&gt;_velocity.x -= abs(this-&gt;_friction.x);
			if(this-&gt;_velocity.x &lt; 0)
				this-&gt;_velocity.x = 0;
		}
		else if(this-&gt;_velocity.x &lt; 0)
		{
			this-&gt;_velocity.x += abs(this-&gt;_friction.x);
			if(this-&gt;_velocity.x &gt; 0)
				this-&gt;_velocity.x = 0;
		}

		if(this-&gt;_velocity.y &gt; 0)
		{
			this-&gt;_velocity.y -= abs(this-&gt;_friction.y);
			if(this-&gt;_velocity.y &lt; 0)
				this-&gt;_velocity.y = 0;
		}
		else if(this-&gt;_velocity.y &lt; 0)
		{
			this-&gt;_velocity.y += abs(this-&gt;_friction.y);
			if(this-&gt;_velocity.y &gt; 0)
				this-&gt;_velocity.y = 0;
		}

		// Finally, it's time to handle gravity
		this-&gt;_velocity -= this-&gt;_gravity;

		// And then we add the velocity (multiplied by the frame delta) to the position
		this-&gt;_position += (this-&gt;_velocity * (delta/100));
	}</pre>
<p>The D3DXVECTOR2 structure provides operator support to allow us to easily add and multiply values and the vectors. We can add, multiply, subtract, etc, for example, two vectors, or even a vector and a floating-point value. Doing operations on a vector will perform that operation on both axes dealt with by the vector (X &#038; Y), or on corresponding vectors in the case of vector-to-vector operations (so this->_velocity += this->_acceleration adds acceleration&#8217;s X to velocity&#8217;s X, and acceleration&#8217;s Y to velocity&#8217;s Y). My explanation may be a little confusing, but looking at the code should clarify things.</p>
<p><strong>Code Listing 3: CTexture.h</strong><br />
Before we move on to defining the Game Sprite class, we need a class to store our game&#8217;s textures. While we could easily just lump this into the Game Sprite class, it makes more sense to move this to its own class &#8211; after all, what if we want to implement an object with rendering that doesn&#8217;t inherit from the game sprite class? So, here we go. This is pretty simple to implement.</p>
<pre class="brush: cpp;">#pragma once

// Global Header Files
#include &lt;Windows.h&gt;
#include &lt;d3d9.h&gt;
#include &lt;d3dx9.h&gt;

namespace D3DPong
{
	class CTexture
	{
	private:
		LPDIRECT3DTEXTURE9 _texture;
		D3DXIMAGE_INFO _imageInfo;

	public:
		CTexture(void);
		~CTexture(void);

		/** Function Declarations **/
		bool Load(LPCWSTR);

		/** Accessors **/
		LPDIRECT3DTEXTURE9 GetTexture(void) { return this-&gt;_texture; }
		float GetWidth(void) { return (float)this-&gt;_imageInfo.Width; }
		float GetHeight(void) { return (float)this-&gt;_imageInfo.Height; }
	};
};</pre>
<pre class="brush: cpp;">		LPDIRECT3DTEXTURE9 _texture;
		D3DXIMAGE_INFO _imageInfo;</pre>
<p>Just a couple small things in the declaration side. The first variable stores the Direct3D texture structure, and will hold the actual image data. The second variable provides us valuable information about the image, and will be populated before we actually load the image. This will allow us to automatically set the texture&#8217;s height, width, etc.</p>
<pre class="brush: cpp; toolbar: false;">		bool Load(LPCWSTR);</pre>
<p>This function takes one argument &#8211; the filename that we want to load. It can be either an absolute location (L&#8221;C:/Sprite.png&#8221;) or a relative location (&#8220;./media/img/Sprite.png&#8221;). In any project you run, you&#8217;ll generally want to use relative locations. The function will return true if it succeeded in loading the file, or false if it failed.</p>
<p><strong>Code Listing 4: CTexture.cpp</strong><strong></p>
<pre class="brush: cpp;">#include &quot;CTexture.h&quot;

#include &quot;CGameEngine.h&quot;

namespace D3DPong
{
	CTexture::CTexture(void)
		: _texture(NULL)
	{
	}

	CTexture::~CTexture(void)
	{
		if(this-&gt;_texture != NULL)
			this-&gt;_texture-&gt;Release();
	}

	bool CTexture::Load(LPCWSTR fileName)
	{
		if(FAILED(D3DXGetImageInfoFromFile(fileName, &amp;_imageInfo)))
		{
			this-&gt;_texture = NULL;
			return false;
		}

		if(FAILED(D3DXCreateTextureFromFileEx(CGameEngine::Instance()-&gt;GetDevice(),
			fileName, 											// File Name
			this-&gt;_imageInfo.Width, this-&gt;_imageInfo.Height, 1,	// Width, Height, and MIP Level Count
			D3DPOOL_DEFAULT, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT,	// Usage method, Format, and Pool
			D3DX_DEFAULT, D3DX_DEFAULT,							// Image and MIP Filters
			D3DCOLOR_XRGB(255, 0, 255),							// Transparency Key (Magenta)
			&amp;this-&gt;_imageInfo, NULL, &amp;this-&gt;_texture			// Image information structure, color pallete, and destination texture
			)))
		{
			this-&gt;_texture = NULL;
			return false;
		}

		return true;
	}
};</pre>
<p>This time, I&#8217;m not going to bother re-pasting the code for the Load function, and I&#8217;ll just explain it here, since it&#8217;s the only thing of importance. Just note that we call the texture structure&#8217;s Release() function when we delete the object, to make sure it is cleared from memory.</p>
<p>The first thing we do in the Load function is get the file information from the file. The only parts we&#8217;re bothering with are the Width and Height of the image file, but we do get some other information from the file.After that, we actually create the texture from a file, using the aptly-named D3DXCreateTextureFromFileEx. This takes the filename, the image size data, various pieces of information that we&#8217;re not going to worry about (usage, format, etc), and we pass the image information and the texture data. The transparency key that we provide will allow us to use a color key transparency method, if we&#8217;re not interested in using alpha transparency provided by several formats (such as PNG). Any pixel of Magenta color (Red 255, Green 0, Blue 255) will be completely transparent. D3DX will also automatically, properly handle images that support transparency, so you can save yourself some trouble by using a good image editor that can output alpha transparency (GIMP, Paint.NET, and Photoshop all support these formats).</p>
<p></strong><strong>Code Listing 5: CGameSprite.h</strong></p>
<pre class="brush: cpp;">#pragma once

// Global Header Files
#include &lt;Windows.h&gt;
#include &lt;d3d9.h&gt;
#include &lt;d3dx9.h&gt;

// Local Header Files
#include &quot;CSceneObject.h&quot;
#include &quot;CTexture.h&quot;

namespace D3DPong
{
	class CGameSprite : public CSceneObject
	{
	private:
		/** Texture Data **/
		CTexture* _texture;
		bool _visible;
		D3DXMATRIX _matRotate, _matScale;

		/** Function Declarations **/
		void transform(void);

	public:
		/** Constructor &amp; Destructor **/
		CGameSprite(void);
		~CGameSprite(void);

		/** Function Declarations **/
		bool Load(LPCWSTR);
		void Render(float);

		/** Accessors &amp; Mutators **/
		CTexture* GetTexture(void) { return this-&gt;_texture; }
		void SetTexture(CTexture* val) { this-&gt;_texture = val; }

		bool IsVisible(void) { return this-&gt;_visible; }
		void SetVisible(bool val) { this-&gt;_visible = val; }
	};
};</pre>
<p>And here&#8217;s our sprite class. Because we&#8217;re inheriting from CSceneObject, we don&#8217;t need to worry about all of the extra atomic data for the object. We&#8217;re going to have to store a texture for the sprite, store whether or not the object is visible (so that we can easily make an object appear and disappear), two Matrices to allow us to apply rotation. When using the D3DXSprite sprite handler, we can easily just provide a position and not worry about matrices at all &#8211; which works great for most games. However, because we want to implement scaling and rotation, we will have to use matrices. Matrices are used to provide translation (position), rotation, and scaling in three-dimensional games. They are also used in a number of two-dimensional games, to provide hardware-accelerated graphics functions. The transformation will be performed in the private &#8220;transform()&#8221; method, which will be called from our Render() method.</p>
<p><strong>Code Listing 6: CGameSprite.cpp</strong></p>
<pre class="brush: cpp;">#include &quot;CGameSprite.h&quot;

#include &quot;CGameEngine.h&quot;

namespace D3DPong
{
	CGameSprite::CGameSprite(void)
		: _texture(NULL), _visible(true)
	{
	}

	CGameSprite::~CGameSprite(void)
	{
		if(this-&gt;_texture != NULL)
			delete this-&gt;_texture;
	}

	void CGameSprite::transform(void)
	{
		D3DXMATRIX mat;

		D3DXMatrixTransformation2D(&amp;mat, NULL, 0,
			&amp;this-&gt;GetScale(), &amp;this-&gt;GetCenter(), this-&gt;GetRotation(), &amp;this-&gt;GetPosition());

		CGameEngine::Instance()-&gt;GetSpriteHandler()-&gt;SetTransform(&amp;mat);
	}

	bool CGameSprite::Load(LPCWSTR fileName)
	{
		if(this-&gt;_texture != NULL)
			delete this-&gt;_texture;

		this-&gt;_texture = new CTexture();
		if(this-&gt;_texture-&gt;Load(fileName))
		{
			this-&gt;SetSize(D3DXVECTOR2(this-&gt;_texture-&gt;GetWidth(), this-&gt;_texture-&gt;GetHeight()));
			return true;
		}
		else
			return false;
	}

	void CGameSprite::Render(float)
	{
		this-&gt;transform();

		if(this-&gt;_texture != NULL &amp;&amp; this-&gt;_visible)
			CGameEngine::Instance()-&gt;GetSpriteHandler()-&gt;Draw(this-&gt;_texture-&gt;GetTexture(), NULL, NULL, NULL, D3DCOLOR_XRGB(255, 255, 255));
	}
};</pre>
<p>It doesn&#8217;t look all that difficult, I hope.</p>
<pre class="brush: cpp;">	void CGameSprite::transform(void)
	{
		D3DXMATRIX mat;

		D3DXMatrixTransformation2D(&amp;mat, NULL, 0,
			&amp;this-&gt;GetScale(), &amp;this-&gt;GetCenter(), this-&gt;GetRotation(), &amp;this-&gt;GetPosition());

		CGameEngine::Instance()-&gt;GetSpriteHandler()-&gt;SetTransform(&amp;mat);
	}</pre>
<p>The first method we handle, as explained, creates the transformation matrix, and then sets the current transformation on the sprite handler. We need a D3DXMATRIX variable to store the transformation Matrix, then we create the actual 2D transformation Matrix. We specify that the matrix to output to is our newly-defined mat variable, and we pass our Scale, Rotation, and Position vectors to the object. We also pass a new vector, that returns the center of the object, which will form the base around which our object rotates.</p>
<pre class="brush: cpp;">	void CGameSprite::Render(float)
	{
		this-&gt;transform();

		if(this-&gt;_texture != NULL &amp;&amp; this-&gt;_visible)
			CGameEngine::Instance()-&gt;GetSpriteHandler()-&gt;Draw(this-&gt;_texture-&gt;GetTexture(), NULL, NULL, NULL, D3DCOLOR_XRGB(255, 255, 255));
	}</pre>
<p>And here is our rendering method. First, we call our transform, so that we can get the object placed, rotated, and scaled properly. Next, we check to make sure that our texture is loaded, and whether or not our object is actually visible, before we render. This ensures that we don&#8217;t try to draw a NULL texture, which would cause our engine to crash. We pass the texture and a color to the Draw() method. The color we pass will saturate the image &#8211; we use white, to keep things as they are in the image.</p>
<pre class="brush: cpp;">bool CGameSprite::Load(LPCWSTR fileName)
	{
		if(this-&gt;_texture != NULL)
			delete this-&gt;_texture;

		this-&gt;_texture = new CTexture();
		if(this-&gt;_texture-&gt;Load(fileName))
		{
			this-&gt;SetSize(D3DXVECTOR2(this-&gt;_texture-&gt;GetWidth(), this-&gt;_texture-&gt;GetHeight()));
			return true;
		}
		else
			return false;
	}</pre>
<p>Finally, we delete the old texture (if we want to load a new texture, we want to prevent memory leaks and clean up after ourselves). Then, we recreate the texture object and attempt to load the image, then set the object&#8217;s size to match that of the image. Simple, clean, efficient.</p>
<p><strong>Code Listing 7: Game.cpp</strong><br />
We can keep the exact same Game.h that we created in the last project, and provide the following code to test our game engine&#8217;s most recent additions:</p>
<pre class="brush: cpp;">#include &quot;Game.h&quot;

#include &quot;CGameEngine.h&quot;

#define GEInstance D3DPong::CGameEngine::Instance()

D3DPong::CGameSprite* testSprite;

bool GamePreLoad(void)
{
	GEInstance-&gt;SetScreenWidth(800);
	GEInstance-&gt;SetScreenHeight(600);
	GEInstance-&gt;SetWindowTitle(L&quot;Direct3D Pong Tutorial 03 - Scene Objects and Sprite Rendering&quot;);

	return true;
}

bool GameInit(void)
{
	// Load a test sprite
	testSprite = new D3DPong::CGameSprite();
	if(!testSprite-&gt;Load(L&quot;./media/img/testSprite.png&quot;))
		MessageBox(NULL, L&quot;Failed to Load&quot;, L&quot;:(&quot;, MB_OK);

	float x = (float)(GEInstance-&gt;GetScreenWidth() - testSprite-&gt;GetWidth());
	float y = (float)(GEInstance-&gt;GetScreenHeight() / 2);

	testSprite-&gt;SetPositionX(x);
	testSprite-&gt;SetPositionY((y - testSprite-&gt;GetCenter().y));

	testSprite-&gt;SetVelocityX(-10);

	return true;
}

void GameUpdate(float delta)
{
	testSprite-&gt;Update(delta);

	if(testSprite-&gt;GetLeft() &lt;= 0)
	{
		testSprite-&gt;SetVelocityX(0);
		testSprite-&gt;SetPositionX(0);
	}
}

void GameRender(float delta)
{
	testSprite-&gt;Render(delta);
}

void GameEnd(void)
{
	if(testSprite != NULL)
		delete testSprite;
}</pre>
<p>All of the code in this test are pretty self-explanatory &#8211; we create and load the sprite, then we position it right and centered on the screen, and give the object a negative velocity on the X-axis &#8211; making it move to the left until it hits the end and stops.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.minalien.com/index.php/2010/03/21/cd3d-pong-tutorial-03-scene-objects-and-sprite-rendering/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>I&#8217;m not dead. I promise. So can I please eat your brains?</title>
		<link>http://www.minalien.com/index.php/2010/03/16/can-i-please-eat-your-brains/</link>
		<comments>http://www.minalien.com/index.php/2010/03/16/can-i-please-eat-your-brains/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 02:08:56 +0000</pubDate>
		<dc:creator>Samantha</dc:creator>
				<category><![CDATA[Life]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[Boredom]]></category>
		<category><![CDATA[Brains]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[IRC]]></category>
		<category><![CDATA[Keyboard-Failure]]></category>
		<category><![CDATA[Safari Books Online]]></category>
		<category><![CDATA[SMF]]></category>
		<category><![CDATA[SyntaxHighlighter]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[Zombies]]></category>

		<guid isPermaLink="false">http://www.minalien.com/?p=180</guid>
		<description><![CDATA[Well, I haven&#8217;t exactly blogged much of anything in the past couple weeks, mostly because I&#8217;ve been side-tracked with a couple projects, such as the new website for the Keyboard-Failure IRC network (http://www.kbfail.net- or point your IRC client of choice to irc://irc.kbfail.net). You can see the new theme (which I think is a major improvement]]></description>
			<content:encoded><![CDATA[<p>Well, I haven&#8217;t exactly blogged much of anything in the past couple weeks, mostly because I&#8217;ve been side-tracked with a couple projects, such as the new website for the Keyboard-Failure IRC network (<a href="http://www.kbfail.net">http://www.kbfail.net</a>- or point your IRC client of choice to <a href="irc://irc.kbfail.net/KBFail">irc://irc.kbfail.net</a>). You can see the new theme (which I think is a major improvement over the pink-background, black-text spawned by the lack of any desire to work to give KBFail a real theme/system. What can I say? We&#8217;re all lazy.) by going to the development page: <a href="http://minalien.dynalias.net/kbfail/">http://minalien.dynalias.net/kbfail/</a>. Guess when I get around to working on it, I&#8217;m going to install the newest Release Candidate of SMF for development/testing of the new site, and I&#8217;ll probably get around to rebuilding my old mod to integrate Alex Gorbatchev&#8217;s SyntaxHighlighter (which I use on this blog) sometime soon, too. Gods, I wish SMF were object-oriented. :P</p>
<p>It seems that I do actually have somebody paying attention to the Direct3D Pong tutorial series I&#8217;ve been writing. I kind of shrugged off doing much more work on the project because I didn&#8217;t think anybody was bothering to read it, but a recent tweet shows me that there is at least one person reading it. That, for me, is plenty &#8211; so <a href="http://twitter.com/Ogreman">@Ogreman</a>, you can look forward to the next tutorial in the series to come along (hopefully) by the end of this weekend.</p>
<p>I&#8217;ve been going through some.. personal issues. Anybody familiar with my past will probably know what I&#8217;m talking about, so I&#8217;ll just kinda leave the details at that. Suffice to say that life is a pain in the ass, but I&#8217;m (uncharacteristically) looking toward the brighter side of things and moving forward with life. On the lighter side of things, my plans to visit Erin in Phoenix are going through, so I&#8217;ll be blissfully free of work <strong>and</strong> hanging out with a friend. I&#8217;ve been acting very out-of-character for me, recently. I mean, social activity? Something seriously has to be wrong with me. I think I broke my head. D:</p>
<p>Recently renewed my subscription to <a href="http://www.safaribooksonline.com">Safari Books Online</a> &#8211; I pay for the $40-ish unlimited subscription, but they&#8217;ve got a $20-ish (monthly) ten-books-per-month plan. Very useful, and I highly recommend it for anybody who deals with technology on a hobby basis. Large collection of full books on there, ready for you to read on your computer at home, or even in mobile-optimized forms on your iPhone or (if you&#8217;re one of the cool kids, like me) Android or other smartphone. Anyway, I&#8217;ve been reading up and studying for the CCNA certifications, which I&#8217;m hoping I will be able to take on-base without first going through the &#8220;NCO&#8221; courses. In the worst case scenario, I&#8217;ll just find somewhere closeby off-base, probably in Los Angeles, to take it on my own. They&#8217;ve got a full collection of Cisco networking books, programming (including game programming) books for a large selection of languages (Obj-C, C, C++, C#, Ruby, Java, HTML/XHTML, PHP, ASP.NET.. you name it, it&#8217;s there). For those who can afford it and will use it, it&#8217;s <strong>much</strong> cheaper than buying each of the books separately, and much more legal (and less of a pain in the ass for searching) than downloading the books on your favorite torrent client.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.minalien.com/index.php/2010/03/16/can-i-please-eat-your-brains/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
