Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hacks;
- 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.Pixmap;
- import com.badlogic.gdx.graphics.Pixmap.Format;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- public class AspectRatioDemoMain extends com.badlogic.gdx.Game {
- private static final int TEXTURE_WIDTH = 64;
- private static final int TEXTURE_HEIGHT = 32;
- private static final float SPRITE_WIDTH = 0.5f;
- private OrthographicCamera camera;
- private SpriteBatch spriteBatch;
- private Sprite sprite;
- private Texture texture;
- @Override
- public void create() {
- camera = new OrthographicCamera();
- Pixmap pixmap = new Pixmap(TEXTURE_WIDTH, TEXTURE_HEIGHT,
- Format.RGBA8888);
- pixmap.setColor(Color.WHITE);
- pixmap.fillRectangle(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT);
- texture = new Texture(pixmap);
- sprite = new Sprite(texture);
- sprite.setSize(SPRITE_WIDTH, SPRITE_WIDTH * TEXTURE_HEIGHT / TEXTURE_WIDTH);
- sprite.setPosition(-0.5f * sprite.getWidth(), -0.5f * sprite.getHeight());
- sprite.setColor(0f, 1f, 0f, 1f);
- spriteBatch = new SpriteBatch();
- }
- @Override
- public void resize(int width, int height) {
- super.resize(width, height);
- float cameraWidth = Gdx.graphics.getWidth();
- float cameraHeight = Gdx.graphics.getHeight();
- float aspect = cameraWidth / cameraHeight;
- camera.setToOrtho(false, 1.0f, 1.0f / aspect);
- camera.position.set(0, 0, 0);
- }
- @Override
- public void render() {
- super.render();
- Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
- camera.update();
- spriteBatch.setProjectionMatrix(camera.combined);
- spriteBatch.begin();
- sprite.draw(spriteBatch);
- spriteBatch.end();
- }
- public static void main(String[] args) {
- LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
- config.title = AspectRatioDemoMain.class.getName();
- config.width = 800;
- config.height = 480;
- config.fullscreen = false;
- config.useGL20 = true;
- config.useCPUSynch = true;
- config.forceExit = true;
- config.vSyncEnabled = true;
- new LwjglApplication(new AspectRatioDemoMain(), config);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement