Showing posts with label wp7dev. Show all posts
Showing posts with label wp7dev. Show all posts

Sunday, March 27, 2011

A follow-on to "IC Game Design – A Key Tip for Success"

Michael Quandt posted a good tip about entering your WP7 app into Imagine Cup Game Design: basically, don't put your game in the Games Hub! The emulator doesn't include the Games Hub, which means when your .xap file gets deployed to the emulator, your game won't be accessible.

But how to actually fix this? Luckily, it's a simple change: in your project, find the WMAppManifest.xml file and change Genre="Apps.Games" to Genre="Apps.Normal". Now when you deploy your .xap to the emulator, it'll show up in the app list rather than the Games Hub.

Thursday, March 3, 2011

High Quality Dithering for Windows Phone 7

In my last post I showed how to use a 32bpp backbuffer to prevent banding in XNA games. But as it turns out, the WP7 hardware specifications only specify a 16-bit LCD at minimum - and that there are devices out there which only have a 16-bit LCD. (I've heard, but have not been able to confirm, that the Samsung devices suffer especially from this).

In that case, if a device only has an LCD capable of 16bpp, setting a 32bpp backbuffer isn't going to help. Instead, you need to dither your images, which will reduce the bit-depth to 16bpp but in a way which reduces visible banding.

I stumbled across this blog post "Photoshop Action for Windows Phone Dithering", which provides a photoshop script to dither images. It gets the job done, but I noticed that it produces a fairly low-quality dither - the end result is very grainy to my eyes.

A while back when I worked on Metropolitan Melbourne, I wrote my own little tool to dither images because WM6.5 only supported 16-bit colour graphics and I couldn't find any other simple tools for high-quality dithering. My tool is called ReduceBitDepthCPP, and takes an input 24-bit/32-bit image in .png or .bmp format, and reduces it to 16-bit (R5G6B5) colour using the Floyd-Steinberg algorithm. The Floyd-Steinberg algorithm eliminates banding while minimising quantisation error. Here's a comparison picture:


As expected, the undithered gradient has extreme banding. The "Photoshop" image was produced by using the script mentioned earlier. If you look closely, the Photoshop image does get rid of banding but also introduces a lot of noise - which makes the image very grainy.

On the top right is the Floyd-Steinberg algorithm produced by my tool, ReduceBitDepthCPP. It also eliminates banding but does so in a way that doesn't introduce a lot of noise, and the end result looks very close to the original.

You can download ReduceBitDepthCPP below, with source as well. It's free for any use, and the source code is distributed under the MIT license.


Instructions

ReduceBitDepthCPP is a command-line tool, which makes it easier to integrate into your build. To use it:
  1. Extract the files somewhere (eg. C:\Temp\ReduceBitDepth\)
  2. Open up a command prompt by pressing [win]+[r] and typing in "cmd"
  3. Navigate to the directory where you extracted the files
  4. Run ReduceBitDepthCPP.exe using the following syntax:
    ReduceBitDepthCPP.exe infile outfile
Sample:

ReduceBitDepthCPP.exe C:\Temp\meow_orig.png C:\Temp\meow_dithered.png

If you found this useful, don't forget to follow me on Twitter, and check out some of the stuff on my website!

Happy coding!

Wednesday, February 23, 2011

Tip of the Day: 32bpp BackBuffers in XNA

Here's a tip: by default, XNA uses a 16-bit R5G6B5 back buffer format. This causes horrific banding if you use lots of gradients in your game. You can fix this by manually telling XNA to use a 32-bit backbuffer format.

In you Game class's constructor, add this line:

graphics.PreparingDeviceSettings += PreparingDeviceSettingsEventHandler;


And somewhere in your Game class, add this method:

void PreparingDeviceSettingsEventHandler(object sender, PreparingDeviceSettingsEventArgs e)
{
    var pp = e.GraphicsDeviceInformation.PresentationParameters;
    pp.BackBufferFormat = SurfaceFormat.Color;
}


The difference won't show up in the emulator. But on an actual device, it'll vastly increase your image quality by getting rid of a lot of your banding.

Monday, September 6, 2010

NextWar: Now in Landscape!

Work continues. Newest feature: landscape support!







Next on the to-do list: two-finger rotate.

Friday, September 3, 2010

Pinch zooming using XNA4 on WP7: Getting it right

If you don't care about the explanations and just want to skip to the code, click here.

Pinch-to-zoom is one of the basic multi-touch gestures. It's an intuitive way to zoom in/out and is found pretty much anywhere multi-touch is found. It's really easy to implement but it seems that more often than not, people get it wrong. Unfortunately, even Nick Gravelyn's touch-gesture sample on the XNA website got it wrong. (But to be fair, the sample was more about demonstrating the API than what you could do with it.)

Let's first take a look at how pinch-to-zoom is supposed to work.

The Gold Standard

If you have an iPhone or iPod Touch, pull it out now and view a big image. Find two "landmarks" in the image: spots which are easy to precisely identify in an image - for example the tip of a skyscraper or somebody's nose. Find two of these spots, place a finger on each and pinch together. As you pinch you'll notice that your two chosen landmark spots still remain in place underneath your fingers.

This is how pinch-to-zoom is supposed to work. Each of your two fingers should act as an "anchor" on the image - once the two fingers are pressed on the screen, the points on the image underneath each finger should stay stationary relative to their respective finger.

Okay, so maybe that explanation wasn't very clear. Maybe an image or two will help:




Now you can see what I mean: the points on the image underneath each finger don't move relative to the finger. After the gesture is complete our friendly koala's eye is still under the thumb and the top of his head is still under the index finger.

Implementing it

Okay, so how do you actually implement that? The easiest way would be to use matrices - using matrices you can easily define the center of scaling to handle much of this for you. But the SpriteBatch.Draw method doesn't allow you to specify a transformation matrix - so you have to make do specifying a scale and a position.

Scaling

Scaling is pretty simple. In your GestureSample, you get position and a delta for each finger. If you subtract the delta from the position, you have the previous position of each finger. And the amount you need to scale by is simply the ratio of the old and new distances between each finger.

position1 = position of finger 1
position2 = position of finger 2
delta1 = delta of finger 1
delta2 = delta of finger 2

oldPosition1 = position1 - delta1;
oldPosition2 = position2 - delta2;
newDistance = dist(position1, position2);
oldDistance = dist(oldPosition1, oldPosition2);
scaleFactor = newDistance / oldDistance;

The scaleFactor is the amount you multiply your object's scale by, not the absolute scale of your object. In other words, it doesn't determine your object's scale directly, only how much to change your object's scale by. In pseudocode:

float scaleFactor = GetScaleFactor();
obj.Scale *= scaleFactor;
renderer.Draw(obj.Position, obj.Scale);


Translation

Translation is a little bit trickier. We've found the amount to scale our object by, but remember that the points underneath each finger shouldn't move relative to the finger - those points should be "anchored" to each finger. So that means we need to translate the object in such a way that whatever was under each finger before the pinch motion are still under the finger afterwards. Again, in pseudocode:

newPos1 = position1 - (oldPosition1 - obj.Position) * scaleFactor;
newPos2 = position2 - (oldPosition2 - obj.Position) * scaleFactor;
obj.Position = midpoint(newPos1, newPos2);

Note: this assumes that obj.Position is in screen-space!

Where the definitions of position1, position2, etc. are the same as previously.

An explanation:

Ignore one of the fingers for now - consider only one of the two fingers. You know that the finger moved by some amount this frame, and the object has also been scaled by some amount this frame. And you want to ensure that the point on the object underneath the finger previously is still underneath the finger now.

All that the code does is find the difference between the object's position and the finger's old position, scales it according to the scaling factor, then adds it to the finger's new position. But wait! There are two fingers - which one do we use? Well, both, actually - which is why we take the midpoint of the two possible new positions.

Summary

That's it. If you apply these simple formulae for the scaling and translation of your objects, your pinch-zoom behaviour will be correct and will "feel" right to your user. Reading and translating all that obtuse prose can be a pain, I know, so I've also provided some sample code that you can use.

The Code

There isn't much to it, really. I've provided a static class PinchZoom which contains the functions necessary to implement pinch zooming, as well as a sample project. You'll need a multi-touch capable monitor or an actual WP7 device to test it, of course. Alternatively you can try this excellent multi-touch simulator drop-in component.

PinchZoom.zip Sample project (789KB)
PinchZoom.cs (5KB)

Here it is in action:

Friday, August 27, 2010

NextWar for WP7 at TechEd

If you were at TechEd Australia 2010, you might have seen NextWar at the keynote on Tuesday. Mad props to Dave Glover for doing that - getting it out there in front of 3000 pairs of eyes was certainly some nice free publicity. ;)

At istartedsomething, Long Zheng (coincidentally also an MHS old boy) has a photostream that includes a couple of photos of NextWar running on prototype hardware.

Dave lent me his TabletPC and his prototype WP7 device so I could test on it before the demo at TechEd. It was only prototype hardware and not representative of the final product, but it ran amazingly well. Prototype or not, it looks amazingly good running on an actual device!

There'll be some more updates coming soon, so stay tuned!