TouchStudio for Windows Phone

image

If you want to create programs on your Windows Phone 7 device you can of course use the delightful Iron7 implementation of Ruby. This works startlingly well, but using it you are painfully reminded of the fact that mobile phones are best suited for consuming programs, not creating them.

TouchStudio is a project from Microsoft Research that aims to change that a bit. It has been designed from the ground up to work with a touch interface, rather than in spite of it. The commands and the syntax of the language have been created to make it easy and engaging to write applications for the phone, on the phone. There is a nice paper that sets out how it works here here and the project web site here. If you search Windows Phone Marketplace for TouchStudio you can download the latest version for free.

The programming model is easy to pick up, although the language syntax is different from ones you might have seen before. I don’t see this as a bad thing actually, I reckon that programmers should be used to the idea of using different types of language. What makes it stand out for me is the ease with which you can create stuff, and the level of integration they have with the phone. You can work with pictures, maps, music and even set up phone calls from within the language. The web integration is very good, in around 10 minutes I had a program that would fetch my Xbox avatar, convert it to black and white, display it and then save it as a picture on my phone:

My first TouchStudio program

I need to do a bit of scaling and tidying up, but the program itself is simple enough:

action AvatarDisplay() : Nothing { 
  pic := web->download_picture('http://avatar.xboxlive.com/avatar/rob the bloke/avatar-body.png'); 
  pic->desaturate; 
  pic->post_to_wall; 
  wall->screenshot->save_to_library; 
}

There are also a bunch of built in programs that play games or even do some simple image processing. This is what one of “\themify Picture” did to one of my photos:

Touch Studio Dalek

If you want to while away some time with your phone and actually achieve something fun and maybe even useful, you should grab a copy and have a play.

LG Optimus Quest 2

Optmis Quest2

Following on from the hugely successful Optimus Quest competition, we now have the inevitable sequel. I’ve got two brand new LG Optimus 7 devices up for grabs. This is a really good Windows Phone with 16G of internal memory, a lovely screen and a nice metal body.

This time the competition is different. All you have to do is send me your download stats from Windows Phone Marketplace and at the end of July and the end of August I’ll give a brand new LG Optimus 7 Windows Phone to the Hull Student with the most downloads. I’ll be checking the numbers with Microsoft, so no photoshopped graphs or tampered screenshots please .

Note that this is open to Hull Students only, and not those who have just graduated (sorry Harry). If you have applied to join us in September, you are eligible to enter too, as long as we have your details on file in our admissions system. 

Just email me your entries at optimusQuest2@robmiles.com before the end of July and I’ll send you a phone if you are the best. Then we do the whole thing again in August.

Some tips to optimise your Marketplace stats:

  1. Always give a trial mode. If you are selling your game, make it a free download with a trial mode so that users can get an idea of what it is like for free.
  2. Release updates. Don’t finish all the levels and then put the game out there. This means you will be late to market. Instead you can release level 1 in trial mode and then add features and functions as you go along. Harry Overs is doing this very successfully with Destruction Golf, which is picking up quite a following on the Marketplace. Bottled Games and Rusty Spoons also deserve a mention here too, as they have applications out there which are chugging along nicely.
  3. Take trouble over your icon and description. Don’t do these the night before you submit. These are your “shop window” and the better they look and the more enticing the text the more chance you have of catching the eye of a customer.
  4. Move your game around the Marketplace. If you are not sure which category your game fits into, move it into different ones (within reason) then you can find the one where it attracts the most attention.

Don’t worry if you only get a few downloads, send me your entry anyway. If everyone else forgets to enter you might sneak in and win the prize. Oh, and sign up for rewards at http://www.my-rewards.com. That way you will win something whatever happens. And enter the Windows Phone competition at the Microsoft UK Student Blog.

Don’t Forget Memory of a Goldfish

GoldFish

David Parker, one of our postgrad students, has made a very nice Windows Phone pelmanism (matching pairs) game for windows phone. You can find it on the Windows Phone Marketplace by looking for “goldfish”.

I think I really ought to create a showcase page for Hull applications that have made it to the device (this would of course include Cheese Lander). If you have created an application or game for WP7 please get in touch and I’ll add you to the list.

Hull Student Winners

image

