二,游戏的game loop搭建

来源:互联网 发布:淘宝api接口开发 编辑:程序博客网 时间:2024/05/14 02:16

关于如何搭建环境的问题在这里不再赘述,网上可以搜到很多,或者你可以在github上libgdx页面找到其他所需要的教程,而且已经在提供的下源码载文件中提供了完整的包和所需要的各种资源,直接导入Eclipse即可。

这次主要讲解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;//当前screenprivate AbstractGameScreen nextScreen;//下一个screenprivate 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 transitionnextScreen = screen;nextScreen.show(); // activate next screennextScreen.resize(w, h);nextScreen.render(0); // let next screen update() onceif (currScreen != null)currScreen.pause();nextScreen.pause();Gdx.input.setInputProcessor(null); // disable inputthis.screenTransition = screenTransition;t = 0;}@Overridepublic 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 transitionif (currScreen != null)currScreen.render(deltaTime);} else {// ongoing transitionfloat 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 finishedif (currScreen != null)currScreen.hide();nextScreen.resume();// enable input for next screenGdx.input.setInputProcessor(nextScreen.getInputProcessor());// switch screenscurrScreen = nextScreen;nextScreen = null;screenTransition = null;} else {// render screens to FBOscurrFbo.begin();if (currScreen != null)currScreen.render(deltaTime);currFbo.end();nextFbo.begin();nextScreen.render(deltaTime);nextFbo.end();// render transition effect to screenfloat alpha = t / duration;screenTransition.render(batch, currFbo.getColorBufferTexture(), nextFbo.getColorBufferTexture(), alpha);}}}@Overridepublic void resize(int width, int height) {if (currScreen != null)currScreen.resize(width, height);if (nextScreen != null)nextScreen.resize(width, height);}@Overridepublic void pause() {if (currScreen != null)currScreen.pause();}@Overridepublic void resume() {if (currScreen != null)currScreen.resume();}@Overridepublic 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();}}


0 0
原创粉丝点击