/* * AnimationManager.cs * Authors: Karl Orosz * Copyright (c) 2007-2008 Cornell University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using System.Collections.Generic; using System.IO; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; namespace StarJack { public class AnimationManager { //Tokens for parsing private static string[] mSplitToken = new String[] { " " }; //private static String mCommentToken = "#"; private static String mAnimationToken = "@"; //Index values for the split line private const int NAME_INDEX = 1; private const int WIDTH_INDEX= 2 ; private const int HEIGHT_INDEX = 3; private const int FRAME_COUNT_INDEX = 4; private const int PREFFERED_FPS_INDEX = 5; //The animations directory private static String mAnimationLoc = @"\Animation"; //The animations manifest private static String mManifestLoc = @"\Manifest.txt"; private static String mContentDir = @"\Content\Texture"; private static String mRootDirectory; public static String RootDirectory { get { return mRootDirectory; } set { mRootDirectory = value; } } //the List of all the different animations private static Dictionary> mAnimations; /// /// Initializes the group listing /// static AnimationManager() { mAnimations = new Dictionary>(); mRootDirectory = Entity.RootDirectory; } //Removes all the animations from the list //Entities will need to be responsible dsiposing their animation infor as well, before the animationdata will be GC'd. public static void Dispose() { mAnimations.Clear(); } /// /// This loads a animation set from the specified group /// /// /// public static Dictionary GetAnimations(string groupName) { if (mAnimations.ContainsKey(groupName)) { return mAnimations[groupName]; } else { LoadAnimations(groupName); return mAnimations[groupName]; } } /// /// Loads a gorup of animations from that groups manifest /// /// /// public static Dictionary LoadAnimations(string groupname) { String dir = mRootDirectory + mContentDir + @"\" + groupname + mAnimationLoc + mManifestLoc; if (File.Exists(dir)) { mAnimations.Add(groupname, new Dictionary()); using (FileStream tManifest = new FileStream(dir, FileMode.Open, FileAccess.Read, FileShare.Read)) using (StreamReader mIn = new StreamReader(tManifest)) { String line; AnimationData tAnim; while (!mIn.EndOfStream) { line = mIn.ReadLine(); if (line.StartsWith(mAnimationToken)) { string[] tSplit = line.Split(mSplitToken, StringSplitOptions.RemoveEmptyEntries); tAnim = new AnimationData(); tAnim.Name = tSplit[NAME_INDEX]; tAnim.SpriteSheet.Name = tSplit[NAME_INDEX]; tAnim.Group = groupname; tAnim.SpriteSheet.TextureGroup = groupname; tAnim.SpriteSheet.SpriteSheet = true; tAnim.AnimationLocation = mContentDir + groupname + mAnimationLoc + @"\" + tSplit[NAME_INDEX]; tAnim.FrameCount = int.Parse(tSplit[FRAME_COUNT_INDEX]); tAnim.FrameSize = new Rectangle(0, 0, int.Parse(tSplit[WIDTH_INDEX]), int.Parse(tSplit[HEIGHT_INDEX])); tAnim.PrefferedFPS = float.Parse(tSplit[PREFFERED_FPS_INDEX]); mAnimations[groupname].Add(tAnim.Name, tAnim); } } } return mAnimations[groupname]; } return null; } /// /// Loads the content for all the animation groups /// /// public static void LoadContent(ContentManager pContent) { foreach (string key1 in mAnimations.Keys) { foreach (string key2 in mAnimations[key1].Keys) { mAnimations[key1][key2].LoadContent(pContent); } } } } }