ExampleGame.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated April 5, 2025. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2025, 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. protected Point mousePos;
  40. public Screen (Example game) {
  41. this.game = game;
  42. skeletonRenderer = new SkeletonRenderer(game.GraphicsDevice);
  43. skeletonRenderer.PremultipliedAlpha = false;
  44. }
  45. public void UpdateInput () {
  46. MouseState state = Mouse.GetState();
  47. mouseClicked = lastMouseState.LeftButton == ButtonState.Pressed && state.LeftButton == ButtonState.Released;
  48. lastMouseState = state;
  49. mousePos = lastMouseState.Position;
  50. }
  51. public abstract void Render (float deltaTime);
  52. }
  53. /// <summary>
  54. /// The raptor screen shows basic loading and rendering of a Spine skeleton.
  55. /// </summary>
  56. internal class RaptorScreen : Screen {
  57. Atlas atlas;
  58. Skeleton skeleton;
  59. AnimationState state;
  60. public RaptorScreen (Example game) : base(game) {
  61. // Load the texture atlas
  62. atlas = new Atlas("data/raptor.atlas", new XnaTextureLoader(game.GraphicsDevice));
  63. // Load the .json file using a scale of 0.5
  64. SkeletonJson json = new SkeletonJson(atlas);
  65. json.Scale = 0.5f;
  66. SkeletonData skeletonData = json.ReadSkeletonData("data/raptor-pro.json");
  67. // Create the skeleton and animation state
  68. skeleton = new Skeleton(skeletonData);
  69. AnimationStateData stateData = new AnimationStateData(skeleton.Data);
  70. state = new AnimationState(stateData);
  71. // Center within the viewport
  72. skeleton.X = game.GraphicsDevice.Viewport.Width / 2;
  73. skeleton.Y = game.GraphicsDevice.Viewport.Height;
  74. // Set the "walk" animation on track one and let it loop forever
  75. state.SetAnimation(0, "walk", true);
  76. }
  77. public override void Render (float deltaTime) {
  78. // Update the animation state and apply the animations
  79. // to the skeleton
  80. state.Update(deltaTime);
  81. skeleton.Update(deltaTime);
  82. state.Apply(skeleton);
  83. // Update the transformations of bones and other parts of the skeleton
  84. skeleton.UpdateWorldTransform(Skeleton.Physics.Update);
  85. // Clear the screen and setup the projection matrix of the skeleton renderer
  86. game.GraphicsDevice.Clear(Color.Black);
  87. ((BasicEffect)skeletonRenderer.Effect).Projection = Matrix.CreateOrthographicOffCenter(0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height, 0, 1, 0);
  88. // Draw the skeletons
  89. skeletonRenderer.Begin();
  90. skeletonRenderer.Draw(skeleton);
  91. skeletonRenderer.End();
  92. // Check if the mouse button was clicked and switch scene
  93. if (mouseClicked) game.currentScreen = new TankScreen(game);
  94. }
  95. }
  96. /// <summary>
  97. /// The tank screen shows how to enable two color tinting.
  98. /// </summary>
  99. internal class TankScreen : Screen {
  100. Atlas atlas;
  101. Skeleton skeleton;
  102. AnimationState state;
  103. public TankScreen (Example game) : base(game) {
  104. // Instantiate and configure the two color tinting effect and
  105. // assign it to the skeleton renderer
  106. var twoColorTintEffect = game.Content.Load<Effect>("Content\\shaders\\SpineEffect");
  107. twoColorTintEffect.Parameters["World"].SetValue(Matrix.Identity);
  108. twoColorTintEffect.Parameters["View"].SetValue(Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, 1.0f), Vector3.Zero, Vector3.Up));
  109. skeletonRenderer.Effect = twoColorTintEffect;
  110. // The remaining code loads the atlas and skeleton data as in the raptor screen
  111. atlas = new Atlas("data/tank.atlas", new XnaTextureLoader(game.GraphicsDevice));
  112. SkeletonJson json = new SkeletonJson(atlas);
  113. json.Scale = 0.25f;
  114. SkeletonData skeletonData = json.ReadSkeletonData("data/tank-pro.json");
  115. skeleton = new Skeleton(skeletonData);
  116. AnimationStateData stateData = new AnimationStateData(skeleton.Data);
  117. state = new AnimationState(stateData);
  118. skeleton.X = game.GraphicsDevice.Viewport.Width / 2 + 200;
  119. skeleton.Y = game.GraphicsDevice.Viewport.Height;
  120. state.SetAnimation(0, "shoot", true);
  121. }
  122. public override void Render (float deltaTime) {
  123. state.Update(deltaTime);
  124. skeleton.Update(deltaTime);
  125. state.Apply(skeleton);
  126. skeleton.UpdateWorldTransform(Skeleton.Physics.Update);
  127. // Clear the screen and setup the projection matrix of the custom effect through the
  128. // "Projection" parameter.
  129. game.GraphicsDevice.Clear(Color.Black);
  130. skeletonRenderer.Effect.Parameters["Projection"].SetValue(Matrix.CreateOrthographicOffCenter(0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height, 0, 1, 0));
  131. skeletonRenderer.Begin();
  132. skeletonRenderer.Draw(skeleton);
  133. skeletonRenderer.End();
  134. if (mouseClicked) game.currentScreen = new SpineboyScreen(game);
  135. }
  136. }
  137. /// <summary>
  138. /// The Spineboy screen shows how to queue up multiple animations via animation state,
  139. /// set the default mix time to smoothly transition between animations, and load a
  140. /// skeleton from a binary .skel file.
  141. /// </summary>
  142. internal class SpineboyScreen : Screen {
  143. Atlas atlas;
  144. Skeleton skeleton;
  145. AnimationState state;
  146. public SpineboyScreen (Example game) : base(game) {
  147. atlas = new Atlas("data/spineboy.atlas", new XnaTextureLoader(game.GraphicsDevice));
  148. SkeletonBinary binary = new SkeletonBinary(atlas);
  149. binary.Scale = 0.5f;
  150. SkeletonData skeletonData = binary.ReadSkeletonData("data/spineboy-pro.skel");
  151. skeleton = new Skeleton(skeletonData);
  152. AnimationStateData stateData = new AnimationStateData(skeleton.Data);
  153. state = new AnimationState(stateData);
  154. skeleton.X = game.GraphicsDevice.Viewport.Width / 2;
  155. skeleton.Y = game.GraphicsDevice.Viewport.Height;
  156. // We want 0.2 seconds of mixing time when transitioning from
  157. // any animation to any other animation.
  158. stateData.DefaultMix = 0.2f;
  159. // Set the "walk" animation on track one and let it loop forever
  160. state.SetAnimation(0, "walk", true);
  161. // Queue another animation after 2 seconds to let Spineboy jump
  162. state.AddAnimation(0, "jump", false, 2);
  163. // After the jump is complete, let Spineboy walk
  164. state.AddAnimation(0, "run", true, 0);
  165. }
  166. public override void Render (float deltaTime) {
  167. state.Update(deltaTime);
  168. skeleton.Update(deltaTime);
  169. state.Apply(skeleton);
  170. skeleton.UpdateWorldTransform(Skeleton.Physics.Update);
  171. game.GraphicsDevice.Clear(Color.Black);
  172. ((BasicEffect)skeletonRenderer.Effect).Projection = Matrix.CreateOrthographicOffCenter(0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height, 0, 1, 0);
  173. skeletonRenderer.Begin();
  174. skeletonRenderer.Draw(skeleton);
  175. skeletonRenderer.End();
  176. if (mouseClicked) game.currentScreen = new PhysicsScreenCelestial(game);
  177. }
  178. }
  179. /// <summary>
  180. /// The mix-and-match screen demonstrates how to create and apply a skin
  181. /// composed of other skins. This method can be used to create customizable
  182. /// avatar systems.
  183. /// </summary>
  184. internal class MixAndMatchScreen : Screen {
  185. Atlas atlas;
  186. Skeleton skeleton;
  187. AnimationState state;
  188. public MixAndMatchScreen (Example game) : base(game) {
  189. atlas = new Atlas("data/mix-and-match.atlas", new XnaTextureLoader(game.GraphicsDevice));
  190. SkeletonJson json = new SkeletonJson(atlas);
  191. json.Scale = 0.5f;
  192. SkeletonData skeletonData = json.ReadSkeletonData("data/mix-and-match-pro.json");
  193. skeleton = new Skeleton(skeletonData);
  194. AnimationStateData stateData = new AnimationStateData(skeleton.Data);
  195. state = new AnimationState(stateData);
  196. skeleton.X = game.GraphicsDevice.Viewport.Width / 2;
  197. skeleton.Y = game.GraphicsDevice.Viewport.Height;
  198. state.SetAnimation(0, "dance", true);
  199. // Create a new skin, by mixing and matching other skins
  200. // that fit together. Items making up the girl are individual
  201. // skins. Using the skin API, a new skin is created which is
  202. // a combination of all these individual item skins.
  203. var mixAndMatchSkin = new Spine.Skin("custom-girl");
  204. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("skin-base"));
  205. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("nose/short"));
  206. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("eyelids/girly"));
  207. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("eyes/violet"));
  208. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("hair/brown"));
  209. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("clothes/hoodie-orange"));
  210. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("legs/pants-jeans"));
  211. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("accessories/bag"));
  212. mixAndMatchSkin.AddSkin(skeletonData.FindSkin("accessories/hat-red-yellow"));
  213. skeleton.SetSkin(mixAndMatchSkin);
  214. }
  215. public override void Render (float deltaTime) {
  216. state.Update(deltaTime);
  217. skeleton.Update(deltaTime);
  218. state.Apply(skeleton);
  219. skeleton.UpdateWorldTransform(Skeleton.Physics.Update);
  220. game.GraphicsDevice.Clear(Color.Black);
  221. ((BasicEffect)skeletonRenderer.Effect).Projection = Matrix.CreateOrthographicOffCenter(0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height, 0, 1, 0);
  222. skeletonRenderer.Begin();
  223. skeletonRenderer.Draw(skeleton);
  224. skeletonRenderer.End();
  225. if (mouseClicked) game.currentScreen = new RaptorScreen(game);
  226. }
  227. }
  228. /// <summary>
  229. /// The physics screen Cloud Pot demonstrates Physics Constraints introduced in Spine 4.2
  230. /// using the cloud-pot skeleton.
  231. /// </summary>
  232. internal class PhysicsScreenCloudPot : Screen {
  233. Atlas atlas;
  234. Skeleton skeleton;
  235. AnimationState state;
  236. public PhysicsScreenCloudPot (Example game) : base(game) {
  237. atlas = new Atlas("data/cloud-pot.atlas", new XnaTextureLoader(game.GraphicsDevice));
  238. SkeletonBinary binary = new SkeletonBinary(atlas);
  239. binary.Scale = 0.15f;
  240. SkeletonData skeletonData = binary.ReadSkeletonData("data/cloud-pot.skel");
  241. skeleton = new Skeleton(skeletonData);
  242. AnimationStateData stateData = new AnimationStateData(skeleton.Data);
  243. state = new AnimationState(stateData);
  244. skeleton.X = game.GraphicsDevice.Viewport.Width / 2;
  245. skeleton.Y = game.GraphicsDevice.Viewport.Height * 2f / 3f;
  246. state.SetAnimation(0, "playing-in-the-rain", true);
  247. }
  248. public override void Render (float deltaTime) {
  249. Vector2 position = mousePos.ToVector2();
  250. skeleton.X = position.X;
  251. skeleton.Y = position.Y;
  252. state.Update(deltaTime);
  253. skeleton.Update(deltaTime);
  254. // Note: if you are not directly modifying skeleton.X or .Y, you can apply external
  255. // movement to physics via the following code:
  256. // Vector2 lastPosition; // add as a member variable
  257. // ..
  258. // Vector2 currentPosition = <current world position>;
  259. // Vector2 externalPositionDelta = currentPosition - lastPosition;
  260. // skeleton.PhysicsTranslate(externalPositionDelta.x, externalPositionDelta.y);
  261. // lastPosition = currentPosition;
  262. state.Apply(skeleton);
  263. skeleton.UpdateWorldTransform(Skeleton.Physics.Update);
  264. game.GraphicsDevice.Clear(Color.Black);
  265. ((BasicEffect)skeletonRenderer.Effect).Projection = Matrix.CreateOrthographicOffCenter(0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height, 0, 1, 0);
  266. skeletonRenderer.Begin();
  267. skeletonRenderer.Draw(skeleton);
  268. skeletonRenderer.End();
  269. if (mouseClicked) game.currentScreen = new MixAndMatchScreen(game);
  270. }
  271. }
  272. /// <summary>
  273. /// The physics screen Celestial demonstrates Physics Constraints introduced in Spine 4.2
  274. /// using the celestial-circus skeleton.
  275. /// </summary>
  276. internal class PhysicsScreenCelestial : Screen {
  277. Atlas atlas;
  278. Skeleton skeleton;
  279. AnimationState state;
  280. public PhysicsScreenCelestial (Example game) : base(game) {
  281. atlas = new Atlas("data/celestial-circus.atlas", new XnaTextureLoader(game.GraphicsDevice));
  282. SkeletonJson json = new SkeletonJson(atlas);
  283. json.Scale = 0.15f;
  284. SkeletonData skeletonData = json.ReadSkeletonData("data/celestial-circus-pro.json");
  285. skeleton = new Skeleton(skeletonData);
  286. AnimationStateData stateData = new AnimationStateData(skeleton.Data);
  287. state = new AnimationState(stateData);
  288. skeleton.X = game.GraphicsDevice.Viewport.Width / 2;
  289. skeleton.Y = game.GraphicsDevice.Viewport.Height * 2f / 3f;
  290. state.SetAnimation(0, "swing", true);
  291. state.SetAnimation(1, "eyeblink", true);
  292. }
  293. public override void Render (float deltaTime) {
  294. Vector2 position = mousePos.ToVector2();
  295. skeleton.X = position.X;
  296. skeleton.Y = position.Y;
  297. state.Update(deltaTime);
  298. skeleton.Update(deltaTime);
  299. // Note: if you are not directly modifying skeleton.X or .Y, you can apply external
  300. // movement to physics via the following code:
  301. // Vector2 lastPosition; // add as a member variable
  302. // ..
  303. // Vector2 currentPosition = <current world position>;
  304. // Vector2 externalPositionDelta = currentPosition - lastPosition;
  305. // skeleton.PhysicsTranslate(externalPositionDelta.x, externalPositionDelta.y);
  306. // lastPosition = currentPosition;
  307. state.Apply(skeleton);
  308. skeleton.UpdateWorldTransform(Skeleton.Physics.Update);
  309. game.GraphicsDevice.Clear(Color.Black);
  310. ((BasicEffect)skeletonRenderer.Effect).Projection = Matrix.CreateOrthographicOffCenter(0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height, 0, 1, 0);
  311. skeletonRenderer.Begin();
  312. skeletonRenderer.Draw(skeleton);
  313. skeletonRenderer.End();
  314. if (mouseClicked) game.currentScreen = new PhysicsScreenCloudPot(game);
  315. }
  316. }
  317. public class Example : Microsoft.Xna.Framework.Game {
  318. GraphicsDeviceManager graphics;
  319. public Screen currentScreen;
  320. public Example () {
  321. IsMouseVisible = true;
  322. graphics = new GraphicsDeviceManager(this);
  323. graphics.IsFullScreen = false;
  324. graphics.PreferredBackBufferWidth = 800;
  325. graphics.PreferredBackBufferHeight = 600;
  326. }
  327. protected override void LoadContent () {
  328. currentScreen = new PhysicsScreenCelestial(this);
  329. }
  330. protected override void Update (GameTime gameTime) {
  331. currentScreen.UpdateInput();
  332. }
  333. protected override void Draw (GameTime gameTime) {
  334. currentScreen.Render((float)(gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0));
  335. }
  336. }
  337. }