<?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>RedBrain @ $HOME : ~ $ &#187; doom</title>
	<atom:link href="http://redbrain.co.uk/index.php/tag/doom/feed/" rel="self" type="application/rss+xml" />
	<link>http://redbrain.co.uk</link>
	<description>Frustrated Software Developer</description>
	<lastBuildDate>Wed, 16 Nov 2011 17:36:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Parsing the Doom Wad File</title>
		<link>http://redbrain.co.uk/index.php/random/parsing-the-doom-wad-file/</link>
		<comments>http://redbrain.co.uk/index.php/random/parsing-the-doom-wad-file/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 01:12:31 +0000</pubDate>
		<dc:creator>redbrain</dc:creator>
				<category><![CDATA[Random]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[c/c++]]></category>
		<category><![CDATA[Crules]]></category>
		<category><![CDATA[doom]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://redbrain.co.uk/?p=528</guid>
		<description><![CDATA[<p>So it was a fine Christmas, I was at home with family armed only with 2 seasons of xFiles and my laptop added to the factI didn&#8217;t upload my ssh key to my laptop so hacking on Crules was out the window. So it got me thinking on the main goals for my Crules Scripting [...]]]></description>
			<content:encoded><![CDATA[<p>So it was a fine Christmas, I was at home with family armed only with 2 seasons of xFiles and my laptop added to the factI didn&#8217;t upload my ssh key to my laptop so hacking on Crules was out the window. So it got me thinking on the main goals for my Crules Scripting Language, one of which ( the main one ) is that it should be embeddable into existing C/C++ applications like Lua or Python or even C# (mono) if your using its recently added reflection api!</p>
<p>So why not make a game engine using my language with crules as a scripting language so I can see what the API _SHOULD_ look like for the client. So i choose doom since i am terrible with graphics, i can use the doom.wad and concentrate on programming; So in the past i have written two separate 2D game engines one was a Java 2D <a href="http://en.wikipedia.org/wiki/Monkey_Island_%28series%29">Monkey Island</a> clone i done for university and it was terrible since Java is a terrible platform in my opinion, then my 2nd was a port  of it over to <a href="http://www.libsdl.org/">C++/SDL</a> and the code was terrible <img src='http://redbrain.co.uk/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  so don&#8217;t bother asking me about it.</p>
<p>Anyway&#8217;s so the long and short i am writing doom engine from scratch, my original idea was to work with an existing game engine like <a href="http://www.crystalspace3d.org/main/Main_Page">Crystal Space</a> or <a href="http://dengine.net/">Doomsday Engine</a> though, Crystal Space i was put off since it already uses Python and i don&#8217;t really feel like trawling though lots of code and forking it essentially would be really more work than its worth. Doomsday Engine is the cream of the crop of Doom Ports at the moment, all others are practically the same in essence using the same ancient code and hacks from throughout the 90&#8242;s. And really the technology has changed somewhat since then. Though Doomsday Engine is pretty amazing and works well, it seems to need a massive cleanup and i don&#8217;t mean to be nasty on the developers or anything they have done a fantastic job!  And they have really made the code much cleaner than any other port about! Plus the lead developer has been really nice to me <img src='http://redbrain.co.uk/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . Anyway&#8217;s Games have always been a great passion to me and really doom can&#8217;t be that hard to re-implement from scratch and really it isn&#8217;t i already have a basic game engine working using OpenGL and <a href="http://www.libsdl.org/">SDL{ _ttf, _mixer }</a>. With audio, and fps counter and input bla bla. But i am just concentrating on differentiating the wad sprite formats etc while revising for exams and working on crules and many other bits and pieces. So in the end  i&#8217;ll just stick in an extract of the log of my game engine when it starts parsing the doom.wad and explain a little on how to parse the wad!</p>
<blockquote><p>debug: main.c:28 -&gt; Trying to start crldoom wad: data/doom.wad!<br />
debug: doom.c:147 -&gt; parsing wad file &lt;data/doom.wad&gt;!<br />
debug: doom.c:80 -&gt; is a wad file :: &lt;IWAD&gt;!<br />
log: doom.c:155 -&gt; wad file data/doom.wad has length 12408292!<br />
debug: doom.c:27 -&gt; debug seeking&#8230;<br />
debug: doom.c:30 -&gt; reading&#8230;.<br />
debug: doom.c:34 -&gt; parsing&#8230;<br />
log: doom.c:49 -&gt; header:: ident -&gt; 1145132873, n_lumps -&gt; 2306, infotableofs -&gt; 12371396!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 12, size -&gt; 10752, name -&gt; 1497451600, ident -&gt; PLAYPAL!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 10764, size -&gt; 8704, name -&gt; 1330401091, ident -&gt; COLORMAP!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 19468, size -&gt; 4000, name -&gt; 1329876549, ident -&gt; ENDOOM!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 23468, size -&gt; 6854, name -&gt; 1330464068, ident -&gt; DEMO1!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 30324, size -&gt; 9402, name -&gt; 1330464068, ident -&gt; DEMO2!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 39728, size -&gt; 15466, name -&gt; 1330464068, ident -&gt; DEMO3!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 55196, size -&gt; 3286, name -&gt; 1330464068, ident -&gt; DEMO4!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 58484, size -&gt; 0, name -&gt; 827142469, ident -&gt; E1M1!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 58484, size -&gt; 1430, name -&gt; 1313425492, ident -&gt; THINGS!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 59916, size -&gt; 6804, name -&gt; 1162758476, ident -&gt; LINEDEFS!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 66720, size -&gt; 19980, name -&gt; 1162103123, ident -&gt; SIDEDEFS!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 86700, size -&gt; 1880, name -&gt; 1414677846, ident -&gt; VERTEXES!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 88580, size -&gt; 8964, name -&gt; 1397179731, ident -&gt; SEGS!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 97544, size -&gt; 956, name -&gt; 1128616787, ident -&gt; SSECTORS!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 98500, size -&gt; 6664, name -&gt; 1162104654, ident -&gt; NODES!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 105164, size -&gt; 2288, name -&gt; 1413694803, ident -&gt; SECTORS!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 107452, size -&gt; 968, name -&gt; 1162495314, ident -&gt; REJECT!<br />
log: doom.c:62 -&gt; directory:: filepos -&gt; 108420, size -&gt; 6948, name -&gt; 1129270338, ident -&gt; BLOCKMAP!</p></blockquote>
<p>So this is just some of the output of the code i had written it parses out whats called the &#8216;directories&#8217; in the file and then i also have functions to parse out whats called the &#8216;lumps&#8217; from these directories the lumps are the actual data. I&#8217;ll explain more detail when i talk about how to parse this. And how to convert the music lumps to proper midi so you can play it in SDL_mixer or even in Totem if in in gnome.</p>
<p>So what do we need, if you have yourself a Doom.wad file lying about i am sure if you go through your old boxes you&#8217;ll find yourself a copy of doom, or if you cant find it you can find it on torrent sites but you didn&#8217;t read that here <img src='http://redbrain.co.uk/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . Thing is I have bought the game like 3/4 times I am sure hehe. Lets get started.</p>
<p>In the Doom wad there is whats called the &#8216;HEADER&#8217; which is of length 12 bytes which contains 3 * 4-byte integers which contain the data we care about, so lets do that lets make some code (i just wrote this code in like 10 min to illustrate the idea its ok does the job!):</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;stdio.h&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;stdlib.h&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;string.h&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="co2">#define WAD_HEADER_LENGTH &nbsp; &nbsp; 12</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">#define WAD_DIRECTORY_LENGTH &nbsp;16</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">unsigned</span> <span class="kw4">long</span> parse_int<span class="br0">&#40;</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> *p <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">return</span> <span class="br0">&#40;</span> <span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">long</span><span class="br0">&#41;</span> &nbsp; p<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| <span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">long</span><span class="br0">&#41;</span> p<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> &lt;&lt; <span class="nu0">8</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| <span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">long</span><span class="br0">&#41;</span> p<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> &lt;&lt; <span class="nu0">16</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| <span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">long</span><span class="br0">&#41;</span> p<span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span> &lt;&lt; <span class="nu0">24</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> main<span class="br0">&#40;</span> <span class="kw4">int</span> argc, <span class="kw4">char</span> *argv<span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">const</span> <span class="kw4">char</span>* wad_file= <span class="st0">&quot;/home/redbrain/workspace/doom-dev/crldoom/data/doom.wad&quot;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; FILE* wad_fd;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> !<span class="br0">&#40;</span>wad_fd= fopen<span class="br0">&#40;</span>wad_file, <span class="st0">&quot;rb&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; fprintf<span class="br0">&#40;</span>stderr, <span class="st0">&quot;error opening &lt;%s&gt;!<span class="es0">\n</span>&quot;</span>, wad_file<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> EXIT_FAILURE;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">unsigned</span> <span class="kw4">char</span> header_buffer<span class="br0">&#91;</span> WAD_HEADER_LENGTH <span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; fread<span class="br0">&#40;</span> header_buffer, WAD_HEADER_LENGTH, <span class="nu0">1</span>, wad_fd <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw4">char</span>* wad_ident= <span class="br0">&#40;</span><span class="kw4">char</span>*<span class="br0">&#41;</span> header_buffer;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; wad_ident<span class="br0">&#91;</span> <span class="nu0">4</span> <span class="br0">&#93;</span>= <span class="st0">&#8216;<span class="es0">\0</span>&#8216;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> strncmp<span class="br0">&#40;</span> wad_ident, <span class="st0">&quot;IWAD&quot;</span>, <span class="nu0">4</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; fprintf<span class="br0">&#40;</span>stderr, <span class="st0">&quot;invalid wad header type &lt;%s&gt;!<span class="es0">\n</span>&quot;</span>, wad_ident <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> EXIT_FAILURE;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&quot;doom wad is a &lt;%s&gt;!<span class="es0">\n</span>&quot;</span>, wad_ident <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">unsigned</span> <span class="kw4">long</span> wad_length= <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; fseek<span class="br0">&#40;</span> wad_fd, <span class="nu0">0</span>, SEEK_END <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; wad_length= ftell<span class="br0">&#40;</span> wad_fd <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">unsigned</span> <span class="kw4">long</span> directory_offset= parse_int<span class="br0">&#40;</span> header_buffer<span class="nu0">+8</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">unsigned</span> <span class="kw4">long</span> number_lumps= parse_int<span class="br0">&#40;</span> header_buffer<span class="nu0">+4</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&quot;wad directory offset &lt;%lu&gt; with &lt;%lu&gt; lumps!<span class="es0">\n</span>&quot;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp;directory_offset, number_lumps <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">unsigned</span> <span class="kw4">long</span> t_ofs= directory_offset; <span class="kw4">unsigned</span> <span class="kw4">long</span> lump_count= <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">while</span><span class="br0">&#40;</span> t_ofs &lt;= <span class="br0">&#40;</span> wad_length -- WAD_DIRECTORY_LENGTH <span class="br0">&#41;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; fseek<span class="br0">&#40;</span> wad_fd, t_ofs, SEEK_SET <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw4">unsigned</span> <span class="kw4">char</span> *directory_buffer= <span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">char</span>*<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; malloc<span class="br0">&#40;</span> <span class="kw4">sizeof</span><span class="br0">&#40;</span><span class="kw4">char</span><span class="br0">&#41;</span> * WAD_DIRECTORY_LENGTH <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; fread<span class="br0">&#40;</span> directory_buffer, WAD_DIRECTORY_LENGTH, <span class="nu0">1</span>, wad_fd <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="kw4">unsigned</span> <span class="kw4">long</span> filepos= parse_int<span class="br0">&#40;</span> directory_buffer <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw4">unsigned</span> <span class="kw4">long</span> size= parse_int<span class="br0">&#40;</span> directory_buffer<span class="nu0">+4</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw4">char</span> *directory_ident= strdup<span class="br0">&#40;</span> <span class="br0">&#40;</span><span class="kw4">char</span>*<span class="br0">&#41;</span> directory_buffer<span class="nu0">+8</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; directory_ident<span class="br0">&#91;</span> strlen<span class="br0">&#40;</span>directory_ident<span class="br0">&#41;</span> <span class="br0">&#93;</span>= <span class="st0">&#8216;<span class="es0">\0</span>&#8216;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&quot;directory name &lt;%s&gt; at offset &lt;%lu&gt; with size &lt;%lu&gt;!<span class="es0">\n</span>&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;directory_ident, filepos, size <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; free<span class="br0">&#40;</span> directory_ident <span class="br0">&#41;</span>; free<span class="br0">&#40;</span> directory_buffer <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; t_ofs += WAD_DIRECTORY_LENGTH; lump_count++;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&quot;directorys parsed &lt;%lu&gt; total lumps &lt;%lu&gt;!<span class="es0">\n</span>&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;lump_count, number_lumps <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; fclose<span class="br0">&#40;</span> wad_fd <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>So lets compile and test this code &lt;remember to change the path to your doom wad&gt;:</p>
<blockquote><p>gcc doom_wad_test.c</p>
<p>./a.out</p></blockquote>
<p>So right what does any of this code mean i just chucked it up there, i wrote it up very quickly from scratch. So as i stated earlier there is a wad header which is a 12 bytes in length which i refer to as &#8216;WAD_HEADER_LENGTH&#8217; which contains 3 * 4-byte integers. So from the code we have fopen&#8217;d the wad_file and i use fread to read in the 12 bytes in a 12 byte &#8216;unsigned char[]&#8216; buffer. So to read out the integers in a neutral endian way i found a nice parse_int function in some of the wad documentation i found and in old doom source code which was GPL so it was like&#8230;. yoink&#8230; but its pretty easy to understand if you know your representations well enough but i wont go into detail.</p>
<p>The first integer is the IWAD or PWAD string, IWAD is what you will find mostly its the deployment version of the wad, the PWAD is a patch wad and very few of them are still around and i think it has similar structure. Anyways, you parse out the first integer like this</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">char</span>* wad_ident= <span class="br0">&#40;</span><span class="kw4">char</span>*<span class="br0">&#41;</span> header_buffer;</div>
</li>
<li class="li1">
<div class="de1">wad_ident<span class="br0">&#91;</span> <span class="nu0">4</span> <span class="br0">&#93;</span>= <span class="st0">&#8216;<span class="es0">\0</span>&#8216;</span>;</div>
</li>
</ol>
</div>
<p>Remember to add null string &#8216;\0&#8242;, so we don&#8217;t get buffer overflow! This string is always a length of 4 so no need to mess about! And then the next integer is the number of lumps in the wad the lumps are the actual binary data like music and graphics. Third int is the &#8216;directory offset&#8217; which contains pointers to where the lumps are their idents and the size of the lumps which have lengths of 16 &#8216;WAD_DIRECTORY_LENGTH&#8217; until the end of the wad file! so note i find the length of the file and do:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">while</span><span class="br0">&#40;</span> t_ofs &lt;= <span class="br0">&#40;</span> wad_length -- WAD_DIRECTORY_LENGTH <span class="br0">&#41;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&#8230;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>So how do we parse out these directories?  We move to the directory offset and then read the 16 bytes into a buffer, which is 4 integers this time. But still contains only 3 things means the ident strings can be longer or can be with some padding between each directory lump.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">unsigned</span> <span class="kw4">long</span> filepos= parse_int<span class="br0">&#40;</span> directory_buffer <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">unsigned</span> <span class="kw4">long</span> size= parse_int<span class="br0">&#40;</span> directory_buffer<span class="nu0">+4</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">char</span> *directory_ident= strdup<span class="br0">&#40;</span> <span class="br0">&#40;</span><span class="kw4">char</span>*<span class="br0">&#41;</span> directory_buffer<span class="nu0">+8</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">directory_ident<span class="br0">&#91;</span> strlen<span class="br0">&#40;</span>directory_ident<span class="br0">&#41;</span> <span class="br0">&#93;</span>= <span class="st0">&#8216;<span class="es0">\0</span>&#8216;</span>;</div>
</li>
</ol>
</div>
<p>Now we know the file offset of the lump the size of the lump and the ident of the lump and we make sure to add the null terminator again to avoid buffer overflow. And then we can parse out the lumps simply just fseek and fread <img src='http://redbrain.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Done its so simple and seems like there would be more to it, yet when you think of it is very much common sense there really isn&#8217;t that many ways to pack data into a file like this! If your interested i&#8217;ll be talking about it soon&#8230; on how to convert the D_E1M* lumps which are music lumps to compatible midi tracks. And finally on how <a href="http://en.wikipedia.org/wiki/Binary_space_partitioning">BSP</a> works for the levels and maybe how to draw them in C/SDL/OpenGL&#8230; if you ask i prefer C over C++, yeah when it comes to building this game engine there have been times i though woo having an object would be really nice here but then i realise well no because that&#8217;s what the scripting engine is going to do&#8230; <img src='http://redbrain.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Mainly due to the fact i dont like C++ syntax it feels messy to me and C is just nice and simple <img src='http://redbrain.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Finally I just want to wrap up with saying <a href="http://en.wikipedia.org/wiki/John_D._Carmack">John Carmack</a> has to be one of my biggest Hero&#8217;s in Computer Science along with <a href="http://tirania.org/blog/">Migel De Icaza</a>, <a href="http://en.wikipedia.org/wiki/Linus_Torvalds">Linus Torvalds</a>, Various GCC hackers&#8230; like Ian Lance Taylor But John Carmack in particular since he made Doom! But also he attended the <a title="University of Missouri–Kansas City" href="http://en.wikipedia.org/wiki/University_of_Missouri%E2%80%93Kansas_City">University of Missouri–Kansas City</a> for two semesters before withdrawing to work as a freelance programmer. Which i think that is brilliant! Since University does not teach students to be serious programmers or technology well very few do anyway and i think its great he had the balls to do that and so did Migel De Icaza and they have both done some of the most amazing things in computer science!</p>
<p>Anyways if your interested in Crules or CrlDoom see: <a href="http://crules.org">http://crules.org</a> code for crldoom is over @ <a href="http://code.redbrain.co.uk/cgit.cgi/crldoom">http://code.redbrain.co.uk/cgit.cgi/crldoom</a></p>
<p>This is a project i love <a href="http://www.moddb.com/mods/classic-doom-3">Classic Doom 3</a> it was Doom re-implemented ontop of the Doom3 Engine what more do you want!!!</p>
<p><!-- Smart Youtube --><span class="youtube"><object width="425" height="373"><param name="movie" value="http://www.youtube.com/v/woBAUk5hMD8&amp;rel=1&amp;color1=3a3a3a&amp;color2=999999&amp;border=1&amp;fs=1&amp;hl=en&amp;autoplay=0&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0" /><param name="allowFullScreen" value="true" /><embed wmode="transparent" src="http://www.youtube.com/v/woBAUk5hMD8&amp;rel=1&amp;color1=3a3a3a&amp;color2=999999&amp;border=1&amp;fs=1&amp;hl=en&amp;autoplay=0&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="373" ></embed><param name="wmode" value="transparent" /></object></span></p>
]]></content:encoded>
			<wfw:commentRss>http://redbrain.co.uk/index.php/random/parsing-the-doom-wad-file/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

