Lags in game loop

问题: It's my game loop code: public void run() { running = true; boolean renderCheck = false; double firstTime = 0; double lastTime = System.nanoTime() / 1000...

问题:

It's my game loop code:

public void run() {
    running = true;

    boolean renderCheck = false;
    double firstTime = 0;
    double lastTime = System.nanoTime() / 1000000000.0;
    double passedTime = 0;
    double unprocessedTime = 0;

    double frameTime = 0;
    int frames = 0;
    int fps = 0;

    while (running) {
        firstTime = System.nanoTime() / 1000000000.0;
        passedTime = firstTime - lastTime;
        lastTime = firstTime;

        unprocessedTime += passedTime;
        frameTime += passedTime;

        while (unprocessedTime >= UPDATE_CAP) {
            tick();
            unprocessedTime -= UPDATE_CAP;
            renderCheck = true;
        }

        if (frameTime >= 1.0) {
            frameTime = 0;
            fps = frames;
            frames = 0;
            System.out.println(fps);
        }

        if (renderCheck) {
            render();
            frames++;
            renderCheck = false;
        } else {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

It's a render zone:

private void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null) {
        this.createBufferStrategy(3);
        return;
    }

    Graphics graphics = bs.getDrawGraphics();

    graphics.setColor(Color.black);
    graphics.fillRect(0, 0, WIDTH, HEIGHT);

    handler.render(graphics);

    graphics.dispose();
    bs.show();
}

And here is a tick part(it's not necessary to show other code for handler because it'll be so much to read):

private void tick() {
    handler.tick();
}

So the main problem is in the next thing. When i press the button my character has to start moving. And, actually, it does but with some delays which make this process look terrible. Than after a second, all is going perfectly. (I've already checked CPU loading - all goes normal)

This problem is happening only on linux PC. I've checked it on Windows 10 in exe format and all was working fine! That's a bit weird.


回答1:

This lazy initialization might be your problem:

private void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null) {
        this.createBufferStrategy(3);
        return;
    }
    ...

Did you consider the fact on the first call of your render() function the getBufferStrategy() will return null at which point

  • you will first create it (that is ok)

  • then return without any action (that is suspicious)

Subsequent calls of render would actually perform rendering ... later. If you know for sure that you will need that bufferStrategy anyway, it would make sense to create it straight away when initializing your system.


回答2:

Soo, finally i found a solution for fixing this issue!

Just set system properties to java2d in your static main method by writing this code:

public static void main(String argsp[]) {
    System.setProperty("sun.java2d.opengl", "true");
    new Game();
}
  • 发表于 2019-01-21 01:48
  • 阅读 ( 231 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除