Game Loop with GageTimer

I’ve recently applied the use of GageTimer in my game loop but now it runs slightly incorrectly with my code when it used to use System.currentTimeMilllis();

I already know about the need for sleepUntil() and all that, but i’m looking for the correct method of getting a millisecond value to update all game objects that will apply no matter what value the AdvancedTimer.getTicksPerSecond() is.

http://www.gamelizard.com/JavaGameProgrammingPresentation.htm

On that page I have an example of how to set up a run loop using the GAGE Timer. I’m not really sure if that’s what you’re asking. If it’s not, please rephrase your question.

Not quite, whilst i can see how it can work, i can’t see where to get the millisecond difference between each cycle of the loop to update the graphics. Whilst you have showGraphics() there is no value being passed and so although it does help a little, it doesn’t quite do what i need it to.

at.sleepUntil(tick + ticksPerFrame);

That sleeps.

at.getClockTicks() - (tick + ticksPerFrame) is how long it will sleep in ticks. Divide by AdvancedTimer.getTicksPerSecond(), and you have the number of seconds that it will be sleeping.

How long it took the work would be ticksPerFrame - that number. Is that what you want?

long milliseconds = at.getClockTicks() * 1000 / at.getTicksPerSecond();

Ok I’ll try to explain things a little clearer by showing the code to you and detailing the exact problem.

The problem has started because i had not put Timer.dll in the right place and as a result the AdvancedTimer came out with about 66 ticks per second which was fine for the below code.

 
private void loop() {
        AdvancedTimer timer = new AdvancedTimer();
        long sleepTime = timer.getTicksPerSecond() / 60; //60 fps
        System.out.println(timer.getTicksPerSecond());
        long ticks = 0;
        timer.start();
        while(engineRunning) {
                long time = 1000 / timer.getTicksPerSecond();
                kb.update(time);
                sound.render();
                update(time);
                Graphics g = screen.getGraphics();
                draw(g);
                g.dispose();
                screen.show();
                timer.sleepUntil(ticks + sleepTime);
                ticks += sleepTime;
        }
        timer.stop();
}

The idea being that the millisecond value it takes to cycle is used to move/animate/process the information.

Now with Timer.dll i get 3579545 ticks per second and so the millisecond value i get is now 0 mainly due to the conversion process, but even fixed it is still too low a value to use as a millisecond value. So, as far as i can see it, i can change my code to take in nano or microsecond timings or find an equation that converts it and determines the best way of making it run at 60fps easily.

This seems backwards:

long time = 1000 / timer.getTicksPerSecond();

It would tend to give a zero value because ticks per second is larger than 1000. Ticks in a millisecond can be obtained with:

//Gets the number of ticks in a millisecond
long time = timer.getTicksPerSecond() / 1000;

However, I think you actually want:

long time = 1000 / 60;

Which will give you the number of milliseconds in a frame. That will work as long as you’re certain that your time won’t ever drift. The alternative is to use the milliseconds conversion I posted above, then figure out the difference between lastMillis and currentMillis.

Ok, i’ve fixed by using the

long milliseconds = at.getClockTicks() * 1000 / at.getTicksPerSecond();

and working out the difference between the two.

The only problem i had was that the sleeptime being determined was already passed by the timer’s ticks so i’ve used the none drag from http://www.gamelizard.com/JavaGameProgrammingPresentation.htm for the moment as then it’s works fine. Seems ok for the moment, things need a little alteration here and there but i’ll let you know if i need anymore help. Thanks

Ok, it works and there is an interval of a few millisecs but the trouble is that sometimes it is too small (i.e. 0). Either the sleepUntil value is too short or i’m getting the time at the wrong moment. Here’s the code.

    
private void loop() {
        AdvancedTimer timer = new AdvancedTimer();
        System.out.println(timer.getTicksPerSecond());
        long ticks = 0;
        long start = 0;
        long time = 0;
        long current = 0;
        timer.start();
        while(engineRunning) {
                long sleepTime = timer.getTicksPerSecond() / 60; //60 fps
                start = timer.getClockTicks() * 1000 / timer.getTicksPerSecond();
                time = start - current;
                System.out.println("start: "+start);
                System.out.println("last: "+current);
                System.out.println("elapsed: "+time);
                kb.update(time);
                sound.render();
                update(time);
                Graphics g = screen.getGraphics();
                draw(g);
                g.dispose();
                screen.show();
                current = timer.getClockTicks() * 1000 / timer.getTicksPerSecond();
               System.out.println("ticks: "+timer.getClockTicks());
               System.out.println("sleep till: "+(ticks + sleepTime));
               timer.sleepUntil(ticks + sleepTime);
               ticks = timer.getClockTicks();
        }
        timer.stop();
}

[quote]Ok, it works and there is an interval of a few millisecs but the trouble is that sometimes it is too small (i.e. 0). Either the sleepUntil value is too short or i’m getting the time at the wrong moment. Here’s the code.
[/quote]
Let’s rework this a bit:

    
private void loop() {
        AdvancedTimer timer = new AdvancedTimer();
        System.out.println(timer.getTicksPerSecond());
        long ticks = 0;
        long last = 0;
        long elapsed = 0;
        long time = 0;

        long sleepTime = timer.getTicksPerSecond() / 60; //60 fps

        timer.start();

        while(engineRunning) {
                
                time = elapsed * 1000 / timer.getTicksPerSecond(); //Changed
                System.out.println("last: "+ time); //Changed
                System.out.println("elapsed: "+elapsed); //Changed
                kb.update(time);
                sound.render();
                update(time);
                Graphics g = screen.getGraphics();
                draw(g);
                g.dispose();
                screen.show();
                System.out.println("ticks: "+timer.getClockTicks());
                System.out.println("sleep till: "+(ticks + sleepTime));
                timer.sleepUntil(ticks + sleepTime);
                last = ticks; //Changed
                ticks += sleepTime; //Changed
                elapsed = ticks - last; //changed
        }
        timer.stop();
}

Ah i see now, er i think…

Thanks for the help, this was starting to give me a major headache.