Summer Bash Fun and Games

Hull Summer Bash Team Fortress 2

We had a “mini-Bash” today, with those students who were still around on the very last day of the semester getting together for some fun and frivolity. We had enough people for a good game of Team Fortress 2, some Lego Rock Band, Buzz Quiz and Wii Sports, with some amazingly amusing sword fights… The pizza turned up right on time and fun was had by all. We even had a Super Word Search with Super Prizes.

Hull Summer Bash Prize Winners

Helen with one of the “super” prizes and a completed wordsearch.

Hull Summer Bash Rock Band\

Sam discovers that yes, unfortunately the speakers are working….

Broken Wings

A couple of weeks ago I was in a toyshop with my dad. We were watching this awesome radio controlled helicopter buzz around the top of the shop.  I mentioned that the thing I wanted at that moment most in the world, really really wanted, was a helicopter just like that. So dad got me one for my birthday, even though it isn’t for a while. He even let me play with it in advance, which was great. It was just like being a kid again.

image

And, just like when I was a kid, I’ve gone and broken my toy. By cunning use of a wall I’ve managed to smash the rotor pivot thing which holds the blades on.  Fortunately I’ve found a supplier of the part needed to fix things up. If you have a Syma S032 “Fiery Dragon” you can get pretty much all the bits you’d need to build another one from here. You have to pay postage from Hong Kong, which is a bit of a pain, but the actual part is very cheap. I’ve bought two.

image

This is what the part should like. Mine is in more than one piece….

The only good news is that I used PayPal, and discovered that I had a little bit of money left on my PayPal account that I’d forgotten about, which means that I can regard the replacement bits as free I suppose.

I just hope I can get everything mended and working before dad comes round again…..

Using RenderTarget2D in XNA on the Zune HD

Have you been lying awake at night wondering why your texture rendering doesn’t work on the Zune HD? No? Oh, well it must be just me then.

Anyhoo, I’ve found the problem, and the story goes like this.

RenderTarget2D is a way that you can draw things on a texture rather than on the game display. Normally I’d write things like:

spriteBatch.Begin();
spriteBatch.Draw (lots of pretty stuff….)
spriteBatch.End();

When the End method runs it takes all my drawing operations, batches them up and pushes them onto the graphics hardware, so the screen now contains the results of all my drawing operations.

However, sometimes you want to do things like rear view mirrors in driving games. You want to draw the view you would see behind the game onto a texture and then draw the texture on top of the rear view mirror.  In this case you need to create a thing called a RenderTarget2D which can accept the draw operations and put them onto a texture rather than the screen. Render targets are not that hard to use:

RenderTarget r = new RenderTarget2D(
   GraphicsDevice,  // my graphics device
   800,              // width of texture   
   600,              // height of texture
   1,               // no. of levels (1 works)
   SurfaceFormat.Color);  // format

GraphicsDevice.SetRenderTarget(0,r);

spriteBatch.Begin();
spriteBatch.Draw (lots of pretty stuff….)
spriteBatch.End();

GraphicsDevices.SetRenderTarget(0,null);

Texture2D result = r.GetTexture();

In the example above I send a bunch of drawing operations to a render target which is 800 pixels wide and 600 pixels high. I set the GraphicsDevice to render to this target, and then at the end I point it at null, which means go back to drawing on the screen again. The render target that I create is called r, and it ends up providing me with a texture which holds the results of all the drawing operations. You can use this to make your whole game display slide about, or bounce around the screen and it is very effective.

I’m using it so that my game can create some textures when it starts, rather than having to load them. I got the rendering working just right on the Windows PC and then transferred my code over the the Zune HD and it broke. I could clear the texture with different colours but I couldn’t draw anything on them. Wah.

After some twiddling I’ve found the problem. When you get a graphics device to render to a texture it is doing something it isn’t particularly used to. Graphics devices are designed to put stuff on the screen, not into memory.  This means that you have to be careful when you set up the target for the rendering, so that the graphics device is happy to write to it. You can find our more about this kind of thing here.

From the point of view of the Zune HD hardware, it is only happy to draw things if the target texture is either the size of the display or half the size of the display. Don’t ask me why, I don’t make the rules.  Any other sizes don’t produce an error, but they don’t work either, which is no fun.

This is not actually a huge problem (except perhaps for memory usage I guess) in that although you have to create textures that are larger than you really want you can use a version of Draw to only pull out the actual part that you want when you finally render them onto the screen. So, if you want to render to textures on a Zune you need to create your render target thus:

