CPU nearly 100%

Trying to address this issue of my game hogging the CPU, but not sure of what the correct method is:

should I be setting a target, for example 50 fps and doing Thread.sleep(time left over until next frame)?

Hello!

I Made like following ->


while ( gameonrun )
{
  Thread.Sleep(1);
  if ( MyVeryOwnTimer.getTime() > float_newtime )
  {
     float_newtime = MyVeryOwnTimer.getTime() + 1f / framerate;
     //gamecode to here.
  }
}

I believe i have the right way to go !!

//----

Thanks,

60fps is a better target then 50. Do you know how many frames it’s currently doing per second? You should find this out before altering your code!

For frame limiting I use code something like…


/**
 * This is a FrameLimiting class. Create an instance of it and then call
 * the limit method on each frame. It will automatically sleep during
 * this in order to try to keep the frame rate close to the rate given
 * in it's constructor.
 * 
 * @author Joseph Lenton - JosephLenton@StudioFortress.com
 */
public class FrameLimit
{
    // the number of milliseconds each frame should take
    private final long frameTime;

    // the timestamp for the last time the action method was called
    private long lastFrameTime;

    /**
     * @param frameRate A positive value stating the maximum frameRate, or 0 to run as fast as possible.
     */
    public FrameLimit(final int frameRate)
    {
        if (frameRate < 0) {
            throw new IllegalArgumentException("The frameRate must be a positive number or 0 to disable it, given: " + frameRate);
        }

        if (frameRate == 0) {
            this.frameTime = 0;
        } else {
            this.frameTime = Math.round(1000000000.0 / (double) frameRate);
        }
    }
    
    public void limit()
    {
        if (frameTime > 0) {
            /* sleep for the rest of the frame */
            long time = frameTime - (System.nanoTime() - lastFrameTime);
            if (time > 0) {
                // sleep properly
                try {
                    long threadWait = time / 1000000l;
                    if (threadWait > 1) {
                        Thread.sleep(threadWait);
                    }
                // or spin for the whole time
                } catch (InterruptedException e) {
                    // the time now minus how long the frame has left to wait for
                    long spinEndTime = System.nanoTime() + (frameTime - (System.nanoTime() - lastFrameTime));
                    while (spinEndTime > System.nanoTime()) {
                        // spin! (do nothing)
                    }
                }
            }

            lastFrameTime = System.nanoTime();
        }
    }
}

Create an instance giving it an expected frame rate. Call it’s limit method on each frame and it should (hopefully) sleep enough to keep the frame rate of your game close to the rate you set it to.

You should also think about profiling your game. It might be that it’s just doing something very expensive on each frame.