<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MQuandt.Blog &#187; sample code</title>
	<atom:link href="http://mquandt.com/blog/tag/sample-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://mquandt.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 09 May 2012 12:16:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='mquandt.com' port='80' path='/blog/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>XNA Cross-Platform Editing in Real-time</title>
		<link>http://mquandt.com/blog/2011/04/xna-cross-platform-editing-in-real-time/</link>
		<comments>http://mquandt.com/blog/2011/04/xna-cross-platform-editing-in-real-time/#comments</comments>
		<pubDate>Sun, 10 Apr 2011 05:00:52 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[sample code]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[xbox 360]]></category>

		<guid isPermaLink="false">http://mquandt.com/blog/?p=129</guid>
		<description><![CDATA[One of the cool new features appearing in a lot of modern engines is the ability to work in an editor on the PC and have the level changes replicate on a running console build of the game in real-time. For commercial games this often happens over the high speed USB connection, however due to &#8230; <a href="http://mquandt.com/blog/2011/04/xna-cross-platform-editing-in-real-time/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One of the cool new features appearing in a lot of modern engines is the ability to work in an editor on the PC and have the level changes replicate on a running console build of the game in real-time. For commercial games this often happens over the high speed USB connection, however due to the cross platform support in XNA, we can replicate this [in single player games/modes] using a System-Link connection and the XNA networking libraries.</p>
<p>The first step is deciding which side will create the session and which side will join. Considering we only have the option of working with the Xbox 360, and you might not want to include any debug views or information in your Xbox code, the option I chose is to have the Xbox create the session. The code is compatible on both platforms so if you&#8217;re willing to add some system to find and join an active session, you can easily change this to create the session on the PC. For the purposes of this article the session will only contain two &#8220;Players&#8221; and the Xbox will create whilst the PC side joins.</p>
<h2><span id="more-129"></span>Creating</h2>
<p>We start by creating a session, in XNA you can do this with the <a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.net.networksession.create.aspx" target="_blank">NetworkSession.Create()</a> command, which creates a <a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.net.networksession.aspx" target="_blank">NetworkSession</a> object that maintains the session. Then specify session.AllowJoinInProgress to be true, this lets you create the session and at any time join or rejoin from the PC side.</p>
<pre class="brush: csharp;">_session = NetworkSession.Create(NetworkSessionType.SystemLink, 1, 2);
_session.AllowJoinInProgress = true;</pre>
<p>Another key thing to note here is that you will want to specify the network session type as SystemLink, this lets you sign in with your Creators Club gamer tag on the Xbox, and use a local player on the PC side &#8211; no extra accounts!<br />
We then specify a maximum of one local gamer on the Xbox side, and a total of two gamers in the session. (Change as needed)<br />
Finally we handle the session.GamerJoined event and in the handler we start the game if the current session state is set to Lobby.</p>
<h2>Receiving</h2>
<p>At this time you might want to also create a PacketReader and PacketWriter, as we will be using these to do our communication. These can be created at any time, and are not tied to a session, so it is a good idea to create them once and re-use over multiple sessions/games.</p>
<p>How you want to receive the data is up to you, however I chose to allow the Xbox game to register some callbacks for named commands. That way the PC side can specify the name of the command and send data along, which can be passed through as a byte array. This is rather simple to setup, you simply maintain a Dictionary&lt;string, Action&lt;byte[]&gt;&gt; that contains all of the callback delegates and names. Then when a packet comes in it will have the name of the callback as well as the byte[] data, which lets you gets the callback from the Dictionary and call it, passing in the data as is.</p>
<p>On the PC side you simply send the callback name, length of the byte array and the bytes themselves, ready for the Xbox to accept and use.</p>
<p>I further split this into &#8220;Commands&#8221; and &#8220;Data&#8221;, to provide a clear separation of things like console commands, and raw data, but both use the same system of a Action&lt;byte[]&gt; delegate callback.</p>
<h2>Joining</h2>
<p>When joining, the PC needs to do two things:</p>
<ol>
<li>Search for sessions</li>
<li>Join a chosen session</li>
</ol>
<p>The <a href="http://msdn.microsoft.com/en-us/library/bb975501.aspx" target="_blank">NetworkSession.Find()</a> method provides a list of available sessions currently running over SystemLink, which is usually going to be just one, however if you&#8217;re working in a larger team with multiple systems on the same network, you might want to list them and display the host gamertag, so that you can choose the right one.</p>
<p>Once you have chosen the network session, you pass that to <a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.net.networksession.join.aspx" target="_blank">NetworkSession.Join()</a> which creates a NetworkSession object that you can use just like you do on the server &#8211; except this time you don&#8217;t worry about starting the game.</p>
<h2>Updating</h2>
<p>Once the session has been created and is up and running, on both platforms you need to call session.Update() in your update loop. This handles the networking side of things and gets/sends the packets as needed.</p>
<p>Once you have updated, you can make use of the PacketReader and PacketWriter objects to receive and send packets respectively. To receive the data, you need to first check if there is data available by checking the session.LocalGamers[0].IsDataAvailable flag. If there is data available, you can call session.LocalGamers[0].ReceiveData() passing in the PacketReader which you can then use to parse the packet and call the appropriate callback.</p>
<p>If you have data you want to send, you just need to call session.LocalGamers[0].SendData(), and pass in the PacketWriter with the data you want to send.</p>
<p>In the background XNA handles the network magic, so you just have to worry about handling the data on either end.</p>
<h2>Code</h2>
<p>I didn&#8217;t have time to put up a complete code sample for this article, however I hope you understand the general technique. Code for something like this really needs to be tailored to your purpose. If you are just writing a debug console or tweak system, you can just use the command system mentioned below. However if you want to write a sync system for your editor, with camera sync and everything else, you will probably want to change how you send and receive data to optimise for what you are sending.</p>
<h3>Create</h3>
<pre class="brush:csharp;">_session = NetworkSession.Create(NetworkSessionType.SystemLink, 1, 2);
_session.AllowHostMigration = false;
_session.AllowJoinInProgress = true;
_session.GamerJoined += (s, e) =&gt;
{
    if (_session.SessionState == NetworkSessionState.Lobby)
        _session.StartGame();
};
_session.GamerLeft += (s, e) =&gt; _session.EndGame();</pre>
<h3>Find</h3>
<pre class="brush:csharp;">AvailableNetworkSessionCollection sessions = NetworkSession.Find(NetworkSessionType.SystemLink, 1, null);</pre>
<h3>Join</h3>
<p>(&#8216;session&#8217; is an AvailableNetworkSession taken from the collection in Find)</p>
<pre class="brush:csharp;">_session = NetworkSession.Join(session);</pre>
<h3>Update</h3>
<pre class="brush:csharp;">if (_session != null)
{
    _session.Update();
    if (_writer.Length &gt; 0)
        _session.LocalGamers[0].SendData(_writer, SendDataOptions.None);
    while (_session != null &amp;&amp; _session.LocalGamers[0].IsDataAvailable)
    {
        NetworkGamer sender;
        int numBytes = _session.LocalGamers[0].ReceiveData(_reader, out sender);
        if (numBytes &gt; 0 &amp;&amp; !sender.IsLocal)
        {
            string methodName = _reader.ReadString();
            int numBytes = _reader.ReadInt32();
            var data = _reader.ReadBytes(numBytes);
            Action method;
            if (!string.IsNullOrEmpty(methodName) &amp;&amp; _commands.TryGetValue(methodName, out method))
                method(data);
        }
    }
}</pre>
<h3>Sending</h3>
<pre class="brush:csharp;">public void SendCommand(string methodName, byte[] data)
{
    _writer.Write(methodName.ToLower());
    if (data != null)
    {
        _writer.Write(data.Length);
        _writer.Write(data);
    }
    else
    {
        _writer.Write(0);
    }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://mquandt.com/blog/2011/04/xna-cross-platform-editing-in-real-time/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>An update on LPP + Sample</title>
		<link>http://mquandt.com/blog/2010/02/an-update-on-lpp-sample/</link>
		<comments>http://mquandt.com/blog/2010/02/an-update-on-lpp-sample/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 04:48:41 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[DirectX]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[game engine gems]]></category>
		<category><![CDATA[game programming gems]]></category>
		<category><![CDATA[gpu pro]]></category>
		<category><![CDATA[light pre pass]]></category>
		<category><![CDATA[sample code]]></category>

		<guid isPermaLink="false">http://mquandt.com/blog/2010/02/an-update-on-lpp-sample/</guid>
		<description><![CDATA[Hi everyone, first of all apologies for the delay. there has been quite a lot going on in my life, but I am working hard on getting the sample done. This time around I wanted to make sure I had a sample ready to go with the article, especially since the article will focus on &#8230; <a href="http://mquandt.com/blog/2010/02/an-update-on-lpp-sample/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hi everyone, first of all apologies for the delay. there has been quite a lot going on in my life, but I am working hard on getting the sample done. This time around I wanted to make sure I had a sample ready to go with the article, especially since the article will focus on the technique and theory and have little to no code – although I will certainly focus on XNA when it comes to mentioning issues and benefits to certain parts.</p>
<p>One of the main delays was getting the sample code out of my engine, and cleaning it up so it can be used as a learning tool. Unfortunately most of the code was hacked on as I fixed issues in the LPP renderer, and added features.</p>
<p>I have a fair bit of the article written, however recently I was implementing shadows and realised that I never thought about how they would integrate into the system, so I decided to rewrite my own renderer (which should not take long) and at the same time keep the code clean so it can serve as a sample as well.</p>
<p>This means I will probably also have Directional Light shadows in the sample. This then allows me to write about point lights, and spotlights later on, and include shadows for both.</p>
<p>So again, sorry for the delay, I am working hard to get it out soon.</p>
<p>As an aside, I noticed that Game Programming Gems is getting an 8th Edition, something I was not expecting, so with that, GPU Pro, and Game Engine Gems, I might be able to find some more cool things to write about.</p>
<p>Thank You for your patience.</p>
]]></content:encoded>
			<wfw:commentRss>http://mquandt.com/blog/2010/02/an-update-on-lpp-sample/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Note on Sample Code</title>
		<link>http://mquandt.com/blog/2009/12/a-note-on-sample-code/</link>
		<comments>http://mquandt.com/blog/2009/12/a-note-on-sample-code/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 14:04:07 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Website]]></category>
		<category><![CDATA[light pre pass]]></category>
		<category><![CDATA[sample code]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://mquandt.com/blog/2009/12/a-note-on-sample-code/</guid>
		<description><![CDATA[Just a quick note about the sample code download for the current Light Pre Pass tutorial, and probably future tutorials. I am in the process of cleaning up and commenting the code, and hope to have it out really soon. I am to have a complete tutorial with all the information on the page, however &#8230; <a href="http://mquandt.com/blog/2009/12/a-note-on-sample-code/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Just a quick note about the sample code download for the current Light Pre Pass tutorial, and probably future tutorials. I am in the process of cleaning up and commenting the code, and hope to have it out really soon. I am to have a complete tutorial with all the information on the page, however in some cases I don’t explicitly provide every bit of code, to prevent the tutorial from becoming too long.</p>
<p>As this implementation and sample is a part of my own engine, I need to split the code from the main project and ensure it is readable and in a suitable educational state. This should not take too long, so if you are watching this blog, I will post when this code is available, and link to it in the original article as well.</p>
<p>Sorry for any inconvenience, I hope the article will satisfy your LPP needs in the meanwhile. As usual if you have any questions, post them in the comments of the respective article and I will answer them as soon as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://mquandt.com/blog/2009/12/a-note-on-sample-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

