ExampleGame.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using Microsoft.Xna.Framework;
  30. using Microsoft.Xna.Framework.Graphics;
  31. using Microsoft.Xna.Framework.Input;
  32. using System;
  33. namespace Spine {
  34. public abstract class Screen {
  35. protected Example game;
  36. protected SkeletonRenderer skeletonRenderer;
  37. private MouseState lastMouseState;
  38. protected Boolean mouseClicked = false;
  39. public Screen (Example game) {
  40. this.game = game;
  41. skeletonRenderer = new SkeletonRenderer(game.GraphicsDevice);
  42. skeletonRenderer.PremultipliedAlpha = false;
  43. }
  44. public void UpdateInput () {
  45. MouseState state = Mouse.GetState();
  46. mouseClicked = lastMouseState.LeftButton == ButtonState.Pressed && state.LeftButton == ButtonState.Released;
  47. lastMouseState = state;
  48. }
  49. public abstract void Render (float deltaTime);
  50. }
  51. /// <summary>
  52. /// The raptor screen shows basic loading and rendering of a Spine skeleton.
  53. /// </summary>
  54. internal class RaptorScreen : Screen {
  55. Atlas atlas;
  56. Skeleton skeleton;
  57. AnimationState state;
  58. public RaptorScreen (Example game) : base(game) {
  59. // Load the texture atlas
  60. atlas = new Atlas("data/raptor.atlas", new XnaTextureLoader(game.GraphicsDevice));
  61. // Load the .json file using a scale of 0.5
  62. SkeletonJson json = new SkeletonJson(atlas);
  63. json.Scale = 0.5f;
  64. SkeletonData skeletonData = json.ReadSkeletonData("data/raptor-pro.json");
  65. // Create the skeleton and animation state
  66. skeleton = new Skeleton(skeletonData);
  67. AnimationStateData stateData = new AnimationStateData(skeleton.Data);
  68. state = new AnimationState(stateData);
  69. // Center within the viewport
  70. skeleton.X = game.GraphicsDevice.Viewport.Width / 2;
  71. skeleton.Y = game.GraphicsDevice.Viewport.Height;
  72. // Set the "walk" animation on track one and let it loop forever
  73. state.SetAnimation(0, "walk", true);
  74. }
  75. public override void Render (float deltaTime) {
  76. // Update the animation state and apply the animations
  77. // to the skeleton
  78. state.Update(deltaTime);
  79. state.Apply(skeleton);
  80. // Update the transformations of bones and other parts of the skeleton
  81. skeleton.UpdateWorldTransform();
  82. // Clear the screen and setup the projection matrix of the skeleton renderer
  83. game.GraphicsDevice.Clear(Color.Black);
  84. ((BasicEffect)skeletonRenderer.Effect).Projection = Matrix.CreateOrthographicOffCenter(0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height, 0, 1, 0);
  85. // Draw the skeletons
  86. skeletonRenderer.Begin();
  87. skeletonRenderer.Draw(skeleton);
  88. skeletonRenderer.End();
  89. // Check if the mouse button was clicked and switch scene
  90. if (mouseClicked) game.currentScreen = new TankScreen(game);
  91. }
  92. }
  93. /// <summary>
  94. /// The tank screen shows how to enable two color tinting.
  95. /// </summary>
  96. internal class TankScreen : Screen {
  97. Atlas atlas;
  98. Skeleton skeleton;
  99. AnimationState state;
  100. public TankScreen (Example game) : base(game) {
  101. // Instantiate and configure the two color tinting effect and
  102. // assign it to the skeleton renderer
  103. var twoColorTintEffect = game.Content.Load<Effect>("Content\\SpineEffect");
  104. twoColorTintEffect.Parameters["World"].SetValue(Matrix.Identity);
  105. twoColorTintEffect.Parameters["View"].SetValue(Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, 1.0f), Vector3.Zero, Vector3.Up));
  106. skeletonRenderer.Effect = twoColorTintEffect;
  107. // The remaining code loads the atlas and skeleton data as in the raptor screen
  108. atlas = new Atlas("data/tank.atlas", new XnaTextureLoader(game.GraphicsDevice));
  109. SkeletonJson json = new SkeletonJson(atlas);
  110. json.Scale = 0.25f;
  111. SkeletonData skeletonData = json.ReadSkeletonData("data/tank-pro.json");
  112. skeleton = new Skeleton(skeletonData);
  113. AnimationStateData stateData = new AnimationStateData(skeleton.Data);
  114. state = new AnimationState(stateData);
  115. skeleton.X = game.GraphicsDevice.Viewport.Width / 2 + 200;
  116. skeleton.Y = game.GraphicsDevice.Viewport.Height;
  117. state.SetAnimation(0, "shoot", true);
  118. }
  119. public override void Render (float deltaTime) {
  120. state.Update(deltaTime);
  121. state.Apply(skeleton);
  122. skeleton.UpdateWorldTransform();
  123. // Clear the screen and setup the projection matrix of the custom effect through the
  124. // "Projection" parameter.
  125. game.GraphicsDevice.Clear(Color.Black);
  126. skeletonRenderer.Effect.Parameters["Projection"].SetValue(Matrix.CreateOrthographicOffCenter(0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height, 0, 1, 0));
  127. skeletonRenderer.Begin();
  128. skeletonRenderer.Draw(skeleton);
  129. skeletonRenderer.End();
  130. if (mouseClicked) game.currentScreen = new SpineboyScreen(game);
  131. }
  132. }
  133. /// <summary>
  134. /// The Spineboy screen shows how to queue up multiple animations via animation state,
  135. /// set the default mix time to smoothly transition between animations, and load a
  136. /// skeleton from a binary .skel file.
  137. /// </summary>
  138. internal class SpineboyScreen : Screen {
  139. Atlas atlas;
  140. Skeleton skeleton;
  141. AnimationState state;
  142. public SpineboyScreen (Example game) : base(game) {
  143. atlas = new Atlas("data/spineboy.atlas", new XnaTextureLoader(game.GraphicsDevice));
  144. SkeletonBinary binary = new SkeletonBinary(atlas);
  145. binary.Scale = 0.5f;
  146. SkeletonData skeletonData = binary.ReadSkeletonData("data/spineboy-pro.skel");
  147. skeleton = new Skeleton(skeletonData);
  148. AnimationStateData stateData = new AnimationStateData(skeleton.Data);
  149. state = new AnimationState(stateData);
  150. skeleton.X = game.GraphicsDevice.Viewport.Width / 2;
  151. skeleton.Y = game.GraphicsDevice.Viewport.Height;
  152. // We want 0.2 seconds of mixing time when transitioning from
  153. // any animation to any other animation.
  154. stateData.DefaultMix = 0.2f;
  155. // Set the "walk" animation on track one and let it loop forever
  156. state.SetAnimation(0, "walk", true);
  157. // Queue another animation after 2 seconds to let Spineboy jump
  158. state.AddAnimation(0, "jump", false, 2);
  159. // After the jump is complete, let Spineboy walk
  160. state.AddAnimation(0, "run", true, 0);
  161. }
  162. public override void Render (float deltaTime) {
  163. state.Update(deltaTime);
  164. state.Apply(skeleton);
  165. skeleton.UpdateWorldTransform();
  166. game.GraphicsDevice.Clear(Color.Black);
  167. ((BasicEffect)skeletonRenderer.Effect).Projection = Matrix.CreateOrthographicOffCenter(0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height, 0, 1, 0);
  168. skeletonRenderer.Begin();
  169. skeletonRenderer.Draw(skeleton);
  170. skeletonRenderer.End();
  171. if (mouseClicked) game.currentScreen = new MixAndMatchScreen(game);
  172. }
  173. }
  174. /// <summary>
  175. /// The mix-and-match screen demonstrates how to create and apply a skin
  176. /// composed of other skins. This method can be used to create customizable
  177. /// avatar systems.
  178. /// </summary>
  179. internal class MixAndMatchScreen : Screen {
  180. Atlas atlas;
  181. Skeleton skeleton;
  182. AnimationState state;
  183. public MixAndMatchScreen (Example game) : base(game) {
  184. atlas = new Atlas("data/mix-and-match.atlas", new XnaTextureLoader(game.GraphicsDevice));
  185. SkeletonJson json = new SkeletonJson(atlas);
  186. json.Scale = 0.5f;
  187. SkeletonData skeletonData = json.ReadSkeletonData("data/mix-and-match-pro.json");
  188. skeleton = new Skeleton(skeletonData);
  189. AnimationStateData stateData = new AnimationStateData(skeleton.Data);
  190. state = new AnimationState(stateData);
  191. skeleton.X = game.GraphicsDevice.Viewport.Width / 2;
  192. skeleton.Y = game.GraphicsDevice.Viewport.Height;
  193. state.SetAnimation(0, "dance", true);
  194. // Create a new skin, by mixing and matching other skins
  195. // that fit together. Items making up the girl are individual
  196. // skins. Using the skin API, a new skin is created which is
  197. // a combination of all these individual item skins.
  198. var mixAndMatchSkin = new Spine.Skin("custom-girl");
  199. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("skin-base"));
  200. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("nose/short"));
  201. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("eyelids/girly"));
  202. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("eyes/violet"));
  203. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("hair/brown"));
  204. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("clothes/hoodie-orange"));
  205. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("legs/pants-jeans"));
  206. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("accessories/bag"));
  207. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("accessories/hat-red-yellow"));
  208. skeleton.SetSkin(mixAndMatchSkin);
  209. }
  210. public override void Render (float deltaTime) {
  211. state.Update(deltaTime);
  212. state.Apply(skeleton);
  213. skeleton.UpdateWorldTransform();
  214. game.GraphicsDevice.Clear(Color.Black);
  215. ((BasicEffect)skeletonRenderer.Effect).Projection = Matrix.CreateOrthographicOffCenter(0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height, 0, 1, 0);
  216. skeletonRenderer.Begin();
  217. skeletonRenderer.Draw(skeleton);
  218. skeletonRenderer.End();
  219. if (mouseClicked) game.currentScreen = new RaptorScreen(game);
  220. }
  221. }
  222. public class Example : Microsoft.Xna.Framework.Game {
  223. GraphicsDeviceManager graphics;
  224. public Screen currentScreen;
  225. public Example () {
  226. IsMouseVisible = true;
  227. graphics = new GraphicsDeviceManager(this);
  228. graphics.IsFullScreen = false;
  229. graphics.PreferredBackBufferWidth = 800;
  230. graphics.PreferredBackBufferHeight = 600;
  231. }
  232. protected override void LoadContent () {
  233. currentScreen = new MixAndMatchScreen(this);
  234. }
  235. protected override void Update (GameTime gameTime) {
  236. currentScreen.UpdateInput();
  237. }
  238. protected override void Draw (GameTime gameTime) {
  239. currentScreen.Render((float)(gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0));
  240. }
  241. }
  242. }