XNA application in Web Pages


You can Run XNA Applications from Web without installing them on the PC.

- Are you joking?
- Absolutely not!

It has some drawbacks and looks silly but the truth is you can call them. And plus its a little bit tricky.

In this article I am going to show you how you can do it.

First of all create a new Empty Web Application:

image1.gif

Add 2 WebForms(Default & Popup)

image2.gif

Add a Button on Default.aspx

image3.gif

In CodeBehind add:

protected void Button2_Click(object sender, EventArgs e)
{
  PopUp("Popup.aspx");
}

public void PopUp(string url)
{
  string popup = "<script language='javascript'>" +
                 "window.open('" + url + "', 'CustomPopUp', " +
                 "'width=200, height=200, resizable=no')" +
                 "</script>";
 
  Page.RegisterStartupScript("Popup", popup);
}


And then add these references in your Popup.aspx:

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.Input.Touch;
using Microsoft.Xna.Framework.Media;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

Update your partial class for your webpage as seen below:

public partial class Popup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
        static Game1 game = new Game1();
 
        delegate void CallXNADelegate(Game1 windows);
        private void updateXNAWindow(Game1 windows)
        {
            if (System.Windows.Forms.Control.FromHandle(game.Window.Handle).InvokeRequired)
            {
                CallXNADelegate del = new CallXNADelegate(updateXNAWindow);
                System.Windows.Forms.Control.FromHandle(game.Window.Handle).Invoke(del, new object[] { windows });
            }
            else
            {
                windows.Run();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            updateXNAWindow(game);
        } 
    }


We are using Threading Invoke our delegate.

Then add this class to your Popup.aspx

public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        protected override void UnloadContent()
        {
            Application.ExitThread();
        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

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

        protected override void EndRun()
        {
            base.EndRun();
            Application.ExitThread();
        }
    }

After that run your WebApplication

image4.gif

So? What do you think?

Not in the Internet but you can use this method in your company's Intranet.

Business Games would look nice in your Intranet.

Note: And hey this is just a recommendation for Intranets.Everyone knows Windows based objects dont work on client computers on Internet ;) So dont make a comment like that below this article.
 

Up Next
    Ebook Download
    View all
    Learn
    View all
    Araf Global is a software consultancy company founded in 2016 focusing on cutting edge technologies.