It is because I want the game loop to be precise no matter how long it takes to execute. If I use Thread.sleep() there will eventually be some millis delay, and that would be growing over time
You'll never get something that's precise. A lost ms or two is no big deal and if you write your loop intelligently it will automatically be picked up in the next timestep. You're essentially doing a fixed timestep right now, but you're doing it wrong.
Here is a fixed game loop test I made. It just draws a ball bouncing around, using a fixed timestep and a target FPS. If the FPS would exceed the target, it yields the thread. This keeps the game from being a processor hog (unless you need it).
Note that you very rarely need a 60 GAME_HERTZ, I just kept it there for the test. Oftentimes you can get away with a game hertz of 20, and it looks fine as long as the FPS is high.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class GameLoopTest extends JFrame implements ActionListener { private GamePanel gamePanel = new GamePanel(); private JButton startButton = new JButton("Start"); private JButton quitButton = new JButton("Quit"); private boolean running = false; private int fps = 60; private int frameCount = 0; public GameLoopTest() { super("Fixed Timestep Game Loop Test"); Container cp = getContentPane(); cp.setLayout(new BorderLayout()); JPanel p = new JPanel(); p.setLayout(new GridLayout(1,2)); p.add(startButton); p.add(quitButton); cp.add(gamePanel, BorderLayout.CENTER); cp.add(p, BorderLayout.SOUTH); setSize(500, 500); startButton.addActionListener(this); quitButton.addActionListener(this); } public static void main(String[] args) { GameLoopTest glt = new GameLoopTest(); glt.setVisible(true); } public void actionPerformed(ActionEvent e) { Object s = e.getSource(); if (s == startButton) { running = !running; if (running) { startButton.setText("Stop"); runGameLoop(); } else { startButton.setText("Start"); } } else if (s == quitButton) { System.exit(0); } } public void runGameLoop() { Thread loop = new Thread() { public void run() { gameLoop(); } }; loop.start(); } private void gameLoop() { final double GAME_HERTZ = 60.0; final double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ; final int MAX_UPDATES_BEFORE_RENDER = 5; double lastUpdateTime = System.nanoTime(); double lastRenderTime = System.nanoTime(); final double TARGET_FPS = 60; final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS; int lastSecondTime = (int) (lastUpdateTime / 1000000000); while (running) { double now = System.nanoTime(); int updateCount = 0; while( now - lastUpdateTime > TIME_BETWEEN_UPDATES && updateCount < MAX_UPDATES_BEFORE_RENDER ) { updateGame(); lastUpdateTime += TIME_BETWEEN_UPDATES; updateCount++; } if (lastUpdateTime - now > TIME_BETWEEN_UPDATES) { lastUpdateTime = now - TIME_BETWEEN_UPDATES; } float interpolation = Math.min(1.0f, (float) ((now - lastUpdateTime) / TIME_BETWEEN_UPDATES) ); drawGame(interpolation); lastRenderTime = now; int thisSecond = (int) (lastUpdateTime / 1000000000); if (thisSecond > lastSecondTime) { System.out.println("NEW SECOND " + thisSecond + " " + frameCount); fps = frameCount; frameCount = 0; lastSecondTime = thisSecond; }
while ( now - lastRenderTime < TARGET_TIME_BETWEEN_RENDERS && now - lastUpdateTime < TIME_BETWEEN_UPDATES) { Thread.yield(); now = System.nanoTime(); } } } private void updateGame() { gamePanel.update(); } private void drawGame(float interpolation) { gamePanel.setInterpolation(interpolation); gamePanel.repaint(); } private class GamePanel extends JPanel { float interpolation; float ballX, ballY, lastBallX, lastBallY; int ballWidth, ballHeight; float ballXVel, ballYVel; int lastDrawX, lastDrawY; public GamePanel() { ballX = lastBallX = 100; ballY = lastBallY = 100; ballWidth = 25; ballHeight = 25; ballXVel = (float) Math.random() * 10 - 5; ballYVel = (float) Math.random() * 10 - 5; } public void setInterpolation(float interp) { interpolation = interp; } public void update() { lastBallX = ballX; lastBallY = ballY; ballX += ballXVel; ballY += ballYVel; if (ballX + ballWidth/2 >= getWidth()) { ballXVel *= -1; ballX = getWidth() - ballWidth/2; } else if (ballX - ballWidth/2 <= 0) { ballXVel *= -1; ballX = ballWidth/2; } if (ballY + ballHeight/2 >= getHeight()) { ballYVel *= -1; ballY = getHeight() - ballHeight/2; } else if (ballY - ballHeight/2 <= 0) { ballYVel *= -1; ballY = ballHeight/2; } } public void paintComponent(Graphics g) { g.setColor(getBackground()); g.fillRect(lastDrawX-1, lastDrawY-1, ballWidth+2, ballHeight+2); g.fillRect(5, 0, 75, 30); g.setColor(Color.RED); int drawX = (int) ((ballX - lastBallX) * interpolation + lastBallX - ballWidth/2); int drawY = (int) ((ballY - lastBallY) * interpolation + lastBallY - ballHeight/2); g.fillOval(drawX, drawY, ballWidth, ballHeight); lastDrawX = drawX; lastDrawY = drawY; g.setColor(Color.BLACK); g.drawString("FPS: " + fps, 5, 10); frameCount++; } } } |