MikeMatrixVisualizer: A VS debugger visualizer for XNA Matrices
One thing that surprised me (in retrospect) about my last sample was that it doesn't make a single reference to a Matrix. All my other apps use tons of matrices. It's hard to do much in 3D graphics without them. Even if you're writing 2D programs with XNA (as I have been, so far), I encourage you to get familiar with the full 3D graphics pipeline, including the functionality of the world, view, and projection matrices. These concepts are still meaningful for 2D apps...just a bit...flatter.
However, one bit of unpleasantness about working with matrices is trying to make sense of them in the Visual Studio debugger. They show up lookiing something like this:
It's tough to look at that mess of text and make sense of it as a matrix.
As it turns out, it's not too hard to write an extension to Visual Studio to view a Matrix (or any other type) in a custom manner. These extensions are called Visualizers, and I decided it was worth learning how to write one so I could more easily look at the Matrix objects in my programs. This article was very helpful (despite being based on VS 2005 Beta 2). Once you have built and installed a visualizer, you will see a magnifying glass icon next to the standard decoding of a variable. Clicking that magnifying glass will bring up your visualizer. Here's what I see now (after clicking the magnifying glass) for Matrix objects:
I don't think it's worth saying a whole lot about the code, because it's pretty straightforward, but let me highlight a few things about it (and visualizers in general):
- The DLL produced by this project needs to be installed into either <VS Install Dir>\Common7\Packages\Debugger\Visualizers or My Documents\Visual Studio\Visualizers in order for the debugger to find and use it. I specified a project post-build event to place MikeMatrixVisualizer in the former location. If you don't have privileges to write to that folder, you may need to copy it manually or use the alternate location.
- If the object you want to create a visualizer for doesn't have the [Serializable] attribute, it is a bit more work. I wanted to create a visualizer for Color as well, but it's not [Serializable].
- Note that under Project --> Add New Item... there is a template for "Debugger Visualizer" to get you started.
- I didn't see an obvious way to get the name of the variable -- only its contents. Let me know if you know how to do this, and I'll put the name in the window caption for the next version.
- I added a KeyPress event handler so the Matrix visualizer goes away when you press any key, just for convenience.
- Visualizers are able to modify the variable contents as well as simply displaying them. Umm, I didn't get around to implementing this for my visualizer.
I find this Matrix visualizer to be very helpful...hopefully you will, too!
As a reminder, if you want to send me feedback about this or any other post on my blog, you can send email to [XYZ]@hotmail.com, replacing [XYZ] with the name of my blog (with no spaces or punctuation).