LIBGDX: What is a “viewport”?

和自甴很熟 提交于 2019-11-29 21:28:32

That depends on the version of LibGDX you are using. With LibGDX 1.0.0 the Viewport class was introduced and probably that's what the latest tutorials and articles mean, when they talk about "viewports".

This class manages two things:

  • The viewport of a camera
  • The OpenGL viewport

The viewport of a camera defines how much you want to see of your game world. If you have a huge TiledMap of the size 10000px X 10000px, then you probably only want to show a small area of that map at once. If you define your cameras viewport to be 800px X 480px, then you will see a part of your map which has exactly this dimension (at least in the case of an OrthographicCamera). It is like a window through which you look through your world and you define the size of it via the viewport.

The OpenGL viewport defines the area to which the camera's view should be mapped to. By default that viewport is 100% of the size of your window on desktop operating systems. But that could also be different. A FitViewport with LibGDX 1.0.0 can scale that viewport so that it keeps the aspect ratio of a "virtual viewport" which you define (which is actually the camera's viewport, which is being managed by the Viewport class). That means in case the window's aspect ratio doesn't match the Viewport (virtual) aspect ratio, the OpenGL viewport will be set to a smaller size and black bars will appear. So you can think of it like another layer between the actual window on your desktop, and the camera. This layer takes the output of your camera and scales that to the defined size within your desktop window.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!