Patrick Godwin's Blog-O-Rama!

Developers, developers, developers!

MonoGame: XNA for Metro Style apps

As most of you may know, XNA is not supported as an option for Windows 8 Metro Style apps. As a result, those who use C# for game development have very few options with Metro Style apps. Wondering what alternatives there are to XNA, I did some scrounging around the internet to discover that MonoGame, an open source game development framework developed alongside Mono, was working on a Windows 8 branch of their technology. This is a great find for any C# developers looking to write games on Windows 8. So here is what you need to do to start porting your XNA games to Windows 8:

Prerequisites:

Windows 8 Release Preview
Visual Studio 2012 Release Candidate
MonoGame Template for Metro Style apps
MonoGame Binary (Built 6/7/2012 for your convenience)

Instructions:

Once you have installed and downloaded the required materials, we can get started. First thing you need to do is extract the MonoGame Template for Metro Style apps attached above to the folder

“%UserProfile%\My Documents\Visual Studio 2012\Templates\ProjectTemplates\Visual C#\”

like so:

image

Next, you can open Visual Studio and create a MonoGame Metro Style app, like so:

image

Now that you’ve created the project, you simply have to add a reference to MonoGame.Framework.Windows8.dll, located in MonoGame.zip:

image

Once that is complete, all of the errors should go away and the app should build without any problems. You’ll recognize the familiar cornflower blue background, except now it’s running as a Metro Style app!

image

Conclusion:

From here, the MonoGame APIs appear to be one to one with the XNA APIs. The only thing to note that, as of this posting, MonoGame for Windows 8 is still lacking support for the Audio and Effects API. That will soon be fixed, and a follow up post will be published.

Keep an eye out for the stable release of MonoGame for Windows 8 by checking the MonoGame GitHub and Codeplex pages in the next few weeks. Until then, you can always build from the Develop3D Branch.

Intro to the Kinect SDK–Drawing Joints in XNA

It’s been a few days since Microsoft released the Kinect for Windows SDK, and we’re already seeing a lot of work being done. I decided to get my hands dirty and try out the fancy new skeletal tracking. First things first, you’re going to need to make sure you have some pre-requisites:

With all of these requirements satisfied, you can get started.

The first thing you need to do is add a few variables to the project:

SkeletonData skeleton;
Runtime nui;

These will be used to track data from the Skeleton provided by the Kinect SDK. These will be used later.

Next, in the LoadContent function of the Primitives3DGame class, add the following code:

nui = new Runtime();
nui.Initialize(RuntimeOptions.UseSkeletalTracking);
nui.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(nui_SkeletonFrameReady);
nui.NuiCamera.ElevationAngle = 0;

In these five lines of code we’ve initialized the Kinect Runtime engine to use the Skeletal Tracking feature of the Kinect, created an event handler for the SkeletonFrameReady event, and made sure the Kinect is not elevated from a previous use.

Next we’ll want to add some code to that nui_SkeletonFrameReady event handler:

void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
    foreach (SkeletonData s in e.SkeletonFrame.Skeletons)
    {
        if (s.TrackingState == SkeletonTrackingState.Tracked)
        {
            skeleton = s;
        }
    }
}

This function is called every time the program gets skeleton data from the Kinect. We make sure that the SkeletonEngine is currently tracking a skeleton, and then we make a reference to that skeleton so it can be rendered later.

Now we want to modify the Draw function of the sample:

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    if (isWireframe)
    {
        GraphicsDevice.RasterizerState = wireFrameState;
    }
    else
    {
        GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
    }

    Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, -20), new Vector3(0, 0, 100), Vector3.Up);
    Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
                                                GraphicsDevice.Viewport.AspectRatio,
                                                1.0f,
                                                100);

    // Draw the current primitive.
    GeometricPrimitive currentPrimitive = primitives[currentPrimitiveIndex];
    Color color = colors[currentColorIndex];

    DrawPrimitveSkeleton(currentPrimitive, view, projection, color);

    // Reset the fill mode renderstate.
    GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;

    // Draw overlay text.
    string text = "A or tap top of screen = Change primitiven" +
                  "B or tap bottom left of screen = Change colorn" +
                  "Y or tap bottom right of screen = Toggle wireframe";

    spriteBatch.Begin();
    spriteBatch.DrawString(spriteFont, text, new Vector2(48, 48), Color.White);
    spriteBatch.End();

    base.Draw(gameTime);
}

