Skip to content

@mattdesl /TexturedTri.java
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
LibGDX: Textured Triangles with SpriteBatch
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.