Thursday, September 13, 2007

XNA & XAct sample

It's been a while, but here I am posting a very simple test program.
If you have 2 background cues, say ambience and battle, you could switch between them with this code.

Keep in mind that LoopEvent = Infinite only in the ambience and battle cues not in the transitions to and from ones.

Here is the code. If you want the exe and resources let me know and I"ll post it somewhere is some 20MB with my music.

This is my Game1.cs file

Code:
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace MusicTransition
{
///
/// This is the main type for your game
///
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;


public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}


///
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
///
///
AudioEngine audioEngine;
WaveBank waveBank;
SoundBank soundBank;
enum GameStates { Ambient, Amb2Bat, Battle, Bat2Amb };
GameStates CurrentState;
Cue Ambient;
Cue Battle;
Cue Amb2Bat;
Cue Bat2Amb;

protected override void Initialize()
{
audioEngine = new AudioEngine("Content\\Audio\\MusicTran.xgs");
waveBank = new WaveBank(audioEngine, "Content\\Audio\\Wave Bank.xwb");
soundBank = new SoundBank(audioEngine, "Content\\Audio\\Sound Bank.xsb");
CurrentState = GameStates.Ambient;
Ambient = soundBank.GetCue("Amb");
Battle = soundBank.GetCue("Bat");
Amb2Bat = soundBank.GetCue("Amb2Bat");
Bat2Amb = soundBank.GetCue("Bat2Amb");
Ambient.Play();

base.Initialize();
}

///
/// Load your graphics content. If loadAllContent is true, you should
/// load content from both ResourceManagementMode pools. Otherwise, just
/// load ResourceManagementMode.Manual content.
///
/// Which type of content to load.

SpriteBatch ForegroundBatch;
SpriteFont CourierNew;
Vector2 FontPos1;
float FontRotation1;
Vector2 FontPos2;
float FontRotation2;
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
CourierNew = content.Load("Times");
ForegroundBatch = new SpriteBatch(graphics.GraphicsDevice);
}
FontPos1 = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2, (graphics.GraphicsDevice.Viewport.Height / 2) - 50 );
FontRotation1 = 0;
FontPos2 = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2, (graphics.GraphicsDevice.Viewport.Height / 2) + 50);
FontRotation2 = 0;
}



///
/// Unload your graphics content. If unloadAllContent is true, you should
/// unload content from both ResourceManagementMode pools. Otherwise, just
/// unload ResourceManagementMode.Manual content. Manual content will get
/// Disposed by the GraphicsDevice during a Reset.
///
/// Which type of content to unload.
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent)
{
// TODO: Unload any ResourceManagementMode.Automatic content
content.Unload();
}

// TODO: Unload any ResourceManagementMode.Manual content
}


///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input and playing audio.
///
/// Provides a snapshot of timing values.
protected override void Update(GameTime gameTime)
{
FontRotation1 += 0.001F;
FontRotation2 += 0.01F;

switch (CurrentState)
{

case GameStates.Amb2Bat:
{
if (!Amb2Bat.IsPlaying)
{
CurrentState = GameStates.Battle;
Battle = soundBank.GetCue("Bat");
Battle.Play();
}
break;
}
case GameStates.Bat2Amb:
{
if (!Bat2Amb.IsPlaying)
{
CurrentState = GameStates.Ambient;
Ambient = soundBank.GetCue("Amb");
Ambient.Play();
}
break;
}
}

if (Keyboard.GetState().IsKeyDown(Keys.A))// user trigered
{
switch (CurrentState)
{
case GameStates.Ambient:
{
Ambient.Stop(AudioStopOptions.Immediate);
Amb2Bat = soundBank.GetCue("Amb2Bat");
Amb2Bat.Play();
CurrentState = GameStates.Amb2Bat;
break;
}

case GameStates.Battle:
{
Battle.Stop(AudioStopOptions.Immediate);
Bat2Amb = soundBank.GetCue("Bat2Amb");
Bat2Amb.Play();
CurrentState = GameStates.Bat2Amb;
break;
}
}
}
// Allows the game to exit
if (Keyboard.GetState().IsKeyDown(Keys.X))
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}


///
/// This is called when the game should draw itself.
///
/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
if(CurrentState == GameStates.Ambient)
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
else if (CurrentState == GameStates.Amb2Bat)
graphics.GraphicsDevice.Clear(Color.Purple);
else if (CurrentState == GameStates.Battle)
graphics.GraphicsDevice.Clear(Color.Red);
else
graphics.GraphicsDevice.Clear(Color.BlueViolet);
ForegroundBatch.Begin();

// Draw Hello World
string output = "© 2007 David Cortés Provencio, Software and Music.";

// Find the center of the string
Vector2 FontOrigin = CourierNew.MeasureString( output ) / 2;
// Draw the string
ForegroundBatch.DrawString( CourierNew, output, FontPos1, Color.White, FontRotation1, FontOrigin, 1.0f, SpriteEffects.None, 0.5f );

output = "Press 'A' to transition and 'X' to exit";

// Find the center of the string
FontOrigin = CourierNew.MeasureString( output ) / 2;
// Draw the string
ForegroundBatch.DrawString( CourierNew, output, FontPos2, Color.Black, FontRotation2, FontOrigin, 1.0f, SpriteEffects.None, 0.5f );

ForegroundBatch.End();


base.Draw(gameTime);
}
}
}