<?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>conceptual inertia &#187; code</title>
	<atom:link href="http://www.conceptualinertia.net/aoakenfo/category/code/feed" rel="self" type="application/rss+xml" />
	<link>http://www.conceptualinertia.net/aoakenfo</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 29 Jul 2010 08:12:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>simple fsm</title>
		<link>http://www.conceptualinertia.net/aoakenfo/simple-fsm</link>
		<comments>http://www.conceptualinertia.net/aoakenfo/simple-fsm#comments</comments>
		<pubDate>Sun, 14 Jan 2007 01:57:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://conceptualinertia.wordpress.com/?p=212</guid>
		<description><![CDATA[Take a look at the following output: The above output was generated by an Orc entity with an associated finite state machine (FSM). Although a FSM can be used for many things, in game development it&#8217;s typically used to model character behaviour. Why? It&#8217;s simple and easy to debug. Let&#8217;s have a look at Main [...]]]></description>
			<content:encoded><![CDATA[<p>Take a look at the following output:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fsmdemo1.jpg"><img class="alignnone size-full wp-image-213" title="fsmdemo1" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fsmdemo1.jpg" alt="" width="666" height="333" /></a></p>
<p>The above output was generated by an Orc entity with an associated finite state machine (FSM). Although a FSM can be used for many things, in game development it&#8217;s typically used to model character behaviour. Why? It&#8217;s simple and easy to debug.</p>
<p>Let&#8217;s have a look at Main to get a high-level feel for using an Orc entity:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">static void</span> Main(<span style="color: #0000ff;">string</span>[] args)
{
    <span style="color: #33cccc;">Orc </span>o = <span style="color: #0000ff;">new </span><span style="color: #33cccc;">Orc</span>();

    o.Event = <span style="color: #33cccc;">OrcEvent</span>.Wander;
    o.UpdateState();

    o.Event = <span style="color: #33cccc;">OrcEvent</span>.Eat;
    o.UpdateState();

    o.Event = <span style="color: #0000ff;">null</span>;
    o.UpdateState();

    <span style="color: #33cccc;">Console</span>.ReadLine();
}
 </code></pre>
<p>In a real game loop the UpdateState method would be called once per frame. Continuing with a top-down approach, let&#8217;s see the components that make up an Orc entity. First, we find a enum to represent simple event identifiers.</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">enum </span><span style="color: #33cccc;">OrcEvent</span>
{
    Eat,
    Wander
}
 </code></pre>
<p>Events can trigger a change in state. It&#8217;s possible to use a table to map events to associated states. For example, the OrcEvent.Eat maps to the EatState and the OrcEvent.Wander maps to the WanderState. However, states don&#8217;t always have a 1-to-1 correspondence. For example, an OrcEvent.SeeEnemy could map to an AttackState. Here we see a transition table specific to Orcs:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">class </span><span style="color: #33cccc;">OrcTransitionTable </span>: Ai.<span style="color: #33cccc;">StateTransitionTable</span>
{
    <span style="color: #0000ff;">public </span>OrcTransitionTable()
    {
        <span style="color: #0000ff;">base</span>.table.Add(<span style="color: #33cccc;">OrcEvent</span>.Eat, <span style="color: #0000ff;">new </span><span style="color: #33cccc;">EatState</span>());
        <span style="color: #0000ff;">base</span>.table.Add(<span style="color: #33cccc;">OrcEvent</span>.Wander, <span style="color: #0000ff;">new </span><span style="color: #33cccc;">OrcWanderState</span>());
    }
}
 </code></pre>
<p>The base type StateTransitionTable handles housekeeping chores for the table:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
</code><span style="color: #0000ff;"><span class="keyword">abstract public class</span></span> <span style="color: #33cccc;"><span class="usertype">StateTransitionTable</span></span>
{
    <span style="color: #0000ff;"><span class="keyword">protected</span> </span><span style="color: #33cccc;"><span class="usertype">Dictionary</span></span>&lt;<span style="color: #0000ff;"><span class="keyword">object</span></span>, <span style="color: #33cccc;"><span class="usertype">IState</span></span>&gt; table = <span style="color: #0000ff;">new </span><span style="color: #33cccc;">Dictionary</span>&lt;<span style="color: #0000ff;"><span class="keyword">object</span></span>, <span style="color: #33cccc;"><span class="usertype">IState</span></span>&gt;();

    <span style="color: #0000ff;"><span class="keyword">public void</span></span> SetState(<span style="color: #0000ff;"><span class="keyword">object</span> </span>evt, <span style="color: #33cccc;"><span class="usertype">IState</span> </span>state)
    {
        table.Add(evt, state);
    }

    <span style="color: #0000ff;"><span class="keyword">public</span> </span><span style="color: #33cccc;"><span class="usertype">IState</span> </span>GetState(<span style="color: #0000ff;"><span class="keyword">object</span> </span>evt)
    {
        Ai.<span style="color: #33cccc;"><span class="usertype">IState</span> </span>i = <span style="color: #0000ff;"><span class="keyword">null</span></span>;

        <span style="color: #0000ff;"><span class="keyword">try</span></span>
        {
            i = table[evt];

        }
        <span style="color: #0000ff;"><span class="keyword">catch</span> </span>(<span style="color: #33cccc;"><span class="usertype">KeyNotFoundException</span></span>)
        {
            <span class="keyword">return <span style="color: #0000ff;">null</span></span>;
        }

        <span style="color: #0000ff;"><span class="keyword">return</span> </span>i;
    }
}</pre>
<p>What exactly is IState? It&#8217;s an interface with 3 methods for state processing:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">public interface</span> <span style="color: #33cccc;">IState</span>
{
    <span style="color: #0000ff;">void </span>Enter(<span style="color: #33cccc;">Entity </span>e);
    <span style="color: #0000ff;">void </span>Execute(<span style="color: #33cccc;">Entity </span>e);
    <span style="color: #0000ff;">void </span>Exit(<span style="color: #33cccc;">Entity </span>e);
}
 </code></pre>
<p>Each method takes an entity type as a parameter. When switching states, the Exit method is called for the current state. The new state&#8217;s Enter method is then called. The current state is then assigned the new state. Execute is called once per frame to perform state processing.</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code><span style="color: #0000ff;">
abstract public class</span> <span style="color: #33cccc;">Entity</span>
{
    <span style="color: #0000ff;">protected </span>Ai.<span style="color: #33cccc;">StateTransitionTable </span>transitionTable = <span style="color: #0000ff;">null</span>;
    <span style="color: #0000ff;">protected </span><span style="color: #33cccc;">IState </span>currentState = <span style="color: #0000ff;">null</span>;

    <span style="color: #0000ff;">public void</span> UpdateState()
    {
        <span style="color: #0000ff;">if </span>(currentState != <span style="color: #0000ff;">null</span>)
            currentState.Execute(<span style="color: #0000ff;">this</span>);
        <span style="color: #0000ff;">else</span>
            System.Diagnostics.<span style="color: #0000ff;">Trace</span>.WriteLine(<span style="color: #993300;">"zero state"</span>);
    }

    <span style="color: #0000ff;">public object</span> Event
    {
        <span style="color: #0000ff;">set</span>
        {
            <span style="color: #0000ff;">if </span>(value == <span style="color: #0000ff;">null</span>)
            {
                currentState.Exit(<span style="color: #0000ff;">this</span>);
                currentState = <span style="color: #0000ff;">null</span>;
                <span style="color: #0000ff;">return</span>;
            }

            <span style="color: #33cccc;">IState </span>i = transitionTable.GetState(<span style="color: #0000ff;">value</span>);

            <span style="color: #0000ff;">if </span>(i != <span style="color: #0000ff;">null</span>)
            {
                <span style="color: #0000ff;">if </span>(currentState != <span style="color: #0000ff;">null</span>)
                    currentState.Exit(<span style="color: #0000ff;">this</span>);

                currentState = i;
                currentState.Enter(<span style="color: #0000ff;">this</span>);
            }
        }
    }
}
 </code></pre>
<p>Implementing an Orc entity is trivial:</p>
<ul>
<li> Inherit from Ai.Entity</li>
<li>Set the transition table in the constructor</li>
<li>Start adding methods for the new character</li>
</ul>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">class </span><span style="color: #33cccc;">Orc </span>: Ai.<span style="color: #33cccc;">Entity</span>
{
    <span style="color: #0000ff;">static </span>Orc()
    {
        transitionTable = <span style="color: #0000ff;">new </span><span style="color: #33cccc;">OrcTransitionTable</span>();
    }

    <span style="color: #0000ff;">public void</span> Drool()
    {
        <span style="color: #33cccc;">Console</span>.WriteLine(<span style="color: #993300;">"drooling..."</span>);
    }
}
 </code></pre>
<p>Of course, an entity will need a variety of states to transition to. To create a new state type implement the IState interface. For example, here are a few Orc-related states:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">class </span><span style="color: #33cccc;">EatState </span>: Ai.<span style="color: #33cccc;">IState</span>
{
    <span style="color: #0000ff;">public void</span> Enter(Ai.<span style="color: #33cccc;">Entity </span>e)
    {
        <span style="color: #33cccc;">Console</span>.WriteLine(<span style="color: #993300;">"entering eat state"</span>);
    }

    <span style="color: #0000ff;">public void</span> Execute(Ai.<span style="color: #33cccc;">Entity </span>e)
    {
        <span style="color: #33cccc;">Console</span>.WriteLine(<span style="color: #993300;">"executing eat state"</span>);
    }

    <span style="color: #0000ff;">public void</span> Exit(Ai.<span style="color: #33cccc;">Entity </span>e)
    {
        <span style="color: #33cccc;">Console</span>.WriteLine(<span style="color: #993300;">"exiting eat state"</span>);
    }
}

<span style="color: #0000ff;">class </span><span style="color: #33cccc;">OrcWanderState </span>: Ai.<span style="color: #33cccc;">IState</span>
{
    <span style="color: #0000ff;">public void</span> Enter(Ai.<span style="color: #33cccc;">Entity </span>e)
    {
        <span style="color: #33cccc;">Console</span>.WriteLine(<span style="color: #993300;">"entering wander state"</span>);
    }

    <span style="color: #0000ff;">public virtual void</span> Execute(Ai.<span style="color: #33cccc;">Entity </span>e)
    {
        <span style="color: #33cccc;">Console</span>.WriteLine(<span style="color: #993300;">"executing wander state"</span>);

        <span style="color: #33cccc;">Orc </span>o = e <span style="color: #0000ff;">as </span><span style="color: #33cccc;">Orc</span>;

        <span style="color: #0000ff;">if </span>(o != <span style="color: #33cccc;">null</span>)
            o.Drool();
    }

    <span style="color: #0000ff;">public void</span> Exit(Ai.<span style="color: #33cccc;">Entity </span>e)
    {
        <span style="color: #33cccc;">Console</span>.WriteLine(<span style="color: #993300;">"exiting wander state"</span>);
    }
}

<span style="color: #0000ff;">class </span><span style="color: #33cccc;">CustomizedOrcWanderState </span>: <span style="color: #33cccc;">OrcWanderState</span>
{
    <span style="color: #0000ff;">public override</span> <span style="color: #0000ff;">void </span>Execute(Ai.<span style="color: #33cccc;">Entity </span>e)
    {
        <span style="color: #0000ff;">base</span>.Execute(e);

        <span style="color: #33cccc;">Console</span>.WriteLine(<span style="color: #993300;">"unique orc personality here..."</span>);
    }
}
 </code></pre>
<p>Those are the basics of implementing a finite state machine for character control. You can download the source code <a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/FsmDemo1.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.conceptualinertia.net/aoakenfo/simple-fsm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>game of life</title>
		<link>http://www.conceptualinertia.net/aoakenfo/game-of-life</link>
		<comments>http://www.conceptualinertia.net/aoakenfo/game-of-life#comments</comments>
		<pubDate>Sat, 13 Jan 2007 10:47:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://conceptualinertia.wordpress.com/?p=214</guid>
		<description><![CDATA[Conway&#8217;s &#8216;Game of Life&#8217; produces some interesting patterns from a few simple rules. This demo utilizes WPF with simple styling and animation. One day I&#8217;ll have to learn how to record, edit, and post videos online. Until then, it&#8217;s nothing but a static screenshot for you: You can download the source here.]]></description>
			<content:encoded><![CDATA[<p>Conway&#8217;s &#8216;Game of Life&#8217; produces some interesting patterns from a few simple rules. This demo utilizes WPF with simple styling and animation.</p>
<p>One day I&#8217;ll have to learn how to record, edit, and post videos online. Until then, it&#8217;s nothing but a static screenshot for you:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/gameoflife1.jpg"><img class="alignnone size-full wp-image-216" title="gameoflife1" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/gameoflife1.jpg" alt="" width="638" height="476" /></a></p>
<p>You can download the source <a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/GameOfLifeDemo.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.conceptualinertia.net/aoakenfo/game-of-life/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>win32 nostalgia</title>
		<link>http://www.conceptualinertia.net/aoakenfo/win32-nostalgia</link>
		<comments>http://www.conceptualinertia.net/aoakenfo/win32-nostalgia#comments</comments>
		<pubDate>Sun, 02 Jul 2006 11:48:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://conceptualinertia.wordpress.com/?p=238</guid>
		<description><![CDATA[Remember the first time you learned Win32 programming? I thought I&#8217;d post a couple of old demos I made during my first encounter with Win32 many, many, years ago. The above image is from the demo called Elasticity. Drag the square and then let it go. The square will snap back to the origin as [...]]]></description>
			<content:encoded><![CDATA[<p>Remember the first time you learned Win32 programming? I thought I&#8217;d post a couple of old demos I made during my first encounter with Win32 many, <em>many</em>, years ago.</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/elasticity1.jpg"><img class="alignnone size-full wp-image-240" title="elasticity1" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/elasticity1.jpg" alt="" /></a></p>
<p>The above image is from the demo called Elasticity. Drag the square and then let it go. The square will snap back to the origin as if it were attached to an elastic band.</p>
<p>The next image is from the demo called Swarm. It tries to mimic a swarm of fruit flies chasing your cursor.</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/swarm.jpg"><img class="alignnone size-full wp-image-241" title="swarm" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/swarm.jpg" alt="" width="521" height="301" /></a></p>
<p>I cleaned up the source just enough so that it would run. You can download the source for Elasticity and Swarm <a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/Elasticity and Swarm.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.conceptualinertia.net/aoakenfo/win32-nostalgia/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>kondo tool</title>
		<link>http://www.conceptualinertia.net/aoakenfo/kondo-tool</link>
		<comments>http://www.conceptualinertia.net/aoakenfo/kondo-tool#comments</comments>
		<pubDate>Wed, 21 Jun 2006 19:17:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://conceptualinertia.wordpress.com/?p=209</guid>
		<description><![CDATA[Here&#8217;s a little tool I made for fun to control my Kondo KHR-1. It hasn&#8217;t been thoroughly tested, so don&#8217;t be surprised if any &#8220;features&#8221; pop up. You can download the source here.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/khr1test1.jpg"><img class="alignnone size-full wp-image-210" title="khr1test1" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/khr1test1.jpg" alt="" width="700" height="534" /></a></p>
<p>Here&#8217;s a little tool I made for fun to control my Kondo KHR-1. It hasn&#8217;t been thoroughly tested, so don&#8217;t be surprised if any &#8220;features&#8221; pop up. You can download the source <a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/Khr1Test2.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.conceptualinertia.net/aoakenfo/kondo-tool/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>novo</title>
		<link>http://www.conceptualinertia.net/aoakenfo/novo</link>
		<comments>http://www.conceptualinertia.net/aoakenfo/novo#comments</comments>
		<pubDate>Sat, 17 Jun 2006 13:44:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://conceptualinertia.wordpress.com/?p=192</guid>
		<description><![CDATA[Novo1.zip &#8211; &#8220;Lesson 101 &#8211; Box on a Plane&#8221; is the first sample in the PhysX SDK Training Programs. Here&#8217;s a screenshot of a DirectX port written in C++: The first thing to do is download the PhysX SDK. After it&#8217;s installed, you&#8217;ll need to set the directory paths in Visual Studio for the include [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/Novo1.zip">Novo1.zip</a> &#8211; &#8220;Lesson 101 &#8211; Box on a Plane&#8221; is the first sample in the PhysX SDK Training Programs. Here&#8217;s a screenshot of a DirectX port written in C++:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/novo1.jpg"><img class="alignnone size-full wp-image-193" title="novo1" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/novo1.jpg" alt="" width="638" height="477" /></a></p>
<p>The first thing to do is download the PhysX SDK. After it&#8217;s installed, you&#8217;ll need to set the directory paths in Visual Studio for the include files:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/novo2.jpg"><img class="alignnone size-full wp-image-194" title="novo2" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/novo2.jpg" alt="" width="642" height="380" /></a></p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/novo3.jpg"><img class="alignnone size-full wp-image-195" title="novo3" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/novo3.jpg" alt="" width="644" height="377" /></a></p>
<p>The demo has standard first-person camera controls. Also, be sure to check out WM_KEYDOWN in the WindowProc for a list of other controls.</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/Novo2.zip">Novo2.zip</a> &#8211; Lesson 102 &#8211; Sphere and Torque:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lesson102.jpg"><img class="alignnone size-full wp-image-196" title="lesson102" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lesson102.jpg" alt="" width="639" height="480" /></a></p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/Novo3.zip">Novo3.zip</a> &#8211; Lesson 103 &#8211; Capsule, Local Pose, and Center of Mass:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lesson103.jpg"><img class="alignnone size-full wp-image-197" title="lesson103" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lesson103.jpg" alt="" width="638" height="477" /></a></p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/Novo4.zip">Novo4.zip</a> &#8211; Lesson 104 &#8211; Convex Shapes and Anisotropic Friction:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lesson104.jpg"><img class="alignnone size-full wp-image-198" title="lesson104" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lesson104.jpg" alt="" width="639" height="476" /></a></p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/Novo5.zip">Novo5.zip</a> &#8211; Lesson 105 &#8211; The User Contact Report and the Contact Stream Iterator:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lesson105.jpg"><img class="alignnone size-full wp-image-199" title="lesson105" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lesson105.jpg" alt="" width="637" height="477" /></a></p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/Novo6.zip">Novo6.zip</a> &#8211; Lesson 106 &#8211; Static and Kinematic Actors. The screenshot doesn&#8221;t look like much, however, the user can control the middle cube (as a kinematic actor) bouncing it off the other actors.</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lesson106.jpg"><img class="alignnone size-full wp-image-200" title="lesson106" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lesson106.jpg" alt="" width="636" height="477" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.conceptualinertia.net/aoakenfo/novo/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>file type associations</title>
		<link>http://www.conceptualinertia.net/aoakenfo/file-type-associations</link>
		<comments>http://www.conceptualinertia.net/aoakenfo/file-type-associations#comments</comments>
		<pubDate>Thu, 15 Jun 2006 14:41:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://conceptualinertia.wordpress.com/?p=207</guid>
		<description><![CDATA[Applications typically have some type of project file format. For example, Maya saves scenes using a binary file: This file type is associated with Maya, it has the .mb extension and a custom icon. Double-clicking the file will open it in Maya. Today, we&#8217;re going to associate TestApp with the .testapp extension: To associate a [...]]]></description>
			<content:encoded><![CDATA[<p>Applications typically have some type of project file format. For example, Maya saves scenes using a binary file:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation1.jpg"><img class="alignnone size-full wp-image-223" title="fileassociation1" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation1.jpg" alt="" width="147" height="67" /></a></p>
<p>This file type is associated with Maya, it has the .mb extension and a custom icon. Double-clicking the file will open it in Maya. Today, we&#8217;re going to associate TestApp with the .testapp extension:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation2.jpg"><img class="alignnone size-full wp-image-224" title="fileassociation2" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation2.jpg" alt="" width="147" height="60" /></a></p>
<p>To associate a file type<strong></strong>, you need to do three things:</p>
<ol>
<li>Add the file extension and associated application to the registry.</li>
<li>When double-clicking a .testapp file, determine whether an instance of the associated process is already running.</li>
<li>If the application is already running, pass th<strong></strong>e file across process boundaries.</li>
</ol>
<p>The first step is easy and makes use of the following Win32 functions to create and set entries:</p>
<ul>
<li>RegOpenKeyEx</li>
<li>RegCreateKeyEx</li>
<li>RegSetValueEx</li>
</ul>
<p>The code to create key/value pairs in the registry is straightforward. Therefore, I&#8217;m not going to list it here so please reference the source code included in today&#8217;s download.</p>
<p>After executing the registration code, you should have new keys under HKEY_CLASSES_ROOT with the following structure:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation3.jpg"><img class="alignnone size-full wp-image-225" title="fileassociation3" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation3.jpg" alt="" width="584" height="125" /></a></p>
<p>And a new TestApp key where we assoicate the file with a default icon (which is indexed at 0 as an embedded resource in the executable):</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation42.jpg"><img class="alignnone size-large wp-image-228" title="fileassociation42" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation42.jpg?w=700" alt="" width="700" height="125" /></a></p>
<p>The executable to launch is set under the Command section, with %1 being the command line param:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation51.jpg"><img class="alignnone size-large wp-image-230" title="fileassociation51" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation51.jpg?w=700" alt="" width="700" height="121" /></a></p>
<p>Use RegEdit to verify the above entries after executing today&#8217;s demo. To execute RegEdit use the Run prompt:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation6.jpg"><img class="alignnone size-full wp-image-231" title="fileassociation6" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation6.jpg" alt="" width="345" height="185" /></a></p>
<p>Once the file associations have been added to the registry, the next thing to do is determine if an instance of TestApp is running. The Process class in the System::Diagnostics namespace makes this step trivial.</p>
<p>So how does one application communicate with another across process boundaries? Under Win32 you use named pipes. A pipe connects two ends together &#8211; a client and server. Under .NET, named pipes have been wrapped up in the System::Runtime::Remoting::Channels::Ipc namespace. An IpcChannel is used when one application needs to communicate with another application in a different process on the same machine.</p>
<p>However, before we look at inter-process communication (IPC) let&#8217;s have a look at class SimpleFile which represents our .testapp file format.</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">public ref class</span> SimpleFile : <span style="color: #0000ff;">public </span>MarshalByRefObject
{
    <span style="color: #0000ff;">public</span>: <span style="color: #0000ff;">delegate void</span> LoadDelegate(SimpleFile^ simpleFile);
    <span style="color: #0000ff;">private</span>: LoadDelegate^ loadDel;

    <span style="color: #0000ff;">public</span>: <span style="color: #0000ff;">event </span>LoadDelegate^ LoadEvent
    {
        <span style="color: #0000ff;">void </span>add(LoadDelegate^ value)
        {
            loadDel += value;
        }

        <span style="color: #0000ff;">void </span>remove(LoadDelegate^ value)
        {
           loadDel -= value;
        }
    }
 </code></pre>
<p>Remoting is used for communication between app domains. An app domain is a logical container for a .NET applciation and is often refered to as a &#8220;lightweight process&#8221;.</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/appdomainlayout1.jpg"><img class="alignnone size-full wp-image-233" title="appdomainlayout1" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/appdomainlayout1.jpg" alt="" width="700" height="227" /></a></p>
<p>The same remoting mechanism is used between two app domains whether those app domains are in the same process, in different processes on the same machine, or different processes on different machines. Any type that wants to expose its availability across app domains must inherit from MarshalByRefObject or be serializable.</p>
<p>To keep things as simple as possible, the SimpleFile &#8220;format&#8221; only contains a single value, which is parsed and stored:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">private</span>: <span style="color: #0000ff;">int </span>ultimateAnswer;
<span style="color: #0000ff;">public</span>: <span style="color: #0000ff;">property int</span> TheAnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything
{
    <span style="color: #0000ff;">int </span>get() { <span style="color: #0000ff;">return this</span>-&gt;ultimateAnswer; }
}

<span style="color: #0000ff;">public</span>: <span style="color: #0000ff;">void </span>Load(String^ filePath)
{
    XmlTextReader^ xtr = <span style="color: #0000ff;">gcnew </span>XmlTextReader(filePath);

    <span style="color: #0000ff;">while</span>( xtr-&gt;Read() )
    {
        <span style="color: #0000ff;">if</span>( xtr-&gt;NodeType == XmlNodeType::Element )
        {
            <span style="color: #0000ff;">if</span>( xtr-&gt;Name == <span style="color: #993300;">"UltimateAnswer"</span> )
            {
                xtr-&gt;Read();
                <span style="color: #0000ff;">this</span>-&gt;ultimateAnswer = <span style="color: #0000ff;">int</span>::Parse(xtr-&gt;Value);
            }
       }
    }

    xtr-&gt;Close();
    <span style="color: #0000ff;">delete </span>xtr;

    <span style="color: #0000ff;">if </span>(loadDel != <span style="color: #0000ff;">nullptr</span>)
        loadDel(<span style="color: #0000ff;">this</span>);
}
 </code></pre>
<p>Here we see the value stored in the sample file foobar.testapp:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation7.jpg"><img class="alignnone size-full wp-image-234" title="fileassociation7" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/fileassociation7.jpg" alt="" width="391" height="185" /></a></p>
<p>The constructor in the Form class is overloaded to accept an array of arguments:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">public</span>: Form1(<span style="color: #0000ff;">array</span>&lt;String^&gt;^ args)
{
    InitializeComponent();

    <span style="color: #008000;">// - in this example, we register the file type associations every time
    //   the application runs (overwriting keys)
    // - however, you would normally do this only once as part of your
    //   installation process</span>
    RegisterFileAssociations();

    <span style="color: #008000;">// - cache any arguments passed from Main</span>
    <span style="color: #0000ff;">this</span>-&gt;args = args;
}
 </code></pre>
<p>If there were no arguments passed, the application is starting up for the first time.</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">private</span>: System::Void OnFormLoad(System::Object^ sender,
                                 System::EventArgs^ e)
{
    <span style="color: #008000;">// - application is loading for the first time</span>
    <span style="color: #0000ff;">if</span>( args-&gt;Length == 0 )
    {
        RegisterService();
    }
 </code></pre>
<p>The host or server has to tell .NET which object&#8217;s it&#8217;s willing to expose. These objects or well-known types are registered using the static method RegisterWellKnownServiceType of the RemotingConfiguration class.</p>
<p>The host also has to register a channel with ChannelServices. A channel will monitor a port and accept incoming calls, servicing those calls with a thread from the thread pool. The IpcChannel is a welcome addition because prior versions of .NET provided only the TcpChannel and HttpChannel for communication. Both TcpChannel and HttpChannel have associated overhead for the network stack regardless if the communication taking place is on the same machine or across a network. IPC provides a direct line, reducing overhead and increasing performance.</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">private</span>: <span style="color: #0000ff;">void </span>RegisterService()
{
    BinaryServerFormatterSinkProvider^ serverProv = <span style="color: #0000ff;">
        gcnew </span>BinaryServerFormatterSinkProvider();
    serverProv-&gt;TypeFilterLevel = TypeFilterLevel::Full;

    Hashtable^ properties = <span style="color: #0000ff;">gcnew </span>Hashtable();
    properties[<span style="color: #993300;">"portName"</span>] = <span style="color: #993300;">"SimpleFileServer"</span>;

    IpcChannel^ ipc = <span style="color: #0000ff;">gcnew </span>IpcChannel(properties, <span style="color: #0000ff;">nullptr</span>, serverProv);
    ChannelServices::RegisterChannel(ipc, <span style="color: #0000ff;">false</span>);

    RemotingConfiguration::RegisterWellKnownServiceType(
                                      SimpleFile::<span style="color: #0000ff;">typeid</span>,
                                      <span style="color: #993300;">"SimpleFile.rem"</span>,
                                      WellKnownObjectMode::Singleton);

    file = (SimpleFile^)Activator::GetObject(
                                      SimpleFile::<span style="color: #0000ff;">typeid</span>,
                                      <span style="color: #993300;">"ipc://SimpleFileServer/SimpleFile.rem"</span>);

    file-&gt;LoadEvent += <span style="color: #0000ff;">gcnew </span>SimpleFile::LoadDelegate(<span style="color: #0000ff;">this</span>,
                                                      &amp;Form1::OnProjectFileOpen);
}
 </code></pre>
<p>During server registration, a SimpleFile is activated and its LoadEvent is hooked up to the following:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">private</span>: <span style="color: #0000ff;">delegate void</span> InvokeDelegate(SimpleFile^ simpleFile);
<span style="color: #0000ff;">private</span>: <span style="color: #0000ff;">void </span>OnProjectFileOpen(SimpleFile^ simpleFile)
{
    <span style="color: #008000;">// cross-thread op</span>
    <span style="color: #0000ff;">array</span>&lt;Object^&gt;^ arr = { simpleFile };
    <span style="color: #0000ff;">this</span>-&gt;Invoke(<span style="color: #0000ff;">gcnew </span>InvokeDelegate(<span style="color: #0000ff;">this</span>, &amp;Form1::ProcessFile), arr);
}

<span style="color: #0000ff;">public</span>: <span style="color: #0000ff;">void </span>ProcessFile(SimpleFile^ simpleFile)
{
    <span style="color: #0000ff;">this</span>-&gt;label2-&gt;Text =
        simpleFile-&gt;TheAnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything.ToString();
}
 </code></pre>
<p>Invoke is used to update the Form&#8217;s user interface. Execution has to take place on the caller&#8217;s thread to avoid a cross-thread exception.</p>
<p>Back to OnFormLoad, where a list of processes is then assembled. It&#8217;s possible the process was launched with a double-click on a .testapp file. If so, services need to be registered and a SimpleFile loaded:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">array</span>&lt;Process^&gt;^ processArray =
    Process::GetProcessesByName(Process::GetCurrentProcess()-&gt;ProcessName);

<span style="color: #008000;">// application loading for the first time from a double-click on file</span>
<span style="color: #0000ff;">if</span>( processArray-&gt;Length == 1 )
{
    RegisterService();

    SimpleFile sf;
    sf.LoadEvent += <span style="color: #0000ff;">gcnew </span>SimpleFile::LoadDelegate(<span style="color: #0000ff;">this</span>,
                                                   &amp;Form1::OnProjectFileOpen);
    sf.Load(filePath.ToString());
}
 </code></pre>
<p>Otherwise, a process is already running and SimpleFile needs to get its information across process boundaries. Here we see the client registering an IpcChannel and activating the well-known file (SimpleFile):</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">else if</span>( processArray-&gt;Length &gt; 1 )
{
    MessageBox::Show(<span style="color: #993300;">"Instance already running, passing args along"</span>);

    <span style="color: #0000ff;">for each</span>( Process^ p <span style="color: #0000ff;">in </span>processArray )
    {
        <span style="color: #0000ff;">if</span>( p-&gt;Id != Process::GetCurrentProcess()-&gt;Id )
        {
            <span style="color: #0000ff;">try</span>
            {
                IpcChannel^ ipc = <span style="color: #0000ff;">gcnew </span>IpcChannel(<span style="color: #993300;">"SimpleClient"</span>);
                ChannelServices::RegisterChannel(ipc, <span style="color: #993300;">false</span>);

                SimpleFile^ file =
                    (SimpleFile^)Activator::GetObject(
                                        SimpleFile::<span style="color: #0000ff;">typeid</span>,
                                        <span style="color: #993300;">"ipc://SimpleFileServer/SimpleFile.rem"</span>);

                if (file == <span style="color: #0000ff;">nullptr</span>)
                {
                    MessageBox::Show(<span style="color: #993300;">"Failure to connect with remote server"</span>);
                    this-&gt;Close();
                }

                file-&gt;Load(args[0]);
                <span style="color: #0000ff;">this</span>-&gt;Close();
            }
            <span style="color: #0000ff;">catch</span>(Exception^ e)
            {
                MessageBox::Show(e-&gt;Message);
            }

        } <span style="color: #008000;">// end if</span>

   } <span style="color: #008000;">// end for each</span>

} <span style="color: #008000;">// end else if</span>
 </code></pre>
<p>In summary, the application is now assoicated with a file type<strong></strong>. Double-clicking the file will launch the app. If the app is already running, IPC will be used to pass the file<strong></strong> info across process boundaries, allowing the application already running to open the file <strong></strong> as a new &#8220;project&#8221;.</p>
<p>Build and run the demo once to associate the file type in the registry. If you don&#8217;t see a blue icon for &#8220;foobar.testapp&#8221;, hit F5 in the explorer window to refresh it. Shut down the application and then try double-clicking various files with the .testapp extension.</p>
<p>You can download the source <a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/FileAssociations.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.conceptualinertia.net/aoakenfo/file-type-associations/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>flash splash</title>
		<link>http://www.conceptualinertia.net/aoakenfo/flash-splash</link>
		<comments>http://www.conceptualinertia.net/aoakenfo/flash-splash#comments</comments>
		<pubDate>Tue, 06 Jun 2006 13:15:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://conceptualinertia.wordpress.com/?p=202</guid>
		<description><![CDATA[Ideally, your application should not need a splash screen because its load time should be nearly instantaneous. However, it&#8217;s not always the case. Updating the user about the progress of initialization is desirable. The ability to entertain the user at the same time is even more desirable. Most splash screens are static bitmaps. Some applications [...]]]></description>
			<content:encoded><![CDATA[<p>Ideally, your application should not need a splash screen because its load time should be nearly instantaneous. However, it&#8217;s not always the case. Updating the user about the progress of initialization is desirable. The ability to entertain the user at the same time is even more desirable. Most splash screens are static bitmaps. Some applications echo textual updates about which modules have been loaded or what systems have been initialized. This text typically flies by very quickly on-screen and may be echoed to a log file. What happens if we use Flash as the basis for a splash screen?</p>
<p>Today&#8217;s Solution consists of a C++/CLI class library containing a Flash splash screen class. A C# Windows application is used to test splash screen functionality. Please note that simply copying an existing Form  (ie. the FlashForm in today&#8217;s download) between projects is not recommended because you&#8217;ll run into a lot of errors. Ultimately, it&#8221;s faster if you create the Form in the target project and then copy &#8216;n paste portions of the code over (rebuilding along the way).</p>
<p>Instructions on how to setup a ShockwaveFlash control can be found <a href="http://www.conceptualinertia.net/aoakenfo/?p=18">here</a>.<br />
The code for using Flash as a splash screen (including the .fla) is available for download <a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/FlashSplashDemo.zip">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.conceptualinertia.net/aoakenfo/flash-splash/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>hosting flash</title>
		<link>http://www.conceptualinertia.net/aoakenfo/hosting-flash</link>
		<comments>http://www.conceptualinertia.net/aoakenfo/hosting-flash#comments</comments>
		<pubDate>Sat, 03 Jun 2006 04:46:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://conceptualinertia.wordpress.com/?p=140</guid>
		<description><![CDATA[I was working on a project a while back that required a fairly rich UI. I was brought a &#8221;sketch&#8221; of the UI in Flash. Without even thinking, I started up Visual Studio and began laying out a few controls to port the sketch. Hmm&#8230;should I skin the controls or derive new ones? Should I [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on a project a while back that required a fairly rich UI. I was brought a &#8221;sketch&#8221; of the UI in Flash. Without even thinking, I started up Visual Studio and began laying out a few controls to port the sketch. Hmm&#8230;should I skin the controls or derive new ones? Should I draw everything manually with the Graphics object? Should I load some vector art from a metafile? How was I going to perform those animations and transitions? Ugh&#8230;it was going to take a long time to implement the UI before I could even get around to real processing functionality. Then it hit me, why not use Flash for the UI? The client had most of the work already done, he simply needed a Windows application to do some behind the scenes processing that would be hard given the constraints of Flash sandboxing.</p>
<p>Today&#8217;s post covers how to host Flash in a C# application with two-way communcation between environments. Here&#8217;s a screenshot of the demo:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflashscreenshot.jpg"><img class="alignnone size-full wp-image-144" title="csflashscreenshot" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflashscreenshot.jpg" alt="" width="427" height="334" /></a></p>
<p>Clicking the green button in Flash pops up a message box in C#. Entering text in C# and clicking the Send button echos it in Flash.<br />
The first step is to add the Shockwave Flash ActiveX control to the Toolbox (it doesn&#8217;t matter which group you add the control to):</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash1.jpg"><img class="alignnone size-full wp-image-145" title="csflash1" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash1.jpg" alt="" width="284" height="447" /></a></p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash2.jpg"><img class="alignnone size-full wp-image-146" title="csflash2" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash2.jpg" alt="" width="568" height="418" /></a></p>
<p>After selecting the component from the COM tab, you should see a new icon in your Toolbox:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash3.jpg"><img class="alignnone size-full wp-image-147" title="csflash3" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash3.jpg" alt="" width="186" height="78" /></a></p>
<p>Now, drag &#8216;n drop this control onto Form1 and you should get the following:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash4.jpg"><img class="alignnone size-full wp-image-148" title="csflash4" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash4.jpg" alt="" width="460" height="125" /></a></p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash5.jpg"><img class="alignnone size-full wp-image-149" title="csflash5" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash5.jpg" alt="" width="691" height="170" /></a></p>
<p>Something broke with the release of VS2005. You should have no problem doing the above in VS2003. However, I don&#8217;t have VS2003 so what&#8217;s the solution? As it turns out, the answer is in the warning messages. There was a failure to create a wrapper assembly due to a COM exception. Well, I know of another .NET language that should have no problem interacting with COM&#8230;</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash6.jpg"><img class="alignnone size-full wp-image-150" title="csflash6" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash6.jpg" alt="" width="678" height="441" /></a></p>
<p>Now create a new FlashPanel user control:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash7.jpg"><img class="alignnone size-full wp-image-151" title="csflash7" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash7.jpg" alt="" width="677" height="441" /></a></p>
<p>The FlashPanel is a derivative of UserControl. In the designer, repeat the above steps &#8211; drag &#8216;n drop the Flash object onto the panel and set its Dock property to &#8221;Fill&#8221;. The C++/CLI control library will successfully create a wrapper assembly for the ActiveX control. Add the following property so C# can access the Flash object:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">public</span>: <span style="color: #0000ff;">property </span>AxShockwaveFlashObjects::AxShockwaveFlash^ Flash
{
    AxShockwaveFlashObjects::AxShockwaveFlash^ get()
    {
        <span style="color: #0000ff;">return this</span>-&gt;axShockwaveFlash1;
    }
}
 </code></pre>
<p>Once you build the control library, you should see your new UserControl in the Toolbox:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash8.jpg"><img class="alignnone size-full wp-image-152" title="csflash8" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash8.jpg" alt="" width="269" height="103" /></a></p>
<p>Now create a C# Windows application. Under the References folder, add the following:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash9.jpg"><img class="alignnone size-full wp-image-153" title="csflash9" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/csflash9.jpg" alt="" width="334" height="342" /></a></p>
<p>Next, we begin using our FlashPanel. The ActiveX Flash control has a LoadMovie function which takes an absolute directory path to the .swf file. It also has an FsCommand delegate:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">public </span>Form1()
{
    InitializeComponent();
    <span style="color: #0000ff;">string </span>moviePath = <span style="color: #33cccc;">Application</span>.StartupPath + <span style="color: #993300;">"test.swf"</span>;
    <span style="color: #0000ff;">this</span>.flashPanel1.Flash.LoadMovie(0, moviePath);
    <span style="color: #0000ff;">this</span>.flashPanel1.Flash.FSCommand += <span style="color: #0000ff;">new</span>
        AxShockwaveFlashObjects.<span style="color: #33cccc;">_IShockwaveFlashEvents_FSCommandEventHandler</span>(
                                                            OnFsCommand);
}
 </code></pre>
<p>FsCommand is used by Flash to communicate with the host environment. In the test.swf file, the green arrow button has the following ActionScript attached to it:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">on</span>(<span style="color: #0000ff;">release</span>)
{
    <span style="color: #0000ff;">fscommand</span>(<span style="color: #339966;">"ButtonClick"</span>, <span style="color: #339966;">"You clicked a button in Flash"</span>);
}
 </code></pre>
<p>OnFsCommand will respond to messages from Flash. In this example, it only checks for the &#8220;ButtonClick&#8221; message:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">void </span>OnFsCommand(<span style="color: #0000ff;">object </span>sender,
                 AxShockwaveFlashObjects.<span style="color: #33cccc;">_IShockwaveFlashEvents_FSCommandEvent</span> e)
{
    <span style="color: #0000ff;">if</span>( e.command == <span style="color: #993300;">"ButtonClick"</span>)
        <span style="color: #33cccc;">MessageBox</span>.Show(e.args);
}
 </code></pre>
<p>When sending a message to Flash, we use SetVariable passing both the message name and value:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">private void</span> OnSendButtonClick(<span style="color: #0000ff;">object </span>sender, <span style="color: #33cccc;">EventArgs </span>e)
{
    <span style="color: #0000ff;">this</span>.flashPanel1.Flash.SetVariable(<span style="color: #993300;">"DotNetMessage"</span>, <span style="color: #0000ff;">this</span>.textBox1.Text);
}
 </code></pre>
<p>If you wanted to send more information in the value param you could use a delimiter. For example, &#8220;foo?bar&#8221; would need to be parsed and split based on &#8221;?&#8221; characters to extract multiple values.<br />
The last bit of ActionScript code is used to setup event listening. The root watches for a &#8220;DotNetMessage&#8221; and then passes it along to the listener object. The code in this example, simple echos any value to the textbox control inside Flash.</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">var </span>listenerObj:<span style="color: #0000ff;">Object </span>= <span style="color: #0000ff;">new Object</span>();
listenerObj.onMessage = <span style="color: #0000ff;">function </span>(prop, oldVal, newVal)
{
    <span style="color: #0000ff;">_root</span>.textBox = newVal;
}
<span style="color: #0000ff;">_root</span>.watch(<span style="color: #339966;">"DotNetMessage"</span>, listenerObj.onMessage);
 </code></pre>
<p>The .fla file is available in the Debug directory. You can download the source code <a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/HostingFlashDemo.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.conceptualinertia.net/aoakenfo/hosting-flash/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>custom serialization</title>
		<link>http://www.conceptualinertia.net/aoakenfo/custom-serialization</link>
		<comments>http://www.conceptualinertia.net/aoakenfo/custom-serialization#comments</comments>
		<pubDate>Fri, 02 Jun 2006 07:52:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://conceptualinertia.wordpress.com/?p=103</guid>
		<description><![CDATA[For a derivative class to be serializable, the base class must be marked as serializable as well. For example, if I do the following: [Serializable] public partial class Form1 : Form And attempt to serialize the object, I will get an exception. Adding a serializable attribute to the base Form is not possible and many [...]]]></description>
			<content:encoded><![CDATA[<p>For a derivative class to be serializable, the base class must be marked as serializable as well. For example, if I do the following:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
[<span style="color: #33cccc;">Serializable</span>]
<span style="color: #0000ff;">public partial class</span> <span style="color: #33cccc;">Form1 </span>: <span style="color: #33cccc;">Form</span>
 </code></pre>
<p>And attempt to serialize the object, I will get an exception.</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/customserial11.jpg"><img class="alignnone size-large wp-image-134" title="customserial11" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/customserial11.jpg?w=700" alt="" width="700" height="299" /></a></p>
<p>Adding a serializable attribute to the base Form is not possible and many people at this point move on looking for another solution. However, it is possible to make Form1 serializable through custom serialization. To do this, first add ISerializable to the inheritance chain:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
[<span style="color: #33cccc;">Serializable</span>]
<span style="color: #0000ff;">public partial class</span> <span style="color: #33cccc;">Form1 </span>: <span style="color: #33cccc;">Form</span>, <span style="color: #33cccc;">ISerializable</span>
 </code></pre>
<p>And then implement it:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/customserial2.jpg"><img class="alignnone size-full wp-image-136" title="customserial2" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/customserial2.jpg" alt="" width="476" height="381" /></a></p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">void </span><span style="color: #33cccc;">ISerializable</span>.GetObjectData(<span style="color: #33cccc;">SerializationInfo </span>info, <span style="color: #33cccc;">StreamingContext </span>context)
{
    info.AddValue(<span style="color: #993300;">"Location"</span>, <span style="color: #0000ff;">this</span>.Location);
    info.AddValue(<span style="color: #993300;">"ClientSize"</span>, <span style="color: #0000ff;">this</span>.ClientSize);
}
 </code></pre>
<p>An exception is no longer produced when running the code now. After execution, you should find a &#8221;Form1.bin&#8221; file in the Debug directory. Despite this, a binary file is not very user-friendly. Let&#8217;s modify the code so we can actually open the file and read it. Rather than using a BinaryFormatter, why not a SoapFormatter? To use soap formatting you&#8217;ll need to add a reference to the assembly:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/customserial3.jpg"><img class="alignnone size-full wp-image-137" title="customserial3" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/customserial3.jpg" alt="" width="464" height="377" /></a></p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">using </span>System.Runtime.Serialization.Formatters.Soap;
<span style="color: #339966;">//...</span>

<span style="color: #33cccc;">FileStream </span>fileStream = <span style="color: #33cccc;">File</span>.Create(<span style="color: #993300;">"Form1.xml"</span>);
<span style="color: #33cccc;">IFormatter </span>iFormatter = <span style="color: #0000ff;">new </span><span style="color: #33cccc;">SoapFormatter</span>();
iFormatter.Serialize(fileStream, form);
 </code></pre>
<p>In the above code, I&#8217;ve changed the file name from Form1.bin to Form1.xml. However, the file extension doesn&#8217;t impact the content. Here&#8217;s a sample of the output:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/customserial4.jpg"><img class="alignnone size-full wp-image-138" title="customserial4" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/customserial4.jpg" alt="" width="326" height="192" /></a></p>
<p>So, serialization works but what about deserialization? To accomplish that, you need to implement a deserialization constructor:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">private </span>Form1(<span style="color: #33cccc;">SerializationInfo </span>info, <span style="color: #33cccc;">StreamingContext </span>context)
{
    DefaultInitialization();
    <span style="color: #0000ff;">this</span>.Location = (<span style="color: #33cccc;">Point</span>)info.GetValue(<span style="color: #993300;">"Location"</span>, <span style="color: #0000ff;">this</span>.Location.GetType());
    <span style="color: #0000ff;">this</span>.ClientSize = (<span style="color: #33cccc;">Size</span>)info.GetValue(<span style="color: #993300;">"ClientSize"</span>, <span style="color: #0000ff;">this</span>.ClientSize.GetType());
}
 </code></pre>
<p>The DefaultInitialization function calls InitializeComponent and then assigns some default values. Calling  InitializeComponent is important if you want the Form and any of its controls to be initialized appropriately.</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">private void</span> DefaultInitialization()
{
    InitializeComponent();
    <span style="color: #0000ff;">this</span>.StartPosition = <span style="color: #33cccc;">FormStartPosition</span>.Manual;
    <span style="color: #0000ff;">this</span>.Location = <span style="color: #0000ff;">new </span><span style="color: #33cccc;">Point</span>(0, 0);
    <span style="color: #0000ff;">this</span>.ClientSize = <span style="color: #0000ff;">new </span><span style="color: #33cccc;">Size</span>(640, 480);
}
 </code></pre>
<p>The DefaultInitialization function is also called from the default constructor:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">public </span>Form1()
{
    DefaultInitialization();
}
 </code></pre>
<p>Deserialization can&#8217;t occur after Application.Run returns because the Form has already been destroyed. Here, we plug-in to the FormClosing event to handle deserialization:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
<span style="color: #0000ff;">private void</span> OnFormClosing(<span style="color: #0000ff;">object </span>sender, <span style="color: #33cccc;">FormClosingEventArgs </span>e)
{
    <span style="color: #33cccc;">FileStream </span>fileStream = <span style="color: #0000ff;">new </span>FileStream(<span style="color: #993300;">"Form1.xml"</span>,
                                           <span style="color: #33cccc;">FileMode</span>.Create,
                                           <span style="color: #33cccc;">FileAccess</span>.Write);

    <span style="color: #33cccc;">IFormatter </span>iFormatter = <span style="color: #0000ff;">new </span><span style="color: #33cccc;">SoapFormatter</span>();
    iFormatter.Serialize(fileStream, <span style="color: #0000ff;">this</span>);

    fileStream.Close();
}
 </code></pre>
<p>Finally, we can bring it all together in Main:</p>
<pre style="padding-left:30px;background-color:#f5f5f5;"><code>
[<span style="color: #33cccc;">STAThread</span>]
<span style="color: #0000ff;">static void</span> Main()
{
    <span style="color: #33cccc;">Form1 </span>form = <span style="color: #0000ff;">null</span>;

    if (<span style="color: #33cccc;">File</span>.Exists(<span style="color: #993300;">"Form1.xml"</span>))
    {
        <span style="color: #33cccc;">FileStream </span>fileStream = <span style="color: #33cccc;">File</span>.Open(<span style="color: #993300;">"Form1.xml"</span>, <span style="color: #33cccc;">FileMode</span>.Open);
        <span style="color: #33cccc;">IFormatter </span>iFormatter = <span style="color: #0000ff;">new </span><span style="color: #33cccc;">SoapFormatter</span>();
        form = iFormatter.Deserialize(fileStream) <span style="color: #0000ff;">as </span><span style="color: #33cccc;">Form1</span>;

        fileStream.Close();
    }

    <span style="color: #0000ff;">if </span>(form == <span style="color: #0000ff;">null</span>)
        form = <span style="color: #0000ff;">new </span><span style="color: #33cccc;">Form1</span>();

    <span style="color: #33cccc;">Application</span>.Run(form);
}
 </code></pre>
<p>With this simple example, you can position and resize the Form; restart the application and it will remember your previous settings. Now I&#8217;m not saying this is a way to save Form settings. There are numerous methods including:</p>
<ul>
<li> app.config</li>
<li> .resources</li>
<li> .ini</li>
<li> manual parsing of various file formats</li>
</ul>
<p>The real demonstration here is how to make an object serializable when the base class isn&#8217;t.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.conceptualinertia.net/aoakenfo/custom-serialization/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>misc directx</title>
		<link>http://www.conceptualinertia.net/aoakenfo/misc-directx</link>
		<comments>http://www.conceptualinertia.net/aoakenfo/misc-directx#comments</comments>
		<pubDate>Tue, 23 May 2006 05:14:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://conceptualinertia.wordpress.com/?p=173</guid>
		<description><![CDATA[ProgressiveMeshDemo.zip &#8211; A simple demo of the D3D progressive mesh interface using C++/CLI. PickingDemo.zip &#8211; A simple ray picking demo using DirectX and C++/CLI .Drag and click the arrow to slide it back and forth. OrthoPickingDemo.zip &#8211; A simple demo of picking in an orthographic viewport using DirectX and C++/CLI: LerpDemo.zip &#8211; C++ DirectX demo [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/ProgressiveMeshDemo.zip">ProgressiveMeshDemo.zip</a> &#8211; A simple demo of the D3D progressive mesh interface using C++/CLI.</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/progressivemeshdemo21.jpg"><img class="alignnone size-medium wp-image-182" title="progressivemeshdemo21" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/progressivemeshdemo21.jpg?w=300" alt="" width="300" height="262" /></a></p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/progressivemeshdemo3.jpg"><img class="alignnone size-medium wp-image-183" title="progressivemeshdemo3" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/progressivemeshdemo3.jpg?w=300" alt="" width="300" height="262" /></a></p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/progressivemeshdemo4.jpg"><img class="alignnone size-medium wp-image-184" title="progressivemeshdemo4" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/progressivemeshdemo4.jpg?w=300" alt="" width="300" height="262" /></a></p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/PickingDemo.zip">PickingDemo.zip</a> &#8211; A simple ray picking demo using DirectX and C++/CLI .Drag and click the arrow to slide it back and forth.</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/pickingdemoscreenshot.jpg"><img class="alignnone size-full wp-image-185" title="pickingdemoscreenshot" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/pickingdemoscreenshot.jpg" alt="" width="442" height="353" /></a></p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/OrthoPickingDemo.zip">OrthoPickingDemo.zip</a> &#8211; A simple demo of picking in an orthographic viewport using DirectX and C++/CLI:</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/orthopickingscreenshot.jpg"><img class="alignnone size-full wp-image-186" title="orthopickingscreenshot" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/orthopickingscreenshot.jpg" alt="" width="324" height="255" /></a></p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/source/LerpDemo.zip">LerpDemo.zip</a> &#8211; C++ DirectX demo of linear interpolation between keyframes using an assembly shader.</p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lerpdemo1.jpg"><img class="alignnone size-full wp-image-187" title="lerpdemo1" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lerpdemo1.jpg" alt="" width="319" height="238" /></a></p>
<p><a href="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lerpdemo2.jpg"><img class="alignnone size-full wp-image-188" title="lerpdemo2" src="http://www.conceptualinertia.net/aoakenfo/wp-content/uploads/2008/10/lerpdemo2.jpg" alt="" width="319" height="239" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.conceptualinertia.net/aoakenfo/misc-directx/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
