Click here to Skip to main content
15,893,668 members
Articles / Mobile Apps

Bunnyaruga: GAPI, Hekkus, Basics of Deployment

Rate me:
Please Sign up or sign in to vote.
3.63/5 (15 votes)
25 Apr 2004CPOL14 min read 54.1K   247   20  
This article shows an example of a game that uses the GAPI and Hekkus libraries. It also shows a nice and free way of deploying your games/applications without requiring the .NET Framework installed on the end user machines.
using System;
using System.Drawing;

namespace GXGraphicsLibrary
{
	/// <summary>
	/// Encapsulates the functionality of converting pixel formats.
	/// </summary>
	public class Pixel
	{
		/// <summary>
		/// Delegate for converting a pixel to a 32 bit ARGB pixel.
		/// </summary>
		public PixelToARGBDelegate PixelToARGB = null;
		public delegate uint PixelToARGBDelegate(ushort pixel);

		/// <summary>
		/// Delegate for converting System.Drawing.Color to the device's pixel
		/// format.
		/// </summary>
		public ColorToPixelDelegate ColorToPixel = null;
		public delegate ushort ColorToPixelDelegate(Color col);

		/// <summary>
		/// Delegate for converting a 32-bit ARGB pixel to the device's pixel
		/// format.
		/// </summary>
		public ARGBToPixelDelegate ARGBToPixel = null;
		public delegate ushort ARGBToPixelDelegate(uint col);

		/// <summary>
		/// Delegate for converting a 16-bit BGR pixel to the device's pixel
		/// format.
		/// </summary>
		public BGRToPixelNoShiftDelegate BGRToPixelNoShift = null;
		public delegate ushort BGRToPixelNoShiftDelegate(byte blue, byte green, byte red);

		/// <summary>
		/// Delegate for converting a 24-bit BGR pixel to the device's pixel
		/// format.
		/// </summary>
		public BGRToPixelDelegate BGRToPixel = null;
		public delegate ushort BGRToPixelDelegate(byte blue, byte green, byte red);

		/// <summary>
		/// Delegate for breaking a pixel into its red, green, and blue components
		/// </summary>
		public PixelToBGRDelegate PixelToBGR = null;
		public delegate void PixelToBGRDelegate(ushort pixel, ref ushort blue, ref ushort green, ref ushort red);

		/// <summary>
		/// Create an instance of this class given the specified pixel format.
		/// The format will be used to initialize all delegates.
		/// </summary>
		/// <param name="pixelFormat">Device pixel format</param>
		public Pixel(uint pixelFormat)
		{
			if ((pixelFormat & (uint)GAPI.kfDirect565) == (uint)GAPI.kfDirect565)
			{
				ColorToPixel = new ColorToPixelDelegate(ColorToPixel565);
				ARGBToPixel = new ARGBToPixelDelegate(ARGBToPixel565);
				BGRToPixel = new BGRToPixelDelegate(BGRToPixel565);
				BGRToPixelNoShift = new BGRToPixelNoShiftDelegate(BGRToPixelNoShift565);
				PixelToBGR = new PixelToBGRDelegate(PixelToBGR565);
				PixelToARGB = new PixelToARGBDelegate(PixelToARGB565);
			}
			else
			{
				ColorToPixel = new ColorToPixelDelegate(ColorToPixel555);
				ARGBToPixel = new ARGBToPixelDelegate(ARGBToPixel555);
				BGRToPixel = new BGRToPixelDelegate(BGRToPixel555);
				BGRToPixelNoShift = new BGRToPixelNoShiftDelegate(BGRToPixelNoShift555);
				PixelToBGR = new PixelToBGRDelegate(PixelToBGR555);
				PixelToARGB = new PixelToARGBDelegate(PixelToARGB555);
			}
		}

		/// <summary>
		/// Converts 888 BGR values to a 565 RGB formatted pixel.
		/// </summary>
		/// <param name="blue">8-bit blue component</param>
		/// <param name="green">8-bit green component</param>
		/// <param name="red">8-bit red component</param>
		/// <returns>A 16-bit 565 RGB formatted pixel</returns>
		public ushort BGRToPixel565(byte blue, byte green, byte red)
		{
			return (ushort)((((ushort)red >> 3) << 11) | (((ushort)green >> 2) << 5) | ((ushort)blue >> 3));
		}

		/// <summary>
		/// Converts 565 BGR values to a 565 RGB formatted pixel.
		/// </summary>
		/// <param name="blue">5-bit blue component</param>
		/// <param name="green">6-bit green component</param>
		/// <param name="red">5-bit red component</param>
		/// <returns>A 16-bit 565 RGB formatted pixel</returns>
		public ushort BGRToPixelNoShift565(byte blue, byte green, byte red)
		{
			return (ushort)((((ushort)red) << 11) | (((ushort)green) << 5) | ((ushort)blue));
		}

