Run a program directly after building in VS Code

April 11, 2018 .net, .netcore, msbuild, vscode

Whenever I have a program that is just a generator of some kind I like to have that program execute directly after having built the program successfully. I'm going to show how to run the program via the dotnet cli but this trick can easily be applied to regular .NET programs or any program which is built using MSBuild.

Read More

Merging Build.Directory.props

April 04, 2018 msbuild, .net

MSBuild version 15 introduced the concept of "Directory.Build.props" files. From the docs:

  • Directory.Build.props is a user-defined file that provides customizations to projects under a directory. This file is automatically imported from Microsoft.Common.props unless the property ImportDirectoryBuildTargets is set to false.

What is not stated here is that only one Directory.Build.props will be imported automatically. Imagine your project exists in the directory C:\repo\src\foo\foo.csproj and there exists a file in both C:\repo\src\Directory.Build.props and C:\repo\Directory.Build.props then only C:\repo\src\Directory.Build.props will be automatically included when building C:\repo\src\foo\foo.csproj. If you would like C:\repo\Directory.Build.props to be included as well, then C:\repo\src\Directory.Build.props will have to include C:\repo\Directory.Build.props like so:

    <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />

This is documented here.

Using Component Glue to build your object graphs automatically

July 26, 2013 Open Source, Dependency Injection, IoC, C#, .NET

Component Glue is an IoC container and you use it of course to wire up your object graphs for you. Component Glue can also build your object graphs for you automatically if there are no interfaces involved. Take this example:

In After.cs, you can see that Component Glue is able to build the entire object graph for us. This will include all future dependencies as well so long as interfaces don't come into play. Should an interface be needed, you can just bind that single component.

This is a very powerful thing. If one component needs to take on a dependency, just ask for it in the constructor and Component Glue will handle it for you.

Component Glue v1.0

June 11, 2013 .NET, C#, ComponentGlue, Dependency Injection, Github, IoC, NuGet

I finally brought Component Glue up to a state where I felt like the product was finished. You can get it via NuGet:

`PM> Install-Package ComponentGlue`

or you can get the source from GitHub. The documentation is definitely sparse I know but I recommend taking a look at the unit tests to get a feel for what you can do with Component Glue.

Credit for the NuGet CSS here.

GLDotNet Version 0.6.0

February 19, 2013 C#, OpenGL, .NET

I just released GLDotNet Version 0.6.0.


  • **GLDotNet.Toolkit**: Assembly containing simple app framework.
  • **GLDotNet.Objects**: Assembly containing higher level objects such as Texture2D and VertexBuffer.
  • More overloads added to GLContext class.
  • byte and sbyte are now mapped correctly in generated code.
  • Fixed the naming of some functions so as not to include type notation, i.e. Color4u.
  • Decreased the number of enum values output.

Introducing GLDotNet

January 02, 2013 C#, OpenGL, .NET, Open Source

Today I released a project I've been playing around with for a year or so on Codeplex. It's called GLDotNet. From the project description:

C# wrapper for OpenGL. Partially generated from the OpenGL spec and partially written by hand, the aim is to have a flexible and native feeling C# binding.

I have generated functions from the OpenGL spec excluding 1 or 2 but unfortunately of the generated code is untested. There is a demo project included in the source code. The Github repository is located here:

Getting Started with Snowball

March 04, 2012 C#, .NET, Game Programming

My game framework Snowball is far enough along that small games can be developed with it by now. The basic overall design is now laid out and not too much is likely to change as I'm now developing my own small games with it.

In order to create some kind of documentation on how to use Snowball, I created a Samples folder in the source. In the Samples folder is a WalkingWizard sample. I'm posting this source code here but it can also be viewed on GitHub here.


December 20, 2011 .NET, Command Shell, Console, Github, Quake, Terminal, Windows, WinQuakeCon

I've always wanted a program that would allow me have a command line that I could slide in and out of view as needed like the console from Quake. I never could find one and so I finally got decided to write a tool myself. A picture is below and the source code is on GitHub.

I'm not putting a download here as I don't consider this finished just yet.

Implementing basic Dependency Injection using a Service Container

June 21, 2010 .NET, C#, Dependency Injection, Design Patterns, Service Continer

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.

Read More

500 Downloads of the Same Game

June 09, 2010 .NET, CodePlex, Same Game Xna, WinForms, Xna

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

June 01, 2010 .NET, C#, Windows 7

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);

Calculating an angle from a Vector2

February 28, 2009 .net, vectors, xna

When you need to calculate an angle from a Vector2 structure, you can use this piece of code:

public static class Vector2Helper { public static float CalculateAngle(Vector2 v) { float angle = 0.0f; if(v != Vector2.Zero) { v.Normalize(); angle = (float)Math.Acos(v.Y); if(v.X &lt; 0.0f) angle = -angle; } return angle; } }

I used this to calculate an angle from the Vector2 of the Left Stick.

The original credit for this source code comes from here.

Drawing 2D Lines as Rotated Quads

