Posts Tagged ‘visual studio’

How to create a silverlight xna game (using silverarcade)

Monday, March 1st, 2010

Also, useful if you want to convert your existing XNA game to a silverlight app.

The silverarcade libraries help you keep most of you existing XNA code intact. I’ll assume that you are using Visual Studio and have all the proper Silverlight SDK stuff installed (if you haven’t, good luck. It took me way too long to figure that out. Maybe I will post details if I ever recover).

I typed up these instructions after my first conversion and have followed them to get few of my own games converted. So hopefully they will help you, too:

  • In Visual Studio, Choose new project > Silverlight > Silverlight Application. Click NO to the prompt about “Host as a new web site”
  • Open MainPage.xaml and add the following the <UserControl> tag as an atribute:
    
    xmlns:game="clr-namespace:[your project name here]"
    
  • In MainPage.xaml, Add the following to the <Grid> contents:
    
      <Canvas>
        <game:Game2D x:Name="Game1"/>
      </Canvas>
    
  • Right click on project name in Solution Explorer > add > class…
    Call the class Game2D.cs
  • In Game2D.cs, replace the “using…” lines with:
    
    
        using System;
        using System.Collections.Generic;
        using System.Linq;
    
        #if(SILVERLIGHT)
        using SilverArcade.SilverSprite;
        using SilverArcade.SilverSprite.Audio;
        using SilverArcade.SilverSprite.Content;
        using SilverArcade.SilverSprite.GamerServices;
        using SilverArcade.SilverSprite.Graphics;
        using SilverArcade.SilverSprite.Input;
        using SilverArcade.SilverSprite.Media;
        using SilverArcade.SilverSprite.Storage;
        #else
        using Microsoft.Xna.Framework;
        using Microsoft.Xna.Framework.Audio;
        using Microsoft.Xna.Framework.Content;
        using Microsoft.Xna.Framework.GamerServices;
        using Microsoft.Xna.Framework.Graphics;
        using Microsoft.Xna.Framework.Input;
        using Microsoft.Xna.Framework.Media;
        using Microsoft.Xna.Framework.Net;
        using Microsoft.Xna.Framework.Storage;
        #endif
    
  • In Game2D.cs, replace the class declaration with:
    
        public class Game2D : SilverArcade.SilverSprite.Game
    
  • In Game2D.cs, Add global variables:
    
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
    
  • In Game2D.cs, add constructor:
    
            public Game2D()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
                graphics.PreferredBackBufferWidth = 1000;
                graphics.PreferredBackBufferHeight = 600;
            }
    
  • In Game2D.cs, Add initialize method:
    
            protected override void Initialize()
            {
                // TODO: Add your initialization logic here
    
                base.Initialize();
            }
    
  • In Game2D.cs, Add loadcontent method:
    
            protected override void LoadContent()
            {
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(GraphicsDevice);
                // TODO: use this.Content to load your game content here
            }
    
  • In Game2D.cs, Add unloadcontent method:
    
            protected override void UnloadContent()
            {
                // TODO: Unload any non ContentManager content here
            }
    
  • In Game2D.cs, Add update method:
    
            protected override void Update(GameTime gameTime)
            {
                // Allows the game to exit
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();
                // TODO: Add your update logic here
                base.Update(gameTime);
            }
    
  • In Game2D.cs, Add draw method:
    
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.Gray);
                spriteBatch.Begin();
                spriteBatch.End();
                base.Draw(gameTime);
            }
    
  • Add silversprite.dll to project by right clicking on references and add existing item and choose your silversprite.dll (probably put this in debug or release folder or wherever).

Important notes:

When adding content you must click each image file/etc. and go to properties and change Build action to Content.

.wav files are not allowed as silversprite audio, must use mp3 or wav

Don’t need graphics.applyChanges() line when requesting preferred size