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

Imagine Cup US 2010 Finals – Anthony Salcito Interview

During the first day of the US Imagine Cup Finals, I had the pleasure to speak with Anthony Salcito, Microsoft’s Vice President of Worldwide Education. We talked about the need for new approaches to technology in education, and then spoke for a while about what the Imagine Cup means for Microsoft and for the community. I filmed our conversation about the Imagine Cup, which you can watch below. A very special thanks to Austin Stewart, from Microsoft’s PR team, who helped get me this great opportunity to meet Mr. Salcito.

More tomorrow morning.

Microsoft Student Insider Kick-Off: Scott Hanselman

During my time at the Redmond campus, I was fortunate enough to meet with Scott Hanselman, Principal Program Manger Lead for Microsoft’s Developer Division. He also runs a very successful blog and podcast. Mr. Hanselman’s position at Microsoft requires him to travel a lot. This, combined with his living in Oregon, makes it difficult to snag a meeting with him. But we were lucky enough to meet with Mr. Hanselman for an hour in Building 5.

We started off our time with Mr. Hanselman by introducing ourselves. He then lectured us on the history of Bulletin Board Systems, relating it to a recent interview he had done on his podcasts. He then talked about his blog, and how useful social media is.

And then we got to the new technology. Mr. Hanselman first showed us Boot to VHD, a new feature in WIndows 7 that allows us to take the VHDs created in Virtual PC 2007, Hyper-V, and other parts of the virtualization platform, and boot our machine to this virtual OS. This allows the virtual hard disk to use hardware rather than the emulated environment when running. This follows the idea of “less virtual, more machine”.

The next demos Mr. Hanselman showed us struck me as very cool. He decided to demonstrate two new features of .Net 4.0: PLINQ and MEF. Now when I heard him say PLINQ, I had to do a double take. My understanding was PLINQ had been around for at least a year. But I was happily surprised to find that PLINQ (Parallel Language Integrated Query) was finally shipping as a part of the .Net Framework. This made me happy. Finally I could add support for multi-core processors with very little hassle. Simply invoke a query with the .AsParrallel() extension method, and you’ve got yourself a query working across multiple cores. There are other features in PLINQ, and I urge you to check out the Parallel Programming With .Net blog for more info.

The final demo Mr. Hanselman showed us was MEF. MEF stands for the Managed Extensibility Framework. Imagine the following situation: You are an enterprise developer working for a company that keeps inventory of different kinds of cars. Each car has similar qualities, defined in an interface, while being unique in itself. Your boss approaches you, requiring a new brand be added to the application used for keeping inventory. Due to the lack of a quality plugin system, this task would be very tedious and an all around pain in the neck. What MEF allows you to do, is simply create your new class, implementing this interface, export it through a new DLL, and the inventory application will do a “composition”, loading any and all classes that use the interface declared in the core of the API. I was extremely excited when Mr. Hanselman showed us this new feature. I’ve always loved the idea of extensibility, but hated using slow Reflection based techniques for dynamically loading classes. I highly recommend you take a look at MEF over on CodePlex. It’s a very cool framework and could make great leaps in improving application development productivity.

After that demo, we had to go our separate ways again. Like I said, Mr. Hanselman is a very busy man, and I was very grateful to have met with him. I highly suggest your follow him on Twitter or read his blog, as he frequently provides interesting links related to social media and new technology.

Excuses, Excuses…

Hey, turns out I have a blog. Who’d have thought.

I really must apologize for my not updating. I’m sure i could come up with plenty of excuses, but where would that get us? So instead, I’m going to give a brief update as to what is going on.

