Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. package com.mygdx.game;
  2.  
  3. import com.badlogic.gdx.ApplicationAdapter;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.graphics.GL20;
  6. import com.badlogic.gdx.graphics.OrthographicCamera;
  7. import com.badlogic.gdx.graphics.Texture;
  8. import com.badlogic.gdx.graphics.g2d.Sprite;
  9. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  10. import com.badlogic.gdx.utils.viewport.ScreenViewport;
  11. import com.badlogic.gdx.utils.viewport.Viewport;
  12.  
  13. public class MyGdxGame extends ApplicationAdapter {
  14.     private SpriteBatch batch;
  15.     private Sprite board;
  16.     private Texture backg;
  17.     private OrthographicCamera camera;
  18.     private Viewport viewport;
  19.     private Viewport viewportbackg;
  20.  
  21.     @Override
  22.     public void create () {
  23.         batch = new SpriteBatch();
  24.         backg = new Texture(Gdx.files.internal("background.png"));
  25.         backg.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
  26.  
  27.         board = new Sprite(new Texture(Gdx.files.internal("board.png")));
  28.         setBoardDefault();
  29.  
  30.         camera = new OrthographicCamera();
  31.         viewportbackg = new ScreenViewport(camera);
  32.         viewport = new ScreenViewport(camera);
  33.  
  34.         camera.position.set(camera.viewportWidth/2,camera.viewportHeight/2,0);
  35.     }
  36.  
  37.     @Override
  38.     public void render () {
  39.         camera.update();
  40.  
  41.         Gdx.gl.glClearColor(1, 0, 0, 1);
  42.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  43.  
  44.         batch.begin();
  45.         viewportbackg.apply(true);
  46.         batch.setProjectionMatrix(camera.combined);
  47.         batch.draw(backg, 0, 0, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  48.         batch.end();
  49.  
  50.         batch.begin();
  51.         viewport.apply(true);
  52.         batch.setProjectionMatrix(camera.combined);
  53.         board.draw(batch);
  54.         batch.end();
  55.     }
  56.  
  57.     @Override
  58.     public void dispose(){
  59.         backg.dispose();
  60.         board.getTexture().dispose();
  61.         batch.dispose();
  62.     }
  63.  
  64.     @Override
  65.     public void resize(int width, int height) {
  66.         viewport.update(width, height);
  67.         viewportbackg.update(width, height);
  68.         camera.position.set(width / 2, height / 2, 0);
  69.         setBoardDefault();
  70.     }
  71.  
  72.     private void setBoardDefault() {
  73.         int size;
  74.         if (Gdx.graphics.getWidth() < Gdx.graphics.getHeight()) {
  75.             size = Gdx.graphics.getWidth() - 10;
  76.             board.setSize(size, size);
  77.             board.setPosition(5, Gdx.graphics.getHeight() / 2 - size / 2);
  78.         }
  79.         else {
  80.             size = Gdx.graphics.getHeight() - 10;
  81.             board.setSize(size, size);
  82.             board.setPosition(Gdx.graphics.getWidth() / 2 - size / 2, 5);
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement