Advertisement
Guest User

Untitled

a guest
May 1st, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1.  
  2.  
  3. // public class MyGame extends Game {
  4. public class MyGame extends ApplicationAdapter {
  5.  
  6.     public ExtendViewport viewport;
  7.     public SpriteBatch sb;
  8.     public Texture bg_image;
  9.  
  10.     // The WIDTH and HEIGHT is to keep a fixed screen ratio of the "playable" part of game
  11.     public static final int WIDTH = 480;
  12.     public static final int HEIGHT = 800;
  13.  
  14.     @Override
  15.     public void create () {
  16.         viewport = new ExtendViewport(MyGame.WIDTH, MyGame.HEIGHT);
  17.         sb = new SpriteBatch();
  18.  
  19.         bg_image = new Texture(Gdx.files.internal("green.png"));
  20.     }
  21.  
  22.     // render part of game
  23.     @Override
  24.     public void render () {
  25.         Gdx.gl.glClearColor(1, 1, 1, 1);
  26.  
  27.         // another_viewport.apply();
  28.         // sb.setProjectionMatrix(another_viewport.getCamer().combined);
  29.  
  30.         //sb.begin();
  31.         //blabla
  32.         //sb.end();
  33.  
  34.         // I have this since I have an additional viewport
  35.         viewport.apply();
  36.         sb.setProjectionMatrix(viewport.getCamera().combined);
  37.  
  38.         sb.begin();
  39.         sb.draw(bg_image, 0, 0, MyGame.WIDTH, MyGame.HEIGHT);
  40.         sb.end();
  41.  
  42.     }
  43.  
  44.     // Called when resizing the screen
  45.     @Override
  46.     public void resize(int width, int height){
  47.         bg_viewport.update(width, height);
  48.         viewport.update(width, height);
  49.     }
  50.  
  51. }
  52.  
  53. public class DesktopLauncher {
  54.     public static void main (String[] arg) {
  55.         LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
  56.         config.title = "My Game";
  57.         // Works perfectly if width and height matches the world width and height
  58.         config.width = 480;
  59.  
  60.         // It will make extendViewport not perfectly placed in centrum
  61.         // But will still adjust as intended when changing the size of the window
  62.         // on Desktop while being not perfecty placed
  63.  
  64.         // Additional. It acts as if you have viewport.apply(true);
  65.         // and viewport.update(x, y, true);
  66.         // So your game extends to the right.
  67.         // After that your game adjusts perfectly, but with an unwanted extend..
  68.  
  69.         //config.width = 680;
  70.  
  71.         // This seems to only happen when MyGame extends "Game", not "ApplicationAdapter"
  72.         // Also the 0,0 coordinate is at middle of screen when extend "ApplicationAdapter"
  73.         // 0,0 coordinate is at bottom left corner when extends "Game".
  74.  
  75.         config.height = 800;
  76.         new LwjglApplication(new MyGame(), config);
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement