Skip to content

Instantly share code, notes, and snippets.

@MobiDevelop
Created June 13, 2013 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MobiDevelop/5770449 to your computer and use it in GitHub Desktop.
Save MobiDevelop/5770449 to your computer and use it in GitHub Desktop.
package nEx.Games.Samples;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class SampleGame implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private TextureRegion region;
private float[] vertices = new float[20];
Color begin = new Color(Color.BLUE);
Color end = new Color(1, 0.5f, 0, 1);
@Override
public void create() {
camera = new OrthographicCamera(1, 1);
batch = new SpriteBatch();
// For example purposes, create a pixmap to use for our gradient
Pixmap pix = new Pixmap(2, 2, Pixmap.Format.RGBA8888);
pix.setColor(1,1,1,1);
pix.fillRectangle(0, 0, 2, 2);
texture = new Texture(pix);
region = new TextureRegion(texture, 0, 0, 2, 2);
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
float inc = 0.1f;
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
setupGradient();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(texture, vertices, 0, 20);
batch.end();
// Animate the gradient a bit, because it is fun
begin.r += inc * Gdx.graphics.getDeltaTime();
end.b = 0.5f - begin.r;
if (begin.r > 0.5f) {
begin.r = 0.5f;
inc *= -1;
}
if (begin.r < 0) {
begin.r = 0;
inc *= -1;
}
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
public void setupGradient() {
float color1 = begin.toFloatBits();
float color2 = end.toFloatBits();
vertices[SpriteBatch.X1] = -0.45f;
vertices[SpriteBatch.Y1] = +0.45f;
vertices[SpriteBatch.C1] = color1;
vertices[SpriteBatch.U1] = region.getU();
vertices[SpriteBatch.V1] = region.getV();
vertices[SpriteBatch.X2] = +0.45f;
vertices[SpriteBatch.Y2] = +0.45f;
vertices[SpriteBatch.C2] = color1;
vertices[SpriteBatch.U2] = region.getU2();
vertices[SpriteBatch.V2] = region.getV();
vertices[SpriteBatch.X3] = +0.45f;
vertices[SpriteBatch.Y3] = -0.45f;
vertices[SpriteBatch.C3] = color2;
vertices[SpriteBatch.U3] = region.getU2();
vertices[SpriteBatch.V3] = region.getV2();
vertices[SpriteBatch.X4] = -0.45f;
vertices[SpriteBatch.Y4] = -0.45f;
vertices[SpriteBatch.C4] = color2;
vertices[SpriteBatch.U4] = region.getU();
vertices[SpriteBatch.V4] = region.getV2();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment