Using Visual Studio Code Snippets for XNA Development

Saturday, March 15 2008 - ,

On the XNA IRC Channel this morning someone asked a question on how to use the Visual Studio Code snippets. I have been using these snippets for a long time, not normally for XNA but for other applications. But when the question popped up I thought I would put something together for the XNA Development world, in some cases the snippets functions could assist us with code that we seem to type all of the time.

For this example I have put to different snippets together, note that the principles here can be used in other different types off applications as well. Also the examples here can be used in all SKU's of the Visual Studio 2005 system; I will update this article once XNA is supported under the Visual Studio 2008 system.

In Visual Studio you have the ability to use two main types of snippets, the first is a surround snippet and the second is a normal code section.

The first snippet that I plan to show you will give you the ability to highlight a section of code and surround it with a #if !XBOX360 call. Now I know that this doesn't seem like much, but when you are developing a complete section of say Mouse Input code and then decide to turn on the Xbox 360 project. With this snippet you should now be able to just highlight the code, right click and select surround with and you are done.

The second snippet will allow you to use a shortcut to enter a block of code into your application. For this example I have decided to design a small section of code that will setup your Graphic Device to use a 720p format (1280x720). Once complete you will be able to add the code by just typing gd720p and pressing tab twice on the keyboard.

Ok let's get started...

The first thing that you will need to do is to create a folder to store the snippets. This folder can be located anywhere as long as Visual Studio and your User can get access to it. In the case of a Team you might be able to create a folder on a shared location and allow all of the team members to connect to it. Once the folder has been created (I Called mine ‘XNA 2.0 Code Snippets', this way as I work on both versions I will not get confused) go back and Fire Up Visual Studio. As you will also be testing these application snippets also start up a basic Windows Game Application so that we have something to work with.

Once inside Visual Studio press ‘CTRL + K + B' to bring up the Code Snippet Manager. Inside the Code Snippets manager press the add button and navigate to the location where you created the folder to store the snippets.

Next step is to add the code snippets, below is the code for both of the snippets that I am planning to use in this example. The code is easy to understand and in some future articles I will go through expanding on them.

[code language="C#"]

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
   <CodeSnippet Format="1.0.0">
      <Header>
         <Title>#if not Xbox 360</Title>
         <Shortcut>#ifxbox</Shortcut>
         <Description>Code snippet for #if !XBOX360</Description>
         <Author>Virtual Realm Consulting</Author>
         <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
            <SnippetType>SurroundsWith</SnippetType>
         </SnippetTypes>
      </Header>
         <Snippet>
            <Code Language="csharp"><![CDATA[#if !XBOX360
                      $selected$
                      $end$ 
                      #endif]]>
            </Code>
         </Snippet>
   </CodeSnippet>
</CodeSnippets>

[/code]

[code language="C#"]

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
   <CodeSnippet Format="1.0.0">
      <Header>
         <Title>Graphic Device - 720p</Title>
         <Shortcut>gd720p</Shortcut>
         <Description>Code Snippet to Set up the Graphic Device for 720p</Description>
         <Author>Virtual Realm Consulting</Author>
         <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
         </SnippetTypes>
      </Header>
      <Snippet>
         <Code Language="csharp"><![CDATA[graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;
            graphics.ApplyChanges();
            $end$]]>
         </Code>
      </Snippet>
   </CodeSnippet>
</CodeSnippets>

[/code]

The next step is to open up your favourite XML Editor; my preference is Primal Script. Create a new file and call it "pp_ifxbox.snippet" making sure the extension is .snippet, once done copy the first section of code into the file. Do the same for the second, but call the file "gd720p.snippet". When complete save both of these files in you new snippet directory.

Now back to your visual Studio Application.

Inside your Windows game application what I would like you to do now is scroll down to where the Content Root Directory is set, underneath that start a new line and type gd720p and press ‘TAB' Twice. And there you have it a simple code block inserted.

Now highlight the code that we just added to the application, once highlighted click on it with the right mouse button. You will see a menu item appear that says "Surround with", select that. Once selected you should be presented with a folder list that has your new XNA Snippet Folder listed, select that and then select the If !XBOX360 tag. This will now surround the highlighted code with the #if !XBOX360....

Hope you have understood the process, I do plan on expanding on this and creating some snippets that will in turn ask you for variables once you enter them. At least now with the main setup done, if you do develop or download others snippets, all you have to do is drop them into the Directory and you are right to go.

kick it on DotNetKicks.com kick it on gamedevkicks.com

Similar Posts

  1. Blender 3D - Texturing a Box
  2. XNA Tutorial - Base Application
  3. Getting Started with Blender 3D and XNA

4 comment(s)

Clicky Web Analytics