"Open source Java Game Framework Libgdx theme" -10-Core library-viewport

Source: Internet
Author: User

Viewport class, also known as the Viewport class, is mainly responsible for managing the game camera and processing the conversion relationship between the game world coordinates and the scene layer coordinates. Development on the mobile side is not as easy as PC-side. Because you want to adapt devices with different resolutions. LIBGDX handles the stage-to-camera relationship with the viewport when processing different device screens. Mainly uses its subclasses. 7 Types of viewport: Stretchviewport: Stretch viewport, support device width and height ratio change, according to the width and height of the parameters to automatically adapt to different device screen Fitviewport: will maintain the width and height ratio within its own parameters, And as a basis, stretch the class to fit the screen. Black edges may appear. Fillviewport: Fills the viewport. Maintains the aspect ratio of the scene dimension layer dimensions, but unlike Fitviewport, the viewport always fills the entire screen screenviewport: the screen viewport. Does not refer to the virtual screen size, always fills the entire window, does not stretch or appear black edge. May cause some game content to be displayed on a small screen. Extendviewport: Extend the viewport. Maintain the aspect ratio of the game world without black edges. Portions of the game window that are not filled will be stretched, and the world of games beyond the screen will stretch along that direction. Staticviewport: Static viewport. does not support any extension, always maintains a fixed aspect ratio. Scalingviewport: Zoom in on the viewport. The device screen can be automatically adapted to scale. Construction method: Extendviewport (float minworldwidth,float minworldheight): Extends the viewport, the first parameter represents the smallest width of the world, and the second parameter represents the minimum height of the game world. Screenviewport (Camera camera): Screen viewport. The camera inside the viewport uses the camera in the parameter. The orthographic projection camera is used by default for an argument-free method. Scalingviewport (Scaling scaling,float worldwidth,float worldheight): The viewport is retracted. The first parameter passes in the indent mode, the second and third parameters represent the width and height of the game World Stretchviewport (float worldwidth,float worldheight,camera Camera): Stretched viewport. The first and second parameters represent the width and height of the world, and the third parameter represents the camera used by the rendering. Fillviewport (float worldwidth,float worldheight,camera Camera): fills the viewport. The first and second parameters represent the width and height of the world, and the third parameter represents the camera used by the rendering Fitviewport(Float worldwidth,float worldheight,camera camera): The first parameter and the second parameter represent the width and height of the world, and the third parameter represents the camera used by the rendering.   Instance Code:
1  PackageCom.mygdx.viewport;2 3 Importcom.badlogic.gdx.graphics.Texture;4 ImportCom.badlogic.gdx.graphics.g2d.Batch;5 ImportCom.badlogic.gdx.scenes.scene2d.Actor;6 /**7 * My class of actors8  * @authorJack (Le Zhi)9 * @blog dtblog.cnTen * @qq 984137183 One  */ A  Public classMyactorextendsActor { -     PrivateTexture Texture; -      PublicMyactor () { the          This. Init (); -     } -     Private voidinit () { -Texture=NewTexture ("Badlogic.jpg"); +     } - @Override +      Public voidDraw (Batch batch,floatParentalpha) { ABatch.draw (texture, 0, 0); at     } -      - } -  -  -  PackageCom.mygdx.viewport; in  - ImportCom.badlogic.gdx.ApplicationAdapter; to ImportCom.badlogic.gdx.Gdx; + Importcom.badlogic.gdx.graphics.GL20; - ImportCom.badlogic.gdx.graphics.g2d.SpriteBatch; the ImportCom.badlogic.gdx.scenes.scene2d.Stage; * ImportCom.badlogic.gdx.utils.viewport.ExtendViewport; $ ImportCom.badlogic.gdx.utils.viewport.ScreenViewport;Panax Notoginseng ImportCom.badlogic.gdx.utils.viewport.StretchViewport; -  the /** + * Stretching viewport A  * @authorJack (Le Zhi) the * @blog dtblog.cn + * @qq 984137183 -  */ $  Public classUsestretchviewportextendsapplicationadapter{ $     //Sprite Brushes -      Publicspritebatch Batch; -     //Statement Actors the      Publicmyactor actor; -     //declaring the stageWuyi      PublicStage stage; the     //declare stretched viewport, equal to zoom -     PrivateStretchviewport viewport; Wu     //declaring an extended viewport -     PrivateExtendviewport Extviewport; About     PrivateScreenviewport Screenviewport;//Screen Viewport $ @Override -      Public voidCreate () { -         //initializing the sprite brush -Batch=NewSpriteBatch (); A         //Initializing actors +Actor=Newmyactor (); theviewport=NewStretchviewport (800, 480); -Extviewport =NewExtendviewport (200, 200); $screenviewport=NewScreenviewport (); the         //Initializing the stage the         //stage=new Stage (viewport); the         //stage=new Stage (extviewport); theStage=NewStage (screenviewport); -         //Add an actor in Stage.addactor (actor); the  the     } About @Override the      Public voidrender () { the         //Set Background color theGdx.gl.glClearColor (1, 1, 1, 1); + Gdx.gl.glClear (gl20.gl_color_buffer_bit); -         //Update Stage Logic the stage.act ();Bayi         //Draw Stage Content the Stage.draw (); the     } -  -}
The original is written by Bo Master Le Zhi Editor, the copyright belongs to the Bo owner. The original address http://www.dtblog.cn/1147.html reprint please specify the source!

"Open source Java Game Framework Libgdx theme" -10-Core library-viewport

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.