Android游戏循环,更新和绘制分开吗?

发布于 2024-12-20 17:49:05 字数 1478 浏览 2 评论 0 原文

我刚刚开始学习安卓开发来制作游戏。以前接触过,但只了解了基础知识。我有点困惑如何设置主循环。我一直对 XNA (C#) 很感兴趣,而且我喜欢分离的更新/绘制循环。

我想知道典型的 Android 游戏循环是如何工作的?我在网上搜索了一下,发现了两种方法:

public void run() {

         while (running) {

                //Method 1: update is called here 
                view.update();
                Canvas c = null;

                try {

                       c = view.getHolder().lockCanvas();

                       synchronized (view.getHolder()) {

                           //Method 2: update is called inside view.onDraw
                              view.onDraw(c);

                       }

                } finally {

                       if (c != null) {

                              view.getHolder().unlockCanvasAndPost(c);
                       }
                }
         }
   }

让我们以更新游戏实体作为这两种方法的例子:

        //METHOD1

public void onDraw(Canvas canvas)
{
    for (GameEntity entity : entities)
    {
        entity.update();
        entity.draw(canvas);
    }
}

//END METHOD 1

//METHOD 2

public void update()
{
    for (GameEntity entity : entities)
    {
        entity.update();
    }
}

public void draw(Canvas canvas)
{
    for (GameEntity entity : entities)
    {
        entity.draw(canvas);
    }
}

//END METHOD 2

现在我对线程没有任何经验,所以我不知道 XNA 是如何在后面进行更新/绘制循环的xna 中的屏幕。 但使用方法 1,我必须循环遍历所有实体两次,一次用于更新,另一次用于单独绘制。我担心这会影响性能,但我已经在网上的示例中看到了这一点。

我是否遗漏了某些内容,或者我是否正确?方法 2 是性能最佳的方法吗?

I'm just picking up android development to make a game. Touched it before, but only picked up the basics. I'm a bit confused how to set up a main loop. I've been into XNA (C#) and I love the separated update/draw loop.

I was wondering how a typical android gameloop works? I've searched online and came across 2 methods:

public void run() {

         while (running) {

                //Method 1: update is called here 
                view.update();
                Canvas c = null;

                try {

                       c = view.getHolder().lockCanvas();

                       synchronized (view.getHolder()) {

                           //Method 2: update is called inside view.onDraw
                              view.onDraw(c);

                       }

                } finally {

                       if (c != null) {

                              view.getHolder().unlockCanvasAndPost(c);
                       }
                }
         }
   }

Let's take the updating of game entities as an example for the 2 methods:

        //METHOD1

public void onDraw(Canvas canvas)
{
    for (GameEntity entity : entities)
    {
        entity.update();
        entity.draw(canvas);
    }
}

//END METHOD 1

//METHOD 2

public void update()
{
    for (GameEntity entity : entities)
    {
        entity.update();
    }
}

public void draw(Canvas canvas)
{
    for (GameEntity entity : entities)
    {
        entity.draw(canvas);
    }
}

//END METHOD 2

Now I have no experience with threads whatsoever, so I have no idea how XNA does the update/draw loops behind the screens in xna.
But using method 1, I would have to loop through all the entities twice, once for updating and another time for seperate drawing. I'm afraid this will kill the performance, but I háve seen this in samples online.

Am I missing something or am I right and is method 2 the best performance wise?

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

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

发布评论

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

评论(1

四叶草在未来唯美盛开 2024-12-27 17:49:05

循环次数并不重要,重要的是执行了多少次操作。而且所做的动作数量也基本相同。因为第二个“for”只为每个实体添加了一个假设的“if”。所以它并不多。

但它使您能够只执行其中一项操作,而不必被迫执行两项操作。
例如:如果我希望游戏每秒更新 60 次,但只绘制 40 fps,我只能采用方法 2。这可以让您以更少的计算获得更流畅的游戏,但前提是您使用得当。

如果 darw 和 update 以相同的速率发生,那么将它们分开是愚蠢的

It matters not how many times you loop since it only matters how many actions you do. And the amount of actions done are basicly the same. since the second "for" only adds an one more supposed "if" for each entety. So its not much.

But it gives you the ability to do only one of the actions and not forced to do both.
for example: if I want the game to update 60 times per sec but only draw 40 fps, I can only do that in method 2. This allows you to have a more fluid game with less calculations, but only if you use it right.

If you have the darw and update happen at the same rate, then it is stupid to split them

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