简体   繁体   English

使用OpengGLSurface为Android制作游戏循环的最佳方式

[英]Best way to make game loop for Android using OpengGLSurface

From what I've seen, if I wanna make a NON-opengl game loop what I can do is have a game thread that during the loop will update the game state (or game physics) and also update the graphics by locking a Canvas from a (normal) SurfaceView , doing the drawing via Canvas.drawXXX(...) methods and then unlocking it at which point the graphics get updated for the loop. 从我所看到的,如果我想做一个非opengl游戏循环我可以做的是有一个游戏线程,在循环期间将更新游戏状态(或游戏物理),并通过锁定Canvas来更新图形一个(普通的) SurfaceView ,通过Canvas.drawXXX(...)方法进行绘图,然后将其解锁,此时图形将更新为循环。

I don't really get how to do that when using GLSurfaceView and Renderer . 使用GLSurfaceViewRenderer时,我真的不知道怎么做。 From what I understand, I can no longer have my hand-made gameThread draw on demand as I had with the regular surface and it's Canvas , because now there's a rendering thread that will call Renderer.onDrawFrame() (on my implementation of the renderer class that I pass to the GLSurfaceView). 根据我的理解,我不能再像我在普通表面和Canvas那样按照手工制作的gameThread绘图,因为现在有一个渲染线程会调用Renderer.onDrawFrame() (在我的渲染器实现上)我传递给GLSurfaceView的类。

My question is then, what's a good way to make a game loop with GLSurfaceView and Renderer? 那么我的问题是,用GLSurfaceView和Renderer制作游戏循环的好方法是什么? Should I still make my separate game thread, but only handle game state (physics) updates in it and then use that game state in my implementation of Renderer.onDrawFrame() to actually draw graphics based on current state? 我是否仍然应该制作单独的游戏线程,但只处理其中的游戏状态(物理)更新,然后在我的Renderer.onDrawFrame()实现中使用该游戏状态来实际绘制基于当前状态的图形? Should I only use the Rendere's thread to also do state updates? 我应该只使用Rendere的线程来进行状态更新吗?

Well, as it turns out, best way to go is to use the thread provided by the GLSurface. 好吧,事实证明,最好的方法是使用GLSurface提供的线程。 The hook to it comes through an implementation of the Renderer interface. 它的钩子来自Renderer接口的实现。 Effectively the Renderer's onDrawFrame() method can be considered analogous to a regular Thread's run() method. 实际上,Renderer的onDrawFrame()方法可以被认为类似于常规Thread的run()方法。 This kind of throws out the window the concept of a fixed updates per second paradigm (not really, but it's too convoluted to do in this context) but on the other hand you get optimal graphics updates. 这种抛出窗口的每秒固定更新范式的概念(不是真的,但在这种情况下它太复杂)但另一方面,你获得了最佳的图形更新。

There's a great tutorial on Android and OpenGL here: 这里有关于Android和OpenGL的精彩教程:

http://insanitydesign.com/wp/projects/nehe-android-ports/ http://insanitydesign.com/wp/projects/nehe-android-ports/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM