Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (416)
games submitted by our members
Games in WIP (306)
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 design - your considerations  (Read 575 times)
0 Members and 1 Guest are viewing this topic.
Offline SHC

Senior Member


Medals: 9



« Posted 2012-12-02 13:29:00 »

I had been using this run method as game loop.

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  
public void run(){
    // Initialize the resources
   Map.initMap();
    initResources();
    // Game loop initialization
   int SKIP_STEPS = 1000/Global.STEPS_FOR_SECOND;
    // The time
   long gameTime = getCurrentTime();
    int loops;
    // FPS counter
   int frames = 0;
    long lastFPSCount = getCurrentTime();
    // UPD counter
   int updates = 0;
    long lastUPDCount = getCurrentTime();
    // The current time
   long now;
    // Benchmarking
   long updStartTime = 0;
    long updEndTime = 0;
    long updTime = 0;
    while (running){
        now = getCurrentTime();
        loops = 0;
        updStartTime = getCurrentTime();
        while(now>gameTime && loops<Global.MAX_FRAMESKIP){
            updateGame(SKIP_STEPS);
            // calculate update count
           updates++;
            if (now - lastUPDCount > 1000){
                lastUPDCount = getCurrentTime();
                Global.ACTUAL_STEPS_FOR_SECOND = updates;
                Global.UPDATE_RATE = (int)((float)((float)Global.ACTUAL_STEPS_FOR_SECOND/(float)Global.STEPS_FOR_SECOND)*100);
                updates = 0;
            }
            gameTime += SKIP_STEPS;
            loops++;
        }
        updEndTime = getCurrentTime();
        updTime = updEndTime - updStartTime;
        System.out.println(updTime);
        interpolation = (now + SKIP_STEPS - gameTime)/SKIP_STEPS;
        displayGame();
        // FPS counter
       frames++;
        if (now - lastFPSCount > 1000) {
            lastFPSCount = now;
            Global.FRAMES_PER_SECOND = frames;
            frames = 0;
        }
    }
}


But the output of the bench marking is

1  
2  
3  
4  
5  
6  
7  
8  
9  
59
8
2
2
1
1
2
1
0


I couldn't understand why 0's appear as elapsed time for updates. Also the STEPS_PER_SECOND is 50. Thanks.

Offline ReBirth
« Reply #1 - Posted 2012-12-03 02:18:03 »

If the updateGame(SKIP_STEPS) doesnt do anything, it's normal right?

Offline SHC

Senior Member


Medals: 9



« Reply #2 - Posted 2012-12-03 16:38:45 »

That method isn't empty, it moves the objects and checks collisions. The objects are nearly 100

Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline ReBirth
« Reply #3 - Posted 2012-12-04 11:38:43 »

After narrowing down your code
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
    while (running){
        now = getCurrentTime();
        loops = 0;
        updStartTime = getCurrentTime();
        while(now>gameTime && loops<Global.MAX_FRAMESKIP){
            updateGame(SKIP_STEPS);
            gameTime += SKIP_STEPS;
            loops++;
        }
        updEndTime = getCurrentTime();
        updTime = updEndTime - updStartTime;
        System.out.println(updTime);
    }

I suspect the 2nd while loop is not executed on later. The 'loops<Global.MAX_FRAMESKIP' looks pretty "stable". Try to debug the 'now' and 'gameTime'.

Offline SHC

Senior Member


Medals: 9



« Reply #4 - Posted 2012-12-04 16:55:52 »

I tried printing them out every loop. Sometimes they're becoming equal. This other question I've asked helped me.

http://gamedev.stackexchange.com/questions/42010/passing-elapsed-time-to-the-update-function-from-the-game-loop/42018#42018

Thanks for the help

Offline ReBirth
« Reply #5 - Posted 2012-12-05 02:38:30 »

Quote
First, you should avoid calling getCurrentTime() multiple times per loop. You will potentially be getting different values at different times.
I just know about this, but yes it seems true Grin

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
BrassApparatus (7 views)
2013-06-19 08:52:37

NegativeZero (11 views)
2013-06-19 03:31:52

NegativeZero (14 views)
2013-06-19 03:24:09

Jesse_Attard (19 views)
2013-06-18 22:03:02

HeroesGraveDev (60 views)
2013-06-15 23:35:23

Vermeer (59 views)
2013-06-14 20:08:06

davedes (59 views)
2013-06-14 16:03:55

alaslipknot (53 views)
2013-06-13 07:56:31

Roquen (74 views)
2013-06-12 04:12:32

alaslipknot (58 views)
2013-06-10 19:30:18
Smoothing Algorithm Question
by UprightPath
2013-05-28 02:58:26

Smoothing Algorithm Question
by UprightPath
2013-05-28 02:57:33

Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38
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!