There are a few things to note here, as this function is quite different than the draw function of the sample. The first thing is the view and projection matrices. We need to move the View Matrix further back from the Origin to get a better view of the skeleton. Then we simply pass any relevent data to the DrawPrimitiveSkeleton function, and allow that to draw all of the joints:

private void DrawPrimitveSkeleton(GeometricPrimitive primitive, Matrix view, Matrix projection, Color color)
{
    try
    {
        if (skeleton != null)
        {
            if (skeleton.TrackingState == SkeletonTrackingState.Tracked)
            {
                foreach (Joint joint in skeleton.Joints)
                {
                    var position = ConvertRealWorldPoint(joint.Position);
                    Matrix world = new Matrix();
                    world = Matrix.CreateTranslation(position);
                    primitive.Draw(world, view, projection, color);
                }
            }
        }
    }
    catch
    {

    }
}

So all we do in this function is check to see if the current Skeleton exists, and then we enumerate through each joint of the skeleton, drawing it to the screen. Note the ConvertRealWorldPoint function:

private Vector3 ConvertRealWorldPoint(Vector position)
{
    var returnVector = new Vector3();
    returnVector.X = position.X * 10;
    returnVector.Y = position.Y * 10;
    returnVector.Z = position.Z;
    return returnVector;
}

All we’re doing here is taking the postion from the Kinect SDK and scaling it up to be used in a 3D World. There are probably better approaches to this, so I’d like to see how anyone else does this. We then take that point and create a World Matrix to draw the primitve for each joint.

Once you’re done, run the sample and stand in front of your Kinect Sensor. The result should look something like this:

screen1

screen2

I’ve uploaded my version of the sample here so you can take a look and compare. Credit for the 3D Primitive Sample goes to Microsoft and the XNA Community Team. Let me know what you think in the comments, and please give me any feedback.

Enjoy,

Patrick Godwin

ASP.NET MVC3 on Windows Azure

In working with Windows Azure, I’ve noticed that the v1.3 of the Azure tools only support ASP.NET MVC2. This makes sense, given that that version of the tooling was released before ASP.NET MVC3 RTM’d. But what if we want to deploy an MVC3 application to Azure? I had to hunt around the internet for some info on this, so I figured I’d make a simple walkthrough for those who need the visuals. Well it’s easy, but takes a few steps.

First, open up Visual Studio and create a blank solution for the project:

step1

Next, add an ASP.NET MVC3 application to the solution:

step2

Next, we need to make sure that the ASP.NET MVC3 Assembly is set up for a bin deployment, as Azure does not yet support MVC3 out of the box. To do this, we set the “Copy Local” property of the System.Web.MVC.dll to true:

step3

Now, it’s time to add our Windows Azure project to the solution. Add another project to the solution, this time choose a “Windows Azure Project”:

Step4

You’ll now be asked what kind of Web or Worker Roles you’d like to add to the Azure project. You can feel free to add any Role you choose, but for this sample we’ll just hit OK with no Roles added:

Step5

Now we’re ready to add our MVC3 Project to our Azure project as a Web Role. Right click on Roles in the Azure project, mouse over Add and select “Web Role in Solution”:

Step6

Select your MVC3 Application and hit OK. It should be in the list of projects:

Step7

Now hit debug on the solution and Viola! MVC3 in the cloud! It should be noted that in order to debug Windows Azure applications, Visual Studio needs to be run at an elevated level.

It’s simple enough to do, and I’m sure the team in charge of the Azure tools for Visual Studio will fix this with the next release. Until then, enjoy ASP.NET MVC3. You can grab ASP.NET MVC3, the Windows Azure tools, and all sorts of other goodies using the Microsoft Web Platform Installer.

Questions? Comments? Feel free to hit me up on Twitter, or leave a comment here on the blog.

Until next time,
Patrick Godwin

Does Managed Programming Make Us Lazy?

With the popularity of managed languages in recent years, we have no doubt seen changes in the way we program. In recent years, we’ve seen many additions to the .Net family of managed languages, such as WPF, LINQ, WCF, and XNA, to name a few. But have we given up our good programming practices for the convenience of managed languages?

