二,游戏的game loop搭建
2015-11-03 22:21
387 查看
关于如何搭建环境的问题在这里不再赘述,网上可以搜到很多,或者你可以在github上libgdx页面找到其他所需要的教程,而且已经在提供的下源码载文件中提供了完整的包和所需要的各种资源,直接导入Eclipse即可。
这次主要讲解game loop的搭建,game loop是一个游戏的主循环,主要包括渲染和更新,代码的主要部分都是围绕这两个部分,当然还有诸如暂停,恢复等其他部分,这些部分可以在代码中见到。实现了ApplicationListener的接口如下,ApplicationListener接口提供了游戏的入口。代码已经详细注释
这次主要讲解game loop的搭建,game loop是一个游戏的主循环,主要包括渲染和更新,代码的主要部分都是围绕这两个部分,当然还有诸如暂停,恢复等其他部分,这些部分可以在代码中见到。实现了ApplicationListener的接口如下,ApplicationListener接口提供了游戏的入口。代码已经详细注释
package com.hj.lee.game; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Pixmap.Format; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.FrameBuffer; import com.hj.lee.game.screens.AbstractGameScreen; import com.hj.lee.game.screens.transitions.ScreenTransition; public abstract class DirectedGame implements ApplicationListener { private boolean init;//是否初始化 private AbstractGameScreen currScreen;//当前screen private AbstractGameScreen nextScreen;//下一个screen private FrameBuffer currFbo;//当前显存对象 private FrameBuffer nextFbo;//下一个显存对象 private SpriteBatch batch; private float t; private ScreenTransition screenTransition;//显存对象 frame buffer object (FBO)之间的过渡效果 public void setScreen(AbstractGameScreen screen) { setScreen(screen, null); } /** * @param screen 下一个screen * @param screenTransition 当前screen和下一个screen之间的过渡效果 */ public void setScreen(AbstractGameScreen screen, ScreenTransition screenTransition) { int w = Gdx.graphics.getWidth();//获取屏幕宽度 int h = Gdx.graphics.getHeight();//获取屏幕高度 if (!init) { currFbo = new FrameBuffer(Format.RGB888, w, h, false); nextFbo = new FrameBuffer(Format.RGB888, w, h, false); batch = new SpriteBatch(); init = true; } // start new transition nextScreen = screen; nextScreen.show(); // activate next screen nextScreen.resize(w, h); nextScreen.render(0); // let next screen update() once if (currScreen != null) currScreen.pause(); nextScreen.pause(); Gdx.input.setInputProcessor(null); // disable input this.screenTransition = screenTransition; t = 0; } @Override public void render() { // get delta time and ensure an upper limit of one 60th second // System.out.println("FPS:" + Gdx.graphics.getFramesPerSecond()); float deltaTime = Math.min(Gdx.graphics.getDeltaTime(), 1.0f / 60.0f); if (nextScreen == null) { // no ongoing transition if (currScreen != null) currScreen.render(deltaTime); } else { // ongoing transition float duration = 0; if (screenTransition != null) duration = screenTransition.getDuration(); t = Math.min(t + deltaTime, duration); if (screenTransition == null || t >= duration) { // no transition effect set or transition has just finished if (currScreen != null) currScreen.hide(); nextScreen.resume(); // enable input for next screen Gdx.input.setInputProcessor(nextScreen.getInputProcessor()); // switch screens currScreen = nextScreen; nextScreen = null; screenTransition = null; } else { // render screens to FBOs currFbo.begin(); if (currScreen != null) currScreen.render(deltaTime); currFbo.end(); nextFbo.begin(); nextScreen.render(deltaTime); nextFbo.end(); // render transition effect to screen float alpha = t / duration; screenTransition.render(batch, currFbo.getColorBufferTexture(), nextFbo.getColorBufferTexture(), alpha); } } } @Override public void resize(int width, int height) { if (currScreen != null) currScreen.resize(width, height); if (nextScreen != null) nextScreen.resize(width, height); } @Override public void pause() { if (currScreen != null) currScreen.pause(); } @Override public void resume() { if (currScreen != null) currScreen.resume(); } @Override public void dispose() { // if (currScreen != null) // currScreen.hide(); // //currScreen.dispose(); // if (nextScreen != null) // nextScreen.hide(); // if (init) { // currFbo.dispose(); // currScreen = null; // nextFbo.dispose(); // nextScreen = null; // batch.dispose(); // init = false; // } Gdx.app.exit(); } }
相关文章推荐
- 我是运营,我没有假期
- 每个 Linux 游戏玩家都绝不想要的恼人体验
- 在 Fedora 上使用 Steam play 和 Proton 来玩 Windows 游戏
- Steam 让我们在 Linux 上玩 Windows 的游戏更加容易
- 如何使用 Steam Play 在 Linux 上玩仅限 Windows 的游戏
- 新一代iPad适配应用之游戏篇
- VB实现的《QQ美女找茬游戏》作弊器实例
- C#实现洗牌游戏实例
- C#实现的算24点游戏算法实例分析
- C#实现简单的井字游戏实例
- C++编写简单的打靶游戏
- C++实现基于控制台界面的吃豆子游戏
- 纯javascript实现的小游戏《Flappy Pig》实例
- JavaScript实现俄罗斯方块游戏过程分析及源码分享
- JS小游戏之仙剑翻牌源码详解
- JS小游戏之宇宙战机源码详解
- Android基本游戏循环实例分析
- JavaScript游戏之优化篇
- js实现俄罗斯方块小游戏分享
- 用javascript做一个webgame连连看大家看下