Forgot to mention that a couple of weeks ago one of our students was lucky enough to win an Acer Iconia (which looks awesome) in the Think.Dev weekly awards. All Josh had to do was sign up and submit some applications to Windows Phone Marketplace.

If you fancy some of this action (and who wouldn’t) you can sign up here.

In other Hull student news, Harry Over’s version of Destruction Golf has been riding high in the Windows Phone Marketplace for a while now, getting loads of downloads and comments for what is an on-going development.

A Tip: Don’t wait until all your game levels are complete before putting the game out into the Marketplace. It is very easy to release upgrades to an existing program and quite a few developers are producing “episodic” content where they are releasing new versions at regular intervals.

XNA Windows Phone Session at DevDays 11

Windows Phone Audience Left

Windows Phone Audience Right

Two more halves of a room and one last Windows Phone XNA session. I seem to have taken a lot of audience pictures over the last few weeks. Thanks to you all for making the final session such a great one.  Remember folks that you can get all the code that you saw  from my demo pages:

/demos

The marks the end of my World Tour for a little while. Thanks to Helen for organising the European leg so well, and to Microsoft for inviting me along.

Windows Phone XNA in TechDays Antwerp

Metropolis1000

This is the Metropolis in Antwerp. Nice venue.

Audience

This is my usual picture of the audience arriving.

Did my first Windows Phone session of this part of my “World Tour” ™. I’m at Tech-Days in the Netherlands tomorrow and Friday. Thanks for being a great audience folks, although with the bright lights it was a bit hard to see if there was anyone out there (although I did hear some clapping from time to time). You can find all the code I talked about, and a lot more, in my demo directory:

/demos

Ready for Tech Days

clip_image001

I’m doing some bits and bobs at TechDays around Europe this week, starting with a Windows Phone gaming session in Antwerp on Wednesday 27th, then a keynote session, a Micro Framework session and a Geek Night Windows Phone session in rhyme on Thursday at The Hague in the Netherlands. On Friday, assuming I survive, I’m doing another Windows Phone gaming session. At the moment I’m busy preparing all the content and demos. I’m making something completely new for the keynote, and I’m rather pleased to find that it is mostly working, which is nice. 

You can find out about TechDays in the Netherlands here:

http://www.techdays.nl/

You can find out about TechDays in Antwerp here:

http://www.microsoft.com/belux/techdays/2011/

I head out tomorrow, I’m trying to squeeze a robot, microcontroller, laptop and all my other bits and bobs into a single carry on bag.  

Positioning Sprites with Rectangles and Vectors in XNA

Prada

Staying with the XNA techie theme, now a word about performing sprite positioning in a game. A sprite is something that has a texture (the image to be displayed when the sprite is drawn) and a position (the place to draw it). Generally sprites map onto objects in a game. If I am creating a game and I need to draw a bat, ball, spaceship, alien or asteroid I will use a sprite to do this.

There are two different ways to position sprites in XNA and each has its advantages and disadvantages. However, it is worth knowing about both.

Rectangle Positioning

With this form you create a rectangle which tells XNA where to draw the item, and how big it is:

Rectangle r = new Rectangle(0, 0, 200, 100);
spriteBatch.Draw(texture, r, Color.White);

This draws our texture in the top left hand corner (0,0) , in a rectangle 200 pixels wide and 100 pixels high.

Using a rectangle like this works well, it also gives you a ready made “bounding box” which you can use to test for collisions:

if (rect1.Intersects(rect2)) {
    // We have a collision between rect1 and rect2
}

However, there are some bad things about using rectangles that make me less keen on them

  • Rectangles are positioned using integers. The x and y properties you use describe where they are do not have a fractional part. This means that if you want to move a rectangle slowly around the screen (i.e. less than a pixel per update) you can’t just use the x and y properties to do this.
  • You can use rectangles to scale drawing, but this gets a bit tedious as you have to work out the size of rectangle you need, and you also need to be mindful of the aspect ratio of the item you are drawing so that it doesn’t end up squashed or stretched.
  • It is impossible to rotate a sprite which is positioned using a rectangle

So, rectangles are good for very simple sprites, but once you have become more familiar with XNA I think it is worth moving on to Vector positioning.

Vector Positioning

