游戏的逻辑从渲染一个活跃的游戏在安卓与Open-GL分开的最好方法? [英] Best way to separate game logic from rendering for a active-game over android with Open-GL?

查看:399
本文介绍了游戏的逻辑从渲染一个活跃的游戏在安卓与Open-GL分开的最好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习和做了一段时间的小游戏,等我上个月决定开发Android游戏。

对于我来说,从本地C ++ code跳跃到Android的Java并不难,但它给了我这样的头痛想想我怎么能保持从渲染逻辑中分离出来。

我一直在阅读这里和其他网站:

  

最好不要创建另一个线程,只是因为   Android将肯定人主处理。

意味着code会是这样的:

 公共无效onDrawFrame(GL10 GL){
    doLogicCalculations();
    clearScreen();
    drawSprites();
}
 

但我不知道这是最好的方法,因为我不喜欢它的外观一样,如果我把我的逻辑GLRenderer :: onDrawFrame方法中。据我所知,这种方法只是为了绘制,如果我把逻辑那里,我可能会减慢游戏的FPS。不提的是,它伤害了我的POO的概念。

我觉得用线程可能是这样的,我的是如何规划:

主要活动:

 公共无效的onCreate(包savedInstanceState){
    //获取全屏等

    GLSurfaceView视图=新GLSurfaceView(本);
    //配置视图

    游戏管理游戏=新游戏管理();
    game.start(环境,查看);

    的setContentView(视图);
}
 

游戏管理:

  OpenGLRenderer渲染器;
布尔运行;
公共无效启动(上下文的背景下,GLSurfaceView视图){
    this.renderer =新OpenGLRenderer(上下文);
    view.setRenderer(this.renderer);

    //创建Texturelib,创造良好的制度blablabla

    运行=真;
    //创建一个线程来运行游戏管理::更新()
}

公共无效更新(){
    而(运行){
        //更新游戏在这里的逻辑
        //把东西从renderer.toDraw编辑的东西,删除的东西
        //设置运行为false退出比赛
    }
}
 

最后,OpenGLRenderer:

  KindOfMap toDraw;
公共无效onDrawFrame(GL10 GL){
    对于(雪碧我:toDraw)
    {
        i.draw();
    }
}
 

这些都只是获取和想法,没有完全完成。 这种模式将保留所有的分离,会很好看,但它是最好的了性能?

只要我搜索,螺纹游戏最例子使用帆布或surfaceview,那些不会适合我的情况,因为我使用OpenGLES。

因此​​,这里是我的问题:

  

这是分离的最佳方法我   从渲染使用OpenGLES时,游戏逻辑?穿线我   应用?把逻辑在一个单独的方法,并从叫它   抽签的方法?

解决方案

所以,我认为有,你可以去这里两种方式。

  1. 请从 onDrawFrame所有更新()这是类似于使用过剩,而Android将会调用这个函数尽可能多可能。 (把它们关掉与 setRenderMode(RENDERMODE_WHEN_DIRTY))这个函数调用它自己的线程(不是UI线程),这意味着你在这里叫你的逻辑更新。你最初的问题是,这似乎有点乱了,我同意,但只是因为功能被命名为 onDrawFrame()。如果它被称为 onUpdateScene(),然后更新逻辑将适合这种模式很好。但它不是世界上糟糕的事情,它被设计成这样,人们做到这一点。

  2. 给逻辑它自己的线程:这是比较复杂的,因为现在你正在处理的三个线程:UI线程输入,渲染线程 onDrawFrame( )绘制的东西,而逻辑线程连续工作具有输入和呈​​现数据。如果你需要有一个正在发生的事情,即使你的帧速率在下降(例如,precise碰撞响应)一个非常精确的模型使用此。这可能是概念上干净了一点,但不是实际的清洁剂。

我会建议#1。你真的不需要#2。如果你这样做,你可以将其添加后,我猜想,但大多数人只是使用第一个选项,因为硬件是不够快,所以你不必来设计解决它。

I've been studying and making little games for a while, and so i have decided last month to develop games for android.

For me, jumping from native C++ code to Android Java is not that hard, but it gives me such headaches to think about how can I maintain the logic separate from the rendering.

I've been reading here and on other sites that:

It is better not to create another thread for it, just because the android will for sure take charge of the processing.

Meant the code would be like that:

public void onDrawFrame(GL10 gl) {
    doLogicCalculations();
    clearScreen();
    drawSprites();
}

But I'm not sure if this is the best approach, since I don't like how it will look like if I put my logic inside the GLRenderer::onDrawFrame method. As far as I know, this method is just meant to draw, and I may slow down the game fps if I put logic there. Not mentioning that it hurts my concept of POO.

I think that using threads could be that way, its how I was planning:

Main Activity:

public void onCreate(Bundle savedInstanceState) {
    //get fullscreen, etc

    GLSurfaceView view = new GLSurfaceView(this);
    //Configure view

    GameManager game = new GameManager();
    game.start(context, view);

    setContentView(view);
}

GameManager:

OpenGLRenderer renderer;
Boolean running;
public void start(Context context, GLSurfaceView view) {
    this.renderer = new OpenGLRenderer(context);
    view.setRenderer(this.renderer);

    //create Texturelib, create sound system blablabla

    running = true;
    //create a thread to run GameManager::update()
}

public void update(){
    while(running){
        //update game logic here
        //put things edit things and remove things from renderer.toDraw
        //set running to false to quit game
    }
}

and finally, OpenGLRenderer:

KindOfMap toDraw;
public void onDrawFrame(GL10 gl) {
    for(sprite i : toDraw)
    {
        i.draw();
    }
}

These are just to get and idea, not fully complete. This pattern would keep all separated and will be good to see, but is it the best over performance?

As long as i searched, most examples of threaded games use canvas or surfaceview, those wont fit my case, because i'm using OpenGLES.

So here are my questions:

Which is the best way to separate my game logic from the rendering when using OpenGLES? Threading my application? Put the logic in a separate method and just call it from the draw method?

解决方案

So I think there are two ways you can go here.

  1. Do all updates from onDrawFrame(): This is similar to using GLUT, and Android will call this function as often as possible. (Turn that off with setRenderMode(RENDERMODE_WHEN_DIRTY).) This function gets called on it own thread (not the UI thread) and it means you call your logic update here. Your initial issue was that this seems a little messy, and I agree, but only because the function is named onDrawFrame(). If it was called onUpdateScene(), then updating the logic would fit this model well. But it's not the worse thing in the world, it was designed this way, and people do it.

  2. Give logic its own thread: This is more complicated since now you're dealing with three threads: the UI thread for input, the render thread onDrawFrame() for drawing stuff, and the logic thread for continuously working with both input and rendering data. Use this if you need to have a really accurate model of what's happening even if your framerate is dropping (for example, precise collision response). This may be conceptually a little cleaner, but not practically cleaner.

I would recommend #1. You really don't need #2. If you do, you can add it later I guess, but most people are just using the first option because the hardware is fast enough so you don't have to design around it.

这篇关于游戏的逻辑从渲染一个活跃的游戏在安卓与Open-GL分开的最好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