Zachary
This user hasn't shared any biographical information
Homepage: http://www.zacharysnow.net
Posts by Zachary
Nystrom’s Programming Patterns
Aug 13th
I was reading the Scientific Ninja Blog this morning and he has posted a link to an in progress site / book by Robert Nystrom about game programming patterns. It’s not complete yet but it’s quite an interesting read to say the least.
Visual Studio: Move referenced DLL to different directory after Build
Jul 20th
If you need to move a referenced DLL to a different directory after build, add these commands to the “Post Build event command line” box in the “Build Events” tab of the project properties:
mkdir $(TargetDir)dir move $(TargetDir)myDLL.dll $(TargetDir)dir\myDLL.dll
Xna: Load Texture2D from Embedded Resource
Jul 3rd
If you’re writing an app which uses Xna, you may need to load a texture from an embedded resource. Here’s how:
First embed the resource in your app. Do so by choosing Embedded Resource as the Build Action in the properties of the resource.
After that you can load the Texture2D using a stream handle to the embedded file.
Stream stream = Assembly.GetCallingAssembly().GetManifestResourceStream("AppNamespace.Folder.font.bmp");
return Texture2D.FromFile(graphicsDevice, stream);
GetCallingAssembly() can be exchanged with GetExecutingAssembly() if needed. The name of the resource must be fully qualified with the app’s namespace and folders. I usually keep my resources in a folder Resources so I would have: AppNamespace.Resources.font.bmp.
Same Game Xna 2.0 Alpha
Jun 29th
I’ve released what I’m calling the 2.0 alpha version of Same Game Xna.
Implementing basic Dependency Injection using a Service Container
Jun 21st
By extending your Service Container class, a very basic version of dependency injection can be implemented. We’ll implement two forms of dependency injection: constructor and property injection.
500 Downloads of the Same Game
Jun 9th
My little Xna game that I wrote nearly 2 years ago reached the 500 downloads mark (binaries and source) the other day. With that said, I’d like to say that I’m working on version 2.0.
In version 2.0 I’m going to make the code more event driven. The old code uses the Xna Game class and in the new version I’ll be making it WinForms based. Almost a complete rewrite.
My work so far is available through SVN on the project’s Codeplex page.
Progress Bar in Windows 7 Taskbars
Jun 1st
I decided to add progress bar to the Windows 7 Taskbar in my Timer app.
I started by downloading and compiling the Windows API Code Pack in Release mode. I then added a reference to the Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll files to the project. After that add the lines:
using Microsoft.WindowsAPICodePack.Taskbar;
to your using statements. When the clock starts running I create the progress bar in the taskbar with:
// Initialize progress bar
if(TaskbarManager.IsPlatformSupported)
{
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal);
TaskbarManager.Instance.SetProgressValue(0, (int)this.totalTime.TotalSeconds, this.Handle);
}
to stop the progress bar:
// Stop progress bar if(TaskbarManager.IsPlatformSupported) TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
and finally to update the progress bar on each tick:
// Update progress bar if(TaskbarManager.IsPlatformSupported) TaskbarManager.Instance.SetProgressValue((int)this.totalTime.TotalSeconds - (int)this.time.TotalSeconds, (int)this.totalTime.TotalSeconds, this.Handle);
WinForms and MVC
May 26th
I recently became interested in doing MVC inside of a Windows Forms app. I found a few MVC frameworks which work with WinForms (see here) but they didn’t really interest me. Too heavy I felt for what I was looking to do. I ended up with a solution looking something like this:
There is really only one controller and that is the “Application” class. It contains all the methods your app can call to manipulate your models, which are in the “Data” folder / namespace. The “WinFormsApplication” class inherits from the “Application” class and just sets the view to an instance of “WinFormsView”. The “Application” class communicates with the view through the “IView” interface. The “WinFormsView” class is a Windows Forms implementation of that view. The “Application” class and your models are not coupled in any way to your Windows Forms implementation of the view.
If you want you view to be as dumb as possible, your view can communicate with the “Application” class only through events. In my case though, I choose to go with a smart view and have the view call back to methods in the “Application” class. The “Application” class tells the view when models are loaded and unloaded. The view subscribes to events on the models and reacts to the events.
All of my forms and controls communicate with each other through the “WinFormsView” class. One control might change the value of a property in the “WinFormsView” class and another control might subscribe to a change event and update as necessary. This keeps the controls and forms slightly less coupled.
It’s not a perfect implementation of MVC but it keeps my model logic decoupled enough from my view logic that I can later implement a WPF version of the view I think.
Drawing Rectangles with SpriteBatch
Mar 29th
Just a quick code snippet which adds an extension method for drawing Rectangles to SpriteBatch:
public static class SpriteBatchHelper
{
static Texture2D pixel;
private static void LoadPixel(GraphicsDevice graphicsDevice)
{
if(pixel == null)
{
pixel = new Texture2D(graphicsDevice, 1, 1);
pixel.SetData<Color>(new Color[] { Color.White });
}
}
public static void DrawRectangle(this SpriteBatch spriteBatch, Rectangle rectangle, Color color)
{
LoadPixel(spriteBatch.GraphicsDevice);
spriteBatch.Draw(pixel, rectangle, color);
}
}
C# Extension Methods in your own Library
Mar 8th
Normally I use extension methods in C# to extend a library that I did not write and therefore I have no control over. There are situations where it makes sense to use extension methods for a library that you yourself are writing.
For example, when you have interfaces in your library. You want to keep the number of methods in that interface as low as possible so that classes implementing the interface don’t have to do a lot of heavy lifting. This means cutting out methods in an interface that are for the most part just syntactic sugar for another method in the interface.
public interface IServiceContainer
{
void AddService(Type type, Object provider);
object GetService(Type type);
}
public static class IServiceContainerExtensions
{
public static void AddService<T>(this IServiceContainer services, object provider)
{
services.AddService(typeof(T), provider);
}
public static T GetService<T>(this IServiceContainer services) where T : class
{
return services.GetService(typeof(T)) as T;
}
public static T GetRequiredService<T>(this IServiceContainer services) where T : class
{
T service = services.GetService(typeof(T)) as T;
if(service == null)
throw new ServiceNotFoundException(typeof(T));
return service;
}
}
All of the methods in IServiceContainerExtensions are just helper methods for method in IServiceContainer. By making them extension methods in our own library though, we’ve made the barrier to entry lower. Other people can implement the interface and in a sense “inherit” the helper methods as well.



