PageRenderTime 55ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/XSpriterTest/XSpriterTest/Game1.cs

https://bitbucket.org/dylanwolf/xspriter
C# | 266 lines | 169 code | 42 blank | 55 comment | 26 complexity | ade3addf355be2476ee279a2b0bf6abb MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using FuncWorks.XNA.XSpriter;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Audio;
  8. using Microsoft.Xna.Framework.Content;
  9. using Microsoft.Xna.Framework.GamerServices;
  10. using Microsoft.Xna.Framework.Graphics;
  11. using Microsoft.Xna.Framework.Input;
  12. using Microsoft.Xna.Framework.Media;
  13. using PerformanceMeasuring.GameDebugTools;
  14. namespace XSpriterTest
  15. {
  16. /// <summary>
  17. /// This is the main type for your game
  18. /// </summary>
  19. public class Game1 : Microsoft.Xna.Framework.Game
  20. {
  21. GraphicsDeviceManager graphics;
  22. SpriteBatch spriteBatch;
  23. private CharacterAnimator character;
  24. private CharacterAnimator character2;
  25. private SpriteFont font;
  26. private Texture2D overlay;
  27. private int index = 0;
  28. private int frame = 0;
  29. public Game1()
  30. {
  31. graphics = new GraphicsDeviceManager(this);
  32. Content.RootDirectory = "Content";
  33. }
  34. /// <summary>
  35. /// Allows the game to perform any initialization it needs to before starting to run.
  36. /// This is where it can query for any required services and load any non-graphic
  37. /// related content. Calling base.Initialize will enumerate through any components
  38. /// and initialize them as well.
  39. /// </summary>
  40. protected override void Initialize()
  41. {
  42. DebugSystem.Initialize(this, "SpriteFont1");
  43. DebugSystem.Instance.FpsCounter.Visible = true;
  44. base.Initialize();
  45. }
  46. /// <summary>
  47. /// LoadContent will be called once per game and is the place to load
  48. /// all of your content.
  49. /// </summary>
  50. protected override void LoadContent()
  51. {
  52. // Create a new SpriteBatch, which can be used to draw textures.
  53. spriteBatch = new SpriteBatch(GraphicsDevice);
  54. overlay = Content.Load<Texture2D>("overlay_test");
  55. CharacterData cdata = Content.Load<CharacterData>("Hero-a2");
  56. character = cdata.GetCharacterAnimator();
  57. character2 = cdata.GetCharacterAnimator();
  58. //character = Content.Load<CharacterData>("Example");
  59. //character = Content.Load<CharacterData>("Bones");
  60. //character = Content.Load<CharacterData>("Scaling");
  61. // Monster transforms: rotate monster's head 180, double scale, move up
  62. //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = MathHelper.PiOver2, Position = Vector2.Zero, Scale = Vector2.One, TimelineId = 11 });
  63. //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = MathHelper.PiOver2, Position = Vector2.Zero, Scale = Vector2.One, TimelineId = 11 });
  64. //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = Vector2.Zero, Scale = Vector2.One * 2, TimelineId = 11 });
  65. //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = new Vector2(0, -100), Scale = Vector2.One, TimelineId = 11 });
  66. // Hide image
  67. //character.Animations[0].Transforms.Add(new RuntimeTransform() { TimelineId = 11, Hidden = true});
  68. //// Bone transforms:
  69. //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = MathHelper.Pi, Position = Vector2.Zero, Scale = Vector2.One, BoneName = "bone_001"});
  70. //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = Vector2.Zero, Scale = Vector2.One * 2, BoneName = "bone_001" });
  71. //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = new Vector2(150, 0), Scale = Vector2.One, BoneName = "bone_001" });
  72. //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = new Vector2(0, -200), Scale = Vector2.One, TimelineId = 0 });
  73. //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = Vector2.Zero, Scale = Vector2.One * 2, TimelineId = 2 });
  74. //character.Animations[0].Transforms.Add(new RuntimeTransform() { Angle = 0, Position = new Vector2(150, 0), Scale = Vector2.One, TimelineId = 2 });
  75. // Hide bone
  76. //character.Animations[0].Transforms.Add(new RuntimeTransform() {TimelineId = 2, Hidden = true});
  77. character.Position = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.Center.X - 200, GraphicsDevice.Viewport.TitleSafeArea.Center.Y + 200);
  78. character.Playing = true;
  79. character.CurrentAnimation = 0;
  80. character.FlipY = false;
  81. character.FlipX = false;
  82. character2.Position = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.Center.X + 200, GraphicsDevice.Viewport.TitleSafeArea.Center.Y + 200);
  83. character2.Playing = true;
  84. character2.CurrentAnimation = 1;
  85. character2.FlipY = false;
  86. character2.FlipX = false;
  87. font = Content.Load<SpriteFont>("SpriteFont1");
  88. }
  89. /// <summary>
  90. /// UnloadContent will be called once per game and is the place to unload
  91. /// all content.
  92. /// </summary>
  93. protected override void UnloadContent()
  94. {
  95. // TODO: Unload any non ContentManager content here
  96. }
  97. /// <summary>
  98. /// Allows the game to run logic such as updating the world,
  99. /// checking for collisions, gathering input, and playing audio.
  100. /// </summary>
  101. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  102. protected override void Update(GameTime gameTime)
  103. {
  104. DebugSystem.Instance.TimeRuler.StartFrame();
  105. DebugSystem.Instance.TimeRuler.BeginMark("Update", Color.Blue);
  106. // Allows the game to exit
  107. GamePadState gp = GamePad.GetState(PlayerIndex.One);
  108. if (gp.Buttons.Back == ButtonState.Pressed)
  109. this.Exit();
  110. KeyboardState kb = Keyboard.GetState();
  111. // Cycle through information display: arrow up/down
  112. if (IsKeyPress(kb, lastKeyboard, Keys.Up))
  113. {
  114. index--;
  115. }
  116. if (IsKeyPress(kb, lastKeyboard, Keys.Down))
  117. {
  118. index++;
  119. }
  120. // Toggle playback: A or Space
  121. if (IsKeyPress(kb, lastKeyboard, Keys.Space) || IsButtonPress(gp, lastGamepad, Buttons.A))
  122. {
  123. character.Playing = !character.Playing;
  124. }
  125. // Advance frame: arrow/DPad left/right
  126. if (IsKeyPress(kb, lastKeyboard, Keys.Left) || IsButtonPress(gp, lastGamepad, Buttons.DPadLeft))
  127. {
  128. frame--;
  129. }
  130. if (IsKeyPress(kb, lastKeyboard, Keys.Right) || IsButtonPress(gp, lastGamepad, Buttons.DPadRight))
  131. {
  132. frame++;
  133. }
  134. // Flip X and Y: X or Y
  135. if (IsKeyPress(kb, lastKeyboard, Keys.X) || IsButtonPress(gp, lastGamepad, Buttons.X))
  136. {
  137. character.FlipX = !character.FlipX;
  138. }
  139. if (IsKeyPress(kb, lastKeyboard, Keys.Y) || IsButtonPress(gp, lastGamepad, Buttons.Y))
  140. {
  141. character.FlipY = !character.FlipY;
  142. }
  143. // Cycle animations: PgUp/PgDown or LB/RB
  144. if (IsKeyPress(kb, lastKeyboard, Keys.PageUp) || IsButtonPress(gp, lastGamepad, Buttons.LeftShoulder))
  145. {
  146. int anim = (int)MathHelper.Clamp(character.CurrentAnimation-1, 0, character.Data.Animations.Count - 1);;
  147. character.SetAnimation(anim);
  148. }
  149. if (IsKeyPress(kb, lastKeyboard, Keys.PageDown) || IsButtonPress(gp, lastGamepad, Buttons.RightShoulder))
  150. {
  151. int anim = (int)MathHelper.Clamp(character.CurrentAnimation + 1, 0, character.Data.Animations.Count - 1); ;
  152. character.SetAnimation(anim);
  153. }
  154. #if XBOX
  155. // Trigger possible garbage collections on XBox
  156. if (gp.IsButtonDown(Buttons.LeftTrigger))
  157. {
  158. int? x = character.Data.Animations[character.CurrentAnimation].GetTimelineIdByTextureName("mon_arms/shoulder_a.png");
  159. int? y = character.Data.Animations[character.CurrentAnimation].GetTimelineIdByBoneName("bone_001");
  160. }
  161. if (gp.IsButtonDown(Buttons.RightTrigger))
  162. {
  163. character.SetAnimation("Idle");
  164. }
  165. #endif
  166. lastKeyboard = kb;
  167. lastGamepad = gp;
  168. if (character.Playing)
  169. {
  170. frame = character.FrameIndex;
  171. }
  172. frame = (int)MathHelper.Clamp(frame, 0, character.Data.Animations[character.CurrentAnimation].Frames.Length - 1);
  173. index = (int)MathHelper.Clamp(index, 0, character.Data.Animations[character.CurrentAnimation].Frames[0].Objects.Length - 1);
  174. base.Update(gameTime);
  175. DebugSystem.Instance.TimeRuler.EndMark("Update");
  176. }
  177. private KeyboardState lastKeyboard = new KeyboardState();
  178. private GamePadState lastGamepad = new GamePadState();
  179. public bool IsKeyPress(KeyboardState current, KeyboardState previous, Keys key)
  180. {
  181. return current.IsKeyUp(key) && previous.IsKeyDown(key);
  182. }
  183. public bool IsButtonPress(GamePadState current, GamePadState previous, Buttons button)
  184. {
  185. return current.IsButtonUp(button) && previous.IsButtonDown(button);
  186. }
  187. /// <summary>
  188. /// This is called when the game should draw itself.
  189. /// </summary>
  190. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  191. protected override void Draw(GameTime gameTime)
  192. {
  193. DebugSystem.Instance.TimeRuler.BeginMark("Draw", Color.Red);
  194. GraphicsDevice.Clear(Color.CornflowerBlue);
  195. spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
  196. character.Update(gameTime);
  197. character2.Update(gameTime);
  198. if (!character.Playing) { character.FrameIndex = frame; }
  199. character.Draw(spriteBatch);
  200. character2.Draw(spriteBatch);
  201. // Add the overlay if we're using Example.scml
  202. int? timelineId = character.GetCurrentAnimation().GetTimelineIdByTextureName("mon_torso/torso_0.png");
  203. if (timelineId.HasValue)
  204. {
  205. RenderedPosition? pos = character.GetCurrentRenderedPositionForTimeline(timelineId.Value);
  206. if (pos.HasValue)
  207. {
  208. spriteBatch.Draw(overlay, pos.Value.Positon, null, Color.White, pos.Value.Angle, pos.Value.Pivot,
  209. pos.Value.Scale, pos.Value.Effects, pos.Value.Layer);
  210. }
  211. }
  212. #if !XBOX
  213. FrameImage k = character.Data.Animations[character.CurrentAnimation].Frames[frame].Objects[index];
  214. string debugInfo = String.Format("Frame: {6}\nIndex: {0}\nPosition: {1}, {2}\nPivot: {3}, {4}\nAngle: {5}\nCharacter Position: {7}, {8}", index, k.Position.X,
  215. k.Position.Y, k.Pivot.X, k.Pivot.Y, k.Angle, frame, character.Position.X, character.Position.Y);
  216. Vector2 position = new Vector2(0, GraphicsDevice.Viewport.TitleSafeArea.Bottom - font.MeasureString(debugInfo).Y);
  217. spriteBatch.DrawString(font, debugInfo, position, Color.White);
  218. #endif
  219. spriteBatch.End();
  220. base.Draw(gameTime);
  221. DebugSystem.Instance.TimeRuler.EndMark("Draw");
  222. }
  223. }
  224. }