0

I have recently moved to Linux Mint 19.3 and run my Java game. In one hand I have FPS that's higher than on Windows 10 (~150 FPS VS ~4000 FPS). But in another hand my game is very lag. And I don't understand why because I don't have this problem in Windows 10. Actually, I am not good at computer graphics, but I think it's problem in VSync or so. That's the screenshot of my GPU temperature and FPS.

enter image description here

GameContainer.java

import javax.swing.JFrame;
import java.awt.*;
import java.awt.image.*; 

public class GameContainer {

public static int width = 320;
public static int height = 240;
public static int scale = 3;
public static String title = "Space Adventure";
public Window window;
public BufferStrategy bs;
public Graphics2D g;
public GameManager gm; 
public KeyInput ki;

public void start() {
    window.canvas.requestFocus();
    long previous = System.currentTimeMillis();
    long current = 0;
    double elapsed = 0;
    double timePerTick = 1000.0 / 60;
    int ticks = 0;
    int frames = 0;
    long timer = previous;
    while (true) {
        current = System.currentTimeMillis();
        elapsed += (current - previous) / timePerTick;
        previous = current;
        while (elapsed >= 1) {
            tick(timePerTick);
            ticks++;
            elapsed -= 1;
        }
        render();
        frames++;
        if (System.currentTimeMillis() - timer >= 1000) {
            System.out.printf("UPS: %3d, FPS: %3d%n", ticks, frames);
            timer += 1000;
            ticks = 0;
            frames = 0;
        }
    }
}

public void tick(double delta) {
    gm.tick(delta);
}

public void render() {
    bs = window.canvas.getBufferStrategy();
    if (bs == null) {
        window.canvas.createBufferStrategy(2);
        return;
    }
    g = (Graphics2D) bs.getDrawGraphics();
    g.scale(scale, scale);
    gm.render(g);
    g.dispose();
    bs.show();
}

public void init(GameManager gm) {
    window = new Window();
    window.create(width , height, scale, title);
    this.gm = gm;
    ki = new KeyInput();
    window.canvas.addKeyListener(ki);
}

}

Archer
  • 67
  • 11
  • Using a tight loop is not the way to go. Try using Swing timer. In fact, I would stay way from AWT and use Swing in general. – WJS Jun 27 '20 at 13:18
  • Thanks. But using `java.awt` package I have more control over the game. For example, I can choose number of buffers. – Archer Jun 27 '20 at 13:37
  • I've already read it. I think if read it I don't solve my problem. Thanks, though. – Archer Jun 27 '20 at 13:57
  • You can also create a bufferStrategy using Swing. But you definitely should take a look at the [Java Tutorials](https://docs.oracle.com/javase/tutorial/index.html) on how to properly paint. And you should rework you timer code. – WJS Jun 27 '20 at 13:57
  • It is very strange that game works fine in Windows but works bad in Linux. – Archer Jun 27 '20 at 14:00
  • You may want to remove one of your tags and put in Linux. I vaguely remember someone having a graphics problem with Linux also. I suggest you remove game-development since this is more than just related to a game. – WJS Jun 27 '20 at 14:03
  • No answers on this question but it may point you to other stuff - e.g. animation test.https://stackoverflow.com/questions/22752240/swing-awt-double-buffering-very-slow-on-linux – WJS Jun 27 '20 at 14:08

0 Answers0