public
Created

LibGDX: Textured Triangles with SpriteBatch

  • Download Gist
TexturedTri.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class TexturedTri implements ApplicationListener {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "atmos";
cfg.useGL20 = true;
cfg.width = 480;
cfg.height = 320;
new LwjglApplication(new TexturedTri(), cfg);
}
BitmapFont font;
SpriteBatch batch;
OrthographicCamera camera;
Texture tex;
TextureRegion reg;
float[] vertices;
@Override
public void create() {
font = new BitmapFont();
batch = new SpriteBatch();
camera = new OrthographicCamera();
tex = new Texture(Gdx.files.internal("data/texture_training_grounds.png"));
reg = new TextureRegion(tex, 50, 0, 100, 100);
vertices = createTriangle(reg, 125, 25, 50, 50, Color.WHITE);
}
protected float[] createTriangle(TextureRegion region, int x, int y, int width, int height, Color color) {
float w = reg.getRegionWidth();
float h = reg.getRegionHeight();
float c = color.toFloatBits();
float u = reg.getU();
float v = reg.getV();
float u2 = reg.getU2();
float v2 = reg.getV2();
return new float[] {
x, y, c, u, v,
x, y+h, c, u, v2,
x+w, y, c, u2, v,
x, y, c, u, v
};
}
@Override
public void resize(int width, int height) {
camera.setToOrtho(false, width, height);
batch.setProjectionMatrix(camera.combined);
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(tex, vertices, 0, vertices.length);
batch.end();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
font.dispose();
batch.dispose();
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.