本文介绍了尽管改变渲染模式GLSurfaceView不断呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建显示游戏区域的地图一GLSurfaceView。当玩家移动,游戏活动呼吁highlightSpot,这反过来会触发呈现请求。唯一一次我想重新绘制的看法是球员的动作时。

不过,我目前的执行情况,尽管调用 setRenderMode(RENDERMODE_WHEN_DIRTY)在我GLSurfaceView,它的渲染模式似乎仍是连续的。要检查,我在onDrawFrame方法抛出一个println语句,以及当我运行我的应用程序,输出迅速填满了我的logcat不动的球员,甚至once--它显然不是表现为我的本意。有没有别的东西,我需要做的,为了使视图,只有当问渲染?

(此code大部分是从教程衍生的http://insanitydesign.com/wp/projects/nehe-android-ports/.我忽略我的onDrawFrame,OnSurfaceChanged和onSurfaceCreated方式为简明起见,因为我不改变的渲染模式或请求在这些方法中渲染的任何地方。如果有人认为这可能是相关的,我可以张贴这些呢。)

 公共类SurfaceViewClass扩展GLSurfaceView实现渲染器{
    公共SurfaceViewClass(上下文的背景下){
        超(上下文);

        ...

        this.setRenderer(本);
        this.setRenderMode(RENDERMODE_WHEN_DIRTY);
    }

    公共无效highlightSpot(INT X,int y)对{
        / *这里修改一些变量* /
        ...

        this.requestRender();
    }
}
 

解决方案

好了,我想我得到这个整理出来。设置渲染模式的地方似乎是包含您的GLSurfaceView对象,而不是在GLSurfaceView构造方法的类。另外(这是我觉得我忽略the Android的文档GLSurfaceView ),然后再设置它的渲染,你不能设置GLSurfaceView的渲染模式。这也许是为什么尝试设置渲染在构造模式下无法正常工作。

这似乎迫使它渲染只有当我想它,这正是我想要的:

 公共类游戏延伸活动{
私人GLSurfaceView glSurface;
私人SurfaceViewClass SVC;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    glSurface =(GLSurfaceView)findViewById(R.id.SurfaceView01);

    SVC =新SurfaceViewClass(本);
    glSurface.setRenderer(SVC);
    glSurface.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}

公共无效movePlayer(){
    svc.highlightSpot(位置[播放],0);
    glSurface.requestRender();
}
}
 

I'm trying to create a GLSurfaceView that displays a map of a game area. When the player moves, the game activity calls highlightSpot, which in turn should trigger a render request. The only time I want to re-draw the view is when the player moves.

However, with my current implementation, despite calling setRenderMode(RENDERMODE_WHEN_DIRTY) on my GLSurfaceView, its render mode still seems to be continuous. To check, I threw a single println statement in my onDrawFrame method, and when I run my application, the output quickly fills up my logcat without the player moving even once-- it's clearly not behaving as I intended. Is there something else I need to do in order to make the view render only when asked?

(The bulk of this code is derived from the tutorials at http://insanitydesign.com/wp/projects/nehe-android-ports/. I omitted my onDrawFrame, OnSurfaceChanged, and onSurfaceCreated methods for the sake of conciseness, as I am not changing the render mode or requesting a render anywhere in those methods. If someone thinks it might be relevant, I can post those too.)

public class SurfaceViewClass extends GLSurfaceView implements Renderer {
    public SurfaceViewClass(Context context) {
        super(context);

        ...

        this.setRenderer(this);
        this.setRenderMode(RENDERMODE_WHEN_DIRTY);
    }

    public void highlightSpot(int x, int y) {
        /* change some variables here */
        ...

        this.requestRender();
    }
}
解决方案

OK, I think I got this sorted out. The place to set the render mode seems to be the class that contains your GLSurfaceView object, not in the GLSurfaceView constructor. Also (something I think I overlooked in the Android documentation for GLSurfaceView) you can't set the render mode of the GLSurfaceView before you set its renderer. This is perhaps why attempting to set the render mode in the constructor does not work.

This seems to force it to render only when I want it to, which is exactly what I wanted:

public class Game extends Activity {
private GLSurfaceView glSurface;
private SurfaceViewClass svc;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    glSurface = (GLSurfaceView) findViewById(R.id.SurfaceView01);

    svc = new SurfaceViewClass(this);
    glSurface.setRenderer(svc);
    glSurface.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}

public void movePlayer() {
    svc.highlightSpot(location[PLAYER], 0);
    glSurface.requestRender();
}
}

这篇关于尽管改变渲染模式GLSurfaceView不断呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 05:40