From f2b935e8391ff06099b62c7200f7e3fe9b6d437a Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Sun, 10 May 2020 21:24:02 +0200 Subject: [PATCH] added lwjgl library to gradle --- build.gradle | 7 +++ core/src/temp/Animator.java | 88 +++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 core/src/temp/Animator.java diff --git a/build.gradle b/build.gradle index 0d33ff6..6f51033 100644 --- a/build.gradle +++ b/build.gradle @@ -53,6 +53,8 @@ project(":desktop") { } project(":core") { + project.ext.lwjglVersion = "3.2.3" + project.ext.lwjglNatives = "natives-windows" apply plugin: "java-library" @@ -62,6 +64,11 @@ project(":core") { api "com.badlogicgames.gdx:gdx-ai:$aiVersion" api "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion" api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" + + implementation platform("org.lwjgl:lwjgl-bom:$lwjglVersion") + + implementation "org.lwjgl:lwjgl" + runtimeOnly "org.lwjgl:lwjgl::$lwjglNatives" } } diff --git a/core/src/temp/Animator.java b/core/src/temp/Animator.java new file mode 100644 index 0000000..b08975f --- /dev/null +++ b/core/src/temp/Animator.java @@ -0,0 +1,88 @@ +package netwerkprog.game; + +import com.badlogic.gdx.ApplicationListener; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Animation; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.g2d.TextureRegion; + +public class Animator implements ApplicationListener { + + // Constant rows and columns of the sprite sheet + private static final int FRAME_COLS = 6, FRAME_ROWS = 5; + + // Objects used + Animation walkAnimation; // Must declare frame type (TextureRegion) + Texture walkSheet; + SpriteBatch spriteBatch; + + // A variable for tracking elapsed time for the animation + float stateTime; + + @Override + public void create() { + + // Load the sprite sheet as a Texture + walkSheet = new Texture(Gdx.files.internal("sprite-animation1.png")); + + // Use the split utility method to create a 2D array of TextureRegions. This is + // possible because this sprite sheet contains frames of equal size and they are + // all aligned. + TextureRegion[][] tmp = TextureRegion.split(walkSheet, + walkSheet.getWidth() / FRAME_COLS, + walkSheet.getHeight() / FRAME_ROWS); + + // Place the regions into a 1D array in the correct order, starting from the top + // left, going across first. The Animation constructor requires a 1D array. + TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS]; + int index = 0; + for (int i = 0; i < FRAME_ROWS; i++) { + for (int j = 0; j < FRAME_COLS; j++) { + walkFrames[index++] = tmp[i][j]; + } + } + + // Initialize the Animation with the frame interval and array of frames + walkAnimation = new Animation(0.025f, walkFrames); + + // Instantiate a SpriteBatch for drawing and reset the elapsed animation + // time to 0 + spriteBatch = new SpriteBatch(); + stateTime = 0f; + } + + @Override + public void resize(int width, int height) { + + } + + @Override + public void render() { + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen + stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time + + // Get current frame of animation for the current stateTime + TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true); + spriteBatch.begin(); + spriteBatch.draw(currentFrame, 50, 50); // Draw current frame at (50, 50) + spriteBatch.end(); + } + + @Override + public void pause() { + + } + + @Override + public void resume() { + + } + + @Override + public void dispose() { // SpriteBatches and Textures must always be disposed + spriteBatch.dispose(); + walkSheet.dispose(); + } +} \ No newline at end of file