/XSpriterTest/XSpriterTest/Game1.cs
C# | 266 lines | 169 code | 42 blank | 55 comment | 26 complexity | ade3addf355be2476ee279a2b0bf6abb MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using FuncWorks.XNA.XSpriter;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using PerformanceMeasuring.GameDebugTools;
-
- namespace XSpriterTest
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
-
- private CharacterAnimator character;
- private CharacterAnimator character2;
- private SpriteFont font;
- private Texture2D overlay;
- private int index = 0;
- private int frame = 0;
-
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
-
- /// <summary>
- /// 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.
- /// </summary>
- protected override void Initialize()
- {
- DebugSystem.Initialize(this, "SpriteFont1");
- DebugSystem.Instance.FpsCounter.Visible = true;
- base.Initialize();
- }
-
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
-
- overlay = Content.Load<Texture2D>("overlay_test");
-
- CharacterData cdata = Content.Load<CharacterData>("Hero-a2");
- character = cdata.GetCharacterAnimator();
- character2 = cdata.GetCharacterAnimator();
- //character = Content.Load<CharacterData>("Example");
- //character = Content.Load<CharacterData>("Bones");
- //character = Content.Load<CharacterData>("Scaling");
-
- // Monster transforms: rotate monster's head 180, double scale, move up
- //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = MathHelper.PiOver2, Position = Vector2.Zero, Scale = Vector2.One, TimelineId = 11 });
- //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = MathHelper.PiOver2, Position = Vector2.Zero, Scale = Vector2.One, TimelineId = 11 });
- //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = Vector2.Zero, Scale = Vector2.One * 2, TimelineId = 11 });
- //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = new Vector2(0, -100), Scale = Vector2.One, TimelineId = 11 });
-
- // Hide image
- //character.Animations[0].Transforms.Add(new RuntimeTransform() { TimelineId = 11, Hidden = true});
-
- //// Bone transforms:
- //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = MathHelper.Pi, Position = Vector2.Zero, Scale = Vector2.One, BoneName = "bone_001"});
- //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = Vector2.Zero, Scale = Vector2.One * 2, BoneName = "bone_001" });
- //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = new Vector2(150, 0), Scale = Vector2.One, BoneName = "bone_001" });
-
- //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = new Vector2(0, -200), Scale = Vector2.One, TimelineId = 0 });
- //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = Vector2.Zero, Scale = Vector2.One * 2, TimelineId = 2 });
- //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = new Vector2(150, 0), Scale = Vector2.One, TimelineId = 2 });
-
- // Hide bone
- //character.Animations[0].Transforms.Add(new RuntimeTransform() {TimelineId = 2, Hidden = true});
-
- character.Position = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.Center.X - 200, GraphicsDevice.Viewport.TitleSafeArea.Center.Y + 200);
- character.Playing = true;
- character.CurrentAnimation = 0;
- character.FlipY = false;
- character.FlipX = false;
-
- character2.Position = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.Center.X + 200, GraphicsDevice.Viewport.TitleSafeArea.Center.Y + 200);
- character2.Playing = true;
- character2.CurrentAnimation = 1;
- character2.FlipY = false;
- character2.FlipX = false;
-
- font = Content.Load<SpriteFont>("SpriteFont1");
- }
-
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
-
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input, and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Update(GameTime gameTime)
- {
- DebugSystem.Instance.TimeRuler.StartFrame();
- DebugSystem.Instance.TimeRuler.BeginMark("Update", Color.Blue);
-
- // Allows the game to exit
- GamePadState gp = GamePad.GetState(PlayerIndex.One);
-
- if (gp.Buttons.Back == ButtonState.Pressed)
- this.Exit();
-
- KeyboardState kb = Keyboard.GetState();
-
- // Cycle through information display: arrow up/down
- if (IsKeyPress(kb, lastKeyboard, Keys.Up))
- {
- index--;
- }
- if (IsKeyPress(kb, lastKeyboard, Keys.Down))
- {
- index++;
- }
-
- // Toggle playback: A or Space
- if (IsKeyPress(kb, lastKeyboard, Keys.Space) || IsButtonPress(gp, lastGamepad, Buttons.A))
- {
- character.Playing = !character.Playing;
- }
-
- // Advance frame: arrow/DPad left/right
- if (IsKeyPress(kb, lastKeyboard, Keys.Left) || IsButtonPress(gp, lastGamepad, Buttons.DPadLeft))
- {
- frame--;
- }
- if (IsKeyPress(kb, lastKeyboard, Keys.Right) || IsButtonPress(gp, lastGamepad, Buttons.DPadRight))
- {
- frame++;
- }
-
- // Flip X and Y: X or Y
- if (IsKeyPress(kb, lastKeyboard, Keys.X) || IsButtonPress(gp, lastGamepad, Buttons.X))
- {
- character.FlipX = !character.FlipX;
- }
- if (IsKeyPress(kb, lastKeyboard, Keys.Y) || IsButtonPress(gp, lastGamepad, Buttons.Y))
- {
- character.FlipY = !character.FlipY;
- }
-
- // Cycle animations: PgUp/PgDown or LB/RB
- if (IsKeyPress(kb, lastKeyboard, Keys.PageUp) || IsButtonPress(gp, lastGamepad, Buttons.LeftShoulder))
- {
- int anim = (int)MathHelper.Clamp(character.CurrentAnimation-1, 0, character.Data.Animations.Count - 1);;
- character.SetAnimation(anim);
- }
- if (IsKeyPress(kb, lastKeyboard, Keys.PageDown) || IsButtonPress(gp, lastGamepad, Buttons.RightShoulder))
- {
- int anim = (int)MathHelper.Clamp(character.CurrentAnimation + 1, 0, character.Data.Animations.Count - 1); ;
- character.SetAnimation(anim);
- }
-
- #if XBOX
- // Trigger possible garbage collections on XBox
- if (gp.IsButtonDown(Buttons.LeftTrigger))
- {
- int? x = character.Data.Animations[character.CurrentAnimation].GetTimelineIdByTextureName("mon_arms/shoulder_a.png");
- int? y = character.Data.Animations[character.CurrentAnimation].GetTimelineIdByBoneName("bone_001");
- }
- if (gp.IsButtonDown(Buttons.RightTrigger))
- {
- character.SetAnimation("Idle");
- }
- #endif
-
- lastKeyboard = kb;
- lastGamepad = gp;
-
- if (character.Playing)
- {
- frame = character.FrameIndex;
- }
- frame = (int)MathHelper.Clamp(frame, 0, character.Data.Animations[character.CurrentAnimation].Frames.Length - 1);
- index = (int)MathHelper.Clamp(index, 0, character.Data.Animations[character.CurrentAnimation].Frames[0].Objects.Length - 1);
-
- base.Update(gameTime);
-
- DebugSystem.Instance.TimeRuler.EndMark("Update");
- }
-
- private KeyboardState lastKeyboard = new KeyboardState();
- private GamePadState lastGamepad = new GamePadState();
-
- public bool IsKeyPress(KeyboardState current, KeyboardState previous, Keys key)
- {
- return current.IsKeyUp(key) && previous.IsKeyDown(key);
- }
-
- public bool IsButtonPress(GamePadState current, GamePadState previous, Buttons button)
- {
- return current.IsButtonUp(button) && previous.IsButtonDown(button);
- }
-
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw(GameTime gameTime)
- {
- DebugSystem.Instance.TimeRuler.BeginMark("Draw", Color.Red);
-
- GraphicsDevice.Clear(Color.CornflowerBlue);
-
- spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
- character.Update(gameTime);
- character2.Update(gameTime);
- if (!character.Playing) { character.FrameIndex = frame; }
- character.Draw(spriteBatch);
- character2.Draw(spriteBatch);
-
- // Add the overlay if we're using Example.scml
- int? timelineId = character.GetCurrentAnimation().GetTimelineIdByTextureName("mon_torso/torso_0.png");
- if (timelineId.HasValue)
- {
- RenderedPosition? pos = character.GetCurrentRenderedPositionForTimeline(timelineId.Value);
- if (pos.HasValue)
- {
- spriteBatch.Draw(overlay, pos.Value.Positon, null, Color.White, pos.Value.Angle, pos.Value.Pivot,
- pos.Value.Scale, pos.Value.Effects, pos.Value.Layer);
- }
- }
-
-
- #if !XBOX
- FrameImage k = character.Data.Animations[character.CurrentAnimation].Frames[frame].Objects[index];
- string debugInfo = String.Format("Frame: {6}\nIndex: {0}\nPosition: {1}, {2}\nPivot: {3}, {4}\nAngle: {5}\nCharacter Position: {7}, {8}", index, k.Position.X,
- k.Position.Y, k.Pivot.X, k.Pivot.Y, k.Angle, frame, character.Position.X, character.Position.Y);
- Vector2 position = new Vector2(0, GraphicsDevice.Viewport.TitleSafeArea.Bottom - font.MeasureString(debugInfo).Y);
- spriteBatch.DrawString(font, debugInfo, position, Color.White);
- #endif
-
- spriteBatch.End();
-
- base.Draw(gameTime);
-
- DebugSystem.Instance.TimeRuler.EndMark("Draw");
- }
- }
- }