XNAtutorial.com


Singletons and XNA GSE Refresh

Posted in New Tutorial by joran on the April 30th, 2007 Comment Feed

Refresh

A new version of XNA Game Studio Express was released, which require Visual C# Express Service Pack 1. The refresh provides Vista support, XACT 3D audio, performance boosts, bitmap fonts, and a distribution packer. You no longer have to give up your source code.

At the same time, Creators.Xna.com received new content. This included the long awaited XNA Racer starter kit, but also many tutorials, samples, articles, and utilities. They cover glow effects, drawing points and lines, menu transitionsDoppler and pitch sound effects, particle effects, vertex lighting, data structures, pipeline programming, the SpaceWar structure, a font maker, and Xbox 360 controller graphics.

Singletons

Scott: “In the Farseer singleton, what’s Farseer(){} for?”

Ah, I missed that? Here we go:

There are no instance fields or properties, only the static field and static property.

There are two constructors, one static constructor, one instance constructor, both private. Private is the default.

The static constructor gets automatically called right before (see Lazy below) some code accesses the Physics property. When called it initializes the static physics field.

The instance constructor, the one you are asking about, gets called when someone tries to instantiate the class. The instance constructor doesn’t do anything, which is good, because there is nothing that should be done.

I probably missed mentioning it, because it doesn’t do anything.

Hold on. Come to think of it, it does do something. It stops people from instantiating the Farseer class, while forcing the creation of the static version of the class.

The C# specification (ECMA 334) states in section 17.11:

“The static constructor for a class executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:

* An instance of the class is created.
* Any of the static members of the class are referenced.”

In other words: When starting the game, the static Farseer is not created. If a piece of code tries to access the Physics property, then the static part of Farseer is created. Or, if someone tries to instantiate Farseer, it is not instantiated, but the static part is activated.

Did that make sense?

Lazy

A class which isn’t instantiated/initialized until it is really needed is called a lazy class. This version of a singleton isn’t 100% lazy, but almost. If someone adds a second static member, the physics field would be initialized when that member was accessed, even if it had nothing to do with physics.

If you for some reason want a fully lazy version, you can go to the same excellent source as I did, Jon Skeet, and read more about singletons in C#.

Also from Jon Skeet: If you make a second singleton which calls the Farseer singleton in its constructor, and the Farseer singleton references the second one again, then you might make the whole thing crash. That’s not a common programming situation, though.

Tiny Singleton

On another note, you could shorten the whole class to this:

public sealed class Farseer
{
    public static readonly PhysicsSimulator physics =
        new PhysicsSimulator(new Vector2(0f, 300f));
    private static Farseer() {}
    Farseer() {}
}

That’s a public field, which is a bad idea, because you can’t add code to it when it gets accessed, as you can with a property. If you don’t need code, then this would look tiny and neat. It doesn’t affect performance to use a property, though. The JIT compiler is too smart for that.

Scott: “I’ve never seen such a good mix of theory and practice, and practically everything’s perfectly clear.”

Thank you!

Cheers!

Joran

Video Pack 9

Posted in New Tutorial by joran on the January 3rd, 2007 Comment Feed

I’m now more or less caught up with the beta 1 videos. I have not covered some areas yet, such as bit fields. In the other hand, I have covered some new ground, such as Farseer Physics.

The total download of the previous series was roughly 90 megabyte. The new series is over a hundred.

I’d say if you viewed the original feuilleton, you can jump back on board right now.

XNA Tutorials Pack 9 - Farseer Physics and Singletons

XNA tutorials pack 9 starts with a quick review of the code execution pattern so far, on request from viewers. I hope it clears things up a bit.

Then it moves on to Farseer physics theory, mostly so you know what Farseer does internally. Maybe we will implement our own physics one day, and then this will help the understanding. I also quickly go over what the singleton pattern is.

It uses the singleton pattern to create global access to the only instance of Farseer we will have. It might not be the best way to use Farseer, but it does mean we learn how to use singletons. The implementation of the singleton pattern itself is solid.

It finishes up by creating a Sprite class, which links physics and graphics together. This Sprite class is similar to how the Mouse class linked mouse input to the graphics, essentially creating a mouse cursor.