drawBuffer =
    new RenderTarget2D(
        GraphicsDevice, 
        GraphicsDevice.Viewport.Width / 2, 
        GraphicsDevice.Viewport.Height / 2,
        1,
        SurfaceFormat.Color);

This is the smallest texture you can use, if you want to draw on the full screen just don’t divide by 2.

Static Constructors

Summer Ball Star

One of the few nice things about marking all the programming exam papers over the last week was the number of people who seemed to understand what “static” means in a programming context. While one or two did make the mistake of putting ‘static means that you can’t change it’ and getting both a muffled oath and zero marks from me most answered “static means that it always there, like the continuous background hiss from a badly tuned FM radio”. Actually, not all of them were as poetic as that, but they all knew that the thing about static items is that you don’t have to make them, your program can use them without having to create any instances.

Pressed for uses of static their answers made sense too, constants that you want everyone in a class to make use of, validation values and methods, all the sensible stuff. I didn’t ask about static constructors though, which is the next step along the road.

If you want to set up an instance of a class you add a constructor method that runs when an object of that type is created. These can be given values so that you can pre-set your object with data at the beginning of its life. Bank accounts can be given account holder names and starting balance values, sprites can be given textures and positions.

Sometimes you might want to perform some code to set up values in your static class. For example a class providing some constants for your program might need to read some system settings first. Unfortunately you can’t use a normal constructor for this because (duh!) we never make an instance of a static class. Instead though you can use a static constructor. This is just like a normal constructor, but has the keyword static in front:

public static class Settings
{
   public static int Number;
   static Settings ()
   {
        // code in here is performed when
        // the class is loaded
        Number = 99;
   }
}

The thing to remember about this is that the static constructor is run when the class is loaded, not at the start of the program. The first time your program reaches a statement like:

int i = Settings.Number;

- that is when the Settings class is loaded and the static constructor runs. In order to reduce memory use and time spent processing unwanted code the .NET runtime system only loads classes when they are needed, so if we never use a value from the Settings class it is never brought into memory and the static constructor never runs. Don’t think of static constructors all running when your program starts, they only run when the classes that contain them are loaded.

In fact any class can contain a static constructor that runs when it is loaded, not just static ones. If you need to get something set up before anybody uses your object just use the construction above to get control when the class is brought in. One way to find out if a particular class is ever actually used in your program is to add a static constructor and then put a breakpoint in it.

Football Crazy

image

I asked number one wife what she thought about the the world cup. She shrugged and replied “Humm. England are  around 8th or 9th in the world. Quarter Finals if we are lucky”.

I think that’s about all that needs to be said on the subject. Me, I’ve got boxed sets of “How I Met Your Mother”, “30 Rock”, “Chuck” and “Big Bang Theory”. Should be a great one….

XNA Cross Platform Compilation Symbols

Garden Flowers 02

I’m writing a single XNA game that should work on a whole bunch of platforms. Actually I’m going for Windows PC, Windows Phone, Xbox 360 and Zune. Which is a full house I reckon.

Anyhoo, this is a bit of a pain, as I have to make the code do different things for the different target devices. For example the Xbox and the Windows PC don’t have a touch screen so I have to simulate this in some way, or provide a different input mechanism for those platforms.

I can do this by using the symbols provided by XNA to let me select code which is passed to the compiler when the program is built. There are a bunch of them.

#if WINDOWS
// only compile this bit if we are targeting Windows PC
#endif

#if XBOX
// only compile this bit if we are targeting XBOX
#endif

#if ZUNE
// only compile this bit if we are targeting ZUNE
#endif

#if WINDOWS_PHONE
// only compile this bit if we are targeting Windows
//  Phone
#endif

It is important that you understand what is happening here. We are not selecting which code to run when the program is active, we are actually selecting which code gets compiled when the program is built.  I could do this to really confuse a programmer:

#if ZUNE
     Har Har
#endif

This is a very nasty thing to do. It means that the program will not compile if the programmer tries to build version for the Zune, it will produce an error when the compiler sees “Har Har” and ties to compile it. It will however work for every other XNA platform.

One other thing worth knowing is that the symbols that you see when editing with Visual Studio depend on the context in which the file was opened. If you have a multi-project solution (one project for Zune, another for Windows and so on) then the project that you open the file from determines which symbols are active when the file is edited and compiled. This is true even if all the entries in the project source are links to the same actual source file. In other words, the view you have of the code depends on where you opened the file from.

Summer Bash

Summer Bash Poster
Yes, it’s that time again. In fact, it is very nearly too late. Because of exam timetabling issues and marking we will be holding our Summer Bash on the very last Friday of the semester starting at 4:30 pm in the department.

