Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Game Loop with GageTimer  (Read 1418 times)
0 Members and 1 Guest are viewing this topic.
Offline mikelomax

Senior Newbie




I know exactly what i'm doing... asking for help


« Posted 2005-02-03 10:00:09 »

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.

The reason for having: A firewall

Independance day, the aliens were destroyed because a computer was used to access their network and disable key systems. If they had had a firewall, the earth would be another worthless rock just floating in space
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #1 - Posted 2005-02-03 11:14:59 »

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.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline mikelomax

Senior Newbie




I know exactly what i'm doing... asking for help


« Reply #2 - Posted 2005-02-03 11:49:27 »

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.

The reason for having: A firewall

Independance day, the aliens were destroyed because a computer was used to access their network and disable key systems. If they had had a firewall, the earth would be another worthless rock just floating in space
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #3 - Posted 2005-02-03 12:19:05 »

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?

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #4 - Posted 2005-02-03 12:32:32 »

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

Java Game Console Project
Last Journal Entry: 12/17/04
Offline mikelomax

Senior Newbie




I know exactly what i'm doing... asking for help


« Reply #5 - Posted 2005-02-03 15:18:56 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
 
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.

The reason for having: A firewall

Independance day, the aliens were destroyed because a computer was used to access their network and disable key systems. If they had had a firewall, the earth would be another worthless rock just floating in space
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #6 - Posted 2005-02-03 15:34:13 »

This seems backwards:

1  
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:

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


However, I think you actually want:

1  
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.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline mikelomax

Senior Newbie




I know exactly what i'm doing... asking for help


« Reply #7 - Posted 2005-02-03 17:08:52 »

Ok, i've fixed by using the
1  
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

The reason for having: A firewall

Independance day, the aliens were destroyed because a computer was used to access their network and disable key systems. If they had had a firewall, the earth would be another worthless rock just floating in space
Offline mikelomax

Senior Newbie




I know exactly what i'm doing... asking for help


« Reply #8 - Posted 2005-02-03 17:33:45 »

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.

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  
    
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();
}

The reason for having: A firewall

Independance day, the aliens were destroyed because a computer was used to access their network and disable key systems. If they had had a firewall, the earth would be another worthless rock just floating in space
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #9 - Posted 2005-02-03 18:09:32 »

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.


Let's rework this a bit:

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  
    
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();
}

Java Game Console Project
Last Journal Entry: 12/17/04
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline mikelomax

Senior Newbie




I know exactly what i'm doing... asking for help


« Reply #10 - Posted 2005-02-03 18:24:59 »

Ah i see now, er i think...

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

The reason for having: A firewall

Independance day, the aliens were destroyed because a computer was used to access their network and disable key systems. If they had had a firewall, the earth would be another worthless rock just floating in space
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (11 views)
2014-04-15 03:46:01

UprightPath (24 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (108 views)
2014-04-08 23:06:04

tom_mai78101 (208 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!