May 042010
 

So, I guess I’ll give this tutorial thing a try.  I’ll begin with something simple.

Let’s learn about Extension Methods in C#!

Extension Methods were introduced in C# version 3.0 (C# Version 3.0 Specification).  Extension methods allow you to add or “extend-to” a pre-existing class a method of some sort.  This is particularly useful when you wish extend a method to a type that is defined in C# (or something closed source) without having to create another class and derive another type.

The format is as follows:

public static void ExtensionMethod(this extensiontype var, type additionalvar1, type additionalvar2, .....)
{
    //Do something...
}

Ideally, you might wish to encapsulate these extension methods in their own class.  Perhaps even a class that only contains extensions for the particular type you’re extending:

public static class SomeTypeExtensions
{
    public static void ExtensionMethod(this extensiontype var, type additionalvar1, type additionalvar2, .....)
    {
        //Do something...
    }
}

Furthermore, you could encapsulate the multitude of extension classes you create into their own namespace and optionally include the use of the extensions on a class to class and/or file to file basis in the using statements.  I’ll let you decide on that at your own discretion.  I like to follow this convention in my projects where I need to use extensions:

namespace Extensions
{
    public static class SomeTypeExtensions
    {
        public static void ExtensionMethod(this extensiontype var, type additionalvar1, type additionalvar2, .....)
        {
            //Do something...
        }
    }
}

It’s important to note that the class containing your extension method must be static and the method you are extending must also be static.  This is a requirement for extensions methods.

OK.  Now that we have the appropriate syntax in our heads for defining an extension method, let’s put what we’ve learned to use.  Let’s define a simple extension to the int type that allows us to print the value of the int to stdout:

namespace Extensions
{
    public static class IntExtensions
    {
        public static void PrintInt(this int integer)
        {
            //Print the integer value for the int object caller
            //and a newline
            Console.Write(integer.ToString() + '\n');
        }
    }
}

You can choose optionally which classes you wish to implement this extension based on the namespace where it lives.  If you have encapsulated the class in an ‘Extensions‘ namespace like I suggested earlier, then you can simply add it among the using statements of the classes you wish to have access to your extensions:

using Extensions;

Once a class has access to your extension you may call the extension method on any variable of the type you provided an extension for:

int number = 5;
number.PrintInt();

The output to the console will look like this:

ExtensionsExample1Output

