您的位置:首页 > 运维架构

二,游戏的game loop搭建

2015-11-03 22:21 387 查看
关于如何搭建环境的问题在这里不再赘述,网上可以搜到很多,或者你可以在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;//当前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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  libgdx 游戏