加载纹理时LWJGL FPS掉落

时间:2017-01-26 01:52:57

标签: java opengl lwjgl

目前我正在使用LWJGL制作2D游戏。但我遇到了一个问题。我正努力创造一个无限的世界"但是出现了一个问题,因为如果我创建了很多对象,那么帧就会掉落,我相信它与加载到对象的纹理数量有关。 目前,我正在以这种网格方式创建我的世界

for(int x = -WORLDWIDTH; x < WORLDWIDTH; x+= BW)
{
    for(int y = -WORLDHEIGHT; y < WORLDHEIGHT; y += BH)
    {
        Tile newTile = new Tile(x,y,BW,BH,TileType.Grass);
        tiles.add(newTile);
    }
}

BW和BH是块的宽度和高度。我尝试使用if语句

将纹理加载到屏幕上的对象
if(x >= 0-BW && x + width < WIDTH+BW && y >= 0-BH && y+ height < HEIGHT+BH)
{
    DrawQuadTexRotate(getTexture(), getX(), getY(), getWidth(),getHeight(),angle);
}

要加载纹理我这样做

public static void DrawQuadTexRotate(Texture tex, float x, float y, float width, float height, float angle)
{
    tex.bind();
    glTranslatef(x + width / 2, y + height / 2, 0);
    glRotatef(angle, 0 ,0 , 1);
    glTranslatef(- width / 2, - height / 2, 0);
    glBegin(GL_QUADS);
    glTexCoord2f(0,0);
    glVertex2f(0,0);
    glTexCoord2f(1,0);
    glVertex2f(width,0);
    glTexCoord2f(1,1);
    glVertex2f(width,height);
    glTexCoord2f(0,1);
    glVertex2f(0,height);
    glEnd();
    glLoadIdentity();
}

    public static Texture LoadTexture(String path, String fileType)
    {
        Texture tex = null;
        InputStream in = ResourceLoader.getResourceAsStream(path);
    try {
        tex = TextureLoader.getTexture(fileType, in);
    } catch (IOException e) {
        e.printStackTrace();
    }
        return tex;
    }

Tile类基本上有基于它是什么类型的方法来做某些事情。它包含我的DrawQuadTex方法使用的宽度,高度,x,y和纹理

这是tile类

public class Tile {
public float x, y, width, height, angle;
private Texture texture;
private TileType type;
public long timeStart = 0;
public int filled = 1;
public int power = 0;
public int transmitPower = 0;
int connected = 0;
public boolean moveRight,moveLeft,moveUp,moveDown,moveAngle;

public Tile(float x, float y, float width, float height,TileType type)
{
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.type = type;
    this.texture = QuickLoad(type.textureName);
    timeStart = Timer.getTime();
    updateType();
    updateFill();
}
public void draw()
{
    if(x >= 0-BW && x + width < WIDTH+BW && y >= 0-BH && y+ height < HEIGHT+BH)
    {
        DrawQuadTexRotate(getTexture(), getX(), getY(), getWidth(),getHeight(),angle);
    }
}
public void addFood()
{
    if(this.type == TileType.Farm && filled == 1)
    {
        long timing = Timer.getTime();
        if(timing >= (timeStart + 10000))
        {
            Values.food += (1+Artist.riverAmount);
            timeStart = timing;
        }
    }
}
public void mineOre()
{
    if(this.type == TileType.Mine && filled == 1)
    {
        long timing = Timer.getTime();
        if(timing >= (timeStart + 20000))
        {
            for(int i = 0; i < tiles.size();i++)
            {
                if(tiles.get(i).getType() == TileType.Ore)
                {
                    tiles.get(i).setType(TileType.LandTile);
                    Values.oreAmount += 1;
                    timeStart = timing;
                    break;
                }
            }
        }
    }
}
public void move()
{
    if(moveRight == true)
    {
        x -= 10;
    }
    if(moveLeft == true)
    {
        x += 10;
    }
    if(moveUp == true)
    {
        y += 10;
    }
    if(moveDown == true)
    {
        y -= 10;
    }
}
public void testPower()
{
    for(int i = 0; i < tiles.size(); i++)
    {
        if(tiles.get(i).x >= x - BW && tiles.get(i).x <= x + BW && tiles.get(i).y >= y - BH && tiles.get(i).y <= y + BH && tiles.get(i).getType() == TileType.Wire && tiles.get(i).transmitPower == 1 && type == TileType.HomeBasic)
        {
            power = 1;
            break;
        }
        else
        {
            power = 0;
        }

        if(tiles.get(i).x >= x - BW && tiles.get(i).x <= x + BW && tiles.get(i).y >= y - BH && tiles.get(i).y <= y + BH && (tiles.get(i).transmitPower == 1 || tiles.get(i).type == TileType.PowerPlant) && type == TileType.Wire)
        {
            transmitPower = 1;
            break;
        }
        else if(tiles.get(i).type == TileType.Wire && tiles.get(i).transmitPower == 0)
        {
            transmitPower = 0;
        }
        else
        {
            transmitPower = 0;
        }
    }
}
public void updateFill()
{
    if(type == TileType.HomeBasic || type == TileType.Farm || type == TileType.Mine)
    {
        filled = 0;
    }
}
public void stats()
{
    if(type == TileType.HomeBasic)
    {
        //wt.drawString((int)x, (int)(y-25), Integer.toString(filled), Color.white);
    }
}
public float getX() {
    return x;
}

public void setX(float x) {
    this.x = x;
}

public float getY() {
    return y;
}

public void setY(float y) {
    this.y = y;
}

public float getWidth() {
    return width;
}

public void setWidth(float width) {
    this.width = width;
}

public float getHeight() {
    return height;
}

public void setHeight(float height) {
    this.height = height;
}

public Texture getTexture() {
    return texture;
}

public void setTexture(Texture texture) {
    this.texture = texture;
}

public TileType getType() {
    return type;
}

public void setType(TileType type) {
    this.type = type;
    updateType();
}
public void updateType()
{
    this.texture = QuickLoad(type.textureName);
    timeStart = Timer.getTime();
    updateFill();
}

}

    public static Texture QuickLoad(String name)
{
    Texture tex = null;
    tex = LoadTexture("res/" + name + ".png", "PNG");
    return tex;
}

那我该如何解决这个问题?

0 个答案:

没有答案