1
\$\begingroup\$

I'm making an RPG-like 2D game from scratch with Java.

By now I'm in a pretty advanced stage with my game, but I encountered a problem- my game using too much CPU.

I know it's because my game loop doesn't use Thread.sleep. I looked for a solution on the internet but most of the answers were "you should move to LWJGL or LIBGDX". I also didn't find any good advice for using Thread.sleep in my game loop.

So, should I move my platform to some of those libraries, or could someone give me some advice how to deal with my CPU usage problem?

Here is my game loop:

public void run(){
    int fps = 60;
    double timePerTick = 1000000000 / fps;
    double delta = 0;
    long now;
    long lastTime = System.nanoTime();

    while (running){
        now = System.nanoTime();
        delta += (now - lastTime) / timePerTick;
        lastTime = now;

        if (delta >= 1) {
            tick();
            render();
            delta--;
        }
    }

    stop();
}

I will just mention that I don't think that moving to one of those libraries will make things easier in the future, and that's because I'm in a pretty advanced stage in my game.

\$\endgroup\$
  • \$\begingroup\$ 100% CPU usage is not always a problem. It's expected behaviour in a standard busy-wait loop, even one that otherwise does very little work, and a standard busy-wait loop is exactly what you have. Moving to a different library won't change that. If it bothers you so much just sleep for 1ms if there's no work to do in the current iteration. \$\endgroup\$ – Maximus Minimus Oct 3 '17 at 20:39
  • 1
    \$\begingroup\$ Libraries aren't magic. There is nothing they can do which you couldn't do on your own when you feel like reinventing the wheel. So when you are already in a pretty advanced development stage, I would recommend you to just find out how the libraries are doing it and do the same. \$\endgroup\$ – Philipp Oct 3 '17 at 21:13
  • \$\begingroup\$ You should probably use two game loops regardless, one for rendering and one for logic. They can run at different speeds depending on your needs. \$\endgroup\$ – Weckar E. Oct 5 '17 at 18:21
  • 1
    \$\begingroup\$ if you want to stick to native functionality, you could look to javafx to monitor the game loop for you. You may be losing a lot of power just by checking whether you should iterate, rather than just iterating. \$\endgroup\$ – Weckar E. Oct 5 '17 at 18:24
1
\$\begingroup\$

There is no reason to switch to a library, unless you want to cut out a particular step from your development process.

In your particular case, I perceive that you may be updating your game logic and render everything too often, which makes your loop inefficient.

Try a game loop with fixed update time step, variable rendering, which is agreed to provide the smoothest possible experience to a player. In that kind of loop, you'd update your game logic a fixed amount of times a second, and render it every time you have an opportunity to (particularly, when the computer has updated all the game logic and is idle).

enter image description here

While this will be more efficient in terms of CPU usage, it may be more of a challenge to implement as certain issues (such as the need for extrapolation) will have to be addressed.

Example code:

double previous = getCurrentTime();
double lag = 0.0;
while (true)
{
  double current = getCurrentTime();
  double elapsed = current - previous;
  previous = current;
  lag += elapsed;

  processInput();

  while (lag >= MS_PER_UPDATE)
  {
    update();
    lag -= MS_PER_UPDATE;
  }

  render();
}

Check out Game Loop chapter of Game Programming Patterns book by Robert Nystrom for some good examples and detailed explanations. That chapter was also used as a source was this answer.

\$\endgroup\$
  • \$\begingroup\$ It might work if he has physics in the same loop as game logic. It depends on his implementation. Of course, he should not compute AI logic every frame and all the other complex stuff. But if he has problems with rendering pipeline it might not help. Though, +1 because rendering depends on GPU but OP has problems with CPU, so it's probably some algorithmic problem on OP side. \$\endgroup\$ – Candid Moon _Max_ Oct 5 '17 at 23:02
  • \$\begingroup\$ Though it looks like OP is concerned about too much % of CPU usage rather than speed of the game. So that other processes don't work correctly if the CPU is unused at the moment. \$\endgroup\$ – Candid Moon _Max_ Oct 6 '17 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.