We will be having all the usual fun and games, including Rock Band, Wii Sports, Buzz Quiz, Team Fortess 2, Fizzy Drinks, Pizza and those little cup cakes with the icing on top. You know, the ones that you like so much.

Anyhoo, If there is anyone left in Hull who fancies a break from packing, then you can get your tickets (priced at an economical 2 pounds each) from the departmental office from 2:15 pm  tomorrow.

Using the Split method in C#

Abstract Wall

I’ve been playing with a little C# program to transfer marks into a spreadsheet. It is not a very clever program,  and probably took me longer to write than it would have taken me to actually type in the numbers, but it was much more fun, and next year I’ll be ahead of the game…

Anyhoo, one of the things I had to do was split up full name into set of strings to get the first name and surname. I used the Split method to do this, which is supplied free with the String class.

Split is wonderful. It does exactly what you want, plus a little bit more, and so I thought it would be worth a blog post. Split looks a bit scary, because it returns an array of strings, which is something you might not be used to.  The other thing that you might not like about Split is that you have to give it an array of separators, which looks a bit of a pain but actually gives you a lot of flexibility. My first string was a bunch of names separated by tab characters. Easy.

string sampleName = "Rob\tMiles";
char[] tabSep = new char[] { '\t' };
string [] allNames = sampleName.Split(tabSep);

The code above would make an array called allNames which holds two string elements, one with "Rob" in it and one with "Miles" in it. To handle the fact that some people have lots of first names I can get the surname (which is the last string in the sample name) by using the Length property of the allNames array:

string surname = allNames[allNames.Length-1];

Remember that this code will not end well if the starting string is empty, as this means that the allNames array will not contain any elements and your code will say a big hello to the ArrayBoundsException shortly afterwards running the above.  My perfect solution checks the Length of the allNames array and displays an error if this is zero.

The second problem I had was to do the same split action on a list of names which were separated by either comma or space (or both). First thing I had to do was create an array of delimiters:

char[] wordSep = new char[] { ',', ' ' };

Now words are split on these two separators. Bad news is that if I give the Split method a string that contains a bunch of both kinds:

string sampleName = "rob,    Miles";

- this means that I get a whole array of  strings (in this case 6) lots of which are empty. Just finding the surname (which should be the last string) would still be easy enough but finding all the proper names in the rest of the array would be a pain. Turns out that Split has got this covered though, you can add an option that controls the behaviour of the split so that it ignores the empty items:

string[] allNames = fullName.Split(wordSep, 
                      StringSplitOptions.RemoveEmptyEntries);

The option is a bit of a mouthful, but as you don’t need to say it this is not really a problem. The great thing though is that all those nasty extra empty strings are removed, leaving allNames just holding the two strings I really want. Split is actually very useful and it will work on really big strings. I used it to split a list of several thousand words and it worked a treat.

Support Jenny

jal

Now, I don’t make many demands of my readers. In fact I’m perpetually surprised how many people keep coming back and reading my stuff. (I really must stop being perpetually surprised though, it is very hard on the eyebrows).

Anyhoo, I don’t ask much of you, dear reader, except every now and then. This is one such situation. Number one daughter, who you can see above doing something daring, is doing something daring again. For money. (at least I taught her that much…)

It is in a very good cause, and I’d be most gratified if you would swing along to her donation site and drop her a little something. They take Paypal, and if you are a UK tax payer you can get the Inland Revenue to bump up your contribution.  You can find the site here:

http://www.justgiving.com/Jennifer-Miles

Stowaway Keyboard for iPad

Stowaway Keyboard

My iPad is turning out to be really nice. Games on it are fun, and the word processing and spreadsheet programs are definitely not toys, in fact they look to be very useful. However, typing at speed on the device is not too much fun. The best solution I've found is to put the device into landscape mode so you can use the larger version of the on-screen keyboard. This is OK, but the keyboard covers up a lot of the screen and I still don't really like the feel of typing on glass. Many years ago, when mobile computing meant a Windows CE device, there were lots of companies providing neat hardware you could use with your Pocket PC. One such company was "Think Outside the Box" who made a lovely little keyboard called the "Stowaway". I dug mine out this evening to see how it mingles with the latest technology.

The answer is "very well indeed". If you can track down a Think Outside the Box Bluetooth keyboard you should get one. The keys themselves are a miracle of folding cleverness. It looks like the company itself has gone now, but if you can track one down cheap I'd strongly advise you to. Paring the keypad with the iPad is a snap. Just hold down the CTRL+BlueFN+GreenFN on the keyboard until the green light flashes. Then get your iPad to discover the keyboard by going to the Bluetooth menu. The iPad will display a "magic number" that it wants you to type into the keyboard. Do that, remembering to hold down the BlueFN key as you type so that numbers are sent. Press enter when you have finished and, bingo.

