render()处于活动状态时计算机冻结

时间:2016-10-08 12:00:17

标签: java

我正在尝试用java创建我的第一个游戏,但是偶然发现了一个问题。当我第一次编写我的游戏循环和FPS计数器时,我试图自己做,但它不能正常工作,所以我使用了我在网上找到的代码:

public void run(){
    long lastTime = System.nanoTime();
    double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;
    long timer = System.currentTimeMillis();
    int frames = 0;
    while(running){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        while(delta >= 1){
            tick();
            delta--;
        }
        if(running){
            render();
        }
        frames++;

        if(System.currentTimeMillis() - timer > 1000){
            timer += 1000;
            System.out.println("FPS: " + frames);
            frames = 0;
        }
    }
    stop();
}

它工作得很好,但是当我添加render()函数时,窗口就像冻结一样。我仍然可以关闭窗口,但有时需要重新启动整个计算机。这是渲染功能。

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

    Graphics g = bs.getDrawGraphics();

    g.setColor(Color.green);
    g.fillRect(0, 0, WIDTH, HEIGHT);

    handler.render(g);

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

如果我在渲染功能中注释掉所有内容,我会看到一个白色的屏幕显示我可以四处移动并且在16200000左右显示“常量”FPS。但是当渲染功能中的代码处于活动状态时,FPS看起来像这样:

FPS: 8644
FPS: 1
FPS: 5977
FPS: 3189
FPS: 3930
FPS: 8120
FPS: 1
FPS: 8024
FPS: 1

没有稳定性或一致性,有人知道我做错了吗?我的操作系统是Ubuntu Mate,如果它有任何重要性,我使用openjdk版本1.8.0_91。提前谢谢。

完整代码:

package com.tutorial.main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

public class Game extends Canvas implements Runnable{

private static final long serialVersionUID = 1550691097823471818L;

public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;

private Thread thread;
private boolean running = false;

private Handler handler;

public Game(){
    new Window(WIDTH, HEIGHT, "Lets Build!", this);
    handler = new Handler();

    handler.addObject(new Player(100, 100, ID.Player));
}

public synchronized void start(){
    thread = new Thread(this);
    thread.start();
    running = true;
}

public synchronized void stop(){
    try{
        thread.join();
        running = false;
    }catch(Exception e){
        e.printStackTrace();
    }
}

public void run(){
    long lastTime = System.nanoTime();
    double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;
    long timer = System.currentTimeMillis();
    int frames = 0;
    while(running){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        while(delta >= 1){
            tick();
            delta--;
        }
        if(running){
            render();
        }
        frames++;

        if(System.currentTimeMillis() - timer > 1000){
            timer += 1000;
            System.out.println("FPS: " + frames);
            frames = 0;
        }
    }
    stop();
}

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

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

    Graphics g = bs.getDrawGraphics();

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

    handler.render(g);

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

public static void main(String args[]){
    new Game();
}
}

1 个答案:

答案 0 :(得分:0)

问题是我的操作系统。当我安装JDK 8切换到Windows时,一切都很顺利。也许因为我在我的Linux机器上安装了OpenJDK有一些问题,老实说我不知道​​。将查看它,看看我是否找到它。如果是的话会更新。