A vector is something that has direction and magnitude. That sounds posh. Actually it just contains an X and Y value, just like a coordinate. The “direction and magnitude” bit kicks in if you draw a line from the origin (0,0) to the X, Y position given. This line is the vector. The direction is the way the line points, and the magnitude is how long the line is.  In XNA terms the Vector2 type is the one you can use to position 2D sprites:

Vector2 v = new Vector2(10, 10);
spriteBatch.Draw(texture, v, Color.White);

This code draws the texture at position (10,10) . The texture is drawn in whatever size it happens to be, i.e. if the texture image was 200 pixels by 100 it would be drawn that size.  This means that you might need to scale your textures to fit a particular screen size – but as we shall see later this is not  a huge problem.

There are quite a few good things about vectors though.

  • The X and Y values of a vector are floating point, so you have very good control of sprite speed
  • The XNA framework supports vector mathematics directly. So you can write code like this:

    position = position + speed;

    - where position and speed are both vectors

When it comes to scaling and rotating a sprite positioned using a vector you can use a more complex version of the draw command (find a detailed description here) to do all this. Also, bear in mind that if you targeting Windows Phone you can fix your resolution in the game to a particular value and then make all your to assets fit.

graphics.PreferredBackBufferWidth = 480;
graphics.PreferredBackBufferHeight = 800;

The phone hardware will scale the display automatically to match whatever size you specify, now and in the future.

If you want to detect bounding box collisions you can write code like this:

public bool Contains(Vector2 pos)
{
    if (pos.X < position.X) return false;
    if (pos.X > (position.X + texture.Width)) return false;
    if (pos.Y < position.Y) return false;
    if (pos.Y > (position.Y + texture.Height)) return false;
    return true;
}

This returns true if the area covered by the sprite contains the given position.

For bounding box collisions you can use this:

public bool Intersects(Sprite c)
{
    if (pos.X + texture.Width < c.pos.X) return false;
    if (c.pos.X + c.texture.Width < pos.X) return false;
    if (pos.Y + texture.Height < c.pos.Y) return false;
    if (c.pos.Y + c.texture.Height < pos.Y) return false;
    return true;
}

This test will tell you if the bounding box around the two sprites intersects. However, if the sprites are textures that don’t fill the entire sprite rectangle this test is not a very accurate one. I’ll be covering pixel level collision detection next.

Game Object State Management in XNA

Vegas Building

Hmm. This sounds a bit deep for a Saturday blog post. I suppose it is, but I got asked a question at Mix 11 and I’ve been pondering it ever since then. The question was about some code like this:

class Cloud : CloudGame.ISprite
{
    public Texture2D CloudTexture;
    public Vector2 CloudPosition;
    public Vector2 CloudSpeed;
    public bool Burst = false;

    public void Draw(CloudGame game)
    {
        if (!Burst)
            game.spriteBatch.Draw(CloudTexture, 
                                                                       CloudPosition, 
                                                                       Color.White);
    }
   // rest of cloud code here
}

This is a tiny part of my “Cloud Bursting” game demo, where players touch clouds to burst them. The above code is the definition of the cloud itself. This has a texture, position, speed of movement and a Burst flag as properties.

Burst is how the cloud “knows” if the cloud has been burst. If the cloud is burst it is not drawn. At the start of the game all the clouds have the Burst flag set to false to indicate that they are still part of the game. As the player touches them the Burst flag is set true.

You see this in a lot of game play situations. Anywhere that things are “killed” they have to be able to remember whether they are still alive or not. The code above is simple and works well, at any instant a cloud is either burst or not. If it is burst (i.e. the Burst flag is true) plays no further part in the game and is not drawn.

However, this design gave rise to the question: “Why do we need to have a flag? Surely it would be more efficient to remove a ‘burst’ cloud from the list of things to be drawn?”

This is a very good question. To understand it you need to remember that in a game we will have many objects on the screen at any given time. To manage this the game contains a collection of some kind (usually a List). During Update and Draw the game works through the list acting on each item in it. If an item is removed from the list it will no longer be part of the game.  When the player bursts a cloud, rather than setting a flag you could just remove that cloud from the list of active game objects.