At the end, a short video explains how to add comments which integrate with IntelliSense. The source code solution is therefore commented this time.

You don’t need to download Farseer yourself, unless you want the latest version. The solution includes a copy of Farseer.

I’ve updated the Download page as well.

Cheers!

Joran

Tutorial Packs 7 and 8

Posted in New Tutorial by joran on the December 13th, 2006 Comment Feed

Two new tutorials. Updated the Download page as well.

XNA Video Pack 7 (Download)

Video 21 is more theoretical, dealing with Implicit and Explicit Casting, Increment and Decrement Operators, Assignment Operators, and While and For Loops.

Video 22 is practical, writing the system to extract a single sprite from a sprite sheet.

Video 23 is again theoretical, and goes through Generics, Type Parameters, and the .Net Generic List.

XNA Video Pack 8 (Download)

Video 24 uses a Generic List to store several frames of an animation, in an Animation class which inherits from the Image class.

Video 25 goes through some less-important vocabulary of .Net and C#, such as assemblies and the different parts of IntelliSense.

Beta 2 and Final Version

I’ve recompiled the code of each solution released, from Video number 1 and forward. They all compiled without a hitch, and run as expected

 I’m considering them as XNA 1.0 material unless I hear from anyone with a problem.

What happened to Video Pack 6?

Video Pack Five was so big I repacked it as two video packs - 5 and 6. I’m trying to keep each download at 10 megabyte, plus-minus a few, and it was over twenty megabyte.

Cheers!

Joran

XNA Tutorials Pack 5

Posted in New Tutorial by joran on the December 3rd, 2006 Comment Feed

I felt talkative today, so this collection is almost two hours long. I’m not sure I’m actually giving that much more real information compared to when I try to be concise. Sorry… (c:

There is still a download page, which will be integrated into the new layout, whenever I get that done. My parents showed up early for the Christmas gathering, so there has been some sightseeing and a lot of catching up to do this last week. Fun, fun!

XNA Practice - This time, we make an Image class, so we get a real-code example of how you can encapsulate functionality. In order to show that encapsulation is good, we then use the Image class in a new Mouse class, to draw a mouse cursor on screen.

C# Theory - Doing all this mostly show practically how to use some of the concepts we’ve already been through, so the only new concept introduced is Properties. However, the last video answers a few design questions, based on feedback.

The packs will have more and more practice videos as we get farther along.

Beta 1 Viewers

Some of you were watching this series in its first incarnation, during XNA beta 1. I’ve tried to provide some new things, not introduced in those videos, so that those old viewers will not feel left out in the rain too long.

For example, one new thing not in the beta 1 videos is the Additive effect when drawing with the SpriteBatch, creating a nice lighting effect.

Another is a redesign of the Mouse class, along better design values. I’m happy with it now.

Interviews and Weekly Updates

The interview with Mark was a success, mostly because I enjoyed making the interview. Yes, the feedback was positive too. (c: As Gregory was hinting at, there might be another one(?) coming up… (c:

I’ve also finished writing a weekly update, but I’m going to wait until tomorrow with publishing it, so that the one after that will come on the 11th of December… (c:

Cheers!

Joran

Fourth Pack of Tutorials

Posted in New Tutorial by joran on the November 24th, 2006 Comment Feed

I’ve made a beta of a download page, where you can access all of my video tutorials. Let me know if you have an idea for a better layout.

I’ve put up both WMV videos, and SWF videos.

The WMVs are for Windows Media Player, and are the ones you should use - the file sizes are smaller, the quality is better.

The SWFs are in Macromedia Flash format, and are mostly an alternative if for some reason the WMVs don’t work for you - I’ve had requests from Macintosh people for this format.

For once I did not download them myself, to do a checksum. Since it has been okay every time so far, I’m guessing I’ve just been paranoid.

As for the side bar to the right, with links to tutorials and other sites, it will be gone next week. I’m currently tweaking a new theme, with a menu bar at the top instead. The layout will be much cleaner.

After that, time to finish up the forum. Sigh… Not looking forward to PHP.

As always, LearnXNA.com is kind enough to mirror my tutorials. Thank you, Greg.

Cheers!

Joran

Next Page »