The experience is so good, and the ability to type at speed so useful, that I expect we will see a return of gadgets like these, which let you make the most of these new fangled devices.

iomega Home Media Server

image

Now this is a useful device. The iomeaga Home Media Network Hard drive. It gives you 1 TByte of online storage that you can use at home. I’ve played with network storage devices before and found them quite useful. This is a bit more than just an online filestore though. It supports Apple Time Machine, so you can use it to back up your Mac. It also works as an iTunes and DLNA server, so that you can steam media to your Playstation or Xbox (or any other device including some TVs).

Finally it has things called Active Folders which are places you can lob files for background processing. One active folder will work with Bit Torrent, another sends files to your Flickr account and third will resize pictures placed into it. There are also ones to display slide shows to web browsers. I’ve used it with Macs and PCs and it seems to work fine once you have got the latest versions of the client programs on your machines. I’ve not tried the Active Folders much yet, I’m particularly interested in the Flickr uploader though. It also does network printing (which I must get around to).

If you are looking for a lot of storage that you can share around the house, or are looking at all the files you have spread around the place and wondering how to organise them, or you want to steam media to your consoles, then this is worth looking at. Particularly as from a price point of view it is only slightly more expensive than a USB hard drive of similar capacity. The best place to get them from in Hull is Staples, who have them at a very attractive 100 pounds at the moment.

Windows Snipping Tool

image

The Windows Snipping Tool started life in the Tablet versions of Windows but has now made it into the big time and is part of the Accessories for Windows 7. It is great for capturing images off the screen that you want to incorporate into other documents. Say for example you wanted to steal a screenshot of a Plants vs Zombies game and drop it into a blog post….

It is great if you are writing a manual for a program and just want to drop parts of the screen into your text. One of my pet peeves is documents that have the whole Windows desktop in their screenshots. Everybody knows about pressing the PrtScrn to copy the screen to the clipboard. A few people know about ALT+PrtScrn which lets you just copy the active window. With the Snipping Tool you can just capture the bit that you want to write about. You can find it in All Programs->Accessories, but it is so useful you’ll probably pin it to your Taskbar or Start Menu.

Making Games close to the Edge

image

Calling a start to the event in noisy style..

Andy Sithers of Microsoft and a few Hull students got mention in an article in this month’s Edge magazine about some recent 48 hour game development competitions.  This is where a bunch of teams are given a theme, 48 hours and a lot of pizza to make a game. XNA is a brilliant tool to use for  this kind of thing, and Microsoft set up a couple of competitions this year.

Some students from Hull took part and while they didn’t win anything this year (having got a “Cheesiest Game” award last time) they did have a great time. I’d love to take part in one of these one day.

iPad Review – Great apart from the broken WIFI

The iPad is a lovely device. It hasn’t made me any cleverer or better looking yet (but then I’ve not had it a day) but it is nice to use.  The screen is great to look at. Browsing web sites is a doddle (until you bash up against somewhere that uses Flash). The applications that you can buy look like they will be really quite useful. And number one wife quite fancies one too. The battery life looks good as well.

As of last night I loved it. This morning, when I woke it up and found that it no longer recognised my WIFI at home, I’m a bit less enamoured. I’d read about these problems when the iPad came out in the ‘states, but I presumed they would have fixed them before I got mine.

They haven’t.

I had to leave the house for work before I could do too much fiddling, and the iPad found the university network and is working fine at the moment, but I’m expecting a tussle when I get home. I’ll probably get around this by hard-wiring the network settings and with a bit of luck this will fix it. Otherwise I’ll have to wipe all the connections and re-connect. I can live with this, at a pinch, but I’m sure that number one wife wouldn’t like it much, along with anybody else expecting to buy an appliance.

I went onto the Apple support site and they have acknowledged there is an issue here, which is nice.  They then said it might be a problem with “Third Party” – i.e. not made by Apple – routers. I found this a bit irritating to be honest. If I have twenty devices (and I must have used that number of WIFI devices at home over the years) and the 21st one doesn’t work I’m more inclined to blame the new device than anything else.  Some of the suggested remedies (“Turn down the brightness”, “Hold the device above your head”, “Stand on one leg” etc) strike me as a bit daft when the proper solution is “Get Apple to replace the driver software with some that works”.

My advice, for what it is worth, is don’t turn your iPad off. I shut mine down last night and the reboot is the thing that seems to have broken it.  Of course you might get the same effect when you wander in and out of range of your “Third Party” access point. Oh well.