Click here to Skip to main content
15,887,434 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.IO;
using System.Drawing;
using System.Diagnostics;

namespace GXGraphicsLibrary
{
	/// <summary>
	/// Summary description for GXAnimation.
	/// </summary>
	public class GXAnimation : IDisposable
	{
		/// <summary>
		/// The first animation cell displayed.
		/// </summary>
		protected int m_startCell;

		/// <summary>
		/// Width of an individual animation cell.
		/// </summary>
		public int CellWidth { get { return m_cellWidth; } }
		protected int m_cellWidth;

		/// <summary>
		/// Height of an individual animation cell.
		/// </summary>
		public int CellHeight { get { return m_cellHeight; } }
		protected int m_cellHeight;

		/// <summary>
		/// Number of cell rows in the bitmap.
		/// </summary>
		protected int m_numRows;

		/// <summary>
		/// Number of cell columns in the bitmap.
		/// </summary>
		protected int m_numCols;

		/// <summary>
		/// Bitmap to be used for animating.
		/// </summary>
		public GXBitmap Image { get { return m_bmp; } }
		protected GXBitmap m_bmp;

		/// <summary>
		/// Index of animation cell currently being displayed.
		/// </summary>
		protected int m_curCell;

		/// <summary>
		/// The time that this animation was started in milliseconds.
		/// </summary>
		public Int64 StartTime { set { m_startTime_ms = value; } }
		protected Int64 m_startTime_ms;

		/// <summary>
		/// Rate of the animation in cells per second.
		/// </summary>
		protected Int64 m_cellsPerSecond;

		/// <summary>
		/// Cached rectangle representing the drawing bounds of the current cell.
		/// </summary>
		protected Rectangle m_srcRegion = new Rectangle(0, 0, 0, 0);

		/// <summary>
		/// Keep track of whether the Bitmap used by this animation was allocated
		/// or not.
		/// </summary>
		protected bool m_allocated = false;

		/// <summary>
		/// Create an animation from a Bitmap stream.
		/// </summary>
		/// <param name="bmpData">Stream representing source bitmap</param>
		/// <param name="gx">GXGraphics object, needed for bitmap loading</param>
		/// <param name="numRows">Number of rows of cells in the animation</param>
		/// <param name="numCols">Number of columns of cells in the animation</param>
		/// <param name="startCell">Index from which to start animating</param>
		/// <param name="cellWidth">Width of individual animation cells</param>
		/// <param name="cellHeight">Height of individual animation cells</param>
		/// <param name="cellsPerSecond">Rate of animation in cells per second</param>
		/// <param name="curTime_ms">The time that this animation started animating in milliseconds</param>
		public GXAnimation(Stream bmpData, GXGraphics gx, int numRows, int numCols, int startCell, int cellWidth, int cellHeight, int cellsPerSecond, Int64 curTime_ms)
		{
			// Initialize the cell information
			m_cellWidth = cellWidth;
			m_cellHeight = cellHeight;
			m_numRows = numRows;
			m_numCols = numCols;
			m_startCell = startCell;

			// Load and initialize the Bitmap object
			m_bmp = new GXBitmap(bmpData, gx);
			m_allocated = true;

			// Initialize timing information
			m_cellsPerSecond = (Int64)cellsPerSecond;
			m_startTime_ms = curTime_ms;

			// Initialize the draw region rectangle
			m_srcRegion.Width = m_cellWidth;
			m_srcRegion.Height = m_cellHeight;

			// Validate information for drawing the first cell
			Update(curTime_ms);
		}

		/// <summary>
		/// Create an animation that shares information from another animation.
		/// </summary>
		/// <param name="anim">Original GXAnimation object</param>
		/// <param name="startCell">Index from which to start animating</param>
		/// <param name="cellsPerSecond">Rate of animation in cells per second</param>
		/// <param name="curTime_ms">The time that this animation started animating in milliseconds</param>
		public GXAnimation(GXAnimation anim, int startCell, int cellsPerSecond, Int64 curTime_ms)
		{
			// Initialize the cell information
			m_cellWidth = anim.m_cellWidth;
			m_cellHeight = anim.m_cellHeight;
			m_numRows = anim.m_numRows;
			m_numCols = anim.m_numCols;
			m_startCell = startCell;

			// Copy the reference to the original bitmap
			m_bmp = anim.m_bmp;
			m_allocated = false;

			// Initialize timing information
			m_cellsPerSecond = (Int64)cellsPerSecond;
			m_startTime_ms = curTime_ms;

			// Initialize the draw region rectangle
			m_srcRegion.Width = m_cellWidth;
			m_srcRegion.Height = m_cellHeight;

			// Validate information for drawing the first cell
			Update(curTime_ms);
		}

		/// <summary>
		/// Draw the current animation cell at the specified location.
		/// </summary>
		/// <param name="gx">GXGraphics object</param>
		/// <param name="x">X screen pixel location</param>
		/// <param name="y">Y screen pixel location</param>
		/// <param name="bmDest">GXBitmap that is the destination of the draw</param>
		public void Draw(GXGraphics gx, int x, int y, GXBitmap bmDest)
		{
			m_bmp.Draw(gx, m_srcRegion, x, y, bmDest);
		}

		/// <summary>
		/// Update the animation cell information.
		/// </summary>
		/// <param name="curTime_ms">Current system time in milliseconds.</param>
		public void Update(Int64 curTime_ms)
		{
			// The current cell is the starting cell offset by the number of cells since the time started
			m_curCell = (int)((m_startCell + (m_cellsPerSecond * (curTime_ms  - m_startTime_ms) / 1000)) % (m_numRows * m_numCols));
			Debug.Assert(m_curCell >= 0 && m_curCell < m_numRows * m_numCols, "GXAnimation.Update - Calculated cell out of range");

			// Set up the draw region rectangle for the current cell
			m_srcRegion.X = (int)((m_curCell % m_numCols) * m_cellWidth);
			m_srcRegion.Y = (int)((m_curCell / m_numCols) * m_cellHeight);
		}

		/// <summary>
		/// Clean up any memory allocated by the bitmap object.
		/// </summary>
		public void Dispose()
		{
			if (!m_allocated)
				return;

			if (m_bmp != null)
				m_bmp.Dispose();
		}
	}
}

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