This approach has the advantage that it is more efficient. Instead of a cloud having to decide not to draw itself the game never tries to draw burst clouds at all.  However, it also adds complication. The game must now have a “master” list of clouds and an “active” list of clouds. At the start of a game the master cloud references are copied into the active list. This is to avoid the overheads of creating and destroying objects, something you really don’t want to be doing on a regular basis.

Furthermore, the time that is saved is probably not going to be that much use to us. If the game works fine with all the clouds visible (which it should do) then saving small amounts of processor time when some clouds are removed is not going to make that much difference to performance. In this situation it is the drawing of the objects that takes the time, not deciding whether or not to draw them.

The fundamental principle here is that you should go for simplicity first, then optimise when you discover you have a performance issue.  I like having the flags present, it makes for greater cohesion and easier debugging. I can look at any cloud and decide whether or not it should be drawn simply by inspecting the flag.  If the flag wasn’t there I’d have to check which lists held the cloud, and so on.

So, I go with internal flags, not lists, which is pretty much what I said at the time as I remember.

Some Mix Videos to Watch

Walk

If you weren’t at Mix and you want to watch the video of my session you can now view it here:

http://channel9.msdn.com/Events/MIX/MIX11/EXT06

You can get all the demo code that I talk about here:

/demos

This afternoon I was invited to take part in a Live Channel 9 session on video game design. Also on the stage was Brandon Foy, perhaps the coolest person I have ever met. He is the creator of an astonishing Windows Phone fan video that you can view on YouTube here:

http://www.youtube.com/windowsphone

Microsoft have said that if they get more that 200,000 views they will pay to put it out as a TV advertisement.  So, if you want to cost Microsoft some money, go take a look. Actually, it is a stunning piece of work and well worth 2 minutes of anyone’s time.

The video game session itself was great fun, as well as Brandon there was Nic Fillingham  from the XNA team and John Papa kept us all under control. For me the interesting thing was how much we all agreed on what is important in video game design. In a nutshell:

  • Figure out what your game is about and then protect this idea against all who would wish to “improve” it.
  • Get help with the graphics. Programmers think they can solve any problem with software. They probably can, but graphical design is not a problem as such. You really need someone with a good eye for that, and a coder is not guaranteed to be that person.
  • Put your game into the hands of people who can give you proper feedback. This doesn’t mean family or friends, this means those whose only reason the like the game is the game itself.
  • Design in the persistence features right at the start. A phone game will have to stop and start. When you make the game objects you need to figure out which bits must be saved and when. Adding this stuff right at the end of the development is really hard. If you have this stuff there all the time it means it will be properly tested when you send the game out.

After this I staggered back to the hotel to do some packing. My flight leaves at 7:15 am tomorrow. Lovely.

Mix 11 Keynote Windows Phone Fun

Scott Guthrie at the keynote
Scott Guthrie describing all the new goodies for Visual Studio and Windows Phone Mango.

Went to the Mix 11 keynote today. The new Windows Phone version, codenamed Mango and due for release later this year, looks very good. It seems as if they’ve worked through the wish list from the previous release and just about added everything.

Mango gives task switching, background processing (in a very well managed way), sockets, an on-board database, application powered Live Tile support and some lovely enhancements to the user interface. Furthermore, the emulator enhancements in Visual Studio will make it really easy to simulate the accelerometer and GPS system. And we will also be getting some impressive performance monitoring tools.

Applications will also now be able to use both Silverlight and XNA components in the same program, which will make creating game menus much easier. There was so much stuff I’ve probably forgotten something..

The tools will be available next month, the phones start being upgraded around autumn. This is great news for Windows Phone lovers like me, and anyone who fancies following the easiest path to mobile development that there is.

After the keynote I went for a wander.

Kinect Powered Lazyboy

This is a Kinect controlled LazyBoy recliner. Just move your hands in front of the sensor to move around and even recline the seat. Amazing.

Curious Cloud readings

Over at the Curious Cloud stand they are now getting readings from teams who have devices that are uploading sensor data to the cloud.

Mix 11 Windows Phone Booth Session

BoothAudience
I asked the audience to sit down and look at me as if I’d just said something amazing. They did a great job. Such good sports. I wasn’t intending to produce a panoramic shot, but the stitching seems to have worked quite well. The guy with the accordion is one of the few people out there who has actually bought a copy of Cheese Lander. Make of that what you will.

