10、绘图(Graphics)

介绍

Graphics 模组提供了关于当前设备显示与应用窗口的信息,同时包含了访问 OpenGL 上下文的工具。明确来讲,就是关于屏幕尺寸,分辨率与帧缓冲属性,例如颜色深度、深度/模板缓冲、反混淆能力都能在这个类中找到。其余通用模组都是 Gdx class 静态成员提供的。

OpenGL 上下文

A particular use of this module concerns more direct access to the current OpenGL context for lower-level commands and queries.

The following example accesses the context in an OpenGL ES2 application to set the viewport and clear the frame and depth buffers:

Gdx.gl20.glViewport( 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );
Gdx.gl20.glClearColor( 0, 0, 0, 1 );
Gdx.gl20.glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );

Note the use of getWidth() / getHeight() to access the current application window dimensions in setting the viewport as well as the use of constants from the GL20 class just as one would do in a regular OpenGL program. A key advantage in Libgdx is the ability to access low level functionality whenever higher level abstraction does not suffice.

Each version of OpenGL ES is available through its own respective interface as well as a GLCommon interface for version agnostic commands. Note that use of GL20 requires instructing the application to use OpenGL ES2 upon start-up.

Access to the OpenGL Utility class ( that doesn’t exist, this article needs to be rewritten! ) is also provided, although this functionality may be better handled through Libgdx’s own Orthographic and Perspective camera classes. There is also a simple method for querying support for named extensions in supportsExtension(). Just supply the name of the extension to determine support on the current device.

Frame Time

One particularly useful method in the Graphics class is getDeltaTime(), which provides the time elapsed since the last rendered frame. This can be useful for time-based animation when frame independence is not necessary. For instance Actor or UI animation in a Stage instance might be controlled by a call such as the following in the application’s render method:

stage.act( Math.min( Gdx.graphics.getDeltaTime(), 1/30 ) );

Notice the use of a maximum time step of 1/30 seconds. This is to avoid potentially large jerks in the resulting animation. This illustrates the fact that while getDeltaTime() can be useful for simple animations, it is still frame dependent and more sensitive actions such as game logic or physics simulation may benefit from other timing strategies.

Another useful method is getFramesPerSecond(), which returns a running average of the current frame-rate for simple diagnostic purposes. However for more serious profiling efforts, the use of FPSLogger is recommended.

Platform Differences

On the desktop, the Graphics class also provides the ability to set a window’s icon and title values. Obviously these methods have no effect on platforms which lack an icon or title.

The methods setDisplayMode() and setVSync() set the display mode to full-screen/windowed and enable/disable vertical display sync respectively. Keep in mind these methods have effects only on certain platforms.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值