Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / WPF

Snail Quest - A Maze Game Using WPF, A* Search Algorithm, C# Midi Toolkit and Irrklang Audio Engine

Rate me:
Please Sign up or sign in to vote.
4.99/5 (68 votes)
7 Apr 2011CPOL12 min read 142K   4.1K   103  
This article covers the creation of maze game all the way from its WPF animations to its music integration with two sound engines.
This article covers the creation of maze game, following it from a text file to a WPF, going through all the WPF animations involved with the game, as well as the non WPF animations, made by switching between differnt configurations. The article also covers how input corresponds to cell coordinates, utilizes the A* Search algorithm for the enemies, and goes over the implementation of the music for the game, which involves two sound engines, Midi Toolkit and IrrKlang.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sanford.Multimedia.Midi;
using System.ComponentModel;

namespace SnailQuest
{
    public class MidiHelper
    {
        private int outDeviceID = 0;
        private OutputDevice outDevice;
        private Dictionary<string, Sequence> dicSequence = new Dictionary<string,Sequence>();
        private Dictionary<string, Sequencer> dicSequencer = new Dictionary<string,Sequencer>();
        private Dictionary<string, NoArgDelegate> dicPlayingCompleteDelegate = new Dictionary<string, NoArgDelegate>();
        private Dictionary<string, int> dicSequencerMessageCount = new Dictionary<string, int>();
        private Dictionary<string, bool> dicSequencerInitialized = new Dictionary<string, bool>();

        private bool playing = false;
        private bool closing = false;
        public delegate void NoArgDelegate();
        NoArgDelegate loadCompleted;
        NoArgDelegate playingCompleted;

        #region ctor
        public MidiHelper()
        {
            if (outDevice == null)
                outDevice = new OutputDevice(outDeviceID);
        }
        #endregion ctor

        #region methods

        public void InitializeSequencer(string midiKey)
        {
            var sequence = dicSequence[midiKey];
            var sequencer = dicSequencer[midiKey];

            sequencer.Stop();
            playing = false;

            sequence.Format = 1;
            sequencer.Position = 0;
            sequencer.Sequence = sequence;
            sequencer.ChannelMessagePlayed += new System.EventHandler<Sanford.Multimedia.Midi.ChannelMessageEventArgs>(this.HandleChannelMessagePlayed);
            sequencer.Stopped += new System.EventHandler<Sanford.Multimedia.Midi.StoppedEventArgs>(this.HandleStopped);
            sequencer.SysExMessagePlayed += new System.EventHandler<Sanford.Multimedia.Midi.SysExMessageEventArgs>(this.HandleSysExMessagePlayed);
            sequencer.Chased += new System.EventHandler<Sanford.Multimedia.Midi.ChasedEventArgs>(this.HandleChased);
            sequence.LoadCompleted += HandleLoadCompleted;
        }

        public void Load(string midiKey, string midiFile)
        {
            dicSequence.Add(midiKey, new Sequence());
            dicSequencer.Add(midiKey, new Sequencer(midiKey));
            dicPlayingCompleteDelegate.Add(midiKey, null);
            dicSequencerMessageCount.Add(midiKey, 0);
            dicSequencerInitialized.Add(midiKey, false);

            InitializeSequencer(midiKey);

            dicSequencer[midiKey].Stop();

            dicSequencer[midiKey].ChannelMessagePlayed += (s, e) =>
            {
                dicSequencerMessageCount[midiKey]++;
            };

            dicSequencer[midiKey].PlayingCompleted += (s, e) =>
            {
                if (dicSequencerMessageCount[midiKey] > 0)
                {
                    var playingCompleted = dicPlayingCompleteDelegate[midiKey];

                    if (playingCompleted != null)
                        playingCompleted();
                    dicSequencer[midiKey].Stop();
                    dicSequencerMessageCount[midiKey] = 0;
                }
            };

            playing = false;
            dicSequence[midiKey].LoadAsync(midiFile);
        }

        public void Play(string midiKey, NoArgDelegate playingCompleted)
        {
            playing = true;

            dicPlayingCompleteDelegate[midiKey] = playingCompleted;

            if (!dicSequencerInitialized[midiKey])
            {
                dicSequencerInitialized[midiKey] = true;
                dicSequencer[midiKey].GetTracks();
            }
            dicSequencer[midiKey].Stop();
            dicSequencer[midiKey].Start();
        }

        public void Continue(string midiKey)
        {
            playing = true;
            dicSequencer[midiKey].Continue();
        }

        public void Stop(string midiKey)
        {
            playing = false;
            dicSequencer[midiKey].Stop();
        }

        public void StopAll()
        {
            foreach (var kv in dicSequencer)
            {
                kv.Value.Stop();
            }
        }

        public void DisposeAll()
        {
            StopAll();
            foreach (var s in dicSequence)
            {
                s.Value.Dispose();
            }

            if (outDevice != null)
            {
                outDevice.Dispose();
            }
        }

        #endregion methods

        #region events
        private void HandleChannelMessagePlayed(object sender, ChannelMessageEventArgs e)
        {
            if (closing)
            {
                return;
            }

            outDevice.Send(e.Message);
        }

        private void HandleChased(object sender, ChasedEventArgs e)
        {
            foreach (ChannelMessage message in e.Messages)
            {
                outDevice.Send(message);
            }
        }

        private void HandleSysExMessagePlayed(object sender, SysExMessageEventArgs e)
        {
            outDevice.Send(e.Message); //Sometimes causes an exception to be thrown because the output device is overloaded.
        }

        private void HandleStopped(object sender, StoppedEventArgs e)
        {
            foreach (ChannelMessage message in e.Messages)
            {
                outDevice.Send(message);
            }
        }

        private void HandleLoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (loadCompleted != null)
                loadCompleted();
        }

        #endregion events
    }
}

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
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions