<?xml version="1.0" encoding="iso-8859-1"?><!-- generator="b2evolution/4.1.3" -->
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Crypton's Blog</title>
		<link>http://crypton.game-alive.com/</link>
		<atom:link rel="self" type="application/rss+xml" href="http://crypton.game-alive.com/?tempskin=_rss2" />
		<description></description>
		<language>en-US</language>
		<docs>http://blogs.law.harvard.edu/tech/rss</docs>
		<admin:generatorAgent rdf:resource="http://b2evolution.net/?v=4.1.3"/>
		<ttl>60</ttl>
				<item>
			<title>4 months...</title>
			<link>http://crypton.game-alive.com/2012/05/5-months</link>
			<pubDate>Wed, 16 May 2012 04:22:00 +0000</pubDate>			<dc:creator>Crypton</dc:creator>
			<category domain="main">Background</category>			<guid isPermaLink="false">53@http://crypton.game-alive.com/</guid>
						<description>&lt;p&gt;It&#039;s been exactly 4 months since my last post, yet I feel like it was just yesterday. Well, a lots of things happened since then, and here is a brief overview of personal stuff, if anyone is interested:&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;-I moved out of home into new apartment. It took me almost two months to find it, the rent is quite high, but the place it&#039;s really worth the money. Finally, I have some own space, privacy and equanimity, so I can work on my projects without being disturbed all the time. Hooray! :)&lt;br /&gt;&lt;br /&gt;-I finally decided to visit my doctor for a medical check-up. I had last one like 10 years ago, so I was a bit afraid of the results. Well, apparently I should be perfectly healthy, as the results of all tests were okay.&lt;br /&gt;&lt;br /&gt;-I found my lost uncle that I haven&#039;t seen for about 20 years. I was five when my father died, but at that time, I was living in a children&#039;s house and I never seen anyone from father&#039;s side of family again since I returned to home.&amp;#160; Unfortunately, I found out that he is the last living member of that family, as my grandparents passed away a long time ago, and the rest of the family as well.&amp;#160; Well, I also found out that my father and my grandparents weren&#039;t buried, thanks to economy situation at that time&amp;#160; (post-communism era). I think that they deserved better, so I&#039;m going to start saving money for a joint/family grave, so they can be buried and be together once again. Anyway, my uncle gave me some new-found photos of my father and my grandparents, so I&#039;m really glad that I found him afterall.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;-I&#039;m currently trying to finish my thesis and prepare myself for a final exams. I already have the practical part of the thesis finished, but I still have to write at least 40 pages for it, which will be the hardest part for me. Well, I still have a month left to manage that (the dealine is July 6, exams starts in August), so I hope that I will be able to finish it in time. It&#039;s my very last chance, and if I don&#039;t finish the thesis or pass the exams, then I&#039;m not going to get my degree. No retakes! :(&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;That&#039;s it. I probably forgot to mention something un/important, so I&#039;ll update this post later :(&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://crypton.game-alive.com/2012/05/5-months&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>It's been exactly 4 months since my last post, yet I feel like it was just yesterday. Well, a lots of things happened since then, and here is a brief overview of personal stuff, if anyone is interested:</p>
<p><br />-I moved out of home into new apartment. It took me almost two months to find it, the rent is quite high, but the place it's really worth the money. Finally, I have some own space, privacy and equanimity, so I can work on my projects without being disturbed all the time. Hooray! :)<br /><br />-I finally decided to visit my doctor for a medical check-up. I had last one like 10 years ago, so I was a bit afraid of the results. Well, apparently I should be perfectly healthy, as the results of all tests were okay.<br /><br />-I found my lost uncle that I haven't seen for about 20 years. I was five when my father died, but at that time, I was living in a children's house and I never seen anyone from father's side of family again since I returned to home.&#160; Unfortunately, I found out that he is the last living member of that family, as my grandparents passed away a long time ago, and the rest of the family as well.&#160; Well, I also found out that my father and my grandparents weren't buried, thanks to economy situation at that time&#160; (post-communism era). I think that they deserved better, so I'm going to start saving money for a joint/family grave, so they can be buried and be together once again. Anyway, my uncle gave me some new-found photos of my father and my grandparents, so I'm really glad that I found him afterall.</p>
<p><br />-I'm currently trying to finish my thesis and prepare myself for a final exams. I already have the practical part of the thesis finished, but I still have to write at least 40 pages for it, which will be the hardest part for me. Well, I still have a month left to manage that (the dealine is July 6, exams starts in August), so I hope that I will be able to finish it in time. It's my very last chance, and if I don't finish the thesis or pass the exams, then I'm not going to get my degree. No retakes! :(</p>
<p><br />That's it. I probably forgot to mention something un/important, so I'll update this post later :(</p><div class="item_footer"><p><small><a href="http://crypton.game-alive.com/2012/05/5-months">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://crypton.game-alive.com/2012/05/5-months#comments</comments>
			<wfw:commentRss>http://crypton.game-alive.com/?tempskin=_rss2&#38;disp=comments&#38;p=53</wfw:commentRss>
		</item>
				<item>
			<title>R.I.P.</title>
			<link>http://crypton.game-alive.com/2012/01/r-i-p</link>
			<pubDate>Mon, 16 Jan 2012 23:06:00 +0000</pubDate>			<dc:creator>Crypton</dc:creator>
			<category domain="main">Background</category>			<guid isPermaLink="false">52@http://crypton.game-alive.com/</guid>
						<description>&lt;p&gt;My dog just passed away this morning, due long and exhausting fight against cancer. She was already 12 years old, yet still had so much life in her, but unfortunately she also had many health problems, and there was no hope she would get better any soon, so we had to put her to sleep. I&#039;ve seen many dogs and other animals in my life, and that&#039;s why I can say that she was truly unique, clever and humane dog, and she was like a sister to me.&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://crypton.game-alive.com/media/blogs/a/P1000676_M.jpg&quot; alt=&quot;Besina&quot; width=&quot;608&quot; height=&quot;456&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;She is truly irreplaceable, and I&#039;m not going to replace her, so this was my very last pet I ever had.&lt;/p&gt;
&lt;p&gt;Rest in peace, my beloved friend... and hold on there, we will see each other again one day...&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://crypton.game-alive.com/2012/01/r-i-p&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>My dog just passed away this morning, due long and exhausting fight against cancer. She was already 12 years old, yet still had so much life in her, but unfortunately she also had many health problems, and there was no hope she would get better any soon, so we had to put her to sleep. I've seen many dogs and other animals in my life, and that's why I can say that she was truly unique, clever and humane dog, and she was like a sister to me.</p>
<p>&#160;</p>
<p><img src="http://crypton.game-alive.com/media/blogs/a/P1000676_M.jpg" alt="Besina" width="608" height="456" /></p>
<p>&#160;</p>
<p>She is truly irreplaceable, and I'm not going to replace her, so this was my very last pet I ever had.</p>
<p>Rest in peace, my beloved friend... and hold on there, we will see each other again one day...</p>
<p>&#160;</p><div class="item_footer"><p><small><a href="http://crypton.game-alive.com/2012/01/r-i-p">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://crypton.game-alive.com/2012/01/r-i-p#comments</comments>
			<wfw:commentRss>http://crypton.game-alive.com/?tempskin=_rss2&#38;disp=comments&#38;p=52</wfw:commentRss>
		</item>
				<item>
			<title>DxEmu - Preview</title>
			<link>http://crypton.game-alive.com/2011/12/dxemu-preview</link>
			<pubDate>Thu, 29 Dec 2011 13:39:00 +0000</pubDate>			<dc:creator>Crypton</dc:creator>
			<category domain="main">ArcanumAlive</category>			<guid isPermaLink="false">51@http://crypton.game-alive.com/</guid>
						<description>&lt;p&gt;Just to remind you, DxEmu (formely DDrawEmu), is the DirectDraw/X emulator I&#039;m currently working on. I&#039;ve managed to implement the OpenGL-based renderer, and here is first preview:&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://img80.imageshack.us/img80/4963/dxemuarcanum2.png&quot; alt=&quot;&quot; width=&quot;544&quot; height=&quot;438&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://i42.tinypic.com/2ywhimg.jpg&quot; alt=&quot;DxEmu Preview&quot; width=&quot;544&quot; height=&quot;425&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;It&#039;s only a two screenshots of main menu, but as you can see, it&#039;s running in windowed mode, which is one of the most requested mods for fullscreen-only games, like Arcanum :) It&#039;s also possible (thru config file) to force any windowed game to run in fullscreen, and force display resolution, window geometry, etc.&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;Now I&#039;m going to implement support for palettes, color keys and other misc stuff. Hopefully, I&#039;ll have it finished before end of this week.&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;Edit: Open those screenshots in new window, if you want to see them in full resolution.&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://crypton.game-alive.com/2011/12/dxemu-preview&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Just to remind you, DxEmu (formely DDrawEmu), is the DirectDraw/X emulator I'm currently working on. I've managed to implement the OpenGL-based renderer, and here is first preview:</p>
<p>&#160;</p>
<p><img src="http://img80.imageshack.us/img80/4963/dxemuarcanum2.png" alt="" width="544" height="438" /></p>
<p>&#160;</p>
<p><img src="http://i42.tinypic.com/2ywhimg.jpg" alt="DxEmu Preview" width="544" height="425" /></p>
<p>&#160;</p>
<p>It's only a two screenshots of main menu, but as you can see, it's running in windowed mode, which is one of the most requested mods for fullscreen-only games, like Arcanum :) It's also possible (thru config file) to force any windowed game to run in fullscreen, and force display resolution, window geometry, etc.</p>
<p>&#160;</p>
<p>Now I'm going to implement support for palettes, color keys and other misc stuff. Hopefully, I'll have it finished before end of this week.</p>
<p>&#160;</p>
<p>Edit: Open those screenshots in new window, if you want to see them in full resolution.</p>
<p>&#160;</p><div class="item_footer"><p><small><a href="http://crypton.game-alive.com/2011/12/dxemu-preview">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://crypton.game-alive.com/2011/12/dxemu-preview#comments</comments>
			<wfw:commentRss>http://crypton.game-alive.com/?tempskin=_rss2&#38;disp=comments&#38;p=51</wfw:commentRss>
		</item>
				<item>
			<title>A one huge "bug" in game Arcanum: Of Steamworks and Magick Obscura</title>
			<link>http://crypton.game-alive.com/2011/12/a-one-huge-bug-in</link>
			<pubDate>Tue, 27 Dec 2011 09:50:00 +0000</pubDate>			<dc:creator>Crypton</dc:creator>
			<category domain="main">ArcanumAlive</category>			<guid isPermaLink="false">50@http://crypton.game-alive.com/</guid>
						<description>&lt;p&gt;As you probably already know, this game is using &quot;standard&quot;&amp;#160;256 color palettes, where every color is saved in X8R8G8B8 pixel format. What you probably also know, is that Arcanum is using DirectDraw for the rendering, and there is no support for windowed mode in Arcanum.&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;Anyway, I just found out that there are a huge design flaws regarding the color palettes and DirectDraw usage.&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;First of all, Arcanum isn&#039;t using IDirectDrawPalette interface at all, and all the color index to pixel conversions are done on CPU. This slows down whole level loading process, but that&#039;s still okay, no big deal.&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;Secondly, the game is running in 16bit fullscreen mode. Yes, you read that correctly, 16 bit color depth, not a 24bit! What does it means? It means, that 8 (or even 9 bits) are removed from every color from the color palletes in pixel format conversion, so the range of possible color shades is significantly reduced, from 16 777 216 to 65 536 (or even 32 768), thus the overall graphics quality is reduced significantly as well.&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;It&#039;s 16bit color depth, but it doesn&#039;t mean that it&#039;s R5G6B5 pixel format. The DirectDraw also supports X1R5G5G5 pixel format, and it&#039;s up to driver which pixel format it chooses for 16bit color depth.&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;Anyway, few year back, I made a mod for Arcanum, that forces the game to run in windowed mode, which was disabled in Arcanum in release version, because it wasn&#039;t finished and fully working feature.&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;What&#039;s surprising, is that when the game is running in windowed mode, there are no pixel format conversions, and game uses a full 24 bit color depth range. That&#039;s great, because it means that it&#039;s possible to force the Arcanum to run in 24 bit fullscreen mode.&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;If my time allows it, I&#039;ll fix this problem in January, and release it as .exe patch. I found this issue thanks to my DirectDraw emulator, and hopefully it will be useful for finding bugs in other games as well :)&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://crypton.game-alive.com/2011/12/a-one-huge-bug-in&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>As you probably already know, this game is using "standard"&#160;256 color palettes, where every color is saved in X8R8G8B8 pixel format. What you probably also know, is that Arcanum is using DirectDraw for the rendering, and there is no support for windowed mode in Arcanum.</p>
<p>&#160;</p>
<p>Anyway, I just found out that there are a huge design flaws regarding the color palettes and DirectDraw usage.</p>
<p>&#160;</p>
<p>First of all, Arcanum isn't using IDirectDrawPalette interface at all, and all the color index to pixel conversions are done on CPU. This slows down whole level loading process, but that's still okay, no big deal.</p>
<p>&#160;</p>
<p>Secondly, the game is running in 16bit fullscreen mode. Yes, you read that correctly, 16 bit color depth, not a 24bit! What does it means? It means, that 8 (or even 9 bits) are removed from every color from the color palletes in pixel format conversion, so the range of possible color shades is significantly reduced, from 16 777 216 to 65 536 (or even 32 768), thus the overall graphics quality is reduced significantly as well.</p>
<p>&#160;</p>
<p>It's 16bit color depth, but it doesn't mean that it's R5G6B5 pixel format. The DirectDraw also supports X1R5G5G5 pixel format, and it's up to driver which pixel format it chooses for 16bit color depth.</p>
<p>&#160;</p>
<p>Anyway, few year back, I made a mod for Arcanum, that forces the game to run in windowed mode, which was disabled in Arcanum in release version, because it wasn't finished and fully working feature.</p>
<p>&#160;</p>
<p>What's surprising, is that when the game is running in windowed mode, there are no pixel format conversions, and game uses a full 24 bit color depth range. That's great, because it means that it's possible to force the Arcanum to run in 24 bit fullscreen mode.</p>
<p>&#160;</p>
<p>If my time allows it, I'll fix this problem in January, and release it as .exe patch. I found this issue thanks to my DirectDraw emulator, and hopefully it will be useful for finding bugs in other games as well :)</p><div class="item_footer"><p><small><a href="http://crypton.game-alive.com/2011/12/a-one-huge-bug-in">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://crypton.game-alive.com/2011/12/a-one-huge-bug-in#comments</comments>
			<wfw:commentRss>http://crypton.game-alive.com/?tempskin=_rss2&#38;disp=comments&#38;p=50</wfw:commentRss>
		</item>
				<item>
			<title>DirectDraw vs Old Games</title>
			<link>http://crypton.game-alive.com/2011/12/directdraw-vs-old-games</link>
			<pubDate>Sun, 11 Dec 2011 23:06:00 +0000</pubDate>			<dc:creator>Crypton</dc:creator>
			<category domain="main">Projects</category>			<guid isPermaLink="false">49@http://crypton.game-alive.com/</guid>
						<description>&lt;p&gt;Have you ever tried to play any DirectDraw based game on Win7? If you haven&#039;t experienced any visual glitches or even crashes, then you&#039;re a lucky guy, because those games often don&#039;t work on newer systems (Vista+).&lt;br /&gt;&lt;br /&gt;Call me paranoid, but it&#039;s possible that the support for DirectDraw is going to be dropped completely in near future (Win8?), and provided compatibility modes often don&#039;t work as they&#039;re supposed, so it&#039;s on us - fans of those games - to fix all the problems, so we can enjoy playing those games forever and ever.&lt;br /&gt;&lt;br /&gt;Anyway, why I&#039;m talking about this? Recently, a friend of mine, has recommended me a game called Wing Commander. I was aware of this game, but I never played it before. So I decided to buy this game, third episode to be exact, as I didn&#039;t liked the graphics of previous episodes (judging from the screenshots).&lt;br /&gt;&lt;br /&gt;What was surprising is that the game is running in DosBox. After a bit of searching, I&#039;ve found out that it&#039;s mainly because of DirectDraw related problems.&lt;br /&gt;&lt;br /&gt;What I also found is a &lt;a href=&quot;http://sol.gfxile.net/ddhack/&quot;&gt;webpage&lt;/a&gt;, which is dedicated to fixing DirectDraw related problems in the Wing Commander series. The author of that website - &lt;a href=&quot;http://sol.gfxile.net/who.html&quot;&gt;Jari Komppa&lt;/a&gt; (AKA sol), also wrote a library as replacement for DirectDraw, to fix all the problems.&lt;br /&gt;&lt;br /&gt;I&#039;ve noticed that the guy has already abandoned the development of that library, and made source code public, and because I really liked the idea of such library, as I also know a few games that don&#039;t work properly on Win7 (e.g. Arcanum and it&#039;s WorldEd), I&#039;ve decided to continue with the development.&lt;br /&gt;&lt;br /&gt;Well, I was very enthusiastic about it, until I looked at the source code. It was really hacky for my taste, and I didn&#039;t liked a lot of design-related decisions in it (I&#039;m really such perfectionist :)).&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;So after that, I rather decided to write my own library, which will be able not only to log all the calls, parameters and other stuff, but it will be also able to redirect all calls to original library, so it&#039;ll serve also as proxy library. Most importantly, it will be also able to emulate all the DirectDraw features.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;That was the plan, but after two weeks working on it (well, in free time only), the only thing I&#039;ve managed to implement is - all the DirectDraw interfaces and it&#039;s versions&amp;#160; (i.e. DirectDraw/Surface/Clipper/Palette - 1-7) , proxy calls and functions/methods &amp;amp; parameters logging. It&#039;s kinda powerful tool already, but something important is missing...&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;What&#039;s missing? the emulation part :) on which I just started working. I chose Direct3D9 for the rendering and stuff, but if I manage to make everything working, I&#039;ll also add support for OpenGL, if needed.&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;Anyway, I&#039;m testing the library on Arcanum, and you can see the debug output here: &lt;a href=&quot;http://pastebin.me/2582666d95f3f39b81a49edb8d00aee0&quot;&gt;http://pastebin.me/2582666d95f3f39b81a49edb8d00aee0&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;Kinda cool, right? I call this library DDrawEmu :)&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://crypton.game-alive.com/2011/12/directdraw-vs-old-games&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Have you ever tried to play any DirectDraw based game on Win7? If you haven't experienced any visual glitches or even crashes, then you're a lucky guy, because those games often don't work on newer systems (Vista+).<br /><br />Call me paranoid, but it's possible that the support for DirectDraw is going to be dropped completely in near future (Win8?), and provided compatibility modes often don't work as they're supposed, so it's on us - fans of those games - to fix all the problems, so we can enjoy playing those games forever and ever.<br /><br />Anyway, why I'm talking about this? Recently, a friend of mine, has recommended me a game called Wing Commander. I was aware of this game, but I never played it before. So I decided to buy this game, third episode to be exact, as I didn't liked the graphics of previous episodes (judging from the screenshots).<br /><br />What was surprising is that the game is running in DosBox. After a bit of searching, I've found out that it's mainly because of DirectDraw related problems.<br /><br />What I also found is a <a href="http://sol.gfxile.net/ddhack/">webpage</a>, which is dedicated to fixing DirectDraw related problems in the Wing Commander series. The author of that website - <a href="http://sol.gfxile.net/who.html">Jari Komppa</a> (AKA sol), also wrote a library as replacement for DirectDraw, to fix all the problems.<br /><br />I've noticed that the guy has already abandoned the development of that library, and made source code public, and because I really liked the idea of such library, as I also know a few games that don't work properly on Win7 (e.g. Arcanum and it's WorldEd), I've decided to continue with the development.<br /><br />Well, I was very enthusiastic about it, until I looked at the source code. It was really hacky for my taste, and I didn't liked a lot of design-related decisions in it (I'm really such perfectionist :)).</p>
<p>&#160;</p>
<p>So after that, I rather decided to write my own library, which will be able not only to log all the calls, parameters and other stuff, but it will be also able to redirect all calls to original library, so it'll serve also as proxy library. Most importantly, it will be also able to emulate all the DirectDraw features.</p>
<p><br />That was the plan, but after two weeks working on it (well, in free time only), the only thing I've managed to implement is - all the DirectDraw interfaces and it's versions&#160; (i.e. DirectDraw/Surface/Clipper/Palette - 1-7) , proxy calls and functions/methods &amp; parameters logging. It's kinda powerful tool already, but something important is missing...</p>
<p>&#160;</p>
<p>What's missing? the emulation part :) on which I just started working. I chose Direct3D9 for the rendering and stuff, but if I manage to make everything working, I'll also add support for OpenGL, if needed.</p>
<p>&#160;</p>
<p>Anyway, I'm testing the library on Arcanum, and you can see the debug output here: <a href="http://pastebin.me/2582666d95f3f39b81a49edb8d00aee0">http://pastebin.me/2582666d95f3f39b81a49edb8d00aee0</a></p>
<p>&#160;</p>
<p>Kinda cool, right? I call this library DDrawEmu :)</p>
<p>&#160;</p><div class="item_footer"><p><small><a href="http://crypton.game-alive.com/2011/12/directdraw-vs-old-games">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://crypton.game-alive.com/2011/12/directdraw-vs-old-games#comments</comments>
			<wfw:commentRss>http://crypton.game-alive.com/?tempskin=_rss2&#38;disp=comments&#38;p=49</wfw:commentRss>
		</item>
				<item>
			<title>Long time no see...</title>
			<link>http://crypton.game-alive.com/2011/12/long-time-no-see</link>
			<pubDate>Sun, 11 Dec 2011 21:08:00 +0000</pubDate>			<dc:creator>Crypton</dc:creator>
			<category domain="main">ArcanumAlive</category>			<guid isPermaLink="false">48@http://crypton.game-alive.com/</guid>
						<description>&lt;p&gt;It&#039;s been a while since I posted here... well almost 5 months :) I was really busy the whole time, and I also (almost completely) forgot about this blog, because of all that busyness.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;Well, guess what... I&#039;m still pretty busy, as I just started to work for two unnamed companies, so I got two jobs, and also I&#039;ll be preparing myself for final exams, for at least next 3 months, because I have to take the exams next semester (I can&#039;t delay them anymore, as I&#039;ve already delayed them for 3 semesters).&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;Why I&#039;m talking about this? Well, I&#039;ve promised to post a long development report for the OpenArcanum project before the end of this year. Also, I&#039;ve promised to fix the source code, and then release at least part of it.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;Unfortunately, it seems that I won&#039;t be able to fulfill all these promises... so I&#039;m really sorry guys! :(&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;I&#039;ll try my best to post at least something, however I was planning to post very detailed information about the engine, plans, and tasks, so it&#039;ll be nothing against what I was initially planning.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;I wish I could split myself into two...&lt;/p&gt;&lt;br /&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://crypton.game-alive.com/2011/12/long-time-no-see&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>It's been a while since I posted here... well almost 5 months :) I was really busy the whole time, and I also (almost completely) forgot about this blog, because of all that busyness.</p><br />

