Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // public class MyGame extends Game {
- public class MyGame extends ApplicationAdapter {
- public ExtendViewport viewport;
- public SpriteBatch sb;
- public Texture bg_image;
- // The WIDTH and HEIGHT is to keep a fixed screen ratio of the "playable" part of game
- public static final int WIDTH = 480;
- public static final int HEIGHT = 800;
- @Override
- public void create () {
- viewport = new ExtendViewport(MyGame.WIDTH, MyGame.HEIGHT);
- sb = new SpriteBatch();
- bg_image = new Texture(Gdx.files.internal("green.png"));
- }
- // render part of game
- @Override
- public void render () {
- Gdx.gl.glClearColor(1, 1, 1, 1);
- // another_viewport.apply();
- // sb.setProjectionMatrix(another_viewport.getCamer().combined);
- //sb.begin();
- //blabla
- //sb.end();
- // I have this since I have an additional viewport
- viewport.apply();
- sb.setProjectionMatrix(viewport.getCamera().combined);
- sb.begin();
- sb.draw(bg_image, 0, 0, MyGame.WIDTH, MyGame.HEIGHT);
- sb.end();
- }
- // Called when resizing the screen
- @Override
- public void resize(int width, int height){
- bg_viewport.update(width, height);
- viewport.update(width, height);
- }
- }
- public class DesktopLauncher {
- public static void main (String[] arg) {
- LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
- config.title = "My Game";
- // Works perfectly if width and height matches the world width and height
- config.width = 480;
- // It will make extendViewport not perfectly placed in centrum
- // But will still adjust as intended when changing the size of the window
- // on Desktop while being not perfecty placed
- // Additional. It acts as if you have viewport.apply(true);
- // and viewport.update(x, y, true);
- // So your game extends to the right.
- // After that your game adjusts perfectly, but with an unwanted extend..
- //config.width = 680;
- // This seems to only happen when MyGame extends "Game", not "ApplicationAdapter"
- // Also the 0,0 coordinate is at middle of screen when extend "ApplicationAdapter"
- // 0,0 coordinate is at bottom left corner when extends "Game".
- config.height = 800;
- new LwjglApplication(new MyGame(), config);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement