Guest User

Untitled

a guest
Jan 13th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 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.scenes.scene2d.Stage;
  11. import com.badlogic.gdx.scenes.scene2d.ui.Label;
  12. import com.badlogic.gdx.scenes.scene2d.ui.Skin;
  13. import com.badlogic.gdx.utils.viewport.ScreenViewport;
  14.  
  15. public class MyGdxGame extends ApplicationAdapter {
  16.     private SpriteBatch batch;
  17.     private OrthographicCamera camera;
  18.     private ScreenViewport viewport;
  19.     private Stage stage;
  20.  
  21.     private Skin skin;
  22.     private Texture img;
  23.     private Sprite sprite;
  24.  
  25.     private final float VIRTUAL_WIDTH = 160.0f;
  26.     private float VIRTUAL_HEIGHT;
  27.    
  28.     @Override
  29.     public void create () {
  30.         batch = new SpriteBatch();
  31.         camera = new OrthographicCamera();
  32.         viewport = new ScreenViewport(camera);
  33.  
  34.         img = new Texture(Gdx.files.internal("badlogic.jpg"));
  35.         sprite = new Sprite(img);
  36.         sprite.setSize(20, 20);
  37.  
  38.         stage = new Stage(viewport);
  39.         skin = new Skin(Gdx.files.internal("uiskin.json"));
  40.         Label label = new Label("Hello World", skin);
  41.         //label.setSize(40, 20);    // bez wplywu
  42.         label.setFontScale(0.3f);   // dziala ale literki nachodza
  43.         stage.addActor(label);
  44.  
  45.     }
  46.  
  47.     @Override
  48.     public void resize(int width, int height)
  49.     {
  50.         stage.getViewport().update(width, height, true);
  51.  
  52.         VIRTUAL_HEIGHT = VIRTUAL_WIDTH *  height / width;
  53.  
  54.         camera.setToOrtho(false, VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
  55.         batch.setProjectionMatrix(camera.combined);
  56.     }
  57.  
  58.     @Override
  59.     public void render () {
  60.         Gdx.gl.glClearColor(1, 0, 0, 1);
  61.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  62.  
  63.         sprite.setPosition(VIRTUAL_WIDTH/2 - sprite.getWidth()/2, VIRTUAL_HEIGHT/2 - sprite.getHeight()/2);
  64.  
  65.         batch.begin();
  66.         sprite.draw(batch);
  67.         batch.end();
  68.  
  69.         stage.act();
  70.         stage.draw();
  71.     }
  72.    
  73.     @Override
  74.     public void dispose () {
  75.         batch.dispose();
  76.         stage.dispose();
  77.         skin.dispose();
  78.         img.dispose();
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment