Click here to Skip to main content
15,895,011 members
Articles / Multimedia / DirectX

Endogine sprite engine

Rate me:
Please Sign up or sign in to vote.
4.84/5 (53 votes)
17 Jul 200615 min read 717.6K   22.1K   216  
Sprite engine for D3D and GDI+ (with several game examples).
/*
 * Created by: Leslie Sanford
 * 
 * Contact: jabberdabber@hotmail.com
 * 
 * Last modified: 08/05/2004
 */

using System;

namespace Endogine.Midi
{
    /// <summary>
    /// The base class for all MIDI short messages.
    /// </summary>
    /// <remarks>
    /// Short messages are any MIDI message except for system exclusive 
    /// messages. This includes all channel, system common, and system 
    /// realtime messages.
    /// </remarks>
	public abstract class ShortMessage : IMidiMessage
	{
        #region ShortMessage Members

        #region Constants

        //
        // Bit manipulation constants.
        //

        private const int StatusMask = 16776960;
        private const int Data1Mask = 16711935;
        private const int Data2Mask = 65535;
        private const int Data1Shift = 8;
        private const int Data2Shift = 16;

        /// <summary>
        /// Maximum value allowed for any status byte.
        /// </summary>
        public const int StatusValueMax = 255;

        /// <summary>
        /// Maximum value allowed for any data byte.
        /// </summary> 
        public const int DataValueMax = 127;

        #endregion

        #region Fields

        // The short message packed as an integer.
        private int message = 0;

        #endregion

        #region Construction

        /// <summary>
        /// Initializes a new instance of the ShortMessage class.
        /// </summary>
		protected ShortMessage()
		{
		}

        #endregion

        #region Methods

        /// <summary>
        /// Unpacks the status value from a packed MIDI short message.
        /// </summary>
        /// <param name="message">
        /// The packed MIDI short message.
        /// </param>
        /// <returns>
        /// The status value of the message.
        /// </returns>
        public static int UnpackStatus(int message)
        {
            return message & ~StatusMask;
        }

        /// <summary>
        /// Unpacks the first data value from a packed MIDI short message.
        /// </summary>
        /// <param name="message">
        /// The packed MIDI short message.
        /// </param>
        /// <returns>
        /// The first data value of the message.
        /// </returns>
        public static int UnpackData1(int message)
        {
            return (message & ~Data1Mask) >> Data1Shift;
        }

        /// <summary>
        /// Unpacks the second data value from a packed MIDI short message.
        /// </summary>
        /// <param name="message">
        /// The packed MIDI short message.
        /// </param>
        /// <returns>
        /// The second data value of the message.
        /// </returns>
        public static int UnpackData2(int message)
        {
            return (message & ~Data2Mask) >> Data2Shift;
        }

        /// <summary>
        /// Sets the status value.
        /// </summary>
        /// <param name="status">
        /// The new status value.
        /// </param>
        protected void SetStatus(int status)
        {
            // Enforce preconditions.
            if(status < 0 || status > StatusValueMax)
                throw new ArgumentOutOfRangeException("status", status,
                    "Status value out of range.");

            // Replace the current status value with the new one.
            message &= StatusMask;
            message |= status;
        }

        /// <summary>
        /// Gets the first data value.
        /// </summary>
        /// <returns>
        /// The first data value.
        /// </returns>
        protected int GetData1()
        {
            return (message & ~Data1Mask) >> Data1Shift;
        }

        /// <summary>
        /// Sets the first data value.
        /// </summary>
        /// <param name="data1">
        /// The first data value.
        /// </param>
        protected void SetData1(int data1)
        {
            // Enforce preconditions.
            if(data1 < 0 || data1 > DataValueMax)
                throw new ArgumentOutOfRangeException("data1", data1,
                    "First data byte for short message out of range.");

            // Replace current data value with the new one.
            message &= Data1Mask;
            message |= data1 << Data1Shift;
        }

        /// <summary>
        /// Gets the second data value.
        /// </summary>
        /// <returns>
        /// The second data value.
        /// </returns>
        protected int GetData2()
        {
            return (message & ~Data2Mask) >> Data2Shift;
        }

        /// <summary>
        /// Sets the second data value.
        /// </summary>
        /// <param name="data2">
        /// The second data value.
        /// </param>
        protected void SetData2(int data2)
        {
            // Enforce preconditions.
            if(data2 < 0 || data2 > DataValueMax)
                throw new ArgumentOutOfRangeException("data2", data2,
                    "Second data byte for short message out of range.");
            
            // Replace current data value with the new one.
            message &= Data2Mask;
            message |= data2 << Data2Shift;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the message as a packed integer.
        /// </summary>
        public int Message
        {
            get
            {
                return message;
            }
        }       

        #endregion

        #endregion        

        #region IMidiMessage Members

        /// <summary>
        /// Accepts a MIDI message visitor.
        /// </summary>
        /// <param name="visitor">
        /// The visitor to accept.
        /// </param>
        public abstract void Accept(MidiMessageVisitor visitor);

        /// <summary>
        /// Creates a deep copy of this message.
        /// </summary>
        /// <returns>
        /// A deep copy of this message.
        /// </returns>
        public abstract object Clone();

        /// <summary>
        /// Gets the Midi message's status value.
        /// </summary>
        public int Status
        {
            get
            {
                return message & ~StatusMask;
            }
        }

        #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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions