<?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>richardjdare.com &#187; programming</title>
	<atom:link href="http://richardjdare.com/blog/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://richardjdare.com</link>
	<description>Games, Ideas, Imagination</description>
	<lastBuildDate>Thu, 06 Apr 2023 12:02:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>Oculus Rift DK2 first impressions</title>
		<link>http://richardjdare.com/blog/2014/10/oculus-rift-dk2-first-impressions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=oculus-rift-dk2-first-impressions</link>
		<comments>http://richardjdare.com/blog/2014/10/oculus-rift-dk2-first-impressions/#comments</comments>
		<pubDate>Fri, 24 Oct 2014 22:30:26 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[game development]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[Oculus Rift]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[virtual reality]]></category>
		<category><![CDATA[vr]]></category>

		<guid isPermaLink="false">http://richardjdare.com/?p=1606</guid>
		<description><![CDATA[&#160; Last week I received my Oculus Rift DK2 headset which I ordered back in August. I had my eye on the Oculus DK1, the.]]></description>
				<content:encoded><![CDATA[<p>&nbsp;</p>
<p><img class="aligncenter size-full wp-image-1618" src="http://richardjdare.com/blog/wp-content/uploads/2014/10/neuromancer2.jpg" alt="neuromancer" width="718" height="388" /></p>
<p>Last week I received my Oculus Rift DK2 headset which I ordered back in August. I had my eye on the Oculus DK1, the first version of their headset, but I was too busy getting <a href="http://antigengame.com">Antigen</a> out of the door to let myself be distracted by other projects. Now Antigen is out of the way, I can let myself experiment a little while I figure out what my next big project will be.</p>
<p>It comes in a padded, reusable cardboard box, which is fine for storage but if I was going to take it anywhere I would probably get a Pelican camera case or <a href="http://www.vrforum.org/threads/oculus-rift-dk2-case-for-only-10.797/" target="_blank">something similar</a>, and cut some custom foam inserts for it, like <a href="http://www.reddit.com/r/oculus/comments/2ceaq6/dk2_protection_easy_custom_fit_case/" target="_blank">this Oculus user</a> has done.</p>
<p>The headset is surprisingly light, and connects to your computer with a sturdy cable that runs over the top of your head along one of the headset&#8217;s straps. I imagine this was done for weight distribution and to help keep the cable out of the way when you move around.</p>
<p>The cable connects to your computer via 1 USB and 1 HDMI port. There is a motion tracking IR camera that clips to your monitor like a webcam. This uses 1 USB port. There&#8217;s also an optional power supply if you want to use the USB port built into the headset. One device that uses this is the <a href="https://www.leapmotion.com/" target="_blank">Leap Motion</a>, which I&#8217;d like to get my hands on sooner or later&#8230;</p>
<p>The Rift also comes with two sets of detachable lenses; a longer lens that is already installed in the headset, and a shorter lens intended for nearsighted users.</p>
<p>Setup was easy. I downloaded the drivers from the Oculus website, installed them and as instructed, updated the headset firmware using a simple and straightforward preferences utility. This tool also lets you create user profiles for the Rift, containing your height and other details.</p>
<p>The Rift appears to your computer as a second monitor. You can view anything you drag on there, but it will look like crap unless it is rendered stereoscopically and positioned correctly. There is a DirectX style &#8220;Direct Mode&#8221; that lets applications talk directly to the Rift, but it doesn&#8217;t work on the Mac yet. (The Mac is a bit of a second-class citizen for the Rift at the moment. Unfortunately, most of the coolest demos are Windows only too.)</p>
<p>At this point I could hardly contain my urge to <a href="http://en.wikipedia.org/wiki/Neuromancer" target="_blank">jack in to the matrix</a> and become the console cowboy I&#8217;d always dreamed of being, so I hit &#8220;Show Demo&#8221; and braced myself.</p>
<p><figure class='media-wrapper player'><iframe width="500" height="281" src="https://www.youtube.com/embed/tOzTikUdvuU?wmode=transparent" frameborder="0" allowfullscreen wmode="Opaque"></iframe></figure></p>
<p>I found myself sitting at a desk in cyberspace, somewhere on an infinite Tron-esque plain. The first thing that struck me was the stereoscopy, the 3d-ness of the lamp and the tower of playing cards on my desk. I really felt I could reach out and touch them.</p>
<p><img class="aligncenter size-full wp-image-1621" src="http://richardjdare.com/blog/wp-content/uploads/2014/10/oculus-demo.jpg" alt="oculus-demo" width="500" height="281" /></p>
<p>The resolution is good at 960&#215;1080 for each eye, but I don&#8217;t think it&#8217;s high enough for consumer use yet. I suspect that in order to approximate the usability and experience of a desktop monitor, VR displays will require a much higher relative pixel density. There is also a kind of &#8220;screen door effect&#8221; where you can see the pixel grid. Most of the time this isn&#8217;t too bad, but it is noticeable when you try and view details that are further away, such as the faces of game characters.</p>
<p>Text is readable as long as it is large and in the middle of the display. It&#8217;s a bit like looking at text on a C64 with a bad TV set. Towards the edges of the display it gets blurry and there is some <a href="http://en.wikipedia.org/wiki/Chromatic_aberration">chromatic aberration</a>. You need to look at text directly in order to read it properly, which will have ramifications for UI design, particularly games where information is usually positioned at the edge of the screen.</p>
<p>All the same, these aren&#8217;t really criticisms, they are just statements about where the developers kit is right now. The DK2 is a prototype that a <del>deranged experimenter</del> committed person works with and adapts himself to, rather than a consumer product. I&#8217;ve not had a chance to try the new <a href="http://www.wired.co.uk/news/archive/2014-09/23/oculus-consumer-vr" target="_blank">Crescent Bay</a> prototype, but reports suggest that it eliminates the screen door effect and that we can expect the consumer version to be a real advance on what is currently available.</p>
<p>There&#8217;s a nice demo included with the SDK where you can walk around an Italian country house and its surrounding grounds. The house is on a cliff over the sea. You really feel like you can fall off when you approach the cliff. One thing I found interesting is how your body is tricked into reacting to things when you accidentally bump into them. It feels suddenly like there is something real there. This demo also shows how high quality shading and lighting can mitigate some of the shortcomings of the headset such as the resolution and screen-door effect. Another demo I tried, <a href="http://www.quitenice.co/blog/2014/8/3/4thflrstudio" target="_blank">4thFlrStudio</a> by Brendan Coyle demonstrates this clearly, with its remarkable lighting and detail.</p>
<p>After a bit of messing about I got Steam and Half-Life 2 working on the Rift. Within minutes it gave me motion sickness! It clearly showed how VR requires both a rock-solid framerate and well-calibrated motion settings in order to be effective. Anything less than 75fps will likely send the user running to the toilet!</p>
<p>The presence of other characters in the game is startling. This was the first time I had encountered another being in VR, albeit a simulated one. At the beginning of HL2 you go through a police checkpoint and one of them thumps you in the chest. I actually felt it! The enemies were extremely intimidating. I dread to think what it is like on the later levels when you encounter the <a href="http://img1.wikia.nocookie.net/__cb20100607081417/half-life/en/images/b/b7/Hunter_from_EP2_teaser.jpg" target="_blank" rel="lightbox[1606]">Hunters</a> and <a href="http://img1.wikia.nocookie.net/__cb20100427144852/half-life/en/images/9/98/Episode2-strider.jpg" target="_blank" rel="lightbox[1606]">Striders</a>! I really felt I was inside the game, it&#8217;s kind of like a fuzzy lucid dream. I will have to experiment with the settings to see how far I can minimise the motion sickness.</p>
<p>The next thing for me to do is get the SDK going and see what I can create with it. I might also need a more powerful computer. Some of the demos I tried only ran at 16fps. My 2012 Macbook Pro is a great for general use and for my iOS development, but its NVidia 650m has about 1/7th the power of a top desktop GPU.</p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2014/10/oculus-rift-dk2-first-impressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making a heat map effect for Toxin</title>
		<link>http://richardjdare.com/blog/2013/06/making-a-heat-map-effect-for-toxin/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=making-a-heat-map-effect-for-toxin</link>
		<comments>http://richardjdare.com/blog/2013/06/making-a-heat-map-effect-for-toxin/#comments</comments>
		<pubDate>Thu, 20 Jun 2013 14:18:07 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[game development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Toxin]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[processing]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=1010</guid>
		<description><![CDATA[As you might already know my forthcoming iOS game, Toxin involves shooting rapidly dividing toxic cells which eventually fill the playfield or burst to spawn.]]></description>
				<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-1011" style="border: 0;" src="http://richardjdare.com/blog/wp-content/uploads/2013/06/heatmap-shot.jpg" alt="Toxin Heatmap" width="500" height="259" /></p>
<p>As you might already know my forthcoming iOS game, <a href="http://toxingame.com" target="_blank">Toxin</a> involves shooting rapidly dividing toxic cells which eventually fill the playfield or burst to spawn flying enemies.</p>
<p>Now, not long ago I read an <a href="http://twitter-heatmap.vertalab.com/" target="_blank">article</a> about generating <a href="http://en.wikipedia.org/wiki/Heat_map" target="_blank">heat maps </a>to display web statistics, and I thought it would be cool to use this technique to colourise the ever changing clumps of cells in Toxin. After finding out how they work, I knocked up a prototype in <a href="http://processing.org" target="_blank">Processing</a> before adding the effect to the game.</p>
<p>Generating heatmaps is pretty simple. Take a 2d grid of values, perform a Gaussian blur on it, then use each resulting value as an index into a gradient of colours.</p>
<p>For Toxin, the first step was to create a simple array of numbers representing the current state of the cell grid. Toxin cells can be different sizes. To simplify things, I store each big cell as several small cells.</p>
<p>Each cell is given an initial value of 255. When we draw our cells, we use a range from black to white, where values of 0 are black, 255 is white and values in between are shades of gray.</p>
<p><img class="aligncenter size-full wp-image-1020" style="border: 0;" src="http://richardjdare.com/blog/wp-content/uploads/2013/06/heatmap-initial.jpg" alt="heatmap-initial" width="500" height="361" /><br />
<em><span style="color: #808080; padding-bottom: 20px;">The cells are represented as a grid of numbers. Each cell is given the initial value of 255</span></em></p>
<p>The next step is to apply a simple <a href="http://en.wikipedia.org/wiki/Box_blur" target="_blank">box blur</a> to this grid of numbers. Normally heat maps use <a href="http://en.wikipedia.org/wiki/Gaussian_blur" target="_blank">Gaussian blur</a>, but since Toxin&#8217;s grid is such a low resolution we can get away with using a simpler algorithm which is much faster to calculate. To learn more about blurring techniques, I recommend <a href="http://www.gamasutra.com/view/feature/3102/four_tricks_for_fast_blurring_in_.php?print=1" target="_blank">Alex Evan&#8217;s article</a> on Gamasutra.</p>
<p><img class="aligncenter size-full wp-image-1027" style="border: 0;" src="http://richardjdare.com/blog/wp-content/uploads/2013/06/heatmap-blur.jpg" alt="Toxin heat map blur" width="500" height="361" /><br />
<em><span style="color: #808080; padding-bottom: 20px;">The cell grid after being blurred. Notice how the values have changed</span></em></p>
<p>Now look what happens when we draw our original cell grid using the values from the blur operation. Cells in the middle of large clumps have higher values than solitary cells.</p>
<p><img class="aligncenter size-full wp-image-1033" style="border: 0;" src="http://richardjdare.com/blog/wp-content/uploads/2013/06/heatmap-grayscale.jpg" alt="Toxin heat map grayscale" width="500" height="361" /><br />
<em><span style="color: #808080; padding-bottom: 20px;">Drawing the original grid with the blurred values</span></em></p>
<p>The final step is to colourise these grayscale values so they look cool. We do this by mapping these values onto a gradient I made in Photoshop. Values of 0 map onto the left of this gradient, 255 to the right, and the others to the colours in between.</p>
<div style="text-align: center;"><img class="aligncenter wp-image-1037 size-full" style="border: 0;" src="http://www.richardjdare.com/blog/wp-content/uploads/2013/06/heatmap-gradient.jpg" alt="Toxin heat map gradient" width="320" height="80" /></div>
<div style="width: 500px; text-align: center;"></div>
<p>When we draw the grid with the colours from the gradient we get our heatmap!</p>
<p><img class="aligncenter size-full wp-image-1044" style="border: 0;" src="http://richardjdare.com/blog/wp-content/uploads/2013/06/final-heatmap.jpg" alt="Toxin final heat map" width="500" height="361" /></p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2013/06/making-a-heat-map-effect-for-toxin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Toxin &#8211; Advanced Sprite Techniques</title>
		<link>http://richardjdare.com/blog/2013/03/toxin-advanced-sprite-techniques/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=toxin-advanced-sprite-techniques</link>
		<comments>http://richardjdare.com/blog/2013/03/toxin-advanced-sprite-techniques/#comments</comments>
		<pubDate>Sun, 24 Mar 2013 15:09:50 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[game development]]></category>
		<category><![CDATA[photoshop]]></category>
		<category><![CDATA[Toxin]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[photoshop scripting]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sprites]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=955</guid>
		<description><![CDATA[Creating good looking graphics for an abstract game like Toxin is harder than it first appears. Not only are your images stripped bare of most signifying.]]></description>
				<content:encoded><![CDATA[<p dir="ltr"><img class="aligncenter size-full wp-image-959" style="border: 0px;" alt="toxin-sprites" src="http://richardjdare.com/blog/wp-content/uploads/2013/03/toxin-sprites.jpg" width="500" height="272" /></p>
<p dir="ltr">Creating good looking graphics for an abstract game like <a href="http://toxingame.com" target="_blank">Toxin</a> is harder than it first appears. Not only are your images stripped bare of most signifying information, requiring a real eye for the principles of graphic design, but creating the artwork itself – giving each line the right balance of sharpness and glow, demands careful image processing and a great deal of experimentation.</p>
<p>When I started making Toxin, the graphics were a real struggle. It took me an enormous amount of work to develop the look of the game. To give you an idea, I have a folder on my laptop with all of Toxin’s artwork in, and it weighs in at 967meg! All for a little mobile game!</p>
<p>Eventually all my experimentation began to pay off, and I developed some powerful techniques for creating animated multicoloured abstract sprites. In this post I want to describe one of my favourite methods.</p>
<p>I start by creating our sprite out of spline shapes in 3D Studio Max. Unlike many 3D programs, Max can render splines at various thicknesses, animate them, and can even apply texture maps and materials to them.</p>
<p>I then use gradient maps to mask out areas of the shapes that I want to be different colours. These gradient maps are then animated, so the different coloured areas can actually move around the shape.</p>
<p>Then, I do two sets of renders. First I render the shape with the masks on, so there’ll be a hole wherever the coloured area will be. Then I do another render with the mask inverted, so that only the coloured area is visible.</p>
<p><img class="aligncenter size-full wp-image-967" style="border: 0px;" alt="make-a-spiker" src="http://richardjdare.com/blog/wp-content/uploads/2013/03/make-a-spiker.jpg" width="500" height="380" /><br />
<em><span style="color: #808080; padding-bottom: 20px;">This diagram shows each step in the creation of one frame of animation for a Toxin sprite. The total animation is 64 frames long. This intricate process is largely automated using Photoshop scripts</span></em></p>
<p>Then its time to start up Photoshop. After deciding what colours I am going to use, I run each set of sprites through a Photoshop script that colourises them and adds a glow. To create the glow, I duplicate the image into another layer, Gaussian blur it, then additive-blend it on top. Sometimes I use several glow layers with different Gaussian blur parameters. I also find that using image-&gt;adjustments-&gt;Hue/Saturation to colourise gives me the best results.</p>
<p>The final step is to compose the two sets of colourised renders into a single sprite. I do this with another Photoshop script. The end result is a lot more interesting than the basic Geometry Wars style sprites everyone else is using, I think.</p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2013/03/toxin-advanced-sprite-techniques/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Next Photoshop</title>
		<link>http://richardjdare.com/blog/2013/02/the-next-photoshop/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-next-photoshop</link>
		<comments>http://richardjdare.com/blog/2013/02/the-next-photoshop/#comments</comments>
		<pubDate>Sun, 24 Feb 2013 15:06:12 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[photoshop]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[photoshop scripting]]></category>
		<category><![CDATA[processing]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=905</guid>
		<description><![CDATA[If you’ve read this blog for very long you’ll know that I am a big fan of Photoshop scripting and procedural graphics. Toxin, the iPhone.]]></description>
				<content:encoded><![CDATA[<p>If you’ve read this blog for very long you’ll know that I am a big fan of Photoshop scripting and procedural graphics. <a href="http://toxingame.com" target="_blank">Toxin</a>, the iPhone game I’ve been <a href="http://richardjdare.com/blog/category/toxin/" target="_blank">working on</a>, uses procedural art and animated image processing effects extensively. I don’t think the game would be possible without Photoshop scripting.</p>
<p>However, Photoshop’s scripting system is far from perfect. You cannot access pixel data from within scripts, nor can you draw lines or circles or anything like that. These limitations have become really frustrating to me.</p>
<p>For example, on the day I started work on Toxin, I needed to draw an ellipse with precise dimensions. The whole game takes place inside an ellipse, you see. I couldn’t do it in Photoshop by clicking and dragging, it was too inaccurate, so I wrote a C program using the <a href="http://alleg.sourceforge.net/" target="_blank">Allegro</a> game library to create the precise shapes I needed. These were then loaded into Photoshop and used as guides while I created the game artwork.</p>
<p>Later I discovered <a href="http://processing.org/" target="_blank">Processing</a>, which is much quicker to develop in than C. I soon developed a workflow where I would go back and forth between Photoshop and Processing, generating procedural content then processing it or running it through scripts until I got the desired result.</p>
<p>Processing, however has some frustrating quirks of its own. As a simplified front end to Java, you often get Java errors that are incomprehensible without digging into the underlying platform. Also, it cannot effectively save semi-transparent PNGs which means that the anti-aliased sprites I was creating all had a solid background which had to be removed. I ended up redesigning my workflow and using the excellent Ghost and AntiGhost plugins from <a href="http://www.flamingpear.com/download.html" target="_blank">Flaming Pear</a> to overcome these problems.</p>
<h3>From graphics app to graphics platform</h3>
<p>All these issues got me thinking about what I really want from a high end graphics program. What I want is something like a programmable, graphical <a href="http://en.wikipedia.org/wiki/Lisp_machine" target="_blank">Lisp Machine</a>; a powerful graphics programming platform with the UI of an art application, not an art application with a bolted-on scripting system.</p>
<p>I want the ability to programmatically control every aspect of Photoshop from both external scripts and from a real-time <a href="http://en.wikipedia.org/wiki/Read–eval–print_loop" target="_blank">REPL</a> inside Photoshop that works like the command line used in some CAD programs.</p>
<p>I want to be able to type<em> line(10,10,100,100)</em> and have a line appear on my presently selected image. I want to be able to open the REPL and assign my current selection to a variable which I can later re-select with a simple command. I want to be able to access the pixel data in my images, and write my own filters in Javascript within the scripting system itself. I think anyone in high-end graphics or scientific imaging would love something like this.</p>
<p>A true graphics platform like I have described would likely have to be written from the ground up with programmability built in. Nevertheless I did spend some time finding out how far I could go with Photoshop&#8217;s native capabilities.</p>
<p>I had the idea of writing a Photoshop plugin that contained a number of drawing functions which could be called from scripts via a Javascript library that integrates with the Photoshop document model. So you could write <em>PSDraw.line(10,10,100,100);</em> in ExtendScript Toolkit, and my Javascript library would construct a call to the plugin which would draw the line.</p>
<p>Unfortunately, it&#8217;s not possible to get information from a plugin back to the Photoshop script thats running it, so it wouldn&#8217;t be possible to do a <em>getPixel()</em> or anything like that. At best, this plugin/script combo would give us a somewhat clunky way of drawing programmatically but little else that would justify the effort involved. (have you ever seen the <a href="http://www.mactech.com/articles/mactech/Vol.15/15.04/PhotoshopPlug-InsPart1/index.html" target="_blank">Photoshop plugin API?</a>)</p>
<p>As a secondary option I thought about actually embedding <a href="https://code.google.com/p/v8/" target="_blank">V8</a> inside a plugin which would contain an editor and debugger. Besides the ugliness of having 2 distinct Javascript engines running in the same app, the plugin architecture put paid to this idea. Photoshop plugins are one-shot operations. Load the plugin, set the parameters, perform the operation, then shutdown. My idea would only be effective if the plugin was a persistent part of the Photoshop environment.</p>
<p>So the only options then, would be for Adobe to integrate scripting more completely, or for someone else to start making what would be a true sucessor to Photoshop.</p>
<p>&nbsp;</p>
<p><figure class='media-wrapper player'><iframe width="500" height="375" src="http://www.youtube.com/embed/mOZqRJzE8xg?wmode=transparent" frameborder="0" allowfullscreen wmode="Opaque"></iframe></figure></p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2013/02/the-next-photoshop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Toxin &#8211; Creating sprites in Processing</title>
		<link>http://richardjdare.com/blog/2012/11/toxin-creating-sprites-in-processing/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=toxin-creating-sprites-in-processing</link>
		<comments>http://richardjdare.com/blog/2012/11/toxin-creating-sprites-in-processing/#comments</comments>
		<pubDate>Sun, 18 Nov 2012 18:19:34 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[game development]]></category>
		<category><![CDATA[Toxin]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=490</guid>
		<description><![CDATA[One of the most powerful graphical tools I am using to make Toxin is Processing, a Java based platform used to create generative art. Processing.]]></description>
				<content:encoded><![CDATA[<p>One of the most powerful graphical tools I am using to make <a href="http://toxingame.com" target="_blank">Toxin</a> is <a href="http://processing.org/" target="_blank">Processing</a>, a Java based platform used to create generative art. Processing provides you with a basic IDE, a graphics API and a simplified front end to Java that lets you get things up and running with a minimal amount of work. It&#8217;s great for doing experiments or when you need to create art programmatically rather than with a graphics program.</p>
<p><img class="aligncenter size-full wp-image-517" style="border: 0px;" title="Processing IDE" src="http://richardjdare.com/blog/wp-content/uploads/2012/11/processing-ide.jpg" alt="" width="480" height="542" /><br />
<em><span style="color: #808080;">Processing provides a simplified front end to Java, ideal for quick experiments</span></em></p>
<p>The green cells in Toxin were all generated using a Processing program. Each cell is a circle made up of a number of connected spline segments. I animate the cell by displacing the spline control points using Perlin noise. The cells are then resized and colourized in Photoshop.</p>
<p><img class="aligncenter size-full wp-image-542" style="border: 0px;" title="Toxin's cells being rendered in processing" src="http://richardjdare.com/blog/wp-content/uploads/2012/11/processing-cells.jpg" alt="" width="400" height="210" /><br />
<em><span style="color: #808080;">Left, a cell being rendered in Processing. Right, the finished result</span></em></p>
<p>As you can see, the finished product does not look exactly like the original Processing output. I had to do a lot of tweaking, re-rendering and reprocessing until I got them looking right. It was very time consuming. There were many moments where I wondered if I was going to get useable results at all.</p>
<p>Many of Toxin&#8217;s graphical effects have their origins in experiments I did with Photoshop blend modes. I discovered that certain combinations of layers and modes resulted in nice abstract animations when the layers were dragged around. I wrote photoshop scripts to perform these movements and spit out animation frames. Unfortunately these scripts revealed many bugs and inaccuracies in Photoshop and its scripting system. You only have to look through the source of my <a title="Texture Atlas Maker" href="http://richardjdare.com/blog/2009/08/texture-atlas-maker/" target="_blank">AtlasMaker</a> Photoshop script to see how many strange workarounds are required to accomplish straightforward tasks.</p>
<p>To solve these problems I implemented Photoshop&#8217;s blending modes in Processing, and use this to create my animations. Below are a few frames from Toxin&#8217;s shot animation that demonstrate the technique. The way the colours shift throughout the animation is down to the movement of multiple layers blended into a simple white shot sprite. You can get a better idea of how this looks in the game by looking at the <a href="http://richardjdare.com/blog/2012/10/toxin-for-iphone-and-ipod-touch/">screenshots</a> I posted a couple of weeks ago.</p>
<p><img class="aligncenter size-full wp-image-550" style="border: 0px;" title="Toxin Shots" src="http://richardjdare.com/blog/wp-content/uploads/2012/11/toxin-shots1.jpg" alt="" width="372" height="84" /><br />
<em><span style="color: #808080;">Many of Toxin&#8217;s sprites use animated image processing effects</span></em></p>
<p><em></em><br />
I also used Processing as a test bed to work out algorithms before implementing them in the game. For example, Toxin has a weapon which freezes the edges of groups of cells. It didn&#8217;t take long to prototype this in Processing.</p>
<p><img class="aligncenter size-full wp-image-553" title="Testing an algorithm in Processing" src="http://richardjdare.com/blog/wp-content/uploads/2012/11/cellbounds.jpg" alt="" width="480" height="368" style="border:0px" /><br />
<em><span style="color: #808080;">Testing a powerup that uses a flood fill algorithm to find edge cells</span></em></p>
<p>Stay tuned for more posts on Toxin, and a new version of my texture atlas Photoshop script which is in final testing&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2012/11/toxin-creating-sprites-in-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Toxin &#8211; Making Levels</title>
		<link>http://richardjdare.com/blog/2012/10/programming-toxin-making-levels/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=programming-toxin-making-levels</link>
		<comments>http://richardjdare.com/blog/2012/10/programming-toxin-making-levels/#comments</comments>
		<pubDate>Thu, 25 Oct 2012 18:07:42 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[Toxin]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[level design]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=335</guid>
		<description><![CDATA[Nearly everyone who asks me about game development will begin with the question, &#8220;How do you create levels?&#8221; The main way of going about it.]]></description>
				<content:encoded><![CDATA[<p>Nearly everyone who asks me about game development will begin with the question, &#8220;How do you create levels?&#8221;</p>
<p>The main way of going about it is to use a custom editing program. Developers either write their own specifically for each game, or they’ll use one provided by a third-party game engine like Unity, Source or UDK. Sometimes, they might take a popular modding tool like one of the Quake editors and use it for their own ends.</p>
<div class="row" style="text-align: center;"><a href="http://richardjdare.com/blog/wp-content/uploads/2012/10/unity-ed.jpeg" rel="lightbox[335]"><img class="wp-image-367 size-full" style="margin-right: 8px;" title="unity-ed-small" src="http://www.richardjdare.com/blog/wp-content/uploads/2012/10/unity-ed-small.jpg" alt="Unity Editor" width="230" height="171" /></a><a href="http://richardjdare.com/blog/wp-content/uploads/2012/10/tiled-ed.jpg" rel="lightbox[335]"><img class="wp-image-368 size-full" title="Tiled Map Editor" src="http://www.richardjdare.com/blog/wp-content/uploads/2012/10/tiled-ed-small.jpg" alt="Tiled 2d map editor" width="229" height="170" /></a></div>
<div class="row" style="text-align: center;"><a style="padding-right:120px;" href="http://unity3d.com/" target="_blank">Unity Editor</a><em><a href="http://www.mapeditor.org/" target="_blank">Tiled Map Editor</a></em></div>
<p>&nbsp;</p>
<p>These editors allow you to create levels visually; you can draw maps, position items and enemies, draw movement paths, edit dialog text and set up events to occur at different points in the level. All this information is saved in a structured file that the game is programmed to understand.</p>
<p>The complexity of these tools and the files they create depends on the game. Some of them are as large and complex as the 3d modelling tools used by animators and architects; others are simple programs coded up in an afternoon that allow a programmer to avoid some laborious data entry.</p>
<p>I’ve always enjoyed writing game development tools. My first map editor was written in 1994 in Blitz Basic on the Amiga for a strategy game I designed that went nowhere. I probably spent more time polishing the editor than working on the game itself. Not long after I wrote an isometric editor that let you sculpt terrain like in Populous, again, for a game that failed to materialise.</p>
<p>My last game, Starblaster didn&#8217;t use an editor; the levels were stored as a script, but when I started <a href="http://toxingame.com" target="_blank">Toxin</a>, my forthcoming iPhone game, I knew it was time to break out the old skills.</p>
<h3>The Toxin Level Editor</h3>
<p style="text-align: center;"><img class="size-full wp-image-337 aligncenter" style="border: 0px;" title="Toxin Level Editor main interface" src="http://richardjdare.com/blog/wp-content/uploads/2012/10/toxin-editor1.jpg" alt="Toxin Level Editor main interface" width="500" height="330" /><br />
<em><span style="color: #808080;">The Toxin Editor uses the same Opengl graphics engine as the iOS game </span></em></p>
<p>The Toxin Level Editor is my first ever Mac program, and it&#8217;s pretty clunky. It was my first attempt at writing a program in Objective C, using the famous Interface Builder which originally came out of <a href="http://en.wikipedia.org/wiki/NeXTSTEP" target="_blank">NeXTStep</a>. I always had a thing about NeXTStep, so it was exciting to finally write a program with its direct descendant.</p>
<p>Once I got the basics up and running, I needed to decide how I was going to save the levels. While many developers create their own file formats, I decided to use an existing one. That way I could use an off-the-shelf library to save time and effort.</p>
<p>Having worked in web application development for the past five years or so, I had some experience with <a href="http://en.wikipedia.org/wiki/XML" target="_blank">XML</a> and <a href="http://www.json.org/" target="_blank">JSON</a>. These are text based formats commonly used by web services to exchange information. I chose JSON as my level format because of it&#8217;s simplicity and readability. I use the <a href="http://stig.github.com/json-framework/">SBJSON</a> library in the editor and in the game for loading and saving.</p>
<h3>Levels in detail</h3>
<p>Toxin levels contain four main things. A list of cells, a list of linear events, a list of timed events and a set of general preferences that are applied to the whole level.</p>
<p>The cells are toxic blobs which spawn and multiply and gradually fill the playfield. The editor allows me to draw cells into the level and directly set their parameters.</p>
<p>Events in Toxin are instructions to the game to perform an action such as add an enemy to the game, set off an explosion or play a sound effect.</p>
<p>Linear events are fired off one after the other from the start of the level. They are the main way I add mobile enemies to the game. I can split the events into waves using a WAIT event which instructs the game not to add events until the previously created enemies are all dead. Enemies have standard parameters for position and angle, and a set of custom parameters defined as key, value pairs.</p>
<p>Timed events let me perform actions and spawn enemies at a specific time, or at a random time within a range. I did toy with the idea of having rule based events that I could fire off when certain game conditions were met. In my next game I plan to embed a scripting language to handle this kind of thing.</p>
<p><img class="aligncenter size-full wp-image-341" style="border: 0px;" title="Editing Level Events" src="http://richardjdare.com/blog/wp-content/uploads/2012/10/toxin-editor2.jpg" alt="Editing Level Events" width="400" height="307" /><br />
<em><span style="color: #808080;">Adding an entity to the level. The linear events list is used to construct consecutive waves of enemies. The entities can have many custom parameters</span></em></p>
<p>The levels are saved as a bunch of text files which are then zipped and put into the game. I think this was a mistake. While it&#8217;s a good technique for larger games, with Toxin I could have got away with storing them in a single file.</p>
<p>If I were working on a desktop game I would try and build the editor into the game itself, as well as give the game a console that lets me manipulate level data and game parameters during play. This isn&#8217;t really feasible on a mobile app, though you might be able to do it on a tablet. I do get tired of having to zip all my level files, then replace the zip inside the project and recompile every time I want to try something.</p>
<p>I have a lot more Toxin posts planned, so stay tuned. Let me know if you have any questions or want more details.</p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2012/10/programming-toxin-making-levels/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Toxin &#8211; Part One</title>
		<link>http://richardjdare.com/blog/2012/10/programming-toxin-part-one/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=programming-toxin-part-one</link>
		<comments>http://richardjdare.com/blog/2012/10/programming-toxin-part-one/#comments</comments>
		<pubDate>Fri, 19 Oct 2012 12:56:43 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[Toxin]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective c]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=243</guid>
		<description><![CDATA[Over the next few posts I want to write a bit about how my first iPhone game, Toxin, is being developed. Although I&#8217;ve been programming.]]></description>
				<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-244" style="border: 0px;" title="Toxin" src="http://richardjdare.com/blog/wp-content/uploads/2012/10/toxin-screen1.jpg" alt="" width="500" height="249" /></p>
<p>Over the next few posts I want to write a bit about how my first iPhone game, Toxin, is being developed. Although I&#8217;ve been programming for a long time, this is my first ever project in the Apple ecosystem; I hadn&#8217;t even used a Mac before starting this game.</p>
<p>Toxin is written in C++ inside a thin layer of Objective C. I did this for a number of reasons. Firstly I am much more familiar with C++. Secondly, as the most common game development language I knew I would have better luck finding additional libraries and answers to any development problems if I worked in it.</p>
<p>It also meant I could reuse some of my own code. Toxin actually contains code used in my first game, <a href="http://richardjdare.com/blog/games/">Starblaster</a> (2003). Some of it goes back to DirectX stuff I did in the late 90&#8217;s. I always liked having a sense of continuity in my work, a feeling that all my games are connected.</p>
<p>The Objective C layer handles iOS specific stuff like getting the app started up, passing touch information to the game logic, and setting up threads for Toxin&#8217;s concurrent level loading. The sound library I use, <a href="http://www.cocos2d-iphone.org/wiki/doku.php/cocosdenshion:faq" target="_blank">CocosDenshion</a> is also in Objective C. I wouldn&#8217;t rule out doing a whole project in Objective C. I&#8217;ve enjoyed learning it and I&#8217;ve used it to create several utilities already, including the Toxin Level Editor, which I will probably write about soon. I would, however, want to spend some time comparing the performance of the Objective C Container libraries with the STL beforehand.</p>
<h3>Engine? What Engine?</h3>
<p>Now, some of my friends believe that the main reason Toxin has taken me so long is because I didn&#8217;t use one of the popular 2d engines out there like <a href="http://www.cocos2d-iphone.org/" target="_blank">Cocos2d</a> and instead wrote all my own graphics code. They don&#8217;t believe me when I tell them that large engines are unnecessary for small games (though I would never stop anyone from using them) and that I could rewrite my entire graphics system in about 2 weeks.</p>
<div id="#whatsinbox" style="margin: 10px; padding: 8px; background-color: #ebebeb; height: 155px;">
<h4>What&#8217;s in Toxin</h4>
<ul>
<li style="float: left; width: 220px;">Batched sprite drawing</li>
<li style="float: left; width: 220px;">Resource management</li>
<li style="float: left; width: 220px;">Messaging system</li>
<li style="float: left; width: 220px;">2d particle system</li>
<li style="float: left; width: 220px;">JSON file loading</li>
<li style="float: left; width: 220px;">Zip file loading</li>
<li style="float: left; width: 220px;">Chipmunk physics</li>
<li style="float: left; width: 220px;">Oriented bounding box collision</li>
<li style="float: left; width: 220px;">Keyframe interpolation system</li>
<li style="float: left; width: 220px;">Concurrent level loading</li>
</ul>
</div>
<p>&nbsp;</p>
<p>My &#8220;engine&#8221; if you could call it that, is a basic set of classes for things like sprite drawing, line drawing, loading textures, and drawing basic primitive shapes. All sprite and line drawing is batched using vertex buffers for maximum speed. The classes are loosely related; I just add something when I need it, not worrying about architecture or anything. The whole thing is about 7000 lines of code, and that includes some significant utility classes I wrote, such as my keyframe interpolation system. I use this all over the place to animate transitions, colour blends and movements of all kinds. I wrote interpolators for many different data types and I&#8217;m sure I could reduce the lines of code by making them into template classes.</p>
<p>I used this graphics system to build Toxin&#8217;s particle engine which I based on John van der Burg&#8217;s Gamasutra article, <em><a href="http://www.gamasutra.com/view/feature/3157/building_an_advanced_particle_.php" target="_blank">Building an advanced Particle System.</a></em> The particles use some simple physics to bounce them off the inside of the elliptical playfield.</p>
<p>At one point, the game had elliptical gravity: When a particle hit the ellipse, gravity would activate for that particle and it would &#8220;fall&#8221; into the ellipse, over 360 degrees. It was a cool effect but it didn&#8217;t look like anything you would expect to happen, so I replaced it with simple bouncing which is much more satisfying to watch.</p>
<p>Some of the enemies in Toxin use the <a href="http://chipmunk-physics.net/" target="_blank">Chipmunk physics engine</a>. One of the things I&#8217;m proud of in this game is how you can easily mix and match different types of objects. Some use Chipmunk, others use simple bounding box and oriented-bounding box collision.</p>
<p>Game structure is accounted for by a basic game state manager with simple transitions and there&#8217;s a resource managment system that lets me load and unload graphics whenever I need to.</p>
<p>After reading <a href="http://www.mcshaffry.com/mrmike/">Mike McShaffry&#8217;s</a> <a href="http://www.amazon.co.uk/Game-Coding-Complete-Mike-McShaffry/dp/1133776574/ref=dp_ob_title_bk">Game Coding Complete</a> I wrote a messaging system which allows different parts of my game to easily communicate with each other with a minimal amount of dependency. I use this messaging system for spawning enemies, creating explosions, and playing sound effects.</p>
<p>Next time I&#8217;ll write something about how Toxin levels work, and I&#8217;ll describe my first native Mac program, the Toxin Level Editor. Please let me know if you have any questions or if you want more information about any of the things in this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2012/10/programming-toxin-part-one/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Toxin for iPhone and iPod Touch</title>
		<link>http://richardjdare.com/blog/2012/10/toxin-for-iphone-and-ipod-touch/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=toxin-for-iphone-and-ipod-touch</link>
		<comments>http://richardjdare.com/blog/2012/10/toxin-for-iphone-and-ipod-touch/#comments</comments>
		<pubDate>Sat, 13 Oct 2012 17:24:39 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[game development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Toxin]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=85</guid>
		<description><![CDATA[Its time to introduce my forthcoming game, Toxin! It&#8217;s my first game since 2003, and has been in development for erm, *too long*. I can&#8217;t.]]></description>
				<content:encoded><![CDATA[<p><!-- br {display:none;} --><img class="aligncenter size-full wp-image-232" style="border: 0px;" title="Toxin" src="http://richardjdare.com/blog/wp-content/uploads/2012/10/toxin1.jpg" alt="Toxin Screenshot" width="480" height="320" /><br />
Its time to introduce my forthcoming game, Toxin! It&#8217;s my first game since 2003, and has been in development for erm, *too long*. I can&#8217;t give a release date yet; every day I sit down and say, &#8220;this week I&#8217;m going to finish it&#8221;, but most of the code and artwork is complete. There are some cosmetic changes I want to make, so please bear in mind that these screenshots might not reflect the finished product.</p>
<p><img class="aligncenter size-full wp-image-236" style="border: 0px;" title="Toxin Screenshot 2" src="http://richardjdare.com/blog/wp-content/uploads/2012/10/toxin2.jpg" alt="" width="480" height="320" /></p>
<p>Toxin is an abstract puzzle/shooter for the iPhone and iPod Touch, running on iOS3.0 and above. The basic gameplay is simple. You control a ship that rotates around the edge of an elliptical playfield, shooting inwards. If anything touches the edge you lose energy. Lose too much energy and you die.</p>
<p>Inside the ellipse are 99 levels of swarming biological foes, multiplying toxic cells, physics puzzles and colourful particle effects.</p>
<p><img class="size-full wp-image-89 alignright" title="Microbes - Vision Software 1991" src="http://richardjdare.com/blog/wp-content/uploads/2011/11/microbes.jpg" alt="Microbes - Vision Software 1991" width="250" height="197" /></p>
<p>Toxin was inspired by a number of different games including Geometry Wars, Tempest, Space Invaders and Ebonstar, but the biggest influence was Microbes, released by Vision Software in 1991. I really enjoyed this back in the day, and like many innovative Amiga titles it seems to have disappeared from the consensual history of video games. You can play it with UAE, but I dont think the emulation is perfect.</p>
<p>I also tried to add some simple puzzle concepts to give the game more depth but without affecting the pace too much. The design process involved me sitting down with a coffee every night after work, staring at my sketchpad and thinking, &#8220;now what can I do inside an ellipse?&#8221;</p>
<p>&nbsp;</p>
<div class="row" style="text-align: center; margin-bottom: 4px;"><img class="size-full wp-image-95" style="margin-right: 8px;" title="Tempest - Atari 1981" src="http://richardjdare.com/blog/wp-content/uploads/2011/11/tempest.jpg" alt="Tempest - Atari 1981" /><img class="size-full wp-image-96" title="Vortex - Visionary Design 1988" src="http://richardjdare.com/blog/wp-content/uploads/2011/11/vortex.jpg" alt="Vortex - 1988 Visionary Design" /></div>
<div class="row" style="text-align: center;"><img class="wp-image-112 size-full" style="margin-right: 8px;" title="Ebonstar - MicroIllusions 1988" src="http://www.richardjdare.com/blog/wp-content/uploads/2011/11/ebonstar.jpg" alt="Ebonstar - 1988 MicroIllusions" /><img class="size-full wp-image-113" title="E-Motion - 1990 US Gold" src="http://richardjdare.com/blog/wp-content/uploads/2011/11/e-motion.jpg" alt="E-Motion - US Gold 1990" /></div>
<div class="row" style="text-align: center;"><em><span style="color: #808080;">Direct and indirect influences.Clockwise from left: Tempest, Vortex, E-motion, Ebonstar</span></em></div>
<p>Once this game is done, I am thinking about writing a Toxin &#8220;Remix&#8221; called Antigen. It will have the same ship-in-an-ellipse structure but the core game will be more puzzle based and slower paced as well as having different artwork. I think the idea of a game remix could work well at low pricepoints, a little like the way remix CDs used to accompany singles.</p>
<p>Soon I&#8217;ll write a little on how the artwork was done, and how it went through a long, painful process of development. I am also looking into producing a gameplay video, so stay tuned&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2012/10/toxin-for-iphone-and-ipod-touch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Website  &#8211; toxingame.com</title>
		<link>http://richardjdare.com/blog/2012/07/new-website-toxingame-com/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=new-website-toxingame-com</link>
		<comments>http://richardjdare.com/blog/2012/07/new-website-toxingame-com/#comments</comments>
		<pubDate>Thu, 12 Jul 2012 17:15:10 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[Toxin]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=220</guid>
		<description><![CDATA[The website for my forthcoming iOS game, Toxin, has gone live today. Its just a teaser at the moment. I&#8217;ll add more details as the.]]></description>
				<content:encoded><![CDATA[<p>The <a href="http://toxingame.com">website</a> for my forthcoming iOS game, Toxin, has gone live today. Its just a teaser at the moment. I&#8217;ll add more details as the game gets closer to release. I&#8217;ll also start posting here a bit more often too:)</p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2012/07/new-website-toxingame-com/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting the size of a PNG without loading it</title>
		<link>http://richardjdare.com/blog/2010/10/getting-the-size-of-a-png-without-loading-it/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-the-size-of-a-png-without-loading-it</link>
		<comments>http://richardjdare.com/blog/2010/10/getting-the-size-of-a-png-without-loading-it/#comments</comments>
		<pubDate>Sat, 09 Oct 2010 13:37:21 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[game development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[png]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=33</guid>
		<description><![CDATA[My current iPhone game (more info coming soon, I promise!) has a resource system that allows me to load texture maps when I need them.]]></description>
				<content:encoded><![CDATA[<p>My current iPhone game (more info coming soon, I promise!) has a resource system that allows me to load texture maps when I need them and unload them when I don’t. This helps makes the game very memory efficient, but it had one annoying flaw. At program startup I had to load and unload several textures to get size information I needed to set up other game structures. To avoid this wasteful operation I started looking at ways to extract information from a png without loading the whole thing.</p>
<p>This <a href="http://stackoverflow.com/questions/1551300/get-size-of-image-without-loading-in-to-memory">StackOverflow discussion</a> was useful, along with this <a href="http://www.w3.org/TR/PNG/">guide to the png format</a> from the W3C, but unfortunately, Apple threw a spanner in the works.</p>
<p>If you’ve been programming the iPhone for a while you’ll know that by default, Xcode premultiplies the alpha channel of PNG files before adding them to the application bundle. It also adds its own undocumented data chunk to the beginning of the file, breaking PNG guidelines which state that the IHDR chunk should always be at the beginning.</p>
<p>This Objective C function will return the size of an image for both standard and Apple PNGs. It scans through the file looking for the IHDR chunk, making no assumptions about its position. It should be easy enough to convert to other members of the C family.</p>
<div style="margin-top:32px;">
<style>
.wp_syntax{overflow-y:auto;max-height:400px};
</style>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//----------------------------------------------------------------------------</span>
<span style="color: #11740a; font-style: italic;">// GetPNGSize() - gets PNG Dimensions without loading any data</span>
<span style="color: #11740a; font-style: italic;">//----------------------------------------------------------------------------</span>
<span style="color: #a61390;">BOOL</span> GetPNGSize<span style="color: #002200;">&#40;</span><span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>fileName,CGSize <span style="color: #002200;">*</span>pngSize<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// PNG Signature</span>
    <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">char</span> png_signature<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">8</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#123;</span><span style="color: #2400d9;">137</span>, <span style="color: #2400d9;">80</span>, <span style="color: #2400d9;">78</span>, <span style="color: #2400d9;">71</span>, <span style="color: #2400d9;">13</span>, <span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">26</span>, <span style="color: #2400d9;">10</span><span style="color: #002200;">&#125;</span>;
    <span style="color: #11740a; font-style: italic;">// IHDR chunk identifier</span>
    <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">char</span> ihdr_signature<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">4</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">=</span><span style="color: #002200;">&#123;</span><span style="color: #bf1d1a;">'I'</span>,<span style="color: #bf1d1a;">'H'</span>,<span style="color: #bf1d1a;">'D'</span>,<span style="color: #bf1d1a;">'R'</span><span style="color: #002200;">&#125;</span>;
&nbsp;
    <span style="color: #a61390;">FILE</span> <span style="color: #002200;">*</span>fp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>fp <span style="color: #002200;">=</span> <span style="color: #a61390;">fopen</span><span style="color: #002200;">&#40;</span>fileName,<span style="color: #bf1d1a;">&quot;rb&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">NULL</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// load png signature and check it</span>
    <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">char</span> sigBuf<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">8</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">fread</span><span style="color: #002200;">&#40;</span>sigBuf, <span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">8</span>, fp<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">memcmp</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>sigBuf<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&amp;</span>png_signature<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>, <span style="color: #2400d9;">8</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">fclose</span><span style="color: #002200;">&#40;</span>fp<span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// examine chunks until we find IHDR</span>
    <span style="color: #a61390;">BOOL</span> done <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
    <span style="color: #a61390;">while</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>done <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #a61390;">feof</span><span style="color: #002200;">&#40;</span>fp<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// get chunk length and type</span>
        <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">char</span> chunkInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">8</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">fread</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>chunkInfo, <span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">8</span>, fp<span style="color: #002200;">&#41;</span>;
&nbsp;
        <span style="color: #a61390;">long</span> length <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>chunkInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">24</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span>chunkInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">16</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span>chunkInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">8</span><span style="color: #002200;">&#41;</span> | chunkInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// is this the IHDR?</span>
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #a61390;">memcmp</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>chunkInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">4</span><span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&amp;</span>ihdr_signature<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>, <span style="color: #2400d9;">4</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">char</span> sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">8</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">fread</span><span style="color: #002200;">&#40;</span>sizeInfo,<span style="color: #2400d9;">1</span>,<span style="color: #2400d9;">8</span>,fp<span style="color: #002200;">&#41;</span>;
&nbsp;
            pngSize<span style="color: #002200;">-</span>&gt;width <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">24</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span>sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">16</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span>sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">8</span><span style="color: #002200;">&#41;</span> | sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span>; 
            pngSize<span style="color: #002200;">-</span>&gt;height<span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">4</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">24</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span>sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">16</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span>sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">6</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">8</span><span style="color: #002200;">&#41;</span> | sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">7</span><span style="color: #002200;">&#93;</span>;
            done <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">else</span> 
        <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// skip over the data and checksum and go to next chunk</span>
            <span style="color: #a61390;">fseek</span><span style="color: #002200;">&#40;</span>fp, length<span style="color: #002200;">+</span><span style="color: #2400d9;">4</span> , <span style="color: #a61390;">SEEK_CUR</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">fclose</span><span style="color: #002200;">&#40;</span>fp<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span> done;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

</div>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2010/10/getting-the-size-of-a-png-without-loading-it/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
