二,游戏的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
- 二,游戏的game loop搭建
- Game Loop
- Game Loop - Game Programming Patterns
- 5-The Game Loop
- The Game Loop
- Game Loop的几种实现方式(转载)
- Android基于box2d开发弹弓类游戏[二]-------------游戏界面的搭建&移动游戏场景
- Unity 游戏框架搭建 (二) 单例的模板
- Unity 游戏框架搭建 (二) 单例的模板
- Unity 游戏框架搭建 (二) 单例的模板
- 游戏编程模式(Game Programming Patterns)的C#&Unity练习二:观察者模式
- 3-A Basic Game Loop
- Craftyjs系列7-Game Loop
- Android Game Development - A Basic Game Loop
- 游戏game
- 【用Cocos Creator给暗恋的女生写一个游戏(5)】——(Run Game)场景搭建
- 【用Cocos Creator给暗恋的女生写一个游戏(10)】——(Jump Game) 场景搭建等
- 走进Run Loop的世界 (二):如何配置Run Loop Sources
- Servlet请求重定向&中文插入数据库
- Android 直接转拨分机号码
- 使用image drawInRect: 方法改变图片大小后,图片画质变差了--(头像修改时)
- 记一次IOS下framwork下.h文件找不到的奇葩bug
- C#Enum类型向sqlserver插入问题
- 二,游戏的game loop搭建
- span>a下拉显示
- 打印9*9 乘法表
- React入门:关于虚拟DOM(Virtual DOM)
- [NOIP 2011]计算系数 乘法逆元+扩展欧几里得
- 移动端APP——微信(1)手机桌面控件和启动画面显示与隐藏
- Security > 对称加密算法AES
- ado.net存储字符串的几种数据类型
- Tomcat环境变量?JDK环境变量?