Guest User

Viewport Test

a guest
May 15th, 2015
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. package com.Test.game;
  2.  
  3. import com.badlogic.gdx.Game;
  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.BitmapFont;
  9. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  10. import com.badlogic.gdx.graphics.g2d.TextureAtlas;
  11. import com.badlogic.gdx.scenes.scene2d.Actor;
  12. import com.badlogic.gdx.scenes.scene2d.Stage;
  13. import com.badlogic.gdx.scenes.scene2d.ui.Image;
  14. import com.badlogic.gdx.scenes.scene2d.ui.Table;
  15. import com.badlogic.gdx.utils.viewport.StretchViewport;
  16.  
  17. import java.awt.TextArea;
  18.  
  19. public class Main extends Game {
  20.  
  21.     private int VIRTUAL_WIDTH = 600; <<== Virtual Width
  22.     private int VIRTUAL_HEIGHT = 900; <<== Virtual Height
  23.  
  24.     OrthographicCamera cam;
  25.     Stage stage;
  26.         TextureAtlas atlas;
  27.     Image Logo;
  28.    
  29.     @Override
  30.     public void create () {
  31.         stage = new Stage(new StretchViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT)); <<= give it the Width + Height
  32.         cam = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT); <== give it the Width + Height
  33.  
  34.         atlas = new TextureAtlas("LoadingScreen.pack");
  35.         Logo = new Image(atlas.findRegion("Logo1"));
  36.         stage.addActor(Logo);
  37.     }
  38.  
  39.     @Override
  40.     public void resize(int Width, int Height) {
  41.         Width = VIRTUAL_WIDTH;  <<== set Width to VirtualWidth
  42.         Height = VIRTUAL_HEIGHT; <<== set Height to VirtualHeight
  43.  
  44.         System.out.println("Width" + Width + "Height" + Height);
  45.         System.out.println("Screen Width" + Gdx.graphics.getWidth() + "Screen Height" + Gdx.graphics.getHeight());
  46.  
  47.         stage.getViewport().update(Width, Height, true); <<== Update the vieuwport
  48.  
  49.         /* here i tried to see if it works by setting it center and making it have 50 width on all sides, atleast i thought.*/
  50.         Logo.setSize(500, 800);
  51.         Logo.setX(Width / 2 - Logo.getWidth() / 2);
  52.         Logo.setY(Height / 2 - Logo.getWidth() / 2);
  53.         Logo.setOrigin(Logo.getWidth() / 2, Logo.getHeight() / 2);
  54.  
  55.     }
  56.  
  57.     @Override
  58.     public void render () {
  59.         Gdx.gl.glClearColor(1, 0, 0, 1);
  60.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  61.  
  62.         stage.act();
  63.         stage.draw();
  64.     }
  65.  
  66.     @Override
  67.     public void dispose() {
  68.         atlas.dispose();
  69.         stage.dispose();
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment