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 (406)
games submitted by our members
Games in WIP (293)
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 558 times)
0 Members and 1 Guest are viewing this topic.
Offline SHC

Senior Member


Medals: 8



« 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: 8



« 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!
Legends of Yore - The Casual Retro Roguelike
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: 8



« 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  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks 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!
hobbles (6 views)
2013-05-22 00:54:55

cubemaster21 (74 views)
2013-05-17 21:29:12

alaslipknot (86 views)
2013-05-16 21:24:48

gouessej (115 views)
2013-05-16 00:53:38

gouessej (110 views)
2013-05-16 00:17:58

theagentd (121 views)
2013-05-15 15:01:13

theagentd (110 views)
2013-05-15 15:00:54

StreetDoggy (155 views)
2013-05-14 15:56:26

kutucuk (177 views)
2013-05-12 17:10:36

kutucuk (174 views)
2013-05-12 15:36:09
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

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.085 seconds with 20 queries.