February 07, 2009 .NET

I haven't had much time lately with work but one question I've seen asked many many times is how to draw lines of different widths. So, to cut to the chase I'll share the code I've used to do it.

public void DrawLine(Vector3 p1, Color c1, Vector3 p2, Color c2, int width) { float distance = Vector3.Distance(p1, p2); float halfDistance = distance / 2.0f; float halfWidth = width / 2.0f; Vector3 difference = p2 - p1; Vector3 destination = new Vector3(p1.X + difference.X / 2.0f, p1.Y + difference.Y / 2.0f, p1.Z + difference.Z); // Calculate angle between two points float angle = (float)Math.Atan2(difference.Y, difference.X); Vector3 v1, v2, v3, v4; v1 = new Vector3(-halfDistance, -halfWidth, 0); // Top Left v2 = new Vector3(halfDistance, -halfWidth, 0); // Top Right v3 = new Vector3(halfDistance, halfWidth, 0); // Bottom Right v4 = new Vector3(-halfDistance, halfWidth, 0); // Bottom Left Matrix m = Matrix.Identity * Matrix.CreateRotationZ(angle) * Matrix.CreateTranslation(destination); v1 = Vector3.Transform(v1, m); v2 = Vector3.Transform(v2, m); v3 = Vector3.Transform(v3, m); v4 = Vector3.Transform(v4, m); DrawQuad(v1, c1, v2, c2, v3, c2, v4, c1); }

I've left a lot of fluff code out. I usually check if the line is a width of 1 and draw a normal line. I also left out the code on how to draw a quad as that can be found many other places already.

SpriteSheet Class

January 09, 2009 .NET, SpriteSheet, XNA

I've been talking with a guy on the creator forums lately about SpriteSheets and so I decided it might be a good idea to post my SpriteSheet class.

It's very simple. Only reads sprites from left to right and assumes all Sprites are the same width and height.

#region Usingusing System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; #endregion namespace Snow.Xna.Graphics { /// Spritesheet class. /// public class SpriteSheet { #region Fields string name; Texture2D texture; Rectangle[] rectangles; int spriteWidth, spriteHeight; #endregion #region Properties /// The name of this SpriteSheet. /// public string Name { get { return name; } } /// The texture for this SpriteSheet. /// public Texture2D Texture { get { return texture; } } /// Returns a rectangle for a sprite in the SpriteSheet. /// /// index /// public Rectangle this[int i] { get { return rectangles[i]; } } /// The number of sprites in this SpriteSheet. /// public int Count { get { return rectangles.Length; } } /// The width of the texture. /// public int Width { get { return texture.Width; } } /// The width of each sprite in the SpriteSheet. /// public int SpriteWidth { get { return spriteWidth; } } /// The height of the texture. /// public int Height { get { return texture.Height; } } /// The height of each sprite in the SpriteSheet. /// public int SpriteHeight { get { return spriteHeight; } } #endregion /// Create a new SpriteSheet and determine the number of sprites in the sheet. /// /// /// Width of each sprite. /// Height of each sprite. public SpriteSheet(string name, Texture2D texture, int spriteWidth, int spriteHeight) : this(name, texture, spriteWidth, spriteHeight, 0) { } /// Create a new SpriteSheet. /// /// /// Width of each sprite. /// Height of each sprite. /// The number of sprites in the sheet. public SpriteSheet(string name, Texture2D texture, int spriteWidth, int spriteHeight, int count) { = name; this.texture = texture; this.spriteWidth = spriteWidth; this.spriteHeight = spriteHeight; if(count == 0) { int numX = texture.Width / spriteWidth; int numY = texture.Height / spriteHeight; rectangles = new Rectangle[numX * numY]; } else { rectangles = new Rectangle[count]; } int x = 0, y = 0; for(int i = 0; i < rectangles.Length; i++) { rectangles[i] = new Rectangle(x, y, spriteWidth, spriteHeight); x += spriteWidth; if(x >= texture.Width) { x = 0; y += spriteHeight; } } } public static implicit operator Texture2D(SpriteSheet spriteSheet) { return spriteSheet.Texture; } } }

You can create a new SpriteSheet and use it like this:


SpriteSheet spriteSheet = new SpriteSheet("tiles", Content.Load<texture2D>("tiles"), 64, 64); spriteBatch.Begin(); spriteBatch.Draw(spriteSheet, new Rectangle(0, 0, spriteSheet.SpriteWidth, spriteSheet.SpriteHeight), spriteSheet[0], Color.White); spriteBatch.End();


Which loads a spritesheet with sprites of size 64x64. It then draws the first Sprite in the SpriteSheet. You of course wouldn't want to load the spritesheet every frame as well.

Feel free to use this code without restriction.

Edit: I copied and pasted the second piece of code from somewhere else so I fixed two typos.

Colors and Hex

December 21, 2008 .NET, XNA

I recently needed to write out Color(s) as an xml attribute. I wrote 2 methods to read and write the Color(s) as Hex strings. Here ya go:

Read More