简体   繁体   English

Java - 为什么我的平台游戏的性能如此之低?

[英]Java - why is the performance of my platformer game so low?

I'm currently working on a platformer game in java, and I'm having trouble figuring out why this decreases performance so much. 我目前正在研究java中的平台游戏,我无法弄清楚为什么这会如此降低性能。 Only the textures in view of the camera are rendered, and I've even tried clearing all objects outside the camera's view, so that the array was almost empty, and I still was unable to get a good framerate. 只渲染相机视图中的纹理,我甚至尝试清除相机视图外的所有对象,这样阵列几乎是空的,我仍然无法获得良好的帧速率。 When I comment out the call to this method, the game runs at 300 FPS, but when I run it, even when I remove everything afterwords, I still only get 40 FPS. 当我注释掉这个方法的调用时,游戏运行速度为300 FPS,但是当我运行它时,即使我删除了所有后续文字,我仍然只能获得40 FPS。 This is not an issue with rendering, as I have tested this thoroughly. 这不是渲染的问题,因为我已经彻底测试了这一点。 Any feedback would be much appreciated. 任何反馈都将非常感激。 Here is the code: 这是代码:

public void buildTerrain(BufferedImage bi) {
    // this method will take an image and build a level based on it.
    int width = bi.getWidth();
    int height = bi.getHeight();
    for(int x = 0; x < width; x++){
        for(int y = 0; y < height; y++){
            int pixel = bi.getRGB(x, y);
            int r = (pixel >> 16) & 0xff;
            int g = (pixel >> 8) & 0xff;
            int b = (pixel) & 0xff;
            if(r == 255 &&
                g == 255 &&
                b == 255)
                h.addObject(new Block(x*32, y*32,
                ID.blockStone,GameState.level1, tex));
            if(r == 0 &&
                g == 0 &&
                b == 255){
                p.setX(x*32);
                p.setY(y*32);
                p.setHeight(64);
            }
        }
    }
}

references: h is a Handler object, witch contains a method addObject(GameObject) Block extends GameObject p is a Player, witch also extends GameObject. 引用:h是一个Handler对象,女巫包含一个方法addObject(GameObject)Block extends GameObject p是一个Player,女巫也扩展了GameObject。

EDIT: this code is not called in a loop, it is ran once at the beginning of each level to load the terrain. 编辑:此代码不在循环中调用,它在每个级别的开头运行一次以加载地形。 All the AddObject() method does is add the Blocks to an array where then are then iterated over in the tick() and render() methods. 所有AddObject()方法都将块添加到数组中,然后在tick()和render()方法中迭代。 Only objects in the scope of the camera are rendered, and the tick() method of blocks is empty. 仅渲染摄像机范围内的对象,并且块的tick()方法为空。

Could you try: 你能尝试一下:

    if(0xffffff00 == (pixel & 0xffffff00))
        h.addObject(new Block(x*32, y*32,
        ID.blockStone,GameState.level1, tex));
    if(0x0000ff00 == (pixel & 0x0000ff00)){
        p.setX(x*32);
        p.setY(y*32);
        p.setHeight(64);
    }

Because I don't understand the need of decomposing (r, g, b) for each pixel while you can do that using a binary & (0xffffff00, it might be 0x00ffffff). 因为我不明白需要为每个像素分解(r,g,b),而你可以使用二进制& (0xffffff00,它可能是0x00ffffff)。

  • in your code, you do width*height*(3 shift + 3 and + 3 equals + 3 equals) operations. 在您的代码中,您执行width*height*(3 shift + 3 and + 3 equals + 3 equals)操作。
  • in my code, you do width*height*2*(and + test) operations. 在我的代码中,你做width*height*2*(and + test)操作。

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

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