Libgdx Viewport screen adaptation

Source: Internet
Author: User
Tags libgdx

Libgdx Viewport screen adaptation

During Game Development, we usually set the size of a world, which is fixed and the location of the game is also fixed. However, for various Android resolutions, we need to adapt game elements to different resolutions displayed at the same proportion. Fortunately, Libgdx defines Viewport for me.

Viewport type

Explanation of Viewport

ScreenViewport: there is no definite world size. The world size is determined by the screen size.
 

/* Creates a new viewport using a new {@link OrthographicCamera}. / public ScreenViewport () { this(new OrthographicCamera()); } public ScreenViewport (Camera camera) { setCamera(camera); } @Override public void update (int screenWidth, int screenHeight, boolean centerCamera) { setScreenBounds(0, 0, screenWidth, screenHeight); setWorldSize(screenWidth * unitsPerPixel, screenHeight * unitsPerPixel); apply(centerCamera); } 

We can see from the above that the world size is not specified when ScreenViewport is initialized.UpdateIs calculated based on the screen. By default, a pixel on the screen corresponds to a world size unit, which can be calledSetUnitsPerPixel (float unitsPerPixel)Modify.In this form, the game elements are not scaled. Each game element is painted based on its size without a black frame. The larger the screen size, the larger the field of view exposed. In other words, if the screen size is small, some game elements will not be displayed. Generally, the upper game elements will not be displayed.

ExtendViewport: First takeScaling. fitExpand the screen size to the right or up.

update (int screenWidth, int screenHeight, boolean centerCamera)

However, if maxWorldWidth is used, a black screen may appear.
3. FitViewport: Keep the aspect ratio unchanged. Scale the world proportionally until one direction (up or down or left) reaches the screen size. The world is in the center of the screen, and black boxes are left on the top or right of the screen.
4. FillViewport: Keep the aspect ratio unchanged, scale proportionally until no black box is left. The world size may exceed the screen size, that is, some elements are outside the screen.

StretchViewport: If the aspect ratio is not maintained, the world size is scaled to the screen size, but sometimes the game elements are distorted.

 

For game development, the StretchViewport view is generally used.
Test code:

Private static final float Limit = 800366f; private static final float MIN_SCENE_HEIGHT = 600366f; private static final float Limit = 1280366f; private static final float Limit = 800366f; Texture bg, sprite; SpriteBatch batch; camera camera; BitmapFont font; private ArrayMap
  
   
Viewports; // The ViewPort camera has the private int currentViewport in the center of the screen; private InputListenEvent inputListenEvent; @ Override public void create () {bg = new Texture (Gdx. files. internal ("background.jpg"); sprite = new Texture (Gdx. files. internal ("badlogic.jpg"); batch = new SpriteBatch (); camera = new OrthographicCamera (); font = new BitmapFont (); font. getData (). setScale (2.0f); font. getRegion (). getTexture (). setFilter (TextureFilter. linear, TextureFilter. linear); createViewports (); selectNextViewport (); inputListenEvent = new InputListenEvent (); // initialize the listening event Gdx. input. setInputProcessor (inputListenEvent); // Libgdx starts listening to inputListenEvent} @ Override public void resize (int width, int height) {viewports. getValueAt (currentViewport ). update (width, height, true); // centerCamera = true sets camera to getTextLog ("resize") in the center of the world;} @ Override public void render () {Gdx. gl. glClearColor (0.39f, 0.58f, 0.92f, 1.0f); Gdx. gl. glClear (gl1_gl _ COLOR_BUFFER_BIT); batch. setProjectionMatrix (camera. combined); batch. begin (); batch. draw (bg, 0, 0, viewports. getValueAt (currentViewport ). getWorldWidth (), viewports. getValueAt (currentViewport ). getWorldHeight (); batch. draw (sprite, 0, 0); font. draw (batch, viewports. getKeyAt (currentViewport), 10,400); batch. end () ;}@ Override public void dispose () {bg. dispose (); sprite. dispose (); batch. dispose (); font. dispose ();} private void createViewports () {viewports = new ArrayMap
   
    
(); Viewports. put ("StretchViewport", new StretchViewport (MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, camera); // The above Initialization is consistent with the following effect // viewports. put ("StretchViewport", new ScalingViewport (Scaling. stretch, MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, camera); viewports. put ("FitViewport", new FitViewport (MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, camera); // The above Initialization is consistent with the following effect // viewports. put ("FitViewport", new ScalingViewport (Scaling. fit, MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, camera); viewports. put ("FillViewport", new FillViewport (MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, camera); // The above Initialization is consistent with the following effect // viewports. put ("FillViewport", new ScalingViewport (Scaling. fill, MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, camera); viewports. put ("ScreenViewport", new ScreenViewport (camera); viewports. put ("ExtendViewport (no max)", new ExtendViewport (MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, camera); viewports. put ("ExtendViewport (max)", new ExtendViewport (MIN_SCENE_WIDTH, MIN_SCENE_HEIGHT, MAX_SCENE_HEIGHT, MAX_SCENE_WIDTH, camera); currentViewport =-1;} private void selectNextViewport () {currentViewport = (currentViewport + 1) % viewports. size; viewports. getValueAt (currentViewport ). apply (true); // set camera to the center of the screen viewports. getValueAt (currentViewport ). update (Gdx. graphics. getWidth (), Gdx. graphics. getHeight (), true); // centerCamera = true sets camera to getTextLog ("selectNextViewport") in the center of world;} private void getTextLog (String flag) {Gdx. app. log (flag, "selcected" + viewports. getKeyAt (currentViewport); Gdx. app. log (flag, "screentWidth =" + viewports. getValueAt (currentViewport ). getScreenWidth () + "screentHeight =" + viewports. getValueAt (currentViewport ). getScreenHeight (); Gdx. app. log (flag, "wordWidth =" + viewports. getValueAt (currentViewport ). getWorldWidth () + "wordHeight =" + viewports. getValueAt (currentViewport ). getWorldHeight (); Gdx. app. log (flag, "camera. x = "+ camera. position. x + "camera. y = "+ camera. position. y); Gdx. app. log (flag, "screen x =" + viewports. getValueAt (currentViewport ). getScreenX () + "y =" + viewports. getValueAt (currentViewport ). getScreenY ();} class InputListenEvent extends InputAdapter {// refer to the content in the previous section. // click the screen to automatically switch Viewport @ Override public boolean touchDown (int screenX, int screenY, int pointer, int button) {selectNextViewport (); return false ;}}
   
  

Test: in fact, this figure does not show the specific effect. It just shows you how to test the image.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.