Click here to Skip to main content
15,894,546 members
Articles / Mobile Apps

Pocket 1945 - A C# .NET CF Shooter

Rate me:
Please Sign up or sign in to vote.
4.90/5 (101 votes)
2 Jun 2004CPOL10 min read 269.2K   2.7K   152  
An article on Pocket PC game development.
using System;
using System.Drawing;
using System.Reflection;

namespace Pocket1945
{
	/// <summary>
	/// To ensure that there is only one instance of the SpriteList class
	/// it implements the Singleton-pattern. This class is used to access 
	/// the sprites used in the game.
	/// </summary>
	public class SpriteList
	{
		/// <summary>
		/// Private variable indicating if the sprites are loaded or not.
		/// </summary>
		private bool doneLoading;

		#region Public bitmap arrays.
		/// <summary>
		/// Public field giving access to an array of sprites used for small planes.
		/// </summary>
		public Bitmap[] SmallPlanes;

		/// <summary>
		/// Public field giving access to an array of sprites used for a big planes.
		/// </summary>
		public Bitmap[] BigPlanes;

		/// <summary>
		/// Public field giving access to an array of sprites used for bullets.
		/// </summary>
		public Bitmap[] Bullets;

		/// <summary>
		/// Public field giving access to an array of sprites used for bonus items.
		/// </summary>
		public Bitmap[] Bonuses;

		/// <summary>
		/// Public field giving access to an array of background tiles.
		/// </summary>
		public Bitmap[] Tiles;

		/// <summary>
		/// Public field giving access to an array of big background elements.
		/// </summary>
		public Bitmap[] BigBackgroundElements;

		/// <summary>
		/// Public field giving access to an array of small background elements.
		/// </summary>
		public Bitmap[] SmallBackgroundElements;

		/// <summary>
		/// Public field giving access to an array of sprites used for a small explotion.
		/// </summary>
		public Bitmap[] SmallExplotion; 

		/// <summary>
		/// Public field giving access to an array of sprites used for a big explotion.
		/// </summary>
		public Bitmap[] BigExplotion;
		#endregion

		/// <summary>
		/// Public field giving access the the instance of the SpriteList class.
		/// </summary>
		public static readonly SpriteList Instance = new SpriteList();

		/// <summary>
		/// Metod loading the sprites from the assembly resource files
		/// into the public bitmap array. To be sure the sprites are only loaded
		/// once a private bool is set to true/false indicating if the sprites
		/// have been loaded or not.
		/// </summary>
		public void LoadSprites()
		{
			if(!doneLoading)
			{				
				//Accessing the executing assembly to read embeded resources.
				Assembly asm = Assembly.GetExecutingAssembly();
				
				//Reads the sprite strip containing the sprites you want to "parse".
				Bitmap tiles = new Bitmap(asm.GetManifestResourceStream("Pocket1945.Data.Sprites.Tiles.bmp"));
				Bitmap bonuses = new Bitmap(asm.GetManifestResourceStream("Pocket1945.Data.Sprites.Bonuses.bmp"));
				Bitmap bullets = new Bitmap(asm.GetManifestResourceStream("Pocket1945.Data.Sprites.Bullets.bmp"));
				Bitmap smallPlanes = new Bitmap(asm.GetManifestResourceStream("Pocket1945.Data.Sprites.SmallPlanes.bmp"));
				Bitmap smallExplotion = new Bitmap(asm.GetManifestResourceStream("Pocket1945.Data.Sprites.SmallExplotion.bmp"));
				Bitmap bigBackgroundElements = new Bitmap(asm.GetManifestResourceStream("Pocket1945.Data.Sprites.BigBackgroundElements.bmp"));
				Bitmap bigExplotion = new Bitmap(asm.GetManifestResourceStream("Pocket1945.Data.Sprites.BigExplotion.bmp"));
				Bitmap bigPlanes = new Bitmap(asm.GetManifestResourceStream("Pocket1945.Data.Sprites.BigPlanes.bmp"));

				//Parse the sprite strips into bitmap arrays.
				Tiles = ParseSpriteStrip(tiles);
				Bullets = ParseSpriteStrip(bullets);
				Bonuses = ParseSpriteStrip(bonuses);
				SmallPlanes = ParseSpriteStrip(smallPlanes);
				SmallExplotion = ParseSpriteStrip(smallExplotion);
				BigBackgroundElements = ParseSpriteStrip(bigBackgroundElements);
				BigExplotion = ParseSpriteStrip(bigExplotion);
				BigPlanes = ParseSpriteStrip(bigPlanes);

				//Clean up.
				tiles.Dispose();
				bullets.Dispose();
				bonuses.Dispose();
				smallPlanes.Dispose();
				smallExplotion.Dispose();
				bigBackgroundElements.Dispose();
				bigExplotion.Dispose();
				bigPlanes.Dispose();

				doneLoading = true;
			}
		}