In the afternoon I got to do my phone session again, as stand-up. Went very well, I was determined to get someone on every seat watching me and by the end I kind of had.

For those of you who made it through to the end, especially the VB developer who went away with a copy of my book, thanks for being there, I’d have felt really silly if I’d just been presenting in front of nobody.

Next stop, Ask the Experts, starring me as an expert. How could that go wrong…..

Mix 11 XNA Madness

Room Left

This is the left hand side of the room. The right hand side were just as good looking. You can find that picture on Flickr, just click through to get there.

Just done my Mix 11 session. Great fun. Needed nerves of steel (or at least corrugated cardboard) when the video projector shut down just at the start. Fortunately the audience was super terrific. And most of them like cheese. Some great questions at the end.

You can find the slides for my session here.

I’ve made a brand new demos site with all the session demos for today, plus loads of other Windows Phone good stuff. You can find it all here:

/demos

I’ll keep this up to date with any new stuff I write, and move some other things in there that are presently hard to find.

If you want to see me go through any of the other demos, catch me at the Windows Phone Booth today at 2:00.

Rube Goldberg and the Micro Framework at Mix

Curious Cloud Poster

I’m going to try and watch the local news over the next few days. I’ll be looking for reports of folks performing strange contortions under street lights and near air conditioning vents. If I do see any, I’ll know what it is about though. They’ll be people taking part in the Rube Coldberg Curious Cloud Contest. The idea is simple enough. You get a magic device that responds to temperature, light and motion. By getting the right conditions of these you can get three lights on the device to come on. This makes you a winner.

It turns out that, with a bit of experimentation, anyone can get the lights to come on (particularly if they take a look at the C# code that powers this .NET Micro Framework based contraption). So the biggest prizes go to those who can make them come on in the most interesting way. Dressed as a chicken perhaps, or using a robot, or on the street. Or all three.

Curious Hardware

The hardware doing all this is pretty impressive. A netDuino board sits at the bottom, talking with a WIFI adapter and a sensor/display board. It captures readings from the sensors to an on-board memory card. When the device is able to detect a WIFI signal it will upload all the recent readings to the Cloud, where anyone can take a look. So, in a package around the size of a cigarette pack we have a .NET capable processor, sensors, data logging and wireless networking.

I got to have an early play with one of the kits, you can find my sensor readings here:

I’ve not found the magic values yet, but I reckon the fun is in the journey.

Colin Miller, of the .NET Micro Framework team, is dishing out complete kits to lucky Mix delegates who want to form a team and want to have a go. He will be on hand from tomorrow in the Connect Lounge (at Shorelines A) dishing out kits for you to play with.

Curious Cloud Box

The kits are beautifully presented, with all you need to get started, including a battery pack so you can take your sensors out and about.  You could even use a Windows Phone app to visualise your results.

Find out more at http://curiouscloudcontest.com when the competition opens tomorrow.

The Rob Miles Roadshow

Next week I’m doing a Mix session all about game development in XNA, stay tuned for some brand new examples and sample code, but unfortunately no new jokes. I’ve just found out that I’m also doing Ask the Experts on Tuesday evening starting at 6:00 pm  (and for once I’m not going to be doing the asking).  They are using a “speed dating” arrangement where we meet up with a bunch of folks for 20 minute slots where we get over the gist of what we are at Mix for. I’ll be doing Windows Phone games (of course) and I’d love to meet you if you come along. Strange this, nothing for years and then two sessions of speed dating in a matter of a weeks.

image

If you are lucky enough to have a Windows Phone you can download the official Mix application which is great. And also mentions me. Search the marketplace for “Mix 11”.

The “Rob Miles Roadshow”™ then rumbles on to TechDays later this month. They also have a really good Windows Phone application too, although this one works best if you can speak Dutch. Search the marketplace for “Techdays”.

DevDays Speaker

I’m doing a session on Windows Phone games, another on the .NET Micro Framework and a third (deep breath) where I’m going to be talking about Windows Phone development in rhyme, just for Geek Night. And I’m also making an appearance at one of the keynotes too. Now, if that’s not a reason to buy a new pair of jeans, I don’t know what is… Find out more here.