SimpleTest1.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. package com.esotericsoftware.spine;
  30. import com.badlogic.gdx.ApplicationAdapter;
  31. import com.badlogic.gdx.Gdx;
  32. import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
  33. import com.badlogic.gdx.graphics.GL20;
  34. import com.badlogic.gdx.graphics.OrthographicCamera;
  35. import com.badlogic.gdx.graphics.g2d.TextureAtlas;
  36. import com.esotericsoftware.spine.utils.TwoColorPolygonBatch;
  37. public class SimpleTest1 extends ApplicationAdapter {
  38. OrthographicCamera camera;
  39. TwoColorPolygonBatch batch;
  40. SkeletonRenderer renderer;
  41. SkeletonRendererDebug debugRenderer;
  42. TextureAtlas atlas;
  43. Skeleton skeleton;
  44. AnimationState state;
  45. public void create () {
  46. camera = new OrthographicCamera();
  47. batch = new TwoColorPolygonBatch();
  48. renderer = new SkeletonRenderer();
  49. renderer.setPremultipliedAlpha(true); // PMA results in correct blending without outlines.
  50. debugRenderer = new SkeletonRendererDebug();
  51. debugRenderer.setBoundingBoxes(false);
  52. debugRenderer.setRegionAttachments(false);
  53. atlas = new TextureAtlas(Gdx.files.internal("spineboy/spineboy-pma.atlas"));
  54. SkeletonBinary json = new SkeletonBinary(atlas); // This loads skeleton JSON data, which is stateless.
  55. json.setScale(0.6f); // Load the skeleton at 60% the size it was in Spine.
  56. SkeletonData skeletonData = json.readSkeletonData(Gdx.files.internal("spineboy/spineboy-pro.skel"));
  57. skeleton = new Skeleton(skeletonData); // Skeleton holds skeleton state (bone positions, slot attachments, etc).
  58. skeleton.setPosition(250, 20);
  59. AnimationStateData stateData = new AnimationStateData(skeletonData); // Defines mixing (crossfading) between animations.
  60. stateData.setMix("run", "jump", 0.2f);
  61. stateData.setMix("jump", "run", 0.2f);
  62. state = new AnimationState(stateData); // Holds the animation state for a skeleton (current animation, time, etc).
  63. state.setTimeScale(0.5f); // Slow all animations down to 50% speed.
  64. // Queue animations on track 0.
  65. state.setAnimation(0, "run", true);
  66. state.addAnimation(0, "jump", false, 2); // Jump after 2 seconds.
  67. state.addAnimation(0, "run", true, 0); // Run after the jump.
  68. }
  69. public void render () {
  70. state.update(Gdx.graphics.getDeltaTime()); // Update the animation time.
  71. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  72. state.apply(skeleton); // Poses skeleton using current animations. This sets the bones' local SRT.
  73. skeleton.updateWorldTransform(); // Uses the bones' local SRT to compute their world SRT.
  74. // Configure the camera, SpriteBatch, and SkeletonRendererDebug.
  75. camera.update();
  76. batch.getProjectionMatrix().set(camera.combined);
  77. debugRenderer.getShapeRenderer().setProjectionMatrix(camera.combined);
  78. batch.begin();
  79. renderer.draw(batch, skeleton); // Draw the skeleton images.
  80. batch.end();
  81. debugRenderer.draw(skeleton); // Draw debug lines.
  82. }
  83. public void resize (int width, int height) {
  84. camera.setToOrtho(false); // Update camera with new size.
  85. }
  86. public void dispose () {
  87. atlas.dispose();
  88. }
  89. public static void main (String[] args) throws Exception {
  90. new LwjglApplication(new SimpleTest1());
  91. }
  92. }