		/// <summary>
		/// Method parsing a sprite strip into a bitmap array.
		/// </summary>
		/// <param name="destinationArray">The destination array for the sprites.</param>		
		/// <param name="spriteStrip">The sprite strip to read the sprites from.</param>
		private Bitmap[] ParseSpriteStrip(Bitmap spriteStrip)
		{							
			Rectangle spriteRectangle = new Rectangle(1, 1, spriteStrip.Height - 2, spriteStrip.Height - 2);
			Bitmap[] destinationArray = new Bitmap[(spriteStrip.Width - 1) / (spriteStrip.Height - 1)];

			//Loop drawing the sprites into the bitmap array. 			
			for(int i = 0; i < destinationArray.Length; ++i)
			{
				destinationArray[i] = new Bitmap(spriteRectangle.Width, spriteRectangle.Height);
				Graphics g = Graphics.FromImage(destinationArray[i]);
				spriteRectangle.X = i * (spriteRectangle.Width + 2) - (i - 1);				
				g.DrawImage(spriteStrip, 0, 0, spriteRectangle, GraphicsUnit.Pixel);				
				g.Dispose();
			}

			return destinationArray;
		}

		/// <summary>
		/// Method returning the tile index for a given ascii char.
		/// </summary>
		/// <param name="tileChar">The ASCII char you want a tile index for.</param>
		/// <returns>The tile index for the ASCII char.</returns>
		public int GetTileIndex(char tileChar)
		{
			switch(tileChar)
			{
				case 'A' :
					return 0;					
				case 'B' : 
					return 1;					
				case 'C' : 
					return 0;									
				case 'D' :
					return 1;					
				default : 
					return 0;					
			}
		}

		/// <summary>
		/// The constructor is made private to ensure that 
		/// the class cannot be explicitly created
		/// </summary>
		private SpriteList() 
		{			
		}
	}
}

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)


Written By
Web Developer
Norway Norway
I'm a C# developer from Norway. I'm a co-founder of a small ISV called GreIT AS. Our company site can be found at http://www.greit.no (all information in norwegian).

My everyday work consists of building ASP.NET web applications and work on our content management system, Webpakken. I use my spare time on side projects such as Pocket 1945 (http://workspaces.gotdotnet.com/pocket1945), a shooter game for the Pocket PC platform. It’s almost the opposite of my everyday work since games are small, focus on graphics and entertainment, while our CMS is large, focus on businesses and data.

When I’m not sitting in front of the computer I go snowboarding or skateboarding, depending on which time of the year it is.

I also enjoy fly fishing for salmon and trout in the summer. My personal record is a 10.5 KG salmon caught in Lakselva last summer and a 2 KG trout caught some where secret place in Finnmark. A bragging picture of me holding the salmon can be found at http://jonas.greit.no/pictures/salmon.jpg .

I did start blogging some time back, but I haven’t been to good at updating my blogg. I just got so much stuff going on that it’s hard to find time to add blogg posts. You can view my blogg at http://jonas.greit.no, just don’t expect too much.

Comments and Discussions