Transitioning to WPF, part 3

First off, I want to clear up any confusion that may be lingering. The thoughts and opinions expressed on this blog are my own and not those of my employer or anyone else.

Lets get to it

When it comes to UX programming I always try to seperate data and display. Nearly every tutorial out there smashes them together, in a production environment that’s a practice for the fool-hearty.

At this point I’m just ignoring all display programming until I get more aligned with the WPF/.NET paradigm. So, I’ve started diving into the data side of things, setting up classes, interfaces and whatnot. Low and behold, I’ve started running into some hurdles:

How do I define optional parameters?

The short answer: you can’t.

The long answer: polymorphism.

That’s right. Good-old family-friendly polymorphism. This is something I haven’t even thought of for years. Primarily because Actionscript doesn’t allow it, even though it does allow optional parameters.

Sure, optional parameters are kind of a hack, but they make code 100x more managable. Let’s look at an example:

Actionscript 3

/*
 * In AS3 you can define function paramaters as optional.
 * In this example, the parameters "x" and "y" are required
 * to call the function. However, "z" and "mass" are optional and if
 * left out of a function call their values will default, in this
 * case, to "1".
 *
 * Correct usages:
 * myFunction(1.5, 2.78, 3.02);
 * myFunction (0.1, 10.4);
 * myFunction(5.8, 2.08, 3.89, 12.2);
 *
 * Incorrect usages:
 * myFunction();
 * myFunction(1.1);
 */
 
public function myFuction(x:Number, y:Number, z:Number=1, mass:Number=1):void {  }

C#

/*
 * In C# optional parameters don't exist. So you
 * have to use polymorphism to achieve similar
 * functionality as optional parameters in AS3.
 */
 
public void myFunction(double x, double y) { }
public void myFunction(double x, double y, double z) { }
public void myFunction(double x, double y, double z, double mass) { }

I’ve grown very fond of optional parameters because in C# you have to manage multiple seperate definitions for the same function. What a pain in the ass.

In situations like this polymorphism really seems tired and dated… a blast from the past. There are new ways to program! What’s the hold up?

That’s it for now

I have more rants queued up but this post is already too long.

Posted in Transitioning to WPF | Tagged , , , | 4 Comments

Transitioning to WPF, part 2

I had an epiphany yesterday while tirelessly searching for WPF tutorials of all things. I realized that .NET developers publish tutorials in only two ways:

1) Cryptic Code Snippets

These are unbelievably long tutorials with huge, unbroken paragraphs explaining far more than what’s necessary. If you’re writing a tutorial or how-to, people want you to get to the point. Show us how to do it first, then you can go into deeper explanation (if you really think it’s necessary).

Then they shove tiny code snippets between these typographic monstrosities and expect you to know where they go. This is only good if you have shown all the code put together first and are now breaking it down and going over each peice.

2) Video Tutorials

If there is one thing I don’t understand about the Microsoft subsculture, then it’s their insistence to use videos FOR EVERYTHING! Whether it’s a demo, presentation, how-to, tutorial or anything else, it doesn’t matter. They post a video and expect people to sit through it. Now it makes sense that all the .NET guys in our office have a video playing on their second monitor while they do something productive.

I’ve never met a developer who wants to watch a video of someone coding. So why do they keep doing it? Videos should only be made for one-off situations like demos or presentations, not a step-by-step on building your own red-black tree.

On top of terrible video content they don’t take to time to make it look halfway presentable. Who want’s to see a scruffy-faced, balding programmer in a depressing office with bad lighting and terrible sound? If you’re going to make a video, then why not take the time to make it look halfway presentable?

Doesn’t it make sense that people might actually want to watch your videos if their eyes don’t bleed? It doesn’t take much to take video production from high school drop-out to halfway decent.

I could go on for hours.

That’s it for now

It’s day two. The first day was rough, but I still made a little headway… after making it through all those videos.

Posted in Transitioning to WPF | Tagged , , | Leave a comment

Transitioning to WPF, part 1

So, I’ve been asked to make the leap from Actionscript to C# and WPF. I’ve done some pretty intense C# in the past, but that was years ago.  I’m not concerned at all with developing in a real language again, after all I was on the deans list during my junior and senior years of comp sci.  The real problem is WPF.

I see it as Microsoft’s stab at Flex.  It’s younger and supported by code-jockeys where Flex and Actionscript is much more mature, has larger restrictions it has to work within (so I consider it more efficient) and is backed by designers and those less visually challenged.

I have 30 days to translate my particle phyics and rendering engine from Actionscript to C# and WPF. This is day 1 and I can’t even find a halfway decent “Hello World” tutorial.

Posted in Transitioning to WPF | Tagged , , , , , | 4 Comments

Resizing BitmapData without scaling

Have you ever wanted to resize a BitmapData object without scaling it? Normally you would perform a matrix transformation on it by calling the draw() method, but that also scales it.  So how do you acheive non-scaling resize?  Hopefully this can help explain it:

// create something that has BitmapData for me to resize
this.bmp = new Bitmap(new BitmapData(400, 400));
this.addChild(this.bmp);
 
// create a new BitmapData instance at the size i want bmp to be
var temp:BitmapData = new BitmapData(200, 200);
 