<p>Well, guess what... I'm still pretty busy, as I just started to work for two unnamed companies, so I got two jobs, and also I'll be preparing myself for final exams, for at least next 3 months, because I have to take the exams next semester (I can't delay them anymore, as I've already delayed them for 3 semesters).</p><br />

<p>Why I'm talking about this? Well, I've promised to post a long development report for the OpenArcanum project before the end of this year. Also, I've promised to fix the source code, and then release at least part of it.</p><br />

<p>Unfortunately, it seems that I won't be able to fulfill all these promises... so I'm really sorry guys! :(</p><br />

<p>I'll try my best to post at least something, however I was planning to post very detailed information about the engine, plans, and tasks, so it'll be nothing against what I was initially planning.</p><br />

<p>I wish I could split myself into two...</p><br /><div class="item_footer"><p><small><a href="http://crypton.game-alive.com/2011/12/long-time-no-see">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://crypton.game-alive.com/2011/12/long-time-no-see#comments</comments>
			<wfw:commentRss>http://crypton.game-alive.com/?tempskin=_rss2&#38;disp=comments&#38;p=48</wfw:commentRss>
		</item>
				<item>
			<title>Simple and effective tile image compression</title>
			<link>http://crypton.game-alive.com/2011/06/simple-and-effective-tile-image-compression</link>
			<pubDate>Fri, 17 Jun 2011 14:22:00 +0000</pubDate>			<dc:creator>Crypton</dc:creator>
			<category domain="main">OpenArcanum</category>			<guid isPermaLink="false">47@http://crypton.game-alive.com/</guid>
						<description>&lt;div&gt;&lt;div&gt;&lt;a href=&quot;http://crypton.game-alive.com/media/blogs/a/tile_compression.PNG?mtime=1308320278&quot; rel=&quot;lightbox[p47]&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://crypton.game-alive.com/htsrv/getfile.php/tile_compression.PNG?root=collection_1&amp;amp;path=tile_compression.PNG&amp;amp;mtime=1308320278&amp;amp;size=fit-320x320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;a href=&quot;http://crypton.game-alive.com/media/blogs/a/tile_compression.PNG?mtime=1308320278&quot; rel=&quot;lightbox[p47]&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://crypton.game-alive.com/htsrv/getfile.php/tile_compression.PNG?root=collection_1&amp;amp;path=/tile_compression.PNG&amp;amp;mtime=1308320278&amp;amp;size=fit-320x320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Few days ago, I got an idea for tile image compression for 2D isometric games like Arcanum, whose tiles are all in same shape, so today, I thought that it would be nice to try to implement it, and compare it with current compression algorithm, the RLE.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;For those who don&#039;t know anything about Arcanum&#039;s file formats, in the Arcanum, all tiles has same dimension, 78x40 pixels, and it&#039;s colors are reduced to 256 color palette, then resulting indices are compressed by RLE compression, and saved with palette into Art file.&lt;/p&gt;&lt;br /&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://crypton.game-alive.com/media/blogs/a/tile_compression.PNG?mtime=1308320278&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://crypton.game-alive.com/media/blogs/a/tile_compression.PNG?mtime=1308320278&quot; width=&quot;471&quot; height=&quot;243&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;

&lt;p&gt;The idea about the new tile compression is very simple: save only what&#039;s visible, which means that the compression algorithm will remove all transparent pixels (mask) from left and right side of the rows in the image, and then save trimmed rows to output (raw) stream.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;Anyway, I did implemeted it today, and I can say that the compression/decompression is very simple and fast, since the algorithm consist only of two single for loops and one memcopy call for every row, so it&#039;s even faster than RLE compression. Also the algorithm has fixed compression, so the compression ratio is constant, and it&#039;s not required to save any meta information for decompression, thus some space gets saved.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;So I tried to compress and then decompress one tile (more precisely, it&#039;s indices) &lt;b&gt;100 000&lt;/b&gt; times. And speed?&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;&lt;b&gt;Tile compression:&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;Compression: &lt;b&gt;274 ms&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Decompression: &lt;strike&gt;433&lt;/strike&gt; &lt;b&gt;260 ms&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;&lt;b&gt;Compared to RLE:&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;Compression: &lt;b&gt;4 041 ms&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Decompression: &lt;b&gt;2 447 ms&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;Oh yeah, in this case, the compression is almost 15 times faster than RLE compression, and more importantly, decompression is also faster, &lt;strike&gt;more than 5&lt;/strike&gt; almost 10 times faster. &lt;/p&gt;&lt;br /&gt;

&lt;p&gt;&lt;strike&gt;The reason why decompression is slower than compression is that, it&#039;s required to fill output buffer with zeros (i.e. transparent pixels) before actual decompression, plus I&#039;m using own memory fill function that is optimized for larger blocks (SSE2), thus making decopression slower.&lt;/strike&gt; (Optimized!)&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;&lt;b&gt;About compression ratio, one tile image has 3 120 (78*40) pixels, where 1 596 pixels are always transparent, so compressed files has 1 524 (3 120 - 1 596) pixels, thus the ratio is 0.488 (~1:2).&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;If you multiply those numbers by size of one pixel (in bytes), for example 3 for 24bit images, then you get almost 5KiB (4 788B) saved per tile. Now multiply that by 100 000...&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;What about RLE? well, it don&#039;t have fixed compression ratio, so it&#039;s hard/impossible to culculate/guess those numbers, but in the Arcanum, terrain is very varied and colorful, and most of the colors in palettes are used, so the RLE won&#039;t be as much effective as my tile compression.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;Anyway, with such fast tile decompression, it&#039;s possible to decompress the tile on fly, when requested, no need to keep them decompressed in RAM anymore.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;Pixel get/set functions can also work on compressed tiles very fastly using precalculated lookup table (for row offsets).&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;Also, all the tiles can be stored into one big tileset bank, and compressed with deflate or huffman compression, so we get even more space saved, and loading will be also faster.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;The whole bank can be now loaded into memory for superfast access!&lt;/p&gt;&lt;br /&gt;&lt;a href=&quot;http://crypton.game-alive.com/2011/06/simple-and-effective-tile-image-compression#more47&quot;&gt;Full story &amp;raquo;&lt;/a&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://crypton.game-alive.com/2011/06/simple-and-effective-tile-image-compression&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<div><div><a href="http://crypton.game-alive.com/media/blogs/a/tile_compression.PNG?mtime=1308320278" rel="lightbox[p47]"><img alt="" src="http://crypton.game-alive.com/htsrv/getfile.php/tile_compression.PNG?root=collection_1&amp;path=tile_compression.PNG&amp;mtime=1308320278&amp;size=fit-320x320" /></a></div><div><a href="http://crypton.game-alive.com/media/blogs/a/tile_compression.PNG?mtime=1308320278" rel="lightbox[p47]"><img alt="" src="http://crypton.game-alive.com/htsrv/getfile.php/tile_compression.PNG?root=collection_1&amp;path=/tile_compression.PNG&amp;mtime=1308320278&amp;size=fit-320x320" /></a></div></div><p>Few days ago, I got an idea for tile image compression for 2D isometric games like Arcanum, whose tiles are all in same shape, so today, I thought that it would be nice to try to implement it, and compare it with current compression algorithm, the RLE.</p><br />

<p>For those who don't know anything about Arcanum's file formats, in the Arcanum, all tiles has same dimension, 78x40 pixels, and it's colors are reduced to 256 color palette, then resulting indices are compressed by RLE compression, and saved with palette into Art file.</p><br />

<div class="image_block"><a href="http://crypton.game-alive.com/media/blogs/a/tile_compression.PNG?mtime=1308320278"><img alt="" src="http://crypton.game-alive.com/media/blogs/a/tile_compression.PNG?mtime=1308320278" width="471" height="243" /></a></div><br />

<p>The idea about the new tile compression is very simple: save only what's visible, which means that the compression algorithm will remove all transparent pixels (mask) from left and right side of the rows in the image, and then save trimmed rows to output (raw) stream.</p><br />

<p>Anyway, I did implemeted it today, and I can say that the compression/decompression is very simple and fast, since the algorithm consist only of two single for loops and one memcopy call for every row, so it's even faster than RLE compression. Also the algorithm has fixed compression, so the compression ratio is constant, and it's not required to save any meta information for decompression, thus some space gets saved.</p><br />

<p>So I tried to compress and then decompress one tile (more precisely, it's indices) <b>100 000</b> times. And speed?</p><br />

<p><b>Tile compression:</b></p><br />
<p>Compression: <b>274 ms</b></p>
<p>Decompression: <strike>433</strike> <b>260 ms</b></p><br />

<p><b>Compared to RLE:</b></p><br />

<p>Compression: <b>4 041 ms</b></p>
<p>Decompression: <b>2 447 ms</b></p><br />

<p>Oh yeah, in this case, the compression is almost 15 times faster than RLE compression, and more importantly, decompression is also faster, <strike>more than 5</strike> almost 10 times faster. </p><br />

<p><strike>The reason why decompression is slower than compression is that, it's required to fill output buffer with zeros (i.e. transparent pixels) before actual decompression, plus I'm using own memory fill function that is optimized for larger blocks (SSE2), thus making decopression slower.</strike> (Optimized!)</p><br />

<p><b>About compression ratio, one tile image has 3 120 (78*40) pixels, where 1 596 pixels are always transparent, so compressed files has 1 524 (3 120 - 1 596) pixels, thus the ratio is 0.488 (~1:2).</b></p><br />

<p>If you multiply those numbers by size of one pixel (in bytes), for example 3 for 24bit images, then you get almost 5KiB (4 788B) saved per tile. Now multiply that by 100 000...</p><br />

<p>What about RLE? well, it don't have fixed compression ratio, so it's hard/impossible to culculate/guess those numbers, but in the Arcanum, terrain is very varied and colorful, and most of the colors in palettes are used, so the RLE won't be as much effective as my tile compression.</p><br />

<p>Anyway, with such fast tile decompression, it's possible to decompress the tile on fly, when requested, no need to keep them decompressed in RAM anymore.</p><br />

<p>Pixel get/set functions can also work on compressed tiles very fastly using precalculated lookup table (for row offsets).</p><br />

<p>Also, all the tiles can be stored into one big tileset bank, and compressed with deflate or huffman compression, so we get even more space saved, and loading will be also faster.</p><br />

<p>The whole bank can be now loaded into memory for superfast access!</p><br /><a href="http://crypton.game-alive.com/2011/06/simple-and-effective-tile-image-compression#more47">Full story &raquo;</a><div class="item_footer"><p><small><a href="http://crypton.game-alive.com/2011/06/simple-and-effective-tile-image-compression">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://crypton.game-alive.com/2011/06/simple-and-effective-tile-image-compression#comments</comments>
			<wfw:commentRss>http://crypton.game-alive.com/?tempskin=_rss2&#38;disp=comments&#38;p=47</wfw:commentRss>
		</item>
				<item>
			<title>C++: The Fastest foreach loop? (Hacking references again)</title>
			<link>http://crypton.game-alive.com/2011/06/cpp-the-fastest-foreach-loop</link>
			<pubDate>Mon, 06 Jun 2011 11:39:00 +0000</pubDate>			<dc:creator>Crypton</dc:creator>
			<category domain="main">Programming</category>
<category domain="alt">Snippets</category>			<guid isPermaLink="false">46@http://crypton.game-alive.com/</guid>
						<description>&lt;p&gt;Have you ever tried to implement foreach loop macro in C++? If you tried, you know that it&#039;s almost impossible to do it, because you have to initialize the reference to iterated value in the initialization section of the for loop (i.e. immediately after &quot;for&quot; keyword), so you cannot change the reference (i.e. make it point to different object), therefore you must split the foreach loop into two for loops. It&#039;s common to implement foreach as two for loops, most of the implemetation I&#039;ve seen (e.g. Qt or &lt;a href=&quot;http://www.codeproject.com/KB/cpp/foreach.aspx&quot;&gt;this&lt;/a&gt;) do it this way.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;What&#039;s wrong on this implementation? it&#039;s slow! Not only that there must be some helper container constructed with a variables whose purpose is to keep the foreach state (to switch and interupt second for loop), but also, for every iterated element, there are several useless condition tests and var assignments, only to make it working.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;That&#039;s a way slower than normal for/while loop with iterators.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;How can we speedup the foreach loop? Can&#039;t we somehow remove the second for loop and the helper container? Surely we can, but it won&#039;t be easy (and for someone nice).&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;I spent all day researching and implementing different foreach loops, but in the end, I had to split it into two for loops anyway. &lt;/p&gt;&lt;br /&gt;

&lt;p&gt;It&#039;s almost impossible to implement it to work with references. Pointers are easy thing, task for 5 min, but it isn&#039;t as convenient as foreach with references.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;Anyway, that way the portable-way attempt, later I managed to implement the foreach loop as one for loop by hacking the reference via templated asm function. Someone may consider it as ugly hack, but who cares, when it&#039;s a way faster than other implementations, and you can always easily switch to generic implementation, because it&#039;s nothing more than a macro.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;So how did I implemented it? Have a look at this picture:&lt;/p&gt;&lt;br /&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://crypton.game-alive.com/media/blogs/a/foreach.PNG?mtime=1307353514&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://crypton.game-alive.com/media/blogs/a/foreach.PNG?mtime=1307353514&quot; width=&quot;100%&quot; height=&quot;100%&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;This picture shows how my foreach implementation works, before I converted it to macro. It consist of 4 variables:&lt;/p&gt;&lt;br /&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;p&gt;-First variable is containter&#039;s iterator object. Nothing special about it, use your own or stl implementation.&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;


  &lt;li&gt;&lt;p&gt;-Second is pointer to current iterated element. While iterating, it&#039;s compared to NULL, so it&#039;s very fast.&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;


  &lt;li&gt;&lt;p&gt;-Third variable (reference) is the most important one.
It&#039;s actually pointer to fourth variable (&quot;element&quot;), which is reference to currently iterated element, so it&#039;s pointer to pointer to &quot;element&quot; reference. After calling pointerToReferenceEx, it will point to variable reference, so we can change reference&#039;s address to point to different (next) element.&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;


  &lt;li&gt;&lt;p&gt;-Fouth variable is currently iterated element, it will always point to new element after every for loop iteration.&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;


&lt;/ol&gt;

&lt;p&gt;As you can see, I&#039;m using new Cpp0x features, auto and decltype, to determine type of variables, because C++ doesn&#039;t have any typeof operator (GCC has it as extension, which is used by Qt foreach loop as well).&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;That&#039;s nothing special, the magic relies in the pointerToReferenceEx function (I&#039;m not going to release the implementation yet), which takes as first argument a pointer to element you want to dereference, and which is able to determine address of reference on the left side (varible &quot;element&quot;) of the expression and assign it to variable &quot;reference&quot; before the function returns! In other words, the function is able to determine the address of reference where the input pointer will be dereferenced and assigned.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;Using this function, we don&#039;t need second for loop, nor any other helper container, since we define the variables out of scope.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;Yes, we can define the variables before for loop, and still be able to have more foreach loops in one scope without any variable name collisions!&lt;/p&gt;&lt;br /&gt;
 
&lt;p&gt;How? Piece of cake, we make the variable names unique, by appending the current source code line number to them!&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;This can be done automatically by using predefined macro __LINE__ and using some helper macro for concating the variable names with the lines number.&lt;/p&gt;&lt;br /&gt;
 
&lt;p&gt;Here&#039;s the final macro:&lt;/p&gt;&lt;br /&gt;
&lt;pre title=&quot;code&quot; class=&quot;cpp:firstline[1]&quot;&gt;#define _ArgConcat(a, b) a##b
#define ArgConcat(a, b) _ArgConcat(a, b)

#define AppendCurrentLine(x) ArgConcat(x, __LINE__)

Usage example (must be on same line!):
bool AppendCurrentLine(myBool); if(AppendCurrentLine(myBool)) return;
&lt;/pre&gt;

&lt;p&gt;So this way, the variables are unique, and we can have more foreach loops in one scope! Final foreach macro might look like this: &lt;/p&gt;&lt;br /&gt;

&lt;pre title=&quot;code&quot; class=&quot;cpp:firstline[1]&quot;&gt;#define ForEach(Variable, Container) ...

ADynamicArray&amp;lt;AInt32u&amp;gt; array;
...
ForEach(x, array) // x is defined as AInt32u&amp;amp;
{
// do something fancy
}
&lt;/pre&gt;

&lt;p&gt;Out of interest, in the AGate engine, there are also more advanced foreach loops, to allow to take control over more features, so there are several foreach macros:&lt;/p&gt;&lt;br /&gt;

&lt;pre title=&quot;code&quot; class=&quot;cpp:firstline[1]&quot;&gt;#define ContainerIterateEx(Container, Variable, Iterator, Direction, Seek) ...
#define ContainerIterate(Container, Variable, Iterator) ...
#define ContainerForEach(Container, Variable) ...&lt;/pre&gt;

&lt;p&gt;That&#039;s it. Nothing more to add, maybe I&#039;ll only note that I&#039;m not using any foreach loops in C++ at all :) I think that it&#039;s feature suitable for scripting languages only, but I did provided foreach loops in AGate engine for those who would like to uses them.&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;That&#039;s all for now. Crypton&#039;s out! :)&lt;/p&gt;&lt;br /&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://crypton.game-alive.com/2011/06/cpp-the-fastest-foreach-loop&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Have you ever tried to implement foreach loop macro in C++? If you tried, you know that it's almost impossible to do it, because you have to initialize the reference to iterated value in the initialization section of the for loop (i.e. immediately after "for" keyword), so you cannot change the reference (i.e. make it point to different object), therefore you must split the foreach loop into two for loops. It's common to implement foreach as two for loops, most of the implemetation I've seen (e.g. Qt or <a href="http://www.codeproject.com/KB/cpp/foreach.aspx">this</a>) do it this way.</p><br />

<p>What's wrong on this implementation? it's slow! Not only that there must be some helper container constructed with a variables whose purpose is to keep the foreach state (to switch and interupt second for loop), but also, for every iterated element, there are several useless condition tests and var assignments, only to make it working.</p><br />

<p>That's a way slower than normal for/while loop with iterators.</p><br />

<p>How can we speedup the foreach loop? Can't we somehow remove the second for loop and the helper container? Surely we can, but it won't be easy (and for someone nice).</p><br />

<p>I spent all day researching and implementing different foreach loops, but in the end, I had to split it into two for loops anyway. </p><br />

<p>It's almost impossible to implement it to work with references. Pointers are easy thing, task for 5 min, but it isn't as convenient as foreach with references.</p><br />

<p>Anyway, that way the portable-way attempt, later I managed to implement the foreach loop as one for loop by hacking the reference via templated asm function. Someone may consider it as ugly hack, but who cares, when it's a way faster than other implementations, and you can always easily switch to generic implementation, because it's nothing more than a macro.</p><br />

<p>So how did I implemented it? Have a look at this picture:</p><br />

<div class="image_block"><a href="http://crypton.game-alive.com/media/blogs/a/foreach.PNG?mtime=1307353514"><img alt="" src="http://crypton.game-alive.com/media/blogs/a/foreach.PNG?mtime=1307353514" width="100%" height="100%" /></a></div>

<p>This picture shows how my foreach implementation works, before I converted it to macro. It consist of 4 variables:</p><br />

<ol>
  <li><p>-First variable is containter's iterator object. Nothing special about it, use your own or stl implementation.</p><br /></li>


  <li><p>-Second is pointer to current iterated element. While iterating, it's compared to NULL, so it's very fast.</p><br /></li>


  <li><p>-Third variable (reference) is the most important one.
It's actually pointer to fourth variable ("element"), which is reference to currently iterated element, so it's pointer to pointer to "element" reference. After calling pointerToReferenceEx, it will point to variable reference, so we can change reference's address to point to different (next) element.</p><br /></li>


  <li><p>-Fouth variable is currently iterated element, it will always point to new element after every for loop iteration.</p><br /></li>


</ol>

<p>As you can see, I'm using new Cpp0x features, auto and decltype, to determine type of variables, because C++ doesn't have any typeof operator (GCC has it as extension, which is used by Qt foreach loop as well).</p><br />

<p>That's nothing special, the magic relies in the pointerToReferenceEx function (I'm not going to release the implementation yet), which takes as first argument a pointer to element you want to dereference, and which is able to determine address of reference on the left side (varible "element") of the expression and assign it to variable "reference" before the function returns! In other words, the function is able to determine the address of reference where the input pointer will be dereferenced and assigned.</p><br />

<p>Using this function, we don't need second for loop, nor any other helper container, since we define the variables out of scope.</p><br />

<p>Yes, we can define the variables before for loop, and still be able to have more foreach loops in one scope without any variable name collisions!</p><br />
 
<p>How? Piece of cake, we make the variable names unique, by appending the current source code line number to them!</p><br />

<p>This can be done automatically by using predefined macro __LINE__ and using some helper macro for concating the variable names with the lines number.</p><br />
 
<p>Here's the final macro:</p><br />
<pre title="code" class="cpp:firstline[1]">#define _ArgConcat(a, b) a##b
#define ArgConcat(a, b) _ArgConcat(a, b)

#define AppendCurrentLine(x) ArgConcat(x, __LINE__)

Usage example (must be on same line!):
bool AppendCurrentLine(myBool); if(AppendCurrentLine(myBool)) return;
</pre>

<p>So this way, the variables are unique, and we can have more foreach loops in one scope! Final foreach macro might look like this: </p><br />

<pre title="code" class="cpp:firstline[1]">#define ForEach(Variable, Container) ...

ADynamicArray&lt;AInt32u&gt; array;
...
ForEach(x, array) // x is defined as AInt32u&amp;
{
// do something fancy
}
</pre>

<p>Out of interest, in the AGate engine, there are also more advanced foreach loops, to allow to take control over more features, so there are several foreach macros:</p><br />

<pre title="code" class="cpp:firstline[1]">#define ContainerIterateEx(Container, Variable, Iterator, Direction, Seek) ...
#define ContainerIterate(Container, Variable, Iterator) ...
#define ContainerForEach(Container, Variable) ...</pre>

<p>That's it. Nothing more to add, maybe I'll only note that I'm not using any foreach loops in C++ at all :) I think that it's feature suitable for scripting languages only, but I did provided foreach loops in AGate engine for those who would like to uses them.</p><br />

<p>That's all for now. Crypton's out! :)</p><br /><div class="item_footer"><p><small><a href="http://crypton.game-alive.com/2011/06/cpp-the-fastest-foreach-loop">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://crypton.game-alive.com/2011/06/cpp-the-fastest-foreach-loop#comments</comments>
			<wfw:commentRss>http://crypton.game-alive.com/?tempskin=_rss2&#38;disp=comments&#38;p=46</wfw:commentRss>
		</item>
			</channel>
</rss>

