Visual Basic for XNA?

So it seems that it was annouced today that XNA is coming to Visual Basic! I have to admit the announcement caught me a bit by surprise. As one of the people who has had to investigate (at various times) what it would take to make this happen, I have no idea where the team has gotten the time to do this! As the article says though, it was one of the most popular feature requests I can remember though so it’s good that it’s finally happening.

Wish there was more information though. For example, when is it coming? Is it coming on all the platforms, particularly Xbox? The picture in the article implies it will, but that doesn’t necessarily mean anything. I know they said to wait a few months, but given people have been waiting for literally years at all, I wish they would have waited until they could give us all the information rather than teasing it early when they cannot.

Although, I suppose I could be in the minority in thinking that. Some folks just like be teased and waiting a few months.

Loading… I hate loading…

I guess I’m impatient, because there are very few things that annoy me more than loading screens. In a world of instant gratification, waiting for any significant amount of time is unacceptable. Hell, I’ve only just started typing this and I’m already annoyed that I’m not finished yet. Loading just bugs me, and I see it creep a lot on Windows Phone 7 games. Perhaps I should ramble on about that for a while.

The first goal of the game should be to show me something on screen as soon as possible. While you can “somewhat” use the splashscreen.jpg trick to show something quick, in XNA applications, it doesn’t stay on screen long enough and looks awkward (in my opinion).  I personally would rather show my splash screen on the first render.  However, that leads me to another common mistake I see people making, and that’s loading a bunch of content (or all!) in the LoadContent method.  This is certainly a bad idea if you want to show something on screen quickly. This is essentially the order you will see things happen at startup:

  1. Game’s Constructor is called
  2. Initialize is called
  3. LoadContent is called
  4. Update is called
  5. Draw is called
  6. Goto 4.

While there are certainly nuances here, that’s the basic order, and notice that Draw isn’t called until way at the end.  I’ve seen people decide that the best way to fix this would be to draw something in LoadContent, and then they implement that, see that it works and call it a success. If only it were so easy. If your application is starting up once more after being tombstoned, essentially the same sequence of events happens, however there is a caveat this time.  While the “startup sequence” is happening, the phone will be displaying the “Resuming…” screen (my how I dislike that screen), and will not show anything from your application until the “startup sequence” has finished. In this case, the sequence doesn’t finish until *after* LoadContent has finished. So if you showed something on screen early in your LoadContent method, and then loaded a bunch of content, it would work in the initial startup scenario, and potentially fail in the restore from tombstone scenarios.

The easy way to get around these issues is to do all of your content loading (aside from the first thing you want to see on screen, such as a splash screen) on the second update call.  This allows you to show something on screen quickly in both the initial startup and coming back from tombstoning cases, and if you don’t have much content, should be sufficient enough. Your splash screen image will stay static on screen (and your application is essentially non-interactive) while the content is loading. If it takes more than a few seconds to get to this state though (ie, you have too much content), you have more work to do.

The most common thing I see happening here is the game will load only the content needed for the main UI portions, and will force you behind a loading screen after you choose the level or what have you. Again, this works perfectly fine, but we can do so much better! Why can’t you load everything you need while I’m playing around with the UI? For ease of explanation, let’s say that you are making a 2D game, and you have a large list of strings that are all the sprite textures you will need to load. What if you had a class such as this:

So long as you called BackgroundLoad.Go(this); sometime after you started up, your game would dutifully start loading all of your assets behind the scenes in a background thread.  This isn’t a foolproof solution though. For one, you have very little control over background threads on Windows Phone 7. You cannot change priority, and you have no control over how much they will execute (although on average for every 4ms your main thread is executing your background thread will run for 2ms).  In this example, you are also using the graphics device on another thread, which will take out a lock to avoid multiple threads manipulating it at the same time. In the larger scheme of things though, it’s still probably faster in this mechanism than without.

You will notice two potentially useless pieces of code here as well. First, a property to dictate when the loading has been complete, and a second in which the actual asset load is happening within an action rather than directly in code. The property is self explanatory if you think about it, you still need to know when all of your content has been loaded so you can allow the user to start the game! This just gives you easy access to it.  The action delegate is slightly less obvious though.

What should you do when the user has gone through the UI and is ready to play the game but the content isn’t done loading? As was mentioned earlier, you don’t have any control over the background threads execution time, and because it is running half the speed as the main thread, as the code is written now, your loading time could potentially be *increased*! This is most definitely not what we wanted. What you really want is a mechanism where you would load things in the background if the user was still messing around with the UI, but will switch to loading in the foreground if the user was waiting on loading, and that is what the action delegate is for. If you replaced the code in the thread method with the following, you will get this behavior:

Now your code is loading in the background while the user is messing around with the UI and will switch to the foreground once they’re done. In the degenerate case, this is still slightly slower than simply forcing them to wait at a loading screen, but given the degenerate case is almost certain to never happen, a mechanism such as this will (for most intents and purposes) always be faster. It certainly took me long enough to exlain all this, I should have had a loading screen for this post…

As a final caveat, this was an extremely simple example of multithreading. If you do anything more complex and don’t have knowledge of multithreading (or what the ‘lock’ keyword does), you should probably read up a bit on that!

Welcome to the new site!

Well, it’s been a long time since I’ve written a real blog post, and this is the first one on my new site, so what do I want to talk about!? I suppose we should start with why I migrated to a new site to begin with!

There are two major reasons in reality. First, I needed a web host for some experiments I wanted to run, so I figured I may as well just catch up with the times and buy one. I’m only a decade or so late, but better late than never right? Second, I needed to have more freedom in what I wanted to say. Having my blog hosted on MSDN was great and all, but despite the disclaimer up top, the reality is that anything I say there is essentially the same as saying it as Microsoft. While it’s certainly true some folks go wild on their MSDN blogs, I never felt very comfortable in doing so.  Now, I’ll be able to say whatever it is that I’m thinking!

Of course, this also means that my MSDN blog is essentially retired as of now, but given the frequency (or lack thereof) of posts there, it probably isn’t a big deal anyway!

So, what do I want to talk about on this new site? Well, a lot of stuff I used to talk about on the old site, game programming, XNA, Xbox, and Windows Phone! For example, while my latest XNA book is already out (look to the right), should I consider writing a new one? Perhaps I should release a few games on XBLIG or the WP7 marketplace? I’m not entirely sure what I want to do yet, but now i’ll have the chance to do them without having to worry about speaking as anyone but myself, which will be nice.

It occurs to me that I forgot to announce a few things on my blog..

For starters, I never mentioned that I had left the XNA team here.  After spending approximately a decade working on XNA and it’s predecessors, I had finally decided it was time to move on.  An opportunity came up within MGS (Microsoft Game Studios) that gave me the opportunity to make games rather than enabling others to do so.  Since I had accomplished virtually everything I had hoped to with the XNA product, and the available opportunity was too good to pass up, I moved to the new group in June.  I did announce that move on my twitter account, completley forgot to announce it here!

Along side that, Dean and I have been hard at work with our book, which you can find out more information about here:

http://www.facebook.com/pages/XNA-Game-Studio-40-Programming-Developing-for-Windows-Phone-and-Xbox-Live/148023561880764

You should expect the book to come out around the same time GS4 does.

I would say that my new role probably doesn’t afford me a bunch of time to be blogging here, but since I didn’t even remember to blog about my move, I’m not sure that it matters anyway.  I don’t even know if people still read this!

A new book on Game Studio 4.0

It has been about five years since my last book came out.  During that time, a lot has happened.  Heck, that book was about Managed DirectX, and at the time it came out XNA Game Studio didn’t even exist as an idea, much less a product.  Since then, it’s not only been released, but we’ve announced the *fourth* version of the product!  We’ve added support for Windows, Xbox, Zune, and soon, Windows Phone 7.

So much has changed, so much time has passed, it was time to do a new book.  This new book will cover Game Studio 4.0 in its entirety, including Windows Phone 7, the new features, Xbox, everything.  While we haven’t announced any release dates for Game Studio, I’m spending my spare time (what little I have) to make sure the book will come out as close as possible to the release of the product.

I’m also having some help with this one, and will have a co-author, so that is a bit exciting too!  Writing has begun so if you have suggestions, now is the time!

XNA Game Studio 3.1 Zune HD Extensions

If you were already developing games for your Zune using XNA Game Studio, you’ll be happy to know that moving that game over to the new Zune HD will be remarkably easy.  Today we’ve released the XNA Game Studio 3.1 Zune HD Extensions that will allow you to deploy to your new device and includes a few new API’s as well.

There is also a thread on the XNA Creators Club forums for any questions or discussions.