I don’t know about the majority of programmers out there, but I find myself writing sloppy code while working in a managed language. I’m not saying that I break all rules of style or flow, but I’m not as careful as I usually am. Let’s say we’re writing a function in C++ that calls a simple integer type function. The rule of thumb would be to check what it returns, and based on predetermined conditions, handle any errors that may arise. A pattern I’ve noticed, mainly with C# programming, is a total disregard for exception handling. And even if the exception is handled, it isn’t handled well. Last time I checked, “Throw Exceptio” was not a smart way to handle errors.

I’m sure there are many other examples of Managed Programming leading to lazy programming. This is only the most obvious one I’ve delt wtih. What issues have you had with managed languages? Please write about them in the comments, get the conversation going.
Thanks for reading,
Patrick Godwin

Note: I’m very sorry for the short length of this post. The quality of this post is also extremely bad, and does a poor job of articulating my thoughts on the subject. Mea culpa.

What Language Should I Choose?

I get asked this question at least once a month. Someone I know will come up and ask me this question. I can never answer this in a clear, concise way. So I’ve decided to write a blog post on all the Pros and Cons of the languages I know, and refer my friends to this post.

VB.NET: I love this language and I would recommend it to anyone starting programming. I learned how to program in Visual Basic 2005 and I can safely say it is one of the easiest languages I have ever used. The syntax reads in an easy, clear way. You don’t have to worry about silly semi-colons or curly braces. Building a User Interface is simple with the Visual Studio WinForm designer. The inclusion of the .NET Framework in Visual Basic has given some great resources to this already great language. The only downside to VB.NET is the dependence on the .Net Framework/Mono Runtime. While this is possibly one of the greatest features in the language, it takes away a lot of the power you see in an unmanaged language. Not to much of an issue, but still a con.

C#: C# is another great language that I would recommend C# to someone who has some prior experience. While the syntax and program design can be annoying at times, it is a very powerful language. While sharing the same advantages as VB.NET, it also has integration with the XNA Framework. This is by far the best managed Graphics API available. With it’s support for Windows, XBox 360, and the Zune it destroys every feature available in Managed DirectX. The biggest issue with C# is, again, it’s dependence on the .Net Framework/Mono Runtime and it’s slightly annoying program design.

C++/C: This is the language I always suggest when I am asked that question. While it is one of the hardest languages to learn, it is the best. I feel that learning a good, unmanaged language gives you a proper idea of independent programming. You aren’t relient on a managed environment, like in the .Net and Java languages, which means you have to manage your own memory. This teaches developers safe coding practices and helps them develop clean code. Once you have a good hold on the language, the object oriented concepts should be studied. This is when C++ becomes a true powerhouse. This allows you to develop complex applications or even great game engines. Once you have C++ under your belt, most other languages should come quite easily.

Python: A very powerful scripting language. You can accomplish many tasks in Python with relative ease, given it’s large, supportive user base. The only issue with Python is it’s annoying syntax. I swear I have so much trouble reading Python code for education. In a language like Python it can be hard to enforce good style, which is a big minus for the language.

Java: What can I say about Java? It has to be one of the worst languages I have ever TRIED to learn. I want to know who the genius was that, while writing the AP Computer Science Curriculum, thought “Well, we assume that these students have very little, if any, prior programming experience. What language should we choose? I know! Java!”. Whoever this man, or group of men, was, he screwed up big time. Why choose a language that has an annoying syntax and no advantages over the .Net Framework and force students to learn it for what should already be a tough class. While it is managed like the .Net languages, the Java Runtime Environment is receiving updates ALL the time. What are they constantly changing? Honestly, if you program in Java you have the patience of a saint. It takes real skill to tame this monstrosity of a language.

Now there are many more languages thant those I have covered here. MANY more. But these are the languages that have seen the most attention in recent years, and these are the languages I have used. I’ll always suggest C++ and then VB.NET for a beginner, but you should take everyone’s suggestion as just that, a suggestion. Read some tutorials, try each language, form your own opinions. Whatever you find the easiest and cleanest should be the language you chose.

Thanks for reading,
Patrick Godwin

Edit: Forgot to mention Mono when talking about the .Net Languages. Edited for clarity.