First and foremost: Happy New Year! 2009 was an interesting and hectic year. We saw history made when Barrack Obama was inaugurated as the first African American president. We had quite a few notable celebrities pass away this year, which got more news time then most issues.
It was also a very good year for social media. Since I joined Twitter, I saw many of my friends from school joining. I’ve also seen many people joining, Facebook. What this has taught me: Social Networking is here to stay.
It was also an exciting year for technology. This year we saw Microsoft announce Project Natal, release Windows 7, Windows Server 2008 R2, Visual Studio 2010 Beta 1 and 2, and the Zune HD. I was very excited to see these technologies released, and I can safely say I’ve gotten to play with all except Project Natal.
But 2009 had it’s low points. Most notable: the economy. It’s been a hard couple of years, and there is more to come. But we will get through it.
Now, seeing as it is 2010, I should make a New Years resolution, no? Well, I think I can make two very simple resolutions: Blog more, and write more code. Like I said, I could create multiple excuses as to why I have not done either of these lately, but where would that get me? So instead, I’m just going to work harder at it.

In closing, I’d like to give shout-outs to people online.

  • LittleKuriboh: You are awesome. You created a YouTube sensation, and have always brought a smile to my face. I wish you the best in the new year, and once again congratulate you on your Open Web Award.
  • Kelson Thomas: You always spark interesting conversation, be it on Twitter, in class, or over IM. You’ve made 2009 a hoot and a half.
  • Eric Hawtin: You’ve been just as entertaining as Kelson. You always keep Twitter alive and provide good (well not always) input to our conversations, and I thank you for that.
  • Microsoft: We saw this coming. They had a great year in releases, and I look forward to seeing what they will do in 2010.
  • Alfred Thompson: You always have interesting resources for students, and you always write interesting blog posts. You’ve had me scratching my chin more then once. I thank you, and wish you the best in 2010

I could go on for hours, listing the people who made 2009 a good year, but I won’t, as that would take up a few pages.

In closing, I’d like to thank everyone for an awesome year, and I am excited to work with you all in 2010. You’ve all been great, and I am keeping my head high for the next 365 days.

Thanks for reading,
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.

High School Computer Science: A Student’s Perspective

Computer Science curriculum has always been a hot topic for discussion, but in the last few years I have seen more in more debate over how Computer Science should be taught. All the time I hear different School District Higher Ups and Teachers trying to come to some sort of agreement on a good plan. From what I have seen, most Computer Science classes are teaching the wrong ideas at the wrong time.

I’ve seen this first hand in Port Huron Area Schools. Don’t get me wrong, I love my programming classes. But there is no set curriculum for our District’s Computer Programming Class. How can a Computer Programming class exist without a curriculum. This forces the teacher to teach what the majority will understand the best while the more advanced students have to focus on their own skills. That isn’t a huge problem, but there should still be some sort of guidelines for teaching the class.

Another issue I’ve noticed with our Computer Science curriculum is the definition of a Computer Science class. When did a Computer Applications class become a Computer Science class. One of the Counselors at Port Huron Northern has gone around telling students that “It looks good to have a Computer Science class on your Transcript. I suggest you take Computer Applications”. Last time I checked “This is how you use Microsoft Powerpoint” was not acceptable as a Computer Science class.

Now I’m sure this is not the same around the country, but this is the main issue I’ve noticed. I also think, from glancing at the AP Computer Science course outline, we’re teaching the wrong things in introductory Computer Programming classes. Style is something that should be emphasized from day one. Button1 or Div1 are never acceptable if you ask me. They then spend the first few weeks of a class going over the introduction to a language, then move on to working on projects. In reality, they should begin by teaching theory. Students should learn how a program flows rather then jumping head first into a programming language. I learned this the hard way when I decided to pick up VB.NET as my first language. I made multiple design mistakes, spent many hours browsing MSDN for guidance, and used silly variable names like Button1 or TextBox2. If only I had understood good style or even the basic concepts of If/Then, For/While Loops, and Switch Statements.

According to the AP Computer Science curriculum, the first part of the course should be spent teaching Object Oriented Programming. This I support. But on closer examination, they recommend introducing Object Oriented Programming. This is ridiculous. If the student has taken a pre-AP Computer Science class, they should be introduced to the basic concepts of Object Oriented Design. If half the class is spent teaching a concept, then the second half is spent teaching the concepts in a language like Java, there is something wrong with that class.

Now these are just my thoughts, but I’m sure many people agree with me. Anyone else have an opinion on this? Your thoughts and comments are always welcome.

Thanks for reading,
Patrick Godwin