// copy the pixels from bmp to the new BitmapData instance
temp.copyPixels(this.bmp.bitmapData, temp.rect, this.bmp.bitmapData.rect.topLeft);
 
// clear out bmp's old BitmapData
this.bmp.bitmapData.dispose();
 
// assign the resized BitmapData to bmp's (now empty) BitmapData
this.bmp.bitmapData = temp;

This may not be the most efficient method, but it’s simple and gets the job done.

Posted in Actionscript | Tagged , , , , | 16 Comments

ND3D – A shout-out

I published a quick post on the Phenomblue blog yesterday about ND3D, the stripped down AS3 3D engine.

Go check it out!

Posted in General | Tagged , , | Leave a comment

PV3DDebug now with Google Code action!

With the great reviews from [draw.logic], the dudes at the Papervison3D dev blog and just the response from the community I figured it was time to set this thing in stone, metaphorically speaking.  I went ahead and revised it (adding a camera is a little different now), included all the libraries (SWCs) you need and put it all on google code.

Go Check It Out!

And yes, we do have a Microsoft Surface machine at Phenomblue. I’m not gonna lie, it’s pretty sextacular. :D

Posted in Papervision | Tagged , , , , , | 8 Comments

Arrays vs. Dictionaries

WARNING! This post is totally nerdcore.  If you have a heart condition, increased risk of an anurism or have difficulties counting your fingers then this is not for you!

Overview

I’ve been scouring the Papervison 2 code over the past couple days and I noticed that they use a lot of Dictionaries in the very heart of the library.  Every single vertex has one, there are a bunch created on the fly for organizing (i.e. merging verticies).  I had always thought that Dictionaries were more resource intensive than arrays.  So I decided to do some benchmarking.

Although I only did one test per senario (I really should do multiple tests for a more complete statistical model), I’m sure it’s not too far from a true statistical analysis.

Set Up

For the primiative tests I just used a uint.  For the object tests I used this simple class:

package
{
	import flash.events.EventDispatcher;
 
	public class ValueObject extends EventDispatcher
	{
		public var x:Number;
		public var y:Number;
		public var z:Number;
 
		public function ValueObject(x:Number=0, y:Number=0, z:Number=0)
		{
			super();
 
			this.x = x;
			this.y = y;
			this.z = z;
		}
 
		override public function toString():String
		{
			return "[ValueObject x:"" + this.x + "" y:"" + this.y + "" z:"" + this.z + ""]";
		}
	}
}

Fill Time

The first part of the test was seeing how long it takes to fill the objects.  All I used was a simple “for” loop to fill each object to any given size.

Retrieval Time

To test retrival of data it was a simple matter of using a position for the Array and the actual primative/object for the Dictionaries.  The test was run grabbing a value that was added halfway through the filling test (i.e. size / 2).

Loop Time

Looping is where things change a bit.  To loop through the Dictionaries I used a “for each” loop and just a regular old “for” loop on the Array.

Conclusion

When dealing with primatives Dictionaries will perform better than Arrays when dealing with amounts of data over 50,000.  The results aren’t shown here because the graphs would be too skewed to get any real value out of them.

The real difference between Dictionaries and Arrays comes out when you fill them with objects and start looping.  Arrays outperform Dictionaries in those tests so much that it almost doesn’t seem real.

Of course, the real value of a Dictionary is in it’s retrieval abilities since you can retrive data using any value object as opposed to a linear position.  To do something similar using an Array would be a more expensive process.

Posted in Actionscript | Tagged , , , , | 4 Comments

Papervision3D + Google SketchUp

One of the more recent updates to Papervision3D 2 was the introduction of Google SketchUp support. So, I decided to prod this new functionality with a stick. Turns out it still has some growing to do. Never the less the basic functionality is there and waiting for a sprinkling of Miracle Grow.

Modeling School

First we need a SketchUp model. Just so happens that KMZ files are just ZIP files. Also, MKZ version 4 files already contain a DAE file. Perfect. So all you have to do is:

  1. Export your SketchUp file as a Google Earth 4 (*.kmz)
  2. Rename the .kmz file to .zip
  3. Open the ZIP
  4. Extract the DAE file from the models folder

If you don’t wanna make your own I’ve included one that I made for testing at the end of this post.

Coding Time

I like to embed my DAE files (you don’t have to, it’s just XML), so that’s what we’re gonna do first:

[Embed(source="../../../../../embedded/skull.dae", mimeType="application/octet-stream")]
private static const daeSkull:Class;

Not a big deal, just like embedding any other file. Now all we have to do is instantiate it and add it to a scene:

var collada:SketchupCollada = new SketchupCollada(XML(new SketchUpView.daeSkull()), null, 0.01);
this.scene.addChild(collada);

Bringin’ It Home

That’s pretty much all there is to it. You’ll probably notice a few things right off the bat like: “Where the f are all my materials?” Well, they’re not there, and you can’t apply new ones at runtime. At least, that’s how it stands as of the 709 build.

Here’s the example (click and drag to orbit the camera around the model):

Download my Google SketchUp file

Posted in Papervision | Tagged , , , , , , , | 7 Comments