如果相机看到它,LibGDX只渲染

时间:2014-10-22 12:05:04

标签: java optimization camera libgdx render

所以,我创造了我的第一个游戏。在电脑上工作(60FPS),但在我的手机上,我有50FPS,有时它跳到30再回来,你看它就像一个滞后。我已将所有对象存储在ArrayList中。因此,我认为,它可能是因为我正在渲染所有内容,所以我想知道,如果只是在相机视图中才能渲染事物。我试图使用视口,但它们仍在渲染,但你不会看到它们。所以我的问题是,如何计算相机视图中是否有东西。

如果他们在相机视图中,我试图渲染它们。而且我遇到了麻烦。

public void render () {
            if (space == 1){ //if you are alive.. space 1 means you are alive, 0 means you are in menu
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            sb.setProjectionMatrix(cam.combined);
            camera.position.x = player1.x + player1.getSize()/2;
            camera.position.y = player1.y + player1.getSize()/2;
            camera.update();
            sb.setProjectionMatrix(camera.combined);
            sb.begin();
            sb.draw(bg, 0, 0, 1920 , 1080 );
            //HP //it means food
            if (HP > 12.4f)
            {
                sb.draw(Jedlo2, player1.x + (450 + player1.seeSize()/2), player1.y + (320 + player1.seeSize()/2), 50, 50);
            }
            else if (HP < 0.5f)
            {
                sb.draw(Jedlo, player1.x + (450 + player1.seeSize()/2), player1.y + (320 + player1.seeSize()/2), 50, 50);
            }
            else if (HP < 12.5f)
            {
                sb.draw(Jedlo3, player1.x + (450 + player1.seeSize()/2), player1.y + (320 + player1.seeSize()/2), 50, 50);
            }
            //kornut 2
            if (HP > 37.4f)
            {
                sb.draw(Jedlo2, player1.x + (500 + player1.seeSize()/2), player1.y + (320 + player1.seeSize()/2), 50, 50);
            }
            else if (HP > 24 && HP < 37.5f)
            {
                sb.draw(Jedlo3, player1.x + (500 + player1.seeSize()/2), player1.y + (320 + player1.seeSize()/2), 50, 50);
            }
            else if (HP < 25)
            {
                sb.draw(Jedlo, player1.x + (500 + player1.seeSize()/2), player1.y + (320 + player1.seeSize()/2), 50, 50);
            }
            //kornut 3
            if (HP > 62.4f)
            {
                sb.draw(Jedlo2, player1.x + (550 + player1.seeSize()/2), player1.y + (320 + player1.seeSize()/2), 50, 50);
            }
            else if (HP > 49.9f && HP < 62.5f)
            {
                sb.draw(Jedlo3, player1.x + (550 + player1.seeSize()/2), player1.y + (320 + player1.seeSize()/2), 50, 50);
            }
            else if (HP < 50)
            {
                sb.draw(Jedlo, player1.x + (550 + player1.seeSize()/2), player1.y + (320 + player1.seeSize()/2), 50, 50);
            }
            //kornut 4
            if (HP > 87.4f)
            {
                sb.draw(Jedlo2, player1.x + (600 + player1.seeSize()/2), player1.y + (320 + player1.seeSize()/2), 50, 50);
            }
            else if (HP > 74.9f && HP < 87.5f)
            {
                sb.draw(Jedlo3, player1.x + (600 + player1.seeSize()/2), player1.y + (320 + player1.seeSize()/2), 50, 50);
            }
            else if (HP < 75)
            {
                sb.draw(Jedlo, player1.x + (600 + player1.seeSize()/2), player1.y + (320 + player1.seeSize()/2), 50, 50);
            }
            //HP
            font.draw(sb, "Score: " + player1.getScore(), player1.x - (620 - player1.seeSize()/2),  player1.y + (370 + player1.seeSize()/2));
    h.draw(sb, "FPS: " + Gdx.graphics.getFramesPerSecond(), player1.x - (620 - player1.seeSize()/2),  player1.y + (300 + player1.seeSize()/2));
            sb.end();
        //entities  
        for (Entity e : entities)
            {  
                e.render(sb); 
            }
        //entities
        //Touchpad
            player1.setrlx(player1.pos(true) + touchpad.getKnobPercentX()*blspeed);
            player1.setrly(player1.pos(false) + touchpad.getKnobPercentY()*blspeed);
            if (touchpad.getKnobPercentX()*blspeed > 0.0f && touchpad.getKnobPercentY()*blspeed > -1.5f && touchpad.getKnobPercentY()*blspeed < 1f)
            {
                player1.an.setRow(0);
            }
            else if (touchpad.getKnobPercentX()*blspeed < 0.0f && touchpad.getKnobPercentY()*blspeed < 1.0f && touchpad.getKnobPercentY()*blspeed > -1.5f)
            {
                player1.an.setRow(1);
            }
            else if (touchpad.getKnobPercentY()*blspeed > 0.0f)
            {
                player1.an.setRow(3);
            }
            else if (touchpad.getKnobPercentY()*blspeed < 0.0f)
            {
                player1.an.setRow(2);
            }
            //Touchpad
            Gdx.input.setInputProcessor(stage);
            stage.act(Gdx.graphics.getDeltaTime()); 
            stage.draw();
            HP -= 0.05f;
            time += Gdx.graphics.getDeltaTime();
            time2 += Gdx.graphics.getDeltaTime();
            if (time >= period)
            {
                time -= period;
                update();
            }

            if (time2 >= p2)
            {
                time2 -= p2;
                updatemobs();
            }
            }
            else if (space == 0) //if you are in menu, my problem is if you are not in menu so you dont need to check this //right now, in this code, the menu is not updated, but there is only small things, you dont need to check that.
            {
                Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
                entities.clear();
                JukeBox.stop("water");
                if (player1.getScore() > lastscore)
                {
                    Bin.create(player1.getScore());
                    lastscore = player1.getScore();
                }
                sb.setProjectionMatrix(camera.combined);
                sb.setProjectionMatrix(cam.combined);
                sb.begin();
                sb.draw(but, 1280/2.3f, 720/2.4f, 200, 100);
                if (player1.getScore() > 0)
                {
                    offx2 = h.getBounds("Your Score: " + player1.getScore()).width/2f;
                    h.draw(sb, "Your Score: " + player1.getScore(), 1280/2-offx2, 600);
                }
                offx = fo.getBounds("Your highscore: " + player1.getScore()).width/2f;
                fo.draw(sb, "Your highscore: " + lastscore, 1280/2-offx, 700);
                sb.end();
                if (Gdx.input.isButtonPressed(0))
                {
                     x = Gdx.input.getX();
                    y = Gdx.input.getY();
                    if ( x > 1279/2.3f && x < 1725/2.3f && y > 775/2.4f && y < 1000/2.4f )
                    {
                        JukeBox.play("button");
                    space = 1;
                    player1.setScore();
                    player1.setPos();
                    player1.SetrlSize();
                   // cr();
                    cr();
                    }
                }
            }
        }

实际上,我试图删除这些更新调用,但它没有帮助,因此问题不存在。

1 个答案:

答案 0 :(得分:1)

您知道相机的位置和大小,并且您知道各种物体的位置。所以你可以计算绘制的内容和不绘制的内容。简单地检查事物的位置比绘制它们更有效率。

更好的是提前知道要绘制的内容,这样就不必遍历所有对象坐标。对于固定在位的瓦片图,很容易确定要绘制的瓦片和不绘制的瓦片。对于更多动态对象,您可以尝试将对象放在列表中,并根据位置对其进行排序。如果您有很多对象,则必须查看散列映射或四叉树以进行有效排序。

如果您在屏幕外有很多事情发生,您可以选择将您的游戏世界分成几块,只更新/绘制靠近实际屏幕的部分。这意味着您必须从世界上的这些部分/部分中移除和添加动态对象。考虑一下你是否真的需要提前4个屏幕宽度移动。

PS: 由于抽奖电话的数量,你确定你有FPS问题吗?你在使用spritesheets,过多的循环等吗?

修改 你告诉过你每个帧都要进行大量的绘制调用,但我找不到你正在做这个的循环。我只看到一些hud元素并且Jedlo jedlo2 jedlo3被绘制,这肯定不是瓶颈。

一步一步练习。尝试熟悉屏幕和舞台,因为你可以用这些来绘制你的hud / UI。而不是jedlo1 to 3,我认为是框架,你可以创建一个方法,甚至是一个为你做这个的类。这样,您可以保持方法和主要类的清洁和有条理。