		/// <summary>
		/// Converts a 565 RGB formatted pixel to its RGB components
		/// </summary>
		/// <param name="pixel">Pixel</param>
		/// <param name="blue">Reference to blue component</param>
		/// <param name="green">Reference to green component</param>
		/// <param name="red">Reference to red component</param>
		public void PixelToBGR565(ushort pixel, ref ushort blue, ref ushort green, ref ushort red)
		{
			red = (ushort)((pixel & 0xf800) >> 11);
			green = (ushort)((pixel & 0x07e0) >> 5);
			blue = (ushort)((pixel & 0x001f));
		}

		/// <summary>
		/// Converts 888 BGR values to a 555 RGB formatted pixel.
		/// </summary>
		/// <param name="blue">8-bit blue component</param>
		/// <param name="green">8-bit green component</param>
		/// <param name="red">8-bit red component</param>
		/// <returns>A 15-bit 555 RGB formatted pixel</returns>
		public ushort BGRToPixel555(byte blue, byte green, byte red)
		{
			return (ushort)((((ushort)red >> 3) << 10) | (((ushort)green >> 3) << 5) | ((ushort)blue >> 3));
		}

		/// <summary>
		/// Converts 555 BGR values to a 555 RGB formatted pixel.
		/// </summary>
		/// <param name="blue">5-bit blue component</param>
		/// <param name="green">5-bit green component</param>
		/// <param name="red">5-bit red component</param>
		/// <returns>A 15-bit 555 RGB formatted pixel</returns>
		public ushort BGRToPixelNoShift555(byte blue, byte green, byte red)
		{
			return (ushort)((((ushort)red) << 10) | (((ushort)green) << 5) | ((ushort)blue));
		}

		/// <summary>
		/// Converts a 555 RGB formatted pixel to its RGB components
		/// </summary>
		/// <param name="pixel">Pixel</param>
		/// <param name="blue">Reference to blue component</param>
		/// <param name="green">Reference to green component</param>
		/// <param name="red">Reference to red component</param>
		public void PixelToBGR555(ushort pixel, ref ushort blue, ref ushort green, ref ushort red)
		{
			red = (ushort)((pixel & 0x7c00) >> 10);
			green = (ushort)((pixel & 0x03e0) >> 5);
			blue = (ushort)((pixel & 0x001f));
		}

		/// <summary>
		/// Converts a System.Drawing.Color object to a 565 RGB formatted pixel.
		/// </summary>
		/// <param name="col">Color to be converted</param>
		/// <returns>A 16-bit 565 RGB formatted pixel</returns>
		public ushort ColorToPixel565(Color col)
		{
			return ((ushort)((((ushort)col.R >> 3) << 11) | (((ushort)col.G >> 2) << 5) | ((ushort)col.B >> 3)));
		}

		/// <summary>
		/// Converts a System.Drawing.Color object to a 555 RGB formatted pixel.
		/// </summary>
		/// <param name="col">Color to be converted</param>
		/// <returns>A 15-bit 555 RGB formatted pixel</returns>
		public ushort ColorToPixel555(Color col)
		{
			return ((ushort)((((ushort)col.R >> 3) << 10) | (((ushort)col.G >> 3) << 5) | ((ushort)col.B >> 3)));
		}

		/// <summary>
		/// Converts a 32-bit ARGB pixel value to a 565 RGB formatted pixel.
		/// </summary>
		/// <param name="col">ARGB color to be converted</param>
		/// <returns>A 16-bit 565 RGB formatted pixel</returns>
		public ushort ARGBToPixel565(uint col)
		{
			return ((ushort)(((col >> 8) & 0xf800) | ((col >> 5) & 0x07e0) | ((col >> 3) & 0x001f)));
		}

		/// <summary>
		/// Converts a 565 RGB formatted pixel to a 32-bit ARGB pixel.
		/// </summary>
		/// <param name="pixel">A 16-bit 565 RGB formatted pixel</param>
		/// <returns>ARGB converted pixel</returns>
		public uint PixelToARGB565(ushort pixel)
		{
			uint bigPixel = (uint)pixel;
			return 0x00f8fcf8 & ((bigPixel << 8) | (bigPixel << 5) | (bigPixel << 3));
		}

		/// <summary>
		/// Converts a 555 RGB formatted pixel to a 32-bit ARGB pixel.
		/// </summary>
		/// <param name="pixel">A 16-bit 555 RGB formatted pixel</param>
		/// <returns>ARGB converted pixel</returns>
		public uint PixelToARGB555(ushort pixel)
		{
			uint bigPixel = (uint)pixel;
			return 0x00f8f8f8 & ((bigPixel << 9) | (bigPixel << 6) | (bigPixel << 3));
		}

		/// <summary>
		/// Converts a 32-bit ARGB pixel value to a 555 RGB formatted pixel.
		/// </summary>
		/// <param name="col">ARGB color to be converted</param>
		/// <returns>A 15-bit 555 RGB formatted pixel</returns>
		public ushort ARGBToPixel555(uint col)
		{
			return ((ushort)(((col >> 9) & 0x7c00) | ((col >> 6) & 0x03d0) | ((col >> 3) & 0x001f)));
		}
	}
}

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
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions