<?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>Paradice Software &#187; Good coding guidelines</title>
	<atom:link href="http://www.paradicesoftware.com/blog/index.php/category/good-coding-guidelines/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.paradicesoftware.com/blog</link>
	<description>Developing a role-playing game since 1981</description>
	<lastBuildDate>Tue, 22 Jun 2010 08:23:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>3rd Party Code</title>
		<link>http://www.paradicesoftware.com/blog/index.php/2010/03/3rd-party-code/</link>
		<comments>http://www.paradicesoftware.com/blog/index.php/2010/03/3rd-party-code/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 22:46:42 +0000</pubDate>
		<dc:creator>Lawrence</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Good coding guidelines]]></category>
		<category><![CDATA[3rd party]]></category>
		<category><![CDATA[libraries]]></category>

		<guid isPermaLink="false">http://www.paradicesoftware.com/blog/?p=203</guid>
		<description><![CDATA[As a general rule, I like to write all of the code used by my games/programs myself. Not because I think I am the best programmer in the world, but because &#8211; for me &#8211; one of the main reasons I program is to learn more about how software functions on the very lowest levels, [...]]]></description>
			<content:encoded><![CDATA[<p>As a general rule, I like to write all of the code used by my games/programs myself. Not because I think I am the best programmer in the world, but because &#8211; for me &#8211; one of the main reasons I program is to learn more about how software functions on the very lowest levels, and if I used extensive 3rd party code (like Unreal Engine for my game, for example) a lot of that stuff that I *want* to learn about would be abstracted away, or worse: still present but obscured and intermingled with the 3rd party code itself.</p>
<p>Coincidentally, this is the same reason I don&#8217;t use managed languages like C#. I quite like knowing about things that managed code wants to hide! The increase in development time is not that important to me, I have no external deadlines to meet.</p>
<p>However, there are points beyond which it&#8217;s not practical for me to write the code myself, because it&#8217;s either A) incredibly complex, B) boring, C) proprietary, or D) so ubiquitous that it really doesn&#8217;t need reinventing. e.g.: writing code to decode an MP3 file into raw audio. I could *potentially* do this, but as it belongs to all four categories, I&#8217;m really comfortable with not doing it myself!</p>
<p>Currently in my Freepascal development I am using three 3rd Party Code libraries every day. And they are all excellent, both in terms of features and being easy enough that I can &#8216;plug them in&#8217; to my own lx7 game engine with very little work. Here they are:</p>
<h3>DirectX &#8211; by Clootie</h3>
<p>No, not DirectX itself (that deserves a whole separate subject!), but a port of all the headers/structures/glue code necessary to use DirectX in Freepascal. This gives me low level access to every function and interface used by DirectX itself, and the port is so perfect I was able to build my own graphics engine on top of Direct3D just by studying the (C++) DirectX SDK. Nothing is missing! Everything works! You probably won&#8217;t appreciate how rare this is if you normally use C++.<br />
<a href="http://www.clootie.ru/">http://www.clootie.ru/</a></p>
<h3>BASSfpc</h3>
<p>BASS is an Audio Library that makes it *dead easy* to play sound and music files of many different types. BASSfpc is the FPC port of this. BASS handles MP3s, WAVs, OGGs, and about a million other sound types, and it does it efficiently and with the bare minimum of code needed (literally about 4 lines of code to init the library and start playing an MP3!).<br />
<a href="http://www.un4seen.com/">http://www.un4seen.com/</a></p>
<h3>LNet</h3>
<p>The newest addition to my Libs folder. LNet provides a set of classes to implement networking, at the very lowest level, without adding any additional features or complexity. That&#8217;s exactly what I needed, because I want to add my own features and complexity! There are many many network libraries out there, but (for Freepascal at least) LNet definately gets my vote, for being simple, class-based, and extremely elegant.<br />
<a href="http://lnet.wordpress.com/">http://lnet.wordpress.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.paradicesoftware.com/blog/index.php/2010/03/3rd-party-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Game Golden Rule #2: Settings &amp; Controls</title>
		<link>http://www.paradicesoftware.com/blog/index.php/2009/12/game-golden-rule-2-settings-controls/</link>
		<comments>http://www.paradicesoftware.com/blog/index.php/2009/12/game-golden-rule-2-settings-controls/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 07:18:21 +0000</pubDate>
		<dc:creator>Lawrence</dc:creator>
				<category><![CDATA[Game Golden Rules]]></category>
		<category><![CDATA[Good coding guidelines]]></category>

		<guid isPermaLink="false">http://www.paradicesoftware.com/blog/?p=193</guid>
		<description><![CDATA[This is the second entry in my set of &#8220;10 Game Development Golden Rules&#8221;. Hopefully, this one is less contentious than the first!
The rule is:
Provide gamma and volume controls, and separate music from sound.
Pretty simple. But essential! The good news is, the AAA titles pretty much universally support this now. And it&#8217;s a good thing, [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second entry in my set of &#8220;10 Game Development Golden Rules&#8221;. Hopefully, this one is less contentious than the first!</p>
<p>The rule is:</p>
<h3>Provide gamma and volume controls, and separate music from sound.</h3>
<p>Pretty simple. But essential! The good news is, the AAA titles pretty much universally support this now. And it&#8217;s a good thing, as I use these settings all the time. </p>
<p>I have an Xbox 360 but, unlike most Xbox 360s, mine isn&#8217;t connected to a TV &#8211; it plugs into a second DVI port on one of my 22&#8221; widescreen PC monitors. This is awesome because I can run it at 1680&#215;1050 and games look beautiful! (handy bonus: I can web-surf on my other screen while waiting for Matchmaking in Halo 3)</p>
<p>BUT my monitor has a very different colour profile than your average TV, and games with a lot of contrasting light/dark elements can look really bad by default. Gamma controls in your game are pretty essential for me to see the graphics the way the designer intended. I&#8217;m sure there are other people in similar situations or even wildly different situations who also need gamma settings. Your game needs to support them too!! And since there&#8217;s nothing on the &#8220;con&#8221; side of the ledger that could make it a bad idea, you have no excuse!</p>
<p>Providing separate controls for sound/music: Music is awesome but people like to be able to hear the game sounds sometimes! Particularly in a &#8216;competitive&#8217; environment, or simply when your game is providing an audio narrative (GTA4 is an excellent example &#8211; the &#8220;in-car radio&#8221; music is huge and contributes to the GTA atmosphere, but you <strong>need</strong> to turn the music volume down to be able to hear what mission you&#8217;re being given via the cellphone). If I&#8217;m playing an FPS single-player, I might turn the music up, but in multiplayer I want to use every audio cue the game gives me for competitive advantage, so I need to kill the music or at least turn it down. </p>
<p>Just like with the gamma controls, the good news is this is pretty universal now. But there are still Flash games that don&#8217;t separate them (and, even worse: there are plenty that have no Volume settings at all &#8211; sound is either On or Off). Sound might *add* to the game experience, but I&#8217;d like to set it so that it doesn&#8217;t completely drown out my background music, thanks! Vista and Windows 7 allow you to set the volume controls at the &#8220;application&#8221; level, which solves the problem for me specifically, but it doesn&#8217;t help anyone using XP (or anyone who doesn&#8217;t know about that feature). Support them too!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paradicesoftware.com/blog/index.php/2009/12/game-golden-rule-2-settings-controls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Assets as Text Files!</title>
		<link>http://www.paradicesoftware.com/blog/index.php/2009/12/assets-as-text-files/</link>
		<comments>http://www.paradicesoftware.com/blog/index.php/2009/12/assets-as-text-files/#comments</comments>
		<pubDate>Sun, 06 Dec 2009 09:42:48 +0000</pubDate>
		<dc:creator>Lawrence</dc:creator>
				<category><![CDATA[Good coding guidelines]]></category>
		<category><![CDATA[Icefall]]></category>

		<guid isPermaLink="false">http://www.paradicesoftware.com/blog/?p=191</guid>
		<description><![CDATA[OK, this trick is probably universally known, but I&#8217;ve only just discovered it (by the classic programmer method of reinventing it), so on the chance there are others who are unfamiliar with this technique, it&#8217;s worth describing.
Any sufficiently complex game is going to have &#8220;assets&#8221;: data external to the main program that the game depends [...]]]></description>
			<content:encoded><![CDATA[<p>OK, this trick is probably universally known, but I&#8217;ve only just discovered it (by the classic programmer method of reinventing it), so on the chance there are others who are unfamiliar with this technique, it&#8217;s worth describing.</p>
<p>Any sufficiently complex game is going to have &#8220;assets&#8221;: data external to the main program that the game depends on to function. This includes &#8220;art assets&#8221; (textures, images, music, etc) as well as more application-specific &#8220;data assets&#8221; which contain data in a format unique to your application (e.g. Icefall has &#8220;item types&#8221; of all the different equipment players can find/buy/receive).</p>
<p>Handling art assets is relatively easy: you create/modify them in Photoshop (or equivalent), you save them as PNGs or JPGs, and you&#8217;re done! You might wrap them all up in a custom file format to compress/encrypt/streamline them, but that&#8217;s pretty simple and you can do that at the very end of development.</p>
<p>Data assets are different. There aren&#8217;t any programs around that can edit &#8220;Icefall item types&#8221; files. I need to write my own &#8220;Item Types Editor&#8221;, which understands this format and can load, modify and save it. This leads to two problems:</p>
<p>1. I have to spend time writing an Editor. (the actual format load/save code could be shared with Icefall itself, but there&#8217;s all the UI!).</p>
<p>2. If I ever change the &#8220;item types&#8221; format, all the data I&#8217;ve previously created is now in jeopardy. (What I typically do is modify the &#8217;save&#8217; code only, run the editor and  load each asset (old-code), and re-save it (new-code), then when that&#8217;s done I can modify the &#8216;load&#8217; code.</p>
<p>Depending on the type of data asset, using Asset Text Files may be superior. I DON&#8217;T mean your game should store all of it&#8217;s data assets in plain text (yuck!). Here&#8217;s what I mean:</p>
<p>Whip up a simple plaintext &#8217;syntax&#8217; that stores all of the information you want to include in your data asset. Then,  instead of creating an &#8220;Editor&#8221; with bulky UI code, create a command line utility that can parse your plaintext files and spit out your compiled data asset files. If you borrow your main game code for load/save, all you have to write is the text parser, and that can be as easy as you like: you&#8217;re dictating the syntax it has to interpret.</p>
<p>The BEST part of this system is that it makes changing the format of the assets later on very very easy. Oops, I forgot a field for my item types? No worries. Add the extra field to your text file sources (bringing to bear the full power of your fantastic Programmer Text Editor / IDE you&#8217;re using &#8211; the programmers&#8217; Photoshop), a quick change to the asset compiler to recognise the new format, and hit execute.</p>
<p>You get other benefits too: you can put &#8216;logic&#8217; into your compiler without having to put it in you main game. (e.g. scale the values, dictate whatever &#8220;optional&#8221; or &#8220;Default&#8221; fields, anything!</p>
<p>It doesn&#8217;t work for everything (e.g. Icefall&#8217;s &#8220;Level Map&#8221; is huge and an Editor really is the right option for that) &#8211; but when it&#8217;s appropriate, I have found it works brilliantly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paradicesoftware.com/blog/index.php/2009/12/assets-as-text-files/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>More Textures than Memory</title>
		<link>http://www.paradicesoftware.com/blog/index.php/2009/11/more-textures-than-memory/</link>
		<comments>http://www.paradicesoftware.com/blog/index.php/2009/11/more-textures-than-memory/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 05:10:19 +0000</pubDate>
		<dc:creator>Lawrence</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Good coding guidelines]]></category>
		<category><![CDATA[Icefall]]></category>
		<category><![CDATA[DirectX]]></category>
		<category><![CDATA[Freepascal]]></category>

		<guid isPermaLink="false">http://www.paradicesoftware.com/blog/?p=155</guid>
		<description><![CDATA[Icefall uses literally dozens of textures&#8230; (and it will probably be &#8216;hundreds&#8217; by the time the game is completed).
Textures are used for everything, from the mouse cursor, menus, fonts, buttons, to the game world itself, spells/action icons, monsters, equipment&#8230; everything.
Although my own main PC has 512MB of video (texture) RAM, not everyone does! And
following on [...]]]></description>
			<content:encoded><![CDATA[<p>Icefall uses literally dozens of textures&#8230; (and it will probably be &#8216;hundreds&#8217; by the time the game is completed).</p>
<p>Textures are used for everything, from the mouse cursor, menus, fonts, buttons, to the game world itself, spells/action icons, monsters, equipment&#8230; everything.</p>
<p>Although my own main PC has 512MB of video (texture) RAM, not everyone does! And<br />
following on from my <a href="http://www.paradicesoftware.com/blog/index.php/2009/11/minimum-requirements/">minimum requirements</a> post, I don&#8217;t really want someone&#8217;s video card memory size to be a limiting factor if everything else meets the requirements, so Icefall needs a way to manage things when there are more textures than there is video memory.</p>
<p>I don&#8217;t want each bit of code that uses textures to worry about whether they&#8217;re loaded or not, so the logical choice is to encapsulate all of the texture handling into one place: I call the class that handles this the <em>TContentManager</em> (&#8221;content&#8221; because it also handles sounds, fonts, music, etc.).</p>
<p>When the game code asks the <em>TContentManager</em> for a texture: the first thing it does is look to see if that texture is already available in video memory: if so, it just hands out a reference. Easy! If not, it retrieves the filename for that texture from Icefall&#8217;s <em>ResourceDatabase</em>, and attempts to load the texture from disk. If that fails, it looks at <strong>why</strong> it failed: if it&#8217;s <em>D3DERR_OUTOFVIDEOMEMORY</em>, the next step is to unload some other texture and try again, repeating until the load succeeds or we have no loaded textures left. (If it still doesn&#8217;t load, or if the texture load fails for some other reason, it&#8217;s goodbye Icefall).</p>
<p>Programmatically:</p>
<blockquote><pre>
function TContentManager.GetTexture(ID: TTextureID): TLXTexture;
var
   HRes: HRESULT;
   NewTexture: TLXTexture;
   FileName: String;
begin
   if TextureLoaded(ID) then
      exit(Texture[ID]);

   FileName := ResourceManager.Textures[ID].FileName;
   repeat
      HRes := LXLoadTexture(FileName,NewTexture);
      case HRes of
         D3D_OK:
            Texture[ID] := NewTexture;
         D3DERR_OUTOFVIDEOMEMORY:
            UnloadTexture(0);
      else
         // Fatal error!
      end;
   until TextureLoaded(ID);
   result := Texture[ID];
end function;
</pre>
</blockquote>
<p><em>UnloadTexture</em> is a method that unloads a texture. It takes a <em>TTextureID</em> as a parameter, but passing 0 in this case tells the method we want it to choose a texture to unload itself. This is where it gets interesting.</p>
<p>Before I got to this, I originally had set up a few states so that they explicitly called <em>TContentManager.UnloadTexture</em> to release textures when the game left that state (e.g. the game would unload the &#8216;Option-selection&#8217; texture when the player closed the Options dialog). However, this turned out to be sub-optimal for several reasons:</p>
<ul>
<li>Players might well leave and re-enter states (like Options) several times to accomplish whatever it is they&#8217;re trying to do.</li>
<li>It doesn&#8217;t make use of extra video memory: it keeps loading from disk (or the disk-cache anyway) while the extra video memory stays idle.</li>
<li>I had to explicitly declare what textures I was done with. Not a problem really but &#8216;just another thing&#8217; that I had to do when changing states.</li>
</ul>
<p>So now I no longer explicitly release anything. <em>UnloadTexture</em> always chooses the texture to unload that was least-recently used (a linked list makes this a very fast and efficient check &#8211; no timers or array scanning involved). In practise &#8220;least recently used&#8221; turns out to be about 97% optimal (when simulated against a 100% optimal algorithm which is permitted to see the future when deciding what to unload) so it&#8217;s virtually perfect &#8211; substantially better than my explicit declarations were. </p>
<p>If for some crazy reason the user doesn&#8217;t want Icefall consuming all of their video RAM*, I can chuck in a call to <em>IDirect3DDevice9.GetAvailableTextureMem</em> and trigger <em>UnloadTexture</em> if it&#8217;s below some specific amount. Alternatively, I could keep a sum of the amount of texture memory I&#8217;m using, and trigger <em>UnloadTexture</em> if that amount threatens to exceed 32MB or whatever (I haven&#8217;t decided yet).</p>
<p>The point is, if you find yourself manually balancing resources, it&#8217;s probably an excellent idea to profile your resource usage and see if you can find a pattern that will let you just automate the whole thing**. You&#8217;ll save yourself much time and your code will be cleaner and more flexible.</p>
<p>*Note: this only actually matters on Vista or Windows 7. Under the Windows XP model, multiple applications can&#8217;t share texture memory anyway. As soon as an application takes focus, DirectX invalidates all texture resources belonging to everything else (forcing them to reload from system-memory or disk when they get restored). The WDDM (Windows Display Driver Model) in Vista &#8220;understands&#8221; video memory, and can share it amongst applications. This is one of the reasons DirectX 10+ can&#8217;t be backported to XP: XP has no concept of video card resources.</p>
<p>**If you&#8217;re creating a game frame-rate locked or just frame-rate dependent, you might still need to explicitly acquire and release resources (e.g. between levels), because a 10ms pause at an unexpected point in your game could be noticeable or detrimental. Either that, or move the resource acquistion to another thread and have low-quality &#8216;emergency&#8217; textures to display while the real ones load&#8230; this is a very common technique for FPS games. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.paradicesoftware.com/blog/index.php/2009/11/more-textures-than-memory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scary Windows development stories</title>
		<link>http://www.paradicesoftware.com/blog/index.php/2009/11/scary-windows-development-stories/</link>
		<comments>http://www.paradicesoftware.com/blog/index.php/2009/11/scary-windows-development-stories/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 08:44:20 +0000</pubDate>
		<dc:creator>Lawrence</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Good coding guidelines]]></category>
		<category><![CDATA[Taxes]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.paradicesoftware.com/blog/?p=148</guid>
		<description><![CDATA[I&#8217;m currently reading a book about the evolution of Windows. One of the most interesting things I keep finding is how stupid some 3rd party application developers must have been&#8230;. for example, it seems to have been common practice that if you wanted your app to grab a particular Windows setting (let&#8217;s say the Fonts [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently reading a book about the evolution of Windows. One of the most interesting things I keep finding is how stupid some 3rd party application developers must have been&#8230;. for example, it seems to have been common practice that if you wanted your app to grab a particular Windows setting (let&#8217;s say the Fonts directory), instead of a quick MSDN search, which turns up both the <em>SHGetFolderPath</em> and <em>SHGetKnownFolderPath</em> functions (either of which is exactly what you want), these programmers fired up RegEdit and searched for the path of their own Fonts folder&#8230; the first key they found, they used as the parameter for a call to  <em>RegOpenKeyEx</em> and they read the value that way.</p>
<p>To me, that is not only a frighteningly ignorant and dangerous way to develop software (you&#8217;re not going to discover caveats &#038; usage notes by browsing RegEdit!) but <strong>of course</strong> this is not the supported way to do it (how could any sane developer think otherwise?), and <strong>of course</strong> that app would have broken immediately on any new of Windows had MS not added a compatibility shim to watch for that program checking that buggy key, and substituting in a quick call to obtain the real value&#8230;</p>
<p>This isn&#8217;t backyard programmers writing in VBA in Excel to accomplish some one-off business task, either.. it&#8217;s commercial software that did this, written by professional developers and sold to unsuspecting customers for big cash. Scary stuff.</p>
<p>The only question worth pondering here is: was this &#8216;grab something promising out of the Registry&#8217; technique better or worse than just hard-coding &#8220;C:\Windows\Fonts\&#8221; into the application? (I&#8217;m not 100% sure but it seems very likely there&#8217;s a shim out there for programs that did that, too.) </p>
]]></content:encoded>
			<wfw:commentRss>http://www.paradicesoftware.com/blog/index.php/2009/11/scary-windows-development-stories/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
