尽管更改渲染模式,GLSurfaceView 仍持续渲染

发布于 2024-10-05 16:40:36 字数 993 浏览 0 评论 0原文

我正在尝试创建一个显示游戏区域地图的 GLSurfaceView。当玩家移动时,游戏活动会调用highlightSpot,这反过来会触发渲染请求。我唯一想重新绘制视图的时间是当玩家移动时。

然而,在我当前的实现中,尽管在 GLSurfaceView 上调用 setRenderMode(RENDERMODE_WHEN_DIRTY),但其渲染模式似乎仍然是连续的。为了进行检查,我在 onDrawFrame 方法中添加了一个 println 语句,当我运行应用程序时,输出很快填满了我的 logcat,而玩家甚至没有移动一次——它显然没有按照我的预期运行。为了使视图仅在被要求时渲染,我还需要做其他事情吗?

(此代码的大部分源自 http://insanitydesign.com/ 上的教程为了简洁起见,我省略了 onDrawFrame、OnSurfaceChanged 和 onSurfaceCreated 方法,因为我没有更改渲染模式或在这些方法中的任何位置请求渲染。认为这可能相关,我也可以发布这些。)

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();
    }
}

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

比忠 2024-10-12 16:40:36

好的,我想我已经解决了这个问题。设置渲染模式的位置似乎是包含 GLSurfaceView 对象的类,而不是在 GLSurfaceView 构造函数中。另外(我认为我在GLSurfaceView的Android文档中忽略了这一点)在设置渲染器之前,您无法设置 GLSurfaceView 的渲染模式。这也许就是为什么尝试在构造函数中设置渲染模式不起作用的原因。

这似乎迫使它只在我想要的时候渲染,这正是我想要的:

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();
}
}

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();
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文