It doesn’t get much straightforward than that.  If you wish to try out the code explained above here is a link to download the Visual Studio Project (requires Visual Studio 2008. Though, it may work with Visual C# 2008 Express or 2010 Express which are free.  I haven’t tested it.  You’re welcome to.):

Example1.zip

I have taken the time to create another, more advanced, example using the Microsoft.Xna.Framework (this requires XNA 3.1).  In the second example we are modifying a Texture2D object to extend a draw method to it.  The end result is a sand texture stretched across the game window.  Here’s a link to the second example project:

Example2.zip

For more guidance and examples you can consult the C# Programming Guide for Extension Methods at MSDN where they provide the general syntax and usage as well as recommendations for when extensions should and should not be implemented.

Hope this was helpful.  Thanks for reading!

Sources:

C# Version 3.0 Specification
(http://msdn.microsoft.com/en-us/library/ms364047%28VS.80%29.aspx)
Extension Methods, C# Programming Guide
(http://msdn.microsoft.com/en-us/library/bb383977.aspx)
Sand Texture Graphic for Example 2
(http://mayang.com/textures/Nature/images/Sand/sahara_sand_patterns_220511.JPG)
Mar 242010
 

I’ve recently rekindled my interest in development using the XNA platform after rifling through some old project files and finding a game project that has gone unfinished.  I had great plans for this game as it was a contender in the 2008 Imagine Cup competition for game development.  We did pretty good.  Team CTW was awarded 3rd place nationally and we were able to showcase our game prototype to many people.  But, as is the case for many projects, it died shortly thereafter.  With impending midterms and graduation for some of us, Team CTW quickly disbanded as well.  As I said, I always had plans for this game.  With that, I’ve been given a spark of motivation as of late to actually finish it.  This would entail a tremendous overhaul of the base game framework and some more art.  For now I guess I will tackle the easier of the two tasks and fix the code base.  Hopefully, with little resistance, I can convince the old team to get back together for one final shot at finishing this game right.

In my browsing I also came across and old DVD with some rudimentary tutorials for 2D and 3D programming using XNA.  The video tutorials in the XNA Beginner’s Guide DVD and the supplementary information provided therein were pretty helpful.  I would recommend the tutorials to any novice or beginner interested in learning the XNA framework having no previous programming experience.  The video tutorials do a good job of easing you into programming with C# and XNA.  If you can’t get your hands on the physical DVD with all the tutorials and resources you can find the tutorials (and resources) here; at the XNA Creator’s Club website.  Here is a listing of the tutorials found on the DVD that are also hosted on the website:

The Creator’s Club website (http://creators.xna.com) is also a great place to find more tutorials and support that go beyond the concepts presented on the DVD.

Thanks for reading.

Nov 252009
 

In my opinion, I would say no. I don’t believe we need an entirely different console configuration at this time. But, if current generation console features could be improved upon, I think that would be a move in the right direction. We are at a place, technologically, where the graphics capabilities in video games have the same effect in entertainment that movies do. We can tell a story and we can tell it well. We can, perhaps, tell a story even better than movies can because video games, by nature, are much more immersive than a movie can be.

With the advent of new technology also comes an awkward period for developers at the very beginning of a platform’s release. Developers are forced to learn, most of the time, an entirely new SDK (Software Development Kit) tailored specifically for the new hardware and the new technology used by the hardware. Aside from the obvious negative aspects that exist for developers, what does this mean for the consumer? This means that for the first few months or even a year or more after a console’s release there is going to be a dry period for game releases. There are going to be very few games released and the quality of each title will more than likely be under par. These are the costs of acceptance of emerging technologies. But, at this point and time are the costs of a new platform worth the benefits? That is the question that must be addressed. It is inevitable that new technology will be developed. I only wonder if we’re ready for it now.

It would seem that most console developers tend to adhere pretty rigidly to the release of some new console over a specified interval:

Nintendo Releases over 6 year intervals (approx.):

Sony Releases over 5 year intervals (approx.):

  • [1995] – PSX
  • [2000] – PS2
  • [2006] – PS3

Given current trends Microsoft is due for another console release. So far, Microsoft has consistently released a console every four years (Xbox in 2001, Xbox 360 in 2005). Now approaching the end of the 2009 year, one can only wonder what Microsoft has in store for us. I can only assume it to be their current R&D focus; Project Natal.

Project Natal might serve as a new and innovative platform (really no more than a peripheral for the current gen platform) capable of pushing both participation and immersion in video games to a previously inconceivable level. Will everyone jump on the band wagon and wait in line to get it on release day? From what we’ve seen so far, the platform seems to be targeted toward the casual gamer, or the “family” gamer. But with the introduction of this new input format or others like it, will the old input format, the controller, be supported still? Or will it instead become obsolete? What will become of competitive gaming if controllers that we’ve been accustomed to using for decades are phased out? These kinds of questions are important to a less represented demographic; the hardcore or competitive gamer.

Regardless of any of my previously mentioned opinions I actually believe that improvement of the Xbox 360 through development of Project Natal is a good move for Microsoft. As described previously Microsoft will avoid the overhead of a completely new platform while directing their current console iteration to a much larger demographic; the casual gamer.

Project Natal is also good for consumers for a couple of reasons. The new console “dry spot” will be avoided since the core hardware hasn’t changed much. While the influx of Project Natal titles might be limited at first, there will still be constant releases of Xbox 360 core titles (titles not utilizing Project Natal technology). The cost of new hardware will be reduced considerably as well since the price of a peripheral will always be less costly than an entirely new console.

Well, despite all of my opinions expressed here, I suppose we’ll just have to wait and see what happens next. I’ll probably end up purchasing Natal eventually, but I’m in no hurry to get it on release day.

Thanks for reading!