Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Writing a Multiplayer Game (in WPF)

, 16 Mar 2012
This article will explain some concepts of game development and how to apply and adapt them for multiplayer development.
Prize winner in Competition "Best C# article of August 2011"
RemoteGameSampleSource.zip
RemoteGameSample
Pfz.ClassLibraries
Pfz
Caching
Collections
DataTypes
DynamicObjects
Internal
Extensions
Factoring
Pfz.csproj.user
Pfz.ruleset
Pfz.snk
Properties
Remoting
Instructions
Serialization
Threading
Contexts
Pfz.Drawing.Wpf
Extensions
Pfz.Drawing.Wpf.csproj.user
Pfz.Drawing.Wpf.snk
Properties
Pfz.RemoteGaming
Properties
Pfz.WpfControls
Extensions
Pfz.WpfControls.csproj.user
Pfz.WpfControls.snk
Properties
themes
TypeConverters
ValueControls
RemoteGameSample
RemoteGameSample
Properties
Settings.settings
RemoteGameSample.csproj.user
RemoteGameSample.Common
Properties
Requests
RemoteGameSample.Server
Data
Cars
BlueCar
1.png
10.png
11.png
12.png
13.png
14.png
15.png
16.png
17.png
18.png
19.png
2.png
20.png
21.png
22.png
23.png
24.png
25.png
26.png
27.png
28.png
29.png
3.png
30.png
31.png
32.png
33.png
34.png
35.png
36.png
4.png
5.png
6.png
7.png
8.png
9.png
GreenCar
1.png
10.png
11.png
12.png
13.png
14.png
15.png
16.png
17.png
18.png
19.png
2.png
20.png
21.png
22.png
23.png
24.png
25.png
26.png
27.png
28.png
29.png
3.png
30.png
31.png
32.png
33.png
34.png
35.png
36.png
4.png
5.png
6.png
7.png
8.png
9.png
RedCar
1.png
10.png
11.png
12.png
13.png
14.png
15.png
16.png
17.png
18.png
19.png
2.png
20.png
21.png
22.png
23.png
24.png
25.png
26.png
27.png
28.png
29.png
3.png
30.png
31.png
32.png
33.png
34.png
35.png
36.png
4.png
5.png
6.png
7.png
8.png
9.png
YellowCar
1.png
10.png
11.png
12.png
13.png
14.png
15.png
16.png
17.png
18.png
19.png
2.png
20.png
21.png
22.png
23.png
24.png
25.png
26.png
27.png
28.png
29.png
3.png
30.png
31.png
32.png
33.png
34.png
35.png
36.png
4.png
5.png
6.png
7.png
8.png
9.png
Maps
BigMap
background.jpg
foreground.jpg
map.png
SmallMap
background.jpg
map.png
Properties
RemoteGameSample.Server.csproj.user
RemoteGameSample_Release.zip
RemoteGameSample_Release
Data
Cars
BlueCar
1.png
10.png
11.png
12.png
13.png
14.png
15.png
16.png
17.png
18.png
19.png
2.png
20.png
21.png
22.png
23.png
24.png
25.png
26.png
27.png
28.png
29.png
3.png
30.png
31.png
32.png
33.png
34.png
35.png
36.png
4.png
5.png
6.png
7.png
8.png
9.png
GreenCar
1.png
10.png
11.png
12.png
13.png
14.png
15.png
16.png
17.png
18.png
19.png
2.png
20.png
21.png
22.png
23.png
24.png
25.png
26.png
27.png
28.png
29.png
3.png
30.png
31.png
32.png
33.png
34.png
35.png
36.png
4.png
5.png
6.png
7.png
8.png
9.png
RedCar
1.png
10.png
11.png
12.png
13.png
14.png
15.png
16.png
17.png
18.png
19.png
2.png
20.png
21.png
22.png
23.png
24.png
25.png
26.png
27.png
28.png
29.png
3.png
30.png
31.png
32.png
33.png
34.png
35.png
36.png
4.png
5.png
6.png
7.png
8.png
9.png
YellowCar
1.png
10.png
11.png
12.png
13.png
14.png
15.png
16.png
17.png
18.png
19.png
2.png
20.png
21.png
22.png
23.png
24.png
25.png
26.png
27.png
28.png
29.png
3.png
30.png
31.png
32.png
33.png
34.png
35.png
36.png
4.png
5.png
6.png
7.png
8.png
9.png
Maps
BigMap
background.jpg
foreground.jpg
map.png
SmallMap
background.jpg
map.png
Pfz.dll
Pfz.Drawing.Wpf.dll
Pfz.RemoteGaming.dll
Pfz.WpfControls.dll
RemoteGameSample.Common.dll
RemoteGameSample.exe
RemoteGameSample.Server.exe
RemoteGameSample_Source.zip
RemoteGameSample_Source
Pfz.ClassLibraries
Pfz
Caching
Collections
DataTypes
DynamicObjects
Internal
Extensions
Factoring
Pfz.csproj.user
Pfz.ruleset
Pfz.snk
Pfz.suo
Properties
Remoting
Instructions
Internal
Udp
Serialization
Threading
Contexts
Disposers
Unsafe
Pfz.ClassLibraries.suo
Pfz.Drawing.Wpf
Extensions
Pfz.Drawing.Wpf.csproj.user
Pfz.Drawing.Wpf.snk
Properties
Pfz.RemoteGaming
Internal
Pfz.RemoteGaming.snk
Pfz.RemoteGaming.suo
Properties
Pfz.WpfControls
Extensions
Pfz.WpfControls.csproj.user
Pfz.WpfControls.snk
Properties
themes
TypeConverters
ValueControls
RemoteGameSample
AppIcon.ico
Properties
Settings.settings
RemoteGameSample.csproj.user
RemoteGameSample.suo
RemoteGameSample.Common
Properties
Requests
RemoteGameSample.Server
AppIcon.ico
Data
Cars
BlueCar
1.png
10.png
11.png
12.png
13.png
14.png
15.png
16.png
17.png
18.png
19.png
2.png
20.png
21.png
22.png
23.png
24.png
25.png
26.png
27.png
28.png
29.png
3.png
30.png
31.png
32.png
33.png
34.png
35.png
36.png
4.png
5.png
6.png
7.png
8.png
9.png
GreenCar
1.png
10.png
11.png
12.png
13.png
14.png
15.png
16.png
17.png
18.png
19.png
2.png
20.png
21.png
22.png
23.png
24.png
25.png
26.png
27.png
28.png
29.png
3.png
30.png
31.png
32.png
33.png
34.png
35.png
36.png
4.png
5.png
6.png
7.png
8.png
9.png
RedCar
1.png
10.png
11.png
12.png
13.png
14.png
15.png
16.png
17.png
18.png
19.png
2.png
20.png
21.png
22.png
23.png
24.png
25.png
26.png
27.png
28.png
29.png
3.png
30.png
31.png
32.png
33.png
34.png
35.png
36.png
4.png
5.png
6.png
7.png
8.png
9.png
YellowCar
1.png
10.png
11.png
12.png
13.png
14.png
15.png
16.png
17.png
18.png
19.png
2.png
20.png
21.png
22.png
23.png
24.png
25.png
26.png
27.png
28.png
29.png
3.png
30.png
31.png
32.png
33.png
34.png
35.png
36.png
4.png
5.png
6.png
7.png
8.png
9.png
Maps
BigMap
background.jpg
foreground.jpg
map.png
SmallMap
background.jpg
map.png
Properties
RemoteGameSample.Server.csproj.user
RemoteGameSample.Server.suo
using System;
using System.Collections.Generic;
using System.Windows;

namespace Pfz.Drawing.Wpf
{
    /// <summary>
    /// This class is returned by ManagedBitmap Lock and TryLock methods. This allows you to access pixels in a safe and relatively fast manner.
    /// </summary>
    public sealed class LockedBitmap:
        IDisposable
    {
		#region Constructor and Destructor
			#region Constructor
				internal LockedBitmap(ManagedBitmap managedBitmap)
				{
					Bitmap = managedBitmap;

					var writeableBitmap = managedBitmap.WriteableBitmap;
					Width = writeableBitmap.PixelWidth;
					Height = writeableBitmap.PixelHeight;
					Stride = writeableBitmap.BackBufferStride;

					unsafe
					{
						Scanline0 = (byte *)writeableBitmap.BackBuffer.ToPointer();
					}
				}
			#endregion
			#region Dispose
				/// <summary>
				/// Unlocks the bitmap.
				/// </summary>
				public void Dispose()
				{
					var bitmap = Bitmap;
					if (bitmap != null)
					{
						Bitmap = null;

						bitmap.WriteableBitmap.Unlock();
					}
				}
			#endregion
		#endregion

		#region Properties
			#region this[]
				/// <summary>
				/// Gets or sets a pixel Argb value.
				/// </summary>
				public Argb this[int x, int y]
				{
					get
					{
						if (x < 0 || x >= Width)
							throw new ArgumentOutOfRangeException("x");

						if (y < 0 || y >= Height)
							throw new ArgumentOutOfRangeException("y");

						unsafe
						{
							byte *line = &Scanline0[y * Stride];
							Argb *typedLine = (Argb* )line;
							return typedLine[x];
						}
					}
					set
					{
						if (x < 0 || x >= Width)
							throw new ArgumentOutOfRangeException("x");

						if (y < 0 || y >= Height)
							throw new ArgumentOutOfRangeException("y");

						unsafe
						{
							byte* line = &Scanline0[y * Stride];
							Argb* typedLine = (Argb*)line;
							typedLine[x] = value;
						}
					}
				}
			#endregion
			#region Bitmap
				/// <summary>
				/// Gets the ManagedBitmap that created this LockedBitmap.
				/// </summary>
				public ManagedBitmap Bitmap { get; private set; }
			#endregion

			#region Width
				/// <summary>
				/// Gets the Width of the Bitmap.
				/// </summary>
				public int Width { get; private set; }
			#endregion
			#region Height
				/// <summary>
				/// Gets the Height of the Bitmap.
				/// </summary>
				public int Height { get; private set; }
			#endregion

			#region Stride
				/// <summary>
				/// Gets the Stride of the bitmap. (the number of bytes used for each row).
				/// </summary>
				public int Stride { get; private set; }
			#endregion
			#region Scanline0
				/// <summary>
				/// Gets the memory address of the first line.
				/// </summary>
				[CLSCompliant(false)]
				public unsafe byte *Scanline0 { get; private set; }
			#endregion
		#endregion
		#region Methods
			#region AddDirtyRect
				/// <summary>
				/// Tells the real bitmap that an area was changed, so controls using it as a BitmapSource can update themselves.
				/// </summary>
				public void AddDirtyRect(Int32Rect rect)
				{
					Bitmap.WriteableBitmap.AddDirtyRect(rect);
				}

				/// <summary>
				/// Tells the real bitmap that an area was changed, so controls using it as a BitmapSource can update themselves.
				/// </summary>
				public void AddDirtyRect(int x, int y, int width, int height)
				{
					AddDirtyRect(new Int32Rect(x, y, width, height));
				}
			#endregion

			#region DrawPoint
				/// <summary>
				/// Applies an Argb color to a given pixel, and allows combinations of transparency to be used.
				/// </summary>
				public void DrawPoint(int x, int y, Argb color, ColorCombineMode combineMode)
				{
					if (x < 0 || x >= Width)
						throw new ArgumentOutOfRangeException("x");

					if (y < 0 || y >= Height)
						throw new ArgumentOutOfRangeException("y");

					unsafe
					{
						byte *line = &Scanline0[y * Stride];
						Argb *typedLine = (Argb *)line;
						typedLine[x] = color.Combine(typedLine[x], combineMode);
					}
				}
			#endregion
			#region FillRectangle
                /// <summary>
                /// Replaces the color of a rectangle with the given color. Note that using a transparent color will replace the area with the transparent
                /// color, instead of applying it.
                /// </summary>
				public void FillRectangle(int left, int top, int width, int height, Argb color, ColorCombineMode combineMode)
				{
                    if (width == 0 || height == 0)
                        return;

                    if (width < 0)
                    {
                        left += width;
                        width = -width;
                    }

                    if (height < 0)
                    {
                        top += height;
                        height = -height;
                    }

                    if (left < 0)
					{
						width += left;
						left = 0;
					}

					if (top < 0)
					{
						height += top;
						top = 0;
					}

					int right = left + width;
					int diff = Width - right;
					if (diff < 0)
						width += diff;

					int bottom = top + height;
					diff = Height - bottom;
					if (diff < 0)
						height += diff;

					if (width <= 0 || height <= 0)
						return;

					unsafe
					{
						byte *lineStartBytes = &Scanline0[top * Stride + left * sizeof(Argb)];

                        switch (combineMode)
                        {
                            case ColorCombineMode.Replace:
						        for(int lineIndex=0; lineIndex<height; lineIndex++)
						        {
							        Argb *colorPointer = (Argb *)lineStartBytes;
							        for(int rowIndex=0; rowIndex<width; rowIndex++)
							        {
								        *colorPointer = color;
								        colorPointer++;
							        }

							        lineStartBytes += Stride;
						        }
                                break;

                            case ColorCombineMode.CombineIn:
						        for(int lineIndex=0; lineIndex<height; lineIndex++)
						        {
							        Argb *colorPointer = (Argb *)lineStartBytes;
							        for(int rowIndex=0; rowIndex<width; rowIndex++)
							        {
                                        Argb oldColor = *colorPointer;
                                        Argb newColor = color.CombineIn(oldColor);
								        *colorPointer = newColor;
								        colorPointer++;
							        }

							        lineStartBytes += Stride;
						        }
                                break;

                            case ColorCombineMode.CombineOver:
						        for(int lineIndex=0; lineIndex<height; lineIndex++)
						        {
							        Argb *colorPointer = (Argb *)lineStartBytes;
							        for(int rowIndex=0; rowIndex<width; rowIndex++)
							        {
                                        Argb oldColor = *colorPointer;
                                        Argb newColor = color.CombineOver(oldColor);
								        *colorPointer = newColor;
								        colorPointer++;
							        }

							        lineStartBytes += Stride;
						        }
                                break;

                            default:
                                throw new ArgumentException("Unknown value for combineMode.", "combineMode");
                        }
					}
				}
			#endregion
            #region Fill
                /// <summary>
                /// Fills an area, starting at the given point,  with the given color.
                /// </summary>
                public void Fill(int x, int y, Argb color, ColorCombineMode combineMode)
                {
                    Argb colorToReplace = this[x, y];
                    HashSet<KeyValuePair<int, int>> visitedPoints = new HashSet<KeyValuePair<int, int>>();
                    _Fill(x, y, colorToReplace, color, combineMode, visitedPoints);
                }
                private void _Fill(int x, int y, Argb colorToReplace, Argb color, ColorCombineMode combineMode, HashSet<KeyValuePair<int, int>> visitedPoints)
                {
                    if (x < 0 || y < 0 || x >= Width || y >= Height)
                        return;

                    var point = new KeyValuePair<int, int>(x, y);
                    if (!visitedPoints.Add(point))
                        return;

                    if (this[x, y] == colorToReplace)
                    {
                        DrawPoint(x, y, color, combineMode);
                        _Fill(x - 1, y - 1, colorToReplace, color, combineMode, visitedPoints);
                        _Fill(x   ,  y - 1, colorToReplace, color, combineMode, visitedPoints);
                        _Fill(x + 1, y - 1, colorToReplace, color, combineMode, visitedPoints);

                        _Fill(x - 1, y, colorToReplace, color, combineMode, visitedPoints);
                        _Fill(x    , y, colorToReplace, color, combineMode, visitedPoints);
                        _Fill(x + 1, y, colorToReplace, color, combineMode, visitedPoints);

                        _Fill(x - 1, y + 1, colorToReplace, color, combineMode, visitedPoints);
                        _Fill(x    , y + 1, colorToReplace, color, combineMode, visitedPoints);
                        _Fill(x + 1, y + 1, colorToReplace, color, combineMode, visitedPoints);
                    }
                }
            #endregion

			#region DrawBlockTo
				/// <summary>
				/// Applies the pixels of this bitmap to another one, so transparent colors will be combined.
				/// </summary>
				public void DrawBlockTo(LockedBitmap to, ColorCombineMode combineMode)
				{
					if (to == null)
						throw new ArgumentNullException("to");

					int minWidth = Math.Min(Width, to.Width);
					int minHeight = Math.Min(Height, to.Height);

					_DrawBlockTo(to, combineMode, 0, 0, 0, 0, minWidth, minHeight);
				}

				/// <summary>
				/// Applies the pixels of this bitmap to another one, so transparent colors will be combined.
				/// </summary>
				public void DrawBlockTo(LockedBitmap to, ColorCombineMode combineMode, int sourceLeft, int sourceTop, int destinationLeft, int destinationTop, int width, int height)
				{
					if (to == null)
						throw new ArgumentNullException("to");

					if (width <= 0 || height <= 0)
						return;

					int minLeft = Math.Min(sourceLeft, destinationLeft);
					if (minLeft < 0)
					{
						width += minLeft;
						sourceLeft -= minLeft;
						destinationLeft -= minLeft;
					}

					int minTop = Math.Min(sourceTop, destinationTop);
					if (minTop < 0)
					{
						height += minTop;
						sourceTop -= minTop;
						destinationTop -= minTop;
					}

					int sourceRight = sourceLeft + width;
					int destRight = destinationLeft + width;
					int diffRight = Math.Min(Width - sourceRight, to.Width - destRight);
					if (diffRight < 0)
						width += diffRight;

					int sourceBottom = sourceTop + height;
					int destBottom = destinationTop + height;
					int diffBottom = Math.Min(Height - sourceBottom, to.Height - destBottom);
					if (diffBottom < 0)
						height += diffBottom;

					if (width <= 0 || height <= 0)
						return;

					_DrawBlockTo(to, combineMode, sourceLeft, sourceTop, destinationLeft, destinationTop, width, height);

				}
				private void _DrawBlockTo(LockedBitmap to, ColorCombineMode combineMode, int sourceLeft, int sourceTop, int destinationLeft, int destinationTop, int width, int height)
				{
					unsafe
					{
						int sourceStride = Stride;
						int destStride = to.Stride;

						byte* sourceBytes = &Scanline0[sourceTop * sourceStride + sourceLeft * sizeof(Argb)];
						byte* destBytes = &to.Scanline0[destinationTop * destStride + destinationLeft * sizeof(Argb)];

                        switch (combineMode)
                        {
                            case ColorCombineMode.Replace:
						        for (int y = 0; y < height; y++)
						        {
							        Argb* source = (Argb*)sourceBytes;
							        Argb* dest = (Argb*)destBytes;

							        for (int x = 0; x < width; x++)
								        *dest++ = *source++;

							        sourceBytes += sourceStride;
							        destBytes += destStride;
						        }
                                break;

                            case ColorCombineMode.CombineIn:
						        for (int y = 0; y < height; y++)
						        {
							        Argb* source = (Argb*)sourceBytes;
							        Argb* dest = (Argb*)destBytes;

							        for (int x = 0; x < width; x++)
							        {
								        Argb sourceColor = *source++;
								        Argb destColor = *dest;

								        *dest++ = sourceColor.CombineIn(destColor);
							        }

							        sourceBytes += sourceStride;
							        destBytes += destStride;
						        }
                                break;

                            case ColorCombineMode.CombineOver:
						        for (int y = 0; y < height; y++)
						        {
							        Argb* source = (Argb*)sourceBytes;
							        Argb* dest = (Argb*)destBytes;

							        for (int x = 0; x < width; x++)
							        {
								        Argb sourceColor = *source++;
								        Argb destColor = *dest;

								        *dest++ = sourceColor.CombineOver(destColor);
							        }

							        sourceBytes += sourceStride;
							        destBytes += destStride;
						        }
                                break;

                            default:
                                throw new ArgumentException("Unknown value for combineMode.", "combineMode");
                        }
					}
				}
            #endregion
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paulo Zemek
Architect
Canada Canada
I started to program computers when I was 11 years old, as a hobbist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.
 
At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do they work easier, faster and with less errors.
 
Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com
 
Codeproject MVP 2012
Microsoft MVP 2013

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 16 Mar 2012
Article Copyright 2011 by Paulo Zemek
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid